1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* General purpose routine to see how much space something will use
* when serialized using XDR.
*/
#include "mt.h"
#include <rpc/types.h>
#include <rpc/xdr.h>
#include <sys/types.h>
#include <stdlib.h>
/* ARGSUSED1 */
static bool_t
x_putlong(XDR *xdrs, long *ip)
{
xdrs->x_handy += BYTES_PER_XDR_UNIT;
return (TRUE);
}
#if defined(_LP64)
/* ARGSUSED1 */
static bool_t
x_putint32_t(XDR *xdrs, int32_t *ip)
{
xdrs->x_handy += BYTES_PER_XDR_UNIT;
return (TRUE);
}
#endif
/* ARGSUSED */
static bool_t
x_putbytes(XDR *xdrs, char *bp, int len)
{
xdrs->x_handy += len;
return (TRUE);
}
static uint_t
x_getpostn(XDR *xdrs)
{
return (xdrs->x_handy);
}
/* ARGSUSED */
static bool_t
x_setpostn(XDR *xdrs, uint_t pos)
{
/* This is not allowed */
return (FALSE);
}
static rpc_inline_t *
x_inline(XDR *xdrs, int len)
{
if (len == 0)
return (NULL);
if (xdrs->x_op != XDR_ENCODE)
return (NULL);
if (len < (intptr_t)xdrs->x_base) {
/* x_private was already allocated */
xdrs->x_handy += len;
/* LINTED pointer cast */
return ((rpc_inline_t *)xdrs->x_private);
}
/* Free the earlier space and allocate new area */
if (xdrs->x_private)
free(xdrs->x_private);
if ((xdrs->x_private = malloc(len)) == NULL) {
xdrs->x_base = 0;
return (NULL);
}
xdrs->x_base = (caddr_t)(intptr_t)len;
xdrs->x_handy += len;
/* LINTED pointer cast */
return ((rpc_inline_t *)xdrs->x_private);
}
static bool_t
harmless_getlong(XDR *xdr __unused, long *l __unused)
{
/* Always return FALSE/NULL, as the case may be */
return (FALSE);
}
static bool_t
harmless_getbytes(XDR *xdr __unused, caddr_t p __unused, int i __unused)
{
return (FALSE);
}
#if defined(_LP64)
static bool_t
harmless_getint32(XDR *xdr __unused, int32_t *p __unused)
{
return (FALSE);
}
#endif
static void
x_destroy(XDR *xdrs)
{
xdrs->x_handy = 0;
xdrs->x_base = 0;
if (xdrs->x_private) {
free(xdrs->x_private);
xdrs->x_private = NULL;
}
}
uint_t
xdr_sizeof(xdrproc_t func, void *data)
{
XDR x;
struct xdr_ops ops;
bool_t stat;
/* to stop ANSI-C compiler from complaining */
ops.x_putlong = x_putlong;
ops.x_getlong = harmless_getlong;
ops.x_putbytes = x_putbytes;
ops.x_inline = x_inline;
ops.x_getpostn = x_getpostn;
ops.x_setpostn = x_setpostn;
ops.x_destroy = x_destroy;
#if defined(_LP64)
ops.x_getint32 = harmless_getint32;
ops.x_putint32 = x_putint32_t;
#endif
/* the other harmless ones */
ops.x_getbytes = harmless_getbytes;
x.x_op = XDR_ENCODE;
x.x_ops = &ops;
x.x_handy = 0;
x.x_private = NULL;
x.x_base = NULL;
stat = func(&x, data);
if (x.x_private)
free(x.x_private);
return (stat == TRUE ? (uint_t)x.x_handy : 0);
}
|