xdr_sizeof.c revision 1.5 1 1.1 manu /*
2 1.5 tron * Copyright (c) 2010, Oracle America, Inc.
3 1.5 tron *
4 1.5 tron * Redistribution and use in source and binary forms, with or without
5 1.5 tron * modification, are permitted provided that the following conditions are
6 1.5 tron * met:
7 1.5 tron *
8 1.5 tron * * Redistributions of source code must retain the above copyright
9 1.5 tron * notice, this list of conditions and the following disclaimer.
10 1.5 tron * * Redistributions in binary form must reproduce the above
11 1.5 tron * copyright notice, this list of conditions and the following
12 1.5 tron * disclaimer in the documentation and/or other materials
13 1.5 tron * provided with the distribution.
14 1.5 tron * * Neither the name of the "Oracle America, Inc." nor the names of its
15 1.5 tron * contributors may be used to endorse or promote products derived
16 1.5 tron * from this software without specific prior written permission.
17 1.5 tron *
18 1.5 tron * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 1.5 tron * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 1.5 tron * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 1.5 tron * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 1.5 tron * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 1.5 tron * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.5 tron * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 1.5 tron * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.5 tron * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 1.5 tron * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 1.5 tron * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 1.5 tron * 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.5 tron __RCSID("$NetBSD: xdr_sizeof.c,v 1.5 2013/03/11 20:19:30 tron 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.4 matt x_putlong(XDR *xdrs, const long *longp)
68 1.1 manu {
69 1.1 manu xdrs->x_handy += BYTES_PER_XDR_UNIT;
70 1.1 manu return (TRUE);
71 1.1 manu }
72 1.1 manu
73 1.1 manu /* ARGSUSED */
74 1.1 manu static bool_t
75 1.4 matt x_putbytes(XDR *xdrs, const char *bp, u_int len)
76 1.1 manu {
77 1.1 manu xdrs->x_handy += len;
78 1.1 manu return (TRUE);
79 1.1 manu }
80 1.1 manu
81 1.1 manu static u_int
82 1.4 matt x_getpostn(XDR *xdrs)
83 1.1 manu {
84 1.1 manu return (xdrs->x_handy);
85 1.1 manu }
86 1.1 manu
87 1.1 manu /* ARGSUSED */
88 1.1 manu static bool_t
89 1.4 matt x_setpostn(XDR *xdrs, u_int pos)
90 1.1 manu {
91 1.1 manu /* This is not allowed */
92 1.1 manu return (FALSE);
93 1.1 manu }
94 1.1 manu
95 1.1 manu static int32_t *
96 1.4 matt x_inline(XDR *xdrs, u_int len)
97 1.1 manu {
98 1.1 manu if (len == 0) {
99 1.1 manu return (NULL);
100 1.1 manu }
101 1.1 manu if (xdrs->x_op != XDR_ENCODE) {
102 1.1 manu return (NULL);
103 1.1 manu }
104 1.2 mrg if (len < (u_int)(uintptr_t)xdrs->x_base) {
105 1.1 manu /* x_private was already allocated */
106 1.1 manu xdrs->x_handy += len;
107 1.1 manu return ((int32_t *) xdrs->x_private);
108 1.1 manu } else {
109 1.1 manu /* Free the earlier space and allocate new area */
110 1.1 manu if (xdrs->x_private)
111 1.1 manu free(xdrs->x_private);
112 1.3 dholland if ((xdrs->x_private = malloc(len)) == NULL) {
113 1.1 manu xdrs->x_base = 0;
114 1.1 manu return (NULL);
115 1.1 manu }
116 1.2 mrg xdrs->x_base = (caddr_t)(uintptr_t)len;
117 1.1 manu xdrs->x_handy += len;
118 1.1 manu return ((int32_t *) xdrs->x_private);
119 1.1 manu }
120 1.1 manu }
121 1.1 manu
122 1.1 manu static int
123 1.4 matt harmless(void)
124 1.1 manu {
125 1.1 manu /* Always return FALSE/NULL, as the case may be */
126 1.1 manu return (0);
127 1.1 manu }
128 1.1 manu
129 1.1 manu static void
130 1.4 matt x_destroy(XDR *xdrs)
131 1.1 manu {
132 1.1 manu xdrs->x_handy = 0;
133 1.1 manu xdrs->x_base = 0;
134 1.1 manu if (xdrs->x_private) {
135 1.1 manu free(xdrs->x_private);
136 1.1 manu xdrs->x_private = NULL;
137 1.1 manu }
138 1.1 manu return;
139 1.1 manu }
140 1.1 manu
141 1.1 manu unsigned long
142 1.4 matt xdr_sizeof(xdrproc_t func, void *data)
143 1.1 manu {
144 1.1 manu XDR x;
145 1.1 manu struct xdr_ops ops;
146 1.1 manu bool_t stat;
147 1.1 manu /* to stop ANSI-C compiler from complaining */
148 1.1 manu typedef bool_t (* dummyfunc1)(XDR *, long *);
149 1.1 manu typedef bool_t (* dummyfunc2)(XDR *, caddr_t, u_int);
150 1.1 manu
151 1.1 manu ops.x_putlong = x_putlong;
152 1.1 manu ops.x_putbytes = x_putbytes;
153 1.1 manu ops.x_inline = x_inline;
154 1.1 manu ops.x_getpostn = x_getpostn;
155 1.1 manu ops.x_setpostn = x_setpostn;
156 1.1 manu ops.x_destroy = x_destroy;
157 1.1 manu
158 1.1 manu /* the other harmless ones */
159 1.1 manu ops.x_getlong = (dummyfunc1) harmless;
160 1.1 manu ops.x_getbytes = (dummyfunc2) harmless;
161 1.1 manu
162 1.1 manu x.x_op = XDR_ENCODE;
163 1.1 manu x.x_ops = &ops;
164 1.1 manu x.x_handy = 0;
165 1.1 manu x.x_private = (caddr_t) NULL;
166 1.1 manu x.x_base = (caddr_t) 0;
167 1.1 manu
168 1.1 manu stat = func(&x, data);
169 1.1 manu if (x.x_private)
170 1.1 manu free(x.x_private);
171 1.1 manu return (stat == TRUE ? (unsigned) x.x_handy: 0);
172 1.1 manu }
173