xref: /illumos-gate/usr/src/uts/common/rpc/xdr_sizeof.c (revision 4c273cfa)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
26*4c273cfaSVitaliy Gusev /*
27*4c273cfaSVitaliy Gusev  * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
28*4c273cfaSVitaliy Gusev  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <rpc/types.h>
317c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /* ARGSUSED */
357c478bd9Sstevel@tonic-gate static bool_t
367c478bd9Sstevel@tonic-gate x_putint32_t(XDR *xdrs, int32_t *ip)
377c478bd9Sstevel@tonic-gate {
387c478bd9Sstevel@tonic-gate 	xdrs->x_handy += BYTES_PER_XDR_UNIT;
397c478bd9Sstevel@tonic-gate 	return (TRUE);
407c478bd9Sstevel@tonic-gate }
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /* ARGSUSED */
437c478bd9Sstevel@tonic-gate static bool_t
447c478bd9Sstevel@tonic-gate x_putbytes(XDR *xdrs, char *bp, int len)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	xdrs->x_handy += len;
477c478bd9Sstevel@tonic-gate 	return (TRUE);
487c478bd9Sstevel@tonic-gate }
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static uint_t
517c478bd9Sstevel@tonic-gate x_getpostn(XDR *xdrs)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	return (xdrs->x_handy);
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* ARGSUSED */
577c478bd9Sstevel@tonic-gate static bool_t
587c478bd9Sstevel@tonic-gate x_setpostn(XDR *xdrs, uint_t pos)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	/* This is not allowed */
617c478bd9Sstevel@tonic-gate 	return (FALSE);
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static rpc_inline_t *
657c478bd9Sstevel@tonic-gate x_inline(XDR *xdrs, int len)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	if (len == 0) {
687c478bd9Sstevel@tonic-gate 		return (NULL);
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate 	if (xdrs->x_op != XDR_ENCODE) {
717c478bd9Sstevel@tonic-gate 		return (NULL);
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 	if (len < (uintptr_t)xdrs->x_base) {
747c478bd9Sstevel@tonic-gate 		/* x_private was already allocated */
757c478bd9Sstevel@tonic-gate 		xdrs->x_handy += len;
767c478bd9Sstevel@tonic-gate 		return ((rpc_inline_t *)xdrs->x_private);
777c478bd9Sstevel@tonic-gate 	} else {
787c478bd9Sstevel@tonic-gate 		/* Free the earlier space and allocate new area */
797c478bd9Sstevel@tonic-gate 		if (xdrs->x_private)
807c478bd9Sstevel@tonic-gate 			mem_free(xdrs->x_private, (uintptr_t)xdrs->x_base);
817c478bd9Sstevel@tonic-gate 		if ((xdrs->x_private = (caddr_t)mem_alloc(len)) == NULL) {
827c478bd9Sstevel@tonic-gate 			xdrs->x_base = 0;
837c478bd9Sstevel@tonic-gate 			return (NULL);
847c478bd9Sstevel@tonic-gate 		}
857c478bd9Sstevel@tonic-gate 		xdrs->x_base = (caddr_t)(uintptr_t)len;
867c478bd9Sstevel@tonic-gate 		xdrs->x_handy += len;
877c478bd9Sstevel@tonic-gate 		return ((rpc_inline_t *)xdrs->x_private);
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static int
927c478bd9Sstevel@tonic-gate harmless()
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	/* Always return FALSE/NULL, as the case may be */
957c478bd9Sstevel@tonic-gate 	return (0);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static void
997c478bd9Sstevel@tonic-gate x_destroy(XDR *xdrs)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	xdrs->x_handy = 0;
1027c478bd9Sstevel@tonic-gate 	if (xdrs->x_private) {
1037c478bd9Sstevel@tonic-gate 		mem_free(xdrs->x_private, (uintptr_t)xdrs->x_base);
1047c478bd9Sstevel@tonic-gate 		xdrs->x_private = NULL;
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 	xdrs->x_base = 0;
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate unsigned int
1107c478bd9Sstevel@tonic-gate xdr_sizeof(xdrproc_t func, void *data)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	XDR x;
1137c478bd9Sstevel@tonic-gate 	struct xdr_ops ops;
1147c478bd9Sstevel@tonic-gate 	bool_t stat;
1157c478bd9Sstevel@tonic-gate 	/* to stop ANSI-C compiler from complaining */
1167c478bd9Sstevel@tonic-gate 	typedef  bool_t (* dummyfunc1)(XDR *, long *);
1177c478bd9Sstevel@tonic-gate 	typedef  bool_t (* dummyfunc2)(XDR *, caddr_t, int);
1187c478bd9Sstevel@tonic-gate 	typedef  bool_t (* dummyfunc3)(XDR *, int32_t *);
1197c478bd9Sstevel@tonic-gate 	typedef	 bool_t (* dummyfunc4)(XDR *, int, void *);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	ops.x_putbytes = x_putbytes;
1227c478bd9Sstevel@tonic-gate 	ops.x_inline = x_inline;
1237c478bd9Sstevel@tonic-gate 	ops.x_getpostn = x_getpostn;
1247c478bd9Sstevel@tonic-gate 	ops.x_setpostn = x_setpostn;
1257c478bd9Sstevel@tonic-gate 	ops.x_destroy = x_destroy;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate #if defined(_LP64) || defined(_KERNEL)
1287c478bd9Sstevel@tonic-gate 	ops.x_getint32 = (dummyfunc3)harmless;
1297c478bd9Sstevel@tonic-gate 	ops.x_putint32 = x_putint32_t;
1307c478bd9Sstevel@tonic-gate #endif
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/* the other harmless ones */
1337c478bd9Sstevel@tonic-gate 	ops.x_getbytes = (dummyfunc2)harmless;
1347c478bd9Sstevel@tonic-gate 	ops.x_control = (dummyfunc4)harmless;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	x.x_op = XDR_ENCODE;
1377c478bd9Sstevel@tonic-gate 	x.x_ops = &ops;
1387c478bd9Sstevel@tonic-gate 	x.x_handy = 0;
1397c478bd9Sstevel@tonic-gate 	x.x_private = (caddr_t)NULL;
140*4c273cfaSVitaliy Gusev 	x.x_base = NULL;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	stat = func(&x, data);
1437c478bd9Sstevel@tonic-gate 	if (x.x_private)
1447c478bd9Sstevel@tonic-gate 		mem_free(x.x_private, (uintptr_t)x.x_base);
1457c478bd9Sstevel@tonic-gate 	return (stat == TRUE ? (unsigned int)x.x_handy: 0);
1467c478bd9Sstevel@tonic-gate }
147