xref: /illumos-gate/usr/src/uts/common/rpc/xdr_sizeof.c (revision 0827cc77)
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  */
264c273cfaSVitaliy Gusev /*
274c273cfaSVitaliy Gusev  * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
28919f1431SAndrew Stormont  * Copyright 2017 RackTop Systems.
294c273cfaSVitaliy Gusev  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <rpc/types.h>
327c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /* ARGSUSED */
367c478bd9Sstevel@tonic-gate static bool_t
x_putint32_t(XDR * xdrs,int32_t * ip)377c478bd9Sstevel@tonic-gate x_putint32_t(XDR *xdrs, int32_t *ip)
387c478bd9Sstevel@tonic-gate {
397c478bd9Sstevel@tonic-gate 	xdrs->x_handy += BYTES_PER_XDR_UNIT;
407c478bd9Sstevel@tonic-gate 	return (TRUE);
417c478bd9Sstevel@tonic-gate }
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /* ARGSUSED */
447c478bd9Sstevel@tonic-gate static bool_t
x_putbytes(XDR * xdrs,char * bp,int len)457c478bd9Sstevel@tonic-gate x_putbytes(XDR *xdrs, char *bp, int len)
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate 	xdrs->x_handy += len;
487c478bd9Sstevel@tonic-gate 	return (TRUE);
497c478bd9Sstevel@tonic-gate }
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static uint_t
x_getpostn(XDR * xdrs)527c478bd9Sstevel@tonic-gate x_getpostn(XDR *xdrs)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	return (xdrs->x_handy);
557c478bd9Sstevel@tonic-gate }
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /* ARGSUSED */
587c478bd9Sstevel@tonic-gate static bool_t
x_setpostn(XDR * xdrs,uint_t pos)597c478bd9Sstevel@tonic-gate x_setpostn(XDR *xdrs, uint_t pos)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	/* This is not allowed */
627c478bd9Sstevel@tonic-gate 	return (FALSE);
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static rpc_inline_t *
x_inline(XDR * xdrs,int len)667c478bd9Sstevel@tonic-gate x_inline(XDR *xdrs, int len)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	if (len == 0) {
697c478bd9Sstevel@tonic-gate 		return (NULL);
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 	if (xdrs->x_op != XDR_ENCODE) {
727c478bd9Sstevel@tonic-gate 		return (NULL);
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 	if (len < (uintptr_t)xdrs->x_base) {
757c478bd9Sstevel@tonic-gate 		/* x_private was already allocated */
767c478bd9Sstevel@tonic-gate 		xdrs->x_handy += len;
777c478bd9Sstevel@tonic-gate 		return ((rpc_inline_t *)xdrs->x_private);
787c478bd9Sstevel@tonic-gate 	} else {
797c478bd9Sstevel@tonic-gate 		/* Free the earlier space and allocate new area */
807c478bd9Sstevel@tonic-gate 		if (xdrs->x_private)
817c478bd9Sstevel@tonic-gate 			mem_free(xdrs->x_private, (uintptr_t)xdrs->x_base);
827c478bd9Sstevel@tonic-gate 		if ((xdrs->x_private = (caddr_t)mem_alloc(len)) == NULL) {
837c478bd9Sstevel@tonic-gate 			xdrs->x_base = 0;
847c478bd9Sstevel@tonic-gate 			return (NULL);
857c478bd9Sstevel@tonic-gate 		}
867c478bd9Sstevel@tonic-gate 		xdrs->x_base = (caddr_t)(uintptr_t)len;
877c478bd9Sstevel@tonic-gate 		xdrs->x_handy += len;
887c478bd9Sstevel@tonic-gate 		return ((rpc_inline_t *)xdrs->x_private);
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static int
harmless()937c478bd9Sstevel@tonic-gate harmless()
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	/* Always return FALSE/NULL, as the case may be */
967c478bd9Sstevel@tonic-gate 	return (0);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static void
x_destroy(XDR * xdrs)1007c478bd9Sstevel@tonic-gate x_destroy(XDR *xdrs)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	xdrs->x_handy = 0;
1037c478bd9Sstevel@tonic-gate 	if (xdrs->x_private) {
1047c478bd9Sstevel@tonic-gate 		mem_free(xdrs->x_private, (uintptr_t)xdrs->x_base);
1057c478bd9Sstevel@tonic-gate 		xdrs->x_private = NULL;
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 	xdrs->x_base = 0;
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate unsigned int
xdr_sizeof(xdrproc_t func,void * data)1117c478bd9Sstevel@tonic-gate xdr_sizeof(xdrproc_t func, void *data)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	XDR x;
1147c478bd9Sstevel@tonic-gate 	struct xdr_ops ops;
1157c478bd9Sstevel@tonic-gate 	bool_t stat;
116*0827cc77SDan McDonald 	/* to stop ANSI-C compiler from complaining */
117*0827cc77SDan McDonald 	typedef  bool_t (* dummyfunc1)(XDR *, caddr_t, int);
118*0827cc77SDan McDonald 	typedef	 bool_t (* dummyfunc2)(XDR *, int, void *);
119*0827cc77SDan McDonald #if defined(_LP64) || defined(_KERNEL)
120*0827cc77SDan McDonald 	typedef  bool_t (* dummyfunc3)(XDR *, int32_t *);
121*0827cc77SDan McDonald #endif
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	ops.x_putbytes = x_putbytes;
1247c478bd9Sstevel@tonic-gate 	ops.x_inline = x_inline;
1257c478bd9Sstevel@tonic-gate 	ops.x_getpostn = x_getpostn;
1267c478bd9Sstevel@tonic-gate 	ops.x_setpostn = x_setpostn;
1277c478bd9Sstevel@tonic-gate 	ops.x_destroy = x_destroy;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #if defined(_LP64) || defined(_KERNEL)
130*0827cc77SDan McDonald 	ops.x_getint32 = (dummyfunc3)harmless;
1317c478bd9Sstevel@tonic-gate 	ops.x_putint32 = x_putint32_t;
1327c478bd9Sstevel@tonic-gate #endif
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	/* the other harmless ones */
135*0827cc77SDan McDonald 	ops.x_getbytes = (dummyfunc1)harmless;
136*0827cc77SDan McDonald 	ops.x_control = (dummyfunc2)harmless;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	x.x_op = XDR_ENCODE;
1397c478bd9Sstevel@tonic-gate 	x.x_ops = &ops;
1407c478bd9Sstevel@tonic-gate 	x.x_handy = 0;
1417c478bd9Sstevel@tonic-gate 	x.x_private = (caddr_t)NULL;
1424c273cfaSVitaliy Gusev 	x.x_base = NULL;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	stat = func(&x, data);
1457c478bd9Sstevel@tonic-gate 	if (x.x_private)
1467c478bd9Sstevel@tonic-gate 		mem_free(x.x_private, (uintptr_t)x.x_base);
1477c478bd9Sstevel@tonic-gate 	return (stat == TRUE ? (unsigned int)x.x_handy: 0);
1487c478bd9Sstevel@tonic-gate }
149