1 /* @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC */
3 * Copyright (c) 2010, Oracle America, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * * Neither the name of the "Oracle America, Inc." nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #if !defined(lint) && defined(SCCSIDS)
35 static char sccsid[] = "@(#)xdr.c 1.35 87/08/12";
39 * xdr.c, Generic XDR routines implementation.
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
49 #include <gssrpc/types.h>
50 #include <gssrpc/xdr.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)
60 #include <valgrind/memcheck.h>
62 #define VALGRIND_CHECK_DEFINED(LVALUE) ((void)0)
63 #define VALGRIND_CHECK_READABLE(PTR,SIZE) ((void)0)
69 static 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)
88 xdr_void(XDR *xdrs, void *addr)
98 xdr_int(XDR *xdrs, int *ip)
102 switch (xdrs->x_op) {
105 VALGRIND_CHECK_DEFINED(*ip);
106 if (*ip > 0x7fffffffL || *ip < -0x7fffffffL - 1L)
110 return (XDR_PUTLONG(xdrs, &l));
113 if (!XDR_GETLONG(xdrs, &l))
116 if (l > INT_MAX || l < INT_MIN)
129 * XDR unsigned integers
132 xdr_u_int(XDR *xdrs, u_int *up)
136 switch (xdrs->x_op) {
139 VALGRIND_CHECK_DEFINED(*up);
140 if (*up > 0xffffffffUL)
144 return (XDR_PUTLONG(xdrs, (long *) &l));
147 if (!XDR_GETLONG(xdrs, (long *) &l))
150 if ((uint32_t)l > UINT_MAX)
167 xdr_long(XDR *xdrs, long *lp)
170 switch (xdrs->x_op) {
172 VALGRIND_CHECK_DEFINED(*lp);
173 if (*lp > 0x7fffffffL || *lp < -0x7fffffffL - 1L)
176 return (XDR_PUTLONG(xdrs, lp));
179 return (XDR_GETLONG(xdrs, lp));
188 * XDR unsigned long integers
191 xdr_u_long(XDR *xdrs, u_long *ulp)
194 switch (xdrs->x_op) {
196 VALGRIND_CHECK_DEFINED(*ulp);
197 if (*ulp > 0xffffffffUL)
200 return (XDR_PUTLONG(xdrs, (long *) ulp));
203 return (XDR_GETLONG(xdrs, (long *) ulp));
215 xdr_short(XDR *xdrs, short *sp)
219 switch (xdrs->x_op) {
222 VALGRIND_CHECK_DEFINED(*sp);
224 return (XDR_PUTLONG(xdrs, &l));
227 if (!XDR_GETLONG(xdrs, &l)) {
230 if (l > SHRT_MAX || l < SHRT_MIN)
243 * XDR unsigned short integers
246 xdr_u_short(XDR *xdrs, u_short *usp)
250 switch (xdrs->x_op) {
253 VALGRIND_CHECK_DEFINED(*usp);
255 return (XDR_PUTLONG(xdrs, (long *) &l));
258 if (!XDR_GETLONG(xdrs, (long *) &l)) {
275 xdr_char(XDR *xdrs, char *cp)
279 switch (xdrs->x_op) {
281 VALGRIND_CHECK_DEFINED(*cp);
287 if (!xdr_int(xdrs, &i)) {
295 * XDR an unsigned char
298 xdr_u_char(XDR *xdrs, u_char *cp)
302 switch (xdrs->x_op) {
304 VALGRIND_CHECK_DEFINED(*cp);
310 if (!xdr_u_int(xdrs, &u)) {
321 xdr_bool(XDR *xdrs, bool_t *bp)
325 switch (xdrs->x_op) {
328 VALGRIND_CHECK_DEFINED(*bp);
329 lb = *bp ? XDR_TRUE : XDR_FALSE;
330 return (XDR_PUTLONG(xdrs, &lb));
333 if (!XDR_GETLONG(xdrs, &lb)) {
336 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
349 xdr_enum(XDR *xdrs, enum_t *ep)
352 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
355 * enums are treated as ints
357 switch (xdrs->x_op) {
359 VALGRIND_CHECK_DEFINED(*ep);
364 if (sizeof (enum sizecheck) == sizeof (long)) {
365 return (xdr_long(xdrs, (long *)(void *)ep));
366 } else if (sizeof (enum sizecheck) == sizeof (int)) {
367 return (xdr_int(xdrs, (int *)(void *)ep));
368 } else if (sizeof (enum sizecheck) == sizeof (short)) {
369 return (xdr_short(xdrs, (short *)(void *)ep));
374 (void) (xdr_short(xdrs, (short *)(void *)ep));
375 return (xdr_long(xdrs, (long *)(void *)ep));
381 * Allows the specification of a fixed size sequence of opaque bytes.
382 * cp points to the opaque object and cnt gives the byte length.
385 xdr_opaque(XDR *xdrs, caddr_t cp, u_int cnt)
388 static int crud[BYTES_PER_XDR_UNIT];
391 * if no data we are done
397 * round byte count to full xdr units
399 rndup = cnt % BYTES_PER_XDR_UNIT;
401 rndup = BYTES_PER_XDR_UNIT - rndup;
403 if (xdrs->x_op == XDR_DECODE) {
404 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
409 return (XDR_GETBYTES(xdrs, (caddr_t) (void *)crud, rndup));
412 if (xdrs->x_op == XDR_ENCODE) {
413 VALGRIND_CHECK_READABLE((volatile void *)cp, cnt);
414 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
419 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
422 if (xdrs->x_op == XDR_FREE) {
431 * *cpp is a pointer to the bytes, *sizep is the count.
432 * If *cpp is NULL maxsize bytes are allocated
441 char *sp = *cpp; /* sp is the actual string pointer */
445 * first deal with the length since xdr bytes are counted
447 if (! xdr_u_int(xdrs, sizep)) {
451 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
456 * now deal with the actual bytes
458 switch (xdrs->x_op) {
465 *cpp = sp = (char *)mem_alloc(nodesize);
468 (void) fprintf(stderr, "xdr_bytes: out of memory\n");
474 return (xdr_opaque(xdrs, sp, nodesize));
478 mem_free(sp, nodesize);
487 * Implemented here due to commonality of the object.
490 xdr_netobj(XDR *xdrs, struct netobj *np)
493 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
497 xdr_int32(XDR *xdrs, int32_t *ip)
501 switch (xdrs->x_op) {
504 VALGRIND_CHECK_DEFINED(*ip);
506 return (xdr_long(xdrs, &l));
509 if (!xdr_long(xdrs, &l)) {
522 xdr_u_int32(XDR *xdrs, uint32_t *up)
526 switch (xdrs->x_op) {
529 VALGRIND_CHECK_DEFINED(*up);
531 return (xdr_u_long(xdrs, &ul));
534 if (!xdr_u_long(xdrs, &ul)) {
547 * XDR a discriminated union
548 * Support routine for discriminated unions.
549 * You create an array of xdrdiscrim structures, terminated with
550 * an entry with a null procedure pointer. The routine gets
551 * the discriminant value and then searches the array of xdrdiscrims
552 * looking for that value. It calls the procedure given in the xdrdiscrim
553 * to handle the discriminant. If there is no specific routine a default
554 * routine may be called.
555 * If there is no specific or default routine an error is returned.
560 enum_t *dscmp, /* enum to decide which arm to work on */
561 char *unp, /* the union itself */
562 struct xdr_discrim *choices, /* [value, xdr proc] for each arm */
563 xdrproc_t dfault /* default xdr routine */
569 * we deal with the discriminator; it's an enum
571 if (! xdr_enum(xdrs, dscmp)) {
577 * search choices for a value that matches the discriminator.
578 * if we find one, execute the xdr routine for that value.
580 for (; choices->proc != NULL_xdrproc_t; choices++) {
581 if (choices->value == dscm)
582 return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
586 * no match - execute the default xdr routine if there is one
588 return ((dfault == NULL_xdrproc_t) ? FALSE :
589 (*dfault)(xdrs, unp, LASTUNSIGNED));
594 * Non-portable xdr primitives.
595 * Care should be taken when moving these routines to new architectures.
600 * XDR null terminated ASCII strings
601 * xdr_string deals with "C strings" - arrays of bytes that are
602 * terminated by a NULL character. The parameter cpp references a
603 * pointer to storage; If the pointer is null, then the necessary
604 * storage is allocated. The last parameter is the max allowed length
605 * of the string as specified by a protocol.
608 xdr_string(XDR *xdrs, char **cpp, u_int maxsize)
610 char *sp = *cpp; /* sp is the actual string pointer */
615 * first deal with the length since xdr strings are counted-strings
617 switch (xdrs->x_op) {
620 return(TRUE); /* already free */
622 /* fall through... */
629 if (! xdr_u_int(xdrs, &size)) {
632 if (size >= maxsize) {
638 * now deal with the actual bytes
640 switch (xdrs->x_op) {
647 *cpp = sp = (char *)mem_alloc(nodesize);
649 (void) fprintf(stderr, "xdr_string: out of memory\n");
656 return (xdr_opaque(xdrs, sp, size));
659 mem_free(sp, nodesize);
667 * Wrapper for xdr_string that can be called directly from
668 * routines like clnt_call
671 xdr_wrapstring(XDR *xdrs, char **cpp)
673 if (xdr_string(xdrs, cpp, LASTUNSIGNED)) {