xref: /netbsd-src/sys/lib/libkern/copystr.c (revision ca08b3e761e6fc5454aa5d028935f5d82d92ea3d)
1*ca08b3e7Smaxv /*	$NetBSD: copystr.c,v 1.1 2020/06/30 16:20:02 maxv Exp $	*/
2*ca08b3e7Smaxv 
3*ca08b3e7Smaxv /*
4*ca08b3e7Smaxv  * Copyright (c) 2020 The NetBSD Foundation, Inc.
5*ca08b3e7Smaxv  * All rights reserved.
6*ca08b3e7Smaxv  *
7*ca08b3e7Smaxv  * This code is derived from software contributed to The NetBSD Foundation
8*ca08b3e7Smaxv  * by Maxime Villard.
9*ca08b3e7Smaxv  *
10*ca08b3e7Smaxv  * Redistribution and use in source and binary forms, with or without
11*ca08b3e7Smaxv  * modification, are permitted provided that the following conditions
12*ca08b3e7Smaxv  * are met:
13*ca08b3e7Smaxv  * 1. Redistributions of source code must retain the above copyright
14*ca08b3e7Smaxv  *    notice, this list of conditions and the following disclaimer.
15*ca08b3e7Smaxv  * 2. Redistributions in binary form must reproduce the above copyright
16*ca08b3e7Smaxv  *    notice, this list of conditions and the following disclaimer in the
17*ca08b3e7Smaxv  *    documentation and/or other materials provided with the distribution.
18*ca08b3e7Smaxv  *
19*ca08b3e7Smaxv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*ca08b3e7Smaxv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*ca08b3e7Smaxv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*ca08b3e7Smaxv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*ca08b3e7Smaxv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*ca08b3e7Smaxv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*ca08b3e7Smaxv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*ca08b3e7Smaxv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*ca08b3e7Smaxv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*ca08b3e7Smaxv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*ca08b3e7Smaxv  * POSSIBILITY OF SUCH DAMAGE.
30*ca08b3e7Smaxv  */
31*ca08b3e7Smaxv 
32*ca08b3e7Smaxv #include <sys/systm.h>
33*ca08b3e7Smaxv #include <sys/errno.h>
34*ca08b3e7Smaxv 
35*ca08b3e7Smaxv int
copystr(const void * kfaddr,void * kdaddr,size_t len,size_t * done)36*ca08b3e7Smaxv copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
37*ca08b3e7Smaxv {
38*ca08b3e7Smaxv 	const char *src = kfaddr;
39*ca08b3e7Smaxv 	char *dst = kdaddr;
40*ca08b3e7Smaxv 	size_t i;
41*ca08b3e7Smaxv 
42*ca08b3e7Smaxv 	for (i = 0; i < len; i++) {
43*ca08b3e7Smaxv 		if ((*dst++ = *src++) == '\0') {
44*ca08b3e7Smaxv 			if (done)
45*ca08b3e7Smaxv 				*done = i + 1;
46*ca08b3e7Smaxv 			return 0;
47*ca08b3e7Smaxv 		}
48*ca08b3e7Smaxv 	}
49*ca08b3e7Smaxv 
50*ca08b3e7Smaxv 	if (done)
51*ca08b3e7Smaxv 		*done = i;
52*ca08b3e7Smaxv 
53*ca08b3e7Smaxv 	return ENAMETOOLONG;
54*ca08b3e7Smaxv }
55