2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
30 #include <afsconfig.h>
32 #include "afs/param.h"
34 #include <afs/param.h>
41 * xdr.c, Generic XDR routines implementation.
43 * Copyright (C) 1984, Sun Microsystems, Inc.
45 * These are the "generic" xdr routines used to serialize and de-serialize
46 * most common data items. See xdr.h for more info on the interface to
53 #include <sys/param.h>
54 #ifndef AFS_LINUX20_ENV
55 #include <sys/systm.h>
63 * constants specific to the xdr "protocol"
65 #define XDR_FALSE ((afs_int32) 0)
66 #define XDR_TRUE ((afs_int32) 1)
67 #define LASTUNSIGNED ((u_int) 0-1)
83 #if !defined(AFS_OSF20_ENV) && !defined(AFS_SGI61_ENV)
85 * XDR afs_int32 integers
86 * same as xdr_u_long - open coded to save a proc call!
89 xdr_int(register XDR * xdrs, int *ip)
92 if (xdrs->x_op == XDR_ENCODE)
93 return (XDR_PUTINT32(xdrs, (long *)ip));
95 if (xdrs->x_op == XDR_DECODE)
96 return (XDR_GETINT32(xdrs, (long *)ip));
98 if (xdrs->x_op == XDR_FREE)
105 * XDR unsigned afs_int32 integers
106 * same as xdr_long - open coded to save a proc call!
109 xdr_u_int(register XDR * xdrs, u_int * up)
112 if (xdrs->x_op == XDR_DECODE)
113 return (XDR_GETINT32(xdrs, (long *)up));
115 if (xdrs->x_op == XDR_ENCODE)
116 return (XDR_PUTINT32(xdrs, (long *)up));
118 if (xdrs->x_op == XDR_FREE)
126 * XDR afs_int32 integers
127 * same as xdr_u_long - open coded to save a proc call!
130 xdr_int(register XDR * xdrs, int *lp)
133 if (xdrs->x_op == XDR_ENCODE)
134 return (XDR_PUTINT32(xdrs, (long *)lp));
136 if (xdrs->x_op == XDR_DECODE)
137 return (XDR_GETINT32(xdrs, (long *)lp));
139 if (xdrs->x_op == XDR_FREE)
146 * XDR unsigned afs_int32 integers
147 * same as xdr_long - open coded to save a proc call!
150 xdr_u_int(register XDR * xdrs, u_int * ulp)
153 if (xdrs->x_op == XDR_DECODE)
154 return (XDR_GETINT32(xdrs, (long *)ulp));
156 if (xdrs->x_op == XDR_ENCODE)
157 return (XDR_PUTINT32(xdrs, (long *)ulp));
159 if (xdrs->x_op == XDR_FREE)
167 * XDR afs_int32 integers
168 * same as xdr_u_long - open coded to save a proc call!
171 xdr_long(register XDR * xdrs, long *lp)
174 if (xdrs->x_op == XDR_ENCODE)
175 return (XDR_PUTINT32(xdrs, lp));
177 if (xdrs->x_op == XDR_DECODE)
178 return (XDR_GETINT32(xdrs, lp));
180 if (xdrs->x_op == XDR_FREE)
187 * XDR unsigned afs_int32 integers
188 * same as xdr_long - open coded to save a proc call!
191 xdr_u_long(register XDR * xdrs, u_long * ulp)
194 if (xdrs->x_op == XDR_DECODE)
195 return (XDR_GETINT32(xdrs, ulp));
196 if (xdrs->x_op == XDR_ENCODE)
197 return (XDR_PUTINT32(xdrs, ulp));
198 if (xdrs->x_op == XDR_FREE)
207 xdr_char(register XDR * xdrs, char *sp)
211 switch (xdrs->x_op) {
214 l = (afs_int32) * sp;
215 return (XDR_PUTINT32(xdrs, &l));
218 if (!XDR_GETINT32(xdrs, &l)) {
234 xdr_u_char(register XDR * xdrs, u_char * usp)
238 switch (xdrs->x_op) {
241 l = (afs_uint32) * usp;
242 return (XDR_PUTINT32(xdrs, &l));
245 if (!XDR_GETINT32(xdrs, &l)) {
262 xdr_short(register XDR * xdrs, short *sp)
266 switch (xdrs->x_op) {
269 l = (afs_int32) * sp;
270 return (XDR_PUTINT32(xdrs, &l));
273 if (!XDR_GETINT32(xdrs, &l)) {
286 * XDR unsigned short integers
289 xdr_u_short(register XDR * xdrs, u_short * usp)
293 switch (xdrs->x_op) {
296 l = (afs_uint32) * usp;
297 return (XDR_PUTINT32(xdrs, &l));
300 if (!XDR_GETINT32(xdrs, &l)) {
317 xdr_bool(register XDR * xdrs, bool_t * bp)
321 switch (xdrs->x_op) {
324 lb = *bp ? XDR_TRUE : XDR_FALSE;
325 return (XDR_PUTINT32(xdrs, &lb));
328 if (!XDR_GETINT32(xdrs, &lb)) {
331 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
344 xdr_enum(register XDR * xdrs, enum_t * ep)
346 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
349 * enums are treated as ints
352 return (xdr_long(xdrs, (long *)ep));
358 * Allows the specification of a fixed size sequence of opaque bytes.
359 * cp points to the opaque object and cnt gives the byte length.
362 xdr_opaque(register XDR * xdrs, caddr_t cp, register u_int cnt)
364 register u_int rndup;
365 int crud[BYTES_PER_XDR_UNIT];
366 char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
369 * if no data we are done
375 * round byte count to full xdr units
377 rndup = cnt % BYTES_PER_XDR_UNIT;
379 rndup = BYTES_PER_XDR_UNIT - rndup;
381 if (xdrs->x_op == XDR_DECODE) {
382 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
387 return (XDR_GETBYTES(xdrs, crud, rndup));
390 if (xdrs->x_op == XDR_ENCODE) {
391 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
396 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
399 if (xdrs->x_op == XDR_FREE) {
408 * *cpp is a pointer to the bytes, *sizep is the count.
409 * If *cpp is NULL maxsize bytes are allocated
412 xdr_bytes(register XDR * xdrs, char **cpp, register u_int * sizep,
415 register char *sp = *cpp; /* sp is the actual string pointer */
416 register u_int nodesize;
419 * first deal with the length since xdr bytes are counted
421 if (!xdr_u_int(xdrs, sizep)) {
425 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
430 * now deal with the actual bytes
432 switch (xdrs->x_op) {
436 *cpp = sp = (char *)osi_alloc(nodesize);
444 return (xdr_opaque(xdrs, sp, nodesize));
448 osi_free(sp, nodesize);
457 * XDR a descriminated union
458 * Support routine for discriminated unions.
459 * You create an array of xdrdiscrim structures, terminated with
460 * an entry with a null procedure pointer. The routine gets
461 * the discriminant value and then searches the array of xdrdiscrims
462 * looking for that value. It calls the procedure given in the xdrdiscrim
463 * to handle the discriminant. If there is no specific routine a default
464 * routine may be called.
465 * If there is no specific or default routine an error is returned.
468 enum_t *dscmp; * enum to decide which arm to work on *
469 caddr_t unp; * the union itself *
470 struct xdr_discrim *choices; * [value, xdr proc] for each arm *
471 xdrproc_t dfault; * default xdr routine *
474 xdr_union(register XDR * xdrs, enum_t * dscmp, caddr_t unp,
475 struct xdr_discrim * choices, xdrproc_t dfault)
477 register enum_t dscm;
480 * we deal with the discriminator; it's an enum
482 if (!xdr_enum(xdrs, dscmp)) {
488 * search choices for a value that matches the discriminator.
489 * if we find one, execute the xdr routine for that value.
491 for (; choices->proc != NULL_xdrproc_t; choices++) {
492 if (choices->value == dscm)
493 return ((*(choices->proc)) (xdrs, unp, LASTUNSIGNED));
497 * no match - execute the default xdr routine if there is one
499 return ((dfault == NULL_xdrproc_t) ? FALSE : (*dfault) (xdrs, unp,
505 * Non-portable xdr primitives.
506 * Care should be taken when moving these routines to new architectures.
511 * XDR null terminated ASCII strings
512 * xdr_string deals with "C strings" - arrays of bytes that are
513 * terminated by a NULL character. The parameter cpp references a
514 * pointer to storage; If the pointer is null, then the necessary
515 * storage is allocated. The last parameter is the max allowed length
516 * of the string as specified by a protocol.
519 xdr_string(register XDR * xdrs, char **cpp, u_int maxsize)
521 register char *sp = *cpp; /* sp is the actual string pointer */
525 if (maxsize > ((~0) >> 1) - 1)
526 maxsize = ((~0) >> 1) - 1;
529 * first deal with the length since xdr strings are counted-strings
531 switch (xdrs->x_op) {
534 return (TRUE); /* already free */
542 if (!xdr_u_int(xdrs, &size)) {
545 if (size > maxsize) {
551 * now deal with the actual bytes
553 switch (xdrs->x_op) {
557 *cpp = sp = (char *)osi_alloc(nodesize);
565 return (xdr_opaque(xdrs, sp, size));
569 osi_free(sp, nodesize);
578 * Wrapper for xdr_string that can be called directly from
579 * routines like clnt_call
583 xdr_wrapstring(register XDR * xdrs, char **cpp)
585 if (xdr_string(xdrs, cpp, BUFSIZ)) {