string.c 17 KB

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