string.c 17 KB

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