string.c 17 KB

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