xref: /dragonfly/sys/opencrypto/criov.c (revision ed5d5720)
1 /*	$FreeBSD: src/sys/opencrypto/criov.c,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $	*/
2 /*	$DragonFly: src/sys/opencrypto/criov.c,v 1.2 2003/06/17 04:28:54 dillon Exp $	*/
3 /*      $OpenBSD: criov.c,v 1.9 2002/01/29 15:48:29 jason Exp $	*/
4 
5 /*
6  * Copyright (c) 1999 Theo de Raadt
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *   notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in the
16  *   documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *   derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/proc.h>
35 #include <sys/errno.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 #include <sys/uio.h>
39 
40 #include <opencrypto/cryptodev.h>
41 
42 void
43 cuio_copydata(struct uio* uio, int off, int len, caddr_t cp)
44 {
45 	struct iovec *iov = uio->uio_iov;
46 	int iol = uio->uio_iovcnt;
47 	unsigned count;
48 
49 	if (off < 0)
50 		panic("cuio_copydata: off %d < 0", off);
51 	if (len < 0)
52 		panic("cuio_copydata: len %d < 0", len);
53 	while (off > 0) {
54 		if (iol == 0)
55 			panic("iov_copydata: empty in skip");
56 		if (off < iov->iov_len)
57 			break;
58 		off -= iov->iov_len;
59 		iol--;
60 		iov++;
61 	}
62 	while (len > 0) {
63 		if (iol == 0)
64 			panic("cuio_copydata: empty");
65 		count = min(iov->iov_len - off, len);
66 		bcopy(((caddr_t)iov->iov_base) + off, cp, count);
67 		len -= count;
68 		cp += count;
69 		off = 0;
70 		iol--;
71 		iov++;
72 	}
73 }
74 
75 void
76 cuio_copyback(struct uio* uio, int off, int len, caddr_t cp)
77 {
78 	struct iovec *iov = uio->uio_iov;
79 	int iol = uio->uio_iovcnt;
80 	unsigned count;
81 
82 	if (off < 0)
83 		panic("cuio_copyback: off %d < 0", off);
84 	if (len < 0)
85 		panic("cuio_copyback: len %d < 0", len);
86 	while (off > 0) {
87 		if (iol == 0)
88 			panic("cuio_copyback: empty in skip");
89 		if (off < iov->iov_len)
90 			break;
91 		off -= iov->iov_len;
92 		iol--;
93 		iov++;
94 	}
95 	while (len > 0) {
96 		if (iol == 0)
97 			panic("uio_copyback: empty");
98 		count = min(iov->iov_len - off, len);
99 		bcopy(cp, ((caddr_t)iov->iov_base) + off, count);
100 		len -= count;
101 		cp += count;
102 		off = 0;
103 		iol--;
104 		iov++;
105 	}
106 }
107 
108 /*
109  * Return a pointer to iov/offset of location in iovec list.
110  */
111 struct iovec *
112 cuio_getptr(struct uio *uio, int loc, int *off)
113 {
114 	struct iovec *iov = uio->uio_iov;
115 	int iol = uio->uio_iovcnt;
116 
117 	while (loc >= 0) {
118 		/* Normal end of search */
119 		if (loc < iov->iov_len) {
120 	    		*off = loc;
121 	    		return (iov);
122 		}
123 
124 		loc -= iov->iov_len;
125 		if (iol == 0) {
126 			if (loc == 0) {
127 				/* Point at the end of valid data */
128 				*off = iov->iov_len;
129 				return (iov);
130 			} else
131 				return (NULL);
132 		} else {
133 			iov++, iol--;
134 		}
135     	}
136 
137 	return (NULL);
138 }
139