2 * xdr.c, Generic XDR routines implementation.
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * These are the "generic" xdr routines used to serialize and de-serialize
34 * most common data items. See xdr.h for more info on the interface to
44 #include <rpc/types.h>
49 * constants specific to the xdr "protocol"
51 #define XDR_FALSE ((long) 0)
52 #define XDR_TRUE ((long) 1)
53 #define LASTUNSIGNED ((u_int) 0-1)
58 static const char xdr_zero
[BYTES_PER_XDR_UNIT
] = {0, 0, 0, 0};
61 * Free a data structure using XDR
62 * Not a filter, but a convenient utility nonetheless
65 xdr_free (xdrproc_t proc
, char *objp
)
72 #ifdef EXPORT_RPC_SYMBOLS
73 libc_hidden_def (xdr_free
)
75 libc_hidden_nolink_sunrpc (xdr_free
, GLIBC_2_0
)
86 #ifdef EXPORT_RPC_SYMBOLS
87 libc_hidden_def (xdr_void
)
89 libc_hidden_nolink_sunrpc (xdr_void
, GLIBC_2_0
)
96 xdr_int (XDR
*xdrs
, int *ip
)
99 #if INT_MAX < LONG_MAX
106 return XDR_PUTLONG (xdrs
, &l
);
109 if (!XDR_GETLONG (xdrs
, &l
))
118 #elif INT_MAX == LONG_MAX
119 return xdr_long (xdrs
, (long *) ip
);
120 #elif INT_MAX == SHRT_MAX
121 return xdr_short (xdrs
, (short *) ip
);
123 #error unexpected integer sizes in_xdr_int()
126 #ifdef EXPORT_RPC_SYMBOLS
127 libc_hidden_def (xdr_int
)
129 libc_hidden_nolink_sunrpc (xdr_int
, GLIBC_2_0
)
133 * XDR unsigned integers
136 xdr_u_int (XDR
*xdrs
, u_int
*up
)
138 #if UINT_MAX < ULONG_MAX
145 return XDR_PUTLONG (xdrs
, &l
);
148 if (!XDR_GETLONG (xdrs
, &l
))
152 *up
= (u_int
) (u_long
) l
;
157 #elif UINT_MAX == ULONG_MAX
158 return xdr_u_long (xdrs
, (u_long
*) up
);
159 #elif UINT_MAX == USHRT_MAX
160 return xdr_short (xdrs
, (short *) up
);
162 #error unexpected integer sizes in_xdr_u_int()
165 #ifdef EXPORT_RPC_SYMBOLS
166 libc_hidden_def (xdr_u_int
)
168 libc_hidden_nolink_sunrpc (xdr_u_int
, GLIBC_2_0
)
173 * The definition of xdr_long() is kept for backward
174 * compatibility. Instead xdr_int() should be used.
177 xdr_long (XDR
*xdrs
, long *lp
)
180 if (xdrs
->x_op
== XDR_ENCODE
181 && (sizeof (int32_t) == sizeof (long)
182 || (int32_t) *lp
== *lp
))
183 return XDR_PUTLONG (xdrs
, lp
);
185 if (xdrs
->x_op
== XDR_DECODE
)
186 return XDR_GETLONG (xdrs
, lp
);
188 if (xdrs
->x_op
== XDR_FREE
)
193 #ifdef EXPORT_RPC_SYMBOLS
194 libc_hidden_def (xdr_long
)
196 libc_hidden_nolink_sunrpc (xdr_long
, GLIBC_2_0
)
200 * XDR unsigned long integers
201 * The definition of xdr_u_long() is kept for backward
202 * compatibility. Instead xdr_u_int() should be used.
205 xdr_u_long (XDR
*xdrs
, u_long
*ulp
)
213 if (XDR_GETLONG (xdrs
, &tmp
) == FALSE
)
216 *ulp
= (uint32_t) tmp
;
221 if (sizeof (uint32_t) != sizeof (u_long
)
222 && (uint32_t) *ulp
!= *ulp
)
225 return XDR_PUTLONG (xdrs
, (long *) ulp
);
232 #ifdef EXPORT_RPC_SYMBOLS
233 libc_hidden_def (xdr_u_long
)
235 libc_hidden_nolink_sunrpc (xdr_u_long
, GLIBC_2_0
)
240 * same as xdr_u_hyper - open coded to save a proc call!
243 xdr_hyper (XDR
*xdrs
, quad_t
*llp
)
247 if (xdrs
->x_op
== XDR_ENCODE
)
249 t1
= (long) ((*llp
) >> 32);
251 return (XDR_PUTLONG(xdrs
, &t1
) && XDR_PUTLONG(xdrs
, &t2
));
254 if (xdrs
->x_op
== XDR_DECODE
)
256 if (!XDR_GETLONG(xdrs
, &t1
) || !XDR_GETLONG(xdrs
, &t2
))
258 *llp
= ((quad_t
) t1
) << 32;
259 *llp
|= (uint32_t) t2
;
263 if (xdrs
->x_op
== XDR_FREE
)
268 #ifdef EXPORT_RPC_SYMBOLS
269 libc_hidden_def (xdr_hyper
)
271 libc_hidden_nolink_sunrpc (xdr_hyper
, GLIBC_2_1_1
)
276 * same as xdr_hyper - open coded to save a proc call!
279 xdr_u_hyper (XDR
*xdrs
, u_quad_t
*ullp
)
283 if (xdrs
->x_op
== XDR_ENCODE
)
285 t1
= (unsigned long) ((*ullp
) >> 32);
286 t2
= (unsigned long) (*ullp
);
287 return (XDR_PUTLONG(xdrs
, &t1
) && XDR_PUTLONG(xdrs
, &t2
));
290 if (xdrs
->x_op
== XDR_DECODE
)
292 if (!XDR_GETLONG(xdrs
, &t1
) || !XDR_GETLONG(xdrs
, &t2
))
294 *ullp
= ((u_quad_t
) t1
) << 32;
295 *ullp
|= (uint32_t) t2
;
299 if (xdrs
->x_op
== XDR_FREE
)
304 #ifdef EXPORT_RPC_SYMBOLS
305 libc_hidden_def (xdr_u_hyper
)
307 libc_hidden_nolink_sunrpc (xdr_u_hyper
, GLIBC_2_1_1
)
311 xdr_longlong_t (XDR
*xdrs
, quad_t
*llp
)
313 return xdr_hyper (xdrs
, llp
);
315 #ifdef EXPORT_RPC_SYMBOLS
316 libc_hidden_def (xdr_longlong_t
)
318 libc_hidden_nolink_sunrpc (xdr_longlong_t
, GLIBC_2_1_1
)
322 xdr_u_longlong_t (XDR
*xdrs
, u_quad_t
*ullp
)
324 return xdr_u_hyper (xdrs
, ullp
);
326 #ifdef EXPORT_RPC_SYMBOLS
327 libc_hidden_def (xdr_u_longlong_t
)
329 libc_hidden_nolink_sunrpc (xdr_u_longlong_t
, GLIBC_2_1_1
)
336 xdr_short (XDR
*xdrs
, short *sp
)
344 return XDR_PUTLONG (xdrs
, &l
);
347 if (!XDR_GETLONG (xdrs
, &l
))
359 #ifdef EXPORT_RPC_SYMBOLS
360 libc_hidden_def (xdr_short
)
362 libc_hidden_nolink_sunrpc (xdr_short
, GLIBC_2_0
)
366 * XDR unsigned short integers
369 xdr_u_short (XDR
*xdrs
, u_short
*usp
)
377 return XDR_PUTLONG (xdrs
, &l
);
380 if (!XDR_GETLONG (xdrs
, &l
))
384 *usp
= (u_short
) (u_long
) l
;
392 #ifdef EXPORT_RPC_SYMBOLS
393 libc_hidden_def (xdr_u_short
)
395 libc_hidden_nolink_sunrpc (xdr_u_short
, GLIBC_2_0
)
403 xdr_char (XDR
*xdrs
, char *cp
)
408 if (!xdr_int (xdrs
, &i
))
415 #ifdef EXPORT_RPC_SYMBOLS
416 libc_hidden_def (xdr_char
)
418 libc_hidden_nolink_sunrpc (xdr_char
, GLIBC_2_0
)
422 * XDR an unsigned char
425 xdr_u_char (XDR
*xdrs
, u_char
*cp
)
430 if (!xdr_u_int (xdrs
, &u
))
437 #ifdef EXPORT_RPC_SYMBOLS
438 libc_hidden_def (xdr_u_char
)
440 libc_hidden_nolink_sunrpc (xdr_u_char
, GLIBC_2_0
)
447 xdr_bool (XDR
*xdrs
, bool_t
*bp
)
454 lb
= *bp
? XDR_TRUE
: XDR_FALSE
;
455 return XDR_PUTLONG (xdrs
, &lb
);
458 if (!XDR_GETLONG (xdrs
, &lb
))
462 *bp
= (lb
== XDR_FALSE
) ? FALSE
: TRUE
;
470 #ifdef EXPORT_RPC_SYMBOLS
471 libc_hidden_def (xdr_bool
)
473 libc_hidden_nolink_sunrpc (xdr_bool
, GLIBC_2_0
)
480 xdr_enum (XDR
*xdrs
, enum_t
*ep
)
485 }; /* used to find the size of an enum */
488 * enums are treated as ints
490 if (sizeof (enum sizecheck
) == 4)
492 #if INT_MAX < LONG_MAX
499 return XDR_PUTLONG (xdrs
, &l
);
502 if (!XDR_GETLONG (xdrs
, &l
))
513 return xdr_long (xdrs
, (long *) ep
);
516 else if (sizeof (enum sizecheck
) == sizeof (short))
518 return xdr_short (xdrs
, (short *) ep
);
525 #ifdef EXPORT_RPC_SYMBOLS
526 libc_hidden_def (xdr_enum
)
528 libc_hidden_nolink_sunrpc (xdr_enum
, GLIBC_2_0
)
533 * Allows the specification of a fixed size sequence of opaque bytes.
534 * cp points to the opaque object and cnt gives the byte length.
537 xdr_opaque (XDR
*xdrs
, caddr_t cp
, u_int cnt
)
540 static char crud
[BYTES_PER_XDR_UNIT
];
543 * if no data we are done
549 * round byte count to full xdr units
551 rndup
= cnt
% BYTES_PER_XDR_UNIT
;
553 rndup
= BYTES_PER_XDR_UNIT
- rndup
;
558 if (!XDR_GETBYTES (xdrs
, cp
, cnt
))
564 return XDR_GETBYTES (xdrs
, (caddr_t
)crud
, rndup
);
567 if (!XDR_PUTBYTES (xdrs
, cp
, cnt
))
573 return XDR_PUTBYTES (xdrs
, xdr_zero
, rndup
);
580 #ifdef EXPORT_RPC_SYMBOLS
581 libc_hidden_def (xdr_opaque
)
583 libc_hidden_nolink_sunrpc (xdr_opaque
, GLIBC_2_0
)
588 * *cpp is a pointer to the bytes, *sizep is the count.
589 * If *cpp is NULL maxsize bytes are allocated
592 xdr_bytes (xdrs
, cpp
, sizep
, maxsize
)
598 char *sp
= *cpp
; /* sp is the actual string pointer */
602 * first deal with the length since xdr bytes are counted
604 if (!xdr_u_int (xdrs
, sizep
))
609 if ((nodesize
> maxsize
) && (xdrs
->x_op
!= XDR_FREE
))
615 * now deal with the actual bytes
626 *cpp
= sp
= (char *) mem_alloc (nodesize
);
630 (void) __fxprintf (NULL
, "%s: %s", __func__
, _("out of memory\n"));
636 return xdr_opaque (xdrs
, sp
, nodesize
);
641 mem_free (sp
, nodesize
);
648 #ifdef EXPORT_RPC_SYMBOLS
649 libc_hidden_def (xdr_bytes
)
651 libc_hidden_nolink_sunrpc (xdr_bytes
, GLIBC_2_0
)
655 * Implemented here due to commonality of the object.
658 xdr_netobj (xdrs
, np
)
663 return xdr_bytes (xdrs
, &np
->n_bytes
, &np
->n_len
, MAX_NETOBJ_SZ
);
665 #ifdef EXPORT_RPC_SYMBOLS
666 libc_hidden_def (xdr_netobj
)
668 libc_hidden_nolink_sunrpc (xdr_netobj
, GLIBC_2_0
)
672 * XDR a discriminated union
673 * Support routine for discriminated unions.
674 * You create an array of xdrdiscrim structures, terminated with
675 * an entry with a null procedure pointer. The routine gets
676 * the discriminant value and then searches the array of xdrdiscrims
677 * looking for that value. It calls the procedure given in the xdrdiscrim
678 * to handle the discriminant. If there is no specific routine a default
679 * routine may be called.
680 * If there is no specific or default routine an error is returned.
683 xdr_union (xdrs
, dscmp
, unp
, choices
, dfault
)
685 enum_t
*dscmp
; /* enum to decide which arm to work on */
686 char *unp
; /* the union itself */
687 const struct xdr_discrim
*choices
; /* [value, xdr proc] for each arm */
688 xdrproc_t dfault
; /* default xdr routine */
693 * we deal with the discriminator; it's an enum
695 if (!xdr_enum (xdrs
, dscmp
))
702 * search choices for a value that matches the discriminator.
703 * if we find one, execute the xdr routine for that value.
705 for (; choices
->proc
!= NULL_xdrproc_t
; choices
++)
707 if (choices
->value
== dscm
)
708 return (*(choices
->proc
)) (xdrs
, unp
, LASTUNSIGNED
);
712 * no match - execute the default xdr routine if there is one
714 return ((dfault
== NULL_xdrproc_t
) ? FALSE
:
715 (*dfault
) (xdrs
, unp
, LASTUNSIGNED
));
717 libc_hidden_nolink_sunrpc (xdr_union
, GLIBC_2_0
)
721 * Non-portable xdr primitives.
722 * Care should be taken when moving these routines to new architectures.
727 * XDR null terminated ASCII strings
728 * xdr_string deals with "C strings" - arrays of bytes that are
729 * terminated by a NULL character. The parameter cpp references a
730 * pointer to storage; If the pointer is null, then the necessary
731 * storage is allocated. The last parameter is the max allowed length
732 * of the string as specified by a protocol.
735 xdr_string (xdrs
, cpp
, maxsize
)
740 char *sp
= *cpp
; /* sp is the actual string pointer */
745 * first deal with the length since xdr strings are counted-strings
752 return TRUE
; /* already free */
754 /* fall through... */
763 if (!xdr_u_int (xdrs
, &size
))
774 /* This means an overflow. It a bug in the caller which
775 provided a too large maxsize but nevertheless catch it
781 * now deal with the actual bytes
787 *cpp
= sp
= (char *) mem_alloc (nodesize
);
790 (void) __fxprintf (NULL
, "%s: %s", __func__
, _("out of memory\n"));
797 return xdr_opaque (xdrs
, sp
, size
);
800 mem_free (sp
, nodesize
);
806 #ifdef EXPORT_RPC_SYMBOLS
807 libc_hidden_def (xdr_string
)
809 libc_hidden_nolink_sunrpc (xdr_string
, GLIBC_2_0
)
813 * Wrapper for xdr_string that can be called directly from
814 * routines like clnt_call
817 xdr_wrapstring (xdrs
, cpp
)
821 if (xdr_string (xdrs
, cpp
, LASTUNSIGNED
))
827 #ifdef EXPORT_RPC_SYMBOLS
828 libc_hidden_def (xdr_wrapstring
)
830 libc_hidden_nolink_sunrpc (xdr_wrapstring
, GLIBC_2_0
)