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