Libinfo-517.200.9.tar.gz
[apple/libinfo.git] / rpc.subproj / xdr_mem.c
1 /*
2 * Copyright (c) 1999-2018 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25 /* $NetBSD: xdr_mem.c,v 1.15 2000/01/22 22:19:18 mycroft Exp $ */
26
27 /*
28 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
29 * unrestricted use provided that this legend is included on all tape
30 * media and as a part of the software program in whole or part. Users
31 * may copy or modify Sun RPC without charge, but are not authorized
32 * to license or distribute it to anyone else except as part of a product or
33 * program developed by the user.
34 *
35 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
36 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
37 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
38 *
39 * Sun RPC is provided with no support and without any obligation on the
40 * part of Sun Microsystems, Inc. to assist in its use, correction,
41 * modification or enhancement.
42 *
43 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
44 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
45 * OR ANY PART THEREOF.
46 *
47 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
48 * or profits or other special, indirect and consequential damages, even if
49 * Sun has been advised of the possibility of such damages.
50 *
51 * Sun Microsystems, Inc.
52 * 2550 Garcia Avenue
53 * Mountain View, California 94043
54 */
55
56 #if defined(LIBC_SCCS) && !defined(lint)
57 static char *sccsid = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
58 static char *sccsid = "@(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC";
59 #endif
60 #include "libinfo_common.h"
61
62 #include <sys/cdefs.h>
63
64 /*
65 * xdr_mem.h, XDR implementation using memory buffers.
66 *
67 * Copyright (C) 1984, Sun Microsystems, Inc.
68 *
69 * If you have some data to be interpreted as external data representation
70 * or to be converted to external data representation in a memory buffer,
71 * then this is the package for you.
72 *
73 */
74
75 #include <sys/types.h>
76 #include <stddef.h>
77
78 #include <netinet/in.h>
79
80 #include <string.h>
81
82 #include <rpc/types.h>
83 #include <rpc/xdr.h>
84
85 #ifdef __LP64__
86 #define xdrlong_t int
87 #else
88 #define xdrlong_t long
89 #endif
90
91 static void xdrmem_destroy(XDR *);
92 static bool_t xdrmem_getlong_aligned(XDR *, xdrlong_t *);
93 static bool_t xdrmem_putlong_aligned(XDR *, const xdrlong_t *);
94 static bool_t xdrmem_getlong_unaligned(XDR *, xdrlong_t *);
95 static bool_t xdrmem_putlong_unaligned(XDR *, const xdrlong_t *);
96 static bool_t xdrmem_getbytes(XDR *, char *, u_int);
97 static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
98 /* XXX: w/64-bit pointers, u_int not enough! */
99 static u_int xdrmem_getpos(XDR *);
100 static bool_t xdrmem_setpos(XDR *, u_int);
101 static int32_t *xdrmem_inline_aligned(XDR *, u_int);
102 static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
103
104 static const struct xdr_ops xdrmem_ops_aligned = {
105 xdrmem_getlong_aligned,
106 xdrmem_putlong_aligned,
107 xdrmem_getbytes,
108 xdrmem_putbytes,
109 xdrmem_getpos,
110 xdrmem_setpos,
111 xdrmem_inline_aligned,
112 xdrmem_destroy
113 };
114
115 static const struct xdr_ops xdrmem_ops_unaligned = {
116 xdrmem_getlong_unaligned,
117 xdrmem_putlong_unaligned,
118 xdrmem_getbytes,
119 xdrmem_putbytes,
120 xdrmem_getpos,
121 xdrmem_setpos,
122 xdrmem_inline_unaligned,
123 xdrmem_destroy
124 };
125
126 /*
127 * The procedure xdrmem_create initializes a stream descriptor for a
128 * memory buffer.
129 */
130 LIBINFO_EXPORT
131 void
132 xdrmem_create(xdrs, addr, size, op)
133 XDR *xdrs;
134 char *addr;
135 u_int size;
136 enum xdr_op op;
137 {
138 xdrs->x_op = op;
139 xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
140 ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
141 xdrs->x_private = xdrs->x_base = addr;
142 xdrs->x_handy = size;
143 }
144
145 /*ARGSUSED*/
146 static void
147 xdrmem_destroy(xdrs)
148 XDR *xdrs;
149 {
150 }
151
152 static bool_t
153 xdrmem_getlong_aligned(xdrs, lp)
154 XDR *xdrs;
155 xdrlong_t *lp;
156 {
157 if (xdrs->x_handy < sizeof(int32_t))
158 return (FALSE);
159 xdrs->x_handy -= sizeof(int32_t);
160 *lp = ntohl(*(u_int32_t *)xdrs->x_private);
161 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
162 return (TRUE);
163 }
164
165 static bool_t
166 xdrmem_putlong_aligned(xdrs, lp)
167 XDR *xdrs;
168 const xdrlong_t *lp;
169 {
170 if (xdrs->x_handy < sizeof(int32_t))
171 return (FALSE);
172 xdrs->x_handy -= sizeof(int32_t);
173 *(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp);
174 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
175 return (TRUE);
176 }
177
178 static bool_t
179 xdrmem_getlong_unaligned(xdrs, lp)
180 XDR *xdrs;
181 xdrlong_t *lp;
182 {
183 u_int32_t l;
184
185 if (xdrs->x_handy < sizeof(int32_t))
186 return (FALSE);
187 xdrs->x_handy -= sizeof(int32_t);
188 memmove(&l, xdrs->x_private, sizeof(int32_t));
189 *lp = ntohl(l);
190 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
191 return (TRUE);
192 }
193
194 static bool_t
195 xdrmem_putlong_unaligned(xdrs, lp)
196 XDR *xdrs;
197 const xdrlong_t *lp;
198 {
199 u_int32_t l;
200
201 if (xdrs->x_handy < sizeof(int32_t))
202 return (FALSE);
203 xdrs->x_handy -= sizeof(int32_t);
204 l = htonl((u_int32_t)*lp);
205 memmove(xdrs->x_private, &l, sizeof(int32_t));
206 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
207 return (TRUE);
208 }
209
210 static bool_t
211 xdrmem_getbytes(xdrs, addr, len)
212 XDR *xdrs;
213 char *addr;
214 u_int len;
215 {
216 if (xdrs->x_handy < len)
217 return (FALSE);
218 xdrs->x_handy -= len;
219 memmove(addr, xdrs->x_private, len);
220 xdrs->x_private = (char *)xdrs->x_private + len;
221 return (TRUE);
222 }
223
224 static bool_t
225 xdrmem_putbytes(xdrs, addr, len)
226 XDR *xdrs;
227 const char *addr;
228 u_int len;
229 {
230 if (xdrs->x_handy < len)
231 return (FALSE);
232 xdrs->x_handy -= len;
233 memmove(xdrs->x_private, addr, len);
234 xdrs->x_private = (char *)xdrs->x_private + len;
235 return (TRUE);
236 }
237
238 static u_int
239 xdrmem_getpos(xdrs)
240 XDR *xdrs;
241 {
242 ptrdiff_t delta;
243 u_int val;
244
245 delta = xdrs->x_private - (void *)(xdrs->x_base);
246 #ifdef __LP64__
247 if (delta > UINT32_MAX) return -1;
248 #endif
249
250 val = delta;
251 return val;
252 }
253
254 static bool_t
255 xdrmem_setpos(xdrs, pos)
256 XDR *xdrs;
257 u_int pos;
258 {
259 ptrdiff_t delta;
260 char *newaddr = xdrs->x_base + pos;
261 char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
262
263 if (newaddr > lastaddr)
264 return (FALSE);
265 xdrs->x_private = newaddr;
266 delta = lastaddr - newaddr;
267 #ifdef __LP64__
268 if (delta > UINT32_MAX) return (FALSE);
269 #endif
270 xdrs->x_handy = delta;
271 return (TRUE);
272 }
273
274 static int32_t *
275 xdrmem_inline_aligned(xdrs, len)
276 XDR *xdrs;
277 u_int len;
278 {
279 int32_t *buf = 0;
280
281 if (xdrs->x_handy >= len) {
282 xdrs->x_handy -= len;
283 buf = (int32_t *)xdrs->x_private;
284 xdrs->x_private = (char *)xdrs->x_private + len;
285 }
286 return (buf);
287 }
288
289 /* ARGSUSED */
290 static int32_t *
291 xdrmem_inline_unaligned(xdrs, len)
292 XDR *xdrs;
293 u_int len;
294 {
295 return (0);
296 }