check that link() doesn't cause statinfo to be corrupted/reverted for
[arla.git] / rxkad / rw.c
blobcac83b1cdb2acc98d4b285ffb92988462203264a
1 /*
2 * Copyright (c) 1995 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 /* Almost all programs use these routines (implicitly) so it's a good
35 * place to put the version string. */
37 #include "rxkad_locl.h"
39 RCSID("$Id$");
41 #ifndef HAVE_KRB4
42 #include "krb4.h"
44 int
45 krb4_get_int(void *f, u_int32_t *to, int size, int lsb)
47 int i;
48 unsigned char *from = (unsigned char *)f;
50 *to = 0;
51 if(lsb){
52 for(i = size-1; i >= 0; i--)
53 *to = (*to << 8) | from[i];
54 }else{
55 for(i = 0; i < size; i++)
56 *to = (*to << 8) | from[i];
58 return size;
61 int
62 krb4_put_int(u_int32_t from, void *to, size_t rem, int size)
64 int i;
65 unsigned char *p = (unsigned char *)to;
67 if (rem < size)
68 return -1;
70 for(i = size - 1; i >= 0; i--){
71 p[i] = from & 0xff;
72 from >>= 8;
74 return size;
78 /* addresses are always sent in network byte order */
80 int
81 krb4_get_address(void *from, u_int32_t *to)
83 unsigned char *p = (unsigned char*)from;
84 *to = htonl((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
85 return 4;
88 int
89 krb4_put_address(u_int32_t addr, void *to, size_t rem)
91 return krb4_put_int(ntohl(addr), to, rem, 4);
94 int
95 krb4_put_string(const char *from, void *to, size_t rem)
97 size_t len = strlen(from) + 1;
99 if (rem < len)
100 return -1;
101 memcpy(to, from, len);
102 return len;
106 krb4_get_string(void *from, char *to, size_t to_size)
108 strlcpy (to, (char *)from, to_size);
109 return strlen((char *)from) + 1;
113 krb4_get_nir(void *from,
114 char *name, size_t name_len,
115 char *instance, size_t instance_len,
116 char *realm, size_t realm_len)
118 char *p = (char *)from;
120 p += krb4_get_string(p, name, name_len);
121 p += krb4_get_string(p, instance, instance_len);
122 if(realm)
123 p += krb4_get_string(p, realm, realm_len);
124 return p - (char *)from;
128 krb4_put_nir(const char *name,
129 const char *instance,
130 const char *realm,
131 void *to,
132 size_t rem)
134 char *p = (char *)to;
135 int tmp;
137 tmp = krb4_put_string(name, p, rem);
138 if (tmp < 0)
139 return tmp;
140 p += tmp;
141 rem -= tmp;
143 tmp = krb4_put_string(instance, p, rem);
144 if (tmp < 0)
145 return tmp;
146 p += tmp;
147 rem -= tmp;
149 if (realm) {
150 tmp = krb4_put_string(realm, p, rem);
151 if (tmp < 0)
152 return tmp;
153 p += tmp;
154 rem -= tmp;
156 return p - (char *)to;
159 #endif