1 /* $NetBSD: xdr.c,v 1.22 2000/07/06 03:10:35 christos Exp $ */
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char *sccsid2 = "@(#)xdr.c 1.35 87/08/12";
34 static char *sccsid = "@(#)xdr.c 2.1 88/07/29 4.0 RPCSRC";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * xdr.c, Generic XDR routines implementation.
42 * Copyright (C) 1986, Sun Microsystems, Inc.
44 * These are the "generic" xdr routines used to serialize and de-serialize
45 * most common data items. See xdr.h for more info on the interface to
49 #include "namespace.h"
55 #include <rpc/types.h>
57 #include "un-namespace.h"
59 typedef quad_t longlong_t; /* ANSI long long type */
60 typedef u_quad_t u_longlong_t; /* ANSI unsigned long long type */
63 * constants specific to the xdr "protocol"
65 #define XDR_FALSE ((long) 0)
66 #define XDR_TRUE ((long) 1)
67 #define LASTUNSIGNED ((u_int) 0-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
110 switch (xdrs->x_op) {
114 return (XDR_PUTLONG(xdrs, &l));
117 if (!XDR_GETLONG(xdrs, &l)) {
131 * XDR unsigned integers
140 switch (xdrs->x_op) {
144 return (XDR_PUTLONG(xdrs, (long *)&l));
147 if (!XDR_GETLONG(xdrs, (long *)&l)) {
163 * same as xdr_u_long - open coded to save a proc call!
170 switch (xdrs->x_op) {
172 return (XDR_PUTLONG(xdrs, lp));
174 return (XDR_GETLONG(xdrs, lp));
183 * XDR unsigned long integers
184 * same as xdr_long - open coded to save a proc call!
187 xdr_u_long(xdrs, ulp)
191 switch (xdrs->x_op) {
193 return (XDR_PUTLONG(xdrs, (long *)ulp));
195 return (XDR_GETLONG(xdrs, (long *)ulp));
205 * XDR 32-bit integers
206 * same as xdr_u_int32_t - open coded to save a proc call!
209 xdr_int32_t(xdrs, int32_p)
215 switch (xdrs->x_op) {
219 return (XDR_PUTLONG(xdrs, &l));
222 if (!XDR_GETLONG(xdrs, &l)) {
225 *int32_p = (int32_t) l;
236 * XDR unsigned 32-bit integers
237 * same as xdr_int32_t - open coded to save a proc call!
240 xdr_u_int32_t(xdrs, u_int32_p)
242 u_int32_t *u_int32_p;
246 switch (xdrs->x_op) {
249 l = (u_long) *u_int32_p;
250 return (XDR_PUTLONG(xdrs, (long *)&l));
253 if (!XDR_GETLONG(xdrs, (long *)&l)) {
256 *u_int32_p = (u_int32_t) l;
277 switch (xdrs->x_op) {
281 return (XDR_PUTLONG(xdrs, &l));
284 if (!XDR_GETLONG(xdrs, &l)) {
298 * XDR unsigned short integers
301 xdr_u_short(xdrs, usp)
307 switch (xdrs->x_op) {
311 return (XDR_PUTLONG(xdrs, (long *)&l));
314 if (!XDR_GETLONG(xdrs, (long *)&l)) {
329 * XDR 16-bit integers
332 xdr_int16_t(xdrs, int16_p)
338 switch (xdrs->x_op) {
342 return (XDR_PUTLONG(xdrs, &l));
345 if (!XDR_GETLONG(xdrs, &l)) {
348 *int16_p = (int16_t) l;
359 * XDR unsigned 16-bit integers
362 xdr_u_int16_t(xdrs, u_int16_p)
364 u_int16_t *u_int16_p;
368 switch (xdrs->x_op) {
371 l = (u_long) *u_int16_p;
372 return (XDR_PUTLONG(xdrs, (long *)&l));
375 if (!XDR_GETLONG(xdrs, (long *)&l)) {
378 *u_int16_p = (u_int16_t) l;
400 if (!xdr_int(xdrs, &i)) {
408 * XDR an unsigned char
418 if (!xdr_u_int(xdrs, &u)) {
435 switch (xdrs->x_op) {
438 lb = *bp ? XDR_TRUE : XDR_FALSE;
439 return (XDR_PUTLONG(xdrs, &lb));
442 if (!XDR_GETLONG(xdrs, &lb)) {
445 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
463 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
466 * enums are treated as ints
468 /* LINTED */ if (sizeof (enum sizecheck) == sizeof (long)) {
469 return (xdr_long(xdrs, (long *)(void *)ep));
470 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) {
471 return (xdr_int(xdrs, (int *)(void *)ep));
472 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (short)) {
473 return (xdr_short(xdrs, (short *)(void *)ep));
481 * Allows the specification of a fixed size sequence of opaque bytes.
482 * cp points to the opaque object and cnt gives the byte length.
485 xdr_opaque(xdrs, cp, cnt)
491 static int crud[BYTES_PER_XDR_UNIT];
494 * if no data we are done
500 * round byte count to full xdr units
502 rndup = cnt % BYTES_PER_XDR_UNIT;
504 rndup = BYTES_PER_XDR_UNIT - rndup;
506 if (xdrs->x_op == XDR_DECODE) {
507 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
512 return (XDR_GETBYTES(xdrs, (caddr_t)(void *)crud, rndup));
515 if (xdrs->x_op == XDR_ENCODE) {
516 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
521 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
524 if (xdrs->x_op == XDR_FREE) {
533 * *cpp is a pointer to the bytes, *sizep is the count.
534 * If *cpp is NULL maxsize bytes are allocated
537 xdr_bytes(xdrs, cpp, sizep, maxsize)
543 char *sp = *cpp; /* sp is the actual string pointer */
547 * first deal with the length since xdr bytes are counted
549 if (! xdr_u_int(xdrs, sizep)) {
553 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
558 * now deal with the actual bytes
560 switch (xdrs->x_op) {
567 *cpp = sp = mem_alloc(nodesize);
570 warnx("xdr_bytes: out of memory");
576 return (xdr_opaque(xdrs, sp, nodesize));
580 mem_free(sp, nodesize);
590 * Implemented here due to commonality of the object.
598 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
602 * XDR a descriminated union
603 * Support routine for discriminated unions.
604 * You create an array of xdrdiscrim structures, terminated with
605 * an entry with a null procedure pointer. The routine gets
606 * the discriminant value and then searches the array of xdrdiscrims
607 * looking for that value. It calls the procedure given in the xdrdiscrim
608 * to handle the discriminant. If there is no specific routine a default
609 * routine may be called.
610 * If there is no specific or default routine an error is returned.
613 xdr_union(xdrs, dscmp, unp, choices, dfault)
615 enum_t *dscmp; /* enum to decide which arm to work on */
616 char *unp; /* the union itself */
617 const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
618 xdrproc_t dfault; /* default xdr routine */
623 * we deal with the discriminator; it's an enum
625 if (! xdr_enum(xdrs, dscmp)) {
631 * search choices for a value that matches the discriminator.
632 * if we find one, execute the xdr routine for that value.
634 for (; choices->proc != NULL_xdrproc_t; choices++) {
635 if (choices->value == dscm)
636 return ((*(choices->proc))(xdrs, unp));
640 * no match - execute the default xdr routine if there is one
642 return ((dfault == NULL_xdrproc_t) ? FALSE :
643 (*dfault)(xdrs, unp));
648 * Non-portable xdr primitives.
649 * Care should be taken when moving these routines to new architectures.
654 * XDR null terminated ASCII strings
655 * xdr_string deals with "C strings" - arrays of bytes that are
656 * terminated by a NULL character. The parameter cpp references a
657 * pointer to storage; If the pointer is null, then the necessary
658 * storage is allocated. The last parameter is the max allowed length
659 * of the string as specified by a protocol.
662 xdr_string(xdrs, cpp, maxsize)
667 char *sp = *cpp; /* sp is the actual string pointer */
672 * first deal with the length since xdr strings are counted-strings
674 switch (xdrs->x_op) {
677 return(TRUE); /* already free */
686 if (! xdr_u_int(xdrs, &size)) {
689 if (size > maxsize) {
695 * now deal with the actual bytes
697 switch (xdrs->x_op) {
704 *cpp = sp = mem_alloc(nodesize);
706 warnx("xdr_string: out of memory");
713 return (xdr_opaque(xdrs, sp, size));
716 mem_free(sp, nodesize);
725 * Wrapper for xdr_string that can be called directly from
726 * routines like clnt_call
729 xdr_wrapstring(xdrs, cpp)
733 return xdr_string(xdrs, cpp, LASTUNSIGNED);
737 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
738 * are in the "non-portable" section because they require that a `long long'
741 * --thorpej@netbsd.org, November 30, 1999
745 * XDR 64-bit integers
748 xdr_int64_t(xdrs, llp)
754 switch (xdrs->x_op) {
756 ul[0] = (u_long)((u_int64_t)*llp >> 32) & 0xffffffff;
757 ul[1] = (u_long)((u_int64_t)*llp) & 0xffffffff;
758 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
760 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
762 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
764 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
767 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
778 * XDR unsigned 64-bit integers
781 xdr_u_int64_t(xdrs, ullp)
787 switch (xdrs->x_op) {
789 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
790 ul[1] = (u_long)(*ullp) & 0xffffffff;
791 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
793 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
795 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
797 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
800 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
820 * Don't bother open-coding this; it's a fair amount of code. Just
821 * call xdr_int64_t().
823 return (xdr_int64_t(xdrs, (int64_t *)llp));
828 * XDR unsigned hypers
831 xdr_u_hyper(xdrs, ullp)
837 * Don't bother open-coding this; it's a fair amount of code. Just
838 * call xdr_u_int64_t().
840 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));
848 xdr_longlong_t(xdrs, llp)
854 * Don't bother open-coding this; it's a fair amount of code. Just
855 * call xdr_int64_t().
857 return (xdr_int64_t(xdrs, (int64_t *)llp));
865 xdr_u_longlong_t(xdrs, ullp)
871 * Don't bother open-coding this; it's a fair amount of code. Just
872 * call xdr_u_int64_t().
874 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));