2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
25 /* $NetBSD: xdr_mem.c,v 1.15 2000/01/22 22:19:18 mycroft Exp $ */
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.
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.
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.
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.
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.
51 * Sun Microsystems, Inc.
53 * Mountain View, California 94043
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";
60 #include <sys/cdefs.h>
63 * xdr_mem.h, XDR implementation using memory buffers.
65 * Copyright (C) 1984, Sun Microsystems, Inc.
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.
73 #include <sys/types.h>
75 #include <netinet/in.h>
79 #include <rpc/types.h>
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
);
95 static const struct xdr_ops xdrmem_ops_aligned
= {
96 xdrmem_getlong_aligned
,
97 xdrmem_putlong_aligned
,
102 xdrmem_inline_aligned
,
106 static const struct xdr_ops xdrmem_ops_unaligned
= {
107 xdrmem_getlong_unaligned
,
108 xdrmem_putlong_unaligned
,
113 xdrmem_inline_unaligned
,
118 * The procedure xdrmem_create initializes a stream descriptor for a
122 xdrmem_create(xdrs
, addr
, size
, 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
;
145 xdrmem_getlong_aligned(xdrs
, lp
)
150 if (xdrs
->x_handy
< sizeof(int32_t))
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);
159 xdrmem_putlong_aligned(xdrs
, lp
)
164 if (xdrs
->x_handy
< sizeof(int32_t))
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);
173 xdrmem_getlong_unaligned(xdrs
, lp
)
179 if (xdrs
->x_handy
< sizeof(int32_t))
181 xdrs
->x_handy
-= sizeof(int32_t);
182 memmove(&l
, xdrs
->x_private
, sizeof(int32_t));
184 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
189 xdrmem_putlong_unaligned(xdrs
, lp
)
195 if (xdrs
->x_handy
< sizeof(int32_t))
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);
205 xdrmem_getbytes(xdrs
, addr
, len
)
211 if (xdrs
->x_handy
< len
)
213 xdrs
->x_handy
-= len
;
214 memmove(addr
, xdrs
->x_private
, len
);
215 xdrs
->x_private
= (char *)xdrs
->x_private
+ len
;
220 xdrmem_putbytes(xdrs
, addr
, len
)
226 if (xdrs
->x_handy
< len
)
228 xdrs
->x_handy
-= len
;
229 memmove(xdrs
->x_private
, addr
, len
);
230 xdrs
->x_private
= (char *)xdrs
->x_private
+ len
;
239 /* XXX w/64-bit pointers, u_int not enough! */
240 return (u_int
)((u_long
)xdrs
->x_private
- (u_long
)xdrs
->x_base
);
244 xdrmem_setpos(xdrs
, pos
)
248 char *newaddr
= xdrs
->x_base
+ pos
;
249 char *lastaddr
= (char *)xdrs
->x_private
+ xdrs
->x_handy
;
251 if (newaddr
> lastaddr
)
253 xdrs
->x_private
= newaddr
;
254 xdrs
->x_handy
= (u_int
)(lastaddr
- newaddr
); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
259 xdrmem_inline_aligned(xdrs
, len
)
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
;
275 xdrmem_inline_unaligned(xdrs
, len
)