xref: /freebsd/sys/xdr/xdr_mem.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1 /*	$NetBSD: xdr_mem.c,v 1.15 2000/01/22 22:19:18 mycroft Exp $	*/
2 
3 /*
4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char *sccsid2 = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
34 static char *sccsid = "@(#)xdr_mem.c	2.1 88/07/29 4.0 RPCSRC";
35 #endif
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 /*
40  * xdr_mem.h, XDR implementation using memory buffers.
41  *
42  * Copyright (C) 1984, Sun Microsystems, Inc.
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 <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/malloc.h>
53 
54 #include <rpc/types.h>
55 #include <rpc/xdr.h>
56 
57 static void xdrmem_destroy(XDR *);
58 static bool_t xdrmem_getlong_aligned(XDR *, long *);
59 static bool_t xdrmem_putlong_aligned(XDR *, const long *);
60 static bool_t xdrmem_getlong_unaligned(XDR *, long *);
61 static bool_t xdrmem_putlong_unaligned(XDR *, const long *);
62 static bool_t xdrmem_getbytes(XDR *, char *, u_int);
63 static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
64 /* XXX: w/64-bit pointers, u_int not enough! */
65 static u_int xdrmem_getpos(XDR *);
66 static bool_t xdrmem_setpos(XDR *, u_int);
67 static int32_t *xdrmem_inline_aligned(XDR *, u_int);
68 static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
69 static bool_t xdrmem_control(XDR *xdrs, int request, void *info);
70 
71 static const struct	xdr_ops xdrmem_ops_aligned = {
72 	xdrmem_getlong_aligned,
73 	xdrmem_putlong_aligned,
74 	xdrmem_getbytes,
75 	xdrmem_putbytes,
76 	xdrmem_getpos,
77 	xdrmem_setpos,
78 	xdrmem_inline_aligned,
79 	xdrmem_destroy,
80 	xdrmem_control
81 };
82 
83 static const struct	xdr_ops xdrmem_ops_unaligned = {
84 	xdrmem_getlong_unaligned,
85 	xdrmem_putlong_unaligned,
86 	xdrmem_getbytes,
87 	xdrmem_putbytes,
88 	xdrmem_getpos,
89 	xdrmem_setpos,
90 	xdrmem_inline_unaligned,
91 	xdrmem_destroy,
92 	xdrmem_control
93 };
94 
95 /*
96  * The procedure xdrmem_create initializes a stream descriptor for a
97  * memory buffer.
98  */
99 void
100 xdrmem_create(XDR *xdrs, char *addr, u_int size, enum xdr_op op)
101 {
102 
103 	xdrs->x_op = op;
104 	xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
105 	    ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
106 	xdrs->x_private = xdrs->x_base = addr;
107 	xdrs->x_handy = size;
108 }
109 
110 /*ARGSUSED*/
111 static void
112 xdrmem_destroy(XDR *xdrs)
113 {
114 
115 }
116 
117 static bool_t
118 xdrmem_getlong_aligned(XDR *xdrs, long *lp)
119 {
120 
121 	if (xdrs->x_handy < sizeof(int32_t))
122 		return (FALSE);
123 	xdrs->x_handy -= sizeof(int32_t);
124 	*lp = ntohl(*(uint32_t *)xdrs->x_private);
125 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
126 	return (TRUE);
127 }
128 
129 static bool_t
130 xdrmem_putlong_aligned(XDR *xdrs, const long *lp)
131 {
132 
133 	if (xdrs->x_handy < sizeof(int32_t))
134 		return (FALSE);
135 	xdrs->x_handy -= sizeof(int32_t);
136 	*(uint32_t *)xdrs->x_private = htonl((uint32_t)*lp);
137 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
138 	return (TRUE);
139 }
140 
141 static bool_t
142 xdrmem_getlong_unaligned(XDR *xdrs, long *lp)
143 {
144 	uint32_t l;
145 
146 	if (xdrs->x_handy < sizeof(int32_t))
147 		return (FALSE);
148 	xdrs->x_handy -= sizeof(int32_t);
149 	memmove(&l, xdrs->x_private, sizeof(int32_t));
150 	*lp = ntohl(l);
151 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
152 	return (TRUE);
153 }
154 
155 static bool_t
156 xdrmem_putlong_unaligned(XDR *xdrs, const long *lp)
157 {
158 	uint32_t l;
159 
160 	if (xdrs->x_handy < sizeof(int32_t))
161 		return (FALSE);
162 	xdrs->x_handy -= sizeof(int32_t);
163 	l = htonl((uint32_t)*lp);
164 	memmove(xdrs->x_private, &l, sizeof(int32_t));
165 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
166 	return (TRUE);
167 }
168 
169 static bool_t
170 xdrmem_getbytes(XDR *xdrs, char *addr, u_int len)
171 {
172 
173 	if (xdrs->x_handy < len)
174 		return (FALSE);
175 	xdrs->x_handy -= len;
176 	memmove(addr, xdrs->x_private, len);
177 	xdrs->x_private = (char *)xdrs->x_private + len;
178 	return (TRUE);
179 }
180 
181 static bool_t
182 xdrmem_putbytes(XDR *xdrs, const char *addr, u_int len)
183 {
184 
185 	if (xdrs->x_handy < len)
186 		return (FALSE);
187 	xdrs->x_handy -= len;
188 	memmove(xdrs->x_private, addr, len);
189 	xdrs->x_private = (char *)xdrs->x_private + len;
190 	return (TRUE);
191 }
192 
193 static u_int
194 xdrmem_getpos(XDR *xdrs)
195 {
196 
197 	/* XXX w/64-bit pointers, u_int not enough! */
198 	return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
199 }
200 
201 static bool_t
202 xdrmem_setpos(XDR *xdrs, u_int pos)
203 {
204 	char *newaddr = xdrs->x_base + pos;
205 	char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
206 
207 	if (newaddr > lastaddr)
208 		return (FALSE);
209 	xdrs->x_private = newaddr;
210 	xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
211 	return (TRUE);
212 }
213 
214 static int32_t *
215 xdrmem_inline_aligned(XDR *xdrs, u_int len)
216 {
217 	int32_t *buf = NULL;
218 
219 	if (xdrs->x_handy >= len) {
220 		xdrs->x_handy -= len;
221 		buf = (int32_t *)xdrs->x_private;
222 		xdrs->x_private = (char *)xdrs->x_private + len;
223 	}
224 	return (buf);
225 }
226 
227 /* ARGSUSED */
228 static int32_t *
229 xdrmem_inline_unaligned(XDR *xdrs, u_int len)
230 {
231 
232 	return (0);
233 }
234 
235 static bool_t
236 xdrmem_control(XDR *xdrs, int request, void *info)
237 {
238 	xdr_bytesrec *xptr;
239 	int32_t *l;
240 	int len;
241 
242 	switch (request) {
243 	case XDR_GET_BYTES_AVAIL:
244 		xptr = (xdr_bytesrec *)info;
245 		xptr->xc_is_last_record = TRUE;
246 		xptr->xc_num_avail = xdrs->x_handy;
247 		return (TRUE);
248 
249 	case XDR_PEEK:
250 		/*
251 		 * Return the next 4 byte unit in the XDR stream.
252 		 */
253 		if (xdrs->x_handy < sizeof (int32_t))
254 			return (FALSE);
255 		l = (int32_t *)info;
256 		*l = (int32_t)ntohl((uint32_t)
257 		    (*((int32_t *)(xdrs->x_private))));
258 		return (TRUE);
259 
260 	case XDR_SKIPBYTES:
261 		/*
262 		 * Skip the next N bytes in the XDR stream.
263 		 */
264 		l = (int32_t *)info;
265 		len = RNDUP((int)(*l));
266 		if (xdrs->x_handy < len)
267 			return (FALSE);
268 		xdrs->x_handy -= len;
269 		xdrs->x_private = (char *)xdrs->x_private + len;
270 		return (TRUE);
271 	}
272 	return (FALSE);
273 }
274