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
47 #include <rpc/types.h>
51 * constants specific to the xdr "protocol"
53 #define XDR_FALSE ((long) 0)
54 #define XDR_TRUE ((long) 1)
55 #define LASTUNSIGNED ((u_int) 0-1)
60 static const char xdr_zero[BYTES_PER_XDR_UNIT] =
64 * Free a data structure using XDR
65 * Not a filter, but a convenient utility nonetheless
96 #if INT_MAX < LONG_MAX
103 return XDR_PUTLONG (xdrs, &l);
106 if (!XDR_GETLONG (xdrs, &l))
115 #elif INT_MAX == LONG_MAX
116 return xdr_long (xdrs, (long *) ip);
117 #elif INT_MAX == SHRT_MAX
118 return xdr_short (xdrs, (short *) ip);
120 #error unexpected integer sizes in_xdr_int()
125 * XDR unsigned integers
132 #if UINT_MAX < ULONG_MAX
139 return XDR_PUTLONG (xdrs, &l);
142 if (!XDR_GETLONG (xdrs, &l))
151 #elif UINT_MAX == ULONG_MAX
152 return xdr_u_long (xdrs, (u_long *) up);
153 #elif UINT_MAX == USHRT_MAX
154 return xdr_short (xdrs, (short *) up);
156 #error unexpected integer sizes in_xdr_u_int()
162 * same as xdr_u_long - open coded to save a proc call!
170 if (xdrs->x_op == XDR_ENCODE)
171 return XDR_PUTLONG (xdrs, lp);
173 if (xdrs->x_op == XDR_DECODE)
174 return XDR_GETLONG (xdrs, lp);
176 if (xdrs->x_op == XDR_FREE)
183 * XDR unsigned long integers
184 * same as xdr_long - open coded to save a proc call!
187 xdr_u_long (xdrs, ulp)
194 return XDR_GETLONG (xdrs, (long *) ulp);
197 return XDR_PUTLONG (xdrs, (long *) ulp);
219 return XDR_PUTLONG (xdrs, &l);
222 if (!XDR_GETLONG (xdrs, &l))
236 * XDR unsigned short integers
239 xdr_u_short (xdrs, usp)
249 return XDR_PUTLONG (xdrs, &l);
252 if (!XDR_GETLONG (xdrs, &l))
277 if (!xdr_int (xdrs, &i))
286 * XDR an unsigned char
289 xdr_u_char (xdrs, cp)
296 if (!xdr_u_int (xdrs, &u))
317 lb = *bp ? XDR_TRUE : XDR_FALSE;
318 return XDR_PUTLONG (xdrs, &lb);
321 if (!XDR_GETLONG (xdrs, &lb))
325 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
345 }; /* used to find the size of an enum */
348 * enums are treated as ints
350 if (sizeof (enum sizecheck) == 4)
352 #if INT_MAX < LONG_MAX
359 return XDR_PUTLONG (xdrs, &l);
362 if (!XDR_GETLONG (xdrs, &l))
373 return xdr_long (xdrs, (long *) ep);
376 else if (sizeof (enum sizecheck) == sizeof (short))
378 return xdr_short (xdrs, (short *) ep);
388 * Allows the specification of a fixed size sequence of opaque bytes.
389 * cp points to the opaque object and cnt gives the byte length.
392 xdr_opaque (xdrs, cp, cnt)
398 static char crud[BYTES_PER_XDR_UNIT];
401 * if no data we are done
407 * round byte count to full xdr units
409 rndup = cnt % BYTES_PER_XDR_UNIT;
411 rndup = BYTES_PER_XDR_UNIT - rndup;
416 if (!XDR_GETBYTES (xdrs, cp, cnt))
422 return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
425 if (!XDR_PUTBYTES (xdrs, cp, cnt))
431 return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
441 * *cpp is a pointer to the bytes, *sizep is the count.
442 * If *cpp is NULL maxsize bytes are allocated
445 xdr_bytes (xdrs, cpp, sizep, maxsize)
451 char *sp = *cpp; /* sp is the actual string pointer */
455 * first deal with the length since xdr bytes are counted
457 if (!xdr_u_int (xdrs, sizep))
462 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
468 * now deal with the actual bytes
479 *cpp = sp = (char *) mem_alloc (nodesize);
483 (void) fprintf (stderr, "xdr_bytes: out of memory\n");
489 return xdr_opaque (xdrs, sp, nodesize);
494 mem_free (sp, nodesize);
503 * Implemented here due to commonality of the object.
506 xdr_netobj (xdrs, np)
511 return xdr_bytes (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
515 * XDR a discriminated union
516 * Support routine for discriminated unions.
517 * You create an array of xdrdiscrim structures, terminated with
518 * an entry with a null procedure pointer. The routine gets
519 * the discriminant value and then searches the array of xdrdiscrims
520 * looking for that value. It calls the procedure given in the xdrdiscrim
521 * to handle the discriminant. If there is no specific routine a default
522 * routine may be called.
523 * If there is no specific or default routine an error is returned.
526 xdr_union (xdrs, dscmp, unp, choices, dfault)
528 enum_t *dscmp; /* enum to decide which arm to work on */
529 char *unp; /* the union itself */
530 const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
531 xdrproc_t dfault; /* default xdr routine */
536 * we deal with the discriminator; it's an enum
538 if (!xdr_enum (xdrs, dscmp))
545 * search choices for a value that matches the discriminator.
546 * if we find one, execute the xdr routine for that value.
548 for (; choices->proc != NULL_xdrproc_t; choices++)
550 if (choices->value == dscm)
551 return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
555 * no match - execute the default xdr routine if there is one
557 return ((dfault == NULL_xdrproc_t) ? FALSE :
558 (*dfault) (xdrs, unp, LASTUNSIGNED));
563 * Non-portable xdr primitives.
564 * Care should be taken when moving these routines to new architectures.
569 * XDR null terminated ASCII strings
570 * xdr_string deals with "C strings" - arrays of bytes that are
571 * terminated by a NULL character. The parameter cpp references a
572 * pointer to storage; If the pointer is null, then the necessary
573 * storage is allocated. The last parameter is the max allowed length
574 * of the string as specified by a protocol.
577 xdr_string (xdrs, cpp, maxsize)
582 char *sp = *cpp; /* sp is the actual string pointer */
587 * first deal with the length since xdr strings are counted-strings
594 return TRUE; /* already free */
596 /* fall through... */
605 if (!xdr_u_int (xdrs, &size))
616 * now deal with the actual bytes
626 *cpp = sp = (char *) mem_alloc (nodesize);
629 (void) fprintf (stderr, "xdr_string: out of memory\n");
636 return xdr_opaque (xdrs, sp, size);
639 mem_free (sp, nodesize);
647 * Wrapper for xdr_string that can be called directly from
648 * routines like clnt_call
651 xdr_wrapstring (xdrs, cpp)
655 if (xdr_string (xdrs, cpp, LASTUNSIGNED))