2 static char sfs_xdr_c_id[] = "@(#)xdr.c 2.1 97/10/23";
4 /* @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC */
6 * Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
8 * Standard Performance Evaluation Corporation (SPEC)
9 * 6585 Merchant Place, Suite 100
12 * This product contains benchmarks acquired from several sources who
13 * understand and agree with SPEC's goal of creating fair and objective
14 * benchmarks to measure computer performance.
16 * This copyright notice is placed here only to protect SPEC in the
17 * event the source is misused in any manner that is contrary to the
18 * spirit, the goals and the intent of SPEC.
20 * The source code is provided to the user or company under the license
21 * agreement for the SPEC Benchmark Suite for this product.
24 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
25 * unrestricted use provided that this legend is included on all tape
26 * media and as a part of the software program in whole or part. Users
27 * may copy or modify Sun RPC without charge, but are not authorized
28 * to license or distribute it to anyone else except as part of a product or
29 * program developed by the user.
31 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
32 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
35 * Sun RPC is provided with no support and without any obligation on the
36 * part of Sun Microsystems, Inc. to assist in its use, correction,
37 * modification or enhancement.
39 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
40 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
41 * OR ANY PART THEREOF.
43 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
44 * or profits or other special, indirect and consequential damages, even if
45 * Sun has been advised of the possibility of such damages.
47 * Sun Microsystems, Inc.
49 * Mountain View, California 94043
51 #if !defined(lint) && defined(SCCSIDS)
52 static char sccsid[] = "@(#)xdr.c 1.35 87/08/12";
56 * xdr.c, Generic XDR routines implementation.
58 * Copyright (C) 1986, Sun Microsystems, Inc.
60 * These are the "generic" xdr routines used to serialize and de-serialize
61 * most common data items. See xdr.h for more info on the interface to
65 static char sfs_clnt_id[] = "@(#)xdr.c 2.1 97/10/23";
72 #include "rpc/types.h"
76 * constants specific to the xdr "protocol"
78 #define XDR_FALSE ((int32_t) 0)
79 #define XDR_TRUE ((int32_t) 1)
80 #define LASTUNSIGNED ((uint_t) 0-1)
85 static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
88 * Free a data structure using XDR
89 * Not a filter, but a convenient utility nonetheless
122 (void) (xdr_int16_t(xdrs, (int16_t *)ip));
123 return (xdr_int32_t(xdrs, (int32_t *)ip));
125 if (sizeof (int) == sizeof (int32_t)) {
126 return (xdr_int32_t(xdrs, (int32_t *)ip));
128 return (xdr_int16_t(xdrs, (int16_t *)ip));
134 * XDR unsigned integers
143 (void) (xdr_int16_t(xdrs, (int16_t *)up));
144 return (xdr_uint32_t(xdrs, (uint32_t *)up));
146 if (sizeof (uint_t) == sizeof (uint32_t)) {
147 return (xdr_uint32_t(xdrs, (uint32_t *)up));
149 return (xdr_int16_t(xdrs, (int16_t *)up));
156 * same as xdr_u_long - open coded to save a proc call!
164 if (xdrs->x_op == XDR_ENCODE)
165 return (XDR_PUTLONG(xdrs, lp));
167 if (xdrs->x_op == XDR_DECODE)
168 return (XDR_GETLONG(xdrs, lp));
170 if (xdrs->x_op == XDR_FREE)
182 if (xdrs->x_op == XDR_ENCODE)
183 return (XDR_PUTLONG(xdrs, lp));
185 if (xdrs->x_op == XDR_DECODE)
186 return (XDR_GETLONG(xdrs, lp));
188 if (xdrs->x_op == XDR_FREE)
195 * XDR unsigned long integers
196 * same as xdr_long - open coded to save a proc call!
204 if (xdrs->x_op == XDR_DECODE)
205 return (XDR_GETLONG(xdrs, (int32_t *)ulp));
206 if (xdrs->x_op == XDR_ENCODE)
207 return (XDR_PUTLONG(xdrs, (int32_t *)ulp));
208 if (xdrs->x_op == XDR_FREE)
219 if (xdrs->x_op == XDR_DECODE)
220 return (XDR_GETLONG(xdrs, (int32_t *)ulp));
221 if (xdrs->x_op == XDR_ENCODE)
222 return (XDR_PUTLONG(xdrs, (int32_t *)ulp));
223 if (xdrs->x_op == XDR_FREE)
238 switch (xdrs->x_op) {
242 return (XDR_PUTLONG(xdrs, &l));
245 if (!XDR_GETLONG(xdrs, &l)) {
264 switch (xdrs->x_op) {
268 return (XDR_PUTLONG(xdrs, &l));
271 if (!XDR_GETLONG(xdrs, &l)) {
284 * XDR unsigned short integers
293 switch (xdrs->x_op) {
297 return (XDR_PUTLONG(xdrs, (int32_t *)&l));
300 if (!XDR_GETLONG(xdrs, (int32_t *)&l)) {
319 switch (xdrs->x_op) {
323 return (XDR_PUTLONG(xdrs, (int32_t *)&l));
326 if (!XDR_GETLONG(xdrs, (int32_t *)&l)) {
350 if (!xdr_int(xdrs, &i)) {
358 * XDR an unsigned char
368 if (!xdr_u_int(xdrs, &u)) {
385 switch (xdrs->x_op) {
388 lb = *bp ? XDR_TRUE : XDR_FALSE;
389 return (XDR_PUTLONG(xdrs, &lb));
392 if (!XDR_GETLONG(xdrs, &lb)) {
395 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
413 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
416 * enums are treated as ints
418 if (sizeof (enum sizecheck) == sizeof (int32_t)) {
419 return (xdr_int32_t(xdrs, (int32_t *)ep));
420 } else if (sizeof (enum sizecheck) == sizeof (int16_t)) {
421 return (xdr_int16_t(xdrs, (int16_t *)ep));
426 (void) (xdr_int16_t(xdrs, (int16_t *)ep));
427 return (xdr_int32_t(xdrs, (int32_t *)ep));
433 * Allows the specification of a fixed size sequence of opaque bytes.
434 * cp points to the opaque object and cnt gives the byte length.
443 static crud[BYTES_PER_XDR_UNIT];
446 * if no data we are done
452 * round byte count to full xdr units
454 rndup = cnt % BYTES_PER_XDR_UNIT;
456 rndup = BYTES_PER_XDR_UNIT - rndup;
458 if (xdrs->x_op == XDR_DECODE) {
459 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
464 return (XDR_GETBYTES(xdrs, crud, rndup));
467 if (xdrs->x_op == XDR_ENCODE) {
468 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
473 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
476 if (xdrs->x_op == XDR_FREE) {
485 * *cpp is a pointer to the bytes, *sizep is the count.
486 * If *cpp is NULL maxsize bytes are allocated
495 char *sp = *cpp; /* sp is the actual string pointer */
499 * first deal with the length since xdr bytes are counted
501 if (! xdr_u_int(xdrs, sizep)) {
505 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
510 * now deal with the actual bytes
512 switch (xdrs->x_op) {
519 *cpp = sp = (char *)mem_alloc(nodesize);
522 (void) fprintf(stderr, "xdr_bytes: out of memory\n");
528 return (xdr_opaque(xdrs, sp, nodesize));
532 mem_free(sp, nodesize);
541 * Implemented here due to commonality of the object.
549 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
553 * XDR a descriminated union
554 * Support routine for discriminated unions.
555 * You create an array of xdrdiscrim structures, terminated with
556 * an entry with a null procedure pointer. The routine gets
557 * the discriminant value and then searches the array of xdrdiscrims
558 * looking for that value. It calls the procedure given in the xdrdiscrim
559 * to handle the discriminant. If there is no specific routine a default
560 * routine may be called.
561 * If there is no specific or default routine an error is returned.
566 enum_t *dscmp, /* enum to decide which arm to work on */
567 char *unp, /* the union itself */
568 struct xdr_discrim *choices, /* [value, xdr proc] for each arm */
569 xdrproc_t dfault) /* default xdr routine */
574 * we deal with the discriminator; it's an enum
576 if (! xdr_enum(xdrs, dscmp)) {
582 * search choices for a value that matches the discriminator.
583 * if we find one, execute the xdr routine for that value.
585 for (; choices->proc != NULL_xdrproc_t; choices++) {
586 if (choices->value == dscm)
587 return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
591 * no match - execute the default xdr routine if there is one
593 return ((dfault == NULL_xdrproc_t) ? FALSE :
594 (*dfault)(xdrs, unp, LASTUNSIGNED));
599 * Non-portable xdr primitives.
600 * Care should be taken when moving these routines to new architectures.
605 * XDR null terminated ASCII strings
606 * xdr_string deals with "C strings" - arrays of bytes that are
607 * terminated by a NULL character. The parameter cpp references a
608 * pointer to storage; If the pointer is null, then the necessary
609 * storage is allocated. The last parameter is the max allowed length
610 * of the string as specified by a protocol.
618 char *sp = *cpp; /* sp is the actual string pointer */
623 * first deal with the length since xdr strings are counted-strings
625 switch (xdrs->x_op) {
628 return(TRUE); /* already free */
630 /* fall through... */
635 if (! xdr_u_int(xdrs, &size)) {
638 if (size > maxsize) {
644 * now deal with the actual bytes
646 switch (xdrs->x_op) {
653 *cpp = sp = (char *)mem_alloc(nodesize);
655 (void) fprintf(stderr, "xdr_string: out of memory\n");
662 return (xdr_opaque(xdrs, sp, size));
665 mem_free(sp, nodesize);
673 * Wrapper for xdr_string that can be called directly from
674 * routines like clnt_call
681 if (xdr_string(xdrs, cpp, LASTUNSIGNED)) {