* sysdeps/m68k/setjmp.c: Also define setjmp and _setjmp if
[glibc.git] / sunrpc / xdr.c
blobcc1aa2c2b97992d260121cf98e3f9413af6cfa77
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>
47 #include <libintl.h>
49 #include <rpc/types.h>
50 #include <rpc/xdr.h>
52 #ifdef USE_IN_LIBIO
53 # include <wchar.h>
54 #endif
57 * constants specific to the xdr "protocol"
59 #define XDR_FALSE ((long) 0)
60 #define XDR_TRUE ((long) 1)
61 #define LASTUNSIGNED ((u_int) 0-1)
64 * for unit alignment
66 static const char xdr_zero[BYTES_PER_XDR_UNIT] = {0, 0, 0, 0};
69 * Free a data structure using XDR
70 * Not a filter, but a convenient utility nonetheless
72 void
73 xdr_free (xdrproc_t proc, char *objp)
75 XDR x;
77 x.x_op = XDR_FREE;
78 (*proc) (&x, objp);
82 * XDR nothing
84 bool_t
85 xdr_void (void)
87 return TRUE;
91 * XDR integers
93 bool_t
94 xdr_int (XDR *xdrs, int *ip)
97 #if INT_MAX < LONG_MAX
98 long l;
100 switch (xdrs->x_op)
102 case XDR_ENCODE:
103 l = (long) *ip;
104 return XDR_PUTLONG (xdrs, &l);
106 case XDR_DECODE:
107 if (!XDR_GETLONG (xdrs, &l))
109 return FALSE;
111 *ip = (int) l;
112 case XDR_FREE:
113 return TRUE;
115 return FALSE;
116 #elif INT_MAX == LONG_MAX
117 return xdr_long (xdrs, (long *) ip);
118 #elif INT_MAX == SHRT_MAX
119 return xdr_short (xdrs, (short *) ip);
120 #else
121 #error unexpected integer sizes in_xdr_int()
122 #endif
126 * XDR unsigned integers
128 bool_t
129 xdr_u_int (XDR *xdrs, u_int *up)
131 #if UINT_MAX < ULONG_MAX
132 u_long l;
134 switch (xdrs->x_op)
136 case XDR_ENCODE:
137 l = (u_long) * up;
138 return XDR_PUTLONG (xdrs, &l);
140 case XDR_DECODE:
141 if (!XDR_GETLONG (xdrs, &l))
143 return FALSE;
145 *up = (u_int) l;
146 case XDR_FREE:
147 return TRUE;
149 return FALSE;
150 #elif UINT_MAX == ULONG_MAX
151 return xdr_u_long (xdrs, (u_long *) up);
152 #elif UINT_MAX == USHRT_MAX
153 return xdr_short (xdrs, (short *) up);
154 #else
155 #error unexpected integer sizes in_xdr_u_int()
156 #endif
160 * XDR long integers
161 * The definition of xdr_long() is kept for backward
162 * compatibility. Instead xdr_int() should be used.
164 bool_t
165 xdr_long (XDR *xdrs, long *lp)
168 if (xdrs->x_op == XDR_ENCODE
169 && (sizeof (int32_t) == sizeof (long)
170 || (int32_t) *lp == *lp))
171 return XDR_PUTLONG (xdrs, lp);
173 if (xdrs->x_op == XDR_DECODE)
174 return XDR_GETLONG (xdrs, lp);
176 if (xdrs->x_op == XDR_FREE)
177 return TRUE;
179 return FALSE;
183 * XDR unsigned long integers
184 * The definition of xdr_u_long() is kept for backward
185 * compatibility. Instead xdr_u_int() should be used.
187 bool_t
188 xdr_u_long (XDR *xdrs, u_long *ulp)
190 switch (xdrs->x_op)
192 case XDR_DECODE:
194 long int tmp;
196 if (XDR_GETLONG (xdrs, &tmp) == FALSE)
197 return FALSE;
199 *ulp = (uint32_t) tmp;
200 return TRUE;
203 case XDR_ENCODE:
204 if (sizeof (uint32_t) != sizeof (u_long)
205 && (uint32_t) *ulp != *ulp)
206 return FALSE;
208 return XDR_PUTLONG (xdrs, (long *) ulp);
210 case XDR_FREE:
211 return TRUE;
213 return FALSE;
217 * XDR hyper integers
218 * same as xdr_u_hyper - open coded to save a proc call!
220 bool_t
221 xdr_hyper (XDR *xdrs, quad_t *llp)
223 long t1;
224 unsigned long int t2;
226 if (xdrs->x_op == XDR_ENCODE)
228 t1 = (long) ((*llp) >> 32);
229 t2 = (long) (*llp);
230 return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
233 if (xdrs->x_op == XDR_DECODE)
235 if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
236 return FALSE;
237 *llp = ((quad_t) t1) << 32;
238 *llp |= t2;
239 return TRUE;
242 if (xdrs->x_op == XDR_FREE)
243 return TRUE;
245 return FALSE;
250 * XDR hyper integers
251 * same as xdr_hyper - open coded to save a proc call!
253 bool_t
254 xdr_u_hyper (XDR *xdrs, u_quad_t *ullp)
256 unsigned long t1;
257 unsigned long t2;
259 if (xdrs->x_op == XDR_ENCODE)
261 t1 = (unsigned long) ((*ullp) >> 32);
262 t2 = (unsigned long) (*ullp);
263 return (XDR_PUTLONG(xdrs, &t1) && XDR_PUTLONG(xdrs, &t2));
266 if (xdrs->x_op == XDR_DECODE)
268 if (!XDR_GETLONG(xdrs, &t1) || !XDR_GETLONG(xdrs, &t2))
269 return FALSE;
270 *ullp = ((u_quad_t) t1) << 32;
271 *ullp |= t2;
272 return TRUE;
275 if (xdrs->x_op == XDR_FREE)
276 return TRUE;
278 return FALSE;
281 bool_t
282 xdr_longlong_t (XDR *xdrs, quad_t *llp)
284 return xdr_hyper (xdrs, llp);
287 bool_t
288 xdr_u_longlong_t (XDR *xdrs, u_quad_t *ullp)
290 return xdr_u_hyper (xdrs, ullp);
294 * XDR short integers
296 bool_t
297 xdr_short (XDR *xdrs, short *sp)
299 long l;
301 switch (xdrs->x_op)
303 case XDR_ENCODE:
304 l = (long) *sp;
305 return XDR_PUTLONG (xdrs, &l);
307 case XDR_DECODE:
308 if (!XDR_GETLONG (xdrs, &l))
310 return FALSE;
312 *sp = (short) l;
313 return TRUE;
315 case XDR_FREE:
316 return TRUE;
318 return FALSE;
322 * XDR unsigned short integers
324 bool_t
325 xdr_u_short (XDR *xdrs, u_short *usp)
327 u_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) l;
341 return TRUE;
343 case XDR_FREE:
344 return TRUE;
346 return FALSE;
351 * XDR a char
353 bool_t
354 xdr_char (XDR *xdrs, char *cp)
356 int i;
358 i = (*cp);
359 if (!xdr_int (xdrs, &i))
361 return FALSE;
363 *cp = i;
364 return TRUE;
368 * XDR an unsigned char
370 bool_t
371 xdr_u_char (XDR *xdrs, u_char *cp)
373 u_int u;
375 u = (*cp);
376 if (!xdr_u_int (xdrs, &u))
378 return FALSE;
380 *cp = u;
381 return TRUE;
385 * XDR booleans
387 bool_t
388 xdr_bool (XDR *xdrs, bool_t *bp)
390 long lb;
392 switch (xdrs->x_op)
394 case XDR_ENCODE:
395 lb = *bp ? XDR_TRUE : XDR_FALSE;
396 return XDR_PUTLONG (xdrs, &lb);
398 case XDR_DECODE:
399 if (!XDR_GETLONG (xdrs, &lb))
401 return FALSE;
403 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
404 return TRUE;
406 case XDR_FREE:
407 return TRUE;
409 return FALSE;
413 * XDR enumerations
415 bool_t
416 xdr_enum (XDR *xdrs, enum_t *ep)
418 enum sizecheck
420 SIZEVAL
421 }; /* used to find the size of an enum */
424 * enums are treated as ints
426 if (sizeof (enum sizecheck) == 4)
428 #if INT_MAX < LONG_MAX
429 long l;
431 switch (xdrs->x_op)
433 case XDR_ENCODE:
434 l = *ep;
435 return XDR_PUTLONG (xdrs, &l);
437 case XDR_DECODE:
438 if (!XDR_GETLONG (xdrs, &l))
440 return FALSE;
442 *ep = l;
443 case XDR_FREE:
444 return TRUE;
447 return FALSE;
448 #else
449 return xdr_long (xdrs, (long *) ep);
450 #endif
452 else if (sizeof (enum sizecheck) == sizeof (short))
454 return xdr_short (xdrs, (short *) ep);
456 else
458 return FALSE;
463 * XDR opaque data
464 * Allows the specification of a fixed size sequence of opaque bytes.
465 * cp points to the opaque object and cnt gives the byte length.
467 bool_t
468 xdr_opaque (XDR *xdrs, caddr_t cp, u_int cnt)
470 u_int rndup;
471 static char crud[BYTES_PER_XDR_UNIT];
474 * if no data we are done
476 if (cnt == 0)
477 return TRUE;
480 * round byte count to full xdr units
482 rndup = cnt % BYTES_PER_XDR_UNIT;
483 if (rndup > 0)
484 rndup = BYTES_PER_XDR_UNIT - rndup;
486 switch (xdrs->x_op)
488 case XDR_DECODE:
489 if (!XDR_GETBYTES (xdrs, cp, cnt))
491 return FALSE;
493 if (rndup == 0)
494 return TRUE;
495 return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
497 case XDR_ENCODE:
498 if (!XDR_PUTBYTES (xdrs, cp, cnt))
500 return FALSE;
502 if (rndup == 0)
503 return TRUE;
504 return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
506 case XDR_FREE:
507 return TRUE;
509 return FALSE;
513 * XDR counted bytes
514 * *cpp is a pointer to the bytes, *sizep is the count.
515 * If *cpp is NULL maxsize bytes are allocated
517 bool_t
518 xdr_bytes (xdrs, cpp, sizep, maxsize)
519 XDR *xdrs;
520 char **cpp;
521 u_int *sizep;
522 u_int maxsize;
524 char *sp = *cpp; /* sp is the actual string pointer */
525 u_int nodesize;
528 * first deal with the length since xdr bytes are counted
530 if (!xdr_u_int (xdrs, sizep))
532 return FALSE;
534 nodesize = *sizep;
535 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
537 return FALSE;
541 * now deal with the actual bytes
543 switch (xdrs->x_op)
545 case XDR_DECODE:
546 if (nodesize == 0)
548 return TRUE;
550 if (sp == NULL)
552 *cpp = sp = (char *) mem_alloc (nodesize);
554 if (sp == NULL)
556 #ifdef USE_IN_LIBIO
557 if (_IO_fwide (stderr, 0) > 0)
558 (void) __fwprintf (stderr, L"%s", _("xdr_bytes: out of memory\n"));
559 else
560 #endif
561 (void) fputs (_("xdr_bytes: out of memory\n"), stderr);
562 return FALSE;
564 /* fall into ... */
566 case XDR_ENCODE:
567 return xdr_opaque (xdrs, sp, nodesize);
569 case XDR_FREE:
570 if (sp != NULL)
572 mem_free (sp, nodesize);
573 *cpp = NULL;
575 return TRUE;
577 return FALSE;
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 xdr_bytes (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
593 * XDR a discriminated union
594 * Support routine for discriminated unions.
595 * You create an array of xdrdiscrim structures, terminated with
596 * an entry with a null procedure pointer. The routine gets
597 * the discriminant value and then searches the array of xdrdiscrims
598 * looking for that value. It calls the procedure given in the xdrdiscrim
599 * to handle the discriminant. If there is no specific routine a default
600 * routine may be called.
601 * If there is no specific or default routine an error is returned.
603 bool_t
604 xdr_union (xdrs, dscmp, unp, choices, dfault)
605 XDR *xdrs;
606 enum_t *dscmp; /* enum to decide which arm to work on */
607 char *unp; /* the union itself */
608 const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
609 xdrproc_t dfault; /* default xdr routine */
611 enum_t dscm;
614 * we deal with the discriminator; it's an enum
616 if (!xdr_enum (xdrs, dscmp))
618 return FALSE;
620 dscm = *dscmp;
623 * search choices for a value that matches the discriminator.
624 * if we find one, execute the xdr routine for that value.
626 for (; choices->proc != NULL_xdrproc_t; choices++)
628 if (choices->value == dscm)
629 return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
633 * no match - execute the default xdr routine if there is one
635 return ((dfault == NULL_xdrproc_t) ? FALSE :
636 (*dfault) (xdrs, unp, LASTUNSIGNED));
641 * Non-portable xdr primitives.
642 * Care should be taken when moving these routines to new architectures.
647 * XDR null terminated ASCII strings
648 * xdr_string deals with "C strings" - arrays of bytes that are
649 * terminated by a NULL character. The parameter cpp references a
650 * pointer to storage; If the pointer is null, then the necessary
651 * storage is allocated. The last parameter is the max allowed length
652 * of the string as specified by a protocol.
654 bool_t
655 xdr_string (xdrs, cpp, maxsize)
656 XDR *xdrs;
657 char **cpp;
658 u_int maxsize;
660 char *sp = *cpp; /* sp is the actual string pointer */
661 u_int size;
662 u_int nodesize;
665 * first deal with the length since xdr strings are counted-strings
667 switch (xdrs->x_op)
669 case XDR_FREE:
670 if (sp == NULL)
672 return TRUE; /* already free */
674 /* fall through... */
675 case XDR_ENCODE:
676 if (sp == NULL)
677 return FALSE;
678 size = strlen (sp);
679 break;
680 case XDR_DECODE:
681 break;
683 if (!xdr_u_int (xdrs, &size))
685 return FALSE;
687 if (size > maxsize)
689 return FALSE;
691 nodesize = size + 1;
694 * now deal with the actual bytes
696 switch (xdrs->x_op)
698 case XDR_DECODE:
699 if (nodesize == 0)
701 return TRUE;
703 if (sp == NULL)
704 *cpp = sp = (char *) mem_alloc (nodesize);
705 if (sp == NULL)
707 #ifdef USE_IN_LIBIO
708 if (_IO_fwide (stderr, 0) > 0)
709 (void) __fwprintf (stderr, L"%s",
710 _("xdr_string: out of memory\n"));
711 else
712 #endif
713 (void) fputs (_("xdr_string: out of memory\n"), stderr);
714 return FALSE;
716 sp[size] = 0;
717 /* fall into ... */
719 case XDR_ENCODE:
720 return xdr_opaque (xdrs, sp, size);
722 case XDR_FREE:
723 mem_free (sp, nodesize);
724 *cpp = NULL;
725 return TRUE;
727 return FALSE;
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 (xdr_string (xdrs, cpp, LASTUNSIGNED))
741 return TRUE;
743 return FALSE;