string.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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_STRCHRNUL
  277. /**
  278. * strchrnul - Find and return a character in a string, or end of string
  279. * @s: The string to be searched
  280. * @c: The character to search for
  281. *
  282. * Returns pointer to first occurrence of 'c' in s. If c is not found, then
  283. * return a pointer to the null byte at the end of s.
  284. */
  285. char *strchrnul(const char *s, int c)
  286. {
  287. while (*s && *s != (char)c)
  288. s++;
  289. return (char *)s;
  290. }
  291. EXPORT_SYMBOL(strchrnul);
  292. #endif
  293. #ifndef __HAVE_ARCH_STRRCHR
  294. /**
  295. * strrchr - Find the last occurrence of a character in a string
  296. * @s: The string to be searched
  297. * @c: The character to search for
  298. */
  299. char *strrchr(const char *s, int c)
  300. {
  301. const char *p = s + strlen(s);
  302. do {
  303. if (*p == (char)c)
  304. return (char *)p;
  305. } while (--p >= s);
  306. return NULL;
  307. }
  308. EXPORT_SYMBOL(strrchr);
  309. #endif
  310. #ifndef __HAVE_ARCH_STRNCHR
  311. /**
  312. * strnchr - Find a character in a length limited string
  313. * @s: The string to be searched
  314. * @count: The number of characters to be searched
  315. * @c: The character to search for
  316. */
  317. char *strnchr(const char *s, size_t count, int c)
  318. {
  319. for (; count-- && *s != '\0'; ++s)
  320. if (*s == (char)c)
  321. return (char *)s;
  322. return NULL;
  323. }
  324. EXPORT_SYMBOL(strnchr);
  325. #endif
  326. /**
  327. * skip_spaces - Removes leading whitespace from @str.
  328. * @str: The string to be stripped.
  329. *
  330. * Returns a pointer to the first non-whitespace character in @str.
  331. */
  332. char *skip_spaces(const char *str)
  333. {
  334. while (isspace(*str))
  335. ++str;
  336. return (char *)str;
  337. }
  338. EXPORT_SYMBOL(skip_spaces);
  339. /**
  340. * strim - Removes leading and trailing whitespace from @s.
  341. * @s: The string to be stripped.
  342. *
  343. * Note that the first trailing whitespace is replaced with a %NUL-terminator
  344. * in the given string @s. Returns a pointer to the first non-whitespace
  345. * character in @s.
  346. */
  347. char *strim(char *s)
  348. {
  349. size_t size;
  350. char *end;
  351. size = strlen(s);
  352. if (!size)
  353. return s;
  354. end = s + size - 1;
  355. while (end >= s && isspace(*end))
  356. end--;
  357. *(end + 1) = '\0';
  358. return skip_spaces(s);
  359. }
  360. EXPORT_SYMBOL(strim);
  361. #ifndef __HAVE_ARCH_STRLEN
  362. /**
  363. * strlen - Find the length of a string
  364. * @s: The string to be sized
  365. */
  366. size_t strlen(const char *s)
  367. {
  368. const char *sc;
  369. for (sc = s; *sc != '\0'; ++sc)
  370. /* nothing */;
  371. return sc - s;
  372. }
  373. EXPORT_SYMBOL(strlen);
  374. #endif
  375. #ifndef __HAVE_ARCH_STRNLEN
  376. /**
  377. * strnlen - Find the length of a length-limited string
  378. * @s: The string to be sized
  379. * @count: The maximum number of bytes to search
  380. */
  381. size_t strnlen(const char *s, size_t count)
  382. {
  383. const char *sc;
  384. for (sc = s; count-- && *sc != '\0'; ++sc)
  385. /* nothing */;
  386. return sc - s;
  387. }
  388. EXPORT_SYMBOL(strnlen);
  389. #endif
  390. #ifndef __HAVE_ARCH_STRSPN
  391. /**
  392. * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
  393. * @s: The string to be searched
  394. * @accept: The string to search for
  395. */
  396. size_t strspn(const char *s, const char *accept)
  397. {
  398. const char *p;
  399. const char *a;
  400. size_t count = 0;
  401. for (p = s; *p != '\0'; ++p) {
  402. for (a = accept; *a != '\0'; ++a) {
  403. if (*p == *a)
  404. break;
  405. }
  406. if (*a == '\0')
  407. return count;
  408. ++count;
  409. }
  410. return count;
  411. }
  412. EXPORT_SYMBOL(strspn);
  413. #endif
  414. #ifndef __HAVE_ARCH_STRCSPN
  415. /**
  416. * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
  417. * @s: The string to be searched
  418. * @reject: The string to avoid
  419. */
  420. size_t strcspn(const char *s, const char *reject)
  421. {
  422. const char *p;
  423. const char *r;
  424. size_t count = 0;
  425. for (p = s; *p != '\0'; ++p) {
  426. for (r = reject; *r != '\0'; ++r) {
  427. if (*p == *r)
  428. return count;
  429. }
  430. ++count;
  431. }
  432. return count;
  433. }
  434. EXPORT_SYMBOL(strcspn);
  435. #endif
  436. #ifndef __HAVE_ARCH_STRPBRK
  437. /**
  438. * strpbrk - Find the first occurrence of a set of characters
  439. * @cs: The string to be searched
  440. * @ct: The characters to search for
  441. */
  442. char *strpbrk(const char *cs, const char *ct)
  443. {
  444. const char *sc1, *sc2;
  445. for (sc1 = cs; *sc1 != '\0'; ++sc1) {
  446. for (sc2 = ct; *sc2 != '\0'; ++sc2) {
  447. if (*sc1 == *sc2)
  448. return (char *)sc1;
  449. }
  450. }
  451. return NULL;
  452. }
  453. EXPORT_SYMBOL(strpbrk);
  454. #endif
  455. #ifndef __HAVE_ARCH_STRSEP
  456. /**
  457. * strsep - Split a string into tokens
  458. * @s: The string to be searched
  459. * @ct: The characters to search for
  460. *
  461. * strsep() updates @s to point after the token, ready for the next call.
  462. *
  463. * It returns empty tokens, too, behaving exactly like the libc function
  464. * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
  465. * Same semantics, slimmer shape. ;)
  466. */
  467. char *strsep(char **s, const char *ct)
  468. {
  469. char *sbegin = *s;
  470. char *end;
  471. if (sbegin == NULL)
  472. return NULL;
  473. end = strpbrk(sbegin, ct);
  474. if (end)
  475. *end++ = '\0';
  476. *s = end;
  477. return sbegin;
  478. }
  479. EXPORT_SYMBOL(strsep);
  480. #endif
  481. /**
  482. * sysfs_streq - return true if strings are equal, modulo trailing newline
  483. * @s1: one string
  484. * @s2: another string
  485. *
  486. * This routine returns true iff two strings are equal, treating both
  487. * NUL and newline-then-NUL as equivalent string terminations. It's
  488. * geared for use with sysfs input strings, which generally terminate
  489. * with newlines but are compared against values without newlines.
  490. */
  491. bool sysfs_streq(const char *s1, const char *s2)
  492. {
  493. while (*s1 && *s1 == *s2) {
  494. s1++;
  495. s2++;
  496. }
  497. if (*s1 == *s2)
  498. return true;
  499. if (!*s1 && *s2 == '\n' && !s2[1])
  500. return true;
  501. if (*s1 == '\n' && !s1[1] && !*s2)
  502. return true;
  503. return false;
  504. }
  505. EXPORT_SYMBOL(sysfs_streq);
  506. /**
  507. * strtobool - convert common user inputs into boolean values
  508. * @s: input string
  509. * @res: result
  510. *
  511. * This routine returns 0 iff the first character is one of 'Yy1Nn0'.
  512. * Otherwise it will return -EINVAL. Value pointed to by res is
  513. * updated upon finding a match.
  514. */
  515. int strtobool(const char *s, bool *res)
  516. {
  517. switch (s[0]) {
  518. case 'y':
  519. case 'Y':
  520. case '1':
  521. *res = true;
  522. break;
  523. case 'n':
  524. case 'N':
  525. case '0':
  526. *res = false;
  527. break;
  528. default:
  529. return -EINVAL;
  530. }
  531. return 0;
  532. }
  533. EXPORT_SYMBOL(strtobool);
  534. #ifndef __HAVE_ARCH_MEMSET
  535. /**
  536. * memset - Fill a region of memory with the given value
  537. * @s: Pointer to the start of the area.
  538. * @c: The byte to fill the area with
  539. * @count: The size of the area.
  540. *
  541. * Do not use memset() to access IO space, use memset_io() instead.
  542. */
  543. void *memset(void *s, int c, size_t count)
  544. {
  545. char *xs = s;
  546. while (count--)
  547. *xs++ = c;
  548. return s;
  549. }
  550. EXPORT_SYMBOL(memset);
  551. #endif
  552. #ifndef __HAVE_ARCH_MEMCPY
  553. /**
  554. * memcpy - 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. * You should not use this function to access IO space, use memcpy_toio()
  560. * or memcpy_fromio() instead.
  561. */
  562. void *memcpy(void *dest, const void *src, size_t count)
  563. {
  564. char *tmp = dest;
  565. const char *s = src;
  566. while (count--)
  567. *tmp++ = *s++;
  568. return dest;
  569. }
  570. EXPORT_SYMBOL(memcpy);
  571. #endif
  572. #ifndef __HAVE_ARCH_MEMMOVE
  573. /**
  574. * memmove - Copy one area of memory to another
  575. * @dest: Where to copy to
  576. * @src: Where to copy from
  577. * @count: The size of the area.
  578. *
  579. * Unlike memcpy(), memmove() copes with overlapping areas.
  580. */
  581. void *memmove(void *dest, const void *src, size_t count)
  582. {
  583. char *tmp;
  584. const char *s;
  585. if (dest <= src) {
  586. tmp = dest;
  587. s = src;
  588. while (count--)
  589. *tmp++ = *s++;
  590. } else {
  591. tmp = dest;
  592. tmp += count;
  593. s = src;
  594. s += count;
  595. while (count--)
  596. *--tmp = *--s;
  597. }
  598. return dest;
  599. }
  600. EXPORT_SYMBOL(memmove);
  601. #endif
  602. #ifndef __HAVE_ARCH_MEMCMP
  603. /**
  604. * memcmp - Compare two areas of memory
  605. * @cs: One area of memory
  606. * @ct: Another area of memory
  607. * @count: The size of the area.
  608. */
  609. #undef memcmp
  610. __visible int memcmp(const void *cs, const void *ct, size_t count)
  611. {
  612. const unsigned char *su1, *su2;
  613. int res = 0;
  614. for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  615. if ((res = *su1 - *su2) != 0)
  616. break;
  617. return res;
  618. }
  619. EXPORT_SYMBOL(memcmp);
  620. #endif
  621. #ifndef __HAVE_ARCH_MEMSCAN
  622. /**
  623. * memscan - Find a character in an area of memory.
  624. * @addr: The memory area
  625. * @c: The byte to search for
  626. * @size: The size of the area.
  627. *
  628. * returns the address of the first occurrence of @c, or 1 byte past
  629. * the area if @c is not found
  630. */
  631. void *memscan(void *addr, int c, size_t size)
  632. {
  633. unsigned char *p = addr;
  634. while (size) {
  635. if (*p == c)
  636. return (void *)p;
  637. p++;
  638. size--;
  639. }
  640. return (void *)p;
  641. }
  642. EXPORT_SYMBOL(memscan);
  643. #endif
  644. #ifndef __HAVE_ARCH_STRSTR
  645. /**
  646. * strstr - Find the first substring in a %NUL terminated string
  647. * @s1: The string to be searched
  648. * @s2: The string to search for
  649. */
  650. char *strstr(const char *s1, const char *s2)
  651. {
  652. size_t l1, l2;
  653. l2 = strlen(s2);
  654. if (!l2)
  655. return (char *)s1;
  656. l1 = strlen(s1);
  657. while (l1 >= l2) {
  658. l1--;
  659. if (!memcmp(s1, s2, l2))
  660. return (char *)s1;
  661. s1++;
  662. }
  663. return NULL;
  664. }
  665. EXPORT_SYMBOL(strstr);
  666. #endif
  667. #ifndef __HAVE_ARCH_STRNSTR
  668. /**
  669. * strnstr - Find the first substring in a length-limited string
  670. * @s1: The string to be searched
  671. * @s2: The string to search for
  672. * @len: the maximum number of characters to search
  673. */
  674. char *strnstr(const char *s1, const char *s2, size_t len)
  675. {
  676. size_t l2;
  677. l2 = strlen(s2);
  678. if (!l2)
  679. return (char *)s1;
  680. while (len >= l2) {
  681. len--;
  682. if (!memcmp(s1, s2, l2))
  683. return (char *)s1;
  684. s1++;
  685. }
  686. return NULL;
  687. }
  688. EXPORT_SYMBOL(strnstr);
  689. #endif
  690. #ifndef __HAVE_ARCH_MEMCHR
  691. /**
  692. * memchr - Find a character in an area of memory.
  693. * @s: The memory area
  694. * @c: The byte to search for
  695. * @n: The size of the area.
  696. *
  697. * returns the address of the first occurrence of @c, or %NULL
  698. * if @c is not found
  699. */
  700. void *memchr(const void *s, int c, size_t n)
  701. {
  702. const unsigned char *p = s;
  703. while (n-- != 0) {
  704. if ((unsigned char)c == *p++) {
  705. return (void *)(p - 1);
  706. }
  707. }
  708. return NULL;
  709. }
  710. EXPORT_SYMBOL(memchr);
  711. #endif
  712. static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
  713. {
  714. while (bytes) {
  715. if (*start != value)
  716. return (void *)start;
  717. start++;
  718. bytes--;
  719. }
  720. return NULL;
  721. }
  722. /**
  723. * memchr_inv - Find an unmatching character in an area of memory.
  724. * @start: The memory area
  725. * @c: Find a character other than c
  726. * @bytes: The size of the area.
  727. *
  728. * returns the address of the first character other than @c, or %NULL
  729. * if the whole buffer contains just @c.
  730. */
  731. void *memchr_inv(const void *start, int c, size_t bytes)
  732. {
  733. u8 value = c;
  734. u64 value64;
  735. unsigned int words, prefix;
  736. if (bytes <= 16)
  737. return check_bytes8(start, value, bytes);
  738. value64 = value;
  739. #if defined(ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
  740. value64 *= 0x0101010101010101;
  741. #elif defined(ARCH_HAS_FAST_MULTIPLIER)
  742. value64 *= 0x01010101;
  743. value64 |= value64 << 32;
  744. #else
  745. value64 |= value64 << 8;
  746. value64 |= value64 << 16;
  747. value64 |= value64 << 32;
  748. #endif
  749. prefix = (unsigned long)start % 8;
  750. if (prefix) {
  751. u8 *r;
  752. prefix = 8 - prefix;
  753. r = check_bytes8(start, value, prefix);
  754. if (r)
  755. return r;
  756. start += prefix;
  757. bytes -= prefix;
  758. }
  759. words = bytes / 8;
  760. while (words) {
  761. if (*(u64 *)start != value64)
  762. return check_bytes8(start, value, 8);
  763. start += 8;
  764. words--;
  765. }
  766. return check_bytes8(start, value, bytes % 8);
  767. }
  768. EXPORT_SYMBOL(memchr_inv);