1 | /* |
---|
2 | * Sun RPC is a product of Sun Microsystems, Inc. and is provided for |
---|
3 | * unrestricted use provided that this legend is included on all tape |
---|
4 | * media and as a part of the software program in whole or part. Users |
---|
5 | * may copy or modify Sun RPC without charge, but are not authorized |
---|
6 | * to license or distribute it to anyone else except as part of a product or |
---|
7 | * program developed by the user. |
---|
8 | * |
---|
9 | * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE |
---|
10 | * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR |
---|
11 | * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. |
---|
12 | * |
---|
13 | * Sun RPC is provided with no support and without any obligation on the |
---|
14 | * part of Sun Microsystems, Inc. to assist in its use, correction, |
---|
15 | * modification or enhancement. |
---|
16 | * |
---|
17 | * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE |
---|
18 | * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC |
---|
19 | * OR ANY PART THEREOF. |
---|
20 | * |
---|
21 | * In no event will Sun Microsystems, Inc. be liable for any lost revenue |
---|
22 | * or profits or other special, indirect and consequential damages, even if |
---|
23 | * Sun has been advised of the possibility of such damages. |
---|
24 | * |
---|
25 | * Sun Microsystems, Inc. |
---|
26 | * 2550 Garcia Avenue |
---|
27 | * Mountain View, California 94043 |
---|
28 | */ |
---|
29 | /* |
---|
30 | * xdr_sizeof.c |
---|
31 | * |
---|
32 | * Copyright 1990 Sun Microsystems, Inc. |
---|
33 | * |
---|
34 | * General purpose routine to see how much space something will use |
---|
35 | * when serialized using XDR. |
---|
36 | */ |
---|
37 | |
---|
38 | #ifdef HAVE_CONFIG_H |
---|
39 | #include "config.h" |
---|
40 | #endif |
---|
41 | |
---|
42 | #include <rpc/types.h> |
---|
43 | #include <rpc/xdr.h> |
---|
44 | #include <sys/types.h> |
---|
45 | #include <stdlib.h> |
---|
46 | |
---|
47 | /* ARGSUSED */ |
---|
48 | static bool_t |
---|
49 | x_putlong( |
---|
50 | XDR *xdrs, |
---|
51 | const long *longp) |
---|
52 | { |
---|
53 | xdrs->x_handy += BYTES_PER_XDR_UNIT; |
---|
54 | return (TRUE); |
---|
55 | } |
---|
56 | |
---|
57 | /* ARGSUSED */ |
---|
58 | static bool_t |
---|
59 | x_putbytes( |
---|
60 | XDR *xdrs, |
---|
61 | const char *bp, |
---|
62 | u_int len) |
---|
63 | { |
---|
64 | xdrs->x_handy += len; |
---|
65 | return (TRUE); |
---|
66 | } |
---|
67 | |
---|
68 | static u_int |
---|
69 | x_getpostn( |
---|
70 | XDR *xdrs) |
---|
71 | { |
---|
72 | return (xdrs->x_handy); |
---|
73 | } |
---|
74 | |
---|
75 | /* ARGSUSED */ |
---|
76 | static bool_t |
---|
77 | x_setpostn( |
---|
78 | XDR *xdrs, |
---|
79 | u_int pos) |
---|
80 | { |
---|
81 | /* This is not allowed */ |
---|
82 | return (FALSE); |
---|
83 | } |
---|
84 | |
---|
85 | static int32_t * |
---|
86 | x_inline( |
---|
87 | XDR *xdrs, |
---|
88 | u_int len) |
---|
89 | { |
---|
90 | if (len == 0) { |
---|
91 | return (NULL); |
---|
92 | } |
---|
93 | if (xdrs->x_op != XDR_ENCODE) { |
---|
94 | return (NULL); |
---|
95 | } |
---|
96 | if (len < (intptr_t) xdrs->x_base) { |
---|
97 | /* x_private was already allocated */ |
---|
98 | xdrs->x_handy += len; |
---|
99 | return ((int32_t *) xdrs->x_private); |
---|
100 | } else { |
---|
101 | /* Free the earlier space and allocate new area */ |
---|
102 | if (xdrs->x_private) |
---|
103 | free(xdrs->x_private); |
---|
104 | if ((xdrs->x_private = (caddr_t) malloc(len)) == NULL) { |
---|
105 | xdrs->x_base = 0; |
---|
106 | return (NULL); |
---|
107 | } |
---|
108 | xdrs->x_base = (caddr_t)((intptr_t) len); |
---|
109 | xdrs->x_handy += len; |
---|
110 | return ((int32_t *) xdrs->x_private); |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | static int |
---|
115 | harmless(void) |
---|
116 | { |
---|
117 | /* Always return FALSE/NULL, as the case may be */ |
---|
118 | return (0); |
---|
119 | } |
---|
120 | |
---|
121 | static void |
---|
122 | x_destroy( |
---|
123 | XDR *xdrs) |
---|
124 | { |
---|
125 | xdrs->x_handy = 0; |
---|
126 | xdrs->x_base = 0; |
---|
127 | if (xdrs->x_private) { |
---|
128 | free(xdrs->x_private); |
---|
129 | xdrs->x_private = NULL; |
---|
130 | } |
---|
131 | return; |
---|
132 | } |
---|
133 | |
---|
134 | unsigned long |
---|
135 | xdr_sizeof( |
---|
136 | xdrproc_t func, |
---|
137 | void *data) |
---|
138 | { |
---|
139 | XDR x; |
---|
140 | struct xdr_ops ops; |
---|
141 | bool_t stat; |
---|
142 | /* to stop ANSI-C compiler from complaining */ |
---|
143 | typedef bool_t (* dummyfunc1)(XDR *, long *); |
---|
144 | typedef bool_t (* dummyfunc2)(XDR *, caddr_t, u_int); |
---|
145 | |
---|
146 | ops.x_putlong = x_putlong; |
---|
147 | ops.x_putbytes = x_putbytes; |
---|
148 | ops.x_inline = x_inline; |
---|
149 | ops.x_getpostn = x_getpostn; |
---|
150 | ops.x_setpostn = x_setpostn; |
---|
151 | ops.x_destroy = x_destroy; |
---|
152 | |
---|
153 | /* the other harmless ones */ |
---|
154 | ops.x_getlong = (dummyfunc1) harmless; |
---|
155 | ops.x_getbytes = (dummyfunc2) harmless; |
---|
156 | |
---|
157 | x.x_op = XDR_ENCODE; |
---|
158 | x.x_ops = &ops; |
---|
159 | x.x_handy = 0; |
---|
160 | x.x_private = (caddr_t) NULL; |
---|
161 | x.x_base = (caddr_t) 0; |
---|
162 | |
---|
163 | stat = func(&x, data); |
---|
164 | if (x.x_private) |
---|
165 | free(x.x_private); |
---|
166 | return (stat == TRUE ? (unsigned) x.x_handy: 0); |
---|
167 | } |
---|