Label elements of rx structures
[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 "xdr.h"
47 #ifndef AFS_NT40_ENV
48 #include <netinet/in.h>
49 #else
50 #include <limits.h>
51 #endif
52
53 static bool_t xdrmem_getint32();
54 static bool_t xdrmem_putint32();
55 static bool_t xdrmem_getbytes();
56 static bool_t xdrmem_putbytes();
57 static u_int xdrmem_getpos();
58 static bool_t xdrmem_setpos();
59 static afs_int32 *xdrmem_inline();
60 static void xdrmem_destroy();
61
62 static struct xdr_ops xdrmem_ops = {
63     .x_getint32 = xdrmem_getint32,
64     .x_putint32 = xdrmem_putint32,
65     .x_getbytes = xdrmem_getbytes,
66     .x_putbytes = xdrmem_putbytes,
67     .x_getpos = xdrmem_getpos,
68     .x_setpos = xdrmem_setpos,
69     .x_inline = xdrmem_inline,
70     .x_destroy = xdrmem_destroy
71 };
72
73 /*
74  * The procedure xdrmem_create initializes a stream descriptor for a
75  * memory buffer.  
76  */
77 void
78 xdrmem_create(XDR * xdrs, caddr_t addr, u_int size, enum xdr_op op)
79 {
80     xdrs->x_op = op;
81     xdrs->x_ops = &xdrmem_ops;
82     xdrs->x_private = xdrs->x_base = addr;
83     xdrs->x_handy = (size > INT_MAX) ? INT_MAX : size;  /* XXX */
84 }
85
86 static void
87 xdrmem_destroy(void)
88 {
89 }
90
91 static bool_t
92 xdrmem_getint32(XDR * xdrs, afs_int32 * lp)
93 {
94     if (xdrs->x_handy < sizeof(afs_int32))
95         return (FALSE);
96     else
97         xdrs->x_handy -= sizeof(afs_int32);
98     *lp = ntohl(*((afs_int32 *) (xdrs->x_private)));
99     xdrs->x_private += sizeof(afs_int32);
100     return (TRUE);
101 }
102
103 static bool_t
104 xdrmem_putint32(XDR * xdrs, afs_int32 * lp)
105 {
106     if (xdrs->x_handy < sizeof(afs_int32))
107         return (FALSE);
108     else
109         xdrs->x_handy -= sizeof(afs_int32);
110     *(afs_int32 *) xdrs->x_private = htonl(*lp);
111     xdrs->x_private += sizeof(afs_int32);
112     return (TRUE);
113 }
114
115 static bool_t
116 xdrmem_getbytes(XDR * xdrs, caddr_t addr, u_int len)
117 {
118     if (xdrs->x_handy < len)
119         return (FALSE);
120     else
121         xdrs->x_handy -= len;
122     memcpy(addr, xdrs->x_private, len);
123     xdrs->x_private += len;
124     return (TRUE);
125 }
126
127 static bool_t
128 xdrmem_putbytes(XDR * xdrs, caddr_t addr, u_int len)
129 {
130     if (xdrs->x_handy < len)
131         return (FALSE);
132     else
133         xdrs->x_handy -= len;
134     memcpy(xdrs->x_private, addr, len);
135     xdrs->x_private += len;
136     return (TRUE);
137 }
138
139 static u_int
140 xdrmem_getpos(XDR * xdrs)
141 {
142     return ((u_int)(xdrs->x_private - xdrs->x_base));
143 }
144
145 static bool_t
146 xdrmem_setpos(XDR * xdrs, u_int pos)
147 {
148     caddr_t newaddr = xdrs->x_base + pos;
149     caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
150
151     if (newaddr > lastaddr)
152         return (FALSE);
153     xdrs->x_private = newaddr;
154     xdrs->x_handy = (int)(lastaddr - newaddr);
155     return (TRUE);
156 }
157
158 static afs_int32 *
159 xdrmem_inline(XDR * xdrs, int len)
160 {
161     afs_int32 *buf = 0;
162
163     if (len >= 0 && xdrs->x_handy >= len) {
164         xdrs->x_handy -= len;
165         buf = (afs_int32 *) xdrs->x_private;
166         xdrs->x_private += len;
167     }
168     return (buf);
169 }
170 #endif /* NeXT */