2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)xdr.c 1.35 87/08/12
30 * @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC
31 * $FreeBSD: src/lib/libc/xdr/xdr.c,v 1.9.2.1 2000/05/06 21:16:04 dec Exp $
32 * $DragonFly: src/lib/libc/xdr/xdr.c,v 1.4 2005/12/05 00:47:57 swildner Exp $
36 * xdr.c, Generic XDR routines implementation.
38 * Copyright (C) 1986, Sun Microsystems, Inc.
40 * These are the "generic" xdr routines used to serialize and de-serialize
41 * most common data items. See xdr.h for more info on the interface to
49 #include <rpc/types.h>
53 * constants specific to the xdr "protocol"
55 #define XDR_FALSE ((long) 0)
56 #define XDR_TRUE ((long) 1)
57 #define LASTUNSIGNED ((u_int) 0-1)
62 static char xdr_zero
[BYTES_PER_XDR_UNIT
] = { 0, 0, 0, 0 };
65 * Free a data structure using XDR
66 * Not a filter, but a convenient utility nonetheless
69 xdr_free(xdrproc_t proc
, char *objp
)
91 xdr_int(XDR
*xdrs
, int *ip
)
99 return (XDR_PUTLONG(xdrs
, &l
));
102 if (!XDR_GETLONG(xdrs
, &l
)) {
115 * XDR unsigned integers
118 xdr_u_int(XDR
*xdrs
, u_int
*up
)
122 switch (xdrs
->x_op
) {
126 return (XDR_PUTLONG(xdrs
, (long *)&l
));
129 if (!XDR_GETLONG(xdrs
, (long *)&l
)) {
144 * same as xdr_u_long - open coded to save a proc call!
147 xdr_long(XDR
*xdrs
, long *lp
)
149 switch (xdrs
->x_op
) {
151 return (XDR_PUTLONG(xdrs
, lp
));
153 return (XDR_GETLONG(xdrs
, lp
));
162 * XDR unsigned long integers
163 * same as xdr_long - open coded to save a proc call!
166 xdr_u_long(XDR
*xdrs
, u_long
*ulp
)
168 switch (xdrs
->x_op
) {
170 return (XDR_PUTLONG(xdrs
, (long *)ulp
));
172 return (XDR_GETLONG(xdrs
, (long *)ulp
));
181 * XDR 32-bit integers
182 * same as xdr_u_int32_t - open coded to save a proc call!
185 xdr_int32_t(XDR
*xdrs
, int32_t *int32_p
)
189 switch (xdrs
->x_op
) {
193 return (XDR_PUTLONG(xdrs
, &l
));
196 if (!XDR_GETLONG(xdrs
, &l
)) {
199 *int32_p
= (int32_t) l
;
209 * XDR unsigned 32-bit integers
210 * same as xdr_int32_t - open coded to save a proc call!
213 xdr_u_int32_t(XDR
*xdrs
, u_int32_t
*u_int32_p
)
217 switch (xdrs
->x_op
) {
220 l
= (u_long
) *u_int32_p
;
221 return (XDR_PUTLONG(xdrs
, (long *)&l
));
224 if (!XDR_GETLONG(xdrs
, (long *)&l
)) {
227 *u_int32_p
= (u_int32_t
) l
;
237 * XDR 64-bit integers
240 xdr_int64_t(XDR
*xdrs
, int64_t *int64_p
)
244 switch (xdrs
->x_op
) {
247 ul
[0] = (u_long
)((u_int64_t
)*int64_p
>> 32) & 0xffffffff;
248 ul
[1] = (u_long
)((u_int64_t
)*int64_p
) & 0xffffffff;
249 if (XDR_PUTLONG(xdrs
, (long *)&ul
[0]) == FALSE
)
251 return (XDR_PUTLONG(xdrs
, (long *)&ul
[1]));
253 if (XDR_GETLONG(xdrs
, (long *)&ul
[0]) == FALSE
)
255 if (XDR_GETLONG(xdrs
, (long *)&ul
[1]) == FALSE
)
258 (((u_int64_t
)ul
[0] << 32) | ((u_int64_t
)ul
[1]));
267 * XDR unsigned 64-bit integers
270 xdr_u_int64_t(XDR
*xdrs
, u_int64_t
*uint64_p
)
274 switch (xdrs
->x_op
) {
277 ul
[0] = (u_long
)(*uint64_p
>> 32) & 0xffffffff;
278 ul
[1] = (u_long
)(*uint64_p
) & 0xffffffff;
279 if (XDR_PUTLONG(xdrs
, (long *)&ul
[0]) == FALSE
)
281 return (XDR_PUTLONG(xdrs
, (long *)&ul
[1]));
284 if (XDR_GETLONG(xdrs
, (long *)&ul
[0]) == FALSE
)
286 if (XDR_GETLONG(xdrs
, (long *)&ul
[1]) == FALSE
)
288 *uint64_p
= (u_int64_t
)
289 (((u_int64_t
)ul
[0] << 32) | ((u_int64_t
)ul
[1]));
302 xdr_short(XDR
*xdrs
, short *sp
)
306 switch (xdrs
->x_op
) {
310 return (XDR_PUTLONG(xdrs
, &l
));
313 if (!XDR_GETLONG(xdrs
, &l
)) {
326 * XDR unsigned short integers
329 xdr_u_short(XDR
*xdrs
, u_short
*usp
)
333 switch (xdrs
->x_op
) {
337 return (XDR_PUTLONG(xdrs
, (long *)&l
));
340 if (!XDR_GETLONG(xdrs
, (long *)&l
)) {
354 * XDR 16-bit integers
357 xdr_int16_t(XDR
*xdrs
, int16_t *int16_p
)
361 switch (xdrs
->x_op
) {
365 return (XDR_PUTLONG(xdrs
, &l
));
368 if (!XDR_GETLONG(xdrs
, &l
)) {
371 *int16_p
= (int16_t) l
;
381 * XDR unsigned 16-bit integers
384 xdr_u_int16_t(XDR
*xdrs
, u_int16_t
*u_int16_p
)
388 switch (xdrs
->x_op
) {
391 l
= (u_long
) *u_int16_p
;
392 return (XDR_PUTLONG(xdrs
, (long *)&l
));
395 if (!XDR_GETLONG(xdrs
, (long *)&l
)) {
398 *u_int16_p
= (u_int16_t
) l
;
412 xdr_char(XDR
*xdrs
, char *cp
)
417 if (!xdr_int(xdrs
, &i
)) {
425 * XDR an unsigned char
428 xdr_u_char(XDR
*xdrs
, u_char
*cp
)
433 if (!xdr_u_int(xdrs
, &u
)) {
444 xdr_bool(XDR
*xdrs
, bool_t
*bp
)
448 switch (xdrs
->x_op
) {
451 lb
= *bp
? XDR_TRUE
: XDR_FALSE
;
452 return (XDR_PUTLONG(xdrs
, &lb
));
455 if (!XDR_GETLONG(xdrs
, &lb
)) {
458 *bp
= (lb
== XDR_FALSE
) ? FALSE
: TRUE
;
471 xdr_enum(XDR
*xdrs
, enum_t
*ep
)
474 enum sizecheck
{ SIZEVAL
}; /* used to find the size of an enum */
477 * enums are treated as ints
479 if (sizeof (enum sizecheck
) == sizeof (long)) {
480 return (xdr_long(xdrs
, (long *)ep
));
481 } else if (sizeof (enum sizecheck
) == sizeof (int)) {
482 return (xdr_int(xdrs
, (int *)ep
));
483 } else if (sizeof (enum sizecheck
) == sizeof (short)) {
484 return (xdr_short(xdrs
, (short *)ep
));
489 (xdr_short(xdrs
, (short *)ep
));
490 (xdr_int(xdrs
, (int *)ep
));
491 return (xdr_long(xdrs
, (long *)ep
));
497 * Allows the specification of a fixed size sequence of opaque bytes.
498 * cp points to the opaque object and cnt gives the byte length.
501 xdr_opaque(XDR
*xdrs
, caddr_t cp
, u_int cnt
)
504 static int crud
[BYTES_PER_XDR_UNIT
];
507 * if no data we are done
513 * round byte count to full xdr units
515 rndup
= cnt
% BYTES_PER_XDR_UNIT
;
517 rndup
= BYTES_PER_XDR_UNIT
- rndup
;
519 if (xdrs
->x_op
== XDR_DECODE
) {
520 if (!XDR_GETBYTES(xdrs
, cp
, cnt
)) {
525 return (XDR_GETBYTES(xdrs
, (caddr_t
)crud
, rndup
));
528 if (xdrs
->x_op
== XDR_ENCODE
) {
529 if (!XDR_PUTBYTES(xdrs
, cp
, cnt
)) {
534 return (XDR_PUTBYTES(xdrs
, xdr_zero
, rndup
));
537 if (xdrs
->x_op
== XDR_FREE
) {
546 * *cpp is a pointer to the bytes, *sizep is the count.
547 * If *cpp is NULL maxsize bytes are allocated
550 xdr_bytes(XDR
*xdrs
, char **cpp
, u_int
*sizep
, u_int maxsize
)
552 char *sp
= *cpp
; /* sp is the actual string pointer */
556 * first deal with the length since xdr bytes are counted
558 if (! xdr_u_int(xdrs
, sizep
)) {
562 if ((nodesize
> maxsize
) && (xdrs
->x_op
!= XDR_FREE
)) {
567 * now deal with the actual bytes
569 switch (xdrs
->x_op
) {
576 *cpp
= sp
= (char *)mem_alloc(nodesize
);
579 fprintf(stderr
, "xdr_bytes: out of memory\n");
585 return (xdr_opaque(xdrs
, sp
, nodesize
));
589 mem_free(sp
, nodesize
);
598 * Implemented here due to commonality of the object.
601 xdr_netobj(XDR
*xdrs
, struct netobj
*np
)
603 return (xdr_bytes(xdrs
, &np
->n_bytes
, &np
->n_len
, MAX_NETOBJ_SZ
));
607 * XDR a descriminated union
608 * Support routine for discriminated unions.
609 * You create an array of xdrdiscrim structures, terminated with
610 * an entry with a null procedure pointer. The routine gets
611 * the discriminant value and then searches the array of xdrdiscrims
612 * looking for that value. It calls the procedure given in the xdrdiscrim
613 * to handle the discriminant. If there is no specific routine a default
614 * routine may be called.
615 * If there is no specific or default routine an error is returned.
618 * dscmp: enum to decide which ar to work on
619 * unp: the union itself
620 * choices: [value, xdr proc] for each arm
621 * dfault: default xdr routine
624 xdr_union(XDR
*xdrs
, enum_t
*dscmp
, char *unp
, struct xdr_discrim
*choices
,
630 * we deal with the discriminator; it's an enum
632 if (! xdr_enum(xdrs
, dscmp
)) {
638 * search choices for a value that matches the discriminator.
639 * if we find one, execute the xdr routine for that value.
641 for (; choices
->proc
!= NULL_xdrproc_t
; choices
++) {
642 if (choices
->value
== dscm
)
643 return ((*(choices
->proc
))(xdrs
, unp
, LASTUNSIGNED
));
647 * no match - execute the default xdr routine if there is one
649 return ((dfault
== NULL_xdrproc_t
) ? FALSE
:
650 (*dfault
)(xdrs
, unp
, LASTUNSIGNED
));
655 * Non-portable xdr primitives.
656 * Care should be taken when moving these routines to new architectures.
661 * XDR null terminated ASCII strings
662 * xdr_string deals with "C strings" - arrays of bytes that are
663 * terminated by a NULL character. The parameter cpp references a
664 * pointer to storage; If the pointer is null, then the necessary
665 * storage is allocated. The last parameter is the max allowed length
666 * of the string as specified by a protocol.
669 xdr_string(XDR
*xdrs
, char **cpp
, u_int maxsize
)
671 char *sp
= *cpp
; /* sp is the actual string pointer */
676 * first deal with the length since xdr strings are counted-strings
678 switch (xdrs
->x_op
) {
681 return(TRUE
); /* already free */
683 /* fall through... */
690 if (! xdr_u_int(xdrs
, &size
)) {
693 if (size
> maxsize
) {
699 * now deal with the actual bytes
701 switch (xdrs
->x_op
) {
708 *cpp
= sp
= (char *)mem_alloc(nodesize
);
710 fprintf(stderr
, "xdr_string: out of memory\n");
717 return (xdr_opaque(xdrs
, sp
, size
));
720 mem_free(sp
, nodesize
);
728 * Wrapper for xdr_string that can be called directly from
729 * routines like clnt_call
732 xdr_wrapstring(XDR
*xdrs
, char **cpp
)
734 return xdr_string(xdrs
, cpp
, LASTUNSIGNED
);