xref: /minix3/lib/libc/rpc/xdr_mem.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1 /*	$NetBSD: xdr_mem.c,v 1.19 2013/03/11 20:19:30 tron 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.19 2013/03/11 20:19:30 tron 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 
83 static const struct	xdr_ops xdrmem_ops_aligned = {
84 	xdrmem_getlong_aligned,
85 	xdrmem_putlong_aligned,
86 	xdrmem_getbytes,
87 	xdrmem_putbytes,
88 	xdrmem_getpos,
89 	xdrmem_setpos,
90 	xdrmem_inline_aligned,
91 	xdrmem_destroy,
92 	NULL,	/* xdrmem_control */
93 };
94 
95 static const struct	xdr_ops xdrmem_ops_unaligned = {
96 	xdrmem_getlong_unaligned,
97 	xdrmem_putlong_unaligned,
98 	xdrmem_getbytes,
99 	xdrmem_putbytes,
100 	xdrmem_getpos,
101 	xdrmem_setpos,
102 	xdrmem_inline_unaligned,
103 	xdrmem_destroy,
104 	NULL,	/* xdrmem_control */
105 };
106 
107 /*
108  * The procedure xdrmem_create initializes a stream descriptor for a
109  * memory buffer.
110  */
111 void
xdrmem_create(XDR * xdrs,char * addr,u_int size,enum xdr_op op)112 xdrmem_create(XDR *xdrs, char *addr, u_int size, enum xdr_op op)
113 {
114 
115 	xdrs->x_op = op;
116 	xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
117 	    ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
118 	xdrs->x_private = xdrs->x_base = addr;
119 	xdrs->x_handy = size;
120 }
121 
122 /*ARGSUSED*/
123 static void
xdrmem_destroy(XDR * xdrs)124 xdrmem_destroy(XDR *xdrs)
125 {
126 
127 }
128 
129 static bool_t
xdrmem_getlong_aligned(XDR * xdrs,long * lp)130 xdrmem_getlong_aligned(XDR *xdrs, long *lp)
131 {
132 
133 	if (xdrs->x_handy < sizeof(int32_t))
134 		return (FALSE);
135 	xdrs->x_handy -= sizeof(int32_t);
136 	*lp = ntohl(*(u_int32_t *)xdrs->x_private);
137 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
138 	return (TRUE);
139 }
140 
141 static bool_t
xdrmem_putlong_aligned(XDR * xdrs,const long * lp)142 xdrmem_putlong_aligned(XDR *xdrs, const long *lp)
143 {
144 
145 	if (xdrs->x_handy < sizeof(int32_t))
146 		return (FALSE);
147 	xdrs->x_handy -= sizeof(int32_t);
148 	*(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp);
149 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
150 	return (TRUE);
151 }
152 
153 static bool_t
xdrmem_getlong_unaligned(XDR * xdrs,long * lp)154 xdrmem_getlong_unaligned(XDR *xdrs, long *lp)
155 {
156 	u_int32_t l;
157 
158 	if (xdrs->x_handy < sizeof(int32_t))
159 		return (FALSE);
160 	xdrs->x_handy -= sizeof(int32_t);
161 	memmove(&l, xdrs->x_private, sizeof(int32_t));
162 	*lp = ntohl(l);
163 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
164 	return (TRUE);
165 }
166 
167 static bool_t
xdrmem_putlong_unaligned(XDR * xdrs,const long * lp)168 xdrmem_putlong_unaligned(XDR *xdrs, const long *lp)
169 {
170 	u_int32_t l;
171 
172 	if (xdrs->x_handy < sizeof(int32_t))
173 		return (FALSE);
174 	xdrs->x_handy -= sizeof(int32_t);
175 	l = htonl((u_int32_t)*lp);
176 	memmove(xdrs->x_private, &l, sizeof(int32_t));
177 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
178 	return (TRUE);
179 }
180 
181 static bool_t
xdrmem_getbytes(XDR * xdrs,char * addr,u_int len)182 xdrmem_getbytes(XDR *xdrs, char *addr, u_int len)
183 {
184 
185 	if (xdrs->x_handy < len)
186 		return (FALSE);
187 	xdrs->x_handy -= len;
188 	memmove(addr, xdrs->x_private, len);
189 	xdrs->x_private = (char *)xdrs->x_private + len;
190 	return (TRUE);
191 }
192 
193 static bool_t
xdrmem_putbytes(XDR * xdrs,const char * addr,u_int len)194 xdrmem_putbytes(XDR *xdrs, const char *addr, u_int len)
195 {
196 
197 	if (xdrs->x_handy < len)
198 		return (FALSE);
199 	xdrs->x_handy -= len;
200 	memmove(xdrs->x_private, addr, len);
201 	xdrs->x_private = (char *)xdrs->x_private + len;
202 	return (TRUE);
203 }
204 
205 static u_int
xdrmem_getpos(XDR * xdrs)206 xdrmem_getpos(XDR *xdrs)
207 {
208 
209 	/* XXX w/64-bit pointers, u_int not enough! */
210 	return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
211 }
212 
213 static bool_t
xdrmem_setpos(XDR * xdrs,u_int pos)214 xdrmem_setpos(XDR *xdrs, u_int pos)
215 {
216 	char *newaddr = xdrs->x_base + pos;
217 	char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
218 
219 	if ((long)newaddr > (long)lastaddr)
220 		return (FALSE);
221 	xdrs->x_private = newaddr;
222 	xdrs->x_handy = (int)((long)lastaddr - (long)newaddr);
223 	return (TRUE);
224 }
225 
226 static int32_t *
xdrmem_inline_aligned(XDR * xdrs,u_int len)227 xdrmem_inline_aligned(XDR *xdrs, u_int len)
228 {
229 	int32_t *buf = 0;
230 
231 	if (xdrs->x_handy >= len) {
232 		xdrs->x_handy -= len;
233 		buf = (int32_t *)xdrs->x_private;
234 		xdrs->x_private = (char *)xdrs->x_private + len;
235 	}
236 	return (buf);
237 }
238 
239 /* ARGSUSED */
240 static int32_t *
xdrmem_inline_unaligned(XDR * xdrs,u_int len)241 xdrmem_inline_unaligned(XDR *xdrs, u_int len)
242 {
243 
244 	return (0);
245 }
246