OSDN Git Service

hidden_def/hidden_proto: convert all users (I hope) termios split, add some missing...
[uclinux-h8/uClibc.git] / libc / inet / rpc / xdr_mem.c
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 /*
31  * xdr_mem.h, XDR implementation using memory buffers.
32  *
33  * Copyright (C) 1984, Sun Microsystems, Inc.
34  *
35  * If you have some data to be interpreted as external data representation
36  * or to be converted to external data representation in a memory buffer,
37  * then this is the package for you.
38  *
39  */
40
41 #define __FORCE_GLIBC
42 #define _GNU_SOURCE
43 #include <features.h>
44
45
46 #include <string.h>
47 #include <rpc/rpc.h>
48
49 libc_hidden_proto(memcpy)
50
51 static bool_t xdrmem_getlong (XDR *, long *);
52 static bool_t xdrmem_putlong (XDR *, const long *);
53 static bool_t xdrmem_getbytes (XDR *, caddr_t, u_int);
54 static bool_t xdrmem_putbytes (XDR *, const char *, u_int);
55 static u_int xdrmem_getpos (const XDR *);
56 static bool_t xdrmem_setpos (XDR *, u_int);
57 static int32_t *xdrmem_inline (XDR *, int);
58 static void xdrmem_destroy (XDR *);
59 static bool_t xdrmem_getint32 (XDR *, int32_t *);
60 static bool_t xdrmem_putint32 (XDR *, const int32_t *);
61
62 static const struct xdr_ops xdrmem_ops =
63 {
64   xdrmem_getlong,
65   xdrmem_putlong,
66   xdrmem_getbytes,
67   xdrmem_putbytes,
68   xdrmem_getpos,
69   xdrmem_setpos,
70   xdrmem_inline,
71   xdrmem_destroy,
72   xdrmem_getint32,
73   xdrmem_putint32
74 };
75
76 /*
77  * The procedure xdrmem_create initializes a stream descriptor for a
78  * memory buffer.
79  */
80 void
81 xdrmem_create (XDR *xdrs, const caddr_t addr, u_int size, enum xdr_op op)
82 {
83   xdrs->x_op = op;
84   /* We have to add the const since the `struct xdr_ops' in `struct XDR'
85      is not `const'.  */
86   xdrs->x_ops = (struct xdr_ops *) &xdrmem_ops;
87   xdrs->x_private = xdrs->x_base = addr;
88   xdrs->x_handy = size;
89 }
90 libc_hidden_proto(xdrmem_create)
91 libc_hidden_def(xdrmem_create)
92
93 /*
94  * Nothing needs to be done for the memory case.  The argument is clearly
95  * const.
96  */
97
98 static void
99 xdrmem_destroy (XDR *xdrs attribute_unused)
100 {
101 }
102
103 /*
104  * Gets the next word from the memory referenced by xdrs and places it
105  * in the long pointed to by lp.  It then increments the private word to
106  * point at the next element.  Neither object pointed to is const
107  */
108 static bool_t
109 xdrmem_getlong (XDR *xdrs, long *lp)
110 {
111   if ((xdrs->x_handy -= 4) < 0)
112     return FALSE;
113   *lp = (int32_t) ntohl ((*((int32_t *) (xdrs->x_private))));
114   xdrs->x_private += 4;
115   return TRUE;
116 }
117
118 /*
119  * Puts the long pointed to by lp in the memory referenced by xdrs.  It
120  * then increments the private word to point at the next element.  The
121  * long pointed at is const
122  */
123 static bool_t
124 xdrmem_putlong (XDR *xdrs, const long *lp)
125 {
126   if ((xdrs->x_handy -= 4) < 0)
127     return FALSE;
128   *(int32_t *) xdrs->x_private = htonl (*lp);
129   xdrs->x_private += 4;
130   return TRUE;
131 }
132
133 /*
134  * Gets an unaligned number of bytes from the xdrs structure and writes them
135  * to the address passed in addr.  Be very careful when calling this routine
136  * as it could leave the xdrs pointing to an unaligned structure which is not
137  * a good idea.  None of the things pointed to are const.
138  */
139 static bool_t
140 xdrmem_getbytes (XDR *xdrs, caddr_t addr, u_int len)
141 {
142   if ((xdrs->x_handy -= len) < 0)
143     return FALSE;
144   memcpy (addr, xdrs->x_private, len);
145   xdrs->x_private += len;
146   return TRUE;
147 }
148
149 /*
150  * The complementary function to the above.  The same warnings apply about
151  * unaligned data.  The source address is const.
152  */
153 static bool_t
154 xdrmem_putbytes (XDR *xdrs, const char *addr, u_int len)
155 {
156   if ((xdrs->x_handy -= len) < 0)
157     return FALSE;
158   memcpy (xdrs->x_private, addr, len);
159   xdrs->x_private += len;
160   return TRUE;
161 }
162
163 /*
164  * Not sure what this one does.  But it clearly doesn't modify the contents
165  * of xdrs.  **FIXME** does this not assume u_int == u_long?
166  */
167 static u_int
168 xdrmem_getpos (const XDR *xdrs)
169 {
170   return (u_long) xdrs->x_private - (u_long) xdrs->x_base;
171 }
172
173 /*
174  * xdrs modified
175  */
176 static bool_t
177 xdrmem_setpos (xdrs, pos)
178      XDR *xdrs;
179      u_int pos;
180 {
181   caddr_t newaddr = xdrs->x_base + pos;
182   caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
183
184   if ((long) newaddr > (long) lastaddr)
185     return FALSE;
186   xdrs->x_private = newaddr;
187   xdrs->x_handy = (long) lastaddr - (long) newaddr;
188   return TRUE;
189 }
190
191 /*
192  * xdrs modified
193  */
194 static int32_t *
195 xdrmem_inline (XDR *xdrs, int len)
196 {
197   int32_t *buf = 0;
198
199   if (xdrs->x_handy >= len)
200     {
201       xdrs->x_handy -= len;
202       buf = (int32_t *) xdrs->x_private;
203       xdrs->x_private += len;
204     }
205   return buf;
206 }
207
208 /*
209  * Gets the next word from the memory referenced by xdrs and places it
210  * in the int pointed to by ip.  It then increments the private word to
211  * point at the next element.  Neither object pointed to is const
212  */
213 static bool_t
214 xdrmem_getint32 (XDR *xdrs, int32_t *ip)
215 {
216   if ((xdrs->x_handy -= 4) < 0)
217     return FALSE;
218   *ip = ntohl ((*((int32_t *) (xdrs->x_private))));
219   xdrs->x_private += 4;
220   return TRUE;
221 }
222
223 /*
224  * Puts the long pointed to by lp in the memory referenced by xdrs.  It
225  * then increments the private word to point at the next element.  The
226  * long pointed at is const
227  */
228 static bool_t
229 xdrmem_putint32 (XDR *xdrs, const int32_t *ip)
230 {
231   if ((xdrs->x_handy -= 4) < 0)
232     return FALSE;
233   *(int32_t *) xdrs->x_private = htonl (*ip);
234   xdrs->x_private += 4;
235   return TRUE;
236 }