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>
40 * xdr.c, Generic XDR routines implementation.
42 * Copyright (C) 1984, Sun Microsystems, Inc.
44 * These are the "generic" xdr routines used to serialize and de-serialize
45 * most common data items. See xdr.h for more info on the interface to
52 #include <sys/param.h>
53 #ifndef AFS_LINUX20_ENV
54 #include <sys/systm.h>
62 * constants specific to the xdr "protocol"
64 #define XDR_FALSE ((afs_int32) 0)
65 #define XDR_TRUE ((afs_int32) 1)
66 #define LASTUNSIGNED ((u_int) 0-1)
81 #if !defined(AFS_OSF20_ENV) && !defined(AFS_SGI61_ENV)
83 * XDR afs_int32 integers
84 * same as xdr_u_long - open coded to save a proc call!
86 bool_t xdr_int(register XDR *xdrs, int *ip)
89 if (xdrs->x_op == XDR_ENCODE)
90 return (XDR_PUTINT32(xdrs, (long *)ip));
92 if (xdrs->x_op == XDR_DECODE)
93 return (XDR_GETINT32(xdrs, (long *)ip));
95 if (xdrs->x_op == XDR_FREE)
102 * XDR unsigned afs_int32 integers
103 * same as xdr_long - open coded to save a proc call!
105 bool_t xdr_u_int(register XDR *xdrs, u_int *up)
108 if (xdrs->x_op == XDR_DECODE)
109 return (XDR_GETINT32(xdrs, (long *)up));
111 if (xdrs->x_op == XDR_ENCODE)
112 return (XDR_PUTINT32(xdrs, (long *)up));
114 if (xdrs->x_op == XDR_FREE)
122 * XDR afs_int32 integers
123 * same as xdr_u_long - open coded to save a proc call!
125 bool_t xdr_int(register XDR *xdrs, int *lp)
128 if (xdrs->x_op == XDR_ENCODE)
129 return (XDR_PUTINT32(xdrs, (long *)lp));
131 if (xdrs->x_op == XDR_DECODE)
132 return (XDR_GETINT32(xdrs, (long *)lp));
134 if (xdrs->x_op == XDR_FREE)
141 * XDR unsigned afs_int32 integers
142 * same as xdr_long - open coded to save a proc call!
144 bool_t xdr_u_int(register XDR *xdrs, u_int *ulp)
147 if (xdrs->x_op == XDR_DECODE)
148 return (XDR_GETINT32(xdrs, (long *)ulp));
150 if (xdrs->x_op == XDR_ENCODE)
151 return (XDR_PUTINT32(xdrs, (long *)ulp));
153 if (xdrs->x_op == XDR_FREE)
161 * XDR afs_int32 integers
162 * same as xdr_u_long - open coded to save a proc call!
164 bool_t xdr_long(register XDR *xdrs, long *lp)
167 if (xdrs->x_op == XDR_ENCODE)
168 return (XDR_PUTINT32(xdrs, lp));
170 if (xdrs->x_op == XDR_DECODE)
171 return (XDR_GETINT32(xdrs, lp));
173 if (xdrs->x_op == XDR_FREE)
180 * XDR unsigned afs_int32 integers
181 * same as xdr_long - open coded to save a proc call!
183 bool_t xdr_u_long(register XDR *xdrs, u_long *ulp)
186 if (xdrs->x_op == XDR_DECODE)
187 return (XDR_GETINT32(xdrs, ulp));
188 if (xdrs->x_op == XDR_ENCODE)
189 return (XDR_PUTINT32(xdrs, ulp));
190 if (xdrs->x_op == XDR_FREE)
198 bool_t xdr_char(register XDR *xdrs, char *sp)
202 switch (xdrs->x_op) {
206 return (XDR_PUTINT32(xdrs, &l));
209 if (!XDR_GETINT32(xdrs, &l)) {
224 bool_t xdr_u_char(register XDR *xdrs, u_char *usp)
228 switch (xdrs->x_op) {
231 l = (afs_uint32) *usp;
232 return (XDR_PUTINT32(xdrs, &l));
235 if (!XDR_GETINT32(xdrs, &l)) {
251 bool_t xdr_short(register XDR *xdrs, short *sp)
255 switch (xdrs->x_op) {
259 return (XDR_PUTINT32(xdrs, &l));
262 if (!XDR_GETINT32(xdrs, &l)) {
275 * XDR unsigned short integers
277 bool_t xdr_u_short(register XDR *xdrs, u_short *usp)
281 switch (xdrs->x_op) {
284 l = (afs_uint32) *usp;
285 return (XDR_PUTINT32(xdrs, &l));
288 if (!XDR_GETINT32(xdrs, &l)) {
304 bool_t xdr_bool(register XDR *xdrs, bool_t *bp)
308 switch (xdrs->x_op) {
311 lb = *bp ? XDR_TRUE : XDR_FALSE;
312 return (XDR_PUTINT32(xdrs, &lb));
315 if (!XDR_GETINT32(xdrs, &lb)) {
318 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
330 bool_t xdr_enum(register XDR *xdrs, enum_t *ep)
332 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
335 * enums are treated as ints
338 return (xdr_long(xdrs, (long *)ep));
344 * Allows the specification of a fixed size sequence of opaque bytes.
345 * cp points to the opaque object and cnt gives the byte length.
347 bool_t xdr_opaque(register XDR *xdrs, caddr_t cp, register u_int cnt)
349 register u_int rndup;
350 int crud[BYTES_PER_XDR_UNIT];
351 char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
354 * if no data we are done
360 * round byte count to full xdr units
362 rndup = cnt % BYTES_PER_XDR_UNIT;
364 rndup = BYTES_PER_XDR_UNIT - rndup;
366 if (xdrs->x_op == XDR_DECODE) {
367 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
372 return (XDR_GETBYTES(xdrs, crud, rndup));
375 if (xdrs->x_op == XDR_ENCODE) {
376 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
381 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
384 if (xdrs->x_op == XDR_FREE) {
393 * *cpp is a pointer to the bytes, *sizep is the count.
394 * If *cpp is NULL maxsize bytes are allocated
396 bool_t xdr_bytes(register XDR *xdrs, char **cpp, register u_int *sizep, u_int maxsize)
398 register char *sp = *cpp; /* sp is the actual string pointer */
399 register u_int nodesize;
402 * first deal with the length since xdr bytes are counted
404 if (! xdr_u_int(xdrs, sizep)) {
408 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
413 * now deal with the actual bytes
415 switch (xdrs->x_op) {
419 *cpp = sp = (char *)osi_alloc(nodesize);
427 return (xdr_opaque(xdrs, sp, nodesize));
431 osi_free(sp, nodesize);
440 * XDR a descriminated union
441 * Support routine for discriminated unions.
442 * You create an array of xdrdiscrim structures, terminated with
443 * an entry with a null procedure pointer. The routine gets
444 * the discriminant value and then searches the array of xdrdiscrims
445 * looking for that value. It calls the procedure given in the xdrdiscrim
446 * to handle the discriminant. If there is no specific routine a default
447 * routine may be called.
448 * If there is no specific or default routine an error is returned.
451 enum_t *dscmp; * enum to decide which arm to work on *
452 caddr_t unp; * the union itself *
453 struct xdr_discrim *choices; * [value, xdr proc] for each arm *
454 xdrproc_t dfault; * default xdr routine *
456 bool_t xdr_union(register XDR *xdrs, enum_t *dscmp, caddr_t unp,
457 struct xdr_discrim *choices, xdrproc_t dfault)
459 register enum_t dscm;
462 * we deal with the discriminator; it's an enum
464 if (! xdr_enum(xdrs, dscmp)) {
470 * search choices for a value that matches the discriminator.
471 * if we find one, execute the xdr routine for that value.
473 for (; choices->proc != NULL_xdrproc_t; choices++) {
474 if (choices->value == dscm)
475 return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
479 * no match - execute the default xdr routine if there is one
481 return ((dfault == NULL_xdrproc_t) ? FALSE :
482 (*dfault)(xdrs, unp, LASTUNSIGNED));
487 * Non-portable xdr primitives.
488 * Care should be taken when moving these routines to new architectures.
493 * XDR null terminated ASCII strings
494 * xdr_string deals with "C strings" - arrays of bytes that are
495 * terminated by a NULL character. The parameter cpp references a
496 * pointer to storage; If the pointer is null, then the necessary
497 * storage is allocated. The last parameter is the max allowed length
498 * of the string as specified by a protocol.
500 bool_t xdr_string(register XDR *xdrs, char **cpp, u_int maxsize)
502 register char *sp = *cpp; /* sp is the actual string pointer */
506 if (maxsize > ((~0) >> 1) - 1) maxsize = ((~0) >> 1) - 1;
509 * first deal with the length since xdr strings are counted-strings
511 switch (xdrs->x_op) {
514 return (TRUE); /* already free */
522 if (! xdr_u_int(xdrs, &size)) {
525 if (size > maxsize) {
531 * now deal with the actual bytes
533 switch (xdrs->x_op) {
537 *cpp = sp = (char *)osi_alloc(nodesize);
545 return (xdr_opaque(xdrs, sp, size));
549 osi_free(sp, nodesize);
558 * Wrapper for xdr_string that can be called directly from
559 * routines like clnt_call
562 bool_t xdr_wrapstring(register XDR *xdrs, char **cpp)
564 if (xdr_string(xdrs, cpp, BUFSIZ)) {