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