xref: /freebsd/sys/xdr/xdr_mem.c (revision 5418b1ebdfa0c6118894ce0e91115c3c13f3850d)
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 #include <sys/cdefs.h>
33 /*
34  * xdr_mem.h, XDR implementation using memory buffers.
35  *
36  * Copyright (C) 1984, Sun Microsystems, Inc.
37  *
38  * If you have some data to be interpreted as external data representation
39  * or to be converted to external data representation in a memory buffer,
40  * then this is the package for you.
41  *
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 
49 #include <rpc/types.h>
50 #include <rpc/xdr.h>
51 
52 static void xdrmem_destroy(XDR *);
53 static bool_t xdrmem_getlong_aligned(XDR *, long *);
54 static bool_t xdrmem_putlong_aligned(XDR *, const long *);
55 static bool_t xdrmem_getlong_unaligned(XDR *, long *);
56 static bool_t xdrmem_putlong_unaligned(XDR *, const long *);
57 static bool_t xdrmem_getbytes(XDR *, char *, u_int);
58 static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
59 static bool_t xdrmem_putmbuf(XDR *, struct mbuf *);
60 /* XXX: w/64-bit pointers, u_int not enough! */
61 static u_int xdrmem_getpos(XDR *);
62 static bool_t xdrmem_setpos(XDR *, u_int);
63 static int32_t *xdrmem_inline_aligned(XDR *, u_int);
64 static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
65 static bool_t xdrmem_control(XDR *xdrs, int request, void *info);
66 
67 static const struct	xdr_ops xdrmem_ops_aligned = {
68 	.x_getlong =	xdrmem_getlong_aligned,
69 	.x_putlong =	xdrmem_putlong_aligned,
70 	.x_getbytes =	xdrmem_getbytes,
71 	.x_putbytes =	xdrmem_putbytes,
72 	.x_putmbuf =	xdrmem_putmbuf,
73 	.x_getpostn =	xdrmem_getpos,
74 	.x_setpostn =	xdrmem_setpos,
75 	.x_inline =	xdrmem_inline_aligned,
76 	.x_destroy = 	xdrmem_destroy,
77 	.x_control =	xdrmem_control,
78 };
79 
80 static const struct	xdr_ops xdrmem_ops_unaligned = {
81 	.x_getlong =	xdrmem_getlong_unaligned,
82 	.x_putlong =	xdrmem_putlong_unaligned,
83 	.x_getbytes =	xdrmem_getbytes,
84 	.x_putbytes =	xdrmem_putbytes,
85 	.x_putmbuf =	xdrmem_putmbuf,
86 	.x_getpostn =	xdrmem_getpos,
87 	.x_setpostn =	xdrmem_setpos,
88 	.x_inline =	xdrmem_inline_unaligned,
89 	.x_destroy =	xdrmem_destroy,
90 	.x_control =	xdrmem_control
91 };
92 
93 /*
94  * The procedure xdrmem_create initializes a stream descriptor for a
95  * memory buffer.
96  */
97 void
xdrmem_create(XDR * xdrs,char * addr,u_int size,enum xdr_op op)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
xdrmem_destroy(XDR * xdrs)110 xdrmem_destroy(XDR *xdrs)
111 {
112 
113 }
114 
115 static bool_t
xdrmem_getlong_aligned(XDR * xdrs,long * lp)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
xdrmem_putlong_aligned(XDR * xdrs,const long * lp)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
xdrmem_getlong_unaligned(XDR * xdrs,long * lp)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
xdrmem_putlong_unaligned(XDR * xdrs,const long * lp)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
xdrmem_getbytes(XDR * xdrs,char * addr,u_int len)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
xdrmem_putbytes(XDR * xdrs,const char * addr,u_int len)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 /*
192  * Append mbuf.  May fail if not enough space.  Caller owns the mbuf.
193  */
194 static bool_t
xdrmem_putmbuf(XDR * xdrs,struct mbuf * m)195 xdrmem_putmbuf(XDR *xdrs, struct mbuf *m)
196 {
197 	u_int len;
198 
199 	if (__predict_false(m == NULL))
200 		return (TRUE);
201 
202 	len = m_length(m, NULL);
203 
204 	if (__predict_false(xdrs->x_handy < len))
205 		return (FALSE);
206 	xdrs->x_handy -= len;
207 	m_copydata(m, 0, len, xdrs->x_private);
208 	xdrs->x_private = (char *)xdrs->x_private + len;
209 	return (TRUE);
210 }
211 
212 static u_int
xdrmem_getpos(XDR * xdrs)213 xdrmem_getpos(XDR *xdrs)
214 {
215 
216 	/* XXX w/64-bit pointers, u_int not enough! */
217 	return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
218 }
219 
220 static bool_t
xdrmem_setpos(XDR * xdrs,u_int pos)221 xdrmem_setpos(XDR *xdrs, u_int pos)
222 {
223 	char *newaddr = xdrs->x_base + pos;
224 	char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
225 
226 	if (newaddr > lastaddr)
227 		return (FALSE);
228 	xdrs->x_private = newaddr;
229 	xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
230 	return (TRUE);
231 }
232 
233 static int32_t *
xdrmem_inline_aligned(XDR * xdrs,u_int len)234 xdrmem_inline_aligned(XDR *xdrs, u_int len)
235 {
236 	int32_t *buf = NULL;
237 
238 	if (xdrs->x_handy >= len) {
239 		xdrs->x_handy -= len;
240 		buf = (int32_t *)xdrs->x_private;
241 		xdrs->x_private = (char *)xdrs->x_private + len;
242 	}
243 	return (buf);
244 }
245 
246 /* ARGSUSED */
247 static int32_t *
xdrmem_inline_unaligned(XDR * xdrs,u_int len)248 xdrmem_inline_unaligned(XDR *xdrs, u_int len)
249 {
250 
251 	return (0);
252 }
253 
254 static bool_t
xdrmem_control(XDR * xdrs,int request,void * info)255 xdrmem_control(XDR *xdrs, int request, void *info)
256 {
257 	xdr_bytesrec *xptr;
258 	int32_t *l;
259 	int len;
260 
261 	switch (request) {
262 	case XDR_GET_BYTES_AVAIL:
263 		xptr = (xdr_bytesrec *)info;
264 		xptr->xc_is_last_record = TRUE;
265 		xptr->xc_num_avail = xdrs->x_handy;
266 		return (TRUE);
267 
268 	case XDR_PEEK:
269 		/*
270 		 * Return the next 4 byte unit in the XDR stream.
271 		 */
272 		if (xdrs->x_handy < sizeof (int32_t))
273 			return (FALSE);
274 		l = (int32_t *)info;
275 		*l = (int32_t)ntohl((uint32_t)
276 		    (*((int32_t *)(xdrs->x_private))));
277 		return (TRUE);
278 
279 	case XDR_SKIPBYTES:
280 		/*
281 		 * Skip the next N bytes in the XDR stream.
282 		 */
283 		l = (int32_t *)info;
284 		len = RNDUP((int)(*l));
285 		if (xdrs->x_handy < len)
286 			return (FALSE);
287 		xdrs->x_handy -= len;
288 		xdrs->x_private = (char *)xdrs->x_private + len;
289 		return (TRUE);
290 	}
291 	return (FALSE);
292 }
293