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