1 /*********************************************************************
2 * RPC for the Windows NT Operating System
3 * 1993 by Martin F. Gergeleit
4 * Users may use, copy or modify Sun RPC for the Windows NT Operating
5 * System according to the Sun copyright below.
7 * RPC for the Windows NT Operating System COMES WITH ABSOLUTELY NO
8 * WARRANTY, NOR WILL I BE LIABLE FOR ANY DAMAGES INCURRED FROM THE
9 * USE OF. USE ENTIRELY AT YOUR OWN RISK!!!
10 *********************************************************************/
12 /* @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC */
14 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
15 * unrestricted use provided that this legend is included on all tape
16 * media and as a part of the software program in whole or part. Users
17 * may copy or modify Sun RPC without charge, but are not authorized
18 * to license or distribute it to anyone else except as part of a product or
19 * program developed by the user.
21 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
22 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
25 * Sun RPC is provided with no support and without any obligation on the
26 * part of Sun Microsystems, Inc. to assist in its use, correction,
27 * modification or enhancement.
29 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
30 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
31 * OR ANY PART THEREOF.
33 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
34 * or profits or other special, indirect and consequential damages, even if
35 * Sun has been advised of the possibility of such damages.
37 * Sun Microsystems, Inc.
39 * Mountain View, California 94043
41 #if !defined(lint) && defined(SCCSIDS)
42 static char sccsid[] = "@(#)xdr.c 1.35 87/08/12";
46 * xdr.c, Generic XDR routines implementation.
48 * Copyright (C) 1986, Sun Microsystems, Inc.
50 * These are the "generic" xdr routines used to serialize and de-serialize
51 * most common data items. See xdr.h for more info on the interface to
58 #include <rpc/types.h>
62 * constants specific to the xdr "protocol"
64 #define XDR_FALSE ((long) 0)
65 #define XDR_TRUE ((long) 1)
66 #define LASTUNSIGNED ((u_int) 0-1)
71 static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
74 * Free a data structure using XDR
75 * Not a filter, but a convenient utility nonetheless
92 xdr_void(/* xdrs, addr */)
110 (void) (xdr_short(xdrs, (short *)ip));
111 return (xdr_long(xdrs, (long *)ip));
113 if (sizeof (int) == sizeof (long)) {
114 return (xdr_long(xdrs, (long *)ip));
116 return (xdr_short(xdrs, (short *)ip));
122 * XDR unsigned integers
131 (void) (xdr_short(xdrs, (short *)up));
132 return (xdr_u_long(xdrs, (u_long *)up));
134 if (sizeof (u_int) == sizeof (u_long)) {
135 return (xdr_u_long(xdrs, (u_long *)up));
137 return (xdr_short(xdrs, (short *)up));
144 * same as xdr_u_long - open coded to save a proc call!
152 if (xdrs->x_op == XDR_ENCODE)
153 return (XDR_PUTLONG(xdrs, lp));
155 if (xdrs->x_op == XDR_DECODE)
156 return (XDR_GETLONG(xdrs, lp));
158 if (xdrs->x_op == XDR_FREE)
165 * XDR unsigned long integers
166 * same as xdr_long - open coded to save a proc call!
169 xdr_u_long(xdrs, ulp)
174 if (xdrs->x_op == XDR_DECODE)
175 return (XDR_GETLONG(xdrs, (long *)ulp));
176 if (xdrs->x_op == XDR_ENCODE)
177 return (XDR_PUTLONG(xdrs, (long *)ulp));
178 if (xdrs->x_op == XDR_FREE)
193 switch (xdrs->x_op) {
197 return (XDR_PUTLONG(xdrs, &l));
200 if (!XDR_GETLONG(xdrs, &l)) {
213 * XDR unsigned short integers
216 xdr_u_short(xdrs, usp)
222 switch (xdrs->x_op) {
226 return (XDR_PUTLONG(xdrs, &l));
229 if (!XDR_GETLONG(xdrs, &l)) {
253 if (!xdr_int(xdrs, &i)) {
261 * XDR an unsigned char
271 if (!xdr_u_int(xdrs, &u)) {
288 switch (xdrs->x_op) {
291 lb = *bp ? XDR_TRUE : XDR_FALSE;
292 return (XDR_PUTLONG(xdrs, &lb));
295 if (!XDR_GETLONG(xdrs, &lb)) {
298 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
316 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
319 * enums are treated as ints
321 if (sizeof (enum sizecheck) == sizeof (long)) {
322 return (xdr_long(xdrs, (long *)ep));
323 } else if (sizeof (enum sizecheck) == sizeof (short)) {
324 return (xdr_short(xdrs, (short *)ep));
329 (void) (xdr_short(xdrs, (short *)ep));
330 return (xdr_long(xdrs, (long *)ep));
336 * Allows the specification of a fixed size sequence of opaque bytes.
337 * cp points to the opaque object and cnt gives the byte length.
340 xdr_opaque(xdrs, cp, cnt)
345 register u_int rndup;
346 static crud[BYTES_PER_XDR_UNIT];
349 * if no data we are done
355 * round byte count to full xdr units
357 rndup = cnt % BYTES_PER_XDR_UNIT;
359 rndup = BYTES_PER_XDR_UNIT - rndup;
361 if (xdrs->x_op == XDR_DECODE) {
362 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
367 return (XDR_GETBYTES(xdrs, crud, rndup));
370 if (xdrs->x_op == XDR_ENCODE) {
371 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
376 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
379 if (xdrs->x_op == XDR_FREE) {
388 * *cpp is a pointer to the bytes, *sizep is the count.
389 * If *cpp is NULL maxsize bytes are allocated
392 xdr_bytes(xdrs, cpp, sizep, maxsize)
395 register u_int *sizep;
398 register char *sp = *cpp; /* sp is the actual string pointer */
399 register u_int nodesize;
402 * first deal with the length since xdr bytes are counted
404 if (! xdr_u_int(xdrs, sizep)) {
408 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
413 * now deal with the actual bytes
415 switch (xdrs->x_op) {
422 *cpp = sp = (char *)mem_alloc(nodesize);
426 nt_rpc_report("xdr_bytes: out of memory\n");
428 (void) fprintf(stderr, "xdr_bytes: out of memory\n");
435 return (xdr_opaque(xdrs, sp, nodesize));
439 mem_free(sp, nodesize);
448 * Implemented here due to commonality of the object.
456 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
460 * XDR a descriminated union
461 * Support routine for discriminated unions.
462 * You create an array of xdrdiscrim structures, terminated with
463 * an entry with a null procedure pointer. The routine gets
464 * the discriminant value and then searches the array of xdrdiscrims
465 * looking for that value. It calls the procedure given in the xdrdiscrim
466 * to handle the discriminant. If there is no specific routine a default
467 * routine may be called.
468 * If there is no specific or default routine an error is returned.
471 xdr_union(xdrs, dscmp, unp, choices, dfault)
473 enum_t *dscmp; /* enum to decide which arm to work on */
474 char *unp; /* the union itself */
475 struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
476 xdrproc_t dfault; /* default xdr routine */
478 register enum_t dscm;
481 * we deal with the discriminator; it's an enum
483 if (! xdr_enum(xdrs, dscmp)) {
489 * search choices for a value that matches the discriminator.
490 * if we find one, execute the xdr routine for that value.
492 for (; choices->proc != NULL_xdrproc_t; choices++) {
493 if (choices->value == dscm)
494 return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
498 * no match - execute the default xdr routine if there is one
500 return ((dfault == NULL_xdrproc_t) ? FALSE :
501 (*dfault)(xdrs, unp, LASTUNSIGNED));
506 * Non-portable xdr primitives.
507 * Care should be taken when moving these routines to new architectures.
512 * XDR null terminated ASCII strings
513 * xdr_string deals with "C strings" - arrays of bytes that are
514 * terminated by a NULL character. The parameter cpp references a
515 * pointer to storage; If the pointer is null, then the necessary
516 * storage is allocated. The last parameter is the max allowed length
517 * of the string as specified by a protocol.
520 xdr_string(xdrs, cpp, maxsize)
525 register char *sp = *cpp; /* sp is the actual string pointer */
530 * first deal with the length since xdr strings are counted-strings
532 switch (xdrs->x_op) {
535 return(TRUE); /* already free */
537 /* fall through... */
542 if (! xdr_u_int(xdrs, &size)) {
545 if (size > maxsize) {
551 * now deal with the actual bytes
553 switch (xdrs->x_op) {
560 *cpp = sp = (char *)mem_alloc(nodesize);
563 nt_rpc_report("xdr_string: out of memory\n");
565 (void) fprintf(stderr, "xdr_string: out of memory\n");
573 return (xdr_opaque(xdrs, sp, size));
576 mem_free(sp, nodesize);
584 * Wrapper for xdr_string that can be called directly from
585 * routines like clnt_call
588 xdr_wrapstring(xdrs, cpp)
592 if (xdr_string(xdrs, cpp, LASTUNSIGNED)) {