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