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>
31 #include <afs/param.h>
39 * xdr.c, Generic XDR routines implementation.
41 * Copyright (C) 1984, Sun Microsystems, Inc.
43 * These are the "generic" xdr routines used to serialize and de-serialize
44 * most common data items. See xdr.h for more info on the interface to
50 #if defined(KERNEL) && !defined(UKERNEL)
51 #include <sys/param.h>
52 #ifndef AFS_LINUX20_ENV
53 #include <sys/systm.h>
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)
84 xdr_int(XDR * xdrs, int *ip)
92 return (XDR_PUTINT32(xdrs, &l));
95 if (!XDR_GETINT32(xdrs, &l)) {
108 * XDR unsigned integers
111 xdr_u_int(XDR * xdrs, u_int * uip)
115 switch (xdrs->x_op) {
118 l = (afs_uint32) * uip;
119 return (XDR_PUTINT32(xdrs, (afs_int32 *) &l));
122 if (!XDR_GETINT32(xdrs, (afs_int32 *) &l)) {
139 xdr_long(XDR * xdrs, long *lp)
143 switch (xdrs->x_op) {
146 l = (afs_int32) * lp;
147 return (XDR_PUTINT32(xdrs, &l));
150 if (!XDR_GETINT32(xdrs, &l)) {
163 * XDR unsigned long integers
166 xdr_u_long(XDR * xdrs, u_long * ulp)
170 switch (xdrs->x_op) {
173 l = (afs_uint32) * ulp;
174 return (XDR_PUTINT32(xdrs, (afs_int32 *)&l));
177 if (!XDR_GETINT32(xdrs, (afs_int32 *)&l)) {
194 xdr_char(XDR * xdrs, char *sp)
198 switch (xdrs->x_op) {
201 l = (afs_int32) * sp;
202 return (XDR_PUTINT32(xdrs, &l));
205 if (!XDR_GETINT32(xdrs, &l)) {
221 xdr_u_char(XDR * xdrs, u_char * usp)
225 switch (xdrs->x_op) {
228 l = (afs_uint32) * usp;
229 return (XDR_PUTINT32(xdrs, (afs_int32 *)&l));
232 if (!XDR_GETINT32(xdrs, (afs_int32 *)&l)) {
249 xdr_short(XDR * xdrs, short *sp)
253 switch (xdrs->x_op) {
256 l = (afs_int32) * sp;
257 return (XDR_PUTINT32(xdrs, &l));
260 if (!XDR_GETINT32(xdrs, &l)) {
273 * XDR unsigned short integers
276 xdr_u_short(XDR * xdrs, u_short * usp)
280 switch (xdrs->x_op) {
283 l = (afs_uint32) * usp;
284 return (XDR_PUTINT32(xdrs, (afs_int32 *)&l));
287 if (!XDR_GETINT32(xdrs, (afs_int32 *)&l)) {
304 xdr_bool(XDR * xdrs, bool_t * bp)
308 switch (xdrs->x_op) {
311 lb = *bp ? XDR_TRUE : XDR_FALSE;
312 return (XDR_PUTINT32(xdrs, &lb));
315 if (!XDR_GETINT32(xdrs, &lb)) {
318 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
331 xdr_enum(XDR * xdrs, enum_t * ep)
333 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
336 * enums are treated as ints
339 return (xdr_long(xdrs, (long *)ep));
345 * Allows the specification of a fixed size sequence of opaque bytes.
346 * cp points to the opaque object and cnt gives the byte length.
349 xdr_opaque(XDR * xdrs, caddr_t cp, u_int cnt)
352 int crud[BYTES_PER_XDR_UNIT];
353 char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
356 * if no data we are done
362 * round byte count to full xdr units
364 rndup = cnt % BYTES_PER_XDR_UNIT;
366 rndup = BYTES_PER_XDR_UNIT - rndup;
368 if (xdrs->x_op == XDR_DECODE) {
369 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
374 return (XDR_GETBYTES(xdrs, (caddr_t)crud, rndup));
377 if (xdrs->x_op == XDR_ENCODE) {
378 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
383 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
386 if (xdrs->x_op == XDR_FREE) {
395 * *cpp is a pointer to the bytes, *sizep is the count.
396 * If *cpp is NULL maxsize bytes are allocated
399 xdr_bytes(XDR * xdrs, char **cpp, u_int * sizep,
402 char *sp = *cpp; /* sp is the actual string pointer */
406 * first deal with the length since xdr bytes are counted
408 if (!xdr_u_int(xdrs, sizep)) {
412 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
417 * now deal with the actual bytes
419 switch (xdrs->x_op) {
423 *cpp = sp = (char *)osi_alloc(nodesize);
431 return (xdr_opaque(xdrs, sp, nodesize));
435 osi_free(sp, nodesize);
444 * XDR a descriminated union
445 * Support routine for discriminated unions.
446 * You create an array of xdrdiscrim structures, terminated with
447 * an entry with a null procedure pointer. The routine gets
448 * the discriminant value and then searches the array of xdrdiscrims
449 * looking for that value. It calls the procedure given in the xdrdiscrim
450 * to handle the discriminant. If there is no specific routine a default
451 * routine may be called.
452 * If there is no specific or default routine an error is returned.
455 enum_t *dscmp; * enum to decide which arm to work on *
456 caddr_t unp; * the union itself *
457 struct xdr_discrim *choices; * [value, xdr proc] for each arm *
458 xdrproc_t dfault; * default xdr routine *
461 xdr_union(XDR * xdrs, enum_t * dscmp, caddr_t unp,
462 struct xdr_discrim * choices, xdrproc_t dfault)
467 * we deal with the discriminator; it's an enum
469 if (!xdr_enum(xdrs, dscmp)) {
475 * search choices for a value that matches the discriminator.
476 * if we find one, execute the xdr routine for that value.
478 for (; choices->proc != NULL_xdrproc_t; choices++) {
479 if (choices->value == dscm)
480 return ((*(choices->proc)) (xdrs, unp, LASTUNSIGNED));
484 * no match - execute the default xdr routine if there is one
486 return ((dfault == NULL_xdrproc_t) ? FALSE : (*dfault) (xdrs, unp,
492 * Non-portable xdr primitives.
493 * Care should be taken when moving these routines to new architectures.
498 * XDR null terminated ASCII strings
499 * xdr_string deals with "C strings" - arrays of bytes that are
500 * terminated by a NULL character. The parameter cpp references a
501 * pointer to storage; If the pointer is null, then the necessary
502 * storage is allocated. The last parameter is the max allowed length
503 * of the string as specified by a protocol.
506 xdr_string(XDR * xdrs, char **cpp, u_int maxsize)
508 char *sp = *cpp; /* sp is the actual string pointer */
512 if (maxsize > ((~0u) >> 1) - 1)
513 maxsize = ((~0u) >> 1) - 1;
516 * first deal with the length since xdr strings are counted-strings
518 switch (xdrs->x_op) {
521 return (TRUE); /* already free */
531 if (!xdr_u_int(xdrs, &size)) {
534 if (size > maxsize) {
540 * now deal with the actual bytes
542 switch (xdrs->x_op) {
546 *cpp = sp = (char *)osi_alloc(nodesize);
554 return (xdr_opaque(xdrs, sp, size));
558 osi_free(sp, nodesize);
567 * Wrapper for xdr_string that can be called directly from
568 * routines like clnt_call
572 xdr_wrapstring(XDR * xdrs, char **cpp)
574 if (xdr_string(xdrs, cpp, BUFSIZ)) {
582 xdr_alloc(afs_int32 size)
584 return osi_alloc(size);
588 xdr_free(xdrproc_t proc, void *obj)
594 /* See note in xdr.h for the method behind this madness */
595 #if defined(AFS_I386_LINUX26_ENV) && defined(KERNEL) && !defined(UKERNEL)