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