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