VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
b24413180f560 (Greg Kroah-Hartman             2017-11-01 15:07:57 +0100    1) // SPDX-License-Identifier: GPL-2.0
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700    2) /*
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700    3)  *  linux/lib/string.c
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700    4)  *
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700    5)  *  Copyright (C) 1991, 1992  Linus Torvalds
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700    6)  */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700    7) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700    8) /*
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700    9)  * stupid library routines.. The optimized versions should generally be found
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   10)  * as inline code in <asm-xx/string.h>
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   11)  *
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   12)  * These are buggy as well..
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   13)  *
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   14)  * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   15)  * -  Added strsep() which will replace strtok() soon (because strsep() is
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   16)  *    reentrant and should be faster). Use only strsep() in new code, please.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   17)  *
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   18)  * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   19)  *                    Matthew Hawkins <matt@mh.dropbear.id.au>
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   20)  * -  Kissed strtok() goodbye
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   21)  */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   22) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   23) #include <linux/types.h>
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   24) #include <linux/string.h>
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   25) #include <linux/ctype.h>
8bc3bcc93a2b4 (Paul Gortmaker                 2011-11-16 21:29:17 -0500   26) #include <linux/kernel.h>
8bc3bcc93a2b4 (Paul Gortmaker                 2011-11-16 21:29:17 -0500   27) #include <linux/export.h>
50af5ead3b44c (Paul Gortmaker                 2012-01-20 18:35:53 -0500   28) #include <linux/bug.h>
8bc3bcc93a2b4 (Paul Gortmaker                 2011-11-16 21:29:17 -0500   29) #include <linux/errno.h>
ce76d938dd988 (Alexander Shishkin             2018-10-05 15:43:05 +0300   30) #include <linux/slab.h>
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   31) 
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400   32) #include <asm/byteorder.h>
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400   33) #include <asm/word-at-a-time.h>
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400   34) #include <asm/page.h>
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400   35) 
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000   36) #define BYTES_LONG	sizeof(long)
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000   37) #define WORD_MASK	(BYTES_LONG - 1)
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000   38) #define MIN_THRESHOLD	(BYTES_LONG * 2)
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000   39) 
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000   40) /* convenience union to avoid cast between different pointer types */
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000   41) union types {
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000   42) 	u8 *as_u8;
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000   43) 	unsigned long *as_ulong;
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000   44) 	uintptr_t as_uptr;
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000   45) };
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000   46) 
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000   47) union const_types {
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000   48) 	const u8 *as_u8;
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000   49) 	const unsigned long *as_ulong;
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000   50) 	uintptr_t as_uptr;
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000   51) };
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000   52) 
cd514e727b18f (Rasmus Villemoes               2014-10-13 15:54:25 -0700   53) #ifndef __HAVE_ARCH_STRNCASECMP
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   54) /**
cd514e727b18f (Rasmus Villemoes               2014-10-13 15:54:25 -0700   55)  * strncasecmp - Case insensitive, length-limited string comparison
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   56)  * @s1: One string
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   57)  * @s2: The other string
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   58)  * @len: the maximum number of characters to compare
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   59)  */
cd514e727b18f (Rasmus Villemoes               2014-10-13 15:54:25 -0700   60) int strncasecmp(const char *s1, const char *s2, size_t len)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   61) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   62) 	/* Yes, Virginia, it had better be unsigned */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   63) 	unsigned char c1, c2;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   64) 
a11d2b64e1f25 (André Goddard Rosa             2010-03-05 13:43:11 -0800   65) 	if (!len)
a11d2b64e1f25 (André Goddard Rosa             2010-03-05 13:43:11 -0800   66) 		return 0;
a11d2b64e1f25 (André Goddard Rosa             2010-03-05 13:43:11 -0800   67) 
a11d2b64e1f25 (André Goddard Rosa             2010-03-05 13:43:11 -0800   68) 	do {
a11d2b64e1f25 (André Goddard Rosa             2010-03-05 13:43:11 -0800   69) 		c1 = *s1++;
a11d2b64e1f25 (André Goddard Rosa             2010-03-05 13:43:11 -0800   70) 		c2 = *s2++;
a11d2b64e1f25 (André Goddard Rosa             2010-03-05 13:43:11 -0800   71) 		if (!c1 || !c2)
a11d2b64e1f25 (André Goddard Rosa             2010-03-05 13:43:11 -0800   72) 			break;
a11d2b64e1f25 (André Goddard Rosa             2010-03-05 13:43:11 -0800   73) 		if (c1 == c2)
a11d2b64e1f25 (André Goddard Rosa             2010-03-05 13:43:11 -0800   74) 			continue;
a11d2b64e1f25 (André Goddard Rosa             2010-03-05 13:43:11 -0800   75) 		c1 = tolower(c1);
a11d2b64e1f25 (André Goddard Rosa             2010-03-05 13:43:11 -0800   76) 		c2 = tolower(c2);
a11d2b64e1f25 (André Goddard Rosa             2010-03-05 13:43:11 -0800   77) 		if (c1 != c2)
a11d2b64e1f25 (André Goddard Rosa             2010-03-05 13:43:11 -0800   78) 			break;
a11d2b64e1f25 (André Goddard Rosa             2010-03-05 13:43:11 -0800   79) 	} while (--len);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   80) 	return (int)c1 - (int)c2;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   81) }
cd514e727b18f (Rasmus Villemoes               2014-10-13 15:54:25 -0700   82) EXPORT_SYMBOL(strncasecmp);
cd514e727b18f (Rasmus Villemoes               2014-10-13 15:54:25 -0700   83) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   84) 
ded220bd8f082 (David S. Miller                2007-03-29 01:18:42 -0700   85) #ifndef __HAVE_ARCH_STRCASECMP
ded220bd8f082 (David S. Miller                2007-03-29 01:18:42 -0700   86) int strcasecmp(const char *s1, const char *s2)
ded220bd8f082 (David S. Miller                2007-03-29 01:18:42 -0700   87) {
ded220bd8f082 (David S. Miller                2007-03-29 01:18:42 -0700   88) 	int c1, c2;
ded220bd8f082 (David S. Miller                2007-03-29 01:18:42 -0700   89) 
ded220bd8f082 (David S. Miller                2007-03-29 01:18:42 -0700   90) 	do {
ded220bd8f082 (David S. Miller                2007-03-29 01:18:42 -0700   91) 		c1 = tolower(*s1++);
ded220bd8f082 (David S. Miller                2007-03-29 01:18:42 -0700   92) 		c2 = tolower(*s2++);
ded220bd8f082 (David S. Miller                2007-03-29 01:18:42 -0700   93) 	} while (c1 == c2 && c1 != 0);
ded220bd8f082 (David S. Miller                2007-03-29 01:18:42 -0700   94) 	return c1 - c2;
ded220bd8f082 (David S. Miller                2007-03-29 01:18:42 -0700   95) }
ded220bd8f082 (David S. Miller                2007-03-29 01:18:42 -0700   96) EXPORT_SYMBOL(strcasecmp);
ded220bd8f082 (David S. Miller                2007-03-29 01:18:42 -0700   97) #endif
ded220bd8f082 (David S. Miller                2007-03-29 01:18:42 -0700   98) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700   99) #ifndef __HAVE_ARCH_STRCPY
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  100) /**
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  101)  * strcpy - Copy a %NUL terminated string
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  102)  * @dest: Where to copy the string to
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  103)  * @src: Where to copy the string from
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  104)  */
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  105) char *strcpy(char *dest, const char *src)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  106) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  107) 	char *tmp = dest;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  108) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  109) 	while ((*dest++ = *src++) != '\0')
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  110) 		/* nothing */;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  111) 	return tmp;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  112) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  113) EXPORT_SYMBOL(strcpy);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  114) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  115) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  116) #ifndef __HAVE_ARCH_STRNCPY
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  117) /**
0046dd9fed0c9 (Dan Carpenter                  2014-06-04 16:11:47 -0700  118)  * strncpy - Copy a length-limited, C-string
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  119)  * @dest: Where to copy the string to
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  120)  * @src: Where to copy the string from
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  121)  * @count: The maximum number of bytes to copy
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  122)  *
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  123)  * The result is not %NUL-terminated if the source exceeds
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  124)  * @count bytes.
252795264df50 (walter harms                   2005-05-05 16:16:20 -0700  125)  *
252795264df50 (walter harms                   2005-05-05 16:16:20 -0700  126)  * In the case where the length of @src is less than  that  of
252795264df50 (walter harms                   2005-05-05 16:16:20 -0700  127)  * count, the remainder of @dest will be padded with %NUL.
252795264df50 (walter harms                   2005-05-05 16:16:20 -0700  128)  *
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  129)  */
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  130) char *strncpy(char *dest, const char *src, size_t count)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  131) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  132) 	char *tmp = dest;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  133) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  134) 	while (count) {
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  135) 		if ((*tmp = *src) != 0)
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  136) 			src++;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  137) 		tmp++;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  138) 		count--;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  139) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  140) 	return dest;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  141) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  142) EXPORT_SYMBOL(strncpy);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  143) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  144) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  145) #ifndef __HAVE_ARCH_STRLCPY
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  146) /**
0046dd9fed0c9 (Dan Carpenter                  2014-06-04 16:11:47 -0700  147)  * strlcpy - Copy a C-string into a sized buffer
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  148)  * @dest: Where to copy the string to
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  149)  * @src: Where to copy the string from
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  150)  * @size: size of destination buffer
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  151)  *
0e056eb5530da (Mauro Carvalho Chehab          2017-03-30 17:11:36 -0300  152)  * Compatible with ``*BSD``: the result is always a valid
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  153)  * NUL-terminated string that fits in the buffer (unless,
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  154)  * of course, the buffer size is zero). It does not pad
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  155)  * out the result like strncpy() does.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  156)  */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  157) size_t strlcpy(char *dest, const char *src, size_t size)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  158) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  159) 	size_t ret = strlen(src);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  160) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  161) 	if (size) {
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  162) 		size_t len = (ret >= size) ? size - 1 : ret;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  163) 		memcpy(dest, src, len);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  164) 		dest[len] = '\0';
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  165) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  166) 	return ret;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  167) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  168) EXPORT_SYMBOL(strlcpy);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  169) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  170) 
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  171) #ifndef __HAVE_ARCH_STRSCPY
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  172) /**
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  173)  * strscpy - Copy a C-string into a sized buffer
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  174)  * @dest: Where to copy the string to
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  175)  * @src: Where to copy the string from
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  176)  * @count: Size of destination buffer
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  177)  *
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  178)  * Copy the string, or as much of it as fits, into the dest buffer.  The
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  179)  * behavior is undefined if the string buffers overlap.  The destination
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  180)  * buffer is always NUL terminated, unless it's zero-sized.
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  181)  *
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  182)  * Preferred to strlcpy() since the API doesn't require reading memory
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  183)  * from the src string beyond the specified "count" bytes, and since
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  184)  * the return value is easier to error-check than strlcpy()'s.
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  185)  * In addition, the implementation is robust to the string changing out
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  186)  * from underneath it, unlike the current strlcpy() implementation.
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  187)  *
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  188)  * Preferred to strncpy() since it always returns a valid string, and
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  189)  * doesn't unnecessarily force the tail of the destination buffer to be
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  190)  * zeroed.  If zeroing is desired please use strscpy_pad().
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  191)  *
917cda2790c4b (Joe Perches                    2019-09-25 16:46:13 -0700  192)  * Returns:
917cda2790c4b (Joe Perches                    2019-09-25 16:46:13 -0700  193)  * * The number of characters copied (not including the trailing %NUL)
917cda2790c4b (Joe Perches                    2019-09-25 16:46:13 -0700  194)  * * -E2BIG if count is 0 or @src was truncated.
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  195)  */
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  196) ssize_t strscpy(char *dest, const char *src, size_t count)
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  197) {
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  198) 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  199) 	size_t max = count;
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  200) 	long res = 0;
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  201) 
9a156466147b6 (Kees Cook                      2019-09-25 16:46:20 -0700  202) 	if (count == 0 || WARN_ON_ONCE(count > INT_MAX))
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  203) 		return -E2BIG;
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  204) 
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  205) #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  206) 	/*
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  207) 	 * If src is unaligned, don't cross a page boundary,
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  208) 	 * since we don't know if the next page is mapped.
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  209) 	 */
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  210) 	if ((long)src & (sizeof(long) - 1)) {
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  211) 		size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  212) 		if (limit < max)
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  213) 			max = limit;
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  214) 	}
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  215) #else
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  216) 	/* If src or dest is unaligned, don't do word-at-a-time. */
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  217) 	if (((long) dest | (long) src) & (sizeof(long) - 1))
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  218) 		max = 0;
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  219) #endif
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  220) 
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  221) 	while (max >= sizeof(unsigned long)) {
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  222) 		unsigned long c, data;
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  223) 
1a3241ff10d03 (Andrey Ryabinin                2018-02-01 21:00:50 +0300  224) 		c = read_word_at_a_time(src+res);
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  225) 		if (has_zero(c, &data, &constants)) {
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  226) 			data = prep_zero_mask(c, data, &constants);
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  227) 			data = create_zero_mask(data);
990486c8af044 (Chris Metcalf                  2015-10-06 12:37:41 -0400  228) 			*(unsigned long *)(dest+res) = c & zero_bytemask(data);
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  229) 			return res + find_zero(data);
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  230) 		}
990486c8af044 (Chris Metcalf                  2015-10-06 12:37:41 -0400  231) 		*(unsigned long *)(dest+res) = c;
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  232) 		res += sizeof(unsigned long);
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  233) 		count -= sizeof(unsigned long);
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  234) 		max -= sizeof(unsigned long);
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  235) 	}
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  236) 
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  237) 	while (count) {
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  238) 		char c;
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  239) 
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  240) 		c = src[res];
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  241) 		dest[res] = c;
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  242) 		if (!c)
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  243) 			return res;
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  244) 		res++;
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  245) 		count--;
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  246) 	}
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  247) 
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  248) 	/* Hit buffer length without finding a NUL; force NUL-termination. */
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  249) 	if (res)
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  250) 		dest[res-1] = '\0';
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  251) 
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  252) 	return -E2BIG;
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  253) }
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  254) EXPORT_SYMBOL(strscpy);
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  255) #endif
30035e45753b7 (Chris Metcalf                  2015-04-29 12:52:04 -0400  256) 
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  257) /**
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  258)  * strscpy_pad() - Copy a C-string into a sized buffer
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  259)  * @dest: Where to copy the string to
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  260)  * @src: Where to copy the string from
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  261)  * @count: Size of destination buffer
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  262)  *
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  263)  * Copy the string, or as much of it as fits, into the dest buffer.  The
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  264)  * behavior is undefined if the string buffers overlap.  The destination
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  265)  * buffer is always %NUL terminated, unless it's zero-sized.
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  266)  *
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  267)  * If the source string is shorter than the destination buffer, zeros
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  268)  * the tail of the destination buffer.
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  269)  *
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  270)  * For full explanation of why you may want to consider using the
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  271)  * 'strscpy' functions please see the function docstring for strscpy().
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  272)  *
917cda2790c4b (Joe Perches                    2019-09-25 16:46:13 -0700  273)  * Returns:
917cda2790c4b (Joe Perches                    2019-09-25 16:46:13 -0700  274)  * * The number of characters copied (not including the trailing %NUL)
917cda2790c4b (Joe Perches                    2019-09-25 16:46:13 -0700  275)  * * -E2BIG if count is 0 or @src was truncated.
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  276)  */
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  277) ssize_t strscpy_pad(char *dest, const char *src, size_t count)
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  278) {
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  279) 	ssize_t written;
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  280) 
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  281) 	written = strscpy(dest, src, count);
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  282) 	if (written < 0 || written == count - 1)
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  283) 		return written;
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  284) 
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  285) 	memset(dest + written + 1, 0, count - written - 1);
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  286) 
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  287) 	return written;
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  288) }
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  289) EXPORT_SYMBOL(strscpy_pad);
458a3bf82df4f (Tobin C. Harding               2019-04-05 12:58:58 +1100  290) 
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  291) /**
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  292)  * stpcpy - copy a string from src to dest returning a pointer to the new end
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  293)  *          of dest, including src's %NUL-terminator. May overrun dest.
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  294)  * @dest: pointer to end of string being copied into. Must be large enough
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  295)  *        to receive copy.
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  296)  * @src: pointer to the beginning of string being copied from. Must not overlap
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  297)  *       dest.
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  298)  *
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  299)  * stpcpy differs from strcpy in a key way: the return value is a pointer
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  300)  * to the new %NUL-terminating character in @dest. (For strcpy, the return
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  301)  * value is a pointer to the start of @dest). This interface is considered
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  302)  * unsafe as it doesn't perform bounds checking of the inputs. As such it's
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  303)  * not recommended for usage. Instead, its definition is provided in case
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  304)  * the compiler lowers other libcalls to stpcpy.
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  305)  */
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  306) char *stpcpy(char *__restrict__ dest, const char *__restrict__ src);
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  307) char *stpcpy(char *__restrict__ dest, const char *__restrict__ src)
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  308) {
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  309) 	while ((*dest++ = *src++) != '\0')
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  310) 		/* nothing */;
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  311) 	return --dest;
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  312) }
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  313) EXPORT_SYMBOL(stpcpy);
1e1b6d63d6340 (Nick Desaulniers               2020-09-25 21:19:18 -0700  314) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  315) #ifndef __HAVE_ARCH_STRCAT
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  316) /**
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  317)  * strcat - Append one %NUL-terminated string to another
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  318)  * @dest: The string to be appended to
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  319)  * @src: The string to append to it
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  320)  */
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  321) char *strcat(char *dest, const char *src)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  322) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  323) 	char *tmp = dest;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  324) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  325) 	while (*dest)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  326) 		dest++;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  327) 	while ((*dest++ = *src++) != '\0')
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  328) 		;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  329) 	return tmp;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  330) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  331) EXPORT_SYMBOL(strcat);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  332) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  333) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  334) #ifndef __HAVE_ARCH_STRNCAT
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  335) /**
0046dd9fed0c9 (Dan Carpenter                  2014-06-04 16:11:47 -0700  336)  * strncat - Append a length-limited, C-string to another
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  337)  * @dest: The string to be appended to
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  338)  * @src: The string to append to it
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  339)  * @count: The maximum numbers of bytes to copy
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  340)  *
72fd4a35a8243 (Robert P. J. Day               2007-02-10 01:45:59 -0800  341)  * Note that in contrast to strncpy(), strncat() ensures the result is
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  342)  * terminated.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  343)  */
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  344) char *strncat(char *dest, const char *src, size_t count)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  345) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  346) 	char *tmp = dest;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  347) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  348) 	if (count) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  349) 		while (*dest)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  350) 			dest++;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  351) 		while ((*dest++ = *src++) != 0) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  352) 			if (--count == 0) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  353) 				*dest = '\0';
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  354) 				break;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  355) 			}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  356) 		}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  357) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  358) 	return tmp;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  359) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  360) EXPORT_SYMBOL(strncat);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  361) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  362) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  363) #ifndef __HAVE_ARCH_STRLCAT
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  364) /**
0046dd9fed0c9 (Dan Carpenter                  2014-06-04 16:11:47 -0700  365)  * strlcat - Append a length-limited, C-string to another
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  366)  * @dest: The string to be appended to
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  367)  * @src: The string to append to it
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  368)  * @count: The size of the destination buffer.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  369)  */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  370) size_t strlcat(char *dest, const char *src, size_t count)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  371) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  372) 	size_t dsize = strlen(dest);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  373) 	size_t len = strlen(src);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  374) 	size_t res = dsize + len;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  375) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  376) 	/* This would be a bug */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  377) 	BUG_ON(dsize >= count);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  378) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  379) 	dest += dsize;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  380) 	count -= dsize;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  381) 	if (len >= count)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  382) 		len = count-1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  383) 	memcpy(dest, src, len);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  384) 	dest[len] = 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  385) 	return res;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  386) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  387) EXPORT_SYMBOL(strlcat);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  388) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  389) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  390) #ifndef __HAVE_ARCH_STRCMP
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  391) /**
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  392)  * strcmp - Compare two strings
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  393)  * @cs: One string
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  394)  * @ct: Another string
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  395)  */
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  396) int strcmp(const char *cs, const char *ct)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  397) {
a414f01ac2899 (Linus Torvalds                 2009-11-18 22:31:52 +0100  398) 	unsigned char c1, c2;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  399) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  400) 	while (1) {
a414f01ac2899 (Linus Torvalds                 2009-11-18 22:31:52 +0100  401) 		c1 = *cs++;
a414f01ac2899 (Linus Torvalds                 2009-11-18 22:31:52 +0100  402) 		c2 = *ct++;
a414f01ac2899 (Linus Torvalds                 2009-11-18 22:31:52 +0100  403) 		if (c1 != c2)
a414f01ac2899 (Linus Torvalds                 2009-11-18 22:31:52 +0100  404) 			return c1 < c2 ? -1 : 1;
a414f01ac2899 (Linus Torvalds                 2009-11-18 22:31:52 +0100  405) 		if (!c1)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  406) 			break;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  407) 	}
a414f01ac2899 (Linus Torvalds                 2009-11-18 22:31:52 +0100  408) 	return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  409) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  410) EXPORT_SYMBOL(strcmp);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  411) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  412) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  413) #ifndef __HAVE_ARCH_STRNCMP
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  414) /**
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  415)  * strncmp - Compare two length-limited strings
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  416)  * @cs: One string
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  417)  * @ct: Another string
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  418)  * @count: The maximum number of bytes to compare
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  419)  */
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  420) int strncmp(const char *cs, const char *ct, size_t count)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  421) {
a414f01ac2899 (Linus Torvalds                 2009-11-18 22:31:52 +0100  422) 	unsigned char c1, c2;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  423) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  424) 	while (count) {
a414f01ac2899 (Linus Torvalds                 2009-11-18 22:31:52 +0100  425) 		c1 = *cs++;
a414f01ac2899 (Linus Torvalds                 2009-11-18 22:31:52 +0100  426) 		c2 = *ct++;
a414f01ac2899 (Linus Torvalds                 2009-11-18 22:31:52 +0100  427) 		if (c1 != c2)
a414f01ac2899 (Linus Torvalds                 2009-11-18 22:31:52 +0100  428) 			return c1 < c2 ? -1 : 1;
a414f01ac2899 (Linus Torvalds                 2009-11-18 22:31:52 +0100  429) 		if (!c1)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  430) 			break;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  431) 		count--;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  432) 	}
a414f01ac2899 (Linus Torvalds                 2009-11-18 22:31:52 +0100  433) 	return 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  434) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  435) EXPORT_SYMBOL(strncmp);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  436) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  437) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  438) #ifndef __HAVE_ARCH_STRCHR
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  439) /**
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  440)  * strchr - Find the first occurrence of a character in a string
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  441)  * @s: The string to be searched
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  442)  * @c: The character to search for
b09757104e433 (Peter Rosin                    2019-07-16 16:27:15 -0700  443)  *
b09757104e433 (Peter Rosin                    2019-07-16 16:27:15 -0700  444)  * Note that the %NUL-terminator is considered part of the string, and can
b09757104e433 (Peter Rosin                    2019-07-16 16:27:15 -0700  445)  * be searched for.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  446)  */
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  447) char *strchr(const char *s, int c)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  448) {
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  449) 	for (; *s != (char)c; ++s)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  450) 		if (*s == '\0')
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  451) 			return NULL;
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  452) 	return (char *)s;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  453) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  454) EXPORT_SYMBOL(strchr);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  455) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  456) 
11d200e95f3e8 (Grant Likely                   2014-03-14 17:00:14 +0000  457) #ifndef __HAVE_ARCH_STRCHRNUL
11d200e95f3e8 (Grant Likely                   2014-03-14 17:00:14 +0000  458) /**
11d200e95f3e8 (Grant Likely                   2014-03-14 17:00:14 +0000  459)  * strchrnul - Find and return a character in a string, or end of string
11d200e95f3e8 (Grant Likely                   2014-03-14 17:00:14 +0000  460)  * @s: The string to be searched
11d200e95f3e8 (Grant Likely                   2014-03-14 17:00:14 +0000  461)  * @c: The character to search for
11d200e95f3e8 (Grant Likely                   2014-03-14 17:00:14 +0000  462)  *
11d200e95f3e8 (Grant Likely                   2014-03-14 17:00:14 +0000  463)  * Returns pointer to first occurrence of 'c' in s. If c is not found, then
11d200e95f3e8 (Grant Likely                   2014-03-14 17:00:14 +0000  464)  * return a pointer to the null byte at the end of s.
11d200e95f3e8 (Grant Likely                   2014-03-14 17:00:14 +0000  465)  */
11d200e95f3e8 (Grant Likely                   2014-03-14 17:00:14 +0000  466) char *strchrnul(const char *s, int c)
11d200e95f3e8 (Grant Likely                   2014-03-14 17:00:14 +0000  467) {
11d200e95f3e8 (Grant Likely                   2014-03-14 17:00:14 +0000  468) 	while (*s && *s != (char)c)
11d200e95f3e8 (Grant Likely                   2014-03-14 17:00:14 +0000  469) 		s++;
11d200e95f3e8 (Grant Likely                   2014-03-14 17:00:14 +0000  470) 	return (char *)s;
11d200e95f3e8 (Grant Likely                   2014-03-14 17:00:14 +0000  471) }
11d200e95f3e8 (Grant Likely                   2014-03-14 17:00:14 +0000  472) EXPORT_SYMBOL(strchrnul);
11d200e95f3e8 (Grant Likely                   2014-03-14 17:00:14 +0000  473) #endif
11d200e95f3e8 (Grant Likely                   2014-03-14 17:00:14 +0000  474) 
0bee0cece2a6a (Yury Norov                     2020-02-03 17:37:20 -0800  475) /**
0bee0cece2a6a (Yury Norov                     2020-02-03 17:37:20 -0800  476)  * strnchrnul - Find and return a character in a length limited string,
0bee0cece2a6a (Yury Norov                     2020-02-03 17:37:20 -0800  477)  * or end of string
0bee0cece2a6a (Yury Norov                     2020-02-03 17:37:20 -0800  478)  * @s: The string to be searched
0bee0cece2a6a (Yury Norov                     2020-02-03 17:37:20 -0800  479)  * @count: The number of characters to be searched
0bee0cece2a6a (Yury Norov                     2020-02-03 17:37:20 -0800  480)  * @c: The character to search for
0bee0cece2a6a (Yury Norov                     2020-02-03 17:37:20 -0800  481)  *
0bee0cece2a6a (Yury Norov                     2020-02-03 17:37:20 -0800  482)  * Returns pointer to the first occurrence of 'c' in s. If c is not found,
0bee0cece2a6a (Yury Norov                     2020-02-03 17:37:20 -0800  483)  * then return a pointer to the last character of the string.
0bee0cece2a6a (Yury Norov                     2020-02-03 17:37:20 -0800  484)  */
0bee0cece2a6a (Yury Norov                     2020-02-03 17:37:20 -0800  485) char *strnchrnul(const char *s, size_t count, int c)
0bee0cece2a6a (Yury Norov                     2020-02-03 17:37:20 -0800  486) {
0bee0cece2a6a (Yury Norov                     2020-02-03 17:37:20 -0800  487) 	while (count-- && *s && *s != (char)c)
0bee0cece2a6a (Yury Norov                     2020-02-03 17:37:20 -0800  488) 		s++;
0bee0cece2a6a (Yury Norov                     2020-02-03 17:37:20 -0800  489) 	return (char *)s;
0bee0cece2a6a (Yury Norov                     2020-02-03 17:37:20 -0800  490) }
0bee0cece2a6a (Yury Norov                     2020-02-03 17:37:20 -0800  491) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  492) #ifndef __HAVE_ARCH_STRRCHR
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  493) /**
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  494)  * strrchr - Find the last occurrence of a character in a string
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  495)  * @s: The string to be searched
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  496)  * @c: The character to search for
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  497)  */
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  498) char *strrchr(const char *s, int c)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  499) {
8da53d4595a53 (Rasmus Villemoes               2015-02-13 14:36:44 -0800  500) 	const char *last = NULL;
8da53d4595a53 (Rasmus Villemoes               2015-02-13 14:36:44 -0800  501) 	do {
8da53d4595a53 (Rasmus Villemoes               2015-02-13 14:36:44 -0800  502) 		if (*s == (char)c)
8da53d4595a53 (Rasmus Villemoes               2015-02-13 14:36:44 -0800  503) 			last = s;
8da53d4595a53 (Rasmus Villemoes               2015-02-13 14:36:44 -0800  504) 	} while (*s++);
8da53d4595a53 (Rasmus Villemoes               2015-02-13 14:36:44 -0800  505) 	return (char *)last;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  506) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  507) EXPORT_SYMBOL(strrchr);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  508) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  509) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  510) #ifndef __HAVE_ARCH_STRNCHR
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  511) /**
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  512)  * strnchr - Find a character in a length limited string
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  513)  * @s: The string to be searched
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  514)  * @count: The number of characters to be searched
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  515)  * @c: The character to search for
b09757104e433 (Peter Rosin                    2019-07-16 16:27:15 -0700  516)  *
b09757104e433 (Peter Rosin                    2019-07-16 16:27:15 -0700  517)  * Note that the %NUL-terminator is considered part of the string, and can
b09757104e433 (Peter Rosin                    2019-07-16 16:27:15 -0700  518)  * be searched for.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  519)  */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  520) char *strnchr(const char *s, size_t count, int c)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  521) {
b09757104e433 (Peter Rosin                    2019-07-16 16:27:15 -0700  522) 	while (count--) {
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  523) 		if (*s == (char)c)
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  524) 			return (char *)s;
b09757104e433 (Peter Rosin                    2019-07-16 16:27:15 -0700  525) 		if (*s++ == '\0')
b09757104e433 (Peter Rosin                    2019-07-16 16:27:15 -0700  526) 			break;
b09757104e433 (Peter Rosin                    2019-07-16 16:27:15 -0700  527) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  528) 	return NULL;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  529) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  530) EXPORT_SYMBOL(strnchr);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  531) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  532) 
f653398c86a1c (André Goddard Rosa             2009-12-14 18:01:04 -0800  533) /**
a6cd13f3c98d1 (Randy Dunlap                   2009-12-21 14:37:22 -0800  534)  * skip_spaces - Removes leading whitespace from @str.
a6cd13f3c98d1 (Randy Dunlap                   2009-12-21 14:37:22 -0800  535)  * @str: The string to be stripped.
f653398c86a1c (André Goddard Rosa             2009-12-14 18:01:04 -0800  536)  *
a6cd13f3c98d1 (Randy Dunlap                   2009-12-21 14:37:22 -0800  537)  * Returns a pointer to the first non-whitespace character in @str.
f653398c86a1c (André Goddard Rosa             2009-12-14 18:01:04 -0800  538)  */
f653398c86a1c (André Goddard Rosa             2009-12-14 18:01:04 -0800  539) char *skip_spaces(const char *str)
f653398c86a1c (André Goddard Rosa             2009-12-14 18:01:04 -0800  540) {
f653398c86a1c (André Goddard Rosa             2009-12-14 18:01:04 -0800  541) 	while (isspace(*str))
f653398c86a1c (André Goddard Rosa             2009-12-14 18:01:04 -0800  542) 		++str;
f653398c86a1c (André Goddard Rosa             2009-12-14 18:01:04 -0800  543) 	return (char *)str;
f653398c86a1c (André Goddard Rosa             2009-12-14 18:01:04 -0800  544) }
f653398c86a1c (André Goddard Rosa             2009-12-14 18:01:04 -0800  545) EXPORT_SYMBOL(skip_spaces);
f653398c86a1c (André Goddard Rosa             2009-12-14 18:01:04 -0800  546) 
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  547) /**
ca54cb8c9eb38 (KOSAKI Motohiro                2009-12-14 18:01:15 -0800  548)  * strim - Removes leading and trailing whitespace from @s.
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  549)  * @s: The string to be stripped.
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  550)  *
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  551)  * Note that the first trailing whitespace is replaced with a %NUL-terminator
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  552)  * in the given string @s. Returns a pointer to the first non-whitespace
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  553)  * character in @s.
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  554)  */
ca54cb8c9eb38 (KOSAKI Motohiro                2009-12-14 18:01:15 -0800  555) char *strim(char *s)
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  556) {
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  557) 	size_t size;
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  558) 	char *end;
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  559) 
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  560) 	size = strlen(s);
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  561) 	if (!size)
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  562) 		return s;
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  563) 
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  564) 	end = s + size - 1;
6e6d9fa6f95e3 (Michael Holzheu                2006-10-28 10:38:47 -0700  565) 	while (end >= s && isspace(*end))
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  566) 		end--;
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  567) 	*(end + 1) = '\0';
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  568) 
66f6958e69d80 (Michael Holzheu                2011-10-31 17:12:37 -0700  569) 	return skip_spaces(s);
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  570) }
ca54cb8c9eb38 (KOSAKI Motohiro                2009-12-14 18:01:15 -0800  571) EXPORT_SYMBOL(strim);
481fad483487e (Pekka Enberg                   2006-06-23 02:05:44 -0700  572) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  573) #ifndef __HAVE_ARCH_STRLEN
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  574) /**
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  575)  * strlen - Find the length of a string
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  576)  * @s: The string to be sized
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  577)  */
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  578) size_t strlen(const char *s)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  579) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  580) 	const char *sc;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  581) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  582) 	for (sc = s; *sc != '\0'; ++sc)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  583) 		/* nothing */;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  584) 	return sc - s;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  585) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  586) EXPORT_SYMBOL(strlen);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  587) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  588) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  589) #ifndef __HAVE_ARCH_STRNLEN
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  590) /**
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  591)  * strnlen - Find the length of a length-limited string
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  592)  * @s: The string to be sized
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  593)  * @count: The maximum number of bytes to search
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  594)  */
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  595) size_t strnlen(const char *s, size_t count)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  596) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  597) 	const char *sc;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  598) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  599) 	for (sc = s; count-- && *sc != '\0'; ++sc)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  600) 		/* nothing */;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  601) 	return sc - s;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  602) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  603) EXPORT_SYMBOL(strnlen);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  604) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  605) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  606) #ifndef __HAVE_ARCH_STRSPN
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  607) /**
72fd4a35a8243 (Robert P. J. Day               2007-02-10 01:45:59 -0800  608)  * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  609)  * @s: The string to be searched
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  610)  * @accept: The string to search for
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  611)  */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  612) size_t strspn(const char *s, const char *accept)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  613) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  614) 	const char *p;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  615) 	const char *a;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  616) 	size_t count = 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  617) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  618) 	for (p = s; *p != '\0'; ++p) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  619) 		for (a = accept; *a != '\0'; ++a) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  620) 			if (*p == *a)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  621) 				break;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  622) 		}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  623) 		if (*a == '\0')
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  624) 			return count;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  625) 		++count;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  626) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  627) 	return count;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  628) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  629) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  630) EXPORT_SYMBOL(strspn);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  631) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  632) 
8833d328caf00 (Kyle McMartin                  2006-04-10 22:53:57 -0700  633) #ifndef __HAVE_ARCH_STRCSPN
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  634) /**
72fd4a35a8243 (Robert P. J. Day               2007-02-10 01:45:59 -0800  635)  * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  636)  * @s: The string to be searched
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  637)  * @reject: The string to avoid
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  638)  */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  639) size_t strcspn(const char *s, const char *reject)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  640) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  641) 	const char *p;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  642) 	const char *r;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  643) 	size_t count = 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  644) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  645) 	for (p = s; *p != '\0'; ++p) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  646) 		for (r = reject; *r != '\0'; ++r) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  647) 			if (*p == *r)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  648) 				return count;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  649) 		}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  650) 		++count;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  651) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  652) 	return count;
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  653) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  654) EXPORT_SYMBOL(strcspn);
8833d328caf00 (Kyle McMartin                  2006-04-10 22:53:57 -0700  655) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  656) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  657) #ifndef __HAVE_ARCH_STRPBRK
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  658) /**
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  659)  * strpbrk - Find the first occurrence of a set of characters
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  660)  * @cs: The string to be searched
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  661)  * @ct: The characters to search for
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  662)  */
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  663) char *strpbrk(const char *cs, const char *ct)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  664) {
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  665) 	const char *sc1, *sc2;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  666) 
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  667) 	for (sc1 = cs; *sc1 != '\0'; ++sc1) {
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  668) 		for (sc2 = ct; *sc2 != '\0'; ++sc2) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  669) 			if (*sc1 == *sc2)
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  670) 				return (char *)sc1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  671) 		}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  672) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  673) 	return NULL;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  674) }
894b5779ceeab (Kyle McMartin                  2006-04-10 22:53:56 -0700  675) EXPORT_SYMBOL(strpbrk);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  676) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  677) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  678) #ifndef __HAVE_ARCH_STRSEP
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  679) /**
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  680)  * strsep - Split a string into tokens
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  681)  * @s: The string to be searched
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  682)  * @ct: The characters to search for
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  683)  *
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  684)  * strsep() updates @s to point after the token, ready for the next call.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  685)  *
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  686)  * It returns empty tokens, too, behaving exactly like the libc function
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  687)  * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  688)  * Same semantics, slimmer shape. ;)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  689)  */
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  690) char *strsep(char **s, const char *ct)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  691) {
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  692) 	char *sbegin = *s;
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  693) 	char *end;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  694) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  695) 	if (sbegin == NULL)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  696) 		return NULL;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  697) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  698) 	end = strpbrk(sbegin, ct);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  699) 	if (end)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  700) 		*end++ = '\0';
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  701) 	*s = end;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  702) 	return sbegin;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  703) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  704) EXPORT_SYMBOL(strsep);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  705) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  706) 
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  707) /**
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  708)  * sysfs_streq - return true if strings are equal, modulo trailing newline
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  709)  * @s1: one string
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  710)  * @s2: another string
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  711)  *
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  712)  * This routine returns true iff two strings are equal, treating both
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  713)  * NUL and newline-then-NUL as equivalent string terminations.  It's
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  714)  * geared for use with sysfs input strings, which generally terminate
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  715)  * with newlines but are compared against values without newlines.
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  716)  */
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  717) bool sysfs_streq(const char *s1, const char *s2)
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  718) {
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  719) 	while (*s1 && *s1 == *s2) {
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  720) 		s1++;
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  721) 		s2++;
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  722) 	}
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  723) 
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  724) 	if (*s1 == *s2)
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  725) 		return true;
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  726) 	if (!*s1 && *s2 == '\n' && !s2[1])
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  727) 		return true;
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  728) 	if (*s1 == '\n' && !s1[1] && !*s2)
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  729) 		return true;
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  730) 	return false;
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  731) }
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  732) EXPORT_SYMBOL(sysfs_streq);
34990cf702bdf (David Brownell                 2008-05-01 04:34:42 -0700  733) 
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  734) /**
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  735)  * match_string - matches given string in an array
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  736)  * @array:	array of strings
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  737)  * @n:		number of strings in the array or -1 for NULL terminated arrays
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  738)  * @string:	string to match with
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  739)  *
c11d3fa0116a6 (Alexandru Ardelean             2020-02-20 20:04:21 -0800  740)  * This routine will look for a string in an array of strings up to the
c11d3fa0116a6 (Alexandru Ardelean             2020-02-20 20:04:21 -0800  741)  * n-th element in the array or until the first NULL element.
c11d3fa0116a6 (Alexandru Ardelean             2020-02-20 20:04:21 -0800  742)  *
c11d3fa0116a6 (Alexandru Ardelean             2020-02-20 20:04:21 -0800  743)  * Historically the value of -1 for @n, was used to search in arrays that
c11d3fa0116a6 (Alexandru Ardelean             2020-02-20 20:04:21 -0800  744)  * are NULL terminated. However, the function does not make a distinction
c11d3fa0116a6 (Alexandru Ardelean             2020-02-20 20:04:21 -0800  745)  * when finishing the search: either @n elements have been compared OR
c11d3fa0116a6 (Alexandru Ardelean             2020-02-20 20:04:21 -0800  746)  * the first NULL element was found.
c11d3fa0116a6 (Alexandru Ardelean             2020-02-20 20:04:21 -0800  747)  *
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  748)  * Return:
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  749)  * index of a @string in the @array if matches, or %-EINVAL otherwise.
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  750)  */
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  751) int match_string(const char * const *array, size_t n, const char *string)
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  752) {
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  753) 	int index;
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  754) 	const char *item;
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  755) 
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  756) 	for (index = 0; index < n; index++) {
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  757) 		item = array[index];
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  758) 		if (!item)
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  759) 			break;
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  760) 		if (!strcmp(item, string))
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  761) 			return index;
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  762) 	}
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  763) 
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  764) 	return -EINVAL;
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  765) }
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  766) EXPORT_SYMBOL(match_string);
56b060814e2d8 (Andy Shevchenko                2016-03-17 14:22:14 -0700  767) 
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  768) /**
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  769)  * __sysfs_match_string - matches given string in an array
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  770)  * @array: array of strings
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  771)  * @n: number of strings in the array or -1 for NULL terminated arrays
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  772)  * @str: string to match with
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  773)  *
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  774)  * Returns index of @str in the @array or -EINVAL, just like match_string().
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  775)  * Uses sysfs_streq instead of strcmp for matching.
c11d3fa0116a6 (Alexandru Ardelean             2020-02-20 20:04:21 -0800  776)  *
c11d3fa0116a6 (Alexandru Ardelean             2020-02-20 20:04:21 -0800  777)  * This routine will look for a string in an array of strings up to the
c11d3fa0116a6 (Alexandru Ardelean             2020-02-20 20:04:21 -0800  778)  * n-th element in the array or until the first NULL element.
c11d3fa0116a6 (Alexandru Ardelean             2020-02-20 20:04:21 -0800  779)  *
c11d3fa0116a6 (Alexandru Ardelean             2020-02-20 20:04:21 -0800  780)  * Historically the value of -1 for @n, was used to search in arrays that
c11d3fa0116a6 (Alexandru Ardelean             2020-02-20 20:04:21 -0800  781)  * are NULL terminated. However, the function does not make a distinction
c11d3fa0116a6 (Alexandru Ardelean             2020-02-20 20:04:21 -0800  782)  * when finishing the search: either @n elements have been compared OR
c11d3fa0116a6 (Alexandru Ardelean             2020-02-20 20:04:21 -0800  783)  * the first NULL element was found.
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  784)  */
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  785) int __sysfs_match_string(const char * const *array, size_t n, const char *str)
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  786) {
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  787) 	const char *item;
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  788) 	int index;
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  789) 
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  790) 	for (index = 0; index < n; index++) {
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  791) 		item = array[index];
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  792) 		if (!item)
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  793) 			break;
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  794) 		if (sysfs_streq(item, str))
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  795) 			return index;
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  796) 	}
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  797) 
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  798) 	return -EINVAL;
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  799) }
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  800) EXPORT_SYMBOL(__sysfs_match_string);
e1fe7b6a7b376 (Heikki Krogerus                2017-03-21 13:56:46 +0200  801) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  802) #ifndef __HAVE_ARCH_MEMSET
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  803) /**
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  804)  * memset - Fill a region of memory with the given value
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  805)  * @s: Pointer to the start of the area.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  806)  * @c: The byte to fill the area with
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  807)  * @count: The size of the area.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  808)  *
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  809)  * Do not use memset() to access IO space, use memset_io() instead.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  810)  */
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  811) void *memset(void *s, int c, size_t count)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  812) {
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  813) 	union types dest = { .as_u8 = s };
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  814) 
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  815) 	if (count >= MIN_THRESHOLD) {
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  816) 		unsigned long cu = (unsigned long)c;
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  817) 
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  818) 		/* Compose an ulong with 'c' repeated 4/8 times */
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  819) #ifdef CONFIG_ARCH_HAS_FAST_MULTIPLIER
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  820) 		cu *= 0x0101010101010101UL;
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  821) #else
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  822) 		cu |= cu << 8;
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  823) 		cu |= cu << 16;
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  824) 		/* Suppress warning on 32 bit machines */
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  825) 		cu |= (cu << 16) << 16;
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  826) #endif
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  827) 		if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  828) 			/*
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  829) 			 * Fill the buffer one byte at time until
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  830) 			 * the destination is word aligned.
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  831) 			 */
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  832) 			for (; count && dest.as_uptr & WORD_MASK; count--)
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  833) 				*dest.as_u8++ = c;
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  834) 		}
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  835) 
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  836) 		/* Copy using the largest size allowed */
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  837) 		for (; count >= BYTES_LONG; count -= BYTES_LONG)
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  838) 			*dest.as_ulong++ = cu;
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  839) 	}
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  840) 
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  841) 	/* copy the remainder */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  842) 	while (count--)
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  843) 		*dest.as_u8++ = c;
c162df4b67ae3 (Matteo Croce                   2021-07-16 13:45:50 +1000  844) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  845) 	return s;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  846) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  847) EXPORT_SYMBOL(memset);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  848) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  849) 
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  850) #ifndef __HAVE_ARCH_MEMSET16
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  851) /**
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  852)  * memset16() - Fill a memory area with a uint16_t
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  853)  * @s: Pointer to the start of the area.
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  854)  * @v: The value to fill the area with
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  855)  * @count: The number of values to store
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  856)  *
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  857)  * Differs from memset() in that it fills with a uint16_t instead
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  858)  * of a byte.  Remember that @count is the number of uint16_ts to
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  859)  * store, not the number of bytes.
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  860)  */
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  861) void *memset16(uint16_t *s, uint16_t v, size_t count)
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  862) {
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  863) 	uint16_t *xs = s;
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  864) 
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  865) 	while (count--)
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  866) 		*xs++ = v;
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  867) 	return s;
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  868) }
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  869) EXPORT_SYMBOL(memset16);
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  870) #endif
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  871) 
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  872) #ifndef __HAVE_ARCH_MEMSET32
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  873) /**
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  874)  * memset32() - Fill a memory area with a uint32_t
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  875)  * @s: Pointer to the start of the area.
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  876)  * @v: The value to fill the area with
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  877)  * @count: The number of values to store
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  878)  *
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  879)  * Differs from memset() in that it fills with a uint32_t instead
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  880)  * of a byte.  Remember that @count is the number of uint32_ts to
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  881)  * store, not the number of bytes.
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  882)  */
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  883) void *memset32(uint32_t *s, uint32_t v, size_t count)
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  884) {
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  885) 	uint32_t *xs = s;
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  886) 
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  887) 	while (count--)
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  888) 		*xs++ = v;
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  889) 	return s;
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  890) }
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  891) EXPORT_SYMBOL(memset32);
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  892) #endif
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  893) 
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  894) #ifndef __HAVE_ARCH_MEMSET64
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  895) /**
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  896)  * memset64() - Fill a memory area with a uint64_t
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  897)  * @s: Pointer to the start of the area.
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  898)  * @v: The value to fill the area with
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  899)  * @count: The number of values to store
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  900)  *
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  901)  * Differs from memset() in that it fills with a uint64_t instead
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  902)  * of a byte.  Remember that @count is the number of uint64_ts to
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  903)  * store, not the number of bytes.
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  904)  */
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  905) void *memset64(uint64_t *s, uint64_t v, size_t count)
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  906) {
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  907) 	uint64_t *xs = s;
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  908) 
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  909) 	while (count--)
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  910) 		*xs++ = v;
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  911) 	return s;
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  912) }
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  913) EXPORT_SYMBOL(memset64);
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  914) #endif
3b3c4babd8987 (Matthew Wilcox                 2017-09-08 16:13:48 -0700  915) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  916) #ifndef __HAVE_ARCH_MEMCPY
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  917) 
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  918) #ifdef __BIG_ENDIAN
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  919) #define MERGE_UL(h, l, d) ((h) << ((d) * 8) | (l) >> ((BYTES_LONG - (d)) * 8))
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  920) #else
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  921) #define MERGE_UL(h, l, d) ((h) >> ((d) * 8) | (l) << ((BYTES_LONG - (d)) * 8))
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  922) #endif
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  923) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  924) /**
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  925)  * memcpy - Copy one area of memory to another
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  926)  * @dest: Where to copy to
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  927)  * @src: Where to copy from
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  928)  * @count: The size of the area.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  929)  *
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  930)  * You should not use this function to access IO space, use memcpy_toio()
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  931)  * or memcpy_fromio() instead.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  932)  */
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800  933) void *memcpy(void *dest, const void *src, size_t count)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  934) {
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  935) 	union const_types s = { .as_u8 = src };
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  936) 	union types d = { .as_u8 = dest };
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  937) 	int distance = 0;
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  938) 
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  939) 	if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  940) 		if (count < MIN_THRESHOLD)
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  941) 			goto copy_remainder;
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  942) 
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  943) 		/* Copy a byte at time until destination is aligned. */
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  944) 		for (; d.as_uptr & WORD_MASK; count--)
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  945) 			*d.as_u8++ = *s.as_u8++;
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  946) 
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  947) 		distance = s.as_uptr & WORD_MASK;
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  948) 	}
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  949) 
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  950) 	if (distance) {
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  951) 		unsigned long last, next;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  952) 
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  953) 		/*
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  954) 		 * s is distance bytes ahead of d, and d just reached
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  955) 		 * the alignment boundary. Move s backward to word align it
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  956) 		 * and shift data to compensate for distance, in order to do
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  957) 		 * word-by-word copy.
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  958) 		 */
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  959) 		s.as_u8 -= distance;
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  960) 
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  961) 		next = s.as_ulong[0];
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  962) 		for (; count >= BYTES_LONG; count -= BYTES_LONG) {
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  963) 			last = next;
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  964) 			next = s.as_ulong[1];
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  965) 
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  966) 			d.as_ulong[0] = MERGE_UL(last, next, distance);
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  967) 
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  968) 			d.as_ulong++;
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  969) 			s.as_ulong++;
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  970) 		}
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  971) 
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  972) 		/* Restore s with the original offset. */
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  973) 		s.as_u8 += distance;
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  974) 	} else {
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  975) 		/*
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  976) 		 * If the source and dest lower bits are the same, do a simple
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  977) 		 * 32/64 bit wide copy.
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  978) 		 */
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  979) 		for (; count >= BYTES_LONG; count -= BYTES_LONG)
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  980) 			*d.as_ulong++ = *s.as_ulong++;
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  981) 	}
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  982) 
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  983) copy_remainder:
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  984) 	while (count--)
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  985) 		*d.as_u8++ = *s.as_u8++;
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  986) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  987) 	return dest;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  988) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  989) EXPORT_SYMBOL(memcpy);
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  990) 
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  991) #undef MERGE_UL
e1172aaf9e9b4 (Matteo Croce                   2021-07-16 13:45:48 +1000  992) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  993) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  994) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  995) #ifndef __HAVE_ARCH_MEMMOVE
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  996) /**
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  997)  * memmove - Copy one area of memory to another
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  998)  * @dest: Where to copy to
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700  999)  * @src: Where to copy from
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1000)  * @count: The size of the area.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1001)  *
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1002)  * Unlike memcpy(), memmove() copes with overlapping areas.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1003)  */
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800 1004) void *memmove(void *dest, const void *src, size_t count)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1005) {
ff339342dced6 (Matteo Croce                   2021-07-16 13:45:49 +1000 1006) 	if (dest < src || src + count <= dest)
ff339342dced6 (Matteo Croce                   2021-07-16 13:45:49 +1000 1007) 		return memcpy(dest, src, count);
ff339342dced6 (Matteo Croce                   2021-07-16 13:45:49 +1000 1008) 
ff339342dced6 (Matteo Croce                   2021-07-16 13:45:49 +1000 1009) 	if (dest > src) {
ff339342dced6 (Matteo Croce                   2021-07-16 13:45:49 +1000 1010) 		const char *s = src + count;
ff339342dced6 (Matteo Croce                   2021-07-16 13:45:49 +1000 1011) 		char *tmp = dest + count;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1012) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1013) 		while (count--)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1014) 			*--tmp = *--s;
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800 1015) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1016) 	return dest;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1017) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1018) EXPORT_SYMBOL(memmove);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1019) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1020) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1021) #ifndef __HAVE_ARCH_MEMCMP
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1022) /**
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1023)  * memcmp - Compare two areas of memory
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1024)  * @cs: One area of memory
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1025)  * @ct: Another area of memory
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1026)  * @count: The size of the area.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1027)  */
0c28130b5c9e8 (Paolo 'Blaisorblade' Giarrusso 2005-05-05 16:15:17 -0700 1028) #undef memcmp
a7330c997d0f7 (Andi Kleen                     2014-02-08 08:52:06 +0100 1029) __visible int memcmp(const void *cs, const void *ct, size_t count)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1030) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1031) 	const unsigned char *su1, *su2;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1032) 	int res = 0;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1033) 
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800 1034) 	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1035) 		if ((res = *su1 - *su2) != 0)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1036) 			break;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1037) 	return res;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1038) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1039) EXPORT_SYMBOL(memcmp);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1040) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1041) 
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1042) #ifndef __HAVE_ARCH_BCMP
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1043) /**
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1044)  * bcmp - returns 0 if and only if the buffers have identical contents.
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1045)  * @a: pointer to first buffer.
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1046)  * @b: pointer to second buffer.
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1047)  * @len: size of buffers.
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1048)  *
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1049)  * The sign or magnitude of a non-zero return value has no particular
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1050)  * meaning, and architectures may implement their own more efficient bcmp(). So
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1051)  * while this particular implementation is a simple (tail) call to memcmp, do
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1052)  * not rely on anything but whether the return value is zero or non-zero.
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1053)  */
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1054) int bcmp(const void *a, const void *b, size_t len)
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1055) {
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1056) 	return memcmp(a, b, len);
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1057) }
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1058) EXPORT_SYMBOL(bcmp);
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1059) #endif
5f074f3e192f1 (Nick Desaulniers               2019-04-05 18:38:45 -0700 1060) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1061) #ifndef __HAVE_ARCH_MEMSCAN
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1062) /**
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1063)  * memscan - Find a character in an area of memory.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1064)  * @addr: The memory area
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1065)  * @c: The byte to search for
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1066)  * @size: The size of the area.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1067)  *
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1068)  * returns the address of the first occurrence of @c, or 1 byte past
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1069)  * the area if @c is not found
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1070)  */
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800 1071) void *memscan(void *addr, int c, size_t size)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1072) {
850b924792669 (Jesper Juhl                    2005-10-30 15:02:13 -0800 1073) 	unsigned char *p = addr;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1074) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1075) 	while (size) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1076) 		if (*p == c)
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800 1077) 			return (void *)p;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1078) 		p++;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1079) 		size--;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1080) 	}
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800 1081)   	return (void *)p;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1082) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1083) EXPORT_SYMBOL(memscan);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1084) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1085) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1086) #ifndef __HAVE_ARCH_STRSTR
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1087) /**
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1088)  * strstr - Find the first substring in a %NUL terminated string
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1089)  * @s1: The string to be searched
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1090)  * @s2: The string to search for
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1091)  */
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800 1092) char *strstr(const char *s1, const char *s2)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1093) {
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1094) 	size_t l1, l2;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1095) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1096) 	l2 = strlen(s2);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1097) 	if (!l2)
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800 1098) 		return (char *)s1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1099) 	l1 = strlen(s1);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1100) 	while (l1 >= l2) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1101) 		l1--;
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800 1102) 		if (!memcmp(s1, s2, l2))
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800 1103) 			return (char *)s1;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1104) 		s1++;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1105) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1106) 	return NULL;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1107) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1108) EXPORT_SYMBOL(strstr);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1109) #endif
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1110) 
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1111) #ifndef __HAVE_ARCH_STRNSTR
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1112) /**
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1113)  * strnstr - Find the first substring in a length-limited string
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1114)  * @s1: The string to be searched
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1115)  * @s2: The string to search for
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1116)  * @len: the maximum number of characters to search
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1117)  */
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1118) char *strnstr(const char *s1, const char *s2, size_t len)
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1119) {
d6a2eedfddcde (André Goddard Rosa             2010-03-05 13:43:12 -0800 1120) 	size_t l2;
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1121) 
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1122) 	l2 = strlen(s2);
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1123) 	if (!l2)
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1124) 		return (char *)s1;
d6a2eedfddcde (André Goddard Rosa             2010-03-05 13:43:12 -0800 1125) 	while (len >= l2) {
d6a2eedfddcde (André Goddard Rosa             2010-03-05 13:43:12 -0800 1126) 		len--;
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1127) 		if (!memcmp(s1, s2, l2))
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1128) 			return (char *)s1;
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1129) 		s1++;
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1130) 	}
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1131) 	return NULL;
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1132) }
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1133) EXPORT_SYMBOL(strnstr);
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1134) #endif
d5f1fb53353ed (Li Zefan                       2010-01-14 10:53:55 +0800 1135) 
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1136) #ifndef __HAVE_ARCH_MEMCHR
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1137) /**
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1138)  * memchr - Find a character in an area of memory.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1139)  * @s: The memory area
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1140)  * @c: The byte to search for
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1141)  * @n: The size of the area.
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1142)  *
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1143)  * returns the address of the first occurrence of @c, or %NULL
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1144)  * if @c is not found
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1145)  */
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1146) void *memchr(const void *s, int c, size_t n)
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1147) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1148) 	const unsigned char *p = s;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1149) 	while (n-- != 0) {
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1150)         	if ((unsigned char)c == *p++) {
51a0f0f658b0e (Jesper Juhl                    2005-10-30 15:02:11 -0800 1151) 			return (void *)(p - 1);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1152) 		}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1153) 	}
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1154) 	return NULL;
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1155) }
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1156) EXPORT_SYMBOL(memchr);
^1da177e4c3f4 (Linus Torvalds                 2005-04-16 15:20:36 -0700 1157) #endif
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1158) 
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1159) static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1160) {
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1161) 	while (bytes) {
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1162) 		if (*start != value)
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1163) 			return (void *)start;
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1164) 		start++;
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1165) 		bytes--;
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1166) 	}
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1167) 	return NULL;
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1168) }
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1169) 
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1170) /**
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1171)  * memchr_inv - Find an unmatching character in an area of memory.
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1172)  * @start: The memory area
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1173)  * @c: Find a character other than c
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1174)  * @bytes: The size of the area.
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1175)  *
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1176)  * returns the address of the first character other than @c, or %NULL
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1177)  * if the whole buffer contains just @c.
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1178)  */
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1179) void *memchr_inv(const void *start, int c, size_t bytes)
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1180) {
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1181) 	u8 value = c;
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1182) 	u64 value64;
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1183) 	unsigned int words, prefix;
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1184) 
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1185) 	if (bytes <= 16)
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1186) 		return check_bytes8(start, value, bytes);
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1187) 
f43804bf5f9ae (Akinobu Mita                   2012-03-23 15:02:14 -0700 1188) 	value64 = value;
72d931046030b (Linus Torvalds                 2014-09-13 11:14:53 -0700 1189) #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
3368e8fbcda53 (Andy Shevchenko                2015-11-10 14:45:14 -0800 1190) 	value64 *= 0x0101010101010101ULL;
72d931046030b (Linus Torvalds                 2014-09-13 11:14:53 -0700 1191) #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
f43804bf5f9ae (Akinobu Mita                   2012-03-23 15:02:14 -0700 1192) 	value64 *= 0x01010101;
f43804bf5f9ae (Akinobu Mita                   2012-03-23 15:02:14 -0700 1193) 	value64 |= value64 << 32;
f43804bf5f9ae (Akinobu Mita                   2012-03-23 15:02:14 -0700 1194) #else
f43804bf5f9ae (Akinobu Mita                   2012-03-23 15:02:14 -0700 1195) 	value64 |= value64 << 8;
f43804bf5f9ae (Akinobu Mita                   2012-03-23 15:02:14 -0700 1196) 	value64 |= value64 << 16;
f43804bf5f9ae (Akinobu Mita                   2012-03-23 15:02:14 -0700 1197) 	value64 |= value64 << 32;
f43804bf5f9ae (Akinobu Mita                   2012-03-23 15:02:14 -0700 1198) #endif
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1199) 
f43804bf5f9ae (Akinobu Mita                   2012-03-23 15:02:14 -0700 1200) 	prefix = (unsigned long)start % 8;
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1201) 	if (prefix) {
f43804bf5f9ae (Akinobu Mita                   2012-03-23 15:02:14 -0700 1202) 		u8 *r;
f43804bf5f9ae (Akinobu Mita                   2012-03-23 15:02:14 -0700 1203) 
f43804bf5f9ae (Akinobu Mita                   2012-03-23 15:02:14 -0700 1204) 		prefix = 8 - prefix;
f43804bf5f9ae (Akinobu Mita                   2012-03-23 15:02:14 -0700 1205) 		r = check_bytes8(start, value, prefix);
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1206) 		if (r)
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1207) 			return r;
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1208) 		start += prefix;
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1209) 		bytes -= prefix;
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1210) 	}
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1211) 
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1212) 	words = bytes / 8;
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1213) 
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1214) 	while (words) {
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1215) 		if (*(u64 *)start != value64)
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1216) 			return check_bytes8(start, value, 8);
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1217) 		start += 8;
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1218) 		words--;
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1219) 	}
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1220) 
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1221) 	return check_bytes8(start, value, bytes % 8);
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1222) }
798248206b59a (Akinobu Mita                   2011-10-31 17:08:07 -0700 1223) EXPORT_SYMBOL(memchr_inv);
94df290404cd0 (Rasmus Villemoes               2015-06-25 15:02:22 -0700 1224) 
94df290404cd0 (Rasmus Villemoes               2015-06-25 15:02:22 -0700 1225) /**
94df290404cd0 (Rasmus Villemoes               2015-06-25 15:02:22 -0700 1226)  * strreplace - Replace all occurrences of character in string.
94df290404cd0 (Rasmus Villemoes               2015-06-25 15:02:22 -0700 1227)  * @s: The string to operate on.
94df290404cd0 (Rasmus Villemoes               2015-06-25 15:02:22 -0700 1228)  * @old: The character being replaced.
94df290404cd0 (Rasmus Villemoes               2015-06-25 15:02:22 -0700 1229)  * @new: The character @old is replaced with.
94df290404cd0 (Rasmus Villemoes               2015-06-25 15:02:22 -0700 1230)  *
94df290404cd0 (Rasmus Villemoes               2015-06-25 15:02:22 -0700 1231)  * Returns pointer to the nul byte at the end of @s.
94df290404cd0 (Rasmus Villemoes               2015-06-25 15:02:22 -0700 1232)  */
94df290404cd0 (Rasmus Villemoes               2015-06-25 15:02:22 -0700 1233) char *strreplace(char *s, char old, char new)
94df290404cd0 (Rasmus Villemoes               2015-06-25 15:02:22 -0700 1234) {
94df290404cd0 (Rasmus Villemoes               2015-06-25 15:02:22 -0700 1235) 	for (; *s; ++s)
94df290404cd0 (Rasmus Villemoes               2015-06-25 15:02:22 -0700 1236) 		if (*s == old)
94df290404cd0 (Rasmus Villemoes               2015-06-25 15:02:22 -0700 1237) 			*s = new;
94df290404cd0 (Rasmus Villemoes               2015-06-25 15:02:22 -0700 1238) 	return s;
94df290404cd0 (Rasmus Villemoes               2015-06-25 15:02:22 -0700 1239) }
94df290404cd0 (Rasmus Villemoes               2015-06-25 15:02:22 -0700 1240) EXPORT_SYMBOL(strreplace);
6974f0c4555e2 (Daniel Micay                   2017-07-12 14:36:10 -0700 1241) 
6974f0c4555e2 (Daniel Micay                   2017-07-12 14:36:10 -0700 1242) void fortify_panic(const char *name)
6974f0c4555e2 (Daniel Micay                   2017-07-12 14:36:10 -0700 1243) {
6974f0c4555e2 (Daniel Micay                   2017-07-12 14:36:10 -0700 1244) 	pr_emerg("detected buffer overflow in %s\n", name);
6974f0c4555e2 (Daniel Micay                   2017-07-12 14:36:10 -0700 1245) 	BUG();
6974f0c4555e2 (Daniel Micay                   2017-07-12 14:36:10 -0700 1246) }
6974f0c4555e2 (Daniel Micay                   2017-07-12 14:36:10 -0700 1247) EXPORT_SYMBOL(fortify_panic);