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