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>
41 * xdr.c, Generic XDR routines implementation.
43 * Copyright (C) 1984, Sun Microsystems, Inc.
45 * These are the "generic" xdr routines used to serialize and de-serialize
46 * most common data items. See xdr.h for more info on the interface to
53 #include <sys/param.h>
54 #ifndef AFS_LINUX20_ENV
55 #include <sys/systm.h>
64 * constants specific to the xdr "protocol"
66 #define XDR_FALSE ((afs_int32) 0)
67 #define XDR_TRUE ((afs_int32) 1)
68 #define LASTUNSIGNED ((u_int) 0-1)
88 xdr_int(register XDR * xdrs, int *ip)
96 return (XDR_PUTINT32(xdrs, &l));
99 if (!XDR_GETINT32(xdrs, &l)) {
112 * XDR unsigned integers
115 xdr_u_int(register XDR * xdrs, u_int * uip)
119 switch (xdrs->x_op) {
122 l = (afs_uint32) * uip;
123 return (XDR_PUTINT32(xdrs, &l));
126 if (!XDR_GETINT32(xdrs, &l)) {
143 xdr_long(register XDR * xdrs, long *lp)
147 switch (xdrs->x_op) {
150 l = (afs_int32) * lp;
151 return (XDR_PUTINT32(xdrs, &l));
154 if (!XDR_GETINT32(xdrs, &l)) {
167 * XDR unsigned long integers
170 xdr_u_long(register XDR * xdrs, u_long * ulp)
174 switch (xdrs->x_op) {
177 l = (afs_uint32) * ulp;
178 return (XDR_PUTINT32(xdrs, &l));
181 if (!XDR_GETINT32(xdrs, &l)) {
198 xdr_char(register XDR * xdrs, char *sp)
202 switch (xdrs->x_op) {
205 l = (afs_int32) * sp;
206 return (XDR_PUTINT32(xdrs, &l));
209 if (!XDR_GETINT32(xdrs, &l)) {
225 xdr_u_char(register XDR * xdrs, u_char * usp)
229 switch (xdrs->x_op) {
232 l = (afs_uint32) * usp;
233 return (XDR_PUTINT32(xdrs, &l));
236 if (!XDR_GETINT32(xdrs, &l)) {
253 xdr_short(register XDR * xdrs, short *sp)
257 switch (xdrs->x_op) {
260 l = (afs_int32) * sp;
261 return (XDR_PUTINT32(xdrs, &l));
264 if (!XDR_GETINT32(xdrs, &l)) {
277 * XDR unsigned short integers
280 xdr_u_short(register XDR * xdrs, u_short * usp)
284 switch (xdrs->x_op) {
287 l = (afs_uint32) * usp;
288 return (XDR_PUTINT32(xdrs, &l));
291 if (!XDR_GETINT32(xdrs, &l)) {
308 xdr_bool(register XDR * xdrs, bool_t * bp)
312 switch (xdrs->x_op) {
315 lb = *bp ? XDR_TRUE : XDR_FALSE;
316 return (XDR_PUTINT32(xdrs, &lb));
319 if (!XDR_GETINT32(xdrs, &lb)) {
322 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
335 xdr_enum(register XDR * xdrs, enum_t * ep)
337 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
340 * enums are treated as ints
343 return (xdr_long(xdrs, (long *)ep));
349 * Allows the specification of a fixed size sequence of opaque bytes.
350 * cp points to the opaque object and cnt gives the byte length.
353 xdr_opaque(register XDR * xdrs, caddr_t cp, register u_int cnt)
355 register u_int rndup;
356 int crud[BYTES_PER_XDR_UNIT];
357 char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
360 * if no data we are done
366 * round byte count to full xdr units
368 rndup = cnt % BYTES_PER_XDR_UNIT;
370 rndup = BYTES_PER_XDR_UNIT - rndup;
372 if (xdrs->x_op == XDR_DECODE) {
373 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
378 return (XDR_GETBYTES(xdrs, (caddr_t)crud, rndup));
381 if (xdrs->x_op == XDR_ENCODE) {
382 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
387 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
390 if (xdrs->x_op == XDR_FREE) {
399 * *cpp is a pointer to the bytes, *sizep is the count.
400 * If *cpp is NULL maxsize bytes are allocated
403 xdr_bytes(register XDR * xdrs, char **cpp, register u_int * sizep,
406 register char *sp = *cpp; /* sp is the actual string pointer */
407 register u_int nodesize;
410 * first deal with the length since xdr bytes are counted
412 if (!xdr_u_int(xdrs, sizep)) {
416 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
421 * now deal with the actual bytes
423 switch (xdrs->x_op) {
427 *cpp = sp = (char *)osi_alloc(nodesize);
435 return (xdr_opaque(xdrs, sp, nodesize));
439 osi_free(sp, nodesize);
448 * XDR a descriminated union
449 * Support routine for discriminated unions.
450 * You create an array of xdrdiscrim structures, terminated with
451 * an entry with a null procedure pointer. The routine gets
452 * the discriminant value and then searches the array of xdrdiscrims
453 * looking for that value. It calls the procedure given in the xdrdiscrim
454 * to handle the discriminant. If there is no specific routine a default
455 * routine may be called.
456 * If there is no specific or default routine an error is returned.
459 enum_t *dscmp; * enum to decide which arm to work on *
460 caddr_t unp; * the union itself *
461 struct xdr_discrim *choices; * [value, xdr proc] for each arm *
462 xdrproc_t dfault; * default xdr routine *
465 xdr_union(register XDR * xdrs, enum_t * dscmp, caddr_t unp,
466 struct xdr_discrim * choices, xdrproc_t dfault)
468 register enum_t dscm;
471 * we deal with the discriminator; it's an enum
473 if (!xdr_enum(xdrs, dscmp)) {
479 * search choices for a value that matches the discriminator.
480 * if we find one, execute the xdr routine for that value.
482 for (; choices->proc != NULL_xdrproc_t; choices++) {
483 if (choices->value == dscm)
484 return ((*(choices->proc)) (xdrs, unp, LASTUNSIGNED));
488 * no match - execute the default xdr routine if there is one
490 return ((dfault == NULL_xdrproc_t) ? FALSE : (*dfault) (xdrs, unp,
496 * Non-portable xdr primitives.
497 * Care should be taken when moving these routines to new architectures.
502 * XDR null terminated ASCII strings
503 * xdr_string deals with "C strings" - arrays of bytes that are
504 * terminated by a NULL character. The parameter cpp references a
505 * pointer to storage; If the pointer is null, then the necessary
506 * storage is allocated. The last parameter is the max allowed length
507 * of the string as specified by a protocol.
510 xdr_string(register XDR * xdrs, char **cpp, u_int maxsize)
512 register char *sp = *cpp; /* sp is the actual string pointer */
516 /* FIXME: this does not look correct: MSVC 6 computes -2 here */
517 if (maxsize > ((~0) >> 1) - 1)
518 maxsize = ((~0) >> 1) - 1;
521 * first deal with the length since xdr strings are counted-strings
523 switch (xdrs->x_op) {
526 return (TRUE); /* already free */
536 if (!xdr_u_int(xdrs, &size)) {
539 if (size > maxsize) {
545 * now deal with the actual bytes
547 switch (xdrs->x_op) {
551 *cpp = sp = (char *)osi_alloc(nodesize);
559 return (xdr_opaque(xdrs, sp, size));
563 osi_free(sp, nodesize);
572 * Wrapper for xdr_string that can be called directly from
573 * routines like clnt_call
577 xdr_wrapstring(register XDR * xdrs, char **cpp)
579 if (xdr_string(xdrs, cpp, BUFSIZ)) {