xref: /freebsd/sys/xdr/xdr_mem.c (revision 2be1a816b9ff69588e55be0a84cbe2a31efc0f2f)
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 #define memmove(dst, src, len)	bcopy(src, dst, len)
58 
59 static void xdrmem_destroy(XDR *);
60 static bool_t xdrmem_getlong_aligned(XDR *, long *);
61 static bool_t xdrmem_putlong_aligned(XDR *, const long *);
62 static bool_t xdrmem_getlong_unaligned(XDR *, long *);
63 static bool_t xdrmem_putlong_unaligned(XDR *, const long *);
64 static bool_t xdrmem_getbytes(XDR *, char *, u_int);
65 static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
66 /* XXX: w/64-bit pointers, u_int not enough! */
67 static u_int xdrmem_getpos(XDR *);
68 static bool_t xdrmem_setpos(XDR *, u_int);
69 static int32_t *xdrmem_inline_aligned(XDR *, u_int);
70 static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
71 
72 static const struct	xdr_ops xdrmem_ops_aligned = {
73 	xdrmem_getlong_aligned,
74 	xdrmem_putlong_aligned,
75 	xdrmem_getbytes,
76 	xdrmem_putbytes,
77 	xdrmem_getpos,
78 	xdrmem_setpos,
79 	xdrmem_inline_aligned,
80 	xdrmem_destroy
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 };
93 
94 /*
95  * The procedure xdrmem_create initializes a stream descriptor for a
96  * memory buffer.
97  */
98 void
99 xdrmem_create(XDR *xdrs, char *addr, u_int size, enum xdr_op op)
100 {
101 
102 	xdrs->x_op = op;
103 	xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
104 	    ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
105 	xdrs->x_private = xdrs->x_base = addr;
106 	xdrs->x_handy = size;
107 }
108 
109 /*ARGSUSED*/
110 static void
111 xdrmem_destroy(XDR *xdrs)
112 {
113 
114 }
115 
116 static bool_t
117 xdrmem_getlong_aligned(XDR *xdrs, long *lp)
118 {
119 
120 	if (xdrs->x_handy < sizeof(int32_t))
121 		return (FALSE);
122 	xdrs->x_handy -= sizeof(int32_t);
123 	*lp = ntohl(*(u_int32_t *)xdrs->x_private);
124 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
125 	return (TRUE);
126 }
127 
128 static bool_t
129 xdrmem_putlong_aligned(XDR *xdrs, const long *lp)
130 {
131 
132 	if (xdrs->x_handy < sizeof(int32_t))
133 		return (FALSE);
134 	xdrs->x_handy -= sizeof(int32_t);
135 	*(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp);
136 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
137 	return (TRUE);
138 }
139 
140 static bool_t
141 xdrmem_getlong_unaligned(XDR *xdrs, long *lp)
142 {
143 	u_int32_t l;
144 
145 	if (xdrs->x_handy < sizeof(int32_t))
146 		return (FALSE);
147 	xdrs->x_handy -= sizeof(int32_t);
148 	memmove(&l, xdrs->x_private, sizeof(int32_t));
149 	*lp = ntohl(l);
150 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
151 	return (TRUE);
152 }
153 
154 static bool_t
155 xdrmem_putlong_unaligned(XDR *xdrs, const long *lp)
156 {
157 	u_int32_t l;
158 
159 	if (xdrs->x_handy < sizeof(int32_t))
160 		return (FALSE);
161 	xdrs->x_handy -= sizeof(int32_t);
162 	l = htonl((u_int32_t)*lp);
163 	memmove(xdrs->x_private, &l, sizeof(int32_t));
164 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
165 	return (TRUE);
166 }
167 
168 static bool_t
169 xdrmem_getbytes(XDR *xdrs, char *addr, u_int len)
170 {
171 
172 	if (xdrs->x_handy < len)
173 		return (FALSE);
174 	xdrs->x_handy -= len;
175 	memmove(addr, xdrs->x_private, len);
176 	xdrs->x_private = (char *)xdrs->x_private + len;
177 	return (TRUE);
178 }
179 
180 static bool_t
181 xdrmem_putbytes(XDR *xdrs, const char *addr, u_int len)
182 {
183 
184 	if (xdrs->x_handy < len)
185 		return (FALSE);
186 	xdrs->x_handy -= len;
187 	memmove(xdrs->x_private, addr, len);
188 	xdrs->x_private = (char *)xdrs->x_private + len;
189 	return (TRUE);
190 }
191 
192 static u_int
193 xdrmem_getpos(XDR *xdrs)
194 {
195 
196 	/* XXX w/64-bit pointers, u_int not enough! */
197 	return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
198 }
199 
200 static bool_t
201 xdrmem_setpos(XDR *xdrs, u_int pos)
202 {
203 	char *newaddr = xdrs->x_base + pos;
204 	char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
205 
206 	if (newaddr > lastaddr)
207 		return (FALSE);
208 	xdrs->x_private = newaddr;
209 	xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
210 	return (TRUE);
211 }
212 
213 static int32_t *
214 xdrmem_inline_aligned(XDR *xdrs, u_int len)
215 {
216 	int32_t *buf = 0;
217 
218 	if (xdrs->x_handy >= len) {
219 		xdrs->x_handy -= len;
220 		buf = (int32_t *)xdrs->x_private;
221 		xdrs->x_private = (char *)xdrs->x_private + len;
222 	}
223 	return (buf);
224 }
225 
226 /* ARGSUSED */
227 static int32_t *
228 xdrmem_inline_unaligned(XDR *xdrs, u_int len)
229 {
230 
231 	return (0);
232 }
233