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
55 // eventually we might be able to support autoconf via cygwin...
56 #if defined (_MSC_VER) || defined(__BORLANDC__)
57 # include "config_win32.h"
71 #include <rpc/types.h>
75 * constants specific to the xdr "protocol"
77 #define XDR_FALSE ((long) 0)
78 #define XDR_TRUE ((long) 1)
79 #define LASTUNSIGNED ((u_int) 0-1)
84 static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
87 * Free a data structure using XDR
88 * Not a filter, but a convenient utility nonetheless
105 xdr_void(/* xdrs, addr */)
123 (void) (xdr_short(xdrs, (short *)ip));
124 return (xdr_long(xdrs, (long *)ip));
126 if (sizeof (int) == sizeof (long)) {
127 return (xdr_long(xdrs, (long *)ip));
129 return (xdr_short(xdrs, (short *)ip));
135 * XDR unsigned integers
144 (void) (xdr_short(xdrs, (short *)up));
145 return (xdr_u_long(xdrs, (u_long *)up));
147 if (sizeof (u_int) == sizeof (u_long)) {
148 return (xdr_u_long(xdrs, (u_long *)up));
150 return (xdr_short(xdrs, (short *)up));
156 * XDR 64-bit integers
159 xdr_int64_t(xdrs, ip)
163 if (sizeof (int64_t) <= sizeof (long)) {
164 return (xdr_long(xdrs, (long *)ip));
166 /* Assumes sizeof(long) == 4 */
170 switch (xdrs->x_op) {
172 t1 = (int32_t) ((*ip) >> 32);
173 t2 = (int32_t) (*ip);
174 return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
177 if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
179 *ip = ((int64_t) t1) << 32;
193 * XDR unsigned 64-bit integers
196 xdr_uint64_t(xdrs, up)
200 if (sizeof (int64_t) <= sizeof (long)) {
201 return (xdr_long(xdrs, (long *)up));
203 /* Assumes sizeof(long) == 4 */
207 switch (xdrs->x_op) {
209 t1 = (uint32_t) ((*up) >> 32);
210 t2 = (uint32_t) (*up);
211 return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
214 if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
216 *up = ((uint64_t) t1) << 32;
231 * same as xdr_u_long - open coded to save a proc call!
239 if (xdrs->x_op == XDR_ENCODE)
240 return (XDR_PUTLONG(xdrs, lp));
242 if (xdrs->x_op == XDR_DECODE)
243 return (XDR_GETLONG(xdrs, lp));
245 if (xdrs->x_op == XDR_FREE)
252 * XDR unsigned long integers
253 * same as xdr_long - open coded to save a proc call!
256 xdr_u_long(xdrs, ulp)
261 if (xdrs->x_op == XDR_DECODE)
262 return (XDR_GETLONG(xdrs, (long *)ulp));
263 if (xdrs->x_op == XDR_ENCODE)
264 return (XDR_PUTLONG(xdrs, (long *)ulp));
265 if (xdrs->x_op == XDR_FREE)
280 switch (xdrs->x_op) {
284 return (XDR_PUTLONG(xdrs, &l));
287 if (!XDR_GETLONG(xdrs, &l)) {
300 * XDR unsigned short integers
303 xdr_u_short(xdrs, usp)
309 switch (xdrs->x_op) {
313 return (XDR_PUTLONG(xdrs, &l));
316 if (!XDR_GETLONG(xdrs, &l)) {
340 if (!xdr_int(xdrs, &i)) {
348 * XDR an unsigned char
358 if (!xdr_u_int(xdrs, &u)) {
375 switch (xdrs->x_op) {
378 lb = *bp ? XDR_TRUE : XDR_FALSE;
379 return (XDR_PUTLONG(xdrs, &lb));
382 if (!XDR_GETLONG(xdrs, &lb)) {
385 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
403 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
406 * enums are treated as ints
408 if (sizeof (enum sizecheck) == sizeof (long)) {
409 return (xdr_long(xdrs, (long *)ep));
410 } else if (sizeof (enum sizecheck) == sizeof (short)) {
411 return (xdr_short(xdrs, (short *)ep));
416 (void) (xdr_short(xdrs, (short *)ep));
417 return (xdr_long(xdrs, (long *)ep));
423 * Allows the specification of a fixed size sequence of opaque bytes.
424 * cp points to the opaque object and cnt gives the byte length.
427 xdr_opaque(xdrs, cp, cnt)
432 register u_int rndup;
433 static crud[BYTES_PER_XDR_UNIT];
436 * if no data we are done
442 * round byte count to full xdr units
444 rndup = cnt % BYTES_PER_XDR_UNIT;
446 rndup = BYTES_PER_XDR_UNIT - rndup;
448 if (xdrs->x_op == XDR_DECODE) {
449 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
454 return (XDR_GETBYTES(xdrs, crud, rndup));
457 if (xdrs->x_op == XDR_ENCODE) {
458 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
463 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
466 if (xdrs->x_op == XDR_FREE) {
475 * *cpp is a pointer to the bytes, *sizep is the count.
476 * If *cpp is NULL maxsize bytes are allocated
479 xdr_bytes(xdrs, cpp, sizep, maxsize)
482 register u_int *sizep;
485 register char *sp = *cpp; /* sp is the actual string pointer */
486 register u_int nodesize;
489 * first deal with the length since xdr bytes are counted
491 if (! xdr_u_int(xdrs, sizep)) {
495 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
500 * now deal with the actual bytes
502 switch (xdrs->x_op) {
509 *cpp = sp = (char *)mem_alloc(nodesize);
513 nt_rpc_report("xdr_bytes: out of memory\n");
515 (void) fprintf(stderr, "xdr_bytes: out of memory\n");
522 return (xdr_opaque(xdrs, sp, nodesize));
526 mem_free(sp, nodesize);
535 * Implemented here due to commonality of the object.
543 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
547 * XDR a descriminated union
548 * Support routine for discriminated unions.
549 * You create an array of xdrdiscrim structures, terminated with
550 * an entry with a null procedure pointer. The routine gets
551 * the discriminant value and then searches the array of xdrdiscrims
552 * looking for that value. It calls the procedure given in the xdrdiscrim
553 * to handle the discriminant. If there is no specific routine a default
554 * routine may be called.
555 * If there is no specific or default routine an error is returned.
558 xdr_union(xdrs, dscmp, unp, choices, dfault)
560 enum_t *dscmp; /* enum to decide which arm to work on */
561 char *unp; /* the union itself */
562 struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
563 xdrproc_t dfault; /* default xdr routine */
565 register enum_t dscm;
568 * we deal with the discriminator; it's an enum
570 if (! xdr_enum(xdrs, dscmp)) {
576 * search choices for a value that matches the discriminator.
577 * if we find one, execute the xdr routine for that value.
579 for (; choices->proc != NULL_xdrproc_t; choices++) {
580 if (choices->value == dscm)
581 return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
585 * no match - execute the default xdr routine if there is one
587 return ((dfault == NULL_xdrproc_t) ? FALSE :
588 (*dfault)(xdrs, unp, LASTUNSIGNED));
593 * Non-portable xdr primitives.
594 * Care should be taken when moving these routines to new architectures.
599 * XDR null terminated ASCII strings
600 * xdr_string deals with "C strings" - arrays of bytes that are
601 * terminated by a NULL character. The parameter cpp references a
602 * pointer to storage; If the pointer is null, then the necessary
603 * storage is allocated. The last parameter is the max allowed length
604 * of the string as specified by a protocol.
607 xdr_string(xdrs, cpp, maxsize)
612 register char *sp = *cpp; /* sp is the actual string pointer */
617 * first deal with the length since xdr strings are counted-strings
619 switch (xdrs->x_op) {
622 return(TRUE); /* already free */
624 /* fall through... */
629 if (! xdr_u_int(xdrs, &size)) {
632 if (size > maxsize) {
638 * now deal with the actual bytes
640 switch (xdrs->x_op) {
647 *cpp = sp = (char *)mem_alloc(nodesize);
650 nt_rpc_report("xdr_string: out of memory\n");
652 (void) fprintf(stderr, "xdr_string: out of memory\n");
660 return (xdr_opaque(xdrs, sp, size));
663 mem_free(sp, nodesize);
671 * Wrapper for xdr_string that can be called directly from
672 * routines like clnt_call
675 xdr_wrapstring(xdrs, cpp)
679 if (xdr_string(xdrs, cpp, LASTUNSIGNED)) {