source: rtems/cpukit/librpc/src/xdr/xdr_mem.c @ 1cdd06ac

4.104.115
Last change on this file since 1cdd06ac was cd791626, checked in by Ralf Corsepius <ralf.corsepius@…>, on 08/01/08 at 16:44:07

Misc. ansifications. Add prototypes.

  • Property mode set to 100644
File size: 5.5 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#include <string.h>
48#include <rpc/types.h>
49#include <rpc/xdr.h>
50#include <netinet/in.h>
51
52static bool_t   xdrmem_getlong_aligned(XDR *xdrs, long *lp);
53static bool_t   xdrmem_putlong_aligned(XDR *xdrs, const long *lp);
54static bool_t   xdrmem_getlong_unaligned(XDR *xdrs, long *lp);
55static bool_t   xdrmem_putlong_unaligned(XDR *xdrs, const long *lp);
56static bool_t   xdrmem_getbytes(XDR *xdrs, caddr_t addr, u_int len);
57static bool_t   xdrmem_putbytes(XDR *xdrs, const char *addr, u_int len);
58static u_int    xdrmem_getpos(XDR *xdrs); /* XXX w/64-bit pointers, u_int not enough! */
59static bool_t   xdrmem_setpos(XDR *xdrs, u_int pos);
60static int32_t *xdrmem_inline_aligned(XDR *xdrs, u_int len);
61static int32_t *xdrmem_inline_unaligned(XDR *xdrs, u_int len);
62static void     xdrmem_destroy(XDR *);
63
64static 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
75static 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 */
90void
91xdrmem_create(
92        XDR *xdrs,
93        caddr_t addr,
94        u_int size,
95        enum xdr_op op)
96{
97
98        xdrs->x_op = op;
99        xdrs->x_ops = ((size_t)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
105static void
106xdrmem_destroy(XDR *xdrs)
107{
108
109}
110
111static bool_t
112xdrmem_getlong_aligned(
113        XDR *xdrs,
114        long *lp)
115{
116
117        if ((xdrs->x_handy -= sizeof(int32_t)) < 0)
118                return (FALSE);
119        *lp = ntohl(*(int32_t *)(xdrs->x_private));
120        xdrs->x_private += sizeof(int32_t);
121        return (TRUE);
122}
123
124static bool_t
125xdrmem_putlong_aligned(
126        XDR *xdrs,
127        const long *lp)
128{
129
130        if ((xdrs->x_handy -= sizeof(int32_t)) < 0)
131                return (FALSE);
132        *(int32_t *)xdrs->x_private = htonl(*lp);
133        xdrs->x_private += sizeof(int32_t);
134        return (TRUE);
135}
136
137static bool_t
138xdrmem_getlong_unaligned(
139        XDR *xdrs,
140        long *lp)
141{
142        int32_t l;
143
144        if ((xdrs->x_handy -= sizeof(int32_t)) < 0)
145                return (FALSE);
146        memcpy(&l, xdrs->x_private, sizeof(int32_t));
147        *lp = ntohl(l);
148        xdrs->x_private += sizeof(int32_t);
149        return (TRUE);
150}
151
152static bool_t
153xdrmem_putlong_unaligned(
154        XDR *xdrs,
155        const long *lp)
156{
157        int32_t l;
158
159        if ((xdrs->x_handy -= sizeof(int32_t)) < 0)
160                return (FALSE);
161        l = htonl(*lp);
162        memcpy(xdrs->x_private, &l, sizeof(int32_t));
163        xdrs->x_private += sizeof(int32_t);
164        return (TRUE);
165}
166
167static bool_t
168xdrmem_getbytes(
169        XDR *xdrs,
170        caddr_t addr,
171        u_int len)
172{
173
174        if ((xdrs->x_handy -= len) < 0)
175                return (FALSE);
176        memcpy(addr, xdrs->x_private, len);
177        xdrs->x_private += len;
178        return (TRUE);
179}
180
181static bool_t
182xdrmem_putbytes(
183        XDR *xdrs,
184        const char *addr,
185        u_int len)
186{
187
188        if ((xdrs->x_handy -= len) < 0)
189                return (FALSE);
190        memcpy(xdrs->x_private, addr, len);
191        xdrs->x_private += len;
192        return (TRUE);
193}
194
195static u_int
196xdrmem_getpos(
197        XDR *xdrs)
198{
199
200        /* XXX w/64-bit pointers, u_int not enough! */
201        return ((u_long)xdrs->x_private - (u_long)xdrs->x_base);
202}
203
204static bool_t
205xdrmem_setpos(
206        XDR *xdrs,
207        u_int pos)
208{
209        register caddr_t newaddr = xdrs->x_base + pos;
210        register caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
211
212        if ((long)newaddr > (long)lastaddr)
213                return (FALSE);
214        xdrs->x_private = newaddr;
215        xdrs->x_handy = (long)lastaddr - (long)newaddr;
216        return (TRUE);
217}
218
219static int32_t *
220xdrmem_inline_aligned(
221        XDR *xdrs,
222        u_int len)
223{
224        int32_t *buf = 0;
225
226        if (xdrs->x_handy >= len) {
227                xdrs->x_handy -= len;
228                buf = (int32_t *) xdrs->x_private;
229                xdrs->x_private += len;
230        }
231        return (buf);
232}
233
234static int32_t *
235xdrmem_inline_unaligned(
236        XDR *xdrs,
237        u_int len)
238{
239       
240        return (0);
241}
Note: See TracBrowser for help on using the repository browser.