]> git.ipfire.org Git - thirdparty/glibc.git/blame - sunrpc/xdr_mem.c
(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[thirdparty/glibc.git] / sunrpc / xdr_mem.c
CommitLineData
28f540f4
RM
1/*
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.
e7fd8a39 8 *
28f540f4
RM
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.
e7fd8a39 12 *
28f540f4
RM
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.
e7fd8a39 16 *
28f540f4
RM
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.
e7fd8a39 20 *
28f540f4
RM
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.
e7fd8a39 24 *
28f540f4
RM
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
28 */
28f540f4
RM
29
30/*
31 * xdr_mem.h, XDR implementation using memory buffers.
32 *
33 * Copyright (C) 1984, Sun Microsystems, Inc.
34 *
35 * If you have some data to be interpreted as external data representation
36 * or to be converted to external data representation in a memory buffer,
37 * then this is the package for you.
38 *
39 */
40
e7fd8a39 41#include <string.h>
fd4c894c 42#include <limits.h>
e7fd8a39
UD
43#include <rpc/rpc.h>
44
45static bool_t xdrmem_getlong (XDR *, long *);
1f205a47 46static bool_t xdrmem_putlong (XDR *, const long *);
e7fd8a39 47static bool_t xdrmem_getbytes (XDR *, caddr_t, u_int);
bfbc5754 48static bool_t xdrmem_putbytes (XDR *, const char *, u_int);
1f205a47 49static u_int xdrmem_getpos (const XDR *);
e7fd8a39 50static bool_t xdrmem_setpos (XDR *, u_int);
55187f62 51static int32_t *xdrmem_inline (XDR *, u_int);
92f1da4d 52static void xdrmem_destroy (XDR *);
7d1de115
UD
53static bool_t xdrmem_getint32 (XDR *, int32_t *);
54static bool_t xdrmem_putint32 (XDR *, const int32_t *);
e7fd8a39
UD
55
56static const struct xdr_ops xdrmem_ops =
57{
58 xdrmem_getlong,
59 xdrmem_putlong,
60 xdrmem_getbytes,
61 xdrmem_putbytes,
62 xdrmem_getpos,
63 xdrmem_setpos,
64 xdrmem_inline,
7d1de115
UD
65 xdrmem_destroy,
66 xdrmem_getint32,
67 xdrmem_putint32
28f540f4
RM
68};
69
70/*
71 * The procedure xdrmem_create initializes a stream descriptor for a
e7fd8a39 72 * memory buffer.
28f540f4
RM
73 */
74void
f8afba91 75xdrmem_create (XDR *xdrs, const caddr_t addr, u_int size, enum xdr_op op)
28f540f4 76{
e7fd8a39 77 xdrs->x_op = op;
737547be
UD
78 /* We have to add the const since the `struct xdr_ops' in `struct XDR'
79 is not `const'. */
80 xdrs->x_ops = (struct xdr_ops *) &xdrmem_ops;
e7fd8a39
UD
81 xdrs->x_private = xdrs->x_base = addr;
82 xdrs->x_handy = size;
28f540f4 83}
77fe0b9c 84INTDEF(xdrmem_create)
28f540f4 85
1f205a47
UD
86/*
87 * Nothing needs to be done for the memory case. The argument is clearly
88 * const.
89 */
90
28f540f4 91static void
92f1da4d 92xdrmem_destroy (XDR *xdrs)
28f540f4
RM
93{
94}
95
1f205a47
UD
96/*
97 * Gets the next word from the memory referenced by xdrs and places it
98 * in the long pointed to by lp. It then increments the private word to
99 * point at the next element. Neither object pointed to is const
100 */
28f540f4 101static bool_t
f8afba91 102xdrmem_getlong (XDR *xdrs, long *lp)
28f540f4 103{
fd4c894c 104 if (xdrs->x_handy < 4)
e7fd8a39 105 return FALSE;
fd4c894c 106 xdrs->x_handy -= 4;
e7fd8a39
UD
107 *lp = (int32_t) ntohl ((*((int32_t *) (xdrs->x_private))));
108 xdrs->x_private += 4;
109 return TRUE;
28f540f4
RM
110}
111
1f205a47
UD
112/*
113 * Puts the long pointed to by lp in the memory referenced by xdrs. It
114 * then increments the private word to point at the next element. The
115 * long pointed at is const
116 */
28f540f4 117static bool_t
f8afba91 118xdrmem_putlong (XDR *xdrs, const long *lp)
28f540f4 119{
fd4c894c 120 if (xdrs->x_handy < 4)
e7fd8a39 121 return FALSE;
fd4c894c 122 xdrs->x_handy -= 4;
e7fd8a39
UD
123 *(int32_t *) xdrs->x_private = htonl (*lp);
124 xdrs->x_private += 4;
125 return TRUE;
28f540f4
RM
126}
127
1f205a47
UD
128/*
129 * Gets an unaligned number of bytes from the xdrs structure and writes them
130 * to the address passed in addr. Be very careful when calling this routine
131 * as it could leave the xdrs pointing to an unaligned structure which is not
132 * a good idea. None of the things pointed to are const.
133 */
28f540f4 134static bool_t
f8afba91 135xdrmem_getbytes (XDR *xdrs, caddr_t addr, u_int len)
28f540f4 136{
fd4c894c 137 if (xdrs->x_handy < len)
e7fd8a39 138 return FALSE;
fd4c894c 139 xdrs->x_handy -= len;
9af652f6 140 memcpy (addr, xdrs->x_private, len);
e7fd8a39
UD
141 xdrs->x_private += len;
142 return TRUE;
28f540f4
RM
143}
144
1f205a47
UD
145/*
146 * The complementary function to the above. The same warnings apply about
147 * unaligned data. The source address is const.
148 */
28f540f4 149static bool_t
f8afba91 150xdrmem_putbytes (XDR *xdrs, const char *addr, u_int len)
28f540f4 151{
fd4c894c 152 if (xdrs->x_handy < len)
e7fd8a39 153 return FALSE;
fd4c894c 154 xdrs->x_handy -= len;
9af652f6 155 memcpy (xdrs->x_private, addr, len);
e7fd8a39
UD
156 xdrs->x_private += len;
157 return TRUE;
28f540f4
RM
158}
159
1f205a47
UD
160/*
161 * Not sure what this one does. But it clearly doesn't modify the contents
162 * of xdrs. **FIXME** does this not assume u_int == u_long?
163 */
28f540f4 164static u_int
f8afba91 165xdrmem_getpos (const XDR *xdrs)
28f540f4 166{
e7fd8a39 167 return (u_long) xdrs->x_private - (u_long) xdrs->x_base;
28f540f4
RM
168}
169
1f205a47
UD
170/*
171 * xdrs modified
172 */
28f540f4 173static bool_t
e7fd8a39
UD
174xdrmem_setpos (xdrs, pos)
175 XDR *xdrs;
176 u_int pos;
28f540f4 177{
e7fd8a39
UD
178 caddr_t newaddr = xdrs->x_base + pos;
179 caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
180
a334319f
UD
181 if ((long) newaddr > (long) lastaddr
182 || (UINT_MAX < LONG_MAX
183 && (long) UINT_MAX < (long) lastaddr - (long) newaddr))
e7fd8a39
UD
184 return FALSE;
185 xdrs->x_private = newaddr;
a334319f 186 xdrs->x_handy = (long) lastaddr - (long) newaddr;
e7fd8a39 187 return TRUE;
28f540f4
RM
188}
189
1f205a47
UD
190/*
191 * xdrs modified
192 */
f8afba91 193static int32_t *
55187f62 194xdrmem_inline (XDR *xdrs, u_int len)
28f540f4 195{
f8afba91 196 int32_t *buf = 0;
e7fd8a39 197
55187f62 198 if (xdrs->x_handy >= len)
e7fd8a39
UD
199 {
200 xdrs->x_handy -= len;
f8afba91 201 buf = (int32_t *) xdrs->x_private;
e7fd8a39
UD
202 xdrs->x_private += len;
203 }
204 return buf;
28f540f4 205}
7d1de115
UD
206
207/*
208 * Gets the next word from the memory referenced by xdrs and places it
209 * in the int pointed to by ip. It then increments the private word to
210 * point at the next element. Neither object pointed to is const
211 */
212static bool_t
213xdrmem_getint32 (XDR *xdrs, int32_t *ip)
214{
fd4c894c 215 if (xdrs->x_handy < 4)
7d1de115 216 return FALSE;
fd4c894c 217 xdrs->x_handy -= 4;
7d1de115
UD
218 *ip = ntohl ((*((int32_t *) (xdrs->x_private))));
219 xdrs->x_private += 4;
220 return TRUE;
221}
222
223/*
224 * Puts the long pointed to by lp in the memory referenced by xdrs. It
225 * then increments the private word to point at the next element. The
226 * long pointed at is const
227 */
228static bool_t
229xdrmem_putint32 (XDR *xdrs, const int32_t *ip)
230{
fd4c894c 231 if (xdrs->x_handy < 4)
7d1de115 232 return FALSE;
fd4c894c 233 xdrs->x_handy -= 4;
7d1de115
UD
234 *(int32_t *) xdrs->x_private = htonl (*ip);
235 xdrs->x_private += 4;
236 return TRUE;
237}