string.c 18 KB

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