Libinfo-173.tar.gz
[apple/libinfo.git] / rpc.subproj / xdr.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
27 * unrestricted use provided that this legend is included on all tape
28 * media and as a part of the software program in whole or part. Users
29 * may copy or modify Sun RPC without charge, but are not authorized
30 * to license or distribute it to anyone else except as part of a product or
31 * program developed by the user.
32 *
33 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
34 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
35 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
36 *
37 * Sun RPC is provided with no support and without any obligation on the
38 * part of Sun Microsystems, Inc. to assist in its use, correction,
39 * modification or enhancement.
40 *
41 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
42 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
43 * OR ANY PART THEREOF.
44 *
45 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
46 * or profits or other special, indirect and consequential damages, even if
47 * Sun has been advised of the possibility of such damages.
48 *
49 * Sun Microsystems, Inc.
50 * 2550 Garcia Avenue
51 * Mountain View, California 94043
52 */
53
54 #if defined(LIBC_SCCS) && !defined(lint)
55 /*static char *sccsid = "from: @(#)xdr.c 1.35 87/08/12";*/
56 /*static char *sccsid = "from: @(#)xdr.c 2.1 88/07/29 4.0 RPCSRC";*/
57 static char *rcsid = "$Id: xdr.c,v 1.4 2003/06/23 17:24:59 majka Exp $";
58 #endif
59
60 /*
61 * xdr.c, Generic XDR routines implementation.
62 *
63 * Copyright (C) 1986, Sun Microsystems, Inc.
64 *
65 * These are the "generic" xdr routines used to serialize and de-serialize
66 * most common data items. See xdr.h for more info on the interface to
67 * xdr.
68 */
69
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73
74 #include <rpc/types.h>
75 #include <rpc/xdr.h>
76
77 /*
78 * constants specific to the xdr "protocol"
79 */
80 #define XDR_FALSE ((long) 0)
81 #define XDR_TRUE ((long) 1)
82 #define LASTUNSIGNED ((u_int) 0-1)
83
84 /*
85 * for unit alignment
86 */
87 static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
88
89 /*
90 * Free a data structure using XDR
91 * Not a filter, but a convenient utility nonetheless
92 */
93 void
94 xdr_free(proc, objp)
95 xdrproc_t proc;
96 #if defined(__APPLE__)
97 void *objp;
98 #else
99 char *objp;
100 #endif /* !NeXT */
101 {
102 XDR x;
103
104 x.x_op = XDR_FREE;
105 (*proc)(&x, objp);
106 }
107
108 /*
109 * XDR nothing
110 */
111 bool_t
112 xdr_void(/* xdrs, addr */)
113 /* XDR *xdrs; */
114 /* caddr_t addr; */
115 {
116
117 return (TRUE);
118 }
119
120 /*
121 * XDR integers
122 */
123 bool_t
124 xdr_int(xdrs, ip)
125 XDR *xdrs;
126 int *ip;
127 {
128
129 #ifdef lint
130 (void) (xdr_short(xdrs, (short *)ip));
131 return (xdr_long(xdrs, (long *)ip));
132 #else
133 if (sizeof (int) == sizeof (long)) {
134 return (xdr_long(xdrs, (long *)ip));
135 } else {
136 return (xdr_short(xdrs, (short *)ip));
137 }
138 #endif
139 }
140
141 /*
142 * XDR unsigned integers
143 */
144 bool_t
145 xdr_u_int(xdrs, up)
146 XDR *xdrs;
147 u_int *up;
148 {
149
150 #ifdef lint
151 (void) (xdr_short(xdrs, (short *)up));
152 return (xdr_u_long(xdrs, (u_long *)up));
153 #else
154 if (sizeof (u_int) == sizeof (u_long)) {
155 return (xdr_u_long(xdrs, (u_long *)up));
156 } else {
157 return (xdr_short(xdrs, (short *)up));
158 }
159 #endif
160 }
161
162 /*
163 * XDR long integers
164 * same as xdr_u_long - open coded to save a proc call!
165 */
166 bool_t
167 xdr_long(xdrs, lp)
168 register XDR *xdrs;
169 long *lp;
170 {
171
172 if (xdrs->x_op == XDR_ENCODE)
173 return (XDR_PUTLONG(xdrs, lp));
174
175 if (xdrs->x_op == XDR_DECODE)
176 return (XDR_GETLONG(xdrs, lp));
177
178 if (xdrs->x_op == XDR_FREE)
179 return (TRUE);
180
181 return (FALSE);
182 }
183
184 /*
185 * XDR unsigned long integers
186 * same as xdr_long - open coded to save a proc call!
187 */
188 bool_t
189 xdr_u_long(xdrs, ulp)
190 register XDR *xdrs;
191 u_long *ulp;
192 {
193
194 if (xdrs->x_op == XDR_DECODE)
195 return (XDR_GETLONG(xdrs, (long *)ulp));
196 if (xdrs->x_op == XDR_ENCODE)
197 return (XDR_PUTLONG(xdrs, (long *)ulp));
198 if (xdrs->x_op == XDR_FREE)
199 return (TRUE);
200 return (FALSE);
201 }
202
203 /*
204 * XDR short integers
205 */
206 bool_t
207 xdr_short(xdrs, sp)
208 register XDR *xdrs;
209 short *sp;
210 {
211 long l;
212
213 switch (xdrs->x_op) {
214
215 case XDR_ENCODE:
216 l = (long) *sp;
217 return (XDR_PUTLONG(xdrs, &l));
218
219 case XDR_DECODE:
220 if (!XDR_GETLONG(xdrs, &l)) {
221 return (FALSE);
222 }
223 *sp = (short) l;
224 return (TRUE);
225
226 case XDR_FREE:
227 return (TRUE);
228 }
229 return (FALSE);
230 }
231
232 /*
233 * XDR unsigned short integers
234 */
235 bool_t
236 xdr_u_short(xdrs, usp)
237 register XDR *xdrs;
238 u_short *usp;
239 {
240 u_long l;
241
242 switch (xdrs->x_op) {
243
244 case XDR_ENCODE:
245 l = (u_long) *usp;
246 return (XDR_PUTLONG(xdrs, &l));
247
248 case XDR_DECODE:
249 if (!XDR_GETLONG(xdrs, &l)) {
250 return (FALSE);
251 }
252 *usp = (u_short) l;
253 return (TRUE);
254
255 case XDR_FREE:
256 return (TRUE);
257 }
258 return (FALSE);
259 }
260
261
262 /*
263 * XDR a char
264 */
265 bool_t
266 xdr_char(xdrs, cp)
267 XDR *xdrs;
268 char *cp;
269 {
270 int i;
271
272 i = (*cp);
273 if (!xdr_int(xdrs, &i)) {
274 return (FALSE);
275 }
276 *cp = i;
277 return (TRUE);
278 }
279
280 /*
281 * XDR an unsigned char
282 */
283 bool_t
284 xdr_u_char(xdrs, cp)
285 XDR *xdrs;
286 u_char *cp;
287 {
288 u_int u;
289
290 u = (*cp);
291 if (!xdr_u_int(xdrs, &u)) {
292 return (FALSE);
293 }
294 *cp = u;
295 return (TRUE);
296 }
297
298 /*
299 * XDR booleans
300 */
301 bool_t
302 xdr_bool(xdrs, bp)
303 register XDR *xdrs;
304 bool_t *bp;
305 {
306 long lb;
307
308 switch (xdrs->x_op) {
309
310 case XDR_ENCODE:
311 lb = *bp ? XDR_TRUE : XDR_FALSE;
312 return (XDR_PUTLONG(xdrs, &lb));
313
314 case XDR_DECODE:
315 if (!XDR_GETLONG(xdrs, &lb)) {
316 return (FALSE);
317 }
318 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
319 return (TRUE);
320
321 case XDR_FREE:
322 return (TRUE);
323 }
324 return (FALSE);
325 }
326
327 /*
328 * XDR enumerations
329 */
330 bool_t
331 xdr_enum(xdrs, ep)
332 XDR *xdrs;
333 enum_t *ep;
334 {
335 #ifndef lint
336 enum sizecheck { SIZEVAL }; /* used to find the size of an enum */
337
338 /*
339 * enums are treated as ints
340 */
341 if (sizeof (enum sizecheck) == sizeof (long)) {
342 return (xdr_long(xdrs, (long *)ep));
343 } else if (sizeof (enum sizecheck) == sizeof (short)) {
344 return (xdr_short(xdrs, (short *)ep));
345 } else {
346 return (FALSE);
347 }
348 #else
349 (void) (xdr_short(xdrs, (short *)ep));
350 return (xdr_long(xdrs, (long *)ep));
351 #endif
352 }
353
354 /*
355 * XDR opaque data
356 * Allows the specification of a fixed size sequence of opaque bytes.
357 * cp points to the opaque object and cnt gives the byte length.
358 */
359 bool_t
360 xdr_opaque(xdrs, cp, cnt)
361 register XDR *xdrs;
362 caddr_t cp;
363 register u_int cnt;
364 {
365 register u_int rndup;
366 static char crud[BYTES_PER_XDR_UNIT];
367
368 /*
369 * if no data we are done
370 */
371 if (cnt == 0)
372 return (TRUE);
373
374 /*
375 * round byte count to full xdr units
376 */
377 rndup = cnt % BYTES_PER_XDR_UNIT;
378 if (rndup > 0)
379 rndup = BYTES_PER_XDR_UNIT - rndup;
380
381 if (xdrs->x_op == XDR_DECODE) {
382 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
383 return (FALSE);
384 }
385 if (rndup == 0)
386 return (TRUE);
387 return (XDR_GETBYTES(xdrs, crud, rndup));
388 }
389
390 if (xdrs->x_op == XDR_ENCODE) {
391 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
392 return (FALSE);
393 }
394 if (rndup == 0)
395 return (TRUE);
396 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
397 }
398
399 if (xdrs->x_op == XDR_FREE) {
400 return (TRUE);
401 }
402
403 return (FALSE);
404 }
405
406 /*
407 * XDR counted bytes
408 * *cpp is a pointer to the bytes, *sizep is the count.
409 * If *cpp is NULL maxsize bytes are allocated
410 */
411 bool_t
412 xdr_bytes(xdrs, cpp, sizep, maxsize)
413 register XDR *xdrs;
414 char **cpp;
415 register u_int *sizep;
416 u_int maxsize;
417 {
418 register char *sp = *cpp; /* sp is the actual string pointer */
419 register u_int nodesize;
420
421 /*
422 * first deal with the length since xdr bytes are counted
423 */
424 if (! xdr_u_int(xdrs, sizep)) {
425 return (FALSE);
426 }
427 nodesize = *sizep;
428 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
429 return (FALSE);
430 }
431
432 /*
433 * now deal with the actual bytes
434 */
435 switch (xdrs->x_op) {
436
437 case XDR_DECODE:
438 if (nodesize == 0) {
439 return (TRUE);
440 }
441 if (sp == NULL) {
442 *cpp = sp = (char *)mem_alloc(nodesize);
443 }
444 if (sp == NULL) {
445 (void) fprintf(stderr, "xdr_bytes: out of memory\n");
446 return (FALSE);
447 }
448 /* fall into ... */
449
450 case XDR_ENCODE:
451 return (xdr_opaque(xdrs, sp, nodesize));
452
453 case XDR_FREE:
454 if (sp != NULL) {
455 mem_free(sp, nodesize);
456 *cpp = NULL;
457 }
458 return (TRUE);
459 }
460 return (FALSE);
461 }
462
463 /*
464 * Implemented here due to commonality of the object.
465 */
466 bool_t
467 xdr_netobj(xdrs, np)
468 XDR *xdrs;
469 struct netobj *np;
470 {
471
472 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
473 }
474
475 /*
476 * XDR a descriminated union
477 * Support routine for discriminated unions.
478 * You create an array of xdrdiscrim structures, terminated with
479 * an entry with a null procedure pointer. The routine gets
480 * the discriminant value and then searches the array of xdrdiscrims
481 * looking for that value. It calls the procedure given in the xdrdiscrim
482 * to handle the discriminant. If there is no specific routine a default
483 * routine may be called.
484 * If there is no specific or default routine an error is returned.
485 */
486 bool_t
487 xdr_union(xdrs, dscmp, unp, choices, dfault)
488 register XDR *xdrs;
489 enum_t *dscmp; /* enum to decide which arm to work on */
490 char *unp; /* the union itself */
491 struct xdr_discrim *choices; /* [value, xdr proc] for each arm */
492 xdrproc_t dfault; /* default xdr routine */
493 {
494 register enum_t dscm;
495
496 /*
497 * we deal with the discriminator; it's an enum
498 */
499 if (! xdr_enum(xdrs, dscmp)) {
500 return (FALSE);
501 }
502 dscm = *dscmp;
503
504 /*
505 * search choices for a value that matches the discriminator.
506 * if we find one, execute the xdr routine for that value.
507 */
508 for (; choices->proc != NULL_xdrproc_t; choices++) {
509 if (choices->value == dscm)
510 return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
511 }
512
513 /*
514 * no match - execute the default xdr routine if there is one
515 */
516 return ((dfault == NULL_xdrproc_t) ? FALSE :
517 (*dfault)(xdrs, unp, LASTUNSIGNED));
518 }
519
520
521 /*
522 * Non-portable xdr primitives.
523 * Care should be taken when moving these routines to new architectures.
524 */
525
526
527 /*
528 * XDR null terminated ASCII strings
529 * xdr_string deals with "C strings" - arrays of bytes that are
530 * terminated by a NULL character. The parameter cpp references a
531 * pointer to storage; If the pointer is null, then the necessary
532 * storage is allocated. The last parameter is the max allowed length
533 * of the string as specified by a protocol.
534 */
535 bool_t
536 xdr_string(xdrs, cpp, maxsize)
537 register XDR *xdrs;
538 char **cpp;
539 u_int maxsize;
540 {
541 register char *sp = *cpp; /* sp is the actual string pointer */
542 u_int size;
543 u_int nodesize;
544
545 /*
546 * first deal with the length since xdr strings are counted-strings
547 */
548 switch (xdrs->x_op) {
549 case XDR_FREE:
550 if (sp == NULL) {
551 return(TRUE); /* already free */
552 }
553 /* fall through... */
554 case XDR_ENCODE:
555 size = strlen(sp);
556 break;
557 default: break;
558 }
559 if (! xdr_u_int(xdrs, &size)) {
560 return (FALSE);
561 }
562 if (size > maxsize) {
563 return (FALSE);
564 }
565 nodesize = size + 1;
566
567 /*
568 * now deal with the actual bytes
569 */
570 switch (xdrs->x_op) {
571
572 case XDR_DECODE:
573 if (nodesize == 0) {
574 return (TRUE);
575 }
576 if (sp == NULL)
577 *cpp = sp = (char *)mem_alloc(nodesize);
578 if (sp == NULL) {
579 (void) fprintf(stderr, "xdr_string: out of memory\n");
580 return (FALSE);
581 }
582 sp[size] = 0;
583 /* fall into ... */
584
585 case XDR_ENCODE:
586 return (xdr_opaque(xdrs, sp, size));
587
588 case XDR_FREE:
589 mem_free(sp, nodesize);
590 *cpp = NULL;
591 return (TRUE);
592 }
593 return (FALSE);
594 }
595
596 /*
597 * Wrapper for xdr_string that can be called directly from
598 * routines like clnt_call
599 */
600 bool_t
601 xdr_wrapstring(xdrs, cpp)
602 XDR *xdrs;
603 char **cpp;
604 {
605 if (xdr_string(xdrs, cpp, LASTUNSIGNED)) {
606 return (TRUE);
607 }
608 return (FALSE);
609 }