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