2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro
30 * @(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC
31 * $NetBSD: xdr_mem.c,v 1.15 2000/01/22 22:19:18 mycroft Exp $
32 * $FreeBSD: src/lib/libc/xdr/xdr_mem.c,v 1.13 2004/10/16 06:32:43 obrien Exp $
33 * $DragonFly: src/lib/libc/xdr/xdr_mem.c,v 1.4 2005/12/05 00:47:57 swildner Exp $
37 * xdr_mem.h, XDR implementation using memory buffers.
39 * Copyright (C) 1984, Sun Microsystems, Inc.
41 * If you have some data to be interpreted as external data representation
42 * or to be converted to external data representation in a memory buffer,
43 * then this is the package for you.
47 #include "namespace.h"
48 #include <sys/types.h>
50 #include <netinet/in.h>
54 #include <rpc/types.h>
56 #include "un-namespace.h"
58 static void xdrmem_destroy(XDR
*);
59 static bool_t
xdrmem_getlong_aligned(XDR
*, long *);
60 static bool_t
xdrmem_putlong_aligned(XDR
*, const long *);
61 static bool_t
xdrmem_getlong_unaligned(XDR
*, long *);
62 static bool_t
xdrmem_putlong_unaligned(XDR
*, const long *);
63 static bool_t
xdrmem_getbytes(XDR
*, char *, u_int
);
64 static bool_t
xdrmem_putbytes(XDR
*, const char *, u_int
);
65 /* XXX: w/64-bit pointers, u_int not enough! */
66 static u_int
xdrmem_getpos(XDR
*);
67 static bool_t
xdrmem_setpos(XDR
*, u_int
);
68 static int32_t *xdrmem_inline_aligned(XDR
*, u_int
);
69 static int32_t *xdrmem_inline_unaligned(XDR
*, u_int
);
71 static const struct xdr_ops xdrmem_ops_aligned
= {
72 xdrmem_getlong_aligned
,
73 xdrmem_putlong_aligned
,
78 xdrmem_inline_aligned
,
82 static const struct xdr_ops xdrmem_ops_unaligned
= {
83 xdrmem_getlong_unaligned
,
84 xdrmem_putlong_unaligned
,
89 xdrmem_inline_unaligned
,
94 * The procedure xdrmem_create initializes a stream descriptor for a
98 xdrmem_create(XDR
*xdrs
, char *addr
, u_int size
, enum xdr_op op
)
102 xdrs
->x_ops
= ((unsigned long)addr
& (sizeof(int32_t) - 1))
103 ? &xdrmem_ops_unaligned
: &xdrmem_ops_aligned
;
104 xdrs
->x_private
= xdrs
->x_base
= addr
;
105 xdrs
->x_handy
= size
;
110 xdrmem_destroy(XDR
*xdrs
)
116 xdrmem_getlong_aligned(XDR
*xdrs
, long *lp
)
119 if (xdrs
->x_handy
< sizeof(int32_t))
121 xdrs
->x_handy
-= sizeof(int32_t);
122 *lp
= ntohl(*(u_int32_t
*)xdrs
->x_private
);
123 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
128 xdrmem_putlong_aligned(XDR
*xdrs
, const long *lp
)
131 if (xdrs
->x_handy
< sizeof(int32_t))
133 xdrs
->x_handy
-= sizeof(int32_t);
134 *(u_int32_t
*)xdrs
->x_private
= htonl((u_int32_t
)*lp
);
135 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
140 xdrmem_getlong_unaligned(XDR
*xdrs
, long *lp
)
144 if (xdrs
->x_handy
< sizeof(int32_t))
146 xdrs
->x_handy
-= sizeof(int32_t);
147 memmove(&l
, xdrs
->x_private
, sizeof(int32_t));
149 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
154 xdrmem_putlong_unaligned(XDR
*xdrs
, const long *lp
)
158 if (xdrs
->x_handy
< sizeof(int32_t))
160 xdrs
->x_handy
-= sizeof(int32_t);
161 l
= htonl((u_int32_t
)*lp
);
162 memmove(xdrs
->x_private
, &l
, sizeof(int32_t));
163 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
168 xdrmem_getbytes(XDR
*xdrs
, char *addr
, u_int len
)
171 if (xdrs
->x_handy
< len
)
173 xdrs
->x_handy
-= len
;
174 memmove(addr
, xdrs
->x_private
, len
);
175 xdrs
->x_private
= (char *)xdrs
->x_private
+ len
;
180 xdrmem_putbytes(XDR
*xdrs
, const char *addr
, u_int len
)
183 if (xdrs
->x_handy
< len
)
185 xdrs
->x_handy
-= len
;
186 memmove(xdrs
->x_private
, addr
, len
);
187 xdrs
->x_private
= (char *)xdrs
->x_private
+ len
;
192 xdrmem_getpos(XDR
*xdrs
)
195 /* XXX w/64-bit pointers, u_int not enough! */
196 return (u_int
)((u_long
)xdrs
->x_private
- (u_long
)xdrs
->x_base
);
200 xdrmem_setpos(XDR
*xdrs
, u_int pos
)
202 char *newaddr
= xdrs
->x_base
+ pos
;
203 char *lastaddr
= (char *)xdrs
->x_private
+ xdrs
->x_handy
;
205 if (newaddr
> lastaddr
)
207 xdrs
->x_private
= newaddr
;
208 xdrs
->x_handy
= (u_int
)(lastaddr
- newaddr
); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
213 xdrmem_inline_aligned(XDR
*xdrs
, u_int len
)
217 if (xdrs
->x_handy
>= len
) {
218 xdrs
->x_handy
-= len
;
219 buf
= (int32_t *)xdrs
->x_private
;
220 xdrs
->x_private
= (char *)xdrs
->x_private
+ len
;
227 xdrmem_inline_unaligned(XDR
*xdrs __unused
, u_int len __unused
)