xref: /freebsd/lib/libc/xdr/xdr_mem.c (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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 __FBSDID("$FreeBSD$");
42 
43 /*
44  * xdr_mem.h, XDR implementation using memory buffers.
45  *
46  * If you have some data to be interpreted as external data representation
47  * or to be converted to external data representation in a memory buffer,
48  * then this is the package for you.
49  *
50  */
51 
52 #include "namespace.h"
53 #include <sys/types.h>
54 
55 #include <netinet/in.h>
56 
57 #include <string.h>
58 
59 #include <rpc/types.h>
60 #include <rpc/xdr.h>
61 #include "un-namespace.h"
62 
63 static void xdrmem_destroy(XDR *);
64 static bool_t xdrmem_getlong_aligned(XDR *, long *);
65 static bool_t xdrmem_putlong_aligned(XDR *, const long *);
66 static bool_t xdrmem_getlong_unaligned(XDR *, long *);
67 static bool_t xdrmem_putlong_unaligned(XDR *, const long *);
68 static bool_t xdrmem_getbytes(XDR *, char *, u_int);
69 static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
70 /* XXX: w/64-bit pointers, u_int not enough! */
71 static u_int xdrmem_getpos(XDR *);
72 static bool_t xdrmem_setpos(XDR *, u_int);
73 static int32_t *xdrmem_inline_aligned(XDR *, u_int);
74 static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
75 
76 static const struct	xdr_ops xdrmem_ops_aligned = {
77 	xdrmem_getlong_aligned,
78 	xdrmem_putlong_aligned,
79 	xdrmem_getbytes,
80 	xdrmem_putbytes,
81 	xdrmem_getpos,
82 	xdrmem_setpos,
83 	xdrmem_inline_aligned,
84 	xdrmem_destroy
85 };
86 
87 static const struct	xdr_ops xdrmem_ops_unaligned = {
88 	xdrmem_getlong_unaligned,
89 	xdrmem_putlong_unaligned,
90 	xdrmem_getbytes,
91 	xdrmem_putbytes,
92 	xdrmem_getpos,
93 	xdrmem_setpos,
94 	xdrmem_inline_unaligned,
95 	xdrmem_destroy
96 };
97 
98 /*
99  * The procedure xdrmem_create initializes a stream descriptor for a
100  * memory buffer.
101  */
102 void
103 xdrmem_create(XDR *xdrs, char *addr, u_int size, enum xdr_op op)
104 {
105 
106 	xdrs->x_op = op;
107 	xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
108 	    ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
109 	xdrs->x_private = xdrs->x_base = addr;
110 	xdrs->x_handy = size;
111 }
112 
113 /*ARGSUSED*/
114 static void
115 xdrmem_destroy(XDR *xdrs)
116 {
117 
118 }
119 
120 static bool_t
121 xdrmem_getlong_aligned(XDR *xdrs, long *lp)
122 {
123 
124 	if (xdrs->x_handy < sizeof(int32_t))
125 		return (FALSE);
126 	xdrs->x_handy -= sizeof(int32_t);
127 	*lp = ntohl(*(u_int32_t *)xdrs->x_private);
128 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
129 	return (TRUE);
130 }
131 
132 static bool_t
133 xdrmem_putlong_aligned(XDR *xdrs, const long *lp)
134 {
135 
136 	if (xdrs->x_handy < sizeof(int32_t))
137 		return (FALSE);
138 	xdrs->x_handy -= sizeof(int32_t);
139 	*(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp);
140 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
141 	return (TRUE);
142 }
143 
144 static bool_t
145 xdrmem_getlong_unaligned(XDR *xdrs, long *lp)
146 {
147 	u_int32_t l;
148 
149 	if (xdrs->x_handy < sizeof(int32_t))
150 		return (FALSE);
151 	xdrs->x_handy -= sizeof(int32_t);
152 	memmove(&l, xdrs->x_private, sizeof(int32_t));
153 	*lp = ntohl(l);
154 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
155 	return (TRUE);
156 }
157 
158 static bool_t
159 xdrmem_putlong_unaligned(XDR *xdrs, const long *lp)
160 {
161 	u_int32_t l;
162 
163 	if (xdrs->x_handy < sizeof(int32_t))
164 		return (FALSE);
165 	xdrs->x_handy -= sizeof(int32_t);
166 	l = htonl((u_int32_t)*lp);
167 	memmove(xdrs->x_private, &l, sizeof(int32_t));
168 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
169 	return (TRUE);
170 }
171 
172 static bool_t
173 xdrmem_getbytes(XDR *xdrs, char *addr, u_int len)
174 {
175 
176 	if (xdrs->x_handy < len)
177 		return (FALSE);
178 	xdrs->x_handy -= len;
179 	memmove(addr, xdrs->x_private, len);
180 	xdrs->x_private = (char *)xdrs->x_private + len;
181 	return (TRUE);
182 }
183 
184 static bool_t
185 xdrmem_putbytes(XDR *xdrs, const char *addr, u_int len)
186 {
187 
188 	if (xdrs->x_handy < len)
189 		return (FALSE);
190 	xdrs->x_handy -= len;
191 	memmove(xdrs->x_private, addr, len);
192 	xdrs->x_private = (char *)xdrs->x_private + len;
193 	return (TRUE);
194 }
195 
196 static u_int
197 xdrmem_getpos(XDR *xdrs)
198 {
199 
200 	/* XXX w/64-bit pointers, u_int not enough! */
201 	return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
202 }
203 
204 static bool_t
205 xdrmem_setpos(XDR *xdrs, u_int pos)
206 {
207 	char *newaddr = xdrs->x_base + pos;
208 	char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
209 
210 	if (newaddr > lastaddr)
211 		return (FALSE);
212 	xdrs->x_private = newaddr;
213 	xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
214 	return (TRUE);
215 }
216 
217 static int32_t *
218 xdrmem_inline_aligned(XDR *xdrs, u_int len)
219 {
220 	int32_t *buf = NULL;
221 
222 	if (xdrs->x_handy >= len) {
223 		xdrs->x_handy -= len;
224 		buf = (int32_t *)xdrs->x_private;
225 		xdrs->x_private = (char *)xdrs->x_private + len;
226 	}
227 	return (buf);
228 }
229 
230 /* ARGSUSED */
231 static int32_t *
232 xdrmem_inline_unaligned(XDR *xdrs, u_int len)
233 {
234 
235 	return (0);
236 }
237