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