]> git.saurik.com Git - apple/libinfo.git/blame - rpc.subproj/xdr_mem.c
Libinfo-222.4.9.tar.gz
[apple/libinfo.git] / rpc.subproj / xdr_mem.c
CommitLineData
03fb6eb0
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
ad21edcc
A
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.
03fb6eb0
A
13 *
14 * The Original Code and all software distributed under the License are
ad21edcc 15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
03fb6eb0
A
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
ad21edcc
A
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.
03fb6eb0
A
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
c29f2fcc
A
24
25/* $NetBSD: xdr_mem.c,v 1.15 2000/01/22 22:19:18 mycroft Exp $ */
26
03fb6eb0
A
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.
c29f2fcc 34 *
03fb6eb0
A
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.
c29f2fcc 38 *
03fb6eb0
A
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.
c29f2fcc 42 *
03fb6eb0
A
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.
c29f2fcc 46 *
03fb6eb0
A
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.
c29f2fcc 50 *
03fb6eb0
A
51 * Sun Microsystems, Inc.
52 * 2550 Garcia Avenue
53 * Mountain View, California 94043
54 */
55
56#if defined(LIBC_SCCS) && !defined(lint)
c29f2fcc
A
57static char *sccsid = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
58static char *sccsid = "@(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC";
03fb6eb0 59#endif
c29f2fcc 60#include <sys/cdefs.h>
03fb6eb0
A
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
c29f2fcc
A
73#include <sys/types.h>
74
75#include <netinet/in.h>
03fb6eb0 76
3b7c7bd7 77#include <string.h>
c29f2fcc 78
03fb6eb0
A
79#include <rpc/types.h>
80#include <rpc/xdr.h>
03fb6eb0 81
c29f2fcc
A
82static void xdrmem_destroy(XDR *);
83static bool_t xdrmem_getlong_aligned(XDR *, long *);
84static bool_t xdrmem_putlong_aligned(XDR *, const long *);
85static bool_t xdrmem_getlong_unaligned(XDR *, long *);
86static bool_t xdrmem_putlong_unaligned(XDR *, const long *);
87static bool_t xdrmem_getbytes(XDR *, char *, u_int);
88static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
89/* XXX: w/64-bit pointers, u_int not enough! */
90static u_int xdrmem_getpos(XDR *);
91static bool_t xdrmem_setpos(XDR *, u_int);
92static int32_t *xdrmem_inline_aligned(XDR *, u_int);
93static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
94
95static 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
106static const struct xdr_ops xdrmem_ops_unaligned = {
107 xdrmem_getlong_unaligned,
108 xdrmem_putlong_unaligned,
03fb6eb0
A
109 xdrmem_getbytes,
110 xdrmem_putbytes,
111 xdrmem_getpos,
112 xdrmem_setpos,
c29f2fcc 113 xdrmem_inline_unaligned,
03fb6eb0
A
114 xdrmem_destroy
115};
116
117/*
118 * The procedure xdrmem_create initializes a stream descriptor for a
c29f2fcc 119 * memory buffer.
03fb6eb0
A
120 */
121void
122xdrmem_create(xdrs, addr, size, op)
c29f2fcc
A
123 XDR *xdrs;
124 char *addr;
03fb6eb0
A
125 u_int size;
126 enum xdr_op op;
127{
128
129 xdrs->x_op = op;
c29f2fcc
A
130 xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
131 ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
03fb6eb0
A
132 xdrs->x_private = xdrs->x_base = addr;
133 xdrs->x_handy = size;
134}
135
c29f2fcc 136/*ARGSUSED*/
03fb6eb0 137static void
c29f2fcc
A
138xdrmem_destroy(xdrs)
139 XDR *xdrs;
03fb6eb0 140{
c29f2fcc 141
03fb6eb0
A
142}
143
144static bool_t
c29f2fcc
A
145xdrmem_getlong_aligned(xdrs, lp)
146 XDR *xdrs;
03fb6eb0
A
147 long *lp;
148{
c29f2fcc
A
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
158static bool_t
159xdrmem_putlong_aligned(xdrs, lp)
160 XDR *xdrs;
161 const long *lp;
162{
163
164 if (xdrs->x_handy < sizeof(int32_t))
03fb6eb0 165 return (FALSE);
c29f2fcc
A
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);
03fb6eb0
A
169 return (TRUE);
170}
171
172static bool_t
c29f2fcc
A
173xdrmem_getlong_unaligned(xdrs, lp)
174 XDR *xdrs;
03fb6eb0
A
175 long *lp;
176{
c29f2fcc
A
177 u_int32_t l;
178
179 if (xdrs->x_handy < sizeof(int32_t))
03fb6eb0 180 return (FALSE);
c29f2fcc
A
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
188static bool_t
189xdrmem_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);
03fb6eb0
A
201 return (TRUE);
202}
203
204static bool_t
205xdrmem_getbytes(xdrs, addr, len)
c29f2fcc
A
206 XDR *xdrs;
207 char *addr;
208 u_int len;
03fb6eb0
A
209{
210
c29f2fcc 211 if (xdrs->x_handy < len)
03fb6eb0 212 return (FALSE);
c29f2fcc
A
213 xdrs->x_handy -= len;
214 memmove(addr, xdrs->x_private, len);
215 xdrs->x_private = (char *)xdrs->x_private + len;
03fb6eb0
A
216 return (TRUE);
217}
218
219static bool_t
220xdrmem_putbytes(xdrs, addr, len)
c29f2fcc
A
221 XDR *xdrs;
222 const char *addr;
223 u_int len;
03fb6eb0
A
224{
225
c29f2fcc 226 if (xdrs->x_handy < len)
03fb6eb0 227 return (FALSE);
c29f2fcc
A
228 xdrs->x_handy -= len;
229 memmove(xdrs->x_private, addr, len);
230 xdrs->x_private = (char *)xdrs->x_private + len;
03fb6eb0
A
231 return (TRUE);
232}
233
234static u_int
235xdrmem_getpos(xdrs)
c29f2fcc 236 XDR *xdrs;
03fb6eb0 237{
c29f2fcc
A
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);
03fb6eb0
A
241}
242
243static bool_t
244xdrmem_setpos(xdrs, pos)
c29f2fcc 245 XDR *xdrs;
03fb6eb0
A
246 u_int pos;
247{
c29f2fcc
A
248 char *newaddr = xdrs->x_base + pos;
249 char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
03fb6eb0 250
c29f2fcc 251 if (newaddr > lastaddr)
03fb6eb0
A
252 return (FALSE);
253 xdrs->x_private = newaddr;
c29f2fcc 254 xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
03fb6eb0
A
255 return (TRUE);
256}
257
c29f2fcc
A
258static int32_t *
259xdrmem_inline_aligned(xdrs, len)
260 XDR *xdrs;
261 u_int len;
03fb6eb0 262{
c29f2fcc 263 int32_t *buf = 0;
03fb6eb0
A
264
265 if (xdrs->x_handy >= len) {
266 xdrs->x_handy -= len;
c29f2fcc
A
267 buf = (int32_t *)xdrs->x_private;
268 xdrs->x_private = (char *)xdrs->x_private + len;
03fb6eb0
A
269 }
270 return (buf);
271}
c29f2fcc
A
272
273/* ARGSUSED */
274static int32_t *
275xdrmem_inline_unaligned(xdrs, len)
276 XDR *xdrs;
277 u_int len;
278{
279
280 return (0);
281}