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