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