mkfs, mkproto: minor improvements
[minix.git] / lib / libc / rpc / xdr_mem.c
blob366d7c9866913f455073ffb7e485a0a064ec1c02
1 /* $NetBSD: xdr_mem.c,v 1.17 2006/10/15 16:14:46 christos Exp $ */
3 /*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char *sccsid = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
36 static char *sccsid = "@(#)xdr_mem.c 2.1 88/07/29 4.0 RPCSRC";
37 #else
38 __RCSID("$NetBSD: xdr_mem.c,v 1.17 2006/10/15 16:14:46 christos Exp $");
39 #endif
40 #endif
43 * xdr_mem.h, XDR implementation using memory buffers.
45 * Copyright (C) 1984, Sun Microsystems, Inc.
47 * If you have some data to be interpreted as external data representation
48 * or to be converted to external data representation in a memory buffer,
49 * then this is the package for you.
53 #include "namespace.h"
55 #include <sys/types.h>
57 #include <netinet/in.h>
59 #include <string.h>
61 #include <rpc/types.h>
62 #include <rpc/xdr.h>
64 #ifdef __weak_alias
65 __weak_alias(xdrmem_create,_xdrmem_create)
66 #endif
68 static void xdrmem_destroy __P((XDR *));
69 static bool_t xdrmem_getlong_aligned __P((XDR *, long *));
70 static bool_t xdrmem_putlong_aligned __P((XDR *, const long *));
71 static bool_t xdrmem_getlong_unaligned __P((XDR *, long *));
72 static bool_t xdrmem_putlong_unaligned __P((XDR *, const long *));
73 static bool_t xdrmem_getbytes __P((XDR *, char *, u_int));
74 static bool_t xdrmem_putbytes __P((XDR *, const char *, u_int));
75 /* XXX: w/64-bit pointers, u_int not enough! */
76 static u_int xdrmem_getpos __P((XDR *));
77 static bool_t xdrmem_setpos __P((XDR *, u_int));
78 static int32_t *xdrmem_inline_aligned __P((XDR *, u_int));
79 static int32_t *xdrmem_inline_unaligned __P((XDR *, u_int));
81 static const struct xdr_ops xdrmem_ops_aligned = {
82 xdrmem_getlong_aligned,
83 xdrmem_putlong_aligned,
84 xdrmem_getbytes,
85 xdrmem_putbytes,
86 xdrmem_getpos,
87 xdrmem_setpos,
88 xdrmem_inline_aligned,
89 xdrmem_destroy,
90 NULL, /* xdrmem_control */
93 static const struct xdr_ops xdrmem_ops_unaligned = {
94 xdrmem_getlong_unaligned,
95 xdrmem_putlong_unaligned,
96 xdrmem_getbytes,
97 xdrmem_putbytes,
98 xdrmem_getpos,
99 xdrmem_setpos,
100 xdrmem_inline_unaligned,
101 xdrmem_destroy,
102 NULL, /* xdrmem_control */
106 * The procedure xdrmem_create initializes a stream descriptor for a
107 * memory buffer.
109 void
110 xdrmem_create(xdrs, addr, size, op)
111 XDR *xdrs;
112 char *addr;
113 u_int size;
114 enum xdr_op op;
117 xdrs->x_op = op;
118 xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
119 ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
120 xdrs->x_private = xdrs->x_base = addr;
121 xdrs->x_handy = size;
124 /*ARGSUSED*/
125 static void
126 xdrmem_destroy(xdrs)
127 XDR *xdrs;
132 static bool_t
133 xdrmem_getlong_aligned(xdrs, lp)
134 XDR *xdrs;
135 long *lp;
138 if (xdrs->x_handy < sizeof(int32_t))
139 return (FALSE);
140 xdrs->x_handy -= sizeof(int32_t);
141 *lp = ntohl(*(u_int32_t *)xdrs->x_private);
142 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
143 return (TRUE);
146 static bool_t
147 xdrmem_putlong_aligned(xdrs, lp)
148 XDR *xdrs;
149 const long *lp;
152 if (xdrs->x_handy < sizeof(int32_t))
153 return (FALSE);
154 xdrs->x_handy -= sizeof(int32_t);
155 *(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp);
156 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
157 return (TRUE);
160 static bool_t
161 xdrmem_getlong_unaligned(xdrs, lp)
162 XDR *xdrs;
163 long *lp;
165 u_int32_t l;
167 if (xdrs->x_handy < sizeof(int32_t))
168 return (FALSE);
169 xdrs->x_handy -= sizeof(int32_t);
170 memmove(&l, xdrs->x_private, sizeof(int32_t));
171 *lp = ntohl(l);
172 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
173 return (TRUE);
176 static bool_t
177 xdrmem_putlong_unaligned(xdrs, lp)
178 XDR *xdrs;
179 const long *lp;
181 u_int32_t l;
183 if (xdrs->x_handy < sizeof(int32_t))
184 return (FALSE);
185 xdrs->x_handy -= sizeof(int32_t);
186 l = htonl((u_int32_t)*lp);
187 memmove(xdrs->x_private, &l, sizeof(int32_t));
188 xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
189 return (TRUE);
192 static bool_t
193 xdrmem_getbytes(xdrs, addr, len)
194 XDR *xdrs;
195 char *addr;
196 u_int len;
199 if (xdrs->x_handy < len)
200 return (FALSE);
201 xdrs->x_handy -= len;
202 memmove(addr, xdrs->x_private, len);
203 xdrs->x_private = (char *)xdrs->x_private + len;
204 return (TRUE);
207 static bool_t
208 xdrmem_putbytes(xdrs, addr, len)
209 XDR *xdrs;
210 const char *addr;
211 u_int len;
214 if (xdrs->x_handy < len)
215 return (FALSE);
216 xdrs->x_handy -= len;
217 memmove(xdrs->x_private, addr, len);
218 xdrs->x_private = (char *)xdrs->x_private + len;
219 return (TRUE);
222 static u_int
223 xdrmem_getpos(xdrs)
224 XDR *xdrs;
227 /* XXX w/64-bit pointers, u_int not enough! */
228 return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
231 static bool_t
232 xdrmem_setpos(xdrs, pos)
233 XDR *xdrs;
234 u_int pos;
236 char *newaddr = xdrs->x_base + pos;
237 char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
239 if ((long)newaddr > (long)lastaddr)
240 return (FALSE);
241 xdrs->x_private = newaddr;
242 xdrs->x_handy = (int)((long)lastaddr - (long)newaddr);
243 return (TRUE);
246 static int32_t *
247 xdrmem_inline_aligned(xdrs, len)
248 XDR *xdrs;
249 u_int len;
251 int32_t *buf = 0;
253 if (xdrs->x_handy >= len) {
254 xdrs->x_handy -= len;
255 buf = (int32_t *)xdrs->x_private;
256 xdrs->x_private = (char *)xdrs->x_private + len;
258 return (buf);
261 /* ARGSUSED */
262 static int32_t *
263 xdrmem_inline_unaligned(xdrs, len)
264 XDR *xdrs;
265 u_int len;
268 return (0);