Imported Upstream version 0.2.5
[platform/upstream/libtirpc.git] / src / xdr_mem.c
1 /*
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  *   this list of conditions and the following disclaimer in the documentation
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  *   contributors may be used to endorse or promote products derived
14  *   from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30
31 /*
32  * xdr_mem.h, XDR implementation using memory buffers.
33  *
34  * Copyright (C) 1984, Sun Microsystems, Inc.
35  *
36  * If you have some data to be interpreted as external data representation
37  * or to be converted to external data representation in a memory buffer,
38  * then this is the package for you.
39  *
40  */
41
42 #include <sys/types.h>
43
44 #include <netinet/in.h>
45
46 #include <string.h>
47
48 #include <rpc/types.h>
49 #include <rpc/xdr.h>
50 #include "un-namespace.h"
51
52 static void xdrmem_destroy(XDR *);
53 static bool_t xdrmem_getlong_aligned(XDR *, long *);
54 static bool_t xdrmem_putlong_aligned(XDR *, const long *);
55 static bool_t xdrmem_getlong_unaligned(XDR *, long *);
56 static bool_t xdrmem_putlong_unaligned(XDR *, const long *);
57 static bool_t xdrmem_getbytes(XDR *, char *, u_int);
58 static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
59 /* XXX: w/64-bit pointers, u_int not enough! */
60 static u_int xdrmem_getpos(XDR *);
61 static bool_t xdrmem_setpos(XDR *, u_int);
62 static int32_t *xdrmem_inline_aligned(XDR *, u_int);
63 static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
64
65 static const struct     xdr_ops xdrmem_ops_aligned = {
66         xdrmem_getlong_aligned,
67         xdrmem_putlong_aligned,
68         xdrmem_getbytes,
69         xdrmem_putbytes,
70         xdrmem_getpos,
71         xdrmem_setpos,
72         xdrmem_inline_aligned,
73         xdrmem_destroy
74 };
75
76 static const struct     xdr_ops xdrmem_ops_unaligned = {
77         xdrmem_getlong_unaligned,
78         xdrmem_putlong_unaligned,
79         xdrmem_getbytes,
80         xdrmem_putbytes,
81         xdrmem_getpos,
82         xdrmem_setpos,
83         xdrmem_inline_unaligned,
84         xdrmem_destroy
85 };
86
87 /*
88  * The procedure xdrmem_create initializes a stream descriptor for a
89  * memory buffer.
90  */
91 void
92 xdrmem_create(xdrs, addr, size, op)
93         XDR *xdrs;
94         char *addr;
95         u_int size;
96         enum xdr_op op;
97 {
98
99         xdrs->x_op = op;
100         xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
101             ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
102         xdrs->x_private = xdrs->x_base = addr;
103         xdrs->x_handy = size;
104 }
105
106 /*ARGSUSED*/
107 static void
108 xdrmem_destroy(xdrs)
109         XDR *xdrs;
110 {
111
112 }
113
114 static bool_t
115 xdrmem_getlong_aligned(xdrs, lp)
116         XDR *xdrs;
117         long *lp;
118 {
119
120         if (xdrs->x_handy < sizeof(int32_t))
121                 return (FALSE);
122         xdrs->x_handy -= sizeof(int32_t);
123         *lp = ntohl(*(u_int32_t *)xdrs->x_private);
124         xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
125         return (TRUE);
126 }
127
128 static bool_t
129 xdrmem_putlong_aligned(xdrs, lp)
130         XDR *xdrs;
131         const long *lp;
132 {
133
134         if (xdrs->x_handy < sizeof(int32_t))
135                 return (FALSE);
136         xdrs->x_handy -= sizeof(int32_t);
137         *(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp);
138         xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
139         return (TRUE);
140 }
141
142 static bool_t
143 xdrmem_getlong_unaligned(xdrs, lp)
144         XDR *xdrs;
145         long *lp;
146 {
147         u_int32_t l;
148
149         if (xdrs->x_handy < sizeof(int32_t))
150                 return (FALSE);
151         xdrs->x_handy -= sizeof(int32_t);
152         memmove(&l, xdrs->x_private, sizeof(int32_t));
153         *lp = ntohl(l);
154         xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
155         return (TRUE);
156 }
157
158 static bool_t
159 xdrmem_putlong_unaligned(xdrs, lp)
160         XDR *xdrs;
161         const long *lp;
162 {
163         u_int32_t l;
164
165         if (xdrs->x_handy < sizeof(int32_t))
166                 return (FALSE);
167         xdrs->x_handy -= sizeof(int32_t);
168         l = htonl((u_int32_t)*lp);
169         memmove(xdrs->x_private, &l, sizeof(int32_t));
170         xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
171         return (TRUE);
172 }
173
174 static bool_t
175 xdrmem_getbytes(xdrs, addr, len)
176         XDR *xdrs;
177         char *addr;
178         u_int len;
179 {
180
181         if (xdrs->x_handy < len)
182                 return (FALSE);
183         xdrs->x_handy -= len;
184         memmove(addr, xdrs->x_private, len);
185         xdrs->x_private = (char *)xdrs->x_private + len;
186         return (TRUE);
187 }
188
189 static bool_t
190 xdrmem_putbytes(xdrs, addr, len)
191         XDR *xdrs;
192         const char *addr;
193         u_int len;
194 {
195
196         if (xdrs->x_handy < len)
197                 return (FALSE);
198         xdrs->x_handy -= len;
199         memmove(xdrs->x_private, addr, len);
200         xdrs->x_private = (char *)xdrs->x_private + len;
201         return (TRUE);
202 }
203
204 static u_int
205 xdrmem_getpos(xdrs)
206         XDR *xdrs;
207 {
208
209         /* XXX w/64-bit pointers, u_int not enough! */
210         return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
211 }
212
213 static bool_t
214 xdrmem_setpos(xdrs, pos)
215         XDR *xdrs;
216         u_int pos;
217 {
218         char *newaddr = xdrs->x_base + pos;
219         char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
220
221         if (newaddr > lastaddr)
222                 return (FALSE);
223         xdrs->x_private = newaddr;
224         xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
225         return (TRUE);
226 }
227
228 static int32_t *
229 xdrmem_inline_aligned(xdrs, len)
230         XDR *xdrs;
231         u_int len;
232 {
233         int32_t *buf = 0;
234
235         if (xdrs->x_handy >= len) {
236                 xdrs->x_handy -= len;
237                 buf = (int32_t *)xdrs->x_private;
238                 xdrs->x_private = (char *)xdrs->x_private + len;
239         }
240         return (buf);
241 }
242
243 /* ARGSUSED */
244 static int32_t *
245 xdrmem_inline_unaligned(xdrs, len)
246         XDR *xdrs;
247         u_int len;
248 {
249
250         return (0);
251 }