1 /* @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC */
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid
[] = "@(#)xdr.c 1.35 87/08/12";
35 * xdr.c, Generic XDR routines implementation.
37 * Copyright (C) 1986, Sun Microsystems, Inc.
39 * These are the "generic" xdr routines used to serialize and de-serialize
40 * most common data items. See xdr.h for more info on the interface to
47 #include <rpc/types.h>
51 * constants specific to the xdr "protocol"
53 #define XDR_FALSE ((long) 0)
54 #define XDR_TRUE ((long) 1)
55 #define LASTUNSIGNED ((u_int) 0-1)
60 static char xdr_zero
[BYTES_PER_XDR_UNIT
] = { 0, 0, 0, 0 };
63 * Free a data structure using XDR
64 * Not a filter, but a convenient utility nonetheless
81 xdr_void(/* xdrs, addr */)
99 (void) (xdr_short(xdrs
, (short *)ip
));
100 return (xdr_long(xdrs
, (long *)ip
));
102 if (sizeof (int) < sizeof (long)) {
105 switch (xdrs
->x_op
) {
108 return XDR_PUTLONG(xdrs
, &l
);
111 if (!XDR_GETLONG(xdrs
, &l
)) {
117 } else if (sizeof (int) == sizeof (long)) {
118 return (xdr_long(xdrs
, (long *)ip
));
119 } else if (sizeof (int) == sizeof (short)) {
120 return (xdr_short(xdrs
, (short *)ip
));
122 abort (); /* roland@gnu */
124 /* force unresolved reference (link-time error): */
125 extern unexpected_sizes_in_xdr_int ();
127 unexpected_sizes_in_xdr_int();
134 * XDR unsigned integers
142 (void) (xdr_short(xdrs
, (short *)up
));
143 return (xdr_u_long(xdrs
, (u_long
*)up
));
145 if (sizeof (u_int
) < sizeof (u_long
)) {
148 switch (xdrs
->x_op
) {
151 return XDR_PUTLONG(xdrs
, &l
);
154 if (!XDR_GETLONG(xdrs
, &l
)) {
160 } else if (sizeof (u_int
) == sizeof (u_long
)) {
161 return (xdr_u_long(xdrs
, (u_long
*)up
));
162 } else if (sizeof (u_int
) == sizeof (u_short
)) {
163 return (xdr_short(xdrs
, (short *)up
));
165 abort (); /* drepper@gnu */
167 /* force unresolved reference (link-time error): */
168 extern unexpected_sizes_in_xdr_u_int ();
170 unexpected_sizes_in_xdr_u_int();
178 * same as xdr_u_long - open coded to save a proc call!
186 if (xdrs
->x_op
== XDR_ENCODE
)
187 return (XDR_PUTLONG(xdrs
, lp
));
189 if (xdrs
->x_op
== XDR_DECODE
)
190 return (XDR_GETLONG(xdrs
, lp
));
192 if (xdrs
->x_op
== XDR_FREE
)
199 * XDR unsigned long integers
200 * same as xdr_long - open coded to save a proc call!
203 xdr_u_long(xdrs
, ulp
)
208 if (xdrs
->x_op
== XDR_DECODE
)
209 return (XDR_GETLONG(xdrs
, (long *)ulp
));
210 if (xdrs
->x_op
== XDR_ENCODE
)
211 return (XDR_PUTLONG(xdrs
, (long *)ulp
));
212 if (xdrs
->x_op
== XDR_FREE
)
227 switch (xdrs
->x_op
) {
231 return (XDR_PUTLONG(xdrs
, &l
));
234 if (!XDR_GETLONG(xdrs
, &l
)) {
247 * XDR unsigned short integers
250 xdr_u_short(xdrs
, usp
)
256 switch (xdrs
->x_op
) {
260 return (XDR_PUTLONG(xdrs
, &l
));
263 if (!XDR_GETLONG(xdrs
, &l
)) {
287 if (!xdr_int(xdrs
, &i
)) {
295 * XDR an unsigned char
305 if (!xdr_u_int(xdrs
, &u
)) {
322 switch (xdrs
->x_op
) {
325 lb
= *bp
? XDR_TRUE
: XDR_FALSE
;
326 return (XDR_PUTLONG(xdrs
, &lb
));
329 if (!XDR_GETLONG(xdrs
, &lb
)) {
332 *bp
= (lb
== XDR_FALSE
) ? FALSE
: TRUE
;
350 enum sizecheck
{ SIZEVAL
}; /* used to find the size of an enum */
353 * enums are treated as ints
355 if (sizeof (enum sizecheck
) == 4) {
356 return (xdr_long(xdrs
, (long *)ep
));
357 } else if (sizeof (enum sizecheck
) == sizeof (short)) {
358 return (xdr_short(xdrs
, (short *)ep
));
363 (void) (xdr_short(xdrs
, (short *)ep
));
364 return (xdr_long(xdrs
, (long *)ep
));
370 * Allows the specification of a fixed size sequence of opaque bytes.
371 * cp points to the opaque object and cnt gives the byte length.
374 xdr_opaque(xdrs
, cp
, cnt
)
379 register u_int rndup
;
380 static crud
[BYTES_PER_XDR_UNIT
];
383 * if no data we are done
389 * round byte count to full xdr units
391 rndup
= cnt
% BYTES_PER_XDR_UNIT
;
393 rndup
= BYTES_PER_XDR_UNIT
- rndup
;
395 if (xdrs
->x_op
== XDR_DECODE
) {
396 if (!XDR_GETBYTES(xdrs
, cp
, cnt
)) {
401 return (XDR_GETBYTES(xdrs
, crud
, rndup
));
404 if (xdrs
->x_op
== XDR_ENCODE
) {
405 if (!XDR_PUTBYTES(xdrs
, cp
, cnt
)) {
410 return (XDR_PUTBYTES(xdrs
, xdr_zero
, rndup
));
413 if (xdrs
->x_op
== XDR_FREE
) {
422 * *cpp is a pointer to the bytes, *sizep is the count.
423 * If *cpp is NULL maxsize bytes are allocated
426 xdr_bytes(xdrs
, cpp
, sizep
, maxsize
)
429 register u_int
*sizep
;
432 register char *sp
= *cpp
; /* sp is the actual string pointer */
433 register u_int nodesize
;
436 * first deal with the length since xdr bytes are counted
438 if (! xdr_u_int(xdrs
, sizep
)) {
442 if ((nodesize
> maxsize
) && (xdrs
->x_op
!= XDR_FREE
)) {
447 * now deal with the actual bytes
449 switch (xdrs
->x_op
) {
456 *cpp
= sp
= (char *)mem_alloc(nodesize
);
459 (void) fprintf(stderr
, "xdr_bytes: out of memory\n");
465 return (xdr_opaque(xdrs
, sp
, nodesize
));
469 mem_free(sp
, nodesize
);
478 * Implemented here due to commonality of the object.
486 return (xdr_bytes(xdrs
, &np
->n_bytes
, &np
->n_len
, MAX_NETOBJ_SZ
));
490 * XDR a descriminated union
491 * Support routine for discriminated unions.
492 * You create an array of xdrdiscrim structures, terminated with
493 * an entry with a null procedure pointer. The routine gets
494 * the discriminant value and then searches the array of xdrdiscrims
495 * looking for that value. It calls the procedure given in the xdrdiscrim
496 * to handle the discriminant. If there is no specific routine a default
497 * routine may be called.
498 * If there is no specific or default routine an error is returned.
501 xdr_union(xdrs
, dscmp
, unp
, choices
, dfault
)
503 enum_t
*dscmp
; /* enum to decide which arm to work on */
504 char *unp
; /* the union itself */
505 struct xdr_discrim
*choices
; /* [value, xdr proc] for each arm */
506 xdrproc_t dfault
; /* default xdr routine */
508 register enum_t dscm
;
511 * we deal with the discriminator; it's an enum
513 if (! xdr_enum(xdrs
, dscmp
)) {
519 * search choices for a value that matches the discriminator.
520 * if we find one, execute the xdr routine for that value.
522 for (; choices
->proc
!= NULL_xdrproc_t
; choices
++) {
523 if (choices
->value
== dscm
)
524 return ((*(choices
->proc
))(xdrs
, unp
, LASTUNSIGNED
));
528 * no match - execute the default xdr routine if there is one
530 return ((dfault
== NULL_xdrproc_t
) ? FALSE
:
531 (*dfault
)(xdrs
, unp
, LASTUNSIGNED
));
536 * Non-portable xdr primitives.
537 * Care should be taken when moving these routines to new architectures.
542 * XDR null terminated ASCII strings
543 * xdr_string deals with "C strings" - arrays of bytes that are
544 * terminated by a NULL character. The parameter cpp references a
545 * pointer to storage; If the pointer is null, then the necessary
546 * storage is allocated. The last parameter is the max allowed length
547 * of the string as specified by a protocol.
550 xdr_string(xdrs
, cpp
, maxsize
)
555 register char *sp
= *cpp
; /* sp is the actual string pointer */
560 * first deal with the length since xdr strings are counted-strings
562 switch (xdrs
->x_op
) {
565 return(TRUE
); /* already free */
567 /* fall through... */
572 if (! xdr_u_int(xdrs
, &size
)) {
575 if (size
> maxsize
) {
581 * now deal with the actual bytes
583 switch (xdrs
->x_op
) {
590 *cpp
= sp
= (char *)mem_alloc(nodesize
);
592 (void) fprintf(stderr
, "xdr_string: out of memory\n");
599 return (xdr_opaque(xdrs
, sp
, size
));
602 mem_free(sp
, nodesize
);
610 * Wrapper for xdr_string that can be called directly from
611 * routines like clnt_call
614 xdr_wrapstring(xdrs
, cpp
)
618 if (xdr_string(xdrs
, cpp
, LASTUNSIGNED
)) {