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.
37 * xdr.c, Generic XDR routines implementation.
39 * These are the "generic" xdr routines used to serialize and de-serialize
40 * most common data items. See xdr.h for more info on the interface to
44 #include "namespace.h"
51 #include <rpc/rpc_com.h>
52 #include <rpc/types.h>
54 #include "un-namespace.h"
56 typedef quad_t longlong_t; /* ANSI long long type */
57 typedef u_quad_t u_longlong_t; /* ANSI unsigned long long type */
60 * constants specific to the xdr "protocol"
62 #define XDR_FALSE ((long) 0)
63 #define XDR_TRUE ((long) 1)
68 static const char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
71 * Free a data structure using XDR
72 * Not a filter, but a convenient utility nonetheless
75 xdr_free(xdrproc_t proc, void *objp)
98 xdr_int(XDR *xdrs, int *ip)
102 switch (xdrs->x_op) {
106 return (XDR_PUTLONG(xdrs, &l));
109 if (!XDR_GETLONG(xdrs, &l)) {
123 * XDR unsigned integers
126 xdr_u_int(XDR *xdrs, u_int *up)
130 switch (xdrs->x_op) {
134 return (XDR_PUTLONG(xdrs, (long *)&l));
137 if (!XDR_GETLONG(xdrs, (long *)&l)) {
153 * same as xdr_u_long - open coded to save a proc call!
156 xdr_long(XDR *xdrs, long *lp)
158 switch (xdrs->x_op) {
160 return (XDR_PUTLONG(xdrs, lp));
162 return (XDR_GETLONG(xdrs, lp));
171 * XDR unsigned long integers
172 * same as xdr_long - open coded to save a proc call!
175 xdr_u_long(XDR *xdrs, u_long *ulp)
177 switch (xdrs->x_op) {
179 return (XDR_PUTLONG(xdrs, (long *)ulp));
181 return (XDR_GETLONG(xdrs, (long *)ulp));
191 * XDR 32-bit integers
192 * same as xdr_u_int32_t - open coded to save a proc call!
195 xdr_int32_t(XDR *xdrs, int32_t *int32_p)
199 switch (xdrs->x_op) {
203 return (XDR_PUTLONG(xdrs, &l));
206 if (!XDR_GETLONG(xdrs, &l)) {
209 *int32_p = (int32_t) l;
220 * XDR unsigned 32-bit integers
221 * same as xdr_int32_t - open coded to save a proc call!
224 xdr_u_int32_t(XDR *xdrs, u_int32_t *u_int32_p)
228 switch (xdrs->x_op) {
231 l = (u_long) *u_int32_p;
232 return (XDR_PUTLONG(xdrs, (long *)&l));
235 if (!XDR_GETLONG(xdrs, (long *)&l)) {
238 *u_int32_p = (u_int32_t) l;
249 * XDR unsigned 32-bit integers
250 * same as xdr_int32_t - open coded to save a proc call!
253 xdr_uint32_t(XDR *xdrs, uint32_t *u_int32_p)
257 switch (xdrs->x_op) {
260 l = (u_long) *u_int32_p;
261 return (XDR_PUTLONG(xdrs, (long *)&l));
264 if (!XDR_GETLONG(xdrs, (long *)&l)) {
267 *u_int32_p = (u_int32_t) l;
281 xdr_short(XDR *xdrs, short *sp)
285 switch (xdrs->x_op) {
289 return (XDR_PUTLONG(xdrs, &l));
292 if (!XDR_GETLONG(xdrs, &l)) {
306 * XDR unsigned short integers
309 xdr_u_short(XDR *xdrs, u_short *usp)
313 switch (xdrs->x_op) {
317 return (XDR_PUTLONG(xdrs, (long *)&l));
320 if (!XDR_GETLONG(xdrs, (long *)&l)) {
335 * XDR 16-bit integers
338 xdr_int16_t(XDR *xdrs, int16_t *int16_p)
342 switch (xdrs->x_op) {
346 return (XDR_PUTLONG(xdrs, &l));
349 if (!XDR_GETLONG(xdrs, &l)) {
352 *int16_p = (int16_t) l;
363 * XDR unsigned 16-bit integers
366 xdr_u_int16_t(XDR *xdrs, u_int16_t *u_int16_p)
370 switch (xdrs->x_op) {
373 l = (u_long) *u_int16_p;
374 return (XDR_PUTLONG(xdrs, (long *)&l));
377 if (!XDR_GETLONG(xdrs, (long *)&l)) {
380 *u_int16_p = (u_int16_t) l;
391 * XDR unsigned 16-bit integers
394 xdr_uint16_t(XDR *xdrs, uint16_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;
423 xdr_char(XDR *xdrs, char *cp)
427 i = *((unsigned char *)cp);
428 if (!xdr_u_int(xdrs, &i)) {
431 *((unsigned char *)cp) = i;
436 * XDR an unsigned char
439 xdr_u_char(XDR *xdrs, u_char *cp)
444 if (!xdr_u_int(xdrs, &u)) {
455 xdr_bool(XDR *xdrs, bool_t *bp)
459 switch (xdrs->x_op) {
462 lb = *bp ? XDR_TRUE : XDR_FALSE;
463 return (XDR_PUTLONG(xdrs, &lb));
466 if (!XDR_GETLONG(xdrs, &lb)) {
469 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
483 xdr_enum(XDR *xdrs, enum_t *ep)
485 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
488 * enums are treated as ints
490 /* LINTED */ if (sizeof (enum sizecheck) == sizeof (long)) {
491 return (xdr_long(xdrs, (long *)(void *)ep));
492 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) {
493 return (xdr_int(xdrs, (int *)(void *)ep));
494 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (short)) {
495 return (xdr_short(xdrs, (short *)(void *)ep));
503 * Allows the specification of a fixed size sequence of opaque bytes.
504 * cp points to the opaque object and cnt gives the byte length.
507 xdr_opaque(XDR *xdrs, caddr_t cp, u_int cnt)
510 static int crud[BYTES_PER_XDR_UNIT];
513 * if no data we are done
519 * round byte count to full xdr units
521 rndup = cnt % BYTES_PER_XDR_UNIT;
523 rndup = BYTES_PER_XDR_UNIT - rndup;
525 if (xdrs->x_op == XDR_DECODE) {
526 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
531 return (XDR_GETBYTES(xdrs, (caddr_t)(void *)crud, rndup));
534 if (xdrs->x_op == XDR_ENCODE) {
535 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
540 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
543 if (xdrs->x_op == XDR_FREE) {
552 * *cpp is a pointer to the bytes, *sizep is the count.
553 * If *cpp is NULL maxsize bytes are allocated
556 xdr_bytes(XDR *xdrs, char **cpp, u_int *sizep, u_int maxsize)
558 char *sp = *cpp; /* sp is the actual string pointer */
560 bool_t ret, allocated = FALSE;
563 * first deal with the length since xdr bytes are counted
565 if (! xdr_u_int(xdrs, sizep)) {
569 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
574 * now deal with the actual bytes
576 switch (xdrs->x_op) {
583 *cpp = sp = mem_alloc(nodesize);
587 warnx("xdr_bytes: out of memory");
593 ret = xdr_opaque(xdrs, sp, nodesize);
594 if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
595 if (allocated == TRUE) {
604 mem_free(sp, nodesize);
614 * Implemented here due to commonality of the object.
617 xdr_netobj(XDR *xdrs, struct netobj *np)
620 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
624 * XDR a descriminated union
625 * Support routine for discriminated unions.
626 * You create an array of xdrdiscrim structures, terminated with
627 * an entry with a null procedure pointer. The routine gets
628 * the discriminant value and then searches the array of xdrdiscrims
629 * looking for that value. It calls the procedure given in the xdrdiscrim
630 * to handle the discriminant. If there is no specific routine a default
631 * routine may be called.
632 * If there is no specific or default routine an error is returned.
635 xdr_union(XDR *xdrs, enum_t *dscmp, char *unp, const struct xdr_discrim *choices, xdrproc_t dfault)
638 * enum_t *dscmp; // enum to decide which arm to work on
639 * char *unp; // the union itself
640 * const struct xdr_discrim *choices; // [value, xdr proc] for each arm
641 * xdrproc_t dfault; // default xdr routine
647 * we deal with the discriminator; it's an enum
649 if (! xdr_enum(xdrs, dscmp)) {
655 * search choices for a value that matches the discriminator.
656 * if we find one, execute the xdr routine for that value.
658 for (; choices->proc != NULL_xdrproc_t; choices++) {
659 if (choices->value == dscm)
660 return ((*(choices->proc))(xdrs, unp));
664 * no match - execute the default xdr routine if there is one
666 return ((dfault == NULL_xdrproc_t) ? FALSE :
667 (*dfault)(xdrs, unp));
672 * Non-portable xdr primitives.
673 * Care should be taken when moving these routines to new architectures.
678 * XDR null terminated ASCII strings
679 * xdr_string deals with "C strings" - arrays of bytes that are
680 * terminated by a NULL character. The parameter cpp references a
681 * pointer to storage; If the pointer is null, then the necessary
682 * storage is allocated. The last parameter is the max allowed length
683 * of the string as specified by a protocol.
686 xdr_string(XDR *xdrs, char **cpp, u_int maxsize)
688 char *sp = *cpp; /* sp is the actual string pointer */
691 bool_t ret, allocated = FALSE;
694 * first deal with the length since xdr strings are counted-strings
696 switch (xdrs->x_op) {
699 return(TRUE); /* already free */
708 if (! xdr_u_int(xdrs, &size)) {
711 if (size > maxsize) {
717 * now deal with the actual bytes
719 switch (xdrs->x_op) {
726 *cpp = sp = mem_alloc(nodesize);
730 warnx("xdr_string: out of memory");
737 ret = xdr_opaque(xdrs, sp, size);
738 if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
739 if (allocated == TRUE) {
747 mem_free(sp, nodesize);
756 * Wrapper for xdr_string that can be called directly from
757 * routines like clnt_call
760 xdr_wrapstring(XDR *xdrs, char **cpp)
762 return xdr_string(xdrs, cpp, RPC_MAXDATASIZE);
766 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
767 * are in the "non-portable" section because they require that a `long long'
770 * --thorpej@netbsd.org, November 30, 1999
774 * XDR 64-bit integers
777 xdr_int64_t(XDR *xdrs, int64_t *llp)
781 switch (xdrs->x_op) {
783 ul[0] = (u_long)((u_int64_t)*llp >> 32) & 0xffffffff;
784 ul[1] = (u_long)((u_int64_t)*llp) & 0xffffffff;
785 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
787 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
789 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
791 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
794 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
805 * XDR unsigned 64-bit integers
808 xdr_u_int64_t(XDR *xdrs, u_int64_t *ullp)
812 switch (xdrs->x_op) {
814 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
815 ul[1] = (u_long)(*ullp) & 0xffffffff;
816 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
818 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
820 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
822 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
825 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
835 * XDR unsigned 64-bit integers
838 xdr_uint64_t(XDR *xdrs, uint64_t *ullp)
842 switch (xdrs->x_op) {
844 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
845 ul[1] = (u_long)(*ullp) & 0xffffffff;
846 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
848 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
850 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
852 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
855 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
869 xdr_hyper(XDR *xdrs, longlong_t *llp)
873 * Don't bother open-coding this; it's a fair amount of code. Just
874 * call xdr_int64_t().
876 return (xdr_int64_t(xdrs, (int64_t *)llp));
881 * XDR unsigned hypers
884 xdr_u_hyper(XDR *xdrs, u_longlong_t *ullp)
888 * Don't bother open-coding this; it's a fair amount of code. Just
889 * call xdr_u_int64_t().
891 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));
899 xdr_longlong_t(XDR *xdrs, longlong_t *llp)
903 * Don't bother open-coding this; it's a fair amount of code. Just
904 * call xdr_int64_t().
906 return (xdr_int64_t(xdrs, (int64_t *)llp));
914 xdr_u_longlong_t(XDR *xdrs, u_longlong_t *ullp)
918 * Don't bother open-coding this; it's a fair amount of code. Just
919 * call xdr_u_int64_t().
921 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));