X-Git-Url: https://git.saurik.com/apple/libinfo.git/blobdiff_plain/3b7c7bd7aae68ec949fb3f4ef1d58b96033e40df..47da0bbe8e69f4b0a44c878d614a89c2e550a7ca:/rpc.subproj/xdr.c diff --git a/rpc.subproj/xdr.c b/rpc.subproj/xdr.c index 227f601..2b167ea 100644 --- a/rpc.subproj/xdr.c +++ b/rpc.subproj/xdr.c @@ -21,6 +21,9 @@ * * @APPLE_LICENSE_HEADER_END@ */ + +/* $NetBSD: xdr.c,v 1.22 2000/07/06 03:10:35 christos Exp $ */ + /* * Sun RPC is a product of Sun Microsystems, Inc. and is provided for * unrestricted use provided that this legend is included on all tape @@ -28,33 +31,33 @@ * may copy or modify Sun RPC without charge, but are not authorized * to license or distribute it to anyone else except as part of a product or * program developed by the user. - * + * * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. - * + * * Sun RPC is provided with no support and without any obligation on the * part of Sun Microsystems, Inc. to assist in its use, correction, * modification or enhancement. - * + * * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC * OR ANY PART THEREOF. - * + * * In no event will Sun Microsystems, Inc. be liable for any lost revenue * or profits or other special, indirect and consequential damages, even if * Sun has been advised of the possibility of such damages. - * + * * Sun Microsystems, Inc. * 2550 Garcia Avenue * Mountain View, California 94043 */ #if defined(LIBC_SCCS) && !defined(lint) -/*static char *sccsid = "from: @(#)xdr.c 1.35 87/08/12";*/ -/*static char *sccsid = "from: @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC";*/ -static char *rcsid = "$Id: xdr.c,v 1.3 2002/02/19 20:36:26 epeyton Exp $"; +static char *sccsid = "@(#)xdr.c 1.35 87/08/12"; +static char *sccsid = "@(#)xdr.c 2.1 88/07/29 4.0 RPCSRC"; #endif +#include /* * xdr.c, Generic XDR routines implementation. @@ -66,6 +69,7 @@ static char *rcsid = "$Id: xdr.c,v 1.3 2002/02/19 20:36:26 epeyton Exp $"; * xdr. */ +#include #include #include #include @@ -73,17 +77,31 @@ static char *rcsid = "$Id: xdr.c,v 1.3 2002/02/19 20:36:26 epeyton Exp $"; #include #include +#ifdef __LP64__ +#define xdrlong_t int +#else +#define xdrlong_t long +#endif + +typedef quad_t longlong_t; /* ANSI long long type */ +typedef u_quad_t u_longlong_t; /* ANSI unsigned long long type */ + /* * constants specific to the xdr "protocol" */ +#ifdef __LP64__ +#define XDR_FALSE ((int) 0) +#define XDR_TRUE ((int) 1) +#else #define XDR_FALSE ((long) 0) #define XDR_TRUE ((long) 1) +#endif #define LASTUNSIGNED ((u_int) 0-1) /* * for unit alignment */ -static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 }; +static const char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 }; /* * Free a data structure using XDR @@ -92,11 +110,7 @@ static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 }; void xdr_free(proc, objp) xdrproc_t proc; -#if defined(__APPLE__) void *objp; -#else - char *objp; -#endif /* !NeXT */ { XDR x; @@ -108,14 +122,13 @@ xdr_free(proc, objp) * XDR nothing */ bool_t -xdr_void(/* xdrs, addr */) - /* XDR *xdrs; */ - /* caddr_t addr; */ +xdr_void(void) { return (TRUE); } + /* * XDR integers */ @@ -124,17 +137,26 @@ xdr_int(xdrs, ip) XDR *xdrs; int *ip; { + xdrlong_t l; -#ifdef lint - (void) (xdr_short(xdrs, (short *)ip)); - return (xdr_long(xdrs, (long *)ip)); -#else - if (sizeof (int) == sizeof (long)) { - return (xdr_long(xdrs, (long *)ip)); - } else { - return (xdr_short(xdrs, (short *)ip)); + switch (xdrs->x_op) { + + case XDR_ENCODE: + l = *ip; + return (XDR_PUTLONG(xdrs, (const xdrlong_t *)&l)); + + case XDR_DECODE: + if (!XDR_GETLONG(xdrs, &l)) { + return (FALSE); + } + *ip = l; + return (TRUE); + + case XDR_FREE: + return (TRUE); } -#endif + /* NOTREACHED */ + return (FALSE); } /* @@ -145,38 +167,51 @@ xdr_u_int(xdrs, up) XDR *xdrs; u_int *up; { + xdrlong_t l; -#ifdef lint - (void) (xdr_short(xdrs, (short *)up)); - return (xdr_u_long(xdrs, (u_long *)up)); -#else - if (sizeof (u_int) == sizeof (u_long)) { - return (xdr_u_long(xdrs, (u_long *)up)); - } else { - return (xdr_short(xdrs, (short *)up)); + switch (xdrs->x_op) { + + case XDR_ENCODE: + l = *up; + return (XDR_PUTLONG(xdrs, (const xdrlong_t *)&l)); + + case XDR_DECODE: + if (!XDR_GETLONG(xdrs, &l)) { + return (FALSE); + } + *up = l; + return (TRUE); + + case XDR_FREE: + return (TRUE); } -#endif + /* NOTREACHED */ + return (FALSE); } + /* * XDR long integers * same as xdr_u_long - open coded to save a proc call! */ bool_t xdr_long(xdrs, lp) - register XDR *xdrs; + XDR *xdrs; +#ifdef __LP64__ + int *lp; +#else long *lp; +#endif { - - if (xdrs->x_op == XDR_ENCODE) + switch (xdrs->x_op) { + case XDR_ENCODE: return (XDR_PUTLONG(xdrs, lp)); - - if (xdrs->x_op == XDR_DECODE) + case XDR_DECODE: return (XDR_GETLONG(xdrs, lp)); - - if (xdrs->x_op == XDR_FREE) + case XDR_FREE: return (TRUE); - + } + /* NOTREACHED */ return (FALSE); } @@ -186,45 +221,114 @@ xdr_long(xdrs, lp) */ bool_t xdr_u_long(xdrs, ulp) - register XDR *xdrs; + XDR *xdrs; +#ifdef __LP64__ + unsigned int *ulp; +#else u_long *ulp; +#endif +{ + switch (xdrs->x_op) { + case XDR_ENCODE: + return (XDR_PUTLONG(xdrs, (const xdrlong_t *)ulp)); + case XDR_DECODE: + return (XDR_GETLONG(xdrs, (xdrlong_t *)ulp)); + case XDR_FREE: + return (TRUE); + } + /* NOTREACHED */ + return (FALSE); +} + + +/* + * XDR 32-bit integers + * same as xdr_u_int32_t - open coded to save a proc call! + */ +bool_t +xdr_int32_t(xdrs, int32_p) + XDR *xdrs; + int32_t *int32_p; { + xdrlong_t l; + + switch (xdrs->x_op) { + + case XDR_ENCODE: + l = *int32_p; + return (XDR_PUTLONG(xdrs, (const xdrlong_t *)&l)); + + case XDR_DECODE: + if (!XDR_GETLONG(xdrs, &l)) { + return (FALSE); + } + *int32_p = l; + return (TRUE); - if (xdrs->x_op == XDR_DECODE) - return (XDR_GETLONG(xdrs, (long *)ulp)); - if (xdrs->x_op == XDR_ENCODE) - return (XDR_PUTLONG(xdrs, (long *)ulp)); - if (xdrs->x_op == XDR_FREE) + case XDR_FREE: return (TRUE); + } + /* NOTREACHED */ return (FALSE); } +/* + * XDR unsigned 32-bit integers + * same as xdr_int32_t - open coded to save a proc call! + */ +bool_t +xdr_u_int32_t(xdrs, u_int32_p) + XDR *xdrs; + u_int32_t *u_int32_p; +{ + u_int32_t l; + + switch (xdrs->x_op) { + + case XDR_ENCODE: + l = *u_int32_p; + return (XDR_PUTLONG(xdrs, (xdrlong_t *)&l)); + + case XDR_DECODE: + if (!XDR_GETLONG(xdrs, (xdrlong_t *)&l)) return (FALSE); + *u_int32_p = l; + return (TRUE); + + case XDR_FREE: + return (TRUE); + } + /* NOTREACHED */ + return (FALSE); +} + + /* * XDR short integers */ bool_t xdr_short(xdrs, sp) - register XDR *xdrs; + XDR *xdrs; short *sp; { - long l; + xdrlong_t l; switch (xdrs->x_op) { case XDR_ENCODE: - l = (long) *sp; - return (XDR_PUTLONG(xdrs, &l)); + l = *sp; + return (XDR_PUTLONG(xdrs, (const xdrlong_t *)&l)); case XDR_DECODE: if (!XDR_GETLONG(xdrs, &l)) { return (FALSE); } - *sp = (short) l; + *sp = l; return (TRUE); case XDR_FREE: return (TRUE); } + /* NOTREACHED */ return (FALSE); } @@ -233,27 +337,89 @@ xdr_short(xdrs, sp) */ bool_t xdr_u_short(xdrs, usp) - register XDR *xdrs; + XDR *xdrs; u_short *usp; { - u_long l; + xdrlong_t l; switch (xdrs->x_op) { case XDR_ENCODE: - l = (u_long) *usp; - return (XDR_PUTLONG(xdrs, &l)); + l = *usp; + return (XDR_PUTLONG(xdrs, (const xdrlong_t *)&l)); case XDR_DECODE: if (!XDR_GETLONG(xdrs, &l)) { return (FALSE); } - *usp = (u_short) l; + *usp = l; return (TRUE); case XDR_FREE: return (TRUE); } + /* NOTREACHED */ + return (FALSE); +} + + +/* + * XDR 16-bit integers + */ +bool_t +xdr_int16_t(xdrs, int16_p) + XDR *xdrs; + int16_t *int16_p; +{ + xdrlong_t l; + + switch (xdrs->x_op) { + + case XDR_ENCODE: + l = *int16_p; + return (XDR_PUTLONG(xdrs, (const xdrlong_t *)&l)); + + case XDR_DECODE: + if (!XDR_GETLONG(xdrs, &l)) { + return (FALSE); + } + *int16_p = l; + return (TRUE); + + case XDR_FREE: + return (TRUE); + } + /* NOTREACHED */ + return (FALSE); +} + +/* + * XDR unsigned 16-bit integers + */ +bool_t +xdr_u_int16_t(xdrs, u_int16_p) + XDR *xdrs; + u_int16_t *u_int16_p; +{ + xdrlong_t l; + + switch (xdrs->x_op) { + + case XDR_ENCODE: + l = *u_int16_p; + return (XDR_PUTLONG(xdrs, (const xdrlong_t *)&l)); + + case XDR_DECODE: + if (!XDR_GETLONG(xdrs, &l)) { + return (FALSE); + } + *u_int16_p = l; + return (TRUE); + + case XDR_FREE: + return (TRUE); + } + /* NOTREACHED */ return (FALSE); } @@ -284,7 +450,7 @@ xdr_u_char(xdrs, cp) XDR *xdrs; u_char *cp; { - u_int u; + u_int32_t u; u = (*cp); if (!xdr_u_int(xdrs, &u)) { @@ -299,16 +465,16 @@ xdr_u_char(xdrs, cp) */ bool_t xdr_bool(xdrs, bp) - register XDR *xdrs; + XDR *xdrs; bool_t *bp; { - long lb; + xdrlong_t lb; switch (xdrs->x_op) { case XDR_ENCODE: lb = *bp ? XDR_TRUE : XDR_FALSE; - return (XDR_PUTLONG(xdrs, &lb)); + return (XDR_PUTLONG(xdrs, (const xdrlong_t *)&lb)); case XDR_DECODE: if (!XDR_GETLONG(xdrs, &lb)) { @@ -320,6 +486,7 @@ xdr_bool(xdrs, bp) case XDR_FREE: return (TRUE); } + /* NOTREACHED */ return (FALSE); } @@ -331,23 +498,20 @@ xdr_enum(xdrs, ep) XDR *xdrs; enum_t *ep; { -#ifndef lint enum sizecheck { SIZEVAL }; /* used to find the size of an enum */ /* * enums are treated as ints */ - if (sizeof (enum sizecheck) == sizeof (long)) { - return (xdr_long(xdrs, (long *)ep)); - } else if (sizeof (enum sizecheck) == sizeof (short)) { - return (xdr_short(xdrs, (short *)ep)); + /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) { + return (xdr_long(xdrs, (xdrlong_t *)(void *)ep)); + } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) { + return (xdr_int(xdrs, (int *)(void *)ep)); + } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (short)) { + return (xdr_short(xdrs, (short *)(void *)ep)); } else { return (FALSE); } -#else - (void) (xdr_short(xdrs, (short *)ep)); - return (xdr_long(xdrs, (long *)ep)); -#endif } /* @@ -357,12 +521,12 @@ xdr_enum(xdrs, ep) */ bool_t xdr_opaque(xdrs, cp, cnt) - register XDR *xdrs; + XDR *xdrs; caddr_t cp; - register u_int cnt; + u_int cnt; { - register u_int rndup; - static char crud[BYTES_PER_XDR_UNIT]; + u_int32_t rndup; + u_int8_t crud[BYTES_PER_XDR_UNIT]; /* * if no data we are done @@ -383,7 +547,7 @@ xdr_opaque(xdrs, cp, cnt) } if (rndup == 0) return (TRUE); - return (XDR_GETBYTES(xdrs, crud, rndup)); + return (XDR_GETBYTES(xdrs, (caddr_t)(void *)crud, rndup)); } if (xdrs->x_op == XDR_ENCODE) { @@ -409,13 +573,13 @@ xdr_opaque(xdrs, cp, cnt) */ bool_t xdr_bytes(xdrs, cpp, sizep, maxsize) - register XDR *xdrs; + XDR *xdrs; char **cpp; - register u_int *sizep; + u_int *sizep; u_int maxsize; { - register char *sp = *cpp; /* sp is the actual string pointer */ - register u_int nodesize; + char *sp = *cpp; /* sp is the actual string pointer */ + u_int32_t nodesize; /* * first deal with the length since xdr bytes are counted @@ -438,13 +602,13 @@ xdr_bytes(xdrs, cpp, sizep, maxsize) return (TRUE); } if (sp == NULL) { - *cpp = sp = (char *)mem_alloc(nodesize); + *cpp = sp = mem_alloc(nodesize); } if (sp == NULL) { - (void) fprintf(stderr, "xdr_bytes: out of memory\n"); + warnx("xdr_bytes: out of memory"); return (FALSE); } - /* fall into ... */ + /* FALLTHROUGH */ case XDR_ENCODE: return (xdr_opaque(xdrs, sp, nodesize)); @@ -456,6 +620,7 @@ xdr_bytes(xdrs, cpp, sizep, maxsize) } return (TRUE); } + /* NOTREACHED */ return (FALSE); } @@ -484,13 +649,13 @@ xdr_netobj(xdrs, np) */ bool_t xdr_union(xdrs, dscmp, unp, choices, dfault) - register XDR *xdrs; + XDR *xdrs; enum_t *dscmp; /* enum to decide which arm to work on */ char *unp; /* the union itself */ - struct xdr_discrim *choices; /* [value, xdr proc] for each arm */ + const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */ xdrproc_t dfault; /* default xdr routine */ { - register enum_t dscm; + enum_t dscm; /* * we deal with the discriminator; it's an enum @@ -506,14 +671,14 @@ xdr_union(xdrs, dscmp, unp, choices, dfault) */ for (; choices->proc != NULL_xdrproc_t; choices++) { if (choices->value == dscm) - return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED)); + return ((*(choices->proc))(xdrs, unp)); } /* * no match - execute the default xdr routine if there is one */ return ((dfault == NULL_xdrproc_t) ? FALSE : - (*dfault)(xdrs, unp, LASTUNSIGNED)); + (*dfault)(xdrs, unp)); } @@ -533,13 +698,13 @@ xdr_union(xdrs, dscmp, unp, choices, dfault) */ bool_t xdr_string(xdrs, cpp, maxsize) - register XDR *xdrs; + XDR *xdrs; char **cpp; u_int maxsize; { - register char *sp = *cpp; /* sp is the actual string pointer */ - u_int size; - u_int nodesize; + char *sp = *cpp; /* sp is the actual string pointer */ + u_int32_t size; + u_int32_t nodesize; /* * first deal with the length since xdr strings are counted-strings @@ -549,10 +714,12 @@ xdr_string(xdrs, cpp, maxsize) if (sp == NULL) { return(TRUE); /* already free */ } - /* fall through... */ + /* FALLTHROUGH */ case XDR_ENCODE: size = strlen(sp); break; + case XDR_DECODE: + break; } if (! xdr_u_int(xdrs, &size)) { return (FALSE); @@ -572,13 +739,13 @@ xdr_string(xdrs, cpp, maxsize) return (TRUE); } if (sp == NULL) - *cpp = sp = (char *)mem_alloc(nodesize); + *cpp = sp = mem_alloc(nodesize); if (sp == NULL) { - (void) fprintf(stderr, "xdr_string: out of memory\n"); + warnx("xdr_string: out of memory"); return (FALSE); } sp[size] = 0; - /* fall into ... */ + /* FALLTHROUGH */ case XDR_ENCODE: return (xdr_opaque(xdrs, sp, size)); @@ -588,6 +755,7 @@ xdr_string(xdrs, cpp, maxsize) *cpp = NULL; return (TRUE); } + /* NOTREACHED */ return (FALSE); } @@ -600,8 +768,143 @@ xdr_wrapstring(xdrs, cpp) XDR *xdrs; char **cpp; { - if (xdr_string(xdrs, cpp, LASTUNSIGNED)) { + return xdr_string(xdrs, cpp, LASTUNSIGNED); +} + +/* + * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t() + * are in the "non-portable" section because they require that a `long long' + * be a 64-bit type. + * + * --thorpej@netbsd.org, November 30, 1999 + */ + +/* + * XDR 64-bit integers + */ +bool_t +xdr_int64_t(xdrs, llp) + XDR *xdrs; + int64_t *llp; +{ + u_int32_t ul[2]; + + switch (xdrs->x_op) { + case XDR_ENCODE: + ul[0] = (u_int32_t)((u_int64_t)*llp >> 32) & 0xffffffff; + ul[1] = (u_int32_t)((u_int64_t)*llp) & 0xffffffff; + if (XDR_PUTLONG(xdrs, (xdrlong_t *)&ul[0]) == FALSE) + return (FALSE); + return (XDR_PUTLONG(xdrs, (xdrlong_t *)&ul[1])); + case XDR_DECODE: + if (XDR_GETLONG(xdrs, (xdrlong_t *)&ul[0]) == FALSE) + return (FALSE); + if (XDR_GETLONG(xdrs, (xdrlong_t *)&ul[1]) == FALSE) + return (FALSE); + *llp = (int64_t) + (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1])); + return (TRUE); + case XDR_FREE: return (TRUE); } + /* NOTREACHED */ return (FALSE); } + + +/* + * XDR unsigned 64-bit integers + */ +bool_t +xdr_u_int64_t(xdrs, ullp) + XDR *xdrs; + u_int64_t *ullp; +{ + u_int32_t ul[2]; + + switch (xdrs->x_op) { + case XDR_ENCODE: + ul[0] = (u_int32_t)(*ullp >> 32) & 0xffffffff; + ul[1] = (u_int32_t)(*ullp) & 0xffffffff; + if (XDR_PUTLONG(xdrs, (xdrlong_t *)&ul[0]) == FALSE) + return (FALSE); + return (XDR_PUTLONG(xdrs, (xdrlong_t *)&ul[1])); + case XDR_DECODE: + if (XDR_GETLONG(xdrs, (xdrlong_t *)&ul[0]) == FALSE) + return (FALSE); + if (XDR_GETLONG(xdrs, (xdrlong_t *)&ul[1]) == FALSE) + return (FALSE); + *ullp = (u_int64_t) + (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1])); + return (TRUE); + case XDR_FREE: + return (TRUE); + } + /* NOTREACHED */ + return (FALSE); +} + + +/* + * XDR hypers + */ +bool_t +xdr_hyper(xdrs, llp) + XDR *xdrs; + longlong_t *llp; +{ + /* + * Don't bother open-coding this; it's a fair amount of code. Just + * call xdr_int64_t(). + */ + return (xdr_int64_t(xdrs, (int64_t *)llp)); +} + + +/* + * XDR unsigned hypers + */ +bool_t +xdr_u_hyper(xdrs, ullp) + XDR *xdrs; + u_longlong_t *ullp; +{ + /* + * Don't bother open-coding this; it's a fair amount of code. Just + * call xdr_u_int64_t(). + */ + return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp)); +} + + +/* + * XDR longlong_t's + */ +bool_t +xdr_longlong_t(xdrs, llp) + XDR *xdrs; + longlong_t *llp; +{ + /* + * Don't bother open-coding this; it's a fair amount of code. Just + * call xdr_int64_t(). + */ + return (xdr_int64_t(xdrs, (int64_t *)llp)); +} + + +/* + * XDR u_longlong_t's + */ +bool_t +xdr_u_longlong_t(xdrs, ullp) + XDR *xdrs; + u_longlong_t *ullp; +{ + + /* + * Don't bother open-coding this; it's a fair amount of code. Just + * call xdr_u_int64_t(). + */ + return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp)); +}