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 #if !defined(AFS_LINUX26_ENV)
52 #include <sys/param.h>
54 #ifndef AFS_LINUX20_ENV
55 #include <sys/systm.h>
62 * constants specific to the xdr "protocol"
64 #define XDR_FALSE ((afs_int32) 0)
65 #define XDR_TRUE ((afs_int32) 1)
66 #define LASTUNSIGNED ((u_int) 0-1)
86 xdr_int(XDR * xdrs, int *ip)
94 return (XDR_PUTINT32(xdrs, &l));
97 if (!XDR_GETINT32(xdrs, &l)) {
110 * XDR unsigned integers
113 xdr_u_int(XDR * xdrs, u_int * uip)
117 switch (xdrs->x_op) {
120 l = (afs_uint32) * uip;
121 return (XDR_PUTINT32(xdrs, (afs_int32 *) &l));
124 if (!XDR_GETINT32(xdrs, (afs_int32 *) &l)) {
141 xdr_long(XDR * xdrs, long *lp)
145 switch (xdrs->x_op) {
148 l = (afs_int32) * lp;
149 return (XDR_PUTINT32(xdrs, &l));
152 if (!XDR_GETINT32(xdrs, &l)) {
165 * XDR unsigned long integers
168 xdr_u_long(XDR * xdrs, u_long * ulp)
172 switch (xdrs->x_op) {
175 l = (afs_uint32) * ulp;
176 return (XDR_PUTINT32(xdrs, (afs_int32 *)&l));
179 if (!XDR_GETINT32(xdrs, (afs_int32 *)&l)) {
196 xdr_char(XDR * xdrs, char *sp)
200 switch (xdrs->x_op) {
203 l = (afs_int32) * sp;
204 return (XDR_PUTINT32(xdrs, &l));
207 if (!XDR_GETINT32(xdrs, &l)) {
223 xdr_u_char(XDR * xdrs, u_char * usp)
227 switch (xdrs->x_op) {
230 l = (afs_uint32) * usp;
231 return (XDR_PUTINT32(xdrs, (afs_int32 *)&l));
234 if (!XDR_GETINT32(xdrs, (afs_int32 *)&l)) {
251 xdr_short(XDR * xdrs, short *sp)
255 switch (xdrs->x_op) {
258 l = (afs_int32) * sp;
259 return (XDR_PUTINT32(xdrs, &l));
262 if (!XDR_GETINT32(xdrs, &l)) {
275 * XDR unsigned short integers
278 xdr_u_short(XDR * xdrs, u_short * usp)
282 switch (xdrs->x_op) {
285 l = (afs_uint32) * usp;
286 return (XDR_PUTINT32(xdrs, (afs_int32 *)&l));
289 if (!XDR_GETINT32(xdrs, (afs_int32 *)&l)) {
306 xdr_bool(XDR * xdrs, bool_t * bp)
310 switch (xdrs->x_op) {
313 lb = *bp ? XDR_TRUE : XDR_FALSE;
314 return (XDR_PUTINT32(xdrs, &lb));
317 if (!XDR_GETINT32(xdrs, &lb)) {
320 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
333 xdr_enum(XDR * xdrs, enum_t * ep)
336 * enums are treated as ints
339 return (xdr_int(xdrs, ep));
344 * Allows the specification of a fixed size sequence of opaque bytes.
345 * cp points to the opaque object and cnt gives the byte length.
348 xdr_opaque(XDR * xdrs, caddr_t cp, u_int cnt)
351 int crud[BYTES_PER_XDR_UNIT];
352 char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
355 * if no data we are done
361 * round byte count to full xdr units
363 rndup = cnt % BYTES_PER_XDR_UNIT;
365 rndup = BYTES_PER_XDR_UNIT - rndup;
367 if (xdrs->x_op == XDR_DECODE) {
368 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
373 return (XDR_GETBYTES(xdrs, (caddr_t)crud, rndup));
376 if (xdrs->x_op == XDR_ENCODE) {
377 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
382 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
385 if (xdrs->x_op == XDR_FREE) {
394 * *cpp is a pointer to the bytes, *sizep is the count.
395 * If *cpp is NULL maxsize bytes are allocated
398 xdr_bytes(XDR * xdrs, char **cpp, u_int * sizep,
401 char *sp = *cpp; /* sp is the actual string pointer */
405 * first deal with the length since xdr bytes are counted
407 if (!xdr_u_int(xdrs, sizep)) {
411 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
416 * now deal with the actual bytes
418 switch (xdrs->x_op) {
422 *cpp = sp = (char *)osi_alloc(nodesize);
430 return (xdr_opaque(xdrs, sp, nodesize));
434 osi_free(sp, nodesize);
443 * XDR a descriminated union
444 * Support routine for discriminated unions.
445 * You create an array of xdrdiscrim structures, terminated with
446 * an entry with a null procedure pointer. The routine gets
447 * the discriminant value and then searches the array of xdrdiscrims
448 * looking for that value. It calls the procedure given in the xdrdiscrim
449 * to handle the discriminant. If there is no specific routine a default
450 * routine may be called.
451 * If there is no specific or default routine an error is returned.
454 enum_t *dscmp; * enum to decide which arm to work on *
455 caddr_t unp; * the union itself *
456 struct xdr_discrim *choices; * [value, xdr proc] for each arm *
457 xdrproc_t dfault; * default xdr routine *
460 xdr_union(XDR * xdrs, enum_t * dscmp, caddr_t unp,
461 struct xdr_discrim * choices, xdrproc_t dfault)
466 * we deal with the discriminator; it's an enum
468 if (!xdr_enum(xdrs, dscmp)) {
474 * search choices for a value that matches the discriminator.
475 * if we find one, execute the xdr routine for that value.
477 for (; choices->proc != NULL_xdrproc_t; choices++) {
478 if (choices->value == dscm)
479 return ((*(choices->proc)) (xdrs, unp, LASTUNSIGNED));
483 * no match - execute the default xdr routine if there is one
485 return ((dfault == NULL_xdrproc_t) ? FALSE : (*dfault) (xdrs, unp,
491 * Non-portable xdr primitives.
492 * Care should be taken when moving these routines to new architectures.
497 * XDR null terminated ASCII strings
498 * xdr_string deals with "C strings" - arrays of bytes that are
499 * terminated by a NULL character. The parameter cpp references a
500 * pointer to storage; If the pointer is null, then the necessary
501 * storage is allocated. The last parameter is the max allowed length
502 * of the string as specified by a protocol.
505 xdr_string(XDR * xdrs, char **cpp, u_int maxsize)
507 char *sp = *cpp; /* sp is the actual string pointer */
511 if (maxsize > ((~0u) >> 1) - 1)
512 maxsize = ((~0u) >> 1) - 1;
515 * first deal with the length since xdr strings are counted-strings
517 switch (xdrs->x_op) {
520 return (TRUE); /* already free */
530 if (!xdr_u_int(xdrs, &size)) {
533 if (size > maxsize) {
539 * now deal with the actual bytes
541 switch (xdrs->x_op) {
545 *cpp = sp = (char *)osi_alloc(nodesize);
553 return (xdr_opaque(xdrs, sp, size));
557 osi_free(sp, nodesize);
566 * Wrapper for xdr_string that can be called directly from
567 * routines like clnt_call
571 xdr_wrapstring(XDR * xdrs, char **cpp)
573 if (xdr_string(xdrs, cpp, BUFSIZ)) {
581 xdr_alloc(afs_int32 size)
583 return osi_alloc(size);
587 xdr_free(xdrproc_t proc, void *obj)
593 /* See note in xdr.h for the method behind this madness */
594 #if defined(AFS_I386_LINUX26_ENV) && defined(KERNEL) && !defined(UKERNEL)