]>
git.saurik.com Git - apple/libinfo.git/blob - rpc.subproj/xdr.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
26 * unrestricted use provided that this legend is included on all tape
27 * media and as a part of the software program in whole or part. Users
28 * may copy or modify Sun RPC without charge, but are not authorized
29 * to license or distribute it to anyone else except as part of a product or
30 * program developed by the user.
32 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
33 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
36 * Sun RPC is provided with no support and without any obligation on the
37 * part of Sun Microsystems, Inc. to assist in its use, correction,
38 * modification or enhancement.
40 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
41 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
42 * OR ANY PART THEREOF.
44 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
45 * or profits or other special, indirect and consequential damages, even if
46 * Sun has been advised of the possibility of such damages.
48 * Sun Microsystems, Inc.
50 * Mountain View, California 94043
53 #if defined(LIBC_SCCS) && !defined(lint)
54 /*static char *sccsid = "from: @(#)xdr.c 1.35 87/08/12";*/
55 /*static char *sccsid = "from: @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC";*/
56 static char *rcsid
= "$Id: xdr.c,v 1.3 2002/02/19 20:36:26 epeyton Exp $";
60 * xdr.c, Generic XDR routines implementation.
62 * Copyright (C) 1986, Sun Microsystems, Inc.
64 * These are the "generic" xdr routines used to serialize and de-serialize
65 * most common data items. See xdr.h for more info on the interface to
73 #include <rpc/types.h>
77 * constants specific to the xdr "protocol"
79 #define XDR_FALSE ((long) 0)
80 #define XDR_TRUE ((long) 1)
81 #define LASTUNSIGNED ((u_int) 0-1)
86 static char xdr_zero
[BYTES_PER_XDR_UNIT
] = { 0, 0, 0, 0 };
89 * Free a data structure using XDR
90 * Not a filter, but a convenient utility nonetheless
95 #if defined(__APPLE__)
111 xdr_void(/* xdrs, addr */)
129 (void) (xdr_short(xdrs
, (short *)ip
));
130 return (xdr_long(xdrs
, (long *)ip
));
132 if (sizeof (int) == sizeof (long)) {
133 return (xdr_long(xdrs
, (long *)ip
));
135 return (xdr_short(xdrs
, (short *)ip
));
141 * XDR unsigned integers
150 (void) (xdr_short(xdrs
, (short *)up
));
151 return (xdr_u_long(xdrs
, (u_long
*)up
));
153 if (sizeof (u_int
) == sizeof (u_long
)) {
154 return (xdr_u_long(xdrs
, (u_long
*)up
));
156 return (xdr_short(xdrs
, (short *)up
));
163 * same as xdr_u_long - open coded to save a proc call!
171 if (xdrs
->x_op
== XDR_ENCODE
)
172 return (XDR_PUTLONG(xdrs
, lp
));
174 if (xdrs
->x_op
== XDR_DECODE
)
175 return (XDR_GETLONG(xdrs
, lp
));
177 if (xdrs
->x_op
== XDR_FREE
)
184 * XDR unsigned long integers
185 * same as xdr_long - open coded to save a proc call!
188 xdr_u_long(xdrs
, ulp
)
193 if (xdrs
->x_op
== XDR_DECODE
)
194 return (XDR_GETLONG(xdrs
, (long *)ulp
));
195 if (xdrs
->x_op
== XDR_ENCODE
)
196 return (XDR_PUTLONG(xdrs
, (long *)ulp
));
197 if (xdrs
->x_op
== XDR_FREE
)
212 switch (xdrs
->x_op
) {
216 return (XDR_PUTLONG(xdrs
, &l
));
219 if (!XDR_GETLONG(xdrs
, &l
)) {
232 * XDR unsigned short integers
235 xdr_u_short(xdrs
, usp
)
241 switch (xdrs
->x_op
) {
245 return (XDR_PUTLONG(xdrs
, &l
));
248 if (!XDR_GETLONG(xdrs
, &l
)) {
272 if (!xdr_int(xdrs
, &i
)) {
280 * XDR an unsigned char
290 if (!xdr_u_int(xdrs
, &u
)) {
307 switch (xdrs
->x_op
) {
310 lb
= *bp
? XDR_TRUE
: XDR_FALSE
;
311 return (XDR_PUTLONG(xdrs
, &lb
));
314 if (!XDR_GETLONG(xdrs
, &lb
)) {
317 *bp
= (lb
== XDR_FALSE
) ? FALSE
: TRUE
;
335 enum sizecheck
{ SIZEVAL
}; /* used to find the size of an enum */
338 * enums are treated as ints
340 if (sizeof (enum sizecheck
) == sizeof (long)) {
341 return (xdr_long(xdrs
, (long *)ep
));
342 } else if (sizeof (enum sizecheck
) == sizeof (short)) {
343 return (xdr_short(xdrs
, (short *)ep
));
348 (void) (xdr_short(xdrs
, (short *)ep
));
349 return (xdr_long(xdrs
, (long *)ep
));
355 * Allows the specification of a fixed size sequence of opaque bytes.
356 * cp points to the opaque object and cnt gives the byte length.
359 xdr_opaque(xdrs
, cp
, cnt
)
364 register u_int rndup
;
365 static char crud
[BYTES_PER_XDR_UNIT
];
368 * if no data we are done
374 * round byte count to full xdr units
376 rndup
= cnt
% BYTES_PER_XDR_UNIT
;
378 rndup
= BYTES_PER_XDR_UNIT
- rndup
;
380 if (xdrs
->x_op
== XDR_DECODE
) {
381 if (!XDR_GETBYTES(xdrs
, cp
, cnt
)) {
386 return (XDR_GETBYTES(xdrs
, crud
, rndup
));
389 if (xdrs
->x_op
== XDR_ENCODE
) {
390 if (!XDR_PUTBYTES(xdrs
, cp
, cnt
)) {
395 return (XDR_PUTBYTES(xdrs
, xdr_zero
, rndup
));
398 if (xdrs
->x_op
== XDR_FREE
) {
407 * *cpp is a pointer to the bytes, *sizep is the count.
408 * If *cpp is NULL maxsize bytes are allocated
411 xdr_bytes(xdrs
, cpp
, sizep
, maxsize
)
414 register u_int
*sizep
;
417 register char *sp
= *cpp
; /* sp is the actual string pointer */
418 register u_int nodesize
;
421 * first deal with the length since xdr bytes are counted
423 if (! xdr_u_int(xdrs
, sizep
)) {
427 if ((nodesize
> maxsize
) && (xdrs
->x_op
!= XDR_FREE
)) {
432 * now deal with the actual bytes
434 switch (xdrs
->x_op
) {
441 *cpp
= sp
= (char *)mem_alloc(nodesize
);
444 (void) fprintf(stderr
, "xdr_bytes: out of memory\n");
450 return (xdr_opaque(xdrs
, sp
, nodesize
));
454 mem_free(sp
, nodesize
);
463 * Implemented here due to commonality of the object.
471 return (xdr_bytes(xdrs
, &np
->n_bytes
, &np
->n_len
, MAX_NETOBJ_SZ
));
475 * XDR a descriminated union
476 * Support routine for discriminated unions.
477 * You create an array of xdrdiscrim structures, terminated with
478 * an entry with a null procedure pointer. The routine gets
479 * the discriminant value and then searches the array of xdrdiscrims
480 * looking for that value. It calls the procedure given in the xdrdiscrim
481 * to handle the discriminant. If there is no specific routine a default
482 * routine may be called.
483 * If there is no specific or default routine an error is returned.
486 xdr_union(xdrs
, dscmp
, unp
, choices
, dfault
)
488 enum_t
*dscmp
; /* enum to decide which arm to work on */
489 char *unp
; /* the union itself */
490 struct xdr_discrim
*choices
; /* [value, xdr proc] for each arm */
491 xdrproc_t dfault
; /* default xdr routine */
493 register enum_t dscm
;
496 * we deal with the discriminator; it's an enum
498 if (! xdr_enum(xdrs
, dscmp
)) {
504 * search choices for a value that matches the discriminator.
505 * if we find one, execute the xdr routine for that value.
507 for (; choices
->proc
!= NULL_xdrproc_t
; choices
++) {
508 if (choices
->value
== dscm
)
509 return ((*(choices
->proc
))(xdrs
, unp
, LASTUNSIGNED
));
513 * no match - execute the default xdr routine if there is one
515 return ((dfault
== NULL_xdrproc_t
) ? FALSE
:
516 (*dfault
)(xdrs
, unp
, LASTUNSIGNED
));
521 * Non-portable xdr primitives.
522 * Care should be taken when moving these routines to new architectures.
527 * XDR null terminated ASCII strings
528 * xdr_string deals with "C strings" - arrays of bytes that are
529 * terminated by a NULL character. The parameter cpp references a
530 * pointer to storage; If the pointer is null, then the necessary
531 * storage is allocated. The last parameter is the max allowed length
532 * of the string as specified by a protocol.
535 xdr_string(xdrs
, cpp
, maxsize
)
540 register char *sp
= *cpp
; /* sp is the actual string pointer */
545 * first deal with the length since xdr strings are counted-strings
547 switch (xdrs
->x_op
) {
550 return(TRUE
); /* already free */
552 /* fall through... */
557 if (! xdr_u_int(xdrs
, &size
)) {
560 if (size
> maxsize
) {
566 * now deal with the actual bytes
568 switch (xdrs
->x_op
) {
575 *cpp
= sp
= (char *)mem_alloc(nodesize
);
577 (void) fprintf(stderr
, "xdr_string: out of memory\n");
584 return (xdr_opaque(xdrs
, sp
, size
));
587 mem_free(sp
, nodesize
);
595 * Wrapper for xdr_string that can be called directly from
596 * routines like clnt_call
599 xdr_wrapstring(xdrs
, cpp
)
603 if (xdr_string(xdrs
, cpp
, LASTUNSIGNED
)) {