xref: /openbsd-src/sys/crypto/criov.c (revision 21dab745d772244ad59a415114e48be2888cfbc8)
1*21dab745Sjsg /*      $OpenBSD: criov.c,v 1.20 2015/03/14 03:38:46 jsg Exp $	*/
24385f535Sderaadt 
34385f535Sderaadt /*
44385f535Sderaadt  * Copyright (c) 1999 Theo de Raadt
54385f535Sderaadt  *
64385f535Sderaadt  * Redistribution and use in source and binary forms, with or without
74385f535Sderaadt  * modification, are permitted provided that the following conditions
84385f535Sderaadt  * are met:
94385f535Sderaadt  *
104385f535Sderaadt  * 1. Redistributions of source code must retain the above copyright
114385f535Sderaadt  *   notice, this list of conditions and the following disclaimer.
124385f535Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
134385f535Sderaadt  *   notice, this list of conditions and the following disclaimer in the
144385f535Sderaadt  *   documentation and/or other materials provided with the distribution.
154385f535Sderaadt  *
164385f535Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
174385f535Sderaadt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
184385f535Sderaadt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
194385f535Sderaadt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
204385f535Sderaadt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
214385f535Sderaadt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
224385f535Sderaadt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
234385f535Sderaadt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
244385f535Sderaadt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
254385f535Sderaadt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
264385f535Sderaadt  */
274385f535Sderaadt 
284385f535Sderaadt #include <sys/param.h>
294385f535Sderaadt #include <sys/systm.h>
304385f535Sderaadt #include <sys/errno.h>
314385f535Sderaadt #include <sys/kernel.h>
324385f535Sderaadt #include <sys/mbuf.h>
330528e431Smickey 
344385f535Sderaadt void
cuio_copydata(struct uio * uio,int off,int len,caddr_t cp)359941f4a6Sjason cuio_copydata(struct uio *uio, int off, int len, caddr_t cp)
364385f535Sderaadt {
378d04f523Sderaadt 	struct iovec *iov = uio->uio_iov;
388d04f523Sderaadt 	int iol = uio->uio_iovcnt;
394385f535Sderaadt 	unsigned count;
404385f535Sderaadt 
414385f535Sderaadt 	if (off < 0)
428d04f523Sderaadt 		panic("cuio_copydata: off %d < 0", off);
434385f535Sderaadt 	if (len < 0)
448d04f523Sderaadt 		panic("cuio_copydata: len %d < 0", len);
454385f535Sderaadt 	while (off > 0) {
464385f535Sderaadt 		if (iol == 0)
474385f535Sderaadt 			panic("iov_copydata: empty in skip");
484385f535Sderaadt 		if (off < iov->iov_len)
494385f535Sderaadt 			break;
504385f535Sderaadt 		off -= iov->iov_len;
514385f535Sderaadt 		iol--;
524385f535Sderaadt 		iov++;
534385f535Sderaadt 	}
544385f535Sderaadt 	while (len > 0) {
554385f535Sderaadt 		if (iol == 0)
568d04f523Sderaadt 			panic("cuio_copydata: empty");
574385f535Sderaadt 		count = min(iov->iov_len - off, len);
584385f535Sderaadt 		bcopy(((caddr_t)iov->iov_base) + off, cp, count);
594385f535Sderaadt 		len -= count;
604385f535Sderaadt 		cp += count;
614385f535Sderaadt 		off = 0;
624385f535Sderaadt 		iol--;
634385f535Sderaadt 		iov++;
644385f535Sderaadt 	}
654385f535Sderaadt }
664385f535Sderaadt 
674385f535Sderaadt void
cuio_copyback(struct uio * uio,int off,int len,const void * _cp)683957c1c0Spedro cuio_copyback(struct uio *uio, int off, int len, const void *_cp)
694385f535Sderaadt {
708d04f523Sderaadt 	struct iovec *iov = uio->uio_iov;
718d04f523Sderaadt 	int iol = uio->uio_iovcnt;
724385f535Sderaadt 	unsigned count;
733957c1c0Spedro 	caddr_t cp = (caddr_t)_cp;
744385f535Sderaadt 
754385f535Sderaadt 	if (off < 0)
768d04f523Sderaadt 		panic("cuio_copyback: off %d < 0", off);
774385f535Sderaadt 	if (len < 0)
788d04f523Sderaadt 		panic("cuio_copyback: len %d < 0", len);
794385f535Sderaadt 	while (off > 0) {
804385f535Sderaadt 		if (iol == 0)
818d04f523Sderaadt 			panic("cuio_copyback: empty in skip");
824385f535Sderaadt 		if (off < iov->iov_len)
834385f535Sderaadt 			break;
844385f535Sderaadt 		off -= iov->iov_len;
854385f535Sderaadt 		iol--;
864385f535Sderaadt 		iov++;
874385f535Sderaadt 	}
884385f535Sderaadt 	while (len > 0) {
894385f535Sderaadt 		if (iol == 0)
908d04f523Sderaadt 			panic("uio_copyback: empty");
914385f535Sderaadt 		count = min(iov->iov_len - off, len);
924385f535Sderaadt 		bcopy(cp, ((caddr_t)iov->iov_base) + off, count);
934385f535Sderaadt 		len -= count;
944385f535Sderaadt 		cp += count;
954385f535Sderaadt 		off = 0;
964385f535Sderaadt 		iol--;
974385f535Sderaadt 		iov++;
984385f535Sderaadt 	}
994385f535Sderaadt }
100e1fbdbb0Sprovos 
101e1fbdbb0Sprovos int
cuio_getptr(struct uio * uio,int loc,int * off)102e1fbdbb0Sprovos cuio_getptr(struct uio *uio, int loc, int *off)
103e1fbdbb0Sprovos {
104e1fbdbb0Sprovos 	int ind, len;
105e1fbdbb0Sprovos 
106e1fbdbb0Sprovos 	ind = 0;
107e1fbdbb0Sprovos 	while (loc >= 0 && ind < uio->uio_iovcnt) {
108e1fbdbb0Sprovos 		len = uio->uio_iov[ind].iov_len;
109e1fbdbb0Sprovos 		if (len > loc) {
110e1fbdbb0Sprovos 			*off = loc;
111e1fbdbb0Sprovos 			return (ind);
112e1fbdbb0Sprovos 		}
113e1fbdbb0Sprovos 		loc -= len;
114e1fbdbb0Sprovos 		ind++;
115e1fbdbb0Sprovos 	}
116e1fbdbb0Sprovos 
117e1fbdbb0Sprovos 	if (ind > 0 && loc == 0) {
118e1fbdbb0Sprovos 		ind--;
119e1fbdbb0Sprovos 		*off = uio->uio_iov[ind].iov_len;
120e1fbdbb0Sprovos 		return (ind);
121e1fbdbb0Sprovos 	}
122e1fbdbb0Sprovos 
123e1fbdbb0Sprovos 	return (-1);
124e1fbdbb0Sprovos }
125e1fbdbb0Sprovos 
126e1fbdbb0Sprovos int
cuio_apply(struct uio * uio,int off,int len,int (* f)(caddr_t,caddr_t,unsigned int),caddr_t fstate)127e1fbdbb0Sprovos cuio_apply(struct uio *uio, int off, int len,
128e1fbdbb0Sprovos     int (*f)(caddr_t, caddr_t, unsigned int), caddr_t fstate)
129e1fbdbb0Sprovos {
130e1fbdbb0Sprovos 	int rval, ind, uiolen;
131e1fbdbb0Sprovos 	unsigned int count;
132e1fbdbb0Sprovos 
133e1fbdbb0Sprovos 	if (len < 0)
134c74b805aSmarkus 		panic("cuio_apply: len %d < 0", len);
135e1fbdbb0Sprovos 	if (off < 0)
136c74b805aSmarkus 		panic("cuio_apply: off %d < 0", off);
137e1fbdbb0Sprovos 
138e1fbdbb0Sprovos 	ind = 0;
139e1fbdbb0Sprovos 	while (off > 0) {
140e1fbdbb0Sprovos 		if (ind >= uio->uio_iovcnt)
14128e1891fSjmc 			panic("cuio_apply: ind %d >= uio_iovcnt %d for off",
142c74b805aSmarkus 			    ind, uio->uio_iovcnt);
143e1fbdbb0Sprovos 		uiolen = uio->uio_iov[ind].iov_len;
144e1fbdbb0Sprovos 		if (off < uiolen)
145e1fbdbb0Sprovos 			break;
146e1fbdbb0Sprovos 		off -= uiolen;
147e1fbdbb0Sprovos 		ind++;
148e1fbdbb0Sprovos 	}
149e1fbdbb0Sprovos 	while (len > 0) {
150e1fbdbb0Sprovos 		if (ind >= uio->uio_iovcnt)
15128e1891fSjmc 			panic("cuio_apply: ind %d >= uio_iovcnt %d for len",
152c74b805aSmarkus 			    ind, uio->uio_iovcnt);
153e1fbdbb0Sprovos 		count = min(uio->uio_iov[ind].iov_len - off, len);
154e1fbdbb0Sprovos 
1553957c1c0Spedro 		rval = f(fstate, (char *)uio->uio_iov[ind].iov_base + off,
1563957c1c0Spedro 		    count);
157e1fbdbb0Sprovos 		if (rval)
158e1fbdbb0Sprovos 			return (rval);
159e1fbdbb0Sprovos 
160e1fbdbb0Sprovos 		len -= count;
161e1fbdbb0Sprovos 		off = 0;
162e1fbdbb0Sprovos 		ind++;
163e1fbdbb0Sprovos 	}
164e1fbdbb0Sprovos 
165e1fbdbb0Sprovos 	return (0);
166e1fbdbb0Sprovos }
167