pull-prototypes-to-head-20020821
[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 "../rx/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 #endif /* defined(UKERNEL) */
42 #include "../afsint/afsint.h"
43 #else /* KERNEL */
44 # include <rx/xdr.h>
45 # include "afsint.h"
46 #endif /* KERNEL */
47
48 #define MAXBS   2048            /* try to avoid horrible allocs */
49 static afs_int32 bslosers = 0;
50
51 #ifdef KERNEL
52 #define NVALLOC(a)      osi_Alloc(a)
53 #define NVFREE(a,b)     osi_Free(a,b)
54 #else /* KERNEL */
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_ALPHA_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_FBSD_ENV)
69 /*
70  * XDR chars; from user mode xdr package.
71  */
72 bool_t xdr_char(register XDR *xdrs, char *sp)
73 {
74         afs_int32 l;
75
76         switch (xdrs->x_op) {
77
78         case XDR_ENCODE:
79                 l = (afs_int32) *sp;
80                 return (XDR_PUTINT32(xdrs, &l));
81
82         case XDR_DECODE:
83                 if (!XDR_GETINT32(xdrs, &l)) {
84                         return (FALSE);
85                 }
86                 *sp = (char) l;
87                 return (TRUE);
88
89         case XDR_FREE:
90                 return (TRUE);
91         }
92         return (FALSE);
93 }
94 #endif /* AFS_HPUX110_ENV && AFS_LINUX20_ENV */
95 #endif 
96 #endif /* defined(AFS_AIX_ENV) && !defined(AUTH_DES) */
97
98
99 /* 
100  * Wrapper for xdr_string that can be called directly from 
101  * routines like clnt_call; from user-mode xdr package.
102  */
103 #ifndef AFS_SUN5_ENV
104 #ifndef AFS_HPUX110_ENV
105 bool_t xdr_wrapstring(XDR *xdrs, char **cpp)
106 {
107         if (xdr_string(xdrs, cpp, 1024)) {
108                 return(TRUE);
109         }
110         return(FALSE);
111 }
112 #endif /* AFS_HPUX110_ENV */
113 #endif /* AFS_SUN5_ENV */
114
115 /*
116  * xdr_vector():
117  *
118  * XDR a fixed length array. Unlike variable-length arrays,
119  * the storage of fixed length arrays is static and unfreeable.
120  * > basep: base of the array
121  * > size: size of the array
122  * > elemsize: size of each element
123  * > xdr_elem: routine to XDR each element
124  */
125 bool_t xdr_vector(register XDR *xdrs, register char *basep, 
126         register u_int nelem, register u_int elemsize, register xdrproc_t xdr_elem)
127 {
128         register u_int i;
129         register char *elptr;
130
131         elptr = basep;
132         for (i = 0; i < nelem; i++) {
133                 if (! (*xdr_elem)(xdrs, elptr, (u_int) (~0))) {
134                         return(FALSE);
135                 }
136                 elptr += elemsize;
137         }
138         return(TRUE);   
139 }
140 #endif
141 #endif /* KERNEL */
142
143 #ifndef KERNEL
144 bool_t xdr_CBS(XDR *x, struct CBS *abbs)
145 {
146     afs_int32 len;
147     if (x->x_op == XDR_FREE) {
148         NVFREE(abbs->SeqBody,abbs->SeqLen);
149         return TRUE;
150     }
151
152     if (x->x_op == XDR_ENCODE) {
153         xdr_afs_int32(x, &abbs->SeqLen);
154         xdr_opaque(x, abbs->SeqBody, abbs->SeqLen);
155         return TRUE;
156     }
157     else {
158         xdr_afs_int32(x, &len);
159         if (len < 0 || len > MAXBS) {bslosers++; return FALSE;}
160         if (!abbs->SeqBody) abbs->SeqBody = (char *) NVALLOC(len);
161         abbs->SeqLen = len;
162         xdr_opaque(x, abbs->SeqBody, len);
163         return TRUE;
164     }
165 }
166
167 bool_t xdr_BBS(XDR *x, struct BBS *abbs)
168 {
169     afs_int32 maxLen, len;
170     if (x->x_op == XDR_FREE) {
171         NVFREE(abbs->SeqBody, abbs->MaxSeqLen);
172         return TRUE;
173     }
174
175     if (x->x_op == XDR_ENCODE) {
176         xdr_afs_int32(x, &abbs->MaxSeqLen);
177         xdr_afs_int32(x, &abbs->SeqLen);
178         xdr_opaque(x, abbs->SeqBody, abbs->SeqLen);
179         return TRUE;
180     }
181     else {
182         xdr_afs_int32(x, &maxLen);
183         xdr_afs_int32(x, &len);
184         if (len < 0 || len > MAXBS || len > maxLen) {bslosers++; return FALSE;}
185         if (!abbs->SeqBody) abbs->SeqBody = (char *) NVALLOC(maxLen);
186         abbs->MaxSeqLen = maxLen;
187         abbs->SeqLen = len;
188         xdr_opaque(x, abbs->SeqBody, len);
189         return TRUE;
190     }
191 }
192
193 bool_t xdr_AFSAccessList(XDR *x, AFSAccessList *abbs)
194 {
195     afs_int32 maxLen, len;
196     if (x->x_op == XDR_FREE) {
197         NVFREE(abbs->SeqBody, abbs->MaxSeqLen);
198         return TRUE;
199     }
200
201     if (x->x_op == XDR_ENCODE) {
202         xdr_afs_int32(x, &abbs->MaxSeqLen);
203         xdr_afs_int32(x, &abbs->SeqLen);
204         xdr_opaque(x, abbs->SeqBody, abbs->SeqLen);
205         return TRUE;
206     }
207     else {
208         xdr_afs_int32(x, &maxLen);
209         xdr_afs_int32(x, &len);
210         if (len < 0 || len > MAXBS || len > maxLen) {bslosers++; return FALSE;}
211         if (!abbs->SeqBody) abbs->SeqBody = (char *) NVALLOC(maxLen);
212         abbs->MaxSeqLen = maxLen;
213         abbs->SeqLen = len;
214         xdr_opaque(x, abbs->SeqBody, len);
215         return TRUE;
216     }
217 }
218 #endif /* KERNEL */