]>
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.2 1999/10/14 21:56:54 wsanchez 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
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
93 #if defined(__APPLE__)
109 xdr_void(/* xdrs, addr */)
127 (void) (xdr_short(xdrs
, (short *)ip
));
128 return (xdr_long(xdrs
, (long *)ip
));
130 if (sizeof (int) == sizeof (long)) {
131 return (xdr_long(xdrs
, (long *)ip
));
133 return (xdr_short(xdrs
, (short *)ip
));
139 * XDR unsigned integers
148 (void) (xdr_short(xdrs
, (short *)up
));
149 return (xdr_u_long(xdrs
, (u_long
*)up
));
151 if (sizeof (u_int
) == sizeof (u_long
)) {
152 return (xdr_u_long(xdrs
, (u_long
*)up
));
154 return (xdr_short(xdrs
, (short *)up
));
161 * same as xdr_u_long - open coded to save a proc call!
169 if (xdrs
->x_op
== XDR_ENCODE
)
170 return (XDR_PUTLONG(xdrs
, lp
));
172 if (xdrs
->x_op
== XDR_DECODE
)
173 return (XDR_GETLONG(xdrs
, lp
));
175 if (xdrs
->x_op
== XDR_FREE
)
182 * XDR unsigned long integers
183 * same as xdr_long - open coded to save a proc call!
186 xdr_u_long(xdrs
, ulp
)
191 if (xdrs
->x_op
== XDR_DECODE
)
192 return (XDR_GETLONG(xdrs
, (long *)ulp
));
193 if (xdrs
->x_op
== XDR_ENCODE
)
194 return (XDR_PUTLONG(xdrs
, (long *)ulp
));
195 if (xdrs
->x_op
== XDR_FREE
)
210 switch (xdrs
->x_op
) {
214 return (XDR_PUTLONG(xdrs
, &l
));
217 if (!XDR_GETLONG(xdrs
, &l
)) {
230 * XDR unsigned short integers
233 xdr_u_short(xdrs
, usp
)
239 switch (xdrs
->x_op
) {
243 return (XDR_PUTLONG(xdrs
, &l
));
246 if (!XDR_GETLONG(xdrs
, &l
)) {
270 if (!xdr_int(xdrs
, &i
)) {
278 * XDR an unsigned char
288 if (!xdr_u_int(xdrs
, &u
)) {
305 switch (xdrs
->x_op
) {
308 lb
= *bp
? XDR_TRUE
: XDR_FALSE
;
309 return (XDR_PUTLONG(xdrs
, &lb
));
312 if (!XDR_GETLONG(xdrs
, &lb
)) {
315 *bp
= (lb
== XDR_FALSE
) ? FALSE
: TRUE
;
333 enum sizecheck
{ SIZEVAL
}; /* used to find the size of an enum */
336 * enums are treated as ints
338 if (sizeof (enum sizecheck
) == sizeof (long)) {
339 return (xdr_long(xdrs
, (long *)ep
));
340 } else if (sizeof (enum sizecheck
) == sizeof (short)) {
341 return (xdr_short(xdrs
, (short *)ep
));
346 (void) (xdr_short(xdrs
, (short *)ep
));
347 return (xdr_long(xdrs
, (long *)ep
));
353 * Allows the specification of a fixed size sequence of opaque bytes.
354 * cp points to the opaque object and cnt gives the byte length.
357 xdr_opaque(xdrs
, cp
, cnt
)
362 register u_int rndup
;
363 static crud
[BYTES_PER_XDR_UNIT
];
366 * if no data we are done
372 * round byte count to full xdr units
374 rndup
= cnt
% BYTES_PER_XDR_UNIT
;
376 rndup
= BYTES_PER_XDR_UNIT
- rndup
;
378 if (xdrs
->x_op
== XDR_DECODE
) {
379 if (!XDR_GETBYTES(xdrs
, cp
, cnt
)) {
384 return (XDR_GETBYTES(xdrs
, crud
, rndup
));
387 if (xdrs
->x_op
== XDR_ENCODE
) {
388 if (!XDR_PUTBYTES(xdrs
, cp
, cnt
)) {
393 return (XDR_PUTBYTES(xdrs
, xdr_zero
, rndup
));
396 if (xdrs
->x_op
== XDR_FREE
) {
405 * *cpp is a pointer to the bytes, *sizep is the count.
406 * If *cpp is NULL maxsize bytes are allocated
409 xdr_bytes(xdrs
, cpp
, sizep
, maxsize
)
412 register u_int
*sizep
;
415 register char *sp
= *cpp
; /* sp is the actual string pointer */
416 register u_int nodesize
;
419 * first deal with the length since xdr bytes are counted
421 if (! xdr_u_int(xdrs
, sizep
)) {
425 if ((nodesize
> maxsize
) && (xdrs
->x_op
!= XDR_FREE
)) {
430 * now deal with the actual bytes
432 switch (xdrs
->x_op
) {
439 *cpp
= sp
= (char *)mem_alloc(nodesize
);
442 (void) fprintf(stderr
, "xdr_bytes: out of memory\n");
448 return (xdr_opaque(xdrs
, sp
, nodesize
));
452 mem_free(sp
, nodesize
);
461 * Implemented here due to commonality of the object.
469 return (xdr_bytes(xdrs
, &np
->n_bytes
, &np
->n_len
, MAX_NETOBJ_SZ
));
473 * XDR a descriminated union
474 * Support routine for discriminated unions.
475 * You create an array of xdrdiscrim structures, terminated with
476 * an entry with a null procedure pointer. The routine gets
477 * the discriminant value and then searches the array of xdrdiscrims
478 * looking for that value. It calls the procedure given in the xdrdiscrim
479 * to handle the discriminant. If there is no specific routine a default
480 * routine may be called.
481 * If there is no specific or default routine an error is returned.
484 xdr_union(xdrs
, dscmp
, unp
, choices
, dfault
)
486 enum_t
*dscmp
; /* enum to decide which arm to work on */
487 char *unp
; /* the union itself */
488 struct xdr_discrim
*choices
; /* [value, xdr proc] for each arm */
489 xdrproc_t dfault
; /* default xdr routine */
491 register enum_t dscm
;
494 * we deal with the discriminator; it's an enum
496 if (! xdr_enum(xdrs
, dscmp
)) {
502 * search choices for a value that matches the discriminator.
503 * if we find one, execute the xdr routine for that value.
505 for (; choices
->proc
!= NULL_xdrproc_t
; choices
++) {
506 if (choices
->value
== dscm
)
507 return ((*(choices
->proc
))(xdrs
, unp
, LASTUNSIGNED
));
511 * no match - execute the default xdr routine if there is one
513 return ((dfault
== NULL_xdrproc_t
) ? FALSE
:
514 (*dfault
)(xdrs
, unp
, LASTUNSIGNED
));
519 * Non-portable xdr primitives.
520 * Care should be taken when moving these routines to new architectures.
525 * XDR null terminated ASCII strings
526 * xdr_string deals with "C strings" - arrays of bytes that are
527 * terminated by a NULL character. The parameter cpp references a
528 * pointer to storage; If the pointer is null, then the necessary
529 * storage is allocated. The last parameter is the max allowed length
530 * of the string as specified by a protocol.
533 xdr_string(xdrs
, cpp
, maxsize
)
538 register char *sp
= *cpp
; /* sp is the actual string pointer */
543 * first deal with the length since xdr strings are counted-strings
545 switch (xdrs
->x_op
) {
548 return(TRUE
); /* already free */
550 /* fall through... */
555 if (! xdr_u_int(xdrs
, &size
)) {
558 if (size
> maxsize
) {
564 * now deal with the actual bytes
566 switch (xdrs
->x_op
) {
573 *cpp
= sp
= (char *)mem_alloc(nodesize
);
575 (void) fprintf(stderr
, "xdr_string: out of memory\n");
582 return (xdr_opaque(xdrs
, sp
, size
));
585 mem_free(sp
, nodesize
);
593 * Wrapper for xdr_string that can be called directly from
594 * routines like clnt_call
597 xdr_wrapstring(xdrs
, cpp
)
601 if (xdr_string(xdrs
, cpp
, LASTUNSIGNED
)) {