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
49 #include <rpc/types.h>
57 * constants specific to the xdr "protocol"
59 #define XDR_FALSE ((long) 0)
60 #define XDR_TRUE ((long) 1)
61 #define LASTUNSIGNED ((u_int) 0-1)
66 static const char xdr_zero
[BYTES_PER_XDR_UNIT
] = {0, 0, 0, 0};
69 * Free a data structure using XDR
70 * Not a filter, but a convenient utility nonetheless
73 xdr_free (xdrproc_t proc
, char *objp
)
95 xdr_int (XDR
*xdrs
, int *ip
)
98 #if INT_MAX < LONG_MAX
105 return XDR_PUTLONG (xdrs
, &l
);
108 if (!XDR_GETLONG (xdrs
, &l
))
117 #elif INT_MAX == LONG_MAX
118 return INTUSE(xdr_long
) (xdrs
, (long *) ip
);
119 #elif INT_MAX == SHRT_MAX
120 return INTUSE(xdr_short
) (xdrs
, (short *) ip
);
122 #error unexpected integer sizes in_xdr_int()
128 * XDR unsigned integers
131 xdr_u_int (XDR
*xdrs
, u_int
*up
)
133 #if UINT_MAX < ULONG_MAX
140 return XDR_PUTLONG (xdrs
, &l
);
143 if (!XDR_GETLONG (xdrs
, &l
))
147 *up
= (u_int
) (u_long
) l
;
152 #elif UINT_MAX == ULONG_MAX
153 return INTUSE(xdr_u_long
) (xdrs
, (u_long
*) up
);
154 #elif UINT_MAX == USHRT_MAX
155 return INTUSE(xdr_short
) (xdrs
, (short *) up
);
157 #error unexpected integer sizes in_xdr_u_int()
164 * The definition of xdr_long() is kept for backward
165 * compatibility. Instead xdr_int() should be used.
168 xdr_long (XDR
*xdrs
, long *lp
)
171 if (xdrs
->x_op
== XDR_ENCODE
172 && (sizeof (int32_t) == sizeof (long)
173 || (int32_t) *lp
== *lp
))
174 return XDR_PUTLONG (xdrs
, lp
);
176 if (xdrs
->x_op
== XDR_DECODE
)
177 return XDR_GETLONG (xdrs
, lp
);
179 if (xdrs
->x_op
== XDR_FREE
)
187 * XDR unsigned long integers
188 * The definition of xdr_u_long() is kept for backward
189 * compatibility. Instead xdr_u_int() should be used.
192 xdr_u_long (XDR
*xdrs
, u_long
*ulp
)
200 if (XDR_GETLONG (xdrs
, &tmp
) == FALSE
)
203 *ulp
= (uint32_t) tmp
;
208 if (sizeof (uint32_t) != sizeof (u_long
)
209 && (uint32_t) *ulp
!= *ulp
)
212 return XDR_PUTLONG (xdrs
, (long *) ulp
);
223 * same as xdr_u_hyper - open coded to save a proc call!
226 xdr_hyper (XDR
*xdrs
, quad_t
*llp
)
230 if (xdrs
->x_op
== XDR_ENCODE
)
232 t1
= (long) ((*llp
) >> 32);
234 return (XDR_PUTLONG(xdrs
, &t1
) && XDR_PUTLONG(xdrs
, &t2
));
237 if (xdrs
->x_op
== XDR_DECODE
)
239 if (!XDR_GETLONG(xdrs
, &t1
) || !XDR_GETLONG(xdrs
, &t2
))
241 *llp
= ((quad_t
) t1
) << 32;
242 *llp
|= (uint32_t) t2
;
246 if (xdrs
->x_op
== XDR_FREE
)
256 * same as xdr_hyper - open coded to save a proc call!
259 xdr_u_hyper (XDR
*xdrs
, u_quad_t
*ullp
)
263 if (xdrs
->x_op
== XDR_ENCODE
)
265 t1
= (unsigned long) ((*ullp
) >> 32);
266 t2
= (unsigned long) (*ullp
);
267 return (XDR_PUTLONG(xdrs
, &t1
) && XDR_PUTLONG(xdrs
, &t2
));
270 if (xdrs
->x_op
== XDR_DECODE
)
272 if (!XDR_GETLONG(xdrs
, &t1
) || !XDR_GETLONG(xdrs
, &t2
))
274 *ullp
= ((u_quad_t
) t1
) << 32;
275 *ullp
|= (uint32_t) t2
;
279 if (xdrs
->x_op
== XDR_FREE
)
287 xdr_longlong_t (XDR
*xdrs
, quad_t
*llp
)
289 return INTUSE(xdr_hyper
) (xdrs
, llp
);
293 xdr_u_longlong_t (XDR
*xdrs
, u_quad_t
*ullp
)
295 return INTUSE(xdr_u_hyper
) (xdrs
, ullp
);
302 xdr_short (XDR
*xdrs
, short *sp
)
310 return XDR_PUTLONG (xdrs
, &l
);
313 if (!XDR_GETLONG (xdrs
, &l
))
328 * XDR unsigned short integers
331 xdr_u_short (XDR
*xdrs
, u_short
*usp
)
339 return XDR_PUTLONG (xdrs
, &l
);
342 if (!XDR_GETLONG (xdrs
, &l
))
346 *usp
= (u_short
) (u_long
) l
;
361 xdr_char (XDR
*xdrs
, char *cp
)
366 if (!INTUSE(xdr_int
) (xdrs
, &i
))
375 * XDR an unsigned char
378 xdr_u_char (XDR
*xdrs
, u_char
*cp
)
383 if (!INTUSE(xdr_u_int
) (xdrs
, &u
))
395 xdr_bool (XDR
*xdrs
, bool_t
*bp
)
402 lb
= *bp
? XDR_TRUE
: XDR_FALSE
;
403 return XDR_PUTLONG (xdrs
, &lb
);
406 if (!XDR_GETLONG (xdrs
, &lb
))
410 *bp
= (lb
== XDR_FALSE
) ? FALSE
: TRUE
;
424 xdr_enum (XDR
*xdrs
, enum_t
*ep
)
429 }; /* used to find the size of an enum */
432 * enums are treated as ints
434 if (sizeof (enum sizecheck
) == 4)
436 #if INT_MAX < LONG_MAX
443 return XDR_PUTLONG (xdrs
, &l
);
446 if (!XDR_GETLONG (xdrs
, &l
))
457 return INTUSE(xdr_long
) (xdrs
, (long *) ep
);
460 else if (sizeof (enum sizecheck
) == sizeof (short))
462 return INTUSE(xdr_short
) (xdrs
, (short *) ep
);
473 * Allows the specification of a fixed size sequence of opaque bytes.
474 * cp points to the opaque object and cnt gives the byte length.
477 xdr_opaque (XDR
*xdrs
, caddr_t cp
, u_int cnt
)
480 static char crud
[BYTES_PER_XDR_UNIT
];
483 * if no data we are done
489 * round byte count to full xdr units
491 rndup
= cnt
% BYTES_PER_XDR_UNIT
;
493 rndup
= BYTES_PER_XDR_UNIT
- rndup
;
498 if (!XDR_GETBYTES (xdrs
, cp
, cnt
))
504 return XDR_GETBYTES (xdrs
, (caddr_t
)crud
, rndup
);
507 if (!XDR_PUTBYTES (xdrs
, cp
, cnt
))
513 return XDR_PUTBYTES (xdrs
, xdr_zero
, rndup
);
524 * *cpp is a pointer to the bytes, *sizep is the count.
525 * If *cpp is NULL maxsize bytes are allocated
528 xdr_bytes (xdrs
, cpp
, sizep
, maxsize
)
534 char *sp
= *cpp
; /* sp is the actual string pointer */
538 * first deal with the length since xdr bytes are counted
540 if (!INTUSE(xdr_u_int
) (xdrs
, sizep
))
545 if ((nodesize
> maxsize
) && (xdrs
->x_op
!= XDR_FREE
))
551 * now deal with the actual bytes
562 *cpp
= sp
= (char *) mem_alloc (nodesize
);
566 (void) __fxprintf (NULL
, "%s: %s", __func__
, _("out of memory\n"));
572 return INTUSE(xdr_opaque
) (xdrs
, sp
, nodesize
);
577 mem_free (sp
, nodesize
);
587 * Implemented here due to commonality of the object.
590 xdr_netobj (xdrs
, np
)
595 return INTUSE(xdr_bytes
) (xdrs
, &np
->n_bytes
, &np
->n_len
, MAX_NETOBJ_SZ
);
600 * XDR a discriminated union
601 * Support routine for discriminated unions.
602 * You create an array of xdrdiscrim structures, terminated with
603 * an entry with a null procedure pointer. The routine gets
604 * the discriminant value and then searches the array of xdrdiscrims
605 * looking for that value. It calls the procedure given in the xdrdiscrim
606 * to handle the discriminant. If there is no specific routine a default
607 * routine may be called.
608 * If there is no specific or default routine an error is returned.
611 xdr_union (xdrs
, dscmp
, unp
, choices
, dfault
)
613 enum_t
*dscmp
; /* enum to decide which arm to work on */
614 char *unp
; /* the union itself */
615 const struct xdr_discrim
*choices
; /* [value, xdr proc] for each arm */
616 xdrproc_t dfault
; /* default xdr routine */
621 * we deal with the discriminator; it's an enum
623 if (!INTUSE(xdr_enum
) (xdrs
, dscmp
))
630 * search choices for a value that matches the discriminator.
631 * if we find one, execute the xdr routine for that value.
633 for (; choices
->proc
!= NULL_xdrproc_t
; choices
++)
635 if (choices
->value
== dscm
)
636 return (*(choices
->proc
)) (xdrs
, unp
, LASTUNSIGNED
);
640 * no match - execute the default xdr routine if there is one
642 return ((dfault
== NULL_xdrproc_t
) ? FALSE
:
643 (*dfault
) (xdrs
, unp
, LASTUNSIGNED
));
649 * Non-portable xdr primitives.
650 * Care should be taken when moving these routines to new architectures.
655 * XDR null terminated ASCII strings
656 * xdr_string deals with "C strings" - arrays of bytes that are
657 * terminated by a NULL character. The parameter cpp references a
658 * pointer to storage; If the pointer is null, then the necessary
659 * storage is allocated. The last parameter is the max allowed length
660 * of the string as specified by a protocol.
663 xdr_string (xdrs
, cpp
, maxsize
)
668 char *sp
= *cpp
; /* sp is the actual string pointer */
673 * first deal with the length since xdr strings are counted-strings
680 return TRUE
; /* already free */
682 /* fall through... */
691 if (!INTUSE(xdr_u_int
) (xdrs
, &size
))
702 /* This means an overflow. It a bug in the caller which
703 provided a too large maxsize but nevertheless catch it
709 * now deal with the actual bytes
715 *cpp
= sp
= (char *) mem_alloc (nodesize
);
718 (void) __fxprintf (NULL
, "%s: %s", __func__
, _("out of memory\n"));
725 return INTUSE(xdr_opaque
) (xdrs
, sp
, size
);
728 mem_free (sp
, nodesize
);
737 * Wrapper for xdr_string that can be called directly from
738 * routines like clnt_call
741 xdr_wrapstring (xdrs
, cpp
)
745 if (INTUSE(xdr_string
) (xdrs
, cpp
, LASTUNSIGNED
))