]>
git.saurik.com Git - apple/libinfo.git/blob - rpc.subproj/xdr.c
d9b60a538999e7e17b68e63800a7fb0b635cff4e
2 * Copyright (c) 1999 Apple Computer, 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
77 #include <rpc/types.h>
80 typedef quad_t longlong_t
; /* ANSI long long type */
81 typedef u_quad_t u_longlong_t
; /* ANSI unsigned long long type */
84 * constants specific to the xdr "protocol"
86 #define XDR_FALSE ((long) 0)
87 #define XDR_TRUE ((long) 1)
88 #define LASTUNSIGNED ((u_int) 0-1)
93 static const char xdr_zero
[BYTES_PER_XDR_UNIT
] = { 0, 0, 0, 0 };
96 * Free a data structure using XDR
97 * Not a filter, but a convenient utility nonetheless
131 switch (xdrs
->x_op
) {
135 return (XDR_PUTLONG(xdrs
, &l
));
138 if (!XDR_GETLONG(xdrs
, &l
)) {
152 * XDR unsigned integers
161 switch (xdrs
->x_op
) {
165 return (XDR_PUTLONG(xdrs
, (long *)&l
));
168 if (!XDR_GETLONG(xdrs
, (long *)&l
)) {
184 * same as xdr_u_long - open coded to save a proc call!
191 switch (xdrs
->x_op
) {
193 return (XDR_PUTLONG(xdrs
, lp
));
195 return (XDR_GETLONG(xdrs
, lp
));
204 * XDR unsigned long integers
205 * same as xdr_long - open coded to save a proc call!
208 xdr_u_long(xdrs
, ulp
)
212 switch (xdrs
->x_op
) {
214 return (XDR_PUTLONG(xdrs
, (long *)ulp
));
216 return (XDR_GETLONG(xdrs
, (long *)ulp
));
226 * XDR 32-bit integers
227 * same as xdr_u_int32_t - open coded to save a proc call!
230 xdr_int32_t(xdrs
, int32_p
)
236 switch (xdrs
->x_op
) {
240 return (XDR_PUTLONG(xdrs
, &l
));
243 if (!XDR_GETLONG(xdrs
, &l
)) {
246 *int32_p
= (int32_t) l
;
257 * XDR unsigned 32-bit integers
258 * same as xdr_int32_t - open coded to save a proc call!
261 xdr_u_int32_t(xdrs
, u_int32_p
)
263 u_int32_t
*u_int32_p
;
267 switch (xdrs
->x_op
) {
270 l
= (u_long
) *u_int32_p
;
271 return (XDR_PUTLONG(xdrs
, (long *)&l
));
274 if (!XDR_GETLONG(xdrs
, (long *)&l
)) {
277 *u_int32_p
= (u_int32_t
) l
;
298 switch (xdrs
->x_op
) {
302 return (XDR_PUTLONG(xdrs
, &l
));
305 if (!XDR_GETLONG(xdrs
, &l
)) {
319 * XDR unsigned short integers
322 xdr_u_short(xdrs
, usp
)
328 switch (xdrs
->x_op
) {
332 return (XDR_PUTLONG(xdrs
, (long *)&l
));
335 if (!XDR_GETLONG(xdrs
, (long *)&l
)) {
350 * XDR 16-bit integers
353 xdr_int16_t(xdrs
, int16_p
)
359 switch (xdrs
->x_op
) {
363 return (XDR_PUTLONG(xdrs
, &l
));
366 if (!XDR_GETLONG(xdrs
, &l
)) {
369 *int16_p
= (int16_t) l
;
380 * XDR unsigned 16-bit integers
383 xdr_u_int16_t(xdrs
, u_int16_p
)
385 u_int16_t
*u_int16_p
;
389 switch (xdrs
->x_op
) {
392 l
= (u_long
) *u_int16_p
;
393 return (XDR_PUTLONG(xdrs
, (long *)&l
));
396 if (!XDR_GETLONG(xdrs
, (long *)&l
)) {
399 *u_int16_p
= (u_int16_t
) l
;
421 if (!xdr_int(xdrs
, &i
)) {
429 * XDR an unsigned char
439 if (!xdr_u_int(xdrs
, &u
)) {
456 switch (xdrs
->x_op
) {
459 lb
= *bp
? XDR_TRUE
: XDR_FALSE
;
460 return (XDR_PUTLONG(xdrs
, &lb
));
463 if (!XDR_GETLONG(xdrs
, &lb
)) {
466 *bp
= (lb
== XDR_FALSE
) ? FALSE
: TRUE
;
484 enum sizecheck
{ SIZEVAL
}; /* used to find the size of an enum */
487 * enums are treated as ints
489 /* LINTED */ if (sizeof (enum sizecheck
) == sizeof (long)) {
490 return (xdr_long(xdrs
, (long *)(void *)ep
));
491 } else /* LINTED */ if (sizeof (enum sizecheck
) == sizeof (int)) {
492 return (xdr_int(xdrs
, (int *)(void *)ep
));
493 } else /* LINTED */ if (sizeof (enum sizecheck
) == sizeof (short)) {
494 return (xdr_short(xdrs
, (short *)(void *)ep
));
502 * Allows the specification of a fixed size sequence of opaque bytes.
503 * cp points to the opaque object and cnt gives the byte length.
506 xdr_opaque(xdrs
, cp
, cnt
)
512 static int crud
[BYTES_PER_XDR_UNIT
];
515 * if no data we are done
521 * round byte count to full xdr units
523 rndup
= cnt
% BYTES_PER_XDR_UNIT
;
525 rndup
= BYTES_PER_XDR_UNIT
- rndup
;
527 if (xdrs
->x_op
== XDR_DECODE
) {
528 if (!XDR_GETBYTES(xdrs
, cp
, cnt
)) {
533 return (XDR_GETBYTES(xdrs
, (caddr_t
)(void *)crud
, rndup
));
536 if (xdrs
->x_op
== XDR_ENCODE
) {
537 if (!XDR_PUTBYTES(xdrs
, cp
, cnt
)) {
542 return (XDR_PUTBYTES(xdrs
, xdr_zero
, rndup
));
545 if (xdrs
->x_op
== XDR_FREE
) {
554 * *cpp is a pointer to the bytes, *sizep is the count.
555 * If *cpp is NULL maxsize bytes are allocated
558 xdr_bytes(xdrs
, cpp
, sizep
, maxsize
)
564 char *sp
= *cpp
; /* sp is the actual string pointer */
568 * first deal with the length since xdr bytes are counted
570 if (! xdr_u_int(xdrs
, sizep
)) {
574 if ((nodesize
> maxsize
) && (xdrs
->x_op
!= XDR_FREE
)) {
579 * now deal with the actual bytes
581 switch (xdrs
->x_op
) {
588 *cpp
= sp
= mem_alloc(nodesize
);
591 warnx("xdr_bytes: out of memory");
597 return (xdr_opaque(xdrs
, sp
, nodesize
));
601 mem_free(sp
, nodesize
);
611 * Implemented here due to commonality of the object.
619 return (xdr_bytes(xdrs
, &np
->n_bytes
, &np
->n_len
, MAX_NETOBJ_SZ
));
623 * XDR a descriminated union
624 * Support routine for discriminated unions.
625 * You create an array of xdrdiscrim structures, terminated with
626 * an entry with a null procedure pointer. The routine gets
627 * the discriminant value and then searches the array of xdrdiscrims
628 * looking for that value. It calls the procedure given in the xdrdiscrim
629 * to handle the discriminant. If there is no specific routine a default
630 * routine may be called.
631 * If there is no specific or default routine an error is returned.
634 xdr_union(xdrs
, dscmp
, unp
, choices
, dfault
)
636 enum_t
*dscmp
; /* enum to decide which arm to work on */
637 char *unp
; /* the union itself */
638 const struct xdr_discrim
*choices
; /* [value, xdr proc] for each arm */
639 xdrproc_t dfault
; /* default xdr routine */
644 * we deal with the discriminator; it's an enum
646 if (! xdr_enum(xdrs
, dscmp
)) {
652 * search choices for a value that matches the discriminator.
653 * if we find one, execute the xdr routine for that value.
655 for (; choices
->proc
!= NULL_xdrproc_t
; choices
++) {
656 if (choices
->value
== dscm
)
657 return ((*(choices
->proc
))(xdrs
, unp
));
661 * no match - execute the default xdr routine if there is one
663 return ((dfault
== NULL_xdrproc_t
) ? FALSE
:
664 (*dfault
)(xdrs
, unp
));
669 * Non-portable xdr primitives.
670 * Care should be taken when moving these routines to new architectures.
675 * XDR null terminated ASCII strings
676 * xdr_string deals with "C strings" - arrays of bytes that are
677 * terminated by a NULL character. The parameter cpp references a
678 * pointer to storage; If the pointer is null, then the necessary
679 * storage is allocated. The last parameter is the max allowed length
680 * of the string as specified by a protocol.
683 xdr_string(xdrs
, cpp
, maxsize
)
688 char *sp
= *cpp
; /* sp is the actual string pointer */
693 * first deal with the length since xdr strings are counted-strings
695 switch (xdrs
->x_op
) {
698 return(TRUE
); /* already free */
707 if (! xdr_u_int(xdrs
, &size
)) {
710 if (size
> maxsize
) {
716 * now deal with the actual bytes
718 switch (xdrs
->x_op
) {
725 *cpp
= sp
= mem_alloc(nodesize
);
727 warnx("xdr_string: out of memory");
734 return (xdr_opaque(xdrs
, sp
, size
));
737 mem_free(sp
, nodesize
);
746 * Wrapper for xdr_string that can be called directly from
747 * routines like clnt_call
750 xdr_wrapstring(xdrs
, cpp
)
754 return xdr_string(xdrs
, cpp
, LASTUNSIGNED
);
758 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
759 * are in the "non-portable" section because they require that a `long long'
762 * --thorpej@netbsd.org, November 30, 1999
766 * XDR 64-bit integers
769 xdr_int64_t(xdrs
, llp
)
775 switch (xdrs
->x_op
) {
777 ul
[0] = (u_long
)((u_int64_t
)*llp
>> 32) & 0xffffffff;
778 ul
[1] = (u_long
)((u_int64_t
)*llp
) & 0xffffffff;
779 if (XDR_PUTLONG(xdrs
, (long *)&ul
[0]) == FALSE
)
781 return (XDR_PUTLONG(xdrs
, (long *)&ul
[1]));
783 if (XDR_GETLONG(xdrs
, (long *)&ul
[0]) == FALSE
)
785 if (XDR_GETLONG(xdrs
, (long *)&ul
[1]) == FALSE
)
788 (((u_int64_t
)ul
[0] << 32) | ((u_int64_t
)ul
[1]));
799 * XDR unsigned 64-bit integers
802 xdr_u_int64_t(xdrs
, ullp
)
808 switch (xdrs
->x_op
) {
810 ul
[0] = (u_long
)(*ullp
>> 32) & 0xffffffff;
811 ul
[1] = (u_long
)(*ullp
) & 0xffffffff;
812 if (XDR_PUTLONG(xdrs
, (long *)&ul
[0]) == FALSE
)
814 return (XDR_PUTLONG(xdrs
, (long *)&ul
[1]));
816 if (XDR_GETLONG(xdrs
, (long *)&ul
[0]) == FALSE
)
818 if (XDR_GETLONG(xdrs
, (long *)&ul
[1]) == FALSE
)
821 (((u_int64_t
)ul
[0] << 32) | ((u_int64_t
)ul
[1]));
841 * Don't bother open-coding this; it's a fair amount of code. Just
842 * call xdr_int64_t().
844 return (xdr_int64_t(xdrs
, (int64_t *)llp
));
849 * XDR unsigned hypers
852 xdr_u_hyper(xdrs
, ullp
)
858 * Don't bother open-coding this; it's a fair amount of code. Just
859 * call xdr_u_int64_t().
861 return (xdr_u_int64_t(xdrs
, (u_int64_t
*)ullp
));
869 xdr_longlong_t(xdrs
, llp
)
875 * Don't bother open-coding this; it's a fair amount of code. Just
876 * call xdr_int64_t().
878 return (xdr_int64_t(xdrs
, (int64_t *)llp
));
886 xdr_u_longlong_t(xdrs
, ullp
)
892 * Don't bother open-coding this; it's a fair amount of code. Just
893 * call xdr_u_int64_t().
895 return (xdr_u_int64_t(xdrs
, (u_int64_t
*)ullp
));