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