2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
30 #include <afsconfig.h>
31 #include <afs/param.h>
39 * xdr.c, Generic XDR routines implementation.
41 * Copyright (C) 1984, Sun Microsystems, Inc.
43 * These are the "generic" xdr routines used to serialize and de-serialize
44 * most common data items. See xdr.h for more info on the interface to
50 #if defined(KERNEL) && !defined(UKERNEL)
51 #if !defined(AFS_LINUX26_ENV)
52 #include <sys/param.h>
54 #ifndef AFS_LINUX20_ENV
55 #include <sys/systm.h>
62 * constants specific to the xdr "protocol"
64 #define XDR_FALSE ((afs_int32) 0)
65 #define XDR_TRUE ((afs_int32) 1)
66 #define LASTUNSIGNED ((u_int) 0-1)
86 xdr_int(XDR
* xdrs
, int *ip
)
94 return (XDR_PUTINT32(xdrs
, &l
));
97 if (!XDR_GETINT32(xdrs
, &l
)) {
110 * XDR unsigned integers
113 xdr_u_int(XDR
* xdrs
, u_int
* uip
)
117 switch (xdrs
->x_op
) {
120 l
= (afs_uint32
) * uip
;
121 return (XDR_PUTINT32(xdrs
, (afs_int32
*) &l
));
124 if (!XDR_GETINT32(xdrs
, (afs_int32
*) &l
)) {
141 xdr_long(XDR
* xdrs
, long *lp
)
145 switch (xdrs
->x_op
) {
148 l
= (afs_int32
) * lp
;
149 return (XDR_PUTINT32(xdrs
, &l
));
152 if (!XDR_GETINT32(xdrs
, &l
)) {
165 * XDR unsigned long integers
168 xdr_u_long(XDR
* xdrs
, u_long
* ulp
)
172 switch (xdrs
->x_op
) {
175 l
= (afs_uint32
) * ulp
;
176 return (XDR_PUTINT32(xdrs
, (afs_int32
*)&l
));
179 if (!XDR_GETINT32(xdrs
, (afs_int32
*)&l
)) {
196 xdr_char(XDR
* xdrs
, char *sp
)
200 switch (xdrs
->x_op
) {
203 l
= (afs_int32
) * sp
;
204 return (XDR_PUTINT32(xdrs
, &l
));
207 if (!XDR_GETINT32(xdrs
, &l
)) {
223 xdr_u_char(XDR
* xdrs
, u_char
* usp
)
227 switch (xdrs
->x_op
) {
230 l
= (afs_uint32
) * usp
;
231 return (XDR_PUTINT32(xdrs
, (afs_int32
*)&l
));
234 if (!XDR_GETINT32(xdrs
, (afs_int32
*)&l
)) {
251 xdr_short(XDR
* xdrs
, short *sp
)
255 switch (xdrs
->x_op
) {
258 l
= (afs_int32
) * sp
;
259 return (XDR_PUTINT32(xdrs
, &l
));
262 if (!XDR_GETINT32(xdrs
, &l
)) {
275 * XDR unsigned short integers
278 xdr_u_short(XDR
* xdrs
, u_short
* usp
)
282 switch (xdrs
->x_op
) {
285 l
= (afs_uint32
) * usp
;
286 return (XDR_PUTINT32(xdrs
, (afs_int32
*)&l
));
289 if (!XDR_GETINT32(xdrs
, (afs_int32
*)&l
)) {
306 xdr_bool(XDR
* xdrs
, bool_t
* bp
)
310 switch (xdrs
->x_op
) {
313 lb
= *bp
? XDR_TRUE
: XDR_FALSE
;
314 return (XDR_PUTINT32(xdrs
, &lb
));
317 if (!XDR_GETINT32(xdrs
, &lb
)) {
320 *bp
= (lb
== XDR_FALSE
) ? FALSE
: TRUE
;
333 xdr_enum(XDR
* xdrs
, enum_t
* ep
)
335 enum sizecheck
{ SIZEVAL
}; /* used to find the size of an enum */
338 * enums are treated as ints
341 return (xdr_long(xdrs
, (long *)ep
));
347 * Allows the specification of a fixed size sequence of opaque bytes.
348 * cp points to the opaque object and cnt gives the byte length.
351 xdr_opaque(XDR
* xdrs
, caddr_t cp
, u_int cnt
)
354 int crud
[BYTES_PER_XDR_UNIT
];
355 char xdr_zero
[BYTES_PER_XDR_UNIT
] = { 0, 0, 0, 0 };
358 * if no data we are done
364 * round byte count to full xdr units
366 rndup
= cnt
% BYTES_PER_XDR_UNIT
;
368 rndup
= BYTES_PER_XDR_UNIT
- rndup
;
370 if (xdrs
->x_op
== XDR_DECODE
) {
371 if (!XDR_GETBYTES(xdrs
, cp
, cnt
)) {
376 return (XDR_GETBYTES(xdrs
, (caddr_t
)crud
, rndup
));
379 if (xdrs
->x_op
== XDR_ENCODE
) {
380 if (!XDR_PUTBYTES(xdrs
, cp
, cnt
)) {
385 return (XDR_PUTBYTES(xdrs
, xdr_zero
, rndup
));
388 if (xdrs
->x_op
== XDR_FREE
) {
397 * *cpp is a pointer to the bytes, *sizep is the count.
398 * If *cpp is NULL maxsize bytes are allocated
401 xdr_bytes(XDR
* xdrs
, char **cpp
, u_int
* sizep
,
404 char *sp
= *cpp
; /* sp is the actual string pointer */
408 * first deal with the length since xdr bytes are counted
410 if (!xdr_u_int(xdrs
, sizep
)) {
414 if ((nodesize
> maxsize
) && (xdrs
->x_op
!= XDR_FREE
)) {
419 * now deal with the actual bytes
421 switch (xdrs
->x_op
) {
425 *cpp
= sp
= (char *)osi_alloc(nodesize
);
433 return (xdr_opaque(xdrs
, sp
, nodesize
));
437 osi_free(sp
, nodesize
);
446 * XDR a descriminated union
447 * Support routine for discriminated unions.
448 * You create an array of xdrdiscrim structures, terminated with
449 * an entry with a null procedure pointer. The routine gets
450 * the discriminant value and then searches the array of xdrdiscrims
451 * looking for that value. It calls the procedure given in the xdrdiscrim
452 * to handle the discriminant. If there is no specific routine a default
453 * routine may be called.
454 * If there is no specific or default routine an error is returned.
457 enum_t *dscmp; * enum to decide which arm to work on *
458 caddr_t unp; * the union itself *
459 struct xdr_discrim *choices; * [value, xdr proc] for each arm *
460 xdrproc_t dfault; * default xdr routine *
463 xdr_union(XDR
* xdrs
, enum_t
* dscmp
, caddr_t unp
,
464 struct xdr_discrim
* choices
, xdrproc_t dfault
)
469 * we deal with the discriminator; it's an enum
471 if (!xdr_enum(xdrs
, dscmp
)) {
477 * search choices for a value that matches the discriminator.
478 * if we find one, execute the xdr routine for that value.
480 for (; choices
->proc
!= NULL_xdrproc_t
; choices
++) {
481 if (choices
->value
== dscm
)
482 return ((*(choices
->proc
)) (xdrs
, unp
, LASTUNSIGNED
));
486 * no match - execute the default xdr routine if there is one
488 return ((dfault
== NULL_xdrproc_t
) ? FALSE
: (*dfault
) (xdrs
, unp
,
494 * Non-portable xdr primitives.
495 * Care should be taken when moving these routines to new architectures.
500 * XDR null terminated ASCII strings
501 * xdr_string deals with "C strings" - arrays of bytes that are
502 * terminated by a NULL character. The parameter cpp references a
503 * pointer to storage; If the pointer is null, then the necessary
504 * storage is allocated. The last parameter is the max allowed length
505 * of the string as specified by a protocol.
508 xdr_string(XDR
* xdrs
, char **cpp
, u_int maxsize
)
510 char *sp
= *cpp
; /* sp is the actual string pointer */
514 if (maxsize
> ((~0u) >> 1) - 1)
515 maxsize
= ((~0u) >> 1) - 1;
518 * first deal with the length since xdr strings are counted-strings
520 switch (xdrs
->x_op
) {
523 return (TRUE
); /* already free */
533 if (!xdr_u_int(xdrs
, &size
)) {
536 if (size
> maxsize
) {
542 * now deal with the actual bytes
544 switch (xdrs
->x_op
) {
548 *cpp
= sp
= (char *)osi_alloc(nodesize
);
556 return (xdr_opaque(xdrs
, sp
, size
));
560 osi_free(sp
, nodesize
);
569 * Wrapper for xdr_string that can be called directly from
570 * routines like clnt_call
574 xdr_wrapstring(XDR
* xdrs
, char **cpp
)
576 if (xdr_string(xdrs
, cpp
, BUFSIZ
)) {
584 xdr_alloc(afs_int32 size
)
586 return osi_alloc(size
);
590 xdr_free(xdrproc_t proc
, void *obj
)
596 /* See note in xdr.h for the method behind this madness */
597 #if defined(AFS_I386_LINUX26_ENV) && defined(KERNEL) && !defined(UKERNEL)