xref: /openbmc/linux/lib/string.c (revision 2640a82b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/lib/string.c
4  *
5  *  Copyright (C) 1991, 1992  Linus Torvalds
6  */
7 
8 /*
9  * This file should be used only for "library" routines that may have
10  * alternative implementations on specific architectures (generally
11  * found in <asm-xx/string.h>), or get overloaded by FORTIFY_SOURCE.
12  * (Specifically, this file is built with __NO_FORTIFY.)
13  *
14  * Other helper functions should live in string_helpers.c.
15  */
16 
17 #define __NO_FORTIFY
18 #include <linux/types.h>
19 #include <linux/string.h>
20 #include <linux/ctype.h>
21 #include <linux/kernel.h>
22 #include <linux/export.h>
23 #include <linux/bug.h>
24 #include <linux/errno.h>
25 #include <linux/slab.h>
26 
27 #include <asm/unaligned.h>
28 #include <asm/byteorder.h>
29 #include <asm/word-at-a-time.h>
30 #include <asm/page.h>
31 
32 #ifndef __HAVE_ARCH_STRNCASECMP
33 /**
34  * strncasecmp - Case insensitive, length-limited string comparison
35  * @s1: One string
36  * @s2: The other string
37  * @len: the maximum number of characters to compare
38  */
39 int strncasecmp(const char *s1, const char *s2, size_t len)
40 {
41 	/* Yes, Virginia, it had better be unsigned */
42 	unsigned char c1, c2;
43 
44 	if (!len)
45 		return 0;
46 
47 	do {
48 		c1 = *s1++;
49 		c2 = *s2++;
50 		if (!c1 || !c2)
51 			break;
52 		if (c1 == c2)
53 			continue;
54 		c1 = tolower(c1);
55 		c2 = tolower(c2);
56 		if (c1 != c2)
57 			break;
58 	} while (--len);
59 	return (int)c1 - (int)c2;
60 }
61 EXPORT_SYMBOL(strncasecmp);
62 #endif
63 
64 #ifndef __HAVE_ARCH_STRCASECMP
65 int strcasecmp(const char *s1, const char *s2)
66 {
67 	int c1, c2;
68 
69 	do {
70 		c1 = tolower(*s1++);
71 		c2 = tolower(*s2++);
72 	} while (c1 == c2 && c1 != 0);
73 	return c1 - c2;
74 }
75 EXPORT_SYMBOL(strcasecmp);
76 #endif
77 
78 #ifndef __HAVE_ARCH_STRCPY
79 /**
80  * strcpy - Copy a %NUL terminated string
81  * @dest: Where to copy the string to
82  * @src: Where to copy the string from
83  */
84 char *strcpy(char *dest, const char *src)
85 {
86 	char *tmp = dest;
87 
88 	while ((*dest++ = *src++) != '\0')
89 		/* nothing */;
90 	return tmp;
91 }
92 EXPORT_SYMBOL(strcpy);
93 #endif
94 
95 #ifndef __HAVE_ARCH_STRNCPY
96 /**
97  * strncpy - Copy a length-limited, C-string
98  * @dest: Where to copy the string to
99  * @src: Where to copy the string from
100  * @count: The maximum number of bytes to copy
101  *
102  * The result is not %NUL-terminated if the source exceeds
103  * @count bytes.
104  *
105  * In the case where the length of @src is less than  that  of
106  * count, the remainder of @dest will be padded with %NUL.
107  *
108  */
109 char *strncpy(char *dest, const char *src, size_t count)
110 {
111 	char *tmp = dest;
112 
113 	while (count) {
114 		if ((*tmp = *src) != 0)
115 			src++;
116 		tmp++;
117 		count--;
118 	}
119 	return dest;
120 }
121 EXPORT_SYMBOL(strncpy);
122 #endif
123 
124 #ifndef __HAVE_ARCH_STRLCPY
125 /**
126  * strlcpy - Copy a C-string into a sized buffer
127  * @dest: Where to copy the string to
128  * @src: Where to copy the string from
129  * @size: size of destination buffer
130  *
131  * Compatible with ``*BSD``: the result is always a valid
132  * NUL-terminated string that fits in the buffer (unless,
133  * of course, the buffer size is zero). It does not pad
134  * out the result like strncpy() does.
135  */
136 size_t strlcpy(char *dest, const char *src, size_t size)
137 {
138 	size_t ret = strlen(src);
139 
140 	if (size) {
141 		size_t len = (ret >= size) ? size - 1 : ret;
142 		memcpy(dest, src, len);
143 		dest[len] = '\0';
144 	}
145 	return ret;
146 }
147 EXPORT_SYMBOL(strlcpy);
148 #endif
149 
150 #ifndef __HAVE_ARCH_STRSCPY
151 /**
152  * strscpy - Copy a C-string into a sized buffer
153  * @dest: Where to copy the string to
154  * @src: Where to copy the string from
155  * @count: Size of destination buffer
156  *
157  * Copy the string, or as much of it as fits, into the dest buffer.  The
158  * behavior is undefined if the string buffers overlap.  The destination
159  * buffer is always NUL terminated, unless it's zero-sized.
160  *
161  * Preferred to strlcpy() since the API doesn't require reading memory
162  * from the src string beyond the specified "count" bytes, and since
163  * the return value is easier to error-check than strlcpy()'s.
164  * In addition, the implementation is robust to the string changing out
165  * from underneath it, unlike the current strlcpy() implementation.
166  *
167  * Preferred to strncpy() since it always returns a valid string, and
168  * doesn't unnecessarily force the tail of the destination buffer to be
169  * zeroed.  If zeroing is desired please use strscpy_pad().
170  *
171  * Returns:
172  * * The number of characters copied (not including the trailing %NUL)
173  * * -E2BIG if count is 0 or @src was truncated.
174  */
175 ssize_t strscpy(char *dest, const char *src, size_t count)
176 {
177 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
178 	size_t max = count;
179 	long res = 0;
180 
181 	if (count == 0 || WARN_ON_ONCE(count > INT_MAX))
182 		return -E2BIG;
183 
184 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
185 	/*
186 	 * If src is unaligned, don't cross a page boundary,
187 	 * since we don't know if the next page is mapped.
188 	 */
189 	if ((long)src & (sizeof(long) - 1)) {
190 		size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
191 		if (limit < max)
192 			max = limit;
193 	}
194 #else
195 	/* If src or dest is unaligned, don't do word-at-a-time. */
196 	if (((long) dest | (long) src) & (sizeof(long) - 1))
197 		max = 0;
198 #endif
199 
200 	/*
201 	 * read_word_at_a_time() below may read uninitialized bytes after the
202 	 * trailing zero and use them in comparisons. Disable this optimization
203 	 * under KMSAN to prevent false positive reports.
204 	 */
205 	if (IS_ENABLED(CONFIG_KMSAN))
206 		max = 0;
207 
208 	while (max >= sizeof(unsigned long)) {
209 		unsigned long c, data;
210 
211 		c = read_word_at_a_time(src+res);
212 		if (has_zero(c, &data, &constants)) {
213 			data = prep_zero_mask(c, data, &constants);
214 			data = create_zero_mask(data);
215 			*(unsigned long *)(dest+res) = c & zero_bytemask(data);
216 			return res + find_zero(data);
217 		}
218 		*(unsigned long *)(dest+res) = c;
219 		res += sizeof(unsigned long);
220 		count -= sizeof(unsigned long);
221 		max -= sizeof(unsigned long);
222 	}
223 
224 	while (count) {
225 		char c;
226 
227 		c = src[res];
228 		dest[res] = c;
229 		if (!c)
230 			return res;
231 		res++;
232 		count--;
233 	}
234 
235 	/* Hit buffer length without finding a NUL; force NUL-termination. */
236 	if (res)
237 		dest[res-1] = '\0';
238 
239 	return -E2BIG;
240 }
241 EXPORT_SYMBOL(strscpy);
242 #endif
243 
244 /**
245  * stpcpy - copy a string from src to dest returning a pointer to the new end
246  *          of dest, including src's %NUL-terminator. May overrun dest.
247  * @dest: pointer to end of string being copied into. Must be large enough
248  *        to receive copy.
249  * @src: pointer to the beginning of string being copied from. Must not overlap
250  *       dest.
251  *
252  * stpcpy differs from strcpy in a key way: the return value is a pointer
253  * to the new %NUL-terminating character in @dest. (For strcpy, the return
254  * value is a pointer to the start of @dest). This interface is considered
255  * unsafe as it doesn't perform bounds checking of the inputs. As such it's
256  * not recommended for usage. Instead, its definition is provided in case
257  * the compiler lowers other libcalls to stpcpy.
258  */
259 char *stpcpy(char *__restrict__ dest, const char *__restrict__ src);
260 char *stpcpy(char *__restrict__ dest, const char *__restrict__ src)
261 {
262 	while ((*dest++ = *src++) != '\0')
263 		/* nothing */;
264 	return --dest;
265 }
266 EXPORT_SYMBOL(stpcpy);
267 
268 #ifndef __HAVE_ARCH_STRCAT
269 /**
270  * strcat - Append one %NUL-terminated string to another
271  * @dest: The string to be appended to
272  * @src: The string to append to it
273  */
274 char *strcat(char *dest, const char *src)
275 {
276 	char *tmp = dest;
277 
278 	while (*dest)
279 		dest++;
280 	while ((*dest++ = *src++) != '\0')
281 		;
282 	return tmp;
283 }
284 EXPORT_SYMBOL(strcat);
285 #endif
286 
287 #ifndef __HAVE_ARCH_STRNCAT
288 /**
289  * strncat - Append a length-limited, C-string to another
290  * @dest: The string to be appended to
291  * @src: The string to append to it
292  * @count: The maximum numbers of bytes to copy
293  *
294  * Note that in contrast to strncpy(), strncat() ensures the result is
295  * terminated.
296  */
297 char *strncat(char *dest, const char *src, size_t count)
298 {
299 	char *tmp = dest;
300 
301 	if (count) {
302 		while (*dest)
303 			dest++;
304 		while ((*dest++ = *src++) != 0) {
305 			if (--count == 0) {
306 				*dest = '\0';
307 				break;
308 			}
309 		}
310 	}
311 	return tmp;
312 }
313 EXPORT_SYMBOL(strncat);
314 #endif
315 
316 #ifndef __HAVE_ARCH_STRLCAT
317 /**
318  * strlcat - Append a length-limited, C-string to another
319  * @dest: The string to be appended to
320  * @src: The string to append to it
321  * @count: The size of the destination buffer.
322  */
323 size_t strlcat(char *dest, const char *src, size_t count)
324 {
325 	size_t dsize = strlen(dest);
326 	size_t len = strlen(src);
327 	size_t res = dsize + len;
328 
329 	/* This would be a bug */
330 	BUG_ON(dsize >= count);
331 
332 	dest += dsize;
333 	count -= dsize;
334 	if (len >= count)
335 		len = count-1;
336 	memcpy(dest, src, len);
337 	dest[len] = 0;
338 	return res;
339 }
340 EXPORT_SYMBOL(strlcat);
341 #endif
342 
343 #ifndef __HAVE_ARCH_STRCMP
344 /**
345  * strcmp - Compare two strings
346  * @cs: One string
347  * @ct: Another string
348  */
349 int strcmp(const char *cs, const char *ct)
350 {
351 	unsigned char c1, c2;
352 
353 	while (1) {
354 		c1 = *cs++;
355 		c2 = *ct++;
356 		if (c1 != c2)
357 			return c1 < c2 ? -1 : 1;
358 		if (!c1)
359 			break;
360 	}
361 	return 0;
362 }
363 EXPORT_SYMBOL(strcmp);
364 #endif
365 
366 #ifndef __HAVE_ARCH_STRNCMP
367 /**
368  * strncmp - Compare two length-limited strings
369  * @cs: One string
370  * @ct: Another string
371  * @count: The maximum number of bytes to compare
372  */
373 int strncmp(const char *cs, const char *ct, size_t count)
374 {
375 	unsigned char c1, c2;
376 
377 	while (count) {
378 		c1 = *cs++;
379 		c2 = *ct++;
380 		if (c1 != c2)
381 			return c1 < c2 ? -1 : 1;
382 		if (!c1)
383 			break;
384 		count--;
385 	}
386 	return 0;
387 }
388 EXPORT_SYMBOL(strncmp);
389 #endif
390 
391 #ifndef __HAVE_ARCH_STRCHR
392 /**
393  * strchr - Find the first occurrence of a character in a string
394  * @s: The string to be searched
395  * @c: The character to search for
396  *
397  * Note that the %NUL-terminator is considered part of the string, and can
398  * be searched for.
399  */
400 char *strchr(const char *s, int c)
401 {
402 	for (; *s != (char)c; ++s)
403 		if (*s == '\0')
404 			return NULL;
405 	return (char *)s;
406 }
407 EXPORT_SYMBOL(strchr);
408 #endif
409 
410 #ifndef __HAVE_ARCH_STRCHRNUL
411 /**
412  * strchrnul - Find and return a character in a string, or end of string
413  * @s: The string to be searched
414  * @c: The character to search for
415  *
416  * Returns pointer to first occurrence of 'c' in s. If c is not found, then
417  * return a pointer to the null byte at the end of s.
418  */
419 char *strchrnul(const char *s, int c)
420 {
421 	while (*s && *s != (char)c)
422 		s++;
423 	return (char *)s;
424 }
425 EXPORT_SYMBOL(strchrnul);
426 #endif
427 
428 /**
429  * strnchrnul - Find and return a character in a length limited string,
430  * or end of string
431  * @s: The string to be searched
432  * @count: The number of characters to be searched
433  * @c: The character to search for
434  *
435  * Returns pointer to the first occurrence of 'c' in s. If c is not found,
436  * then return a pointer to the last character of the string.
437  */
438 char *strnchrnul(const char *s, size_t count, int c)
439 {
440 	while (count-- && *s && *s != (char)c)
441 		s++;
442 	return (char *)s;
443 }
444 
445 #ifndef __HAVE_ARCH_STRRCHR
446 /**
447  * strrchr - Find the last occurrence of a character in a string
448  * @s: The string to be searched
449  * @c: The character to search for
450  */
451 char *strrchr(const char *s, int c)
452 {
453 	const char *last = NULL;
454 	do {
455 		if (*s == (char)c)
456 			last = s;
457 	} while (*s++);
458 	return (char *)last;
459 }
460 EXPORT_SYMBOL(strrchr);
461 #endif
462 
463 #ifndef __HAVE_ARCH_STRNCHR
464 /**
465  * strnchr - Find a character in a length limited string
466  * @s: The string to be searched
467  * @count: The number of characters to be searched
468  * @c: The character to search for
469  *
470  * Note that the %NUL-terminator is considered part of the string, and can
471  * be searched for.
472  */
473 char *strnchr(const char *s, size_t count, int c)
474 {
475 	while (count--) {
476 		if (*s == (char)c)
477 			return (char *)s;
478 		if (*s++ == '\0')
479 			break;
480 	}
481 	return NULL;
482 }
483 EXPORT_SYMBOL(strnchr);
484 #endif
485 
486 #ifndef __HAVE_ARCH_STRLEN
487 /**
488  * strlen - Find the length of a string
489  * @s: The string to be sized
490  */
491 size_t strlen(const char *s)
492 {
493 	const char *sc;
494 
495 	for (sc = s; *sc != '\0'; ++sc)
496 		/* nothing */;
497 	return sc - s;
498 }
499 EXPORT_SYMBOL(strlen);
500 #endif
501 
502 #ifndef __HAVE_ARCH_STRNLEN
503 /**
504  * strnlen - Find the length of a length-limited string
505  * @s: The string to be sized
506  * @count: The maximum number of bytes to search
507  */
508 size_t strnlen(const char *s, size_t count)
509 {
510 	const char *sc;
511 
512 	for (sc = s; count-- && *sc != '\0'; ++sc)
513 		/* nothing */;
514 	return sc - s;
515 }
516 EXPORT_SYMBOL(strnlen);
517 #endif
518 
519 #ifndef __HAVE_ARCH_STRSPN
520 /**
521  * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
522  * @s: The string to be searched
523  * @accept: The string to search for
524  */
525 size_t strspn(const char *s, const char *accept)
526 {
527 	const char *p;
528 
529 	for (p = s; *p != '\0'; ++p) {
530 		if (!strchr(accept, *p))
531 			break;
532 	}
533 	return p - s;
534 }
535 EXPORT_SYMBOL(strspn);
536 #endif
537 
538 #ifndef __HAVE_ARCH_STRCSPN
539 /**
540  * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
541  * @s: The string to be searched
542  * @reject: The string to avoid
543  */
544 size_t strcspn(const char *s, const char *reject)
545 {
546 	const char *p;
547 
548 	for (p = s; *p != '\0'; ++p) {
549 		if (strchr(reject, *p))
550 			break;
551 	}
552 	return p - s;
553 }
554 EXPORT_SYMBOL(strcspn);
555 #endif
556 
557 #ifndef __HAVE_ARCH_STRPBRK
558 /**
559  * strpbrk - Find the first occurrence of a set of characters
560  * @cs: The string to be searched
561  * @ct: The characters to search for
562  */
563 char *strpbrk(const char *cs, const char *ct)
564 {
565 	const char *sc1, *sc2;
566 
567 	for (sc1 = cs; *sc1 != '\0'; ++sc1) {
568 		for (sc2 = ct; *sc2 != '\0'; ++sc2) {
569 			if (*sc1 == *sc2)
570 				return (char *)sc1;
571 		}
572 	}
573 	return NULL;
574 }
575 EXPORT_SYMBOL(strpbrk);
576 #endif
577 
578 #ifndef __HAVE_ARCH_STRSEP
579 /**
580  * strsep - Split a string into tokens
581  * @s: The string to be searched
582  * @ct: The characters to search for
583  *
584  * strsep() updates @s to point after the token, ready for the next call.
585  *
586  * It returns empty tokens, too, behaving exactly like the libc function
587  * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
588  * Same semantics, slimmer shape. ;)
589  */
590 char *strsep(char **s, const char *ct)
591 {
592 	char *sbegin = *s;
593 	char *end;
594 
595 	if (sbegin == NULL)
596 		return NULL;
597 
598 	end = strpbrk(sbegin, ct);
599 	if (end)
600 		*end++ = '\0';
601 	*s = end;
602 	return sbegin;
603 }
604 EXPORT_SYMBOL(strsep);
605 #endif
606 
607 #ifndef __HAVE_ARCH_MEMSET
608 /**
609  * memset - Fill a region of memory with the given value
610  * @s: Pointer to the start of the area.
611  * @c: The byte to fill the area with
612  * @count: The size of the area.
613  *
614  * Do not use memset() to access IO space, use memset_io() instead.
615  */
616 void *memset(void *s, int c, size_t count)
617 {
618 	char *xs = s;
619 
620 	while (count--)
621 		*xs++ = c;
622 	return s;
623 }
624 EXPORT_SYMBOL(memset);
625 #endif
626 
627 #ifndef __HAVE_ARCH_MEMSET16
628 /**
629  * memset16() - Fill a memory area with a uint16_t
630  * @s: Pointer to the start of the area.
631  * @v: The value to fill the area with
632  * @count: The number of values to store
633  *
634  * Differs from memset() in that it fills with a uint16_t instead
635  * of a byte.  Remember that @count is the number of uint16_ts to
636  * store, not the number of bytes.
637  */
638 void *memset16(uint16_t *s, uint16_t v, size_t count)
639 {
640 	uint16_t *xs = s;
641 
642 	while (count--)
643 		*xs++ = v;
644 	return s;
645 }
646 EXPORT_SYMBOL(memset16);
647 #endif
648 
649 #ifndef __HAVE_ARCH_MEMSET32
650 /**
651  * memset32() - Fill a memory area with a uint32_t
652  * @s: Pointer to the start of the area.
653  * @v: The value to fill the area with
654  * @count: The number of values to store
655  *
656  * Differs from memset() in that it fills with a uint32_t instead
657  * of a byte.  Remember that @count is the number of uint32_ts to
658  * store, not the number of bytes.
659  */
660 void *memset32(uint32_t *s, uint32_t v, size_t count)
661 {
662 	uint32_t *xs = s;
663 
664 	while (count--)
665 		*xs++ = v;
666 	return s;
667 }
668 EXPORT_SYMBOL(memset32);
669 #endif
670 
671 #ifndef __HAVE_ARCH_MEMSET64
672 /**
673  * memset64() - Fill a memory area with a uint64_t
674  * @s: Pointer to the start of the area.
675  * @v: The value to fill the area with
676  * @count: The number of values to store
677  *
678  * Differs from memset() in that it fills with a uint64_t instead
679  * of a byte.  Remember that @count is the number of uint64_ts to
680  * store, not the number of bytes.
681  */
682 void *memset64(uint64_t *s, uint64_t v, size_t count)
683 {
684 	uint64_t *xs = s;
685 
686 	while (count--)
687 		*xs++ = v;
688 	return s;
689 }
690 EXPORT_SYMBOL(memset64);
691 #endif
692 
693 #ifndef __HAVE_ARCH_MEMCPY
694 /**
695  * memcpy - Copy one area of memory to another
696  * @dest: Where to copy to
697  * @src: Where to copy from
698  * @count: The size of the area.
699  *
700  * You should not use this function to access IO space, use memcpy_toio()
701  * or memcpy_fromio() instead.
702  */
703 void *memcpy(void *dest, const void *src, size_t count)
704 {
705 	char *tmp = dest;
706 	const char *s = src;
707 
708 	while (count--)
709 		*tmp++ = *s++;
710 	return dest;
711 }
712 EXPORT_SYMBOL(memcpy);
713 #endif
714 
715 #ifndef __HAVE_ARCH_MEMMOVE
716 /**
717  * memmove - Copy one area of memory to another
718  * @dest: Where to copy to
719  * @src: Where to copy from
720  * @count: The size of the area.
721  *
722  * Unlike memcpy(), memmove() copes with overlapping areas.
723  */
724 void *memmove(void *dest, const void *src, size_t count)
725 {
726 	char *tmp;
727 	const char *s;
728 
729 	if (dest <= src) {
730 		tmp = dest;
731 		s = src;
732 		while (count--)
733 			*tmp++ = *s++;
734 	} else {
735 		tmp = dest;
736 		tmp += count;
737 		s = src;
738 		s += count;
739 		while (count--)
740 			*--tmp = *--s;
741 	}
742 	return dest;
743 }
744 EXPORT_SYMBOL(memmove);
745 #endif
746 
747 #ifndef __HAVE_ARCH_MEMCMP
748 /**
749  * memcmp - Compare two areas of memory
750  * @cs: One area of memory
751  * @ct: Another area of memory
752  * @count: The size of the area.
753  */
754 #undef memcmp
755 __visible int memcmp(const void *cs, const void *ct, size_t count)
756 {
757 	const unsigned char *su1, *su2;
758 	int res = 0;
759 
760 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
761 	if (count >= sizeof(unsigned long)) {
762 		const unsigned long *u1 = cs;
763 		const unsigned long *u2 = ct;
764 		do {
765 			if (get_unaligned(u1) != get_unaligned(u2))
766 				break;
767 			u1++;
768 			u2++;
769 			count -= sizeof(unsigned long);
770 		} while (count >= sizeof(unsigned long));
771 		cs = u1;
772 		ct = u2;
773 	}
774 #endif
775 	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
776 		if ((res = *su1 - *su2) != 0)
777 			break;
778 	return res;
779 }
780 EXPORT_SYMBOL(memcmp);
781 #endif
782 
783 #ifndef __HAVE_ARCH_BCMP
784 /**
785  * bcmp - returns 0 if and only if the buffers have identical contents.
786  * @a: pointer to first buffer.
787  * @b: pointer to second buffer.
788  * @len: size of buffers.
789  *
790  * The sign or magnitude of a non-zero return value has no particular
791  * meaning, and architectures may implement their own more efficient bcmp(). So
792  * while this particular implementation is a simple (tail) call to memcmp, do
793  * not rely on anything but whether the return value is zero or non-zero.
794  */
795 int bcmp(const void *a, const void *b, size_t len)
796 {
797 	return memcmp(a, b, len);
798 }
799 EXPORT_SYMBOL(bcmp);
800 #endif
801 
802 #ifndef __HAVE_ARCH_MEMSCAN
803 /**
804  * memscan - Find a character in an area of memory.
805  * @addr: The memory area
806  * @c: The byte to search for
807  * @size: The size of the area.
808  *
809  * returns the address of the first occurrence of @c, or 1 byte past
810  * the area if @c is not found
811  */
812 void *memscan(void *addr, int c, size_t size)
813 {
814 	unsigned char *p = addr;
815 
816 	while (size) {
817 		if (*p == (unsigned char)c)
818 			return (void *)p;
819 		p++;
820 		size--;
821 	}
822   	return (void *)p;
823 }
824 EXPORT_SYMBOL(memscan);
825 #endif
826 
827 #ifndef __HAVE_ARCH_STRSTR
828 /**
829  * strstr - Find the first substring in a %NUL terminated string
830  * @s1: The string to be searched
831  * @s2: The string to search for
832  */
833 char *strstr(const char *s1, const char *s2)
834 {
835 	size_t l1, l2;
836 
837 	l2 = strlen(s2);
838 	if (!l2)
839 		return (char *)s1;
840 	l1 = strlen(s1);
841 	while (l1 >= l2) {
842 		l1--;
843 		if (!memcmp(s1, s2, l2))
844 			return (char *)s1;
845 		s1++;
846 	}
847 	return NULL;
848 }
849 EXPORT_SYMBOL(strstr);
850 #endif
851 
852 #ifndef __HAVE_ARCH_STRNSTR
853 /**
854  * strnstr - Find the first substring in a length-limited string
855  * @s1: The string to be searched
856  * @s2: The string to search for
857  * @len: the maximum number of characters to search
858  */
859 char *strnstr(const char *s1, const char *s2, size_t len)
860 {
861 	size_t l2;
862 
863 	l2 = strlen(s2);
864 	if (!l2)
865 		return (char *)s1;
866 	while (len >= l2) {
867 		len--;
868 		if (!memcmp(s1, s2, l2))
869 			return (char *)s1;
870 		s1++;
871 	}
872 	return NULL;
873 }
874 EXPORT_SYMBOL(strnstr);
875 #endif
876 
877 #ifndef __HAVE_ARCH_MEMCHR
878 /**
879  * memchr - Find a character in an area of memory.
880  * @s: The memory area
881  * @c: The byte to search for
882  * @n: The size of the area.
883  *
884  * returns the address of the first occurrence of @c, or %NULL
885  * if @c is not found
886  */
887 void *memchr(const void *s, int c, size_t n)
888 {
889 	const unsigned char *p = s;
890 	while (n-- != 0) {
891         	if ((unsigned char)c == *p++) {
892 			return (void *)(p - 1);
893 		}
894 	}
895 	return NULL;
896 }
897 EXPORT_SYMBOL(memchr);
898 #endif
899 
900 static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
901 {
902 	while (bytes) {
903 		if (*start != value)
904 			return (void *)start;
905 		start++;
906 		bytes--;
907 	}
908 	return NULL;
909 }
910 
911 /**
912  * memchr_inv - Find an unmatching character in an area of memory.
913  * @start: The memory area
914  * @c: Find a character other than c
915  * @bytes: The size of the area.
916  *
917  * returns the address of the first character other than @c, or %NULL
918  * if the whole buffer contains just @c.
919  */
920 void *memchr_inv(const void *start, int c, size_t bytes)
921 {
922 	u8 value = c;
923 	u64 value64;
924 	unsigned int words, prefix;
925 
926 	if (bytes <= 16)
927 		return check_bytes8(start, value, bytes);
928 
929 	value64 = value;
930 #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
931 	value64 *= 0x0101010101010101ULL;
932 #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
933 	value64 *= 0x01010101;
934 	value64 |= value64 << 32;
935 #else
936 	value64 |= value64 << 8;
937 	value64 |= value64 << 16;
938 	value64 |= value64 << 32;
939 #endif
940 
941 	prefix = (unsigned long)start % 8;
942 	if (prefix) {
943 		u8 *r;
944 
945 		prefix = 8 - prefix;
946 		r = check_bytes8(start, value, prefix);
947 		if (r)
948 			return r;
949 		start += prefix;
950 		bytes -= prefix;
951 	}
952 
953 	words = bytes / 8;
954 
955 	while (words) {
956 		if (*(u64 *)start != value64)
957 			return check_bytes8(start, value, 8);
958 		start += 8;
959 		words--;
960 	}
961 
962 	return check_bytes8(start, value, bytes % 8);
963 }
964 EXPORT_SYMBOL(memchr_inv);
965