xref: /minix3/lib/libc/rpc/xdr_mem.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: xdr_mem.c,v 1.19 2013/03/11 20:19:30 tron Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
4*84d9c625SLionel Sambuc  * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras  *
6*84d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
7*84d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions are
8*84d9c625SLionel Sambuc  * met:
92fe8fb19SBen Gras  *
10*84d9c625SLionel Sambuc  *     * Redistributions of source code must retain the above copyright
11*84d9c625SLionel Sambuc  *       notice, this list of conditions and the following disclaimer.
12*84d9c625SLionel Sambuc  *     * Redistributions in binary form must reproduce the above
13*84d9c625SLionel Sambuc  *       copyright notice, this list of conditions and the following
14*84d9c625SLionel Sambuc  *       disclaimer in the documentation and/or other materials
15*84d9c625SLionel Sambuc  *       provided with the distribution.
16*84d9c625SLionel Sambuc  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17*84d9c625SLionel Sambuc  *       contributors may be used to endorse or promote products derived
18*84d9c625SLionel Sambuc  *       from this software without specific prior written permission.
192fe8fb19SBen Gras  *
20*84d9c625SLionel Sambuc  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*84d9c625SLionel Sambuc  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*84d9c625SLionel Sambuc  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*84d9c625SLionel Sambuc  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*84d9c625SLionel Sambuc  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*84d9c625SLionel Sambuc  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*84d9c625SLionel Sambuc  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*84d9c625SLionel Sambuc  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*84d9c625SLionel Sambuc  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*84d9c625SLionel Sambuc  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*84d9c625SLionel Sambuc  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*84d9c625SLionel Sambuc  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras  */
332fe8fb19SBen Gras 
342fe8fb19SBen Gras #include <sys/cdefs.h>
352fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
362fe8fb19SBen Gras #if 0
372fe8fb19SBen Gras static char *sccsid = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro";
382fe8fb19SBen Gras static char *sccsid = "@(#)xdr_mem.c	2.1 88/07/29 4.0 RPCSRC";
392fe8fb19SBen Gras #else
40*84d9c625SLionel Sambuc __RCSID("$NetBSD: xdr_mem.c,v 1.19 2013/03/11 20:19:30 tron Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras 
442fe8fb19SBen Gras /*
452fe8fb19SBen Gras  * xdr_mem.h, XDR implementation using memory buffers.
462fe8fb19SBen Gras  *
472fe8fb19SBen Gras  * Copyright (C) 1984, Sun Microsystems, Inc.
482fe8fb19SBen Gras  *
492fe8fb19SBen Gras  * If you have some data to be interpreted as external data representation
502fe8fb19SBen Gras  * or to be converted to external data representation in a memory buffer,
512fe8fb19SBen Gras  * then this is the package for you.
522fe8fb19SBen Gras  *
532fe8fb19SBen Gras  */
542fe8fb19SBen Gras 
552fe8fb19SBen Gras #include "namespace.h"
562fe8fb19SBen Gras 
572fe8fb19SBen Gras #include <sys/types.h>
582fe8fb19SBen Gras 
592fe8fb19SBen Gras #include <netinet/in.h>
602fe8fb19SBen Gras 
612fe8fb19SBen Gras #include <string.h>
622fe8fb19SBen Gras 
632fe8fb19SBen Gras #include <rpc/types.h>
642fe8fb19SBen Gras #include <rpc/xdr.h>
652fe8fb19SBen Gras 
662fe8fb19SBen Gras #ifdef __weak_alias
672fe8fb19SBen Gras __weak_alias(xdrmem_create,_xdrmem_create)
682fe8fb19SBen Gras #endif
692fe8fb19SBen Gras 
70f14fb602SLionel Sambuc static void xdrmem_destroy(XDR *);
71f14fb602SLionel Sambuc static bool_t xdrmem_getlong_aligned(XDR *, long *);
72f14fb602SLionel Sambuc static bool_t xdrmem_putlong_aligned(XDR *, const long *);
73f14fb602SLionel Sambuc static bool_t xdrmem_getlong_unaligned(XDR *, long *);
74f14fb602SLionel Sambuc static bool_t xdrmem_putlong_unaligned(XDR *, const long *);
75f14fb602SLionel Sambuc static bool_t xdrmem_getbytes(XDR *, char *, u_int);
76f14fb602SLionel Sambuc static bool_t xdrmem_putbytes(XDR *, const char *, u_int);
772fe8fb19SBen Gras /* XXX: w/64-bit pointers, u_int not enough! */
78f14fb602SLionel Sambuc static u_int xdrmem_getpos(XDR *);
79f14fb602SLionel Sambuc static bool_t xdrmem_setpos(XDR *, u_int);
80f14fb602SLionel Sambuc static int32_t *xdrmem_inline_aligned(XDR *, u_int);
81f14fb602SLionel Sambuc static int32_t *xdrmem_inline_unaligned(XDR *, u_int);
822fe8fb19SBen Gras 
832fe8fb19SBen Gras static const struct	xdr_ops xdrmem_ops_aligned = {
842fe8fb19SBen Gras 	xdrmem_getlong_aligned,
852fe8fb19SBen Gras 	xdrmem_putlong_aligned,
862fe8fb19SBen Gras 	xdrmem_getbytes,
872fe8fb19SBen Gras 	xdrmem_putbytes,
882fe8fb19SBen Gras 	xdrmem_getpos,
892fe8fb19SBen Gras 	xdrmem_setpos,
902fe8fb19SBen Gras 	xdrmem_inline_aligned,
912fe8fb19SBen Gras 	xdrmem_destroy,
922fe8fb19SBen Gras 	NULL,	/* xdrmem_control */
932fe8fb19SBen Gras };
942fe8fb19SBen Gras 
952fe8fb19SBen Gras static const struct	xdr_ops xdrmem_ops_unaligned = {
962fe8fb19SBen Gras 	xdrmem_getlong_unaligned,
972fe8fb19SBen Gras 	xdrmem_putlong_unaligned,
982fe8fb19SBen Gras 	xdrmem_getbytes,
992fe8fb19SBen Gras 	xdrmem_putbytes,
1002fe8fb19SBen Gras 	xdrmem_getpos,
1012fe8fb19SBen Gras 	xdrmem_setpos,
1022fe8fb19SBen Gras 	xdrmem_inline_unaligned,
1032fe8fb19SBen Gras 	xdrmem_destroy,
1042fe8fb19SBen Gras 	NULL,	/* xdrmem_control */
1052fe8fb19SBen Gras };
1062fe8fb19SBen Gras 
1072fe8fb19SBen Gras /*
1082fe8fb19SBen Gras  * The procedure xdrmem_create initializes a stream descriptor for a
1092fe8fb19SBen Gras  * memory buffer.
1102fe8fb19SBen Gras  */
1112fe8fb19SBen Gras void
xdrmem_create(XDR * xdrs,char * addr,u_int size,enum xdr_op op)112f14fb602SLionel Sambuc xdrmem_create(XDR *xdrs, char *addr, u_int size, enum xdr_op op)
1132fe8fb19SBen Gras {
1142fe8fb19SBen Gras 
1152fe8fb19SBen Gras 	xdrs->x_op = op;
1162fe8fb19SBen Gras 	xdrs->x_ops = ((unsigned long)addr & (sizeof(int32_t) - 1))
1172fe8fb19SBen Gras 	    ? &xdrmem_ops_unaligned : &xdrmem_ops_aligned;
1182fe8fb19SBen Gras 	xdrs->x_private = xdrs->x_base = addr;
1192fe8fb19SBen Gras 	xdrs->x_handy = size;
1202fe8fb19SBen Gras }
1212fe8fb19SBen Gras 
1222fe8fb19SBen Gras /*ARGSUSED*/
1232fe8fb19SBen Gras static void
xdrmem_destroy(XDR * xdrs)124f14fb602SLionel Sambuc xdrmem_destroy(XDR *xdrs)
1252fe8fb19SBen Gras {
1262fe8fb19SBen Gras 
1272fe8fb19SBen Gras }
1282fe8fb19SBen Gras 
1292fe8fb19SBen Gras static bool_t
xdrmem_getlong_aligned(XDR * xdrs,long * lp)130f14fb602SLionel Sambuc xdrmem_getlong_aligned(XDR *xdrs, long *lp)
1312fe8fb19SBen Gras {
1322fe8fb19SBen Gras 
1332fe8fb19SBen Gras 	if (xdrs->x_handy < sizeof(int32_t))
1342fe8fb19SBen Gras 		return (FALSE);
1352fe8fb19SBen Gras 	xdrs->x_handy -= sizeof(int32_t);
1362fe8fb19SBen Gras 	*lp = ntohl(*(u_int32_t *)xdrs->x_private);
1372fe8fb19SBen Gras 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
1382fe8fb19SBen Gras 	return (TRUE);
1392fe8fb19SBen Gras }
1402fe8fb19SBen Gras 
1412fe8fb19SBen Gras static bool_t
xdrmem_putlong_aligned(XDR * xdrs,const long * lp)142f14fb602SLionel Sambuc xdrmem_putlong_aligned(XDR *xdrs, const long *lp)
1432fe8fb19SBen Gras {
1442fe8fb19SBen Gras 
1452fe8fb19SBen Gras 	if (xdrs->x_handy < sizeof(int32_t))
1462fe8fb19SBen Gras 		return (FALSE);
1472fe8fb19SBen Gras 	xdrs->x_handy -= sizeof(int32_t);
1482fe8fb19SBen Gras 	*(u_int32_t *)xdrs->x_private = htonl((u_int32_t)*lp);
1492fe8fb19SBen Gras 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
1502fe8fb19SBen Gras 	return (TRUE);
1512fe8fb19SBen Gras }
1522fe8fb19SBen Gras 
1532fe8fb19SBen Gras static bool_t
xdrmem_getlong_unaligned(XDR * xdrs,long * lp)154f14fb602SLionel Sambuc xdrmem_getlong_unaligned(XDR *xdrs, long *lp)
1552fe8fb19SBen Gras {
1562fe8fb19SBen Gras 	u_int32_t l;
1572fe8fb19SBen Gras 
1582fe8fb19SBen Gras 	if (xdrs->x_handy < sizeof(int32_t))
1592fe8fb19SBen Gras 		return (FALSE);
1602fe8fb19SBen Gras 	xdrs->x_handy -= sizeof(int32_t);
1612fe8fb19SBen Gras 	memmove(&l, xdrs->x_private, sizeof(int32_t));
1622fe8fb19SBen Gras 	*lp = ntohl(l);
1632fe8fb19SBen Gras 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
1642fe8fb19SBen Gras 	return (TRUE);
1652fe8fb19SBen Gras }
1662fe8fb19SBen Gras 
1672fe8fb19SBen Gras static bool_t
xdrmem_putlong_unaligned(XDR * xdrs,const long * lp)168f14fb602SLionel Sambuc xdrmem_putlong_unaligned(XDR *xdrs, const long *lp)
1692fe8fb19SBen Gras {
1702fe8fb19SBen Gras 	u_int32_t l;
1712fe8fb19SBen Gras 
1722fe8fb19SBen Gras 	if (xdrs->x_handy < sizeof(int32_t))
1732fe8fb19SBen Gras 		return (FALSE);
1742fe8fb19SBen Gras 	xdrs->x_handy -= sizeof(int32_t);
1752fe8fb19SBen Gras 	l = htonl((u_int32_t)*lp);
1762fe8fb19SBen Gras 	memmove(xdrs->x_private, &l, sizeof(int32_t));
1772fe8fb19SBen Gras 	xdrs->x_private = (char *)xdrs->x_private + sizeof(int32_t);
1782fe8fb19SBen Gras 	return (TRUE);
1792fe8fb19SBen Gras }
1802fe8fb19SBen Gras 
1812fe8fb19SBen Gras static bool_t
xdrmem_getbytes(XDR * xdrs,char * addr,u_int len)182f14fb602SLionel Sambuc xdrmem_getbytes(XDR *xdrs, char *addr, u_int len)
1832fe8fb19SBen Gras {
1842fe8fb19SBen Gras 
1852fe8fb19SBen Gras 	if (xdrs->x_handy < len)
1862fe8fb19SBen Gras 		return (FALSE);
1872fe8fb19SBen Gras 	xdrs->x_handy -= len;
1882fe8fb19SBen Gras 	memmove(addr, xdrs->x_private, len);
1892fe8fb19SBen Gras 	xdrs->x_private = (char *)xdrs->x_private + len;
1902fe8fb19SBen Gras 	return (TRUE);
1912fe8fb19SBen Gras }
1922fe8fb19SBen Gras 
1932fe8fb19SBen Gras static bool_t
xdrmem_putbytes(XDR * xdrs,const char * addr,u_int len)194f14fb602SLionel Sambuc xdrmem_putbytes(XDR *xdrs, const char *addr, u_int len)
1952fe8fb19SBen Gras {
1962fe8fb19SBen Gras 
1972fe8fb19SBen Gras 	if (xdrs->x_handy < len)
1982fe8fb19SBen Gras 		return (FALSE);
1992fe8fb19SBen Gras 	xdrs->x_handy -= len;
2002fe8fb19SBen Gras 	memmove(xdrs->x_private, addr, len);
2012fe8fb19SBen Gras 	xdrs->x_private = (char *)xdrs->x_private + len;
2022fe8fb19SBen Gras 	return (TRUE);
2032fe8fb19SBen Gras }
2042fe8fb19SBen Gras 
2052fe8fb19SBen Gras static u_int
xdrmem_getpos(XDR * xdrs)206f14fb602SLionel Sambuc xdrmem_getpos(XDR *xdrs)
2072fe8fb19SBen Gras {
2082fe8fb19SBen Gras 
2092fe8fb19SBen Gras 	/* XXX w/64-bit pointers, u_int not enough! */
2102fe8fb19SBen Gras 	return (u_int)((u_long)xdrs->x_private - (u_long)xdrs->x_base);
2112fe8fb19SBen Gras }
2122fe8fb19SBen Gras 
2132fe8fb19SBen Gras static bool_t
xdrmem_setpos(XDR * xdrs,u_int pos)214f14fb602SLionel Sambuc xdrmem_setpos(XDR *xdrs, u_int pos)
2152fe8fb19SBen Gras {
2162fe8fb19SBen Gras 	char *newaddr = xdrs->x_base + pos;
2172fe8fb19SBen Gras 	char *lastaddr = (char *)xdrs->x_private + xdrs->x_handy;
2182fe8fb19SBen Gras 
2192fe8fb19SBen Gras 	if ((long)newaddr > (long)lastaddr)
2202fe8fb19SBen Gras 		return (FALSE);
2212fe8fb19SBen Gras 	xdrs->x_private = newaddr;
2222fe8fb19SBen Gras 	xdrs->x_handy = (int)((long)lastaddr - (long)newaddr);
2232fe8fb19SBen Gras 	return (TRUE);
2242fe8fb19SBen Gras }
2252fe8fb19SBen Gras 
2262fe8fb19SBen Gras static int32_t *
xdrmem_inline_aligned(XDR * xdrs,u_int len)227f14fb602SLionel Sambuc xdrmem_inline_aligned(XDR *xdrs, u_int len)
2282fe8fb19SBen Gras {
2292fe8fb19SBen Gras 	int32_t *buf = 0;
2302fe8fb19SBen Gras 
2312fe8fb19SBen Gras 	if (xdrs->x_handy >= len) {
2322fe8fb19SBen Gras 		xdrs->x_handy -= len;
2332fe8fb19SBen Gras 		buf = (int32_t *)xdrs->x_private;
2342fe8fb19SBen Gras 		xdrs->x_private = (char *)xdrs->x_private + len;
2352fe8fb19SBen Gras 	}
2362fe8fb19SBen Gras 	return (buf);
2372fe8fb19SBen Gras }
2382fe8fb19SBen Gras 
2392fe8fb19SBen Gras /* ARGSUSED */
2402fe8fb19SBen Gras static int32_t *
xdrmem_inline_unaligned(XDR * xdrs,u_int len)241f14fb602SLionel Sambuc xdrmem_inline_unaligned(XDR *xdrs, u_int len)
2422fe8fb19SBen Gras {
2432fe8fb19SBen Gras 
2442fe8fb19SBen Gras 	return (0);
2452fe8fb19SBen Gras }
246