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 | |
---|
30 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
31 | /*static char *sccsid = "from: @(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";*/ |
---|
32 | /*static char *sccsid = "from: @(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC";*/ |
---|
33 | static char *rcsid = "$FreeBSD: src/lib/libc/xdr/xdr_mem.c,v 1.8 1999/08/28 00:02:56 peter Exp $"; |
---|
34 | #endif |
---|
35 | |
---|
36 | /* |
---|
37 | * xdr_mem.h, XDR implementation using memory buffers. |
---|
38 | * |
---|
39 | * Copyright (C) 1984, Sun Microsystems, Inc. |
---|
40 | * |
---|
41 | * If you have some data to be interpreted as external data representation |
---|
42 | * or to be converted to external data representation in a memory buffer, |
---|
43 | * then this is the package for you. |
---|
44 | * |
---|
45 | */ |
---|
46 | |
---|
47 | #ifdef HAVE_CONFIG_H |
---|
48 | #include "config.h" |
---|
49 | #endif |
---|
50 | |
---|
51 | #include <string.h> |
---|
52 | #include <rpc/types.h> |
---|
53 | #include <rpc/xdr.h> |
---|
54 | #include <netinet/in.h> |
---|
55 | |
---|
56 | static bool_t xdrmem_getlong_aligned(XDR *xdrs, long *lp); |
---|
57 | static bool_t xdrmem_putlong_aligned(XDR *xdrs, const long *lp); |
---|
58 | static bool_t xdrmem_getlong_unaligned(XDR *xdrs, long *lp); |
---|
59 | static bool_t xdrmem_putlong_unaligned(XDR *xdrs, const long *lp); |
---|
60 | static bool_t xdrmem_getbytes(XDR *xdrs, caddr_t addr, u_int len); |
---|
61 | static bool_t xdrmem_putbytes(XDR *xdrs, const char *addr, u_int len); |
---|
62 | static u_int xdrmem_getpos(XDR *xdrs); /* XXX w/64-bit pointers, u_int not enough! */ |
---|
63 | static bool_t xdrmem_setpos(XDR *xdrs, u_int pos); |
---|
64 | static int32_t *xdrmem_inline_aligned(XDR *xdrs, u_int len); |
---|
65 | static int32_t *xdrmem_inline_unaligned(XDR *xdrs, u_int len); |
---|
66 | static void xdrmem_destroy(XDR *); |
---|
67 | |
---|
68 | static struct xdr_ops xdrmem_ops_aligned = { |
---|
69 | xdrmem_getlong_aligned, |
---|
70 | xdrmem_putlong_aligned, |
---|
71 | xdrmem_getbytes, |
---|
72 | xdrmem_putbytes, |
---|
73 | xdrmem_getpos, |
---|
74 | xdrmem_setpos, |
---|
75 | xdrmem_inline_aligned, |
---|
76 | xdrmem_destroy |
---|
77 | }; |
---|
78 | |
---|
79 | static struct xdr_ops xdrmem_ops_unaligned = { |
---|
80 | xdrmem_getlong_unaligned, |
---|
81 | xdrmem_putlong_unaligned, |
---|
82 | xdrmem_getbytes, |
---|
83 | xdrmem_putbytes, |
---|
84 | xdrmem_getpos, |
---|
85 | xdrmem_setpos, |
---|
86 | xdrmem_inline_unaligned, |
---|
87 | xdrmem_destroy |
---|
88 | }; |
---|
89 | |
---|
90 | /* |
---|
91 | * The procedure xdrmem_create initializes a stream descriptor for a |
---|
92 | * memory buffer. |
---|
93 | */ |
---|
94 | void |
---|
95 | xdrmem_create( |
---|
96 | XDR *xdrs, |
---|
97 | char * addr, |
---|
98 | u_int size, |
---|
99 | enum xdr_op op) |
---|
100 | { |
---|
101 | |
---|
102 | xdrs->x_op = op; |
---|
103 | xdrs->x_ops = ((uintptr_t)addr & (sizeof(int32_t) - 1)) |
---|
104 | ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned; |
---|
105 | xdrs->x_private = xdrs->x_base = addr; |
---|
106 | xdrs->x_handy = size; |
---|
107 | } |
---|
108 | |
---|
109 | static void |
---|
110 | xdrmem_destroy(XDR *xdrs) |
---|
111 | { |
---|
112 | |
---|
113 | } |
---|
114 | |
---|
115 | static bool_t |
---|
116 | xdrmem_getlong_aligned( |
---|
117 | XDR *xdrs, |
---|
118 | long *lp) |
---|
119 | { |
---|
120 | |
---|
121 | if ((xdrs->x_handy -= sizeof(int32_t)) < 0) |
---|
122 | return (FALSE); |
---|
123 | *lp = ntohl(*(int32_t *)(xdrs->x_private)); |
---|
124 | xdrs->x_private += sizeof(int32_t); |
---|
125 | return (TRUE); |
---|
126 | } |
---|
127 | |
---|
128 | static bool_t |
---|
129 | xdrmem_putlong_aligned( |
---|
130 | XDR *xdrs, |
---|
131 | const long *lp) |
---|
132 | { |
---|
133 | |
---|
134 | if ((xdrs->x_handy -= sizeof(int32_t)) < 0) |
---|
135 | return (FALSE); |
---|
136 | *(int32_t *)xdrs->x_private = htonl(*lp); |
---|
137 | xdrs->x_private += sizeof(int32_t); |
---|
138 | return (TRUE); |
---|
139 | } |
---|
140 | |
---|
141 | static bool_t |
---|
142 | xdrmem_getlong_unaligned( |
---|
143 | XDR *xdrs, |
---|
144 | long *lp) |
---|
145 | { |
---|
146 | int32_t l; |
---|
147 | |
---|
148 | if ((xdrs->x_handy -= sizeof(int32_t)) < 0) |
---|
149 | return (FALSE); |
---|
150 | memcpy(&l, xdrs->x_private, sizeof(int32_t)); |
---|
151 | *lp = ntohl(l); |
---|
152 | xdrs->x_private += sizeof(int32_t); |
---|
153 | return (TRUE); |
---|
154 | } |
---|
155 | |
---|
156 | static bool_t |
---|
157 | xdrmem_putlong_unaligned( |
---|
158 | XDR *xdrs, |
---|
159 | const long *lp) |
---|
160 | { |
---|
161 | int32_t l; |
---|
162 | |
---|
163 | if ((xdrs->x_handy -= sizeof(int32_t)) < 0) |
---|
164 | return (FALSE); |
---|
165 | l = htonl(*lp); |
---|
166 | memcpy(xdrs->x_private, &l, sizeof(int32_t)); |
---|
167 | xdrs->x_private += sizeof(int32_t); |
---|
168 | return (TRUE); |
---|
169 | } |
---|
170 | |
---|
171 | static bool_t |
---|
172 | xdrmem_getbytes( |
---|
173 | XDR *xdrs, |
---|
174 | caddr_t addr, |
---|
175 | u_int len) |
---|
176 | { |
---|
177 | |
---|
178 | if ((xdrs->x_handy -= len) < 0) |
---|
179 | return (FALSE); |
---|
180 | memcpy(addr, xdrs->x_private, len); |
---|
181 | xdrs->x_private += len; |
---|
182 | return (TRUE); |
---|
183 | } |
---|
184 | |
---|
185 | static bool_t |
---|
186 | xdrmem_putbytes( |
---|
187 | XDR *xdrs, |
---|
188 | const char *addr, |
---|
189 | u_int len) |
---|
190 | { |
---|
191 | |
---|
192 | if ((xdrs->x_handy -= len) < 0) |
---|
193 | return (FALSE); |
---|
194 | memcpy(xdrs->x_private, addr, len); |
---|
195 | xdrs->x_private += len; |
---|
196 | return (TRUE); |
---|
197 | } |
---|
198 | |
---|
199 | static u_int |
---|
200 | xdrmem_getpos( |
---|
201 | XDR *xdrs) |
---|
202 | { |
---|
203 | |
---|
204 | /* XXX w/64-bit pointers, u_int not enough! */ |
---|
205 | return ((uintptr_t)xdrs->x_private - (uintptr_t)xdrs->x_base); |
---|
206 | } |
---|
207 | |
---|
208 | static bool_t |
---|
209 | xdrmem_setpos( |
---|
210 | XDR *xdrs, |
---|
211 | u_int pos) |
---|
212 | { |
---|
213 | void *newaddr = xdrs->x_base + pos; |
---|
214 | void *lastaddr = xdrs->x_private + xdrs->x_handy; |
---|
215 | |
---|
216 | if (newaddr > lastaddr) |
---|
217 | return (FALSE); |
---|
218 | xdrs->x_private = newaddr; |
---|
219 | xdrs->x_handy = (intptr_t)lastaddr - (intptr_t)newaddr; |
---|
220 | return (TRUE); |
---|
221 | } |
---|
222 | |
---|
223 | static int32_t * |
---|
224 | xdrmem_inline_aligned( |
---|
225 | XDR *xdrs, |
---|
226 | u_int len) |
---|
227 | { |
---|
228 | int32_t *buf = 0; |
---|
229 | |
---|
230 | if (xdrs->x_handy >= len) { |
---|
231 | xdrs->x_handy -= len; |
---|
232 | buf = (int32_t *)xdrs->x_private; |
---|
233 | xdrs->x_private += len; |
---|
234 | } |
---|
235 | return (buf); |
---|
236 | } |
---|
237 | |
---|
238 | static int32_t * |
---|
239 | xdrmem_inline_unaligned( |
---|
240 | XDR *xdrs, |
---|
241 | u_int len) |
---|
242 | { |
---|
243 | |
---|
244 | return (0); |
---|
245 | } |
---|