(_ISwbit): Protext use of parameter with parentheses.
[glibc.git] / sunrpc / xdr.c
blob57552c442740e3f2948b8ff39d58699c7571ee3f
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 short integers
198 bool_t
199 xdr_short (xdrs, sp)
200 XDR *xdrs;
201 short *sp;
203 long l;
205 switch (xdrs->x_op)
207 case XDR_ENCODE:
208 l = (long) *sp;
209 return XDR_PUTLONG (xdrs, &l);
211 case XDR_DECODE:
212 if (!XDR_GETLONG (xdrs, &l))
214 return FALSE;
216 *sp = (short) l;
217 return TRUE;
219 case XDR_FREE:
220 return TRUE;
222 return FALSE;
226 * XDR unsigned short integers
228 bool_t
229 xdr_u_short (xdrs, usp)
230 XDR *xdrs;
231 u_short *usp;
233 u_long l;
235 switch (xdrs->x_op)
237 case XDR_ENCODE:
238 l = (u_long) * usp;
239 return XDR_PUTLONG (xdrs, &l);
241 case XDR_DECODE:
242 if (!XDR_GETLONG (xdrs, &l))
244 return FALSE;
246 *usp = (u_short) l;
247 return TRUE;
249 case XDR_FREE:
250 return TRUE;
252 return FALSE;
257 * XDR a char
259 bool_t
260 xdr_char (xdrs, cp)
261 XDR *xdrs;
262 char *cp;
264 int i;
266 i = (*cp);
267 if (!xdr_int (xdrs, &i))
269 return FALSE;
271 *cp = i;
272 return TRUE;
276 * XDR an unsigned char
278 bool_t
279 xdr_u_char (xdrs, cp)
280 XDR *xdrs;
281 u_char *cp;
283 u_int u;
285 u = (*cp);
286 if (!xdr_u_int (xdrs, &u))
288 return FALSE;
290 *cp = u;
291 return TRUE;
295 * XDR booleans
297 bool_t
298 xdr_bool (xdrs, bp)
299 XDR *xdrs;
300 bool_t *bp;
302 long lb;
304 switch (xdrs->x_op)
306 case XDR_ENCODE:
307 lb = *bp ? XDR_TRUE : XDR_FALSE;
308 return XDR_PUTLONG (xdrs, &lb);
310 case XDR_DECODE:
311 if (!XDR_GETLONG (xdrs, &lb))
313 return FALSE;
315 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
316 return TRUE;
318 case XDR_FREE:
319 return TRUE;
321 return FALSE;
325 * XDR enumerations
327 bool_t
328 xdr_enum (xdrs, ep)
329 XDR *xdrs;
330 enum_t *ep;
332 enum sizecheck
334 SIZEVAL
335 }; /* used to find the size of an enum */
338 * enums are treated as ints
340 if (sizeof (enum sizecheck) == 4)
342 #if INT_MAX < LONG_MAX
343 long l;
345 switch (xdrs->x_op)
347 case XDR_ENCODE:
348 l = *ep;
349 return XDR_PUTLONG (xdrs, &l);
351 case XDR_DECODE:
352 if (!XDR_GETLONG (xdrs, &l))
354 return FALSE;
356 *ep = l;
357 case XDR_FREE:
358 return TRUE;
361 return FALSE;
362 #else
363 return xdr_long (xdrs, (long *) ep);
364 #endif
366 else if (sizeof (enum sizecheck) == sizeof (short))
368 return xdr_short (xdrs, (short *) ep);
370 else
372 return FALSE;
377 * XDR opaque data
378 * Allows the specification of a fixed size sequence of opaque bytes.
379 * cp points to the opaque object and cnt gives the byte length.
381 bool_t
382 xdr_opaque (xdrs, cp, cnt)
383 XDR *xdrs;
384 caddr_t cp;
385 u_int cnt;
387 u_int rndup;
388 static char crud[BYTES_PER_XDR_UNIT];
391 * if no data we are done
393 if (cnt == 0)
394 return TRUE;
397 * round byte count to full xdr units
399 rndup = cnt % BYTES_PER_XDR_UNIT;
400 if (rndup > 0)
401 rndup = BYTES_PER_XDR_UNIT - rndup;
403 switch (xdrs->x_op)
405 case XDR_DECODE:
406 if (!XDR_GETBYTES (xdrs, cp, cnt))
408 return FALSE;
410 if (rndup == 0)
411 return TRUE;
412 return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
414 case XDR_ENCODE:
415 if (!XDR_PUTBYTES (xdrs, cp, cnt))
417 return FALSE;
419 if (rndup == 0)
420 return TRUE;
421 return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
423 case XDR_FREE:
424 return TRUE;
426 return FALSE;
430 * XDR counted bytes
431 * *cpp is a pointer to the bytes, *sizep is the count.
432 * If *cpp is NULL maxsize bytes are allocated
434 bool_t
435 xdr_bytes (xdrs, cpp, sizep, maxsize)
436 XDR *xdrs;
437 char **cpp;
438 u_int *sizep;
439 u_int maxsize;
441 char *sp = *cpp; /* sp is the actual string pointer */
442 u_int nodesize;
445 * first deal with the length since xdr bytes are counted
447 if (!xdr_u_int (xdrs, sizep))
449 return FALSE;
451 nodesize = *sizep;
452 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
454 return FALSE;
458 * now deal with the actual bytes
460 switch (xdrs->x_op)
462 case XDR_DECODE:
463 if (nodesize == 0)
465 return TRUE;
467 if (sp == NULL)
469 *cpp = sp = (char *) mem_alloc (nodesize);
471 if (sp == NULL)
473 (void) fprintf (stderr, "xdr_bytes: out of memory\n");
474 return FALSE;
476 /* fall into ... */
478 case XDR_ENCODE:
479 return xdr_opaque (xdrs, sp, nodesize);
481 case XDR_FREE:
482 if (sp != NULL)
484 mem_free (sp, nodesize);
485 *cpp = NULL;
487 return TRUE;
489 return FALSE;
493 * Implemented here due to commonality of the object.
495 bool_t
496 xdr_netobj (xdrs, np)
497 XDR *xdrs;
498 struct netobj *np;
501 return xdr_bytes (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
505 * XDR a discriminated union
506 * Support routine for discriminated unions.
507 * You create an array of xdrdiscrim structures, terminated with
508 * an entry with a null procedure pointer. The routine gets
509 * the discriminant value and then searches the array of xdrdiscrims
510 * looking for that value. It calls the procedure given in the xdrdiscrim
511 * to handle the discriminant. If there is no specific routine a default
512 * routine may be called.
513 * If there is no specific or default routine an error is returned.
515 bool_t
516 xdr_union (xdrs, dscmp, unp, choices, dfault)
517 XDR *xdrs;
518 enum_t *dscmp; /* enum to decide which arm to work on */
519 char *unp; /* the union itself */
520 const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
521 xdrproc_t dfault; /* default xdr routine */
523 enum_t dscm;
526 * we deal with the discriminator; it's an enum
528 if (!xdr_enum (xdrs, dscmp))
530 return FALSE;
532 dscm = *dscmp;
535 * search choices for a value that matches the discriminator.
536 * if we find one, execute the xdr routine for that value.
538 for (; choices->proc != NULL_xdrproc_t; choices++)
540 if (choices->value == dscm)
541 return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
545 * no match - execute the default xdr routine if there is one
547 return ((dfault == NULL_xdrproc_t) ? FALSE :
548 (*dfault) (xdrs, unp, LASTUNSIGNED));
553 * Non-portable xdr primitives.
554 * Care should be taken when moving these routines to new architectures.
559 * XDR null terminated ASCII strings
560 * xdr_string deals with "C strings" - arrays of bytes that are
561 * terminated by a NULL character. The parameter cpp references a
562 * pointer to storage; If the pointer is null, then the necessary
563 * storage is allocated. The last parameter is the max allowed length
564 * of the string as specified by a protocol.
566 bool_t
567 xdr_string (xdrs, cpp, maxsize)
568 XDR *xdrs;
569 char **cpp;
570 u_int maxsize;
572 char *sp = *cpp; /* sp is the actual string pointer */
573 u_int size;
574 u_int nodesize;
577 * first deal with the length since xdr strings are counted-strings
579 switch (xdrs->x_op)
581 case XDR_FREE:
582 if (sp == NULL)
584 return TRUE; /* already free */
586 /* fall through... */
587 case XDR_ENCODE:
588 if (sp == NULL)
589 return FALSE;
590 size = strlen (sp);
591 break;
592 case XDR_DECODE:
593 break;
595 if (!xdr_u_int (xdrs, &size))
597 return FALSE;
599 if (size > maxsize)
601 return FALSE;
603 nodesize = size + 1;
606 * now deal with the actual bytes
608 switch (xdrs->x_op)
610 case XDR_DECODE:
611 if (nodesize == 0)
613 return TRUE;
615 if (sp == NULL)
616 *cpp = sp = (char *) mem_alloc (nodesize);
617 if (sp == NULL)
619 (void) fprintf (stderr, "xdr_string: out of memory\n");
620 return FALSE;
622 sp[size] = 0;
623 /* fall into ... */
625 case XDR_ENCODE:
626 return xdr_opaque (xdrs, sp, size);
628 case XDR_FREE:
629 mem_free (sp, nodesize);
630 *cpp = NULL;
631 return TRUE;
633 return FALSE;
637 * Wrapper for xdr_string that can be called directly from
638 * routines like clnt_call
640 bool_t
641 xdr_wrapstring (xdrs, cpp)
642 XDR *xdrs;
643 char **cpp;
645 if (xdr_string (xdrs, cpp, LASTUNSIGNED))
647 return TRUE;
649 return FALSE;