string.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * linux/lib/string.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * stupid library routines.. The optimized versions should generally be found
  8. * as inline code in <asm-xx/string.h>
  9. *
  10. * These are buggy as well..
  11. *
  12. * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
  13. * - Added strsep() which will replace strtok() soon (because strsep() is
  14. * reentrant and should be faster). Use only strsep() in new code, please.
  15. *
  16. * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
  17. * Matthew Hawkins <matt@mh.dropbear.id.au>
  18. * - Kissed strtok() goodbye
  19. */
  20. #include <linux/types.h>
  21. #include <linux/string.h>
  22. #include <linux/ctype.h>
  23. #include <linux/bug.h>
  24. #include <linux/module.h>
  25. #ifndef __HAVE_ARCH_STRNICMP
  26. /**
  27. * strnicmp - Case insensitive, length-limited string comparison
  28. * @s1: One string
  29. * @s2: The other string
  30. * @len: the maximum number of characters to compare
  31. */
  32. int strnicmp(const char *s1, const char *s2, size_t len)
  33. {
  34. /* Yes, Virginia, it had better be unsigned */
  35. unsigned char c1, c2;
  36. if (!len)
  37. return 0;
  38. do {
  39. c1 = *s1++;
  40. c2 = *s2++;
  41. if (!c1 || !c2)
  42. break;
  43. if (c1 == c2)
  44. continue;
  45. c1 = tolower(c1);
  46. c2 = tolower(c2);
  47. if (c1 != c2)
  48. break;
  49. } while (--len);
  50. return (int)c1 - (int)c2;
  51. }
  52. EXPORT_SYMBOL(strnicmp);
  53. #endif
  54. #ifndef __HAVE_ARCH_STRCASECMP
  55. int strcasecmp(const char *s1, const char *s2)
  56. {
  57. int c1, c2;
  58. do {
  59. c1 = tolower(*s1++);
  60. c2 = tolower(*s2++);
  61. } while (c1 == c2 && c1 != 0);
  62. return c1 - c2;
  63. }
  64. EXPORT_SYMBOL(strcasecmp);
  65. #endif
  66. #ifndef __HAVE_ARCH_STRNCASECMP
  67. int strncasecmp(const char *s1, const char *s2, size_t n)
  68. {
  69. int c1, c2;
  70. do {
  71. c1 = tolower(*s1++);
  72. c2 = tolower(*s2++);
  73. } while ((--n > 0) && c1 == c2 && c1 != 0);
  74. return c1 - c2;
  75. }
  76. EXPORT_SYMBOL(strncasecmp);
  77. #endif
  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. #undef strcpy
  85. char *strcpy(char *dest, const char *src)
  86. {
  87. char *tmp = dest;
  88. while ((*dest++ = *src++) != '\0')
  89. /* nothing */;
  90. return tmp;
  91. }
  92. EXPORT_SYMBOL(strcpy);
  93. #endif
  94. #ifndef __HAVE_ARCH_STRNCPY
  95. /**
  96. * strncpy - Copy a length-limited, %NUL-terminated string
  97. * @dest: Where to copy the string to
  98. * @src: Where to copy the string from
  99. * @count: The maximum number of bytes to copy
  100. *
  101. * The result is not %NUL-terminated if the source exceeds
  102. * @count bytes.
  103. *
  104. * In the case where the length of @src is less than that of
  105. * count, the remainder of @dest will be padded with %NUL.
  106. *
  107. */
  108. char *strncpy(char *dest, const char *src, size_t count)
  109. {
  110. char *tmp = dest;
  111. while (count) {
  112. if ((*tmp = *src) != 0)
  113. src++;
  114. tmp++;
  115. count--;
  116. }
  117. return dest;
  118. }
  119. EXPORT_SYMBOL(strncpy);
  120. #endif
  121. #ifndef __HAVE_ARCH_STRLCPY
  122. /**
  123. * strlcpy - Copy a %NUL terminated string into a sized buffer
  124. * @dest: Where to copy the string to
  125. * @src: Where to copy the string from
  126. * @size: size of destination buffer
  127. *
  128. * Compatible with *BSD: the result is always a valid
  129. * NUL-terminated string that fits in the buffer (unless,
  130. * of course, the buffer size is zero). It does not pad
  131. * out the result like strncpy() does.
  132. */
  133. size_t strlcpy(char *dest, const char *src, size_t size)
  134. {
  135. size_t ret = strlen(src);
  136. if (size) {
  137. size_t len = (ret >= size) ? size - 1 : ret;
  138. memcpy(dest, src, len);
  139. dest[len] = '\0';
  140. }
  141. return ret;
  142. }
  143. EXPORT_SYMBOL(strlcpy);
  144. #endif
  145. #ifndef __HAVE_ARCH_STRCAT
  146. /**
  147. * strcat - Append one %NUL-terminated string to another
  148. * @dest: The string to be appended to
  149. * @src: The string to append to it
  150. */
  151. #undef strcat
  152. char *strcat(char *dest, const char *src)
  153. {
  154. char *tmp = dest;
  155. while (*dest)
  156. dest++;
  157. while ((*dest++ = *src++) != '\0')
  158. ;
  159. return tmp;
  160. }
  161. EXPORT_SYMBOL(strcat);
  162. #endif
  163. #ifndef __HAVE_ARCH_STRNCAT
  164. /**
  165. * strncat - Append a length-limited, %NUL-terminated string to another
  166. * @dest: The string to be appended to
  167. * @src: The string to append to it
  168. * @count: The maximum numbers of bytes to copy
  169. *
  170. * Note that in contrast to strncpy(), strncat() ensures the result is
  171. * terminated.
  172. */
  173. char *strncat(char *dest, const char *src, size_t count)
  174. {
  175. char *tmp = dest;
  176. if (count) {
  177. while (*dest)
  178. dest++;
  179. while ((*dest++ = *src++) != 0) {
  180. if (--count == 0) {
  181. *dest = '\0';
  182. break;
  183. }
  184. }
  185. }
  186. return tmp;
  187. }
  188. EXPORT_SYMBOL(strncat);
  189. #endif
  190. #ifndef __HAVE_ARCH_STRLCAT
  191. /**
  192. * strlcat - Append a length-limited, %NUL-terminated string to another
  193. * @dest: The string to be appended to
  194. * @src: The string to append to it
  195. * @count: The size of the destination buffer.
  196. */
  197. size_t strlcat(char *dest, const char *src, size_t count)
  198. {
  199. size_t dsize = strlen(dest);
  200. size_t len = strlen(src);
  201. size_t res = dsize + len;
  202. /* This would be a bug */
  203. BUG_ON(dsize >= count);
  204. dest += dsize;
  205. count -= dsize;
  206. if (len >= count)
  207. len = count-1;
  208. memcpy(dest, src, len);
  209. dest[len] = 0;
  210. return res;
  211. }
  212. EXPORT_SYMBOL(strlcat);
  213. #endif
  214. #ifndef __HAVE_ARCH_STRCMP
  215. /**
  216. * strcmp - Compare two strings
  217. * @cs: One string
  218. * @ct: Another string
  219. */
  220. #undef strcmp
  221. int strcmp(const char *cs, const char *ct)
  222. {
  223. unsigned char c1, c2;
  224. while (1) {
  225. c1 = *cs++;
  226. c2 = *ct++;
  227. if (c1 != c2)
  228. return c1 < c2 ? -1 : 1;
  229. if (!c1)
  230. break;
  231. }
  232. return 0;
  233. }
  234. EXPORT_SYMBOL(strcmp);
  235. #endif
  236. #ifndef __HAVE_ARCH_STRNCMP
  237. /**
  238. * strncmp - Compare two length-limited strings
  239. * @cs: One string
  240. * @ct: Another string
  241. * @count: The maximum number of bytes to compare
  242. */
  243. int strncmp(const char *cs, const char *ct, size_t count)
  244. {
  245. unsigned char c1, c2;
  246. while (count) {
  247. c1 = *cs++;
  248. c2 = *ct++;
  249. if (c1 != c2)
  250. return c1 < c2 ? -1 : 1;
  251. if (!c1)
  252. break;
  253. count--;
  254. }
  255. return 0;
  256. }
  257. EXPORT_SYMBOL(strncmp);
  258. #endif
  259. #ifndef __HAVE_ARCH_STRCHR
  260. /**
  261. * strchr - Find the first occurrence of a character in a string
  262. * @s: The string to be searched
  263. * @c: The character to search for
  264. */
  265. char *strchr(const char *s, int c)
  266. {
  267. for (; *s != (char)c; ++s)
  268. if (*s == '\0')
  269. return NULL;
  270. return (char *)s;
  271. }
  272. EXPORT_SYMBOL(strchr);
  273. #endif
  274. #ifndef __HAVE_ARCH_STRRCHR
  275. /**
  276. * strrchr - Find the last occurrence of a character in a string
  277. * @s: The string to be searched
  278. * @c: The character to search for
  279. */
  280. char *strrchr(const char *s, int c)
  281. {
  282. const char *p = s + strlen(s);
  283. do {
  284. if (*p == (char)c)
  285. return (char *)p;
  286. } while (--p >= s);
  287. return NULL;
  288. }
  289. EXPORT_SYMBOL(strrchr);
  290. #endif
  291. #ifndef __HAVE_ARCH_STRNCHR
  292. /**
  293. * strnchr - Find a character in a length limited string
  294. * @s: The string to be searched
  295. * @count: The number of characters to be searched
  296. * @c: The character to search for
  297. */
  298. char *strnchr(const char *s, size_t count, int c)
  299. {
  300. for (; count-- && *s != '\0'; ++s)
  301. if (*s == (char)c)
  302. return (char *)s;
  303. return NULL;
  304. }
  305. EXPORT_SYMBOL(strnchr);
  306. #endif
  307. /**
  308. * skip_spaces - Removes leading whitespace from @str.
  309. * @str: The string to be stripped.
  310. *
  311. * Returns a pointer to the first non-whitespace character in @str.
  312. */
  313. char *skip_spaces(const char *str)
  314. {
  315. while (isspace(*str))
  316. ++str;
  317. return (char *)str;
  318. }
  319. EXPORT_SYMBOL(skip_spaces);
  320. /**
  321. * strim - Removes leading and trailing whitespace from @s.
  322. * @s: The string to be stripped.
  323. *
  324. * Note that the first trailing whitespace is replaced with a %NUL-terminator
  325. * in the given string @s. Returns a pointer to the first non-whitespace
  326. * character in @s.
  327. */
  328. char *strim(char *s)
  329. {
  330. size_t size;
  331. char *end;
  332. size = strlen(s);
  333. if (!size)
  334. return s;
  335. end = s + size - 1;
  336. while (end >= s && isspace(*end))
  337. end--;
  338. *(end + 1) = '\0';
  339. return skip_spaces(s);
  340. }
  341. EXPORT_SYMBOL(strim);
  342. #ifndef __HAVE_ARCH_STRLEN
  343. /**
  344. * strlen - Find the length of a string
  345. * @s: The string to be sized
  346. */
  347. size_t strlen(const char *s)
  348. {
  349. const char *sc;
  350. for (sc = s; *sc != '\0'; ++sc)
  351. /* nothing */;
  352. return sc - s;
  353. }
  354. EXPORT_SYMBOL(strlen);
  355. #endif
  356. #ifndef __HAVE_ARCH_STRNLEN
  357. /**
  358. * strnlen - Find the length of a length-limited string
  359. * @s: The string to be sized
  360. * @count: The maximum number of bytes to search
  361. */
  362. size_t strnlen(const char *s, size_t count)
  363. {
  364. const char *sc;
  365. for (sc = s; count-- && *sc != '\0'; ++sc)
  366. /* nothing */;
  367. return sc - s;
  368. }
  369. EXPORT_SYMBOL(strnlen);
  370. #endif
  371. #ifndef __HAVE_ARCH_STRSPN
  372. /**
  373. * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
  374. * @s: The string to be searched
  375. * @accept: The string to search for
  376. */
  377. size_t strspn(const char *s, const char *accept)
  378. {
  379. const char *p;
  380. const char *a;
  381. size_t count = 0;
  382. for (p = s; *p != '\0'; ++p) {
  383. for (a = accept; *a != '\0'; ++a) {
  384. if (*p == *a)
  385. break;
  386. }
  387. if (*a == '\0')
  388. return count;
  389. ++count;
  390. }
  391. return count;
  392. }
  393. EXPORT_SYMBOL(strspn);
  394. #endif
  395. #ifndef __HAVE_ARCH_STRCSPN
  396. /**
  397. * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
  398. * @s: The string to be searched
  399. * @reject: The string to avoid
  400. */
  401. size_t strcspn(const char *s, const char *reject)
  402. {
  403. const char *p;
  404. const char *r;
  405. size_t count = 0;
  406. for (p = s; *p != '\0'; ++p) {
  407. for (r = reject; *r != '\0'; ++r) {
  408. if (*p == *r)
  409. return count;
  410. }
  411. ++count;
  412. }
  413. return count;
  414. }
  415. EXPORT_SYMBOL(strcspn);
  416. #endif
  417. #ifndef __HAVE_ARCH_STRPBRK
  418. /**
  419. * strpbrk - Find the first occurrence of a set of characters
  420. * @cs: The string to be searched
  421. * @ct: The characters to search for
  422. */
  423. char *strpbrk(const char *cs, const char *ct)
  424. {
  425. const char *sc1, *sc2;
  426. for (sc1 = cs; *sc1 != '\0'; ++sc1) {
  427. for (sc2 = ct; *sc2 != '\0'; ++sc2) {
  428. if (*sc1 == *sc2)
  429. return (char *)sc1;
  430. }
  431. }
  432. return NULL;
  433. }
  434. EXPORT_SYMBOL(strpbrk);
  435. #endif
  436. #ifndef __HAVE_ARCH_STRSEP
  437. /**
  438. * strsep - Split a string into tokens
  439. * @s: The string to be searched
  440. * @ct: The characters to search for
  441. *
  442. * strsep() updates @s to point after the token, ready for the next call.
  443. *
  444. * It returns empty tokens, too, behaving exactly like the libc function
  445. * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
  446. * Same semantics, slimmer shape. ;)
  447. */
  448. char *strsep(char **s, const char *ct)
  449. {
  450. char *sbegin = *s;
  451. char *end;
  452. if (sbegin == NULL)
  453. return NULL;
  454. end = strpbrk(sbegin, ct);
  455. if (end)
  456. *end++ = '\0';
  457. *s = end;
  458. return sbegin;
  459. }
  460. EXPORT_SYMBOL(strsep);
  461. #endif
  462. /**
  463. * sysfs_streq - return true if strings are equal, modulo trailing newline
  464. * @s1: one string
  465. * @s2: another string
  466. *
  467. * This routine returns true iff two strings are equal, treating both
  468. * NUL and newline-then-NUL as equivalent string terminations. It's
  469. * geared for use with sysfs input strings, which generally terminate
  470. * with newlines but are compared against values without newlines.
  471. */
  472. bool sysfs_streq(const char *s1, const char *s2)
  473. {
  474. while (*s1 && *s1 == *s2) {
  475. s1++;
  476. s2++;
  477. }
  478. if (*s1 == *s2)
  479. return true;
  480. if (!*s1 && *s2 == '\n' && !s2[1])
  481. return true;
  482. if (*s1 == '\n' && !s1[1] && !*s2)
  483. return true;
  484. return false;
  485. }
  486. EXPORT_SYMBOL(sysfs_streq);
  487. /**
  488. * strtobool - convert common user inputs into boolean values
  489. * @s: input string
  490. * @res: result
  491. *
  492. * This routine returns 0 iff the first character is one of 'Yy1Nn0'.
  493. * Otherwise it will return -EINVAL. Value pointed to by res is
  494. * updated upon finding a match.
  495. */
  496. int strtobool(const char *s, bool *res)
  497. {
  498. switch (s[0]) {
  499. case 'y':
  500. case 'Y':
  501. case '1':
  502. *res = true;
  503. break;
  504. case 'n':
  505. case 'N':
  506. case '0':
  507. *res = false;
  508. break;
  509. default:
  510. return -EINVAL;
  511. }
  512. return 0;
  513. }
  514. EXPORT_SYMBOL(strtobool);
  515. #ifndef __HAVE_ARCH_MEMSET
  516. /**
  517. * memset - Fill a region of memory with the given value
  518. * @s: Pointer to the start of the area.
  519. * @c: The byte to fill the area with
  520. * @count: The size of the area.
  521. *
  522. * Do not use memset() to access IO space, use memset_io() instead.
  523. */
  524. void *memset(void *s, int c, size_t count)
  525. {
  526. char *xs = s;
  527. while (count--)
  528. *xs++ = c;
  529. return s;
  530. }
  531. EXPORT_SYMBOL(memset);
  532. #endif
  533. #ifndef __HAVE_ARCH_MEMCPY
  534. /**
  535. * memcpy - Copy one area of memory to another
  536. * @dest: Where to copy to
  537. * @src: Where to copy from
  538. * @count: The size of the area.
  539. *
  540. * You should not use this function to access IO space, use memcpy_toio()
  541. * or memcpy_fromio() instead.
  542. */
  543. void *memcpy(void *dest, const void *src, size_t count)
  544. {
  545. char *tmp = dest;
  546. const char *s = src;
  547. while (count--)
  548. *tmp++ = *s++;
  549. return dest;
  550. }
  551. EXPORT_SYMBOL(memcpy);
  552. #endif
  553. #ifndef __HAVE_ARCH_MEMMOVE
  554. /**
  555. * memmove - Copy one area of memory to another
  556. * @dest: Where to copy to
  557. * @src: Where to copy from
  558. * @count: The size of the area.
  559. *
  560. * Unlike memcpy(), memmove() copes with overlapping areas.
  561. */
  562. void *memmove(void *dest, const void *src, size_t count)
  563. {
  564. char *tmp;
  565. const char *s;
  566. if (dest <= src) {
  567. tmp = dest;
  568. s = src;
  569. while (count--)
  570. *tmp++ = *s++;
  571. } else {
  572. tmp = dest;
  573. tmp += count;
  574. s = src;
  575. s += count;
  576. while (count--)
  577. *--tmp = *--s;
  578. }
  579. return dest;
  580. }
  581. EXPORT_SYMBOL(memmove);
  582. #endif
  583. #ifndef __HAVE_ARCH_MEMCMP
  584. /**
  585. * memcmp - Compare two areas of memory
  586. * @cs: One area of memory
  587. * @ct: Another area of memory
  588. * @count: The size of the area.
  589. */
  590. #undef memcmp
  591. int memcmp(const void *cs, const void *ct, size_t count)
  592. {
  593. const unsigned char *su1, *su2;
  594. int res = 0;
  595. for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  596. if ((res = *su1 - *su2) != 0)
  597. break;
  598. return res;
  599. }
  600. EXPORT_SYMBOL(memcmp);
  601. #endif
  602. #ifndef __HAVE_ARCH_MEMSCAN
  603. /**
  604. * memscan - Find a character in an area of memory.
  605. * @addr: The memory area
  606. * @c: The byte to search for
  607. * @size: The size of the area.
  608. *
  609. * returns the address of the first occurrence of @c, or 1 byte past
  610. * the area if @c is not found
  611. */
  612. void *memscan(void *addr, int c, size_t size)
  613. {
  614. unsigned char *p = addr;
  615. while (size) {
  616. if (*p == c)
  617. return (void *)p;
  618. p++;
  619. size--;
  620. }
  621. return (void *)p;
  622. }
  623. EXPORT_SYMBOL(memscan);
  624. #endif
  625. #ifndef __HAVE_ARCH_STRSTR
  626. /**
  627. * strstr - Find the first substring in a %NUL terminated string
  628. * @s1: The string to be searched
  629. * @s2: The string to search for
  630. */
  631. char *strstr(const char *s1, const char *s2)
  632. {
  633. size_t l1, l2;
  634. l2 = strlen(s2);
  635. if (!l2)
  636. return (char *)s1;
  637. l1 = strlen(s1);
  638. while (l1 >= l2) {
  639. l1--;
  640. if (!memcmp(s1, s2, l2))
  641. return (char *)s1;
  642. s1++;
  643. }
  644. return NULL;
  645. }
  646. EXPORT_SYMBOL(strstr);
  647. #endif
  648. #ifndef __HAVE_ARCH_STRNSTR
  649. /**
  650. * strnstr - Find the first substring in a length-limited string
  651. * @s1: The string to be searched
  652. * @s2: The string to search for
  653. * @len: the maximum number of characters to search
  654. */
  655. char *strnstr(const char *s1, const char *s2, size_t len)
  656. {
  657. size_t l2;
  658. l2 = strlen(s2);
  659. if (!l2)
  660. return (char *)s1;
  661. while (len >= l2) {
  662. len--;
  663. if (!memcmp(s1, s2, l2))
  664. return (char *)s1;
  665. s1++;
  666. }
  667. return NULL;
  668. }
  669. EXPORT_SYMBOL(strnstr);
  670. #endif
  671. #ifndef __HAVE_ARCH_MEMCHR
  672. /**
  673. * memchr - Find a character in an area of memory.
  674. * @s: The memory area
  675. * @c: The byte to search for
  676. * @n: The size of the area.
  677. *
  678. * returns the address of the first occurrence of @c, or %NULL
  679. * if @c is not found
  680. */
  681. void *memchr(const void *s, int c, size_t n)
  682. {
  683. const unsigned char *p = s;
  684. while (n-- != 0) {
  685. if ((unsigned char)c == *p++) {
  686. return (void *)(p - 1);
  687. }
  688. }
  689. return NULL;
  690. }
  691. EXPORT_SYMBOL(memchr);
  692. #endif
  693. static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
  694. {
  695. while (bytes) {
  696. if (*start != value)
  697. return (void *)start;
  698. start++;
  699. bytes--;
  700. }
  701. return NULL;
  702. }
  703. /**
  704. * memchr_inv - Find an unmatching character in an area of memory.
  705. * @start: The memory area
  706. * @c: Find a character other than c
  707. * @bytes: The size of the area.
  708. *
  709. * returns the address of the first character other than @c, or %NULL
  710. * if the whole buffer contains just @c.
  711. */
  712. void *memchr_inv(const void *start, int c, size_t bytes)
  713. {
  714. u8 value = c;
  715. u64 value64;
  716. unsigned int words, prefix;
  717. if (bytes <= 16)
  718. return check_bytes8(start, value, bytes);
  719. value64 = value | value << 8 | value << 16 | value << 24;
  720. value64 = (value64 & 0xffffffff) | value64 << 32;
  721. prefix = 8 - ((unsigned long)start) % 8;
  722. if (prefix) {
  723. u8 *r = check_bytes8(start, value, prefix);
  724. if (r)
  725. return r;
  726. start += prefix;
  727. bytes -= prefix;
  728. }
  729. words = bytes / 8;
  730. while (words) {
  731. if (*(u64 *)start != value64)
  732. return check_bytes8(start, value, 8);
  733. start += 8;
  734. words--;
  735. }
  736. return check_bytes8(start, value, bytes % 8);
  737. }
  738. EXPORT_SYMBOL(memchr_inv);