Libinfo-222.3.6.tar.gz
[apple/libinfo.git] / rpc.subproj / xdr_sizeof.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25 /*
26 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
27 * unrestricted use provided that this legend is included on all tape
28 * media and as a part of the software program in whole or part. Users
29 * may copy or modify Sun RPC without charge, but are not authorized
30 * to license or distribute it to anyone else except as part of a product or
31 * program developed by the user.
32 *
33 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
34 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
35 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
36 *
37 * Sun RPC is provided with no support and without any obligation on the
38 * part of Sun Microsystems, Inc. to assist in its use, correction,
39 * modification or enhancement.
40 *
41 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
42 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
43 * OR ANY PART THEREOF.
44 *
45 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
46 * or profits or other special, indirect and consequential damages, even if
47 * Sun has been advised of the possibility of such damages.
48 *
49 * Sun Microsystems, Inc.
50 * 2550 Garcia Avenue
51 * Mountain View, California 94043
52 */
53 /*
54 * xdr_sizeof.c
55 *
56 * Copyright 1990 Sun Microsystems, Inc.
57 *
58 * General purpose routine to see how much space something will use
59 * when serialized using XDR.
60 */
61
62 #include <sys/cdefs.h>
63
64 #include <rpc/types.h>
65 #include <rpc/xdr.h>
66 #include <sys/types.h>
67 #include <stdlib.h>
68
69 /* ARGSUSED */
70 static bool_t
71 x_putlong(xdrs, longp)
72 XDR *xdrs;
73 long *longp;
74 {
75 xdrs->x_handy += BYTES_PER_XDR_UNIT;
76 return (TRUE);
77 }
78
79 /* ARGSUSED */
80 static bool_t
81 x_putbytes(xdrs, bp, len)
82 XDR *xdrs;
83 char *bp;
84 u_int len;
85 {
86 xdrs->x_handy += len;
87 return (TRUE);
88 }
89
90 static u_int
91 x_getpostn(xdrs)
92 XDR *xdrs;
93 {
94 return (xdrs->x_handy);
95 }
96
97 /* ARGSUSED */
98 static bool_t
99 x_setpostn(xdrs, pos)
100 XDR *xdrs;
101 u_int pos;
102 {
103 /* This is not allowed */
104 return (FALSE);
105 }
106
107 static int32_t *
108 x_inline(xdrs, len)
109 XDR *xdrs;
110 u_int len;
111 {
112 long llen;
113
114 if (len == 0) {
115 return (NULL);
116 }
117 if (xdrs->x_op != XDR_ENCODE) {
118 return (NULL);
119 }
120
121 llen = len;
122
123 if (llen < xdrs->x_base) {
124 /* x_private was already allocated */
125 xdrs->x_handy += llen;
126 return ((int32_t *) xdrs->x_private);
127 } else {
128 /* Free the earlier space and allocate new area */
129 if (xdrs->x_private)
130 free(xdrs->x_private);
131 if ((xdrs->x_private = (caddr_t) malloc(len)) == NULL) {
132 xdrs->x_base = 0;
133 return (NULL);
134 }
135 xdrs->x_base = (caddr_t)llen;
136 xdrs->x_handy += llen;
137 return ((int32_t *) xdrs->x_private);
138 }
139 }
140
141 static int
142 harmless()
143 {
144 /* Always return FALSE/NULL, as the case may be */
145 return (0);
146 }
147
148 static void
149 x_destroy(xdrs)
150 XDR *xdrs;
151 {
152 xdrs->x_handy = 0;
153 xdrs->x_base = 0;
154 if (xdrs->x_private) {
155 free(xdrs->x_private);
156 xdrs->x_private = NULL;
157 }
158 return;
159 }
160
161 unsigned long
162 xdr_sizeof(func, data)
163 xdrproc_t func;
164 void *data;
165 {
166 XDR x;
167 struct xdr_ops ops;
168 bool_t stat;
169 /* to stop ANSI-C compiler from complaining */
170 typedef bool_t (* dummyfunc1)(XDR *, long *);
171 typedef bool_t (* dummyfunc2)(XDR *, caddr_t, u_int);
172
173 ops.x_putlong = x_putlong;
174 ops.x_putbytes = x_putbytes;
175 ops.x_inline = x_inline;
176 ops.x_getpostn = x_getpostn;
177 ops.x_setpostn = x_setpostn;
178 ops.x_destroy = x_destroy;
179
180 /* the other harmless ones */
181 ops.x_getlong = (dummyfunc1) harmless;
182 ops.x_getbytes = (dummyfunc2) harmless;
183
184 x.x_op = XDR_ENCODE;
185 x.x_ops = &ops;
186 x.x_handy = 0;
187 x.x_private = (caddr_t) NULL;
188 x.x_base = (caddr_t) 0;
189
190 stat = func(&x, data);
191 if (x.x_private)
192 free(x.x_private);
193 return (stat == TRUE ? (unsigned) x.x_handy: 0);
194 }