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, lp));
146 if (xdrs->x_op == XDR_DECODE)
147 return (XDR_GETINT32(xdrs, 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!
160 xdr_u_long(xdrs, ulp)
165 if (xdrs->x_op == XDR_DECODE)
166 return (XDR_GETINT32(xdrs, (long *)ulp));
168 if (xdrs->x_op == XDR_ENCODE)
169 return (XDR_PUTINT32(xdrs, (long *)ulp));
171 if (xdrs->x_op == XDR_FREE)
178 * XDR afs_int32 integers
179 * same as xdr_u_long - open coded to save a proc call!
187 if (xdrs->x_op == XDR_ENCODE)
188 return (XDR_PUTINT32(xdrs, (long *)lp));
190 if (xdrs->x_op == XDR_DECODE)
191 return (XDR_GETINT32(xdrs, (long *)lp));
193 if (xdrs->x_op == XDR_FREE)
200 * XDR unsigned afs_int32 integers
201 * same as xdr_long - open coded to save a proc call!
209 if (xdrs->x_op == XDR_DECODE)
210 return (XDR_GETINT32(xdrs, (long *)ulp));
211 if (xdrs->x_op == XDR_ENCODE)
212 return (XDR_PUTINT32(xdrs, (long *)ulp));
213 if (xdrs->x_op == XDR_FREE)
228 switch (xdrs->x_op) {
232 return (XDR_PUTINT32(xdrs, &l));
235 if (!XDR_GETINT32(xdrs, &l)) {
251 xdr_u_char(xdrs, usp)
257 switch (xdrs->x_op) {
260 l = (afs_uint32) *usp;
261 return (XDR_PUTINT32(xdrs, &l));
264 if (!XDR_GETINT32(xdrs, &l)) {
287 switch (xdrs->x_op) {
291 return (XDR_PUTINT32(xdrs, &l));
294 if (!XDR_GETINT32(xdrs, &l)) {
307 * XDR unsigned short integers
310 xdr_u_short(xdrs, usp)
316 switch (xdrs->x_op) {
319 l = (afs_uint32) *usp;
320 return (XDR_PUTINT32(xdrs, &l));
323 if (!XDR_GETINT32(xdrs, &l)) {
346 switch (xdrs->x_op) {
349 lb = *bp ? XDR_TRUE : XDR_FALSE;
350 return (XDR_PUTINT32(xdrs, &lb));
353 if (!XDR_GETINT32(xdrs, &lb)) {
356 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
373 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
376 * enums are treated as ints
379 return (xdr_long(xdrs, (long *)ep));
385 * Allows the specification of a fixed size sequence of opaque bytes.
386 * cp points to the opaque object and cnt gives the byte length.
389 xdr_opaque(xdrs, cp, cnt)
394 register u_int rndup;
395 int crud[BYTES_PER_XDR_UNIT];
396 char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
399 * if no data we are done
405 * round byte count to full xdr units
407 rndup = cnt % BYTES_PER_XDR_UNIT;
409 rndup = BYTES_PER_XDR_UNIT - rndup;
411 if (xdrs->x_op == XDR_DECODE) {
412 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
417 return (XDR_GETBYTES(xdrs, crud, rndup));
420 if (xdrs->x_op == XDR_ENCODE) {
421 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
426 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
429 if (xdrs->x_op == XDR_FREE) {
438 * *cpp is a pointer to the bytes, *sizep is the count.
439 * If *cpp is NULL maxsize bytes are allocated
442 xdr_bytes(xdrs, cpp, sizep, maxsize)
445 register u_int *sizep;
448 register char *sp = *cpp; /* sp is the actual string pointer */
449 register u_int nodesize;
452 * first deal with the length since xdr bytes are counted
454 if (! xdr_u_int(xdrs, sizep)) {
458 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
463 * now deal with the actual bytes
465 switch (xdrs->x_op) {
469 *cpp = sp = (char *)osi_alloc(nodesize);
477 return (xdr_opaque(xdrs, sp, nodesize));
481 osi_free(sp, nodesize);
490 * XDR a descriminated union
491 * Support routine for discriminated unions.
492 * You create an array of xdrdiscrim structures, terminated with
493 * an entry with a null procedure pointer. The routine gets
494 * the discriminant value and then searches the array of xdrdiscrims
495 * looking for that value. It calls the procedure given in the xdrdiscrim
496 * to handle the discriminant. If there is no specific routine a default
497 * routine may be called.
498 * If there is no specific or default routine an error is returned.
501 xdr_union(xdrs, dscmp, unp, choices, dfault)
503 enum_t *dscmp; /* enum to decide which arm to work on */
504 caddr_t unp; /* the union itself */
505 struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
506 xdrproc_t dfault; /* default xdr routine */
508 register enum_t dscm;
511 * we deal with the discriminator; it's an enum
513 if (! xdr_enum(xdrs, dscmp)) {
519 * search choices for a value that matches the discriminator.
520 * if we find one, execute the xdr routine for that value.
522 for (; choices->proc != NULL_xdrproc_t; choices++) {
523 if (choices->value == dscm)
524 return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
528 * no match - execute the default xdr routine if there is one
530 return ((dfault == NULL_xdrproc_t) ? FALSE :
531 (*dfault)(xdrs, unp, LASTUNSIGNED));
536 * Non-portable xdr primitives.
537 * Care should be taken when moving these routines to new architectures.
542 * XDR null terminated ASCII strings
543 * xdr_string deals with "C strings" - arrays of bytes that are
544 * terminated by a NULL character. The parameter cpp references a
545 * pointer to storage; If the pointer is null, then the necessary
546 * storage is allocated. The last parameter is the max allowed length
547 * of the string as specified by a protocol.
550 xdr_string(xdrs, cpp, maxsize)
555 register char *sp = *cpp; /* sp is the actual string pointer */
560 * first deal with the length since xdr strings are counted-strings
562 switch (xdrs->x_op) {
565 return (TRUE); /* already free */
573 if (! xdr_u_int(xdrs, &size)) {
576 if (size > maxsize) {
582 * now deal with the actual bytes
584 switch (xdrs->x_op) {
588 *cpp = sp = (char *)osi_alloc(nodesize);
596 return (xdr_opaque(xdrs, sp, size));
600 osi_free(sp, nodesize);
609 * Wrapper for xdr_string that can be called directly from
610 * routines like clnt_call
614 xdr_wrapstring(xdrs, cpp)
618 if (xdr_string(xdrs, cpp, BUFSIZ)) {