xref: /illumos-gate/usr/src/lib/libnsl/rpc/xdr_sizeof.c (revision 3665ce8a)
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  */
2261961e0fSrobinson 
237c478bd9Sstevel@tonic-gate /*
24e8031f0aSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
2861961e0fSrobinson /*
2961961e0fSrobinson  * General purpose routine to see how much space something will use
3061961e0fSrobinson  * when serialized using XDR.
3161961e0fSrobinson  */
32e8031f0aSraf #include "mt.h"
337c478bd9Sstevel@tonic-gate #include <rpc/types.h>
347c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate 
3861961e0fSrobinson /* ARGSUSED1 */
397c478bd9Sstevel@tonic-gate static bool_t
x_putlong(XDR * xdrs,long * ip)407c478bd9Sstevel@tonic-gate x_putlong(XDR *xdrs, long *ip)
417c478bd9Sstevel@tonic-gate {
427c478bd9Sstevel@tonic-gate 	xdrs->x_handy += BYTES_PER_XDR_UNIT;
437c478bd9Sstevel@tonic-gate 	return (TRUE);
447c478bd9Sstevel@tonic-gate }
457c478bd9Sstevel@tonic-gate 
4661961e0fSrobinson #if defined(_LP64)
4761961e0fSrobinson /* ARGSUSED1 */
487c478bd9Sstevel@tonic-gate static bool_t
x_putint32_t(XDR * xdrs,int32_t * ip)497c478bd9Sstevel@tonic-gate x_putint32_t(XDR *xdrs, int32_t *ip)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	xdrs->x_handy += BYTES_PER_XDR_UNIT;
527c478bd9Sstevel@tonic-gate 	return (TRUE);
537c478bd9Sstevel@tonic-gate }
5461961e0fSrobinson #endif
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* ARGSUSED */
577c478bd9Sstevel@tonic-gate static bool_t
x_putbytes(XDR * xdrs,char * bp,int len)587c478bd9Sstevel@tonic-gate x_putbytes(XDR *xdrs, char *bp, int len)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	xdrs->x_handy += len;
617c478bd9Sstevel@tonic-gate 	return (TRUE);
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static uint_t
x_getpostn(XDR * xdrs)657c478bd9Sstevel@tonic-gate x_getpostn(XDR *xdrs)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	return (xdrs->x_handy);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /* ARGSUSED */
717c478bd9Sstevel@tonic-gate static bool_t
x_setpostn(XDR * xdrs,uint_t pos)727c478bd9Sstevel@tonic-gate x_setpostn(XDR *xdrs, uint_t pos)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	/* This is not allowed */
757c478bd9Sstevel@tonic-gate 	return (FALSE);
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static rpc_inline_t *
x_inline(XDR * xdrs,int len)797c478bd9Sstevel@tonic-gate x_inline(XDR *xdrs, int len)
807c478bd9Sstevel@tonic-gate {
8161961e0fSrobinson 	if (len == 0)
827c478bd9Sstevel@tonic-gate 		return (NULL);
8361961e0fSrobinson 	if (xdrs->x_op != XDR_ENCODE)
847c478bd9Sstevel@tonic-gate 		return (NULL);
857c478bd9Sstevel@tonic-gate 	if (len < (intptr_t)xdrs->x_base) {
867c478bd9Sstevel@tonic-gate 		/* x_private was already allocated */
877c478bd9Sstevel@tonic-gate 		xdrs->x_handy += len;
8861961e0fSrobinson 		/* LINTED pointer cast */
897c478bd9Sstevel@tonic-gate 		return ((rpc_inline_t *)xdrs->x_private);
907c478bd9Sstevel@tonic-gate 	}
9161961e0fSrobinson 	/* Free the earlier space and allocate new area */
9261961e0fSrobinson 	if (xdrs->x_private)
9361961e0fSrobinson 		free(xdrs->x_private);
9461961e0fSrobinson 	if ((xdrs->x_private = malloc(len)) == NULL) {
9561961e0fSrobinson 		xdrs->x_base = 0;
9661961e0fSrobinson 		return (NULL);
9761961e0fSrobinson 	}
9861961e0fSrobinson 	xdrs->x_base = (caddr_t)(intptr_t)len;
9961961e0fSrobinson 	xdrs->x_handy += len;
10061961e0fSrobinson 	/* LINTED pointer cast */
10161961e0fSrobinson 	return ((rpc_inline_t *)xdrs->x_private);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
104*3665ce8aSToomas Soome static bool_t
harmless_getlong(XDR * xdr __unused,long * l __unused)105*3665ce8aSToomas Soome harmless_getlong(XDR *xdr __unused, long *l __unused)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	/* Always return FALSE/NULL, as the case may be */
108*3665ce8aSToomas Soome 	return (FALSE);
109*3665ce8aSToomas Soome }
110*3665ce8aSToomas Soome 
111*3665ce8aSToomas Soome static bool_t
harmless_getbytes(XDR * xdr __unused,caddr_t p __unused,int i __unused)112*3665ce8aSToomas Soome harmless_getbytes(XDR *xdr __unused, caddr_t p __unused, int i __unused)
113*3665ce8aSToomas Soome {
114*3665ce8aSToomas Soome 	return (FALSE);
115*3665ce8aSToomas Soome }
116*3665ce8aSToomas Soome 
117*3665ce8aSToomas Soome #if defined(_LP64)
118*3665ce8aSToomas Soome static bool_t
harmless_getint32(XDR * xdr __unused,int32_t * p __unused)119*3665ce8aSToomas Soome harmless_getint32(XDR *xdr __unused, int32_t *p __unused)
120*3665ce8aSToomas Soome {
121*3665ce8aSToomas Soome 	return (FALSE);
1227c478bd9Sstevel@tonic-gate }
123*3665ce8aSToomas Soome #endif
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate static void
x_destroy(XDR * xdrs)1267c478bd9Sstevel@tonic-gate x_destroy(XDR *xdrs)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	xdrs->x_handy = 0;
1297c478bd9Sstevel@tonic-gate 	xdrs->x_base = 0;
1307c478bd9Sstevel@tonic-gate 	if (xdrs->x_private) {
1317c478bd9Sstevel@tonic-gate 		free(xdrs->x_private);
1327c478bd9Sstevel@tonic-gate 		xdrs->x_private = NULL;
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
13661961e0fSrobinson uint_t
xdr_sizeof(xdrproc_t func,void * data)1377c478bd9Sstevel@tonic-gate xdr_sizeof(xdrproc_t func, void *data)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	XDR x;
1407c478bd9Sstevel@tonic-gate 	struct xdr_ops ops;
1417c478bd9Sstevel@tonic-gate 	bool_t stat;
1427c478bd9Sstevel@tonic-gate 	/* to stop ANSI-C compiler from complaining */
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	ops.x_putlong = x_putlong;
145*3665ce8aSToomas Soome 	ops.x_getlong =  harmless_getlong;
1467c478bd9Sstevel@tonic-gate 	ops.x_putbytes = x_putbytes;
1477c478bd9Sstevel@tonic-gate 	ops.x_inline = x_inline;
1487c478bd9Sstevel@tonic-gate 	ops.x_getpostn = x_getpostn;
1497c478bd9Sstevel@tonic-gate 	ops.x_setpostn = x_setpostn;
1507c478bd9Sstevel@tonic-gate 	ops.x_destroy = x_destroy;
1517c478bd9Sstevel@tonic-gate #if defined(_LP64)
152*3665ce8aSToomas Soome 	ops.x_getint32 = harmless_getint32;
1537c478bd9Sstevel@tonic-gate 	ops.x_putint32 = x_putint32_t;
1547c478bd9Sstevel@tonic-gate #endif
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/* the other harmless ones */
157*3665ce8aSToomas Soome 	ops.x_getbytes = harmless_getbytes;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	x.x_op = XDR_ENCODE;
1607c478bd9Sstevel@tonic-gate 	x.x_ops = &ops;
1617c478bd9Sstevel@tonic-gate 	x.x_handy = 0;
16261961e0fSrobinson 	x.x_private = NULL;
16361961e0fSrobinson 	x.x_base = NULL;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	stat = func(&x, data);
1667c478bd9Sstevel@tonic-gate 	if (x.x_private)
1677c478bd9Sstevel@tonic-gate 		free(x.x_private);
16861961e0fSrobinson 	return (stat == TRUE ? (uint_t)x.x_handy : 0);
1697c478bd9Sstevel@tonic-gate }
170