Update.
[glibc.git] / sunrpc / xdr.c
blob6f1aed742f6076f904106f39bb866be34c7d016b
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:
184 return XDR_GETLONG (xdrs, (long *) ulp);
186 case XDR_ENCODE:
187 return XDR_PUTLONG (xdrs, (long *) ulp);
189 case XDR_FREE:
190 return TRUE;
192 return FALSE;
196 * XDR hyper integers
197 * same as xdr_u_hyper - open coded to save a proc call!
199 bool_t
200 xdr_hyper (XDR *xdrs, quad_t *llp)
202 long t1;
203 long t2;
205 if (xdrs->x_op == XDR_ENCODE)
207 t1 = (long) ((*llp) >> 32);
208 t2 = (long) (*llp);
209 return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
212 if (xdrs->x_op == XDR_DECODE)
214 if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
215 return FALSE;
216 *llp = ((quad_t) t1) << 32;
217 *llp |= t2;
218 return TRUE;
221 if (xdrs->x_op == XDR_FREE)
222 return TRUE;
224 return FALSE;
229 * XDR hyper integers
230 * same as xdr_hyper - open coded to save a proc call!
232 bool_t
233 xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
235 unsigned long t1;
236 unsigned long t2;
238 if (xdrs->x_op == XDR_ENCODE)
240 t1 = (unsigned long) ((*ullp) >> 32);
241 t2 = (unsigned long) (*ullp);
242 return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
245 if (xdrs->x_op == XDR_DECODE)
247 if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
248 return FALSE;
249 *ullp = ((u_quad_t) t1) << 32;
250 *ullp |= t2;
251 return TRUE;
254 if (xdrs->x_op == XDR_FREE)
255 return TRUE;
257 return FALSE;
260 bool_t
261 xdr_longlong_t (XDR *xdrs, quad_t *llp)
263 return xdr_hyper (xdrs, llp);
266 bool_t
267 xdr_u_longlong_t (XDR *xdrs, u_quad_t *ullp)
269 return xdr_u_hyper (xdrs, ullp);
273 * XDR short integers
275 bool_t
276 xdr_short (XDR *xdrs, short *sp)
278 long l;
280 switch (xdrs->x_op)
282 case XDR_ENCODE:
283 l = (long) *sp;
284 return XDR_PUTLONG (xdrs, &l);
286 case XDR_DECODE:
287 if (!XDR_GETLONG (xdrs, &l))
289 return FALSE;
291 *sp = (short) l;
292 return TRUE;
294 case XDR_FREE:
295 return TRUE;
297 return FALSE;
301 * XDR unsigned short integers
303 bool_t
304 xdr_u_short (XDR *xdrs, u_short *usp)
306 u_long l;
308 switch (xdrs->x_op)
310 case XDR_ENCODE:
311 l = (u_long) * usp;
312 return XDR_PUTLONG (xdrs, &l);
314 case XDR_DECODE:
315 if (!XDR_GETLONG (xdrs, &l))
317 return FALSE;
319 *usp = (u_short) l;
320 return TRUE;
322 case XDR_FREE:
323 return TRUE;
325 return FALSE;
330 * XDR a char
332 bool_t
333 xdr_char (XDR *xdrs, char *cp)
335 int i;
337 i = (*cp);
338 if (!xdr_int (xdrs, &i))
340 return FALSE;
342 *cp = i;
343 return TRUE;
347 * XDR an unsigned char
349 bool_t
350 xdr_u_char (XDR *xdrs, u_char *cp)
352 u_int u;
354 u = (*cp);
355 if (!xdr_u_int (xdrs, &u))
357 return FALSE;
359 *cp = u;
360 return TRUE;
364 * XDR booleans
366 bool_t
367 xdr_bool (XDR *xdrs, bool_t *bp)
369 long lb;
371 switch (xdrs->x_op)
373 case XDR_ENCODE:
374 lb = *bp ? XDR_TRUE : XDR_FALSE;
375 return XDR_PUTLONG (xdrs, &lb);
377 case XDR_DECODE:
378 if (!XDR_GETLONG (xdrs, &lb))
380 return FALSE;
382 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
383 return TRUE;
385 case XDR_FREE:
386 return TRUE;
388 return FALSE;
392 * XDR enumerations
394 bool_t
395 xdr_enum (XDR *xdrs, enum_t *ep)
397 enum sizecheck
399 SIZEVAL
400 }; /* used to find the size of an enum */
403 * enums are treated as ints
405 if (sizeof (enum sizecheck) == 4)
407 #if INT_MAX < LONG_MAX
408 long l;
410 switch (xdrs->x_op)
412 case XDR_ENCODE:
413 l = *ep;
414 return XDR_PUTLONG (xdrs, &l);
416 case XDR_DECODE:
417 if (!XDR_GETLONG (xdrs, &l))
419 return FALSE;
421 *ep = l;
422 case XDR_FREE:
423 return TRUE;
426 return FALSE;
427 #else
428 return xdr_long (xdrs, (long *) ep);
429 #endif
431 else if (sizeof (enum sizecheck) == sizeof (short))
433 return xdr_short (xdrs, (short *) ep);
435 else
437 return FALSE;
442 * XDR opaque data
443 * Allows the specification of a fixed size sequence of opaque bytes.
444 * cp points to the opaque object and cnt gives the byte length.
446 bool_t
447 xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
449 u_int rndup;
450 static char crud[BYTES_PER_XDR_UNIT];
453 * if no data we are done
455 if (cnt == 0)
456 return TRUE;
459 * round byte count to full xdr units
461 rndup = cnt % BYTES_PER_XDR_UNIT;
462 if (rndup > 0)
463 rndup = BYTES_PER_XDR_UNIT - rndup;
465 switch (xdrs->x_op)
467 case XDR_DECODE:
468 if (!XDR_GETBYTES (xdrs, cp, cnt))
470 return FALSE;
472 if (rndup == 0)
473 return TRUE;
474 return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
476 case XDR_ENCODE:
477 if (!XDR_PUTBYTES (xdrs, cp, cnt))
479 return FALSE;
481 if (rndup == 0)
482 return TRUE;
483 return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
485 case XDR_FREE:
486 return TRUE;
488 return FALSE;
492 * XDR counted bytes
493 * *cpp is a pointer to the bytes, *sizep is the count.
494 * If *cpp is NULL maxsize bytes are allocated
496 bool_t
497 xdr_bytes (xdrs, cpp, sizep, maxsize)
498 XDR *xdrs;
499 char **cpp;
500 u_int *sizep;
501 u_int maxsize;
503 char *sp = *cpp; /* sp is the actual string pointer */
504 u_int nodesize;
507 * first deal with the length since xdr bytes are counted
509 if (!xdr_u_int (xdrs, sizep))
511 return FALSE;
513 nodesize = *sizep;
514 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
516 return FALSE;
520 * now deal with the actual bytes
522 switch (xdrs->x_op)
524 case XDR_DECODE:
525 if (nodesize == 0)
527 return TRUE;
529 if (sp == NULL)
531 *cpp = sp = (char *) mem_alloc (nodesize);
533 if (sp == NULL)
535 (void) fprintf (stderr, "xdr_bytes: out of memory\n");
536 return FALSE;
538 /* fall into ... */
540 case XDR_ENCODE:
541 return xdr_opaque (xdrs, sp, nodesize);
543 case XDR_FREE:
544 if (sp != NULL)
546 mem_free (sp, nodesize);
547 *cpp = NULL;
549 return TRUE;
551 return FALSE;
555 * Implemented here due to commonality of the object.
557 bool_t
558 xdr_netobj (xdrs, np)
559 XDR *xdrs;
560 struct netobj *np;
563 return xdr_bytes (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
567 * XDR a discriminated union
568 * Support routine for discriminated unions.
569 * You create an array of xdrdiscrim structures, terminated with
570 * an entry with a null procedure pointer. The routine gets
571 * the discriminant value and then searches the array of xdrdiscrims
572 * looking for that value. It calls the procedure given in the xdrdiscrim
573 * to handle the discriminant. If there is no specific routine a default
574 * routine may be called.
575 * If there is no specific or default routine an error is returned.
577 bool_t
578 xdr_union (xdrs, dscmp, unp, choices, dfault)
579 XDR *xdrs;
580 enum_t *dscmp; /* enum to decide which arm to work on */
581 char *unp; /* the union itself */
582 const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
583 xdrproc_t dfault; /* default xdr routine */
585 enum_t dscm;
588 * we deal with the discriminator; it's an enum
590 if (!xdr_enum (xdrs, dscmp))
592 return FALSE;
594 dscm = *dscmp;
597 * search choices for a value that matches the discriminator.
598 * if we find one, execute the xdr routine for that value.
600 for (; choices->proc != NULL_xdrproc_t; choices++)
602 if (choices->value == dscm)
603 return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
607 * no match - execute the default xdr routine if there is one
609 return ((dfault == NULL_xdrproc_t) ? FALSE :
610 (*dfault) (xdrs, unp, LASTUNSIGNED));
615 * Non-portable xdr primitives.
616 * Care should be taken when moving these routines to new architectures.
621 * XDR null terminated ASCII strings
622 * xdr_string deals with "C strings" - arrays of bytes that are
623 * terminated by a NULL character. The parameter cpp references a
624 * pointer to storage; If the pointer is null, then the necessary
625 * storage is allocated. The last parameter is the max allowed length
626 * of the string as specified by a protocol.
628 bool_t
629 xdr_string (xdrs, cpp, maxsize)
630 XDR *xdrs;
631 char **cpp;
632 u_int maxsize;
634 char *sp = *cpp; /* sp is the actual string pointer */
635 u_int size;
636 u_int nodesize;
639 * first deal with the length since xdr strings are counted-strings
641 switch (xdrs->x_op)
643 case XDR_FREE:
644 if (sp == NULL)
646 return TRUE; /* already free */
648 /* fall through... */
649 case XDR_ENCODE:
650 if (sp == NULL)
651 return FALSE;
652 size = strlen (sp);
653 break;
654 case XDR_DECODE:
655 break;
657 if (!xdr_u_int (xdrs, &size))
659 return FALSE;
661 if (size > maxsize)
663 return FALSE;
665 nodesize = size + 1;
668 * now deal with the actual bytes
670 switch (xdrs->x_op)
672 case XDR_DECODE:
673 if (nodesize == 0)
675 return TRUE;
677 if (sp == NULL)
678 *cpp = sp = (char *) mem_alloc (nodesize);
679 if (sp == NULL)
681 (void) fprintf (stderr, "xdr_string: out of memory\n");
682 return FALSE;
684 sp[size] = 0;
685 /* fall into ... */
687 case XDR_ENCODE:
688 return xdr_opaque (xdrs, sp, size);
690 case XDR_FREE:
691 mem_free (sp, nodesize);
692 *cpp = NULL;
693 return TRUE;
695 return FALSE;
699 * Wrapper for xdr_string that can be called directly from
700 * routines like clnt_call
702 bool_t
703 xdr_wrapstring (xdrs, cpp)
704 XDR *xdrs;
705 char **cpp;
707 if (xdr_string (xdrs, cpp, LASTUNSIGNED))
709 return TRUE;
711 return FALSE;