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 * $FreeBSD: src/lib/libc/xdr/xdr_mem.c,v 1.8.2.1 2003/03/20 12:59:55 jedgar Exp $
32 * $DragonFly: src/lib/libc/xdr/xdr_mem.c,v 1.4 2005/12/05 00:47:57 swildner Exp $
36 * xdr_mem.h, XDR implementation using memory buffers.
38 * Copyright (C) 1984, Sun Microsystems, Inc.
40 * If you have some data to be interpreted as external data representation
41 * or to be converted to external data representation in a memory buffer,
42 * then this is the package for you.
47 #include <rpc/types.h>
49 #include <netinet/in.h>
51 static bool_t
xdrmem_getlong_aligned(XDR
*, long *);
52 static bool_t
xdrmem_putlong_aligned(XDR
*, long *);
53 static bool_t
xdrmem_getlong_unaligned(XDR
*, long *);
54 static bool_t
xdrmem_putlong_unaligned(XDR
*, long *);
55 static bool_t
xdrmem_getbytes(XDR
*, caddr_t
, u_int
);
56 static bool_t
xdrmem_putbytes(XDR
*, caddr_t
, u_int
);
57 static u_int
xdrmem_getpos(XDR
*); /* XXX w/64-bit pointers, u_int not enough! */
58 static bool_t
xdrmem_setpos(XDR
*, u_int
);
59 static int32_t *xdrmem_inline_aligned(XDR
*, int);
60 static int32_t *xdrmem_inline_unaligned(XDR
*, int);
61 static void xdrmem_destroy(void);
63 static struct xdr_ops xdrmem_ops_aligned
= {
64 xdrmem_getlong_aligned
,
65 xdrmem_putlong_aligned
,
70 xdrmem_inline_aligned
,
74 static struct xdr_ops xdrmem_ops_unaligned
= {
75 xdrmem_getlong_unaligned
,
76 xdrmem_putlong_unaligned
,
81 xdrmem_inline_unaligned
,
86 * The procedure xdrmem_create initializes a stream descriptor for a
90 xdrmem_create(XDR
*xdrs
, caddr_t addr
, u_int size
, enum xdr_op op
)
94 xdrs
->x_ops
= ((size_t)addr
& (sizeof(int32_t) - 1))
95 ? &xdrmem_ops_unaligned
: &xdrmem_ops_aligned
;
96 xdrs
->x_private
= xdrs
->x_base
= addr
;
106 xdrmem_getlong_aligned(XDR
*xdrs
, long *lp
)
109 if (xdrs
->x_handy
< sizeof(int32_t))
111 xdrs
->x_handy
-= sizeof(int32_t);
112 *lp
= ntohl(*(int32_t *)(xdrs
->x_private
));
113 xdrs
->x_private
+= sizeof(int32_t);
118 xdrmem_putlong_aligned(XDR
*xdrs
, long *lp
)
121 if (xdrs
->x_handy
< sizeof(int32_t))
123 xdrs
->x_handy
-= sizeof(int32_t);
124 *(int32_t *)xdrs
->x_private
= htonl(*lp
);
125 xdrs
->x_private
+= sizeof(int32_t);
130 xdrmem_getlong_unaligned(XDR
*xdrs
, long *lp
)
134 if (xdrs
->x_handy
< sizeof(int32_t))
136 xdrs
->x_handy
-= sizeof(int32_t);
137 memcpy(&l
, xdrs
->x_private
, sizeof(int32_t));
139 xdrs
->x_private
+= sizeof(int32_t);
144 xdrmem_putlong_unaligned(XDR
*xdrs
, long *lp
)
148 if (xdrs
->x_handy
< sizeof(int32_t))
150 xdrs
->x_handy
-= sizeof(int32_t);
152 memcpy(xdrs
->x_private
, &l
, sizeof(int32_t));
153 xdrs
->x_private
+= sizeof(int32_t);
158 xdrmem_getbytes(XDR
*xdrs
, caddr_t addr
, u_int len
)
161 if (xdrs
->x_handy
< len
)
163 xdrs
->x_handy
-= len
;
164 memcpy(addr
, xdrs
->x_private
, len
);
165 xdrs
->x_private
+= len
;
170 xdrmem_putbytes(XDR
*xdrs
, caddr_t addr
, u_int len
)
173 if (xdrs
->x_handy
< len
)
175 xdrs
->x_handy
-= len
;
176 memcpy(xdrs
->x_private
, addr
, len
);
177 xdrs
->x_private
+= len
;
182 xdrmem_getpos(XDR
*xdrs
)
185 /* XXX w/64-bit pointers, u_int not enough! */
186 return ((u_long
)xdrs
->x_private
- (u_long
)xdrs
->x_base
);
190 xdrmem_setpos(XDR
*xdrs
, u_int pos
)
192 caddr_t newaddr
= xdrs
->x_base
+ pos
;
193 caddr_t lastaddr
= xdrs
->x_private
+ xdrs
->x_handy
;
195 if (newaddr
> lastaddr
)
197 xdrs
->x_private
= newaddr
;
198 xdrs
->x_handy
= (u_int
)(lastaddr
- newaddr
); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
203 xdrmem_inline_aligned(XDR
*xdrs
, int len
)
207 if (xdrs
->x_handy
>= len
) {
208 xdrs
->x_handy
-= len
;
209 buf
= (int32_t *) xdrs
->x_private
;
210 xdrs
->x_private
+= len
;
216 xdrmem_inline_unaligned(XDR
*xdrs __unused
, int len __unused
)