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
45 #include <rpc/types.h>
50 * constants specific to the xdr "protocol"
52 #define XDR_FALSE ((long) 0)
53 #define XDR_TRUE ((long) 1)
54 #define LASTUNSIGNED ((u_int) 0-1)
59 static const char xdr_zero
[BYTES_PER_XDR_UNIT
] = {0, 0, 0, 0};
62 * Free a data structure using XDR
63 * Not a filter, but a convenient utility nonetheless
66 xdr_free (xdrproc_t proc
, char *objp
)
73 #ifdef EXPORT_RPC_SYMBOLS
74 libc_hidden_def (xdr_free
)
76 libc_hidden_nolink_sunrpc (xdr_free
, GLIBC_2_0
)
87 #ifdef EXPORT_RPC_SYMBOLS
88 libc_hidden_def (xdr_void
)
90 libc_hidden_nolink_sunrpc (xdr_void
, GLIBC_2_0
)
97 xdr_int (XDR
*xdrs
, int *ip
)
100 #if INT_MAX < LONG_MAX
107 return XDR_PUTLONG (xdrs
, &l
);
110 if (!XDR_GETLONG (xdrs
, &l
))
119 #elif INT_MAX == LONG_MAX
120 return xdr_long (xdrs
, (long *) ip
);
121 #elif INT_MAX == SHRT_MAX
122 return xdr_short (xdrs
, (short *) ip
);
124 #error unexpected integer sizes in_xdr_int()
127 #ifdef EXPORT_RPC_SYMBOLS
128 libc_hidden_def (xdr_int
)
130 libc_hidden_nolink_sunrpc (xdr_int
, GLIBC_2_0
)
134 * XDR unsigned integers
137 xdr_u_int (XDR
*xdrs
, u_int
*up
)
139 #if UINT_MAX < ULONG_MAX
146 return XDR_PUTLONG (xdrs
, &l
);
149 if (!XDR_GETLONG (xdrs
, &l
))
153 *up
= (u_int
) (u_long
) l
;
158 #elif UINT_MAX == ULONG_MAX
159 return xdr_u_long (xdrs
, (u_long
*) up
);
160 #elif UINT_MAX == USHRT_MAX
161 return xdr_short (xdrs
, (short *) up
);
163 #error unexpected integer sizes in_xdr_u_int()
166 #ifdef EXPORT_RPC_SYMBOLS
167 libc_hidden_def (xdr_u_int
)
169 libc_hidden_nolink_sunrpc (xdr_u_int
, GLIBC_2_0
)
174 * The definition of xdr_long() is kept for backward
175 * compatibility. Instead xdr_int() should be used.
178 xdr_long (XDR
*xdrs
, long *lp
)
181 if (xdrs
->x_op
== XDR_ENCODE
182 && (sizeof (int32_t) == sizeof (long)
183 || (int32_t) *lp
== *lp
))
184 return XDR_PUTLONG (xdrs
, lp
);
186 if (xdrs
->x_op
== XDR_DECODE
)
187 return XDR_GETLONG (xdrs
, lp
);
189 if (xdrs
->x_op
== XDR_FREE
)
194 #ifdef EXPORT_RPC_SYMBOLS
195 libc_hidden_def (xdr_long
)
197 libc_hidden_nolink_sunrpc (xdr_long
, GLIBC_2_0
)
201 * XDR unsigned long integers
202 * The definition of xdr_u_long() is kept for backward
203 * compatibility. Instead xdr_u_int() should be used.
206 xdr_u_long (XDR
*xdrs
, u_long
*ulp
)
214 if (XDR_GETLONG (xdrs
, &tmp
) == FALSE
)
217 *ulp
= (uint32_t) tmp
;
222 if (sizeof (uint32_t) != sizeof (u_long
)
223 && (uint32_t) *ulp
!= *ulp
)
226 return XDR_PUTLONG (xdrs
, (long *) ulp
);
233 #ifdef EXPORT_RPC_SYMBOLS
234 libc_hidden_def (xdr_u_long
)
236 libc_hidden_nolink_sunrpc (xdr_u_long
, GLIBC_2_0
)
241 * same as xdr_u_hyper - open coded to save a proc call!
244 xdr_hyper (XDR
*xdrs
, quad_t
*llp
)
248 if (xdrs
->x_op
== XDR_ENCODE
)
250 t1
= (long) ((*llp
) >> 32);
252 return (XDR_PUTLONG(xdrs
, &t1
) && XDR_PUTLONG(xdrs
, &t2
));
255 if (xdrs
->x_op
== XDR_DECODE
)
257 if (!XDR_GETLONG(xdrs
, &t1
) || !XDR_GETLONG(xdrs
, &t2
))
259 *llp
= ((quad_t
) t1
) << 32;
260 *llp
|= (uint32_t) t2
;
264 if (xdrs
->x_op
== XDR_FREE
)
269 #ifdef EXPORT_RPC_SYMBOLS
270 libc_hidden_def (xdr_hyper
)
272 libc_hidden_nolink_sunrpc (xdr_hyper
, GLIBC_2_1_1
)
277 * same as xdr_hyper - open coded to save a proc call!
280 xdr_u_hyper (XDR
*xdrs
, u_quad_t
*ullp
)
284 if (xdrs
->x_op
== XDR_ENCODE
)
286 t1
= (unsigned long) ((*ullp
) >> 32);
287 t2
= (unsigned long) (*ullp
);
288 return (XDR_PUTLONG(xdrs
, &t1
) && XDR_PUTLONG(xdrs
, &t2
));
291 if (xdrs
->x_op
== XDR_DECODE
)
293 if (!XDR_GETLONG(xdrs
, &t1
) || !XDR_GETLONG(xdrs
, &t2
))
295 *ullp
= ((u_quad_t
) t1
) << 32;
296 *ullp
|= (uint32_t) t2
;
300 if (xdrs
->x_op
== XDR_FREE
)
305 #ifdef EXPORT_RPC_SYMBOLS
306 libc_hidden_def (xdr_u_hyper
)
308 libc_hidden_nolink_sunrpc (xdr_u_hyper
, GLIBC_2_1_1
)
312 xdr_longlong_t (XDR
*xdrs
, quad_t
*llp
)
314 return xdr_hyper (xdrs
, llp
);
316 #ifdef EXPORT_RPC_SYMBOLS
317 libc_hidden_def (xdr_longlong_t
)
319 libc_hidden_nolink_sunrpc (xdr_longlong_t
, GLIBC_2_1_1
)
323 xdr_u_longlong_t (XDR
*xdrs
, u_quad_t
*ullp
)
325 return xdr_u_hyper (xdrs
, ullp
);
327 #ifdef EXPORT_RPC_SYMBOLS
328 libc_hidden_def (xdr_u_longlong_t
)
330 libc_hidden_nolink_sunrpc (xdr_u_longlong_t
, GLIBC_2_1_1
)
337 xdr_short (XDR
*xdrs
, short *sp
)
345 return XDR_PUTLONG (xdrs
, &l
);
348 if (!XDR_GETLONG (xdrs
, &l
))
360 #ifdef EXPORT_RPC_SYMBOLS
361 libc_hidden_def (xdr_short
)
363 libc_hidden_nolink_sunrpc (xdr_short
, GLIBC_2_0
)
367 * XDR unsigned short integers
370 xdr_u_short (XDR
*xdrs
, u_short
*usp
)
378 return XDR_PUTLONG (xdrs
, &l
);
381 if (!XDR_GETLONG (xdrs
, &l
))
385 *usp
= (u_short
) (u_long
) l
;
393 #ifdef EXPORT_RPC_SYMBOLS
394 libc_hidden_def (xdr_u_short
)
396 libc_hidden_nolink_sunrpc (xdr_u_short
, GLIBC_2_0
)
404 xdr_char (XDR
*xdrs
, char *cp
)
409 if (!xdr_int (xdrs
, &i
))
416 #ifdef EXPORT_RPC_SYMBOLS
417 libc_hidden_def (xdr_char
)
419 libc_hidden_nolink_sunrpc (xdr_char
, GLIBC_2_0
)
423 * XDR an unsigned char
426 xdr_u_char (XDR
*xdrs
, u_char
*cp
)
431 if (!xdr_u_int (xdrs
, &u
))
438 #ifdef EXPORT_RPC_SYMBOLS
439 libc_hidden_def (xdr_u_char
)
441 libc_hidden_nolink_sunrpc (xdr_u_char
, GLIBC_2_0
)
448 xdr_bool (XDR
*xdrs
, bool_t
*bp
)
455 lb
= *bp
? XDR_TRUE
: XDR_FALSE
;
456 return XDR_PUTLONG (xdrs
, &lb
);
459 if (!XDR_GETLONG (xdrs
, &lb
))
463 *bp
= (lb
== XDR_FALSE
) ? FALSE
: TRUE
;
471 #ifdef EXPORT_RPC_SYMBOLS
472 libc_hidden_def (xdr_bool
)
474 libc_hidden_nolink_sunrpc (xdr_bool
, GLIBC_2_0
)
481 xdr_enum (XDR
*xdrs
, enum_t
*ep
)
486 }; /* used to find the size of an enum */
489 * enums are treated as ints
491 if (sizeof (enum sizecheck
) == 4)
493 #if INT_MAX < LONG_MAX
500 return XDR_PUTLONG (xdrs
, &l
);
503 if (!XDR_GETLONG (xdrs
, &l
))
514 return xdr_long (xdrs
, (long *) ep
);
517 else if (sizeof (enum sizecheck
) == sizeof (short))
519 return xdr_short (xdrs
, (short *) ep
);
526 #ifdef EXPORT_RPC_SYMBOLS
527 libc_hidden_def (xdr_enum
)
529 libc_hidden_nolink_sunrpc (xdr_enum
, GLIBC_2_0
)
534 * Allows the specification of a fixed size sequence of opaque bytes.
535 * cp points to the opaque object and cnt gives the byte length.
538 xdr_opaque (XDR
*xdrs
, caddr_t cp
, u_int cnt
)
541 static char crud
[BYTES_PER_XDR_UNIT
];
544 * if no data we are done
550 * round byte count to full xdr units
552 rndup
= cnt
% BYTES_PER_XDR_UNIT
;
554 rndup
= BYTES_PER_XDR_UNIT
- rndup
;
559 if (!XDR_GETBYTES (xdrs
, cp
, cnt
))
565 return XDR_GETBYTES (xdrs
, (caddr_t
)crud
, rndup
);
568 if (!XDR_PUTBYTES (xdrs
, cp
, cnt
))
574 return XDR_PUTBYTES (xdrs
, xdr_zero
, rndup
);
581 #ifdef EXPORT_RPC_SYMBOLS
582 libc_hidden_def (xdr_opaque
)
584 libc_hidden_nolink_sunrpc (xdr_opaque
, GLIBC_2_0
)
589 * *cpp is a pointer to the bytes, *sizep is the count.
590 * If *cpp is NULL maxsize bytes are allocated
593 xdr_bytes (XDR
*xdrs
, char **cpp
, u_int
*sizep
, u_int maxsize
)
595 char *sp
= *cpp
; /* sp is the actual string pointer */
599 * first deal with the length since xdr bytes are counted
601 if (!xdr_u_int (xdrs
, sizep
))
606 if ((nodesize
> maxsize
) && (xdrs
->x_op
!= XDR_FREE
))
612 * now deal with the actual bytes
623 *cpp
= sp
= (char *) mem_alloc (nodesize
);
627 (void) __fxprintf (NULL
, "%s: %s", __func__
, _("out of memory\n"));
633 return xdr_opaque (xdrs
, sp
, nodesize
);
638 mem_free (sp
, nodesize
);
645 #ifdef EXPORT_RPC_SYMBOLS
646 libc_hidden_def (xdr_bytes
)
648 libc_hidden_nolink_sunrpc (xdr_bytes
, GLIBC_2_0
)
652 * Implemented here due to commonality of the object.
655 xdr_netobj (XDR
*xdrs
, struct netobj
*np
)
658 return xdr_bytes (xdrs
, &np
->n_bytes
, &np
->n_len
, MAX_NETOBJ_SZ
);
660 #ifdef EXPORT_RPC_SYMBOLS
661 libc_hidden_def (xdr_netobj
)
663 libc_hidden_nolink_sunrpc (xdr_netobj
, GLIBC_2_0
)
667 * XDR a discriminated union
668 * Support routine for discriminated unions.
669 * You create an array of xdrdiscrim structures, terminated with
670 * an entry with a null procedure pointer. The routine gets
671 * the discriminant value and then searches the array of xdrdiscrims
672 * looking for that value. It calls the procedure given in the xdrdiscrim
673 * to handle the discriminant. If there is no specific routine a default
674 * routine may be called.
675 * If there is no specific or default routine an error is returned.
678 xdr_union (XDR
*xdrs
,
679 /* enum to decide which arm to work on */
681 /* the union itself */
683 /* [value, xdr proc] for each arm */
684 const struct xdr_discrim
*choices
,
685 /* default xdr routine */
691 * we deal with the discriminator; it's an enum
693 if (!xdr_enum (xdrs
, dscmp
))
700 * search choices for a value that matches the discriminator.
701 * if we find one, execute the xdr routine for that value.
703 for (; choices
->proc
!= NULL_xdrproc_t
; choices
++)
705 if (choices
->value
== dscm
)
706 return (*(choices
->proc
)) (xdrs
, unp
, LASTUNSIGNED
);
710 * no match - execute the default xdr routine if there is one
712 return ((dfault
== NULL_xdrproc_t
) ? FALSE
:
713 (*dfault
) (xdrs
, unp
, LASTUNSIGNED
));
715 libc_hidden_nolink_sunrpc (xdr_union
, GLIBC_2_0
)
719 * Non-portable xdr primitives.
720 * Care should be taken when moving these routines to new architectures.
725 * XDR null terminated ASCII strings
726 * xdr_string deals with "C strings" - arrays of bytes that are
727 * terminated by a NULL character. The parameter cpp references a
728 * pointer to storage; If the pointer is null, then the necessary
729 * storage is allocated. The last parameter is the max allowed length
730 * of the string as specified by a protocol.
733 xdr_string (XDR
*xdrs
, char **cpp
, u_int maxsize
)
735 char *sp
= *cpp
; /* sp is the actual string pointer */
736 /* Initialize to silence the compiler. It is not really needed because SIZE
737 never actually gets used without being initialized. */
742 * first deal with the length since xdr strings are counted-strings
749 return TRUE
; /* already free */
751 /* fall through... */
760 if (!xdr_u_int (xdrs
, &size
))
771 /* This means an overflow. It a bug in the caller which
772 provided a too large maxsize but nevertheless catch it
778 * now deal with the actual bytes
784 *cpp
= sp
= (char *) mem_alloc (nodesize
);
787 (void) __fxprintf (NULL
, "%s: %s", __func__
, _("out of memory\n"));
794 return xdr_opaque (xdrs
, sp
, size
);
797 mem_free (sp
, nodesize
);
803 #ifdef EXPORT_RPC_SYMBOLS
804 libc_hidden_def (xdr_string
)
806 libc_hidden_nolink_sunrpc (xdr_string
, GLIBC_2_0
)
810 * Wrapper for xdr_string that can be called directly from
811 * routines like clnt_call
814 xdr_wrapstring (XDR
*xdrs
, char **cpp
)
816 if (xdr_string (xdrs
, cpp
, LASTUNSIGNED
))
822 #ifdef EXPORT_RPC_SYMBOLS
823 libc_hidden_def (xdr_wrapstring
)
825 libc_hidden_nolink_sunrpc (xdr_wrapstring
, GLIBC_2_0
)