Update.
[glibc.git] / sunrpc / xdr.c
blob0e3e5fc63a41bba3af239892869939af13dc66a6
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 32bit integers
157 bool_t
158 xdr_int32_t (XDR *xdrs, int32_t *lp)
161 if (xdrs->x_op == XDR_ENCODE)
162 return XDR_PUTINT32 (xdrs, lp);
164 if (xdrs->x_op == XDR_DECODE)
165 return XDR_GETINT32 (xdrs, lp);
167 if (xdrs->x_op == XDR_FREE)
168 return TRUE;
170 return FALSE;
174 * XDR 32bit unsigned integers
176 bool_t
177 xdr_uint32_t (XDR *xdrs, uint32_t *ulp)
179 switch (xdrs->x_op)
181 case XDR_DECODE:
182 return XDR_GETINT32 (xdrs, (uint32_t *) ulp);
184 case XDR_ENCODE:
185 return XDR_PUTINT32 (xdrs, (uint32_t *) ulp);
187 case XDR_FREE:
188 return TRUE;
190 return FALSE;
194 * XDR long integers
195 * same as xdr_u_long - open coded to save a proc call!
197 bool_t
198 xdr_long (XDR *xdrs, long *lp)
201 if (xdrs->x_op == XDR_ENCODE)
202 return XDR_PUTLONG (xdrs, lp);
204 if (xdrs->x_op == XDR_DECODE)
205 return XDR_GETLONG (xdrs, lp);
207 if (xdrs->x_op == XDR_FREE)
208 return TRUE;
210 return FALSE;
214 * XDR unsigned long integers
215 * same as xdr_long - open coded to save a proc call!
217 bool_t
218 xdr_u_long (XDR *xdrs, u_long *ulp)
220 switch (xdrs->x_op)
222 case XDR_DECODE:
223 return XDR_GETLONG (xdrs, (long *) ulp);
225 case XDR_ENCODE:
226 return XDR_PUTLONG (xdrs, (long *) ulp);
228 case XDR_FREE:
229 return TRUE;
231 return FALSE;
235 * XDR short integers
237 bool_t
238 xdr_short (xdrs, sp)
239 XDR *xdrs;
240 short *sp;
242 long l;
244 switch (xdrs->x_op)
246 case XDR_ENCODE:
247 l = (long) *sp;
248 return XDR_PUTLONG (xdrs, &l);
250 case XDR_DECODE:
251 if (!XDR_GETLONG (xdrs, &l))
253 return FALSE;
255 *sp = (short) l;
256 return TRUE;
258 case XDR_FREE:
259 return TRUE;
261 return FALSE;
265 * XDR unsigned short integers
267 bool_t
268 xdr_u_short (xdrs, usp)
269 XDR *xdrs;
270 u_short *usp;
272 u_long l;
274 switch (xdrs->x_op)
276 case XDR_ENCODE:
277 l = (u_long) * usp;
278 return XDR_PUTLONG (xdrs, &l);
280 case XDR_DECODE:
281 if (!XDR_GETLONG (xdrs, &l))
283 return FALSE;
285 *usp = (u_short) l;
286 return TRUE;
288 case XDR_FREE:
289 return TRUE;
291 return FALSE;
296 * XDR a char
298 bool_t
299 xdr_char (xdrs, cp)
300 XDR *xdrs;
301 char *cp;
303 int i;
305 i = (*cp);
306 if (!xdr_int (xdrs, &i))
308 return FALSE;
310 *cp = i;
311 return TRUE;
315 * XDR an unsigned char
317 bool_t
318 xdr_u_char (xdrs, cp)
319 XDR *xdrs;
320 u_char *cp;
322 u_int u;
324 u = (*cp);
325 if (!xdr_u_int (xdrs, &u))
327 return FALSE;
329 *cp = u;
330 return TRUE;
334 * XDR booleans
336 bool_t
337 xdr_bool (xdrs, bp)
338 XDR *xdrs;
339 bool_t *bp;
341 long lb;
343 switch (xdrs->x_op)
345 case XDR_ENCODE:
346 lb = *bp ? XDR_TRUE : XDR_FALSE;
347 return XDR_PUTLONG (xdrs, &lb);
349 case XDR_DECODE:
350 if (!XDR_GETLONG (xdrs, &lb))
352 return FALSE;
354 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
355 return TRUE;
357 case XDR_FREE:
358 return TRUE;
360 return FALSE;
364 * XDR enumerations
366 bool_t
367 xdr_enum (xdrs, ep)
368 XDR *xdrs;
369 enum_t *ep;
371 enum sizecheck
373 SIZEVAL
374 }; /* used to find the size of an enum */
377 * enums are treated as ints
379 if (sizeof (enum sizecheck) == 4)
381 #if INT_MAX < LONG_MAX
382 long l;
384 switch (xdrs->x_op)
386 case XDR_ENCODE:
387 l = *ep;
388 return XDR_PUTLONG (xdrs, &l);
390 case XDR_DECODE:
391 if (!XDR_GETLONG (xdrs, &l))
393 return FALSE;
395 *ep = l;
396 case XDR_FREE:
397 return TRUE;
400 return FALSE;
401 #else
402 return xdr_long (xdrs, (long *) ep);
403 #endif
405 else if (sizeof (enum sizecheck) == sizeof (short))
407 return xdr_short (xdrs, (short *) ep);
409 else
411 return FALSE;
416 * XDR opaque data
417 * Allows the specification of a fixed size sequence of opaque bytes.
418 * cp points to the opaque object and cnt gives the byte length.
420 bool_t
421 xdr_opaque (xdrs, cp, cnt)
422 XDR *xdrs;
423 caddr_t cp;
424 u_int cnt;
426 u_int rndup;
427 static char crud[BYTES_PER_XDR_UNIT];
430 * if no data we are done
432 if (cnt == 0)
433 return TRUE;
436 * round byte count to full xdr units
438 rndup = cnt % BYTES_PER_XDR_UNIT;
439 if (rndup > 0)
440 rndup = BYTES_PER_XDR_UNIT - rndup;
442 switch (xdrs->x_op)
444 case XDR_DECODE:
445 if (!XDR_GETBYTES (xdrs, cp, cnt))
447 return FALSE;
449 if (rndup == 0)
450 return TRUE;
451 return XDR_GETBYTES (xdrs, (caddr_t)crud, rndup);
453 case XDR_ENCODE:
454 if (!XDR_PUTBYTES (xdrs, cp, cnt))
456 return FALSE;
458 if (rndup == 0)
459 return TRUE;
460 return XDR_PUTBYTES (xdrs, xdr_zero, rndup);
462 case XDR_FREE:
463 return TRUE;
465 return FALSE;
469 * XDR counted bytes
470 * *cpp is a pointer to the bytes, *sizep is the count.
471 * If *cpp is NULL maxsize bytes are allocated
473 bool_t
474 xdr_bytes (xdrs, cpp, sizep, maxsize)
475 XDR *xdrs;
476 char **cpp;
477 u_int *sizep;
478 u_int maxsize;
480 char *sp = *cpp; /* sp is the actual string pointer */
481 u_int nodesize;
484 * first deal with the length since xdr bytes are counted
486 if (!xdr_u_int (xdrs, sizep))
488 return FALSE;
490 nodesize = *sizep;
491 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE))
493 return FALSE;
497 * now deal with the actual bytes
499 switch (xdrs->x_op)
501 case XDR_DECODE:
502 if (nodesize == 0)
504 return TRUE;
506 if (sp == NULL)
508 *cpp = sp = (char *) mem_alloc (nodesize);
510 if (sp == NULL)
512 (void) fprintf (stderr, "xdr_bytes: out of memory\n");
513 return FALSE;
515 /* fall into ... */
517 case XDR_ENCODE:
518 return xdr_opaque (xdrs, sp, nodesize);
520 case XDR_FREE:
521 if (sp != NULL)
523 mem_free (sp, nodesize);
524 *cpp = NULL;
526 return TRUE;
528 return FALSE;
532 * Implemented here due to commonality of the object.
534 bool_t
535 xdr_netobj (xdrs, np)
536 XDR *xdrs;
537 struct netobj *np;
540 return xdr_bytes (xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ);
544 * XDR a discriminated union
545 * Support routine for discriminated unions.
546 * You create an array of xdrdiscrim structures, terminated with
547 * an entry with a null procedure pointer. The routine gets
548 * the discriminant value and then searches the array of xdrdiscrims
549 * looking for that value. It calls the procedure given in the xdrdiscrim
550 * to handle the discriminant. If there is no specific routine a default
551 * routine may be called.
552 * If there is no specific or default routine an error is returned.
554 bool_t
555 xdr_union (xdrs, dscmp, unp, choices, dfault)
556 XDR *xdrs;
557 enum_t *dscmp; /* enum to decide which arm to work on */
558 char *unp; /* the union itself */
559 const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
560 xdrproc_t dfault; /* default xdr routine */
562 enum_t dscm;
565 * we deal with the discriminator; it's an enum
567 if (!xdr_enum (xdrs, dscmp))
569 return FALSE;
571 dscm = *dscmp;
574 * search choices for a value that matches the discriminator.
575 * if we find one, execute the xdr routine for that value.
577 for (; choices->proc != NULL_xdrproc_t; choices++)
579 if (choices->value == dscm)
580 return (*(choices->proc)) (xdrs, unp, LASTUNSIGNED);
584 * no match - execute the default xdr routine if there is one
586 return ((dfault == NULL_xdrproc_t) ? FALSE :
587 (*dfault) (xdrs, unp, LASTUNSIGNED));
592 * Non-portable xdr primitives.
593 * Care should be taken when moving these routines to new architectures.
598 * XDR null terminated ASCII strings
599 * xdr_string deals with "C strings" - arrays of bytes that are
600 * terminated by a NULL character. The parameter cpp references a
601 * pointer to storage; If the pointer is null, then the necessary
602 * storage is allocated. The last parameter is the max allowed length
603 * of the string as specified by a protocol.
605 bool_t
606 xdr_string (xdrs, cpp, maxsize)
607 XDR *xdrs;
608 char **cpp;
609 u_int maxsize;
611 char *sp = *cpp; /* sp is the actual string pointer */
612 u_int size;
613 u_int nodesize;
616 * first deal with the length since xdr strings are counted-strings
618 switch (xdrs->x_op)
620 case XDR_FREE:
621 if (sp == NULL)
623 return TRUE; /* already free */
625 /* fall through... */
626 case XDR_ENCODE:
627 if (sp == NULL)
628 return FALSE;
629 size = strlen (sp);
630 break;
631 case XDR_DECODE:
632 break;
634 if (!xdr_u_int (xdrs, &size))
636 return FALSE;
638 if (size > maxsize)
640 return FALSE;
642 nodesize = size + 1;
645 * now deal with the actual bytes
647 switch (xdrs->x_op)
649 case XDR_DECODE:
650 if (nodesize == 0)
652 return TRUE;
654 if (sp == NULL)
655 *cpp = sp = (char *) mem_alloc (nodesize);
656 if (sp == NULL)
658 (void) fprintf (stderr, "xdr_string: out of memory\n");
659 return FALSE;
661 sp[size] = 0;
662 /* fall into ... */
664 case XDR_ENCODE:
665 return xdr_opaque (xdrs, sp, size);
667 case XDR_FREE:
668 mem_free (sp, nodesize);
669 *cpp = NULL;
670 return TRUE;
672 return FALSE;
676 * Wrapper for xdr_string that can be called directly from
677 * routines like clnt_call
679 bool_t
680 xdr_wrapstring (xdrs, cpp)
681 XDR *xdrs;
682 char **cpp;
684 if (xdr_string (xdrs, cpp, LASTUNSIGNED))
686 return TRUE;
688 return FALSE;