Use xdr_alloc and xdr_free within ptuser
[openafs.git] / src / rx / xdr.c
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  * 
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  * 
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  * 
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  * 
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  * 
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  */
29
30 #include <afsconfig.h>
31 #ifdef KERNEL
32 #include "afs/param.h"
33 #else
34 #include <afs/param.h>
35 #include <string.h>
36 #endif
37
38
39 /*
40  * xdr.c, Generic XDR routines implementation.
41  *
42  * Copyright (C) 1984, Sun Microsystems, Inc.
43  *
44  * These are the "generic" xdr routines used to serialize and de-serialize
45  * most common data items.  See xdr.h for more info on the interface to
46  * xdr.
47  */
48
49 #ifndef NeXT
50
51 #if defined(KERNEL) && !defined(UKERNEL)
52 #include <sys/param.h>
53 #ifndef AFS_LINUX20_ENV
54 #include <sys/systm.h>
55 #endif
56 #else
57 #include <stdio.h>
58 #endif
59 #include "xdr.h"
60 #include "rx.h"
61
62 /*
63  * constants specific to the xdr "protocol"
64  */
65 #define XDR_FALSE       ((afs_int32) 0)
66 #define XDR_TRUE        ((afs_int32) 1)
67 #define LASTUNSIGNED    ((u_int) 0-1)
68
69 /*
70  * for unit alignment
71  */
72
73
74 /*
75  * XDR nothing
76  */
77 bool_t
78 xdr_void(void)
79 {
80     return (TRUE);
81 }
82
83 /*
84  * XDR integers
85  */
86 bool_t
87 xdr_int(XDR * xdrs, int *ip)
88 {
89     afs_int32 l;
90
91     switch (xdrs->x_op) {
92
93     case XDR_ENCODE:
94         l = (afs_int32) * ip;
95         return (XDR_PUTINT32(xdrs, &l));
96
97     case XDR_DECODE:
98         if (!XDR_GETINT32(xdrs, &l)) {
99             return (FALSE);
100         }
101         *ip = (int)l;
102         return (TRUE);
103
104     case XDR_FREE:
105         return (TRUE);
106     }
107     return (FALSE);
108 }
109
110 /*
111  * XDR unsigned integers
112  */
113 bool_t
114 xdr_u_int(XDR * xdrs, u_int * uip)
115 {
116     afs_uint32 l;
117
118     switch (xdrs->x_op) {
119
120     case XDR_ENCODE:
121         l = (afs_uint32) * uip;
122         return (XDR_PUTINT32(xdrs, (afs_int32 *) &l));
123
124     case XDR_DECODE:
125         if (!XDR_GETINT32(xdrs, (afs_int32 *) &l)) {
126             return (FALSE);
127         }
128         *uip = (u_int) l;
129         return (TRUE);
130
131     case XDR_FREE:
132         return (TRUE);
133     }
134     return (FALSE);
135 }
136
137
138 /*
139  * XDR long integers
140  */
141 bool_t
142 xdr_long(XDR * xdrs, long *lp)
143 {
144     afs_int32 l;
145
146     switch (xdrs->x_op) {
147
148     case XDR_ENCODE:
149         l = (afs_int32) * lp;
150         return (XDR_PUTINT32(xdrs, &l));
151
152     case XDR_DECODE:
153         if (!XDR_GETINT32(xdrs, &l)) {
154             return (FALSE);
155         }
156         *lp = (long)l;
157         return (TRUE);
158
159     case XDR_FREE:
160         return (TRUE);
161     }
162     return (FALSE);
163 }
164
165 /*
166  * XDR unsigned long integers
167  */
168 bool_t
169 xdr_u_long(XDR * xdrs, u_long * ulp)
170 {
171     afs_uint32 l;
172
173     switch (xdrs->x_op) {
174
175     case XDR_ENCODE:
176         l = (afs_uint32) * ulp;
177         return (XDR_PUTINT32(xdrs, (afs_int32 *)&l));
178
179     case XDR_DECODE:
180         if (!XDR_GETINT32(xdrs, (afs_int32 *)&l)) {
181             return (FALSE);
182         }
183         *ulp = (u_long) l;
184         return (TRUE);
185
186     case XDR_FREE:
187         return (TRUE);
188     }
189     return (FALSE);
190 }
191
192
193 /*
194  * XDR chars
195  */
196 bool_t
197 xdr_char(XDR * xdrs, char *sp)
198 {
199     afs_int32 l;
200
201     switch (xdrs->x_op) {
202
203     case XDR_ENCODE:
204         l = (afs_int32) * sp;
205         return (XDR_PUTINT32(xdrs, &l));
206
207     case XDR_DECODE:
208         if (!XDR_GETINT32(xdrs, &l)) {
209             return (FALSE);
210         }
211         *sp = (char)l;
212         return (TRUE);
213
214     case XDR_FREE:
215         return (TRUE);
216     }
217     return (FALSE);
218 }
219
220 /*
221  * XDR unsigned chars
222  */
223 bool_t
224 xdr_u_char(XDR * xdrs, u_char * usp)
225 {
226     afs_uint32 l;
227
228     switch (xdrs->x_op) {
229
230     case XDR_ENCODE:
231         l = (afs_uint32) * usp;
232         return (XDR_PUTINT32(xdrs, (afs_int32 *)&l));
233
234     case XDR_DECODE:
235         if (!XDR_GETINT32(xdrs, (afs_int32 *)&l)) {
236             return (FALSE);
237         }
238         *usp = (u_char) l;
239         return (TRUE);
240
241     case XDR_FREE:
242         return (TRUE);
243     }
244     return (FALSE);
245 }
246
247
248 /*
249  * XDR short integers
250  */
251 bool_t
252 xdr_short(XDR * xdrs, short *sp)
253 {
254     afs_int32 l;
255
256     switch (xdrs->x_op) {
257
258     case XDR_ENCODE:
259         l = (afs_int32) * sp;
260         return (XDR_PUTINT32(xdrs, &l));
261
262     case XDR_DECODE:
263         if (!XDR_GETINT32(xdrs, &l)) {
264             return (FALSE);
265         }
266         *sp = (short)l;
267         return (TRUE);
268
269     case XDR_FREE:
270         return (TRUE);
271     }
272     return (FALSE);
273 }
274
275 /*
276  * XDR unsigned short integers
277  */
278 bool_t
279 xdr_u_short(XDR * xdrs, u_short * usp)
280 {
281     afs_uint32 l;
282
283     switch (xdrs->x_op) {
284
285     case XDR_ENCODE:
286         l = (afs_uint32) * usp;
287         return (XDR_PUTINT32(xdrs, (afs_int32 *)&l));
288
289     case XDR_DECODE:
290         if (!XDR_GETINT32(xdrs, (afs_int32 *)&l)) {
291             return (FALSE);
292         }
293         *usp = (u_short) l;
294         return (TRUE);
295
296     case XDR_FREE:
297         return (TRUE);
298     }
299     return (FALSE);
300 }
301
302
303 /*
304  * XDR booleans
305  */
306 bool_t
307 xdr_bool(XDR * xdrs, bool_t * bp)
308 {
309     afs_int32 lb;
310
311     switch (xdrs->x_op) {
312
313     case XDR_ENCODE:
314         lb = *bp ? XDR_TRUE : XDR_FALSE;
315         return (XDR_PUTINT32(xdrs, &lb));
316
317     case XDR_DECODE:
318         if (!XDR_GETINT32(xdrs, &lb)) {
319             return (FALSE);
320         }
321         *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
322         return (TRUE);
323
324     case XDR_FREE:
325         return (TRUE);
326     }
327     return (FALSE);
328 }
329
330 /*
331  * XDR enumerations
332  */
333 bool_t
334 xdr_enum(XDR * xdrs, enum_t * ep)
335 {
336     enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
337
338     /*
339      * enums are treated as ints
340      */
341
342     return (xdr_long(xdrs, (long *)ep));
343
344 }
345
346 /*
347  * XDR opaque data
348  * Allows the specification of a fixed size sequence of opaque bytes.
349  * cp points to the opaque object and cnt gives the byte length.
350  */
351 bool_t
352 xdr_opaque(XDR * xdrs, caddr_t cp, u_int cnt)
353 {
354     u_int rndup;
355     int crud[BYTES_PER_XDR_UNIT];
356     char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
357
358     /*
359      * if no data we are done
360      */
361     if (cnt == 0)
362         return (TRUE);
363
364     /*
365      * round byte count to full xdr units
366      */
367     rndup = cnt % BYTES_PER_XDR_UNIT;
368     if (rndup > 0)
369         rndup = BYTES_PER_XDR_UNIT - rndup;
370
371     if (xdrs->x_op == XDR_DECODE) {
372         if (!XDR_GETBYTES(xdrs, cp, cnt)) {
373             return (FALSE);
374         }
375         if (rndup == 0)
376             return (TRUE);
377         return (XDR_GETBYTES(xdrs, (caddr_t)crud, rndup));
378     }
379
380     if (xdrs->x_op == XDR_ENCODE) {
381         if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
382             return (FALSE);
383         }
384         if (rndup == 0)
385             return (TRUE);
386         return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
387     }
388
389     if (xdrs->x_op == XDR_FREE) {
390         return (TRUE);
391     }
392
393     return (FALSE);
394 }
395
396 /*
397  * XDR counted bytes
398  * *cpp is a pointer to the bytes, *sizep is the count.
399  * If *cpp is NULL maxsize bytes are allocated
400  */
401 bool_t
402 xdr_bytes(XDR * xdrs, char **cpp, u_int * sizep,
403           u_int maxsize)
404 {
405     char *sp = *cpp;    /* sp is the actual string pointer */
406     u_int nodesize;
407
408     /*
409      * first deal with the length since xdr bytes are counted
410      */
411     if (!xdr_u_int(xdrs, sizep)) {
412         return (FALSE);
413     }
414     nodesize = *sizep;
415     if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
416         return (FALSE);
417     }
418
419     /*
420      * now deal with the actual bytes
421      */
422     switch (xdrs->x_op) {
423
424     case XDR_DECODE:
425         if (sp == NULL) {
426             *cpp = sp = (char *)osi_alloc(nodesize);
427         }
428         if (sp == NULL) {
429             return (FALSE);
430         }
431         /* fall into ... */
432
433     case XDR_ENCODE:
434         return (xdr_opaque(xdrs, sp, nodesize));
435
436     case XDR_FREE:
437         if (sp != NULL) {
438             osi_free(sp, nodesize);
439             *cpp = NULL;
440         }
441         return (TRUE);
442     }
443     return (FALSE);
444 }
445
446 /*
447  * XDR a descriminated union
448  * Support routine for discriminated unions.
449  * You create an array of xdrdiscrim structures, terminated with
450  * an entry with a null procedure pointer.  The routine gets
451  * the discriminant value and then searches the array of xdrdiscrims
452  * looking for that value.  It calls the procedure given in the xdrdiscrim
453  * to handle the discriminant.  If there is no specific routine a default
454  * routine may be called.
455  * If there is no specific or default routine an error is returned.
456  */
457 /*
458         enum_t *dscmp;          * enum to decide which arm to work on *
459         caddr_t unp;            * the union itself *
460         struct xdr_discrim *choices;    * [value, xdr proc] for each arm *
461         xdrproc_t dfault;       * default xdr routine *
462 */
463 bool_t
464 xdr_union(XDR * xdrs, enum_t * dscmp, caddr_t unp,
465           struct xdr_discrim * choices, xdrproc_t dfault)
466 {
467     enum_t dscm;
468
469     /*
470      * we deal with the discriminator;  it's an enum
471      */
472     if (!xdr_enum(xdrs, dscmp)) {
473         return (FALSE);
474     }
475     dscm = *dscmp;
476
477     /*
478      * search choices for a value that matches the discriminator.
479      * if we find one, execute the xdr routine for that value.
480      */
481     for (; choices->proc != NULL_xdrproc_t; choices++) {
482         if (choices->value == dscm)
483             return ((*(choices->proc)) (xdrs, unp, LASTUNSIGNED));
484     }
485
486     /*
487      * no match - execute the default xdr routine if there is one
488      */
489     return ((dfault == NULL_xdrproc_t) ? FALSE : (*dfault) (xdrs, unp,
490                                                             LASTUNSIGNED));
491 }
492
493
494 /*
495  * Non-portable xdr primitives.
496  * Care should be taken when moving these routines to new architectures.
497  */
498
499
500 /*
501  * XDR null terminated ASCII strings
502  * xdr_string deals with "C strings" - arrays of bytes that are
503  * terminated by a NULL character.  The parameter cpp references a
504  * pointer to storage; If the pointer is null, then the necessary
505  * storage is allocated.  The last parameter is the max allowed length
506  * of the string as specified by a protocol.
507  */
508 bool_t
509 xdr_string(XDR * xdrs, char **cpp, u_int maxsize)
510 {
511     char *sp = *cpp;    /* sp is the actual string pointer */
512     u_int size;
513     u_int nodesize;
514
515     /* FIXME: this does not look correct: MSVC 6 computes -2 here */
516     if (maxsize > ((~0) >> 1) - 1)
517         maxsize = ((~0) >> 1) - 1;
518
519     /*
520      * first deal with the length since xdr strings are counted-strings
521      */
522     switch (xdrs->x_op) {
523     case XDR_FREE:
524         if (sp == NULL) {
525             return (TRUE);      /* already free */
526         }
527         /* Fall through */
528     case XDR_ENCODE:
529         size = strlen(sp);
530         break;
531     case XDR_DECODE:
532         break;
533     }
534
535     if (!xdr_u_int(xdrs, &size)) {
536         return (FALSE);
537     }
538     if (size > maxsize) {
539         return (FALSE);
540     }
541     nodesize = size + 1;
542
543     /*
544      * now deal with the actual bytes
545      */
546     switch (xdrs->x_op) {
547
548     case XDR_DECODE:
549         if (sp == NULL)
550             *cpp = sp = (char *)osi_alloc(nodesize);
551         if (sp == NULL) {
552             return (FALSE);
553         }
554         sp[size] = 0;
555         /* fall into ... */
556
557     case XDR_ENCODE:
558         return (xdr_opaque(xdrs, sp, size));
559
560     case XDR_FREE:
561         if (sp != NULL) {
562             osi_free(sp, nodesize);
563             *cpp = NULL;
564         }
565         return (TRUE);
566     }
567     return (FALSE);
568 }
569
570 /* 
571  * Wrapper for xdr_string that can be called directly from 
572  * routines like clnt_call
573  */
574 #ifndef KERNEL
575 bool_t
576 xdr_wrapstring(XDR * xdrs, char **cpp)
577 {
578     if (xdr_string(xdrs, cpp, BUFSIZ)) {
579         return (TRUE);
580     }
581     return (FALSE);
582 }
583 #endif
584
585 void *
586 xdr_alloc(afs_int32 size)
587 {
588     return osi_alloc(size);
589 }
590
591 void
592 xdr_free(void *x, afs_int32 size)
593 {
594     osi_free(x, size);
595 }
596 #endif /* NeXT */