xref: /dragonfly/lib/libc/xdr/xdr_sizeof.c (revision 0085a56d)
1 /*-
2  * Copyright (c) 2010, Oracle America, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  *       notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  *       copyright notice, this list of conditions and the following
12  *       disclaimer in the documentation and/or other materials
13  *       provided with the distribution.
14  *     * Neither the name of the "Oracle America, Inc." nor the names of its
15  *       contributors may be used to endorse or promote products derived
16  *       from this software without specific prior written permission.
17  *
18  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * $FreeBSD: head/lib/libc/xdr/xdr_sizeof.c 283835 2015-05-31 19:09:24Z rodrigc $
32  */
33 
34 /*
35  * xdr_sizeof.c
36  *
37  * General purpose routine to see how much space something will use
38  * when serialized using XDR.
39  */
40 
41 #include "namespace.h"
42 #include <rpc/types.h>
43 #include <rpc/xdr.h>
44 #include <sys/types.h>
45 #include <stdlib.h>
46 #include "un-namespace.h"
47 
48 /* ARGSUSED */
49 static bool_t
50 x_putlong(XDR *xdrs, const long *longp __unused)
51 {
52 	xdrs->x_handy += BYTES_PER_XDR_UNIT;
53 	return (TRUE);
54 }
55 
56 /* ARGSUSED */
57 static bool_t
58 x_putbytes(XDR *xdrs, const char *bp __unused, u_int len)
59 {
60 	xdrs->x_handy += len;
61 	return (TRUE);
62 }
63 
64 static u_int
65 x_getpostn(XDR *xdrs)
66 {
67 	return (xdrs->x_handy);
68 }
69 
70 /* ARGSUSED */
71 static bool_t
72 x_setpostn(XDR *xdrs __unused, u_int pos __unused)
73 {
74 	/* This is not allowed */
75 	return (FALSE);
76 }
77 
78 static int32_t *
79 x_inline(XDR *xdrs, u_int len)
80 {
81 	if (len == 0) {
82 		return (NULL);
83 	}
84 	if (xdrs->x_op != XDR_ENCODE) {
85 		return (NULL);
86 	}
87 	if (len < (u_int)(uintptr_t)xdrs->x_base) {
88 		/* x_private was already allocated */
89 		xdrs->x_handy += len;
90 		return ((int32_t *) xdrs->x_private);
91 	} else {
92 		/* Free the earlier space and allocate new area */
93 		if (xdrs->x_private)
94 			free(xdrs->x_private);
95 		if ((xdrs->x_private = (caddr_t) malloc(len)) == NULL) {
96 			xdrs->x_base = 0;
97 			return (NULL);
98 		}
99 		xdrs->x_base = (caddr_t)(uintptr_t)len;
100 		xdrs->x_handy += len;
101 		return ((int32_t *) xdrs->x_private);
102 	}
103 }
104 
105 static int
106 harmless(void)
107 {
108 	/* Always return FALSE/NULL, as the case may be */
109 	return (0);
110 }
111 
112 static void
113 x_destroy(XDR *xdrs)
114 {
115 	xdrs->x_handy = 0;
116 	xdrs->x_base = 0;
117 	if (xdrs->x_private) {
118 		free(xdrs->x_private);
119 		xdrs->x_private = NULL;
120 	}
121 	return;
122 }
123 
124 unsigned long
125 xdr_sizeof(xdrproc_t func, void *data)
126 {
127 	XDR x;
128 	struct xdr_ops ops;
129 	bool_t stat;
130 	/* to stop ANSI-C compiler from complaining */
131 	typedef  bool_t (* dummyfunc1)(XDR *, long *);
132 	typedef  bool_t (* dummyfunc2)(XDR *, caddr_t, u_int);
133 
134 	ops.x_putlong = x_putlong;
135 	ops.x_putbytes = x_putbytes;
136 	ops.x_inline = x_inline;
137 	ops.x_getpostn = x_getpostn;
138 	ops.x_setpostn = x_setpostn;
139 	ops.x_destroy = x_destroy;
140 
141 	/* the other harmless ones */
142 	ops.x_getlong =  (dummyfunc1) harmless;
143 	ops.x_getbytes = (dummyfunc2) harmless;
144 
145 	x.x_op = XDR_ENCODE;
146 	x.x_ops = &ops;
147 	x.x_handy = 0;
148 	x.x_private = NULL;
149 	x.x_base = (caddr_t) 0;
150 
151 	stat = func(&x, data);
152 	if (x.x_private)
153 		free(x.x_private);
154 	return (stat == TRUE ? (unsigned) x.x_handy: 0);
155 }
156