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
80 xdr_free(xdrproc_t proc, void *objp)
103 xdr_int(XDR *xdrs, int *ip)
107 switch (xdrs->x_op) {
111 return (XDR_PUTLONG(xdrs, &l));
114 if (!XDR_GETLONG(xdrs, &l)) {
128 * XDR unsigned integers
131 xdr_u_int(XDR *xdrs, u_int *up)
135 switch (xdrs->x_op) {
139 return (XDR_PUTLONG(xdrs, (long *)&l));
142 if (!XDR_GETLONG(xdrs, (long *)&l)) {
158 * same as xdr_u_long - open coded to save a proc call!
161 xdr_long(XDR *xdrs, long *lp)
163 switch (xdrs->x_op) {
165 return (XDR_PUTLONG(xdrs, lp));
167 return (XDR_GETLONG(xdrs, lp));
176 * XDR unsigned long integers
177 * same as xdr_long - open coded to save a proc call!
180 xdr_u_long(XDR *xdrs, u_long *ulp)
182 switch (xdrs->x_op) {
184 return (XDR_PUTLONG(xdrs, (long *)ulp));
186 return (XDR_GETLONG(xdrs, (long *)ulp));
196 * XDR 32-bit integers
197 * same as xdr_u_int32_t - open coded to save a proc call!
200 xdr_int32_t(XDR *xdrs, int32_t *int32_p)
204 switch (xdrs->x_op) {
208 return (XDR_PUTLONG(xdrs, &l));
211 if (!XDR_GETLONG(xdrs, &l)) {
214 *int32_p = (int32_t) l;
225 * XDR unsigned 32-bit integers
226 * same as xdr_int32_t - open coded to save a proc call!
229 xdr_u_int32_t(XDR *xdrs, u_int32_t *u_int32_p)
233 switch (xdrs->x_op) {
236 l = (u_long) *u_int32_p;
237 return (XDR_PUTLONG(xdrs, (long *)&l));
240 if (!XDR_GETLONG(xdrs, (long *)&l)) {
243 *u_int32_p = (u_int32_t) l;
254 * XDR unsigned 32-bit integers
255 * same as xdr_int32_t - open coded to save a proc call!
258 xdr_uint32_t(XDR *xdrs, uint32_t *u_int32_p)
262 switch (xdrs->x_op) {
265 l = (u_long) *u_int32_p;
266 return (XDR_PUTLONG(xdrs, (long *)&l));
269 if (!XDR_GETLONG(xdrs, (long *)&l)) {
272 *u_int32_p = (u_int32_t) l;
286 xdr_short(XDR *xdrs, short *sp)
290 switch (xdrs->x_op) {
294 return (XDR_PUTLONG(xdrs, &l));
297 if (!XDR_GETLONG(xdrs, &l)) {
311 * XDR unsigned short integers
314 xdr_u_short(XDR *xdrs, u_short *usp)
318 switch (xdrs->x_op) {
322 return (XDR_PUTLONG(xdrs, (long *)&l));
325 if (!XDR_GETLONG(xdrs, (long *)&l)) {
340 * XDR 16-bit integers
343 xdr_int16_t(XDR *xdrs, int16_t *int16_p)
347 switch (xdrs->x_op) {
351 return (XDR_PUTLONG(xdrs, &l));
354 if (!XDR_GETLONG(xdrs, &l)) {
357 *int16_p = (int16_t) l;
368 * XDR unsigned 16-bit integers
371 xdr_u_int16_t(XDR *xdrs, u_int16_t *u_int16_p)
375 switch (xdrs->x_op) {
378 l = (u_long) *u_int16_p;
379 return (XDR_PUTLONG(xdrs, (long *)&l));
382 if (!XDR_GETLONG(xdrs, (long *)&l)) {
385 *u_int16_p = (u_int16_t) l;
396 * XDR unsigned 16-bit integers
399 xdr_uint16_t(XDR *xdrs, uint16_t *u_int16_p)
403 switch (xdrs->x_op) {
406 l = (u_long) *u_int16_p;
407 return (XDR_PUTLONG(xdrs, (long *)&l));
410 if (!XDR_GETLONG(xdrs, (long *)&l)) {
413 *u_int16_p = (u_int16_t) l;
428 xdr_char(XDR *xdrs, char *cp)
433 if (!xdr_int(xdrs, &i)) {
441 * XDR an unsigned char
444 xdr_u_char(XDR *xdrs, u_char *cp)
449 if (!xdr_u_int(xdrs, &u)) {
460 xdr_bool(XDR *xdrs, bool_t *bp)
464 switch (xdrs->x_op) {
467 lb = *bp ? XDR_TRUE : XDR_FALSE;
468 return (XDR_PUTLONG(xdrs, &lb));
471 if (!XDR_GETLONG(xdrs, &lb)) {
474 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
488 xdr_enum(XDR *xdrs, enum_t *ep)
490 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
493 * enums are treated as ints
495 /* LINTED */ if (sizeof (enum sizecheck) == sizeof (long)) {
496 return (xdr_long(xdrs, (long *)(void *)ep));
497 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) {
498 return (xdr_int(xdrs, (int *)(void *)ep));
499 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (short)) {
500 return (xdr_short(xdrs, (short *)(void *)ep));
508 * Allows the specification of a fixed size sequence of opaque bytes.
509 * cp points to the opaque object and cnt gives the byte length.
512 xdr_opaque(XDR *xdrs, caddr_t cp, u_int cnt)
515 static int crud[BYTES_PER_XDR_UNIT];
518 * if no data we are done
524 * round byte count to full xdr units
526 rndup = cnt % BYTES_PER_XDR_UNIT;
528 rndup = BYTES_PER_XDR_UNIT - rndup;
530 if (xdrs->x_op == XDR_DECODE) {
531 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
536 return (XDR_GETBYTES(xdrs, (caddr_t)(void *)crud, rndup));
539 if (xdrs->x_op == XDR_ENCODE) {
540 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
545 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
548 if (xdrs->x_op == XDR_FREE) {
557 * *cpp is a pointer to the bytes, *sizep is the count.
558 * If *cpp is NULL maxsize bytes are allocated
561 xdr_bytes(XDR *xdrs, char **cpp, u_int *sizep, u_int maxsize)
563 char *sp = *cpp; /* sp is the actual string pointer */
565 bool_t ret, allocated = FALSE;
568 * first deal with the length since xdr bytes are counted
570 if (! xdr_u_int(xdrs, sizep)) {
574 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
579 * now deal with the actual bytes
581 switch (xdrs->x_op) {
588 *cpp = sp = mem_alloc(nodesize);
592 warnx("xdr_bytes: out of memory");
598 ret = xdr_opaque(xdrs, sp, nodesize);
599 if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
600 if (allocated == TRUE) {
609 mem_free(sp, nodesize);
619 * Implemented here due to commonality of the object.
622 xdr_netobj(XDR *xdrs, struct netobj *np)
625 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
629 * XDR a descriminated union
630 * Support routine for discriminated unions.
631 * You create an array of xdrdiscrim structures, terminated with
632 * an entry with a null procedure pointer. The routine gets
633 * the discriminant value and then searches the array of xdrdiscrims
634 * looking for that value. It calls the procedure given in the xdrdiscrim
635 * to handle the discriminant. If there is no specific routine a default
636 * routine may be called.
637 * If there is no specific or default routine an error is returned.
640 xdr_union(XDR *xdrs, enum_t *dscmp, char *unp, const struct xdr_discrim *choices, xdrproc_t dfault)
643 * enum_t *dscmp; // enum to decide which arm to work on
644 * char *unp; // the union itself
645 * const struct xdr_discrim *choices; // [value, xdr proc] for each arm
646 * xdrproc_t dfault; // default xdr routine
652 * we deal with the discriminator; it's an enum
654 if (! xdr_enum(xdrs, dscmp)) {
660 * search choices for a value that matches the discriminator.
661 * if we find one, execute the xdr routine for that value.
663 for (; choices->proc != NULL_xdrproc_t; choices++) {
664 if (choices->value == dscm)
665 return ((*(choices->proc))(xdrs, unp));
669 * no match - execute the default xdr routine if there is one
671 return ((dfault == NULL_xdrproc_t) ? FALSE :
672 (*dfault)(xdrs, unp));
677 * Non-portable xdr primitives.
678 * Care should be taken when moving these routines to new architectures.
683 * XDR null terminated ASCII strings
684 * xdr_string deals with "C strings" - arrays of bytes that are
685 * terminated by a NULL character. The parameter cpp references a
686 * pointer to storage; If the pointer is null, then the necessary
687 * storage is allocated. The last parameter is the max allowed length
688 * of the string as specified by a protocol.
691 xdr_string(XDR *xdrs, char **cpp, u_int maxsize)
693 char *sp = *cpp; /* sp is the actual string pointer */
696 bool_t ret, allocated = FALSE;
699 * first deal with the length since xdr strings are counted-strings
701 switch (xdrs->x_op) {
704 return(TRUE); /* already free */
713 if (! xdr_u_int(xdrs, &size)) {
716 if (size > maxsize) {
722 * now deal with the actual bytes
724 switch (xdrs->x_op) {
731 *cpp = sp = mem_alloc(nodesize);
735 warnx("xdr_string: out of memory");
742 ret = xdr_opaque(xdrs, sp, size);
743 if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
744 if (allocated == TRUE) {
752 mem_free(sp, nodesize);
761 * Wrapper for xdr_string that can be called directly from
762 * routines like clnt_call
765 xdr_wrapstring(XDR *xdrs, char **cpp)
767 return xdr_string(xdrs, cpp, RPC_MAXDATASIZE);
771 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
772 * are in the "non-portable" section because they require that a `long long'
775 * --thorpej@netbsd.org, November 30, 1999
779 * XDR 64-bit integers
782 xdr_int64_t(XDR *xdrs, int64_t *llp)
786 switch (xdrs->x_op) {
788 ul[0] = (u_long)((u_int64_t)*llp >> 32) & 0xffffffff;
789 ul[1] = (u_long)((u_int64_t)*llp) & 0xffffffff;
790 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
792 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
794 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
796 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
799 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
810 * XDR unsigned 64-bit integers
813 xdr_u_int64_t(XDR *xdrs, u_int64_t *ullp)
817 switch (xdrs->x_op) {
819 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
820 ul[1] = (u_long)(*ullp) & 0xffffffff;
821 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
823 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
825 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
827 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
830 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
840 * XDR unsigned 64-bit integers
843 xdr_uint64_t(XDR *xdrs, uint64_t *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]));
874 xdr_hyper(XDR *xdrs, longlong_t *llp)
878 * Don't bother open-coding this; it's a fair amount of code. Just
879 * call xdr_int64_t().
881 return (xdr_int64_t(xdrs, (int64_t *)llp));
886 * XDR unsigned hypers
889 xdr_u_hyper(XDR *xdrs, u_longlong_t *ullp)
893 * Don't bother open-coding this; it's a fair amount of code. Just
894 * call xdr_u_int64_t().
896 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));
904 xdr_longlong_t(XDR *xdrs, longlong_t *llp)
908 * Don't bother open-coding this; it's a fair amount of code. Just
909 * call xdr_int64_t().
911 return (xdr_int64_t(xdrs, (int64_t *)llp));
919 xdr_u_longlong_t(XDR *xdrs, u_longlong_t *ullp)
923 * Don't bother open-coding this; it's a fair amount of code. Just
924 * call xdr_u_int64_t().
926 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));