FreeBSD/Linux Kernel Cross Reference
sys/xdr/xdr.c
1 /* $NetBSD: xdr.c,v 1.22 2000/07/06 03:10:35 christos Exp $ */
2
3 /*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char *sccsid2 = "@(#)xdr.c 1.35 87/08/12";
34 static char *sccsid = "@(#)xdr.c 2.1 88/07/29 4.0 RPCSRC";
35 #endif
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 /*
40 * xdr.c, Generic XDR routines implementation.
41 *
42 * Copyright (C) 1986, 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 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53
54 #include <rpc/rpc.h>
55 #include <rpc/rpc_com.h>
56 #include <rpc/types.h>
57 #include <rpc/xdr.h>
58
59 typedef quad_t longlong_t; /* ANSI long long type */
60 typedef u_quad_t u_longlong_t; /* ANSI unsigned long long type */
61
62 /*
63 * constants specific to the xdr "protocol"
64 */
65 #define XDR_FALSE ((long) 0)
66 #define XDR_TRUE ((long) 1)
67
68 /*
69 * for unit alignment
70 */
71 static const char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
72
73 /*
74 * Free a data structure using XDR
75 * Not a filter, but a convenient utility nonetheless
76 */
77 void
78 xdr_free(xdrproc_t proc, void *objp)
79 {
80 XDR x;
81
82 x.x_op = XDR_FREE;
83 (*proc)(&x, objp);
84 }
85
86 /*
87 * XDR nothing
88 */
89 bool_t
90 xdr_void(void)
91 {
92
93 return (TRUE);
94 }
95
96
97 /*
98 * XDR integers
99 */
100 bool_t
101 xdr_int(XDR *xdrs, int *ip)
102 {
103 long l;
104
105 switch (xdrs->x_op) {
106
107 case XDR_ENCODE:
108 l = (long) *ip;
109 return (XDR_PUTLONG(xdrs, &l));
110
111 case XDR_DECODE:
112 if (!XDR_GETLONG(xdrs, &l)) {
113 return (FALSE);
114 }
115 *ip = (int) l;
116 return (TRUE);
117
118 case XDR_FREE:
119 return (TRUE);
120 }
121 /* NOTREACHED */
122 return (FALSE);
123 }
124
125 /*
126 * XDR unsigned integers
127 */
128 bool_t
129 xdr_u_int(XDR *xdrs, u_int *up)
130 {
131 u_long l;
132
133 switch (xdrs->x_op) {
134
135 case XDR_ENCODE:
136 l = (u_long) *up;
137 return (XDR_PUTLONG(xdrs, (long *)&l));
138
139 case XDR_DECODE:
140 if (!XDR_GETLONG(xdrs, (long *)&l)) {
141 return (FALSE);
142 }
143 *up = (u_int) l;
144 return (TRUE);
145
146 case XDR_FREE:
147 return (TRUE);
148 }
149 /* NOTREACHED */
150 return (FALSE);
151 }
152
153
154 /*
155 * XDR long integers
156 * same as xdr_u_long - open coded to save a proc call!
157 */
158 bool_t
159 xdr_long(XDR *xdrs, long *lp)
160 {
161 switch (xdrs->x_op) {
162 case XDR_ENCODE:
163 return (XDR_PUTLONG(xdrs, lp));
164 case XDR_DECODE:
165 return (XDR_GETLONG(xdrs, lp));
166 case XDR_FREE:
167 return (TRUE);
168 }
169 /* NOTREACHED */
170 return (FALSE);
171 }
172
173 /*
174 * XDR unsigned long integers
175 * same as xdr_long - open coded to save a proc call!
176 */
177 bool_t
178 xdr_u_long(XDR *xdrs, u_long *ulp)
179 {
180 switch (xdrs->x_op) {
181 case XDR_ENCODE:
182 return (XDR_PUTLONG(xdrs, (long *)ulp));
183 case XDR_DECODE:
184 return (XDR_GETLONG(xdrs, (long *)ulp));
185 case XDR_FREE:
186 return (TRUE);
187 }
188 /* NOTREACHED */
189 return (FALSE);
190 }
191
192
193 /*
194 * XDR 32-bit integers
195 * same as xdr_uint32_t - open coded to save a proc call!
196 */
197 bool_t
198 xdr_int32_t(XDR *xdrs, int32_t *int32_p)
199 {
200 long l;
201
202 switch (xdrs->x_op) {
203
204 case XDR_ENCODE:
205 l = (long) *int32_p;
206 return (XDR_PUTLONG(xdrs, &l));
207
208 case XDR_DECODE:
209 if (!XDR_GETLONG(xdrs, &l)) {
210 return (FALSE);
211 }
212 *int32_p = (int32_t) l;
213 return (TRUE);
214
215 case XDR_FREE:
216 return (TRUE);
217 }
218 /* NOTREACHED */
219 return (FALSE);
220 }
221
222 /*
223 * XDR unsigned 32-bit integers
224 * same as xdr_int32_t - open coded to save a proc call!
225 */
226 bool_t
227 xdr_uint32_t(XDR *xdrs, uint32_t *uint32_p)
228 {
229 u_long l;
230
231 switch (xdrs->x_op) {
232
233 case XDR_ENCODE:
234 l = (u_long) *uint32_p;
235 return (XDR_PUTLONG(xdrs, (long *)&l));
236
237 case XDR_DECODE:
238 if (!XDR_GETLONG(xdrs, (long *)&l)) {
239 return (FALSE);
240 }
241 *uint32_p = (uint32_t) l;
242 return (TRUE);
243
244 case XDR_FREE:
245 return (TRUE);
246 }
247 /* NOTREACHED */
248 return (FALSE);
249 }
250
251
252 /*
253 * XDR short integers
254 */
255 bool_t
256 xdr_short(XDR *xdrs, short *sp)
257 {
258 long l;
259
260 switch (xdrs->x_op) {
261
262 case XDR_ENCODE:
263 l = (long) *sp;
264 return (XDR_PUTLONG(xdrs, &l));
265
266 case XDR_DECODE:
267 if (!XDR_GETLONG(xdrs, &l)) {
268 return (FALSE);
269 }
270 *sp = (short) l;
271 return (TRUE);
272
273 case XDR_FREE:
274 return (TRUE);
275 }
276 /* NOTREACHED */
277 return (FALSE);
278 }
279
280 /*
281 * XDR unsigned short integers
282 */
283 bool_t
284 xdr_u_short(XDR *xdrs, u_short *usp)
285 {
286 u_long l;
287
288 switch (xdrs->x_op) {
289
290 case XDR_ENCODE:
291 l = (u_long) *usp;
292 return (XDR_PUTLONG(xdrs, (long *)&l));
293
294 case XDR_DECODE:
295 if (!XDR_GETLONG(xdrs, (long *)&l)) {
296 return (FALSE);
297 }
298 *usp = (u_short) l;
299 return (TRUE);
300
301 case XDR_FREE:
302 return (TRUE);
303 }
304 /* NOTREACHED */
305 return (FALSE);
306 }
307
308
309 /*
310 * XDR 16-bit integers
311 */
312 bool_t
313 xdr_int16_t(XDR *xdrs, int16_t *int16_p)
314 {
315 long l;
316
317 switch (xdrs->x_op) {
318
319 case XDR_ENCODE:
320 l = (long) *int16_p;
321 return (XDR_PUTLONG(xdrs, &l));
322
323 case XDR_DECODE:
324 if (!XDR_GETLONG(xdrs, &l)) {
325 return (FALSE);
326 }
327 *int16_p = (int16_t) l;
328 return (TRUE);
329
330 case XDR_FREE:
331 return (TRUE);
332 }
333 /* NOTREACHED */
334 return (FALSE);
335 }
336
337 /*
338 * XDR unsigned 16-bit integers
339 */
340 bool_t
341 xdr_uint16_t(XDR *xdrs, uint16_t *uint16_p)
342 {
343 u_long l;
344
345 switch (xdrs->x_op) {
346
347 case XDR_ENCODE:
348 l = (u_long) *uint16_p;
349 return (XDR_PUTLONG(xdrs, (long *)&l));
350
351 case XDR_DECODE:
352 if (!XDR_GETLONG(xdrs, (long *)&l)) {
353 return (FALSE);
354 }
355 *uint16_p = (uint16_t) l;
356 return (TRUE);
357
358 case XDR_FREE:
359 return (TRUE);
360 }
361 /* NOTREACHED */
362 return (FALSE);
363 }
364
365
366 /*
367 * XDR a char
368 */
369 bool_t
370 xdr_char(XDR *xdrs, char *cp)
371 {
372 int i;
373
374 i = (*cp);
375 if (!xdr_int(xdrs, &i)) {
376 return (FALSE);
377 }
378 *cp = i;
379 return (TRUE);
380 }
381
382 /*
383 * XDR an unsigned char
384 */
385 bool_t
386 xdr_u_char(XDR *xdrs, u_char *cp)
387 {
388 u_int u;
389
390 u = (*cp);
391 if (!xdr_u_int(xdrs, &u)) {
392 return (FALSE);
393 }
394 *cp = u;
395 return (TRUE);
396 }
397
398 /*
399 * XDR booleans
400 */
401 bool_t
402 xdr_bool(XDR *xdrs, bool_t *bp)
403 {
404 long lb;
405
406 switch (xdrs->x_op) {
407
408 case XDR_ENCODE:
409 lb = *bp ? XDR_TRUE : XDR_FALSE;
410 return (XDR_PUTLONG(xdrs, &lb));
411
412 case XDR_DECODE:
413 if (!XDR_GETLONG(xdrs, &lb)) {
414 return (FALSE);
415 }
416 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
417 return (TRUE);
418
419 case XDR_FREE:
420 return (TRUE);
421 }
422 /* NOTREACHED */
423 return (FALSE);
424 }
425
426 /*
427 * XDR enumerations
428 */
429 bool_t
430 xdr_enum(XDR *xdrs, enum_t *ep)
431 {
432 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
433
434 /*
435 * enums are treated as ints
436 */
437 /* LINTED */ if (sizeof (enum sizecheck) == sizeof (long)) {
438 return (xdr_long(xdrs, (long *)(void *)ep));
439 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) {
440 return (xdr_int(xdrs, (int *)(void *)ep));
441 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (short)) {
442 return (xdr_short(xdrs, (short *)(void *)ep));
443 } else {
444 return (FALSE);
445 }
446 }
447
448 /*
449 * XDR opaque data
450 * Allows the specification of a fixed size sequence of opaque bytes.
451 * cp points to the opaque object and cnt gives the byte length.
452 */
453 bool_t
454 xdr_opaque(XDR *xdrs, caddr_t cp, u_int cnt)
455 {
456 u_int rndup;
457 static int crud[BYTES_PER_XDR_UNIT];
458
459 /*
460 * if no data we are done
461 */
462 if (cnt == 0)
463 return (TRUE);
464
465 /*
466 * round byte count to full xdr units
467 */
468 rndup = cnt % BYTES_PER_XDR_UNIT;
469 if (rndup > 0)
470 rndup = BYTES_PER_XDR_UNIT - rndup;
471
472 if (xdrs->x_op == XDR_DECODE) {
473 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
474 return (FALSE);
475 }
476 if (rndup == 0)
477 return (TRUE);
478 return (XDR_GETBYTES(xdrs, (caddr_t)(void *)crud, rndup));
479 }
480
481 if (xdrs->x_op == XDR_ENCODE) {
482 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
483 return (FALSE);
484 }
485 if (rndup == 0)
486 return (TRUE);
487 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
488 }
489
490 if (xdrs->x_op == XDR_FREE) {
491 return (TRUE);
492 }
493
494 return (FALSE);
495 }
496
497 /*
498 * XDR counted bytes
499 * *cpp is a pointer to the bytes, *sizep is the count.
500 * If *cpp is NULL maxsize bytes are allocated
501 */
502 bool_t
503 xdr_bytes(XDR *xdrs, char **cpp, u_int *sizep, u_int maxsize)
504 {
505 char *sp = *cpp; /* sp is the actual string pointer */
506 u_int nodesize;
507 bool_t ret, allocated = FALSE;
508
509 /*
510 * first deal with the length since xdr bytes are counted
511 */
512 if (! xdr_u_int(xdrs, sizep)) {
513 return (FALSE);
514 }
515 nodesize = *sizep;
516 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
517 return (FALSE);
518 }
519
520 /*
521 * now deal with the actual bytes
522 */
523 switch (xdrs->x_op) {
524
525 case XDR_DECODE:
526 if (nodesize == 0) {
527 return (TRUE);
528 }
529 if (sp == NULL) {
530 *cpp = sp = mem_alloc(nodesize);
531 allocated = TRUE;
532 }
533 if (sp == NULL) {
534 printf("xdr_bytes: out of memory");
535 return (FALSE);
536 }
537 /* FALLTHROUGH */
538
539 case XDR_ENCODE:
540 ret = xdr_opaque(xdrs, sp, nodesize);
541 if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
542 if (allocated == TRUE) {
543 mem_free(sp, nodesize);
544 *cpp = NULL;
545 }
546 }
547 return (ret);
548
549 case XDR_FREE:
550 if (sp != NULL) {
551 mem_free(sp, nodesize);
552 *cpp = NULL;
553 }
554 return (TRUE);
555 }
556 /* NOTREACHED */
557 return (FALSE);
558 }
559
560 /*
561 * Implemented here due to commonality of the object.
562 */
563 bool_t
564 xdr_netobj(XDR *xdrs, struct netobj *np)
565 {
566
567 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
568 }
569
570 /*
571 * XDR a descriminated union
572 * Support routine for discriminated unions.
573 * You create an array of xdrdiscrim structures, terminated with
574 * an entry with a null procedure pointer. The routine gets
575 * the discriminant value and then searches the array of xdrdiscrims
576 * looking for that value. It calls the procedure given in the xdrdiscrim
577 * to handle the discriminant. If there is no specific routine a default
578 * routine may be called.
579 * If there is no specific or default routine an error is returned.
580 */
581 bool_t
582 xdr_union(XDR *xdrs,
583 enum_t *dscmp, /* enum to decide which arm to work on */
584 char *unp, /* the union itself */
585 const struct xdr_discrim *choices, /* [value, xdr proc] for each arm */
586 xdrproc_t dfault) /* default xdr routine */
587 {
588 enum_t dscm;
589
590 /*
591 * we deal with the discriminator; it's an enum
592 */
593 if (! xdr_enum(xdrs, dscmp)) {
594 return (FALSE);
595 }
596 dscm = *dscmp;
597
598 /*
599 * search choices for a value that matches the discriminator.
600 * if we find one, execute the xdr routine for that value.
601 */
602 for (; choices->proc != NULL_xdrproc_t; choices++) {
603 if (choices->value == dscm)
604 return ((*(choices->proc))(xdrs, unp));
605 }
606
607 /*
608 * no match - execute the default xdr routine if there is one
609 */
610 return ((dfault == NULL_xdrproc_t) ? FALSE :
611 (*dfault)(xdrs, unp));
612 }
613
614
615 /*
616 * Non-portable xdr primitives.
617 * Care should be taken when moving these routines to new architectures.
618 */
619
620
621 /*
622 * XDR null terminated ASCII strings
623 * xdr_string deals with "C strings" - arrays of bytes that are
624 * terminated by a NULL character. The parameter cpp references a
625 * pointer to storage; If the pointer is null, then the necessary
626 * storage is allocated. The last parameter is the max allowed length
627 * of the string as specified by a protocol.
628 */
629 bool_t
630 xdr_string(XDR *xdrs, char **cpp, u_int maxsize)
631 {
632 char *sp = *cpp; /* sp is the actual string pointer */
633 u_int size;
634 u_int nodesize;
635 bool_t ret, allocated = FALSE;
636
637 /*
638 * first deal with the length since xdr strings are counted-strings
639 */
640 switch (xdrs->x_op) {
641 case XDR_FREE:
642 if (sp == NULL) {
643 return(TRUE); /* already free */
644 }
645 /* FALLTHROUGH */
646 case XDR_ENCODE:
647 size = strlen(sp);
648 break;
649 case XDR_DECODE:
650 break;
651 }
652 if (! xdr_u_int(xdrs, &size)) {
653 return (FALSE);
654 }
655 if (size > maxsize) {
656 return (FALSE);
657 }
658 nodesize = size + 1;
659
660 /*
661 * now deal with the actual bytes
662 */
663 switch (xdrs->x_op) {
664
665 case XDR_DECODE:
666 if (nodesize == 0) {
667 return (TRUE);
668 }
669 if (sp == NULL) {
670 *cpp = sp = mem_alloc(nodesize);
671 allocated = TRUE;
672 }
673 if (sp == NULL) {
674 printf("xdr_string: out of memory");
675 return (FALSE);
676 }
677 sp[size] = 0;
678 /* FALLTHROUGH */
679
680 case XDR_ENCODE:
681 ret = xdr_opaque(xdrs, sp, size);
682 if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
683 if (allocated == TRUE) {
684 mem_free(sp, nodesize);
685 *cpp = NULL;
686 }
687 }
688 return (ret);
689
690 case XDR_FREE:
691 mem_free(sp, nodesize);
692 *cpp = NULL;
693 return (TRUE);
694 }
695 /* NOTREACHED */
696 return (FALSE);
697 }
698
699 /*
700 * Wrapper for xdr_string that can be called directly from
701 * routines like clnt_call
702 */
703 bool_t
704 xdr_wrapstring(XDR *xdrs, char **cpp)
705 {
706 return xdr_string(xdrs, cpp, RPC_MAXDATASIZE);
707 }
708
709 /*
710 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
711 * are in the "non-portable" section because they require that a `long long'
712 * be a 64-bit type.
713 *
714 * --thorpej@netbsd.org, November 30, 1999
715 */
716
717 /*
718 * XDR 64-bit integers
719 */
720 bool_t
721 xdr_int64_t(XDR *xdrs, int64_t *llp)
722 {
723 u_long ul[2];
724
725 switch (xdrs->x_op) {
726 case XDR_ENCODE:
727 ul[0] = (u_long)((uint64_t)*llp >> 32) & 0xffffffff;
728 ul[1] = (u_long)((uint64_t)*llp) & 0xffffffff;
729 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
730 return (FALSE);
731 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
732 case XDR_DECODE:
733 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
734 return (FALSE);
735 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
736 return (FALSE);
737 *llp = (int64_t)
738 (((uint64_t)ul[0] << 32) | ((uint64_t)ul[1]));
739 return (TRUE);
740 case XDR_FREE:
741 return (TRUE);
742 }
743 /* NOTREACHED */
744 return (FALSE);
745 }
746
747
748 /*
749 * XDR unsigned 64-bit integers
750 */
751 bool_t
752 xdr_uint64_t(XDR *xdrs, uint64_t *ullp)
753 {
754 u_long ul[2];
755
756 switch (xdrs->x_op) {
757 case XDR_ENCODE:
758 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
759 ul[1] = (u_long)(*ullp) & 0xffffffff;
760 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
761 return (FALSE);
762 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
763 case XDR_DECODE:
764 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
765 return (FALSE);
766 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
767 return (FALSE);
768 *ullp = (uint64_t)
769 (((uint64_t)ul[0] << 32) | ((uint64_t)ul[1]));
770 return (TRUE);
771 case XDR_FREE:
772 return (TRUE);
773 }
774 /* NOTREACHED */
775 return (FALSE);
776 }
777
778
779 /*
780 * XDR hypers
781 */
782 bool_t
783 xdr_hyper(XDR *xdrs, longlong_t *llp)
784 {
785
786 /*
787 * Don't bother open-coding this; it's a fair amount of code. Just
788 * call xdr_int64_t().
789 */
790 return (xdr_int64_t(xdrs, (int64_t *)llp));
791 }
792
793
794 /*
795 * XDR unsigned hypers
796 */
797 bool_t
798 xdr_u_hyper(XDR *xdrs, u_longlong_t *ullp)
799 {
800
801 /*
802 * Don't bother open-coding this; it's a fair amount of code. Just
803 * call xdr_uint64_t().
804 */
805 return (xdr_uint64_t(xdrs, (uint64_t *)ullp));
806 }
807
808
809 /*
810 * XDR longlong_t's
811 */
812 bool_t
813 xdr_longlong_t(XDR *xdrs, longlong_t *llp)
814 {
815
816 /*
817 * Don't bother open-coding this; it's a fair amount of code. Just
818 * call xdr_int64_t().
819 */
820 return (xdr_int64_t(xdrs, (int64_t *)llp));
821 }
822
823
824 /*
825 * XDR u_longlong_t's
826 */
827 bool_t
828 xdr_u_longlong_t(XDR *xdrs, u_longlong_t *ullp)
829 {
830
831 /*
832 * Don't bother open-coding this; it's a fair amount of code. Just
833 * call xdr_uint64_t().
834 */
835 return (xdr_uint64_t(xdrs, (uint64_t *)ullp));
836 }
Cache object: d5a28db87f686037baaa76e5a10c3e54
|