Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/xdr_mem.c

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