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