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