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
] = {0, 0, 0, 0};
64 * Free a data structure using XDR
65 * Not a filter, but a convenient utility nonetheless
68 xdr_free (xdrproc_t proc
, char *objp
)
89 xdr_int (XDR
*xdrs
, int *ip
)
92 #if INT_MAX < LONG_MAX
99 return XDR_PUTLONG (xdrs
, &l
);
102 if (!XDR_GETLONG (xdrs
, &l
))
111 #elif INT_MAX == LONG_MAX
112 return xdr_long (xdrs
, (long *) ip
);
113 #elif INT_MAX == SHRT_MAX
114 return xdr_short (xdrs
, (short *) ip
);
116 #error unexpected integer sizes in_xdr_int()
121 * XDR unsigned integers
124 xdr_u_int (XDR
*xdrs
, u_int
*up
)
126 #if UINT_MAX < ULONG_MAX
133 return XDR_PUTLONG (xdrs
, &l
);
136 if (!XDR_GETLONG (xdrs
, &l
))
145 #elif UINT_MAX == ULONG_MAX
146 return xdr_u_long (xdrs
, (u_long
*) up
);
147 #elif UINT_MAX == USHRT_MAX
148 return xdr_short (xdrs
, (short *) up
);
150 #error unexpected integer sizes in_xdr_u_int()
156 * same as xdr_u_long - open coded to save a proc call!
159 xdr_long (XDR
*xdrs
, long *lp
)
162 if (xdrs
->x_op
== XDR_ENCODE
)
163 return XDR_PUTLONG (xdrs
, lp
);
165 if (xdrs
->x_op
== XDR_DECODE
)
166 return XDR_GETLONG (xdrs
, lp
);
168 if (xdrs
->x_op
== XDR_FREE
)
175 * XDR unsigned long integers
176 * same as xdr_long - open coded to save a proc call!
179 xdr_u_long (XDR
*xdrs
, u_long
*ulp
)
184 return XDR_GETLONG (xdrs
, (long *) ulp
);
187 return XDR_PUTLONG (xdrs
, (long *) ulp
);
197 * same as xdr_u_hyper - open coded to save a proc call!
200 xdr_hyper (XDR
*xdrs
, quad_t
*llp
)
203 unsigned long int t2
;
205 if (xdrs
->x_op
== XDR_ENCODE
)
207 t1
= (long) ((*llp
) >> 32);
209 return (XDR_PUTLONG(xdrs
, &t1
) && XDR_PUTLONG(xdrs
, &t2
));
212 if (xdrs
->x_op
== XDR_DECODE
)
214 if (!XDR_GETLONG(xdrs
, &t1
) || !XDR_GETLONG(xdrs
, &t2
))
216 *llp
= ((quad_t
) t1
) << 32;
221 if (xdrs
->x_op
== XDR_FREE
)
230 * same as xdr_hyper - open coded to save a proc call!
233 xdr_u_hyper (XDR
*xdrs
, u_quad_t
*ullp
)
238 if (xdrs
->x_op
== XDR_ENCODE
)
240 t1
= (unsigned long) ((*ullp
) >> 32);
241 t2
= (unsigned long) (*ullp
);
242 return (XDR_PUTLONG(xdrs
, &t1
) && XDR_PUTLONG(xdrs
, &t2
));
245 if (xdrs
->x_op
== XDR_DECODE
)
247 if (!XDR_GETLONG(xdrs
, &t1
) || !XDR_GETLONG(xdrs
, &t2
))
249 *ullp
= ((u_quad_t
) t1
) << 32;
254 if (xdrs
->x_op
== XDR_FREE
)
261 xdr_longlong_t (XDR
*xdrs
, quad_t
*llp
)
263 return xdr_hyper (xdrs
, llp
);
267 xdr_u_longlong_t (XDR
*xdrs
, u_quad_t
*ullp
)
269 return xdr_u_hyper (xdrs
, ullp
);
276 xdr_short (XDR
*xdrs
, short *sp
)
284 return XDR_PUTLONG (xdrs
, &l
);
287 if (!XDR_GETLONG (xdrs
, &l
))
301 * XDR unsigned short integers
304 xdr_u_short (XDR
*xdrs
, u_short
*usp
)
312 return XDR_PUTLONG (xdrs
, &l
);
315 if (!XDR_GETLONG (xdrs
, &l
))
333 xdr_char (XDR
*xdrs
, char *cp
)
338 if (!xdr_int (xdrs
, &i
))
347 * XDR an unsigned char
350 xdr_u_char (XDR
*xdrs
, u_char
*cp
)
355 if (!xdr_u_int (xdrs
, &u
))
367 xdr_bool (XDR
*xdrs
, bool_t
*bp
)
374 lb
= *bp
? XDR_TRUE
: XDR_FALSE
;
375 return XDR_PUTLONG (xdrs
, &lb
);
378 if (!XDR_GETLONG (xdrs
, &lb
))
382 *bp
= (lb
== XDR_FALSE
) ? FALSE
: TRUE
;
395 xdr_enum (XDR
*xdrs
, enum_t
*ep
)
400 }; /* used to find the size of an enum */
403 * enums are treated as ints
405 if (sizeof (enum sizecheck
) == 4)
407 #if INT_MAX < LONG_MAX
414 return XDR_PUTLONG (xdrs
, &l
);
417 if (!XDR_GETLONG (xdrs
, &l
))
428 return xdr_long (xdrs
, (long *) ep
);
431 else if (sizeof (enum sizecheck
) == sizeof (short))
433 return xdr_short (xdrs
, (short *) ep
);
443 * Allows the specification of a fixed size sequence of opaque bytes.
444 * cp points to the opaque object and cnt gives the byte length.
447 xdr_opaque (XDR
*xdrs
, caddr_t cp
, u_int cnt
)
450 static char crud
[BYTES_PER_XDR_UNIT
];
453 * if no data we are done
459 * round byte count to full xdr units
461 rndup
= cnt
% BYTES_PER_XDR_UNIT
;
463 rndup
= BYTES_PER_XDR_UNIT
- rndup
;
468 if (!XDR_GETBYTES (xdrs
, cp
, cnt
))
474 return XDR_GETBYTES (xdrs
, (caddr_t
)crud
, rndup
);
477 if (!XDR_PUTBYTES (xdrs
, cp
, cnt
))
483 return XDR_PUTBYTES (xdrs
, xdr_zero
, rndup
);
493 * *cpp is a pointer to the bytes, *sizep is the count.
494 * If *cpp is NULL maxsize bytes are allocated
497 xdr_bytes (xdrs
, cpp
, sizep
, maxsize
)
503 char *sp
= *cpp
; /* sp is the actual string pointer */
507 * first deal with the length since xdr bytes are counted
509 if (!xdr_u_int (xdrs
, sizep
))
514 if ((nodesize
> maxsize
) && (xdrs
->x_op
!= XDR_FREE
))
520 * now deal with the actual bytes
531 *cpp
= sp
= (char *) mem_alloc (nodesize
);
535 (void) fprintf (stderr
, "xdr_bytes: out of memory\n");
541 return xdr_opaque (xdrs
, sp
, nodesize
);
546 mem_free (sp
, nodesize
);
555 * Implemented here due to commonality of the object.
558 xdr_netobj (xdrs
, np
)
563 return xdr_bytes (xdrs
, &np
->n_bytes
, &np
->n_len
, MAX_NETOBJ_SZ
);
567 * XDR a discriminated union
568 * Support routine for discriminated unions.
569 * You create an array of xdrdiscrim structures, terminated with
570 * an entry with a null procedure pointer. The routine gets
571 * the discriminant value and then searches the array of xdrdiscrims
572 * looking for that value. It calls the procedure given in the xdrdiscrim
573 * to handle the discriminant. If there is no specific routine a default
574 * routine may be called.
575 * If there is no specific or default routine an error is returned.
578 xdr_union (xdrs
, dscmp
, unp
, choices
, dfault
)
580 enum_t
*dscmp
; /* enum to decide which arm to work on */
581 char *unp
; /* the union itself */
582 const struct xdr_discrim
*choices
; /* [value, xdr proc] for each arm */
583 xdrproc_t dfault
; /* default xdr routine */
588 * we deal with the discriminator; it's an enum
590 if (!xdr_enum (xdrs
, dscmp
))
597 * search choices for a value that matches the discriminator.
598 * if we find one, execute the xdr routine for that value.
600 for (; choices
->proc
!= NULL_xdrproc_t
; choices
++)
602 if (choices
->value
== dscm
)
603 return (*(choices
->proc
)) (xdrs
, unp
, LASTUNSIGNED
);
607 * no match - execute the default xdr routine if there is one
609 return ((dfault
== NULL_xdrproc_t
) ? FALSE
:
610 (*dfault
) (xdrs
, unp
, LASTUNSIGNED
));
615 * Non-portable xdr primitives.
616 * Care should be taken when moving these routines to new architectures.
621 * XDR null terminated ASCII strings
622 * xdr_string deals with "C strings" - arrays of bytes that are
623 * terminated by a NULL character. The parameter cpp references a
624 * pointer to storage; If the pointer is null, then the necessary
625 * storage is allocated. The last parameter is the max allowed length
626 * of the string as specified by a protocol.
629 xdr_string (xdrs
, cpp
, maxsize
)
634 char *sp
= *cpp
; /* sp is the actual string pointer */
639 * first deal with the length since xdr strings are counted-strings
646 return TRUE
; /* already free */
648 /* fall through... */
657 if (!xdr_u_int (xdrs
, &size
))
668 * now deal with the actual bytes
678 *cpp
= sp
= (char *) mem_alloc (nodesize
);
681 (void) fprintf (stderr
, "xdr_string: out of memory\n");
688 return xdr_opaque (xdrs
, sp
, size
);
691 mem_free (sp
, nodesize
);
699 * Wrapper for xdr_string that can be called directly from
700 * routines like clnt_call
703 xdr_wrapstring (xdrs
, cpp
)
707 if (xdr_string (xdrs
, cpp
, LASTUNSIGNED
))