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