xref: /freebsd/lib/libc/xdr/xdr_sizeof.c (revision 9c75ed7f390b10eeef1047a39c3ddd41ba57de9b)
1a204967aSHiroki Sato /*-
2a204967aSHiroki Sato  * Copyright (c) 2010, Oracle America, Inc.
37d0a5a39SBill Paul  *
4a204967aSHiroki Sato  * Redistribution and use in source and binary forms, with or without
5a204967aSHiroki Sato  * modification, are permitted provided that the following conditions are
6a204967aSHiroki Sato  * met:
77d0a5a39SBill Paul  *
8a204967aSHiroki Sato  *     * Redistributions of source code must retain the above copyright
9a204967aSHiroki Sato  *       notice, this list of conditions and the following disclaimer.
10a204967aSHiroki Sato  *     * Redistributions in binary form must reproduce the above
11a204967aSHiroki Sato  *       copyright notice, this list of conditions and the following
12a204967aSHiroki Sato  *       disclaimer in the documentation and/or other materials
13a204967aSHiroki Sato  *       provided with the distribution.
14a204967aSHiroki Sato  *     * Neither the name of the "Oracle America, Inc." nor the names of its
15a204967aSHiroki Sato  *       contributors may be used to endorse or promote products derived
16a204967aSHiroki Sato  *       from this software without specific prior written permission.
177d0a5a39SBill Paul  *
18a204967aSHiroki Sato  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19a204967aSHiroki Sato  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20a204967aSHiroki Sato  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21a204967aSHiroki Sato  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22a204967aSHiroki Sato  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23a204967aSHiroki Sato  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24a204967aSHiroki Sato  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25a204967aSHiroki Sato  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26a204967aSHiroki Sato  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27a204967aSHiroki Sato  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28a204967aSHiroki Sato  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29a204967aSHiroki Sato  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
307d0a5a39SBill Paul  */
317d0a5a39SBill Paul /*
327d0a5a39SBill Paul  * xdr_sizeof.c
337d0a5a39SBill Paul  *
347d0a5a39SBill Paul  * General purpose routine to see how much space something will use
357d0a5a39SBill Paul  * when serialized using XDR.
367d0a5a39SBill Paul  */
377d0a5a39SBill Paul 
38333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
39333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
40333fc21eSDavid E. O'Brien 
418360efbdSAlfred Perlstein #include "namespace.h"
427d0a5a39SBill Paul #include <rpc/types.h>
437d0a5a39SBill Paul #include <rpc/xdr.h>
447d0a5a39SBill Paul #include <sys/types.h>
457d0a5a39SBill Paul #include <stdlib.h>
468360efbdSAlfred Perlstein #include "un-namespace.h"
477d0a5a39SBill Paul 
487d0a5a39SBill Paul /* ARGSUSED */
497d0a5a39SBill Paul static bool_t
50*9c75ed7fSCraig Rodrigues x_putlong(XDR *xdrs, const long *longp)
517d0a5a39SBill Paul {
527d0a5a39SBill Paul 	xdrs->x_handy += BYTES_PER_XDR_UNIT;
537d0a5a39SBill Paul 	return (TRUE);
547d0a5a39SBill Paul }
557d0a5a39SBill Paul 
567d0a5a39SBill Paul /* ARGSUSED */
577d0a5a39SBill Paul static bool_t
58*9c75ed7fSCraig Rodrigues x_putbytes(XDR *xdrs, const char *bp, u_int len)
597d0a5a39SBill Paul {
607d0a5a39SBill Paul 	xdrs->x_handy += len;
617d0a5a39SBill Paul 	return (TRUE);
627d0a5a39SBill Paul }
637d0a5a39SBill Paul 
647d0a5a39SBill Paul static u_int
65d660d38dSCraig Rodrigues x_getpostn(XDR *xdrs)
667d0a5a39SBill Paul {
677d0a5a39SBill Paul 	return (xdrs->x_handy);
687d0a5a39SBill Paul }
697d0a5a39SBill Paul 
707d0a5a39SBill Paul /* ARGSUSED */
717d0a5a39SBill Paul static bool_t
72d660d38dSCraig Rodrigues x_setpostn(XDR *xdrs, u_int pos)
737d0a5a39SBill Paul {
747d0a5a39SBill Paul 	/* This is not allowed */
757d0a5a39SBill Paul 	return (FALSE);
767d0a5a39SBill Paul }
777d0a5a39SBill Paul 
787d0a5a39SBill Paul static int32_t *
79d660d38dSCraig Rodrigues x_inline(XDR *xdrs, u_int len)
807d0a5a39SBill Paul {
817d0a5a39SBill Paul 	if (len == 0) {
827d0a5a39SBill Paul 		return (NULL);
837d0a5a39SBill Paul 	}
847d0a5a39SBill Paul 	if (xdrs->x_op != XDR_ENCODE) {
857d0a5a39SBill Paul 		return (NULL);
867d0a5a39SBill Paul 	}
87d98b6d6eSKevin Lo 	if (len < (u_int)(uintptr_t)xdrs->x_base) {
887d0a5a39SBill Paul 		/* x_private was already allocated */
897d0a5a39SBill Paul 		xdrs->x_handy += len;
907d0a5a39SBill Paul 		return ((int32_t *) xdrs->x_private);
917d0a5a39SBill Paul 	} else {
927d0a5a39SBill Paul 		/* Free the earlier space and allocate new area */
937d0a5a39SBill Paul 		if (xdrs->x_private)
947d0a5a39SBill Paul 			free(xdrs->x_private);
957d0a5a39SBill Paul 		if ((xdrs->x_private = (caddr_t) malloc(len)) == NULL) {
967d0a5a39SBill Paul 			xdrs->x_base = 0;
977d0a5a39SBill Paul 			return (NULL);
987d0a5a39SBill Paul 		}
99d98b6d6eSKevin Lo 		xdrs->x_base = (caddr_t)(uintptr_t)len;
1007d0a5a39SBill Paul 		xdrs->x_handy += len;
1017d0a5a39SBill Paul 		return ((int32_t *) xdrs->x_private);
1027d0a5a39SBill Paul 	}
1037d0a5a39SBill Paul }
1047d0a5a39SBill Paul 
1057d0a5a39SBill Paul static int
1067d0a5a39SBill Paul harmless()
1077d0a5a39SBill Paul {
1087d0a5a39SBill Paul 	/* Always return FALSE/NULL, as the case may be */
1097d0a5a39SBill Paul 	return (0);
1107d0a5a39SBill Paul }
1117d0a5a39SBill Paul 
1127d0a5a39SBill Paul static void
113d660d38dSCraig Rodrigues x_destroy(XDR *xdrs)
1147d0a5a39SBill Paul {
1157d0a5a39SBill Paul 	xdrs->x_handy = 0;
1167d0a5a39SBill Paul 	xdrs->x_base = 0;
1177d0a5a39SBill Paul 	if (xdrs->x_private) {
1187d0a5a39SBill Paul 		free(xdrs->x_private);
1197d0a5a39SBill Paul 		xdrs->x_private = NULL;
1207d0a5a39SBill Paul 	}
1217d0a5a39SBill Paul 	return;
1227d0a5a39SBill Paul }
1237d0a5a39SBill Paul 
1247d0a5a39SBill Paul unsigned long
125d660d38dSCraig Rodrigues xdr_sizeof(xdrproc_t func, void *data)
1267d0a5a39SBill Paul {
1277d0a5a39SBill Paul 	XDR x;
1287d0a5a39SBill Paul 	struct xdr_ops ops;
1297d0a5a39SBill Paul 	bool_t stat;
1307d0a5a39SBill Paul 	/* to stop ANSI-C compiler from complaining */
1317d0a5a39SBill Paul 	typedef  bool_t (* dummyfunc1)(XDR *, long *);
1327d0a5a39SBill Paul 	typedef  bool_t (* dummyfunc2)(XDR *, caddr_t, u_int);
1337d0a5a39SBill Paul 
1347d0a5a39SBill Paul 	ops.x_putlong = x_putlong;
1357d0a5a39SBill Paul 	ops.x_putbytes = x_putbytes;
1367d0a5a39SBill Paul 	ops.x_inline = x_inline;
1377d0a5a39SBill Paul 	ops.x_getpostn = x_getpostn;
1387d0a5a39SBill Paul 	ops.x_setpostn = x_setpostn;
1397d0a5a39SBill Paul 	ops.x_destroy = x_destroy;
1407d0a5a39SBill Paul 
1417d0a5a39SBill Paul 	/* the other harmless ones */
1427d0a5a39SBill Paul 	ops.x_getlong =  (dummyfunc1) harmless;
1437d0a5a39SBill Paul 	ops.x_getbytes = (dummyfunc2) harmless;
1447d0a5a39SBill Paul 
1457d0a5a39SBill Paul 	x.x_op = XDR_ENCODE;
1467d0a5a39SBill Paul 	x.x_ops = &ops;
1477d0a5a39SBill Paul 	x.x_handy = 0;
1487d0a5a39SBill Paul 	x.x_private = (caddr_t) NULL;
1497d0a5a39SBill Paul 	x.x_base = (caddr_t) 0;
1507d0a5a39SBill Paul 
1517d0a5a39SBill Paul 	stat = func(&x, data);
1527d0a5a39SBill Paul 	if (x.x_private)
1537d0a5a39SBill Paul 		free(x.x_private);
1547d0a5a39SBill Paul 	return (stat == TRUE ? (unsigned) x.x_handy: 0);
1557d0a5a39SBill Paul }
156