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