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 extern char *osi_alloc();
66 * constants specific to the xdr "protocol"
68 #define XDR_FALSE ((afs_int32) 0)
69 #define XDR_TRUE ((afs_int32) 1)
70 #define LASTUNSIGNED ((u_int) 0-1)
81 xdr_void(/* xdrs, addr */)
89 #if !defined(AFS_OSF20_ENV) && !defined(AFS_SGI61_ENV)
99 if (xdrs->x_op == XDR_ENCODE)
100 return (XDR_PUTINT32(xdrs, (long *)ip));
102 if (xdrs->x_op == XDR_DECODE)
103 return (XDR_GETINT32(xdrs, (long *)ip));
105 if (xdrs->x_op == XDR_FREE)
112 * XDR unsigned integers
120 if (xdrs->x_op == XDR_DECODE)
121 return (XDR_GETINT32(xdrs, (long *)up));
123 if (xdrs->x_op == XDR_ENCODE)
124 return (XDR_PUTINT32(xdrs, (long *)up));
126 if (xdrs->x_op == XDR_FREE)
134 * XDR afs_int32 integers
135 * same as xdr_u_long - open coded to save a proc call!
143 if (xdrs->x_op == XDR_ENCODE)
144 return (XDR_PUTINT32(xdrs, (long *)lp));
146 if (xdrs->x_op == XDR_DECODE)
147 return (XDR_GETINT32(xdrs, (long *)lp));
149 if (xdrs->x_op == XDR_FREE)
156 * XDR unsigned afs_int32 integers
157 * same as xdr_long - open coded to save a proc call!
165 if (xdrs->x_op == XDR_DECODE)
166 return (XDR_GETINT32(xdrs, (long *)ulp));
167 if (xdrs->x_op == XDR_ENCODE)
168 return (XDR_PUTINT32(xdrs, (long *)ulp));
169 if (xdrs->x_op == XDR_FREE)
177 * XDR afs_int32 integers
178 * same as xdr_u_long - open coded to save a proc call!
186 if (xdrs->x_op == XDR_ENCODE)
187 return (XDR_PUTINT32(xdrs, lp));
189 if (xdrs->x_op == XDR_DECODE)
190 return (XDR_GETINT32(xdrs, lp));
192 if (xdrs->x_op == XDR_FREE)
199 * XDR unsigned afs_int32 integers
200 * same as xdr_long - open coded to save a proc call!
203 xdr_u_long(xdrs, ulp)
208 if (xdrs->x_op == XDR_DECODE)
209 return (XDR_GETINT32(xdrs, (long *)ulp));
211 if (xdrs->x_op == XDR_ENCODE)
212 return (XDR_PUTINT32(xdrs, (long *)ulp));
214 if (xdrs->x_op == XDR_FREE)
230 switch (xdrs->x_op) {
234 return (XDR_PUTINT32(xdrs, &l));
237 if (!XDR_GETINT32(xdrs, &l)) {
253 xdr_u_char(xdrs, usp)
259 switch (xdrs->x_op) {
262 l = (afs_uint32) *usp;
263 return (XDR_PUTINT32(xdrs, &l));
266 if (!XDR_GETINT32(xdrs, &l)) {
289 switch (xdrs->x_op) {
293 return (XDR_PUTINT32(xdrs, &l));
296 if (!XDR_GETINT32(xdrs, &l)) {
309 * XDR unsigned short integers
312 xdr_u_short(xdrs, usp)
318 switch (xdrs->x_op) {
321 l = (afs_uint32) *usp;
322 return (XDR_PUTINT32(xdrs, &l));
325 if (!XDR_GETINT32(xdrs, &l)) {
348 switch (xdrs->x_op) {
351 lb = *bp ? XDR_TRUE : XDR_FALSE;
352 return (XDR_PUTINT32(xdrs, &lb));
355 if (!XDR_GETINT32(xdrs, &lb)) {
358 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
375 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
378 * enums are treated as ints
381 return (xdr_long(xdrs, (long *)ep));
387 * Allows the specification of a fixed size sequence of opaque bytes.
388 * cp points to the opaque object and cnt gives the byte length.
391 xdr_opaque(xdrs, cp, cnt)
396 register u_int rndup;
397 int crud[BYTES_PER_XDR_UNIT];
398 char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
401 * if no data we are done
407 * round byte count to full xdr units
409 rndup = cnt % BYTES_PER_XDR_UNIT;
411 rndup = BYTES_PER_XDR_UNIT - rndup;
413 if (xdrs->x_op == XDR_DECODE) {
414 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
419 return (XDR_GETBYTES(xdrs, crud, rndup));
422 if (xdrs->x_op == XDR_ENCODE) {
423 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
428 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
431 if (xdrs->x_op == XDR_FREE) {
440 * *cpp is a pointer to the bytes, *sizep is the count.
441 * If *cpp is NULL maxsize bytes are allocated
444 xdr_bytes(xdrs, cpp, sizep, maxsize)
447 register u_int *sizep;
450 register char *sp = *cpp; /* sp is the actual string pointer */
451 register u_int nodesize;
454 * first deal with the length since xdr bytes are counted
456 if (! xdr_u_int(xdrs, sizep)) {
460 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
465 * now deal with the actual bytes
467 switch (xdrs->x_op) {
471 *cpp = sp = (char *)osi_alloc(nodesize);
479 return (xdr_opaque(xdrs, sp, nodesize));
483 osi_free(sp, nodesize);
492 * XDR a descriminated union
493 * Support routine for discriminated unions.
494 * You create an array of xdrdiscrim structures, terminated with
495 * an entry with a null procedure pointer. The routine gets
496 * the discriminant value and then searches the array of xdrdiscrims
497 * looking for that value. It calls the procedure given in the xdrdiscrim
498 * to handle the discriminant. If there is no specific routine a default
499 * routine may be called.
500 * If there is no specific or default routine an error is returned.
503 xdr_union(xdrs, dscmp, unp, choices, dfault)
505 enum_t *dscmp; /* enum to decide which arm to work on */
506 caddr_t unp; /* the union itself */
507 struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
508 xdrproc_t dfault; /* default xdr routine */
510 register enum_t dscm;
513 * we deal with the discriminator; it's an enum
515 if (! xdr_enum(xdrs, dscmp)) {
521 * search choices for a value that matches the discriminator.
522 * if we find one, execute the xdr routine for that value.
524 for (; choices->proc != NULL_xdrproc_t; choices++) {
525 if (choices->value == dscm)
526 return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
530 * no match - execute the default xdr routine if there is one
532 return ((dfault == NULL_xdrproc_t) ? FALSE :
533 (*dfault)(xdrs, unp, LASTUNSIGNED));
538 * Non-portable xdr primitives.
539 * Care should be taken when moving these routines to new architectures.
544 * XDR null terminated ASCII strings
545 * xdr_string deals with "C strings" - arrays of bytes that are
546 * terminated by a NULL character. The parameter cpp references a
547 * pointer to storage; If the pointer is null, then the necessary
548 * storage is allocated. The last parameter is the max allowed length
549 * of the string as specified by a protocol.
552 xdr_string(xdrs, cpp, maxsize)
557 register char *sp = *cpp; /* sp is the actual string pointer */
561 if (maxsize > ((~0) >> 1) - 1) maxsize = ((~0) >> 1) - 1;
564 * first deal with the length since xdr strings are counted-strings
566 switch (xdrs->x_op) {
569 return (TRUE); /* already free */
577 if (! xdr_u_int(xdrs, &size)) {
580 if (size > maxsize) {
586 * now deal with the actual bytes
588 switch (xdrs->x_op) {
592 *cpp = sp = (char *)osi_alloc(nodesize);
600 return (xdr_opaque(xdrs, sp, size));
604 osi_free(sp, nodesize);
613 * Wrapper for xdr_string that can be called directly from
614 * routines like clnt_call
618 xdr_wrapstring(xdrs, cpp)
622 if (xdr_string(xdrs, cpp, BUFSIZ)) {