source: rtems-libbsd/freebsd/lib/libc/xdr/xdr_mem.c

6-freebsd-12
Last change on this file was bb80d9d, checked in by Sebastian Huber <sebastian.huber@…>, on 08/09/18 at 12:02:09

Update to FreeBSD head 2017-12-01

Git mirror commit e724f51f811a4b2bd29447f8b85ab5c2f9b88266.

Update #3472.

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