2 * Copyright (c) 2006,2011,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 /* $NetBSD: xdr_mem.c,v 1.15 2000/01/22 22:19:18 mycroft Exp $ */
27 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
28 * unrestricted use provided that this legend is included on all tape
29 * media and as a part of the software program in whole or part. Users
30 * may copy or modify Sun RPC without charge, but are not authorized
31 * to license or distribute it to anyone else except as part of a product or
32 * program developed by the user.
34 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
35 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
36 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
38 * Sun RPC is provided with no support and without any obligation on the
39 * part of Sun Microsystems, Inc. to assist in its use, correction,
40 * modification or enhancement.
42 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
43 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
44 * OR ANY PART THEREOF.
46 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
47 * or profits or other special, indirect and consequential damages, even if
48 * Sun has been advised of the possibility of such damages.
50 * Sun Microsystems, Inc.
52 * Mountain View, California 94043
55 #if defined(LIBC_SCCS) && !defined(lint)
56 static char *sccsid
= "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
57 static char *sccsid
= "@(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC";
59 #include <sys/cdefs.h>
62 * xdr_mem.h, XDR implementation using memory buffers.
64 * Copyright (C) 1984, Sun Microsystems, Inc.
66 * If you have some data to be interpreted as external data representation
67 * or to be converted to external data representation in a memory buffer,
68 * then this is the package for you.
72 #include <sys/types.h>
74 #include <netinet/in.h>
80 static void sec_xdrmem_destroy(XDR
*);
83 #define long_callback_ptr_t int *
85 #define long_callback_ptr_t long *
87 static bool_t
sec_xdrmem_getlong_aligned(XDR
*, long_callback_ptr_t
);
88 static bool_t
sec_xdrmem_putlong_aligned(XDR
*, const long_callback_ptr_t
);
89 static bool_t
sec_xdrmem_getlong_unaligned(XDR
*, long_callback_ptr_t
);
90 static bool_t
sec_xdrmem_putlong_unaligned(XDR
*, const long_callback_ptr_t
);
92 static bool_t
sec_xdrmem_getbytes(XDR
*, char *, u_int
);
93 static bool_t
sec_xdrmem_putbytes(XDR
*, const char *, u_int
);
94 /* XXX: w/64-bit pointers, u_int not enough! */
95 static u_int
sec_xdrmem_getpos(XDR
*);
96 static bool_t
sec_xdrmem_setpos(XDR
*, u_int
);
97 static int32_t *sec_xdrmem_inline_aligned(XDR
*, u_int
);
98 static int32_t *sec_xdrmem_inline_unaligned(XDR
*, u_int
);
100 static const struct xdr_ops sec_xdrmem_ops_aligned
= {
101 sec_xdrmem_getlong_aligned
,
102 sec_xdrmem_putlong_aligned
,
107 sec_xdrmem_inline_aligned
,
111 static const struct xdr_ops sec_xdrmem_ops_unaligned
= {
112 sec_xdrmem_getlong_unaligned
,
113 sec_xdrmem_putlong_unaligned
,
118 sec_xdrmem_inline_unaligned
,
123 * The procedure sec_xdrmem_create initializes a stream descriptor for a
127 sec_xdrmem_create(xdrs
, addr
, size
, op
)
134 xdrs
->x_ops
= ((unsigned long)addr
& (sizeof(int32_t) - 1))
135 ? &sec_xdrmem_ops_unaligned
: &sec_xdrmem_ops_aligned
;
136 xdrs
->x_private
= xdrs
->x_base
= addr
;
137 xdrs
->x_public
= NULL
;
138 xdrs
->x_handy
= size
;
143 sec_xdrmem_destroy(xdrs
)
150 sec_xdrmem_getlong_aligned(xdrs
, lp
)
152 long_callback_ptr_t lp
;
154 if (xdrs
->x_handy
< sizeof(int32_t))
156 xdrs
->x_handy
-= sizeof(int32_t);
157 if (lp
) *lp
= ntohl(*(u_int32_t
*)xdrs
->x_private
);
158 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
163 sec_xdrmem_putlong_aligned(xdrs
, lp
)
165 const long_callback_ptr_t lp
;
167 if (xdrs
->x_handy
< sizeof(int32_t))
169 xdrs
->x_handy
-= sizeof(int32_t);
170 if (lp
) *(u_int32_t
*)xdrs
->x_private
= htonl((u_int32_t
)*lp
);
171 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
176 sec_xdrmem_getlong_unaligned(xdrs
, lp
)
178 long_callback_ptr_t 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));
186 if (lp
) *lp
= ntohl(l
);
187 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
192 sec_xdrmem_putlong_unaligned(xdrs
, lp
)
194 const long_callback_ptr_t lp
;
198 if (xdrs
->x_handy
< sizeof(int32_t))
200 xdrs
->x_handy
-= sizeof(int32_t);
201 if (lp
) 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 sec_xdrmem_getbytes(xdrs
, addr
, len
)
213 if (xdrs
->x_handy
< len
)
215 xdrs
->x_handy
-= len
;
216 if (addr
) memmove(addr
, xdrs
->x_private
, len
);
217 xdrs
->x_private
= (char *)xdrs
->x_private
+ len
;
222 sec_xdrmem_putbytes(xdrs
, addr
, len
)
227 if (xdrs
->x_handy
< len
)
229 xdrs
->x_handy
-= len
;
230 if (addr
) memmove(xdrs
->x_private
, addr
, len
);
231 xdrs
->x_private
= (char *)xdrs
->x_private
+ len
;
236 sec_xdrmem_getpos(xdrs
)
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 sec_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 sec_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 sec_xdrmem_inline_unaligned(xdrs
, len
)
283 * This is almost a straight copy of the standard implementation, except
284 * that all calls made that move memory don't do so if passed a NULL dest.
285 * getbytes in particular when called from xdr_bytes during sizing of a decode