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
51 #if defined(KERNEL) && !defined(UKERNEL)
52 #include <sys/param.h>
53 #ifndef AFS_LINUX20_ENV
54 #include <sys/systm.h>
63 * constants specific to the xdr "protocol"
65 #define XDR_FALSE ((afs_int32) 0)
66 #define XDR_TRUE ((afs_int32) 1)
67 #define LASTUNSIGNED ((u_int) 0-1)
87 xdr_int(XDR * xdrs, int *ip)
95 return (XDR_PUTINT32(xdrs, &l));
98 if (!XDR_GETINT32(xdrs, &l)) {
111 * XDR unsigned integers
114 xdr_u_int(XDR * xdrs, u_int * uip)
118 switch (xdrs->x_op) {
121 l = (afs_uint32) * uip;
122 return (XDR_PUTINT32(xdrs, (afs_int32 *) &l));
125 if (!XDR_GETINT32(xdrs, (afs_int32 *) &l)) {
142 xdr_long(XDR * xdrs, long *lp)
146 switch (xdrs->x_op) {
149 l = (afs_int32) * lp;
150 return (XDR_PUTINT32(xdrs, &l));
153 if (!XDR_GETINT32(xdrs, &l)) {
166 * XDR unsigned long integers
169 xdr_u_long(XDR * xdrs, u_long * ulp)
173 switch (xdrs->x_op) {
176 l = (afs_uint32) * ulp;
177 return (XDR_PUTINT32(xdrs, (afs_int32 *)&l));
180 if (!XDR_GETINT32(xdrs, (afs_int32 *)&l)) {
197 xdr_char(XDR * xdrs, char *sp)
201 switch (xdrs->x_op) {
204 l = (afs_int32) * sp;
205 return (XDR_PUTINT32(xdrs, &l));
208 if (!XDR_GETINT32(xdrs, &l)) {
224 xdr_u_char(XDR * xdrs, u_char * usp)
228 switch (xdrs->x_op) {
231 l = (afs_uint32) * usp;
232 return (XDR_PUTINT32(xdrs, (afs_int32 *)&l));
235 if (!XDR_GETINT32(xdrs, (afs_int32 *)&l)) {
252 xdr_short(XDR * xdrs, short *sp)
256 switch (xdrs->x_op) {
259 l = (afs_int32) * sp;
260 return (XDR_PUTINT32(xdrs, &l));
263 if (!XDR_GETINT32(xdrs, &l)) {
276 * XDR unsigned short integers
279 xdr_u_short(XDR * xdrs, u_short * usp)
283 switch (xdrs->x_op) {
286 l = (afs_uint32) * usp;
287 return (XDR_PUTINT32(xdrs, (afs_int32 *)&l));
290 if (!XDR_GETINT32(xdrs, (afs_int32 *)&l)) {
307 xdr_bool(XDR * xdrs, bool_t * bp)
311 switch (xdrs->x_op) {
314 lb = *bp ? XDR_TRUE : XDR_FALSE;
315 return (XDR_PUTINT32(xdrs, &lb));
318 if (!XDR_GETINT32(xdrs, &lb)) {
321 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
334 xdr_enum(XDR * xdrs, enum_t * ep)
336 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
339 * enums are treated as ints
342 return (xdr_long(xdrs, (long *)ep));
348 * Allows the specification of a fixed size sequence of opaque bytes.
349 * cp points to the opaque object and cnt gives the byte length.
352 xdr_opaque(XDR * xdrs, caddr_t cp, u_int cnt)
355 int crud[BYTES_PER_XDR_UNIT];
356 char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
359 * if no data we are done
365 * round byte count to full xdr units
367 rndup = cnt % BYTES_PER_XDR_UNIT;
369 rndup = BYTES_PER_XDR_UNIT - rndup;
371 if (xdrs->x_op == XDR_DECODE) {
372 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
377 return (XDR_GETBYTES(xdrs, (caddr_t)crud, rndup));
380 if (xdrs->x_op == XDR_ENCODE) {
381 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
386 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
389 if (xdrs->x_op == XDR_FREE) {
398 * *cpp is a pointer to the bytes, *sizep is the count.
399 * If *cpp is NULL maxsize bytes are allocated
402 xdr_bytes(XDR * xdrs, char **cpp, u_int * sizep,
405 char *sp = *cpp; /* sp is the actual string pointer */
409 * first deal with the length since xdr bytes are counted
411 if (!xdr_u_int(xdrs, sizep)) {
415 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
420 * now deal with the actual bytes
422 switch (xdrs->x_op) {
426 *cpp = sp = (char *)osi_alloc(nodesize);
434 return (xdr_opaque(xdrs, sp, nodesize));
438 osi_free(sp, nodesize);
447 * XDR a descriminated union
448 * Support routine for discriminated unions.
449 * You create an array of xdrdiscrim structures, terminated with
450 * an entry with a null procedure pointer. The routine gets
451 * the discriminant value and then searches the array of xdrdiscrims
452 * looking for that value. It calls the procedure given in the xdrdiscrim
453 * to handle the discriminant. If there is no specific routine a default
454 * routine may be called.
455 * If there is no specific or default routine an error is returned.
458 enum_t *dscmp; * enum to decide which arm to work on *
459 caddr_t unp; * the union itself *
460 struct xdr_discrim *choices; * [value, xdr proc] for each arm *
461 xdrproc_t dfault; * default xdr routine *
464 xdr_union(XDR * xdrs, enum_t * dscmp, caddr_t unp,
465 struct xdr_discrim * choices, xdrproc_t dfault)
470 * we deal with the discriminator; it's an enum
472 if (!xdr_enum(xdrs, dscmp)) {
478 * search choices for a value that matches the discriminator.
479 * if we find one, execute the xdr routine for that value.
481 for (; choices->proc != NULL_xdrproc_t; choices++) {
482 if (choices->value == dscm)
483 return ((*(choices->proc)) (xdrs, unp, LASTUNSIGNED));
487 * no match - execute the default xdr routine if there is one
489 return ((dfault == NULL_xdrproc_t) ? FALSE : (*dfault) (xdrs, unp,
495 * Non-portable xdr primitives.
496 * Care should be taken when moving these routines to new architectures.
501 * XDR null terminated ASCII strings
502 * xdr_string deals with "C strings" - arrays of bytes that are
503 * terminated by a NULL character. The parameter cpp references a
504 * pointer to storage; If the pointer is null, then the necessary
505 * storage is allocated. The last parameter is the max allowed length
506 * of the string as specified by a protocol.
509 xdr_string(XDR * xdrs, char **cpp, u_int maxsize)
511 char *sp = *cpp; /* sp is the actual string pointer */
515 /* FIXME: this does not look correct: MSVC 6 computes -2 here */
516 if (maxsize > ((~0) >> 1) - 1)
517 maxsize = ((~0) >> 1) - 1;
520 * first deal with the length since xdr strings are counted-strings
522 switch (xdrs->x_op) {
525 return (TRUE); /* already free */
535 if (!xdr_u_int(xdrs, &size)) {
538 if (size > maxsize) {
544 * now deal with the actual bytes
546 switch (xdrs->x_op) {
550 *cpp = sp = (char *)osi_alloc(nodesize);
558 return (xdr_opaque(xdrs, sp, size));
562 osi_free(sp, nodesize);
571 * Wrapper for xdr_string that can be called directly from
572 * routines like clnt_call
576 xdr_wrapstring(XDR * xdrs, char **cpp)
578 if (xdr_string(xdrs, cpp, BUFSIZ)) {
586 xdr_alloc(afs_int32 size)
588 return osi_alloc(size);
592 xdr_free(xdrproc_t proc, void *obj)
598 /* See note in xdr.h for the method behind this madness */
599 #if defined(AFS_I386_LINUX26_ENV) && defined(KERNEL) && !defined(UKERNEL)