tools/llvm: Do not build with symbols
[minix3.git] / lib / libc / rpc / xdr_mem.c
blob05d77c34a3c2a81fd89b3db3c1ff0293a46ac024
1 /* $NetBSD: xdr_mem.c,v 1.19 2013/03/11 20:19:30 tron Exp $ */
3 /*
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
36 #if 0
37 static char *sccsid = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
38 static char *sccsid = "@(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC";
39 #else
40 __RCSID("$NetBSD: xdr_mem.c,v 1.19 2013/03/11 20:19:30 tron Exp $");
41 #endif
42 #endif
45 * xdr_mem.h, XDR implementation using memory buffers.
47 * Copyright (C) 1984, Sun Microsystems, Inc.
49 * If you have some data to be interpreted as external data representation
50 * or to be converted to external data representation in a memory buffer,
51 * then this is the package for you.
55 #include "namespace.h"
57 #include <sys/types.h>
59 #include <netinet/in.h>
61 #include <string.h>
63 #include <rpc/types.h>
64 #include <rpc/xdr.h>
66 #ifdef __weak_alias
67 __weak_alias(xdrmem_create,_xdrmem_create)
68 #endif
70 static void xdrmem_destroy(XDR *);
71 static bool_t xdrmem_getlong_aligned(XDR *, long *);
72 static bool_t xdrmem_putlong_aligned(XDR *, const long *);
73 static bool_t xdrmem_getlong_unaligned(XDR *, long *);
74 static bool_t xdrmem_putlong_unaligned(XDR *, const long *);
75 static bool_t xdrmem_getbytes(XDR *, char *, u_int);
76 static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
77 /* XXX: w/64-bit pointers, u_int not enough! */
78 static u_int xdrmem_getpos(XDR *);
79 static bool_t xdrmem_setpos(XDR *, u_int);
80 static int32_t *xdrmem_inline_aligned(XDR *, u_int);
81 static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
83 static const struct xdr_ops xdrmem_ops_aligned = {
84 xdrmem_getlong_aligned,
85 xdrmem_putlong_aligned,
86 xdrmem_getbytes,
87 xdrmem_putbytes,
88 xdrmem_getpos,
89 xdrmem_setpos,
90 xdrmem_inline_aligned,
91 xdrmem_destroy,
92 NULL, /* xdrmem_control */
95 static const struct xdr_ops xdrmem_ops_unaligned = {
96 xdrmem_getlong_unaligned,
97 xdrmem_putlong_unaligned,
98 xdrmem_getbytes,
99 xdrmem_putbytes,
100 xdrmem_getpos,
101 xdrmem_setpos,
102 xdrmem_inline_unaligned,
103 xdrmem_destroy,
104 NULL, /* xdrmem_control */
108 * The procedure xdrmem_create initializes a stream descriptor for a
109 * memory buffer.
111 void
112 xdrmem_create(XDR *xdrs, char *addr, u_int size, enum xdr_op op)
115 xdrs->x_op = op;
116 xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
117 ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
118 xdrs->x_private = xdrs->x_base = addr;
119 xdrs->x_handy = size;
122 /*ARGSUSED*/
123 static void
124 xdrmem_destroy(XDR *xdrs)
129 static bool_t
130 xdrmem_getlong_aligned(XDR *xdrs, long *lp)
133 if (xdrs->x_handy < sizeof(int32_t))
134 return (FALSE);
135 xdrs->x_handy -= sizeof(int32_t);
136 *lp = ntohl(*(u_int32_t *)xdrs->x_private);
137 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
138 return (TRUE);
141 static bool_t
142 xdrmem_putlong_aligned(XDR *xdrs, const long *lp)
145 if (xdrs->x_handy < sizeof(int32_t))
146 return (FALSE);
147 xdrs->x_handy -= sizeof(int32_t);
148 *(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp);
149 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
150 return (TRUE);
153 static bool_t
154 xdrmem_getlong_unaligned(XDR *xdrs, long *lp)
156 u_int32_t l;
158 if (xdrs->x_handy < sizeof(int32_t))
159 return (FALSE);
160 xdrs->x_handy -= sizeof(int32_t);
161 memmove(&l, xdrs->x_private, sizeof(int32_t));
162 *lp = ntohl(l);
163 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
164 return (TRUE);
167 static bool_t
168 xdrmem_putlong_unaligned(XDR *xdrs, const long *lp)
170 u_int32_t l;
172 if (xdrs->x_handy < sizeof(int32_t))
173 return (FALSE);
174 xdrs->x_handy -= sizeof(int32_t);
175 l = htonl((u_int32_t)*lp);
176 memmove(xdrs->x_private, &l, sizeof(int32_t));
177 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
178 return (TRUE);
181 static bool_t
182 xdrmem_getbytes(XDR *xdrs, char *addr, u_int len)
185 if (xdrs->x_handy < len)
186 return (FALSE);
187 xdrs->x_handy -= len;
188 memmove(addr, xdrs->x_private, len);
189 xdrs->x_private = (char *)xdrs->x_private + len;
190 return (TRUE);
193 static bool_t
194 xdrmem_putbytes(XDR *xdrs, const char *addr, u_int len)
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);
205 static u_int
206 xdrmem_getpos(XDR *xdrs)
209 /* XXX w/64-bit pointers, u_int not enough! */
210 return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
213 static bool_t
214 xdrmem_setpos(XDR *xdrs, u_int pos)
216 char *newaddr = xdrs->x_base + pos;
217 char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
219 if ((long)newaddr > (long)lastaddr)
220 return (FALSE);
221 xdrs->x_private = newaddr;
222 xdrs->x_handy = (int)((long)lastaddr - (long)newaddr);
223 return (TRUE);
226 static int32_t *
227 xdrmem_inline_aligned(XDR *xdrs, u_int len)
229 int32_t *buf = 0;
231 if (xdrs->x_handy >= len) {
232 xdrs->x_handy -= len;
233 buf = (int32_t *)xdrs->x_private;
234 xdrs->x_private = (char *)xdrs->x_private + len;
236 return (buf);
239 /* ARGSUSED */
240 static int32_t *
241 xdrmem_inline_unaligned(XDR *xdrs, u_int len)
244 return (0);