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";
41 * xdr.c, Generic XDR routines implementation.
43 * These are the "generic" xdr routines used to serialize and de-serialize
44 * most common data items. See xdr.h for more info on the interface to
48 #include "namespace.h"
55 #include <rpc/rpc_com.h>
56 #include <rpc/types.h>
58 #include "un-namespace.h"
60 typedef quad_t longlong_t; /* ANSI long long type */
61 typedef u_quad_t u_longlong_t; /* ANSI unsigned long long type */
64 * constants specific to the xdr "protocol"
66 #define XDR_FALSE ((long) 0)
67 #define XDR_TRUE ((long) 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
79 xdr_free(xdrproc_t proc, void *objp)
102 xdr_int(XDR *xdrs, int *ip)
106 switch (xdrs->x_op) {
110 return (XDR_PUTLONG(xdrs, &l));
113 if (!XDR_GETLONG(xdrs, &l)) {
127 * XDR unsigned integers
130 xdr_u_int(XDR *xdrs, u_int *up)
134 switch (xdrs->x_op) {
138 return (XDR_PUTLONG(xdrs, (long *)&l));
141 if (!XDR_GETLONG(xdrs, (long *)&l)) {
157 * same as xdr_u_long - open coded to save a proc call!
160 xdr_long(XDR *xdrs, long *lp)
162 switch (xdrs->x_op) {
164 return (XDR_PUTLONG(xdrs, lp));
166 return (XDR_GETLONG(xdrs, lp));
175 * XDR unsigned long integers
176 * same as xdr_long - open coded to save a proc call!
179 xdr_u_long(XDR *xdrs, u_long *ulp)
181 switch (xdrs->x_op) {
183 return (XDR_PUTLONG(xdrs, (long *)ulp));
185 return (XDR_GETLONG(xdrs, (long *)ulp));
195 * XDR 32-bit integers
196 * same as xdr_u_int32_t - open coded to save a proc call!
199 xdr_int32_t(XDR *xdrs, int32_t *int32_p)
203 switch (xdrs->x_op) {
207 return (XDR_PUTLONG(xdrs, &l));
210 if (!XDR_GETLONG(xdrs, &l)) {
213 *int32_p = (int32_t) l;
224 * XDR unsigned 32-bit integers
225 * same as xdr_int32_t - open coded to save a proc call!
228 xdr_u_int32_t(XDR *xdrs, u_int32_t *u_int32_p)
232 switch (xdrs->x_op) {
235 l = (u_long) *u_int32_p;
236 return (XDR_PUTLONG(xdrs, (long *)&l));
239 if (!XDR_GETLONG(xdrs, (long *)&l)) {
242 *u_int32_p = (u_int32_t) l;
253 * XDR unsigned 32-bit integers
254 * same as xdr_int32_t - open coded to save a proc call!
257 xdr_uint32_t(XDR *xdrs, uint32_t *u_int32_p)
261 switch (xdrs->x_op) {
264 l = (u_long) *u_int32_p;
265 return (XDR_PUTLONG(xdrs, (long *)&l));
268 if (!XDR_GETLONG(xdrs, (long *)&l)) {
271 *u_int32_p = (u_int32_t) l;
285 xdr_short(XDR *xdrs, short *sp)
289 switch (xdrs->x_op) {
293 return (XDR_PUTLONG(xdrs, &l));
296 if (!XDR_GETLONG(xdrs, &l)) {
310 * XDR unsigned short integers
313 xdr_u_short(XDR *xdrs, u_short *usp)
317 switch (xdrs->x_op) {
321 return (XDR_PUTLONG(xdrs, (long *)&l));
324 if (!XDR_GETLONG(xdrs, (long *)&l)) {
339 * XDR 16-bit integers
342 xdr_int16_t(XDR *xdrs, int16_t *int16_p)
346 switch (xdrs->x_op) {
350 return (XDR_PUTLONG(xdrs, &l));
353 if (!XDR_GETLONG(xdrs, &l)) {
356 *int16_p = (int16_t) l;
367 * XDR unsigned 16-bit integers
370 xdr_u_int16_t(XDR *xdrs, u_int16_t *u_int16_p)
374 switch (xdrs->x_op) {
377 l = (u_long) *u_int16_p;
378 return (XDR_PUTLONG(xdrs, (long *)&l));
381 if (!XDR_GETLONG(xdrs, (long *)&l)) {
384 *u_int16_p = (u_int16_t) l;
395 * XDR unsigned 16-bit integers
398 xdr_uint16_t(XDR *xdrs, uint16_t *u_int16_p)
402 switch (xdrs->x_op) {
405 l = (u_long) *u_int16_p;
406 return (XDR_PUTLONG(xdrs, (long *)&l));
409 if (!XDR_GETLONG(xdrs, (long *)&l)) {
412 *u_int16_p = (u_int16_t) l;
427 xdr_char(XDR *xdrs, char *cp)
431 i = *((unsigned char *)cp);
432 if (!xdr_u_int(xdrs, &i)) {
435 *((unsigned char *)cp) = i;
440 * XDR an unsigned char
443 xdr_u_char(XDR *xdrs, u_char *cp)
448 if (!xdr_u_int(xdrs, &u)) {
459 xdr_bool(XDR *xdrs, bool_t *bp)
463 switch (xdrs->x_op) {
466 lb = *bp ? XDR_TRUE : XDR_FALSE;
467 return (XDR_PUTLONG(xdrs, &lb));
470 if (!XDR_GETLONG(xdrs, &lb)) {
473 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
487 xdr_enum(XDR *xdrs, enum_t *ep)
489 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
492 * enums are treated as ints
494 /* LINTED */ if (sizeof (enum sizecheck) == sizeof (long)) {
495 return (xdr_long(xdrs, (long *)(void *)ep));
496 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) {
497 return (xdr_int(xdrs, (int *)(void *)ep));
498 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (short)) {
499 return (xdr_short(xdrs, (short *)(void *)ep));
507 * Allows the specification of a fixed size sequence of opaque bytes.
508 * cp points to the opaque object and cnt gives the byte length.
511 xdr_opaque(XDR *xdrs, caddr_t cp, u_int cnt)
514 static int crud[BYTES_PER_XDR_UNIT];
517 * if no data we are done
523 * round byte count to full xdr units
525 rndup = cnt % BYTES_PER_XDR_UNIT;
527 rndup = BYTES_PER_XDR_UNIT - rndup;
529 if (xdrs->x_op == XDR_DECODE) {
530 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
535 return (XDR_GETBYTES(xdrs, (caddr_t)(void *)crud, rndup));
538 if (xdrs->x_op == XDR_ENCODE) {
539 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
544 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
547 if (xdrs->x_op == XDR_FREE) {
556 * *cpp is a pointer to the bytes, *sizep is the count.
557 * If *cpp is NULL maxsize bytes are allocated
560 xdr_bytes(XDR *xdrs, char **cpp, u_int *sizep, u_int maxsize)
562 char *sp = *cpp; /* sp is the actual string pointer */
564 bool_t ret, allocated = FALSE;
567 * first deal with the length since xdr bytes are counted
569 if (! xdr_u_int(xdrs, sizep)) {
573 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
578 * now deal with the actual bytes
580 switch (xdrs->x_op) {
587 *cpp = sp = mem_alloc(nodesize);
591 warnx("xdr_bytes: out of memory");
597 ret = xdr_opaque(xdrs, sp, nodesize);
598 if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
599 if (allocated == TRUE) {
608 mem_free(sp, nodesize);
618 * Implemented here due to commonality of the object.
621 xdr_netobj(XDR *xdrs, struct netobj *np)
624 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
628 * XDR a descriminated union
629 * Support routine for discriminated unions.
630 * You create an array of xdrdiscrim structures, terminated with
631 * an entry with a null procedure pointer. The routine gets
632 * the discriminant value and then searches the array of xdrdiscrims
633 * looking for that value. It calls the procedure given in the xdrdiscrim
634 * to handle the discriminant. If there is no specific routine a default
635 * routine may be called.
636 * If there is no specific or default routine an error is returned.
639 xdr_union(XDR *xdrs, enum_t *dscmp, char *unp, const struct xdr_discrim *choices, xdrproc_t dfault)
642 * enum_t *dscmp; // enum to decide which arm to work on
643 * char *unp; // the union itself
644 * const struct xdr_discrim *choices; // [value, xdr proc] for each arm
645 * xdrproc_t dfault; // default xdr routine
651 * we deal with the discriminator; it's an enum
653 if (! xdr_enum(xdrs, dscmp)) {
659 * search choices for a value that matches the discriminator.
660 * if we find one, execute the xdr routine for that value.
662 for (; choices->proc != NULL_xdrproc_t; choices++) {
663 if (choices->value == dscm)
664 return ((*(choices->proc))(xdrs, unp));
668 * no match - execute the default xdr routine if there is one
670 return ((dfault == NULL_xdrproc_t) ? FALSE :
671 (*dfault)(xdrs, unp));
676 * Non-portable xdr primitives.
677 * Care should be taken when moving these routines to new architectures.
682 * XDR null terminated ASCII strings
683 * xdr_string deals with "C strings" - arrays of bytes that are
684 * terminated by a NULL character. The parameter cpp references a
685 * pointer to storage; If the pointer is null, then the necessary
686 * storage is allocated. The last parameter is the max allowed length
687 * of the string as specified by a protocol.
690 xdr_string(XDR *xdrs, char **cpp, u_int maxsize)
692 char *sp = *cpp; /* sp is the actual string pointer */
695 bool_t ret, allocated = FALSE;
698 * first deal with the length since xdr strings are counted-strings
700 switch (xdrs->x_op) {
703 return(TRUE); /* already free */
712 if (! xdr_u_int(xdrs, &size)) {
715 if (size > maxsize) {
721 * now deal with the actual bytes
723 switch (xdrs->x_op) {
730 *cpp = sp = mem_alloc(nodesize);
734 warnx("xdr_string: out of memory");
741 ret = xdr_opaque(xdrs, sp, size);
742 if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
743 if (allocated == TRUE) {
751 mem_free(sp, nodesize);
760 * Wrapper for xdr_string that can be called directly from
761 * routines like clnt_call
764 xdr_wrapstring(XDR *xdrs, char **cpp)
766 return xdr_string(xdrs, cpp, RPC_MAXDATASIZE);
770 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
771 * are in the "non-portable" section because they require that a `long long'
774 * --thorpej@netbsd.org, November 30, 1999
778 * XDR 64-bit integers
781 xdr_int64_t(XDR *xdrs, int64_t *llp)
785 switch (xdrs->x_op) {
787 ul[0] = (u_long)((u_int64_t)*llp >> 32) & 0xffffffff;
788 ul[1] = (u_long)((u_int64_t)*llp) & 0xffffffff;
789 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
791 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
793 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
795 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
798 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
809 * XDR unsigned 64-bit integers
812 xdr_u_int64_t(XDR *xdrs, u_int64_t *ullp)
816 switch (xdrs->x_op) {
818 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
819 ul[1] = (u_long)(*ullp) & 0xffffffff;
820 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
822 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
824 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
826 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
829 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
839 * XDR unsigned 64-bit integers
842 xdr_uint64_t(XDR *xdrs, uint64_t *ullp)
846 switch (xdrs->x_op) {
848 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
849 ul[1] = (u_long)(*ullp) & 0xffffffff;
850 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
852 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
854 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
856 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
859 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
873 xdr_hyper(XDR *xdrs, longlong_t *llp)
877 * Don't bother open-coding this; it's a fair amount of code. Just
878 * call xdr_int64_t().
880 return (xdr_int64_t(xdrs, (int64_t *)llp));
885 * XDR unsigned hypers
888 xdr_u_hyper(XDR *xdrs, u_longlong_t *ullp)
892 * Don't bother open-coding this; it's a fair amount of code. Just
893 * call xdr_u_int64_t().
895 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));
903 xdr_longlong_t(XDR *xdrs, longlong_t *llp)
907 * Don't bother open-coding this; it's a fair amount of code. Just
908 * call xdr_int64_t().
910 return (xdr_int64_t(xdrs, (int64_t *)llp));
918 xdr_u_longlong_t(XDR *xdrs, u_longlong_t *ullp)
922 * Don't bother open-coding this; it's a fair amount of code. Just
923 * call xdr_u_int64_t().
925 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));