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