xdr_mem.c revision 1.1 1 /* $NetBSD: xdr_mem.c,v 1.1 2019/06/04 15:07:55 hannken 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 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
36 #if 0
37 static char *sccsid = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
38 static char *sccsid = "@(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC";
39 #else
40 __RCSID("$NetBSD: xdr_mem.c,v 1.1 2019/06/04 15:07:55 hannken Exp $");
41 #endif
42 #endif
43
44 /*
45 * xdr_mem.h, XDR implementation using memory buffers.
46 *
47 * Copyright (C) 1984, Sun Microsystems, Inc.
48 *
49 * If you have some data to be interpreted as external data representation
50 * or to be converted to external data representation in a memory buffer,
51 * then this is the package for you.
52 *
53 */
54
55 #include "namespace.h"
56
57 #include <sys/types.h>
58
59 #include <netinet/in.h>
60
61 #include <string.h>
62
63 #include <rpc/types.h>
64 #include <rpc/xdr.h>
65
66 #ifdef __weak_alias
67 __weak_alias(xdrmem_create,_xdrmem_create)
68 #endif
69
70 static void xdrmem_destroy(XDR *);
71 static bool_t xdrmem_getlong_aligned(XDR *, long *);
72 static bool_t xdrmem_putlong_aligned(XDR *, const long *);
73 static bool_t xdrmem_getlong_unaligned(XDR *, long *);
74 static bool_t xdrmem_putlong_unaligned(XDR *, const long *);
75 static bool_t xdrmem_getbytes(XDR *, char *, u_int);
76 static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
77 /* XXX: w/64-bit pointers, u_int not enough! */
78 static u_int xdrmem_getpos(XDR *);
79 static bool_t xdrmem_setpos(XDR *, u_int);
80 static int32_t *xdrmem_inline_aligned(XDR *, u_int);
81 static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
82 static bool_t xdrmem_control(XDR *xdrs, int request, void *info);
83
84 static const struct xdr_ops xdrmem_ops_aligned = {
85 xdrmem_getlong_aligned,
86 xdrmem_putlong_aligned,
87 xdrmem_getbytes,
88 xdrmem_putbytes,
89 xdrmem_getpos,
90 xdrmem_setpos,
91 xdrmem_inline_aligned,
92 xdrmem_destroy,
93 xdrmem_control
94 };
95
96 static const struct xdr_ops xdrmem_ops_unaligned = {
97 xdrmem_getlong_unaligned,
98 xdrmem_putlong_unaligned,
99 xdrmem_getbytes,
100 xdrmem_putbytes,
101 xdrmem_getpos,
102 xdrmem_setpos,
103 xdrmem_inline_unaligned,
104 xdrmem_destroy,
105 xdrmem_control
106 };
107
108 /*
109 * The procedure xdrmem_create initializes a stream descriptor for a
110 * memory buffer.
111 */
112 void
113 xdrmem_create(XDR *xdrs, char *addr, u_int size, enum xdr_op op)
114 {
115
116 xdrs->x_op = op;
117 xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
118 ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
119 xdrs->x_private = xdrs->x_base = addr;
120 xdrs->x_handy = size;
121 }
122
123 /*ARGSUSED*/
124 static void
125 xdrmem_destroy(XDR *xdrs)
126 {
127
128 }
129
130 static bool_t
131 xdrmem_getlong_aligned(XDR *xdrs, long *lp)
132 {
133
134 if (xdrs->x_handy < sizeof(int32_t))
135 return (FALSE);
136 xdrs->x_handy -= sizeof(int32_t);
137 *lp = ntohl(*(u_int32_t *)xdrs->x_private);
138 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
139 return (TRUE);
140 }
141
142 static bool_t
143 xdrmem_putlong_aligned(XDR *xdrs, const long *lp)
144 {
145
146 if (xdrs->x_handy < sizeof(int32_t))
147 return (FALSE);
148 xdrs->x_handy -= sizeof(int32_t);
149 *(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp);
150 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
151 return (TRUE);
152 }
153
154 static bool_t
155 xdrmem_getlong_unaligned(XDR *xdrs, long *lp)
156 {
157 u_int32_t l;
158
159 if (xdrs->x_handy < sizeof(int32_t))
160 return (FALSE);
161 xdrs->x_handy -= sizeof(int32_t);
162 memmove(&l, xdrs->x_private, sizeof(int32_t));
163 *lp = ntohl(l);
164 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
165 return (TRUE);
166 }
167
168 static bool_t
169 xdrmem_putlong_unaligned(XDR *xdrs, const long *lp)
170 {
171 u_int32_t l;
172
173 if (xdrs->x_handy < sizeof(int32_t))
174 return (FALSE);
175 xdrs->x_handy -= sizeof(int32_t);
176 l = htonl((u_int32_t)*lp);
177 memmove(xdrs->x_private, &l, sizeof(int32_t));
178 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
179 return (TRUE);
180 }
181
182 static bool_t
183 xdrmem_getbytes(XDR *xdrs, char *addr, u_int len)
184 {
185
186 if (xdrs->x_handy < len)
187 return (FALSE);
188 xdrs->x_handy -= len;
189 memmove(addr, xdrs->x_private, len);
190 xdrs->x_private = (char *)xdrs->x_private + len;
191 return (TRUE);
192 }
193
194 static bool_t
195 xdrmem_putbytes(XDR *xdrs, const char *addr, u_int len)
196 {
197
198 if (xdrs->x_handy < len)
199 return (FALSE);
200 xdrs->x_handy -= len;
201 memmove(xdrs->x_private, addr, len);
202 xdrs->x_private = (char *)xdrs->x_private + len;
203 return (TRUE);
204 }
205
206 static u_int
207 xdrmem_getpos(XDR *xdrs)
208 {
209
210 /* XXX w/64-bit pointers, u_int not enough! */
211 return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
212 }
213
214 static bool_t
215 xdrmem_setpos(XDR *xdrs, u_int pos)
216 {
217 char *newaddr = xdrs->x_base + pos;
218 char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
219
220 if ((long)newaddr > (long)lastaddr)
221 return (FALSE);
222 xdrs->x_private = newaddr;
223 xdrs->x_handy = (int)((long)lastaddr - (long)newaddr);
224 return (TRUE);
225 }
226
227 static int32_t *
228 xdrmem_inline_aligned(XDR *xdrs, u_int len)
229 {
230 int32_t *buf = 0;
231
232 if (xdrs->x_handy >= len) {
233 xdrs->x_handy -= len;
234 buf = (int32_t *)xdrs->x_private;
235 xdrs->x_private = (char *)xdrs->x_private + len;
236 }
237 return (buf);
238 }
239
240 /* ARGSUSED */
241 static int32_t *
242 xdrmem_inline_unaligned(XDR *xdrs, u_int len)
243 {
244
245 return (0);
246 }
247
248 static bool_t
249 xdrmem_control(XDR *xdrs, int request, void *info)
250 {
251 xdr_bytesrec *xptr;
252
253 switch (request) {
254
255 case XDR_GET_BYTES_AVAIL:
256 xptr = (xdr_bytesrec *)info;
257 xptr->xc_is_last_record = TRUE;
258 xptr->xc_num_avail = xdrs->x_handy;
259 return (TRUE);
260
261 }
262 return (FALSE);
263 }
264