1 /*
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  *   this list of conditions and the following disclaimer in the documentation
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  *   contributors may be used to endorse or promote products derived
14  *   from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 /*
29  * xdr_sizeof.c
30  *
31  * Copyright 1990 Sun Microsystems, Inc.
32  *
33  * General purpose routine to see how much space something will use
34  * when serialized using XDR.
35  */
36 
37 #define _DEFAULT_SOURCE
38 #include <rpc/types.h>
39 #include <rpc/xdr.h>
40 #include <sys/types.h>
41 #include <stdlib.h>
42 
43 #include "xdr_private.h"
44 
45 /* ARGSUSED */
46 static bool_t
x_putlong(XDR * xdrs,const long * longp)47 x_putlong (XDR * xdrs,
48 	const long *longp)
49 {
50   (void) longp;
51   xdrs->x_handy += BYTES_PER_XDR_UNIT;
52   return TRUE;
53 }
54 
55 /* ARGSUSED */
56 static bool_t
x_putbytes(XDR * xdrs,const char * bp,u_int len)57 x_putbytes (XDR * xdrs,
58 	const char *bp,
59 	u_int len)
60 {
61   (void) bp;
62   xdrs->x_handy += len;
63   return TRUE;
64 }
65 
66 static u_int
x_getpostn(XDR * xdrs)67 x_getpostn (XDR * xdrs)
68 {
69   return xdrs->x_handy;
70 }
71 
72 /* ARGSUSED */
73 static bool_t
x_setpostn(XDR * xdrs,u_int pos)74 x_setpostn (XDR * xdrs,
75 	u_int pos)
76 {
77   (void) xdrs;
78   (void) pos;
79   /* This is not allowed */
80   return FALSE;
81 }
82 
83 static int32_t *
x_inline(XDR * xdrs,u_int len)84 x_inline (XDR * xdrs,
85 	u_int len)
86 {
87   if (len == 0)
88     return NULL;
89   if (xdrs->x_op != XDR_ENCODE)
90     return NULL;
91   if (len < (u_int) (uintptr_t) xdrs->x_base)
92     {
93       /* x_private was already allocated */
94       xdrs->x_handy += len;
95       return (int32_t *) xdrs->x_private;
96     }
97   else
98     {
99       /* Free the earlier space and allocate new area */
100       if (xdrs->x_private)
101         mem_free (xdrs->x_private, sizeof (xdrs->x_private));
102       if ((xdrs->x_private = (caddr_t) mem_alloc (len)) == NULL)
103         {
104           xdrs->x_base = 0;
105           return NULL;
106         }
107       xdrs->x_base = (caddr_t) (intptr_t) len;
108       xdrs->x_handy += len;
109       return (int32_t *) xdrs->x_private;
110     }
111 }
112 
113 static int
harmless(void)114 harmless (void)
115 {
116   /* Always return FALSE/NULL, as the case may be */
117   return 0;
118 }
119 
120 static void
x_destroy(XDR * xdrs)121 x_destroy (XDR * xdrs)
122 {
123   xdrs->x_handy = 0;
124   xdrs->x_base = 0;
125   if (xdrs->x_private)
126     {
127       mem_free (xdrs->x_private, sizeof (xdrs->x_private));
128       xdrs->x_private = NULL;
129     }
130   return;
131 }
132 
133 static bool_t
x_putint32(XDR * xdrs,const int32_t * int32p)134 x_putint32 (XDR *xdrs,
135 	const int32_t *int32p)
136 {
137   (void) int32p;
138   xdrs->x_handy += BYTES_PER_XDR_UNIT;
139   return TRUE;
140 }
141 
142 
143 unsigned long
xdr_sizeof(xdrproc_t func,void * data)144 xdr_sizeof (xdrproc_t func,
145 	void *data)
146 {
147   XDR x;
148   struct xdr_ops ops;
149   bool_t stat;
150   /* to stop ANSI-C compiler from complaining */
151   typedef bool_t (*dummyfunc1) (XDR *, long *);
152   typedef bool_t (*dummyfunc2) (XDR *, caddr_t, u_int);
153   typedef bool_t (*dummyfunc3) (XDR *, int32_t *);
154 
155   ops.x_putlong = x_putlong;
156   ops.x_putbytes = x_putbytes;
157   ops.x_inline = x_inline;
158   ops.x_getpostn = x_getpostn;
159   ops.x_setpostn = x_setpostn;
160   ops.x_destroy = x_destroy;
161   ops.x_putint32 = x_putint32;
162 
163   /* the other harmless ones */
164   ops.x_getlong = (dummyfunc1) (void *) harmless;
165   ops.x_getbytes = (dummyfunc2) (void *) harmless;
166   ops.x_getint32 = (dummyfunc3) (void *) harmless;
167 
168   x.x_op = XDR_ENCODE;
169   x.x_ops = &ops;
170   x.x_handy = 0;
171   x.x_private = (caddr_t) NULL;
172   x.x_base = (caddr_t) 0;
173 
174   stat = func (&x, data);
175   if (x.x_private)
176     mem_free (x.x_private, sizeof (x.x_private));
177   return (stat == TRUE ? x.x_handy : 0);
178 }
179 
180