source: rtems/cpukit/librpc/src/xdr/xdr_mem.c @ 37da47a

4.104.115
Last change on this file since 37da47a was 37da47a, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/28/10 at 02:40:16

Add HAVE_CONFIG_H support to let files receive configure defines.

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