- Ansify function definitions.
[dragonfly.git] / lib / libc / xdr / xdr_sizeof.c
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.
8  * 
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.
12  * 
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.
16  * 
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.
20  * 
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.
24  * 
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  *
29  * $DragonFly: src/lib/libc/xdr/xdr_sizeof.c,v 1.2 2005/12/05 00:47:57 swildner Exp $
30  */
31
32 /*
33  * xdr_sizeof.c
34  *
35  * Copyright 1990 Sun Microsystems, Inc.
36  *
37  * General purpose routine to see how much space something will use
38  * when serialized using XDR.
39  */
40
41 #include <rpc/types.h>
42 #include <rpc/xdr.h>
43 #include <sys/types.h>
44 #include <stdlib.h>
45
46 /* ARGSUSED */
47 static bool_t
48 x_putlong(XDR *xdrs, long *longp __unused)
49 {
50         xdrs->x_handy += BYTES_PER_XDR_UNIT;
51         return (TRUE);
52 }
53
54 /* ARGSUSED */
55 static bool_t
56 x_putbytes(XDR *xdrs, char *bp __unused, u_int len)
57 {
58         xdrs->x_handy += len;
59         return (TRUE);
60 }
61
62 static u_int
63 x_getpostn(XDR *xdrs)
64 {
65         return (xdrs->x_handy);
66 }
67
68 /* ARGSUSED */
69 static bool_t
70 x_setpostn(XDR *xdrs __unused, u_int pos __unused)
71 {
72         /* This is not allowed */
73         return (FALSE);
74 }
75
76 static int32_t *
77 x_inline(XDR *xdrs, long len)
78 {
79         if (len == 0) {
80                 return (NULL);
81         }
82         if (xdrs->x_op != XDR_ENCODE) {
83                 return (NULL);
84         }
85         if (len < (long) xdrs->x_base) {
86                 /* x_private was already allocated */
87                 xdrs->x_handy += len;
88                 return ((int32_t *) xdrs->x_private);
89         } else {
90                 /* Free the earlier space and allocate new area */
91                 if (xdrs->x_private)
92                         free(xdrs->x_private);
93                 if ((xdrs->x_private = (caddr_t) malloc(len)) == NULL) {
94                         xdrs->x_base = 0;
95                         return (NULL);
96                 }
97                 xdrs->x_base = (caddr_t) len;
98                 xdrs->x_handy += len;
99                 return ((int32_t *) xdrs->x_private);
100         }
101 }
102
103 static int
104 harmless(void)
105 {
106         /* Always return FALSE/NULL, as the case may be */
107         return (0);
108 }
109
110 static void
111 x_destroy(XDR *xdrs)
112 {
113         xdrs->x_handy = 0;
114         xdrs->x_base = 0;
115         if (xdrs->x_private) {
116                 free(xdrs->x_private);
117                 xdrs->x_private = NULL;
118         }
119         return;
120 }
121
122 unsigned long
123 xdr_sizeof(xdrproc_t func, void *data)
124 {
125         XDR x;
126         struct xdr_ops ops;
127         bool_t stat;
128         /* to stop ANSI-C compiler from complaining */
129         typedef  bool_t (* dummyfunc1)(XDR *, long *);
130         typedef  bool_t (* dummyfunc2)(XDR *, caddr_t, u_int);
131
132         ops.x_putlong = x_putlong;
133         ops.x_putbytes = x_putbytes;
134         ops.x_inline = x_inline;
135         ops.x_getpostn = x_getpostn;
136         ops.x_setpostn = x_setpostn;
137         ops.x_destroy = x_destroy;
138
139         /* the other harmless ones */
140         ops.x_getlong =  (dummyfunc1) harmless;
141         ops.x_getbytes = (dummyfunc2) harmless;
142
143         x.x_op = XDR_ENCODE;
144         x.x_ops = &ops;
145         x.x_handy = 0;
146         x.x_private = (caddr_t) NULL;
147         x.x_base = (caddr_t) 0;
148
149         stat = func(&x, data);
150         if (x.x_private)
151                 free(x.x_private);
152         return (stat == TRUE ? (unsigned) x.x_handy: 0);
153 }