1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* $NetBSD: xdr.c,v 1.22 2000/07/06 03:10:35 christos Exp $ */ |
---|
4 | |
---|
5 | /* |
---|
6 | * Sun RPC is a product of Sun Microsystems, Inc. and is provided for |
---|
7 | * unrestricted use provided that this legend is included on all tape |
---|
8 | * media and as a part of the software program in whole or part. Users |
---|
9 | * may copy or modify Sun RPC without charge, but are not authorized |
---|
10 | * to license or distribute it to anyone else except as part of a product or |
---|
11 | * program developed by the user. |
---|
12 | * |
---|
13 | * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE |
---|
14 | * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR |
---|
15 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. |
---|
16 | * |
---|
17 | * Sun RPC is provided with no support and without any obligation on the |
---|
18 | * part of Sun Microsystems, Inc. to assist in its use, correction, |
---|
19 | * modification or enhancement. |
---|
20 | * |
---|
21 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE |
---|
22 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC |
---|
23 | * OR ANY PART THEREOF. |
---|
24 | * |
---|
25 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue |
---|
26 | * or profits or other special, indirect and consequential damages, even if |
---|
27 | * Sun has been advised of the possibility of such damages. |
---|
28 | * |
---|
29 | * Sun Microsystems, Inc. |
---|
30 | * 2550 Garcia Avenue |
---|
31 | * Mountain View, California 94043 |
---|
32 | */ |
---|
33 | |
---|
34 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
35 | static char *sccsid2 = "@(#)xdr.c 1.35 87/08/12"; |
---|
36 | static char *sccsid = "@(#)xdr.c 2.1 88/07/29 4.0 RPCSRC"; |
---|
37 | #endif |
---|
38 | #include <sys/cdefs.h> |
---|
39 | __FBSDID("$FreeBSD$"); |
---|
40 | |
---|
41 | /* |
---|
42 | * xdr.c, Generic XDR routines implementation. |
---|
43 | * |
---|
44 | * Copyright (C) 1986, Sun Microsystems, Inc. |
---|
45 | * |
---|
46 | * These are the "generic" xdr routines used to serialize and de-serialize |
---|
47 | * most common data items. See xdr.h for more info on the interface to |
---|
48 | * xdr. |
---|
49 | */ |
---|
50 | |
---|
51 | #include "namespace.h" |
---|
52 | #include <err.h> |
---|
53 | #include <stdio.h> |
---|
54 | #include <stdlib.h> |
---|
55 | #include <string.h> |
---|
56 | |
---|
57 | #include <rpc/types.h> |
---|
58 | #include <rpc/xdr.h> |
---|
59 | #include "un-namespace.h" |
---|
60 | |
---|
61 | typedef quad_t longlong_t; /* ANSI long long type */ |
---|
62 | typedef u_quad_t u_longlong_t; /* ANSI unsigned long long type */ |
---|
63 | |
---|
64 | /* |
---|
65 | * constants specific to the xdr "protocol" |
---|
66 | */ |
---|
67 | #define XDR_FALSE ((long) 0) |
---|
68 | #define XDR_TRUE ((long) 1) |
---|
69 | #define LASTUNSIGNED ((u_int) 0-1) |
---|
70 | |
---|
71 | /* |
---|
72 | * for unit alignment |
---|
73 | */ |
---|
74 | static const char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 }; |
---|
75 | |
---|
76 | /* |
---|
77 | * Free a data structure using XDR |
---|
78 | * Not a filter, but a convenient utility nonetheless |
---|
79 | */ |
---|
80 | void |
---|
81 | xdr_free(proc, objp) |
---|
82 | xdrproc_t proc; |
---|
83 | void *objp; |
---|
84 | { |
---|
85 | XDR x; |
---|
86 | |
---|
87 | x.x_op = XDR_FREE; |
---|
88 | (*proc)(&x, objp); |
---|
89 | } |
---|
90 | |
---|
91 | /* |
---|
92 | * XDR nothing |
---|
93 | */ |
---|
94 | bool_t |
---|
95 | xdr_void(void) |
---|
96 | { |
---|
97 | |
---|
98 | return (TRUE); |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | /* |
---|
103 | * XDR integers |
---|
104 | */ |
---|
105 | bool_t |
---|
106 | xdr_int(xdrs, ip) |
---|
107 | XDR *xdrs; |
---|
108 | int *ip; |
---|
109 | { |
---|
110 | long l; |
---|
111 | |
---|
112 | switch (xdrs->x_op) { |
---|
113 | |
---|
114 | case XDR_ENCODE: |
---|
115 | l = (long) *ip; |
---|
116 | return (XDR_PUTLONG(xdrs, &l)); |
---|
117 | |
---|
118 | case XDR_DECODE: |
---|
119 | if (!XDR_GETLONG(xdrs, &l)) { |
---|
120 | return (FALSE); |
---|
121 | } |
---|
122 | *ip = (int) l; |
---|
123 | return (TRUE); |
---|
124 | |
---|
125 | case XDR_FREE: |
---|
126 | return (TRUE); |
---|
127 | } |
---|
128 | /* NOTREACHED */ |
---|
129 | return (FALSE); |
---|
130 | } |
---|
131 | |
---|
132 | /* |
---|
133 | * XDR unsigned integers |
---|
134 | */ |
---|
135 | bool_t |
---|
136 | xdr_u_int(xdrs, up) |
---|
137 | XDR *xdrs; |
---|
138 | u_int *up; |
---|
139 | { |
---|
140 | u_long l; |
---|
141 | |
---|
142 | switch (xdrs->x_op) { |
---|
143 | |
---|
144 | case XDR_ENCODE: |
---|
145 | l = (u_long) *up; |
---|
146 | return (XDR_PUTLONG(xdrs, (long *)&l)); |
---|
147 | |
---|
148 | case XDR_DECODE: |
---|
149 | if (!XDR_GETLONG(xdrs, (long *)&l)) { |
---|
150 | return (FALSE); |
---|
151 | } |
---|
152 | *up = (u_int) l; |
---|
153 | return (TRUE); |
---|
154 | |
---|
155 | case XDR_FREE: |
---|
156 | return (TRUE); |
---|
157 | } |
---|
158 | /* NOTREACHED */ |
---|
159 | return (FALSE); |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | /* |
---|
164 | * XDR long integers |
---|
165 | * same as xdr_u_long - open coded to save a proc call! |
---|
166 | */ |
---|
167 | bool_t |
---|
168 | xdr_long(xdrs, lp) |
---|
169 | XDR *xdrs; |
---|
170 | long *lp; |
---|
171 | { |
---|
172 | switch (xdrs->x_op) { |
---|
173 | case XDR_ENCODE: |
---|
174 | return (XDR_PUTLONG(xdrs, lp)); |
---|
175 | case XDR_DECODE: |
---|
176 | return (XDR_GETLONG(xdrs, lp)); |
---|
177 | case XDR_FREE: |
---|
178 | return (TRUE); |
---|
179 | } |
---|
180 | /* NOTREACHED */ |
---|
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 | XDR *xdrs; |
---|
191 | u_long *ulp; |
---|
192 | { |
---|
193 | switch (xdrs->x_op) { |
---|
194 | case XDR_ENCODE: |
---|
195 | return (XDR_PUTLONG(xdrs, (long *)ulp)); |
---|
196 | case XDR_DECODE: |
---|
197 | return (XDR_GETLONG(xdrs, (long *)ulp)); |
---|
198 | case XDR_FREE: |
---|
199 | return (TRUE); |
---|
200 | } |
---|
201 | /* NOTREACHED */ |
---|
202 | return (FALSE); |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | /* |
---|
207 | * XDR 32-bit integers |
---|
208 | * same as xdr_u_int32_t - open coded to save a proc call! |
---|
209 | */ |
---|
210 | bool_t |
---|
211 | xdr_int32_t(xdrs, int32_p) |
---|
212 | XDR *xdrs; |
---|
213 | int32_t *int32_p; |
---|
214 | { |
---|
215 | long l; |
---|
216 | |
---|
217 | switch (xdrs->x_op) { |
---|
218 | |
---|
219 | case XDR_ENCODE: |
---|
220 | l = (long) *int32_p; |
---|
221 | return (XDR_PUTLONG(xdrs, &l)); |
---|
222 | |
---|
223 | case XDR_DECODE: |
---|
224 | if (!XDR_GETLONG(xdrs, &l)) { |
---|
225 | return (FALSE); |
---|
226 | } |
---|
227 | *int32_p = (int32_t) l; |
---|
228 | return (TRUE); |
---|
229 | |
---|
230 | case XDR_FREE: |
---|
231 | return (TRUE); |
---|
232 | } |
---|
233 | /* NOTREACHED */ |
---|
234 | return (FALSE); |
---|
235 | } |
---|
236 | |
---|
237 | /* |
---|
238 | * XDR unsigned 32-bit integers |
---|
239 | * same as xdr_int32_t - open coded to save a proc call! |
---|
240 | */ |
---|
241 | bool_t |
---|
242 | xdr_u_int32_t(xdrs, u_int32_p) |
---|
243 | XDR *xdrs; |
---|
244 | u_int32_t *u_int32_p; |
---|
245 | { |
---|
246 | u_long l; |
---|
247 | |
---|
248 | switch (xdrs->x_op) { |
---|
249 | |
---|
250 | case XDR_ENCODE: |
---|
251 | l = (u_long) *u_int32_p; |
---|
252 | return (XDR_PUTLONG(xdrs, (long *)&l)); |
---|
253 | |
---|
254 | case XDR_DECODE: |
---|
255 | if (!XDR_GETLONG(xdrs, (long *)&l)) { |
---|
256 | return (FALSE); |
---|
257 | } |
---|
258 | *u_int32_p = (u_int32_t) l; |
---|
259 | return (TRUE); |
---|
260 | |
---|
261 | case XDR_FREE: |
---|
262 | return (TRUE); |
---|
263 | } |
---|
264 | /* NOTREACHED */ |
---|
265 | return (FALSE); |
---|
266 | } |
---|
267 | |
---|
268 | /* |
---|
269 | * XDR unsigned 32-bit integers |
---|
270 | * same as xdr_int32_t - open coded to save a proc call! |
---|
271 | */ |
---|
272 | bool_t |
---|
273 | xdr_uint32_t(xdrs, u_int32_p) |
---|
274 | XDR *xdrs; |
---|
275 | uint32_t *u_int32_p; |
---|
276 | { |
---|
277 | u_long l; |
---|
278 | |
---|
279 | switch (xdrs->x_op) { |
---|
280 | |
---|
281 | case XDR_ENCODE: |
---|
282 | l = (u_long) *u_int32_p; |
---|
283 | return (XDR_PUTLONG(xdrs, (long *)&l)); |
---|
284 | |
---|
285 | case XDR_DECODE: |
---|
286 | if (!XDR_GETLONG(xdrs, (long *)&l)) { |
---|
287 | return (FALSE); |
---|
288 | } |
---|
289 | *u_int32_p = (u_int32_t) l; |
---|
290 | return (TRUE); |
---|
291 | |
---|
292 | case XDR_FREE: |
---|
293 | return (TRUE); |
---|
294 | } |
---|
295 | /* NOTREACHED */ |
---|
296 | return (FALSE); |
---|
297 | } |
---|
298 | |
---|
299 | /* |
---|
300 | * XDR short integers |
---|
301 | */ |
---|
302 | bool_t |
---|
303 | xdr_short(xdrs, sp) |
---|
304 | XDR *xdrs; |
---|
305 | short *sp; |
---|
306 | { |
---|
307 | long l; |
---|
308 | |
---|
309 | switch (xdrs->x_op) { |
---|
310 | |
---|
311 | case XDR_ENCODE: |
---|
312 | l = (long) *sp; |
---|
313 | return (XDR_PUTLONG(xdrs, &l)); |
---|
314 | |
---|
315 | case XDR_DECODE: |
---|
316 | if (!XDR_GETLONG(xdrs, &l)) { |
---|
317 | return (FALSE); |
---|
318 | } |
---|
319 | *sp = (short) l; |
---|
320 | return (TRUE); |
---|
321 | |
---|
322 | case XDR_FREE: |
---|
323 | return (TRUE); |
---|
324 | } |
---|
325 | /* NOTREACHED */ |
---|
326 | return (FALSE); |
---|
327 | } |
---|
328 | |
---|
329 | /* |
---|
330 | * XDR unsigned short integers |
---|
331 | */ |
---|
332 | bool_t |
---|
333 | xdr_u_short(xdrs, usp) |
---|
334 | XDR *xdrs; |
---|
335 | u_short *usp; |
---|
336 | { |
---|
337 | u_long l; |
---|
338 | |
---|
339 | switch (xdrs->x_op) { |
---|
340 | |
---|
341 | case XDR_ENCODE: |
---|
342 | l = (u_long) *usp; |
---|
343 | return (XDR_PUTLONG(xdrs, (long *)&l)); |
---|
344 | |
---|
345 | case XDR_DECODE: |
---|
346 | if (!XDR_GETLONG(xdrs, (long *)&l)) { |
---|
347 | return (FALSE); |
---|
348 | } |
---|
349 | *usp = (u_short) l; |
---|
350 | return (TRUE); |
---|
351 | |
---|
352 | case XDR_FREE: |
---|
353 | return (TRUE); |
---|
354 | } |
---|
355 | /* NOTREACHED */ |
---|
356 | return (FALSE); |
---|
357 | } |
---|
358 | |
---|
359 | |
---|
360 | /* |
---|
361 | * XDR 16-bit integers |
---|
362 | */ |
---|
363 | bool_t |
---|
364 | xdr_int16_t(xdrs, int16_p) |
---|
365 | XDR *xdrs; |
---|
366 | int16_t *int16_p; |
---|
367 | { |
---|
368 | long l; |
---|
369 | |
---|
370 | switch (xdrs->x_op) { |
---|
371 | |
---|
372 | case XDR_ENCODE: |
---|
373 | l = (long) *int16_p; |
---|
374 | return (XDR_PUTLONG(xdrs, &l)); |
---|
375 | |
---|
376 | case XDR_DECODE: |
---|
377 | if (!XDR_GETLONG(xdrs, &l)) { |
---|
378 | return (FALSE); |
---|
379 | } |
---|
380 | *int16_p = (int16_t) l; |
---|
381 | return (TRUE); |
---|
382 | |
---|
383 | case XDR_FREE: |
---|
384 | return (TRUE); |
---|
385 | } |
---|
386 | /* NOTREACHED */ |
---|
387 | return (FALSE); |
---|
388 | } |
---|
389 | |
---|
390 | /* |
---|
391 | * XDR unsigned 16-bit integers |
---|
392 | */ |
---|
393 | bool_t |
---|
394 | xdr_u_int16_t(xdrs, u_int16_p) |
---|
395 | XDR *xdrs; |
---|
396 | u_int16_t *u_int16_p; |
---|
397 | { |
---|
398 | u_long l; |
---|
399 | |
---|
400 | switch (xdrs->x_op) { |
---|
401 | |
---|
402 | case XDR_ENCODE: |
---|
403 | l = (u_long) *u_int16_p; |
---|
404 | return (XDR_PUTLONG(xdrs, (long *)&l)); |
---|
405 | |
---|
406 | case XDR_DECODE: |
---|
407 | if (!XDR_GETLONG(xdrs, (long *)&l)) { |
---|
408 | return (FALSE); |
---|
409 | } |
---|
410 | *u_int16_p = (u_int16_t) l; |
---|
411 | return (TRUE); |
---|
412 | |
---|
413 | case XDR_FREE: |
---|
414 | return (TRUE); |
---|
415 | } |
---|
416 | /* NOTREACHED */ |
---|
417 | return (FALSE); |
---|
418 | } |
---|
419 | |
---|
420 | /* |
---|
421 | * XDR unsigned 16-bit integers |
---|
422 | */ |
---|
423 | bool_t |
---|
424 | xdr_uint16_t(xdrs, u_int16_p) |
---|
425 | XDR *xdrs; |
---|
426 | uint16_t *u_int16_p; |
---|
427 | { |
---|
428 | u_long l; |
---|
429 | |
---|
430 | switch (xdrs->x_op) { |
---|
431 | |
---|
432 | case XDR_ENCODE: |
---|
433 | l = (u_long) *u_int16_p; |
---|
434 | return (XDR_PUTLONG(xdrs, (long *)&l)); |
---|
435 | |
---|
436 | case XDR_DECODE: |
---|
437 | if (!XDR_GETLONG(xdrs, (long *)&l)) { |
---|
438 | return (FALSE); |
---|
439 | } |
---|
440 | *u_int16_p = (u_int16_t) l; |
---|
441 | return (TRUE); |
---|
442 | |
---|
443 | case XDR_FREE: |
---|
444 | return (TRUE); |
---|
445 | } |
---|
446 | /* NOTREACHED */ |
---|
447 | return (FALSE); |
---|
448 | } |
---|
449 | |
---|
450 | |
---|
451 | /* |
---|
452 | * XDR a char |
---|
453 | */ |
---|
454 | bool_t |
---|
455 | xdr_char(xdrs, cp) |
---|
456 | XDR *xdrs; |
---|
457 | char *cp; |
---|
458 | { |
---|
459 | int i; |
---|
460 | |
---|
461 | i = (*cp); |
---|
462 | if (!xdr_int(xdrs, &i)) { |
---|
463 | return (FALSE); |
---|
464 | } |
---|
465 | *cp = i; |
---|
466 | return (TRUE); |
---|
467 | } |
---|
468 | |
---|
469 | /* |
---|
470 | * XDR an unsigned char |
---|
471 | */ |
---|
472 | bool_t |
---|
473 | xdr_u_char(xdrs, cp) |
---|
474 | XDR *xdrs; |
---|
475 | u_char *cp; |
---|
476 | { |
---|
477 | u_int u; |
---|
478 | |
---|
479 | u = (*cp); |
---|
480 | if (!xdr_u_int(xdrs, &u)) { |
---|
481 | return (FALSE); |
---|
482 | } |
---|
483 | *cp = u; |
---|
484 | return (TRUE); |
---|
485 | } |
---|
486 | |
---|
487 | /* |
---|
488 | * XDR booleans |
---|
489 | */ |
---|
490 | bool_t |
---|
491 | xdr_bool(xdrs, bp) |
---|
492 | XDR *xdrs; |
---|
493 | bool_t *bp; |
---|
494 | { |
---|
495 | long lb; |
---|
496 | |
---|
497 | switch (xdrs->x_op) { |
---|
498 | |
---|
499 | case XDR_ENCODE: |
---|
500 | lb = *bp ? XDR_TRUE : XDR_FALSE; |
---|
501 | return (XDR_PUTLONG(xdrs, &lb)); |
---|
502 | |
---|
503 | case XDR_DECODE: |
---|
504 | if (!XDR_GETLONG(xdrs, &lb)) { |
---|
505 | return (FALSE); |
---|
506 | } |
---|
507 | *bp = (lb == XDR_FALSE) ? FALSE : TRUE; |
---|
508 | return (TRUE); |
---|
509 | |
---|
510 | case XDR_FREE: |
---|
511 | return (TRUE); |
---|
512 | } |
---|
513 | /* NOTREACHED */ |
---|
514 | return (FALSE); |
---|
515 | } |
---|
516 | |
---|
517 | /* |
---|
518 | * XDR enumerations |
---|
519 | */ |
---|
520 | bool_t |
---|
521 | xdr_enum(xdrs, ep) |
---|
522 | XDR *xdrs; |
---|
523 | enum_t *ep; |
---|
524 | { |
---|
525 | enum sizecheck { SIZEVAL }; /* used to find the size of an enum */ |
---|
526 | |
---|
527 | /* |
---|
528 | * enums are treated as ints |
---|
529 | */ |
---|
530 | /* LINTED */ if (sizeof (enum sizecheck) == sizeof (long)) { |
---|
531 | return (xdr_long(xdrs, (long *)(void *)ep)); |
---|
532 | } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (int)) { |
---|
533 | return (xdr_int(xdrs, (int *)(void *)ep)); |
---|
534 | } else /* LINTED */ if (sizeof (enum sizecheck) == sizeof (short)) { |
---|
535 | return (xdr_short(xdrs, (short *)(void *)ep)); |
---|
536 | } else { |
---|
537 | return (FALSE); |
---|
538 | } |
---|
539 | } |
---|
540 | |
---|
541 | /* |
---|
542 | * XDR opaque data |
---|
543 | * Allows the specification of a fixed size sequence of opaque bytes. |
---|
544 | * cp points to the opaque object and cnt gives the byte length. |
---|
545 | */ |
---|
546 | bool_t |
---|
547 | xdr_opaque(xdrs, cp, cnt) |
---|
548 | XDR *xdrs; |
---|
549 | caddr_t cp; |
---|
550 | u_int cnt; |
---|
551 | { |
---|
552 | u_int rndup; |
---|
553 | static int crud[BYTES_PER_XDR_UNIT]; |
---|
554 | |
---|
555 | /* |
---|
556 | * if no data we are done |
---|
557 | */ |
---|
558 | if (cnt == 0) |
---|
559 | return (TRUE); |
---|
560 | |
---|
561 | /* |
---|
562 | * round byte count to full xdr units |
---|
563 | */ |
---|
564 | rndup = cnt % BYTES_PER_XDR_UNIT; |
---|
565 | if (rndup > 0) |
---|
566 | rndup = BYTES_PER_XDR_UNIT - rndup; |
---|
567 | |
---|
568 | if (xdrs->x_op == XDR_DECODE) { |
---|
569 | if (!XDR_GETBYTES(xdrs, cp, cnt)) { |
---|
570 | return (FALSE); |
---|
571 | } |
---|
572 | if (rndup == 0) |
---|
573 | return (TRUE); |
---|
574 | return (XDR_GETBYTES(xdrs, (caddr_t)(void *)crud, rndup)); |
---|
575 | } |
---|
576 | |
---|
577 | if (xdrs->x_op == XDR_ENCODE) { |
---|
578 | if (!XDR_PUTBYTES(xdrs, cp, cnt)) { |
---|
579 | return (FALSE); |
---|
580 | } |
---|
581 | if (rndup == 0) |
---|
582 | return (TRUE); |
---|
583 | return (XDR_PUTBYTES(xdrs, xdr_zero, rndup)); |
---|
584 | } |
---|
585 | |
---|
586 | if (xdrs->x_op == XDR_FREE) { |
---|
587 | return (TRUE); |
---|
588 | } |
---|
589 | |
---|
590 | return (FALSE); |
---|
591 | } |
---|
592 | |
---|
593 | /* |
---|
594 | * XDR counted bytes |
---|
595 | * *cpp is a pointer to the bytes, *sizep is the count. |
---|
596 | * If *cpp is NULL maxsize bytes are allocated |
---|
597 | */ |
---|
598 | bool_t |
---|
599 | xdr_bytes(xdrs, cpp, sizep, maxsize) |
---|
600 | XDR *xdrs; |
---|
601 | char **cpp; |
---|
602 | u_int *sizep; |
---|
603 | u_int maxsize; |
---|
604 | { |
---|
605 | char *sp = *cpp; /* sp is the actual string pointer */ |
---|
606 | u_int nodesize; |
---|
607 | |
---|
608 | /* |
---|
609 | * first deal with the length since xdr bytes are counted |
---|
610 | */ |
---|
611 | if (! xdr_u_int(xdrs, sizep)) { |
---|
612 | return (FALSE); |
---|
613 | } |
---|
614 | nodesize = *sizep; |
---|
615 | if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) { |
---|
616 | return (FALSE); |
---|
617 | } |
---|
618 | |
---|
619 | /* |
---|
620 | * now deal with the actual bytes |
---|
621 | */ |
---|
622 | switch (xdrs->x_op) { |
---|
623 | |
---|
624 | case XDR_DECODE: |
---|
625 | if (nodesize == 0) { |
---|
626 | return (TRUE); |
---|
627 | } |
---|
628 | if (sp == NULL) { |
---|
629 | *cpp = sp = mem_alloc(nodesize); |
---|
630 | } |
---|
631 | if (sp == NULL) { |
---|
632 | warnx("xdr_bytes: out of memory"); |
---|
633 | return (FALSE); |
---|
634 | } |
---|
635 | /* FALLTHROUGH */ |
---|
636 | |
---|
637 | case XDR_ENCODE: |
---|
638 | return (xdr_opaque(xdrs, sp, nodesize)); |
---|
639 | |
---|
640 | case XDR_FREE: |
---|
641 | if (sp != NULL) { |
---|
642 | mem_free(sp, nodesize); |
---|
643 | *cpp = NULL; |
---|
644 | } |
---|
645 | return (TRUE); |
---|
646 | } |
---|
647 | /* NOTREACHED */ |
---|
648 | return (FALSE); |
---|
649 | } |
---|
650 | |
---|
651 | /* |
---|
652 | * Implemented here due to commonality of the object. |
---|
653 | */ |
---|
654 | bool_t |
---|
655 | xdr_netobj(xdrs, np) |
---|
656 | XDR *xdrs; |
---|
657 | struct netobj *np; |
---|
658 | { |
---|
659 | |
---|
660 | return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ)); |
---|
661 | } |
---|
662 | |
---|
663 | /* |
---|
664 | * XDR a descriminated union |
---|
665 | * Support routine for discriminated unions. |
---|
666 | * You create an array of xdrdiscrim structures, terminated with |
---|
667 | * an entry with a null procedure pointer. The routine gets |
---|
668 | * the discriminant value and then searches the array of xdrdiscrims |
---|
669 | * looking for that value. It calls the procedure given in the xdrdiscrim |
---|
670 | * to handle the discriminant. If there is no specific routine a default |
---|
671 | * routine may be called. |
---|
672 | * If there is no specific or default routine an error is returned. |
---|
673 | */ |
---|
674 | bool_t |
---|
675 | xdr_union(xdrs, dscmp, unp, choices, dfault) |
---|
676 | XDR *xdrs; |
---|
677 | enum_t *dscmp; /* enum to decide which arm to work on */ |
---|
678 | char *unp; /* the union itself */ |
---|
679 | const struct xdr_discrim *choices; /* [value, xdr proc] for each arm */ |
---|
680 | xdrproc_t dfault; /* default xdr routine */ |
---|
681 | { |
---|
682 | enum_t dscm; |
---|
683 | |
---|
684 | /* |
---|
685 | * we deal with the discriminator; it's an enum |
---|
686 | */ |
---|
687 | if (! xdr_enum(xdrs, dscmp)) { |
---|
688 | return (FALSE); |
---|
689 | } |
---|
690 | dscm = *dscmp; |
---|
691 | |
---|
692 | /* |
---|
693 | * search choices for a value that matches the discriminator. |
---|
694 | * if we find one, execute the xdr routine for that value. |
---|
695 | */ |
---|
696 | for (; choices->proc != NULL_xdrproc_t; choices++) { |
---|
697 | if (choices->value == dscm) |
---|
698 | return ((*(choices->proc))(xdrs, unp)); |
---|
699 | } |
---|
700 | |
---|
701 | /* |
---|
702 | * no match - execute the default xdr routine if there is one |
---|
703 | */ |
---|
704 | return ((dfault == NULL_xdrproc_t) ? FALSE : |
---|
705 | (*dfault)(xdrs, unp)); |
---|
706 | } |
---|
707 | |
---|
708 | |
---|
709 | /* |
---|
710 | * Non-portable xdr primitives. |
---|
711 | * Care should be taken when moving these routines to new architectures. |
---|
712 | */ |
---|
713 | |
---|
714 | |
---|
715 | /* |
---|
716 | * XDR null terminated ASCII strings |
---|
717 | * xdr_string deals with "C strings" - arrays of bytes that are |
---|
718 | * terminated by a NULL character. The parameter cpp references a |
---|
719 | * pointer to storage; If the pointer is null, then the necessary |
---|
720 | * storage is allocated. The last parameter is the max allowed length |
---|
721 | * of the string as specified by a protocol. |
---|
722 | */ |
---|
723 | bool_t |
---|
724 | xdr_string(xdrs, cpp, maxsize) |
---|
725 | XDR *xdrs; |
---|
726 | char **cpp; |
---|
727 | u_int maxsize; |
---|
728 | { |
---|
729 | char *sp = *cpp; /* sp is the actual string pointer */ |
---|
730 | u_int size; |
---|
731 | u_int nodesize; |
---|
732 | |
---|
733 | /* |
---|
734 | * first deal with the length since xdr strings are counted-strings |
---|
735 | */ |
---|
736 | switch (xdrs->x_op) { |
---|
737 | case XDR_FREE: |
---|
738 | if (sp == NULL) { |
---|
739 | return(TRUE); /* already free */ |
---|
740 | } |
---|
741 | /* FALLTHROUGH */ |
---|
742 | case XDR_ENCODE: |
---|
743 | size = strlen(sp); |
---|
744 | break; |
---|
745 | case XDR_DECODE: |
---|
746 | break; |
---|
747 | } |
---|
748 | if (! xdr_u_int(xdrs, &size)) { |
---|
749 | return (FALSE); |
---|
750 | } |
---|
751 | if (size > maxsize) { |
---|
752 | return (FALSE); |
---|
753 | } |
---|
754 | nodesize = size + 1; |
---|
755 | |
---|
756 | /* |
---|
757 | * now deal with the actual bytes |
---|
758 | */ |
---|
759 | switch (xdrs->x_op) { |
---|
760 | |
---|
761 | case XDR_DECODE: |
---|
762 | if (nodesize == 0) { |
---|
763 | return (TRUE); |
---|
764 | } |
---|
765 | if (sp == NULL) |
---|
766 | *cpp = sp = mem_alloc(nodesize); |
---|
767 | if (sp == NULL) { |
---|
768 | warnx("xdr_string: out of memory"); |
---|
769 | return (FALSE); |
---|
770 | } |
---|
771 | sp[size] = 0; |
---|
772 | /* FALLTHROUGH */ |
---|
773 | |
---|
774 | case XDR_ENCODE: |
---|
775 | return (xdr_opaque(xdrs, sp, size)); |
---|
776 | |
---|
777 | case XDR_FREE: |
---|
778 | mem_free(sp, nodesize); |
---|
779 | *cpp = NULL; |
---|
780 | return (TRUE); |
---|
781 | } |
---|
782 | /* NOTREACHED */ |
---|
783 | return (FALSE); |
---|
784 | } |
---|
785 | |
---|
786 | /* |
---|
787 | * Wrapper for xdr_string that can be called directly from |
---|
788 | * routines like clnt_call |
---|
789 | */ |
---|
790 | bool_t |
---|
791 | xdr_wrapstring(xdrs, cpp) |
---|
792 | XDR *xdrs; |
---|
793 | char **cpp; |
---|
794 | { |
---|
795 | return xdr_string(xdrs, cpp, LASTUNSIGNED); |
---|
796 | } |
---|
797 | |
---|
798 | /* |
---|
799 | * NOTE: xdr_hyper(), xdr_u_hyper(), xdr_longlong_t(), and xdr_u_longlong_t() |
---|
800 | * are in the "non-portable" section because they require that a `long long' |
---|
801 | * be a 64-bit type. |
---|
802 | * |
---|
803 | * --thorpej@netbsd.org, November 30, 1999 |
---|
804 | */ |
---|
805 | |
---|
806 | /* |
---|
807 | * XDR 64-bit integers |
---|
808 | */ |
---|
809 | bool_t |
---|
810 | xdr_int64_t(xdrs, llp) |
---|
811 | XDR *xdrs; |
---|
812 | int64_t *llp; |
---|
813 | { |
---|
814 | u_long ul[2]; |
---|
815 | |
---|
816 | switch (xdrs->x_op) { |
---|
817 | case XDR_ENCODE: |
---|
818 | ul[0] = (u_long)((u_int64_t)*llp >> 32) & 0xffffffff; |
---|
819 | ul[1] = (u_long)((u_int64_t)*llp) & 0xffffffff; |
---|
820 | if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE) |
---|
821 | return (FALSE); |
---|
822 | return (XDR_PUTLONG(xdrs, (long *)&ul[1])); |
---|
823 | case XDR_DECODE: |
---|
824 | if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE) |
---|
825 | return (FALSE); |
---|
826 | if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE) |
---|
827 | return (FALSE); |
---|
828 | *llp = (int64_t) |
---|
829 | (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1])); |
---|
830 | return (TRUE); |
---|
831 | case XDR_FREE: |
---|
832 | return (TRUE); |
---|
833 | } |
---|
834 | /* NOTREACHED */ |
---|
835 | return (FALSE); |
---|
836 | } |
---|
837 | |
---|
838 | |
---|
839 | /* |
---|
840 | * XDR unsigned 64-bit integers |
---|
841 | */ |
---|
842 | bool_t |
---|
843 | xdr_u_int64_t(xdrs, ullp) |
---|
844 | XDR *xdrs; |
---|
845 | u_int64_t *ullp; |
---|
846 | { |
---|
847 | u_long ul[2]; |
---|
848 | |
---|
849 | switch (xdrs->x_op) { |
---|
850 | case XDR_ENCODE: |
---|
851 | ul[0] = (u_long)(*ullp >> 32) & 0xffffffff; |
---|
852 | ul[1] = (u_long)(*ullp) & 0xffffffff; |
---|
853 | if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE) |
---|
854 | return (FALSE); |
---|
855 | return (XDR_PUTLONG(xdrs, (long *)&ul[1])); |
---|
856 | case XDR_DECODE: |
---|
857 | if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE) |
---|
858 | return (FALSE); |
---|
859 | if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE) |
---|
860 | return (FALSE); |
---|
861 | *ullp = (u_int64_t) |
---|
862 | (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1])); |
---|
863 | return (TRUE); |
---|
864 | case XDR_FREE: |
---|
865 | return (TRUE); |
---|
866 | } |
---|
867 | /* NOTREACHED */ |
---|
868 | return (FALSE); |
---|
869 | } |
---|
870 | |
---|
871 | /* |
---|
872 | * XDR unsigned 64-bit integers |
---|
873 | */ |
---|
874 | bool_t |
---|
875 | xdr_uint64_t(xdrs, ullp) |
---|
876 | XDR *xdrs; |
---|
877 | uint64_t *ullp; |
---|
878 | { |
---|
879 | u_long ul[2]; |
---|
880 | |
---|
881 | switch (xdrs->x_op) { |
---|
882 | case XDR_ENCODE: |
---|
883 | ul[0] = (u_long)(*ullp >> 32) & 0xffffffff; |
---|
884 | ul[1] = (u_long)(*ullp) & 0xffffffff; |
---|
885 | if (XDR_PUTLONG(xdrs, (long *)&ul[0]) == FALSE) |
---|
886 | return (FALSE); |
---|
887 | return (XDR_PUTLONG(xdrs, (long *)&ul[1])); |
---|
888 | case XDR_DECODE: |
---|
889 | if (XDR_GETLONG(xdrs, (long *)&ul[0]) == FALSE) |
---|
890 | return (FALSE); |
---|
891 | if (XDR_GETLONG(xdrs, (long *)&ul[1]) == FALSE) |
---|
892 | return (FALSE); |
---|
893 | *ullp = (u_int64_t) |
---|
894 | (((u_int64_t)ul[0] << 32) | ((u_int64_t)ul[1])); |
---|
895 | return (TRUE); |
---|
896 | case XDR_FREE: |
---|
897 | return (TRUE); |
---|
898 | } |
---|
899 | /* NOTREACHED */ |
---|
900 | return (FALSE); |
---|
901 | } |
---|
902 | |
---|
903 | |
---|
904 | /* |
---|
905 | * XDR hypers |
---|
906 | */ |
---|
907 | bool_t |
---|
908 | xdr_hyper(xdrs, llp) |
---|
909 | XDR *xdrs; |
---|
910 | longlong_t *llp; |
---|
911 | { |
---|
912 | |
---|
913 | /* |
---|
914 | * Don't bother open-coding this; it's a fair amount of code. Just |
---|
915 | * call xdr_int64_t(). |
---|
916 | */ |
---|
917 | return (xdr_int64_t(xdrs, (int64_t *)llp)); |
---|
918 | } |
---|
919 | |
---|
920 | |
---|
921 | /* |
---|
922 | * XDR unsigned hypers |
---|
923 | */ |
---|
924 | bool_t |
---|
925 | xdr_u_hyper(xdrs, ullp) |
---|
926 | XDR *xdrs; |
---|
927 | u_longlong_t *ullp; |
---|
928 | { |
---|
929 | |
---|
930 | /* |
---|
931 | * Don't bother open-coding this; it's a fair amount of code. Just |
---|
932 | * call xdr_u_int64_t(). |
---|
933 | */ |
---|
934 | return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp)); |
---|
935 | } |
---|
936 | |
---|
937 | |
---|
938 | /* |
---|
939 | * XDR longlong_t's |
---|
940 | */ |
---|
941 | bool_t |
---|
942 | xdr_longlong_t(xdrs, llp) |
---|
943 | XDR *xdrs; |
---|
944 | longlong_t *llp; |
---|
945 | { |
---|
946 | |
---|
947 | /* |
---|
948 | * Don't bother open-coding this; it's a fair amount of code. Just |
---|
949 | * call xdr_int64_t(). |
---|
950 | */ |
---|
951 | return (xdr_int64_t(xdrs, (int64_t *)llp)); |
---|
952 | } |
---|
953 | |
---|
954 | |
---|
955 | /* |
---|
956 | * XDR u_longlong_t's |
---|
957 | */ |
---|
958 | bool_t |
---|
959 | xdr_u_longlong_t(xdrs, ullp) |
---|
960 | XDR *xdrs; |
---|
961 | u_longlong_t *ullp; |
---|
962 | { |
---|
963 | |
---|
964 | /* |
---|
965 | * Don't bother open-coding this; it's a fair amount of code. Just |
---|
966 | * call xdr_u_int64_t(). |
---|
967 | */ |
---|
968 | return (xdr_u_int64_t(xdrs, (u_int64_t *)ullp)); |
---|
969 | } |
---|