]>
git.saurik.com Git - apple/libinfo.git/blob - rpc.subproj/xdr.c
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>
83 #define xdrlong_t long
86 typedef quad_t longlong_t
; /* ANSI long long type */
87 typedef u_quad_t u_longlong_t
; /* ANSI unsigned long long type */
90 * constants specific to the xdr "protocol"
93 #define XDR_FALSE ((int) 0)
94 #define XDR_TRUE ((int) 1)
96 #define XDR_FALSE ((long) 0)
97 #define XDR_TRUE ((long) 1)
99 #define LASTUNSIGNED ((u_int) 0-1)
104 static const char xdr_zero
[BYTES_PER_XDR_UNIT
] = { 0, 0, 0, 0 };
107 * Free a data structure using XDR
108 * Not a filter, but a convenient utility nonetheless
142 switch (xdrs
->x_op
) {
146 return (XDR_PUTLONG(xdrs
, (const xdrlong_t
*)&l
));
149 if (!XDR_GETLONG(xdrs
, &l
)) {
163 * XDR unsigned integers
172 switch (xdrs
->x_op
) {
176 return (XDR_PUTLONG(xdrs
, (const xdrlong_t
*)&l
));
179 if (!XDR_GETLONG(xdrs
, &l
)) {
195 * same as xdr_u_long - open coded to save a proc call!
206 switch (xdrs
->x_op
) {
208 return (XDR_PUTLONG(xdrs
, lp
));
210 return (XDR_GETLONG(xdrs
, lp
));
219 * XDR unsigned long integers
220 * same as xdr_long - open coded to save a proc call!
223 xdr_u_long(xdrs
, ulp
)
231 switch (xdrs
->x_op
) {
233 return (XDR_PUTLONG(xdrs
, (const xdrlong_t
*)ulp
));
235 return (XDR_GETLONG(xdrs
, (xdrlong_t
*)ulp
));
245 * XDR 32-bit integers
246 * same as xdr_u_int32_t - open coded to save a proc call!
249 xdr_int32_t(xdrs
, int32_p
)
255 switch (xdrs
->x_op
) {
259 return (XDR_PUTLONG(xdrs
, (const xdrlong_t
*)&l
));
262 if (!XDR_GETLONG(xdrs
, &l
)) {
276 * XDR unsigned 32-bit integers
277 * same as xdr_int32_t - open coded to save a proc call!
280 xdr_u_int32_t(xdrs
, u_int32_p
)
282 u_int32_t
*u_int32_p
;
286 switch (xdrs
->x_op
) {
290 return (XDR_PUTLONG(xdrs
, (xdrlong_t
*)&l
));
293 if (!XDR_GETLONG(xdrs
, (xdrlong_t
*)&l
)) return (FALSE
);
315 switch (xdrs
->x_op
) {
319 return (XDR_PUTLONG(xdrs
, (const xdrlong_t
*)&l
));
322 if (!XDR_GETLONG(xdrs
, &l
)) {
336 * XDR unsigned short integers
339 xdr_u_short(xdrs
, usp
)
345 switch (xdrs
->x_op
) {
349 return (XDR_PUTLONG(xdrs
, (const xdrlong_t
*)&l
));
352 if (!XDR_GETLONG(xdrs
, &l
)) {
367 * XDR 16-bit integers
370 xdr_int16_t(xdrs
, int16_p
)
376 switch (xdrs
->x_op
) {
380 return (XDR_PUTLONG(xdrs
, (const xdrlong_t
*)&l
));
383 if (!XDR_GETLONG(xdrs
, &l
)) {
397 * XDR unsigned 16-bit integers
400 xdr_u_int16_t(xdrs
, u_int16_p
)
402 u_int16_t
*u_int16_p
;
406 switch (xdrs
->x_op
) {
410 return (XDR_PUTLONG(xdrs
, (const xdrlong_t
*)&l
));
413 if (!XDR_GETLONG(xdrs
, &l
)) {
438 if (!xdr_int(xdrs
, &i
)) {
446 * XDR an unsigned char
456 if (!xdr_u_int(xdrs
, &u
)) {
473 switch (xdrs
->x_op
) {
476 lb
= *bp
? XDR_TRUE
: XDR_FALSE
;
477 return (XDR_PUTLONG(xdrs
, (const xdrlong_t
*)&lb
));
480 if (!XDR_GETLONG(xdrs
, &lb
)) {
483 *bp
= (lb
== XDR_FALSE
) ? FALSE
: TRUE
;
501 enum sizecheck
{ SIZEVAL
}; /* used to find the size of an enum */
504 * enums are treated as ints
506 /* LINTED */ if (sizeof (enum sizecheck
) == sizeof (int)) {
507 return (xdr_long(xdrs
, (xdrlong_t
*)(void *)ep
));
508 } else /* LINTED */ if (sizeof (enum sizecheck
) == sizeof (int)) {
509 return (xdr_int(xdrs
, (int *)(void *)ep
));
510 } else /* LINTED */ if (sizeof (enum sizecheck
) == sizeof (short)) {
511 return (xdr_short(xdrs
, (short *)(void *)ep
));
519 * Allows the specification of a fixed size sequence of opaque bytes.
520 * cp points to the opaque object and cnt gives the byte length.
523 xdr_opaque(xdrs
, cp
, cnt
)
529 u_int8_t crud
[BYTES_PER_XDR_UNIT
];
532 * if no data we are done
538 * round byte count to full xdr units
540 rndup
= cnt
% BYTES_PER_XDR_UNIT
;
542 rndup
= BYTES_PER_XDR_UNIT
- rndup
;
544 if (xdrs
->x_op
== XDR_DECODE
) {
545 if (!XDR_GETBYTES(xdrs
, cp
, cnt
)) {
550 return (XDR_GETBYTES(xdrs
, (caddr_t
)(void *)crud
, rndup
));
553 if (xdrs
->x_op
== XDR_ENCODE
) {
554 if (!XDR_PUTBYTES(xdrs
, cp
, cnt
)) {
559 return (XDR_PUTBYTES(xdrs
, xdr_zero
, rndup
));
562 if (xdrs
->x_op
== XDR_FREE
) {
571 * *cpp is a pointer to the bytes, *sizep is the count.
572 * If *cpp is NULL maxsize bytes are allocated
575 xdr_bytes(xdrs
, cpp
, sizep
, maxsize
)
581 char *sp
= *cpp
; /* sp is the actual string pointer */
585 * first deal with the length since xdr bytes are counted
587 if (! xdr_u_int(xdrs
, sizep
)) {
591 if ((nodesize
> maxsize
) && (xdrs
->x_op
!= XDR_FREE
)) {
596 * now deal with the actual bytes
598 switch (xdrs
->x_op
) {
605 *cpp
= sp
= mem_alloc(nodesize
);
608 warnx("xdr_bytes: out of memory");
614 return (xdr_opaque(xdrs
, sp
, nodesize
));
618 mem_free(sp
, nodesize
);
628 * Implemented here due to commonality of the object.
636 return (xdr_bytes(xdrs
, &np
->n_bytes
, &np
->n_len
, MAX_NETOBJ_SZ
));
640 * XDR a descriminated union
641 * Support routine for discriminated unions.
642 * You create an array of xdrdiscrim structures, terminated with
643 * an entry with a null procedure pointer. The routine gets
644 * the discriminant value and then searches the array of xdrdiscrims
645 * looking for that value. It calls the procedure given in the xdrdiscrim
646 * to handle the discriminant. If there is no specific routine a default
647 * routine may be called.
648 * If there is no specific or default routine an error is returned.
651 xdr_union(xdrs
, dscmp
, unp
, choices
, dfault
)
653 enum_t
*dscmp
; /* enum to decide which arm to work on */
654 char *unp
; /* the union itself */
655 const struct xdr_discrim
*choices
; /* [value, xdr proc] for each arm */
656 xdrproc_t dfault
; /* default xdr routine */
661 * we deal with the discriminator; it's an enum
663 if (! xdr_enum(xdrs
, dscmp
)) {
669 * search choices for a value that matches the discriminator.
670 * if we find one, execute the xdr routine for that value.
672 for (; choices
->proc
!= NULL_xdrproc_t
; choices
++) {
673 if (choices
->value
== dscm
)
674 return ((*(choices
->proc
))(xdrs
, unp
));
678 * no match - execute the default xdr routine if there is one
680 return ((dfault
== NULL_xdrproc_t
) ? FALSE
:
681 (*dfault
)(xdrs
, unp
));
686 * Non-portable xdr primitives.
687 * Care should be taken when moving these routines to new architectures.
692 * XDR null terminated ASCII strings
693 * xdr_string deals with "C strings" - arrays of bytes that are
694 * terminated by a NULL character. The parameter cpp references a
695 * pointer to storage; If the pointer is null, then the necessary
696 * storage is allocated. The last parameter is the max allowed length
697 * of the string as specified by a protocol.
700 xdr_string(xdrs
, cpp
, maxsize
)
705 char *sp
= *cpp
; /* sp is the actual string pointer */
710 * first deal with the length since xdr strings are counted-strings
712 switch (xdrs
->x_op
) {
715 return(TRUE
); /* already free */
724 if (! xdr_u_int(xdrs
, &size
)) {
727 if (size
> maxsize
) {
733 * now deal with the actual bytes
735 switch (xdrs
->x_op
) {
742 *cpp
= sp
= mem_alloc(nodesize
);
744 warnx("xdr_string: out of memory");
751 return (xdr_opaque(xdrs
, sp
, size
));
754 mem_free(sp
, nodesize
);
763 * Wrapper for xdr_string that can be called directly from
764 * routines like clnt_call
767 xdr_wrapstring(xdrs
, cpp
)
771 return xdr_string(xdrs
, cpp
, LASTUNSIGNED
);
775 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
776 * are in the "non-portable" section because they require that a `long long'
779 * --thorpej@netbsd.org, November 30, 1999
783 * XDR 64-bit integers
786 xdr_int64_t(xdrs
, llp
)
792 switch (xdrs
->x_op
) {
794 ul
[0] = (u_int32_t
)((u_int64_t
)*llp
>> 32) & 0xffffffff;
795 ul
[1] = (u_int32_t
)((u_int64_t
)*llp
) & 0xffffffff;
796 if (XDR_PUTLONG(xdrs
, (xdrlong_t
*)&ul
[0]) == FALSE
)
798 return (XDR_PUTLONG(xdrs
, (xdrlong_t
*)&ul
[1]));
800 if (XDR_GETLONG(xdrs
, (xdrlong_t
*)&ul
[0]) == FALSE
)
802 if (XDR_GETLONG(xdrs
, (xdrlong_t
*)&ul
[1]) == FALSE
)
805 (((u_int64_t
)ul
[0] << 32) | ((u_int64_t
)ul
[1]));
816 * XDR unsigned 64-bit integers
819 xdr_u_int64_t(xdrs
, ullp
)
825 switch (xdrs
->x_op
) {
827 ul
[0] = (u_int32_t
)(*ullp
>> 32) & 0xffffffff;
828 ul
[1] = (u_int32_t
)(*ullp
) & 0xffffffff;
829 if (XDR_PUTLONG(xdrs
, (xdrlong_t
*)&ul
[0]) == FALSE
)
831 return (XDR_PUTLONG(xdrs
, (xdrlong_t
*)&ul
[1]));
833 if (XDR_GETLONG(xdrs
, (xdrlong_t
*)&ul
[0]) == FALSE
)
835 if (XDR_GETLONG(xdrs
, (xdrlong_t
*)&ul
[1]) == FALSE
)
838 (((u_int64_t
)ul
[0] << 32) | ((u_int64_t
)ul
[1]));
857 * Don't bother open-coding this; it's a fair amount of code. Just
858 * call xdr_int64_t().
860 return (xdr_int64_t(xdrs
, (int64_t *)llp
));
865 * XDR unsigned hypers
868 xdr_u_hyper(xdrs
, ullp
)
873 * Don't bother open-coding this; it's a fair amount of code. Just
874 * call xdr_u_int64_t().
876 return (xdr_u_int64_t(xdrs
, (u_int64_t
*)ullp
));
884 xdr_longlong_t(xdrs
, llp
)
889 * Don't bother open-coding this; it's a fair amount of code. Just
890 * call xdr_int64_t().
892 return (xdr_int64_t(xdrs
, (int64_t *)llp
));
900 xdr_u_longlong_t(xdrs
, ullp
)
906 * Don't bother open-coding this; it's a fair amount of code. Just
907 * call xdr_u_int64_t().
909 return (xdr_u_int64_t(xdrs
, (u_int64_t
*)ullp
));