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"
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));