Libinfo-221.tar.gz
[apple/libinfo.git] / rpc.subproj / xdr_mem.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25 /* $NetBSD: xdr_mem.c,v 1.15 2000/01/22 22:19:18 mycroft Exp $ */
26
27 /*
28 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
29 * unrestricted use provided that this legend is included on all tape
30 * media and as a part of the software program in whole or part. Users
31 * may copy or modify Sun RPC without charge, but are not authorized
32 * to license or distribute it to anyone else except as part of a product or
33 * program developed by the user.
34 *
35 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
36 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
37 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
38 *
39 * Sun RPC is provided with no support and without any obligation on the
40 * part of Sun Microsystems, Inc. to assist in its use, correction,
41 * modification or enhancement.
42 *
43 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
44 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
45 * OR ANY PART THEREOF.
46 *
47 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
48 * or profits or other special, indirect and consequential damages, even if
49 * Sun has been advised of the possibility of such damages.
50 *
51 * Sun Microsystems, Inc.
52 * 2550 Garcia Avenue
53 * Mountain View, California 94043
54 */
55
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char *sccsid = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
58 static char *sccsid = "@(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC";
59 #endif
60 #include <sys/cdefs.h>
61
62 /*
63 * xdr_mem.h, XDR implementation using memory buffers.
64 *
65 * Copyright (C) 1984, Sun Microsystems, Inc.
66 *
67 * If you have some data to be interpreted as external data representation
68 * or to be converted to external data representation in a memory buffer,
69 * then this is the package for you.
70 *
71 */
72
73 #include <sys/types.h>
74
75 #include <netinet/in.h>
76
77 #include <string.h>
78
79 #include <rpc/types.h>
80 #include <rpc/xdr.h>
81
82 static void xdrmem_destroy(XDR *);
83 static bool_t xdrmem_getlong_aligned(XDR *, long *);
84 static bool_t xdrmem_putlong_aligned(XDR *, const long *);
85 static bool_t xdrmem_getlong_unaligned(XDR *, long *);
86 static bool_t xdrmem_putlong_unaligned(XDR *, const long *);
87 static bool_t xdrmem_getbytes(XDR *, char *, u_int);
88 static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
89 /* XXX: w/64-bit pointers, u_int not enough! */
90 static u_int xdrmem_getpos(XDR *);
91 static bool_t xdrmem_setpos(XDR *, u_int);
92 static int32_t *xdrmem_inline_aligned(XDR *, u_int);
93 static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
94
95 static const struct xdr_ops xdrmem_ops_aligned = {
96 xdrmem_getlong_aligned,
97 xdrmem_putlong_aligned,
98 xdrmem_getbytes,
99 xdrmem_putbytes,
100 xdrmem_getpos,
101 xdrmem_setpos,
102 xdrmem_inline_aligned,
103 xdrmem_destroy
104 };
105
106 static const struct xdr_ops xdrmem_ops_unaligned = {
107 xdrmem_getlong_unaligned,
108 xdrmem_putlong_unaligned,
109 xdrmem_getbytes,
110 xdrmem_putbytes,
111 xdrmem_getpos,
112 xdrmem_setpos,
113 xdrmem_inline_unaligned,
114 xdrmem_destroy
115 };
116
117 /*
118 * The procedure xdrmem_create initializes a stream descriptor for a
119 * memory buffer.
120 */
121 void
122 xdrmem_create(xdrs, addr, size, op)
123 XDR *xdrs;
124 char *addr;
125 u_int size;
126 enum xdr_op op;
127 {
128
129 xdrs->x_op = op;
130 xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
131 ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
132 xdrs->x_private = xdrs->x_base = addr;
133 xdrs->x_handy = size;
134 }
135
136 /*ARGSUSED*/
137 static void
138 xdrmem_destroy(xdrs)
139 XDR *xdrs;
140 {
141
142 }
143
144 static bool_t
145 xdrmem_getlong_aligned(xdrs, lp)
146 XDR *xdrs;
147 long *lp;
148 {
149
150 if (xdrs->x_handy < sizeof(int32_t))
151 return (FALSE);
152 xdrs->x_handy -= sizeof(int32_t);
153 *lp = ntohl(*(u_int32_t *)xdrs->x_private);
154 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
155 return (TRUE);
156 }
157
158 static bool_t
159 xdrmem_putlong_aligned(xdrs, lp)
160 XDR *xdrs;
161 const long *lp;
162 {
163
164 if (xdrs->x_handy < sizeof(int32_t))
165 return (FALSE);
166 xdrs->x_handy -= sizeof(int32_t);
167 *(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp);
168 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
169 return (TRUE);
170 }
171
172 static bool_t
173 xdrmem_getlong_unaligned(xdrs, lp)
174 XDR *xdrs;
175 long *lp;
176 {
177 u_int32_t l;
178
179 if (xdrs->x_handy < sizeof(int32_t))
180 return (FALSE);
181 xdrs->x_handy -= sizeof(int32_t);
182 memmove(&l, xdrs->x_private, sizeof(int32_t));
183 *lp = ntohl(l);
184 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
185 return (TRUE);
186 }
187
188 static bool_t
189 xdrmem_putlong_unaligned(xdrs, lp)
190 XDR *xdrs;
191 const long *lp;
192 {
193 u_int32_t l;
194
195 if (xdrs->x_handy < sizeof(int32_t))
196 return (FALSE);
197 xdrs->x_handy -= sizeof(int32_t);
198 l = htonl((u_int32_t)*lp);
199 memmove(xdrs->x_private, &l, sizeof(int32_t));
200 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
201 return (TRUE);
202 }
203
204 static bool_t
205 xdrmem_getbytes(xdrs, addr, len)
206 XDR *xdrs;
207 char *addr;
208 u_int len;
209 {
210
211 if (xdrs->x_handy < len)
212 return (FALSE);
213 xdrs->x_handy -= len;
214 memmove(addr, xdrs->x_private, len);
215 xdrs->x_private = (char *)xdrs->x_private + len;
216 return (TRUE);
217 }
218
219 static bool_t
220 xdrmem_putbytes(xdrs, addr, len)
221 XDR *xdrs;
222 const char *addr;
223 u_int len;
224 {
225
226 if (xdrs->x_handy < len)
227 return (FALSE);
228 xdrs->x_handy -= len;
229 memmove(xdrs->x_private, addr, len);
230 xdrs->x_private = (char *)xdrs->x_private + len;
231 return (TRUE);
232 }
233
234 static u_int
235 xdrmem_getpos(xdrs)
236 XDR *xdrs;
237 {
238
239 /* XXX w/64-bit pointers, u_int not enough! */
240 return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
241 }
242
243 static bool_t
244 xdrmem_setpos(xdrs, pos)
245 XDR *xdrs;
246 u_int pos;
247 {
248 char *newaddr = xdrs->x_base + pos;
249 char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
250
251 if (newaddr > lastaddr)
252 return (FALSE);
253 xdrs->x_private = newaddr;
254 xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
255 return (TRUE);
256 }
257
258 static int32_t *
259 xdrmem_inline_aligned(xdrs, len)
260 XDR *xdrs;
261 u_int len;
262 {
263 int32_t *buf = 0;
264
265 if (xdrs->x_handy >= len) {
266 xdrs->x_handy -= len;
267 buf = (int32_t *)xdrs->x_private;
268 xdrs->x_private = (char *)xdrs->x_private + len;
269 }
270 return (buf);
271 }
272
273 /* ARGSUSED */
274 static int32_t *
275 xdrmem_inline_unaligned(xdrs, len)
276 XDR *xdrs;
277 u_int len;
278 {
279
280 return (0);
281 }