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>
42 * xdr.c, Generic XDR routines implementation.
44 * Copyright (C) 1984, Sun Microsystems, Inc.
46 * These are the "generic" xdr routines used to serialize and de-serialize
47 * most common data items. See xdr.h for more info on the interface to
54 #include <sys/param.h>
55 #ifndef AFS_LINUX20_ENV
56 #include <sys/systm.h>
62 #include "rx_internal.h"
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)
90 xdr_int(XDR * xdrs, int *ip)
98 return (XDR_PUTINT32(xdrs, &l));
101 if (!XDR_GETINT32(xdrs, &l)) {
114 * XDR unsigned integers
117 xdr_u_int(XDR * xdrs, u_int * uip)
121 switch (xdrs->x_op) {
124 l = (afs_uint32) * uip;
125 return (XDR_PUTINT32(xdrs, &l));
128 if (!XDR_GETINT32(xdrs, &l)) {
145 xdr_long(XDR * xdrs, long *lp)
149 switch (xdrs->x_op) {
152 l = (afs_int32) * lp;
153 return (XDR_PUTINT32(xdrs, &l));
156 if (!XDR_GETINT32(xdrs, &l)) {
169 * XDR unsigned long integers
172 xdr_u_long(XDR * xdrs, u_long * ulp)
176 switch (xdrs->x_op) {
179 l = (afs_uint32) * ulp;
180 return (XDR_PUTINT32(xdrs, &l));
183 if (!XDR_GETINT32(xdrs, &l)) {
200 xdr_char(XDR * xdrs, char *sp)
204 switch (xdrs->x_op) {
207 l = (afs_int32) * sp;
208 return (XDR_PUTINT32(xdrs, &l));
211 if (!XDR_GETINT32(xdrs, &l)) {
227 xdr_u_char(XDR * xdrs, u_char * usp)
231 switch (xdrs->x_op) {
234 l = (afs_uint32) * usp;
235 return (XDR_PUTINT32(xdrs, &l));
238 if (!XDR_GETINT32(xdrs, &l)) {
255 xdr_short(XDR * xdrs, short *sp)
259 switch (xdrs->x_op) {
262 l = (afs_int32) * sp;
263 return (XDR_PUTINT32(xdrs, &l));
266 if (!XDR_GETINT32(xdrs, &l)) {
279 * XDR unsigned short integers
282 xdr_u_short(XDR * xdrs, u_short * usp)
286 switch (xdrs->x_op) {
289 l = (afs_uint32) * usp;
290 return (XDR_PUTINT32(xdrs, &l));
293 if (!XDR_GETINT32(xdrs, &l)) {
310 xdr_bool(XDR * xdrs, bool_t * bp)
314 switch (xdrs->x_op) {
317 lb = *bp ? XDR_TRUE : XDR_FALSE;
318 return (XDR_PUTINT32(xdrs, &lb));
321 if (!XDR_GETINT32(xdrs, &lb)) {
324 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
337 xdr_enum(XDR * xdrs, enum_t * ep)
339 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
342 * enums are treated as ints
345 return (xdr_long(xdrs, (long *)ep));
351 * Allows the specification of a fixed size sequence of opaque bytes.
352 * cp points to the opaque object and cnt gives the byte length.
355 xdr_opaque(XDR * xdrs, caddr_t cp, u_int cnt)
358 int crud[BYTES_PER_XDR_UNIT];
359 char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
362 * if no data we are done
368 * round byte count to full xdr units
370 rndup = cnt % BYTES_PER_XDR_UNIT;
372 rndup = BYTES_PER_XDR_UNIT - rndup;
374 if (xdrs->x_op == XDR_DECODE) {
375 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
380 return (XDR_GETBYTES(xdrs, (caddr_t)crud, rndup));
383 if (xdrs->x_op == XDR_ENCODE) {
384 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
389 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
392 if (xdrs->x_op == XDR_FREE) {
401 * *cpp is a pointer to the bytes, *sizep is the count.
402 * If *cpp is NULL maxsize bytes are allocated
405 xdr_bytes(XDR * xdrs, char **cpp, u_int * sizep,
408 char *sp = *cpp; /* sp is the actual string pointer */
412 * first deal with the length since xdr bytes are counted
414 if (!xdr_u_int(xdrs, sizep)) {
418 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
423 * now deal with the actual bytes
425 switch (xdrs->x_op) {
429 *cpp = sp = (char *)osi_alloc(nodesize);
437 return (xdr_opaque(xdrs, sp, nodesize));
441 osi_free(sp, nodesize);
450 * XDR a descriminated union
451 * Support routine for discriminated unions.
452 * You create an array of xdrdiscrim structures, terminated with
453 * an entry with a null procedure pointer. The routine gets
454 * the discriminant value and then searches the array of xdrdiscrims
455 * looking for that value. It calls the procedure given in the xdrdiscrim
456 * to handle the discriminant. If there is no specific routine a default
457 * routine may be called.
458 * If there is no specific or default routine an error is returned.
461 enum_t *dscmp; * enum to decide which arm to work on *
462 caddr_t unp; * the union itself *
463 struct xdr_discrim *choices; * [value, xdr proc] for each arm *
464 xdrproc_t dfault; * default xdr routine *
467 xdr_union(XDR * xdrs, enum_t * dscmp, caddr_t unp,
468 struct xdr_discrim * choices, xdrproc_t dfault)
473 * we deal with the discriminator; it's an enum
475 if (!xdr_enum(xdrs, dscmp)) {
481 * search choices for a value that matches the discriminator.
482 * if we find one, execute the xdr routine for that value.
484 for (; choices->proc != NULL_xdrproc_t; choices++) {
485 if (choices->value == dscm)
486 return ((*(choices->proc)) (xdrs, unp, LASTUNSIGNED));
490 * no match - execute the default xdr routine if there is one
492 return ((dfault == NULL_xdrproc_t) ? FALSE : (*dfault) (xdrs, unp,
498 * Non-portable xdr primitives.
499 * Care should be taken when moving these routines to new architectures.
504 * XDR null terminated ASCII strings
505 * xdr_string deals with "C strings" - arrays of bytes that are
506 * terminated by a NULL character. The parameter cpp references a
507 * pointer to storage; If the pointer is null, then the necessary
508 * storage is allocated. The last parameter is the max allowed length
509 * of the string as specified by a protocol.
512 xdr_string(XDR * xdrs, char **cpp, u_int maxsize)
514 char *sp = *cpp; /* sp is the actual string pointer */
518 /* FIXME: this does not look correct: MSVC 6 computes -2 here */
519 if (maxsize > ((~0) >> 1) - 1)
520 maxsize = ((~0) >> 1) - 1;
523 * first deal with the length since xdr strings are counted-strings
525 switch (xdrs->x_op) {
528 return (TRUE); /* already free */
538 if (!xdr_u_int(xdrs, &size)) {
541 if (size > maxsize) {
547 * now deal with the actual bytes
549 switch (xdrs->x_op) {
553 *cpp = sp = (char *)osi_alloc(nodesize);
561 return (xdr_opaque(xdrs, sp, size));
565 osi_free(sp, nodesize);
574 * Wrapper for xdr_string that can be called directly from
575 * routines like clnt_call
579 xdr_wrapstring(XDR * xdrs, char **cpp)
581 if (xdr_string(xdrs, cpp, BUFSIZ)) {