Update.
[glibc.git] / sunrpc / xdr.c
blobe5d706dd20877335eec1e0990e2b53f1e400f574
1 /* @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC */
2 /*
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)xdr.c 1.35 87/08/12";
32 #endif
35 * xdr.c, Generic XDR routines implementation.
37 * Copyright (C) 1986, Sun Microsystems, Inc.
39 * These are the "generic" xdr routines used to serialize and de-serialize
40 * most common data items. See xdr.h for more info on the interface to
41 * xdr.
44 #include <stdio.h>
45 #include <limits.h>
46 #include <string.h>
48 #include <rpc/types.h>
49 #include <rpc/xdr.h>
52 * constants specific to the xdr "protocol"
54 #define XDR_FALSE ((long) 0)
55 #define XDR_TRUE ((long) 1)
56 #define LASTUNSIGNED ((u_int) 0-1)
59 * for unit alignment
61 static const char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
64 * Free a data structure using XDR
65 * Not a filter, but a convenient utility nonetheless
67 void
68 xdr_free (xdrproc_t proc, char *objp)
70 XDR x;
72 x.x_op = XDR_FREE;
73 (*proc) (&x, objp);
77 * XDR nothing
79 bool_t
80 xdr_void (void)
82 return TRUE;
86 * XDR integers
88 bool_t
89 xdr_int (XDR *xdrs, int *ip)
92 #if INT_MAX < LONG_MAX
93 long l;
95 switch (xdrs->x_op)
97 case XDR_ENCODE:
98 l = (long) *ip;
99 return XDR_PUTLONG (xdrs, &l);
101 case XDR_DECODE:
102 if (!XDR_GETLONG (xdrs, &l))
104 return FALSE;
106 *ip = (int) l;
107 case XDR_FREE:
108 return TRUE;
110 return FALSE;
111 #elif INT_MAX == LONG_MAX
112 return xdr_long (xdrs, (long *) ip);
113 #elif INT_MAX == SHRT_MAX
114 return xdr_short (xdrs, (short *) ip);
115 #else
116 #error unexpected integer sizes in_xdr_int()
117 #endif
121 * XDR unsigned integers
123 bool_t
124 xdr_u_int (XDR *xdrs, u_int *up)
126 #if UINT_MAX < ULONG_MAX
127 u_long l;
129 switch (xdrs->x_op)
131 case XDR_ENCODE:
132 l = (u_long) * up;
133 return XDR_PUTLONG (xdrs, &l);
135 case XDR_DECODE:
136 if (!XDR_GETLONG (xdrs, &l))
138 return FALSE;
140 *up = (u_int) l;
141 case XDR_FREE:
142 return TRUE;
144 return FALSE;
145 #elif UINT_MAX == ULONG_MAX
146 return xdr_u_long (xdrs, (u_long *) up);
147 #elif UINT_MAX == USHRT_MAX
148 return xdr_short (xdrs, (short *) up);
149 #else
150 #error unexpected integer sizes in_xdr_u_int()
151 #endif
155 * XDR long integers
156 * same as xdr_u_long - open coded to save a proc call!
158 bool_t
159 xdr_long (XDR *xdrs, long *lp)
162 if (xdrs->x_op == XDR_ENCODE)
163 return XDR_PUTLONG (xdrs, lp);
165 if (xdrs->x_op == XDR_DECODE)
166 return XDR_GETLONG (xdrs, lp);
168 if (xdrs->x_op == XDR_FREE)
169 return TRUE;
171 return FALSE;
175 * XDR unsigned long integers
176 * same as xdr_long - open coded to save a proc call!
178 bool_t
179 xdr_u_long (XDR *xdrs, u_long *ulp)
181 switch (xdrs->x_op)
183 case XDR_DECODE:
185 long int tmp;
187 if (XDR_GETLONG (xdrs, &tmp) == FALSE)
188 return FALSE;
190 *ulp = (uint32_t) tmp;
191 return TRUE;
194 case XDR_ENCODE:
195 return XDR_PUTLONG (xdrs, (long *) ulp);
197 case XDR_FREE:
198 return TRUE;
200 return FALSE;
204 * XDR hyper integers
205 * same as xdr_u_hyper - open coded to save a proc call!
207 bool_t
208 xdr_hyper (XDR *xdrs, quad_t *llp)
210 long t1;
211 unsigned long int t2;
213 if (xdrs->x_op == XDR_ENCODE)
215 t1 = (long) ((*llp) >> 32);
216 t2 = (long) (*llp);
217 return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
220 if (xdrs->x_op == XDR_DECODE)
222 if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
223 return FALSE;
224 *llp = ((quad_t) t1) << 32;
225 *llp |= t2;
226 return TRUE;
229 if (xdrs->x_op == XDR_FREE)
230 return TRUE;
232 return FALSE;
237 * XDR hyper integers
238 * same as xdr_hyper - open coded to save a proc call!
240 bool_t
241 xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
243 unsigned long t1;
244 unsigned long t2;
246 if (xdrs->x_op == XDR_ENCODE)
248 t1 = (unsigned long) ((*ullp) >> 32);
249 t2 = (unsigned long) (*ullp);
250 return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
253 if (xdrs->x_op == XDR_DECODE)
255 if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
256 return FALSE;
257 *ullp = ((u_quad_t) t1) << 32;
258 *ullp |= t2;
259 return TRUE;
262 if (xdrs->x_op == XDR_FREE)
263 return TRUE;
265 return FALSE;
268 bool_t
269 xdr_longlong_t (XDR *xdrs, quad_t *llp)
271 return xdr_hyper (xdrs, llp);
274 bool_t
275 xdr_u_longlong_t (XDR *xdrs, u_quad_t *ullp)
277 return xdr_u_hyper (xdrs, ullp);
281 * XDR short integers
283 bool_t
284 xdr_short (XDR *xdrs, short *sp)
286 long l;
288 switch (xdrs->x_op)
290 case XDR_ENCODE:
291 l = (long) *sp;
292 return XDR_PUTLONG (xdrs, &l);
294 case XDR_DECODE:
295 if (!XDR_GETLONG (xdrs, &l))
297 return FALSE;
299 *sp = (short) l;
300 return TRUE;
302 case XDR_FREE:
303 return TRUE;
305 return FALSE;
309 * XDR unsigned short integers
311 bool_t
312 xdr_u_short (XDR *xdrs, u_short *usp)
314 u_long l;
316 switch (xdrs->x_op)
318 case XDR_ENCODE:
319 l = (u_long) * usp;
320 return XDR_PUTLONG (xdrs, &l);
322 case XDR_DECODE:
323 if (!XDR_GETLONG (xdrs, &l))
325 return FALSE;
327 *usp = (u_short) l;
328 return TRUE;
330 case XDR_FREE:
331 return TRUE;
333 return FALSE;
338 * XDR a char
340 bool_t
341 xdr_char (XDR *xdrs, char *cp)
343 int i;
345 i = (*cp);
346 if (!xdr_int (xdrs, &i))
348 return FALSE;
350 *cp = i;
351 return TRUE;
355 * XDR an unsigned char
357 bool_t
358 xdr_u_char (XDR *xdrs, u_char *cp)
360 u_int u;
362 u = (*cp);
363 if (!xdr_u_int (xdrs, &u))
365 return FALSE;
367 *cp = u;
368 return TRUE;
372 * XDR booleans
374 bool_t
375 xdr_bool (XDR *xdrs, bool_t *bp)
377 long lb;
379 switch (xdrs->x_op)
381 case XDR_ENCODE:
382 lb = *bp ? XDR_TRUE : XDR_FALSE;
383 return XDR_PUTLONG (xdrs, &lb);
385 case XDR_DECODE:
386 if (!XDR_GETLONG (xdrs, &lb))
388 return FALSE;
390 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
391 return TRUE;
393 case XDR_FREE:
394 return TRUE;
396 return FALSE;
400 * XDR enumerations
402 bool_t
403 xdr_enum (XDR *xdrs, enum_t *ep)
405 enum sizecheck
407 SIZEVAL
408 }; /* used to find the size of an enum */
411 * enums are treated as ints
413 if (sizeof (enum sizecheck) == 4)
415 #if INT_MAX < LONG_MAX
416 long l;
418 switch (xdrs->x_op)
420 case XDR_ENCODE:
421 l = *ep;
422 return XDR_PUTLONG (xdrs, &l);
424 case XDR_DECODE:
425 if (!XDR_GETLONG (xdrs, &l))
427 return FALSE;
429 *ep = l;
430 case XDR_FREE:
431 return TRUE;
434 return FALSE;
435 #else
436 return xdr_long (xdrs, (long *) ep);
437 #endif
439 else if (sizeof (enum sizecheck) == sizeof (short))
441 return xdr_short (xdrs, (short *) ep);
443 else
445 return FALSE;
450 * XDR opaque data
451 * Allows the specification of a fixed size sequence of opaque bytes.
452 * cp points to the opaque object and cnt gives the byte length.
454 bool_t
455 xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
457 u_int rndup;
458 static char crud[BYTES_PER_XDR_UNIT];
461 * if no data we are done
463 if (cnt == 0)
464 return TRUE;
467 * round byte count to full xdr units
469 rndup = cnt % BYTES_PER_XDR_UNIT;
470 if (rndup > 0)
471 rndup = BYTES_PER_XDR_UNIT - rndup;
473 switch (xdrs->x_op)
475 case XDR_DECODE:
476 if (!XDR_GETBYTES (xdrs, cp, cnt))
478 return FALSE;
480 if (rndup == 0)
481 return TRUE;
482 return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
484 case XDR_ENCODE:
485 if (!XDR_PUTBYTES (xdrs, cp, cnt))
487 return FALSE;
489 if (rndup == 0)
490 return TRUE;
491 return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
493 case XDR_FREE:
494 return TRUE;
496 return FALSE;
500 * XDR counted bytes
501 * *cpp is a pointer to the bytes, *sizep is the count.
502 * If *cpp is NULL maxsize bytes are allocated
504 bool_t
505 xdr_bytes (xdrs, cpp, sizep, maxsize)
506 XDR *xdrs;
507 char **cpp;
508 u_int *sizep;
509 u_int maxsize;
511 char *sp = *cpp; /* sp is the actual string pointer */
512 u_int nodesize;
515 * first deal with the length since xdr bytes are counted
517 if (!xdr_u_int (xdrs, sizep))
519 return FALSE;
521 nodesize = *sizep;
522 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
524 return FALSE;
528 * now deal with the actual bytes
530 switch (xdrs->x_op)
532 case XDR_DECODE:
533 if (nodesize == 0)
535 return TRUE;
537 if (sp == NULL)
539 *cpp = sp = (char *) mem_alloc (nodesize);
541 if (sp == NULL)
543 (void) fprintf (stderr, "xdr_bytes: out of memory\n");
544 return FALSE;
546 /* fall into ... */
548 case XDR_ENCODE:
549 return xdr_opaque (xdrs, sp, nodesize);
551 case XDR_FREE:
552 if (sp != NULL)
554 mem_free (sp, nodesize);
555 *cpp = NULL;
557 return TRUE;
559 return FALSE;
563 * Implemented here due to commonality of the object.
565 bool_t
566 xdr_netobj (xdrs, np)
567 XDR *xdrs;
568 struct netobj *np;
571 return xdr_bytes (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
575 * XDR a discriminated union
576 * Support routine for discriminated unions.
577 * You create an array of xdrdiscrim structures, terminated with
578 * an entry with a null procedure pointer. The routine gets
579 * the discriminant value and then searches the array of xdrdiscrims
580 * looking for that value. It calls the procedure given in the xdrdiscrim
581 * to handle the discriminant. If there is no specific routine a default
582 * routine may be called.
583 * If there is no specific or default routine an error is returned.
585 bool_t
586 xdr_union (xdrs, dscmp, unp, choices, dfault)
587 XDR *xdrs;
588 enum_t *dscmp; /* enum to decide which arm to work on */
589 char *unp; /* the union itself */
590 const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
591 xdrproc_t dfault; /* default xdr routine */
593 enum_t dscm;
596 * we deal with the discriminator; it's an enum
598 if (!xdr_enum (xdrs, dscmp))
600 return FALSE;
602 dscm = *dscmp;
605 * search choices for a value that matches the discriminator.
606 * if we find one, execute the xdr routine for that value.
608 for (; choices->proc != NULL_xdrproc_t; choices++)
610 if (choices->value == dscm)
611 return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
615 * no match - execute the default xdr routine if there is one
617 return ((dfault == NULL_xdrproc_t) ? FALSE :
618 (*dfault) (xdrs, unp, LASTUNSIGNED));
623 * Non-portable xdr primitives.
624 * Care should be taken when moving these routines to new architectures.
629 * XDR null terminated ASCII strings
630 * xdr_string deals with "C strings" - arrays of bytes that are
631 * terminated by a NULL character. The parameter cpp references a
632 * pointer to storage; If the pointer is null, then the necessary
633 * storage is allocated. The last parameter is the max allowed length
634 * of the string as specified by a protocol.
636 bool_t
637 xdr_string (xdrs, cpp, maxsize)
638 XDR *xdrs;
639 char **cpp;
640 u_int maxsize;
642 char *sp = *cpp; /* sp is the actual string pointer */
643 u_int size;
644 u_int nodesize;
647 * first deal with the length since xdr strings are counted-strings
649 switch (xdrs->x_op)
651 case XDR_FREE:
652 if (sp == NULL)
654 return TRUE; /* already free */
656 /* fall through... */
657 case XDR_ENCODE:
658 if (sp == NULL)
659 return FALSE;
660 size = strlen (sp);
661 break;
662 case XDR_DECODE:
663 break;
665 if (!xdr_u_int (xdrs, &size))
667 return FALSE;
669 if (size > maxsize)
671 return FALSE;
673 nodesize = size + 1;
676 * now deal with the actual bytes
678 switch (xdrs->x_op)
680 case XDR_DECODE:
681 if (nodesize == 0)
683 return TRUE;
685 if (sp == NULL)
686 *cpp = sp = (char *) mem_alloc (nodesize);
687 if (sp == NULL)
689 (void) fprintf (stderr, "xdr_string: out of memory\n");
690 return FALSE;
692 sp[size] = 0;
693 /* fall into ... */
695 case XDR_ENCODE:
696 return xdr_opaque (xdrs, sp, size);
698 case XDR_FREE:
699 mem_free (sp, nodesize);
700 *cpp = NULL;
701 return TRUE;
703 return FALSE;
707 * Wrapper for xdr_string that can be called directly from
708 * routines like clnt_call
710 bool_t
711 xdr_wrapstring (xdrs, cpp)
712 XDR *xdrs;
713 char **cpp;
715 if (xdr_string (xdrs, cpp, LASTUNSIGNED))
717 return TRUE;
719 return FALSE;