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
58 #if !defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
59 #error "Exactly one of _BIG_ENDIAN or _LITTLE_ENDIAN must be defined"
60 #elif defined(_BIG_ENDIAN) && defined(_LITTLE_ENDIAN)
61 #error "Only one of _BIG_ENDIAN or _LITTLE_ENDIAN may be defined"
66 * constants specific to the xdr "protocol"
68 #define XDR_FALSE ((int32_t)0)
69 #define XDR_TRUE ((int32_t)1)
70 #define LASTUNSIGNED ((uint_t)0-1)
75 static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
78 * Free a data structure using XDR
79 * Not a filter, but a convenient utility nonetheless
82 xdr_free(xdrproc_t proc, char *objp)
102 * PSARC 2003/523 Contract Private Interface
104 * Changes must be reviewed by Solaris File Sharing
105 * Changes must be communicated to contract-2003-523@sun.com
108 xdr_int(XDR *xdrs, int *ip)
110 if (xdrs->x_op == XDR_ENCODE)
111 return (XDR_PUTINT32(xdrs, ip));
113 if (xdrs->x_op == XDR_DECODE)
114 return (XDR_GETINT32(xdrs, ip));
116 if (xdrs->x_op == XDR_FREE)
120 printf("xdr_int: FAILED\n");
126 * XDR unsigned integers
128 * PSARC 2003/523 Contract Private Interface
130 * Changes must be reviewed by Solaris File Sharing
131 * Changes must be communicated to contract-2003-523@sun.com
134 xdr_u_int(XDR *xdrs, uint_t *up)
136 if (xdrs->x_op == XDR_ENCODE)
137 return (XDR_PUTINT32(xdrs, (int32_t *)up));
139 if (xdrs->x_op == XDR_DECODE)
140 return (XDR_GETINT32(xdrs, (int32_t *)up));
142 if (xdrs->x_op == XDR_FREE)
146 printf("xdr_int: FAILED\n");
154 * xdr_long and xdr_u_long for binary compatability on ILP32 kernels.
156 * No prototypes since new code should not be using these interfaces.
159 xdr_long(XDR *xdrs, long *ip)
161 return (xdr_int(xdrs, (int *)ip));
165 xdr_u_long(XDR *xdrs, unsigned long *up)
167 return (xdr_u_int(xdrs, (uint_t *)up));
173 * XDR long long integers
176 xdr_longlong_t(XDR *xdrs, longlong_t *hp)
178 if (xdrs->x_op == XDR_ENCODE) {
179 #if BYTE_ORDER == _LITTLE_ENDIAN
180 if (XDR_PUTINT32(xdrs, (int32_t *)((char *)hp +
181 BYTES_PER_XDR_UNIT)) == TRUE) {
182 return (XDR_PUTINT32(xdrs, (int32_t *)hp));
185 if (XDR_PUTINT32(xdrs, (int32_t *)hp) == TRUE) {
186 return (XDR_PUTINT32(xdrs, (int32_t *)((char *)hp +
187 BYTES_PER_XDR_UNIT)));
193 if (xdrs->x_op == XDR_DECODE) {
194 #if BYTE_ORDER == _LITTLE_ENDIAN
195 if (XDR_GETINT32(xdrs, (int32_t *)((char *)hp +
196 BYTES_PER_XDR_UNIT)) == TRUE) {
197 return (XDR_GETINT32(xdrs, (int32_t *)hp));
200 if (XDR_GETINT32(xdrs, (int32_t *)hp) == TRUE) {
201 return (XDR_GETINT32(xdrs, (int32_t *)((char *)hp +
202 BYTES_PER_XDR_UNIT)));
211 * XDR unsigned long long integers
214 xdr_u_longlong_t(XDR *xdrs, u_longlong_t *hp)
217 if (xdrs->x_op == XDR_ENCODE) {
218 #if BYTE_ORDER == _LITTLE_ENDIAN
219 if (XDR_PUTINT32(xdrs, (int32_t *)((char *)hp +
220 BYTES_PER_XDR_UNIT)) == TRUE) {
221 return (XDR_PUTINT32(xdrs, (int32_t *)hp));
224 if (XDR_PUTINT32(xdrs, (int32_t *)hp) == TRUE) {
225 return (XDR_PUTINT32(xdrs, (int32_t *)((char *)hp +
226 BYTES_PER_XDR_UNIT)));
232 if (xdrs->x_op == XDR_DECODE) {
233 #if BYTE_ORDER == _LITTLE_ENDIAN
234 if (XDR_GETINT32(xdrs, (int32_t *)((char *)hp +
235 BYTES_PER_XDR_UNIT)) == TRUE) {
236 return (XDR_GETINT32(xdrs, (int32_t *)hp));
239 if (XDR_GETINT32(xdrs, (int32_t *)hp) == TRUE) {
240 return (XDR_GETINT32(xdrs, (int32_t *)((char *)hp +
241 BYTES_PER_XDR_UNIT)));
253 xdr_short(XDR *xdrs, short *sp)
257 switch (xdrs->x_op) {
261 return (XDR_PUTINT32(xdrs, &l));
264 if (!XDR_GETINT32(xdrs, &l))
276 * XDR unsigned short integers
279 xdr_u_short(XDR *xdrs, ushort_t *usp)
283 switch (xdrs->x_op) {
287 return (XDR_PUTINT32(xdrs, (int32_t *)&l));
290 if (!XDR_GETINT32(xdrs, (int32_t *)&l)) {
292 printf("xdr_u_short: decode FAILED\n");
303 printf("xdr_u_short: bad op FAILED\n");
313 xdr_char(XDR *xdrs, char *cp)
318 if (!xdr_int(xdrs, &i)) {
328 * PSARC 2003/523 Contract Private Interface
330 * Changes must be reviewed by Solaris File Sharing
331 * Changes must be communicated to contract-2003-523@sun.com
334 xdr_bool(XDR *xdrs, bool_t *bp)
338 switch (xdrs->x_op) {
341 i32b = *bp ? XDR_TRUE : XDR_FALSE;
342 return (XDR_PUTINT32(xdrs, &i32b));
345 if (!XDR_GETINT32(xdrs, &i32b)) {
347 printf("xdr_bool: decode FAILED\n");
351 *bp = (i32b == XDR_FALSE) ? FALSE : TRUE;
358 printf("xdr_bool: bad op FAILED\n");
366 * PSARC 2003/523 Contract Private Interface
368 * Changes must be reviewed by Solaris File Sharing
369 * Changes must be communicated to contract-2003-523@sun.com
372 enum sizecheck { SIZEVAL } sizecheckvar; /* used to find the size of */
376 xdr_enum(XDR *xdrs, enum_t *ep)
380 * enums are treated as ints
382 if (sizeof (sizecheckvar) == sizeof (int32_t)) {
383 return (xdr_int(xdrs, (int32_t *)ep));
384 } else if (sizeof (sizecheckvar) == sizeof (short)) {
385 return (xdr_short(xdrs, (short *)ep));
390 (void) (xdr_short(xdrs, (short *)ep));
391 return (xdr_int(xdrs, (int32_t *)ep));
397 * Allows the specification of a fixed size sequence of opaque bytes.
398 * cp points to the opaque object and cnt gives the byte length.
400 * PSARC 2003/523 Contract Private Interface
402 * Changes must be reviewed by Solaris File Sharing
403 * Changes must be communicated to contract-2003-523@sun.com
406 xdr_opaque(XDR *xdrs, caddr_t cp, const uint_t cnt)
409 static char crud[BYTES_PER_XDR_UNIT];
412 * if no data we are done
418 * round byte count to full xdr units
420 rndup = cnt % BYTES_PER_XDR_UNIT;
422 rndup = BYTES_PER_XDR_UNIT - rndup;
424 if (xdrs->x_op == XDR_DECODE) {
425 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
427 printf("xdr_opaque: decode FAILED\n");
433 return (XDR_GETBYTES(xdrs, (caddr_t)crud, rndup));
436 if (xdrs->x_op == XDR_ENCODE) {
437 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
439 printf("xdr_opaque: encode FAILED\n");
445 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
448 if (xdrs->x_op == XDR_FREE)
452 printf("xdr_opaque: bad op FAILED\n");
459 * *cpp is a pointer to the bytes, *sizep is the count.
460 * If *cpp is NULL maxsize bytes are allocated
462 * PSARC 2003/523 Contract Private Interface
464 * Changes must be reviewed by Solaris File Sharing
465 * Changes must be communicated to contract-2003-523@sun.com
468 xdr_bytes(XDR *xdrs, char **cpp, uint_t *sizep, const uint_t maxsize)
470 char *sp = *cpp; /* sp is the actual string pointer */
474 * first deal with the length since xdr bytes are counted
476 if (!xdr_u_int(xdrs, sizep)) {
478 printf("xdr_bytes: size FAILED\n");
483 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
485 printf("xdr_bytes: bad size (%d) FAILED (%d max)\n",
492 * now deal with the actual bytes
494 switch (xdrs->x_op) {
499 *cpp = sp = (char *)mem_alloc(nodesize);
503 return (xdr_opaque(xdrs, sp, nodesize));
507 mem_free(sp, nodesize);
513 printf("xdr_bytes: bad op FAILED\n");
519 * Implemented here due to commonality of the object.
522 xdr_netobj(XDR *xdrs, struct netobj *np)
524 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
528 * XDR a descriminated union
529 * Support routine for discriminated unions.
530 * You create an array of xdrdiscrim structures, terminated with
531 * an entry with a null procedure pointer. The routine gets
532 * the discriminant value and then searches the array of xdrdiscrims
533 * looking for that value. It calls the procedure given in the xdrdiscrim
534 * to handle the discriminant. If there is no specific routine a default
535 * routine may be called.
536 * If there is no specific or default routine an error is returned.
539 xdr_union(XDR *xdrs, enum_t *dscmp, char *unp,
540 const struct xdr_discrim *choices, const xdrproc_t dfault)
545 * we deal with the discriminator; it's an enum
547 if (!xdr_enum(xdrs, dscmp)) {
549 printf("xdr_enum: dscmp FAILED\n");
556 * search choices for a value that matches the discriminator.
557 * if we find one, execute the xdr routine for that value.
559 for (; choices->proc != NULL_xdrproc_t; choices++) {
560 if (choices->value == dscm)
561 return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
565 * no match - execute the default xdr routine if there is one
567 return ((dfault == NULL_xdrproc_t) ? FALSE :
568 (*dfault)(xdrs, unp, LASTUNSIGNED));
573 * Non-portable xdr primitives.
574 * Care should be taken when moving these routines to new architectures.
579 * XDR null terminated ASCII strings
580 * xdr_string deals with "C strings" - arrays of bytes that are
581 * terminated by a NULL character. The parameter cpp references a
582 * pointer to storage; If the pointer is null, then the necessary
583 * storage is allocated. The last parameter is the max allowed length
584 * of the string as specified by a protocol.
587 xdr_string(XDR *xdrs, char **cpp, const uint_t maxsize)
589 char *sp = *cpp; /* sp is the actual string pointer */
594 * first deal with the length since xdr strings are counted-strings
596 switch (xdrs->x_op) {
599 return (TRUE); /* already free */
602 size = (sp != NULL) ? (uint_t)strlen(sp) : 0;
607 if (!xdr_u_int(xdrs, &size)) {
609 printf("xdr_string: size FAILED\n");
613 if (size > maxsize) {
615 printf("xdr_string: bad size FAILED\n");
622 * now deal with the actual bytes
624 switch (xdrs->x_op) {
629 sp = (char *)mem_alloc(nodesize);
631 if (!xdr_opaque(xdrs, sp, size)) {
633 * free up memory if allocated here
636 mem_free(sp, nodesize);
640 if (strlen(sp) != size) {
642 mem_free(sp, nodesize);
650 return (xdr_opaque(xdrs, sp, size));
653 mem_free(sp, nodesize);
658 printf("xdr_string: bad op FAILED\n");
664 * Wrapper for xdr_string that can be called directly from
665 * routines like clnt_call
668 xdr_wrapstring(XDR *xdrs, char **cpp)
670 if (xdr_string(xdrs, cpp, LASTUNSIGNED))