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