xref: /illumos-gate/usr/src/uts/common/rpc/xdr_mem.c (revision bd97c7ce2344fa3252d8785c35895490916bc79b)
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 /*
36  * xdr_mem.c, XDR implementation using memory buffers.
37  *
38  * If you have some data to be interpreted as external data representation
39  * or to be converted to external data representation in a memory buffer,
40  * then this is the package for you.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/types.h>
45 #include <sys/systm.h>
46 
47 #include <rpc/types.h>
48 #include <rpc/xdr.h>
49 
50 static struct xdr_ops *xdrmem_ops(void);
51 
52 /*
53  * The procedure xdrmem_create initializes a stream descriptor for a
54  * memory buffer.
55  */
56 void
57 xdrmem_create(XDR *xdrs, caddr_t addr, uint_t size, enum xdr_op op)
58 {
59 	xdrs->x_op = op;
60 	xdrs->x_ops = xdrmem_ops();
61 	xdrs->x_private = xdrs->x_base = addr;
62 	xdrs->x_handy = size;
63 	xdrs->x_public = NULL;
64 }
65 
66 /* ARGSUSED */
67 static void
68 xdrmem_destroy(XDR *xdrs)
69 {
70 }
71 
72 static bool_t
73 xdrmem_getint32(XDR *xdrs, int32_t *int32p)
74 {
75 	if ((xdrs->x_handy -= (int)sizeof (int32_t)) < 0)
76 		return (FALSE);
77 	/* LINTED pointer alignment */
78 	*int32p = (int32_t)ntohl((uint32_t)(*((int32_t *)(xdrs->x_private))));
79 	xdrs->x_private += sizeof (int32_t);
80 	return (TRUE);
81 }
82 
83 static bool_t
84 xdrmem_putint32(XDR *xdrs, int32_t *int32p)
85 {
86 	if ((xdrs->x_handy -= (int)sizeof (int32_t)) < 0)
87 		return (FALSE);
88 	/* LINTED pointer alignment */
89 	*(int32_t *)xdrs->x_private = (int32_t)htonl((uint32_t)(*int32p));
90 	xdrs->x_private += sizeof (int32_t);
91 	return (TRUE);
92 }
93 
94 static bool_t
95 xdrmem_getbytes(XDR *xdrs, caddr_t addr, int len)
96 {
97 	if ((xdrs->x_handy -= len) < 0)
98 		return (FALSE);
99 	bcopy(xdrs->x_private, addr, len);
100 	xdrs->x_private += len;
101 	return (TRUE);
102 }
103 
104 static bool_t
105 xdrmem_putbytes(XDR *xdrs, caddr_t addr, int len)
106 {
107 	if ((xdrs->x_handy -= len) < 0)
108 		return (FALSE);
109 	bcopy(addr, xdrs->x_private, len);
110 	xdrs->x_private += len;
111 	return (TRUE);
112 }
113 
114 static uint_t
115 xdrmem_getpos(XDR *xdrs)
116 {
117 	return ((uint_t)((uintptr_t)xdrs->x_private - (uintptr_t)xdrs->x_base));
118 }
119 
120 static bool_t
121 xdrmem_setpos(XDR *xdrs, uint_t pos)
122 {
123 	caddr_t newaddr = xdrs->x_base + pos;
124 	caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
125 	ptrdiff_t diff;
126 
127 	if (newaddr > lastaddr)
128 		return (FALSE);
129 	xdrs->x_private = newaddr;
130 	diff = lastaddr - newaddr;
131 	xdrs->x_handy = (int)diff;
132 	return (TRUE);
133 }
134 
135 static rpc_inline_t *
136 xdrmem_inline(XDR *xdrs, int len)
137 {
138 	rpc_inline_t *buf = NULL;
139 
140 	if (xdrs->x_handy >= len) {
141 		xdrs->x_handy -= len;
142 		/* LINTED pointer alignment */
143 		buf = (rpc_inline_t *)xdrs->x_private;
144 		xdrs->x_private += len;
145 	}
146 	return (buf);
147 }
148 
149 static bool_t
150 xdrmem_control(XDR *xdrs, int request, void *info)
151 {
152 	xdr_bytesrec *xptr;
153 	int32_t *int32p;
154 	int len;
155 
156 	switch (request) {
157 
158 	case XDR_GET_BYTES_AVAIL:
159 		xptr = (xdr_bytesrec *)info;
160 		xptr->xc_is_last_record = TRUE;
161 		xptr->xc_num_avail = xdrs->x_handy;
162 		return (TRUE);
163 
164 	case XDR_PEEK:
165 		/*
166 		 * Return the next 4 byte unit in the XDR stream.
167 		 */
168 		if (xdrs->x_handy < sizeof (int32_t))
169 			return (FALSE);
170 		int32p = (int32_t *)info;
171 		*int32p = (int32_t)ntohl((uint32_t)
172 		    (*((int32_t *)(xdrs->x_private))));
173 		return (TRUE);
174 
175 	case XDR_SKIPBYTES:
176 		/*
177 		 * Skip the next N bytes in the XDR stream.
178 		 */
179 		int32p = (int32_t *)info;
180 		len = RNDUP((int)(*int32p));
181 		if ((xdrs->x_handy -= len) < 0)
182 			return (FALSE);
183 		xdrs->x_private += len;
184 		return (TRUE);
185 
186 	}
187 	return (FALSE);
188 }
189 
190 static struct xdr_ops *
191 xdrmem_ops(void)
192 {
193 	static struct xdr_ops ops;
194 
195 	if (ops.x_getint32 == NULL) {
196 		ops.x_getbytes = xdrmem_getbytes;
197 		ops.x_putbytes = xdrmem_putbytes;
198 		ops.x_getpostn = xdrmem_getpos;
199 		ops.x_setpostn = xdrmem_setpos;
200 		ops.x_inline = xdrmem_inline;
201 		ops.x_destroy = xdrmem_destroy;
202 		ops.x_control = xdrmem_control;
203 		ops.x_getint32 = xdrmem_getint32;
204 		ops.x_putint32 = xdrmem_putint32;
205 	}
206 	return (&ops);
207 }
208