xref: /minix3/lib/libc/rpc/xdr_mem.c (revision 6e5a113837816c382dd4f2a25924f16fbbb1c493)
1 /*	$NetBSD: xdr_mem.c,v 1.18 2012/03/20 17:14:50 matt 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 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char *sccsid = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
36 static char *sccsid = "@(#)xdr_mem.c	2.1 88/07/29 4.0 RPCSRC";
37 #else
38 __RCSID("$NetBSD: xdr_mem.c,v 1.18 2012/03/20 17:14:50 matt Exp $");
39 #endif
40 #endif
41 
42 /*
43  * xdr_mem.h, XDR implementation using memory buffers.
44  *
45  * Copyright (C) 1984, Sun Microsystems, Inc.
46  *
47  * If you have some data to be interpreted as external data representation
48  * or to be converted to external data representation in a memory buffer,
49  * then this is the package for you.
50  *
51  */
52 
53 #include "namespace.h"
54 
55 #include <sys/types.h>
56 
57 #include <netinet/in.h>
58 
59 #include <string.h>
60 
61 #include <rpc/types.h>
62 #include <rpc/xdr.h>
63 
64 #ifdef __weak_alias
65 __weak_alias(xdrmem_create,_xdrmem_create)
66 #endif
67 
68 static void xdrmem_destroy(XDR *);
69 static bool_t xdrmem_getlong_aligned(XDR *, long *);
70 static bool_t xdrmem_putlong_aligned(XDR *, const long *);
71 static bool_t xdrmem_getlong_unaligned(XDR *, long *);
72 static bool_t xdrmem_putlong_unaligned(XDR *, const long *);
73 static bool_t xdrmem_getbytes(XDR *, char *, u_int);
74 static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
75 /* XXX: w/64-bit pointers, u_int not enough! */
76 static u_int xdrmem_getpos(XDR *);
77 static bool_t xdrmem_setpos(XDR *, u_int);
78 static int32_t *xdrmem_inline_aligned(XDR *, u_int);
79 static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
80 
81 static const struct	xdr_ops xdrmem_ops_aligned = {
82 	xdrmem_getlong_aligned,
83 	xdrmem_putlong_aligned,
84 	xdrmem_getbytes,
85 	xdrmem_putbytes,
86 	xdrmem_getpos,
87 	xdrmem_setpos,
88 	xdrmem_inline_aligned,
89 	xdrmem_destroy,
90 	NULL,	/* xdrmem_control */
91 };
92 
93 static const struct	xdr_ops xdrmem_ops_unaligned = {
94 	xdrmem_getlong_unaligned,
95 	xdrmem_putlong_unaligned,
96 	xdrmem_getbytes,
97 	xdrmem_putbytes,
98 	xdrmem_getpos,
99 	xdrmem_setpos,
100 	xdrmem_inline_unaligned,
101 	xdrmem_destroy,
102 	NULL,	/* xdrmem_control */
103 };
104 
105 /*
106  * The procedure xdrmem_create initializes a stream descriptor for a
107  * memory buffer.
108  */
109 void
110 xdrmem_create(XDR *xdrs, char *addr, u_int size, enum xdr_op op)
111 {
112 
113 	xdrs->x_op = op;
114 	xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
115 	    ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
116 	xdrs->x_private = xdrs->x_base = addr;
117 	xdrs->x_handy = size;
118 }
119 
120 /*ARGSUSED*/
121 static void
122 xdrmem_destroy(XDR *xdrs)
123 {
124 
125 }
126 
127 static bool_t
128 xdrmem_getlong_aligned(XDR *xdrs, long *lp)
129 {
130 
131 	if (xdrs->x_handy < sizeof(int32_t))
132 		return (FALSE);
133 	xdrs->x_handy -= sizeof(int32_t);
134 	*lp = ntohl(*(u_int32_t *)xdrs->x_private);
135 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
136 	return (TRUE);
137 }
138 
139 static bool_t
140 xdrmem_putlong_aligned(XDR *xdrs, const long *lp)
141 {
142 
143 	if (xdrs->x_handy < sizeof(int32_t))
144 		return (FALSE);
145 	xdrs->x_handy -= sizeof(int32_t);
146 	*(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp);
147 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
148 	return (TRUE);
149 }
150 
151 static bool_t
152 xdrmem_getlong_unaligned(XDR *xdrs, long *lp)
153 {
154 	u_int32_t l;
155 
156 	if (xdrs->x_handy < sizeof(int32_t))
157 		return (FALSE);
158 	xdrs->x_handy -= sizeof(int32_t);
159 	memmove(&l, xdrs->x_private, sizeof(int32_t));
160 	*lp = ntohl(l);
161 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
162 	return (TRUE);
163 }
164 
165 static bool_t
166 xdrmem_putlong_unaligned(XDR *xdrs, const long *lp)
167 {
168 	u_int32_t l;
169 
170 	if (xdrs->x_handy < sizeof(int32_t))
171 		return (FALSE);
172 	xdrs->x_handy -= sizeof(int32_t);
173 	l = htonl((u_int32_t)*lp);
174 	memmove(xdrs->x_private, &l, sizeof(int32_t));
175 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
176 	return (TRUE);
177 }
178 
179 static bool_t
180 xdrmem_getbytes(XDR *xdrs, char *addr, u_int len)
181 {
182 
183 	if (xdrs->x_handy < len)
184 		return (FALSE);
185 	xdrs->x_handy -= len;
186 	memmove(addr, xdrs->x_private, len);
187 	xdrs->x_private = (char *)xdrs->x_private + len;
188 	return (TRUE);
189 }
190 
191 static bool_t
192 xdrmem_putbytes(XDR *xdrs, const char *addr, u_int len)
193 {
194 
195 	if (xdrs->x_handy < len)
196 		return (FALSE);
197 	xdrs->x_handy -= len;
198 	memmove(xdrs->x_private, addr, len);
199 	xdrs->x_private = (char *)xdrs->x_private + len;
200 	return (TRUE);
201 }
202 
203 static u_int
204 xdrmem_getpos(XDR *xdrs)
205 {
206 
207 	/* XXX w/64-bit pointers, u_int not enough! */
208 	return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
209 }
210 
211 static bool_t
212 xdrmem_setpos(XDR *xdrs, u_int pos)
213 {
214 	char *newaddr = xdrs->x_base + pos;
215 	char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
216 
217 	if ((long)newaddr > (long)lastaddr)
218 		return (FALSE);
219 	xdrs->x_private = newaddr;
220 	xdrs->x_handy = (int)((long)lastaddr - (long)newaddr);
221 	return (TRUE);
222 }
223 
224 static int32_t *
225 xdrmem_inline_aligned(XDR *xdrs, u_int len)
226 {
227 	int32_t *buf = 0;
228 
229 	if (xdrs->x_handy >= len) {
230 		xdrs->x_handy -= len;
231 		buf = (int32_t *)xdrs->x_private;
232 		xdrs->x_private = (char *)xdrs->x_private + len;
233 	}
234 	return (buf);
235 }
236 
237 /* ARGSUSED */
238 static int32_t *
239 xdrmem_inline_unaligned(XDR *xdrs, u_int len)
240 {
241 
242 	return (0);
243 }
244