xref: /dragonfly/lib/libc/xdr/xdr_mem.c (revision 678e8cc6)
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  *
29  * @(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro
30  * @(#)xdr_mem.c	2.1 88/07/29 4.0 RPCSRC
31  * $NetBSD: xdr_mem.c,v 1.15 2000/01/22 22:19:18 mycroft Exp $
32  * $FreeBSD: src/lib/libc/xdr/xdr_mem.c,v 1.13 2004/10/16 06:32:43 obrien Exp $
33  */
34 
35 /*
36  * xdr_mem.h, XDR implementation using memory buffers.
37  *
38  * Copyright (C) 1984, Sun Microsystems, Inc.
39  *
40  * If you have some data to be interpreted as external data representation
41  * or to be converted to external data representation in a memory buffer,
42  * then this is the package for you.
43  *
44  */
45 
46 #include "namespace.h"
47 #include <sys/types.h>
48 
49 #include <netinet/in.h>
50 
51 #include <string.h>
52 
53 #include <rpc/types.h>
54 #include <rpc/xdr.h>
55 #include "un-namespace.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 
70 static const struct	xdr_ops xdrmem_ops_aligned = {
71 	xdrmem_getlong_aligned,
72 	xdrmem_putlong_aligned,
73 	xdrmem_getbytes,
74 	xdrmem_putbytes,
75 	xdrmem_getpos,
76 	xdrmem_setpos,
77 	xdrmem_inline_aligned,
78 	xdrmem_destroy
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 };
91 
92 /*
93  * The procedure xdrmem_create initializes a stream descriptor for a
94  * memory buffer.
95  */
96 void
97 xdrmem_create(XDR *xdrs, char *addr, u_int size, enum xdr_op op)
98 {
99 
100 	xdrs->x_op = op;
101 	xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
102 	    ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
103 	xdrs->x_private = xdrs->x_base = addr;
104 	xdrs->x_handy = size;
105 }
106 
107 /*ARGSUSED*/
108 static void
109 xdrmem_destroy(XDR *xdrs)
110 {
111 
112 }
113 
114 static bool_t
115 xdrmem_getlong_aligned(XDR *xdrs, long *lp)
116 {
117 
118 	if (xdrs->x_handy < sizeof(int32_t))
119 		return (FALSE);
120 	xdrs->x_handy -= sizeof(int32_t);
121 	*lp = ntohl(*(u_int32_t *)xdrs->x_private);
122 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
123 	return (TRUE);
124 }
125 
126 static bool_t
127 xdrmem_putlong_aligned(XDR *xdrs, const long *lp)
128 {
129 
130 	if (xdrs->x_handy < sizeof(int32_t))
131 		return (FALSE);
132 	xdrs->x_handy -= sizeof(int32_t);
133 	*(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp);
134 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
135 	return (TRUE);
136 }
137 
138 static bool_t
139 xdrmem_getlong_unaligned(XDR *xdrs, long *lp)
140 {
141 	u_int32_t l;
142 
143 	if (xdrs->x_handy < sizeof(int32_t))
144 		return (FALSE);
145 	xdrs->x_handy -= sizeof(int32_t);
146 	memmove(&l, xdrs->x_private, sizeof(int32_t));
147 	*lp = ntohl(l);
148 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
149 	return (TRUE);
150 }
151 
152 static bool_t
153 xdrmem_putlong_unaligned(XDR *xdrs, const long *lp)
154 {
155 	u_int32_t l;
156 
157 	if (xdrs->x_handy < sizeof(int32_t))
158 		return (FALSE);
159 	xdrs->x_handy -= sizeof(int32_t);
160 	l = htonl((u_int32_t)*lp);
161 	memmove(xdrs->x_private, &l, sizeof(int32_t));
162 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
163 	return (TRUE);
164 }
165 
166 static bool_t
167 xdrmem_getbytes(XDR *xdrs, char *addr, u_int len)
168 {
169 
170 	if (xdrs->x_handy < len)
171 		return (FALSE);
172 	xdrs->x_handy -= len;
173 	memmove(addr, xdrs->x_private, len);
174 	xdrs->x_private = (char *)xdrs->x_private + len;
175 	return (TRUE);
176 }
177 
178 static bool_t
179 xdrmem_putbytes(XDR *xdrs, const char *addr, u_int len)
180 {
181 
182 	if (xdrs->x_handy < len)
183 		return (FALSE);
184 	xdrs->x_handy -= len;
185 	memmove(xdrs->x_private, addr, len);
186 	xdrs->x_private = (char *)xdrs->x_private + len;
187 	return (TRUE);
188 }
189 
190 static u_int
191 xdrmem_getpos(XDR *xdrs)
192 {
193 
194 	/* XXX w/64-bit pointers, u_int not enough! */
195 	return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
196 }
197 
198 static bool_t
199 xdrmem_setpos(XDR *xdrs, u_int pos)
200 {
201 	char *newaddr = xdrs->x_base + pos;
202 	char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
203 
204 	if (newaddr > lastaddr)
205 		return (FALSE);
206 	xdrs->x_private = newaddr;
207 	xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
208 	return (TRUE);
209 }
210 
211 static int32_t *
212 xdrmem_inline_aligned(XDR *xdrs, u_int len)
213 {
214 	int32_t *buf = NULL;
215 
216 	if (xdrs->x_handy >= len) {
217 		xdrs->x_handy -= len;
218 		buf = (int32_t *)xdrs->x_private;
219 		xdrs->x_private = (char *)xdrs->x_private + len;
220 	}
221 	return (buf);
222 }
223 
224 /* ARGSUSED */
225 static int32_t *
226 xdrmem_inline_unaligned(XDR *xdrs __unused, u_int len __unused)
227 {
228 
229 	return (0);
230 }
231