xref: /openbmc/linux/lib/string.c (revision f9cfb191)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/lib/string.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds /*
9cfecea6eSKees Cook  * This file should be used only for "library" routines that may have
10cfecea6eSKees Cook  * alternative implementations on specific architectures (generally
11cfecea6eSKees Cook  * found in <asm-xx/string.h>), or get overloaded by FORTIFY_SOURCE.
12cfecea6eSKees Cook  * (Specifically, this file is built with __NO_FORTIFY.)
131da177e4SLinus Torvalds  *
14cfecea6eSKees Cook  * Other helper functions should live in string_helpers.c.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
17cfecea6eSKees Cook #define __NO_FORTIFY
181da177e4SLinus Torvalds #include <linux/types.h>
191da177e4SLinus Torvalds #include <linux/string.h>
201da177e4SLinus Torvalds #include <linux/ctype.h>
218bc3bcc9SPaul Gortmaker #include <linux/kernel.h>
228bc3bcc9SPaul Gortmaker #include <linux/export.h>
2350af5eadSPaul Gortmaker #include <linux/bug.h>
248bc3bcc9SPaul Gortmaker #include <linux/errno.h>
25ce76d938SAlexander Shishkin #include <linux/slab.h>
261da177e4SLinus Torvalds 
27291d47ccSLinus Torvalds #include <asm/unaligned.h>
2830035e45SChris Metcalf #include <asm/byteorder.h>
2930035e45SChris Metcalf #include <asm/word-at-a-time.h>
3030035e45SChris Metcalf #include <asm/page.h>
3130035e45SChris Metcalf 
32cd514e72SRasmus Villemoes #ifndef __HAVE_ARCH_STRNCASECMP
331da177e4SLinus Torvalds /**
34cd514e72SRasmus Villemoes  * strncasecmp - Case insensitive, length-limited string comparison
351da177e4SLinus Torvalds  * @s1: One string
361da177e4SLinus Torvalds  * @s2: The other string
371da177e4SLinus Torvalds  * @len: the maximum number of characters to compare
381da177e4SLinus Torvalds  */
strncasecmp(const char * s1,const char * s2,size_t len)39cd514e72SRasmus Villemoes int strncasecmp(const char *s1, const char *s2, size_t len)
401da177e4SLinus Torvalds {
411da177e4SLinus Torvalds 	/* Yes, Virginia, it had better be unsigned */
421da177e4SLinus Torvalds 	unsigned char c1, c2;
431da177e4SLinus Torvalds 
44a11d2b64SAndré Goddard Rosa 	if (!len)
45a11d2b64SAndré Goddard Rosa 		return 0;
46a11d2b64SAndré Goddard Rosa 
471da177e4SLinus Torvalds 	do {
48a11d2b64SAndré Goddard Rosa 		c1 = *s1++;
49a11d2b64SAndré Goddard Rosa 		c2 = *s2++;
50a11d2b64SAndré Goddard Rosa 		if (!c1 || !c2)
511da177e4SLinus Torvalds 			break;
521da177e4SLinus Torvalds 		if (c1 == c2)
531da177e4SLinus Torvalds 			continue;
541da177e4SLinus Torvalds 		c1 = tolower(c1);
551da177e4SLinus Torvalds 		c2 = tolower(c2);
561da177e4SLinus Torvalds 		if (c1 != c2)
571da177e4SLinus Torvalds 			break;
581da177e4SLinus Torvalds 	} while (--len);
591da177e4SLinus Torvalds 	return (int)c1 - (int)c2;
601da177e4SLinus Torvalds }
61cd514e72SRasmus Villemoes EXPORT_SYMBOL(strncasecmp);
62cd514e72SRasmus Villemoes #endif
631da177e4SLinus Torvalds 
64ded220bdSDavid S. Miller #ifndef __HAVE_ARCH_STRCASECMP
strcasecmp(const char * s1,const char * s2)65ded220bdSDavid S. Miller int strcasecmp(const char *s1, const char *s2)
66ded220bdSDavid S. Miller {
67ded220bdSDavid S. Miller 	int c1, c2;
68ded220bdSDavid S. Miller 
69ded220bdSDavid S. Miller 	do {
70ded220bdSDavid S. Miller 		c1 = tolower(*s1++);
71ded220bdSDavid S. Miller 		c2 = tolower(*s2++);
72ded220bdSDavid S. Miller 	} while (c1 == c2 && c1 != 0);
73ded220bdSDavid S. Miller 	return c1 - c2;
74ded220bdSDavid S. Miller }
75ded220bdSDavid S. Miller EXPORT_SYMBOL(strcasecmp);
76ded220bdSDavid S. Miller #endif
77ded220bdSDavid S. Miller 
781da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRCPY
strcpy(char * dest,const char * src)791da177e4SLinus Torvalds char *strcpy(char *dest, const char *src)
801da177e4SLinus Torvalds {
811da177e4SLinus Torvalds 	char *tmp = dest;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	while ((*dest++ = *src++) != '\0')
841da177e4SLinus Torvalds 		/* nothing */;
851da177e4SLinus Torvalds 	return tmp;
861da177e4SLinus Torvalds }
871da177e4SLinus Torvalds EXPORT_SYMBOL(strcpy);
881da177e4SLinus Torvalds #endif
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRNCPY
strncpy(char * dest,const char * src,size_t count)911da177e4SLinus Torvalds char *strncpy(char *dest, const char *src, size_t count)
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds 	char *tmp = dest;
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	while (count) {
9651a0f0f6SJesper Juhl 		if ((*tmp = *src) != 0)
9751a0f0f6SJesper Juhl 			src++;
981da177e4SLinus Torvalds 		tmp++;
991da177e4SLinus Torvalds 		count--;
1001da177e4SLinus Torvalds 	}
1011da177e4SLinus Torvalds 	return dest;
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds EXPORT_SYMBOL(strncpy);
1041da177e4SLinus Torvalds #endif
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRLCPY
strlcpy(char * dest,const char * src,size_t size)1071da177e4SLinus Torvalds size_t strlcpy(char *dest, const char *src, size_t size)
1081da177e4SLinus Torvalds {
1091da177e4SLinus Torvalds 	size_t ret = strlen(src);
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	if (size) {
1121da177e4SLinus Torvalds 		size_t len = (ret >= size) ? size - 1 : ret;
113*f9cfb191SAlexander Potapenko 		__builtin_memcpy(dest, src, len);
1141da177e4SLinus Torvalds 		dest[len] = '\0';
1151da177e4SLinus Torvalds 	}
1161da177e4SLinus Torvalds 	return ret;
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds EXPORT_SYMBOL(strlcpy);
1191da177e4SLinus Torvalds #endif
1201da177e4SLinus Torvalds 
12130035e45SChris Metcalf #ifndef __HAVE_ARCH_STRSCPY
strscpy(char * dest,const char * src,size_t count)12230035e45SChris Metcalf ssize_t strscpy(char *dest, const char *src, size_t count)
12330035e45SChris Metcalf {
12430035e45SChris Metcalf 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
12530035e45SChris Metcalf 	size_t max = count;
12630035e45SChris Metcalf 	long res = 0;
12730035e45SChris Metcalf 
1289a156466SKees Cook 	if (count == 0 || WARN_ON_ONCE(count > INT_MAX))
12930035e45SChris Metcalf 		return -E2BIG;
13030035e45SChris Metcalf 
13130035e45SChris Metcalf #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
13230035e45SChris Metcalf 	/*
13330035e45SChris Metcalf 	 * If src is unaligned, don't cross a page boundary,
13430035e45SChris Metcalf 	 * since we don't know if the next page is mapped.
13530035e45SChris Metcalf 	 */
13630035e45SChris Metcalf 	if ((long)src & (sizeof(long) - 1)) {
13730035e45SChris Metcalf 		size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
13830035e45SChris Metcalf 		if (limit < max)
13930035e45SChris Metcalf 			max = limit;
14030035e45SChris Metcalf 	}
14130035e45SChris Metcalf #else
14230035e45SChris Metcalf 	/* If src or dest is unaligned, don't do word-at-a-time. */
14330035e45SChris Metcalf 	if (((long) dest | (long) src) & (sizeof(long) - 1))
14430035e45SChris Metcalf 		max = 0;
14530035e45SChris Metcalf #endif
14630035e45SChris Metcalf 
1472de6f3bfSAlexander Potapenko 	/*
1482de6f3bfSAlexander Potapenko 	 * read_word_at_a_time() below may read uninitialized bytes after the
1492de6f3bfSAlexander Potapenko 	 * trailing zero and use them in comparisons. Disable this optimization
1502de6f3bfSAlexander Potapenko 	 * under KMSAN to prevent false positive reports.
1512de6f3bfSAlexander Potapenko 	 */
1522de6f3bfSAlexander Potapenko 	if (IS_ENABLED(CONFIG_KMSAN))
1532de6f3bfSAlexander Potapenko 		max = 0;
1542de6f3bfSAlexander Potapenko 
15530035e45SChris Metcalf 	while (max >= sizeof(unsigned long)) {
15630035e45SChris Metcalf 		unsigned long c, data;
15730035e45SChris Metcalf 
1581a3241ffSAndrey Ryabinin 		c = read_word_at_a_time(src+res);
15930035e45SChris Metcalf 		if (has_zero(c, &data, &constants)) {
16030035e45SChris Metcalf 			data = prep_zero_mask(c, data, &constants);
16130035e45SChris Metcalf 			data = create_zero_mask(data);
162990486c8SChris Metcalf 			*(unsigned long *)(dest+res) = c & zero_bytemask(data);
16330035e45SChris Metcalf 			return res + find_zero(data);
16430035e45SChris Metcalf 		}
165990486c8SChris Metcalf 		*(unsigned long *)(dest+res) = c;
16630035e45SChris Metcalf 		res += sizeof(unsigned long);
16730035e45SChris Metcalf 		count -= sizeof(unsigned long);
16830035e45SChris Metcalf 		max -= sizeof(unsigned long);
16930035e45SChris Metcalf 	}
17030035e45SChris Metcalf 
17130035e45SChris Metcalf 	while (count) {
17230035e45SChris Metcalf 		char c;
17330035e45SChris Metcalf 
17430035e45SChris Metcalf 		c = src[res];
17530035e45SChris Metcalf 		dest[res] = c;
17630035e45SChris Metcalf 		if (!c)
17730035e45SChris Metcalf 			return res;
17830035e45SChris Metcalf 		res++;
17930035e45SChris Metcalf 		count--;
18030035e45SChris Metcalf 	}
18130035e45SChris Metcalf 
18230035e45SChris Metcalf 	/* Hit buffer length without finding a NUL; force NUL-termination. */
18330035e45SChris Metcalf 	if (res)
18430035e45SChris Metcalf 		dest[res-1] = '\0';
18530035e45SChris Metcalf 
18630035e45SChris Metcalf 	return -E2BIG;
18730035e45SChris Metcalf }
18830035e45SChris Metcalf EXPORT_SYMBOL(strscpy);
18930035e45SChris Metcalf #endif
19030035e45SChris Metcalf 
191458a3bf8STobin C. Harding /**
1921e1b6d63SNick Desaulniers  * stpcpy - copy a string from src to dest returning a pointer to the new end
1931e1b6d63SNick Desaulniers  *          of dest, including src's %NUL-terminator. May overrun dest.
1941e1b6d63SNick Desaulniers  * @dest: pointer to end of string being copied into. Must be large enough
1951e1b6d63SNick Desaulniers  *        to receive copy.
1961e1b6d63SNick Desaulniers  * @src: pointer to the beginning of string being copied from. Must not overlap
1971e1b6d63SNick Desaulniers  *       dest.
1981e1b6d63SNick Desaulniers  *
1991e1b6d63SNick Desaulniers  * stpcpy differs from strcpy in a key way: the return value is a pointer
2001e1b6d63SNick Desaulniers  * to the new %NUL-terminating character in @dest. (For strcpy, the return
2011e1b6d63SNick Desaulniers  * value is a pointer to the start of @dest). This interface is considered
2021e1b6d63SNick Desaulniers  * unsafe as it doesn't perform bounds checking of the inputs. As such it's
2031e1b6d63SNick Desaulniers  * not recommended for usage. Instead, its definition is provided in case
2041e1b6d63SNick Desaulniers  * the compiler lowers other libcalls to stpcpy.
2051e1b6d63SNick Desaulniers  */
2061e1b6d63SNick Desaulniers char *stpcpy(char *__restrict__ dest, const char *__restrict__ src);
stpcpy(char * __restrict__ dest,const char * __restrict__ src)2071e1b6d63SNick Desaulniers char *stpcpy(char *__restrict__ dest, const char *__restrict__ src)
2081e1b6d63SNick Desaulniers {
2091e1b6d63SNick Desaulniers 	while ((*dest++ = *src++) != '\0')
2101e1b6d63SNick Desaulniers 		/* nothing */;
2111e1b6d63SNick Desaulniers 	return --dest;
2121e1b6d63SNick Desaulniers }
2131e1b6d63SNick Desaulniers EXPORT_SYMBOL(stpcpy);
2141e1b6d63SNick Desaulniers 
2151da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRCAT
strcat(char * dest,const char * src)2161da177e4SLinus Torvalds char *strcat(char *dest, const char *src)
2171da177e4SLinus Torvalds {
2181da177e4SLinus Torvalds 	char *tmp = dest;
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	while (*dest)
2211da177e4SLinus Torvalds 		dest++;
2221da177e4SLinus Torvalds 	while ((*dest++ = *src++) != '\0')
2231da177e4SLinus Torvalds 		;
2241da177e4SLinus Torvalds 	return tmp;
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds EXPORT_SYMBOL(strcat);
2271da177e4SLinus Torvalds #endif
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRNCAT
strncat(char * dest,const char * src,size_t count)2301da177e4SLinus Torvalds char *strncat(char *dest, const char *src, size_t count)
2311da177e4SLinus Torvalds {
2321da177e4SLinus Torvalds 	char *tmp = dest;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	if (count) {
2351da177e4SLinus Torvalds 		while (*dest)
2361da177e4SLinus Torvalds 			dest++;
2371da177e4SLinus Torvalds 		while ((*dest++ = *src++) != 0) {
2381da177e4SLinus Torvalds 			if (--count == 0) {
2391da177e4SLinus Torvalds 				*dest = '\0';
2401da177e4SLinus Torvalds 				break;
2411da177e4SLinus Torvalds 			}
2421da177e4SLinus Torvalds 		}
2431da177e4SLinus Torvalds 	}
2441da177e4SLinus Torvalds 	return tmp;
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds EXPORT_SYMBOL(strncat);
2471da177e4SLinus Torvalds #endif
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRLCAT
strlcat(char * dest,const char * src,size_t count)2501da177e4SLinus Torvalds size_t strlcat(char *dest, const char *src, size_t count)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	size_t dsize = strlen(dest);
2531da177e4SLinus Torvalds 	size_t len = strlen(src);
2541da177e4SLinus Torvalds 	size_t res = dsize + len;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	/* This would be a bug */
2571da177e4SLinus Torvalds 	BUG_ON(dsize >= count);
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	dest += dsize;
2601da177e4SLinus Torvalds 	count -= dsize;
2611da177e4SLinus Torvalds 	if (len >= count)
2621da177e4SLinus Torvalds 		len = count-1;
263*f9cfb191SAlexander Potapenko 	__builtin_memcpy(dest, src, len);
2641da177e4SLinus Torvalds 	dest[len] = 0;
2651da177e4SLinus Torvalds 	return res;
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds EXPORT_SYMBOL(strlcat);
2681da177e4SLinus Torvalds #endif
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRCMP
2711da177e4SLinus Torvalds /**
2721da177e4SLinus Torvalds  * strcmp - Compare two strings
2731da177e4SLinus Torvalds  * @cs: One string
2741da177e4SLinus Torvalds  * @ct: Another string
2751da177e4SLinus Torvalds  */
strcmp(const char * cs,const char * ct)2761da177e4SLinus Torvalds int strcmp(const char *cs, const char *ct)
2771da177e4SLinus Torvalds {
278a414f01aSLinus Torvalds 	unsigned char c1, c2;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	while (1) {
281a414f01aSLinus Torvalds 		c1 = *cs++;
282a414f01aSLinus Torvalds 		c2 = *ct++;
283a414f01aSLinus Torvalds 		if (c1 != c2)
284a414f01aSLinus Torvalds 			return c1 < c2 ? -1 : 1;
285a414f01aSLinus Torvalds 		if (!c1)
2861da177e4SLinus Torvalds 			break;
2871da177e4SLinus Torvalds 	}
288a414f01aSLinus Torvalds 	return 0;
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds EXPORT_SYMBOL(strcmp);
2911da177e4SLinus Torvalds #endif
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRNCMP
2941da177e4SLinus Torvalds /**
2951da177e4SLinus Torvalds  * strncmp - Compare two length-limited strings
2961da177e4SLinus Torvalds  * @cs: One string
2971da177e4SLinus Torvalds  * @ct: Another string
2981da177e4SLinus Torvalds  * @count: The maximum number of bytes to compare
2991da177e4SLinus Torvalds  */
strncmp(const char * cs,const char * ct,size_t count)3001da177e4SLinus Torvalds int strncmp(const char *cs, const char *ct, size_t count)
3011da177e4SLinus Torvalds {
302a414f01aSLinus Torvalds 	unsigned char c1, c2;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	while (count) {
305a414f01aSLinus Torvalds 		c1 = *cs++;
306a414f01aSLinus Torvalds 		c2 = *ct++;
307a414f01aSLinus Torvalds 		if (c1 != c2)
308a414f01aSLinus Torvalds 			return c1 < c2 ? -1 : 1;
309a414f01aSLinus Torvalds 		if (!c1)
3101da177e4SLinus Torvalds 			break;
3111da177e4SLinus Torvalds 		count--;
3121da177e4SLinus Torvalds 	}
313a414f01aSLinus Torvalds 	return 0;
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds EXPORT_SYMBOL(strncmp);
3161da177e4SLinus Torvalds #endif
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRCHR
3191da177e4SLinus Torvalds /**
3201da177e4SLinus Torvalds  * strchr - Find the first occurrence of a character in a string
3211da177e4SLinus Torvalds  * @s: The string to be searched
3221da177e4SLinus Torvalds  * @c: The character to search for
323b0975710SPeter Rosin  *
324b0975710SPeter Rosin  * Note that the %NUL-terminator is considered part of the string, and can
325b0975710SPeter Rosin  * be searched for.
3261da177e4SLinus Torvalds  */
strchr(const char * s,int c)3271da177e4SLinus Torvalds char *strchr(const char *s, int c)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	for (; *s != (char)c; ++s)
3301da177e4SLinus Torvalds 		if (*s == '\0')
3311da177e4SLinus Torvalds 			return NULL;
3321da177e4SLinus Torvalds 	return (char *)s;
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds EXPORT_SYMBOL(strchr);
3351da177e4SLinus Torvalds #endif
3361da177e4SLinus Torvalds 
33711d200e9SGrant Likely #ifndef __HAVE_ARCH_STRCHRNUL
33811d200e9SGrant Likely /**
33911d200e9SGrant Likely  * strchrnul - Find and return a character in a string, or end of string
34011d200e9SGrant Likely  * @s: The string to be searched
34111d200e9SGrant Likely  * @c: The character to search for
34211d200e9SGrant Likely  *
34311d200e9SGrant Likely  * Returns pointer to first occurrence of 'c' in s. If c is not found, then
34411d200e9SGrant Likely  * return a pointer to the null byte at the end of s.
34511d200e9SGrant Likely  */
strchrnul(const char * s,int c)34611d200e9SGrant Likely char *strchrnul(const char *s, int c)
34711d200e9SGrant Likely {
34811d200e9SGrant Likely 	while (*s && *s != (char)c)
34911d200e9SGrant Likely 		s++;
35011d200e9SGrant Likely 	return (char *)s;
35111d200e9SGrant Likely }
35211d200e9SGrant Likely EXPORT_SYMBOL(strchrnul);
35311d200e9SGrant Likely #endif
35411d200e9SGrant Likely 
3550bee0cecSYury Norov /**
3560bee0cecSYury Norov  * strnchrnul - Find and return a character in a length limited string,
3570bee0cecSYury Norov  * or end of string
3580bee0cecSYury Norov  * @s: The string to be searched
3590bee0cecSYury Norov  * @count: The number of characters to be searched
3600bee0cecSYury Norov  * @c: The character to search for
3610bee0cecSYury Norov  *
3620bee0cecSYury Norov  * Returns pointer to the first occurrence of 'c' in s. If c is not found,
3630bee0cecSYury Norov  * then return a pointer to the last character of the string.
3640bee0cecSYury Norov  */
strnchrnul(const char * s,size_t count,int c)3650bee0cecSYury Norov char *strnchrnul(const char *s, size_t count, int c)
3660bee0cecSYury Norov {
3670bee0cecSYury Norov 	while (count-- && *s && *s != (char)c)
3680bee0cecSYury Norov 		s++;
3690bee0cecSYury Norov 	return (char *)s;
3700bee0cecSYury Norov }
3710bee0cecSYury Norov 
3721da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRRCHR
3731da177e4SLinus Torvalds /**
3741da177e4SLinus Torvalds  * strrchr - Find the last occurrence of a character in a string
3751da177e4SLinus Torvalds  * @s: The string to be searched
3761da177e4SLinus Torvalds  * @c: The character to search for
3771da177e4SLinus Torvalds  */
strrchr(const char * s,int c)3781da177e4SLinus Torvalds char *strrchr(const char *s, int c)
3791da177e4SLinus Torvalds {
3808da53d45SRasmus Villemoes 	const char *last = NULL;
3811da177e4SLinus Torvalds 	do {
3828da53d45SRasmus Villemoes 		if (*s == (char)c)
3838da53d45SRasmus Villemoes 			last = s;
3848da53d45SRasmus Villemoes 	} while (*s++);
3858da53d45SRasmus Villemoes 	return (char *)last;
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds EXPORT_SYMBOL(strrchr);
3881da177e4SLinus Torvalds #endif
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRNCHR
3911da177e4SLinus Torvalds /**
3921da177e4SLinus Torvalds  * strnchr - Find a character in a length limited string
3931da177e4SLinus Torvalds  * @s: The string to be searched
3941da177e4SLinus Torvalds  * @count: The number of characters to be searched
3951da177e4SLinus Torvalds  * @c: The character to search for
396b0975710SPeter Rosin  *
397b0975710SPeter Rosin  * Note that the %NUL-terminator is considered part of the string, and can
398b0975710SPeter Rosin  * be searched for.
3991da177e4SLinus Torvalds  */
strnchr(const char * s,size_t count,int c)4001da177e4SLinus Torvalds char *strnchr(const char *s, size_t count, int c)
4011da177e4SLinus Torvalds {
402b0975710SPeter Rosin 	while (count--) {
4031da177e4SLinus Torvalds 		if (*s == (char)c)
4041da177e4SLinus Torvalds 			return (char *)s;
405b0975710SPeter Rosin 		if (*s++ == '\0')
406b0975710SPeter Rosin 			break;
407b0975710SPeter Rosin 	}
4081da177e4SLinus Torvalds 	return NULL;
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds EXPORT_SYMBOL(strnchr);
4111da177e4SLinus Torvalds #endif
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRLEN
strlen(const char * s)4141da177e4SLinus Torvalds size_t strlen(const char *s)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds 	const char *sc;
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	for (sc = s; *sc != '\0'; ++sc)
4191da177e4SLinus Torvalds 		/* nothing */;
4201da177e4SLinus Torvalds 	return sc - s;
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds EXPORT_SYMBOL(strlen);
4231da177e4SLinus Torvalds #endif
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRNLEN
strnlen(const char * s,size_t count)4261da177e4SLinus Torvalds size_t strnlen(const char *s, size_t count)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds 	const char *sc;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	for (sc = s; count-- && *sc != '\0'; ++sc)
4311da177e4SLinus Torvalds 		/* nothing */;
4321da177e4SLinus Torvalds 	return sc - s;
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds EXPORT_SYMBOL(strnlen);
4351da177e4SLinus Torvalds #endif
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRSPN
4381da177e4SLinus Torvalds /**
43972fd4a35SRobert P. J. Day  * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
4401da177e4SLinus Torvalds  * @s: The string to be searched
4411da177e4SLinus Torvalds  * @accept: The string to search for
4421da177e4SLinus Torvalds  */
strspn(const char * s,const char * accept)4431da177e4SLinus Torvalds size_t strspn(const char *s, const char *accept)
4441da177e4SLinus Torvalds {
4451da177e4SLinus Torvalds 	const char *p;
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	for (p = s; *p != '\0'; ++p) {
448dffad91bSRasmus Villemoes 		if (!strchr(accept, *p))
4491da177e4SLinus Torvalds 			break;
4501da177e4SLinus Torvalds 	}
451dffad91bSRasmus Villemoes 	return p - s;
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds EXPORT_SYMBOL(strspn);
4541da177e4SLinus Torvalds #endif
4551da177e4SLinus Torvalds 
4568833d328SKyle McMartin #ifndef __HAVE_ARCH_STRCSPN
4571da177e4SLinus Torvalds /**
45872fd4a35SRobert P. J. Day  * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
4591da177e4SLinus Torvalds  * @s: The string to be searched
4601da177e4SLinus Torvalds  * @reject: The string to avoid
4611da177e4SLinus Torvalds  */
strcspn(const char * s,const char * reject)4621da177e4SLinus Torvalds size_t strcspn(const char *s, const char *reject)
4631da177e4SLinus Torvalds {
4641da177e4SLinus Torvalds 	const char *p;
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	for (p = s; *p != '\0'; ++p) {
467dffad91bSRasmus Villemoes 		if (strchr(reject, *p))
468dffad91bSRasmus Villemoes 			break;
4691da177e4SLinus Torvalds 	}
470dffad91bSRasmus Villemoes 	return p - s;
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds EXPORT_SYMBOL(strcspn);
4738833d328SKyle McMartin #endif
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRPBRK
4761da177e4SLinus Torvalds /**
4771da177e4SLinus Torvalds  * strpbrk - Find the first occurrence of a set of characters
4781da177e4SLinus Torvalds  * @cs: The string to be searched
4791da177e4SLinus Torvalds  * @ct: The characters to search for
4801da177e4SLinus Torvalds  */
strpbrk(const char * cs,const char * ct)4811da177e4SLinus Torvalds char *strpbrk(const char *cs, const char *ct)
4821da177e4SLinus Torvalds {
483a8c55407SAndy Shevchenko 	const char *sc;
4841da177e4SLinus Torvalds 
485a8c55407SAndy Shevchenko 	for (sc = cs; *sc != '\0'; ++sc) {
486a8c55407SAndy Shevchenko 		if (strchr(ct, *sc))
487a8c55407SAndy Shevchenko 			return (char *)sc;
4881da177e4SLinus Torvalds 	}
4891da177e4SLinus Torvalds 	return NULL;
4901da177e4SLinus Torvalds }
491894b5779SKyle McMartin EXPORT_SYMBOL(strpbrk);
4921da177e4SLinus Torvalds #endif
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRSEP
4951da177e4SLinus Torvalds /**
4961da177e4SLinus Torvalds  * strsep - Split a string into tokens
4971da177e4SLinus Torvalds  * @s: The string to be searched
4981da177e4SLinus Torvalds  * @ct: The characters to search for
4991da177e4SLinus Torvalds  *
5001da177e4SLinus Torvalds  * strsep() updates @s to point after the token, ready for the next call.
5011da177e4SLinus Torvalds  *
5021da177e4SLinus Torvalds  * It returns empty tokens, too, behaving exactly like the libc function
5031da177e4SLinus Torvalds  * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
5041da177e4SLinus Torvalds  * Same semantics, slimmer shape. ;)
5051da177e4SLinus Torvalds  */
strsep(char ** s,const char * ct)5061da177e4SLinus Torvalds char *strsep(char **s, const char *ct)
5071da177e4SLinus Torvalds {
50851a0f0f6SJesper Juhl 	char *sbegin = *s;
50951a0f0f6SJesper Juhl 	char *end;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	if (sbegin == NULL)
5121da177e4SLinus Torvalds 		return NULL;
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	end = strpbrk(sbegin, ct);
5151da177e4SLinus Torvalds 	if (end)
5161da177e4SLinus Torvalds 		*end++ = '\0';
5171da177e4SLinus Torvalds 	*s = end;
5181da177e4SLinus Torvalds 	return sbegin;
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds EXPORT_SYMBOL(strsep);
5211da177e4SLinus Torvalds #endif
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds #ifndef __HAVE_ARCH_MEMSET
5241da177e4SLinus Torvalds /**
5251da177e4SLinus Torvalds  * memset - Fill a region of memory with the given value
5261da177e4SLinus Torvalds  * @s: Pointer to the start of the area.
5271da177e4SLinus Torvalds  * @c: The byte to fill the area with
5281da177e4SLinus Torvalds  * @count: The size of the area.
5291da177e4SLinus Torvalds  *
5301da177e4SLinus Torvalds  * Do not use memset() to access IO space, use memset_io() instead.
5311da177e4SLinus Torvalds  */
memset(void * s,int c,size_t count)5321da177e4SLinus Torvalds void *memset(void *s, int c, size_t count)
5331da177e4SLinus Torvalds {
534850b9247SJesper Juhl 	char *xs = s;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	while (count--)
5371da177e4SLinus Torvalds 		*xs++ = c;
5381da177e4SLinus Torvalds 	return s;
5391da177e4SLinus Torvalds }
5401da177e4SLinus Torvalds EXPORT_SYMBOL(memset);
5411da177e4SLinus Torvalds #endif
5421da177e4SLinus Torvalds 
5433b3c4babSMatthew Wilcox #ifndef __HAVE_ARCH_MEMSET16
5443b3c4babSMatthew Wilcox /**
5453b3c4babSMatthew Wilcox  * memset16() - Fill a memory area with a uint16_t
5463b3c4babSMatthew Wilcox  * @s: Pointer to the start of the area.
5473b3c4babSMatthew Wilcox  * @v: The value to fill the area with
5483b3c4babSMatthew Wilcox  * @count: The number of values to store
5493b3c4babSMatthew Wilcox  *
5503b3c4babSMatthew Wilcox  * Differs from memset() in that it fills with a uint16_t instead
5513b3c4babSMatthew Wilcox  * of a byte.  Remember that @count is the number of uint16_ts to
5523b3c4babSMatthew Wilcox  * store, not the number of bytes.
5533b3c4babSMatthew Wilcox  */
memset16(uint16_t * s,uint16_t v,size_t count)5543b3c4babSMatthew Wilcox void *memset16(uint16_t *s, uint16_t v, size_t count)
5553b3c4babSMatthew Wilcox {
5563b3c4babSMatthew Wilcox 	uint16_t *xs = s;
5573b3c4babSMatthew Wilcox 
5583b3c4babSMatthew Wilcox 	while (count--)
5593b3c4babSMatthew Wilcox 		*xs++ = v;
5603b3c4babSMatthew Wilcox 	return s;
5613b3c4babSMatthew Wilcox }
5623b3c4babSMatthew Wilcox EXPORT_SYMBOL(memset16);
5633b3c4babSMatthew Wilcox #endif
5643b3c4babSMatthew Wilcox 
5653b3c4babSMatthew Wilcox #ifndef __HAVE_ARCH_MEMSET32
5663b3c4babSMatthew Wilcox /**
5673b3c4babSMatthew Wilcox  * memset32() - Fill a memory area with a uint32_t
5683b3c4babSMatthew Wilcox  * @s: Pointer to the start of the area.
5693b3c4babSMatthew Wilcox  * @v: The value to fill the area with
5703b3c4babSMatthew Wilcox  * @count: The number of values to store
5713b3c4babSMatthew Wilcox  *
5723b3c4babSMatthew Wilcox  * Differs from memset() in that it fills with a uint32_t instead
5733b3c4babSMatthew Wilcox  * of a byte.  Remember that @count is the number of uint32_ts to
5743b3c4babSMatthew Wilcox  * store, not the number of bytes.
5753b3c4babSMatthew Wilcox  */
memset32(uint32_t * s,uint32_t v,size_t count)5763b3c4babSMatthew Wilcox void *memset32(uint32_t *s, uint32_t v, size_t count)
5773b3c4babSMatthew Wilcox {
5783b3c4babSMatthew Wilcox 	uint32_t *xs = s;
5793b3c4babSMatthew Wilcox 
5803b3c4babSMatthew Wilcox 	while (count--)
5813b3c4babSMatthew Wilcox 		*xs++ = v;
5823b3c4babSMatthew Wilcox 	return s;
5833b3c4babSMatthew Wilcox }
5843b3c4babSMatthew Wilcox EXPORT_SYMBOL(memset32);
5853b3c4babSMatthew Wilcox #endif
5863b3c4babSMatthew Wilcox 
5873b3c4babSMatthew Wilcox #ifndef __HAVE_ARCH_MEMSET64
5883b3c4babSMatthew Wilcox /**
5893b3c4babSMatthew Wilcox  * memset64() - Fill a memory area with a uint64_t
5903b3c4babSMatthew Wilcox  * @s: Pointer to the start of the area.
5913b3c4babSMatthew Wilcox  * @v: The value to fill the area with
5923b3c4babSMatthew Wilcox  * @count: The number of values to store
5933b3c4babSMatthew Wilcox  *
5943b3c4babSMatthew Wilcox  * Differs from memset() in that it fills with a uint64_t instead
5953b3c4babSMatthew Wilcox  * of a byte.  Remember that @count is the number of uint64_ts to
5963b3c4babSMatthew Wilcox  * store, not the number of bytes.
5973b3c4babSMatthew Wilcox  */
memset64(uint64_t * s,uint64_t v,size_t count)5983b3c4babSMatthew Wilcox void *memset64(uint64_t *s, uint64_t v, size_t count)
5993b3c4babSMatthew Wilcox {
6003b3c4babSMatthew Wilcox 	uint64_t *xs = s;
6013b3c4babSMatthew Wilcox 
6023b3c4babSMatthew Wilcox 	while (count--)
6033b3c4babSMatthew Wilcox 		*xs++ = v;
6043b3c4babSMatthew Wilcox 	return s;
6053b3c4babSMatthew Wilcox }
6063b3c4babSMatthew Wilcox EXPORT_SYMBOL(memset64);
6073b3c4babSMatthew Wilcox #endif
6083b3c4babSMatthew Wilcox 
6091da177e4SLinus Torvalds #ifndef __HAVE_ARCH_MEMCPY
6101da177e4SLinus Torvalds /**
6111da177e4SLinus Torvalds  * memcpy - Copy one area of memory to another
6121da177e4SLinus Torvalds  * @dest: Where to copy to
6131da177e4SLinus Torvalds  * @src: Where to copy from
6141da177e4SLinus Torvalds  * @count: The size of the area.
6151da177e4SLinus Torvalds  *
6161da177e4SLinus Torvalds  * You should not use this function to access IO space, use memcpy_toio()
6171da177e4SLinus Torvalds  * or memcpy_fromio() instead.
6181da177e4SLinus Torvalds  */
memcpy(void * dest,const void * src,size_t count)6191da177e4SLinus Torvalds void *memcpy(void *dest, const void *src, size_t count)
6201da177e4SLinus Torvalds {
621850b9247SJesper Juhl 	char *tmp = dest;
6224c416ab7SJan-Benedict Glaw 	const char *s = src;
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	while (count--)
6251da177e4SLinus Torvalds 		*tmp++ = *s++;
6261da177e4SLinus Torvalds 	return dest;
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds EXPORT_SYMBOL(memcpy);
6291da177e4SLinus Torvalds #endif
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds #ifndef __HAVE_ARCH_MEMMOVE
6321da177e4SLinus Torvalds /**
6331da177e4SLinus Torvalds  * memmove - Copy one area of memory to another
6341da177e4SLinus Torvalds  * @dest: Where to copy to
6351da177e4SLinus Torvalds  * @src: Where to copy from
6361da177e4SLinus Torvalds  * @count: The size of the area.
6371da177e4SLinus Torvalds  *
6381da177e4SLinus Torvalds  * Unlike memcpy(), memmove() copes with overlapping areas.
6391da177e4SLinus Torvalds  */
memmove(void * dest,const void * src,size_t count)6401da177e4SLinus Torvalds void *memmove(void *dest, const void *src, size_t count)
6411da177e4SLinus Torvalds {
64282da2c37SPaul Jackson 	char *tmp;
64382da2c37SPaul Jackson 	const char *s;
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	if (dest <= src) {
646850b9247SJesper Juhl 		tmp = dest;
647850b9247SJesper Juhl 		s = src;
6481da177e4SLinus Torvalds 		while (count--)
6491da177e4SLinus Torvalds 			*tmp++ = *s++;
65051a0f0f6SJesper Juhl 	} else {
651850b9247SJesper Juhl 		tmp = dest;
652850b9247SJesper Juhl 		tmp += count;
653850b9247SJesper Juhl 		s = src;
654850b9247SJesper Juhl 		s += count;
6551da177e4SLinus Torvalds 		while (count--)
6561da177e4SLinus Torvalds 			*--tmp = *--s;
6571da177e4SLinus Torvalds 	}
6581da177e4SLinus Torvalds 	return dest;
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds EXPORT_SYMBOL(memmove);
6611da177e4SLinus Torvalds #endif
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds #ifndef __HAVE_ARCH_MEMCMP
6641da177e4SLinus Torvalds /**
6651da177e4SLinus Torvalds  * memcmp - Compare two areas of memory
6661da177e4SLinus Torvalds  * @cs: One area of memory
6671da177e4SLinus Torvalds  * @ct: Another area of memory
6681da177e4SLinus Torvalds  * @count: The size of the area.
6691da177e4SLinus Torvalds  */
6700c28130bSPaolo 'Blaisorblade' Giarrusso #undef memcmp
memcmp(const void * cs,const void * ct,size_t count)671a7330c99SAndi Kleen __visible int memcmp(const void *cs, const void *ct, size_t count)
6721da177e4SLinus Torvalds {
6731da177e4SLinus Torvalds 	const unsigned char *su1, *su2;
6741da177e4SLinus Torvalds 	int res = 0;
6751da177e4SLinus Torvalds 
676291d47ccSLinus Torvalds #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
677291d47ccSLinus Torvalds 	if (count >= sizeof(unsigned long)) {
678291d47ccSLinus Torvalds 		const unsigned long *u1 = cs;
679291d47ccSLinus Torvalds 		const unsigned long *u2 = ct;
680291d47ccSLinus Torvalds 		do {
681291d47ccSLinus Torvalds 			if (get_unaligned(u1) != get_unaligned(u2))
682291d47ccSLinus Torvalds 				break;
683291d47ccSLinus Torvalds 			u1++;
684291d47ccSLinus Torvalds 			u2++;
685291d47ccSLinus Torvalds 			count -= sizeof(unsigned long);
686291d47ccSLinus Torvalds 		} while (count >= sizeof(unsigned long));
687291d47ccSLinus Torvalds 		cs = u1;
688291d47ccSLinus Torvalds 		ct = u2;
689291d47ccSLinus Torvalds 	}
690291d47ccSLinus Torvalds #endif
6911da177e4SLinus Torvalds 	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
6921da177e4SLinus Torvalds 		if ((res = *su1 - *su2) != 0)
6931da177e4SLinus Torvalds 			break;
6941da177e4SLinus Torvalds 	return res;
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds EXPORT_SYMBOL(memcmp);
6971da177e4SLinus Torvalds #endif
6981da177e4SLinus Torvalds 
6995f074f3eSNick Desaulniers #ifndef __HAVE_ARCH_BCMP
7005f074f3eSNick Desaulniers /**
7015f074f3eSNick Desaulniers  * bcmp - returns 0 if and only if the buffers have identical contents.
7025f074f3eSNick Desaulniers  * @a: pointer to first buffer.
7035f074f3eSNick Desaulniers  * @b: pointer to second buffer.
7045f074f3eSNick Desaulniers  * @len: size of buffers.
7055f074f3eSNick Desaulniers  *
7065f074f3eSNick Desaulniers  * The sign or magnitude of a non-zero return value has no particular
7075f074f3eSNick Desaulniers  * meaning, and architectures may implement their own more efficient bcmp(). So
7085f074f3eSNick Desaulniers  * while this particular implementation is a simple (tail) call to memcmp, do
7095f074f3eSNick Desaulniers  * not rely on anything but whether the return value is zero or non-zero.
7105f074f3eSNick Desaulniers  */
bcmp(const void * a,const void * b,size_t len)7115f074f3eSNick Desaulniers int bcmp(const void *a, const void *b, size_t len)
7125f074f3eSNick Desaulniers {
7135f074f3eSNick Desaulniers 	return memcmp(a, b, len);
7145f074f3eSNick Desaulniers }
7155f074f3eSNick Desaulniers EXPORT_SYMBOL(bcmp);
7165f074f3eSNick Desaulniers #endif
7175f074f3eSNick Desaulniers 
7181da177e4SLinus Torvalds #ifndef __HAVE_ARCH_MEMSCAN
7191da177e4SLinus Torvalds /**
7201da177e4SLinus Torvalds  * memscan - Find a character in an area of memory.
7211da177e4SLinus Torvalds  * @addr: The memory area
7221da177e4SLinus Torvalds  * @c: The byte to search for
7231da177e4SLinus Torvalds  * @size: The size of the area.
7241da177e4SLinus Torvalds  *
7251da177e4SLinus Torvalds  * returns the address of the first occurrence of @c, or 1 byte past
7261da177e4SLinus Torvalds  * the area if @c is not found
7271da177e4SLinus Torvalds  */
memscan(void * addr,int c,size_t size)7281da177e4SLinus Torvalds void *memscan(void *addr, int c, size_t size)
7291da177e4SLinus Torvalds {
730850b9247SJesper Juhl 	unsigned char *p = addr;
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	while (size) {
7331a58be62SAlexey Dobriyan 		if (*p == (unsigned char)c)
7341da177e4SLinus Torvalds 			return (void *)p;
7351da177e4SLinus Torvalds 		p++;
7361da177e4SLinus Torvalds 		size--;
7371da177e4SLinus Torvalds 	}
7381da177e4SLinus Torvalds   	return (void *)p;
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds EXPORT_SYMBOL(memscan);
7411da177e4SLinus Torvalds #endif
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRSTR
7441da177e4SLinus Torvalds /**
7451da177e4SLinus Torvalds  * strstr - Find the first substring in a %NUL terminated string
7461da177e4SLinus Torvalds  * @s1: The string to be searched
7471da177e4SLinus Torvalds  * @s2: The string to search for
7481da177e4SLinus Torvalds  */
strstr(const char * s1,const char * s2)7491da177e4SLinus Torvalds char *strstr(const char *s1, const char *s2)
7501da177e4SLinus Torvalds {
751d5f1fb53SLi Zefan 	size_t l1, l2;
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	l2 = strlen(s2);
7541da177e4SLinus Torvalds 	if (!l2)
7551da177e4SLinus Torvalds 		return (char *)s1;
7561da177e4SLinus Torvalds 	l1 = strlen(s1);
7571da177e4SLinus Torvalds 	while (l1 >= l2) {
7581da177e4SLinus Torvalds 		l1--;
7591da177e4SLinus Torvalds 		if (!memcmp(s1, s2, l2))
7601da177e4SLinus Torvalds 			return (char *)s1;
7611da177e4SLinus Torvalds 		s1++;
7621da177e4SLinus Torvalds 	}
7631da177e4SLinus Torvalds 	return NULL;
7641da177e4SLinus Torvalds }
7651da177e4SLinus Torvalds EXPORT_SYMBOL(strstr);
7661da177e4SLinus Torvalds #endif
7671da177e4SLinus Torvalds 
768d5f1fb53SLi Zefan #ifndef __HAVE_ARCH_STRNSTR
769d5f1fb53SLi Zefan /**
770d5f1fb53SLi Zefan  * strnstr - Find the first substring in a length-limited string
771d5f1fb53SLi Zefan  * @s1: The string to be searched
772d5f1fb53SLi Zefan  * @s2: The string to search for
773d5f1fb53SLi Zefan  * @len: the maximum number of characters to search
774d5f1fb53SLi Zefan  */
strnstr(const char * s1,const char * s2,size_t len)775d5f1fb53SLi Zefan char *strnstr(const char *s1, const char *s2, size_t len)
776d5f1fb53SLi Zefan {
777d6a2eedfSAndré Goddard Rosa 	size_t l2;
778d5f1fb53SLi Zefan 
779d5f1fb53SLi Zefan 	l2 = strlen(s2);
780d5f1fb53SLi Zefan 	if (!l2)
781d5f1fb53SLi Zefan 		return (char *)s1;
782d6a2eedfSAndré Goddard Rosa 	while (len >= l2) {
783d6a2eedfSAndré Goddard Rosa 		len--;
784d5f1fb53SLi Zefan 		if (!memcmp(s1, s2, l2))
785d5f1fb53SLi Zefan 			return (char *)s1;
786d5f1fb53SLi Zefan 		s1++;
787d5f1fb53SLi Zefan 	}
788d5f1fb53SLi Zefan 	return NULL;
789d5f1fb53SLi Zefan }
790d5f1fb53SLi Zefan EXPORT_SYMBOL(strnstr);
791d5f1fb53SLi Zefan #endif
792d5f1fb53SLi Zefan 
7931da177e4SLinus Torvalds #ifndef __HAVE_ARCH_MEMCHR
7941da177e4SLinus Torvalds /**
7951da177e4SLinus Torvalds  * memchr - Find a character in an area of memory.
7961da177e4SLinus Torvalds  * @s: The memory area
7971da177e4SLinus Torvalds  * @c: The byte to search for
7981da177e4SLinus Torvalds  * @n: The size of the area.
7991da177e4SLinus Torvalds  *
8001da177e4SLinus Torvalds  * returns the address of the first occurrence of @c, or %NULL
8011da177e4SLinus Torvalds  * if @c is not found
8021da177e4SLinus Torvalds  */
memchr(const void * s,int c,size_t n)8031da177e4SLinus Torvalds void *memchr(const void *s, int c, size_t n)
8041da177e4SLinus Torvalds {
8051da177e4SLinus Torvalds 	const unsigned char *p = s;
8061da177e4SLinus Torvalds 	while (n-- != 0) {
8071da177e4SLinus Torvalds         	if ((unsigned char)c == *p++) {
8081da177e4SLinus Torvalds 			return (void *)(p - 1);
8091da177e4SLinus Torvalds 		}
8101da177e4SLinus Torvalds 	}
8111da177e4SLinus Torvalds 	return NULL;
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds EXPORT_SYMBOL(memchr);
8141da177e4SLinus Torvalds #endif
81579824820SAkinobu Mita 
check_bytes8(const u8 * start,u8 value,unsigned int bytes)81679824820SAkinobu Mita static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
81779824820SAkinobu Mita {
81879824820SAkinobu Mita 	while (bytes) {
81979824820SAkinobu Mita 		if (*start != value)
82079824820SAkinobu Mita 			return (void *)start;
82179824820SAkinobu Mita 		start++;
82279824820SAkinobu Mita 		bytes--;
82379824820SAkinobu Mita 	}
82479824820SAkinobu Mita 	return NULL;
82579824820SAkinobu Mita }
82679824820SAkinobu Mita 
82779824820SAkinobu Mita /**
82879824820SAkinobu Mita  * memchr_inv - Find an unmatching character in an area of memory.
82979824820SAkinobu Mita  * @start: The memory area
83079824820SAkinobu Mita  * @c: Find a character other than c
83179824820SAkinobu Mita  * @bytes: The size of the area.
83279824820SAkinobu Mita  *
83379824820SAkinobu Mita  * returns the address of the first character other than @c, or %NULL
83479824820SAkinobu Mita  * if the whole buffer contains just @c.
83579824820SAkinobu Mita  */
memchr_inv(const void * start,int c,size_t bytes)83679824820SAkinobu Mita void *memchr_inv(const void *start, int c, size_t bytes)
83779824820SAkinobu Mita {
83879824820SAkinobu Mita 	u8 value = c;
83979824820SAkinobu Mita 	u64 value64;
84079824820SAkinobu Mita 	unsigned int words, prefix;
84179824820SAkinobu Mita 
84279824820SAkinobu Mita 	if (bytes <= 16)
84379824820SAkinobu Mita 		return check_bytes8(start, value, bytes);
84479824820SAkinobu Mita 
845f43804bfSAkinobu Mita 	value64 = value;
84672d93104SLinus Torvalds #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
8473368e8fbSAndy Shevchenko 	value64 *= 0x0101010101010101ULL;
84872d93104SLinus Torvalds #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
849f43804bfSAkinobu Mita 	value64 *= 0x01010101;
850f43804bfSAkinobu Mita 	value64 |= value64 << 32;
851f43804bfSAkinobu Mita #else
852f43804bfSAkinobu Mita 	value64 |= value64 << 8;
853f43804bfSAkinobu Mita 	value64 |= value64 << 16;
854f43804bfSAkinobu Mita 	value64 |= value64 << 32;
855f43804bfSAkinobu Mita #endif
85679824820SAkinobu Mita 
857f43804bfSAkinobu Mita 	prefix = (unsigned long)start % 8;
85879824820SAkinobu Mita 	if (prefix) {
859f43804bfSAkinobu Mita 		u8 *r;
860f43804bfSAkinobu Mita 
861f43804bfSAkinobu Mita 		prefix = 8 - prefix;
862f43804bfSAkinobu Mita 		r = check_bytes8(start, value, prefix);
86379824820SAkinobu Mita 		if (r)
86479824820SAkinobu Mita 			return r;
86579824820SAkinobu Mita 		start += prefix;
86679824820SAkinobu Mita 		bytes -= prefix;
86779824820SAkinobu Mita 	}
86879824820SAkinobu Mita 
86979824820SAkinobu Mita 	words = bytes / 8;
87079824820SAkinobu Mita 
87179824820SAkinobu Mita 	while (words) {
87279824820SAkinobu Mita 		if (*(u64 *)start != value64)
87379824820SAkinobu Mita 			return check_bytes8(start, value, 8);
87479824820SAkinobu Mita 		start += 8;
87579824820SAkinobu Mita 		words--;
87679824820SAkinobu Mita 	}
87779824820SAkinobu Mita 
87879824820SAkinobu Mita 	return check_bytes8(start, value, bytes % 8);
87979824820SAkinobu Mita }
88079824820SAkinobu Mita EXPORT_SYMBOL(memchr_inv);
881