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