2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
32 * xdr.c, Generic XDR routines implementation.
34 * Copyright (C) 1986, Sun Microsystems, Inc.
36 * These are the "generic" xdr routines used to serialize and de-serialize
37 * most common data items. See xdr.h for more info on the interface to
46 #include <rpc/types.h>
49 typedef quad_t longlong_t; /* ANSI long long type */
50 typedef u_quad_t u_longlong_t; /* ANSI unsigned long long type */
53 * constants specific to the xdr "protocol"
55 #define XDR_FALSE ((long) 0)
56 #define XDR_TRUE ((long) 1)
57 #define LASTUNSIGNED ((u_int) 0-1)
62 static const char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
65 * Free a data structure using XDR
66 * Not a filter, but a convenient utility nonetheless
100 switch (xdrs->x_op) {
104 return (XDR_PUTLONG(xdrs, &l));
107 if (!XDR_GETLONG(xdrs, &l)) {
121 * XDR unsigned integers
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!
160 switch (xdrs->x_op) {
162 return (XDR_PUTLONG(xdrs, lp));
164 return (XDR_GETLONG(xdrs, lp));
173 * XDR unsigned long integers
174 * same as xdr_long - open coded to save a proc call!
177 xdr_u_long(xdrs, 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(xdrs, int32_p)
205 switch (xdrs->x_op) {
209 return (XDR_PUTLONG(xdrs, &l));
212 if (!XDR_GETLONG(xdrs, &l)) {
215 *int32_p = (int32_t) l;
226 * XDR unsigned 32-bit integers
227 * same as xdr_int32_t - open coded to save a proc call!
230 xdr_u_int32_t(xdrs, u_int32_p)
232 u_int32_t *u_int32_p;
236 switch (xdrs->x_op) {
239 l = (u_long) *u_int32_p;
240 return (XDR_PUTLONG(xdrs, (long *)&l));
243 if (!XDR_GETLONG(xdrs, (long *)&l)) {
246 *u_int32_p = (u_int32_t) l;
267 switch (xdrs->x_op) {
271 return (XDR_PUTLONG(xdrs, &l));
274 if (!XDR_GETLONG(xdrs, &l)) {
288 * XDR unsigned short integers
291 xdr_u_short(xdrs, usp)
297 switch (xdrs->x_op) {
301 return (XDR_PUTLONG(xdrs, (long *)&l));
304 if (!XDR_GETLONG(xdrs, (long *)&l)) {
319 * XDR 16-bit integers
322 xdr_int16_t(xdrs, int16_p)
328 switch (xdrs->x_op) {
332 return (XDR_PUTLONG(xdrs, &l));
335 if (!XDR_GETLONG(xdrs, &l)) {
338 *int16_p = (int16_t) l;
349 * XDR unsigned 16-bit integers
352 xdr_u_int16_t(xdrs, u_int16_p)
354 u_int16_t *u_int16_p;
358 switch (xdrs->x_op) {
361 l = (u_long) *u_int16_p;
362 return (XDR_PUTLONG(xdrs, (long *)&l));
365 if (!XDR_GETLONG(xdrs, (long *)&l)) {
368 *u_int16_p = (u_int16_t) l;
390 if (!xdr_int(xdrs, &i)) {
398 * XDR an unsigned char
408 if (!xdr_u_int(xdrs, &u)) {
425 switch (xdrs->x_op) {
428 lb = *bp ? XDR_TRUE : XDR_FALSE;
429 return (XDR_PUTLONG(xdrs, &lb));
432 if (!XDR_GETLONG(xdrs, &lb)) {
435 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
453 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
456 * enums are treated as ints
458 /* LINTED */ if (sizeof (enum sizecheck) == sizeof (long)) {
459 return (xdr_long(xdrs, (long *)(void *)ep));
460 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) {
461 return (xdr_int(xdrs, (int *)(void *)ep));
462 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (short)) {
463 return (xdr_short(xdrs, (short *)(void *)ep));
471 * Allows the specification of a fixed size sequence of opaque bytes.
472 * cp points to the opaque object and cnt gives the byte length.
475 xdr_opaque(xdrs, cp, cnt)
481 static int crud[BYTES_PER_XDR_UNIT];
484 * if no data we are done
490 * round byte count to full xdr units
492 rndup = cnt % BYTES_PER_XDR_UNIT;
494 rndup = BYTES_PER_XDR_UNIT - rndup;
496 if (xdrs->x_op == XDR_DECODE) {
497 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
502 return (XDR_GETBYTES(xdrs, (caddr_t)(void *)crud, rndup));
505 if (xdrs->x_op == XDR_ENCODE) {
506 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
511 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
514 if (xdrs->x_op == XDR_FREE) {
523 * *cpp is a pointer to the bytes, *sizep is the count.
524 * If *cpp is NULL maxsize bytes are allocated
527 xdr_bytes(xdrs, cpp, sizep, maxsize)
533 char *sp = *cpp; /* sp is the actual string pointer */
537 * first deal with the length since xdr bytes are counted
539 if (! xdr_u_int(xdrs, sizep)) {
543 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
548 * now deal with the actual bytes
550 switch (xdrs->x_op) {
557 *cpp = sp = mem_alloc(nodesize);
560 warnx("xdr_bytes: out of memory");
566 return (xdr_opaque(xdrs, sp, nodesize));
570 mem_free(sp, nodesize);
580 * Implemented here due to commonality of the object.
588 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
592 * XDR a descriminated union
593 * Support routine for discriminated unions.
594 * You create an array of xdrdiscrim structures, terminated with
595 * an entry with a null procedure pointer. The routine gets
596 * the discriminant value and then searches the array of xdrdiscrims
597 * looking for that value. It calls the procedure given in the xdrdiscrim
598 * to handle the discriminant. If there is no specific routine a default
599 * routine may be called.
600 * If there is no specific or default routine an error is returned.
603 xdr_union(xdrs, dscmp, unp, choices, dfault)
605 enum_t *dscmp; /* enum to decide which arm to work on */
606 char *unp; /* the union itself */
607 const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
608 xdrproc_t dfault; /* default xdr routine */
613 * we deal with the discriminator; it's an enum
615 if (! xdr_enum(xdrs, dscmp)) {
621 * search choices for a value that matches the discriminator.
622 * if we find one, execute the xdr routine for that value.
624 for (; choices->proc != NULL_xdrproc_t; choices++) {
625 if (choices->value == dscm)
626 return ((*(choices->proc))(xdrs, unp));
630 * no match - execute the default xdr routine if there is one
632 return ((dfault == NULL_xdrproc_t) ? FALSE :
633 (*dfault)(xdrs, unp));
638 * Non-portable xdr primitives.
639 * Care should be taken when moving these routines to new architectures.
644 * XDR null terminated ASCII strings
645 * xdr_string deals with "C strings" - arrays of bytes that are
646 * terminated by a NULL character. The parameter cpp references a
647 * pointer to storage; If the pointer is null, then the necessary
648 * storage is allocated. The last parameter is the max allowed length
649 * of the string as specified by a protocol.
652 xdr_string(xdrs, cpp, maxsize)
657 char *sp = *cpp; /* sp is the actual string pointer */
662 * first deal with the length since xdr strings are counted-strings
664 switch (xdrs->x_op) {
667 return(TRUE); /* already free */
678 if (! xdr_u_int(xdrs, &size)) {
681 if (size > maxsize) {
686 /* This means an overflow. It a bug in the caller which
687 * provided a too large maxsize but nevertheless catch it
694 * now deal with the actual bytes
696 switch (xdrs->x_op) {
700 *cpp = sp = mem_alloc(nodesize);
702 warnx("xdr_string: out of memory");
709 return (xdr_opaque(xdrs, sp, size));
712 mem_free(sp, nodesize);
721 * Wrapper for xdr_string that can be called directly from
722 * routines like clnt_call
725 xdr_wrapstring(xdrs, cpp)
729 return xdr_string(xdrs, cpp, LASTUNSIGNED);
733 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
734 * are in the "non-portable" section because they require that a `long long'
737 * --thorpej@netbsd.org, November 30, 1999
741 * XDR 64-bit integers
744 xdr_int64_t(xdrs, llp)
750 switch (xdrs->x_op) {
752 ul[0] = (u_long)((u_int64_t)*llp >> 32) & 0xffffffff;
753 ul[1] = (u_long)((u_int64_t)*llp) & 0xffffffff;
754 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
756 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
758 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
760 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
763 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
774 * XDR unsigned 64-bit integers
777 xdr_u_int64_t(xdrs, ullp)
783 switch (xdrs->x_op) {
785 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
786 ul[1] = (u_long)(*ullp) & 0xffffffff;
787 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
789 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
791 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
793 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
796 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
816 * Don't bother open-coding this; it's a fair amount of code. Just
817 * call xdr_int64_t().
819 return (xdr_int64_t(xdrs, (int64_t *)llp));
824 * XDR unsigned hypers
827 xdr_u_hyper(xdrs, ullp)
833 * Don't bother open-coding this; it's a fair amount of code. Just
834 * call xdr_u_int64_t().
836 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));
844 xdr_longlong_t(xdrs, llp)
850 * Don't bother open-coding this; it's a fair amount of code. Just
851 * call xdr_int64_t().
853 return (xdr_int64_t(xdrs, (int64_t *)llp));
861 xdr_u_longlong_t(xdrs, ullp)
867 * Don't bother open-coding this; it's a fair amount of code. Just
868 * call xdr_u_int64_t().
870 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));