Windows: build src/rx/tests
[openafs.git] / src / rx / 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 #include <afsconfig.h>
30 #include <afs/param.h>
31
32 /*
33  * xdr_mem.h, XDR implementation using memory buffers.
34  *
35  * Copyright (C) 1984, Sun Microsystems, Inc.
36  *
37  * If you have some data to be interpreted as external data representation
38  * or to be converted to external data representation in a memory buffer,
39  * then this is the package for you.
40  *
41  */
42
43 #ifdef KERNEL
44 # include "afs/sysincludes.h"
45 #else
46 # include <string.h>
47 # include <limits.h>
48 # ifndef AFS_NT40_ENV
49 #  include <netinet/in.h>
50 # endif
51 #endif
52
53 #include "xdr.h"
54
55 static bool_t xdrmem_getint32(XDR *, afs_int32 *);
56 static bool_t xdrmem_putint32(XDR *, afs_int32 *);
57 static bool_t xdrmem_getbytes(XDR *, caddr_t, u_int);
58 static bool_t xdrmem_putbytes(XDR *, caddr_t, u_int);
59 static u_int xdrmem_getpos(XDR *);
60 static bool_t xdrmem_setpos(XDR *, u_int);
61 static afs_int32 *xdrmem_inline(XDR *, u_int);
62 static void xdrmem_destroy(XDR *);
63
64 static struct xdr_ops xdrmem_ops = {
65 #ifndef HAVE_STRUCT_LABEL_SUPPORT
66 #ifdef AFS_XDR_64BITOPS
67     NULL,
68     NULL,
69 #endif
70     /* Windows does not support labeled assigments */
71 #if !(defined(KERNEL) && defined(AFS_SUN57_ENV))
72     xdrmem_getint32,    /* deserialize an afs_int32 */
73     xdrmem_putint32,    /* serialize an afs_int32 */
74 #endif
75     xdrmem_getbytes,    /* deserialize counted bytes */
76     xdrmem_putbytes,    /* serialize counted bytes */
77     xdrmem_getpos,      /* get offset in the stream: not supported. */
78     xdrmem_setpos,      /* set offset in the stream: not supported. */
79     xdrmem_inline,      /* prime stream for inline macros */
80     xdrmem_destroy,     /* destroy stream */
81 #if (defined(KERNEL) && defined(AFS_SUN57_ENV))
82     NULL,               /* control - not implemented */
83     xdrmem_getint32,    /* not supported */
84     xdrmem_putint32,    /* serialize an afs_int32 */
85 #endif
86 #else
87 #ifdef AFS_XDR_64BITOPS
88     .x_getint64 = NULL,
89     .x_putint64 = NULL,
90 #endif
91     .x_getint32 = xdrmem_getint32,
92     .x_putint32 = xdrmem_putint32,
93     .x_getbytes = xdrmem_getbytes,
94     .x_putbytes = xdrmem_putbytes,
95     .x_getpostn = xdrmem_getpos,
96     .x_setpostn = xdrmem_setpos,
97     .x_inline = xdrmem_inline,
98     .x_destroy = xdrmem_destroy
99 #endif
100 };
101
102 /*
103  * The procedure xdrmem_create initializes a stream descriptor for a
104  * memory buffer.
105  */
106 void
107 xdrmem_create(XDR * xdrs, caddr_t addr, u_int size, enum xdr_op op)
108 {
109     xdrs->x_op = op;
110     xdrs->x_ops = &xdrmem_ops;
111     xdrs->x_private = xdrs->x_base = addr;
112     xdrs->x_handy = (size > INT_MAX) ? INT_MAX : size;  /* XXX */
113 }
114
115 static void
116 xdrmem_destroy(XDR *xdrs)
117 {
118 }
119
120 static bool_t
121 xdrmem_getint32(XDR *xdrs, afs_int32 * lp)
122 {
123     if (xdrs->x_handy < sizeof(afs_int32))
124         return (FALSE);
125     else
126         xdrs->x_handy -= sizeof(afs_int32);
127     *lp = ntohl(*((afs_int32 *) (xdrs->x_private)));
128     xdrs->x_private += sizeof(afs_int32);
129     return (TRUE);
130 }
131
132 static bool_t
133 xdrmem_putint32(XDR *xdrs, afs_int32 * lp)
134 {
135     if (xdrs->x_handy < sizeof(afs_int32))
136         return (FALSE);
137     else
138         xdrs->x_handy -= sizeof(afs_int32);
139     *(afs_int32 *) xdrs->x_private = htonl(*lp);
140     xdrs->x_private += sizeof(afs_int32);
141     return (TRUE);
142 }
143
144 static bool_t
145 xdrmem_getbytes(XDR *xdrs, caddr_t addr, u_int len)
146 {
147     if (xdrs->x_handy < len)
148         return (FALSE);
149     else
150         xdrs->x_handy -= len;
151     memcpy(addr, xdrs->x_private, len);
152     xdrs->x_private += len;
153     return (TRUE);
154 }
155
156 static bool_t
157 xdrmem_putbytes(XDR *xdrs, caddr_t addr, u_int len)
158 {
159     if (xdrs->x_handy < len)
160         return (FALSE);
161     else
162         xdrs->x_handy -= len;
163     memcpy(xdrs->x_private, addr, len);
164     xdrs->x_private += len;
165     return (TRUE);
166 }
167
168 static u_int
169 xdrmem_getpos(XDR *xdrs)
170 {
171     return ((u_int)(xdrs->x_private - xdrs->x_base));
172 }
173
174 static bool_t
175 xdrmem_setpos(XDR *xdrs, u_int pos)
176 {
177     caddr_t newaddr = xdrs->x_base + pos;
178     caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
179
180     if (newaddr > lastaddr)
181         return (FALSE);
182     xdrs->x_private = newaddr;
183     xdrs->x_handy = (int)(lastaddr - newaddr);
184     return (TRUE);
185 }
186
187 static afs_int32 *
188 xdrmem_inline(XDR *xdrs, u_int len)
189 {
190     afs_int32 *buf = 0;
191
192     if (len >= 0 && xdrs->x_handy >= len) {
193         xdrs->x_handy -= len;
194         buf = (afs_int32 *) xdrs->x_private;
195         xdrs->x_private += len;
196     }
197     return (buf);
198 }