Some doc path fixes from Anders
[pkg-k5-afs_openafs.git] / src / rx / xdr.c
blobdf6d179c5f45e1733a2fe3ccc2e788ff3305a05b
1 /*
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.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
30 #include <afsconfig.h>
31 #include <afs/param.h>
33 #ifndef KERNEL
34 # include <roken.h>
35 #endif
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
45 * xdr.
48 #ifndef NeXT
50 #if defined(KERNEL) && !defined(UKERNEL)
51 #if !defined(AFS_LINUX26_ENV)
52 #include <sys/param.h>
53 #endif
54 #ifndef AFS_LINUX20_ENV
55 #include <sys/systm.h>
56 #endif
57 #endif
58 #include "xdr.h"
59 #include "rx.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)
69 * for unit alignment
74 * XDR nothing
76 bool_t
77 xdr_void(void)
79 return (TRUE);
83 * XDR integers
85 bool_t
86 xdr_int(XDR * xdrs, int *ip)
88 afs_int32 l;
90 switch (xdrs->x_op) {
92 case XDR_ENCODE:
93 l = (afs_int32) * ip;
94 return (XDR_PUTINT32(xdrs, &l));
96 case XDR_DECODE:
97 if (!XDR_GETINT32(xdrs, &l)) {
98 return (FALSE);
100 *ip = (int)l;
101 return (TRUE);
103 case XDR_FREE:
104 return (TRUE);
106 return (FALSE);
110 * XDR unsigned integers
112 bool_t
113 xdr_u_int(XDR * xdrs, u_int * uip)
115 afs_uint32 l;
117 switch (xdrs->x_op) {
119 case XDR_ENCODE:
120 l = (afs_uint32) * uip;
121 return (XDR_PUTINT32(xdrs, (afs_int32 *) &l));
123 case XDR_DECODE:
124 if (!XDR_GETINT32(xdrs, (afs_int32 *) &l)) {
125 return (FALSE);
127 *uip = (u_int) l;
128 return (TRUE);
130 case XDR_FREE:
131 return (TRUE);
133 return (FALSE);
138 * XDR long integers
140 bool_t
141 xdr_long(XDR * xdrs, long *lp)
143 afs_int32 l;
145 switch (xdrs->x_op) {
147 case XDR_ENCODE:
148 l = (afs_int32) * lp;
149 return (XDR_PUTINT32(xdrs, &l));
151 case XDR_DECODE:
152 if (!XDR_GETINT32(xdrs, &l)) {
153 return (FALSE);
155 *lp = (long)l;
156 return (TRUE);
158 case XDR_FREE:
159 return (TRUE);
161 return (FALSE);
165 * XDR unsigned long integers
167 bool_t
168 xdr_u_long(XDR * xdrs, u_long * ulp)
170 afs_uint32 l;
172 switch (xdrs->x_op) {
174 case XDR_ENCODE:
175 l = (afs_uint32) * ulp;
176 return (XDR_PUTINT32(xdrs, (afs_int32 *)&l));
178 case XDR_DECODE:
179 if (!XDR_GETINT32(xdrs, (afs_int32 *)&l)) {
180 return (FALSE);
182 *ulp = (u_long) l;
183 return (TRUE);
185 case XDR_FREE:
186 return (TRUE);
188 return (FALSE);
193 * XDR chars
195 bool_t
196 xdr_char(XDR * xdrs, char *sp)
198 afs_int32 l;
200 switch (xdrs->x_op) {
202 case XDR_ENCODE:
203 l = (afs_int32) * sp;
204 return (XDR_PUTINT32(xdrs, &l));
206 case XDR_DECODE:
207 if (!XDR_GETINT32(xdrs, &l)) {
208 return (FALSE);
210 *sp = (char)l;
211 return (TRUE);
213 case XDR_FREE:
214 return (TRUE);
216 return (FALSE);
220 * XDR unsigned chars
222 bool_t
223 xdr_u_char(XDR * xdrs, u_char * usp)
225 afs_uint32 l;
227 switch (xdrs->x_op) {
229 case XDR_ENCODE:
230 l = (afs_uint32) * usp;
231 return (XDR_PUTINT32(xdrs, (afs_int32 *)&l));
233 case XDR_DECODE:
234 if (!XDR_GETINT32(xdrs, (afs_int32 *)&l)) {
235 return (FALSE);
237 *usp = (u_char) l;
238 return (TRUE);
240 case XDR_FREE:
241 return (TRUE);
243 return (FALSE);
248 * XDR short integers
250 bool_t
251 xdr_short(XDR * xdrs, short *sp)
253 afs_int32 l;
255 switch (xdrs->x_op) {
257 case XDR_ENCODE:
258 l = (afs_int32) * sp;
259 return (XDR_PUTINT32(xdrs, &l));
261 case XDR_DECODE:
262 if (!XDR_GETINT32(xdrs, &l)) {
263 return (FALSE);
265 *sp = (short)l;
266 return (TRUE);
268 case XDR_FREE:
269 return (TRUE);
271 return (FALSE);
275 * XDR unsigned short integers
277 bool_t
278 xdr_u_short(XDR * xdrs, u_short * usp)
280 afs_uint32 l;
282 switch (xdrs->x_op) {
284 case XDR_ENCODE:
285 l = (afs_uint32) * usp;
286 return (XDR_PUTINT32(xdrs, (afs_int32 *)&l));
288 case XDR_DECODE:
289 if (!XDR_GETINT32(xdrs, (afs_int32 *)&l)) {
290 return (FALSE);
292 *usp = (u_short) l;
293 return (TRUE);
295 case XDR_FREE:
296 return (TRUE);
298 return (FALSE);
303 * XDR booleans
305 bool_t
306 xdr_bool(XDR * xdrs, bool_t * bp)
308 afs_int32 lb;
310 switch (xdrs->x_op) {
312 case XDR_ENCODE:
313 lb = *bp ? XDR_TRUE : XDR_FALSE;
314 return (XDR_PUTINT32(xdrs, &lb));
316 case XDR_DECODE:
317 if (!XDR_GETINT32(xdrs, &lb)) {
318 return (FALSE);
320 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
321 return (TRUE);
323 case XDR_FREE:
324 return (TRUE);
326 return (FALSE);
330 * XDR enumerations
332 bool_t
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));
346 * XDR opaque data
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.
350 bool_t
351 xdr_opaque(XDR * xdrs, caddr_t cp, u_int cnt)
353 u_int rndup;
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
360 if (cnt == 0)
361 return (TRUE);
364 * round byte count to full xdr units
366 rndup = cnt % BYTES_PER_XDR_UNIT;
367 if (rndup > 0)
368 rndup = BYTES_PER_XDR_UNIT - rndup;
370 if (xdrs->x_op == XDR_DECODE) {
371 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
372 return (FALSE);
374 if (rndup == 0)
375 return (TRUE);
376 return (XDR_GETBYTES(xdrs, (caddr_t)crud, rndup));
379 if (xdrs->x_op == XDR_ENCODE) {
380 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
381 return (FALSE);
383 if (rndup == 0)
384 return (TRUE);
385 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
388 if (xdrs->x_op == XDR_FREE) {
389 return (TRUE);
392 return (FALSE);
396 * XDR counted bytes
397 * *cpp is a pointer to the bytes, *sizep is the count.
398 * If *cpp is NULL maxsize bytes are allocated
400 bool_t
401 xdr_bytes(XDR * xdrs, char **cpp, u_int * sizep,
402 u_int maxsize)
404 char *sp = *cpp; /* sp is the actual string pointer */
405 u_int nodesize;
408 * first deal with the length since xdr bytes are counted
410 if (!xdr_u_int(xdrs, sizep)) {
411 return (FALSE);
413 nodesize = *sizep;
414 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
415 return (FALSE);
419 * now deal with the actual bytes
421 switch (xdrs->x_op) {
423 case XDR_DECODE:
424 if (sp == NULL) {
425 *cpp = sp = (char *)osi_alloc(nodesize);
427 if (sp == NULL) {
428 return (FALSE);
430 /* fall into ... */
432 case XDR_ENCODE:
433 return (xdr_opaque(xdrs, sp, nodesize));
435 case XDR_FREE:
436 if (sp != NULL) {
437 osi_free(sp, nodesize);
438 *cpp = NULL;
440 return (TRUE);
442 return (FALSE);
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 *
462 bool_t
463 xdr_union(XDR * xdrs, enum_t * dscmp, caddr_t unp,
464 struct xdr_discrim * choices, xdrproc_t dfault)
466 enum_t dscm;
469 * we deal with the discriminator; it's an enum
471 if (!xdr_enum(xdrs, dscmp)) {
472 return (FALSE);
474 dscm = *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,
489 LASTUNSIGNED));
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.
507 bool_t
508 xdr_string(XDR * xdrs, char **cpp, u_int maxsize)
510 char *sp = *cpp; /* sp is the actual string pointer */
511 u_int size;
512 u_int nodesize;
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) {
521 case XDR_FREE:
522 if (sp == NULL) {
523 return (TRUE); /* already free */
525 /* Fall through */
526 case XDR_ENCODE:
527 size = strlen(sp);
528 break;
529 case XDR_DECODE:
530 break;
533 if (!xdr_u_int(xdrs, &size)) {
534 return (FALSE);
536 if (size > maxsize) {
537 return (FALSE);
539 nodesize = size + 1;
542 * now deal with the actual bytes
544 switch (xdrs->x_op) {
546 case XDR_DECODE:
547 if (sp == NULL)
548 *cpp = sp = (char *)osi_alloc(nodesize);
549 if (sp == NULL) {
550 return (FALSE);
552 sp[size] = 0;
553 /* fall into ... */
555 case XDR_ENCODE:
556 return (xdr_opaque(xdrs, sp, size));
558 case XDR_FREE:
559 if (sp != NULL) {
560 osi_free(sp, nodesize);
561 *cpp = NULL;
563 return (TRUE);
565 return (FALSE);
569 * Wrapper for xdr_string that can be called directly from
570 * routines like clnt_call
572 #ifndef KERNEL
573 bool_t
574 xdr_wrapstring(XDR * xdrs, char **cpp)
576 if (xdr_string(xdrs, cpp, BUFSIZ)) {
577 return (TRUE);
579 return (FALSE);
581 #endif
583 void *
584 xdr_alloc(afs_int32 size)
586 return osi_alloc(size);
589 void
590 xdr_free(xdrproc_t proc, void *obj)
592 XDR x;
594 x.x_op = XDR_FREE;
596 /* See note in xdr.h for the method behind this madness */
597 #if defined(AFS_I386_LINUX26_ENV) && defined(KERNEL) && !defined(UKERNEL)
598 (*proc)(&x, obj, 0);
599 #else
600 (*proc)(&x, obj);
601 #endif
603 #endif /* NeXT */