Imported Upstream version 0.2.2
[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 "namespace.h"
43 #include <sys/types.h>
44
45 #include <netinet/in.h>
46
47 #include <string.h>
48
49 #include <rpc/types.h>
50 #include <rpc/xdr.h>
51 #include "un-namespace.h"
52
53 static void xdrmem_destroy(XDR *);
54 static bool_t xdrmem_getlong_aligned(XDR *, long *);
55 static bool_t xdrmem_putlong_aligned(XDR *, const long *);
56 static bool_t xdrmem_getlong_unaligned(XDR *, long *);
57 static bool_t xdrmem_putlong_unaligned(XDR *, const long *);
58 static bool_t xdrmem_getbytes(XDR *, char *, u_int);
59 static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
60 /* XXX: w/64-bit pointers, u_int not enough! */
61 static u_int xdrmem_getpos(XDR *);
62 static bool_t xdrmem_setpos(XDR *, u_int);
63 static int32_t *xdrmem_inline_aligned(XDR *, u_int);
64 static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
65
66 static const struct     xdr_ops xdrmem_ops_aligned = {
67         xdrmem_getlong_aligned,
68         xdrmem_putlong_aligned,
69         xdrmem_getbytes,
70         xdrmem_putbytes,
71         xdrmem_getpos,
72         xdrmem_setpos,
73         xdrmem_inline_aligned,
74         xdrmem_destroy
75 };
76
77 static const struct     xdr_ops xdrmem_ops_unaligned = {
78         xdrmem_getlong_unaligned,
79         xdrmem_putlong_unaligned,
80         xdrmem_getbytes,
81         xdrmem_putbytes,
82         xdrmem_getpos,
83         xdrmem_setpos,
84         xdrmem_inline_unaligned,
85         xdrmem_destroy
86 };
87
88 /*
89  * The procedure xdrmem_create initializes a stream descriptor for a
90  * memory buffer.
91  */
92 void
93 xdrmem_create(xdrs, addr, size, op)
94         XDR *xdrs;
95         char *addr;
96         u_int size;
97         enum xdr_op op;
98 {
99
100         xdrs->x_op = op;
101         xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
102             ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
103         xdrs->x_private = xdrs->x_base = addr;
104         xdrs->x_handy = size;
105 }
106
107 /*ARGSUSED*/
108 static void
109 xdrmem_destroy(xdrs)
110         XDR *xdrs;
111 {
112
113 }
114
115 static bool_t
116 xdrmem_getlong_aligned(xdrs, lp)
117         XDR *xdrs;
118         long *lp;
119 {
120
121         if (xdrs->x_handy < sizeof(int32_t))
122                 return (FALSE);
123         xdrs->x_handy -= sizeof(int32_t);
124         *lp = ntohl(*(u_int32_t *)xdrs->x_private);
125         xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
126         return (TRUE);
127 }
128
129 static bool_t
130 xdrmem_putlong_aligned(xdrs, lp)
131         XDR *xdrs;
132         const long *lp;
133 {
134
135         if (xdrs->x_handy < sizeof(int32_t))
136                 return (FALSE);
137         xdrs->x_handy -= sizeof(int32_t);
138         *(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp);
139         xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
140         return (TRUE);
141 }
142
143 static bool_t
144 xdrmem_getlong_unaligned(xdrs, lp)
145         XDR *xdrs;
146         long *lp;
147 {
148         u_int32_t l;
149
150         if (xdrs->x_handy < sizeof(int32_t))
151                 return (FALSE);
152         xdrs->x_handy -= sizeof(int32_t);
153         memmove(&l, xdrs->x_private, sizeof(int32_t));
154         *lp = ntohl(l);
155         xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
156         return (TRUE);
157 }
158
159 static bool_t
160 xdrmem_putlong_unaligned(xdrs, lp)
161         XDR *xdrs;
162         const long *lp;
163 {
164         u_int32_t l;
165
166         if (xdrs->x_handy < sizeof(int32_t))
167                 return (FALSE);
168         xdrs->x_handy -= sizeof(int32_t);
169         l = htonl((u_int32_t)*lp);
170         memmove(xdrs->x_private, &l, sizeof(int32_t));
171         xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
172         return (TRUE);
173 }
174
175 static bool_t
176 xdrmem_getbytes(xdrs, addr, len)
177         XDR *xdrs;
178         char *addr;
179         u_int len;
180 {
181
182         if (xdrs->x_handy < len)
183                 return (FALSE);
184         xdrs->x_handy -= len;
185         memmove(addr, xdrs->x_private, len);
186         xdrs->x_private = (char *)xdrs->x_private + len;
187         return (TRUE);
188 }
189
190 static bool_t
191 xdrmem_putbytes(xdrs, addr, len)
192         XDR *xdrs;
193         const char *addr;
194         u_int len;
195 {
196
197         if (xdrs->x_handy < len)
198                 return (FALSE);
199         xdrs->x_handy -= len;
200         memmove(xdrs->x_private, addr, len);
201         xdrs->x_private = (char *)xdrs->x_private + len;
202         return (TRUE);
203 }
204
205 static u_int
206 xdrmem_getpos(xdrs)
207         XDR *xdrs;
208 {
209
210         /* XXX w/64-bit pointers, u_int not enough! */
211         return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
212 }
213
214 static bool_t
215 xdrmem_setpos(xdrs, pos)
216         XDR *xdrs;
217         u_int pos;
218 {
219         char *newaddr = xdrs->x_base + pos;
220         char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
221
222         if (newaddr > lastaddr)
223                 return (FALSE);
224         xdrs->x_private = newaddr;
225         xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
226         return (TRUE);
227 }
228
229 static int32_t *
230 xdrmem_inline_aligned(xdrs, len)
231         XDR *xdrs;
232         u_int len;
233 {
234         int32_t *buf = 0;
235
236         if (xdrs->x_handy >= len) {
237                 xdrs->x_handy -= len;
238                 buf = (int32_t *)xdrs->x_private;
239                 xdrs->x_private = (char *)xdrs->x_private + len;
240         }
241         return (buf);
242 }
243
244 /* ARGSUSED */
245 static int32_t *
246 xdrmem_inline_unaligned(xdrs, len)
247         XDR *xdrs;
248         u_int len;
249 {
250
251         return (0);
252 }