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