2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)xdr.c 1.35 87/08/12
30 * @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC
31 * $NetBSD: xdr.c,v 1.22 2000/07/06 03:10:35 christos Exp $
32 * $FreeBSD: src/lib/libc/xdr/xdr.c,v 1.14 2004/10/16 06:32:43 obrien Exp $
36 * xdr.c, Generic XDR routines implementation.
38 * Copyright (C) 1986, Sun Microsystems, Inc.
40 * These are the "generic" xdr routines used to serialize and de-serialize
41 * most common data items. See xdr.h for more info on the interface to
45 #include "namespace.h"
51 #include <rpc/types.h>
53 #include "un-namespace.h"
55 typedef quad_t longlong_t; /* ANSI long long type */
56 typedef u_quad_t u_longlong_t; /* ANSI unsigned long long type */
59 * constants specific to the xdr "protocol"
61 #define XDR_FALSE ((long) 0)
62 #define XDR_TRUE ((long) 1)
63 #define LASTUNSIGNED ((u_int) 0-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;
395 xdr_char(XDR *xdrs, char *cp)
400 if (!xdr_int(xdrs, &i)) {
408 * XDR an unsigned char
411 xdr_u_char(XDR *xdrs, u_char *cp)
416 if (!xdr_u_int(xdrs, &u)) {
427 xdr_bool(XDR *xdrs, bool_t *bp)
431 switch (xdrs->x_op) {
434 lb = *bp ? XDR_TRUE : XDR_FALSE;
435 return (XDR_PUTLONG(xdrs, &lb));
438 if (!XDR_GETLONG(xdrs, &lb)) {
441 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
455 xdr_enum(XDR *xdrs, enum_t *ep)
457 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
460 * enums are treated as ints
462 /* LINTED */ if (sizeof (enum sizecheck) == sizeof (long)) {
463 return (xdr_long(xdrs, (long *)(void *)ep));
464 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) {
465 return (xdr_int(xdrs, (int *)(void *)ep));
466 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (short)) {
467 return (xdr_short(xdrs, (short *)(void *)ep));
475 * Allows the specification of a fixed size sequence of opaque bytes.
476 * cp points to the opaque object and cnt gives the byte length.
479 xdr_opaque(XDR *xdrs, caddr_t cp, u_int cnt)
482 static int crud[BYTES_PER_XDR_UNIT];
485 * if no data we are done
491 * round byte count to full xdr units
493 rndup = cnt % BYTES_PER_XDR_UNIT;
495 rndup = BYTES_PER_XDR_UNIT - rndup;
497 if (xdrs->x_op == XDR_DECODE) {
498 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
503 return (XDR_GETBYTES(xdrs, (caddr_t)(void *)crud, rndup));
506 if (xdrs->x_op == XDR_ENCODE) {
507 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
512 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
515 if (xdrs->x_op == XDR_FREE) {
524 * *cpp is a pointer to the bytes, *sizep is the count.
525 * If *cpp is NULL maxsize bytes are allocated
528 xdr_bytes(XDR *xdrs, char **cpp, u_int *sizep, u_int maxsize)
530 char *sp = *cpp; /* sp is the actual string pointer */
534 * first deal with the length since xdr bytes are counted
536 if (! xdr_u_int(xdrs, sizep)) {
540 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
545 * now deal with the actual bytes
547 switch (xdrs->x_op) {
554 *cpp = sp = mem_alloc(nodesize);
557 warnx("xdr_bytes: out of memory");
563 return (xdr_opaque(xdrs, sp, nodesize));
567 mem_free(sp, nodesize);
577 * Implemented here due to commonality of the object.
580 xdr_netobj(XDR *xdrs, struct netobj *np)
582 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
586 * XDR a descriminated union
587 * Support routine for discriminated unions.
588 * You create an array of xdrdiscrim structures, terminated with
589 * an entry with a null procedure pointer. The routine gets
590 * the discriminant value and then searches the array of xdrdiscrims
591 * looking for that value. It calls the procedure given in the xdrdiscrim
592 * to handle the discriminant. If there is no specific routine a default
593 * routine may be called.
594 * If there is no specific or default routine an error is returned.
597 * dscmp: enum to decide which ar to work on
598 * unp: the union itself
599 * choices: [value, xdr proc] for each arm
600 * dfault: default xdr routine
603 xdr_union(XDR *xdrs, enum_t *dscmp, char *unp,
604 const struct xdr_discrim *choices, xdrproc_t dfault)
609 * we deal with the discriminator; it's an enum
611 if (! xdr_enum(xdrs, dscmp)) {
617 * search choices for a value that matches the discriminator.
618 * if we find one, execute the xdr routine for that value.
620 for (; choices->proc != NULL_xdrproc_t; choices++) {
621 if (choices->value == dscm)
622 return ((*(choices->proc))(xdrs, unp));
626 * no match - execute the default xdr routine if there is one
628 return ((dfault == NULL_xdrproc_t) ? FALSE :
629 (*dfault)(xdrs, unp));
634 * Non-portable xdr primitives.
635 * Care should be taken when moving these routines to new architectures.
640 * XDR null terminated ASCII strings
641 * xdr_string deals with "C strings" - arrays of bytes that are
642 * terminated by a NULL character. The parameter cpp references a
643 * pointer to storage; If the pointer is null, then the necessary
644 * storage is allocated. The last parameter is the max allowed length
645 * of the string as specified by a protocol.
648 xdr_string(XDR *xdrs, char **cpp, u_int maxsize)
650 char *sp = *cpp; /* sp is the actual string pointer */
655 * first deal with the length since xdr strings are counted-strings
657 switch (xdrs->x_op) {
660 return(TRUE); /* already free */
669 if (! xdr_u_int(xdrs, &size)) {
672 if (size > maxsize) {
678 * now deal with the actual bytes
680 switch (xdrs->x_op) {
687 *cpp = sp = mem_alloc(nodesize);
689 warnx("xdr_string: out of memory");
696 return (xdr_opaque(xdrs, sp, size));
699 mem_free(sp, nodesize);
708 * Wrapper for xdr_string that can be called directly from
709 * routines like clnt_call
712 xdr_wrapstring(XDR *xdrs, char **cpp)
714 return xdr_string(xdrs, cpp, LASTUNSIGNED);
718 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
719 * are in the "non-portable" section because they require that a `long long'
722 * --thorpej@netbsd.org, November 30, 1999
726 * XDR 64-bit integers
729 xdr_int64_t(XDR *xdrs, int64_t *llp)
733 switch (xdrs->x_op) {
735 ul[0] = (u_long)((u_int64_t)*llp >> 32) & 0xffffffff;
736 ul[1] = (u_long)((u_int64_t)*llp) & 0xffffffff;
737 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
739 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
741 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
743 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
746 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
757 * XDR unsigned 64-bit integers
760 xdr_u_int64_t(XDR *xdrs, u_int64_t *ullp)
764 switch (xdrs->x_op) {
766 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
767 ul[1] = (u_long)(*ullp) & 0xffffffff;
768 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
770 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
772 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
774 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
777 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
787 * XDR unsigned 64-bit integers
790 xdr_uint64_t(XDR *xdrs, uint64_t *ullp)
794 switch (xdrs->x_op) {
796 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
797 ul[1] = (u_long)(*ullp) & 0xffffffff;
798 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
800 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
802 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
804 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
807 (((uint64_t)ul[0] << 32) | ((uint64_t)ul[1]));
821 xdr_hyper(XDR *xdrs, longlong_t *llp)
825 * Don't bother open-coding this; it's a fair amount of code. Just
826 * call xdr_int64_t().
828 return (xdr_int64_t(xdrs, (int64_t *)llp));
833 * XDR unsigned hypers
836 xdr_u_hyper(XDR *xdrs, u_longlong_t *ullp)
840 * Don't bother open-coding this; it's a fair amount of code. Just
841 * call xdr_u_int64_t().
843 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));
851 xdr_longlong_t(XDR *xdrs, longlong_t *llp)
855 * Don't bother open-coding this; it's a fair amount of code. Just
856 * call xdr_int64_t().
858 return (xdr_int64_t(xdrs, (int64_t *)llp));
866 xdr_u_longlong_t(XDR *xdrs, u_longlong_t *ullp)
870 * Don't bother open-coding this; it's a fair amount of code. Just
871 * call xdr_u_int64_t().
873 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));