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>
60 * constants specific to the xdr "protocol"
62 #define XDR_FALSE ((long) 0)
63 #define XDR_TRUE ((long) 1)
64 #define LASTUNSIGNED ((u_int) 0-1)
69 static const char xdr_zero
[BYTES_PER_XDR_UNIT
] = {0, 0, 0, 0};
72 * Free a data structure using XDR
73 * Not a filter, but a convenient utility nonetheless
76 xdr_free (xdrproc_t proc
, char *objp
)
92 libc_hidden_def(xdr_void
)
96 * The definition of xdr_long() is kept for backward
97 * compatibility. Instead xdr_int() should be used.
100 xdr_long (XDR
*xdrs
, long *lp
)
102 if (xdrs
->x_op
== XDR_ENCODE
103 && (sizeof (int32_t) == sizeof (long)
104 || (int32_t) *lp
== *lp
))
105 return XDR_PUTLONG (xdrs
, lp
);
107 if (xdrs
->x_op
== XDR_DECODE
)
108 return XDR_GETLONG (xdrs
, lp
);
110 if (xdrs
->x_op
== XDR_FREE
)
115 libc_hidden_def(xdr_long
)
121 xdr_short (XDR
*xdrs
, short *sp
)
129 return XDR_PUTLONG (xdrs
, &l
);
132 if (!XDR_GETLONG (xdrs
, &l
))
144 libc_hidden_def(xdr_short
)
150 xdr_int (XDR
*xdrs
, int *ip
)
153 #if INT_MAX < LONG_MAX
160 return XDR_PUTLONG (xdrs
, &l
);
163 if (!XDR_GETLONG (xdrs
, &l
))
172 #elif INT_MAX == LONG_MAX
173 return xdr_long (xdrs
, (long *) ip
);
174 #elif INT_MAX == SHRT_MAX
175 return xdr_short (xdrs
, (short *) ip
);
177 #error unexpected integer sizes in xdr_int()
180 libc_hidden_def(xdr_int
)
183 * XDR unsigned long integers
184 * The definition of xdr_u_long() is kept for backward
185 * compatibility. Instead xdr_u_int() should be used.
188 xdr_u_long (XDR
*xdrs
, u_long
*ulp
)
196 if (XDR_GETLONG (xdrs
, &tmp
) == FALSE
)
199 *ulp
= (uint32_t) tmp
;
204 if (sizeof (uint32_t) != sizeof (u_long
)
205 && (uint32_t) *ulp
!= *ulp
)
208 return XDR_PUTLONG (xdrs
, (long *) ulp
);
215 libc_hidden_def(xdr_u_long
)
218 * XDR unsigned integers
221 xdr_u_int (XDR
*xdrs
, u_int
*up
)
223 #if UINT_MAX < ULONG_MAX
230 return XDR_PUTLONG (xdrs
, (long *) &l
);
233 if (!XDR_GETLONG (xdrs
, (long *) &l
))
242 #elif UINT_MAX == ULONG_MAX
243 return xdr_u_long (xdrs
, (u_long
*) up
);
244 #elif UINT_MAX == USHRT_MAX
245 return xdr_short (xdrs
, (short *) up
);
247 #error unexpected integer sizes in xdr_u_int()
250 libc_hidden_def(xdr_u_int
)
254 * same as xdr_u_hyper - open coded to save a proc call!
257 xdr_hyper (XDR
*xdrs
, quad_t
*llp
)
262 if (xdrs
->x_op
== XDR_ENCODE
)
264 t1
= (long) ((*llp
) >> 32);
266 return (XDR_PUTLONG(xdrs
, &t1
) && XDR_PUTLONG(xdrs
, (long *) &t2
));
269 if (xdrs
->x_op
== XDR_DECODE
)
271 if (!XDR_GETLONG(xdrs
, &t1
) || !XDR_GETLONG(xdrs
, (long *) &t2
))
273 /* t2 must be unsigned for this to work */
274 *llp
= ((quad_t
) t1
) << 32;
279 if (xdrs
->x_op
== XDR_FREE
)
284 libc_hidden_def(xdr_hyper
)
289 * same as xdr_hyper - open coded to save a proc call!
292 xdr_u_hyper (XDR
*xdrs
, u_quad_t
*ullp
)
297 if (xdrs
->x_op
== XDR_ENCODE
)
299 t1
= (unsigned long) ((*ullp
) >> 32);
300 t2
= (unsigned long) (*ullp
);
301 return (XDR_PUTLONG(xdrs
, (long *) &t1
) && XDR_PUTLONG(xdrs
, (long *) &t2
));
304 if (xdrs
->x_op
== XDR_DECODE
)
306 if (!XDR_GETLONG(xdrs
, (long *) &t1
) || !XDR_GETLONG(xdrs
, (long *) &t2
))
308 *ullp
= ((u_quad_t
) t1
) << 32;
313 if (xdrs
->x_op
== XDR_FREE
)
318 libc_hidden_def(xdr_u_hyper
)
321 xdr_longlong_t (XDR
*xdrs
, quad_t
*llp
)
323 return xdr_hyper (xdrs
, llp
);
327 xdr_u_longlong_t (XDR
*xdrs
, u_quad_t
*ullp
)
329 return xdr_u_hyper (xdrs
, ullp
);
333 * XDR unsigned short integers
336 xdr_u_short (XDR
*xdrs
, u_short
*usp
)
344 return XDR_PUTLONG (xdrs
, (long *) &l
);
347 if (!XDR_GETLONG (xdrs
, (long *) &l
))
359 libc_hidden_def(xdr_u_short
)
366 xdr_char (XDR
*xdrs
, char *cp
)
371 if (!xdr_int (xdrs
, &i
))
380 * XDR an unsigned char
383 xdr_u_char (XDR
*xdrs
, u_char
*cp
)
388 if (!xdr_u_int (xdrs
, &u
))
400 xdr_bool (XDR
*xdrs
, bool_t
*bp
)
407 lb
= *bp
? XDR_TRUE
: XDR_FALSE
;
408 return XDR_PUTLONG (xdrs
, &lb
);
411 if (!XDR_GETLONG (xdrs
, &lb
))
415 *bp
= (lb
== XDR_FALSE
) ? FALSE
: TRUE
;
423 libc_hidden_def(xdr_bool
)
429 xdr_enum (XDR
*xdrs
, enum_t
*ep
)
434 }; /* used to find the size of an enum */
437 * enums are treated as ints
439 if (sizeof (enum sizecheck
) == 4)
441 #if INT_MAX < LONG_MAX
448 return XDR_PUTLONG (xdrs
, &l
);
451 if (!XDR_GETLONG (xdrs
, &l
))
462 return xdr_long (xdrs
, (long *) ep
);
465 else if (sizeof (enum sizecheck
) == sizeof (short))
467 return xdr_short (xdrs
, (short *) ep
);
474 libc_hidden_def(xdr_enum
)
478 * Allows the specification of a fixed size sequence of opaque bytes.
479 * cp points to the opaque object and cnt gives the byte length.
482 xdr_opaque (XDR
*xdrs
, caddr_t cp
, u_int cnt
)
485 static char crud
[BYTES_PER_XDR_UNIT
];
488 * if no data we are done
494 * round byte count to full xdr units
496 rndup
= cnt
% BYTES_PER_XDR_UNIT
;
498 rndup
= BYTES_PER_XDR_UNIT
- rndup
;
503 if (!XDR_GETBYTES (xdrs
, cp
, cnt
))
509 return XDR_GETBYTES (xdrs
, (caddr_t
)crud
, rndup
);
512 if (!XDR_PUTBYTES (xdrs
, cp
, cnt
))
518 return XDR_PUTBYTES (xdrs
, xdr_zero
, rndup
);
525 libc_hidden_def(xdr_opaque
)
529 * *cpp is a pointer to the bytes, *sizep is the count.
530 * If *cpp is NULL maxsize bytes are allocated
533 xdr_bytes (XDR
*xdrs
, char **cpp
, u_int
*sizep
, u_int maxsize
)
535 char *sp
= *cpp
; /* sp is the actual string pointer */
539 * first deal with the length since xdr bytes are counted
541 if (!xdr_u_int (xdrs
, sizep
))
546 if ((nodesize
> maxsize
) && (xdrs
->x_op
!= XDR_FREE
))
552 * now deal with the actual bytes
563 *cpp
= sp
= (char *) mem_alloc (nodesize
);
568 if (_IO_fwide (stderr
, 0) > 0)
569 (void) fwprintf (stderr
, L
"%s", _("xdr_bytes: out of memory\n"));
572 (void) fputs (_("xdr_bytes: out of memory\n"), stderr
);
578 return xdr_opaque (xdrs
, sp
, nodesize
);
583 mem_free (sp
, nodesize
);
590 libc_hidden_def(xdr_bytes
)
593 * Implemented here due to commonality of the object.
596 xdr_netobj (XDR
*xdrs
, struct netobj
*np
)
599 return xdr_bytes (xdrs
, &np
->n_bytes
, &np
->n_len
, MAX_NETOBJ_SZ
);
603 * XDR a discriminated union
604 * Support routine for discriminated unions.
605 * You create an array of xdrdiscrim structures, terminated with
606 * an entry with a null procedure pointer. The routine gets
607 * the discriminant value and then searches the array of xdrdiscrims
608 * looking for that value. It calls the procedure given in the xdrdiscrim
609 * to handle the discriminant. If there is no specific routine a default
610 * routine may be called.
611 * If there is no specific or default routine an error is returned.
614 xdr_union (XDR
*xdrs
, enum_t
*dscmp
, char *unp
, const struct xdr_discrim
*choices
, xdrproc_t dfault
)
619 * we deal with the discriminator; it's an enum
621 if (!xdr_enum (xdrs
, dscmp
))
628 * search choices for a value that matches the discriminator.
629 * if we find one, execute the xdr routine for that value.
631 for (; choices
->proc
!= NULL_xdrproc_t
; choices
++)
633 if (choices
->value
== dscm
)
634 return (*(choices
->proc
)) (xdrs
, unp
, LASTUNSIGNED
);
638 * no match - execute the default xdr routine if there is one
640 return ((dfault
== NULL_xdrproc_t
) ? FALSE
:
641 (*dfault
) (xdrs
, unp
, LASTUNSIGNED
));
643 libc_hidden_def(xdr_union
)
646 * Non-portable xdr primitives.
647 * Care should be taken when moving these routines to new architectures.
652 * XDR null terminated ASCII strings
653 * xdr_string deals with "C strings" - arrays of bytes that are
654 * terminated by a NULL character. The parameter cpp references a
655 * pointer to storage; If the pointer is null, then the necessary
656 * storage is allocated. The last parameter is the max allowed length
657 * of the string as specified by a protocol.
660 xdr_string (XDR
*xdrs
, char **cpp
, u_int maxsize
)
662 char *sp
= *cpp
; /* sp is the actual string pointer */
667 * first deal with the length since xdr strings are counted-strings
674 return TRUE
; /* already free */
676 /* fall through... */
685 if (!xdr_u_int (xdrs
, &size
))
696 * now deal with the actual bytes
706 *cpp
= sp
= (char *) mem_alloc (nodesize
);
710 if (_IO_fwide (stderr
, 0) > 0)
711 (void) fwprintf (stderr
, L
"%s",
712 _("xdr_string: out of memory\n"));
715 (void) fputs (_("xdr_string: out of memory\n"), stderr
);
722 return xdr_opaque (xdrs
, sp
, size
);
725 mem_free (sp
, nodesize
);
731 libc_hidden_def(xdr_string
)
734 * Wrapper for xdr_string that can be called directly from
735 * routines like clnt_call
738 xdr_wrapstring (XDR
*xdrs
, char **cpp
)
740 if (xdr_string (xdrs
, cpp
, LASTUNSIGNED
))