]> git.ipfire.org Git - thirdparty/glibc.git/blame - sunrpc/xdr.c
Tue Jun 25 17:22:55 1996 Miles Bader <miles@gnu.ai.mit.edu>
[thirdparty/glibc.git] / sunrpc / xdr.c
CommitLineData
28f540f4
RM
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.
9 *
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.
13 *
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.
17 *
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.
21 *
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.
25 *
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
29 */
30#if !defined(lint) && defined(SCCSIDS)
31static char sccsid[] = "@(#)xdr.c 1.35 87/08/12";
32#endif
33
34/*
35 * xdr.c, Generic XDR routines implementation.
36 *
37 * Copyright (C) 1986, Sun Microsystems, Inc.
38 *
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.
42 */
43
44#include <stdio.h>
45char *malloc();
46
47#include <rpc/types.h>
48#include <rpc/xdr.h>
49
50/*
51 * constants specific to the xdr "protocol"
52 */
53#define XDR_FALSE ((long) 0)
54#define XDR_TRUE ((long) 1)
55#define LASTUNSIGNED ((u_int) 0-1)
56
57/*
58 * for unit alignment
59 */
60static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
61
62/*
63 * Free a data structure using XDR
64 * Not a filter, but a convenient utility nonetheless
65 */
66void
67xdr_free(proc, objp)
68 xdrproc_t proc;
69 char *objp;
70{
71 XDR x;
72
73 x.x_op = XDR_FREE;
74 (*proc)(&x, objp);
75}
76
77/*
78 * XDR nothing
79 */
80bool_t
81xdr_void(/* xdrs, addr */)
82 /* XDR *xdrs; */
83 /* caddr_t addr; */
84{
85
86 return (TRUE);
87}
88
89/*
90 * XDR integers
91 */
92bool_t
93xdr_int(xdrs, ip)
94 XDR *xdrs;
95 int *ip;
96{
97
98#ifdef lint
99 (void) (xdr_short(xdrs, (short *)ip));
100 return (xdr_long(xdrs, (long *)ip));
101#else
a1470b6f
RM
102 if (sizeof (int) < sizeof (long)) {
103 long l;
104
105 switch (xdrs->x_op) {
106 case XDR_ENCODE:
107 l = (long) *ip;
108 return XDR_PUTLONG(xdrs, &l);
109
110 case XDR_DECODE:
111 if (!XDR_GETLONG(xdrs, &l)) {
112 return FALSE;
113 }
114 *ip = (int) l;
115 return TRUE;
116 }
117 } else if (sizeof (int) == sizeof (long)) {
28f540f4 118 return (xdr_long(xdrs, (long *)ip));
a1470b6f 119 } else if (sizeof (int) == sizeof (short)) {
28f540f4 120 return (xdr_short(xdrs, (short *)ip));
a1470b6f
RM
121 } else {
122 /* force unresolved reference (link-time error): */
123 extern unexpected_sizes_in_xdr_int ();
124
125 unexpected_sizes_in_xdr_int();
28f540f4
RM
126 }
127#endif
128}
129
130/*
131 * XDR unsigned integers
132 */
133bool_t
134xdr_u_int(xdrs, up)
135 XDR *xdrs;
136 u_int *up;
137{
28f540f4
RM
138#ifdef lint
139 (void) (xdr_short(xdrs, (short *)up));
140 return (xdr_u_long(xdrs, (u_long *)up));
141#else
a1470b6f
RM
142 if (sizeof (u_int) < sizeof (u_long)) {
143 u_long l;
144
145 switch (xdrs->x_op) {
146 case XDR_ENCODE:
147 l = (u_long) *up;
148 return XDR_PUTLONG(xdrs, &l);
149
150 case XDR_DECODE:
151 if (!XDR_GETLONG(xdrs, &l)) {
152 return FALSE;
153 }
154 *up = (u_int) l;
155 return TRUE;
156 }
157 } else if (sizeof (u_int) == sizeof (u_long)) {
28f540f4 158 return (xdr_u_long(xdrs, (u_long *)up));
a1470b6f 159 } else if (sizeof (u_int) == sizeof (u_short)) {
28f540f4 160 return (xdr_short(xdrs, (short *)up));
a1470b6f
RM
161 } else {
162 /* force unresolved reference (link-time error): */
163 extern unexpected_sizes_in_xdr_u_int ();
164
165 unexpected_sizes_in_xdr_u_int();
28f540f4
RM
166 }
167#endif
168}
169
170/*
171 * XDR long integers
172 * same as xdr_u_long - open coded to save a proc call!
173 */
174bool_t
175xdr_long(xdrs, lp)
176 register XDR *xdrs;
177 long *lp;
178{
179
180 if (xdrs->x_op == XDR_ENCODE)
181 return (XDR_PUTLONG(xdrs, lp));
182
183 if (xdrs->x_op == XDR_DECODE)
184 return (XDR_GETLONG(xdrs, lp));
185
186 if (xdrs->x_op == XDR_FREE)
187 return (TRUE);
188
189 return (FALSE);
190}
191
192/*
193 * XDR unsigned long integers
194 * same as xdr_long - open coded to save a proc call!
195 */
196bool_t
197xdr_u_long(xdrs, ulp)
198 register XDR *xdrs;
199 u_long *ulp;
200{
201
202 if (xdrs->x_op == XDR_DECODE)
203 return (XDR_GETLONG(xdrs, (long *)ulp));
204 if (xdrs->x_op == XDR_ENCODE)
205 return (XDR_PUTLONG(xdrs, (long *)ulp));
206 if (xdrs->x_op == XDR_FREE)
207 return (TRUE);
208 return (FALSE);
209}
210
211/*
212 * XDR short integers
213 */
214bool_t
215xdr_short(xdrs, sp)
216 register XDR *xdrs;
217 short *sp;
218{
219 long l;
220
221 switch (xdrs->x_op) {
222
223 case XDR_ENCODE:
224 l = (long) *sp;
225 return (XDR_PUTLONG(xdrs, &l));
226
227 case XDR_DECODE:
228 if (!XDR_GETLONG(xdrs, &l)) {
229 return (FALSE);
230 }
231 *sp = (short) l;
232 return (TRUE);
233
234 case XDR_FREE:
235 return (TRUE);
236 }
237 return (FALSE);
238}
239
240/*
241 * XDR unsigned short integers
242 */
243bool_t
244xdr_u_short(xdrs, usp)
245 register XDR *xdrs;
246 u_short *usp;
247{
248 u_long l;
249
250 switch (xdrs->x_op) {
251
252 case XDR_ENCODE:
253 l = (u_long) *usp;
254 return (XDR_PUTLONG(xdrs, &l));
255
256 case XDR_DECODE:
257 if (!XDR_GETLONG(xdrs, &l)) {
258 return (FALSE);
259 }
260 *usp = (u_short) l;
261 return (TRUE);
262
263 case XDR_FREE:
264 return (TRUE);
265 }
266 return (FALSE);
267}
268
269
270/*
271 * XDR a char
272 */
273bool_t
274xdr_char(xdrs, cp)
275 XDR *xdrs;
276 char *cp;
277{
278 int i;
279
280 i = (*cp);
281 if (!xdr_int(xdrs, &i)) {
282 return (FALSE);
283 }
284 *cp = i;
285 return (TRUE);
286}
287
288/*
289 * XDR an unsigned char
290 */
291bool_t
292xdr_u_char(xdrs, cp)
293 XDR *xdrs;
294 char *cp;
295{
296 u_int u;
297
298 u = (*cp);
299 if (!xdr_u_int(xdrs, &u)) {
300 return (FALSE);
301 }
302 *cp = u;
303 return (TRUE);
304}
305
306/*
307 * XDR booleans
308 */
309bool_t
310xdr_bool(xdrs, bp)
311 register XDR *xdrs;
312 bool_t *bp;
313{
314 long lb;
315
316 switch (xdrs->x_op) {
317
318 case XDR_ENCODE:
319 lb = *bp ? XDR_TRUE : XDR_FALSE;
320 return (XDR_PUTLONG(xdrs, &lb));
321
322 case XDR_DECODE:
323 if (!XDR_GETLONG(xdrs, &lb)) {
324 return (FALSE);
325 }
326 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
327 return (TRUE);
328
329 case XDR_FREE:
330 return (TRUE);
331 }
332 return (FALSE);
333}
334
335/*
336 * XDR enumerations
337 */
338bool_t
339xdr_enum(xdrs, ep)
340 XDR *xdrs;
341 enum_t *ep;
342{
343#ifndef lint
344 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
345
346 /*
347 * enums are treated as ints
348 */
b20e47cb 349 if (sizeof (enum sizecheck) == 4) {
28f540f4
RM
350 return (xdr_long(xdrs, (long *)ep));
351 } else if (sizeof (enum sizecheck) == sizeof (short)) {
352 return (xdr_short(xdrs, (short *)ep));
353 } else {
354 return (FALSE);
355 }
356#else
357 (void) (xdr_short(xdrs, (short *)ep));
358 return (xdr_long(xdrs, (long *)ep));
359#endif
360}
361
362/*
363 * XDR opaque data
364 * Allows the specification of a fixed size sequence of opaque bytes.
365 * cp points to the opaque object and cnt gives the byte length.
366 */
367bool_t
368xdr_opaque(xdrs, cp, cnt)
369 register XDR *xdrs;
370 caddr_t cp;
371 register u_int cnt;
372{
373 register u_int rndup;
374 static crud[BYTES_PER_XDR_UNIT];
375
376 /*
377 * if no data we are done
378 */
379 if (cnt == 0)
380 return (TRUE);
381
382 /*
383 * round byte count to full xdr units
384 */
385 rndup = cnt % BYTES_PER_XDR_UNIT;
386 if (rndup > 0)
387 rndup = BYTES_PER_XDR_UNIT - rndup;
388
389 if (xdrs->x_op == XDR_DECODE) {
390 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
391 return (FALSE);
392 }
393 if (rndup == 0)
394 return (TRUE);
395 return (XDR_GETBYTES(xdrs, crud, rndup));
396 }
397
398 if (xdrs->x_op == XDR_ENCODE) {
399 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
400 return (FALSE);
401 }
402 if (rndup == 0)
403 return (TRUE);
404 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
405 }
406
407 if (xdrs->x_op == XDR_FREE) {
408 return (TRUE);
409 }
410
411 return (FALSE);
412}
413
414/*
415 * XDR counted bytes
416 * *cpp is a pointer to the bytes, *sizep is the count.
417 * If *cpp is NULL maxsize bytes are allocated
418 */
419bool_t
420xdr_bytes(xdrs, cpp, sizep, maxsize)
421 register XDR *xdrs;
422 char **cpp;
423 register u_int *sizep;
424 u_int maxsize;
425{
426 register char *sp = *cpp; /* sp is the actual string pointer */
427 register u_int nodesize;
428
429 /*
430 * first deal with the length since xdr bytes are counted
431 */
432 if (! xdr_u_int(xdrs, sizep)) {
433 return (FALSE);
434 }
435 nodesize = *sizep;
436 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
437 return (FALSE);
438 }
439
440 /*
441 * now deal with the actual bytes
442 */
443 switch (xdrs->x_op) {
444
445 case XDR_DECODE:
446 if (nodesize == 0) {
447 return (TRUE);
448 }
449 if (sp == NULL) {
450 *cpp = sp = (char *)mem_alloc(nodesize);
451 }
452 if (sp == NULL) {
453 (void) fprintf(stderr, "xdr_bytes: out of memory\n");
454 return (FALSE);
455 }
456 /* fall into ... */
457
458 case XDR_ENCODE:
459 return (xdr_opaque(xdrs, sp, nodesize));
460
461 case XDR_FREE:
462 if (sp != NULL) {
463 mem_free(sp, nodesize);
464 *cpp = NULL;
465 }
466 return (TRUE);
467 }
468 return (FALSE);
469}
470
471/*
472 * Implemented here due to commonality of the object.
473 */
474bool_t
475xdr_netobj(xdrs, np)
476 XDR *xdrs;
477 struct netobj *np;
478{
479
480 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
481}
482
483/*
484 * XDR a descriminated union
485 * Support routine for discriminated unions.
486 * You create an array of xdrdiscrim structures, terminated with
487 * an entry with a null procedure pointer. The routine gets
488 * the discriminant value and then searches the array of xdrdiscrims
489 * looking for that value. It calls the procedure given in the xdrdiscrim
490 * to handle the discriminant. If there is no specific routine a default
491 * routine may be called.
492 * If there is no specific or default routine an error is returned.
493 */
494bool_t
495xdr_union(xdrs, dscmp, unp, choices, dfault)
496 register XDR *xdrs;
497 enum_t *dscmp; /* enum to decide which arm to work on */
498 char *unp; /* the union itself */
499 struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
500 xdrproc_t dfault; /* default xdr routine */
501{
502 register enum_t dscm;
503
504 /*
505 * we deal with the discriminator; it's an enum
506 */
507 if (! xdr_enum(xdrs, dscmp)) {
508 return (FALSE);
509 }
510 dscm = *dscmp;
511
512 /*
513 * search choices for a value that matches the discriminator.
514 * if we find one, execute the xdr routine for that value.
515 */
516 for (; choices->proc != NULL_xdrproc_t; choices++) {
517 if (choices->value == dscm)
518 return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
519 }
520
521 /*
522 * no match - execute the default xdr routine if there is one
523 */
524 return ((dfault == NULL_xdrproc_t) ? FALSE :
525 (*dfault)(xdrs, unp, LASTUNSIGNED));
526}
527
528
529/*
530 * Non-portable xdr primitives.
531 * Care should be taken when moving these routines to new architectures.
532 */
533
534
535/*
536 * XDR null terminated ASCII strings
537 * xdr_string deals with "C strings" - arrays of bytes that are
538 * terminated by a NULL character. The parameter cpp references a
539 * pointer to storage; If the pointer is null, then the necessary
540 * storage is allocated. The last parameter is the max allowed length
541 * of the string as specified by a protocol.
542 */
543bool_t
544xdr_string(xdrs, cpp, maxsize)
545 register XDR *xdrs;
546 char **cpp;
547 u_int maxsize;
548{
549 register char *sp = *cpp; /* sp is the actual string pointer */
550 u_int size;
551 u_int nodesize;
552
553 /*
554 * first deal with the length since xdr strings are counted-strings
555 */
556 switch (xdrs->x_op) {
557 case XDR_FREE:
558 if (sp == NULL) {
559 return(TRUE); /* already free */
560 }
561 /* fall through... */
562 case XDR_ENCODE:
563 size = strlen(sp);
564 break;
565 }
566 if (! xdr_u_int(xdrs, &size)) {
567 return (FALSE);
568 }
569 if (size > maxsize) {
570 return (FALSE);
571 }
572 nodesize = size + 1;
573
574 /*
575 * now deal with the actual bytes
576 */
577 switch (xdrs->x_op) {
578
579 case XDR_DECODE:
580 if (nodesize == 0) {
581 return (TRUE);
582 }
583 if (sp == NULL)
584 *cpp = sp = (char *)mem_alloc(nodesize);
585 if (sp == NULL) {
586 (void) fprintf(stderr, "xdr_string: out of memory\n");
587 return (FALSE);
588 }
589 sp[size] = 0;
590 /* fall into ... */
591
592 case XDR_ENCODE:
593 return (xdr_opaque(xdrs, sp, size));
594
595 case XDR_FREE:
596 mem_free(sp, nodesize);
597 *cpp = NULL;
598 return (TRUE);
599 }
600 return (FALSE);
601}
602
603/*
604 * Wrapper for xdr_string that can be called directly from
605 * routines like clnt_call
606 */
607bool_t
608xdr_wrapstring(xdrs, cpp)
609 XDR *xdrs;
610 char **cpp;
611{
612 if (xdr_string(xdrs, cpp, LASTUNSIGNED)) {
613 return (TRUE);
614 }
615 return (FALSE);
616}