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