string.c 20 KB

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