xref: /linux-6.15/lib/string.c (revision d94c12bd)
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
1838b9baf1STanzir Hasan #include <linux/bits.h>
1950af5eadSPaul Gortmaker #include <linux/bug.h>
2038b9baf1STanzir Hasan #include <linux/ctype.h>
218bc3bcc9SPaul Gortmaker #include <linux/errno.h>
2238b9baf1STanzir Hasan #include <linux/limits.h>
2338b9baf1STanzir Hasan #include <linux/linkage.h>
2438b9baf1STanzir Hasan #include <linux/stddef.h>
2538b9baf1STanzir Hasan #include <linux/string.h>
2638b9baf1STanzir Hasan #include <linux/types.h>
271da177e4SLinus Torvalds 
2830035e45SChris Metcalf #include <asm/page.h>
2938b9baf1STanzir Hasan #include <asm/rwonce.h>
305f60d5f6SAl Viro #include <linux/unaligned.h>
3138b9baf1STanzir Hasan #include <asm/word-at-a-time.h>
3230035e45SChris Metcalf 
33cd514e72SRasmus Villemoes #ifndef __HAVE_ARCH_STRNCASECMP
341da177e4SLinus Torvalds /**
35cd514e72SRasmus Villemoes  * strncasecmp - Case insensitive, length-limited string comparison
361da177e4SLinus Torvalds  * @s1: One string
371da177e4SLinus Torvalds  * @s2: The other string
381da177e4SLinus Torvalds  * @len: the maximum number of characters to compare
391da177e4SLinus Torvalds  */
strncasecmp(const char * s1,const char * s2,size_t len)40cd514e72SRasmus Villemoes int strncasecmp(const char *s1, const char *s2, size_t len)
411da177e4SLinus Torvalds {
421da177e4SLinus Torvalds 	/* Yes, Virginia, it had better be unsigned */
431da177e4SLinus Torvalds 	unsigned char c1, c2;
441da177e4SLinus Torvalds 
45a11d2b64SAndré Goddard Rosa 	if (!len)
46a11d2b64SAndré Goddard Rosa 		return 0;
47a11d2b64SAndré Goddard Rosa 
481da177e4SLinus Torvalds 	do {
49a11d2b64SAndré Goddard Rosa 		c1 = *s1++;
50a11d2b64SAndré Goddard Rosa 		c2 = *s2++;
51a11d2b64SAndré Goddard Rosa 		if (!c1 || !c2)
521da177e4SLinus Torvalds 			break;
531da177e4SLinus Torvalds 		if (c1 == c2)
541da177e4SLinus Torvalds 			continue;
551da177e4SLinus Torvalds 		c1 = tolower(c1);
561da177e4SLinus Torvalds 		c2 = tolower(c2);
571da177e4SLinus Torvalds 		if (c1 != c2)
581da177e4SLinus Torvalds 			break;
591da177e4SLinus Torvalds 	} while (--len);
601da177e4SLinus Torvalds 	return (int)c1 - (int)c2;
611da177e4SLinus Torvalds }
62cd514e72SRasmus Villemoes EXPORT_SYMBOL(strncasecmp);
63cd514e72SRasmus Villemoes #endif
641da177e4SLinus Torvalds 
65ded220bdSDavid S. Miller #ifndef __HAVE_ARCH_STRCASECMP
strcasecmp(const char * s1,const char * s2)66ded220bdSDavid S. Miller int strcasecmp(const char *s1, const char *s2)
67ded220bdSDavid S. Miller {
68ded220bdSDavid S. Miller 	int c1, c2;
69ded220bdSDavid S. Miller 
70ded220bdSDavid S. Miller 	do {
71ded220bdSDavid S. Miller 		c1 = tolower(*s1++);
72ded220bdSDavid S. Miller 		c2 = tolower(*s2++);
73ded220bdSDavid S. Miller 	} while (c1 == c2 && c1 != 0);
74ded220bdSDavid S. Miller 	return c1 - c2;
75ded220bdSDavid S. Miller }
76ded220bdSDavid S. Miller EXPORT_SYMBOL(strcasecmp);
77ded220bdSDavid S. Miller #endif
78ded220bdSDavid S. Miller 
791da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRCPY
strcpy(char * dest,const char * src)801da177e4SLinus Torvalds char *strcpy(char *dest, const char *src)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds 	char *tmp = dest;
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	while ((*dest++ = *src++) != '\0')
851da177e4SLinus Torvalds 		/* nothing */;
861da177e4SLinus Torvalds 	return tmp;
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds EXPORT_SYMBOL(strcpy);
891da177e4SLinus Torvalds #endif
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRNCPY
strncpy(char * dest,const char * src,size_t count)921da177e4SLinus Torvalds char *strncpy(char *dest, const char *src, size_t count)
931da177e4SLinus Torvalds {
941da177e4SLinus Torvalds 	char *tmp = dest;
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	while (count) {
9751a0f0f6SJesper Juhl 		if ((*tmp = *src) != 0)
9851a0f0f6SJesper Juhl 			src++;
991da177e4SLinus Torvalds 		tmp++;
1001da177e4SLinus Torvalds 		count--;
1011da177e4SLinus Torvalds 	}
1021da177e4SLinus Torvalds 	return dest;
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds EXPORT_SYMBOL(strncpy);
1051da177e4SLinus Torvalds #endif
1061da177e4SLinus Torvalds 
1079022ed0eSLinus Torvalds #ifdef __BIG_ENDIAN
1089022ed0eSLinus Torvalds # define ALLBUTLAST_BYTE_MASK (~255ul)
1099022ed0eSLinus Torvalds #else
1109022ed0eSLinus Torvalds # define ALLBUTLAST_BYTE_MASK (~0ul >> 8)
1119022ed0eSLinus Torvalds #endif
1129022ed0eSLinus Torvalds 
sized_strscpy(char * dest,const char * src,size_t count)113e6584c39SKees Cook ssize_t sized_strscpy(char *dest, const char *src, size_t count)
11430035e45SChris Metcalf {
11530035e45SChris Metcalf 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
11630035e45SChris Metcalf 	size_t max = count;
11730035e45SChris Metcalf 	long res = 0;
11830035e45SChris Metcalf 
1199a156466SKees Cook 	if (count == 0 || WARN_ON_ONCE(count > INT_MAX))
12030035e45SChris Metcalf 		return -E2BIG;
12130035e45SChris Metcalf 
122*d94c12bdSPeter Collingbourne #ifndef CONFIG_DCACHE_WORD_ACCESS
12330035e45SChris Metcalf #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
12430035e45SChris Metcalf 	/*
12530035e45SChris Metcalf 	 * If src is unaligned, don't cross a page boundary,
12630035e45SChris Metcalf 	 * since we don't know if the next page is mapped.
12730035e45SChris Metcalf 	 */
12830035e45SChris Metcalf 	if ((long)src & (sizeof(long) - 1)) {
12930035e45SChris Metcalf 		size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
13030035e45SChris Metcalf 		if (limit < max)
13130035e45SChris Metcalf 			max = limit;
13230035e45SChris Metcalf 	}
13330035e45SChris Metcalf #else
13430035e45SChris Metcalf 	/* If src or dest is unaligned, don't do word-at-a-time. */
13530035e45SChris Metcalf 	if (((long) dest | (long) src) & (sizeof(long) - 1))
13630035e45SChris Metcalf 		max = 0;
13730035e45SChris Metcalf #endif
138*d94c12bdSPeter Collingbourne #endif
13930035e45SChris Metcalf 
1402de6f3bfSAlexander Potapenko 	/*
141*d94c12bdSPeter Collingbourne 	 * load_unaligned_zeropad() or read_word_at_a_time() below may read
142*d94c12bdSPeter Collingbourne 	 * uninitialized bytes after the trailing zero and use them in
143*d94c12bdSPeter Collingbourne 	 * comparisons. Disable this optimization under KMSAN to prevent
144*d94c12bdSPeter Collingbourne 	 * false positive reports.
1452de6f3bfSAlexander Potapenko 	 */
1462de6f3bfSAlexander Potapenko 	if (IS_ENABLED(CONFIG_KMSAN))
1472de6f3bfSAlexander Potapenko 		max = 0;
1482de6f3bfSAlexander Potapenko 
14930035e45SChris Metcalf 	while (max >= sizeof(unsigned long)) {
15030035e45SChris Metcalf 		unsigned long c, data;
15130035e45SChris Metcalf 
152*d94c12bdSPeter Collingbourne #ifdef CONFIG_DCACHE_WORD_ACCESS
153*d94c12bdSPeter Collingbourne 		c = load_unaligned_zeropad(src+res);
154*d94c12bdSPeter Collingbourne #else
1551a3241ffSAndrey Ryabinin 		c = read_word_at_a_time(src+res);
156*d94c12bdSPeter Collingbourne #endif
15730035e45SChris Metcalf 		if (has_zero(c, &data, &constants)) {
15830035e45SChris Metcalf 			data = prep_zero_mask(c, data, &constants);
15930035e45SChris Metcalf 			data = create_zero_mask(data);
160990486c8SChris Metcalf 			*(unsigned long *)(dest+res) = c & zero_bytemask(data);
16130035e45SChris Metcalf 			return res + find_zero(data);
16230035e45SChris Metcalf 		}
1639022ed0eSLinus Torvalds 		count -= sizeof(unsigned long);
1649022ed0eSLinus Torvalds 		if (unlikely(!count)) {
1659022ed0eSLinus Torvalds 			c &= ALLBUTLAST_BYTE_MASK;
1669022ed0eSLinus Torvalds 			*(unsigned long *)(dest+res) = c;
1679022ed0eSLinus Torvalds 			return -E2BIG;
1689022ed0eSLinus Torvalds 		}
169990486c8SChris Metcalf 		*(unsigned long *)(dest+res) = c;
17030035e45SChris Metcalf 		res += sizeof(unsigned long);
17130035e45SChris Metcalf 		max -= sizeof(unsigned long);
17230035e45SChris Metcalf 	}
17330035e45SChris Metcalf 
1749022ed0eSLinus Torvalds 	while (count > 1) {
17530035e45SChris Metcalf 		char c;
17630035e45SChris Metcalf 
17730035e45SChris Metcalf 		c = src[res];
17830035e45SChris Metcalf 		dest[res] = c;
17930035e45SChris Metcalf 		if (!c)
18030035e45SChris Metcalf 			return res;
18130035e45SChris Metcalf 		res++;
18230035e45SChris Metcalf 		count--;
18330035e45SChris Metcalf 	}
18430035e45SChris Metcalf 
1859022ed0eSLinus Torvalds 	/* Force NUL-termination. */
1869022ed0eSLinus Torvalds 	dest[res] = '\0';
18730035e45SChris Metcalf 
1889022ed0eSLinus Torvalds 	/* Return E2BIG if the source didn't stop */
1899022ed0eSLinus Torvalds 	return src[res] ? -E2BIG : res;
19030035e45SChris Metcalf }
191e6584c39SKees Cook EXPORT_SYMBOL(sized_strscpy);
19230035e45SChris Metcalf 
193458a3bf8STobin C. Harding /**
1941e1b6d63SNick Desaulniers  * stpcpy - copy a string from src to dest returning a pointer to the new end
1951e1b6d63SNick Desaulniers  *          of dest, including src's %NUL-terminator. May overrun dest.
1961e1b6d63SNick Desaulniers  * @dest: pointer to end of string being copied into. Must be large enough
1971e1b6d63SNick Desaulniers  *        to receive copy.
1981e1b6d63SNick Desaulniers  * @src: pointer to the beginning of string being copied from. Must not overlap
1991e1b6d63SNick Desaulniers  *       dest.
2001e1b6d63SNick Desaulniers  *
2011e1b6d63SNick Desaulniers  * stpcpy differs from strcpy in a key way: the return value is a pointer
2021e1b6d63SNick Desaulniers  * to the new %NUL-terminating character in @dest. (For strcpy, the return
2031e1b6d63SNick Desaulniers  * value is a pointer to the start of @dest). This interface is considered
2041e1b6d63SNick Desaulniers  * unsafe as it doesn't perform bounds checking of the inputs. As such it's
2051e1b6d63SNick Desaulniers  * not recommended for usage. Instead, its definition is provided in case
2061e1b6d63SNick Desaulniers  * the compiler lowers other libcalls to stpcpy.
2071e1b6d63SNick Desaulniers  */
2081e1b6d63SNick Desaulniers char *stpcpy(char *__restrict__ dest, const char *__restrict__ src);
stpcpy(char * __restrict__ dest,const char * __restrict__ src)2091e1b6d63SNick Desaulniers char *stpcpy(char *__restrict__ dest, const char *__restrict__ src)
2101e1b6d63SNick Desaulniers {
2111e1b6d63SNick Desaulniers 	while ((*dest++ = *src++) != '\0')
2121e1b6d63SNick Desaulniers 		/* nothing */;
2131e1b6d63SNick Desaulniers 	return --dest;
2141e1b6d63SNick Desaulniers }
2151e1b6d63SNick Desaulniers EXPORT_SYMBOL(stpcpy);
2161e1b6d63SNick Desaulniers 
2171da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRCAT
strcat(char * dest,const char * src)2181da177e4SLinus Torvalds char *strcat(char *dest, const char *src)
2191da177e4SLinus Torvalds {
2201da177e4SLinus Torvalds 	char *tmp = dest;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	while (*dest)
2231da177e4SLinus Torvalds 		dest++;
2241da177e4SLinus Torvalds 	while ((*dest++ = *src++) != '\0')
2251da177e4SLinus Torvalds 		;
2261da177e4SLinus Torvalds 	return tmp;
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds EXPORT_SYMBOL(strcat);
2291da177e4SLinus Torvalds #endif
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRNCAT
strncat(char * dest,const char * src,size_t count)2321da177e4SLinus Torvalds char *strncat(char *dest, const char *src, size_t count)
2331da177e4SLinus Torvalds {
2341da177e4SLinus Torvalds 	char *tmp = dest;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	if (count) {
2371da177e4SLinus Torvalds 		while (*dest)
2381da177e4SLinus Torvalds 			dest++;
2391da177e4SLinus Torvalds 		while ((*dest++ = *src++) != 0) {
2401da177e4SLinus Torvalds 			if (--count == 0) {
2411da177e4SLinus Torvalds 				*dest = '\0';
2421da177e4SLinus Torvalds 				break;
2431da177e4SLinus Torvalds 			}
2441da177e4SLinus Torvalds 		}
2451da177e4SLinus Torvalds 	}
2461da177e4SLinus Torvalds 	return tmp;
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds EXPORT_SYMBOL(strncat);
2491da177e4SLinus Torvalds #endif
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRLCAT
strlcat(char * dest,const char * src,size_t count)2521da177e4SLinus Torvalds size_t strlcat(char *dest, const char *src, size_t count)
2531da177e4SLinus Torvalds {
2541da177e4SLinus Torvalds 	size_t dsize = strlen(dest);
2551da177e4SLinus Torvalds 	size_t len = strlen(src);
2561da177e4SLinus Torvalds 	size_t res = dsize + len;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	/* This would be a bug */
2591da177e4SLinus Torvalds 	BUG_ON(dsize >= count);
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	dest += dsize;
2621da177e4SLinus Torvalds 	count -= dsize;
2631da177e4SLinus Torvalds 	if (len >= count)
2641da177e4SLinus Torvalds 		len = count-1;
265f9cfb191SAlexander Potapenko 	__builtin_memcpy(dest, src, len);
2661da177e4SLinus Torvalds 	dest[len] = 0;
2671da177e4SLinus Torvalds 	return res;
2681da177e4SLinus Torvalds }
2691da177e4SLinus Torvalds EXPORT_SYMBOL(strlcat);
2701da177e4SLinus Torvalds #endif
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRCMP
2731da177e4SLinus Torvalds /**
2741da177e4SLinus Torvalds  * strcmp - Compare two strings
2751da177e4SLinus Torvalds  * @cs: One string
2761da177e4SLinus Torvalds  * @ct: Another string
2771da177e4SLinus Torvalds  */
strcmp(const char * cs,const char * ct)2781da177e4SLinus Torvalds int strcmp(const char *cs, const char *ct)
2791da177e4SLinus Torvalds {
280a414f01aSLinus Torvalds 	unsigned char c1, c2;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	while (1) {
283a414f01aSLinus Torvalds 		c1 = *cs++;
284a414f01aSLinus Torvalds 		c2 = *ct++;
285a414f01aSLinus Torvalds 		if (c1 != c2)
286a414f01aSLinus Torvalds 			return c1 < c2 ? -1 : 1;
287a414f01aSLinus Torvalds 		if (!c1)
2881da177e4SLinus Torvalds 			break;
2891da177e4SLinus Torvalds 	}
290a414f01aSLinus Torvalds 	return 0;
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds EXPORT_SYMBOL(strcmp);
2931da177e4SLinus Torvalds #endif
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRNCMP
2961da177e4SLinus Torvalds /**
2971da177e4SLinus Torvalds  * strncmp - Compare two length-limited strings
2981da177e4SLinus Torvalds  * @cs: One string
2991da177e4SLinus Torvalds  * @ct: Another string
3001da177e4SLinus Torvalds  * @count: The maximum number of bytes to compare
3011da177e4SLinus Torvalds  */
strncmp(const char * cs,const char * ct,size_t count)3021da177e4SLinus Torvalds int strncmp(const char *cs, const char *ct, size_t count)
3031da177e4SLinus Torvalds {
304a414f01aSLinus Torvalds 	unsigned char c1, c2;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	while (count) {
307a414f01aSLinus Torvalds 		c1 = *cs++;
308a414f01aSLinus Torvalds 		c2 = *ct++;
309a414f01aSLinus Torvalds 		if (c1 != c2)
310a414f01aSLinus Torvalds 			return c1 < c2 ? -1 : 1;
311a414f01aSLinus Torvalds 		if (!c1)
3121da177e4SLinus Torvalds 			break;
3131da177e4SLinus Torvalds 		count--;
3141da177e4SLinus Torvalds 	}
315a414f01aSLinus Torvalds 	return 0;
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds EXPORT_SYMBOL(strncmp);
3181da177e4SLinus Torvalds #endif
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRCHR
3211da177e4SLinus Torvalds /**
3221da177e4SLinus Torvalds  * strchr - Find the first occurrence of a character in a string
3231da177e4SLinus Torvalds  * @s: The string to be searched
3241da177e4SLinus Torvalds  * @c: The character to search for
325b0975710SPeter Rosin  *
326b0975710SPeter Rosin  * Note that the %NUL-terminator is considered part of the string, and can
327b0975710SPeter Rosin  * be searched for.
3281da177e4SLinus Torvalds  */
strchr(const char * s,int c)3291da177e4SLinus Torvalds char *strchr(const char *s, int c)
3301da177e4SLinus Torvalds {
3311da177e4SLinus Torvalds 	for (; *s != (char)c; ++s)
3321da177e4SLinus Torvalds 		if (*s == '\0')
3331da177e4SLinus Torvalds 			return NULL;
3341da177e4SLinus Torvalds 	return (char *)s;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds EXPORT_SYMBOL(strchr);
3371da177e4SLinus Torvalds #endif
3381da177e4SLinus Torvalds 
33911d200e9SGrant Likely #ifndef __HAVE_ARCH_STRCHRNUL
34011d200e9SGrant Likely /**
34111d200e9SGrant Likely  * strchrnul - Find and return a character in a string, or end of string
34211d200e9SGrant Likely  * @s: The string to be searched
34311d200e9SGrant Likely  * @c: The character to search for
34411d200e9SGrant Likely  *
34511d200e9SGrant Likely  * Returns pointer to first occurrence of 'c' in s. If c is not found, then
34611d200e9SGrant Likely  * return a pointer to the null byte at the end of s.
34711d200e9SGrant Likely  */
strchrnul(const char * s,int c)34811d200e9SGrant Likely char *strchrnul(const char *s, int c)
34911d200e9SGrant Likely {
35011d200e9SGrant Likely 	while (*s && *s != (char)c)
35111d200e9SGrant Likely 		s++;
35211d200e9SGrant Likely 	return (char *)s;
35311d200e9SGrant Likely }
35411d200e9SGrant Likely EXPORT_SYMBOL(strchrnul);
35511d200e9SGrant Likely #endif
35611d200e9SGrant Likely 
3570bee0cecSYury Norov /**
3580bee0cecSYury Norov  * strnchrnul - Find and return a character in a length limited string,
3590bee0cecSYury Norov  * or end of string
3600bee0cecSYury Norov  * @s: The string to be searched
3610bee0cecSYury Norov  * @count: The number of characters to be searched
3620bee0cecSYury Norov  * @c: The character to search for
3630bee0cecSYury Norov  *
3640bee0cecSYury Norov  * Returns pointer to the first occurrence of 'c' in s. If c is not found,
3650bee0cecSYury Norov  * then return a pointer to the last character of the string.
3660bee0cecSYury Norov  */
strnchrnul(const char * s,size_t count,int c)3670bee0cecSYury Norov char *strnchrnul(const char *s, size_t count, int c)
3680bee0cecSYury Norov {
3690bee0cecSYury Norov 	while (count-- && *s && *s != (char)c)
3700bee0cecSYury Norov 		s++;
3710bee0cecSYury Norov 	return (char *)s;
3720bee0cecSYury Norov }
3730bee0cecSYury Norov 
3741da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRRCHR
3751da177e4SLinus Torvalds /**
3761da177e4SLinus Torvalds  * strrchr - Find the last occurrence of a character in a string
3771da177e4SLinus Torvalds  * @s: The string to be searched
3781da177e4SLinus Torvalds  * @c: The character to search for
3791da177e4SLinus Torvalds  */
strrchr(const char * s,int c)3801da177e4SLinus Torvalds char *strrchr(const char *s, int c)
3811da177e4SLinus Torvalds {
3828da53d45SRasmus Villemoes 	const char *last = NULL;
3831da177e4SLinus Torvalds 	do {
3848da53d45SRasmus Villemoes 		if (*s == (char)c)
3858da53d45SRasmus Villemoes 			last = s;
3868da53d45SRasmus Villemoes 	} while (*s++);
3878da53d45SRasmus Villemoes 	return (char *)last;
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds EXPORT_SYMBOL(strrchr);
3901da177e4SLinus Torvalds #endif
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRNCHR
3931da177e4SLinus Torvalds /**
3941da177e4SLinus Torvalds  * strnchr - Find a character in a length limited string
3951da177e4SLinus Torvalds  * @s: The string to be searched
3961da177e4SLinus Torvalds  * @count: The number of characters to be searched
3971da177e4SLinus Torvalds  * @c: The character to search for
398b0975710SPeter Rosin  *
399b0975710SPeter Rosin  * Note that the %NUL-terminator is considered part of the string, and can
400b0975710SPeter Rosin  * be searched for.
4011da177e4SLinus Torvalds  */
strnchr(const char * s,size_t count,int c)4021da177e4SLinus Torvalds char *strnchr(const char *s, size_t count, int c)
4031da177e4SLinus Torvalds {
404b0975710SPeter Rosin 	while (count--) {
4051da177e4SLinus Torvalds 		if (*s == (char)c)
4061da177e4SLinus Torvalds 			return (char *)s;
407b0975710SPeter Rosin 		if (*s++ == '\0')
408b0975710SPeter Rosin 			break;
409b0975710SPeter Rosin 	}
4101da177e4SLinus Torvalds 	return NULL;
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds EXPORT_SYMBOL(strnchr);
4131da177e4SLinus Torvalds #endif
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRLEN
strlen(const char * s)4161da177e4SLinus Torvalds size_t strlen(const char *s)
4171da177e4SLinus Torvalds {
4181da177e4SLinus Torvalds 	const char *sc;
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	for (sc = s; *sc != '\0'; ++sc)
4211da177e4SLinus Torvalds 		/* nothing */;
4221da177e4SLinus Torvalds 	return sc - s;
4231da177e4SLinus Torvalds }
4241da177e4SLinus Torvalds EXPORT_SYMBOL(strlen);
4251da177e4SLinus Torvalds #endif
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRNLEN
strnlen(const char * s,size_t count)4281da177e4SLinus Torvalds size_t strnlen(const char *s, size_t count)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds 	const char *sc;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	for (sc = s; count-- && *sc != '\0'; ++sc)
4331da177e4SLinus Torvalds 		/* nothing */;
4341da177e4SLinus Torvalds 	return sc - s;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds EXPORT_SYMBOL(strnlen);
4371da177e4SLinus Torvalds #endif
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRSPN
4401da177e4SLinus Torvalds /**
44172fd4a35SRobert P. J. Day  * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
4421da177e4SLinus Torvalds  * @s: The string to be searched
4431da177e4SLinus Torvalds  * @accept: The string to search for
4441da177e4SLinus Torvalds  */
strspn(const char * s,const char * accept)4451da177e4SLinus Torvalds size_t strspn(const char *s, const char *accept)
4461da177e4SLinus Torvalds {
4471da177e4SLinus Torvalds 	const char *p;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	for (p = s; *p != '\0'; ++p) {
450dffad91bSRasmus Villemoes 		if (!strchr(accept, *p))
4511da177e4SLinus Torvalds 			break;
4521da177e4SLinus Torvalds 	}
453dffad91bSRasmus Villemoes 	return p - s;
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds EXPORT_SYMBOL(strspn);
4561da177e4SLinus Torvalds #endif
4571da177e4SLinus Torvalds 
4588833d328SKyle McMartin #ifndef __HAVE_ARCH_STRCSPN
4591da177e4SLinus Torvalds /**
46072fd4a35SRobert P. J. Day  * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
4611da177e4SLinus Torvalds  * @s: The string to be searched
4621da177e4SLinus Torvalds  * @reject: The string to avoid
4631da177e4SLinus Torvalds  */
strcspn(const char * s,const char * reject)4641da177e4SLinus Torvalds size_t strcspn(const char *s, const char *reject)
4651da177e4SLinus Torvalds {
4661da177e4SLinus Torvalds 	const char *p;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	for (p = s; *p != '\0'; ++p) {
469dffad91bSRasmus Villemoes 		if (strchr(reject, *p))
470dffad91bSRasmus Villemoes 			break;
4711da177e4SLinus Torvalds 	}
472dffad91bSRasmus Villemoes 	return p - s;
4731da177e4SLinus Torvalds }
4741da177e4SLinus Torvalds EXPORT_SYMBOL(strcspn);
4758833d328SKyle McMartin #endif
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRPBRK
4781da177e4SLinus Torvalds /**
4791da177e4SLinus Torvalds  * strpbrk - Find the first occurrence of a set of characters
4801da177e4SLinus Torvalds  * @cs: The string to be searched
4811da177e4SLinus Torvalds  * @ct: The characters to search for
4821da177e4SLinus Torvalds  */
strpbrk(const char * cs,const char * ct)4831da177e4SLinus Torvalds char *strpbrk(const char *cs, const char *ct)
4841da177e4SLinus Torvalds {
485a8c55407SAndy Shevchenko 	const char *sc;
4861da177e4SLinus Torvalds 
487a8c55407SAndy Shevchenko 	for (sc = cs; *sc != '\0'; ++sc) {
488a8c55407SAndy Shevchenko 		if (strchr(ct, *sc))
489a8c55407SAndy Shevchenko 			return (char *)sc;
4901da177e4SLinus Torvalds 	}
4911da177e4SLinus Torvalds 	return NULL;
4921da177e4SLinus Torvalds }
493894b5779SKyle McMartin EXPORT_SYMBOL(strpbrk);
4941da177e4SLinus Torvalds #endif
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRSEP
4971da177e4SLinus Torvalds /**
4981da177e4SLinus Torvalds  * strsep - Split a string into tokens
4991da177e4SLinus Torvalds  * @s: The string to be searched
5001da177e4SLinus Torvalds  * @ct: The characters to search for
5011da177e4SLinus Torvalds  *
5021da177e4SLinus Torvalds  * strsep() updates @s to point after the token, ready for the next call.
5031da177e4SLinus Torvalds  *
5041da177e4SLinus Torvalds  * It returns empty tokens, too, behaving exactly like the libc function
5051da177e4SLinus Torvalds  * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
5061da177e4SLinus Torvalds  * Same semantics, slimmer shape. ;)
5071da177e4SLinus Torvalds  */
strsep(char ** s,const char * ct)5081da177e4SLinus Torvalds char *strsep(char **s, const char *ct)
5091da177e4SLinus Torvalds {
51051a0f0f6SJesper Juhl 	char *sbegin = *s;
51151a0f0f6SJesper Juhl 	char *end;
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	if (sbegin == NULL)
5141da177e4SLinus Torvalds 		return NULL;
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	end = strpbrk(sbegin, ct);
5171da177e4SLinus Torvalds 	if (end)
5181da177e4SLinus Torvalds 		*end++ = '\0';
5191da177e4SLinus Torvalds 	*s = end;
5201da177e4SLinus Torvalds 	return sbegin;
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds EXPORT_SYMBOL(strsep);
5231da177e4SLinus Torvalds #endif
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds #ifndef __HAVE_ARCH_MEMSET
5261da177e4SLinus Torvalds /**
5271da177e4SLinus Torvalds  * memset - Fill a region of memory with the given value
5281da177e4SLinus Torvalds  * @s: Pointer to the start of the area.
5291da177e4SLinus Torvalds  * @c: The byte to fill the area with
5301da177e4SLinus Torvalds  * @count: The size of the area.
5311da177e4SLinus Torvalds  *
5321da177e4SLinus Torvalds  * Do not use memset() to access IO space, use memset_io() instead.
5331da177e4SLinus Torvalds  */
memset(void * s,int c,size_t count)5341da177e4SLinus Torvalds void *memset(void *s, int c, size_t count)
5351da177e4SLinus Torvalds {
536850b9247SJesper Juhl 	char *xs = s;
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	while (count--)
5391da177e4SLinus Torvalds 		*xs++ = c;
5401da177e4SLinus Torvalds 	return s;
5411da177e4SLinus Torvalds }
5421da177e4SLinus Torvalds EXPORT_SYMBOL(memset);
5431da177e4SLinus Torvalds #endif
5441da177e4SLinus Torvalds 
5453b3c4babSMatthew Wilcox #ifndef __HAVE_ARCH_MEMSET16
5463b3c4babSMatthew Wilcox /**
5473b3c4babSMatthew Wilcox  * memset16() - Fill a memory area with a uint16_t
5483b3c4babSMatthew Wilcox  * @s: Pointer to the start of the area.
5493b3c4babSMatthew Wilcox  * @v: The value to fill the area with
5503b3c4babSMatthew Wilcox  * @count: The number of values to store
5513b3c4babSMatthew Wilcox  *
5523b3c4babSMatthew Wilcox  * Differs from memset() in that it fills with a uint16_t instead
5533b3c4babSMatthew Wilcox  * of a byte.  Remember that @count is the number of uint16_ts to
5543b3c4babSMatthew Wilcox  * store, not the number of bytes.
5553b3c4babSMatthew Wilcox  */
memset16(uint16_t * s,uint16_t v,size_t count)5563b3c4babSMatthew Wilcox void *memset16(uint16_t *s, uint16_t v, size_t count)
5573b3c4babSMatthew Wilcox {
5583b3c4babSMatthew Wilcox 	uint16_t *xs = s;
5593b3c4babSMatthew Wilcox 
5603b3c4babSMatthew Wilcox 	while (count--)
5613b3c4babSMatthew Wilcox 		*xs++ = v;
5623b3c4babSMatthew Wilcox 	return s;
5633b3c4babSMatthew Wilcox }
5643b3c4babSMatthew Wilcox EXPORT_SYMBOL(memset16);
5653b3c4babSMatthew Wilcox #endif
5663b3c4babSMatthew Wilcox 
5673b3c4babSMatthew Wilcox #ifndef __HAVE_ARCH_MEMSET32
5683b3c4babSMatthew Wilcox /**
5693b3c4babSMatthew Wilcox  * memset32() - Fill a memory area with a uint32_t
5703b3c4babSMatthew Wilcox  * @s: Pointer to the start of the area.
5713b3c4babSMatthew Wilcox  * @v: The value to fill the area with
5723b3c4babSMatthew Wilcox  * @count: The number of values to store
5733b3c4babSMatthew Wilcox  *
5743b3c4babSMatthew Wilcox  * Differs from memset() in that it fills with a uint32_t instead
5753b3c4babSMatthew Wilcox  * of a byte.  Remember that @count is the number of uint32_ts to
5763b3c4babSMatthew Wilcox  * store, not the number of bytes.
5773b3c4babSMatthew Wilcox  */
memset32(uint32_t * s,uint32_t v,size_t count)5783b3c4babSMatthew Wilcox void *memset32(uint32_t *s, uint32_t v, size_t count)
5793b3c4babSMatthew Wilcox {
5803b3c4babSMatthew Wilcox 	uint32_t *xs = s;
5813b3c4babSMatthew Wilcox 
5823b3c4babSMatthew Wilcox 	while (count--)
5833b3c4babSMatthew Wilcox 		*xs++ = v;
5843b3c4babSMatthew Wilcox 	return s;
5853b3c4babSMatthew Wilcox }
5863b3c4babSMatthew Wilcox EXPORT_SYMBOL(memset32);
5873b3c4babSMatthew Wilcox #endif
5883b3c4babSMatthew Wilcox 
5893b3c4babSMatthew Wilcox #ifndef __HAVE_ARCH_MEMSET64
5903b3c4babSMatthew Wilcox /**
5913b3c4babSMatthew Wilcox  * memset64() - Fill a memory area with a uint64_t
5923b3c4babSMatthew Wilcox  * @s: Pointer to the start of the area.
5933b3c4babSMatthew Wilcox  * @v: The value to fill the area with
5943b3c4babSMatthew Wilcox  * @count: The number of values to store
5953b3c4babSMatthew Wilcox  *
5963b3c4babSMatthew Wilcox  * Differs from memset() in that it fills with a uint64_t instead
5973b3c4babSMatthew Wilcox  * of a byte.  Remember that @count is the number of uint64_ts to
5983b3c4babSMatthew Wilcox  * store, not the number of bytes.
5993b3c4babSMatthew Wilcox  */
memset64(uint64_t * s,uint64_t v,size_t count)6003b3c4babSMatthew Wilcox void *memset64(uint64_t *s, uint64_t v, size_t count)
6013b3c4babSMatthew Wilcox {
6023b3c4babSMatthew Wilcox 	uint64_t *xs = s;
6033b3c4babSMatthew Wilcox 
6043b3c4babSMatthew Wilcox 	while (count--)
6053b3c4babSMatthew Wilcox 		*xs++ = v;
6063b3c4babSMatthew Wilcox 	return s;
6073b3c4babSMatthew Wilcox }
6083b3c4babSMatthew Wilcox EXPORT_SYMBOL(memset64);
6093b3c4babSMatthew Wilcox #endif
6103b3c4babSMatthew Wilcox 
6111da177e4SLinus Torvalds #ifndef __HAVE_ARCH_MEMCPY
6121da177e4SLinus Torvalds /**
6131da177e4SLinus Torvalds  * memcpy - Copy one area of memory to another
6141da177e4SLinus Torvalds  * @dest: Where to copy to
6151da177e4SLinus Torvalds  * @src: Where to copy from
6161da177e4SLinus Torvalds  * @count: The size of the area.
6171da177e4SLinus Torvalds  *
6181da177e4SLinus Torvalds  * You should not use this function to access IO space, use memcpy_toio()
6191da177e4SLinus Torvalds  * or memcpy_fromio() instead.
6201da177e4SLinus Torvalds  */
memcpy(void * dest,const void * src,size_t count)6211da177e4SLinus Torvalds void *memcpy(void *dest, const void *src, size_t count)
6221da177e4SLinus Torvalds {
623850b9247SJesper Juhl 	char *tmp = dest;
6244c416ab7SJan-Benedict Glaw 	const char *s = src;
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	while (count--)
6271da177e4SLinus Torvalds 		*tmp++ = *s++;
6281da177e4SLinus Torvalds 	return dest;
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds EXPORT_SYMBOL(memcpy);
6311da177e4SLinus Torvalds #endif
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds #ifndef __HAVE_ARCH_MEMMOVE
6341da177e4SLinus Torvalds /**
6351da177e4SLinus Torvalds  * memmove - Copy one area of memory to another
6361da177e4SLinus Torvalds  * @dest: Where to copy to
6371da177e4SLinus Torvalds  * @src: Where to copy from
6381da177e4SLinus Torvalds  * @count: The size of the area.
6391da177e4SLinus Torvalds  *
6401da177e4SLinus Torvalds  * Unlike memcpy(), memmove() copes with overlapping areas.
6411da177e4SLinus Torvalds  */
memmove(void * dest,const void * src,size_t count)6421da177e4SLinus Torvalds void *memmove(void *dest, const void *src, size_t count)
6431da177e4SLinus Torvalds {
64482da2c37SPaul Jackson 	char *tmp;
64582da2c37SPaul Jackson 	const char *s;
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 	if (dest <= src) {
648850b9247SJesper Juhl 		tmp = dest;
649850b9247SJesper Juhl 		s = src;
6501da177e4SLinus Torvalds 		while (count--)
6511da177e4SLinus Torvalds 			*tmp++ = *s++;
65251a0f0f6SJesper Juhl 	} else {
653850b9247SJesper Juhl 		tmp = dest;
654850b9247SJesper Juhl 		tmp += count;
655850b9247SJesper Juhl 		s = src;
656850b9247SJesper Juhl 		s += count;
6571da177e4SLinus Torvalds 		while (count--)
6581da177e4SLinus Torvalds 			*--tmp = *--s;
6591da177e4SLinus Torvalds 	}
6601da177e4SLinus Torvalds 	return dest;
6611da177e4SLinus Torvalds }
6621da177e4SLinus Torvalds EXPORT_SYMBOL(memmove);
6631da177e4SLinus Torvalds #endif
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds #ifndef __HAVE_ARCH_MEMCMP
6661da177e4SLinus Torvalds /**
6671da177e4SLinus Torvalds  * memcmp - Compare two areas of memory
6681da177e4SLinus Torvalds  * @cs: One area of memory
6691da177e4SLinus Torvalds  * @ct: Another area of memory
6701da177e4SLinus Torvalds  * @count: The size of the area.
6711da177e4SLinus Torvalds  */
6720c28130bSPaolo 'Blaisorblade' Giarrusso #undef memcmp
memcmp(const void * cs,const void * ct,size_t count)673a7330c99SAndi Kleen __visible int memcmp(const void *cs, const void *ct, size_t count)
6741da177e4SLinus Torvalds {
6751da177e4SLinus Torvalds 	const unsigned char *su1, *su2;
6761da177e4SLinus Torvalds 	int res = 0;
6771da177e4SLinus Torvalds 
678291d47ccSLinus Torvalds #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
679291d47ccSLinus Torvalds 	if (count >= sizeof(unsigned long)) {
680291d47ccSLinus Torvalds 		const unsigned long *u1 = cs;
681291d47ccSLinus Torvalds 		const unsigned long *u2 = ct;
682291d47ccSLinus Torvalds 		do {
683291d47ccSLinus Torvalds 			if (get_unaligned(u1) != get_unaligned(u2))
684291d47ccSLinus Torvalds 				break;
685291d47ccSLinus Torvalds 			u1++;
686291d47ccSLinus Torvalds 			u2++;
687291d47ccSLinus Torvalds 			count -= sizeof(unsigned long);
688291d47ccSLinus Torvalds 		} while (count >= sizeof(unsigned long));
689291d47ccSLinus Torvalds 		cs = u1;
690291d47ccSLinus Torvalds 		ct = u2;
691291d47ccSLinus Torvalds 	}
692291d47ccSLinus Torvalds #endif
6931da177e4SLinus Torvalds 	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
6941da177e4SLinus Torvalds 		if ((res = *su1 - *su2) != 0)
6951da177e4SLinus Torvalds 			break;
6961da177e4SLinus Torvalds 	return res;
6971da177e4SLinus Torvalds }
6981da177e4SLinus Torvalds EXPORT_SYMBOL(memcmp);
6991da177e4SLinus Torvalds #endif
7001da177e4SLinus Torvalds 
7015f074f3eSNick Desaulniers #ifndef __HAVE_ARCH_BCMP
7025f074f3eSNick Desaulniers /**
7035f074f3eSNick Desaulniers  * bcmp - returns 0 if and only if the buffers have identical contents.
7045f074f3eSNick Desaulniers  * @a: pointer to first buffer.
7055f074f3eSNick Desaulniers  * @b: pointer to second buffer.
7065f074f3eSNick Desaulniers  * @len: size of buffers.
7075f074f3eSNick Desaulniers  *
7085f074f3eSNick Desaulniers  * The sign or magnitude of a non-zero return value has no particular
7095f074f3eSNick Desaulniers  * meaning, and architectures may implement their own more efficient bcmp(). So
7105f074f3eSNick Desaulniers  * while this particular implementation is a simple (tail) call to memcmp, do
7115f074f3eSNick Desaulniers  * not rely on anything but whether the return value is zero or non-zero.
7125f074f3eSNick Desaulniers  */
bcmp(const void * a,const void * b,size_t len)7135f074f3eSNick Desaulniers int bcmp(const void *a, const void *b, size_t len)
7145f074f3eSNick Desaulniers {
7155f074f3eSNick Desaulniers 	return memcmp(a, b, len);
7165f074f3eSNick Desaulniers }
7175f074f3eSNick Desaulniers EXPORT_SYMBOL(bcmp);
7185f074f3eSNick Desaulniers #endif
7195f074f3eSNick Desaulniers 
7201da177e4SLinus Torvalds #ifndef __HAVE_ARCH_MEMSCAN
7211da177e4SLinus Torvalds /**
7221da177e4SLinus Torvalds  * memscan - Find a character in an area of memory.
7231da177e4SLinus Torvalds  * @addr: The memory area
7241da177e4SLinus Torvalds  * @c: The byte to search for
7251da177e4SLinus Torvalds  * @size: The size of the area.
7261da177e4SLinus Torvalds  *
7271da177e4SLinus Torvalds  * returns the address of the first occurrence of @c, or 1 byte past
7281da177e4SLinus Torvalds  * the area if @c is not found
7291da177e4SLinus Torvalds  */
memscan(void * addr,int c,size_t size)7301da177e4SLinus Torvalds void *memscan(void *addr, int c, size_t size)
7311da177e4SLinus Torvalds {
732850b9247SJesper Juhl 	unsigned char *p = addr;
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	while (size) {
7351a58be62SAlexey Dobriyan 		if (*p == (unsigned char)c)
7361da177e4SLinus Torvalds 			return (void *)p;
7371da177e4SLinus Torvalds 		p++;
7381da177e4SLinus Torvalds 		size--;
7391da177e4SLinus Torvalds 	}
7401da177e4SLinus Torvalds   	return (void *)p;
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds EXPORT_SYMBOL(memscan);
7431da177e4SLinus Torvalds #endif
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds #ifndef __HAVE_ARCH_STRSTR
7461da177e4SLinus Torvalds /**
7471da177e4SLinus Torvalds  * strstr - Find the first substring in a %NUL terminated string
7481da177e4SLinus Torvalds  * @s1: The string to be searched
7491da177e4SLinus Torvalds  * @s2: The string to search for
7501da177e4SLinus Torvalds  */
strstr(const char * s1,const char * s2)7511da177e4SLinus Torvalds char *strstr(const char *s1, const char *s2)
7521da177e4SLinus Torvalds {
753d5f1fb53SLi Zefan 	size_t l1, l2;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	l2 = strlen(s2);
7561da177e4SLinus Torvalds 	if (!l2)
7571da177e4SLinus Torvalds 		return (char *)s1;
7581da177e4SLinus Torvalds 	l1 = strlen(s1);
7591da177e4SLinus Torvalds 	while (l1 >= l2) {
7601da177e4SLinus Torvalds 		l1--;
7611da177e4SLinus Torvalds 		if (!memcmp(s1, s2, l2))
7621da177e4SLinus Torvalds 			return (char *)s1;
7631da177e4SLinus Torvalds 		s1++;
7641da177e4SLinus Torvalds 	}
7651da177e4SLinus Torvalds 	return NULL;
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds EXPORT_SYMBOL(strstr);
7681da177e4SLinus Torvalds #endif
7691da177e4SLinus Torvalds 
770d5f1fb53SLi Zefan #ifndef __HAVE_ARCH_STRNSTR
771d5f1fb53SLi Zefan /**
772d5f1fb53SLi Zefan  * strnstr - Find the first substring in a length-limited string
773d5f1fb53SLi Zefan  * @s1: The string to be searched
774d5f1fb53SLi Zefan  * @s2: The string to search for
775d5f1fb53SLi Zefan  * @len: the maximum number of characters to search
776d5f1fb53SLi Zefan  */
strnstr(const char * s1,const char * s2,size_t len)777d5f1fb53SLi Zefan char *strnstr(const char *s1, const char *s2, size_t len)
778d5f1fb53SLi Zefan {
779d6a2eedfSAndré Goddard Rosa 	size_t l2;
780d5f1fb53SLi Zefan 
781d5f1fb53SLi Zefan 	l2 = strlen(s2);
782d5f1fb53SLi Zefan 	if (!l2)
783d5f1fb53SLi Zefan 		return (char *)s1;
784d6a2eedfSAndré Goddard Rosa 	while (len >= l2) {
785d6a2eedfSAndré Goddard Rosa 		len--;
786d5f1fb53SLi Zefan 		if (!memcmp(s1, s2, l2))
787d5f1fb53SLi Zefan 			return (char *)s1;
788d5f1fb53SLi Zefan 		s1++;
789d5f1fb53SLi Zefan 	}
790d5f1fb53SLi Zefan 	return NULL;
791d5f1fb53SLi Zefan }
792d5f1fb53SLi Zefan EXPORT_SYMBOL(strnstr);
793d5f1fb53SLi Zefan #endif
794d5f1fb53SLi Zefan 
7951da177e4SLinus Torvalds #ifndef __HAVE_ARCH_MEMCHR
7961da177e4SLinus Torvalds /**
7971da177e4SLinus Torvalds  * memchr - Find a character in an area of memory.
7981da177e4SLinus Torvalds  * @s: The memory area
7991da177e4SLinus Torvalds  * @c: The byte to search for
8001da177e4SLinus Torvalds  * @n: The size of the area.
8011da177e4SLinus Torvalds  *
8021da177e4SLinus Torvalds  * returns the address of the first occurrence of @c, or %NULL
8031da177e4SLinus Torvalds  * if @c is not found
8041da177e4SLinus Torvalds  */
memchr(const void * s,int c,size_t n)8051da177e4SLinus Torvalds void *memchr(const void *s, int c, size_t n)
8061da177e4SLinus Torvalds {
8071da177e4SLinus Torvalds 	const unsigned char *p = s;
8081da177e4SLinus Torvalds 	while (n-- != 0) {
8091da177e4SLinus Torvalds         	if ((unsigned char)c == *p++) {
8101da177e4SLinus Torvalds 			return (void *)(p - 1);
8111da177e4SLinus Torvalds 		}
8121da177e4SLinus Torvalds 	}
8131da177e4SLinus Torvalds 	return NULL;
8141da177e4SLinus Torvalds }
8151da177e4SLinus Torvalds EXPORT_SYMBOL(memchr);
8161da177e4SLinus Torvalds #endif
81779824820SAkinobu Mita 
check_bytes8(const u8 * start,u8 value,unsigned int bytes)81879824820SAkinobu Mita static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
81979824820SAkinobu Mita {
82079824820SAkinobu Mita 	while (bytes) {
82179824820SAkinobu Mita 		if (*start != value)
82279824820SAkinobu Mita 			return (void *)start;
82379824820SAkinobu Mita 		start++;
82479824820SAkinobu Mita 		bytes--;
82579824820SAkinobu Mita 	}
82679824820SAkinobu Mita 	return NULL;
82779824820SAkinobu Mita }
82879824820SAkinobu Mita 
82979824820SAkinobu Mita /**
83079824820SAkinobu Mita  * memchr_inv - Find an unmatching character in an area of memory.
83179824820SAkinobu Mita  * @start: The memory area
83279824820SAkinobu Mita  * @c: Find a character other than c
83379824820SAkinobu Mita  * @bytes: The size of the area.
83479824820SAkinobu Mita  *
83579824820SAkinobu Mita  * returns the address of the first character other than @c, or %NULL
83679824820SAkinobu Mita  * if the whole buffer contains just @c.
83779824820SAkinobu Mita  */
memchr_inv(const void * start,int c,size_t bytes)83879824820SAkinobu Mita void *memchr_inv(const void *start, int c, size_t bytes)
83979824820SAkinobu Mita {
84079824820SAkinobu Mita 	u8 value = c;
84179824820SAkinobu Mita 	u64 value64;
84279824820SAkinobu Mita 	unsigned int words, prefix;
84379824820SAkinobu Mita 
84479824820SAkinobu Mita 	if (bytes <= 16)
84579824820SAkinobu Mita 		return check_bytes8(start, value, bytes);
84679824820SAkinobu Mita 
847f43804bfSAkinobu Mita 	value64 = value;
84872d93104SLinus Torvalds #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
8493368e8fbSAndy Shevchenko 	value64 *= 0x0101010101010101ULL;
85072d93104SLinus Torvalds #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
851f43804bfSAkinobu Mita 	value64 *= 0x01010101;
852f43804bfSAkinobu Mita 	value64 |= value64 << 32;
853f43804bfSAkinobu Mita #else
854f43804bfSAkinobu Mita 	value64 |= value64 << 8;
855f43804bfSAkinobu Mita 	value64 |= value64 << 16;
856f43804bfSAkinobu Mita 	value64 |= value64 << 32;
857f43804bfSAkinobu Mita #endif
85879824820SAkinobu Mita 
859f43804bfSAkinobu Mita 	prefix = (unsigned long)start % 8;
86079824820SAkinobu Mita 	if (prefix) {
861f43804bfSAkinobu Mita 		u8 *r;
862f43804bfSAkinobu Mita 
863f43804bfSAkinobu Mita 		prefix = 8 - prefix;
864f43804bfSAkinobu Mita 		r = check_bytes8(start, value, prefix);
86579824820SAkinobu Mita 		if (r)
86679824820SAkinobu Mita 			return r;
86779824820SAkinobu Mita 		start += prefix;
86879824820SAkinobu Mita 		bytes -= prefix;
86979824820SAkinobu Mita 	}
87079824820SAkinobu Mita 
87179824820SAkinobu Mita 	words = bytes / 8;
87279824820SAkinobu Mita 
87379824820SAkinobu Mita 	while (words) {
87479824820SAkinobu Mita 		if (*(u64 *)start != value64)
87579824820SAkinobu Mita 			return check_bytes8(start, value, 8);
87679824820SAkinobu Mita 		start += 8;
87779824820SAkinobu Mita 		words--;
87879824820SAkinobu Mita 	}
87979824820SAkinobu Mita 
88079824820SAkinobu Mita 	return check_bytes8(start, value, bytes % 8);
88179824820SAkinobu Mita }
88279824820SAkinobu Mita EXPORT_SYMBOL(memchr_inv);
883