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 #include <sys/cdefs.h>
34 * xdr.c, Generic XDR routines implementation.
36 * Copyright (C) 1986, Sun Microsystems, Inc.
38 * These are the "generic" xdr routines used to serialize and de-serialize
39 * most common data items. See xdr.h for more info on the interface to
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
50 #include <rpc/rpc_com.h>
51 #include <rpc/types.h>
54 typedef quad_t longlong_t; /* ANSI long long type */
55 typedef u_quad_t u_longlong_t; /* ANSI unsigned long long type */
58 * constants specific to the xdr "protocol"
60 #define XDR_FALSE ((long) 0)
61 #define XDR_TRUE ((long) 1)
63 MALLOC_DEFINE(M_RPC, "rpc", "Remote Procedure Call");
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)
97 xdr_int(XDR *xdrs, int *ip)
101 switch (xdrs->x_op) {
104 return (XDR_PUTLONG(xdrs, &l));
107 if (!XDR_GETLONG(xdrs, &l)) {
121 * XDR unsigned integers
124 xdr_u_int(XDR *xdrs, u_int *up)
128 switch (xdrs->x_op) {
131 return (XDR_PUTLONG(xdrs, (long *)&l));
134 if (!XDR_GETLONG(xdrs, (long *)&l)) {
149 * same as xdr_u_long - open coded to save a proc call!
152 xdr_long(XDR *xdrs, long *lp)
154 switch (xdrs->x_op) {
156 return (XDR_PUTLONG(xdrs, lp));
158 return (XDR_GETLONG(xdrs, lp));
167 * XDR unsigned long integers
168 * same as xdr_long - open coded to save a proc call!
171 xdr_u_long(XDR *xdrs, u_long *ulp)
173 switch (xdrs->x_op) {
175 return (XDR_PUTLONG(xdrs, (long *)ulp));
177 return (XDR_GETLONG(xdrs, (long *)ulp));
186 * XDR 32-bit integers
187 * same as xdr_uint32_t - open coded to save a proc call!
190 xdr_int32_t(XDR *xdrs, int32_t *int32_p)
194 switch (xdrs->x_op) {
197 return (XDR_PUTLONG(xdrs, &l));
200 if (!XDR_GETLONG(xdrs, &l)) {
203 *int32_p = (int32_t) l;
214 * XDR unsigned 32-bit integers
215 * same as xdr_int32_t - open coded to save a proc call!
218 xdr_uint32_t(XDR *xdrs, uint32_t *uint32_p)
222 switch (xdrs->x_op) {
224 l = (u_long) *uint32_p;
225 return (XDR_PUTLONG(xdrs, (long *)&l));
228 if (!XDR_GETLONG(xdrs, (long *)&l)) {
231 *uint32_p = (uint32_t) l;
245 xdr_short(XDR *xdrs, short *sp)
249 switch (xdrs->x_op) {
252 return (XDR_PUTLONG(xdrs, &l));
255 if (!XDR_GETLONG(xdrs, &l)) {
269 * XDR unsigned short integers
272 xdr_u_short(XDR *xdrs, u_short *usp)
276 switch (xdrs->x_op) {
279 return (XDR_PUTLONG(xdrs, (long *)&l));
282 if (!XDR_GETLONG(xdrs, (long *)&l)) {
296 * XDR 16-bit integers
299 xdr_int16_t(XDR *xdrs, int16_t *int16_p)
303 switch (xdrs->x_op) {
306 return (XDR_PUTLONG(xdrs, &l));
309 if (!XDR_GETLONG(xdrs, &l)) {
312 *int16_p = (int16_t) l;
323 * XDR unsigned 16-bit integers
326 xdr_uint16_t(XDR *xdrs, uint16_t *uint16_p)
330 switch (xdrs->x_op) {
332 l = (u_long) *uint16_p;
333 return (XDR_PUTLONG(xdrs, (long *)&l));
336 if (!XDR_GETLONG(xdrs, (long *)&l)) {
339 *uint16_p = (uint16_t) l;
353 xdr_char(XDR *xdrs, char *cp)
357 i = *((unsigned char *)cp);
358 if (!xdr_u_int(xdrs, &i)) {
361 *((unsigned char *)cp) = i;
366 * XDR an unsigned char
369 xdr_u_char(XDR *xdrs, u_char *cp)
374 if (!xdr_u_int(xdrs, &u)) {
385 xdr_bool(XDR *xdrs, bool_t *bp)
389 switch (xdrs->x_op) {
391 lb = *bp ? XDR_TRUE : XDR_FALSE;
392 return (XDR_PUTLONG(xdrs, &lb));
395 if (!XDR_GETLONG(xdrs, &lb)) {
398 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
412 xdr_enum(XDR *xdrs, enum_t *ep)
414 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
417 * enums are treated as ints
419 /* LINTED */ if (sizeof (enum sizecheck) == sizeof (long)) {
420 return (xdr_long(xdrs, (long *)(void *)ep));
421 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) {
422 return (xdr_int(xdrs, (int *)(void *)ep));
423 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (short)) {
424 return (xdr_short(xdrs, (short *)(void *)ep));
432 * Allows the specification of a fixed size sequence of opaque bytes.
433 * cp points to the opaque object and cnt gives the byte length.
436 xdr_opaque(XDR *xdrs, caddr_t cp, u_int cnt)
439 static int crud[BYTES_PER_XDR_UNIT];
442 * if no data we are done
448 * round byte count to full xdr units
450 rndup = cnt % BYTES_PER_XDR_UNIT;
452 rndup = BYTES_PER_XDR_UNIT - rndup;
454 if (xdrs->x_op == XDR_DECODE) {
455 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
460 return (XDR_GETBYTES(xdrs, (caddr_t)(void *)crud, rndup));
463 if (xdrs->x_op == XDR_ENCODE) {
464 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
469 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
472 if (xdrs->x_op == XDR_FREE) {
481 * *cpp is a pointer to the bytes, *sizep is the count.
482 * If *cpp is NULL maxsize bytes are allocated
485 xdr_bytes(XDR *xdrs, char **cpp, u_int *sizep, u_int maxsize)
487 char *sp = *cpp; /* sp is the actual string pointer */
489 bool_t ret, allocated = FALSE;
492 * first deal with the length since xdr bytes are counted
494 if (! xdr_u_int(xdrs, sizep)) {
498 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
503 * now deal with the actual bytes
505 switch (xdrs->x_op) {
511 *cpp = sp = mem_alloc(nodesize);
515 printf("xdr_bytes: out of memory");
521 ret = xdr_opaque(xdrs, sp, nodesize);
522 if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
523 if (allocated == TRUE) {
524 mem_free(sp, nodesize);
532 mem_free(sp, nodesize);
542 * Implemented here due to commonality of the object.
545 xdr_netobj(XDR *xdrs, struct netobj *np)
548 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
552 * XDR a descriminated union
553 * Support routine for discriminated unions.
554 * You create an array of xdrdiscrim structures, terminated with
555 * an entry with a null procedure pointer. The routine gets
556 * the discriminant value and then searches the array of xdrdiscrims
557 * looking for that value. It calls the procedure given in the xdrdiscrim
558 * to handle the discriminant. If there is no specific routine a default
559 * routine may be called.
560 * If there is no specific or default routine an error is returned.
564 enum_t *dscmp, /* enum to decide which arm to work on */
565 char *unp, /* the union itself */
566 const struct xdr_discrim *choices, /* [value, xdr proc] for each arm */
567 xdrproc_t dfault) /* default xdr routine */
572 * we deal with the discriminator; it's an enum
574 if (! xdr_enum(xdrs, dscmp)) {
580 * search choices for a value that matches the discriminator.
581 * if we find one, execute the xdr routine for that value.
583 for (; choices->proc != NULL_xdrproc_t; choices++) {
584 if (choices->value == dscm)
585 return ((*(choices->proc))(xdrs, unp));
589 * no match - execute the default xdr routine if there is one
591 return ((dfault == NULL_xdrproc_t) ? FALSE :
592 (*dfault)(xdrs, unp));
596 * Non-portable xdr primitives.
597 * Care should be taken when moving these routines to new architectures.
601 * XDR null terminated ASCII strings
602 * xdr_string deals with "C strings" - arrays of bytes that are
603 * terminated by a NULL character. The parameter cpp references a
604 * pointer to storage; If the pointer is null, then the necessary
605 * storage is allocated. The last parameter is the max allowed length
606 * of the string as specified by a protocol.
609 xdr_string(XDR *xdrs, char **cpp, u_int maxsize)
611 char *sp = *cpp; /* sp is the actual string pointer */
614 bool_t ret, allocated = FALSE;
617 * first deal with the length since xdr strings are counted-strings
619 switch (xdrs->x_op) {
622 return(TRUE); /* already free */
631 if (! xdr_u_int(xdrs, &size)) {
634 if (size > maxsize) {
640 * now deal with the actual bytes
642 switch (xdrs->x_op) {
648 *cpp = sp = mem_alloc(nodesize);
652 printf("xdr_string: out of memory");
659 ret = xdr_opaque(xdrs, sp, size);
660 if ((xdrs->x_op == XDR_DECODE) && (ret == FALSE)) {
661 if (allocated == TRUE) {
662 mem_free(sp, nodesize);
669 mem_free(sp, nodesize);
678 * Wrapper for xdr_string that can be called directly from
679 * routines like clnt_call
682 xdr_wrapstring(XDR *xdrs, char **cpp)
684 return xdr_string(xdrs, cpp, RPC_MAXDATASIZE);
688 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
689 * are in the "non-portable" section because they require that a `long long'
692 * --thorpej@netbsd.org, November 30, 1999
696 * XDR 64-bit integers
699 xdr_int64_t(XDR *xdrs, int64_t *llp)
703 switch (xdrs->x_op) {
705 ul[0] = (u_long)((uint64_t)*llp >> 32) & 0xffffffff;
706 ul[1] = (u_long)((uint64_t)*llp) & 0xffffffff;
707 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
709 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
711 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
713 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
716 (((uint64_t)ul[0] << 32) | ((uint64_t)ul[1]));
726 * XDR unsigned 64-bit integers
729 xdr_uint64_t(XDR *xdrs, uint64_t *ullp)
733 switch (xdrs->x_op) {
735 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
736 ul[1] = (u_long)(*ullp) & 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 (((uint64_t)ul[0] << 32) | ((uint64_t)ul[1]));
759 xdr_hyper(XDR *xdrs, longlong_t *llp)
763 * Don't bother open-coding this; it's a fair amount of code. Just
764 * call xdr_int64_t().
766 return (xdr_int64_t(xdrs, (int64_t *)llp));
770 * XDR unsigned hypers
773 xdr_u_hyper(XDR *xdrs, u_longlong_t *ullp)
777 * Don't bother open-coding this; it's a fair amount of code. Just
778 * call xdr_uint64_t().
780 return (xdr_uint64_t(xdrs, (uint64_t *)ullp));
787 xdr_longlong_t(XDR *xdrs, longlong_t *llp)
791 * Don't bother open-coding this; it's a fair amount of code. Just
792 * call xdr_int64_t().
794 return (xdr_int64_t(xdrs, (int64_t *)llp));
801 xdr_u_longlong_t(XDR *xdrs, u_longlong_t *ullp)
805 * Don't bother open-coding this; it's a fair amount of code. Just
806 * call xdr_uint64_t().
808 return (xdr_uint64_t(xdrs, (uint64_t *)ullp));
815 xdr_modevent(module_t mod, int type, void *data)
820 static moduledata_t xdr_mod = {
825 DECLARE_MODULE(xdr, xdr_mod, SI_SUB_VFS, SI_ORDER_ANY);
826 MODULE_VERSION(xdr, 1);