xref: /netbsd-src/sys/opencrypto/criov.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: criov.c,v 1.6 2008/02/01 04:52:35 tls Exp $ */
2 /*      $OpenBSD: criov.c,v 1.11 2002/06/10 19:36:43 espie Exp $	*/
3 
4 /*
5  * Copyright (c) 1999 Theo de Raadt
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *   notice, this list of conditions and the following disclaimer in the
15  *   documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *   derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: criov.c,v 1.6 2008/02/01 04:52:35 tls Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/proc.h>
37 #include <sys/errno.h>
38 #include <sys/malloc.h>
39 #include <sys/kernel.h>
40 #include <sys/mbuf.h>
41 
42 #include <uvm/uvm_extern.h>
43 
44 #include <opencrypto/cryptodev.h>
45 int cuio_getindx(struct uio *uio, int loc, int *off);
46 
47 
48 void
49 cuio_copydata(uio, off, len, cp)
50 	struct uio *uio;
51 	int off, len;
52 	void *cp;
53 {
54 	struct iovec *iov = uio->uio_iov;
55 	int iol = uio->uio_iovcnt;
56 	unsigned count;
57 
58 	if (off < 0)
59 		panic("cuio_copydata: off %d < 0", off);
60 	if (len < 0)
61 		panic("cuio_copydata: len %d < 0", len);
62 	while (off > 0) {
63 		if (iol == 0)
64 			panic("iov_copydata: empty in skip");
65 		if (off < iov->iov_len)
66 			break;
67 		off -= iov->iov_len;
68 		iol--;
69 		iov++;
70 	}
71 	while (len > 0) {
72 		if (iol == 0)
73 			panic("cuio_copydata: empty");
74 		count = min(iov->iov_len - off, len);
75 		memcpy(cp, (char *)iov->iov_base + off, count);
76 		len -= count;
77 		cp = (char *)cp + count;
78 		off = 0;
79 		iol--;
80 		iov++;
81 	}
82 }
83 
84 void
85 cuio_copyback(uio, off, len, cp)
86 	struct uio *uio;
87 	int off, len;
88 	void *cp;
89 {
90 	struct iovec *iov = uio->uio_iov;
91 	int iol = uio->uio_iovcnt;
92 	unsigned count;
93 
94 	if (off < 0)
95 		panic("cuio_copyback: off %d < 0", off);
96 	if (len < 0)
97 		panic("cuio_copyback: len %d < 0", len);
98 	while (off > 0) {
99 		if (iol == 0)
100 			panic("cuio_copyback: empty in skip");
101 		if (off < iov->iov_len)
102 			break;
103 		off -= iov->iov_len;
104 		iol--;
105 		iov++;
106 	}
107 	while (len > 0) {
108 		if (iol == 0)
109 			panic("uio_copyback: empty");
110 		count = min(iov->iov_len - off, len);
111 		memcpy((char *)iov->iov_base + off, cp, count);
112 		len -= count;
113 		cp = (char *)cp + count;
114 		off = 0;
115 		iol--;
116 		iov++;
117 	}
118 }
119 
120 /*
121  * Return a pointer to iov/offset of location in iovec list.
122  */
123 
124 int
125 cuio_getptr(struct uio *uio, int loc, int *off)
126 {
127 	int ind, len;
128 
129 	ind = 0;
130 	while (loc >= 0 && ind < uio->uio_iovcnt) {
131 		len = uio->uio_iov[ind].iov_len;
132 		if (len > loc) {
133 			*off = loc;
134 			return (ind);
135 		}
136 		loc -= len;
137 		ind++;
138 	}
139 
140 	if (ind > 0 && loc == 0) {
141 		ind--;
142 		*off = uio->uio_iov[ind].iov_len;
143 		return (ind);
144 	}
145 
146 	return (-1);
147 }
148 
149 int
150 cuio_apply(struct uio *uio, int off, int len,
151     int (*f)(void *, void *, unsigned int), void *fstate)
152 {
153 	int rval, ind, uiolen;
154 	unsigned int count;
155 
156 	if (len < 0)
157 		panic("%s: len %d < 0", __func__, len);
158 	if (off < 0)
159 		panic("%s: off %d < 0", __func__, off);
160 
161 	ind = 0;
162 	while (off > 0) {
163 		if (ind >= uio->uio_iovcnt)
164 			panic("cuio_apply: out of ivecs before data in uio");
165 		uiolen = uio->uio_iov[ind].iov_len;
166 		if (off < uiolen)
167 			break;
168 		off -= uiolen;
169 		ind++;
170 	}
171 	while (len > 0) {
172 		if (ind >= uio->uio_iovcnt)
173 			panic("cuio_apply: out of ivecs when processing uio");
174 		count = min(uio->uio_iov[ind].iov_len - off, len);
175 
176 		rval = f(fstate,
177 			 ((char *)uio->uio_iov[ind].iov_base + off), count);
178 		if (rval)
179 			return (rval);
180 
181 		len -= count;
182 		off = 0;
183 		ind++;
184 	}
185 
186 	return (0);
187 }
188