1 /* $NetBSD: xdr.c,v 1.22 2000/07/06 03:10:35 christos Exp $ */
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char *sccsid2 = "@(#)xdr.c 1.35 87/08/12";
36 static char *sccsid = "@(#)xdr.c 2.1 88/07/29 4.0 RPCSRC";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * xdr.c, Generic XDR routines implementation.
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"
56 #include <rpc/rpc_com.h>
57 #include <rpc/types.h>
59 #include "un-namespace.h"
61 typedef quad_t longlong_t; /* ANSI long long type */
62 typedef u_quad_t u_longlong_t; /* ANSI unsigned long long type */
65 * constants specific to the xdr "protocol"
67 #define XDR_FALSE ((long) 0)
68 #define XDR_TRUE ((long) 1)
73 static const char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
76 * Free a data structure using XDR
77 * Not a filter, but a convenient utility nonetheless
111 switch (xdrs->x_op) {
115 return (XDR_PUTLONG(xdrs, &l));
118 if (!XDR_GETLONG(xdrs, &l)) {
132 * XDR unsigned integers
141 switch (xdrs->x_op) {
145 return (XDR_PUTLONG(xdrs, (long *)&l));
148 if (!XDR_GETLONG(xdrs, (long *)&l)) {
164 * same as xdr_u_long - open coded to save a proc call!
171 switch (xdrs->x_op) {
173 return (XDR_PUTLONG(xdrs, lp));
175 return (XDR_GETLONG(xdrs, lp));
184 * XDR unsigned long integers
185 * same as xdr_long - open coded to save a proc call!
188 xdr_u_long(xdrs, ulp)
192 switch (xdrs->x_op) {
194 return (XDR_PUTLONG(xdrs, (long *)ulp));
196 return (XDR_GETLONG(xdrs, (long *)ulp));
206 * XDR 32-bit integers
207 * same as xdr_u_int32_t - open coded to save a proc call!
210 xdr_int32_t(xdrs, int32_p)
216 switch (xdrs->x_op) {
220 return (XDR_PUTLONG(xdrs, &l));
223 if (!XDR_GETLONG(xdrs, &l)) {
226 *int32_p = (int32_t) l;
237 * XDR unsigned 32-bit integers
238 * same as xdr_int32_t - open coded to save a proc call!
241 xdr_u_int32_t(xdrs, u_int32_p)
243 u_int32_t *u_int32_p;
247 switch (xdrs->x_op) {
250 l = (u_long) *u_int32_p;
251 return (XDR_PUTLONG(xdrs, (long *)&l));
254 if (!XDR_GETLONG(xdrs, (long *)&l)) {
257 *u_int32_p = (u_int32_t) l;
268 * XDR unsigned 32-bit integers
269 * same as xdr_int32_t - open coded to save a proc call!
272 xdr_uint32_t(xdrs, u_int32_p)
278 switch (xdrs->x_op) {
281 l = (u_long) *u_int32_p;
282 return (XDR_PUTLONG(xdrs, (long *)&l));
285 if (!XDR_GETLONG(xdrs, (long *)&l)) {
288 *u_int32_p = (u_int32_t) l;
308 switch (xdrs->x_op) {
312 return (XDR_PUTLONG(xdrs, &l));
315 if (!XDR_GETLONG(xdrs, &l)) {
329 * XDR unsigned short integers
332 xdr_u_short(xdrs, usp)
338 switch (xdrs->x_op) {
342 return (XDR_PUTLONG(xdrs, (long *)&l));
345 if (!XDR_GETLONG(xdrs, (long *)&l)) {
360 * XDR 16-bit integers
363 xdr_int16_t(xdrs, int16_p)
369 switch (xdrs->x_op) {
373 return (XDR_PUTLONG(xdrs, &l));
376 if (!XDR_GETLONG(xdrs, &l)) {
379 *int16_p = (int16_t) l;
390 * XDR unsigned 16-bit integers
393 xdr_u_int16_t(xdrs, u_int16_p)
395 u_int16_t *u_int16_p;
399 switch (xdrs->x_op) {
402 l = (u_long) *u_int16_p;
403 return (XDR_PUTLONG(xdrs, (long *)&l));
406 if (!XDR_GETLONG(xdrs, (long *)&l)) {
409 *u_int16_p = (u_int16_t) l;
420 * XDR unsigned 16-bit integers
423 xdr_uint16_t(xdrs, u_int16_p)
429 switch (xdrs->x_op) {
432 l = (u_long) *u_int16_p;
433 return (XDR_PUTLONG(xdrs, (long *)&l));
436 if (!XDR_GETLONG(xdrs, (long *)&l)) {
439 *u_int16_p = (u_int16_t) l;
461 if (!xdr_int(xdrs, &i)) {
469 * XDR an unsigned char
479 if (!xdr_u_int(xdrs, &u)) {
496 switch (xdrs->x_op) {
499 lb = *bp ? XDR_TRUE : XDR_FALSE;
500 return (XDR_PUTLONG(xdrs, &lb));
503 if (!XDR_GETLONG(xdrs, &lb)) {
506 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
524 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
527 * enums are treated as ints
529 /* LINTED */ if (sizeof (enum sizecheck) == sizeof (long)) {
530 return (xdr_long(xdrs, (long *)(void *)ep));
531 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) {
532 return (xdr_int(xdrs, (int *)(void *)ep));
533 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (short)) {
534 return (xdr_short(xdrs, (short *)(void *)ep));
542 * Allows the specification of a fixed size sequence of opaque bytes.
543 * cp points to the opaque object and cnt gives the byte length.
546 xdr_opaque(xdrs, cp, cnt)
552 static int crud[BYTES_PER_XDR_UNIT];
555 * if no data we are done
561 * round byte count to full xdr units
563 rndup = cnt % BYTES_PER_XDR_UNIT;
565 rndup = BYTES_PER_XDR_UNIT - rndup;
567 if (xdrs->x_op == XDR_DECODE) {
568 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
573 return (XDR_GETBYTES(xdrs, (caddr_t)(void *)crud, rndup));
576 if (xdrs->x_op == XDR_ENCODE) {
577 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
582 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
585 if (xdrs->x_op == XDR_FREE) {
594 * *cpp is a pointer to the bytes, *sizep is the count.
595 * If *cpp is NULL maxsize bytes are allocated
598 xdr_bytes(xdrs, cpp, sizep, maxsize)
604 char *sp = *cpp; /* sp is the actual string pointer */
606 bool_t ret, allocated = FALSE;
609 * first deal with the length since xdr bytes are counted
611 if (! xdr_u_int(xdrs, sizep)) {
615 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
620 * now deal with the actual bytes
622 switch (xdrs->x_op) {
629 *cpp = sp = mem_alloc(nodesize);
633 warnx("xdr_bytes: out of memory");
639 ret = xdr_opaque(xdrs, sp, nodesize);
640 if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
641 if (allocated == TRUE) {
650 mem_free(sp, nodesize);
660 * Implemented here due to commonality of the object.
668 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
672 * XDR a descriminated union
673 * Support routine for discriminated unions.
674 * You create an array of xdrdiscrim structures, terminated with
675 * an entry with a null procedure pointer. The routine gets
676 * the discriminant value and then searches the array of xdrdiscrims
677 * looking for that value. It calls the procedure given in the xdrdiscrim
678 * to handle the discriminant. If there is no specific routine a default
679 * routine may be called.
680 * If there is no specific or default routine an error is returned.
683 xdr_union(xdrs, dscmp, unp, choices, dfault)
685 enum_t *dscmp; /* enum to decide which arm to work on */
686 char *unp; /* the union itself */
687 const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
688 xdrproc_t dfault; /* default xdr routine */
693 * we deal with the discriminator; it's an enum
695 if (! xdr_enum(xdrs, dscmp)) {
701 * search choices for a value that matches the discriminator.
702 * if we find one, execute the xdr routine for that value.
704 for (; choices->proc != NULL_xdrproc_t; choices++) {
705 if (choices->value == dscm)
706 return ((*(choices->proc))(xdrs, unp));
710 * no match - execute the default xdr routine if there is one
712 return ((dfault == NULL_xdrproc_t) ? FALSE :
713 (*dfault)(xdrs, unp));
718 * Non-portable xdr primitives.
719 * Care should be taken when moving these routines to new architectures.
724 * XDR null terminated ASCII strings
725 * xdr_string deals with "C strings" - arrays of bytes that are
726 * terminated by a NULL character. The parameter cpp references a
727 * pointer to storage; If the pointer is null, then the necessary
728 * storage is allocated. The last parameter is the max allowed length
729 * of the string as specified by a protocol.
732 xdr_string(xdrs, cpp, maxsize)
737 char *sp = *cpp; /* sp is the actual string pointer */
740 bool_t ret, allocated = FALSE;
743 * first deal with the length since xdr strings are counted-strings
745 switch (xdrs->x_op) {
748 return(TRUE); /* already free */
757 if (! xdr_u_int(xdrs, &size)) {
760 if (size > maxsize) {
766 * now deal with the actual bytes
768 switch (xdrs->x_op) {
775 *cpp = sp = mem_alloc(nodesize);
779 warnx("xdr_string: out of memory");
786 ret = xdr_opaque(xdrs, sp, size);
787 if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
788 if (allocated == TRUE) {
796 mem_free(sp, nodesize);
805 * Wrapper for xdr_string that can be called directly from
806 * routines like clnt_call
809 xdr_wrapstring(xdrs, cpp)
813 return xdr_string(xdrs, cpp, RPC_MAXDATASIZE);
817 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
818 * are in the "non-portable" section because they require that a `long long'
821 * --thorpej@netbsd.org, November 30, 1999
825 * XDR 64-bit integers
828 xdr_int64_t(xdrs, llp)
834 switch (xdrs->x_op) {
836 ul[0] = (u_long)((u_int64_t)*llp >> 32) & 0xffffffff;
837 ul[1] = (u_long)((u_int64_t)*llp) & 0xffffffff;
838 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
840 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
842 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
844 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
847 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
858 * XDR unsigned 64-bit integers
861 xdr_u_int64_t(xdrs, ullp)
867 switch (xdrs->x_op) {
869 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
870 ul[1] = (u_long)(*ullp) & 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]));
890 * XDR unsigned 64-bit integers
893 xdr_uint64_t(xdrs, ullp)
899 switch (xdrs->x_op) {
901 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
902 ul[1] = (u_long)(*ullp) & 0xffffffff;
903 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
905 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
907 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
909 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
912 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
932 * Don't bother open-coding this; it's a fair amount of code. Just
933 * call xdr_int64_t().
935 return (xdr_int64_t(xdrs, (int64_t *)llp));
940 * XDR unsigned hypers
943 xdr_u_hyper(xdrs, ullp)
949 * Don't bother open-coding this; it's a fair amount of code. Just
950 * call xdr_u_int64_t().
952 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));
960 xdr_longlong_t(xdrs, llp)
966 * Don't bother open-coding this; it's a fair amount of code. Just
967 * call xdr_int64_t().
969 return (xdr_int64_t(xdrs, (int64_t *)llp));
977 xdr_u_longlong_t(xdrs, ullp)
983 * Don't bother open-coding this; it's a fair amount of code. Just
984 * call xdr_u_int64_t().
986 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));