Fri Jun 28 02:41:08 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / sunrpc / xdr.c
bloba5241a512065c0631503c7a544204bd204c2f46d
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 char *malloc();
47 #include <rpc/types.h>
48 #include <rpc/xdr.h>
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 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(proc, objp)
68 xdrproc_t proc;
69 char *objp;
71 XDR x;
73 x.x_op = XDR_FREE;
74 (*proc)(&x, objp);
78 * XDR nothing
80 bool_t
81 xdr_void(/* xdrs, addr */)
82 /* XDR *xdrs; */
83 /* caddr_t addr; */
86 return (TRUE);
90 * XDR integers
92 bool_t
93 xdr_int(xdrs, ip)
94 XDR *xdrs;
95 int *ip;
98 #ifdef lint
99 (void) (xdr_short(xdrs, (short *)ip));
100 return (xdr_long(xdrs, (long *)ip));
101 #else
102 if (sizeof (int) < sizeof (long)) {
103 long l;
105 switch (xdrs->x_op) {
106 case XDR_ENCODE:
107 l = (long) *ip;
108 return XDR_PUTLONG(xdrs, &l);
110 case XDR_DECODE:
111 if (!XDR_GETLONG(xdrs, &l)) {
112 return FALSE;
114 *ip = (int) l;
115 return TRUE;
117 } else if (sizeof (int) == sizeof (long)) {
118 return (xdr_long(xdrs, (long *)ip));
119 } else if (sizeof (int) == sizeof (short)) {
120 return (xdr_short(xdrs, (short *)ip));
121 } else {
122 abort (); /* roland@gnu */
123 #if 0
124 /* force unresolved reference (link-time error): */
125 extern unexpected_sizes_in_xdr_int ();
127 unexpected_sizes_in_xdr_int();
128 #endif
130 #endif
134 * XDR unsigned integers
136 bool_t
137 xdr_u_int(xdrs, up)
138 XDR *xdrs;
139 u_int *up;
141 #ifdef lint
142 (void) (xdr_short(xdrs, (short *)up));
143 return (xdr_u_long(xdrs, (u_long *)up));
144 #else
145 if (sizeof (u_int) < sizeof (u_long)) {
146 u_long l;
148 switch (xdrs->x_op) {
149 case XDR_ENCODE:
150 l = (u_long) *up;
151 return XDR_PUTLONG(xdrs, &l);
153 case XDR_DECODE:
154 if (!XDR_GETLONG(xdrs, &l)) {
155 return FALSE;
157 *up = (u_int) l;
158 return TRUE;
160 } else if (sizeof (u_int) == sizeof (u_long)) {
161 return (xdr_u_long(xdrs, (u_long *)up));
162 } else if (sizeof (u_int) == sizeof (u_short)) {
163 return (xdr_short(xdrs, (short *)up));
164 } else {
165 abort (); /* drepper@gnu */
166 #if 0
167 /* force unresolved reference (link-time error): */
168 extern unexpected_sizes_in_xdr_u_int ();
170 unexpected_sizes_in_xdr_u_int();
171 #endif
173 #endif
177 * XDR long integers
178 * same as xdr_u_long - open coded to save a proc call!
180 bool_t
181 xdr_long(xdrs, lp)
182 register XDR *xdrs;
183 long *lp;
186 if (xdrs->x_op == XDR_ENCODE)
187 return (XDR_PUTLONG(xdrs, lp));
189 if (xdrs->x_op == XDR_DECODE)
190 return (XDR_GETLONG(xdrs, lp));
192 if (xdrs->x_op == XDR_FREE)
193 return (TRUE);
195 return (FALSE);
199 * XDR unsigned long integers
200 * same as xdr_long - open coded to save a proc call!
202 bool_t
203 xdr_u_long(xdrs, ulp)
204 register XDR *xdrs;
205 u_long *ulp;
208 if (xdrs->x_op == XDR_DECODE)
209 return (XDR_GETLONG(xdrs, (long *)ulp));
210 if (xdrs->x_op == XDR_ENCODE)
211 return (XDR_PUTLONG(xdrs, (long *)ulp));
212 if (xdrs->x_op == XDR_FREE)
213 return (TRUE);
214 return (FALSE);
218 * XDR short integers
220 bool_t
221 xdr_short(xdrs, sp)
222 register XDR *xdrs;
223 short *sp;
225 long l;
227 switch (xdrs->x_op) {
229 case XDR_ENCODE:
230 l = (long) *sp;
231 return (XDR_PUTLONG(xdrs, &l));
233 case XDR_DECODE:
234 if (!XDR_GETLONG(xdrs, &l)) {
235 return (FALSE);
237 *sp = (short) l;
238 return (TRUE);
240 case XDR_FREE:
241 return (TRUE);
243 return (FALSE);
247 * XDR unsigned short integers
249 bool_t
250 xdr_u_short(xdrs, usp)
251 register XDR *xdrs;
252 u_short *usp;
254 u_long l;
256 switch (xdrs->x_op) {
258 case XDR_ENCODE:
259 l = (u_long) *usp;
260 return (XDR_PUTLONG(xdrs, &l));
262 case XDR_DECODE:
263 if (!XDR_GETLONG(xdrs, &l)) {
264 return (FALSE);
266 *usp = (u_short) l;
267 return (TRUE);
269 case XDR_FREE:
270 return (TRUE);
272 return (FALSE);
277 * XDR a char
279 bool_t
280 xdr_char(xdrs, cp)
281 XDR *xdrs;
282 char *cp;
284 int i;
286 i = (*cp);
287 if (!xdr_int(xdrs, &i)) {
288 return (FALSE);
290 *cp = i;
291 return (TRUE);
295 * XDR an unsigned char
297 bool_t
298 xdr_u_char(xdrs, cp)
299 XDR *xdrs;
300 char *cp;
302 u_int u;
304 u = (*cp);
305 if (!xdr_u_int(xdrs, &u)) {
306 return (FALSE);
308 *cp = u;
309 return (TRUE);
313 * XDR booleans
315 bool_t
316 xdr_bool(xdrs, bp)
317 register XDR *xdrs;
318 bool_t *bp;
320 long lb;
322 switch (xdrs->x_op) {
324 case XDR_ENCODE:
325 lb = *bp ? XDR_TRUE : XDR_FALSE;
326 return (XDR_PUTLONG(xdrs, &lb));
328 case XDR_DECODE:
329 if (!XDR_GETLONG(xdrs, &lb)) {
330 return (FALSE);
332 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
333 return (TRUE);
335 case XDR_FREE:
336 return (TRUE);
338 return (FALSE);
342 * XDR enumerations
344 bool_t
345 xdr_enum(xdrs, ep)
346 XDR *xdrs;
347 enum_t *ep;
349 #ifndef lint
350 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
353 * enums are treated as ints
355 if (sizeof (enum sizecheck) == 4) {
356 return (xdr_long(xdrs, (long *)ep));
357 } else if (sizeof (enum sizecheck) == sizeof (short)) {
358 return (xdr_short(xdrs, (short *)ep));
359 } else {
360 return (FALSE);
362 #else
363 (void) (xdr_short(xdrs, (short *)ep));
364 return (xdr_long(xdrs, (long *)ep));
365 #endif
369 * XDR opaque data
370 * Allows the specification of a fixed size sequence of opaque bytes.
371 * cp points to the opaque object and cnt gives the byte length.
373 bool_t
374 xdr_opaque(xdrs, cp, cnt)
375 register XDR *xdrs;
376 caddr_t cp;
377 register u_int cnt;
379 register u_int rndup;
380 static crud[BYTES_PER_XDR_UNIT];
383 * if no data we are done
385 if (cnt == 0)
386 return (TRUE);
389 * round byte count to full xdr units
391 rndup = cnt % BYTES_PER_XDR_UNIT;
392 if (rndup > 0)
393 rndup = BYTES_PER_XDR_UNIT - rndup;
395 if (xdrs->x_op == XDR_DECODE) {
396 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
397 return (FALSE);
399 if (rndup == 0)
400 return (TRUE);
401 return (XDR_GETBYTES(xdrs, crud, rndup));
404 if (xdrs->x_op == XDR_ENCODE) {
405 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
406 return (FALSE);
408 if (rndup == 0)
409 return (TRUE);
410 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
413 if (xdrs->x_op == XDR_FREE) {
414 return (TRUE);
417 return (FALSE);
421 * XDR counted bytes
422 * *cpp is a pointer to the bytes, *sizep is the count.
423 * If *cpp is NULL maxsize bytes are allocated
425 bool_t
426 xdr_bytes(xdrs, cpp, sizep, maxsize)
427 register XDR *xdrs;
428 char **cpp;
429 register u_int *sizep;
430 u_int maxsize;
432 register char *sp = *cpp; /* sp is the actual string pointer */
433 register u_int nodesize;
436 * first deal with the length since xdr bytes are counted
438 if (! xdr_u_int(xdrs, sizep)) {
439 return (FALSE);
441 nodesize = *sizep;
442 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
443 return (FALSE);
447 * now deal with the actual bytes
449 switch (xdrs->x_op) {
451 case XDR_DECODE:
452 if (nodesize == 0) {
453 return (TRUE);
455 if (sp == NULL) {
456 *cpp = sp = (char *)mem_alloc(nodesize);
458 if (sp == NULL) {
459 (void) fprintf(stderr, "xdr_bytes: out of memory\n");
460 return (FALSE);
462 /* fall into ... */
464 case XDR_ENCODE:
465 return (xdr_opaque(xdrs, sp, nodesize));
467 case XDR_FREE:
468 if (sp != NULL) {
469 mem_free(sp, nodesize);
470 *cpp = NULL;
472 return (TRUE);
474 return (FALSE);
478 * Implemented here due to commonality of the object.
480 bool_t
481 xdr_netobj(xdrs, np)
482 XDR *xdrs;
483 struct netobj *np;
486 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
490 * XDR a descriminated union
491 * Support routine for discriminated unions.
492 * You create an array of xdrdiscrim structures, terminated with
493 * an entry with a null procedure pointer. The routine gets
494 * the discriminant value and then searches the array of xdrdiscrims
495 * looking for that value. It calls the procedure given in the xdrdiscrim
496 * to handle the discriminant. If there is no specific routine a default
497 * routine may be called.
498 * If there is no specific or default routine an error is returned.
500 bool_t
501 xdr_union(xdrs, dscmp, unp, choices, dfault)
502 register XDR *xdrs;
503 enum_t *dscmp; /* enum to decide which arm to work on */
504 char *unp; /* the union itself */
505 struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
506 xdrproc_t dfault; /* default xdr routine */
508 register enum_t dscm;
511 * we deal with the discriminator; it's an enum
513 if (! xdr_enum(xdrs, dscmp)) {
514 return (FALSE);
516 dscm = *dscmp;
519 * search choices for a value that matches the discriminator.
520 * if we find one, execute the xdr routine for that value.
522 for (; choices->proc != NULL_xdrproc_t; choices++) {
523 if (choices->value == dscm)
524 return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
528 * no match - execute the default xdr routine if there is one
530 return ((dfault == NULL_xdrproc_t) ? FALSE :
531 (*dfault)(xdrs, unp, LASTUNSIGNED));
536 * Non-portable xdr primitives.
537 * Care should be taken when moving these routines to new architectures.
542 * XDR null terminated ASCII strings
543 * xdr_string deals with "C strings" - arrays of bytes that are
544 * terminated by a NULL character. The parameter cpp references a
545 * pointer to storage; If the pointer is null, then the necessary
546 * storage is allocated. The last parameter is the max allowed length
547 * of the string as specified by a protocol.
549 bool_t
550 xdr_string(xdrs, cpp, maxsize)
551 register XDR *xdrs;
552 char **cpp;
553 u_int maxsize;
555 register char *sp = *cpp; /* sp is the actual string pointer */
556 u_int size;
557 u_int nodesize;
560 * first deal with the length since xdr strings are counted-strings
562 switch (xdrs->x_op) {
563 case XDR_FREE:
564 if (sp == NULL) {
565 return(TRUE); /* already free */
567 /* fall through... */
568 case XDR_ENCODE:
569 size = strlen(sp);
570 break;
572 if (! xdr_u_int(xdrs, &size)) {
573 return (FALSE);
575 if (size > maxsize) {
576 return (FALSE);
578 nodesize = size + 1;
581 * now deal with the actual bytes
583 switch (xdrs->x_op) {
585 case XDR_DECODE:
586 if (nodesize == 0) {
587 return (TRUE);
589 if (sp == NULL)
590 *cpp = sp = (char *)mem_alloc(nodesize);
591 if (sp == NULL) {
592 (void) fprintf(stderr, "xdr_string: out of memory\n");
593 return (FALSE);
595 sp[size] = 0;
596 /* fall into ... */
598 case XDR_ENCODE:
599 return (xdr_opaque(xdrs, sp, size));
601 case XDR_FREE:
602 mem_free(sp, nodesize);
603 *cpp = NULL;
604 return (TRUE);
606 return (FALSE);
610 * Wrapper for xdr_string that can be called directly from
611 * routines like clnt_call
613 bool_t
614 xdr_wrapstring(xdrs, cpp)
615 XDR *xdrs;
616 char **cpp;
618 if (xdr_string(xdrs, cpp, LASTUNSIGNED)) {
619 return (TRUE);
621 return (FALSE);