1 # define INTUSE(name) name
3 /* @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC */
5 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
6 * unrestricted use provided that this legend is included on all tape
7 * media and as a part of the software program in whole or part. Users
8 * may copy or modify Sun RPC without charge, but are not authorized
9 * to license or distribute it to anyone else except as part of a product or
10 * program developed by the user.
12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
16 * Sun RPC is provided with no support and without any obligation on the
17 * part of Sun Microsystems, Inc. to assist in its use, correction,
18 * modification or enhancement.
20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22 * OR ANY PART THEREOF.
24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25 * or profits or other special, indirect and consequential damages, even if
26 * Sun has been advised of the possibility of such damages.
28 * Sun Microsystems, Inc.
30 * Mountain View, California 94043
32 #if !defined(lint) && defined(SCCSIDS)
33 static char sccsid[] = "@(#)xdr.c 1.35 87/08/12";
37 * xdr.c, Generic XDR routines implementation.
39 * Copyright (C) 1986, Sun Microsystems, Inc.
41 * These are the "generic" xdr routines used to serialize and de-serialize
42 * most common data items. See xdr.h for more info on the interface to
59 * constants specific to the xdr "protocol"
61 #define XDR_FALSE ((long) 0)
62 #define XDR_TRUE ((long) 1)
63 #define LASTUNSIGNED ((u_int) 0-1)
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, char *objp)
97 xdr_int (XDR *xdrs, int *ip)
100 #if INT_MAX < LONG_MAX
107 return XDR_PUTLONG (xdrs, &l);
110 if (!XDR_GETLONG (xdrs, &l))
119 #elif INT_MAX == LONG_MAX
120 return INTUSE(xdr_long) (xdrs, (long *) ip);
121 #elif INT_MAX == SHRT_MAX
122 return INTUSE(xdr_short) (xdrs, (short *) ip);
124 #error unexpected integer sizes in_xdr_int()
130 * XDR unsigned integers
133 xdr_u_int (XDR *xdrs, u_int *up)
135 #if UINT_MAX < ULONG_MAX
142 return XDR_PUTLONG (xdrs, &l);
145 if (!XDR_GETLONG (xdrs, &l))
149 *up = (u_int) (u_long) l;
154 #elif UINT_MAX == ULONG_MAX
155 return INTUSE(xdr_u_long) (xdrs, (u_long *) up);
156 #elif UINT_MAX == USHRT_MAX
157 return INTUSE(xdr_short) (xdrs, (short *) up);
159 #error unexpected integer sizes in_xdr_u_int()
166 * The definition of xdr_long() is kept for backward
167 * compatibility. Instead xdr_int() should be used.
170 xdr_long (XDR *xdrs, long *lp)
173 if (xdrs->x_op == XDR_ENCODE
174 && (sizeof (int32_t) == sizeof (long)
175 || (int32_t) *lp == *lp))
176 return XDR_PUTLONG (xdrs, lp);
178 if (xdrs->x_op == XDR_DECODE)
179 return XDR_GETLONG (xdrs, lp);
181 if (xdrs->x_op == XDR_FREE)
189 * XDR unsigned long integers
190 * The definition of xdr_u_long() is kept for backward
191 * compatibility. Instead xdr_u_int() should be used.
194 xdr_u_long (XDR *xdrs, u_long *ulp)
202 if (XDR_GETLONG (xdrs, &tmp) == FALSE)
205 *ulp = (uint32_t) tmp;
210 if (sizeof (uint32_t) != sizeof (u_long)
211 && (uint32_t) *ulp != *ulp)
214 return XDR_PUTLONG (xdrs, (long *) ulp);
225 * same as xdr_u_hyper - open coded to save a proc call!
228 xdr_hyper (XDR *xdrs, quad_t *llp)
232 if (xdrs->x_op == XDR_ENCODE)
234 t1 = (long) ((*llp) >> 32);
236 return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
239 if (xdrs->x_op == XDR_DECODE)
241 if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
243 *llp = ((quad_t) t1) << 32;
244 *llp |= (uint32_t) t2;
248 if (xdrs->x_op == XDR_FREE)
258 * same as xdr_hyper - open coded to save a proc call!
261 xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
265 if (xdrs->x_op == XDR_ENCODE)
267 t1 = (unsigned long) ((*ullp) >> 32);
268 t2 = (unsigned long) (*ullp);
269 return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
272 if (xdrs->x_op == XDR_DECODE)
274 if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
276 *ullp = ((u_quad_t) t1) << 32;
277 *ullp |= (uint32_t) t2;
281 if (xdrs->x_op == XDR_FREE)
289 xdr_longlong_t (XDR *xdrs, quad_t *llp)
291 return INTUSE(xdr_hyper) (xdrs, llp);
295 xdr_u_longlong_t (XDR *xdrs, u_quad_t *ullp)
297 return INTUSE(xdr_u_hyper) (xdrs, ullp);
304 xdr_short (XDR *xdrs, short *sp)
312 return XDR_PUTLONG (xdrs, &l);
315 if (!XDR_GETLONG (xdrs, &l))
330 * XDR unsigned short integers
333 xdr_u_short (XDR *xdrs, u_short *usp)
341 return XDR_PUTLONG (xdrs, &l);
344 if (!XDR_GETLONG (xdrs, &l))
348 *usp = (u_short) (u_long) l;
363 xdr_char (XDR *xdrs, char *cp)
368 if (!INTUSE(xdr_int) (xdrs, &i))
377 * XDR an unsigned char
380 xdr_u_char (XDR *xdrs, u_char *cp)
385 if (!INTUSE(xdr_u_int) (xdrs, &u))
397 xdr_bool (XDR *xdrs, bool_t *bp)
404 lb = *bp ? XDR_TRUE : XDR_FALSE;
405 return XDR_PUTLONG (xdrs, &lb);
408 if (!XDR_GETLONG (xdrs, &lb))
412 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
426 xdr_enum (XDR *xdrs, enum_t *ep)
431 }; /* used to find the size of an enum */
434 * enums are treated as ints
436 if (sizeof (enum sizecheck) == 4)
438 #if INT_MAX < LONG_MAX
445 return XDR_PUTLONG (xdrs, &l);
448 if (!XDR_GETLONG (xdrs, &l))
459 return INTUSE(xdr_long) (xdrs, (long *) ep);
462 else if (sizeof (enum sizecheck) == sizeof (short))
464 return INTUSE(xdr_short) (xdrs, (short *) ep);
475 * Allows the specification of a fixed size sequence of opaque bytes.
476 * cp points to the opaque object and cnt gives the byte length.
479 xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
482 static char crud[BYTES_PER_XDR_UNIT];
485 * if no data we are done
491 * round byte count to full xdr units
493 rndup = cnt % BYTES_PER_XDR_UNIT;
495 rndup = BYTES_PER_XDR_UNIT - rndup;
500 if (!XDR_GETBYTES (xdrs, cp, cnt))
506 return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
509 if (!XDR_PUTBYTES (xdrs, cp, cnt))
515 return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
526 * *cpp is a pointer to the bytes, *sizep is the count.
527 * If *cpp is NULL maxsize bytes are allocated
530 xdr_bytes (xdrs, cpp, sizep, maxsize)
536 char *sp = *cpp; /* sp is the actual string pointer */
540 * first deal with the length since xdr bytes are counted
542 if (!INTUSE(xdr_u_int) (xdrs, sizep))
547 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
553 * now deal with the actual bytes
564 *cpp = sp = (char *) mem_alloc (nodesize);
568 fprintf (NULL, "%s", "xdr_bytes: out of memory\n");
574 return INTUSE(xdr_opaque) (xdrs, sp, nodesize);
579 mem_free (sp, nodesize);
589 * Implemented here due to commonality of the object.
592 xdr_netobj (xdrs, np)
597 return INTUSE(xdr_bytes) (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
602 * XDR a discriminated union
603 * Support routine for discriminated unions.
604 * You create an array of xdrdiscrim structures, terminated with
605 * an entry with a null procedure pointer. The routine gets
606 * the discriminant value and then searches the array of xdrdiscrims
607 * looking for that value. It calls the procedure given in the xdrdiscrim
608 * to handle the discriminant. If there is no specific routine a default
609 * routine may be called.
610 * If there is no specific or default routine an error is returned.
613 xdr_union (xdrs, dscmp, unp, choices, dfault)
615 enum_t *dscmp; /* enum to decide which arm to work on */
616 char *unp; /* the union itself */
617 const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
618 xdrproc_t dfault; /* default xdr routine */
623 * we deal with the discriminator; it's an enum
625 if (!INTUSE(xdr_enum) (xdrs, dscmp))
632 * search choices for a value that matches the discriminator.
633 * if we find one, execute the xdr routine for that value.
635 for (; choices->proc != NULL_xdrproc_t; choices++)
637 if (choices->value == dscm)
638 return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
642 * no match - execute the default xdr routine if there is one
644 return ((dfault == NULL_xdrproc_t) ? FALSE :
645 (*dfault) (xdrs, unp, LASTUNSIGNED));
651 * Non-portable xdr primitives.
652 * Care should be taken when moving these routines to new architectures.
657 * XDR null terminated ASCII strings
658 * xdr_string deals with "C strings" - arrays of bytes that are
659 * terminated by a NULL character. The parameter cpp references a
660 * pointer to storage; If the pointer is null, then the necessary
661 * storage is allocated. The last parameter is the max allowed length
662 * of the string as specified by a protocol.
665 xdr_string (xdrs, cpp, maxsize)
670 char *sp = *cpp; /* sp is the actual string pointer */
675 * first deal with the length since xdr strings are counted-strings
682 return TRUE; /* already free */
684 /* fall through... */
693 if (!INTUSE(xdr_u_int) (xdrs, &size))
704 /* This means an overflow. It a bug in the caller which
705 provided a too large maxsize but nevertheless catch it
711 * now deal with the actual bytes
717 *cpp = sp = (char *) mem_alloc (nodesize);
720 fprintf (NULL, "%s", "xdr_string: out of memory\n");
727 return INTUSE(xdr_opaque) (xdrs, sp, size);
730 mem_free (sp, nodesize);
739 * Wrapper for xdr_string that can be called directly from
740 * routines like clnt_call
743 xdr_wrapstring (xdrs, cpp)
747 if (INTUSE(xdr_string) (xdrs, cpp, LASTUNSIGNED))