1 /* $NetBSD: xdr_mem.c,v 1.17 2006/10/15 16:14:46 christos Exp $ */
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
35 static char *sccsid
= "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
36 static char *sccsid
= "@(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC";
38 __RCSID("$NetBSD: xdr_mem.c,v 1.17 2006/10/15 16:14:46 christos Exp $");
43 * xdr_mem.h, XDR implementation using memory buffers.
45 * Copyright (C) 1984, Sun Microsystems, Inc.
47 * If you have some data to be interpreted as external data representation
48 * or to be converted to external data representation in a memory buffer,
49 * then this is the package for you.
53 #include "namespace.h"
55 #include <sys/types.h>
57 #include <netinet/in.h>
61 #include <rpc/types.h>
65 __weak_alias(xdrmem_create
,_xdrmem_create
)
68 static void xdrmem_destroy
__P((XDR
*));
69 static bool_t xdrmem_getlong_aligned
__P((XDR
*, long *));
70 static bool_t xdrmem_putlong_aligned
__P((XDR
*, const long *));
71 static bool_t xdrmem_getlong_unaligned
__P((XDR
*, long *));
72 static bool_t xdrmem_putlong_unaligned
__P((XDR
*, const long *));
73 static bool_t xdrmem_getbytes
__P((XDR
*, char *, u_int
));
74 static bool_t xdrmem_putbytes
__P((XDR
*, const char *, u_int
));
75 /* XXX: w/64-bit pointers, u_int not enough! */
76 static u_int xdrmem_getpos
__P((XDR
*));
77 static bool_t xdrmem_setpos
__P((XDR
*, u_int
));
78 static int32_t *xdrmem_inline_aligned
__P((XDR
*, u_int
));
79 static int32_t *xdrmem_inline_unaligned
__P((XDR
*, u_int
));
81 static const struct xdr_ops xdrmem_ops_aligned
= {
82 xdrmem_getlong_aligned
,
83 xdrmem_putlong_aligned
,
88 xdrmem_inline_aligned
,
90 NULL
, /* xdrmem_control */
93 static const struct xdr_ops xdrmem_ops_unaligned
= {
94 xdrmem_getlong_unaligned
,
95 xdrmem_putlong_unaligned
,
100 xdrmem_inline_unaligned
,
102 NULL
, /* xdrmem_control */
106 * The procedure xdrmem_create initializes a stream descriptor for a
110 xdrmem_create(xdrs
, addr
, size
, op
)
118 xdrs
->x_ops
= ((unsigned long)addr
& (sizeof(int32_t) - 1))
119 ? &xdrmem_ops_unaligned
: &xdrmem_ops_aligned
;
120 xdrs
->x_private
= xdrs
->x_base
= addr
;
121 xdrs
->x_handy
= size
;
133 xdrmem_getlong_aligned(xdrs
, lp
)
138 if (xdrs
->x_handy
< sizeof(int32_t))
140 xdrs
->x_handy
-= sizeof(int32_t);
141 *lp
= ntohl(*(u_int32_t
*)xdrs
->x_private
);
142 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
147 xdrmem_putlong_aligned(xdrs
, lp
)
152 if (xdrs
->x_handy
< sizeof(int32_t))
154 xdrs
->x_handy
-= sizeof(int32_t);
155 *(u_int32_t
*)xdrs
->x_private
= htonl((u_int32_t
)*lp
);
156 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
161 xdrmem_getlong_unaligned(xdrs
, lp
)
167 if (xdrs
->x_handy
< sizeof(int32_t))
169 xdrs
->x_handy
-= sizeof(int32_t);
170 memmove(&l
, xdrs
->x_private
, sizeof(int32_t));
172 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
177 xdrmem_putlong_unaligned(xdrs
, lp
)
183 if (xdrs
->x_handy
< sizeof(int32_t))
185 xdrs
->x_handy
-= sizeof(int32_t);
186 l
= htonl((u_int32_t
)*lp
);
187 memmove(xdrs
->x_private
, &l
, sizeof(int32_t));
188 xdrs
->x_private
= (char *)xdrs
->x_private
+ sizeof(int32_t);
193 xdrmem_getbytes(xdrs
, addr
, len
)
199 if (xdrs
->x_handy
< len
)
201 xdrs
->x_handy
-= len
;
202 memmove(addr
, xdrs
->x_private
, len
);
203 xdrs
->x_private
= (char *)xdrs
->x_private
+ len
;
208 xdrmem_putbytes(xdrs
, addr
, len
)
214 if (xdrs
->x_handy
< len
)
216 xdrs
->x_handy
-= len
;
217 memmove(xdrs
->x_private
, addr
, len
);
218 xdrs
->x_private
= (char *)xdrs
->x_private
+ len
;
227 /* XXX w/64-bit pointers, u_int not enough! */
228 return (u_int
)((u_long
)xdrs
->x_private
- (u_long
)xdrs
->x_base
);
232 xdrmem_setpos(xdrs
, pos
)
236 char *newaddr
= xdrs
->x_base
+ pos
;
237 char *lastaddr
= (char *)xdrs
->x_private
+ xdrs
->x_handy
;
239 if ((long)newaddr
> (long)lastaddr
)
241 xdrs
->x_private
= newaddr
;
242 xdrs
->x_handy
= (int)((long)lastaddr
- (long)newaddr
);
247 xdrmem_inline_aligned(xdrs
, len
)
253 if (xdrs
->x_handy
>= len
) {
254 xdrs
->x_handy
-= len
;
255 buf
= (int32_t *)xdrs
->x_private
;
256 xdrs
->x_private
= (char *)xdrs
->x_private
+ len
;
263 xdrmem_inline_unaligned(xdrs
, len
)