xref: /freebsd/lib/libc/xdr/xdr_mem.c (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
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 = "$Id: xdr_mem.c,v 1.1 1993/10/27 05:41:11 paul 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 
48 #include <rpc/types.h>
49 #include <rpc/xdr.h>
50 #include <netinet/in.h>
51 
52 static bool_t	xdrmem_getlong();
53 static bool_t	xdrmem_putlong();
54 static bool_t	xdrmem_getbytes();
55 static bool_t	xdrmem_putbytes();
56 static u_int	xdrmem_getpos();
57 static bool_t	xdrmem_setpos();
58 static long *	xdrmem_inline();
59 static void	xdrmem_destroy();
60 
61 static struct	xdr_ops xdrmem_ops = {
62 	xdrmem_getlong,
63 	xdrmem_putlong,
64 	xdrmem_getbytes,
65 	xdrmem_putbytes,
66 	xdrmem_getpos,
67 	xdrmem_setpos,
68 	xdrmem_inline,
69 	xdrmem_destroy
70 };
71 
72 /*
73  * The procedure xdrmem_create initializes a stream descriptor for a
74  * memory buffer.
75  */
76 void
77 xdrmem_create(xdrs, addr, size, op)
78 	register XDR *xdrs;
79 	caddr_t addr;
80 	u_int size;
81 	enum xdr_op op;
82 {
83 
84 	xdrs->x_op = op;
85 	xdrs->x_ops = &xdrmem_ops;
86 	xdrs->x_private = xdrs->x_base = addr;
87 	xdrs->x_handy = size;
88 }
89 
90 static void
91 xdrmem_destroy(/*xdrs*/)
92 	/*XDR *xdrs;*/
93 {
94 }
95 
96 static bool_t
97 xdrmem_getlong(xdrs, lp)
98 	register XDR *xdrs;
99 	long *lp;
100 {
101 
102 	if ((xdrs->x_handy -= sizeof(long)) < 0)
103 		return (FALSE);
104 	*lp = (long)ntohl((u_long)(*((long *)(xdrs->x_private))));
105 	xdrs->x_private += sizeof(long);
106 	return (TRUE);
107 }
108 
109 static bool_t
110 xdrmem_putlong(xdrs, lp)
111 	register XDR *xdrs;
112 	long *lp;
113 {
114 
115 	if ((xdrs->x_handy -= sizeof(long)) < 0)
116 		return (FALSE);
117 	*(long *)xdrs->x_private = (long)htonl((u_long)(*lp));
118 	xdrs->x_private += sizeof(long);
119 	return (TRUE);
120 }
121 
122 static bool_t
123 xdrmem_getbytes(xdrs, addr, len)
124 	register XDR *xdrs;
125 	caddr_t addr;
126 	register u_int len;
127 {
128 
129 	if ((xdrs->x_handy -= len) < 0)
130 		return (FALSE);
131 	bcopy(xdrs->x_private, addr, len);
132 	xdrs->x_private += len;
133 	return (TRUE);
134 }
135 
136 static bool_t
137 xdrmem_putbytes(xdrs, addr, len)
138 	register XDR *xdrs;
139 	caddr_t addr;
140 	register u_int len;
141 {
142 
143 	if ((xdrs->x_handy -= len) < 0)
144 		return (FALSE);
145 	bcopy(addr, xdrs->x_private, len);
146 	xdrs->x_private += len;
147 	return (TRUE);
148 }
149 
150 static u_int
151 xdrmem_getpos(xdrs)
152 	register XDR *xdrs;
153 {
154 
155 	return ((u_int)xdrs->x_private - (u_int)xdrs->x_base);
156 }
157 
158 static bool_t
159 xdrmem_setpos(xdrs, pos)
160 	register XDR *xdrs;
161 	u_int pos;
162 {
163 	register caddr_t newaddr = xdrs->x_base + pos;
164 	register caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
165 
166 	if ((long)newaddr > (long)lastaddr)
167 		return (FALSE);
168 	xdrs->x_private = newaddr;
169 	xdrs->x_handy = (int)lastaddr - (int)newaddr;
170 	return (TRUE);
171 }
172 
173 static long *
174 xdrmem_inline(xdrs, len)
175 	register XDR *xdrs;
176 	int len;
177 {
178 	long *buf = 0;
179 
180 	if (xdrs->x_handy >= len) {
181 		xdrs->x_handy -= len;
182 		buf = (long *) xdrs->x_private;
183 		xdrs->x_private += len;
184 	}
185 	return (buf);
186 }
187