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 <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
55 #include <rpc/rpc_com.h>
56 #include <rpc/types.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)
71 static const char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
74 * Free a data structure using XDR
75 * Not a filter, but a convenient utility nonetheless
78 xdr_free(xdrproc_t proc, void *objp)
101 xdr_int(XDR *xdrs, int *ip)
105 switch (xdrs->x_op) {
109 return (XDR_PUTLONG(xdrs, &l));
112 if (!XDR_GETLONG(xdrs, &l)) {
126 * XDR unsigned integers
129 xdr_u_int(XDR *xdrs, u_int *up)
133 switch (xdrs->x_op) {
137 return (XDR_PUTLONG(xdrs, (long *)&l));
140 if (!XDR_GETLONG(xdrs, (long *)&l)) {
156 * same as xdr_u_long - open coded to save a proc call!
159 xdr_long(XDR *xdrs, long *lp)
161 switch (xdrs->x_op) {
163 return (XDR_PUTLONG(xdrs, lp));
165 return (XDR_GETLONG(xdrs, lp));
174 * XDR unsigned long integers
175 * same as xdr_long - open coded to save a proc call!
178 xdr_u_long(XDR *xdrs, u_long *ulp)
180 switch (xdrs->x_op) {
182 return (XDR_PUTLONG(xdrs, (long *)ulp));
184 return (XDR_GETLONG(xdrs, (long *)ulp));
194 * XDR 32-bit integers
195 * same as xdr_uint32_t - open coded to save a proc call!
198 xdr_int32_t(XDR *xdrs, int32_t *int32_p)
202 switch (xdrs->x_op) {
206 return (XDR_PUTLONG(xdrs, &l));
209 if (!XDR_GETLONG(xdrs, &l)) {
212 *int32_p = (int32_t) l;
223 * XDR unsigned 32-bit integers
224 * same as xdr_int32_t - open coded to save a proc call!
227 xdr_uint32_t(XDR *xdrs, uint32_t *uint32_p)
231 switch (xdrs->x_op) {
234 l = (u_long) *uint32_p;
235 return (XDR_PUTLONG(xdrs, (long *)&l));
238 if (!XDR_GETLONG(xdrs, (long *)&l)) {
241 *uint32_p = (uint32_t) l;
256 xdr_short(XDR *xdrs, short *sp)
260 switch (xdrs->x_op) {
264 return (XDR_PUTLONG(xdrs, &l));
267 if (!XDR_GETLONG(xdrs, &l)) {
281 * XDR unsigned short integers
284 xdr_u_short(XDR *xdrs, u_short *usp)
288 switch (xdrs->x_op) {
292 return (XDR_PUTLONG(xdrs, (long *)&l));
295 if (!XDR_GETLONG(xdrs, (long *)&l)) {
310 * XDR 16-bit integers
313 xdr_int16_t(XDR *xdrs, int16_t *int16_p)
317 switch (xdrs->x_op) {
321 return (XDR_PUTLONG(xdrs, &l));
324 if (!XDR_GETLONG(xdrs, &l)) {
327 *int16_p = (int16_t) l;
338 * XDR unsigned 16-bit integers
341 xdr_uint16_t(XDR *xdrs, uint16_t *uint16_p)
345 switch (xdrs->x_op) {
348 l = (u_long) *uint16_p;
349 return (XDR_PUTLONG(xdrs, (long *)&l));
352 if (!XDR_GETLONG(xdrs, (long *)&l)) {
355 *uint16_p = (uint16_t) l;
370 xdr_char(XDR *xdrs, char *cp)
375 if (!xdr_int(xdrs, &i)) {
383 * XDR an unsigned char
386 xdr_u_char(XDR *xdrs, u_char *cp)
391 if (!xdr_u_int(xdrs, &u)) {
402 xdr_bool(XDR *xdrs, bool_t *bp)
406 switch (xdrs->x_op) {
409 lb = *bp ? XDR_TRUE : XDR_FALSE;
410 return (XDR_PUTLONG(xdrs, &lb));
413 if (!XDR_GETLONG(xdrs, &lb)) {
416 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
430 xdr_enum(XDR *xdrs, enum_t *ep)
432 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
435 * enums are treated as ints
437 /* LINTED */ if (sizeof (enum sizecheck) == sizeof (long)) {
438 return (xdr_long(xdrs, (long *)(void *)ep));
439 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) {
440 return (xdr_int(xdrs, (int *)(void *)ep));
441 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (short)) {
442 return (xdr_short(xdrs, (short *)(void *)ep));
450 * Allows the specification of a fixed size sequence of opaque bytes.
451 * cp points to the opaque object and cnt gives the byte length.
454 xdr_opaque(XDR *xdrs, caddr_t cp, u_int cnt)
457 static int crud[BYTES_PER_XDR_UNIT];
460 * if no data we are done
466 * round byte count to full xdr units
468 rndup = cnt % BYTES_PER_XDR_UNIT;
470 rndup = BYTES_PER_XDR_UNIT - rndup;
472 if (xdrs->x_op == XDR_DECODE) {
473 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
478 return (XDR_GETBYTES(xdrs, (caddr_t)(void *)crud, rndup));
481 if (xdrs->x_op == XDR_ENCODE) {
482 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
487 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
490 if (xdrs->x_op == XDR_FREE) {
499 * *cpp is a pointer to the bytes, *sizep is the count.
500 * If *cpp is NULL maxsize bytes are allocated
503 xdr_bytes(XDR *xdrs, char **cpp, u_int *sizep, u_int maxsize)
505 char *sp = *cpp; /* sp is the actual string pointer */
507 bool_t ret, allocated = FALSE;
510 * first deal with the length since xdr bytes are counted
512 if (! xdr_u_int(xdrs, sizep)) {
516 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
521 * now deal with the actual bytes
523 switch (xdrs->x_op) {
530 *cpp = sp = mem_alloc(nodesize);
534 printf("xdr_bytes: out of memory");
540 ret = xdr_opaque(xdrs, sp, nodesize);
541 if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
542 if (allocated == TRUE) {
543 mem_free(sp, nodesize);
551 mem_free(sp, nodesize);
561 * Implemented here due to commonality of the object.
564 xdr_netobj(XDR *xdrs, struct netobj *np)
567 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
571 * XDR a descriminated union
572 * Support routine for discriminated unions.
573 * You create an array of xdrdiscrim structures, terminated with
574 * an entry with a null procedure pointer. The routine gets
575 * the discriminant value and then searches the array of xdrdiscrims
576 * looking for that value. It calls the procedure given in the xdrdiscrim
577 * to handle the discriminant. If there is no specific routine a default
578 * routine may be called.
579 * If there is no specific or default routine an error is returned.
583 enum_t *dscmp, /* enum to decide which arm to work on */
584 char *unp, /* the union itself */
585 const struct xdr_discrim *choices, /* [value, xdr proc] for each arm */
586 xdrproc_t dfault) /* default xdr routine */
591 * we deal with the discriminator; it's an enum
593 if (! xdr_enum(xdrs, dscmp)) {
599 * search choices for a value that matches the discriminator.
600 * if we find one, execute the xdr routine for that value.
602 for (; choices->proc != NULL_xdrproc_t; choices++) {
603 if (choices->value == dscm)
604 return ((*(choices->proc))(xdrs, unp));
608 * no match - execute the default xdr routine if there is one
610 return ((dfault == NULL_xdrproc_t) ? FALSE :
611 (*dfault)(xdrs, unp));
616 * Non-portable xdr primitives.
617 * Care should be taken when moving these routines to new architectures.
622 * XDR null terminated ASCII strings
623 * xdr_string deals with "C strings" - arrays of bytes that are
624 * terminated by a NULL character. The parameter cpp references a
625 * pointer to storage; If the pointer is null, then the necessary
626 * storage is allocated. The last parameter is the max allowed length
627 * of the string as specified by a protocol.
630 xdr_string(XDR *xdrs, char **cpp, u_int maxsize)
632 char *sp = *cpp; /* sp is the actual string pointer */
635 bool_t ret, allocated = FALSE;
638 * first deal with the length since xdr strings are counted-strings
640 switch (xdrs->x_op) {
643 return(TRUE); /* already free */
652 if (! xdr_u_int(xdrs, &size)) {
655 if (size > maxsize) {
661 * now deal with the actual bytes
663 switch (xdrs->x_op) {
670 *cpp = sp = mem_alloc(nodesize);
674 printf("xdr_string: out of memory");
681 ret = xdr_opaque(xdrs, sp, size);
682 if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
683 if (allocated == TRUE) {
684 mem_free(sp, nodesize);
691 mem_free(sp, nodesize);
700 * Wrapper for xdr_string that can be called directly from
701 * routines like clnt_call
704 xdr_wrapstring(XDR *xdrs, char **cpp)
706 return xdr_string(xdrs, cpp, RPC_MAXDATASIZE);
710 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
711 * are in the "non-portable" section because they require that a `long long'
714 * --thorpej@netbsd.org, November 30, 1999
718 * XDR 64-bit integers
721 xdr_int64_t(XDR *xdrs, int64_t *llp)
725 switch (xdrs->x_op) {
727 ul[0] = (u_long)((uint64_t)*llp >> 32) & 0xffffffff;
728 ul[1] = (u_long)((uint64_t)*llp) & 0xffffffff;
729 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
731 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
733 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
735 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
738 (((uint64_t)ul[0] << 32) | ((uint64_t)ul[1]));
749 * XDR unsigned 64-bit integers
752 xdr_uint64_t(XDR *xdrs, uint64_t *ullp)
756 switch (xdrs->x_op) {
758 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
759 ul[1] = (u_long)(*ullp) & 0xffffffff;
760 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
762 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
764 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
766 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
769 (((uint64_t)ul[0] << 32) | ((uint64_t)ul[1]));
783 xdr_hyper(XDR *xdrs, longlong_t *llp)
787 * Don't bother open-coding this; it's a fair amount of code. Just
788 * call xdr_int64_t().
790 return (xdr_int64_t(xdrs, (int64_t *)llp));
795 * XDR unsigned hypers
798 xdr_u_hyper(XDR *xdrs, u_longlong_t *ullp)
802 * Don't bother open-coding this; it's a fair amount of code. Just
803 * call xdr_uint64_t().
805 return (xdr_uint64_t(xdrs, (uint64_t *)ullp));
813 xdr_longlong_t(XDR *xdrs, longlong_t *llp)
817 * Don't bother open-coding this; it's a fair amount of code. Just
818 * call xdr_int64_t().
820 return (xdr_int64_t(xdrs, (int64_t *)llp));
828 xdr_u_longlong_t(XDR *xdrs, u_longlong_t *ullp)
832 * Don't bother open-coding this; it's a fair amount of code. Just
833 * call xdr_uint64_t().
835 return (xdr_uint64_t(xdrs, (uint64_t *)ullp));