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