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