1 /* $NetBSD: xdr_mem.c,v 1.19 2013/03/11 20:19:30 tron Exp $ */
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
37 static char *sccsid
= "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
38 static char *sccsid
= "@(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC";
40 __RCSID("$NetBSD: xdr_mem.c,v 1.19 2013/03/11 20:19:30 tron Exp $");
45 * xdr_mem.h, XDR implementation using memory buffers.
47 * Copyright (C) 1984, Sun Microsystems, Inc.
49 * If you have some data to be interpreted as external data representation
50 * or to be converted to external data representation in a memory buffer,
51 * then this is the package for you.
55 #include "namespace.h"
57 #include <sys/types.h>
59 #include <netinet/in.h>
63 #include <rpc/types.h>
67 __weak_alias(xdrmem_create
,_xdrmem_create
)
70 static void xdrmem_destroy(XDR
*);
71 static bool_t
xdrmem_getlong_aligned(XDR
*, long *);
72 static bool_t
xdrmem_putlong_aligned(XDR
*, const long *);
73 static bool_t
xdrmem_getlong_unaligned(XDR
*, long *);
74 static bool_t
xdrmem_putlong_unaligned(XDR
*, const long *);
75 static bool_t
xdrmem_getbytes(XDR
*, char *, u_int
);
76 static bool_t
xdrmem_putbytes(XDR
*, const char *, u_int
);
77 /* XXX: w/64-bit pointers, u_int not enough! */
78 static u_int
xdrmem_getpos(XDR
*);
79 static bool_t
xdrmem_setpos(XDR
*, u_int
);
80 static int32_t *xdrmem_inline_aligned(XDR
*, u_int
);
81 static int32_t *xdrmem_inline_unaligned(XDR
*, u_int
);
83 static const struct xdr_ops xdrmem_ops_aligned
= {
84 xdrmem_getlong_aligned
,
85 xdrmem_putlong_aligned
,
90 xdrmem_inline_aligned
,
92 NULL
, /* xdrmem_control */
95 static const struct xdr_ops xdrmem_ops_unaligned
= {
96 xdrmem_getlong_unaligned
,
97 xdrmem_putlong_unaligned
,
102 xdrmem_inline_unaligned
,
104 NULL
, /* xdrmem_control */
108 * The procedure xdrmem_create initializes a stream descriptor for a
112 xdrmem_create(XDR
*xdrs
, char *addr
, u_int size
, enum xdr_op op
)
116 xdrs
->x_ops
= ((unsigned long)addr
& (sizeof(int32_t) - 1))
117 ? &xdrmem_ops_unaligned
: &xdrmem_ops_aligned
;
118 xdrs
->x_private
= xdrs
->x_base
= addr
;
119 xdrs
->x_handy
= size
;
124 xdrmem_destroy(XDR
*xdrs
)
130 xdrmem_getlong_aligned(XDR
*xdrs
, long *lp
)
133 if (xdrs
->x_handy
< sizeof(int32_t))
135 xdrs
->x_handy
-= sizeof(int32_t);
136 *lp
= ntohl(*(u_int32_t
*)xdrs
->x_private
);
137 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
142 xdrmem_putlong_aligned(XDR
*xdrs
, const long *lp
)
145 if (xdrs
->x_handy
< sizeof(int32_t))
147 xdrs
->x_handy
-= sizeof(int32_t);
148 *(u_int32_t
*)xdrs
->x_private
= htonl((u_int32_t
)*lp
);
149 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
154 xdrmem_getlong_unaligned(XDR
*xdrs
, long *lp
)
158 if (xdrs
->x_handy
< sizeof(int32_t))
160 xdrs
->x_handy
-= sizeof(int32_t);
161 memmove(&l
, xdrs
->x_private
, sizeof(int32_t));
163 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
168 xdrmem_putlong_unaligned(XDR
*xdrs
, const long *lp
)
172 if (xdrs
->x_handy
< sizeof(int32_t))
174 xdrs
->x_handy
-= sizeof(int32_t);
175 l
= htonl((u_int32_t
)*lp
);
176 memmove(xdrs
->x_private
, &l
, sizeof(int32_t));
177 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
182 xdrmem_getbytes(XDR
*xdrs
, char *addr
, u_int len
)
185 if (xdrs
->x_handy
< len
)
187 xdrs
->x_handy
-= len
;
188 memmove(addr
, xdrs
->x_private
, len
);
189 xdrs
->x_private
= (char *)xdrs
->x_private
+ len
;
194 xdrmem_putbytes(XDR
*xdrs
, const char *addr
, u_int len
)
197 if (xdrs
->x_handy
< len
)
199 xdrs
->x_handy
-= len
;
200 memmove(xdrs
->x_private
, addr
, len
);
201 xdrs
->x_private
= (char *)xdrs
->x_private
+ len
;
206 xdrmem_getpos(XDR
*xdrs
)
209 /* XXX w/64-bit pointers, u_int not enough! */
210 return (u_int
)((u_long
)xdrs
->x_private
- (u_long
)xdrs
->x_base
);
214 xdrmem_setpos(XDR
*xdrs
, u_int pos
)
216 char *newaddr
= xdrs
->x_base
+ pos
;
217 char *lastaddr
= (char *)xdrs
->x_private
+ xdrs
->x_handy
;
219 if ((long)newaddr
> (long)lastaddr
)
221 xdrs
->x_private
= newaddr
;
222 xdrs
->x_handy
= (int)((long)lastaddr
- (long)newaddr
);
227 xdrmem_inline_aligned(XDR
*xdrs
, u_int len
)
231 if (xdrs
->x_handy
>= len
) {
232 xdrs
->x_handy
-= len
;
233 buf
= (int32_t *)xdrs
->x_private
;
234 xdrs
->x_private
= (char *)xdrs
->x_private
+ len
;
241 xdrmem_inline_unaligned(XDR
*xdrs
, u_int len
)