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
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
51 #include <rpc/types.h>
56 libc_hidden_proto(fwprintf)
59 /* Experimentally off - libc_hidden_proto(strlen) */
60 libc_hidden_proto(fputs)
63 * constants specific to the xdr "protocol"
65 #define XDR_FALSE ((long) 0)
66 #define XDR_TRUE ((long) 1)
67 #define LASTUNSIGNED ((u_int) 0-1)
72 static const char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
75 * Free a data structure using XDR
76 * Not a filter, but a convenient utility nonetheless
79 xdr_free (xdrproc_t proc, char *objp)
90 libc_hidden_proto(xdr_void)
96 libc_hidden_def(xdr_void)
100 * The definition of xdr_long() is kept for backward
101 * compatibility. Instead xdr_int() should be used.
103 libc_hidden_proto(xdr_long)
105 xdr_long (XDR *xdrs, long *lp)
108 if (xdrs->x_op == XDR_ENCODE
109 && (sizeof (int32_t) == sizeof (long)
110 || (int32_t) *lp == *lp))
111 return XDR_PUTLONG (xdrs, lp);
113 if (xdrs->x_op == XDR_DECODE)
114 return XDR_GETLONG (xdrs, lp);
116 if (xdrs->x_op == XDR_FREE)
121 libc_hidden_def(xdr_long)
126 libc_hidden_proto(xdr_short)
128 xdr_short (XDR *xdrs, short *sp)
136 return XDR_PUTLONG (xdrs, &l);
139 if (!XDR_GETLONG (xdrs, &l))
151 libc_hidden_def(xdr_short)
156 libc_hidden_proto(xdr_int)
158 xdr_int (XDR *xdrs, int *ip)
161 #if INT_MAX < LONG_MAX
168 return XDR_PUTLONG (xdrs, &l);
171 if (!XDR_GETLONG (xdrs, &l))
180 #elif INT_MAX == LONG_MAX
181 return xdr_long (xdrs, (long *) ip);
182 #elif INT_MAX == SHRT_MAX
183 return xdr_short (xdrs, (short *) ip);
185 #error unexpected integer sizes in xdr_int()
188 libc_hidden_def(xdr_int)
191 * XDR unsigned long integers
192 * The definition of xdr_u_long() is kept for backward
193 * compatibility. Instead xdr_u_int() should be used.
195 libc_hidden_proto(xdr_u_long)
197 xdr_u_long (XDR *xdrs, u_long *ulp)
205 if (XDR_GETLONG (xdrs, &tmp) == FALSE)
208 *ulp = (uint32_t) tmp;
213 if (sizeof (uint32_t) != sizeof (u_long)
214 && (uint32_t) *ulp != *ulp)
217 return XDR_PUTLONG (xdrs, (long *) ulp);
224 libc_hidden_def(xdr_u_long)
227 * XDR unsigned integers
229 libc_hidden_proto(xdr_u_int)
231 xdr_u_int (XDR *xdrs, u_int *up)
233 #if UINT_MAX < ULONG_MAX
240 return XDR_PUTLONG (xdrs, &l);
243 if (!XDR_GETLONG (xdrs, &l))
252 #elif UINT_MAX == ULONG_MAX
253 return xdr_u_long (xdrs, (u_long *) up);
254 #elif UINT_MAX == USHRT_MAX
255 return xdr_short (xdrs, (short *) up);
257 #error unexpected integer sizes in xdr_u_int()
260 libc_hidden_def(xdr_u_int)
264 * same as xdr_u_hyper - open coded to save a proc call!
266 libc_hidden_proto(xdr_hyper)
268 xdr_hyper (XDR *xdrs, quad_t *llp)
271 unsigned long int t2;
273 if (xdrs->x_op == XDR_ENCODE)
275 t1 = (long) ((*llp) >> 32);
277 return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
280 if (xdrs->x_op == XDR_DECODE)
282 if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
284 *llp = ((quad_t) t1) << 32;
289 if (xdrs->x_op == XDR_FREE)
294 libc_hidden_def(xdr_hyper)
299 * same as xdr_hyper - open coded to save a proc call!
301 libc_hidden_proto(xdr_u_hyper)
303 xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
308 if (xdrs->x_op == XDR_ENCODE)
310 t1 = (unsigned long) ((*ullp) >> 32);
311 t2 = (unsigned long) (*ullp);
312 return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
315 if (xdrs->x_op == XDR_DECODE)
317 if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
319 *ullp = ((u_quad_t) t1) << 32;
324 if (xdrs->x_op == XDR_FREE)
329 libc_hidden_def(xdr_u_hyper)
332 xdr_longlong_t (XDR *xdrs, quad_t *llp)
334 return xdr_hyper (xdrs, llp);
338 xdr_u_longlong_t (XDR *xdrs, u_quad_t *ullp)
340 return xdr_u_hyper (xdrs, ullp);
344 * XDR unsigned short integers
346 libc_hidden_proto(xdr_u_short)
348 xdr_u_short (XDR *xdrs, u_short *usp)
356 return XDR_PUTLONG (xdrs, &l);
359 if (!XDR_GETLONG (xdrs, &l))
371 libc_hidden_def(xdr_u_short)
378 xdr_char (XDR *xdrs, char *cp)
383 if (!xdr_int (xdrs, &i))
392 * XDR an unsigned char
395 xdr_u_char (XDR *xdrs, u_char *cp)
400 if (!xdr_u_int (xdrs, &u))
411 libc_hidden_proto(xdr_bool)
413 xdr_bool (XDR *xdrs, bool_t *bp)
420 lb = *bp ? XDR_TRUE : XDR_FALSE;
421 return XDR_PUTLONG (xdrs, &lb);
424 if (!XDR_GETLONG (xdrs, &lb))
428 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
436 libc_hidden_def(xdr_bool)
441 libc_hidden_proto(xdr_enum)
443 xdr_enum (XDR *xdrs, enum_t *ep)
448 }; /* used to find the size of an enum */
451 * enums are treated as ints
453 if (sizeof (enum sizecheck) == 4)
455 #if INT_MAX < LONG_MAX
462 return XDR_PUTLONG (xdrs, &l);
465 if (!XDR_GETLONG (xdrs, &l))
476 return xdr_long (xdrs, (long *) ep);
479 else if (sizeof (enum sizecheck) == sizeof (short))
481 return xdr_short (xdrs, (short *) ep);
488 libc_hidden_def(xdr_enum)
492 * Allows the specification of a fixed size sequence of opaque bytes.
493 * cp points to the opaque object and cnt gives the byte length.
495 libc_hidden_proto(xdr_opaque)
497 xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
500 static char crud[BYTES_PER_XDR_UNIT];
503 * if no data we are done
509 * round byte count to full xdr units
511 rndup = cnt % BYTES_PER_XDR_UNIT;
513 rndup = BYTES_PER_XDR_UNIT - rndup;
518 if (!XDR_GETBYTES (xdrs, cp, cnt))
524 return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
527 if (!XDR_PUTBYTES (xdrs, cp, cnt))
533 return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
540 libc_hidden_def(xdr_opaque)
544 * *cpp is a pointer to the bytes, *sizep is the count.
545 * If *cpp is NULL maxsize bytes are allocated
547 libc_hidden_proto(xdr_bytes)
549 xdr_bytes (XDR *xdrs, char **cpp, u_int *sizep, u_int maxsize)
551 char *sp = *cpp; /* sp is the actual string pointer */
555 * first deal with the length since xdr bytes are counted
557 if (!xdr_u_int (xdrs, sizep))
562 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
568 * now deal with the actual bytes
579 *cpp = sp = (char *) mem_alloc (nodesize);
584 if (_IO_fwide (stderr, 0) > 0)
585 (void) fwprintf (stderr, L"%s", _("xdr_bytes: out of memory\n"));
588 (void) fputs (_("xdr_bytes: out of memory\n"), stderr);
594 return xdr_opaque (xdrs, sp, nodesize);
599 mem_free (sp, nodesize);
606 libc_hidden_def(xdr_bytes)
609 * Implemented here due to commonality of the object.
612 xdr_netobj (xdrs, np)
617 return xdr_bytes (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
621 * XDR a discriminated union
622 * Support routine for discriminated unions.
623 * You create an array of xdrdiscrim structures, terminated with
624 * an entry with a null procedure pointer. The routine gets
625 * the discriminant value and then searches the array of xdrdiscrims
626 * looking for that value. It calls the procedure given in the xdrdiscrim
627 * to handle the discriminant. If there is no specific routine a default
628 * routine may be called.
629 * If there is no specific or default routine an error is returned.
631 libc_hidden_proto(xdr_union)
633 xdr_union (XDR *xdrs, enum_t *dscmp, char *unp, const struct xdr_discrim *choices, xdrproc_t dfault)
638 * we deal with the discriminator; it's an enum
640 if (!xdr_enum (xdrs, dscmp))
647 * search choices for a value that matches the discriminator.
648 * if we find one, execute the xdr routine for that value.
650 for (; choices->proc != NULL_xdrproc_t; choices++)
652 if (choices->value == dscm)
653 return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
657 * no match - execute the default xdr routine if there is one
659 return ((dfault == NULL_xdrproc_t) ? FALSE :
660 (*dfault) (xdrs, unp, LASTUNSIGNED));
662 libc_hidden_def(xdr_union)
665 * Non-portable xdr primitives.
666 * Care should be taken when moving these routines to new architectures.
671 * XDR null terminated ASCII strings
672 * xdr_string deals with "C strings" - arrays of bytes that are
673 * terminated by a NULL character. The parameter cpp references a
674 * pointer to storage; If the pointer is null, then the necessary
675 * storage is allocated. The last parameter is the max allowed length
676 * of the string as specified by a protocol.
678 libc_hidden_proto(xdr_string)
680 xdr_string (XDR *xdrs, char **cpp, u_int maxsize)
682 char *sp = *cpp; /* sp is the actual string pointer */
687 * first deal with the length since xdr strings are counted-strings
694 return TRUE; /* already free */
696 /* fall through... */
705 if (!xdr_u_int (xdrs, &size))
716 * now deal with the actual bytes
726 *cpp = sp = (char *) mem_alloc (nodesize);
730 if (_IO_fwide (stderr, 0) > 0)
731 (void) fwprintf (stderr, L"%s",
732 _("xdr_string: out of memory\n"));
735 (void) fputs (_("xdr_string: out of memory\n"), stderr);
742 return xdr_opaque (xdrs, sp, size);
745 mem_free (sp, nodesize);
751 libc_hidden_def(xdr_string)
754 * Wrapper for xdr_string that can be called directly from
755 * routines like clnt_call
758 xdr_wrapstring (xdrs, cpp)
762 if (xdr_string (xdrs, cpp, LASTUNSIGNED))