freebsd-almost-working-client-20020216
[openafs.git] / src / fsint / afsaux.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #ifdef KERNEL
12 #include "../afs/param.h"
13 #else
14 #include <afs/param.h>
15 #endif
16
17 RCSID("$Header$");
18
19 #ifdef KERNEL
20 #if defined(UKERNEL)
21 #include "../afs/sysincludes.h"
22 #include "../afs/afsincludes.h"
23 #include "../rx/xdr.h"
24 #else /* defined(UKERNEL) */
25 #if defined(AFS_ALPHA_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
26 #include "../afs/sysincludes.h"
27 #include "../afs/afsincludes.h"
28 #else
29 #include "../h/types.h"
30 #include "../rpc/types.h"
31 #include "../rpc/xdr.h"
32 #endif
33 #if !defined(AFS_ALPHA_ENV)
34 #ifndef XDR_GETINT32
35 #define XDR_GETINT32    XDR_GETLONG
36 #endif
37 #ifndef XDR_PUTINT32
38 #define XDR_PUTINT32    XDR_PUTLONG
39 #endif
40 #endif
41 #ifndef AFS_LINUX22_ENV
42 #include "../rpc/auth.h"
43 #endif
44 #endif /* defined(UKERNEL) */
45 #include "../afsint/afsint.h"
46 #else /* KERNEL */
47 # include <rx/xdr.h>
48 # include "afsint.h"
49 #endif /* KERNEL */
50
51 #define MAXBS   2048            /* try to avoid horrible allocs */
52 static afs_int32 bslosers = 0;
53
54 #ifdef KERNEL
55 #define NVALLOC(a)      osi_Alloc(a)
56 #define NVFREE(a,b)     osi_Free(a,b)
57 #else /* KERNEL */
58 #define NVALLOC(a)      malloc(a)
59 #define NVFREE(a,b)     free(a)
60 #endif /* KERNEL */
61
62 /* these things are defined in R (but not RX's) library.  For now, we add them
63     only for the kernel system.  Later, when R is expunged, we'll remove the ifdef */
64 #ifdef KERNEL
65 #ifndef AFS_USR_DARWIN_ENV
66 #ifdef  AFS_AIXNFS11
67 #define AUTH_DES 1
68 #endif
69 #if (defined(AFS_AIX_ENV) && !defined(AUTH_DES)) || (!defined(AFS_SUN_ENV)) && !defined(AFS_SGI_ENV) && !defined(AFS_ALPHA_ENV) && !defined(AFS_SUN5_ENV)
70 #ifndef AFS_AIX32_ENV
71 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_FBSD_ENV)
72 /*
73  * XDR chars; from user mode xdr package.
74  */
75 bool_t
76 xdr_char(xdrs, sp)
77         register XDR *xdrs;
78         char *sp;
79 {
80         afs_int32 l;
81
82         switch (xdrs->x_op) {
83
84         case XDR_ENCODE:
85                 l = (afs_int32) *sp;
86                 return (XDR_PUTINT32(xdrs, &l));
87
88         case XDR_DECODE:
89                 if (!XDR_GETINT32(xdrs, &l)) {
90                         return (FALSE);
91                 }
92                 *sp = (char) l;
93                 return (TRUE);
94
95         case XDR_FREE:
96                 return (TRUE);
97         }
98         return (FALSE);
99 }
100 #endif /* AFS_HPUX110_ENV && AFS_LINUX20_ENV */
101 #endif 
102 #endif /* defined(AFS_AIX_ENV) && !defined(AUTH_DES) */
103
104
105 /* 
106  * Wrapper for xdr_string that can be called directly from 
107  * routines like clnt_call; from user-mode xdr package.
108  */
109 #ifndef AFS_SUN5_ENV
110 #ifndef AFS_HPUX110_ENV
111 bool_t
112 xdr_wrapstring(xdrs, cpp)
113         XDR *xdrs;
114         char **cpp;
115 {
116         if (xdr_string(xdrs, cpp, 1024)) {
117                 return(TRUE);
118         }
119         return(FALSE);
120 }
121 #endif /* AFS_HPUX110_ENV */
122 #endif /* AFS_SUN5_ENV */
123
124 /*
125  * xdr_vector():
126  *
127  * XDR a fixed length array. Unlike variable-length arrays,
128  * the storage of fixed length arrays is static and unfreeable.
129  * > basep: base of the array
130  * > size: size of the array
131  * > elemsize: size of each element
132  * > xdr_elem: routine to XDR each element
133  */
134 bool_t
135 xdr_vector(xdrs, basep, nelem, elemsize, xdr_elem)
136         register XDR *xdrs;
137         register char *basep;
138         register u_int nelem;
139         register u_int elemsize;
140         register xdrproc_t xdr_elem;    
141 {
142         register u_int i;
143         register char *elptr;
144
145         elptr = basep;
146         for (i = 0; i < nelem; i++) {
147                 if (! (*xdr_elem)(xdrs, elptr, (u_int) (~0))) {
148                         return(FALSE);
149                 }
150                 elptr += elemsize;
151         }
152         return(TRUE);   
153 }
154 #endif
155 #endif /* KERNEL */
156
157 #ifndef KERNEL
158 xdr_CBS(x, abbs)
159     XDR *x;
160     struct CBS *abbs; {
161     afs_int32 len;
162     if (x->x_op == XDR_FREE) {
163         NVFREE(abbs->SeqBody,abbs->SeqLen);
164         return TRUE;
165     }
166
167     if (x->x_op == XDR_ENCODE) {
168         xdr_afs_int32(x, &abbs->SeqLen);
169         xdr_opaque(x, abbs->SeqBody, abbs->SeqLen);
170         return TRUE;
171     }
172     else {
173         xdr_afs_int32(x, &len);
174         if (len < 0 || len > MAXBS) {bslosers++; return FALSE;}
175         if (!abbs->SeqBody) abbs->SeqBody = (char *) NVALLOC(len);
176         abbs->SeqLen = len;
177         xdr_opaque(x, abbs->SeqBody, len);
178         return TRUE;
179     }
180 }
181
182 xdr_BBS(x, abbs)
183     XDR *x;
184     struct BBS *abbs; {
185     afs_int32 maxLen, len;
186     if (x->x_op == XDR_FREE) {
187         NVFREE(abbs->SeqBody, abbs->MaxSeqLen);
188         return TRUE;
189     }
190
191     if (x->x_op == XDR_ENCODE) {
192         xdr_afs_int32(x, &abbs->MaxSeqLen);
193         xdr_afs_int32(x, &abbs->SeqLen);
194         xdr_opaque(x, abbs->SeqBody, abbs->SeqLen);
195         return TRUE;
196     }
197     else {
198         xdr_afs_int32(x, &maxLen);
199         xdr_afs_int32(x, &len);
200         if (len < 0 || len > MAXBS || len > maxLen) {bslosers++; return FALSE;}
201         if (!abbs->SeqBody) abbs->SeqBody = (char *) NVALLOC(maxLen);
202         abbs->MaxSeqLen = maxLen;
203         abbs->SeqLen = len;
204         xdr_opaque(x, abbs->SeqBody, len);
205         return TRUE;
206     }
207 }
208
209 xdr_AFSAccessList(x, abbs)
210     XDR *x;
211     struct BBS *abbs; {
212     afs_int32 maxLen, len;
213     if (x->x_op == XDR_FREE) {
214         NVFREE(abbs->SeqBody, abbs->MaxSeqLen);
215         return TRUE;
216     }
217
218     if (x->x_op == XDR_ENCODE) {
219         xdr_afs_int32(x, &abbs->MaxSeqLen);
220         xdr_afs_int32(x, &abbs->SeqLen);
221         xdr_opaque(x, abbs->SeqBody, abbs->SeqLen);
222         return TRUE;
223     }
224     else {
225         xdr_afs_int32(x, &maxLen);
226         xdr_afs_int32(x, &len);
227         if (len < 0 || len > MAXBS || len > maxLen) {bslosers++; return FALSE;}
228         if (!abbs->SeqBody) abbs->SeqBody = (char *) NVALLOC(maxLen);
229         abbs->MaxSeqLen = maxLen;
230         abbs->SeqLen = len;
231         xdr_opaque(x, abbs->SeqBody, len);
232         return TRUE;
233     }
234 }
235 #endif /* KERNEL */