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
31 static char sccsid[] = "@(#)xdr.c 1.1 86/02/03 Copyr 1984 Sun Micro";
35 * xdr.c, Generic XDR routines implementation.
37 * Copyright (C) 1984, Sun Microsystems, Inc.
39 * These are the "generic" xdr routines used to serialize and de-serialize
40 * most common data items. See xdr.h for more info on the interface to
45 #include "../afs/param.h"
46 #include <sys/param.h>
47 #ifndef AFS_LINUX20_ENV
48 #include <sys/systm.h>
56 extern char *osi_alloc();
60 * constants specific to the xdr "protocol"
62 #define XDR_FALSE ((afs_int32) 0)
63 #define XDR_TRUE ((afs_int32) 1)
64 #define LASTUNSIGNED ((u_int) 0-1)
75 xdr_void(/* xdrs, addr */)
83 #if !defined(AFS_OSF20_ENV) && !defined(AFS_SGI61_ENV)
93 if (xdrs->x_op == XDR_ENCODE)
94 return (XDR_PUTINT32(xdrs, (long *)ip));
96 if (xdrs->x_op == XDR_DECODE)
97 return (XDR_GETINT32(xdrs, (long *)ip));
99 if (xdrs->x_op == XDR_FREE)
106 * XDR unsigned integers
114 if (xdrs->x_op == XDR_DECODE)
115 return (XDR_GETINT32(xdrs, (long *)up));
117 if (xdrs->x_op == XDR_ENCODE)
118 return (XDR_PUTINT32(xdrs, (long *)up));
120 if (xdrs->x_op == XDR_FREE)
128 * XDR afs_int32 integers
129 * same as xdr_u_long - open coded to save a proc call!
137 if (xdrs->x_op == XDR_ENCODE)
138 return (XDR_PUTINT32(xdrs, lp));
140 if (xdrs->x_op == XDR_DECODE)
141 return (XDR_GETINT32(xdrs, lp));
143 if (xdrs->x_op == XDR_FREE)
150 * XDR unsigned afs_int32 integers
151 * same as xdr_long - open coded to save a proc call!
154 xdr_u_long(xdrs, ulp)
159 if (xdrs->x_op == XDR_DECODE)
160 return (XDR_GETINT32(xdrs, (long *)ulp));
162 if (xdrs->x_op == XDR_ENCODE)
163 return (XDR_PUTINT32(xdrs, (long *)ulp));
165 if (xdrs->x_op == XDR_FREE)
172 * XDR afs_int32 integers
173 * same as xdr_u_long - open coded to save a proc call!
181 if (xdrs->x_op == XDR_ENCODE)
182 return (XDR_PUTINT32(xdrs, (long *)lp));
184 if (xdrs->x_op == XDR_DECODE)
185 return (XDR_GETINT32(xdrs, (long *)lp));
187 if (xdrs->x_op == XDR_FREE)
194 * XDR unsigned afs_int32 integers
195 * same as xdr_long - open coded to save a proc call!
203 if (xdrs->x_op == XDR_DECODE)
204 return (XDR_GETINT32(xdrs, (long *)ulp));
205 if (xdrs->x_op == XDR_ENCODE)
206 return (XDR_PUTINT32(xdrs, (long *)ulp));
207 if (xdrs->x_op == XDR_FREE)
222 switch (xdrs->x_op) {
226 return (XDR_PUTINT32(xdrs, &l));
229 if (!XDR_GETINT32(xdrs, &l)) {
245 xdr_u_char(xdrs, usp)
251 switch (xdrs->x_op) {
254 l = (afs_uint32) *usp;
255 return (XDR_PUTINT32(xdrs, &l));
258 if (!XDR_GETINT32(xdrs, &l)) {
281 switch (xdrs->x_op) {
285 return (XDR_PUTINT32(xdrs, &l));
288 if (!XDR_GETINT32(xdrs, &l)) {
301 * XDR unsigned short integers
304 xdr_u_short(xdrs, usp)
310 switch (xdrs->x_op) {
313 l = (afs_uint32) *usp;
314 return (XDR_PUTINT32(xdrs, &l));
317 if (!XDR_GETINT32(xdrs, &l)) {
340 switch (xdrs->x_op) {
343 lb = *bp ? XDR_TRUE : XDR_FALSE;
344 return (XDR_PUTINT32(xdrs, &lb));
347 if (!XDR_GETINT32(xdrs, &lb)) {
350 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
367 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
370 * enums are treated as ints
373 return (xdr_long(xdrs, (long *)ep));
379 * Allows the specification of a fixed size sequence of opaque bytes.
380 * cp points to the opaque object and cnt gives the byte length.
383 xdr_opaque(xdrs, cp, cnt)
388 register u_int rndup;
389 int crud[BYTES_PER_XDR_UNIT];
390 char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
393 * if no data we are done
399 * round byte count to full xdr units
401 rndup = cnt % BYTES_PER_XDR_UNIT;
403 rndup = BYTES_PER_XDR_UNIT - rndup;
405 if (xdrs->x_op == XDR_DECODE) {
406 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
411 return (XDR_GETBYTES(xdrs, crud, rndup));
414 if (xdrs->x_op == XDR_ENCODE) {
415 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
420 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
423 if (xdrs->x_op == XDR_FREE) {
432 * *cpp is a pointer to the bytes, *sizep is the count.
433 * If *cpp is NULL maxsize bytes are allocated
436 xdr_bytes(xdrs, cpp, sizep, maxsize)
439 register u_int *sizep;
442 register char *sp = *cpp; /* sp is the actual string pointer */
443 register u_int nodesize;
446 * first deal with the length since xdr bytes are counted
448 if (! xdr_u_int(xdrs, sizep)) {
452 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
457 * now deal with the actual bytes
459 switch (xdrs->x_op) {
463 *cpp = sp = (char *)osi_alloc(nodesize);
471 return (xdr_opaque(xdrs, sp, nodesize));
475 osi_free(sp, nodesize);
484 * XDR a descriminated union
485 * Support routine for discriminated unions.
486 * You create an array of xdrdiscrim structures, terminated with
487 * an entry with a null procedure pointer. The routine gets
488 * the discriminant value and then searches the array of xdrdiscrims
489 * looking for that value. It calls the procedure given in the xdrdiscrim
490 * to handle the discriminant. If there is no specific routine a default
491 * routine may be called.
492 * If there is no specific or default routine an error is returned.
495 xdr_union(xdrs, dscmp, unp, choices, dfault)
497 enum_t *dscmp; /* enum to decide which arm to work on */
498 caddr_t unp; /* the union itself */
499 struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
500 xdrproc_t dfault; /* default xdr routine */
502 register enum_t dscm;
505 * we deal with the discriminator; it's an enum
507 if (! xdr_enum(xdrs, dscmp)) {
513 * search choices for a value that matches the discriminator.
514 * if we find one, execute the xdr routine for that value.
516 for (; choices->proc != NULL_xdrproc_t; choices++) {
517 if (choices->value == dscm)
518 return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
522 * no match - execute the default xdr routine if there is one
524 return ((dfault == NULL_xdrproc_t) ? FALSE :
525 (*dfault)(xdrs, unp, LASTUNSIGNED));
530 * Non-portable xdr primitives.
531 * Care should be taken when moving these routines to new architectures.
536 * XDR null terminated ASCII strings
537 * xdr_string deals with "C strings" - arrays of bytes that are
538 * terminated by a NULL character. The parameter cpp references a
539 * pointer to storage; If the pointer is null, then the necessary
540 * storage is allocated. The last parameter is the max allowed length
541 * of the string as specified by a protocol.
544 xdr_string(xdrs, cpp, maxsize)
549 register char *sp = *cpp; /* sp is the actual string pointer */
554 * first deal with the length since xdr strings are counted-strings
556 switch (xdrs->x_op) {
559 return (TRUE); /* already free */
567 if (! xdr_u_int(xdrs, &size)) {
570 if (size > maxsize) {
576 * now deal with the actual bytes
578 switch (xdrs->x_op) {
582 *cpp = sp = (char *)osi_alloc(nodesize);
590 return (xdr_opaque(xdrs, sp, size));
594 osi_free(sp, nodesize);
603 * Wrapper for xdr_string that can be called directly from
604 * routines like clnt_call
608 xdr_wrapstring(xdrs, cpp)
612 if (xdr_string(xdrs, cpp, BUFSIZ)) {