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>
76 #include <netinet/in.h>
80 #include <rpc/types.h>
86 #define xdrlong_t long
89 static void xdrmem_destroy(XDR
*);
90 static bool_t
xdrmem_getlong_aligned(XDR
*, xdrlong_t
*);
91 static bool_t
xdrmem_putlong_aligned(XDR
*, const xdrlong_t
*);
92 static bool_t
xdrmem_getlong_unaligned(XDR
*, xdrlong_t
*);
93 static bool_t
xdrmem_putlong_unaligned(XDR
*, const xdrlong_t
*);
94 static bool_t
xdrmem_getbytes(XDR
*, char *, u_int
);
95 static bool_t
xdrmem_putbytes(XDR
*, const char *, u_int
);
96 /* XXX: w/64-bit pointers, u_int not enough! */
97 static u_int
xdrmem_getpos(XDR
*);
98 static bool_t
xdrmem_setpos(XDR
*, u_int
);
99 static int32_t *xdrmem_inline_aligned(XDR
*, u_int
);
100 static int32_t *xdrmem_inline_unaligned(XDR
*, u_int
);
102 static const struct xdr_ops xdrmem_ops_aligned
= {
103 xdrmem_getlong_aligned
,
104 xdrmem_putlong_aligned
,
109 xdrmem_inline_aligned
,
113 static const struct xdr_ops xdrmem_ops_unaligned
= {
114 xdrmem_getlong_unaligned
,
115 xdrmem_putlong_unaligned
,
120 xdrmem_inline_unaligned
,
125 * The procedure xdrmem_create initializes a stream descriptor for a
129 xdrmem_create(xdrs
, addr
, size
, op
)
136 xdrs
->x_ops
= ((unsigned long)addr
& (sizeof(int32_t) - 1))
137 ? &xdrmem_ops_unaligned
: &xdrmem_ops_aligned
;
138 xdrs
->x_private
= xdrs
->x_base
= addr
;
139 xdrs
->x_handy
= size
;
150 xdrmem_getlong_aligned(xdrs
, lp
)
154 if (xdrs
->x_handy
< sizeof(int32_t))
156 xdrs
->x_handy
-= sizeof(int32_t);
157 *lp
= ntohl(*(u_int32_t
*)xdrs
->x_private
);
158 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
163 xdrmem_putlong_aligned(xdrs
, lp
)
167 if (xdrs
->x_handy
< sizeof(int32_t))
169 xdrs
->x_handy
-= sizeof(int32_t);
170 *(u_int32_t
*)xdrs
->x_private
= htonl((u_int32_t
)*lp
);
171 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
176 xdrmem_getlong_unaligned(xdrs
, lp
)
182 if (xdrs
->x_handy
< sizeof(int32_t))
184 xdrs
->x_handy
-= sizeof(int32_t);
185 memmove(&l
, xdrs
->x_private
, sizeof(int32_t));
187 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
192 xdrmem_putlong_unaligned(xdrs
, lp
)
198 if (xdrs
->x_handy
< sizeof(int32_t))
200 xdrs
->x_handy
-= sizeof(int32_t);
201 l
= htonl((u_int32_t
)*lp
);
202 memmove(xdrs
->x_private
, &l
, sizeof(int32_t));
203 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
208 xdrmem_getbytes(xdrs
, addr
, len
)
213 if (xdrs
->x_handy
< len
)
215 xdrs
->x_handy
-= len
;
216 memmove(addr
, xdrs
->x_private
, len
);
217 xdrs
->x_private
= (char *)xdrs
->x_private
+ len
;
222 xdrmem_putbytes(xdrs
, addr
, len
)
227 if (xdrs
->x_handy
< len
)
229 xdrs
->x_handy
-= len
;
230 memmove(xdrs
->x_private
, addr
, len
);
231 xdrs
->x_private
= (char *)xdrs
->x_private
+ len
;
242 delta
= xdrs
->x_private
- (void *)(xdrs
->x_base
);
244 if (delta
> UINT32_MAX
) return -1;
252 xdrmem_setpos(xdrs
, pos
)
257 char *newaddr
= xdrs
->x_base
+ pos
;
258 char *lastaddr
= (char *)xdrs
->x_private
+ xdrs
->x_handy
;
260 if (newaddr
> lastaddr
)
262 xdrs
->x_private
= newaddr
;
263 delta
= lastaddr
- newaddr
;
265 if (delta
> UINT32_MAX
) return (FALSE
);
267 xdrs
->x_handy
= delta
;
272 xdrmem_inline_aligned(xdrs
, len
)
278 if (xdrs
->x_handy
>= len
) {
279 xdrs
->x_handy
-= len
;
280 buf
= (int32_t *)xdrs
->x_private
;
281 xdrs
->x_private
= (char *)xdrs
->x_private
+ len
;
288 xdrmem_inline_unaligned(xdrs
, len
)