string.c 16 KB

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