]>
Commit | Line | Data |
---|---|---|
0413b54c | 1 | /* |
a7ab6ec8 | 2 | * xdr_sizeof.c |
cb636bb2 | 3 | * |
a7ab6ec8 | 4 | * Copyright (c) 2010, Oracle America, Inc. |
0413b54c | 5 | * |
a7ab6ec8 UD |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions are | |
8 | * met: | |
0413b54c | 9 | * |
a7ab6ec8 UD |
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. | |
0413b54c | 19 | * |
a7ab6ec8 UD |
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. | |
0413b54c UD |
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> | |
82f43dd2 | 41 | #include <shlib-compat.h> |
0413b54c UD |
42 | |
43 | /* ARGSUSED */ | |
44 | static bool_t | |
45 | x_putlong (XDR *xdrs, const long *longp) | |
46 | { | |
47 | xdrs->x_handy += BYTES_PER_XDR_UNIT; | |
48 | return TRUE; | |
49 | } | |
50 | ||
51 | /* ARGSUSED */ | |
52 | static bool_t | |
53 | x_putbytes (XDR *xdrs, const char *bp, u_int len) | |
54 | { | |
55 | xdrs->x_handy += len; | |
56 | return TRUE; | |
57 | } | |
58 | ||
59 | static u_int | |
60 | x_getpostn (const XDR *xdrs) | |
61 | { | |
62 | return xdrs->x_handy; | |
63 | } | |
64 | ||
65 | /* ARGSUSED */ | |
66 | static bool_t | |
67 | x_setpostn (XDR *xdrs, u_int len) | |
68 | { | |
69 | /* This is not allowed */ | |
70 | return FALSE; | |
71 | } | |
72 | ||
f8afba91 | 73 | static int32_t * |
fd4c894c | 74 | x_inline (XDR *xdrs, u_int len) |
0413b54c UD |
75 | { |
76 | if (len == 0) | |
f8afba91 | 77 | return NULL; |
0413b54c | 78 | if (xdrs->x_op != XDR_ENCODE) |
f8afba91 | 79 | return NULL; |
fd4c894c | 80 | if (len < (u_int) (long int) xdrs->x_base) |
0413b54c UD |
81 | { |
82 | /* x_private was already allocated */ | |
83 | xdrs->x_handy += len; | |
f8afba91 | 84 | return (int32_t *) xdrs->x_private; |
0413b54c UD |
85 | } |
86 | else | |
87 | { | |
88 | /* Free the earlier space and allocate new area */ | |
72e6cdfa | 89 | free (xdrs->x_private); |
0413b54c UD |
90 | if ((xdrs->x_private = (caddr_t) malloc (len)) == NULL) |
91 | { | |
92 | xdrs->x_base = 0; | |
93 | return NULL; | |
94 | } | |
0e5d0c0a | 95 | xdrs->x_base = (void *) (long) len; |
0413b54c | 96 | xdrs->x_handy += len; |
f8afba91 | 97 | return (int32_t *) xdrs->x_private; |
0413b54c UD |
98 | } |
99 | } | |
100 | ||
101 | static int | |
102 | harmless (void) | |
103 | { | |
104 | /* Always return FALSE/NULL, as the case may be */ | |
105 | return 0; | |
106 | } | |
107 | ||
108 | static void | |
109 | x_destroy (XDR *xdrs) | |
110 | { | |
111 | xdrs->x_handy = 0; | |
112 | xdrs->x_base = 0; | |
113 | if (xdrs->x_private) | |
114 | { | |
115 | free (xdrs->x_private); | |
116 | xdrs->x_private = NULL; | |
117 | } | |
118 | return; | |
119 | } | |
120 | ||
7d1de115 UD |
121 | static bool_t |
122 | x_putint32 (XDR *xdrs, const int32_t *int32p) | |
123 | { | |
124 | xdrs->x_handy += BYTES_PER_XDR_UNIT; | |
125 | return TRUE; | |
126 | } | |
127 | ||
0413b54c UD |
128 | unsigned long |
129 | xdr_sizeof (xdrproc_t func, void *data) | |
130 | { | |
131 | XDR x; | |
132 | struct xdr_ops ops; | |
133 | bool_t stat; | |
134 | /* to stop ANSI-C compiler from complaining */ | |
135 | typedef bool_t (*dummyfunc1) (XDR *, long *); | |
136 | typedef bool_t (*dummyfunc2) (XDR *, caddr_t, u_int); | |
7d1de115 | 137 | typedef bool_t (*dummyfunc3) (XDR *, int32_t *); |
0413b54c UD |
138 | |
139 | ops.x_putlong = x_putlong; | |
140 | ops.x_putbytes = x_putbytes; | |
141 | ops.x_inline = x_inline; | |
142 | ops.x_getpostn = x_getpostn; | |
143 | ops.x_setpostn = x_setpostn; | |
144 | ops.x_destroy = x_destroy; | |
7d1de115 | 145 | ops.x_putint32 = x_putint32; |
0413b54c UD |
146 | |
147 | /* the other harmless ones */ | |
148 | ops.x_getlong = (dummyfunc1) harmless; | |
149 | ops.x_getbytes = (dummyfunc2) harmless; | |
7d1de115 | 150 | ops.x_getint32 = (dummyfunc3) harmless; |
0413b54c UD |
151 | |
152 | x.x_op = XDR_ENCODE; | |
153 | x.x_ops = &ops; | |
154 | x.x_handy = 0; | |
155 | x.x_private = (caddr_t) NULL; | |
156 | x.x_base = (caddr_t) 0; | |
157 | ||
158 | stat = func (&x, data); | |
72e6cdfa | 159 | free (x.x_private); |
fd4c894c | 160 | return stat == TRUE ? x.x_handy : 0; |
0413b54c | 161 | } |
7b57bfe5 UD |
162 | #ifdef EXPORT_RPC_SYMBOLS |
163 | libc_hidden_def (xdr_sizeof) | |
164 | #else | |
021db4be | 165 | libc_hidden_nolink_sunrpc (xdr_sizeof, GLIBC_2_1) |
7b57bfe5 | 166 | #endif |