]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/cddl/contrib/opensolaris/uts/common/rpc/xdr.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / cddl / contrib / opensolaris / uts / common / rpc / xdr.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26
27 /*      Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T     */
28 /*        All Rights Reserved   */
29
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34
35 #pragma ident   "%Z%%M% %I%     %E% SMI"
36
37 /*
38  * xdr.c, generic XDR routines implementation.
39  * These are the "generic" xdr routines used to serialize and de-serialize
40  * most common data items.  See xdr.h for more info on the interface to
41  * xdr.
42  */
43
44 #include <sys/param.h>
45 #include <sys/cmn_err.h>
46 #include <sys/types.h>
47 #include <sys/systm.h>
48
49 #include <rpc/types.h>
50 #include <rpc/xdr.h>
51
52 #pragma weak xdr_int32_t = xdr_int
53 #pragma weak xdr_uint32_t = xdr_u_int
54 #pragma weak xdr_int64_t = xdr_longlong_t
55 #pragma weak xdr_uint64_t = xdr_u_longlong_t
56
57 #if defined(sun)
58 #if !defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
59 #error "Exactly one of _BIG_ENDIAN or _LITTLE_ENDIAN must be defined"
60 #elif defined(_BIG_ENDIAN) && defined(_LITTLE_ENDIAN)
61 #error "Only one of _BIG_ENDIAN or _LITTLE_ENDIAN may be defined"
62 #endif
63 #endif
64
65 /*
66  * constants specific to the xdr "protocol"
67  */
68 #define XDR_FALSE       ((int32_t)0)
69 #define XDR_TRUE        ((int32_t)1)
70 #define LASTUNSIGNED    ((uint_t)0-1)
71
72 /*
73  * for unit alignment
74  */
75 static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
76
77 /*
78  * Free a data structure using XDR
79  * Not a filter, but a convenient utility nonetheless
80  */
81 void
82 xdr_free(xdrproc_t proc, char *objp)
83 {
84         XDR x;
85
86         x.x_op = XDR_FREE;
87         (*proc)(&x, objp);
88 }
89
90 /*
91  * XDR nothing
92  */
93 bool_t
94 xdr_void(void)
95 {
96         return (TRUE);
97 }
98
99 /*
100  * XDR integers
101  *
102  * PSARC 2003/523 Contract Private Interface
103  * xdr_int
104  * Changes must be reviewed by Solaris File Sharing
105  * Changes must be communicated to contract-2003-523@sun.com
106  */
107 bool_t
108 xdr_int(XDR *xdrs, int *ip)
109 {
110         if (xdrs->x_op == XDR_ENCODE)
111                 return (XDR_PUTINT32(xdrs, ip));
112
113         if (xdrs->x_op == XDR_DECODE)
114                 return (XDR_GETINT32(xdrs, ip));
115
116         if (xdrs->x_op == XDR_FREE)
117                 return (TRUE);
118
119 #ifdef DEBUG
120         printf("xdr_int: FAILED\n");
121 #endif
122         return (FALSE);
123 }
124
125 /*
126  * XDR unsigned integers
127  *
128  * PSARC 2003/523 Contract Private Interface
129  * xdr_u_int
130  * Changes must be reviewed by Solaris File Sharing
131  * Changes must be communicated to contract-2003-523@sun.com
132  */
133 bool_t
134 xdr_u_int(XDR *xdrs, uint_t *up)
135 {
136         if (xdrs->x_op == XDR_ENCODE)
137                 return (XDR_PUTINT32(xdrs, (int32_t *)up));
138
139         if (xdrs->x_op == XDR_DECODE)
140                 return (XDR_GETINT32(xdrs, (int32_t *)up));
141
142         if (xdrs->x_op == XDR_FREE)
143                 return (TRUE);
144
145 #ifdef DEBUG
146         printf("xdr_int: FAILED\n");
147 #endif
148         return (FALSE);
149 }
150
151
152 #if defined(_ILP32)
153 /*
154  * xdr_long and xdr_u_long for binary compatability on ILP32 kernels.
155  *
156  * No prototypes since new code should not be using these interfaces.
157  */
158 bool_t
159 xdr_long(XDR *xdrs, long *ip)
160 {
161         return (xdr_int(xdrs, (int *)ip));
162 }
163
164 bool_t
165 xdr_u_long(XDR *xdrs, unsigned long *up)
166 {
167         return (xdr_u_int(xdrs, (uint_t *)up));
168 }
169 #endif /* _ILP32 */
170
171
172 /*
173  * XDR long long integers
174  */
175 bool_t
176 xdr_longlong_t(XDR *xdrs, longlong_t *hp)
177 {
178         if (xdrs->x_op == XDR_ENCODE) {
179 #if BYTE_ORDER == _LITTLE_ENDIAN
180                 if (XDR_PUTINT32(xdrs, (int32_t *)((char *)hp +
181                     BYTES_PER_XDR_UNIT)) == TRUE) {
182                         return (XDR_PUTINT32(xdrs, (int32_t *)hp));
183                 }
184 #else
185                 if (XDR_PUTINT32(xdrs, (int32_t *)hp) == TRUE) {
186                         return (XDR_PUTINT32(xdrs, (int32_t *)((char *)hp +
187                             BYTES_PER_XDR_UNIT)));
188                 }
189 #endif
190                 return (FALSE);
191
192         }
193         if (xdrs->x_op == XDR_DECODE) {
194 #if BYTE_ORDER == _LITTLE_ENDIAN
195                 if (XDR_GETINT32(xdrs, (int32_t *)((char *)hp +
196                     BYTES_PER_XDR_UNIT)) == TRUE) {
197                         return (XDR_GETINT32(xdrs, (int32_t *)hp));
198                 }
199 #else
200                 if (XDR_GETINT32(xdrs, (int32_t *)hp) == TRUE) {
201                         return (XDR_GETINT32(xdrs, (int32_t *)((char *)hp +
202                             BYTES_PER_XDR_UNIT)));
203                 }
204 #endif
205                 return (FALSE);
206         }
207         return (TRUE);
208 }
209
210 /*
211  * XDR unsigned long long integers
212  */
213 bool_t
214 xdr_u_longlong_t(XDR *xdrs, u_longlong_t *hp)
215 {
216
217         if (xdrs->x_op == XDR_ENCODE) {
218 #if BYTE_ORDER == _LITTLE_ENDIAN
219                 if (XDR_PUTINT32(xdrs, (int32_t *)((char *)hp +
220                     BYTES_PER_XDR_UNIT)) == TRUE) {
221                         return (XDR_PUTINT32(xdrs, (int32_t *)hp));
222                 }
223 #else
224                 if (XDR_PUTINT32(xdrs, (int32_t *)hp) == TRUE) {
225                         return (XDR_PUTINT32(xdrs, (int32_t *)((char *)hp +
226                             BYTES_PER_XDR_UNIT)));
227                 }
228 #endif
229                 return (FALSE);
230
231         }
232         if (xdrs->x_op == XDR_DECODE) {
233 #if BYTE_ORDER == _LITTLE_ENDIAN
234                 if (XDR_GETINT32(xdrs, (int32_t *)((char *)hp +
235                     BYTES_PER_XDR_UNIT)) == TRUE) {
236                         return (XDR_GETINT32(xdrs, (int32_t *)hp));
237                 }
238 #else
239                 if (XDR_GETINT32(xdrs, (int32_t *)hp) == TRUE) {
240                         return (XDR_GETINT32(xdrs, (int32_t *)((char *)hp +
241                             BYTES_PER_XDR_UNIT)));
242                 }
243 #endif
244                 return (FALSE);
245         }
246         return (TRUE);
247 }
248
249 /*
250  * XDR short integers
251  */
252 bool_t
253 xdr_short(XDR *xdrs, short *sp)
254 {
255         int32_t l;
256
257         switch (xdrs->x_op) {
258
259         case XDR_ENCODE:
260                 l = (int32_t)*sp;
261                 return (XDR_PUTINT32(xdrs, &l));
262
263         case XDR_DECODE:
264                 if (!XDR_GETINT32(xdrs, &l))
265                         return (FALSE);
266                 *sp = (short)l;
267                 return (TRUE);
268
269         case XDR_FREE:
270                 return (TRUE);
271         }
272         return (FALSE);
273 }
274
275 /*
276  * XDR unsigned short integers
277  */
278 bool_t
279 xdr_u_short(XDR *xdrs, ushort_t *usp)
280 {
281         uint32_t l;
282
283         switch (xdrs->x_op) {
284
285         case XDR_ENCODE:
286                 l = (uint32_t)*usp;
287                 return (XDR_PUTINT32(xdrs, (int32_t *)&l));
288
289         case XDR_DECODE:
290                 if (!XDR_GETINT32(xdrs, (int32_t *)&l)) {
291 #ifdef DEBUG
292                         printf("xdr_u_short: decode FAILED\n");
293 #endif
294                         return (FALSE);
295                 }
296                 *usp = (ushort_t)l;
297                 return (TRUE);
298
299         case XDR_FREE:
300                 return (TRUE);
301         }
302 #ifdef DEBUG
303         printf("xdr_u_short: bad op FAILED\n");
304 #endif
305         return (FALSE);
306 }
307
308
309 /*
310  * XDR a char
311  */
312 bool_t
313 xdr_char(XDR *xdrs, char *cp)
314 {
315         int i;
316
317         i = (*cp);
318         if (!xdr_int(xdrs, &i)) {
319                 return (FALSE);
320         }
321         *cp = (char)i;
322         return (TRUE);
323 }
324
325 /*
326  * XDR booleans
327  *
328  * PSARC 2003/523 Contract Private Interface
329  * xdr_bool
330  * Changes must be reviewed by Solaris File Sharing
331  * Changes must be communicated to contract-2003-523@sun.com
332  */
333 bool_t
334 xdr_bool(XDR *xdrs, bool_t *bp)
335 {
336         int32_t i32b;
337
338         switch (xdrs->x_op) {
339
340         case XDR_ENCODE:
341                 i32b = *bp ? XDR_TRUE : XDR_FALSE;
342                 return (XDR_PUTINT32(xdrs, &i32b));
343
344         case XDR_DECODE:
345                 if (!XDR_GETINT32(xdrs, &i32b)) {
346 #ifdef DEBUG
347                         printf("xdr_bool: decode FAILED\n");
348 #endif
349                         return (FALSE);
350                 }
351                 *bp = (i32b == XDR_FALSE) ? FALSE : TRUE;
352                 return (TRUE);
353
354         case XDR_FREE:
355                 return (TRUE);
356         }
357 #ifdef DEBUG
358         printf("xdr_bool: bad op FAILED\n");
359 #endif
360         return (FALSE);
361 }
362
363 /*
364  * XDR enumerations
365  *
366  * PSARC 2003/523 Contract Private Interface
367  * xdr_enum
368  * Changes must be reviewed by Solaris File Sharing
369  * Changes must be communicated to contract-2003-523@sun.com
370  */
371 #ifndef lint
372 enum sizecheck { SIZEVAL } sizecheckvar;        /* used to find the size of */
373                                                 /* an enum */
374 #endif
375 bool_t
376 xdr_enum(XDR *xdrs, enum_t *ep)
377 {
378 #ifndef lint
379         /*
380          * enums are treated as ints
381          */
382         if (sizeof (sizecheckvar) == sizeof (int32_t)) {
383                 return (xdr_int(xdrs, (int32_t *)ep));
384         } else if (sizeof (sizecheckvar) == sizeof (short)) {
385                 return (xdr_short(xdrs, (short *)ep));
386         } else {
387                 return (FALSE);
388         }
389 #else
390         (void) (xdr_short(xdrs, (short *)ep));
391         return (xdr_int(xdrs, (int32_t *)ep));
392 #endif
393 }
394
395 /*
396  * XDR opaque data
397  * Allows the specification of a fixed size sequence of opaque bytes.
398  * cp points to the opaque object and cnt gives the byte length.
399  *
400  * PSARC 2003/523 Contract Private Interface
401  * xdr_opaque
402  * Changes must be reviewed by Solaris File Sharing
403  * Changes must be communicated to contract-2003-523@sun.com
404  */
405 bool_t
406 xdr_opaque(XDR *xdrs, caddr_t cp, const uint_t cnt)
407 {
408         uint_t rndup;
409         static char crud[BYTES_PER_XDR_UNIT];
410
411         /*
412          * if no data we are done
413          */
414         if (cnt == 0)
415                 return (TRUE);
416
417         /*
418          * round byte count to full xdr units
419          */
420         rndup = cnt % BYTES_PER_XDR_UNIT;
421         if (rndup != 0)
422                 rndup = BYTES_PER_XDR_UNIT - rndup;
423
424         if (xdrs->x_op == XDR_DECODE) {
425                 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
426 #ifdef DEBUG
427                         printf("xdr_opaque: decode FAILED\n");
428 #endif
429                         return (FALSE);
430                 }
431                 if (rndup == 0)
432                         return (TRUE);
433                 return (XDR_GETBYTES(xdrs, (caddr_t)crud, rndup));
434         }
435
436         if (xdrs->x_op == XDR_ENCODE) {
437                 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
438 #ifdef DEBUG
439                         printf("xdr_opaque: encode FAILED\n");
440 #endif
441                         return (FALSE);
442                 }
443                 if (rndup == 0)
444                         return (TRUE);
445                 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
446         }
447
448         if (xdrs->x_op == XDR_FREE)
449                 return (TRUE);
450
451 #ifdef DEBUG
452         printf("xdr_opaque: bad op FAILED\n");
453 #endif
454         return (FALSE);
455 }
456
457 /*
458  * XDR counted bytes
459  * *cpp is a pointer to the bytes, *sizep is the count.
460  * If *cpp is NULL maxsize bytes are allocated
461  *
462  * PSARC 2003/523 Contract Private Interface
463  * xdr_bytes
464  * Changes must be reviewed by Solaris File Sharing
465  * Changes must be communicated to contract-2003-523@sun.com
466  */
467 bool_t
468 xdr_bytes(XDR *xdrs, char **cpp, uint_t *sizep, const uint_t maxsize)
469 {
470         char *sp = *cpp;  /* sp is the actual string pointer */
471         uint_t nodesize;
472
473         /*
474          * first deal with the length since xdr bytes are counted
475          */
476         if (!xdr_u_int(xdrs, sizep)) {
477 #ifdef DEBUG
478                 printf("xdr_bytes: size FAILED\n");
479 #endif
480                 return (FALSE);
481         }
482         nodesize = *sizep;
483         if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
484 #ifdef DEBUG
485                 printf("xdr_bytes: bad size (%d) FAILED (%d max)\n",
486                     nodesize, maxsize);
487 #endif
488                 return (FALSE);
489         }
490
491         /*
492          * now deal with the actual bytes
493          */
494         switch (xdrs->x_op) {
495         case XDR_DECODE:
496                 if (nodesize == 0)
497                         return (TRUE);
498                 if (sp == NULL)
499                         *cpp = sp = (char *)mem_alloc(nodesize);
500                 /* FALLTHROUGH */
501
502         case XDR_ENCODE:
503                 return (xdr_opaque(xdrs, sp, nodesize));
504
505         case XDR_FREE:
506                 if (sp != NULL) {
507                         mem_free(sp, nodesize);
508                         *cpp = NULL;
509                 }
510                 return (TRUE);
511         }
512 #ifdef DEBUG
513         printf("xdr_bytes: bad op FAILED\n");
514 #endif
515         return (FALSE);
516 }
517
518 /*
519  * Implemented here due to commonality of the object.
520  */
521 bool_t
522 xdr_netobj(XDR *xdrs, struct netobj *np)
523 {
524         return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
525 }
526
527 /*
528  * XDR a descriminated union
529  * Support routine for discriminated unions.
530  * You create an array of xdrdiscrim structures, terminated with
531  * an entry with a null procedure pointer.  The routine gets
532  * the discriminant value and then searches the array of xdrdiscrims
533  * looking for that value.  It calls the procedure given in the xdrdiscrim
534  * to handle the discriminant.  If there is no specific routine a default
535  * routine may be called.
536  * If there is no specific or default routine an error is returned.
537  */
538 bool_t
539 xdr_union(XDR *xdrs, enum_t *dscmp, char *unp,
540         const struct xdr_discrim *choices, const xdrproc_t dfault)
541 {
542         enum_t dscm;
543
544         /*
545          * we deal with the discriminator;  it's an enum
546          */
547         if (!xdr_enum(xdrs, dscmp)) {
548 #ifdef DEBUG
549                 printf("xdr_enum: dscmp FAILED\n");
550 #endif
551                 return (FALSE);
552         }
553         dscm = *dscmp;
554
555         /*
556          * search choices for a value that matches the discriminator.
557          * if we find one, execute the xdr routine for that value.
558          */
559         for (; choices->proc != NULL_xdrproc_t; choices++) {
560                 if (choices->value == dscm)
561                         return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
562         }
563
564         /*
565          * no match - execute the default xdr routine if there is one
566          */
567         return ((dfault == NULL_xdrproc_t) ? FALSE :
568             (*dfault)(xdrs, unp, LASTUNSIGNED));
569 }
570
571
572 /*
573  * Non-portable xdr primitives.
574  * Care should be taken when moving these routines to new architectures.
575  */
576
577
578 /*
579  * XDR null terminated ASCII strings
580  * xdr_string deals with "C strings" - arrays of bytes that are
581  * terminated by a NULL character.  The parameter cpp references a
582  * pointer to storage; If the pointer is null, then the necessary
583  * storage is allocated.  The last parameter is the max allowed length
584  * of the string as specified by a protocol.
585  */
586 bool_t
587 xdr_string(XDR *xdrs, char **cpp, const uint_t maxsize)
588 {
589         char *sp = *cpp;  /* sp is the actual string pointer */
590         uint_t size;
591         uint_t nodesize;
592
593         /*
594          * first deal with the length since xdr strings are counted-strings
595          */
596         switch (xdrs->x_op) {
597         case XDR_FREE:
598                 if (sp == NULL)
599                         return (TRUE);  /* already free */
600                 /* FALLTHROUGH */
601         case XDR_ENCODE:
602                 size = (sp != NULL) ? (uint_t)strlen(sp) : 0;
603                 break;
604         case XDR_DECODE:
605                 break;
606         }
607         if (!xdr_u_int(xdrs, &size)) {
608 #ifdef DEBUG
609                 printf("xdr_string: size FAILED\n");
610 #endif
611                 return (FALSE);
612         }
613         if (size > maxsize) {
614 #ifdef DEBUG
615                 printf("xdr_string: bad size FAILED\n");
616 #endif
617                 return (FALSE);
618         }
619         nodesize = size + 1;
620
621         /*
622          * now deal with the actual bytes
623          */
624         switch (xdrs->x_op) {
625         case XDR_DECODE:
626                 if (nodesize == 0)
627                         return (TRUE);
628                 if (sp == NULL)
629                         sp = (char *)mem_alloc(nodesize);
630                 sp[size] = 0;
631                 if (!xdr_opaque(xdrs, sp, size)) {
632                         /*
633                          * free up memory if allocated here
634                          */
635                         if (*cpp == NULL) {
636                                 mem_free(sp, nodesize);
637                         }
638                         return (FALSE);
639                 }
640                 if (strlen(sp) != size) {
641                         if (*cpp == NULL) {
642                                 mem_free(sp, nodesize);
643                         }
644                         return (FALSE);
645                 }
646                 *cpp = sp;
647                 return (TRUE);
648
649         case XDR_ENCODE:
650                 return (xdr_opaque(xdrs, sp, size));
651
652         case XDR_FREE:
653                 mem_free(sp, nodesize);
654                 *cpp = NULL;
655                 return (TRUE);
656         }
657 #ifdef DEBUG
658         printf("xdr_string: bad op FAILED\n");
659 #endif
660         return (FALSE);
661 }
662
663 /*
664  * Wrapper for xdr_string that can be called directly from
665  * routines like clnt_call
666  */
667 bool_t
668 xdr_wrapstring(XDR *xdrs, char **cpp)
669 {
670         if (xdr_string(xdrs, cpp, LASTUNSIGNED))
671                 return (TRUE);
672         return (FALSE);
673 }