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>
38 * xdr.c, Generic XDR routines implementation.
40 * Copyright (C) 1986, Sun Microsystems, Inc.
42 * These are the "generic" xdr routines used to serialize and de-serialize
43 * most common data items. See xdr.h for more info on the interface to
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
54 #include <rpc/rpc_com.h>
55 #include <rpc/types.h>
58 typedef quad_t longlong_t; /* ANSI long long type */
59 typedef u_quad_t u_longlong_t; /* ANSI unsigned long long type */
62 * constants specific to the xdr "protocol"
64 #define XDR_FALSE ((long) 0)
65 #define XDR_TRUE ((long) 1)
67 MALLOC_DEFINE(M_RPC, "rpc", "Remote Procedure Call");
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
79 xdr_free(xdrproc_t proc, void *objp)
101 xdr_int(XDR *xdrs, int *ip)
105 switch (xdrs->x_op) {
108 return (XDR_PUTLONG(xdrs, &l));
111 if (!XDR_GETLONG(xdrs, &l)) {
125 * XDR unsigned integers
128 xdr_u_int(XDR *xdrs, u_int *up)
132 switch (xdrs->x_op) {
135 return (XDR_PUTLONG(xdrs, (long *)&l));
138 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));
190 * XDR 32-bit integers
191 * same as xdr_uint32_t - open coded to save a proc call!
194 xdr_int32_t(XDR *xdrs, int32_t *int32_p)
198 switch (xdrs->x_op) {
201 return (XDR_PUTLONG(xdrs, &l));
204 if (!XDR_GETLONG(xdrs, &l)) {
207 *int32_p = (int32_t) l;
218 * XDR unsigned 32-bit integers
219 * same as xdr_int32_t - open coded to save a proc call!
222 xdr_uint32_t(XDR *xdrs, uint32_t *uint32_p)
226 switch (xdrs->x_op) {
228 l = (u_long) *uint32_p;
229 return (XDR_PUTLONG(xdrs, (long *)&l));
232 if (!XDR_GETLONG(xdrs, (long *)&l)) {
235 *uint32_p = (uint32_t) l;
249 xdr_short(XDR *xdrs, short *sp)
253 switch (xdrs->x_op) {
256 return (XDR_PUTLONG(xdrs, &l));
259 if (!XDR_GETLONG(xdrs, &l)) {
273 * XDR unsigned short integers
276 xdr_u_short(XDR *xdrs, u_short *usp)
280 switch (xdrs->x_op) {
283 return (XDR_PUTLONG(xdrs, (long *)&l));
286 if (!XDR_GETLONG(xdrs, (long *)&l)) {
300 * XDR 16-bit integers
303 xdr_int16_t(XDR *xdrs, int16_t *int16_p)
307 switch (xdrs->x_op) {
310 return (XDR_PUTLONG(xdrs, &l));
313 if (!XDR_GETLONG(xdrs, &l)) {
316 *int16_p = (int16_t) l;
327 * XDR unsigned 16-bit integers
330 xdr_uint16_t(XDR *xdrs, uint16_t *uint16_p)
334 switch (xdrs->x_op) {
336 l = (u_long) *uint16_p;
337 return (XDR_PUTLONG(xdrs, (long *)&l));
340 if (!XDR_GETLONG(xdrs, (long *)&l)) {
343 *uint16_p = (uint16_t) l;
357 xdr_char(XDR *xdrs, char *cp)
361 i = *((unsigned char *)cp);
362 if (!xdr_u_int(xdrs, &i)) {
365 *((unsigned char *)cp) = i;
370 * XDR an unsigned char
373 xdr_u_char(XDR *xdrs, u_char *cp)
378 if (!xdr_u_int(xdrs, &u)) {
389 xdr_bool(XDR *xdrs, bool_t *bp)
393 switch (xdrs->x_op) {
395 lb = *bp ? XDR_TRUE : XDR_FALSE;
396 return (XDR_PUTLONG(xdrs, &lb));
399 if (!XDR_GETLONG(xdrs, &lb)) {
402 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
416 xdr_enum(XDR *xdrs, enum_t *ep)
418 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
421 * enums are treated as ints
423 /* LINTED */ if (sizeof (enum sizecheck) == sizeof (long)) {
424 return (xdr_long(xdrs, (long *)(void *)ep));
425 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) {
426 return (xdr_int(xdrs, (int *)(void *)ep));
427 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (short)) {
428 return (xdr_short(xdrs, (short *)(void *)ep));
436 * Allows the specification of a fixed size sequence of opaque bytes.
437 * cp points to the opaque object and cnt gives the byte length.
440 xdr_opaque(XDR *xdrs, caddr_t cp, u_int cnt)
443 static int crud[BYTES_PER_XDR_UNIT];
446 * if no data we are done
452 * round byte count to full xdr units
454 rndup = cnt % BYTES_PER_XDR_UNIT;
456 rndup = BYTES_PER_XDR_UNIT - rndup;
458 if (xdrs->x_op == XDR_DECODE) {
459 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
464 return (XDR_GETBYTES(xdrs, (caddr_t)(void *)crud, rndup));
467 if (xdrs->x_op == XDR_ENCODE) {
468 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
473 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
476 if (xdrs->x_op == XDR_FREE) {
485 * *cpp is a pointer to the bytes, *sizep is the count.
486 * If *cpp is NULL maxsize bytes are allocated
489 xdr_bytes(XDR *xdrs, char **cpp, u_int *sizep, u_int maxsize)
491 char *sp = *cpp; /* sp is the actual string pointer */
493 bool_t ret, allocated = FALSE;
496 * first deal with the length since xdr bytes are counted
498 if (! xdr_u_int(xdrs, sizep)) {
502 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
507 * now deal with the actual bytes
509 switch (xdrs->x_op) {
515 *cpp = sp = mem_alloc(nodesize);
519 printf("xdr_bytes: out of memory");
525 ret = xdr_opaque(xdrs, sp, nodesize);
526 if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
527 if (allocated == TRUE) {
528 mem_free(sp, nodesize);
536 mem_free(sp, nodesize);
546 * Implemented here due to commonality of the object.
549 xdr_netobj(XDR *xdrs, struct netobj *np)
552 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
556 * XDR a descriminated union
557 * Support routine for discriminated unions.
558 * You create an array of xdrdiscrim structures, terminated with
559 * an entry with a null procedure pointer. The routine gets
560 * the discriminant value and then searches the array of xdrdiscrims
561 * looking for that value. It calls the procedure given in the xdrdiscrim
562 * to handle the discriminant. If there is no specific routine a default
563 * routine may be called.
564 * If there is no specific or default routine an error is returned.
568 enum_t *dscmp, /* enum to decide which arm to work on */
569 char *unp, /* the union itself */
570 const struct xdr_discrim *choices, /* [value, xdr proc] for each arm */
571 xdrproc_t dfault) /* default xdr routine */
576 * we deal with the discriminator; it's an enum
578 if (! xdr_enum(xdrs, dscmp)) {
584 * search choices for a value that matches the discriminator.
585 * if we find one, execute the xdr routine for that value.
587 for (; choices->proc != NULL_xdrproc_t; choices++) {
588 if (choices->value == dscm)
589 return ((*(choices->proc))(xdrs, unp));
593 * no match - execute the default xdr routine if there is one
595 return ((dfault == NULL_xdrproc_t) ? FALSE :
596 (*dfault)(xdrs, unp));
600 * Non-portable xdr primitives.
601 * Care should be taken when moving these routines to new architectures.
605 * XDR null terminated ASCII strings
606 * xdr_string deals with "C strings" - arrays of bytes that are
607 * terminated by a NULL character. The parameter cpp references a
608 * pointer to storage; If the pointer is null, then the necessary
609 * storage is allocated. The last parameter is the max allowed length
610 * of the string as specified by a protocol.
613 xdr_string(XDR *xdrs, char **cpp, u_int maxsize)
615 char *sp = *cpp; /* sp is the actual string pointer */
618 bool_t ret, allocated = FALSE;
621 * first deal with the length since xdr strings are counted-strings
623 switch (xdrs->x_op) {
626 return(TRUE); /* already free */
635 if (! xdr_u_int(xdrs, &size)) {
638 if (size > maxsize) {
644 * now deal with the actual bytes
646 switch (xdrs->x_op) {
652 *cpp = sp = mem_alloc(nodesize);
656 printf("xdr_string: out of memory");
663 ret = xdr_opaque(xdrs, sp, size);
664 if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
665 if (allocated == TRUE) {
666 mem_free(sp, nodesize);
673 mem_free(sp, nodesize);
682 * Wrapper for xdr_string that can be called directly from
683 * routines like clnt_call
686 xdr_wrapstring(XDR *xdrs, char **cpp)
688 return xdr_string(xdrs, cpp, RPC_MAXDATASIZE);
692 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
693 * are in the "non-portable" section because they require that a `long long'
696 * --thorpej@netbsd.org, November 30, 1999
700 * XDR 64-bit integers
703 xdr_int64_t(XDR *xdrs, int64_t *llp)
707 switch (xdrs->x_op) {
709 ul[0] = (u_long)((uint64_t)*llp >> 32) & 0xffffffff;
710 ul[1] = (u_long)((uint64_t)*llp) & 0xffffffff;
711 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
713 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
715 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
717 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
720 (((uint64_t)ul[0] << 32) | ((uint64_t)ul[1]));
730 * XDR unsigned 64-bit integers
733 xdr_uint64_t(XDR *xdrs, uint64_t *ullp)
737 switch (xdrs->x_op) {
739 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
740 ul[1] = (u_long)(*ullp) & 0xffffffff;
741 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
743 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
745 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
747 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
750 (((uint64_t)ul[0] << 32) | ((uint64_t)ul[1]));
763 xdr_hyper(XDR *xdrs, longlong_t *llp)
767 * Don't bother open-coding this; it's a fair amount of code. Just
768 * call xdr_int64_t().
770 return (xdr_int64_t(xdrs, (int64_t *)llp));
774 * XDR unsigned hypers
777 xdr_u_hyper(XDR *xdrs, u_longlong_t *ullp)
781 * Don't bother open-coding this; it's a fair amount of code. Just
782 * call xdr_uint64_t().
784 return (xdr_uint64_t(xdrs, (uint64_t *)ullp));
791 xdr_longlong_t(XDR *xdrs, longlong_t *llp)
795 * Don't bother open-coding this; it's a fair amount of code. Just
796 * call xdr_int64_t().
798 return (xdr_int64_t(xdrs, (int64_t *)llp));
805 xdr_u_longlong_t(XDR *xdrs, u_longlong_t *ullp)
809 * Don't bother open-coding this; it's a fair amount of code. Just
810 * call xdr_uint64_t().
812 return (xdr_uint64_t(xdrs, (uint64_t *)ullp));
819 xdr_modevent(module_t mod, int type, void *data)
824 static moduledata_t xdr_mod = {
829 DECLARE_MODULE(xdr, xdr_mod, SI_SUB_VFS, SI_ORDER_ANY);
830 MODULE_VERSION(xdr, 1);