xref: /illumos-gate/usr/src/uts/common/rpc/xdr_mem.c (revision 51396a8ee7fb52fe0ab33bfe7b4f495ad431904a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 /*
38  * xdr_mem.c, XDR implementation using memory buffers.
39  *
40  * If you have some data to be interpreted as external data representation
41  * or to be converted to external data representation in a memory buffer,
42  * then this is the package for you.
43  */
44 
45 #include <sys/param.h>
46 #include <sys/types.h>
47 #include <sys/systm.h>
48 
49 #include <rpc/types.h>
50 #include <rpc/xdr.h>
51 
52 static struct xdr_ops *xdrmem_ops(void);
53 
54 /*
55  * The procedure xdrmem_create initializes a stream descriptor for a
56  * memory buffer.
57  */
58 void
59 xdrmem_create(XDR *xdrs, caddr_t addr, uint_t size, enum xdr_op op)
60 {
61 	xdrs->x_op = op;
62 	xdrs->x_ops = xdrmem_ops();
63 	xdrs->x_private = xdrs->x_base = addr;
64 	xdrs->x_handy = size;
65 	xdrs->x_public = NULL;
66 }
67 
68 /* ARGSUSED */
69 static void
70 xdrmem_destroy(XDR *xdrs)
71 {
72 }
73 
74 static bool_t
75 xdrmem_getint32(XDR *xdrs, int32_t *int32p)
76 {
77 	if ((xdrs->x_handy -= (int)sizeof (int32_t)) < 0)
78 		return (FALSE);
79 	/* LINTED pointer alignment */
80 	*int32p = (int32_t)ntohl((uint32_t)(*((int32_t *)(xdrs->x_private))));
81 	xdrs->x_private += sizeof (int32_t);
82 	return (TRUE);
83 }
84 
85 static bool_t
86 xdrmem_putint32(XDR *xdrs, int32_t *int32p)
87 {
88 	if ((xdrs->x_handy -= (int)sizeof (int32_t)) < 0)
89 		return (FALSE);
90 	/* LINTED pointer alignment */
91 	*(int32_t *)xdrs->x_private = (int32_t)htonl((uint32_t)(*int32p));
92 	xdrs->x_private += sizeof (int32_t);
93 	return (TRUE);
94 }
95 
96 static bool_t
97 xdrmem_getbytes(XDR *xdrs, caddr_t addr, int len)
98 {
99 	if ((xdrs->x_handy -= len) < 0)
100 		return (FALSE);
101 	bcopy(xdrs->x_private, addr, len);
102 	xdrs->x_private += len;
103 	return (TRUE);
104 }
105 
106 static bool_t
107 xdrmem_putbytes(XDR *xdrs, caddr_t addr, int len)
108 {
109 	if ((xdrs->x_handy -= len) < 0)
110 		return (FALSE);
111 	bcopy(addr, xdrs->x_private, len);
112 	xdrs->x_private += len;
113 	return (TRUE);
114 }
115 
116 static uint_t
117 xdrmem_getpos(XDR *xdrs)
118 {
119 	return ((uint_t)((uintptr_t)xdrs->x_private - (uintptr_t)xdrs->x_base));
120 }
121 
122 static bool_t
123 xdrmem_setpos(XDR *xdrs, uint_t pos)
124 {
125 	caddr_t newaddr = xdrs->x_base + pos;
126 	caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
127 	ptrdiff_t diff;
128 
129 	if (newaddr > lastaddr)
130 		return (FALSE);
131 	xdrs->x_private = newaddr;
132 	diff = lastaddr - newaddr;
133 	xdrs->x_handy = (int)diff;
134 	return (TRUE);
135 }
136 
137 static rpc_inline_t *
138 xdrmem_inline(XDR *xdrs, int len)
139 {
140 	rpc_inline_t *buf = NULL;
141 
142 	if (xdrs->x_handy >= len) {
143 		xdrs->x_handy -= len;
144 		/* LINTED pointer alignment */
145 		buf = (rpc_inline_t *)xdrs->x_private;
146 		xdrs->x_private += len;
147 	}
148 	return (buf);
149 }
150 
151 static bool_t
152 xdrmem_control(XDR *xdrs, int request, void *info)
153 {
154 	xdr_bytesrec *xptr;
155 	int32_t *int32p;
156 	int len;
157 
158 	switch (request) {
159 
160 	case XDR_GET_BYTES_AVAIL:
161 		xptr = (xdr_bytesrec *)info;
162 		xptr->xc_is_last_record = TRUE;
163 		xptr->xc_num_avail = xdrs->x_handy;
164 		return (TRUE);
165 
166 	case XDR_PEEK:
167 		/*
168 		 * Return the next 4 byte unit in the XDR stream.
169 		 */
170 		if (xdrs->x_handy < sizeof (int32_t))
171 			return (FALSE);
172 		int32p = (int32_t *)info;
173 		*int32p = (int32_t)ntohl((uint32_t)
174 		    (*((int32_t *)(xdrs->x_private))));
175 		return (TRUE);
176 
177 	case XDR_SKIPBYTES:
178 		/*
179 		 * Skip the next N bytes in the XDR stream.
180 		 */
181 		int32p = (int32_t *)info;
182 		len = RNDUP((int)(*int32p));
183 		if ((xdrs->x_handy -= len) < 0)
184 			return (FALSE);
185 		xdrs->x_private += len;
186 		return (TRUE);
187 
188 	}
189 	return (FALSE);
190 }
191 
192 static struct xdr_ops *
193 xdrmem_ops(void)
194 {
195 	static struct xdr_ops ops;
196 
197 	if (ops.x_getint32 == NULL) {
198 		ops.x_getbytes = xdrmem_getbytes;
199 		ops.x_putbytes = xdrmem_putbytes;
200 		ops.x_getpostn = xdrmem_getpos;
201 		ops.x_setpostn = xdrmem_setpos;
202 		ops.x_inline = xdrmem_inline;
203 		ops.x_destroy = xdrmem_destroy;
204 		ops.x_control = xdrmem_control;
205 		ops.x_getint32 = xdrmem_getint32;
206 		ops.x_putint32 = xdrmem_putint32;
207 	}
208 	return (&ops);
209 }
210