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