4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * Portions of this source code were derived from Berkeley 4.3 BSD
32 * under license from the Regents of the University of California.
35 #pragma ident "%Z%%M% %I% %E% SMI"
38 * xdr.c, generic XDR routines implementation.
39 * These are the "generic" xdr routines used to serialize and de-serialize
40 * most common data items. See xdr.h for more info on the interface to
44 #include <sys/param.h>
45 #include <sys/cmn_err.h>
46 #include <sys/types.h>
47 #include <sys/systm.h>
49 #include <rpc/types.h>
52 #pragma weak xdr_int32_t = xdr_int
53 #pragma weak xdr_uint32_t = xdr_u_int
54 #pragma weak xdr_int64_t = xdr_longlong_t
55 #pragma weak xdr_uint64_t = xdr_u_longlong_t
57 #if !defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
58 #error "Exactly one of _BIG_ENDIAN or _LITTLE_ENDIAN must be defined"
59 #elif defined(_BIG_ENDIAN) && defined(_LITTLE_ENDIAN)
60 #error "Only one of _BIG_ENDIAN or _LITTLE_ENDIAN may be defined"
64 * constants specific to the xdr "protocol"
66 #define XDR_FALSE ((int32_t)0)
67 #define XDR_TRUE ((int32_t)1)
68 #define LASTUNSIGNED ((uint_t)0-1)
73 static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
76 * Free a data structure using XDR
77 * Not a filter, but a convenient utility nonetheless
80 xdr_free(xdrproc_t proc, char *objp)
100 * PSARC 2003/523 Contract Private Interface
102 * Changes must be reviewed by Solaris File Sharing
103 * Changes must be communicated to contract-2003-523@sun.com
106 xdr_int(XDR *xdrs, int *ip)
108 if (xdrs->x_op == XDR_ENCODE)
109 return (XDR_PUTINT32(xdrs, ip));
111 if (xdrs->x_op == XDR_DECODE)
112 return (XDR_GETINT32(xdrs, ip));
114 if (xdrs->x_op == XDR_FREE)
118 printf("xdr_int: FAILED\n");
124 * XDR unsigned integers
126 * PSARC 2003/523 Contract Private Interface
128 * Changes must be reviewed by Solaris File Sharing
129 * Changes must be communicated to contract-2003-523@sun.com
132 xdr_u_int(XDR *xdrs, uint_t *up)
134 if (xdrs->x_op == XDR_ENCODE)
135 return (XDR_PUTINT32(xdrs, (int32_t *)up));
137 if (xdrs->x_op == XDR_DECODE)
138 return (XDR_GETINT32(xdrs, (int32_t *)up));
140 if (xdrs->x_op == XDR_FREE)
144 printf("xdr_int: FAILED\n");
152 * xdr_long and xdr_u_long for binary compatability on ILP32 kernels.
154 * No prototypes since new code should not be using these interfaces.
157 xdr_long(XDR *xdrs, long *ip)
159 return (xdr_int(xdrs, (int *)ip));
163 xdr_u_long(XDR *xdrs, unsigned long *up)
165 return (xdr_u_int(xdrs, (uint_t *)up));
171 * XDR long long integers
174 xdr_longlong_t(XDR *xdrs, longlong_t *hp)
176 if (xdrs->x_op == XDR_ENCODE) {
177 #if defined(_LITTLE_ENDIAN)
178 if (XDR_PUTINT32(xdrs, (int32_t *)((char *)hp +
179 BYTES_PER_XDR_UNIT)) == TRUE) {
180 return (XDR_PUTINT32(xdrs, (int32_t *)hp));
182 #elif defined(_BIG_ENDIAN)
183 if (XDR_PUTINT32(xdrs, (int32_t *)hp) == TRUE) {
184 return (XDR_PUTINT32(xdrs, (int32_t *)((char *)hp +
185 BYTES_PER_XDR_UNIT)));
191 if (xdrs->x_op == XDR_DECODE) {
192 #if defined(_LITTLE_ENDIAN)
193 if (XDR_GETINT32(xdrs, (int32_t *)((char *)hp +
194 BYTES_PER_XDR_UNIT)) == TRUE) {
195 return (XDR_GETINT32(xdrs, (int32_t *)hp));
197 #elif defined(_BIG_ENDIAN)
198 if (XDR_GETINT32(xdrs, (int32_t *)hp) == TRUE) {
199 return (XDR_GETINT32(xdrs, (int32_t *)((char *)hp +
200 BYTES_PER_XDR_UNIT)));
209 * XDR unsigned long long integers
212 xdr_u_longlong_t(XDR *xdrs, u_longlong_t *hp)
215 if (xdrs->x_op == XDR_ENCODE) {
216 #if defined(_LITTLE_ENDIAN)
217 if (XDR_PUTINT32(xdrs, (int32_t *)((char *)hp +
218 BYTES_PER_XDR_UNIT)) == TRUE) {
219 return (XDR_PUTINT32(xdrs, (int32_t *)hp));
221 #elif defined(_BIG_ENDIAN)
222 if (XDR_PUTINT32(xdrs, (int32_t *)hp) == TRUE) {
223 return (XDR_PUTINT32(xdrs, (int32_t *)((char *)hp +
224 BYTES_PER_XDR_UNIT)));
230 if (xdrs->x_op == XDR_DECODE) {
231 #if defined(_LITTLE_ENDIAN)
232 if (XDR_GETINT32(xdrs, (int32_t *)((char *)hp +
233 BYTES_PER_XDR_UNIT)) == TRUE) {
234 return (XDR_GETINT32(xdrs, (int32_t *)hp));
236 #elif defined(_BIG_ENDIAN)
237 if (XDR_GETINT32(xdrs, (int32_t *)hp) == TRUE) {
238 return (XDR_GETINT32(xdrs, (int32_t *)((char *)hp +
239 BYTES_PER_XDR_UNIT)));
251 xdr_short(XDR *xdrs, short *sp)
255 switch (xdrs->x_op) {
259 return (XDR_PUTINT32(xdrs, &l));
262 if (!XDR_GETINT32(xdrs, &l))
274 * XDR unsigned short integers
277 xdr_u_short(XDR *xdrs, ushort_t *usp)
281 switch (xdrs->x_op) {
285 return (XDR_PUTINT32(xdrs, (int32_t *)&l));
288 if (!XDR_GETINT32(xdrs, (int32_t *)&l)) {
290 printf("xdr_u_short: decode FAILED\n");
301 printf("xdr_u_short: bad op FAILED\n");
311 xdr_char(XDR *xdrs, char *cp)
316 if (!xdr_int(xdrs, &i)) {
326 * PSARC 2003/523 Contract Private Interface
328 * Changes must be reviewed by Solaris File Sharing
329 * Changes must be communicated to contract-2003-523@sun.com
332 xdr_bool(XDR *xdrs, bool_t *bp)
336 switch (xdrs->x_op) {
339 i32b = *bp ? XDR_TRUE : XDR_FALSE;
340 return (XDR_PUTINT32(xdrs, &i32b));
343 if (!XDR_GETINT32(xdrs, &i32b)) {
345 printf("xdr_bool: decode FAILED\n");
349 *bp = (i32b == XDR_FALSE) ? FALSE : TRUE;
356 printf("xdr_bool: bad op FAILED\n");
364 * PSARC 2003/523 Contract Private Interface
366 * Changes must be reviewed by Solaris File Sharing
367 * Changes must be communicated to contract-2003-523@sun.com
370 enum sizecheck { SIZEVAL } sizecheckvar; /* used to find the size of */
374 xdr_enum(XDR *xdrs, enum_t *ep)
378 * enums are treated as ints
380 if (sizeof (sizecheckvar) == sizeof (int32_t)) {
381 return (xdr_int(xdrs, (int32_t *)ep));
382 } else if (sizeof (sizecheckvar) == sizeof (short)) {
383 return (xdr_short(xdrs, (short *)ep));
388 (void) (xdr_short(xdrs, (short *)ep));
389 return (xdr_int(xdrs, (int32_t *)ep));
395 * Allows the specification of a fixed size sequence of opaque bytes.
396 * cp points to the opaque object and cnt gives the byte length.
398 * PSARC 2003/523 Contract Private Interface
400 * Changes must be reviewed by Solaris File Sharing
401 * Changes must be communicated to contract-2003-523@sun.com
404 xdr_opaque(XDR *xdrs, caddr_t cp, const uint_t cnt)
407 static char crud[BYTES_PER_XDR_UNIT];
410 * if no data we are done
416 * round byte count to full xdr units
418 rndup = cnt % BYTES_PER_XDR_UNIT;
420 rndup = BYTES_PER_XDR_UNIT - rndup;
422 if (xdrs->x_op == XDR_DECODE) {
423 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
425 printf("xdr_opaque: decode FAILED\n");
431 return (XDR_GETBYTES(xdrs, (caddr_t)crud, rndup));
434 if (xdrs->x_op == XDR_ENCODE) {
435 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
437 printf("xdr_opaque: encode FAILED\n");
443 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
446 if (xdrs->x_op == XDR_FREE)
450 printf("xdr_opaque: bad op FAILED\n");
457 * *cpp is a pointer to the bytes, *sizep is the count.
458 * If *cpp is NULL maxsize bytes are allocated
460 * PSARC 2003/523 Contract Private Interface
462 * Changes must be reviewed by Solaris File Sharing
463 * Changes must be communicated to contract-2003-523@sun.com
466 xdr_bytes(XDR *xdrs, char **cpp, uint_t *sizep, const uint_t maxsize)
468 char *sp = *cpp; /* sp is the actual string pointer */
472 * first deal with the length since xdr bytes are counted
474 if (!xdr_u_int(xdrs, sizep)) {
476 printf("xdr_bytes: size FAILED\n");
481 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
483 printf("xdr_bytes: bad size (%d) FAILED (%d max)\n",
490 * now deal with the actual bytes
492 switch (xdrs->x_op) {
497 *cpp = sp = (char *)mem_alloc(nodesize);
501 return (xdr_opaque(xdrs, sp, nodesize));
505 mem_free(sp, nodesize);
511 printf("xdr_bytes: bad op FAILED\n");
517 * Implemented here due to commonality of the object.
520 xdr_netobj(XDR *xdrs, struct netobj *np)
522 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
526 * XDR a descriminated union
527 * Support routine for discriminated unions.
528 * You create an array of xdrdiscrim structures, terminated with
529 * an entry with a null procedure pointer. The routine gets
530 * the discriminant value and then searches the array of xdrdiscrims
531 * looking for that value. It calls the procedure given in the xdrdiscrim
532 * to handle the discriminant. If there is no specific routine a default
533 * routine may be called.
534 * If there is no specific or default routine an error is returned.
537 xdr_union(XDR *xdrs, enum_t *dscmp, char *unp,
538 const struct xdr_discrim *choices, const xdrproc_t dfault)
543 * we deal with the discriminator; it's an enum
545 if (!xdr_enum(xdrs, dscmp)) {
547 printf("xdr_enum: dscmp FAILED\n");
554 * search choices for a value that matches the discriminator.
555 * if we find one, execute the xdr routine for that value.
557 for (; choices->proc != NULL_xdrproc_t; choices++) {
558 if (choices->value == dscm)
559 return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
563 * no match - execute the default xdr routine if there is one
565 return ((dfault == NULL_xdrproc_t) ? FALSE :
566 (*dfault)(xdrs, unp, LASTUNSIGNED));
571 * Non-portable xdr primitives.
572 * Care should be taken when moving these routines to new architectures.
577 * XDR null terminated ASCII strings
578 * xdr_string deals with "C strings" - arrays of bytes that are
579 * terminated by a NULL character. The parameter cpp references a
580 * pointer to storage; If the pointer is null, then the necessary
581 * storage is allocated. The last parameter is the max allowed length
582 * of the string as specified by a protocol.
585 xdr_string(XDR *xdrs, char **cpp, const uint_t maxsize)
587 char *sp = *cpp; /* sp is the actual string pointer */
592 * first deal with the length since xdr strings are counted-strings
594 switch (xdrs->x_op) {
597 return (TRUE); /* already free */
600 size = (sp != NULL) ? (uint_t)strlen(sp) : 0;
605 if (!xdr_u_int(xdrs, &size)) {
607 printf("xdr_string: size FAILED\n");
611 if (size > maxsize) {
613 printf("xdr_string: bad size FAILED\n");
620 * now deal with the actual bytes
622 switch (xdrs->x_op) {
627 sp = (char *)mem_alloc(nodesize);
629 if (!xdr_opaque(xdrs, sp, size)) {
631 * free up memory if allocated here
634 mem_free(sp, nodesize);
638 if (strlen(sp) != size) {
640 mem_free(sp, nodesize);
648 return (xdr_opaque(xdrs, sp, size));
651 mem_free(sp, nodesize);
656 printf("xdr_string: bad op FAILED\n");
662 * Wrapper for xdr_string that can be called directly from
663 * routines like clnt_call
666 xdr_wrapstring(XDR *xdrs, char **cpp)
668 if (xdr_string(xdrs, cpp, LASTUNSIGNED))