Add the missing "; \".
[glibc.git] / sunrpc / xdr.c
blobc5e961f52bc2da641347044078b970a5d6115427
1 /*
2 * xdr.c, Generic XDR routines implementation.
4 * Copyright (C) 1986, Sun Microsystems, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of Sun Microsystems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * These are the "generic" xdr routines used to serialize and de-serialize
34 * most common data items. See xdr.h for more info on the interface to
35 * xdr.
38 #include <stdio.h>
39 #include <limits.h>
40 #include <string.h>
41 #include <libintl.h>
43 #include <rpc/types.h>
44 #include <rpc/xdr.h>
46 #ifdef USE_IN_LIBIO
47 # include <wchar.h>
48 #endif
51 * constants specific to the xdr "protocol"
53 #define XDR_FALSE ((long) 0)
54 #define XDR_TRUE ((long) 1)
55 #define LASTUNSIGNED ((u_int) 0-1)
58 * for unit alignment
60 static const char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
63 * Free a data structure using XDR
64 * Not a filter, but a convenient utility nonetheless
66 void
67 xdr_free (xdrproc_t proc, char *objp)
69 XDR x;
71 x.x_op = XDR_FREE;
72 (*proc) (&x, objp);
76 * XDR nothing
78 bool_t
79 xdr_void (void)
81 return TRUE;
83 INTDEF(xdr_void)
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 INTUSE(xdr_long) (xdrs, (long *) ip);
113 #elif INT_MAX == SHRT_MAX
114 return INTUSE(xdr_short) (xdrs, (short *) ip);
115 #else
116 #error unexpected integer sizes in_xdr_int()
117 #endif
119 INTDEF(xdr_int)
122 * XDR unsigned integers
124 bool_t
125 xdr_u_int (XDR *xdrs, u_int *up)
127 #if UINT_MAX < ULONG_MAX
128 long l;
130 switch (xdrs->x_op)
132 case XDR_ENCODE:
133 l = (u_long) * up;
134 return XDR_PUTLONG (xdrs, &l);
136 case XDR_DECODE:
137 if (!XDR_GETLONG (xdrs, &l))
139 return FALSE;
141 *up = (u_int) (u_long) l;
142 case XDR_FREE:
143 return TRUE;
145 return FALSE;
146 #elif UINT_MAX == ULONG_MAX
147 return INTUSE(xdr_u_long) (xdrs, (u_long *) up);
148 #elif UINT_MAX == USHRT_MAX
149 return INTUSE(xdr_short) (xdrs, (short *) up);
150 #else
151 #error unexpected integer sizes in_xdr_u_int()
152 #endif
154 INTDEF(xdr_u_int)
157 * XDR long integers
158 * The definition of xdr_long() is kept for backward
159 * compatibility. Instead xdr_int() should be used.
161 bool_t
162 xdr_long (XDR *xdrs, long *lp)
165 if (xdrs->x_op == XDR_ENCODE
166 && (sizeof (int32_t) == sizeof (long)
167 || (int32_t) *lp == *lp))
168 return XDR_PUTLONG (xdrs, lp);
170 if (xdrs->x_op == XDR_DECODE)
171 return XDR_GETLONG (xdrs, lp);
173 if (xdrs->x_op == XDR_FREE)
174 return TRUE;
176 return FALSE;
178 INTDEF(xdr_long)
181 * XDR unsigned long integers
182 * The definition of xdr_u_long() is kept for backward
183 * compatibility. Instead xdr_u_int() should be used.
185 bool_t
186 xdr_u_long (XDR *xdrs, u_long *ulp)
188 switch (xdrs->x_op)
190 case XDR_DECODE:
192 long int tmp;
194 if (XDR_GETLONG (xdrs, &tmp) == FALSE)
195 return FALSE;
197 *ulp = (uint32_t) tmp;
198 return TRUE;
201 case XDR_ENCODE:
202 if (sizeof (uint32_t) != sizeof (u_long)
203 && (uint32_t) *ulp != *ulp)
204 return FALSE;
206 return XDR_PUTLONG (xdrs, (long *) ulp);
208 case XDR_FREE:
209 return TRUE;
211 return FALSE;
213 INTDEF(xdr_u_long)
216 * XDR hyper integers
217 * same as xdr_u_hyper - open coded to save a proc call!
219 bool_t
220 xdr_hyper (XDR *xdrs, quad_t *llp)
222 long int t1, t2;
224 if (xdrs->x_op == XDR_ENCODE)
226 t1 = (long) ((*llp) >> 32);
227 t2 = (long) (*llp);
228 return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
231 if (xdrs->x_op == XDR_DECODE)
233 if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
234 return FALSE;
235 *llp = ((quad_t) t1) << 32;
236 *llp |= (uint32_t) t2;
237 return TRUE;
240 if (xdrs->x_op == XDR_FREE)
241 return TRUE;
243 return FALSE;
245 INTDEF(xdr_hyper)
249 * XDR hyper integers
250 * same as xdr_hyper - open coded to save a proc call!
252 bool_t
253 xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
255 long int t1, t2;
257 if (xdrs->x_op == XDR_ENCODE)
259 t1 = (unsigned long) ((*ullp) >> 32);
260 t2 = (unsigned long) (*ullp);
261 return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
264 if (xdrs->x_op == XDR_DECODE)
266 if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
267 return FALSE;
268 *ullp = ((u_quad_t) t1) << 32;
269 *ullp |= (uint32_t) t2;
270 return TRUE;
273 if (xdrs->x_op == XDR_FREE)
274 return TRUE;
276 return FALSE;
278 INTDEF(xdr_u_hyper)
280 bool_t
281 xdr_longlong_t (XDR *xdrs, quad_t *llp)
283 return INTUSE(xdr_hyper) (xdrs, llp);
286 bool_t
287 xdr_u_longlong_t (XDR *xdrs, u_quad_t *ullp)
289 return INTUSE(xdr_u_hyper) (xdrs, ullp);
293 * XDR short integers
295 bool_t
296 xdr_short (XDR *xdrs, short *sp)
298 long l;
300 switch (xdrs->x_op)
302 case XDR_ENCODE:
303 l = (long) *sp;
304 return XDR_PUTLONG (xdrs, &l);
306 case XDR_DECODE:
307 if (!XDR_GETLONG (xdrs, &l))
309 return FALSE;
311 *sp = (short) l;
312 return TRUE;
314 case XDR_FREE:
315 return TRUE;
317 return FALSE;
319 INTDEF(xdr_short)
322 * XDR unsigned short integers
324 bool_t
325 xdr_u_short (XDR *xdrs, u_short *usp)
327 long l;
329 switch (xdrs->x_op)
331 case XDR_ENCODE:
332 l = (u_long) * usp;
333 return XDR_PUTLONG (xdrs, &l);
335 case XDR_DECODE:
336 if (!XDR_GETLONG (xdrs, &l))
338 return FALSE;
340 *usp = (u_short) (u_long) l;
341 return TRUE;
343 case XDR_FREE:
344 return TRUE;
346 return FALSE;
348 INTDEF(xdr_u_short)
352 * XDR a char
354 bool_t
355 xdr_char (XDR *xdrs, char *cp)
357 int i;
359 i = (*cp);
360 if (!INTUSE(xdr_int) (xdrs, &i))
362 return FALSE;
364 *cp = i;
365 return TRUE;
369 * XDR an unsigned char
371 bool_t
372 xdr_u_char (XDR *xdrs, u_char *cp)
374 u_int u;
376 u = (*cp);
377 if (!INTUSE(xdr_u_int) (xdrs, &u))
379 return FALSE;
381 *cp = u;
382 return TRUE;
386 * XDR booleans
388 bool_t
389 xdr_bool (XDR *xdrs, bool_t *bp)
391 long lb;
393 switch (xdrs->x_op)
395 case XDR_ENCODE:
396 lb = *bp ? XDR_TRUE : XDR_FALSE;
397 return XDR_PUTLONG (xdrs, &lb);
399 case XDR_DECODE:
400 if (!XDR_GETLONG (xdrs, &lb))
402 return FALSE;
404 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
405 return TRUE;
407 case XDR_FREE:
408 return TRUE;
410 return FALSE;
412 INTDEF(xdr_bool)
415 * XDR enumerations
417 bool_t
418 xdr_enum (XDR *xdrs, enum_t *ep)
420 enum sizecheck
422 SIZEVAL
423 }; /* used to find the size of an enum */
426 * enums are treated as ints
428 if (sizeof (enum sizecheck) == 4)
430 #if INT_MAX < LONG_MAX
431 long l;
433 switch (xdrs->x_op)
435 case XDR_ENCODE:
436 l = *ep;
437 return XDR_PUTLONG (xdrs, &l);
439 case XDR_DECODE:
440 if (!XDR_GETLONG (xdrs, &l))
442 return FALSE;
444 *ep = l;
445 case XDR_FREE:
446 return TRUE;
449 return FALSE;
450 #else
451 return INTUSE(xdr_long) (xdrs, (long *) ep);
452 #endif
454 else if (sizeof (enum sizecheck) == sizeof (short))
456 return INTUSE(xdr_short) (xdrs, (short *) ep);
458 else
460 return FALSE;
463 INTDEF(xdr_enum)
466 * XDR opaque data
467 * Allows the specification of a fixed size sequence of opaque bytes.
468 * cp points to the opaque object and cnt gives the byte length.
470 bool_t
471 xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
473 u_int rndup;
474 static char crud[BYTES_PER_XDR_UNIT];
477 * if no data we are done
479 if (cnt == 0)
480 return TRUE;
483 * round byte count to full xdr units
485 rndup = cnt % BYTES_PER_XDR_UNIT;
486 if (rndup > 0)
487 rndup = BYTES_PER_XDR_UNIT - rndup;
489 switch (xdrs->x_op)
491 case XDR_DECODE:
492 if (!XDR_GETBYTES (xdrs, cp, cnt))
494 return FALSE;
496 if (rndup == 0)
497 return TRUE;
498 return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
500 case XDR_ENCODE:
501 if (!XDR_PUTBYTES (xdrs, cp, cnt))
503 return FALSE;
505 if (rndup == 0)
506 return TRUE;
507 return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
509 case XDR_FREE:
510 return TRUE;
512 return FALSE;
514 INTDEF(xdr_opaque)
517 * XDR counted bytes
518 * *cpp is a pointer to the bytes, *sizep is the count.
519 * If *cpp is NULL maxsize bytes are allocated
521 bool_t
522 xdr_bytes (xdrs, cpp, sizep, maxsize)
523 XDR *xdrs;
524 char **cpp;
525 u_int *sizep;
526 u_int maxsize;
528 char *sp = *cpp; /* sp is the actual string pointer */
529 u_int nodesize;
532 * first deal with the length since xdr bytes are counted
534 if (!INTUSE(xdr_u_int) (xdrs, sizep))
536 return FALSE;
538 nodesize = *sizep;
539 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
541 return FALSE;
545 * now deal with the actual bytes
547 switch (xdrs->x_op)
549 case XDR_DECODE:
550 if (nodesize == 0)
552 return TRUE;
554 if (sp == NULL)
556 *cpp = sp = (char *) mem_alloc (nodesize);
558 if (sp == NULL)
560 (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
561 return FALSE;
563 /* fall into ... */
565 case XDR_ENCODE:
566 return INTUSE(xdr_opaque) (xdrs, sp, nodesize);
568 case XDR_FREE:
569 if (sp != NULL)
571 mem_free (sp, nodesize);
572 *cpp = NULL;
574 return TRUE;
576 return FALSE;
578 INTDEF(xdr_bytes)
581 * Implemented here due to commonality of the object.
583 bool_t
584 xdr_netobj (xdrs, np)
585 XDR *xdrs;
586 struct netobj *np;
589 return INTUSE(xdr_bytes) (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
591 INTDEF(xdr_netobj)
594 * XDR a discriminated union
595 * Support routine for discriminated unions.
596 * You create an array of xdrdiscrim structures, terminated with
597 * an entry with a null procedure pointer. The routine gets
598 * the discriminant value and then searches the array of xdrdiscrims
599 * looking for that value. It calls the procedure given in the xdrdiscrim
600 * to handle the discriminant. If there is no specific routine a default
601 * routine may be called.
602 * If there is no specific or default routine an error is returned.
604 bool_t
605 xdr_union (xdrs, dscmp, unp, choices, dfault)
606 XDR *xdrs;
607 enum_t *dscmp; /* enum to decide which arm to work on */
608 char *unp; /* the union itself */
609 const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
610 xdrproc_t dfault; /* default xdr routine */
612 enum_t dscm;
615 * we deal with the discriminator; it's an enum
617 if (!INTUSE(xdr_enum) (xdrs, dscmp))
619 return FALSE;
621 dscm = *dscmp;
624 * search choices for a value that matches the discriminator.
625 * if we find one, execute the xdr routine for that value.
627 for (; choices->proc != NULL_xdrproc_t; choices++)
629 if (choices->value == dscm)
630 return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
634 * no match - execute the default xdr routine if there is one
636 return ((dfault == NULL_xdrproc_t) ? FALSE :
637 (*dfault) (xdrs, unp, LASTUNSIGNED));
639 INTDEF(xdr_union)
643 * Non-portable xdr primitives.
644 * Care should be taken when moving these routines to new architectures.
649 * XDR null terminated ASCII strings
650 * xdr_string deals with "C strings" - arrays of bytes that are
651 * terminated by a NULL character. The parameter cpp references a
652 * pointer to storage; If the pointer is null, then the necessary
653 * storage is allocated. The last parameter is the max allowed length
654 * of the string as specified by a protocol.
656 bool_t
657 xdr_string (xdrs, cpp, maxsize)
658 XDR *xdrs;
659 char **cpp;
660 u_int maxsize;
662 char *sp = *cpp; /* sp is the actual string pointer */
663 u_int size;
664 u_int nodesize;
667 * first deal with the length since xdr strings are counted-strings
669 switch (xdrs->x_op)
671 case XDR_FREE:
672 if (sp == NULL)
674 return TRUE; /* already free */
676 /* fall through... */
677 case XDR_ENCODE:
678 if (sp == NULL)
679 return FALSE;
680 size = strlen (sp);
681 break;
682 case XDR_DECODE:
683 break;
685 if (!INTUSE(xdr_u_int) (xdrs, &size))
687 return FALSE;
689 if (size > maxsize)
691 return FALSE;
693 nodesize = size + 1;
694 if (nodesize == 0)
696 /* This means an overflow. It a bug in the caller which
697 provided a too large maxsize but nevertheless catch it
698 here. */
699 return FALSE;
703 * now deal with the actual bytes
705 switch (xdrs->x_op)
707 case XDR_DECODE:
708 if (sp == NULL)
709 *cpp = sp = (char *) mem_alloc (nodesize);
710 if (sp == NULL)
712 (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
713 return FALSE;
715 sp[size] = 0;
716 /* fall into ... */
718 case XDR_ENCODE:
719 return INTUSE(xdr_opaque) (xdrs, sp, size);
721 case XDR_FREE:
722 mem_free (sp, nodesize);
723 *cpp = NULL;
724 return TRUE;
726 return FALSE;
728 INTDEF(xdr_string)
731 * Wrapper for xdr_string that can be called directly from
732 * routines like clnt_call
734 bool_t
735 xdr_wrapstring (xdrs, cpp)
736 XDR *xdrs;
737 char **cpp;
739 if (INTUSE(xdr_string) (xdrs, cpp, LASTUNSIGNED))
741 return TRUE;
743 return FALSE;