string.c 16 KB

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