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 | #include <rpc/types.h> |
---|
38 | #include <rpc/xdr.h> |
---|
39 | #include <sys/types.h> |
---|
40 | #include <stdlib.h> |
---|
41 | |
---|
42 | #include "xdr_private.h" |
---|
43 | |
---|
44 | /* ARGSUSED */ |
---|
45 | static bool_t |
---|
46 | x_putlong (XDR * xdrs, |
---|
47 | const long *longp) |
---|
48 | { |
---|
49 | xdrs->x_handy += BYTES_PER_XDR_UNIT; |
---|
50 | return TRUE; |
---|
51 | } |
---|
52 | |
---|
53 | /* ARGSUSED */ |
---|
54 | static bool_t |
---|
55 | x_putbytes (XDR * xdrs, |
---|
56 | const char *bp, |
---|
57 | u_int len) |
---|
58 | { |
---|
59 | xdrs->x_handy += len; |
---|
60 | return TRUE; |
---|
61 | } |
---|
62 | |
---|
63 | static u_int |
---|
64 | x_getpostn (XDR * xdrs) |
---|
65 | { |
---|
66 | return xdrs->x_handy; |
---|
67 | } |
---|
68 | |
---|
69 | /* ARGSUSED */ |
---|
70 | static bool_t |
---|
71 | x_setpostn (XDR * xdrs, |
---|
72 | u_int pos) |
---|
73 | { |
---|
74 | /* This is not allowed */ |
---|
75 | return FALSE; |
---|
76 | } |
---|
77 | |
---|
78 | static int32_t * |
---|
79 | x_inline (XDR * xdrs, |
---|
80 | u_int len) |
---|
81 | { |
---|
82 | if (len == 0) |
---|
83 | return NULL; |
---|
84 | if (xdrs->x_op != XDR_ENCODE) |
---|
85 | return NULL; |
---|
86 | if (len < (u_int) (long int) xdrs->x_base) |
---|
87 | { |
---|
88 | /* x_private was already allocated */ |
---|
89 | xdrs->x_handy += len; |
---|
90 | return (int32_t *) xdrs->x_private; |
---|
91 | } |
---|
92 | else |
---|
93 | { |
---|
94 | /* Free the earlier space and allocate new area */ |
---|
95 | if (xdrs->x_private) |
---|
96 | mem_free (xdrs->x_private, sizeof (xdrs->x_private)); |
---|
97 | if ((xdrs->x_private = (caddr_t) mem_alloc (len)) == NULL) |
---|
98 | { |
---|
99 | xdrs->x_base = 0; |
---|
100 | return NULL; |
---|
101 | } |
---|
102 | xdrs->x_base = (caddr_t) (intptr_t) len; |
---|
103 | xdrs->x_handy += len; |
---|
104 | return (int32_t *) xdrs->x_private; |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | static int |
---|
109 | harmless (void) |
---|
110 | { |
---|
111 | /* Always return FALSE/NULL, as the case may be */ |
---|
112 | return 0; |
---|
113 | } |
---|
114 | |
---|
115 | static void |
---|
116 | x_destroy (XDR * xdrs) |
---|
117 | { |
---|
118 | xdrs->x_handy = 0; |
---|
119 | xdrs->x_base = 0; |
---|
120 | if (xdrs->x_private) |
---|
121 | { |
---|
122 | mem_free (xdrs->x_private, sizeof (xdrs->x_private)); |
---|
123 | xdrs->x_private = NULL; |
---|
124 | } |
---|
125 | return; |
---|
126 | } |
---|
127 | |
---|
128 | static bool_t |
---|
129 | x_putint32 (XDR *xdrs, |
---|
130 | const int32_t *int32p) |
---|
131 | { |
---|
132 | xdrs->x_handy += BYTES_PER_XDR_UNIT; |
---|
133 | return TRUE; |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | unsigned long |
---|
138 | xdr_sizeof (xdrproc_t func, |
---|
139 | void *data) |
---|
140 | { |
---|
141 | XDR x; |
---|
142 | struct xdr_ops ops; |
---|
143 | bool_t stat; |
---|
144 | /* to stop ANSI-C compiler from complaining */ |
---|
145 | typedef bool_t (*dummyfunc1) (XDR *, long *); |
---|
146 | typedef bool_t (*dummyfunc2) (XDR *, caddr_t, u_int); |
---|
147 | typedef bool_t (*dummyfunc3) (XDR *, int32_t *); |
---|
148 | |
---|
149 | ops.x_putlong = x_putlong; |
---|
150 | ops.x_putbytes = x_putbytes; |
---|
151 | ops.x_inline = x_inline; |
---|
152 | ops.x_getpostn = x_getpostn; |
---|
153 | ops.x_setpostn = x_setpostn; |
---|
154 | ops.x_destroy = x_destroy; |
---|
155 | ops.x_putint32 = x_putint32; |
---|
156 | |
---|
157 | /* the other harmless ones */ |
---|
158 | ops.x_getlong = (dummyfunc1) harmless; |
---|
159 | ops.x_getbytes = (dummyfunc2) harmless; |
---|
160 | ops.x_getint32 = (dummyfunc3) harmless; |
---|
161 | |
---|
162 | x.x_op = XDR_ENCODE; |
---|
163 | x.x_ops = &ops; |
---|
164 | x.x_handy = 0; |
---|
165 | x.x_private = (caddr_t) NULL; |
---|
166 | x.x_base = (caddr_t) 0; |
---|
167 | |
---|
168 | stat = func (&x, data); |
---|
169 | if (x.x_private) |
---|
170 | mem_free (x.x_private, sizeof (x.x_private)); |
---|
171 | return (stat == TRUE ? x.x_handy : 0); |
---|
172 | } |
---|
173 | |
---|