2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
27 * unrestricted use provided that this legend is included on all tape
28 * media and as a part of the software program in whole or part. Users
29 * may copy or modify Sun RPC without charge, but are not authorized
30 * to license or distribute it to anyone else except as part of a product or
31 * program developed by the user.
33 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
34 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
35 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
37 * Sun RPC is provided with no support and without any obligation on the
38 * part of Sun Microsystems, Inc. to assist in its use, correction,
39 * modification or enhancement.
41 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
42 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
43 * OR ANY PART THEREOF.
45 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
46 * or profits or other special, indirect and consequential damages, even if
47 * Sun has been advised of the possibility of such damages.
49 * Sun Microsystems, Inc.
51 * Mountain View, California 94043
54 #if defined(LIBC_SCCS) && !defined(lint)
55 /*static char *sccsid = "from: @(#)xdr.c 1.35 87/08/12";*/
56 /*static char *sccsid = "from: @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC";*/
57 static char *rcsid
= "$Id: xdr.c,v 1.4 2003/06/23 17:24:59 majka Exp $";
61 * xdr.c, Generic XDR routines implementation.
63 * Copyright (C) 1986, Sun Microsystems, Inc.
65 * These are the "generic" xdr routines used to serialize and de-serialize
66 * most common data items. See xdr.h for more info on the interface to
74 #include <rpc/types.h>
78 * constants specific to the xdr "protocol"
80 #define XDR_FALSE ((long) 0)
81 #define XDR_TRUE ((long) 1)
82 #define LASTUNSIGNED ((u_int) 0-1)
87 static char xdr_zero
[BYTES_PER_XDR_UNIT
] = { 0, 0, 0, 0 };
90 * Free a data structure using XDR
91 * Not a filter, but a convenient utility nonetheless
96 #if defined(__APPLE__)
112 xdr_void(/* xdrs, addr */)
130 (void) (xdr_short(xdrs
, (short *)ip
));
131 return (xdr_long(xdrs
, (long *)ip
));
133 if (sizeof (int) == sizeof (long)) {
134 return (xdr_long(xdrs
, (long *)ip
));
136 return (xdr_short(xdrs
, (short *)ip
));
142 * XDR unsigned integers
151 (void) (xdr_short(xdrs
, (short *)up
));
152 return (xdr_u_long(xdrs
, (u_long
*)up
));
154 if (sizeof (u_int
) == sizeof (u_long
)) {
155 return (xdr_u_long(xdrs
, (u_long
*)up
));
157 return (xdr_short(xdrs
, (short *)up
));
164 * same as xdr_u_long - open coded to save a proc call!
172 if (xdrs
->x_op
== XDR_ENCODE
)
173 return (XDR_PUTLONG(xdrs
, lp
));
175 if (xdrs
->x_op
== XDR_DECODE
)
176 return (XDR_GETLONG(xdrs
, lp
));
178 if (xdrs
->x_op
== XDR_FREE
)
185 * XDR unsigned long integers
186 * same as xdr_long - open coded to save a proc call!
189 xdr_u_long(xdrs
, ulp
)
194 if (xdrs
->x_op
== XDR_DECODE
)
195 return (XDR_GETLONG(xdrs
, (long *)ulp
));
196 if (xdrs
->x_op
== XDR_ENCODE
)
197 return (XDR_PUTLONG(xdrs
, (long *)ulp
));
198 if (xdrs
->x_op
== XDR_FREE
)
213 switch (xdrs
->x_op
) {
217 return (XDR_PUTLONG(xdrs
, &l
));
220 if (!XDR_GETLONG(xdrs
, &l
)) {
233 * XDR unsigned short integers
236 xdr_u_short(xdrs
, usp
)
242 switch (xdrs
->x_op
) {
246 return (XDR_PUTLONG(xdrs
, &l
));
249 if (!XDR_GETLONG(xdrs
, &l
)) {
273 if (!xdr_int(xdrs
, &i
)) {
281 * XDR an unsigned char
291 if (!xdr_u_int(xdrs
, &u
)) {
308 switch (xdrs
->x_op
) {
311 lb
= *bp ? XDR_TRUE
: XDR_FALSE
;
312 return (XDR_PUTLONG(xdrs
, &lb
));
315 if (!XDR_GETLONG(xdrs
, &lb
)) {
318 *bp
= (lb
== XDR_FALSE
) ? FALSE
: TRUE
;
336 enum sizecheck
{ SIZEVAL
}; /* used to find the size of an enum */
339 * enums are treated as ints
341 if (sizeof (enum sizecheck
) == sizeof (long)) {
342 return (xdr_long(xdrs
, (long *)ep
));
343 } else if (sizeof (enum sizecheck
) == sizeof (short)) {
344 return (xdr_short(xdrs
, (short *)ep
));
349 (void) (xdr_short(xdrs
, (short *)ep
));
350 return (xdr_long(xdrs
, (long *)ep
));
356 * Allows the specification of a fixed size sequence of opaque bytes.
357 * cp points to the opaque object and cnt gives the byte length.
360 xdr_opaque(xdrs
, cp
, cnt
)
365 register u_int rndup
;
366 static char crud
[BYTES_PER_XDR_UNIT
];
369 * if no data we are done
375 * round byte count to full xdr units
377 rndup
= cnt
% BYTES_PER_XDR_UNIT
;
379 rndup
= BYTES_PER_XDR_UNIT
- rndup
;
381 if (xdrs
->x_op
== XDR_DECODE
) {
382 if (!XDR_GETBYTES(xdrs
, cp
, cnt
)) {
387 return (XDR_GETBYTES(xdrs
, crud
, rndup
));
390 if (xdrs
->x_op
== XDR_ENCODE
) {
391 if (!XDR_PUTBYTES(xdrs
, cp
, cnt
)) {
396 return (XDR_PUTBYTES(xdrs
, xdr_zero
, rndup
));
399 if (xdrs
->x_op
== XDR_FREE
) {
408 * *cpp is a pointer to the bytes, *sizep is the count.
409 * If *cpp is NULL maxsize bytes are allocated
412 xdr_bytes(xdrs
, cpp
, sizep
, maxsize
)
415 register u_int
*sizep
;
418 register char *sp
= *cpp
; /* sp is the actual string pointer */
419 register u_int nodesize
;
422 * first deal with the length since xdr bytes are counted
424 if (! xdr_u_int(xdrs
, sizep
)) {
428 if ((nodesize
> maxsize
) && (xdrs
->x_op
!= XDR_FREE
)) {
433 * now deal with the actual bytes
435 switch (xdrs
->x_op
) {
442 *cpp
= sp
= (char *)mem_alloc(nodesize
);
445 (void) fprintf(stderr
, "xdr_bytes: out of memory\n");
451 return (xdr_opaque(xdrs
, sp
, nodesize
));
455 mem_free(sp
, nodesize
);
464 * Implemented here due to commonality of the object.
472 return (xdr_bytes(xdrs
, &np
->n_bytes
, &np
->n_len
, MAX_NETOBJ_SZ
));
476 * XDR a descriminated union
477 * Support routine for discriminated unions.
478 * You create an array of xdrdiscrim structures, terminated with
479 * an entry with a null procedure pointer. The routine gets
480 * the discriminant value and then searches the array of xdrdiscrims
481 * looking for that value. It calls the procedure given in the xdrdiscrim
482 * to handle the discriminant. If there is no specific routine a default
483 * routine may be called.
484 * If there is no specific or default routine an error is returned.
487 xdr_union(xdrs
, dscmp
, unp
, choices
, dfault
)
489 enum_t
*dscmp
; /* enum to decide which arm to work on */
490 char *unp
; /* the union itself */
491 struct xdr_discrim
*choices
; /* [value, xdr proc] for each arm */
492 xdrproc_t dfault
; /* default xdr routine */
494 register enum_t dscm
;
497 * we deal with the discriminator; it's an enum
499 if (! xdr_enum(xdrs
, dscmp
)) {
505 * search choices for a value that matches the discriminator.
506 * if we find one, execute the xdr routine for that value.
508 for (; choices
->proc
!= NULL_xdrproc_t
; choices
++) {
509 if (choices
->value
== dscm
)
510 return ((*(choices
->proc
))(xdrs
, unp
, LASTUNSIGNED
));
514 * no match - execute the default xdr routine if there is one
516 return ((dfault
== NULL_xdrproc_t
) ? FALSE
:
517 (*dfault
)(xdrs
, unp
, LASTUNSIGNED
));
522 * Non-portable xdr primitives.
523 * Care should be taken when moving these routines to new architectures.
528 * XDR null terminated ASCII strings
529 * xdr_string deals with "C strings" - arrays of bytes that are
530 * terminated by a NULL character. The parameter cpp references a
531 * pointer to storage; If the pointer is null, then the necessary
532 * storage is allocated. The last parameter is the max allowed length
533 * of the string as specified by a protocol.
536 xdr_string(xdrs
, cpp
, maxsize
)
541 register char *sp
= *cpp
; /* sp is the actual string pointer */
546 * first deal with the length since xdr strings are counted-strings
548 switch (xdrs
->x_op
) {
551 return(TRUE
); /* already free */
553 /* fall through... */
559 if (! xdr_u_int(xdrs
, &size
)) {
562 if (size
> maxsize
) {
568 * now deal with the actual bytes
570 switch (xdrs
->x_op
) {
577 *cpp
= sp
= (char *)mem_alloc(nodesize
);
579 (void) fprintf(stderr
, "xdr_string: out of memory\n");
586 return (xdr_opaque(xdrs
, sp
, size
));
589 mem_free(sp
, nodesize
);
597 * Wrapper for xdr_string that can be called directly from
598 * routines like clnt_call
601 xdr_wrapstring(xdrs
, cpp
)
605 if (xdr_string(xdrs
, cpp
, LASTUNSIGNED
)) {