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