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