1 /* @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC */
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)xdr.c 1.35 87/08/12";
35 * xdr.c, Generic XDR routines implementation.
37 * Copyright (C) 1986, Sun Microsystems, Inc.
39 * These are the "generic" xdr routines used to serialize and de-serialize
40 * most common data items. See xdr.h for more info on the interface to
48 #include <rpc/types.h>
52 * constants specific to the xdr "protocol"
54 #define XDR_FALSE ((long) 0)
55 #define XDR_TRUE ((long) 1)
56 #define LASTUNSIGNED ((u_int) 0-1)
61 static const char xdr_zero[BYTES_PER_XDR_UNIT] =
65 * Free a data structure using XDR
66 * Not a filter, but a convenient utility nonetheless
97 #if INT_MAX < LONG_MAX
104 return XDR_PUTLONG (xdrs, &l);
107 if (!XDR_GETLONG (xdrs, &l))
116 #elif INT_MAX == LONG_MAX
117 return xdr_long (xdrs, (long *) ip);
118 #elif INT_MAX == SHRT_MAX
119 return xdr_short (xdrs, (short *) ip);
121 #error unexpected integer sizes in_xdr_int()
126 * XDR unsigned integers
133 #if UINT_MAX < ULONG_MAX
140 return XDR_PUTLONG (xdrs, &l);
143 if (!XDR_GETLONG (xdrs, &l))
152 #elif UINT_MAX == ULONG_MAX
153 return xdr_u_long (xdrs, (u_long *) up);
154 #elif UINT_MAX == USHRT_MAX
155 return xdr_short (xdrs, (short *) up);
157 #error unexpected integer sizes in_xdr_u_int()
163 * same as xdr_u_long - open coded to save a proc call!
171 if (xdrs->x_op == XDR_ENCODE)
172 return XDR_PUTLONG (xdrs, lp);
174 if (xdrs->x_op == XDR_DECODE)
175 return XDR_GETLONG (xdrs, lp);
177 if (xdrs->x_op == XDR_FREE)
184 * XDR unsigned long integers
185 * same as xdr_long - open coded to save a proc call!
188 xdr_u_long (xdrs, ulp)
195 return XDR_GETLONG (xdrs, (long *) ulp);
198 return XDR_PUTLONG (xdrs, (long *) ulp);
220 return XDR_PUTLONG (xdrs, &l);
223 if (!XDR_GETLONG (xdrs, &l))
237 * XDR unsigned short integers
240 xdr_u_short (xdrs, usp)
250 return XDR_PUTLONG (xdrs, &l);
253 if (!XDR_GETLONG (xdrs, &l))
278 if (!xdr_int (xdrs, &i))
287 * XDR an unsigned char
290 xdr_u_char (xdrs, cp)
297 if (!xdr_u_int (xdrs, &u))
318 lb = *bp ? XDR_TRUE : XDR_FALSE;
319 return XDR_PUTLONG (xdrs, &lb);
322 if (!XDR_GETLONG (xdrs, &lb))
326 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
346 }; /* used to find the size of an enum */
349 * enums are treated as ints
351 if (sizeof (enum sizecheck) == 4)
353 #if INT_MAX < LONG_MAX
360 return XDR_PUTLONG (xdrs, &l);
363 if (!XDR_GETLONG (xdrs, &l))
374 return xdr_long (xdrs, (long *) ep);
377 else if (sizeof (enum sizecheck) == sizeof (short))
379 return xdr_short (xdrs, (short *) ep);
389 * Allows the specification of a fixed size sequence of opaque bytes.
390 * cp points to the opaque object and cnt gives the byte length.
393 xdr_opaque (xdrs, cp, cnt)
399 static char crud[BYTES_PER_XDR_UNIT];
402 * if no data we are done
408 * round byte count to full xdr units
410 rndup = cnt % BYTES_PER_XDR_UNIT;
412 rndup = BYTES_PER_XDR_UNIT - rndup;
417 if (!XDR_GETBYTES (xdrs, cp, cnt))
423 return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
426 if (!XDR_PUTBYTES (xdrs, cp, cnt))
432 return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
442 * *cpp is a pointer to the bytes, *sizep is the count.
443 * If *cpp is NULL maxsize bytes are allocated
446 xdr_bytes (xdrs, cpp, sizep, maxsize)
452 char *sp = *cpp; /* sp is the actual string pointer */
456 * first deal with the length since xdr bytes are counted
458 if (!xdr_u_int (xdrs, sizep))
463 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
469 * now deal with the actual bytes
480 *cpp = sp = (char *) mem_alloc (nodesize);
484 (void) fprintf (stderr, "xdr_bytes: out of memory\n");
490 return xdr_opaque (xdrs, sp, nodesize);
495 mem_free (sp, nodesize);
504 * Implemented here due to commonality of the object.
507 xdr_netobj (xdrs, np)
512 return xdr_bytes (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
516 * XDR a discriminated union
517 * Support routine for discriminated unions.
518 * You create an array of xdrdiscrim structures, terminated with
519 * an entry with a null procedure pointer. The routine gets
520 * the discriminant value and then searches the array of xdrdiscrims
521 * looking for that value. It calls the procedure given in the xdrdiscrim
522 * to handle the discriminant. If there is no specific routine a default
523 * routine may be called.
524 * If there is no specific or default routine an error is returned.
527 xdr_union (xdrs, dscmp, unp, choices, dfault)
529 enum_t *dscmp; /* enum to decide which arm to work on */
530 char *unp; /* the union itself */
531 const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
532 xdrproc_t dfault; /* default xdr routine */
537 * we deal with the discriminator; it's an enum
539 if (!xdr_enum (xdrs, dscmp))
546 * search choices for a value that matches the discriminator.
547 * if we find one, execute the xdr routine for that value.
549 for (; choices->proc != NULL_xdrproc_t; choices++)
551 if (choices->value == dscm)
552 return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
556 * no match - execute the default xdr routine if there is one
558 return ((dfault == NULL_xdrproc_t) ? FALSE :
559 (*dfault) (xdrs, unp, LASTUNSIGNED));
564 * Non-portable xdr primitives.
565 * Care should be taken when moving these routines to new architectures.
570 * XDR null terminated ASCII strings
571 * xdr_string deals with "C strings" - arrays of bytes that are
572 * terminated by a NULL character. The parameter cpp references a
573 * pointer to storage; If the pointer is null, then the necessary
574 * storage is allocated. The last parameter is the max allowed length
575 * of the string as specified by a protocol.
578 xdr_string (xdrs, cpp, maxsize)
583 char *sp = *cpp; /* sp is the actual string pointer */
588 * first deal with the length since xdr strings are counted-strings
595 return TRUE; /* already free */
597 /* fall through... */
606 if (!xdr_u_int (xdrs, &size))
617 * now deal with the actual bytes
627 *cpp = sp = (char *) mem_alloc (nodesize);
630 (void) fprintf (stderr, "xdr_string: out of memory\n");
637 return xdr_opaque (xdrs, sp, size);
640 mem_free (sp, nodesize);
648 * Wrapper for xdr_string that can be called directly from
649 * routines like clnt_call
652 xdr_wrapstring (xdrs, cpp)
656 if (xdr_string (xdrs, cpp, LASTUNSIGNED))