1 |
|
|
/* $OpenBSD: xdr_mem.c,v 1.17 2015/11/01 03:45:29 guenther Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 2010, Oracle America, Inc. |
5 |
|
|
* |
6 |
|
|
* Redistribution and use in source and binary forms, with or without |
7 |
|
|
* modification, are permitted provided that the following conditions are |
8 |
|
|
* met: |
9 |
|
|
* |
10 |
|
|
* * Redistributions of source code must retain the above copyright |
11 |
|
|
* notice, this list of conditions and the following disclaimer. |
12 |
|
|
* * Redistributions in binary form must reproduce the above |
13 |
|
|
* copyright notice, this list of conditions and the following |
14 |
|
|
* disclaimer in the documentation and/or other materials |
15 |
|
|
* provided with the distribution. |
16 |
|
|
* * Neither the name of the "Oracle America, Inc." nor the names of its |
17 |
|
|
* contributors may be used to endorse or promote products derived |
18 |
|
|
* from this software without specific prior written permission. |
19 |
|
|
* |
20 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
21 |
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
22 |
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
23 |
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
24 |
|
|
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
25 |
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
27 |
|
|
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
28 |
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
29 |
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
30 |
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
31 |
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 |
|
|
*/ |
33 |
|
|
|
34 |
|
|
/* |
35 |
|
|
* xdr_mem.h, XDR implementation using memory buffers. |
36 |
|
|
* |
37 |
|
|
* If you have some data to be interpreted as external data representation |
38 |
|
|
* or to be converted to external data representation in a memory buffer, |
39 |
|
|
* then this is the package for you. |
40 |
|
|
* |
41 |
|
|
*/ |
42 |
|
|
|
43 |
|
|
#include <sys/types.h> |
44 |
|
|
#include <netinet/in.h> |
45 |
|
|
#include <string.h> |
46 |
|
|
|
47 |
|
|
#include <rpc/types.h> |
48 |
|
|
#include <rpc/xdr.h> |
49 |
|
|
|
50 |
|
|
static bool_t xdrmem_getlong_aligned(XDR *, long *); |
51 |
|
|
static bool_t xdrmem_putlong_aligned(XDR *, long *); |
52 |
|
|
static bool_t xdrmem_getlong_unaligned(XDR *, long *); |
53 |
|
|
static bool_t xdrmem_putlong_unaligned(XDR *, long *); |
54 |
|
|
static bool_t xdrmem_getbytes(XDR *, caddr_t, u_int); |
55 |
|
|
static bool_t xdrmem_putbytes(XDR *, caddr_t, u_int); |
56 |
|
|
static u_int xdrmem_getpos(XDR *); /* XXX w/64-bit pointers, u_int not enough! */ |
57 |
|
|
static bool_t xdrmem_setpos(XDR *, u_int); |
58 |
|
|
static int32_t *xdrmem_inline_aligned(XDR *, u_int); |
59 |
|
|
static int32_t *xdrmem_inline_unaligned(XDR *, u_int); |
60 |
|
|
static void xdrmem_destroy(XDR *); |
61 |
|
|
|
62 |
|
|
static struct xdr_ops xdrmem_ops_aligned = { |
63 |
|
|
xdrmem_getlong_aligned, |
64 |
|
|
xdrmem_putlong_aligned, |
65 |
|
|
xdrmem_getbytes, |
66 |
|
|
xdrmem_putbytes, |
67 |
|
|
xdrmem_getpos, |
68 |
|
|
xdrmem_setpos, |
69 |
|
|
xdrmem_inline_aligned, |
70 |
|
|
xdrmem_destroy, |
71 |
|
|
NULL, /* xdrmem_control */ |
72 |
|
|
}; |
73 |
|
|
|
74 |
|
|
static struct xdr_ops xdrmem_ops_unaligned = { |
75 |
|
|
xdrmem_getlong_unaligned, |
76 |
|
|
xdrmem_putlong_unaligned, |
77 |
|
|
xdrmem_getbytes, |
78 |
|
|
xdrmem_putbytes, |
79 |
|
|
xdrmem_getpos, |
80 |
|
|
xdrmem_setpos, |
81 |
|
|
xdrmem_inline_unaligned, |
82 |
|
|
xdrmem_destroy, |
83 |
|
|
NULL, /* xdrmem_control */ |
84 |
|
|
}; |
85 |
|
|
|
86 |
|
|
/* |
87 |
|
|
* The procedure xdrmem_create initializes a stream descriptor for a |
88 |
|
|
* memory buffer. |
89 |
|
|
*/ |
90 |
|
|
void |
91 |
|
|
xdrmem_create(XDR *xdrs, caddr_t addr, u_int size, enum xdr_op op) |
92 |
|
|
{ |
93 |
|
|
|
94 |
|
|
xdrs->x_op = op; |
95 |
|
|
xdrs->x_ops = ((size_t)addr & (sizeof(int32_t) - 1)) |
96 |
|
|
? &xdrmem_ops_unaligned : &xdrmem_ops_aligned; |
97 |
|
|
xdrs->x_private = xdrs->x_base = addr; |
98 |
|
|
xdrs->x_handy = size; |
99 |
|
|
} |
100 |
|
|
DEF_WEAK(xdrmem_create); |
101 |
|
|
|
102 |
|
|
static void |
103 |
|
|
xdrmem_destroy(XDR *xdrs) |
104 |
|
|
{ |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
static bool_t |
108 |
|
|
xdrmem_getlong_aligned(XDR *xdrs, long int *lp) |
109 |
|
|
{ |
110 |
|
|
|
111 |
|
|
if (xdrs->x_handy < sizeof(int32_t)) |
112 |
|
|
return (FALSE); |
113 |
|
|
xdrs->x_handy -= sizeof(int32_t); |
114 |
|
|
*lp = ntohl(*(int32_t *)xdrs->x_private); |
115 |
|
|
xdrs->x_private += sizeof(int32_t); |
116 |
|
|
return (TRUE); |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
static bool_t |
120 |
|
|
xdrmem_putlong_aligned(XDR *xdrs, long int *lp) |
121 |
|
|
{ |
122 |
|
|
|
123 |
|
|
if (xdrs->x_handy < sizeof(int32_t)) |
124 |
|
|
return (FALSE); |
125 |
|
|
xdrs->x_handy -= sizeof(int32_t); |
126 |
|
|
*(int32_t *)xdrs->x_private = htonl((u_int32_t)*lp); |
127 |
|
|
xdrs->x_private += sizeof(int32_t); |
128 |
|
|
return (TRUE); |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
static bool_t |
132 |
|
|
xdrmem_getlong_unaligned(XDR *xdrs, long int *lp) |
133 |
|
|
{ |
134 |
|
|
int32_t l; |
135 |
|
|
|
136 |
|
|
if (xdrs->x_handy < sizeof(int32_t)) |
137 |
|
|
return (FALSE); |
138 |
|
|
xdrs->x_handy -= sizeof(int32_t); |
139 |
|
|
memcpy(&l, xdrs->x_private, sizeof(int32_t)); |
140 |
|
|
*lp = ntohl(l); |
141 |
|
|
xdrs->x_private += sizeof(int32_t); |
142 |
|
|
return (TRUE); |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
static bool_t |
146 |
|
|
xdrmem_putlong_unaligned(XDR *xdrs, long int *lp) |
147 |
|
|
{ |
148 |
|
|
int32_t l; |
149 |
|
|
|
150 |
|
|
if (xdrs->x_handy < sizeof(int32_t)) |
151 |
|
|
return (FALSE); |
152 |
|
|
xdrs->x_handy -= sizeof(int32_t); |
153 |
|
|
l = htonl((u_int32_t)*lp); |
154 |
|
|
memcpy(xdrs->x_private, &l, sizeof(int32_t)); |
155 |
|
|
xdrs->x_private += sizeof(int32_t); |
156 |
|
|
return (TRUE); |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
static bool_t |
160 |
|
|
xdrmem_getbytes(XDR *xdrs, caddr_t addr, u_int len) |
161 |
|
|
{ |
162 |
|
|
|
163 |
|
|
if (xdrs->x_handy < len) |
164 |
|
|
return (FALSE); |
165 |
|
|
xdrs->x_handy -= len; |
166 |
|
|
memcpy(addr, xdrs->x_private, len); |
167 |
|
|
xdrs->x_private += len; |
168 |
|
|
return (TRUE); |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
static bool_t |
172 |
|
|
xdrmem_putbytes(XDR *xdrs, caddr_t addr, u_int len) |
173 |
|
|
{ |
174 |
|
|
|
175 |
|
|
if (xdrs->x_handy < len) |
176 |
|
|
return (FALSE); |
177 |
|
|
xdrs->x_handy -= len; |
178 |
|
|
memcpy(xdrs->x_private, addr, len); |
179 |
|
|
xdrs->x_private += len; |
180 |
|
|
return (TRUE); |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
static u_int |
184 |
|
|
xdrmem_getpos(XDR *xdrs) |
185 |
|
|
{ |
186 |
|
|
|
187 |
|
|
/* XXX w/64-bit pointers, u_int not enough! */ |
188 |
|
|
return ((u_long)xdrs->x_private - (u_long)xdrs->x_base); |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
static bool_t |
192 |
|
|
xdrmem_setpos(XDR *xdrs, u_int pos) |
193 |
|
|
{ |
194 |
|
|
caddr_t newaddr = xdrs->x_base + pos; |
195 |
|
|
caddr_t lastaddr = xdrs->x_private + xdrs->x_handy; |
196 |
|
|
|
197 |
|
|
if (newaddr > lastaddr) |
198 |
|
|
return (FALSE); |
199 |
|
|
xdrs->x_private = newaddr; |
200 |
|
|
xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX w/64-bit pointers, u_int not enough! */ |
201 |
|
|
return (TRUE); |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
static int32_t * |
205 |
|
|
xdrmem_inline_aligned(XDR *xdrs, u_int len) |
206 |
|
|
{ |
207 |
|
|
int32_t *buf = 0; |
208 |
|
|
|
209 |
|
|
if (xdrs->x_handy >= len) { |
210 |
|
|
xdrs->x_handy -= len; |
211 |
|
|
buf = (int32_t *)xdrs->x_private; |
212 |
|
|
xdrs->x_private += len; |
213 |
|
|
} |
214 |
|
|
return (buf); |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
static int32_t * |
218 |
|
|
xdrmem_inline_unaligned(XDR *xdrs, u_int len) |
219 |
|
|
{ |
220 |
|
|
|
221 |
|
|
return (0); |
222 |
|
|
} |