1 /* $NetBSD: xdr.c,v 1.28 2006/05/14 02:15:31 christos Exp $ */
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.
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.
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.
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.
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.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
35 static char *sccsid
= "@(#)xdr.c 1.35 87/08/12";
36 static char *sccsid
= "@(#)xdr.c 2.1 88/07/29 4.0 RPCSRC";
38 __RCSID("$NetBSD: xdr.c,v 1.28 2006/05/14 02:15:31 christos Exp $");
43 * xdr.c, Generic XDR routines implementation.
45 * Copyright (C) 1986, Sun Microsystems, Inc.
47 * These are the "generic" xdr routines used to serialize and de-serialize
48 * most common data items. See xdr.h for more info on the interface to
52 #include "namespace.h"
60 #include <rpc/types.h>
64 __weak_alias(xdr_bool
,_xdr_bool
)
65 __weak_alias(xdr_bytes
,_xdr_bytes
)
66 __weak_alias(xdr_char
,_xdr_char
)
67 __weak_alias(xdr_enum
,_xdr_enum
)
68 __weak_alias(xdr_free
,_xdr_free
)
69 __weak_alias(xdr_hyper
,_xdr_hyper
)
70 __weak_alias(xdr_int
,_xdr_int
)
71 __weak_alias(xdr_int16_t
,_xdr_int16_t
)
72 __weak_alias(xdr_int32_t
,_xdr_int32_t
)
73 __weak_alias(xdr_int64_t
,_xdr_int64_t
)
74 __weak_alias(xdr_long
,_xdr_long
)
75 __weak_alias(xdr_longlong_t
,_xdr_longlong_t
)
76 __weak_alias(xdr_netobj
,_xdr_netobj
)
77 __weak_alias(xdr_opaque
,_xdr_opaque
)
78 __weak_alias(xdr_short
,_xdr_short
)
79 __weak_alias(xdr_string
,_xdr_string
)
80 __weak_alias(xdr_u_char
,_xdr_u_char
)
81 __weak_alias(xdr_u_hyper
,_xdr_u_hyper
)
82 __weak_alias(xdr_u_int
,_xdr_u_int
)
83 __weak_alias(xdr_u_int16_t
,_xdr_u_int16_t
)
84 __weak_alias(xdr_u_int32_t
,_xdr_u_int32_t
)
85 __weak_alias(xdr_u_int64_t
,_xdr_u_int64_t
)
86 __weak_alias(xdr_u_long
,_xdr_u_long
)
87 __weak_alias(xdr_u_longlong_t
,_xdr_u_longlong_t
)
88 __weak_alias(xdr_u_short
,_xdr_u_short
)
89 __weak_alias(xdr_union
,_xdr_union
)
90 __weak_alias(xdr_void
,_xdr_void
)
91 __weak_alias(xdr_wrapstring
,_xdr_wrapstring
)
95 * constants specific to the xdr "protocol"
97 #define XDR_FALSE ((long) 0)
98 #define XDR_TRUE ((long) 1)
99 #define LASTUNSIGNED ((u_int) 0-1)
104 static const char xdr_zero
[BYTES_PER_XDR_UNIT
] = { 0, 0, 0, 0 };
107 * Free a data structure using XDR
108 * Not a filter, but a convenient utility nonetheless
125 xdr_void(/* xdrs, addr */)
144 _DIAGASSERT(xdrs
!= NULL
);
145 _DIAGASSERT(ip
!= NULL
);
147 switch (xdrs
->x_op
) {
151 return (XDR_PUTLONG(xdrs
, &l
));
154 if (!XDR_GETLONG(xdrs
, &l
)) {
168 * XDR unsigned integers
177 _DIAGASSERT(xdrs
!= NULL
);
178 _DIAGASSERT(up
!= NULL
);
180 switch (xdrs
->x_op
) {
184 return (XDR_PUTLONG(xdrs
, (long *)&l
));
187 if (!XDR_GETLONG(xdrs
, (long *)&l
)) {
203 * same as xdr_u_long - open coded to save a proc call!
211 _DIAGASSERT(xdrs
!= NULL
);
212 _DIAGASSERT(lp
!= NULL
);
214 switch (xdrs
->x_op
) {
216 return (XDR_PUTLONG(xdrs
, lp
));
218 return (XDR_GETLONG(xdrs
, lp
));
227 * XDR unsigned long integers
228 * same as xdr_long - open coded to save a proc call!
231 xdr_u_long(xdrs
, ulp
)
236 _DIAGASSERT(xdrs
!= NULL
);
237 _DIAGASSERT(ulp
!= NULL
);
239 switch (xdrs
->x_op
) {
241 return (XDR_PUTLONG(xdrs
, (long *)ulp
));
243 return (XDR_GETLONG(xdrs
, (long *)ulp
));
253 * XDR 32-bit integers
254 * same as xdr_u_int32_t - open coded to save a proc call!
257 xdr_int32_t(xdrs
, int32_p
)
263 _DIAGASSERT(xdrs
!= NULL
);
264 _DIAGASSERT(int32_p
!= NULL
);
266 switch (xdrs
->x_op
) {
270 return (XDR_PUTLONG(xdrs
, &l
));
273 if (!XDR_GETLONG(xdrs
, &l
)) {
276 *int32_p
= (int32_t) l
;
287 * XDR unsigned 32-bit integers
288 * same as xdr_int32_t - open coded to save a proc call!
291 xdr_u_int32_t(xdrs
, u_int32_p
)
293 u_int32_t
*u_int32_p
;
297 _DIAGASSERT(xdrs
!= NULL
);
298 _DIAGASSERT(u_int32_p
!= NULL
);
300 switch (xdrs
->x_op
) {
303 l
= (u_long
) *u_int32_p
;
304 return (XDR_PUTLONG(xdrs
, (long *)&l
));
307 if (!XDR_GETLONG(xdrs
, (long *)&l
)) {
310 *u_int32_p
= (u_int32_t
) l
;
331 _DIAGASSERT(xdrs
!= NULL
);
332 _DIAGASSERT(sp
!= NULL
);
334 switch (xdrs
->x_op
) {
338 return (XDR_PUTLONG(xdrs
, &l
));
341 if (!XDR_GETLONG(xdrs
, &l
)) {
355 * XDR unsigned short integers
358 xdr_u_short(xdrs
, usp
)
364 _DIAGASSERT(xdrs
!= NULL
);
365 _DIAGASSERT(usp
!= NULL
);
367 switch (xdrs
->x_op
) {
371 return (XDR_PUTLONG(xdrs
, (long *)&l
));
374 if (!XDR_GETLONG(xdrs
, (long *)&l
)) {
389 * XDR 16-bit integers
392 xdr_int16_t(xdrs
, int16_p
)
398 _DIAGASSERT(xdrs
!= NULL
);
399 _DIAGASSERT(int16_p
!= NULL
);
401 switch (xdrs
->x_op
) {
405 return (XDR_PUTLONG(xdrs
, &l
));
408 if (!XDR_GETLONG(xdrs
, &l
)) {
411 *int16_p
= (int16_t) l
;
422 * XDR unsigned 16-bit integers
425 xdr_u_int16_t(xdrs
, u_int16_p
)
427 u_int16_t
*u_int16_p
;
431 _DIAGASSERT(xdrs
!= NULL
);
432 _DIAGASSERT(u_int16_p
!= NULL
);
434 switch (xdrs
->x_op
) {
437 l
= (u_long
) *u_int16_p
;
438 return (XDR_PUTLONG(xdrs
, (long *)&l
));
441 if (!XDR_GETLONG(xdrs
, (long *)&l
)) {
444 *u_int16_p
= (u_int16_t
) l
;
465 _DIAGASSERT(xdrs
!= NULL
);
466 _DIAGASSERT(cp
!= NULL
);
469 if (!xdr_int(xdrs
, &i
)) {
477 * XDR an unsigned char
486 _DIAGASSERT(xdrs
!= NULL
);
487 _DIAGASSERT(cp
!= NULL
);
490 if (!xdr_u_int(xdrs
, &u
)) {
507 _DIAGASSERT(xdrs
!= NULL
);
508 _DIAGASSERT(bp
!= NULL
);
510 switch (xdrs
->x_op
) {
513 lb
= *bp
? XDR_TRUE
: XDR_FALSE
;
514 return (XDR_PUTLONG(xdrs
, &lb
));
517 if (!XDR_GETLONG(xdrs
, &lb
)) {
520 *bp
= (lb
== XDR_FALSE
) ? FALSE
: TRUE
;
540 _DIAGASSERT(xdrs
!= NULL
);
541 _DIAGASSERT(ep
!= NULL
);
543 switch (xdrs
->x_op
) {
547 return (XDR_PUTLONG(xdrs
, &l
));
550 if (!XDR_GETLONG(xdrs
, &l
)) {
565 * Allows the specification of a fixed size sequence of opaque bytes.
566 * cp points to the opaque object and cnt gives the byte length.
569 xdr_opaque(xdrs
, cp
, cnt
)
575 static int crud
[BYTES_PER_XDR_UNIT
];
577 _DIAGASSERT(xdrs
!= NULL
);
579 * if no data we are done
583 _DIAGASSERT(cp
!= NULL
);
586 * round byte count to full xdr units
588 rndup
= cnt
% BYTES_PER_XDR_UNIT
;
590 rndup
= BYTES_PER_XDR_UNIT
- rndup
;
592 if (xdrs
->x_op
== XDR_DECODE
) {
593 if (!XDR_GETBYTES(xdrs
, cp
, cnt
)) {
598 return (XDR_GETBYTES(xdrs
, (caddr_t
)(void *)crud
, rndup
));
601 if (xdrs
->x_op
== XDR_ENCODE
) {
602 if (!XDR_PUTBYTES(xdrs
, cp
, cnt
)) {
607 return (XDR_PUTBYTES(xdrs
, xdr_zero
, rndup
));
610 if (xdrs
->x_op
== XDR_FREE
) {
619 * *cpp is a pointer to the bytes, *sizep is the count.
620 * If *cpp is NULL maxsize bytes are allocated
623 xdr_bytes(xdrs
, cpp
, sizep
, maxsize
)
629 char *sp
; /* sp is the actual string pointer */
632 _DIAGASSERT(xdrs
!= NULL
);
633 _DIAGASSERT(cpp
!= NULL
);
634 _DIAGASSERT(sizep
!= NULL
);
639 * first deal with the length since xdr bytes are counted
641 if (! xdr_u_int(xdrs
, sizep
)) {
645 if ((nodesize
> maxsize
) && (xdrs
->x_op
!= XDR_FREE
)) {
650 * now deal with the actual bytes
652 switch (xdrs
->x_op
) {
659 *cpp
= sp
= mem_alloc(nodesize
);
662 warnx("xdr_bytes: out of memory");
668 return (xdr_opaque(xdrs
, sp
, nodesize
));
672 mem_free(sp
, nodesize
);
682 * Implemented here due to commonality of the object.
690 _DIAGASSERT(xdrs
!= NULL
);
691 _DIAGASSERT(np
!= NULL
);
693 return (xdr_bytes(xdrs
, &np
->n_bytes
, &np
->n_len
, MAX_NETOBJ_SZ
));
697 * XDR a descriminated union
698 * Support routine for discriminated unions.
699 * You create an array of xdrdiscrim structures, terminated with
700 * an entry with a null procedure pointer. The routine gets
701 * the discriminant value and then searches the array of xdrdiscrims
702 * looking for that value. It calls the procedure given in the xdrdiscrim
703 * to handle the discriminant. If there is no specific routine a default
704 * routine may be called.
705 * If there is no specific or default routine an error is returned.
708 xdr_union(xdrs
, dscmp
, unp
, choices
, dfault
)
710 enum_t
*dscmp
; /* enum to decide which arm to work on */
711 char *unp
; /* the union itself */
712 const struct xdr_discrim
*choices
; /* [value, xdr proc] for each arm */
713 xdrproc_t dfault
; /* default xdr routine */
717 _DIAGASSERT(xdrs
!= NULL
);
718 _DIAGASSERT(dscmp
!= NULL
);
719 _DIAGASSERT(unp
!= NULL
);
720 _DIAGASSERT(choices
!= NULL
);
721 /* dfault may be NULL */
724 * we deal with the discriminator; it's an enum
726 if (! xdr_enum(xdrs
, dscmp
)) {
732 * search choices for a value that matches the discriminator.
733 * if we find one, execute the xdr routine for that value.
735 for (; choices
->proc
!= NULL_xdrproc_t
; choices
++) {
736 if (choices
->value
== dscm
)
737 return ((*(choices
->proc
))(xdrs
, unp
));
741 * no match - execute the default xdr routine if there is one
743 return ((dfault
== NULL_xdrproc_t
) ? FALSE
:
744 (*dfault
)(xdrs
, unp
));
749 * Non-portable xdr primitives.
750 * Care should be taken when moving these routines to new architectures.
755 * XDR null terminated ASCII strings
756 * xdr_string deals with "C strings" - arrays of bytes that are
757 * terminated by a NULL character. The parameter cpp references a
758 * pointer to storage; If the pointer is null, then the necessary
759 * storage is allocated. The last parameter is the max allowed length
760 * of the string as specified by a protocol.
763 xdr_string(xdrs
, cpp
, maxsize
)
768 char *sp
; /* sp is the actual string pointer */
769 u_int size
= 0; /* XXX: GCC */
772 _DIAGASSERT(xdrs
!= NULL
);
773 _DIAGASSERT(cpp
!= NULL
);
778 * first deal with the length since xdr strings are counted-strings
780 switch (xdrs
->x_op
) {
783 return(TRUE
); /* already free */
792 if (! xdr_u_int(xdrs
, &size
)) {
795 if (size
> maxsize
) {
801 * now deal with the actual bytes
803 switch (xdrs
->x_op
) {
810 *cpp
= sp
= mem_alloc(nodesize
);
812 warnx("xdr_string: out of memory");
819 return (xdr_opaque(xdrs
, sp
, size
));
822 mem_free(sp
, nodesize
);
831 * Wrapper for xdr_string that can be called directly from
832 * routines like clnt_call
835 xdr_wrapstring(xdrs
, cpp
)
840 _DIAGASSERT(xdrs
!= NULL
);
841 _DIAGASSERT(cpp
!= NULL
);
843 return xdr_string(xdrs
, cpp
, LASTUNSIGNED
);
847 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
848 * are in the "non-portable" section because they require that a `long long'
851 * --thorpej@NetBSD.org, November 30, 1999
855 * XDR 64-bit integers
858 xdr_int64_t(xdrs
, llp
)
864 _DIAGASSERT(xdrs
!= NULL
);
865 _DIAGASSERT(llp
!= NULL
);
867 switch (xdrs
->x_op
) {
869 ul
[0] = (u_long
)((u_int64_t
)*llp
>> 32) & 0xffffffff;
870 ul
[1] = (u_long
)((u_int64_t
)*llp
) & 0xffffffff;
871 if (XDR_PUTLONG(xdrs
, (long *)&ul
[0]) == FALSE
)
873 return (XDR_PUTLONG(xdrs
, (long *)&ul
[1]));
875 if (XDR_GETLONG(xdrs
, (long *)&ul
[0]) == FALSE
)
877 if (XDR_GETLONG(xdrs
, (long *)&ul
[1]) == FALSE
)
880 (((u_int64_t
)ul
[0] << 32) | ((u_int64_t
)ul
[1]));
891 * XDR unsigned 64-bit integers
894 xdr_u_int64_t(xdrs
, ullp
)
900 _DIAGASSERT(xdrs
!= NULL
);
901 _DIAGASSERT(ullp
!= NULL
);
903 switch (xdrs
->x_op
) {
905 ul
[0] = (u_long
)(*ullp
>> 32) & 0xffffffff;
906 ul
[1] = (u_long
)(*ullp
) & 0xffffffff;
907 if (XDR_PUTLONG(xdrs
, (long *)&ul
[0]) == FALSE
)
909 return (XDR_PUTLONG(xdrs
, (long *)&ul
[1]));
911 if (XDR_GETLONG(xdrs
, (long *)&ul
[0]) == FALSE
)
913 if (XDR_GETLONG(xdrs
, (long *)&ul
[1]) == FALSE
)
916 (((u_int64_t
)ul
[0] << 32) | ((u_int64_t
)ul
[1]));
935 _DIAGASSERT(xdrs
!= NULL
);
936 _DIAGASSERT(llp
!= NULL
);
939 * Don't bother open-coding this; it's a fair amount of code. Just
940 * call xdr_int64_t().
942 return (xdr_int64_t(xdrs
, (int64_t *)llp
));
947 * XDR unsigned hypers
950 xdr_u_hyper(xdrs
, ullp
)
955 _DIAGASSERT(xdrs
!= NULL
);
956 _DIAGASSERT(ullp
!= NULL
);
959 * Don't bother open-coding this; it's a fair amount of code. Just
960 * call xdr_u_int64_t().
962 return (xdr_u_int64_t(xdrs
, (u_int64_t
*)ullp
));
970 xdr_longlong_t(xdrs
, llp
)
975 _DIAGASSERT(xdrs
!= NULL
);
976 _DIAGASSERT(llp
!= NULL
);
979 * Don't bother open-coding this; it's a fair amount of code. Just
980 * call xdr_int64_t().
982 return (xdr_int64_t(xdrs
, (int64_t *)llp
));
990 xdr_u_longlong_t(xdrs
, ullp
)
995 _DIAGASSERT(xdrs
!= NULL
);
996 _DIAGASSERT(ullp
!= NULL
);
999 * Don't bother open-coding this; it's a fair amount of code. Just
1000 * call xdr_u_int64_t().
1002 return (xdr_u_int64_t(xdrs
, (u_int64_t
*)ullp
));