PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/string.c

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