1 /* $NetBSD: xdr.c,v 1.22 2000/07/06 03:10:35 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 #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";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * xdr.c, Generic XDR routines implementation.
42 * Copyright (C) 1986, Sun Microsystems, Inc.
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
49 #include "namespace.h"
55 #include <rpc/types.h>
57 #include "un-namespace.h"
59 typedef quad_t longlong_t; /* ANSI long long type */
60 typedef u_quad_t u_longlong_t; /* ANSI unsigned long long type */
63 * constants specific to the xdr "protocol"
65 #define XDR_FALSE ((long) 0)
66 #define XDR_TRUE ((long) 1)
67 #define LASTUNSIGNED ((u_int) 0-1)
72 static const char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
75 * Free a data structure using XDR
76 * Not a filter, but a convenient utility nonetheless
110 switch (xdrs->x_op) {
114 return (XDR_PUTLONG(xdrs, &l));
117 if (!XDR_GETLONG(xdrs, &l)) {
131 * XDR unsigned integers
140 switch (xdrs->x_op) {
144 return (XDR_PUTLONG(xdrs, (long *)&l));
147 if (!XDR_GETLONG(xdrs, (long *)&l)) {
163 * same as xdr_u_long - open coded to save a proc call!
170 switch (xdrs->x_op) {
172 return (XDR_PUTLONG(xdrs, lp));
174 return (XDR_GETLONG(xdrs, lp));
183 * XDR unsigned long integers
184 * same as xdr_long - open coded to save a proc call!
187 xdr_u_long(xdrs, ulp)
191 switch (xdrs->x_op) {
193 return (XDR_PUTLONG(xdrs, (long *)ulp));
195 return (XDR_GETLONG(xdrs, (long *)ulp));
205 * XDR 32-bit integers
206 * same as xdr_u_int32_t - open coded to save a proc call!
209 xdr_int32_t(xdrs, int32_p)
215 switch (xdrs->x_op) {
219 return (XDR_PUTLONG(xdrs, &l));
222 if (!XDR_GETLONG(xdrs, &l)) {
225 *int32_p = (int32_t) l;
236 * XDR unsigned 32-bit integers
237 * same as xdr_int32_t - open coded to save a proc call!
240 xdr_u_int32_t(xdrs, u_int32_p)
242 u_int32_t *u_int32_p;
246 switch (xdrs->x_op) {
249 l = (u_long) *u_int32_p;
250 return (XDR_PUTLONG(xdrs, (long *)&l));
253 if (!XDR_GETLONG(xdrs, (long *)&l)) {
256 *u_int32_p = (u_int32_t) l;
267 * XDR unsigned 32-bit integers
268 * same as xdr_int32_t - open coded to save a proc call!
271 xdr_uint32_t(xdrs, u_int32_p)
277 switch (xdrs->x_op) {
280 l = (u_long) *u_int32_p;
281 return (XDR_PUTLONG(xdrs, (long *)&l));
284 if (!XDR_GETLONG(xdrs, (long *)&l)) {
287 *u_int32_p = (u_int32_t) l;
307 switch (xdrs->x_op) {
311 return (XDR_PUTLONG(xdrs, &l));
314 if (!XDR_GETLONG(xdrs, &l)) {
328 * XDR unsigned short integers
331 xdr_u_short(xdrs, usp)
337 switch (xdrs->x_op) {
341 return (XDR_PUTLONG(xdrs, (long *)&l));
344 if (!XDR_GETLONG(xdrs, (long *)&l)) {
359 * XDR 16-bit integers
362 xdr_int16_t(xdrs, int16_p)
368 switch (xdrs->x_op) {
372 return (XDR_PUTLONG(xdrs, &l));
375 if (!XDR_GETLONG(xdrs, &l)) {
378 *int16_p = (int16_t) l;
389 * XDR unsigned 16-bit integers
392 xdr_u_int16_t(xdrs, u_int16_p)
394 u_int16_t *u_int16_p;
398 switch (xdrs->x_op) {
401 l = (u_long) *u_int16_p;
402 return (XDR_PUTLONG(xdrs, (long *)&l));
405 if (!XDR_GETLONG(xdrs, (long *)&l)) {
408 *u_int16_p = (u_int16_t) l;
419 * XDR unsigned 16-bit integers
422 xdr_uint16_t(xdrs, u_int16_p)
428 switch (xdrs->x_op) {
431 l = (u_long) *u_int16_p;
432 return (XDR_PUTLONG(xdrs, (long *)&l));
435 if (!XDR_GETLONG(xdrs, (long *)&l)) {
438 *u_int16_p = (u_int16_t) l;
460 if (!xdr_int(xdrs, &i)) {
468 * XDR an unsigned char
478 if (!xdr_u_int(xdrs, &u)) {
495 switch (xdrs->x_op) {
498 lb = *bp ? XDR_TRUE : XDR_FALSE;
499 return (XDR_PUTLONG(xdrs, &lb));
502 if (!XDR_GETLONG(xdrs, &lb)) {
505 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
523 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
526 * enums are treated as ints
528 /* LINTED */ if (sizeof (enum sizecheck) == sizeof (long)) {
529 return (xdr_long(xdrs, (long *)(void *)ep));
530 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) {
531 return (xdr_int(xdrs, (int *)(void *)ep));
532 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (short)) {
533 return (xdr_short(xdrs, (short *)(void *)ep));
541 * Allows the specification of a fixed size sequence of opaque bytes.
542 * cp points to the opaque object and cnt gives the byte length.
545 xdr_opaque(xdrs, cp, cnt)
551 static int crud[BYTES_PER_XDR_UNIT];
554 * if no data we are done
560 * round byte count to full xdr units
562 rndup = cnt % BYTES_PER_XDR_UNIT;
564 rndup = BYTES_PER_XDR_UNIT - rndup;
566 if (xdrs->x_op == XDR_DECODE) {
567 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
572 return (XDR_GETBYTES(xdrs, (caddr_t)(void *)crud, rndup));
575 if (xdrs->x_op == XDR_ENCODE) {
576 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
581 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
584 if (xdrs->x_op == XDR_FREE) {
593 * *cpp is a pointer to the bytes, *sizep is the count.
594 * If *cpp is NULL maxsize bytes are allocated
597 xdr_bytes(xdrs, cpp, sizep, maxsize)
603 char *sp = *cpp; /* sp is the actual string pointer */
607 * first deal with the length since xdr bytes are counted
609 if (! xdr_u_int(xdrs, sizep)) {
613 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
618 * now deal with the actual bytes
620 switch (xdrs->x_op) {
627 *cpp = sp = mem_alloc(nodesize);
630 warnx("xdr_bytes: out of memory");
636 return (xdr_opaque(xdrs, sp, nodesize));
640 mem_free(sp, nodesize);
650 * Implemented here due to commonality of the object.
658 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
662 * XDR a descriminated union
663 * Support routine for discriminated unions.
664 * You create an array of xdrdiscrim structures, terminated with
665 * an entry with a null procedure pointer. The routine gets
666 * the discriminant value and then searches the array of xdrdiscrims
667 * looking for that value. It calls the procedure given in the xdrdiscrim
668 * to handle the discriminant. If there is no specific routine a default
669 * routine may be called.
670 * If there is no specific or default routine an error is returned.
673 xdr_union(xdrs, dscmp, unp, choices, dfault)
675 enum_t *dscmp; /* enum to decide which arm to work on */
676 char *unp; /* the union itself */
677 const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
678 xdrproc_t dfault; /* default xdr routine */
683 * we deal with the discriminator; it's an enum
685 if (! xdr_enum(xdrs, dscmp)) {
691 * search choices for a value that matches the discriminator.
692 * if we find one, execute the xdr routine for that value.
694 for (; choices->proc != NULL_xdrproc_t; choices++) {
695 if (choices->value == dscm)
696 return ((*(choices->proc))(xdrs, unp));
700 * no match - execute the default xdr routine if there is one
702 return ((dfault == NULL_xdrproc_t) ? FALSE :
703 (*dfault)(xdrs, unp));
708 * Non-portable xdr primitives.
709 * Care should be taken when moving these routines to new architectures.
714 * XDR null terminated ASCII strings
715 * xdr_string deals with "C strings" - arrays of bytes that are
716 * terminated by a NULL character. The parameter cpp references a
717 * pointer to storage; If the pointer is null, then the necessary
718 * storage is allocated. The last parameter is the max allowed length
719 * of the string as specified by a protocol.
722 xdr_string(xdrs, cpp, maxsize)
727 char *sp = *cpp; /* sp is the actual string pointer */
732 * first deal with the length since xdr strings are counted-strings
734 switch (xdrs->x_op) {
737 return(TRUE); /* already free */
746 if (! xdr_u_int(xdrs, &size)) {
749 if (size > maxsize) {
755 * now deal with the actual bytes
757 switch (xdrs->x_op) {
764 *cpp = sp = mem_alloc(nodesize);
766 warnx("xdr_string: out of memory");
773 return (xdr_opaque(xdrs, sp, size));
776 mem_free(sp, nodesize);
785 * Wrapper for xdr_string that can be called directly from
786 * routines like clnt_call
789 xdr_wrapstring(xdrs, cpp)
793 return xdr_string(xdrs, cpp, LASTUNSIGNED);
797 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
798 * are in the "non-portable" section because they require that a `long long'
801 * --thorpej@netbsd.org, November 30, 1999
805 * XDR 64-bit integers
808 xdr_int64_t(xdrs, llp)
814 switch (xdrs->x_op) {
816 ul[0] = (u_long)((u_int64_t)*llp >> 32) & 0xffffffff;
817 ul[1] = (u_long)((u_int64_t)*llp) & 0xffffffff;
818 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
820 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
822 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
824 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
827 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
838 * XDR unsigned 64-bit integers
841 xdr_u_int64_t(xdrs, ullp)
847 switch (xdrs->x_op) {
849 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
850 ul[1] = (u_long)(*ullp) & 0xffffffff;
851 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
853 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
855 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
857 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
860 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
870 * XDR unsigned 64-bit integers
873 xdr_uint64_t(xdrs, ullp)
879 switch (xdrs->x_op) {
881 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
882 ul[1] = (u_long)(*ullp) & 0xffffffff;
883 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
885 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
887 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
889 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
892 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
912 * Don't bother open-coding this; it's a fair amount of code. Just
913 * call xdr_int64_t().
915 return (xdr_int64_t(xdrs, (int64_t *)llp));
920 * XDR unsigned hypers
923 xdr_u_hyper(xdrs, ullp)
929 * Don't bother open-coding this; it's a fair amount of code. Just
930 * call xdr_u_int64_t().
932 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));
940 xdr_longlong_t(xdrs, llp)
946 * Don't bother open-coding this; it's a fair amount of code. Just
947 * call xdr_int64_t().
949 return (xdr_int64_t(xdrs, (int64_t *)llp));
957 xdr_u_longlong_t(xdrs, ullp)
963 * Don't bother open-coding this; it's a fair amount of code. Just
964 * call xdr_u_int64_t().
966 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));