2 * Copyright (c) 1999-2018 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 /* $NetBSD: xdr.c,v 1.22 2000/07/06 03:10:35 christos Exp $ */
28 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
29 * unrestricted use provided that this legend is included on all tape
30 * media and as a part of the software program in whole or part. Users
31 * may copy or modify Sun RPC without charge, but are not authorized
32 * to license or distribute it to anyone else except as part of a product or
33 * program developed by the user.
35 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
36 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
37 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
39 * Sun RPC is provided with no support and without any obligation on the
40 * part of Sun Microsystems, Inc. to assist in its use, correction,
41 * modification or enhancement.
43 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
44 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
45 * OR ANY PART THEREOF.
47 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
48 * or profits or other special, indirect and consequential damages, even if
49 * Sun has been advised of the possibility of such damages.
51 * Sun Microsystems, Inc.
53 * Mountain View, California 94043
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char *sccsid
= "@(#)xdr.c 1.35 87/08/12";
58 static char *sccsid
= "@(#)xdr.c 2.1 88/07/29 4.0 RPCSRC";
60 #include <sys/cdefs.h>
63 * xdr.c, Generic XDR routines implementation.
65 * Copyright (C) 1986, Sun Microsystems, Inc.
67 * These are the "generic" xdr routines used to serialize and de-serialize
68 * most common data items. See xdr.h for more info on the interface to
72 #include "libinfo_common.h"
79 #include <rpc/types.h>
85 #define xdrlong_t long
88 typedef quad_t longlong_t
; /* ANSI long long type */
89 typedef u_quad_t u_longlong_t
; /* ANSI unsigned long long type */
92 * constants specific to the xdr "protocol"
95 #define XDR_FALSE ((int) 0)
96 #define XDR_TRUE ((int) 1)
98 #define XDR_FALSE ((long) 0)
99 #define XDR_TRUE ((long) 1)
101 #define LASTUNSIGNED ((u_int) 0-1)
106 static const char xdr_zero
[BYTES_PER_XDR_UNIT
] = { 0, 0, 0, 0 };
109 * Free a data structure using XDR
110 * Not a filter, but a convenient utility nonetheless
121 (*proc
)(&x
, objp
, 0);
147 switch (xdrs
->x_op
) {
151 return (XDR_PUTLONG(xdrs
, (const xdrlong_t
*)&l
));
154 if (!XDR_GETLONG(xdrs
, &l
)) {
168 * XDR unsigned integers
178 switch (xdrs
->x_op
) {
182 return (XDR_PUTLONG(xdrs
, (const xdrlong_t
*)&l
));
185 if (!XDR_GETLONG(xdrs
, &l
)) {
201 * same as xdr_u_long - open coded to save a proc call!
213 switch (xdrs
->x_op
) {
215 return (XDR_PUTLONG(xdrs
, lp
));
217 return (XDR_GETLONG(xdrs
, lp
));
226 * XDR unsigned long integers
227 * same as xdr_long - open coded to save a proc call!
231 xdr_u_long(xdrs
, ulp
)
239 switch (xdrs
->x_op
) {
241 return (XDR_PUTLONG(xdrs
, (const xdrlong_t
*)ulp
));
243 return (XDR_GETLONG(xdrs
, (xdrlong_t
*)ulp
));
253 * XDR 32-bit integers
254 * same as xdr_u_int32_t - open coded to save a proc call!
258 xdr_int32_t(xdrs
, int32_p
)
264 switch (xdrs
->x_op
) {
268 return (XDR_PUTLONG(xdrs
, (const xdrlong_t
*)&l
));
271 if (!XDR_GETLONG(xdrs
, &l
)) {
285 * XDR unsigned 32-bit integers
286 * same as xdr_int32_t - open coded to save a proc call!
290 xdr_u_int32_t(xdrs
, u_int32_p
)
292 u_int32_t
*u_int32_p
;
296 switch (xdrs
->x_op
) {
300 return (XDR_PUTLONG(xdrs
, (xdrlong_t
*)&l
));
303 if (!XDR_GETLONG(xdrs
, (xdrlong_t
*)&l
)) return (FALSE
);
326 switch (xdrs
->x_op
) {
330 return (XDR_PUTLONG(xdrs
, (const xdrlong_t
*)&l
));
333 if (!XDR_GETLONG(xdrs
, &l
)) {
347 * XDR unsigned short integers
351 xdr_u_short(xdrs
, usp
)
357 switch (xdrs
->x_op
) {
361 return (XDR_PUTLONG(xdrs
, (const xdrlong_t
*)&l
));
364 if (!XDR_GETLONG(xdrs
, &l
)) {
379 * XDR 16-bit integers
383 xdr_int16_t(xdrs
, int16_p
)
389 switch (xdrs
->x_op
) {
393 return (XDR_PUTLONG(xdrs
, (const xdrlong_t
*)&l
));
396 if (!XDR_GETLONG(xdrs
, &l
)) {
410 * XDR unsigned 16-bit integers
414 xdr_u_int16_t(xdrs
, u_int16_p
)
416 u_int16_t
*u_int16_p
;
420 switch (xdrs
->x_op
) {
424 return (XDR_PUTLONG(xdrs
, (const xdrlong_t
*)&l
));
427 if (!XDR_GETLONG(xdrs
, &l
)) {
453 if (!xdr_int(xdrs
, &i
)) {
461 * XDR an unsigned char
472 if (!xdr_u_int(xdrs
, &u
)) {
490 switch (xdrs
->x_op
) {
493 lb
= *bp ? XDR_TRUE
: XDR_FALSE
;
494 return (XDR_PUTLONG(xdrs
, (const xdrlong_t
*)&lb
));
497 if (!XDR_GETLONG(xdrs
, &lb
)) {
500 *bp
= (lb
== XDR_FALSE
) ? FALSE
: TRUE
;
519 enum sizecheck
{ SIZEVAL
}; /* used to find the size of an enum */
522 * enums are treated as ints
524 /* LINTED */ if (sizeof (enum sizecheck
) == sizeof (int)) {
525 return (xdr_long(xdrs
, (xdrlong_t
*)(void *)ep
));
526 } else /* LINTED */ if (sizeof (enum sizecheck
) == sizeof (int)) {
527 return (xdr_int(xdrs
, (int *)(void *)ep
));
528 } else /* LINTED */ if (sizeof (enum sizecheck
) == sizeof (short)) {
529 return (xdr_short(xdrs
, (short *)(void *)ep
));
537 * Allows the specification of a fixed size sequence of opaque bytes.
538 * cp points to the opaque object and cnt gives the byte length.
542 xdr_opaque(xdrs
, cp
, cnt
)
548 u_int8_t crud
[BYTES_PER_XDR_UNIT
];
551 * if no data we are done
557 * round byte count to full xdr units
559 rndup
= cnt
% BYTES_PER_XDR_UNIT
;
561 rndup
= BYTES_PER_XDR_UNIT
- rndup
;
563 if (xdrs
->x_op
== XDR_DECODE
) {
564 if (!XDR_GETBYTES(xdrs
, cp
, cnt
)) {
569 return (XDR_GETBYTES(xdrs
, (caddr_t
)(void *)crud
, rndup
));
572 if (xdrs
->x_op
== XDR_ENCODE
) {
573 if (!XDR_PUTBYTES(xdrs
, cp
, cnt
)) {
578 return (XDR_PUTBYTES(xdrs
, xdr_zero
, rndup
));
581 if (xdrs
->x_op
== XDR_FREE
) {
590 * *cpp is a pointer to the bytes, *sizep is the count.
591 * If *cpp is NULL maxsize bytes are allocated
595 xdr_bytes(xdrs
, cpp
, sizep
, maxsize
)
601 char *sp
= *cpp
; /* sp is the actual string pointer */
605 * first deal with the length since xdr bytes are counted
607 if (! xdr_u_int(xdrs
, sizep
)) {
611 if ((nodesize
> maxsize
) && (xdrs
->x_op
!= XDR_FREE
)) {
616 * now deal with the actual bytes
618 switch (xdrs
->x_op
) {
625 *cpp
= sp
= mem_alloc(nodesize
);
628 warnx("xdr_bytes: out of memory");
634 return (xdr_opaque(xdrs
, sp
, nodesize
));
638 mem_free(sp
, nodesize
);
648 * Implemented here due to commonality of the object.
657 return (xdr_bytes(xdrs
, &np
->n_bytes
, &np
->n_len
, MAX_NETOBJ_SZ
));
661 * XDR a descriminated union
662 * Support routine for discriminated unions.
663 * You create an array of xdrdiscrim structures, terminated with
664 * an entry with a null procedure pointer. The routine gets
665 * the discriminant value and then searches the array of xdrdiscrims
666 * looking for that value. It calls the procedure given in the xdrdiscrim
667 * to handle the discriminant. If there is no specific routine a default
668 * routine may be called.
669 * If there is no specific or default routine an error is returned.
673 xdr_union(xdrs
, dscmp
, unp
, choices
, dfault
)
675 enum_t
*dscmp
; /* enum to decide which arm to work on */
676 char *unp
; /* the union itself */
677 const struct xdr_discrim
*choices
; /* [value, xdr proc] for each arm */
678 xdrproc_t dfault
; /* default xdr routine */
683 * we deal with the discriminator; it's an enum
685 if (! xdr_enum(xdrs
, dscmp
)) {
691 * search choices for a value that matches the discriminator.
692 * if we find one, execute the xdr routine for that value.
694 for (; choices
->proc
!= NULL_xdrproc_t
; choices
++) {
695 if (choices
->value
== dscm
)
696 return ((*(choices
->proc
))(xdrs
, unp
, 0));
700 * no match - execute the default xdr routine if there is one
702 return ((dfault
== NULL_xdrproc_t
) ? FALSE
:
703 (*dfault
)(xdrs
, unp
, 0));
708 * Non-portable xdr primitives.
709 * Care should be taken when moving these routines to new architectures.
714 * XDR null terminated ASCII strings
715 * xdr_string deals with "C strings" - arrays of bytes that are
716 * terminated by a NULL character. The parameter cpp references a
717 * pointer to storage; If the pointer is null, then the necessary
718 * storage is allocated. The last parameter is the max allowed length
719 * of the string as specified by a protocol.
723 xdr_string(xdrs
, cpp
, maxsize
)
728 char *sp
= *cpp
; /* sp is the actual string pointer */
733 * first deal with the length since xdr strings are counted-strings
735 switch (xdrs
->x_op
) {
738 return(TRUE
); /* already free */
747 if (! xdr_u_int(xdrs
, &size
)) {
750 if (size
> maxsize
) {
756 * now deal with the actual bytes
758 switch (xdrs
->x_op
) {
765 *cpp
= sp
= mem_alloc(nodesize
);
767 warnx("xdr_string: out of memory");
774 return (xdr_opaque(xdrs
, sp
, size
));
777 mem_free(sp
, nodesize
);
786 * Wrapper for xdr_string that can be called directly from
787 * routines like clnt_call
791 xdr_wrapstring(xdrs
, cpp
)
795 return xdr_string(xdrs
, cpp
, LASTUNSIGNED
);
799 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
800 * are in the "non-portable" section because they require that a `long long'
803 * --thorpej@netbsd.org, November 30, 1999
807 * XDR 64-bit integers
811 xdr_int64_t(xdrs
, llp
)
817 switch (xdrs
->x_op
) {
819 ul
[0] = (u_int32_t
)((u_int64_t
)*llp
>> 32) & 0xffffffff;
820 ul
[1] = (u_int32_t
)((u_int64_t
)*llp
) & 0xffffffff;
821 if (XDR_PUTLONG(xdrs
, (xdrlong_t
*)&ul
[0]) == FALSE
)
823 return (XDR_PUTLONG(xdrs
, (xdrlong_t
*)&ul
[1]));
825 if (XDR_GETLONG(xdrs
, (xdrlong_t
*)&ul
[0]) == FALSE
)
827 if (XDR_GETLONG(xdrs
, (xdrlong_t
*)&ul
[1]) == FALSE
)
830 (((u_int64_t
)ul
[0] << 32) | ((u_int64_t
)ul
[1]));
841 * XDR unsigned 64-bit integers
845 xdr_u_int64_t(xdrs
, ullp
)
851 switch (xdrs
->x_op
) {
853 ul
[0] = (u_int32_t
)(*ullp
>> 32) & 0xffffffff;
854 ul
[1] = (u_int32_t
)(*ullp
) & 0xffffffff;
855 if (XDR_PUTLONG(xdrs
, (xdrlong_t
*)&ul
[0]) == FALSE
)
857 return (XDR_PUTLONG(xdrs
, (xdrlong_t
*)&ul
[1]));
859 if (XDR_GETLONG(xdrs
, (xdrlong_t
*)&ul
[0]) == FALSE
)
861 if (XDR_GETLONG(xdrs
, (xdrlong_t
*)&ul
[1]) == FALSE
)
864 (((u_int64_t
)ul
[0] << 32) | ((u_int64_t
)ul
[1]));
884 * Don't bother open-coding this; it's a fair amount of code. Just
885 * call xdr_int64_t().
887 return (xdr_int64_t(xdrs
, (int64_t *)llp
));
892 * XDR unsigned hypers
896 xdr_u_hyper(xdrs
, ullp
)
901 * Don't bother open-coding this; it's a fair amount of code. Just
902 * call xdr_u_int64_t().
904 return (xdr_u_int64_t(xdrs
, (u_int64_t
*)ullp
));
913 xdr_longlong_t(xdrs
, llp
)
918 * Don't bother open-coding this; it's a fair amount of code. Just
919 * call xdr_int64_t().
921 return (xdr_int64_t(xdrs
, (int64_t *)llp
));
930 xdr_u_longlong_t(xdrs
, ullp
)
936 * Don't bother open-coding this; it's a fair amount of code. Just
937 * call xdr_u_int64_t().
939 return (xdr_u_int64_t(xdrs
, (u_int64_t
*)ullp
));