Ported xdr-4.0-mingw5 back to Linux.
[portablexdr.git] / xdr_mem.c
1 /* @(#)xdr_mem.c        2.1 88/07/29 4.0 RPCSRC */
2 /*
3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4  * unrestricted use provided that this legend is included on all tape
5  * media and as a part of the software program in whole or part.  Users
6  * may copy or modify Sun RPC without charge, but are not authorized
7  * to license or distribute it to anyone else except as part of a product or
8  * program developed by the user.
9  * 
10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13  * 
14  * Sun RPC is provided with no support and without any obligation on the
15  * part of Sun Microsystems, Inc. to assist in its use, correction,
16  * modification or enhancement.
17  * 
18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20  * OR ANY PART THEREOF.
21  * 
22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23  * or profits or other special, indirect and consequential damages, even if
24  * Sun has been advised of the possibility of such damages.
25  * 
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
32 #endif
33
34 /*
35  * xdr_mem.h, XDR implementation using memory buffers.
36  *
37  * Copyright (C) 1984, Sun Microsystems, Inc.
38  *
39  * If you have some data to be interpreted as external data representation
40  * or to be converted to external data representation in a memory buffer,
41  * then this is the package for you.
42  *
43  */
44
45 #include <config.h>
46
47 #ifdef HAVE_WINSOCK2_H
48 #include <winsock2.h>
49 #endif
50
51 #ifdef HAVE_ARPA_INET_H
52 #include <arpa/inet.h>
53 #endif
54
55 #include <string.h>
56
57 #include <rpc/types.h>
58 #include <rpc/xdr.h>
59
60 static bool_t   xdrmem_getlong();
61 static bool_t   xdrmem_putlong();
62 static bool_t   xdrmem_getbytes();
63 static bool_t   xdrmem_putbytes();
64 static u_int    xdrmem_getpos();
65 static bool_t   xdrmem_setpos();
66 static long *   xdrmem_inline();
67 static void     xdrmem_destroy();
68
69 static struct   xdr_ops xdrmem_ops = {
70         xdrmem_getlong,
71         xdrmem_putlong,
72         xdrmem_getbytes,
73         xdrmem_putbytes,
74         xdrmem_getpos,
75         xdrmem_setpos,
76         xdrmem_inline,
77         xdrmem_destroy
78 };
79
80 /*
81  * The procedure xdrmem_create initializes a stream descriptor for a
82  * memory buffer.  
83  */
84 void
85 xdrmem_create(xdrs, addr, size, op)
86         register XDR *xdrs;
87         caddr_t addr;
88         u_int size;
89         enum xdr_op op;
90 {
91
92         xdrs->x_op = op;
93         xdrs->x_ops = &xdrmem_ops;
94         xdrs->x_private = xdrs->x_base = addr;
95         xdrs->x_handy = size;
96 }
97
98 static void
99 xdrmem_destroy(/*xdrs*/)
100         /*XDR *xdrs;*/
101 {
102 }
103
104 static bool_t
105 xdrmem_getlong(xdrs, lp)
106         register XDR *xdrs;
107         long *lp;
108 {
109
110         if ((xdrs->x_handy -= sizeof(long)) < 0)
111                 return (FALSE);
112         *lp = (long)ntohl((u_long)(*((long *)(xdrs->x_private))));
113         xdrs->x_private += sizeof(long);
114         return (TRUE);
115 }
116
117 static bool_t
118 xdrmem_putlong(xdrs, lp)
119         register XDR *xdrs;
120         long *lp;
121 {
122
123         if ((xdrs->x_handy -= sizeof(long)) < 0)
124                 return (FALSE);
125         *(long *)xdrs->x_private = (long)htonl((u_long)(*lp));
126         xdrs->x_private += sizeof(long);
127         return (TRUE);
128 }
129
130 static bool_t
131 xdrmem_getbytes(xdrs, addr, len)
132         register XDR *xdrs;
133         caddr_t addr;
134         register u_int len;
135 {
136
137         if ((xdrs->x_handy -= len) < 0)
138                 return (FALSE);
139         memcpy (addr, xdrs->x_private, len);
140         xdrs->x_private += len;
141         return (TRUE);
142 }
143
144 static bool_t
145 xdrmem_putbytes(xdrs, addr, len)
146         register XDR *xdrs;
147         caddr_t addr;
148         register u_int len;
149 {
150
151         if ((xdrs->x_handy -= len) < 0)
152                 return (FALSE);
153         memcpy (xdrs->x_private, addr, len);
154         xdrs->x_private += len;
155         return (TRUE);
156 }
157
158 static u_int
159 xdrmem_getpos(xdrs)
160         register XDR *xdrs;
161 {
162
163         return xdrs->x_private - xdrs->x_base;
164 }
165
166 static bool_t
167 xdrmem_setpos(xdrs, pos)
168         register XDR *xdrs;
169         u_int pos;
170 {
171         register caddr_t newaddr = xdrs->x_base + pos;
172         register caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
173
174         if ((long)newaddr > (long)lastaddr)
175                 return (FALSE);
176         xdrs->x_private = newaddr;
177         xdrs->x_handy = lastaddr - newaddr;
178         return (TRUE);
179 }
180
181 static long *
182 xdrmem_inline(xdrs, len)
183         register XDR *xdrs;
184         int len;
185 {
186         long *buf = 0;
187
188         if (xdrs->x_handy >= len) {
189                 xdrs->x_handy -= len;
190                 buf = (long *) xdrs->x_private;
191                 xdrs->x_private += len;
192         }
193         return (buf);
194 }