string.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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. * strtobool - convert common user inputs into boolean values
  575. * @s: input string
  576. * @res: result
  577. *
  578. * This routine returns 0 iff the first character is one of 'Yy1Nn0'.
  579. * Otherwise it will return -EINVAL. Value pointed to by res is
  580. * updated upon finding a match.
  581. */
  582. int strtobool(const char *s, bool *res)
  583. {
  584. switch (s[0]) {
  585. case 'y':
  586. case 'Y':
  587. case '1':
  588. *res = true;
  589. break;
  590. case 'n':
  591. case 'N':
  592. case '0':
  593. *res = false;
  594. break;
  595. default:
  596. return -EINVAL;
  597. }
  598. return 0;
  599. }
  600. EXPORT_SYMBOL(strtobool);
  601. #ifndef __HAVE_ARCH_MEMSET
  602. /**
  603. * memset - Fill a region of memory with the given value
  604. * @s: Pointer to the start of the area.
  605. * @c: The byte to fill the area with
  606. * @count: The size of the area.
  607. *
  608. * Do not use memset() to access IO space, use memset_io() instead.
  609. */
  610. void *memset(void *s, int c, size_t count)
  611. {
  612. char *xs = s;
  613. while (count--)
  614. *xs++ = c;
  615. return s;
  616. }
  617. EXPORT_SYMBOL(memset);
  618. #endif
  619. /**
  620. * memzero_explicit - Fill a region of memory (e.g. sensitive
  621. * keying data) with 0s.
  622. * @s: Pointer to the start of the area.
  623. * @count: The size of the area.
  624. *
  625. * Note: usually using memset() is just fine (!), but in cases
  626. * where clearing out _local_ data at the end of a scope is
  627. * necessary, memzero_explicit() should be used instead in
  628. * order to prevent the compiler from optimising away zeroing.
  629. *
  630. * memzero_explicit() doesn't need an arch-specific version as
  631. * it just invokes the one of memset() implicitly.
  632. */
  633. void memzero_explicit(void *s, size_t count)
  634. {
  635. memset(s, 0, count);
  636. barrier_data(s);
  637. }
  638. EXPORT_SYMBOL(memzero_explicit);
  639. #ifndef __HAVE_ARCH_MEMCPY
  640. /**
  641. * memcpy - Copy one area of memory to another
  642. * @dest: Where to copy to
  643. * @src: Where to copy from
  644. * @count: The size of the area.
  645. *
  646. * You should not use this function to access IO space, use memcpy_toio()
  647. * or memcpy_fromio() instead.
  648. */
  649. void *memcpy(void *dest, const void *src, size_t count)
  650. {
  651. char *tmp = dest;
  652. const char *s = src;
  653. while (count--)
  654. *tmp++ = *s++;
  655. return dest;
  656. }
  657. EXPORT_SYMBOL(memcpy);
  658. #endif
  659. #ifndef __HAVE_ARCH_MEMMOVE
  660. /**
  661. * memmove - Copy one area of memory to another
  662. * @dest: Where to copy to
  663. * @src: Where to copy from
  664. * @count: The size of the area.
  665. *
  666. * Unlike memcpy(), memmove() copes with overlapping areas.
  667. */
  668. void *memmove(void *dest, const void *src, size_t count)
  669. {
  670. char *tmp;
  671. const char *s;
  672. if (dest <= src) {
  673. tmp = dest;
  674. s = src;
  675. while (count--)
  676. *tmp++ = *s++;
  677. } else {
  678. tmp = dest;
  679. tmp += count;
  680. s = src;
  681. s += count;
  682. while (count--)
  683. *--tmp = *--s;
  684. }
  685. return dest;
  686. }
  687. EXPORT_SYMBOL(memmove);
  688. #endif
  689. #ifndef __HAVE_ARCH_MEMCMP
  690. /**
  691. * memcmp - Compare two areas of memory
  692. * @cs: One area of memory
  693. * @ct: Another area of memory
  694. * @count: The size of the area.
  695. */
  696. #undef memcmp
  697. __visible int memcmp(const void *cs, const void *ct, size_t count)
  698. {
  699. const unsigned char *su1, *su2;
  700. int res = 0;
  701. for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  702. if ((res = *su1 - *su2) != 0)
  703. break;
  704. return res;
  705. }
  706. EXPORT_SYMBOL(memcmp);
  707. #endif
  708. #ifndef __HAVE_ARCH_MEMSCAN
  709. /**
  710. * memscan - Find a character in an area of memory.
  711. * @addr: The memory area
  712. * @c: The byte to search for
  713. * @size: The size of the area.
  714. *
  715. * returns the address of the first occurrence of @c, or 1 byte past
  716. * the area if @c is not found
  717. */
  718. void *memscan(void *addr, int c, size_t size)
  719. {
  720. unsigned char *p = addr;
  721. while (size) {
  722. if (*p == c)
  723. return (void *)p;
  724. p++;
  725. size--;
  726. }
  727. return (void *)p;
  728. }
  729. EXPORT_SYMBOL(memscan);
  730. #endif
  731. #ifndef __HAVE_ARCH_STRSTR
  732. /**
  733. * strstr - Find the first substring in a %NUL terminated string
  734. * @s1: The string to be searched
  735. * @s2: The string to search for
  736. */
  737. char *strstr(const char *s1, const char *s2)
  738. {
  739. size_t l1, l2;
  740. l2 = strlen(s2);
  741. if (!l2)
  742. return (char *)s1;
  743. l1 = strlen(s1);
  744. while (l1 >= l2) {
  745. l1--;
  746. if (!memcmp(s1, s2, l2))
  747. return (char *)s1;
  748. s1++;
  749. }
  750. return NULL;
  751. }
  752. EXPORT_SYMBOL(strstr);
  753. #endif
  754. #ifndef __HAVE_ARCH_STRNSTR
  755. /**
  756. * strnstr - Find the first substring in a length-limited string
  757. * @s1: The string to be searched
  758. * @s2: The string to search for
  759. * @len: the maximum number of characters to search
  760. */
  761. char *strnstr(const char *s1, const char *s2, size_t len)
  762. {
  763. size_t l2;
  764. l2 = strlen(s2);
  765. if (!l2)
  766. return (char *)s1;
  767. while (len >= l2) {
  768. len--;
  769. if (!memcmp(s1, s2, l2))
  770. return (char *)s1;
  771. s1++;
  772. }
  773. return NULL;
  774. }
  775. EXPORT_SYMBOL(strnstr);
  776. #endif
  777. #ifndef __HAVE_ARCH_MEMCHR
  778. /**
  779. * memchr - Find a character in an area of memory.
  780. * @s: The memory area
  781. * @c: The byte to search for
  782. * @n: The size of the area.
  783. *
  784. * returns the address of the first occurrence of @c, or %NULL
  785. * if @c is not found
  786. */
  787. void *memchr(const void *s, int c, size_t n)
  788. {
  789. const unsigned char *p = s;
  790. while (n-- != 0) {
  791. if ((unsigned char)c == *p++) {
  792. return (void *)(p - 1);
  793. }
  794. }
  795. return NULL;
  796. }
  797. EXPORT_SYMBOL(memchr);
  798. #endif
  799. static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
  800. {
  801. while (bytes) {
  802. if (*start != value)
  803. return (void *)start;
  804. start++;
  805. bytes--;
  806. }
  807. return NULL;
  808. }
  809. /**
  810. * memchr_inv - Find an unmatching character in an area of memory.
  811. * @start: The memory area
  812. * @c: Find a character other than c
  813. * @bytes: The size of the area.
  814. *
  815. * returns the address of the first character other than @c, or %NULL
  816. * if the whole buffer contains just @c.
  817. */
  818. void *memchr_inv(const void *start, int c, size_t bytes)
  819. {
  820. u8 value = c;
  821. u64 value64;
  822. unsigned int words, prefix;
  823. if (bytes <= 16)
  824. return check_bytes8(start, value, bytes);
  825. value64 = value;
  826. #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
  827. value64 *= 0x0101010101010101;
  828. #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
  829. value64 *= 0x01010101;
  830. value64 |= value64 << 32;
  831. #else
  832. value64 |= value64 << 8;
  833. value64 |= value64 << 16;
  834. value64 |= value64 << 32;
  835. #endif
  836. prefix = (unsigned long)start % 8;
  837. if (prefix) {
  838. u8 *r;
  839. prefix = 8 - prefix;
  840. r = check_bytes8(start, value, prefix);
  841. if (r)
  842. return r;
  843. start += prefix;
  844. bytes -= prefix;
  845. }
  846. words = bytes / 8;
  847. while (words) {
  848. if (*(u64 *)start != value64)
  849. return check_bytes8(start, value, 8);
  850. start += 8;
  851. words--;
  852. }
  853. return check_bytes8(start, value, bytes % 8);
  854. }
  855. EXPORT_SYMBOL(memchr_inv);
  856. /**
  857. * strreplace - Replace all occurrences of character in string.
  858. * @s: The string to operate on.
  859. * @old: The character being replaced.
  860. * @new: The character @old is replaced with.
  861. *
  862. * Returns pointer to the nul byte at the end of @s.
  863. */
  864. char *strreplace(char *s, char old, char new)
  865. {
  866. for (; *s; ++s)
  867. if (*s == old)
  868. *s = new;
  869. return s;
  870. }
  871. EXPORT_SYMBOL(strreplace);