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 * $NetBSD: xdr.c,v 1.22 2000/07/06 03:10:35 christos Exp $
32 * $FreeBSD: src/lib/libc/xdr/xdr.c,v 1.14 2004/10/16 06:32:43 obrien Exp $
33 * $DragonFly: src/lib/libc/xdr/xdr.c,v 1.4 2005/12/05 00:47:57 swildner Exp $
37 * xdr.c, Generic XDR routines implementation.
39 * Copyright (C) 1986, Sun Microsystems, Inc.
41 * These are the "generic" xdr routines used to serialize and de-serialize
42 * most common data items. See xdr.h for more info on the interface to
46 #include "namespace.h"
52 #include <rpc/types.h>
54 #include "un-namespace.h"
56 typedef quad_t longlong_t
; /* ANSI long long type */
57 typedef u_quad_t u_longlong_t
; /* ANSI unsigned long long type */
60 * constants specific to the xdr "protocol"
62 #define XDR_FALSE ((long) 0)
63 #define XDR_TRUE ((long) 1)
64 #define LASTUNSIGNED ((u_int) 0-1)
69 static const char xdr_zero
[BYTES_PER_XDR_UNIT
] = { 0, 0, 0, 0 };
72 * Free a data structure using XDR
73 * Not a filter, but a convenient utility nonetheless
76 xdr_free(xdrproc_t proc
, void *objp
)
99 xdr_int(XDR
*xdrs
, int *ip
)
103 switch (xdrs
->x_op
) {
107 return (XDR_PUTLONG(xdrs
, &l
));
110 if (!XDR_GETLONG(xdrs
, &l
)) {
124 * XDR unsigned integers
127 xdr_u_int(XDR
*xdrs
, u_int
*up
)
131 switch (xdrs
->x_op
) {
135 return (XDR_PUTLONG(xdrs
, (long *)&l
));
138 if (!XDR_GETLONG(xdrs
, (long *)&l
)) {
154 * same as xdr_u_long - open coded to save a proc call!
157 xdr_long(XDR
*xdrs
, long *lp
)
159 switch (xdrs
->x_op
) {
161 return (XDR_PUTLONG(xdrs
, lp
));
163 return (XDR_GETLONG(xdrs
, lp
));
172 * XDR unsigned long integers
173 * same as xdr_long - open coded to save a proc call!
176 xdr_u_long(XDR
*xdrs
, u_long
*ulp
)
178 switch (xdrs
->x_op
) {
180 return (XDR_PUTLONG(xdrs
, (long *)ulp
));
182 return (XDR_GETLONG(xdrs
, (long *)ulp
));
192 * XDR 32-bit integers
193 * same as xdr_u_int32_t - open coded to save a proc call!
196 xdr_int32_t(XDR
*xdrs
, int32_t *int32_p
)
200 switch (xdrs
->x_op
) {
204 return (XDR_PUTLONG(xdrs
, &l
));
207 if (!XDR_GETLONG(xdrs
, &l
)) {
210 *int32_p
= (int32_t) l
;
221 * XDR unsigned 32-bit integers
222 * same as xdr_int32_t - open coded to save a proc call!
225 xdr_u_int32_t(XDR
*xdrs
, u_int32_t
*u_int32_p
)
229 switch (xdrs
->x_op
) {
232 l
= (u_long
) *u_int32_p
;
233 return (XDR_PUTLONG(xdrs
, (long *)&l
));
236 if (!XDR_GETLONG(xdrs
, (long *)&l
)) {
239 *u_int32_p
= (u_int32_t
) l
;
254 xdr_short(XDR
*xdrs
, short *sp
)
258 switch (xdrs
->x_op
) {
262 return (XDR_PUTLONG(xdrs
, &l
));
265 if (!XDR_GETLONG(xdrs
, &l
)) {
279 * XDR unsigned short integers
282 xdr_u_short(XDR
*xdrs
, u_short
*usp
)
286 switch (xdrs
->x_op
) {
290 return (XDR_PUTLONG(xdrs
, (long *)&l
));
293 if (!XDR_GETLONG(xdrs
, (long *)&l
)) {
308 * XDR 16-bit integers
311 xdr_int16_t(XDR
*xdrs
, int16_t *int16_p
)
315 switch (xdrs
->x_op
) {
319 return (XDR_PUTLONG(xdrs
, &l
));
322 if (!XDR_GETLONG(xdrs
, &l
)) {
325 *int16_p
= (int16_t) l
;
336 * XDR unsigned 16-bit integers
339 xdr_u_int16_t(XDR
*xdrs
, u_int16_t
*u_int16_p
)
343 switch (xdrs
->x_op
) {
346 l
= (u_long
) *u_int16_p
;
347 return (XDR_PUTLONG(xdrs
, (long *)&l
));
350 if (!XDR_GETLONG(xdrs
, (long *)&l
)) {
353 *u_int16_p
= (u_int16_t
) l
;
368 xdr_char(XDR
*xdrs
, char *cp
)
373 if (!xdr_int(xdrs
, &i
)) {
381 * XDR an unsigned char
384 xdr_u_char(XDR
*xdrs
, u_char
*cp
)
389 if (!xdr_u_int(xdrs
, &u
)) {
400 xdr_bool(XDR
*xdrs
, bool_t
*bp
)
404 switch (xdrs
->x_op
) {
407 lb
= *bp
? XDR_TRUE
: XDR_FALSE
;
408 return (XDR_PUTLONG(xdrs
, &lb
));
411 if (!XDR_GETLONG(xdrs
, &lb
)) {
414 *bp
= (lb
== XDR_FALSE
) ? FALSE
: TRUE
;
428 xdr_enum(XDR
*xdrs
, enum_t
*ep
)
430 enum sizecheck
{ SIZEVAL
}; /* used to find the size of an enum */
433 * enums are treated as ints
435 /* LINTED */ if (sizeof (enum sizecheck
) == sizeof (long)) {
436 return (xdr_long(xdrs
, (long *)(void *)ep
));
437 } else /* LINTED */ if (sizeof (enum sizecheck
) == sizeof (int)) {
438 return (xdr_int(xdrs
, (int *)(void *)ep
));
439 } else /* LINTED */ if (sizeof (enum sizecheck
) == sizeof (short)) {
440 return (xdr_short(xdrs
, (short *)(void *)ep
));
448 * Allows the specification of a fixed size sequence of opaque bytes.
449 * cp points to the opaque object and cnt gives the byte length.
452 xdr_opaque(XDR
*xdrs
, caddr_t cp
, u_int cnt
)
455 static int crud
[BYTES_PER_XDR_UNIT
];
458 * if no data we are done
464 * round byte count to full xdr units
466 rndup
= cnt
% BYTES_PER_XDR_UNIT
;
468 rndup
= BYTES_PER_XDR_UNIT
- rndup
;
470 if (xdrs
->x_op
== XDR_DECODE
) {
471 if (!XDR_GETBYTES(xdrs
, cp
, cnt
)) {
476 return (XDR_GETBYTES(xdrs
, (caddr_t
)(void *)crud
, rndup
));
479 if (xdrs
->x_op
== XDR_ENCODE
) {
480 if (!XDR_PUTBYTES(xdrs
, cp
, cnt
)) {
485 return (XDR_PUTBYTES(xdrs
, xdr_zero
, rndup
));
488 if (xdrs
->x_op
== XDR_FREE
) {
497 * *cpp is a pointer to the bytes, *sizep is the count.
498 * If *cpp is NULL maxsize bytes are allocated
501 xdr_bytes(XDR
*xdrs
, char **cpp
, u_int
*sizep
, u_int maxsize
)
503 char *sp
= *cpp
; /* sp is the actual string pointer */
507 * first deal with the length since xdr bytes are counted
509 if (! xdr_u_int(xdrs
, sizep
)) {
513 if ((nodesize
> maxsize
) && (xdrs
->x_op
!= XDR_FREE
)) {
518 * now deal with the actual bytes
520 switch (xdrs
->x_op
) {
527 *cpp
= sp
= mem_alloc(nodesize
);
530 warnx("xdr_bytes: out of memory");
536 return (xdr_opaque(xdrs
, sp
, nodesize
));
540 mem_free(sp
, nodesize
);
550 * Implemented here due to commonality of the object.
553 xdr_netobj(XDR
*xdrs
, struct netobj
*np
)
555 return (xdr_bytes(xdrs
, &np
->n_bytes
, &np
->n_len
, MAX_NETOBJ_SZ
));
559 * XDR a descriminated union
560 * Support routine for discriminated unions.
561 * You create an array of xdrdiscrim structures, terminated with
562 * an entry with a null procedure pointer. The routine gets
563 * the discriminant value and then searches the array of xdrdiscrims
564 * looking for that value. It calls the procedure given in the xdrdiscrim
565 * to handle the discriminant. If there is no specific routine a default
566 * routine may be called.
567 * If there is no specific or default routine an error is returned.
570 * dscmp: enum to decide which ar to work on
571 * unp: the union itself
572 * choices: [value, xdr proc] for each arm
573 * dfault: default xdr routine
576 xdr_union(XDR
*xdrs
, enum_t
*dscmp
, char *unp
,
577 const struct xdr_discrim
*choices
, xdrproc_t dfault
)
582 * we deal with the discriminator; it's an enum
584 if (! xdr_enum(xdrs
, dscmp
)) {
590 * search choices for a value that matches the discriminator.
591 * if we find one, execute the xdr routine for that value.
593 for (; choices
->proc
!= NULL_xdrproc_t
; choices
++) {
594 if (choices
->value
== dscm
)
595 return ((*(choices
->proc
))(xdrs
, unp
));
599 * no match - execute the default xdr routine if there is one
601 return ((dfault
== NULL_xdrproc_t
) ? FALSE
:
602 (*dfault
)(xdrs
, unp
));
607 * Non-portable xdr primitives.
608 * Care should be taken when moving these routines to new architectures.
613 * XDR null terminated ASCII strings
614 * xdr_string deals with "C strings" - arrays of bytes that are
615 * terminated by a NULL character. The parameter cpp references a
616 * pointer to storage; If the pointer is null, then the necessary
617 * storage is allocated. The last parameter is the max allowed length
618 * of the string as specified by a protocol.
621 xdr_string(XDR
*xdrs
, char **cpp
, u_int maxsize
)
623 char *sp
= *cpp
; /* sp is the actual string pointer */
628 * first deal with the length since xdr strings are counted-strings
630 switch (xdrs
->x_op
) {
633 return(TRUE
); /* already free */
642 if (! xdr_u_int(xdrs
, &size
)) {
645 if (size
> maxsize
) {
651 * now deal with the actual bytes
653 switch (xdrs
->x_op
) {
660 *cpp
= sp
= mem_alloc(nodesize
);
662 warnx("xdr_string: out of memory");
669 return (xdr_opaque(xdrs
, sp
, size
));
672 mem_free(sp
, nodesize
);
681 * Wrapper for xdr_string that can be called directly from
682 * routines like clnt_call
685 xdr_wrapstring(XDR
*xdrs
, char **cpp
)
687 return xdr_string(xdrs
, cpp
, LASTUNSIGNED
);
691 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
692 * are in the "non-portable" section because they require that a `long long'
695 * --thorpej@netbsd.org, November 30, 1999
699 * XDR 64-bit integers
702 xdr_int64_t(XDR
*xdrs
, int64_t *llp
)
706 switch (xdrs
->x_op
) {
708 ul
[0] = (u_long
)((u_int64_t
)*llp
>> 32) & 0xffffffff;
709 ul
[1] = (u_long
)((u_int64_t
)*llp
) & 0xffffffff;
710 if (XDR_PUTLONG(xdrs
, (long *)&ul
[0]) == FALSE
)
712 return (XDR_PUTLONG(xdrs
, (long *)&ul
[1]));
714 if (XDR_GETLONG(xdrs
, (long *)&ul
[0]) == FALSE
)
716 if (XDR_GETLONG(xdrs
, (long *)&ul
[1]) == FALSE
)
719 (((u_int64_t
)ul
[0] << 32) | ((u_int64_t
)ul
[1]));
730 * XDR unsigned 64-bit integers
733 xdr_u_int64_t(XDR
*xdrs
, u_int64_t
*ullp
)
737 switch (xdrs
->x_op
) {
739 ul
[0] = (u_long
)(*ullp
>> 32) & 0xffffffff;
740 ul
[1] = (u_long
)(*ullp
) & 0xffffffff;
741 if (XDR_PUTLONG(xdrs
, (long *)&ul
[0]) == FALSE
)
743 return (XDR_PUTLONG(xdrs
, (long *)&ul
[1]));
745 if (XDR_GETLONG(xdrs
, (long *)&ul
[0]) == FALSE
)
747 if (XDR_GETLONG(xdrs
, (long *)&ul
[1]) == FALSE
)
750 (((u_int64_t
)ul
[0] << 32) | ((u_int64_t
)ul
[1]));
764 xdr_hyper(XDR
*xdrs
, longlong_t
*llp
)
768 * Don't bother open-coding this; it's a fair amount of code. Just
769 * call xdr_int64_t().
771 return (xdr_int64_t(xdrs
, (int64_t *)llp
));
776 * XDR unsigned hypers
779 xdr_u_hyper(XDR
*xdrs
, u_longlong_t
*ullp
)
783 * Don't bother open-coding this; it's a fair amount of code. Just
784 * call xdr_u_int64_t().
786 return (xdr_u_int64_t(xdrs
, (u_int64_t
*)ullp
));
794 xdr_longlong_t(XDR
*xdrs
, longlong_t
*llp
)
798 * Don't bother open-coding this; it's a fair amount of code. Just
799 * call xdr_int64_t().
801 return (xdr_int64_t(xdrs
, (int64_t *)llp
));
809 xdr_u_longlong_t(XDR
*xdrs
, u_longlong_t
*ullp
)
813 * Don't bother open-coding this; it's a fair amount of code. Just
814 * call xdr_u_int64_t().
816 return (xdr_u_int64_t(xdrs
, (u_int64_t
*)ullp
));