1 /* $NetBSD: xdr.c,v 1.22 2000/07/06 03:10:35 christos Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2010, Oracle America, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials
17 * provided with the distribution.
18 * * Neither the name of the "Oracle America, Inc." nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static char *sccsid2 = "@(#)xdr.c 1.35 87/08/12";
38 static char *sccsid = "@(#)xdr.c 2.1 88/07/29 4.0 RPCSRC";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 * xdr.c, Generic XDR routines implementation.
46 * These are the "generic" xdr routines used to serialize and de-serialize
47 * most common data items. See xdr.h for more info on the interface to
51 #include "namespace.h"
58 #include <rpc/rpc_com.h>
59 #include <rpc/types.h>
61 #include "un-namespace.h"
63 typedef quad_t longlong_t; /* ANSI long long type */
64 typedef u_quad_t u_longlong_t; /* ANSI unsigned long long type */
67 * constants specific to the xdr "protocol"
69 #define XDR_FALSE ((long) 0)
70 #define XDR_TRUE ((long) 1)
75 static const char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
78 * Free a data structure using XDR
79 * Not a filter, but a convenient utility nonetheless
82 xdr_free(xdrproc_t proc, void *objp)
105 xdr_int(XDR *xdrs, int *ip)
109 switch (xdrs->x_op) {
113 return (XDR_PUTLONG(xdrs, &l));
116 if (!XDR_GETLONG(xdrs, &l)) {
130 * XDR unsigned integers
133 xdr_u_int(XDR *xdrs, u_int *up)
137 switch (xdrs->x_op) {
141 return (XDR_PUTLONG(xdrs, (long *)&l));
144 if (!XDR_GETLONG(xdrs, (long *)&l)) {
160 * same as xdr_u_long - open coded to save a proc call!
163 xdr_long(XDR *xdrs, long *lp)
165 switch (xdrs->x_op) {
167 return (XDR_PUTLONG(xdrs, lp));
169 return (XDR_GETLONG(xdrs, lp));
178 * XDR unsigned long integers
179 * same as xdr_long - open coded to save a proc call!
182 xdr_u_long(XDR *xdrs, u_long *ulp)
184 switch (xdrs->x_op) {
186 return (XDR_PUTLONG(xdrs, (long *)ulp));
188 return (XDR_GETLONG(xdrs, (long *)ulp));
198 * XDR 32-bit integers
199 * same as xdr_u_int32_t - open coded to save a proc call!
202 xdr_int32_t(XDR *xdrs, int32_t *int32_p)
206 switch (xdrs->x_op) {
210 return (XDR_PUTLONG(xdrs, &l));
213 if (!XDR_GETLONG(xdrs, &l)) {
216 *int32_p = (int32_t) l;
227 * XDR unsigned 32-bit integers
228 * same as xdr_int32_t - open coded to save a proc call!
231 xdr_u_int32_t(XDR *xdrs, u_int32_t *u_int32_p)
235 switch (xdrs->x_op) {
238 l = (u_long) *u_int32_p;
239 return (XDR_PUTLONG(xdrs, (long *)&l));
242 if (!XDR_GETLONG(xdrs, (long *)&l)) {
245 *u_int32_p = (u_int32_t) l;
256 * XDR unsigned 32-bit integers
257 * same as xdr_int32_t - open coded to save a proc call!
260 xdr_uint32_t(XDR *xdrs, uint32_t *u_int32_p)
264 switch (xdrs->x_op) {
267 l = (u_long) *u_int32_p;
268 return (XDR_PUTLONG(xdrs, (long *)&l));
271 if (!XDR_GETLONG(xdrs, (long *)&l)) {
274 *u_int32_p = (u_int32_t) l;
288 xdr_short(XDR *xdrs, short *sp)
292 switch (xdrs->x_op) {
296 return (XDR_PUTLONG(xdrs, &l));
299 if (!XDR_GETLONG(xdrs, &l)) {
313 * XDR unsigned short integers
316 xdr_u_short(XDR *xdrs, u_short *usp)
320 switch (xdrs->x_op) {
324 return (XDR_PUTLONG(xdrs, (long *)&l));
327 if (!XDR_GETLONG(xdrs, (long *)&l)) {
342 * XDR 16-bit integers
345 xdr_int16_t(XDR *xdrs, int16_t *int16_p)
349 switch (xdrs->x_op) {
353 return (XDR_PUTLONG(xdrs, &l));
356 if (!XDR_GETLONG(xdrs, &l)) {
359 *int16_p = (int16_t) l;
370 * XDR unsigned 16-bit integers
373 xdr_u_int16_t(XDR *xdrs, u_int16_t *u_int16_p)
377 switch (xdrs->x_op) {
380 l = (u_long) *u_int16_p;
381 return (XDR_PUTLONG(xdrs, (long *)&l));
384 if (!XDR_GETLONG(xdrs, (long *)&l)) {
387 *u_int16_p = (u_int16_t) l;
398 * XDR unsigned 16-bit integers
401 xdr_uint16_t(XDR *xdrs, uint16_t *u_int16_p)
405 switch (xdrs->x_op) {
408 l = (u_long) *u_int16_p;
409 return (XDR_PUTLONG(xdrs, (long *)&l));
412 if (!XDR_GETLONG(xdrs, (long *)&l)) {
415 *u_int16_p = (u_int16_t) l;
430 xdr_char(XDR *xdrs, char *cp)
435 if (!xdr_int(xdrs, &i)) {
443 * XDR an unsigned char
446 xdr_u_char(XDR *xdrs, u_char *cp)
451 if (!xdr_u_int(xdrs, &u)) {
462 xdr_bool(XDR *xdrs, bool_t *bp)
466 switch (xdrs->x_op) {
469 lb = *bp ? XDR_TRUE : XDR_FALSE;
470 return (XDR_PUTLONG(xdrs, &lb));
473 if (!XDR_GETLONG(xdrs, &lb)) {
476 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
490 xdr_enum(XDR *xdrs, enum_t *ep)
492 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
495 * enums are treated as ints
497 /* LINTED */ if (sizeof (enum sizecheck) == sizeof (long)) {
498 return (xdr_long(xdrs, (long *)(void *)ep));
499 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) {
500 return (xdr_int(xdrs, (int *)(void *)ep));
501 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (short)) {
502 return (xdr_short(xdrs, (short *)(void *)ep));
510 * Allows the specification of a fixed size sequence of opaque bytes.
511 * cp points to the opaque object and cnt gives the byte length.
514 xdr_opaque(XDR *xdrs, caddr_t cp, u_int cnt)
517 static int crud[BYTES_PER_XDR_UNIT];
520 * if no data we are done
526 * round byte count to full xdr units
528 rndup = cnt % BYTES_PER_XDR_UNIT;
530 rndup = BYTES_PER_XDR_UNIT - rndup;
532 if (xdrs->x_op == XDR_DECODE) {
533 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
538 return (XDR_GETBYTES(xdrs, (caddr_t)(void *)crud, rndup));
541 if (xdrs->x_op == XDR_ENCODE) {
542 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
547 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
550 if (xdrs->x_op == XDR_FREE) {
559 * *cpp is a pointer to the bytes, *sizep is the count.
560 * If *cpp is NULL maxsize bytes are allocated
563 xdr_bytes(XDR *xdrs, char **cpp, u_int *sizep, u_int maxsize)
565 char *sp = *cpp; /* sp is the actual string pointer */
567 bool_t ret, allocated = FALSE;
570 * first deal with the length since xdr bytes are counted
572 if (! xdr_u_int(xdrs, sizep)) {
576 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
581 * now deal with the actual bytes
583 switch (xdrs->x_op) {
590 *cpp = sp = mem_alloc(nodesize);
594 warnx("xdr_bytes: out of memory");
600 ret = xdr_opaque(xdrs, sp, nodesize);
601 if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
602 if (allocated == TRUE) {
611 mem_free(sp, nodesize);
621 * Implemented here due to commonality of the object.
624 xdr_netobj(XDR *xdrs, struct netobj *np)
627 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
631 * XDR a descriminated union
632 * Support routine for discriminated unions.
633 * You create an array of xdrdiscrim structures, terminated with
634 * an entry with a null procedure pointer. The routine gets
635 * the discriminant value and then searches the array of xdrdiscrims
636 * looking for that value. It calls the procedure given in the xdrdiscrim
637 * to handle the discriminant. If there is no specific routine a default
638 * routine may be called.
639 * If there is no specific or default routine an error is returned.
642 xdr_union(XDR *xdrs, enum_t *dscmp, char *unp, const struct xdr_discrim *choices, xdrproc_t dfault)
645 * enum_t *dscmp; // enum to decide which arm to work on
646 * char *unp; // the union itself
647 * const struct xdr_discrim *choices; // [value, xdr proc] for each arm
648 * xdrproc_t dfault; // default xdr routine
654 * we deal with the discriminator; it's an enum
656 if (! xdr_enum(xdrs, dscmp)) {
662 * search choices for a value that matches the discriminator.
663 * if we find one, execute the xdr routine for that value.
665 for (; choices->proc != NULL_xdrproc_t; choices++) {
666 if (choices->value == dscm)
667 return ((*(choices->proc))(xdrs, unp));
671 * no match - execute the default xdr routine if there is one
673 return ((dfault == NULL_xdrproc_t) ? FALSE :
674 (*dfault)(xdrs, unp));
679 * Non-portable xdr primitives.
680 * Care should be taken when moving these routines to new architectures.
685 * XDR null terminated ASCII strings
686 * xdr_string deals with "C strings" - arrays of bytes that are
687 * terminated by a NULL character. The parameter cpp references a
688 * pointer to storage; If the pointer is null, then the necessary
689 * storage is allocated. The last parameter is the max allowed length
690 * of the string as specified by a protocol.
693 xdr_string(XDR *xdrs, char **cpp, u_int maxsize)
695 char *sp = *cpp; /* sp is the actual string pointer */
698 bool_t ret, allocated = FALSE;
701 * first deal with the length since xdr strings are counted-strings
703 switch (xdrs->x_op) {
706 return(TRUE); /* already free */
715 if (! xdr_u_int(xdrs, &size)) {
718 if (size > maxsize) {
724 * now deal with the actual bytes
726 switch (xdrs->x_op) {
733 *cpp = sp = mem_alloc(nodesize);
737 warnx("xdr_string: out of memory");
744 ret = xdr_opaque(xdrs, sp, size);
745 if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
746 if (allocated == TRUE) {
754 mem_free(sp, nodesize);
763 * Wrapper for xdr_string that can be called directly from
764 * routines like clnt_call
767 xdr_wrapstring(XDR *xdrs, char **cpp)
769 return xdr_string(xdrs, cpp, RPC_MAXDATASIZE);
773 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
774 * are in the "non-portable" section because they require that a `long long'
777 * --thorpej@netbsd.org, November 30, 1999
781 * XDR 64-bit integers
784 xdr_int64_t(XDR *xdrs, int64_t *llp)
788 switch (xdrs->x_op) {
790 ul[0] = (u_long)((u_int64_t)*llp >> 32) & 0xffffffff;
791 ul[1] = (u_long)((u_int64_t)*llp) & 0xffffffff;
792 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
794 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
796 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
798 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
801 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
812 * XDR unsigned 64-bit integers
815 xdr_u_int64_t(XDR *xdrs, u_int64_t *ullp)
819 switch (xdrs->x_op) {
821 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
822 ul[1] = (u_long)(*ullp) & 0xffffffff;
823 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
825 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
827 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
829 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
832 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
842 * XDR unsigned 64-bit integers
845 xdr_uint64_t(XDR *xdrs, uint64_t *ullp)
849 switch (xdrs->x_op) {
851 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
852 ul[1] = (u_long)(*ullp) & 0xffffffff;
853 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
855 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
857 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
859 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
862 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
876 xdr_hyper(XDR *xdrs, longlong_t *llp)
880 * Don't bother open-coding this; it's a fair amount of code. Just
881 * call xdr_int64_t().
883 return (xdr_int64_t(xdrs, (int64_t *)llp));
888 * XDR unsigned hypers
891 xdr_u_hyper(XDR *xdrs, u_longlong_t *ullp)
895 * Don't bother open-coding this; it's a fair amount of code. Just
896 * call xdr_u_int64_t().
898 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));
906 xdr_longlong_t(XDR *xdrs, longlong_t *llp)
910 * Don't bother open-coding this; it's a fair amount of code. Just
911 * call xdr_int64_t().
913 return (xdr_int64_t(xdrs, (int64_t *)llp));
921 xdr_u_longlong_t(XDR *xdrs, u_longlong_t *ullp)
925 * Don't bother open-coding this; it's a fair amount of code. Just
926 * call xdr_u_int64_t().
928 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));