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 * $FreeBSD: src/lib/libc/xdr/xdr.c,v 1.9.2.1 2000/05/06 21:16:04 dec Exp $
32 * $DragonFly: src/lib/libc/xdr/xdr.c,v 1.3 2004/10/25 19:38:02 drhodus 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
49 #include <rpc/types.h>
53 * constants specific to the xdr "protocol"
55 #define XDR_FALSE ((long) 0)
56 #define XDR_TRUE ((long) 1)
57 #define LASTUNSIGNED ((u_int) 0-1)
62 static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
65 * Free a data structure using XDR
66 * Not a filter, but a convenient utility nonetheless
83 xdr_void(/* xdrs, addr */)
102 switch (xdrs->x_op) {
106 return (XDR_PUTLONG(xdrs, &l));
109 if (!XDR_GETLONG(xdrs, &l)) {
122 * XDR unsigned integers
131 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!
160 switch (xdrs->x_op) {
162 return (XDR_PUTLONG(xdrs, lp));
164 return (XDR_GETLONG(xdrs, lp));
173 * XDR unsigned long integers
174 * same as xdr_long - open coded to save a proc call!
177 xdr_u_long(xdrs, ulp)
181 switch (xdrs->x_op) {
183 return (XDR_PUTLONG(xdrs, (long *)ulp));
185 return (XDR_GETLONG(xdrs, (long *)ulp));
194 * XDR 32-bit integers
195 * same as xdr_u_int32_t - open coded to save a proc call!
198 xdr_int32_t(xdrs, int32_p)
204 switch (xdrs->x_op) {
208 return (XDR_PUTLONG(xdrs, &l));
211 if (!XDR_GETLONG(xdrs, &l)) {
214 *int32_p = (int32_t) l;
224 * XDR unsigned 32-bit integers
225 * same as xdr_int32_t - open coded to save a proc call!
228 xdr_u_int32_t(xdrs, u_int32_p)
230 u_int32_t *u_int32_p;
234 switch (xdrs->x_op) {
237 l = (u_long) *u_int32_p;
238 return (XDR_PUTLONG(xdrs, (long *)&l));
241 if (!XDR_GETLONG(xdrs, (long *)&l)) {
244 *u_int32_p = (u_int32_t) l;
254 * XDR 64-bit integers
257 xdr_int64_t(xdrs, int64_p)
263 switch (xdrs->x_op) {
266 ul[0] = (u_long)((u_int64_t)*int64_p >> 32) & 0xffffffff;
267 ul[1] = (u_long)((u_int64_t)*int64_p) & 0xffffffff;
268 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
270 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
272 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
274 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
277 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
286 * XDR unsigned 64-bit integers
289 xdr_u_int64_t(xdrs, uint64_p)
295 switch (xdrs->x_op) {
298 ul[0] = (u_long)(*uint64_p >> 32) & 0xffffffff;
299 ul[1] = (u_long)(*uint64_p) & 0xffffffff;
300 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
302 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
305 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
307 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
309 *uint64_p = (u_int64_t)
310 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
329 switch (xdrs->x_op) {
333 return (XDR_PUTLONG(xdrs, &l));
336 if (!XDR_GETLONG(xdrs, &l)) {
349 * XDR unsigned short integers
352 xdr_u_short(xdrs, usp)
358 switch (xdrs->x_op) {
362 return (XDR_PUTLONG(xdrs, (long *)&l));
365 if (!XDR_GETLONG(xdrs, (long *)&l)) {
379 * XDR 16-bit integers
382 xdr_int16_t(xdrs, int16_p)
388 switch (xdrs->x_op) {
392 return (XDR_PUTLONG(xdrs, &l));
395 if (!XDR_GETLONG(xdrs, &l)) {
398 *int16_p = (int16_t) l;
408 * XDR unsigned 16-bit integers
411 xdr_u_int16_t(xdrs, u_int16_p)
413 u_int16_t *u_int16_p;
417 switch (xdrs->x_op) {
420 l = (u_long) *u_int16_p;
421 return (XDR_PUTLONG(xdrs, (long *)&l));
424 if (!XDR_GETLONG(xdrs, (long *)&l)) {
427 *u_int16_p = (u_int16_t) l;
448 if (!xdr_int(xdrs, &i)) {
456 * XDR an unsigned char
466 if (!xdr_u_int(xdrs, &u)) {
483 switch (xdrs->x_op) {
486 lb = *bp ? XDR_TRUE : XDR_FALSE;
487 return (XDR_PUTLONG(xdrs, &lb));
490 if (!XDR_GETLONG(xdrs, &lb)) {
493 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
511 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
514 * enums are treated as ints
516 if (sizeof (enum sizecheck) == sizeof (long)) {
517 return (xdr_long(xdrs, (long *)ep));
518 } else if (sizeof (enum sizecheck) == sizeof (int)) {
519 return (xdr_int(xdrs, (int *)ep));
520 } else if (sizeof (enum sizecheck) == sizeof (short)) {
521 return (xdr_short(xdrs, (short *)ep));
526 (void) (xdr_short(xdrs, (short *)ep));
527 (void) (xdr_int(xdrs, (int *)ep));
528 return (xdr_long(xdrs, (long *)ep));
534 * Allows the specification of a fixed size sequence of opaque bytes.
535 * cp points to the opaque object and cnt gives the byte length.
538 xdr_opaque(xdrs, cp, cnt)
544 static crud[BYTES_PER_XDR_UNIT];
547 * if no data we are done
553 * round byte count to full xdr units
555 rndup = cnt % BYTES_PER_XDR_UNIT;
557 rndup = BYTES_PER_XDR_UNIT - rndup;
559 if (xdrs->x_op == XDR_DECODE) {
560 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
565 return (XDR_GETBYTES(xdrs, (caddr_t)crud, rndup));
568 if (xdrs->x_op == XDR_ENCODE) {
569 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
574 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
577 if (xdrs->x_op == XDR_FREE) {
586 * *cpp is a pointer to the bytes, *sizep is the count.
587 * If *cpp is NULL maxsize bytes are allocated
590 xdr_bytes(xdrs, cpp, sizep, maxsize)
596 char *sp = *cpp; /* sp is the actual string pointer */
600 * first deal with the length since xdr bytes are counted
602 if (! xdr_u_int(xdrs, sizep)) {
606 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
611 * now deal with the actual bytes
613 switch (xdrs->x_op) {
620 *cpp = sp = (char *)mem_alloc(nodesize);
623 (void) fprintf(stderr, "xdr_bytes: out of memory\n");
629 return (xdr_opaque(xdrs, sp, nodesize));
633 mem_free(sp, nodesize);
642 * Implemented here due to commonality of the object.
650 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
654 * XDR a descriminated union
655 * Support routine for discriminated unions.
656 * You create an array of xdrdiscrim structures, terminated with
657 * an entry with a null procedure pointer. The routine gets
658 * the discriminant value and then searches the array of xdrdiscrims
659 * looking for that value. It calls the procedure given in the xdrdiscrim
660 * to handle the discriminant. If there is no specific routine a default
661 * routine may be called.
662 * If there is no specific or default routine an error is returned.
665 xdr_union(xdrs, dscmp, unp, choices, dfault)
667 enum_t *dscmp; /* enum to decide which arm to work on */
668 char *unp; /* the union itself */
669 struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
670 xdrproc_t dfault; /* default xdr routine */
675 * we deal with the discriminator; it's an enum
677 if (! xdr_enum(xdrs, dscmp)) {
683 * search choices for a value that matches the discriminator.
684 * if we find one, execute the xdr routine for that value.
686 for (; choices->proc != NULL_xdrproc_t; choices++) {
687 if (choices->value == dscm)
688 return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
692 * no match - execute the default xdr routine if there is one
694 return ((dfault == NULL_xdrproc_t) ? FALSE :
695 (*dfault)(xdrs, unp, LASTUNSIGNED));
700 * Non-portable xdr primitives.
701 * Care should be taken when moving these routines to new architectures.
706 * XDR null terminated ASCII strings
707 * xdr_string deals with "C strings" - arrays of bytes that are
708 * terminated by a NULL character. The parameter cpp references a
709 * pointer to storage; If the pointer is null, then the necessary
710 * storage is allocated. The last parameter is the max allowed length
711 * of the string as specified by a protocol.
714 xdr_string(xdrs, cpp, maxsize)
719 char *sp = *cpp; /* sp is the actual string pointer */
724 * first deal with the length since xdr strings are counted-strings
726 switch (xdrs->x_op) {
729 return(TRUE); /* already free */
731 /* fall through... */
736 if (! xdr_u_int(xdrs, &size)) {
739 if (size > maxsize) {
745 * now deal with the actual bytes
747 switch (xdrs->x_op) {
754 *cpp = sp = (char *)mem_alloc(nodesize);
756 (void) fprintf(stderr, "xdr_string: out of memory\n");
763 return (xdr_opaque(xdrs, sp, size));
766 mem_free(sp, nodesize);
774 * Wrapper for xdr_string that can be called directly from
775 * routines like clnt_call
778 xdr_wrapstring(xdrs, cpp)
782 return xdr_string(xdrs, cpp, LASTUNSIGNED);