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