remove unnecessary uint_t casts of 0
[unleashed.git] / usr / src / uts / common / rpc / xdr.c
blob4d355f5530b92f832ab54b0fc53afd8344bea927
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * Portions of this source code were derived from Berkeley 4.3 BSD
31 * under license from the Regents of the University of California.
35 * xdr.c, generic XDR routines implementation.
36 * These are the "generic" xdr routines used to serialize and de-serialize
37 * most common data items. See xdr.h for more info on the interface to
38 * xdr.
41 #include <sys/param.h>
42 #include <sys/cmn_err.h>
43 #include <sys/types.h>
44 #include <sys/systm.h>
46 #include <rpc/types.h>
47 #include <rpc/xdr.h>
48 #include <sys/isa_defs.h>
50 #pragma weak xdr_int32_t = xdr_int
51 #pragma weak xdr_uint32_t = xdr_u_int
52 #pragma weak xdr_int64_t = xdr_longlong_t
53 #pragma weak xdr_uint64_t = xdr_u_longlong_t
55 #if !defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
56 #error "Exactly one of _BIG_ENDIAN or _LITTLE_ENDIAN must be defined"
57 #elif defined(_BIG_ENDIAN) && defined(_LITTLE_ENDIAN)
58 #error "Only one of _BIG_ENDIAN or _LITTLE_ENDIAN may be defined"
59 #endif
62 * constants specific to the xdr "protocol"
64 #define XDR_FALSE ((int32_t)0)
65 #define XDR_TRUE ((int32_t)1)
66 #define LASTUNSIGNED (~0u)
69 * for unit alignment
71 static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
74 * Free a data structure using XDR
75 * Not a filter, but a convenient utility nonetheless
77 void
78 xdr_free(xdrproc_t proc, char *objp)
80 XDR x;
82 x.x_op = XDR_FREE;
83 (*proc)(&x, objp);
87 * XDR nothing
89 bool_t
90 xdr_void(void)
92 return (TRUE);
96 * XDR integers
98 * PSARC 2003/523 Contract Private Interface
99 * xdr_int
100 * Changes must be reviewed by Solaris File Sharing
101 * Changes must be communicated to contract-2003-523@sun.com
103 bool_t
104 xdr_int(XDR *xdrs, int *ip)
106 if (xdrs->x_op == XDR_ENCODE)
107 return (XDR_PUTINT32(xdrs, ip));
109 if (xdrs->x_op == XDR_DECODE)
110 return (XDR_GETINT32(xdrs, ip));
112 if (xdrs->x_op == XDR_FREE)
113 return (TRUE);
115 return (FALSE);
119 * XDR unsigned integers
121 * PSARC 2003/523 Contract Private Interface
122 * xdr_u_int
123 * Changes must be reviewed by Solaris File Sharing
124 * Changes must be communicated to contract-2003-523@sun.com
126 bool_t
127 xdr_u_int(XDR *xdrs, uint_t *up)
129 if (xdrs->x_op == XDR_ENCODE)
130 return (XDR_PUTINT32(xdrs, (int32_t *)up));
132 if (xdrs->x_op == XDR_DECODE)
133 return (XDR_GETINT32(xdrs, (int32_t *)up));
135 if (xdrs->x_op == XDR_FREE)
136 return (TRUE);
138 return (FALSE);
142 #if defined(_ILP32)
144 * xdr_long and xdr_u_long for binary compatability on ILP32 kernels.
146 * No prototypes since new code should not be using these interfaces.
148 bool_t
149 xdr_long(XDR *xdrs, long *ip)
151 return (xdr_int(xdrs, (int *)ip));
154 bool_t
155 xdr_u_long(XDR *xdrs, unsigned long *up)
157 return (xdr_u_int(xdrs, (uint_t *)up));
159 #endif /* _ILP32 */
163 * XDR long long integers
165 bool_t
166 xdr_longlong_t(XDR *xdrs, longlong_t *hp)
168 if (xdrs->x_op == XDR_ENCODE) {
169 #if defined(_LITTLE_ENDIAN)
170 if (XDR_PUTINT32(xdrs, (int32_t *)((char *)hp +
171 BYTES_PER_XDR_UNIT)) == TRUE) {
172 return (XDR_PUTINT32(xdrs, (int32_t *)hp));
174 #elif defined(_BIG_ENDIAN)
175 if (XDR_PUTINT32(xdrs, (int32_t *)hp) == TRUE) {
176 return (XDR_PUTINT32(xdrs, (int32_t *)((char *)hp +
177 BYTES_PER_XDR_UNIT)));
179 #endif
180 return (FALSE);
183 if (xdrs->x_op == XDR_DECODE) {
184 #if defined(_LITTLE_ENDIAN)
185 if (XDR_GETINT32(xdrs, (int32_t *)((char *)hp +
186 BYTES_PER_XDR_UNIT)) == TRUE) {
187 return (XDR_GETINT32(xdrs, (int32_t *)hp));
189 #elif defined(_BIG_ENDIAN)
190 if (XDR_GETINT32(xdrs, (int32_t *)hp) == TRUE) {
191 return (XDR_GETINT32(xdrs, (int32_t *)((char *)hp +
192 BYTES_PER_XDR_UNIT)));
194 #endif
195 return (FALSE);
197 return (TRUE);
201 * XDR unsigned long long integers
203 bool_t
204 xdr_u_longlong_t(XDR *xdrs, u_longlong_t *hp)
207 if (xdrs->x_op == XDR_ENCODE) {
208 #if defined(_LITTLE_ENDIAN)
209 if (XDR_PUTINT32(xdrs, (int32_t *)((char *)hp +
210 BYTES_PER_XDR_UNIT)) == TRUE) {
211 return (XDR_PUTINT32(xdrs, (int32_t *)hp));
213 #elif defined(_BIG_ENDIAN)
214 if (XDR_PUTINT32(xdrs, (int32_t *)hp) == TRUE) {
215 return (XDR_PUTINT32(xdrs, (int32_t *)((char *)hp +
216 BYTES_PER_XDR_UNIT)));
218 #endif
219 return (FALSE);
222 if (xdrs->x_op == XDR_DECODE) {
223 #if defined(_LITTLE_ENDIAN)
224 if (XDR_GETINT32(xdrs, (int32_t *)((char *)hp +
225 BYTES_PER_XDR_UNIT)) == TRUE) {
226 return (XDR_GETINT32(xdrs, (int32_t *)hp));
228 #elif defined(_BIG_ENDIAN)
229 if (XDR_GETINT32(xdrs, (int32_t *)hp) == TRUE) {
230 return (XDR_GETINT32(xdrs, (int32_t *)((char *)hp +
231 BYTES_PER_XDR_UNIT)));
233 #endif
234 return (FALSE);
236 return (TRUE);
240 * XDR short integers
242 bool_t
243 xdr_short(XDR *xdrs, short *sp)
245 int32_t l;
247 switch (xdrs->x_op) {
249 case XDR_ENCODE:
250 l = (int32_t)*sp;
251 return (XDR_PUTINT32(xdrs, &l));
253 case XDR_DECODE:
254 if (!XDR_GETINT32(xdrs, &l))
255 return (FALSE);
256 *sp = (short)l;
257 return (TRUE);
259 case XDR_FREE:
260 return (TRUE);
262 return (FALSE);
266 * XDR unsigned short integers
268 bool_t
269 xdr_u_short(XDR *xdrs, ushort_t *usp)
271 uint32_t l;
273 switch (xdrs->x_op) {
275 case XDR_ENCODE:
276 l = (uint32_t)*usp;
277 return (XDR_PUTINT32(xdrs, (int32_t *)&l));
279 case XDR_DECODE:
280 if (!XDR_GETINT32(xdrs, (int32_t *)&l)) {
281 return (FALSE);
283 *usp = (ushort_t)l;
284 return (TRUE);
286 case XDR_FREE:
287 return (TRUE);
289 return (FALSE);
294 * XDR a char
296 bool_t
297 xdr_char(XDR *xdrs, char *cp)
299 int i;
301 i = (*cp);
302 if (!xdr_int(xdrs, &i)) {
303 return (FALSE);
305 *cp = (char)i;
306 return (TRUE);
310 * XDR an unsigned char
312 bool_t
313 xdr_u_char(XDR *xdrs, uchar_t *cp)
315 int i;
317 switch (xdrs->x_op) {
318 case XDR_ENCODE:
319 i = (*cp);
320 return (XDR_PUTINT32(xdrs, &i));
321 case XDR_DECODE:
322 if (!XDR_GETINT32(xdrs, &i))
323 return (FALSE);
324 *cp = (uchar_t)i;
325 return (TRUE);
326 case XDR_FREE:
327 return (TRUE);
329 return (FALSE);
333 * XDR booleans
335 * PSARC 2003/523 Contract Private Interface
336 * xdr_bool
337 * Changes must be reviewed by Solaris File Sharing
338 * Changes must be communicated to contract-2003-523@sun.com
340 bool_t
341 xdr_bool(XDR *xdrs, bool_t *bp)
343 int32_t i32b;
345 switch (xdrs->x_op) {
347 case XDR_ENCODE:
348 i32b = *bp ? XDR_TRUE : XDR_FALSE;
349 return (XDR_PUTINT32(xdrs, &i32b));
351 case XDR_DECODE:
352 if (!XDR_GETINT32(xdrs, &i32b)) {
353 return (FALSE);
355 *bp = (i32b == XDR_FALSE) ? FALSE : TRUE;
356 return (TRUE);
358 case XDR_FREE:
359 return (TRUE);
361 return (FALSE);
365 * XDR enumerations
367 * PSARC 2003/523 Contract Private Interface
368 * xdr_enum
369 * Changes must be reviewed by Solaris File Sharing
370 * Changes must be communicated to contract-2003-523@sun.com
372 enum sizecheck { SIZEVAL } sizecheckvar; /* used to find the size of */
373 /* an enum */
374 bool_t
375 xdr_enum(XDR *xdrs, enum_t *ep)
378 * enums are treated as ints
380 if (sizeof (sizecheckvar) == sizeof (int32_t)) {
381 return (xdr_int(xdrs, (int32_t *)ep));
382 } else if (sizeof (sizecheckvar) == sizeof (short)) {
383 return (xdr_short(xdrs, (short *)ep));
384 } else {
385 return (FALSE);
390 * XDR opaque data
391 * Allows the specification of a fixed size sequence of opaque bytes.
392 * cp points to the opaque object and cnt gives the byte length.
394 * PSARC 2003/523 Contract Private Interface
395 * xdr_opaque
396 * Changes must be reviewed by Solaris File Sharing
397 * Changes must be communicated to contract-2003-523@sun.com
399 bool_t
400 xdr_opaque(XDR *xdrs, caddr_t cp, const uint_t cnt)
402 uint_t rndup;
403 static char crud[BYTES_PER_XDR_UNIT];
406 * if no data we are done
408 if (cnt == 0)
409 return (TRUE);
412 * round byte count to full xdr units
414 rndup = cnt % BYTES_PER_XDR_UNIT;
415 if (rndup != 0)
416 rndup = BYTES_PER_XDR_UNIT - rndup;
418 if (xdrs->x_op == XDR_DECODE) {
419 if (!XDR_GETBYTES(xdrs, cp, cnt)) {
420 return (FALSE);
422 if (rndup == 0)
423 return (TRUE);
424 return (XDR_GETBYTES(xdrs, (caddr_t)crud, rndup));
427 if (xdrs->x_op == XDR_ENCODE) {
428 if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
429 return (FALSE);
431 if (rndup == 0)
432 return (TRUE);
433 return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
436 if (xdrs->x_op == XDR_FREE)
437 return (TRUE);
439 return (FALSE);
443 * XDR counted bytes
444 * *cpp is a pointer to the bytes, *sizep is the count.
445 * If *cpp is NULL maxsize bytes are allocated
447 * PSARC 2003/523 Contract Private Interface
448 * xdr_bytes
449 * Changes must be reviewed by Solaris File Sharing
450 * Changes must be communicated to contract-2003-523@sun.com
452 bool_t
453 xdr_bytes(XDR *xdrs, char **cpp, uint_t *sizep, const uint_t maxsize)
455 char *sp = *cpp; /* sp is the actual string pointer */
456 uint_t nodesize;
459 * first deal with the length since xdr bytes are counted
461 if (!xdr_u_int(xdrs, sizep)) {
462 return (FALSE);
464 nodesize = *sizep;
465 if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
466 return (FALSE);
470 * now deal with the actual bytes
472 switch (xdrs->x_op) {
473 case XDR_DECODE:
474 if (nodesize == 0)
475 return (TRUE);
476 if (sp == NULL)
477 *cpp = sp = (char *)mem_alloc(nodesize);
478 /* FALLTHROUGH */
480 case XDR_ENCODE:
481 return (xdr_opaque(xdrs, sp, nodesize));
483 case XDR_FREE:
484 if (sp != NULL) {
485 mem_free(sp, nodesize);
486 *cpp = NULL;
488 return (TRUE);
490 return (FALSE);
494 * Implemented here due to commonality of the object.
496 bool_t
497 xdr_netobj(XDR *xdrs, struct netobj *np)
499 return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ));
503 * XDR a descriminated union
504 * Support routine for discriminated unions.
505 * You create an array of xdrdiscrim structures, terminated with
506 * an entry with a null procedure pointer. The routine gets
507 * the discriminant value and then searches the array of xdrdiscrims
508 * looking for that value. It calls the procedure given in the xdrdiscrim
509 * to handle the discriminant. If there is no specific routine a default
510 * routine may be called.
511 * If there is no specific or default routine an error is returned.
513 bool_t
514 xdr_union(XDR *xdrs, enum_t *dscmp, char *unp,
515 const struct xdr_discrim *choices, const xdrproc_t dfault)
517 enum_t dscm;
520 * we deal with the discriminator; it's an enum
522 if (!xdr_enum(xdrs, dscmp)) {
523 return (FALSE);
525 dscm = *dscmp;
528 * search choices for a value that matches the discriminator.
529 * if we find one, execute the xdr routine for that value.
531 for (; choices->proc != NULL_xdrproc_t; choices++) {
532 if (choices->value == dscm)
533 return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
537 * no match - execute the default xdr routine if there is one
539 return ((dfault == NULL_xdrproc_t) ? FALSE :
540 (*dfault)(xdrs, unp, LASTUNSIGNED));
545 * Non-portable xdr primitives.
546 * Care should be taken when moving these routines to new architectures.
551 * XDR null terminated ASCII strings
552 * xdr_string deals with "C strings" - arrays of bytes that are
553 * terminated by a NULL character. The parameter cpp references a
554 * pointer to storage; If the pointer is null, then the necessary
555 * storage is allocated. The last parameter is the max allowed length
556 * of the string as specified by a protocol.
558 bool_t
559 xdr_string(XDR *xdrs, char **cpp, const uint_t maxsize)
561 char *sp = *cpp; /* sp is the actual string pointer */
562 uint_t size;
563 uint_t nodesize;
566 * first deal with the length since xdr strings are counted-strings
568 switch (xdrs->x_op) {
569 case XDR_FREE:
570 if (sp == NULL)
571 return (TRUE); /* already free */
572 /* FALLTHROUGH */
573 case XDR_ENCODE:
574 size = (sp != NULL) ? (uint_t)strlen(sp) : 0;
575 break;
576 case XDR_DECODE:
577 break;
579 if (!xdr_u_int(xdrs, &size)) {
580 return (FALSE);
582 if (size > maxsize) {
583 return (FALSE);
585 nodesize = size + 1;
588 * now deal with the actual bytes
590 switch (xdrs->x_op) {
591 case XDR_DECODE:
592 if (nodesize == 0)
593 return (TRUE);
594 if (sp == NULL)
595 sp = (char *)mem_alloc(nodesize);
596 sp[size] = 0;
597 if (!xdr_opaque(xdrs, sp, size)) {
599 * free up memory if allocated here
601 if (*cpp == NULL) {
602 mem_free(sp, nodesize);
604 return (FALSE);
606 if (strlen(sp) != size) {
607 if (*cpp == NULL) {
608 mem_free(sp, nodesize);
610 return (FALSE);
612 *cpp = sp;
613 return (TRUE);
615 case XDR_ENCODE:
616 return (xdr_opaque(xdrs, sp, size));
618 case XDR_FREE:
619 mem_free(sp, nodesize);
620 *cpp = NULL;
621 return (TRUE);
623 return (FALSE);
627 * xdr_vector():
629 * XDR a fixed length array. Unlike variable-length arrays, the storage
630 * of fixed length arrays is static and unfreeable.
631 * > basep: base of the array
632 * > size: size of the array
633 * > elemsize: size of each element
634 * > xdr_elem: routine to XDR each element
636 bool_t
637 xdr_vector(XDR *xdrs, char *basep, const uint_t nelem,
638 const uint_t elemsize, const xdrproc_t xdr_elem)
640 uint_t i;
641 char *elptr;
643 elptr = basep;
644 for (i = 0; i < nelem; i++) {
645 if (!(*xdr_elem)(xdrs, elptr, LASTUNSIGNED))
646 return (FALSE);
647 elptr += elemsize;
649 return (TRUE);
653 * Wrapper for xdr_string that can be called directly from
654 * routines like clnt_call
656 bool_t
657 xdr_wrapstring(XDR *xdrs, char **cpp)
659 if (xdr_string(xdrs, cpp, LASTUNSIGNED))
660 return (TRUE);
661 return (FALSE);