1319b6ef0Smanu /*
2*47c0e0c3Stron * Copyright (c) 2010, Oracle America, Inc.
3319b6ef0Smanu *
4*47c0e0c3Stron * Redistribution and use in source and binary forms, with or without
5*47c0e0c3Stron * modification, are permitted provided that the following conditions are
6*47c0e0c3Stron * met:
7319b6ef0Smanu *
8*47c0e0c3Stron * * Redistributions of source code must retain the above copyright
9*47c0e0c3Stron * notice, this list of conditions and the following disclaimer.
10*47c0e0c3Stron * * Redistributions in binary form must reproduce the above
11*47c0e0c3Stron * copyright notice, this list of conditions and the following
12*47c0e0c3Stron * disclaimer in the documentation and/or other materials
13*47c0e0c3Stron * provided with the distribution.
14*47c0e0c3Stron * * Neither the name of the "Oracle America, Inc." nor the names of its
15*47c0e0c3Stron * contributors may be used to endorse or promote products derived
16*47c0e0c3Stron * from this software without specific prior written permission.
17319b6ef0Smanu *
18*47c0e0c3Stron * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*47c0e0c3Stron * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*47c0e0c3Stron * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21*47c0e0c3Stron * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22*47c0e0c3Stron * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23*47c0e0c3Stron * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*47c0e0c3Stron * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25*47c0e0c3Stron * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*47c0e0c3Stron * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27*47c0e0c3Stron * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28*47c0e0c3Stron * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29*47c0e0c3Stron * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30319b6ef0Smanu */
31319b6ef0Smanu /*
32319b6ef0Smanu * xdr_sizeof.c
33319b6ef0Smanu *
34319b6ef0Smanu * Copyright 1990 Sun Microsystems, Inc.
35319b6ef0Smanu *
36319b6ef0Smanu * General purpose routine to see how much space something will use
37319b6ef0Smanu * when serialized using XDR.
38319b6ef0Smanu */
39319b6ef0Smanu
40319b6ef0Smanu #include <sys/cdefs.h>
41319b6ef0Smanu #if 0
42319b6ef0Smanu __FBSDID("$FreeBSD: src/lib/libc/xdr/xdr_sizeof.c,v 1.5.38.1 2010/12/21 17:10:29 kensmith Exp $");
43319b6ef0Smanu #else
44*47c0e0c3Stron __RCSID("$NetBSD: xdr_sizeof.c,v 1.5 2013/03/11 20:19:30 tron Exp $");
45319b6ef0Smanu #endif
46319b6ef0Smanu
47319b6ef0Smanu #include "namespace.h"
48319b6ef0Smanu #include <rpc/types.h>
49319b6ef0Smanu #include <rpc/xdr.h>
50319b6ef0Smanu #include <sys/types.h>
51319b6ef0Smanu #include <stdlib.h>
52319b6ef0Smanu
53319b6ef0Smanu #ifdef __weak_alias
54319b6ef0Smanu __weak_alias(xdr_sizeof,_xdr_sizeof)
55319b6ef0Smanu #endif
56319b6ef0Smanu
57319b6ef0Smanu static bool_t x_putlong(XDR *, const long *);
58319b6ef0Smanu static bool_t x_putbytes(XDR *, const char *, u_int);
59319b6ef0Smanu static u_int x_getpostn(XDR *);
60319b6ef0Smanu static bool_t x_setpostn(XDR *, u_int);
61319b6ef0Smanu static int32_t *x_inline(XDR *, u_int);
62319b6ef0Smanu static int harmless(void);
63319b6ef0Smanu static void x_destroy(XDR *);
64319b6ef0Smanu
65319b6ef0Smanu /* ARGSUSED */
66319b6ef0Smanu static bool_t
x_putlong(XDR * xdrs,const long * longp)67adb74221Smatt x_putlong(XDR *xdrs, const long *longp)
68319b6ef0Smanu {
69319b6ef0Smanu xdrs->x_handy += BYTES_PER_XDR_UNIT;
70319b6ef0Smanu return (TRUE);
71319b6ef0Smanu }
72319b6ef0Smanu
73319b6ef0Smanu /* ARGSUSED */
74319b6ef0Smanu static bool_t
x_putbytes(XDR * xdrs,const char * bp,u_int len)75adb74221Smatt x_putbytes(XDR *xdrs, const char *bp, u_int len)
76319b6ef0Smanu {
77319b6ef0Smanu xdrs->x_handy += len;
78319b6ef0Smanu return (TRUE);
79319b6ef0Smanu }
80319b6ef0Smanu
81319b6ef0Smanu static u_int
x_getpostn(XDR * xdrs)82adb74221Smatt x_getpostn(XDR *xdrs)
83319b6ef0Smanu {
84319b6ef0Smanu return (xdrs->x_handy);
85319b6ef0Smanu }
86319b6ef0Smanu
87319b6ef0Smanu /* ARGSUSED */
88319b6ef0Smanu static bool_t
x_setpostn(XDR * xdrs,u_int pos)89adb74221Smatt x_setpostn(XDR *xdrs, u_int pos)
90319b6ef0Smanu {
91319b6ef0Smanu /* This is not allowed */
92319b6ef0Smanu return (FALSE);
93319b6ef0Smanu }
94319b6ef0Smanu
95319b6ef0Smanu static int32_t *
x_inline(XDR * xdrs,u_int len)96adb74221Smatt x_inline(XDR *xdrs, u_int len)
97319b6ef0Smanu {
98319b6ef0Smanu if (len == 0) {
99319b6ef0Smanu return (NULL);
100319b6ef0Smanu }
101319b6ef0Smanu if (xdrs->x_op != XDR_ENCODE) {
102319b6ef0Smanu return (NULL);
103319b6ef0Smanu }
104ceb5b44cSmrg if (len < (u_int)(uintptr_t)xdrs->x_base) {
105319b6ef0Smanu /* x_private was already allocated */
106319b6ef0Smanu xdrs->x_handy += len;
107319b6ef0Smanu return ((int32_t *) xdrs->x_private);
108319b6ef0Smanu } else {
109319b6ef0Smanu /* Free the earlier space and allocate new area */
110319b6ef0Smanu if (xdrs->x_private)
111319b6ef0Smanu free(xdrs->x_private);
112b972515eSdholland if ((xdrs->x_private = malloc(len)) == NULL) {
113319b6ef0Smanu xdrs->x_base = 0;
114319b6ef0Smanu return (NULL);
115319b6ef0Smanu }
116ceb5b44cSmrg xdrs->x_base = (caddr_t)(uintptr_t)len;
117319b6ef0Smanu xdrs->x_handy += len;
118319b6ef0Smanu return ((int32_t *) xdrs->x_private);
119319b6ef0Smanu }
120319b6ef0Smanu }
121319b6ef0Smanu
122319b6ef0Smanu static int
harmless(void)123adb74221Smatt harmless(void)
124319b6ef0Smanu {
125319b6ef0Smanu /* Always return FALSE/NULL, as the case may be */
126319b6ef0Smanu return (0);
127319b6ef0Smanu }
128319b6ef0Smanu
129319b6ef0Smanu static void
x_destroy(XDR * xdrs)130adb74221Smatt x_destroy(XDR *xdrs)
131319b6ef0Smanu {
132319b6ef0Smanu xdrs->x_handy = 0;
133319b6ef0Smanu xdrs->x_base = 0;
134319b6ef0Smanu if (xdrs->x_private) {
135319b6ef0Smanu free(xdrs->x_private);
136319b6ef0Smanu xdrs->x_private = NULL;
137319b6ef0Smanu }
138319b6ef0Smanu return;
139319b6ef0Smanu }
140319b6ef0Smanu
141319b6ef0Smanu unsigned long
xdr_sizeof(xdrproc_t func,void * data)142adb74221Smatt xdr_sizeof(xdrproc_t func, void *data)
143319b6ef0Smanu {
144319b6ef0Smanu XDR x;
145319b6ef0Smanu struct xdr_ops ops;
146319b6ef0Smanu bool_t stat;
147319b6ef0Smanu /* to stop ANSI-C compiler from complaining */
148319b6ef0Smanu typedef bool_t (* dummyfunc1)(XDR *, long *);
149319b6ef0Smanu typedef bool_t (* dummyfunc2)(XDR *, caddr_t, u_int);
150319b6ef0Smanu
151319b6ef0Smanu ops.x_putlong = x_putlong;
152319b6ef0Smanu ops.x_putbytes = x_putbytes;
153319b6ef0Smanu ops.x_inline = x_inline;
154319b6ef0Smanu ops.x_getpostn = x_getpostn;
155319b6ef0Smanu ops.x_setpostn = x_setpostn;
156319b6ef0Smanu ops.x_destroy = x_destroy;
157319b6ef0Smanu
158319b6ef0Smanu /* the other harmless ones */
159319b6ef0Smanu ops.x_getlong = (dummyfunc1) harmless;
160319b6ef0Smanu ops.x_getbytes = (dummyfunc2) harmless;
161319b6ef0Smanu
162319b6ef0Smanu x.x_op = XDR_ENCODE;
163319b6ef0Smanu x.x_ops = &ops;
164319b6ef0Smanu x.x_handy = 0;
165319b6ef0Smanu x.x_private = (caddr_t) NULL;
166319b6ef0Smanu x.x_base = (caddr_t) 0;
167319b6ef0Smanu
168319b6ef0Smanu stat = func(&x, data);
169319b6ef0Smanu if (x.x_private)
170319b6ef0Smanu free(x.x_private);
171319b6ef0Smanu return (stat == TRUE ? (unsigned) x.x_handy: 0);
172319b6ef0Smanu }
173