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