2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
32 * xdr.c, Generic XDR routines implementation.
34 * Copyright (C) 1986, Sun Microsystems, Inc.
36 * These are the "generic" xdr routines used to serialize and de-serialize
37 * most common data items. See xdr.h for more info on the interface to
46 #include <rpc/types.h>
49 typedef quad_t longlong_t; /* ANSI long long type */
50 typedef u_quad_t u_longlong_t; /* ANSI unsigned long long type */
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 const 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
100 switch (xdrs->x_op) {
104 return (XDR_PUTLONG(xdrs, &l));
107 if (!XDR_GETLONG(xdrs, &l)) {
121 * XDR unsigned integers
130 switch (xdrs->x_op) {
134 return (XDR_PUTLONG(xdrs, (long *)&l));
137 if (!XDR_GETLONG(xdrs, (long *)&l)) {
153 * same as xdr_u_long - open coded to save a proc call!
160 switch (xdrs->x_op) {
162 return (XDR_PUTLONG(xdrs, lp));
164 return (XDR_GETLONG(xdrs, lp));
173 * XDR unsigned long integers
174 * same as xdr_long - open coded to save a proc call!
177 xdr_u_long(xdrs, ulp)
181 switch (xdrs->x_op) {
183 return (XDR_PUTLONG(xdrs, (long *)ulp));
185 return (XDR_GETLONG(xdrs, (long *)ulp));
195 * XDR 32-bit integers
196 * same as xdr_u_int32_t - open coded to save a proc call!
199 xdr_int32_t(xdrs, int32_p)
205 switch (xdrs->x_op) {
209 return (XDR_PUTLONG(xdrs, &l));
212 if (!XDR_GETLONG(xdrs, &l)) {
215 *int32_p = (int32_t) l;
226 * XDR unsigned 32-bit integers
227 * same as xdr_int32_t - open coded to save a proc call!
230 xdr_u_int32_t(xdrs, u_int32_p)
232 u_int32_t *u_int32_p;
236 switch (xdrs->x_op) {
239 l = (u_long) *u_int32_p;
240 return (XDR_PUTLONG(xdrs, (long *)&l));
243 if (!XDR_GETLONG(xdrs, (long *)&l)) {
246 *u_int32_p = (u_int32_t) l;
258 * XDR unsigned 32-bit integers
261 xdr_uint32_t(xdrs, uint32_p)
265 return (xdr_u_int32_t(xdrs, (u_int32_t *)uint32_p));
279 switch (xdrs->x_op) {
283 return (XDR_PUTLONG(xdrs, &l));
286 if (!XDR_GETLONG(xdrs, &l)) {
300 * XDR unsigned short integers
303 xdr_u_short(xdrs, usp)
309 switch (xdrs->x_op) {
313 return (XDR_PUTLONG(xdrs, (long *)&l));
316 if (!XDR_GETLONG(xdrs, (long *)&l)) {
331 * XDR 16-bit integers
334 xdr_int16_t(xdrs, int16_p)
340 switch (xdrs->x_op) {
344 return (XDR_PUTLONG(xdrs, &l));
347 if (!XDR_GETLONG(xdrs, &l)) {
350 *int16_p = (int16_t) l;
361 * XDR unsigned 16-bit integers
364 xdr_u_int16_t(xdrs, u_int16_p)
366 u_int16_t *u_int16_p;
370 switch (xdrs->x_op) {
373 l = (u_long) *u_int16_p;
374 return (XDR_PUTLONG(xdrs, (long *)&l));
377 if (!XDR_GETLONG(xdrs, (long *)&l)) {
380 *u_int16_p = (u_int16_t) l;
392 * XDR unsigned 16-bit integers
395 xdr_uint16_t(xdrs, uint16_p)
399 return (xdr_u_int16_t(xdrs, (u_int16_t *)uint16_p));
407 xdr_int8_t(xdrs, int8_p)
413 switch (xdrs->x_op) {
417 return (XDR_PUTLONG(xdrs, &l));
420 if (!XDR_GETLONG(xdrs, &l)) {
423 *int8_p = (int8_t) l;
435 * XDR unsigned 8-bit integers
438 xdr_u_int8_t(xdrs, uint8_p)
444 switch (xdrs->x_op) {
447 l = (u_long) *uint8_p;
448 return (XDR_PUTLONG(xdrs, (long *)&l));
451 if (!XDR_GETLONG(xdrs, (long *)&l)) {
454 *uint8_p = (uint8_t) l;
466 * XDR unsigned 8-bit integers
469 xdr_uint8_t(xdrs, uint8_p)
473 return (xdr_u_int8_t(xdrs, (uint8_t *)uint8_p));
488 if (!xdr_int(xdrs, &i)) {
496 * XDR an unsigned char
506 if (!xdr_u_int(xdrs, &u)) {
523 switch (xdrs->x_op) {
526 lb = *bp ? XDR_TRUE : XDR_FALSE;
527 return (XDR_PUTLONG(xdrs, &lb));
530 if (!XDR_GETLONG(xdrs, &lb)) {
533 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
551 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
554 * enums are treated as ints
556 /* LINTED */ if (sizeof (enum sizecheck) == sizeof (long)) {
557 return (xdr_long(xdrs, (long *)(void *)ep));
558 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) {
559 return (xdr_int(xdrs, (int *)(void *)ep));
560 } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (short)) {
561 return (xdr_short(xdrs, (short *)(void *)ep));
569 * Allows the specification of a fixed size sequence of opaque bytes.
570 * cp points to the opaque object and cnt gives the byte length.
573 xdr_opaque(xdrs, cp, cnt)
579 static int crud[BYTES_PER_XDR_UNIT];
582 * if no data we are done
588 * round byte count to full xdr units
590 rndup = cnt % BYTES_PER_XDR_UNIT;
592 rndup = BYTES_PER_XDR_UNIT - rndup;
594 if (xdrs->x_op == XDR_DECODE) {
595 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
600 return (XDR_GETBYTES(xdrs, (caddr_t)(void *)crud, rndup));
603 if (xdrs->x_op == XDR_ENCODE) {
604 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
609 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
612 if (xdrs->x_op == XDR_FREE) {
621 * *cpp is a pointer to the bytes, *sizep is the count.
622 * If *cpp is NULL maxsize bytes are allocated
625 xdr_bytes(xdrs, cpp, sizep, maxsize)
631 char *sp = *cpp; /* sp is the actual string pointer */
635 * first deal with the length since xdr bytes are counted
637 if (! xdr_u_int(xdrs, sizep)) {
641 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
646 * now deal with the actual bytes
648 switch (xdrs->x_op) {
655 *cpp = sp = mem_alloc(nodesize);
658 warnx("xdr_bytes: out of memory");
664 return (xdr_opaque(xdrs, sp, nodesize));
668 mem_free(sp, nodesize);
678 * Implemented here due to commonality of the object.
686 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
690 * XDR a descriminated union
691 * Support routine for discriminated unions.
692 * You create an array of xdrdiscrim structures, terminated with
693 * an entry with a null procedure pointer. The routine gets
694 * the discriminant value and then searches the array of xdrdiscrims
695 * looking for that value. It calls the procedure given in the xdrdiscrim
696 * to handle the discriminant. If there is no specific routine a default
697 * routine may be called.
698 * If there is no specific or default routine an error is returned.
701 xdr_union(xdrs, dscmp, unp, choices, dfault)
703 enum_t *dscmp; /* enum to decide which arm to work on */
704 char *unp; /* the union itself */
705 const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
706 xdrproc_t dfault; /* default xdr routine */
711 * we deal with the discriminator; it's an enum
713 if (! xdr_enum(xdrs, dscmp)) {
719 * search choices for a value that matches the discriminator.
720 * if we find one, execute the xdr routine for that value.
722 for (; choices->proc != NULL_xdrproc_t; choices++) {
723 if (choices->value == dscm)
724 return ((*(choices->proc))(xdrs, unp));
728 * no match - execute the default xdr routine if there is one
730 return ((dfault == NULL_xdrproc_t) ? FALSE :
731 (*dfault)(xdrs, unp));
736 * Non-portable xdr primitives.
737 * Care should be taken when moving these routines to new architectures.
742 * XDR null terminated ASCII strings
743 * xdr_string deals with "C strings" - arrays of bytes that are
744 * terminated by a NULL character. The parameter cpp references a
745 * pointer to storage; If the pointer is null, then the necessary
746 * storage is allocated. The last parameter is the max allowed length
747 * of the string as specified by a protocol.
750 xdr_string(xdrs, cpp, maxsize)
755 char *sp = *cpp; /* sp is the actual string pointer */
760 * first deal with the length since xdr strings are counted-strings
762 switch (xdrs->x_op) {
765 return(TRUE); /* already free */
776 if (! xdr_u_int(xdrs, &size)) {
779 if (size > maxsize) {
784 /* This means an overflow. It a bug in the caller which
785 * provided a too large maxsize but nevertheless catch it
792 * now deal with the actual bytes
794 switch (xdrs->x_op) {
798 *cpp = sp = mem_alloc(nodesize);
800 warnx("xdr_string: out of memory");
807 return (xdr_opaque(xdrs, sp, size));
810 mem_free(sp, nodesize);
819 * Wrapper for xdr_string that can be called directly from
820 * routines like clnt_call
823 xdr_wrapstring(xdrs, cpp)
827 return xdr_string(xdrs, cpp, LASTUNSIGNED);
831 * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t()
832 * are in the "non-portable" section because they require that a `long long'
835 * --thorpej@netbsd.org, November 30, 1999
839 * XDR 64-bit integers
842 xdr_int64_t(xdrs, llp)
848 switch (xdrs->x_op) {
850 ul[0] = (u_long)((u_int64_t)*llp >> 32) & 0xffffffff;
851 ul[1] = (u_long)((u_int64_t)*llp) & 0xffffffff;
852 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
854 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
856 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
858 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
861 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
872 * XDR unsigned 64-bit integers
875 xdr_u_int64_t(xdrs, ullp)
881 switch (xdrs->x_op) {
883 ul[0] = (u_long)(*ullp >> 32) & 0xffffffff;
884 ul[1] = (u_long)(*ullp) & 0xffffffff;
885 if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE)
887 return (XDR_PUTLONG(xdrs, (long *)&ul[1]));
889 if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE)
891 if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE)
894 (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1]));
905 * XDR unsigned 64-bit integers
908 xdr_uint64_t(xdrs, ullp)
912 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));
926 * Don't bother open-coding this; it's a fair amount of code. Just
927 * call xdr_int64_t().
929 return (xdr_int64_t(xdrs, (int64_t *)llp));
934 * XDR unsigned hypers
937 xdr_u_hyper(xdrs, ullp)
943 * Don't bother open-coding this; it's a fair amount of code. Just
944 * call xdr_u_int64_t().
946 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));
954 xdr_longlong_t(xdrs, llp)
960 * Don't bother open-coding this; it's a fair amount of code. Just
961 * call xdr_int64_t().
963 return (xdr_int64_t(xdrs, (int64_t *)llp));
971 xdr_u_longlong_t(xdrs, ullp)
977 * Don't bother open-coding this; it's a fair amount of code. Just
978 * call xdr_u_int64_t().
980 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));
987 xdr_quad_t(xdrs, llp)
991 return (xdr_int64_t(xdrs, (int64_t *)llp));
999 xdr_u_quad_t(xdrs, ullp)
1003 return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp));