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

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since f41a394 was f41a394, checked in by Sebastian Huber <sebastian.huber@…>, on Jun 9, 2016 at 9:46:09 AM

XDR(3): Import from FreeBSD

  • Property mode set to 100644
File size: 6.1 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 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
7 * unrestricted use provided that this legend is included on all tape
8 * media and as a part of the software program in whole or part.  Users
9 * may copy or modify Sun RPC without charge, but are not authorized
10 * to license or distribute it to anyone else except as part of a product or
11 * program developed by the user.
12 *
13 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
14 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
15 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
16 *
17 * Sun RPC is provided with no support and without any obligation on the
18 * part of Sun Microsystems, Inc. to assist in its use, correction,
19 * modification or enhancement.
20 *
21 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
22 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
23 * OR ANY PART THEREOF.
24 *
25 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
26 * or profits or other special, indirect and consequential damages, even if
27 * Sun has been advised of the possibility of such damages.
28 *
29 * Sun Microsystems, Inc.
30 * 2550 Garcia Avenue
31 * Mountain View, California  94043
32 */
33
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *sccsid2 = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
36static char *sccsid = "@(#)xdr_mem.c    2.1 88/07/29 4.0 RPCSRC";
37#endif
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD$");
40
41/*
42 * xdr_mem.h, XDR implementation using memory buffers.
43 *
44 * Copyright (C) 1984, Sun Microsystems, Inc.
45 *
46 * If you have some data to be interpreted as external data representation
47 * or to be converted to external data representation in a memory buffer,
48 * then this is the package for you.
49 *
50 */
51
52#include "namespace.h"
53#include <sys/types.h>
54
55#include <netinet/in.h>
56
57#include <string.h>
58
59#include <rpc/types.h>
60#include <rpc/xdr.h>
61#include "un-namespace.h"
62
63static void xdrmem_destroy(XDR *);
64static bool_t xdrmem_getlong_aligned(XDR *, long *);
65static bool_t xdrmem_putlong_aligned(XDR *, const long *);
66static bool_t xdrmem_getlong_unaligned(XDR *, long *);
67static bool_t xdrmem_putlong_unaligned(XDR *, const long *);
68static bool_t xdrmem_getbytes(XDR *, char *, u_int);
69static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
70/* XXX: w/64-bit pointers, u_int not enough! */
71static u_int xdrmem_getpos(XDR *);
72static bool_t xdrmem_setpos(XDR *, u_int);
73static int32_t *xdrmem_inline_aligned(XDR *, u_int);
74static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
75
76static const struct     xdr_ops xdrmem_ops_aligned = {
77        xdrmem_getlong_aligned,
78        xdrmem_putlong_aligned,
79        xdrmem_getbytes,
80        xdrmem_putbytes,
81        xdrmem_getpos,
82        xdrmem_setpos,
83        xdrmem_inline_aligned,
84        xdrmem_destroy
85};
86
87static const struct     xdr_ops xdrmem_ops_unaligned = {
88        xdrmem_getlong_unaligned,
89        xdrmem_putlong_unaligned,
90        xdrmem_getbytes,
91        xdrmem_putbytes,
92        xdrmem_getpos,
93        xdrmem_setpos,
94        xdrmem_inline_unaligned,
95        xdrmem_destroy
96};
97
98/*
99 * The procedure xdrmem_create initializes a stream descriptor for a
100 * memory buffer.
101 */
102void
103xdrmem_create(xdrs, addr, size, op)
104        XDR *xdrs;
105        char *addr;
106        u_int size;
107        enum xdr_op op;
108{
109
110        xdrs->x_op = op;
111        xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
112            ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
113        xdrs->x_private = xdrs->x_base = addr;
114        xdrs->x_handy = size;
115}
116
117/*ARGSUSED*/
118static void
119xdrmem_destroy(xdrs)
120        XDR *xdrs;
121{
122
123}
124
125static bool_t
126xdrmem_getlong_aligned(xdrs, lp)
127        XDR *xdrs;
128        long *lp;
129{
130
131        if (xdrs->x_handy < sizeof(int32_t))
132                return (FALSE);
133        xdrs->x_handy -= sizeof(int32_t);
134        *lp = ntohl(*(u_int32_t *)xdrs->x_private);
135        xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
136        return (TRUE);
137}
138
139static bool_t
140xdrmem_putlong_aligned(xdrs, lp)
141        XDR *xdrs;
142        const long *lp;
143{
144
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);
151}
152
153static bool_t
154xdrmem_getlong_unaligned(xdrs, lp)
155        XDR *xdrs;
156        long *lp;
157{
158        u_int32_t l;
159
160        if (xdrs->x_handy < sizeof(int32_t))
161                return (FALSE);
162        xdrs->x_handy -= sizeof(int32_t);
163        memmove(&l, xdrs->x_private, sizeof(int32_t));
164        *lp = ntohl(l);
165        xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
166        return (TRUE);
167}
168
169static bool_t
170xdrmem_putlong_unaligned(xdrs, lp)
171        XDR *xdrs;
172        const long *lp;
173{
174        u_int32_t l;
175
176        if (xdrs->x_handy < sizeof(int32_t))
177                return (FALSE);
178        xdrs->x_handy -= sizeof(int32_t);
179        l = htonl((u_int32_t)*lp);
180        memmove(xdrs->x_private, &l, sizeof(int32_t));
181        xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
182        return (TRUE);
183}
184
185static bool_t
186xdrmem_getbytes(xdrs, addr, len)
187        XDR *xdrs;
188        char *addr;
189        u_int len;
190{
191
192        if (xdrs->x_handy < len)
193                return (FALSE);
194        xdrs->x_handy -= len;
195        memmove(addr, xdrs->x_private, len);
196        xdrs->x_private = (char *)xdrs->x_private + len;
197        return (TRUE);
198}
199
200static bool_t
201xdrmem_putbytes(xdrs, addr, len)
202        XDR *xdrs;
203        const char *addr;
204        u_int len;
205{
206
207        if (xdrs->x_handy < len)
208                return (FALSE);
209        xdrs->x_handy -= len;
210        memmove(xdrs->x_private, addr, len);
211        xdrs->x_private = (char *)xdrs->x_private + len;
212        return (TRUE);
213}
214
215static u_int
216xdrmem_getpos(xdrs)
217        XDR *xdrs;
218{
219
220        /* XXX w/64-bit pointers, u_int not enough! */
221        return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
222}
223
224static bool_t
225xdrmem_setpos(xdrs, pos)
226        XDR *xdrs;
227        u_int pos;
228{
229        char *newaddr = xdrs->x_base + pos;
230        char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
231
232        if (newaddr > lastaddr)
233                return (FALSE);
234        xdrs->x_private = newaddr;
235        xdrs->x_handy = (u_int)(lastaddr - newaddr); /* XXX sizeof(u_int) <? sizeof(ptrdiff_t) */
236        return (TRUE);
237}
238
239static int32_t *
240xdrmem_inline_aligned(xdrs, len)
241        XDR *xdrs;
242        u_int len;
243{
244        int32_t *buf = 0;
245
246        if (xdrs->x_handy >= len) {
247                xdrs->x_handy -= len;
248                buf = (int32_t *)xdrs->x_private;
249                xdrs->x_private = (char *)xdrs->x_private + len;
250        }
251        return (buf);
252}
253
254/* ARGSUSED */
255static int32_t *
256xdrmem_inline_unaligned(xdrs, len)
257        XDR *xdrs;
258        u_int len;
259{
260
261        return (0);
262}
Note: See TracBrowser for help on using the repository browser.