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