string.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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. /**
  536. * memzero_explicit - Fill a region of memory (e.g. sensitive
  537. * keying data) with 0s.
  538. * @s: Pointer to the start of the area.
  539. * @count: The size of the area.
  540. *
  541. * memzero_explicit() doesn't need an arch-specific version as
  542. * it just invokes the one of memset() implicitly.
  543. */
  544. void memzero_explicit(void *s, size_t count)
  545. {
  546. memset(s, 0, count);
  547. OPTIMIZER_HIDE_VAR(s);
  548. }
  549. EXPORT_SYMBOL(memzero_explicit);
  550. #ifndef __HAVE_ARCH_MEMCPY
  551. /**
  552. * memcpy - Copy one area of memory to another
  553. * @dest: Where to copy to
  554. * @src: Where to copy from
  555. * @count: The size of the area.
  556. *
  557. * You should not use this function to access IO space, use memcpy_toio()
  558. * or memcpy_fromio() instead.
  559. */
  560. void *memcpy(void *dest, const void *src, size_t count)
  561. {
  562. char *tmp = dest;
  563. const char *s = src;
  564. while (count--)
  565. *tmp++ = *s++;
  566. return dest;
  567. }
  568. EXPORT_SYMBOL(memcpy);
  569. #endif
  570. #ifndef __HAVE_ARCH_MEMMOVE
  571. /**
  572. * memmove - Copy one area of memory to another
  573. * @dest: Where to copy to
  574. * @src: Where to copy from
  575. * @count: The size of the area.
  576. *
  577. * Unlike memcpy(), memmove() copes with overlapping areas.
  578. */
  579. void *memmove(void *dest, const void *src, size_t count)
  580. {
  581. char *tmp;
  582. const char *s;
  583. if (dest <= src) {
  584. tmp = dest;
  585. s = src;
  586. while (count--)
  587. *tmp++ = *s++;
  588. } else {
  589. tmp = dest;
  590. tmp += count;
  591. s = src;
  592. s += count;
  593. while (count--)
  594. *--tmp = *--s;
  595. }
  596. return dest;
  597. }
  598. EXPORT_SYMBOL(memmove);
  599. #endif
  600. #ifndef __HAVE_ARCH_MEMCMP
  601. /**
  602. * memcmp - Compare two areas of memory
  603. * @cs: One area of memory
  604. * @ct: Another area of memory
  605. * @count: The size of the area.
  606. */
  607. #undef memcmp
  608. int memcmp(const void *cs, const void *ct, size_t count)
  609. {
  610. const unsigned char *su1, *su2;
  611. int res = 0;
  612. for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  613. if ((res = *su1 - *su2) != 0)
  614. break;
  615. return res;
  616. }
  617. EXPORT_SYMBOL(memcmp);
  618. #endif
  619. #ifndef __HAVE_ARCH_MEMSCAN
  620. /**
  621. * memscan - Find a character in an area of memory.
  622. * @addr: The memory area
  623. * @c: The byte to search for
  624. * @size: The size of the area.
  625. *
  626. * returns the address of the first occurrence of @c, or 1 byte past
  627. * the area if @c is not found
  628. */
  629. void *memscan(void *addr, int c, size_t size)
  630. {
  631. unsigned char *p = addr;
  632. while (size) {
  633. if (*p == c)
  634. return (void *)p;
  635. p++;
  636. size--;
  637. }
  638. return (void *)p;
  639. }
  640. EXPORT_SYMBOL(memscan);
  641. #endif
  642. #ifndef __HAVE_ARCH_STRSTR
  643. /**
  644. * strstr - Find the first substring in a %NUL terminated string
  645. * @s1: The string to be searched
  646. * @s2: The string to search for
  647. */
  648. char *strstr(const char *s1, const char *s2)
  649. {
  650. size_t l1, l2;
  651. l2 = strlen(s2);
  652. if (!l2)
  653. return (char *)s1;
  654. l1 = strlen(s1);
  655. while (l1 >= l2) {
  656. l1--;
  657. if (!memcmp(s1, s2, l2))
  658. return (char *)s1;
  659. s1++;
  660. }
  661. return NULL;
  662. }
  663. EXPORT_SYMBOL(strstr);
  664. #endif
  665. #ifndef __HAVE_ARCH_STRNSTR
  666. /**
  667. * strnstr - Find the first substring in a length-limited string
  668. * @s1: The string to be searched
  669. * @s2: The string to search for
  670. * @len: the maximum number of characters to search
  671. */
  672. char *strnstr(const char *s1, const char *s2, size_t len)
  673. {
  674. size_t l2;
  675. l2 = strlen(s2);
  676. if (!l2)
  677. return (char *)s1;
  678. while (len >= l2) {
  679. len--;
  680. if (!memcmp(s1, s2, l2))
  681. return (char *)s1;
  682. s1++;
  683. }
  684. return NULL;
  685. }
  686. EXPORT_SYMBOL(strnstr);
  687. #endif
  688. #ifndef __HAVE_ARCH_MEMCHR
  689. /**
  690. * memchr - Find a character in an area of memory.
  691. * @s: The memory area
  692. * @c: The byte to search for
  693. * @n: The size of the area.
  694. *
  695. * returns the address of the first occurrence of @c, or %NULL
  696. * if @c is not found
  697. */
  698. void *memchr(const void *s, int c, size_t n)
  699. {
  700. const unsigned char *p = s;
  701. while (n-- != 0) {
  702. if ((unsigned char)c == *p++) {
  703. return (void *)(p - 1);
  704. }
  705. }
  706. return NULL;
  707. }
  708. EXPORT_SYMBOL(memchr);
  709. #endif
  710. static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
  711. {
  712. while (bytes) {
  713. if (*start != value)
  714. return (void *)start;
  715. start++;
  716. bytes--;
  717. }
  718. return NULL;
  719. }
  720. /**
  721. * memchr_inv - Find an unmatching character in an area of memory.
  722. * @start: The memory area
  723. * @c: Find a character other than c
  724. * @bytes: The size of the area.
  725. *
  726. * returns the address of the first character other than @c, or %NULL
  727. * if the whole buffer contains just @c.
  728. */
  729. void *memchr_inv(const void *start, int c, size_t bytes)
  730. {
  731. u8 value = c;
  732. u64 value64;
  733. unsigned int words, prefix;
  734. if (bytes <= 16)
  735. return check_bytes8(start, value, bytes);
  736. value64 = value;
  737. #if defined(ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
  738. value64 *= 0x0101010101010101;
  739. #elif defined(ARCH_HAS_FAST_MULTIPLIER)
  740. value64 *= 0x01010101;
  741. value64 |= value64 << 32;
  742. #else
  743. value64 |= value64 << 8;
  744. value64 |= value64 << 16;
  745. value64 |= value64 << 32;
  746. #endif
  747. prefix = (unsigned long)start % 8;
  748. if (prefix) {
  749. u8 *r;
  750. prefix = 8 - prefix;
  751. r = check_bytes8(start, value, prefix);
  752. if (r)
  753. return r;
  754. start += prefix;
  755. bytes -= prefix;
  756. }
  757. words = bytes / 8;
  758. while (words) {
  759. if (*(u64 *)start != value64)
  760. return check_bytes8(start, value, 8);
  761. start += 8;
  762. words--;
  763. }
  764. return check_bytes8(start, value, bytes % 8);
  765. }
  766. EXPORT_SYMBOL(memchr_inv);