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