source: rtems-libbsd/freebsd/lib/libc/xdr/xdr_sizeof.c @ f41a394

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since f41a394 was f41a394, checked in by Sebastian Huber <sebastian.huber@…>, on Jun 9, 2016 at 9:46:09 AM

XDR(3): Import from FreeBSD

  • Property mode set to 100644
File size: 3.7 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part.  Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California  94043
30 */
31/*
32 * xdr_sizeof.c
33 *
34 * Copyright 1990 Sun Microsystems, Inc.
35 *
36 * General purpose routine to see how much space something will use
37 * when serialized using XDR.
38 */
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD$");
42
43#include "namespace.h"
44#include <rpc/types.h>
45#include <rpc/xdr.h>
46#include <sys/types.h>
47#include <stdlib.h>
48#include "un-namespace.h"
49
50/* ARGSUSED */
51static bool_t
52x_putlong(xdrs, longp)
53        XDR *xdrs;
54        long *longp;
55{
56        xdrs->x_handy += BYTES_PER_XDR_UNIT;
57        return (TRUE);
58}
59
60/* ARGSUSED */
61static bool_t
62x_putbytes(xdrs, bp, len)
63        XDR *xdrs;
64        char  *bp;
65        u_int len;
66{
67        xdrs->x_handy += len;
68        return (TRUE);
69}
70
71static u_int
72x_getpostn(xdrs)
73        XDR *xdrs;
74{
75        return (xdrs->x_handy);
76}
77
78/* ARGSUSED */
79static bool_t
80x_setpostn(xdrs, pos)
81        XDR *xdrs;
82        u_int pos;
83{
84        /* This is not allowed */
85        return (FALSE);
86}
87
88static int32_t *
89x_inline(xdrs, len)
90        XDR *xdrs;
91        u_int len;
92{
93        if (len == 0) {
94                return (NULL);
95        }
96        if (xdrs->x_op != XDR_ENCODE) {
97                return (NULL);
98        }
99        if (len < (u_int)(uintptr_t)xdrs->x_base) {
100                /* x_private was already allocated */
101                xdrs->x_handy += len;
102                return ((int32_t *) xdrs->x_private);
103        } else {
104                /* Free the earlier space and allocate new area */
105                if (xdrs->x_private)
106                        free(xdrs->x_private);
107                if ((xdrs->x_private = (caddr_t) malloc(len)) == NULL) {
108                        xdrs->x_base = 0;
109                        return (NULL);
110                }
111                xdrs->x_base = (caddr_t)(uintptr_t)len;
112                xdrs->x_handy += len;
113                return ((int32_t *) xdrs->x_private);
114        }
115}
116
117static int
118harmless()
119{
120        /* Always return FALSE/NULL, as the case may be */
121        return (0);
122}
123
124static void
125x_destroy(xdrs)
126        XDR *xdrs;
127{
128        xdrs->x_handy = 0;
129        xdrs->x_base = 0;
130        if (xdrs->x_private) {
131                free(xdrs->x_private);
132                xdrs->x_private = NULL;
133        }
134        return;
135}
136
137unsigned long
138xdr_sizeof(func, data)
139        xdrproc_t func;
140        void *data;
141{
142        XDR x;
143        struct xdr_ops ops;
144        bool_t stat;
145        /* to stop ANSI-C compiler from complaining */
146        typedef  bool_t (* dummyfunc1)(XDR *, long *);
147        typedef  bool_t (* dummyfunc2)(XDR *, caddr_t, u_int);
148
149        ops.x_putlong = x_putlong;
150        ops.x_putbytes = x_putbytes;
151        ops.x_inline = x_inline;
152        ops.x_getpostn = x_getpostn;
153        ops.x_setpostn = x_setpostn;
154        ops.x_destroy = x_destroy;
155
156        /* the other harmless ones */
157        ops.x_getlong =  (dummyfunc1) harmless;
158        ops.x_getbytes = (dummyfunc2) harmless;
159
160        x.x_op = XDR_ENCODE;
161        x.x_ops = &ops;
162        x.x_handy = 0;
163        x.x_private = (caddr_t) NULL;
164        x.x_base = (caddr_t) 0;
165
166        stat = func(&x, data);
167        if (x.x_private)
168                free(x.x_private);
169        return (stat == TRUE ? (unsigned) x.x_handy: 0);
170}
Note: See TracBrowser for help on using the repository browser.