string.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/lib/string.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. */
  7. /*
  8. * This file should be used only for "library" routines that may have
  9. * alternative implementations on specific architectures (generally
  10. * found in <asm-xx/string.h>), or get overloaded by FORTIFY_SOURCE.
  11. * (Specifically, this file is built with __NO_FORTIFY.)
  12. *
  13. * Other helper functions should live in string_helpers.c.
  14. */
  15. #define __NO_FORTIFY
  16. #include <linux/types.h>
  17. #include <linux/string.h>
  18. #include <linux/ctype.h>
  19. #include <linux/kernel.h>
  20. #include <linux/export.h>
  21. #include <linux/bug.h>
  22. #include <linux/errno.h>
  23. #include <linux/slab.h>
  24. #include <asm/unaligned.h>
  25. #include <asm/byteorder.h>
  26. #include <asm/word-at-a-time.h>
  27. #include <asm/page.h>
  28. #ifndef __HAVE_ARCH_STRNCASECMP
  29. /**
  30. * strncasecmp - Case insensitive, length-limited string comparison
  31. * @s1: One string
  32. * @s2: The other string
  33. * @len: the maximum number of characters to compare
  34. */
  35. int strncasecmp(const char *s1, const char *s2, size_t len)
  36. {
  37. /* Yes, Virginia, it had better be unsigned */
  38. unsigned char c1, c2;
  39. if (!len)
  40. return 0;
  41. do {
  42. c1 = *s1++;
  43. c2 = *s2++;
  44. if (!c1 || !c2)
  45. break;
  46. if (c1 == c2)
  47. continue;
  48. c1 = tolower(c1);
  49. c2 = tolower(c2);
  50. if (c1 != c2)
  51. break;
  52. } while (--len);
  53. return (int)c1 - (int)c2;
  54. }
  55. EXPORT_SYMBOL(strncasecmp);
  56. #endif
  57. #ifndef __HAVE_ARCH_STRCASECMP
  58. int strcasecmp(const char *s1, const char *s2)
  59. {
  60. int c1, c2;
  61. do {
  62. c1 = tolower(*s1++);
  63. c2 = tolower(*s2++);
  64. } while (c1 == c2 && c1 != 0);
  65. return c1 - c2;
  66. }
  67. EXPORT_SYMBOL(strcasecmp);
  68. #endif
  69. #ifndef __HAVE_ARCH_STRCPY
  70. /**
  71. * strcpy - Copy a %NUL terminated string
  72. * @dest: Where to copy the string to
  73. * @src: Where to copy the string from
  74. */
  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_STRSCPY
  136. /**
  137. * strscpy - Copy a C-string into a sized buffer
  138. * @dest: Where to copy the string to
  139. * @src: Where to copy the string from
  140. * @count: Size of destination buffer
  141. *
  142. * Copy the string, or as much of it as fits, into the dest buffer. The
  143. * behavior is undefined if the string buffers overlap. The destination
  144. * buffer is always NUL terminated, unless it's zero-sized.
  145. *
  146. * Preferred to strlcpy() since the API doesn't require reading memory
  147. * from the src string beyond the specified "count" bytes, and since
  148. * the return value is easier to error-check than strlcpy()'s.
  149. * In addition, the implementation is robust to the string changing out
  150. * from underneath it, unlike the current strlcpy() implementation.
  151. *
  152. * Preferred to strncpy() since it always returns a valid string, and
  153. * doesn't unnecessarily force the tail of the destination buffer to be
  154. * zeroed. If zeroing is desired please use strscpy_pad().
  155. *
  156. * Returns:
  157. * * The number of characters copied (not including the trailing %NUL)
  158. * * -E2BIG if count is 0 or @src was truncated.
  159. */
  160. ssize_t strscpy(char *dest, const char *src, size_t count)
  161. {
  162. const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
  163. size_t max = count;
  164. long res = 0;
  165. if (count == 0 || WARN_ON_ONCE(count > INT_MAX))
  166. return -E2BIG;
  167. #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  168. /*
  169. * If src is unaligned, don't cross a page boundary,
  170. * since we don't know if the next page is mapped.
  171. */
  172. if ((long)src & (sizeof(long) - 1)) {
  173. size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
  174. if (limit < max)
  175. max = limit;
  176. }
  177. #else
  178. /* If src or dest is unaligned, don't do word-at-a-time. */
  179. if (((long) dest | (long) src) & (sizeof(long) - 1))
  180. max = 0;
  181. #endif
  182. /*
  183. * read_word_at_a_time() below may read uninitialized bytes after the
  184. * trailing zero and use them in comparisons. Disable this optimization
  185. * under KMSAN to prevent false positive reports.
  186. */
  187. if (IS_ENABLED(CONFIG_KMSAN))
  188. max = 0;
  189. while (max >= sizeof(unsigned long)) {
  190. unsigned long c, data;
  191. c = read_word_at_a_time(src+res);
  192. if (has_zero(c, &data, &constants)) {
  193. data = prep_zero_mask(c, data, &constants);
  194. data = create_zero_mask(data);
  195. *(unsigned long *)(dest+res) = c & zero_bytemask(data);
  196. return res + find_zero(data);
  197. }
  198. *(unsigned long *)(dest+res) = c;
  199. res += sizeof(unsigned long);
  200. count -= sizeof(unsigned long);
  201. max -= sizeof(unsigned long);
  202. }
  203. while (count) {
  204. char c;
  205. c = src[res];
  206. dest[res] = c;
  207. if (!c)
  208. return res;
  209. res++;
  210. count--;
  211. }
  212. /* Hit buffer length without finding a NUL; force NUL-termination. */
  213. if (res)
  214. dest[res-1] = '\0';
  215. return -E2BIG;
  216. }
  217. EXPORT_SYMBOL(strscpy);
  218. #endif
  219. /**
  220. * stpcpy - copy a string from src to dest returning a pointer to the new end
  221. * of dest, including src's %NUL-terminator. May overrun dest.
  222. * @dest: pointer to end of string being copied into. Must be large enough
  223. * to receive copy.
  224. * @src: pointer to the beginning of string being copied from. Must not overlap
  225. * dest.
  226. *
  227. * stpcpy differs from strcpy in a key way: the return value is a pointer
  228. * to the new %NUL-terminating character in @dest. (For strcpy, the return
  229. * value is a pointer to the start of @dest). This interface is considered
  230. * unsafe as it doesn't perform bounds checking of the inputs. As such it's
  231. * not recommended for usage. Instead, its definition is provided in case
  232. * the compiler lowers other libcalls to stpcpy.
  233. */
  234. char *stpcpy(char *__restrict__ dest, const char *__restrict__ src);
  235. char *stpcpy(char *__restrict__ dest, const char *__restrict__ src)
  236. {
  237. while ((*dest++ = *src++) != '\0')
  238. /* nothing */;
  239. return --dest;
  240. }
  241. EXPORT_SYMBOL(stpcpy);
  242. #ifndef __HAVE_ARCH_STRCAT
  243. /**
  244. * strcat - Append one %NUL-terminated string to another
  245. * @dest: The string to be appended to
  246. * @src: The string to append to it
  247. */
  248. char *strcat(char *dest, const char *src)
  249. {
  250. char *tmp = dest;
  251. while (*dest)
  252. dest++;
  253. while ((*dest++ = *src++) != '\0')
  254. ;
  255. return tmp;
  256. }
  257. EXPORT_SYMBOL(strcat);
  258. #endif
  259. #ifndef __HAVE_ARCH_STRNCAT
  260. /**
  261. * strncat - 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 maximum numbers of bytes to copy
  265. *
  266. * Note that in contrast to strncpy(), strncat() ensures the result is
  267. * terminated.
  268. */
  269. char *strncat(char *dest, const char *src, size_t count)
  270. {
  271. char *tmp = dest;
  272. if (count) {
  273. while (*dest)
  274. dest++;
  275. while ((*dest++ = *src++) != 0) {
  276. if (--count == 0) {
  277. *dest = '\0';
  278. break;
  279. }
  280. }
  281. }
  282. return tmp;
  283. }
  284. EXPORT_SYMBOL(strncat);
  285. #endif
  286. #ifndef __HAVE_ARCH_STRLCAT
  287. /**
  288. * strlcat - Append a length-limited, C-string to another
  289. * @dest: The string to be appended to
  290. * @src: The string to append to it
  291. * @count: The size of the destination buffer.
  292. */
  293. size_t strlcat(char *dest, const char *src, size_t count)
  294. {
  295. size_t dsize = strlen(dest);
  296. size_t len = strlen(src);
  297. size_t res = dsize + len;
  298. /* This would be a bug */
  299. BUG_ON(dsize >= count);
  300. dest += dsize;
  301. count -= dsize;
  302. if (len >= count)
  303. len = count-1;
  304. memcpy(dest, src, len);
  305. dest[len] = 0;
  306. return res;
  307. }
  308. EXPORT_SYMBOL(strlcat);
  309. #endif
  310. #ifndef __HAVE_ARCH_STRCMP
  311. /**
  312. * strcmp - Compare two strings
  313. * @cs: One string
  314. * @ct: Another string
  315. */
  316. int strcmp(const char *cs, const char *ct)
  317. {
  318. unsigned char c1, c2;
  319. while (1) {
  320. c1 = *cs++;
  321. c2 = *ct++;
  322. if (c1 != c2)
  323. return c1 < c2 ? -1 : 1;
  324. if (!c1)
  325. break;
  326. }
  327. return 0;
  328. }
  329. EXPORT_SYMBOL(strcmp);
  330. #endif
  331. #ifndef __HAVE_ARCH_STRNCMP
  332. /**
  333. * strncmp - Compare two length-limited strings
  334. * @cs: One string
  335. * @ct: Another string
  336. * @count: The maximum number of bytes to compare
  337. */
  338. int strncmp(const char *cs, const char *ct, size_t count)
  339. {
  340. unsigned char c1, c2;
  341. while (count) {
  342. c1 = *cs++;
  343. c2 = *ct++;
  344. if (c1 != c2)
  345. return c1 < c2 ? -1 : 1;
  346. if (!c1)
  347. break;
  348. count--;
  349. }
  350. return 0;
  351. }
  352. EXPORT_SYMBOL(strncmp);
  353. #endif
  354. #ifndef __HAVE_ARCH_STRCHR
  355. /**
  356. * strchr - Find the first occurrence of a character in a string
  357. * @s: The string to be searched
  358. * @c: The character to search for
  359. *
  360. * Note that the %NUL-terminator is considered part of the string, and can
  361. * be searched for.
  362. */
  363. char *strchr(const char *s, int c)
  364. {
  365. for (; *s != (char)c; ++s)
  366. if (*s == '\0')
  367. return NULL;
  368. return (char *)s;
  369. }
  370. EXPORT_SYMBOL(strchr);
  371. #endif
  372. #ifndef __HAVE_ARCH_STRCHRNUL
  373. /**
  374. * strchrnul - Find and return a character in a string, or end of string
  375. * @s: The string to be searched
  376. * @c: The character to search for
  377. *
  378. * Returns pointer to first occurrence of 'c' in s. If c is not found, then
  379. * return a pointer to the null byte at the end of s.
  380. */
  381. char *strchrnul(const char *s, int c)
  382. {
  383. while (*s && *s != (char)c)
  384. s++;
  385. return (char *)s;
  386. }
  387. EXPORT_SYMBOL(strchrnul);
  388. #endif
  389. /**
  390. * strnchrnul - Find and return a character in a length limited string,
  391. * or end of string
  392. * @s: The string to be searched
  393. * @count: The number of characters to be searched
  394. * @c: The character to search for
  395. *
  396. * Returns pointer to the first occurrence of 'c' in s. If c is not found,
  397. * then return a pointer to the last character of the string.
  398. */
  399. char *strnchrnul(const char *s, size_t count, int c)
  400. {
  401. while (count-- && *s && *s != (char)c)
  402. s++;
  403. return (char *)s;
  404. }
  405. #ifndef __HAVE_ARCH_STRRCHR
  406. /**
  407. * strrchr - Find the last occurrence of a character in a string
  408. * @s: The string to be searched
  409. * @c: The character to search for
  410. */
  411. char *strrchr(const char *s, int c)
  412. {
  413. const char *last = NULL;
  414. do {
  415. if (*s == (char)c)
  416. last = s;
  417. } while (*s++);
  418. return (char *)last;
  419. }
  420. EXPORT_SYMBOL(strrchr);
  421. #endif
  422. #ifndef __HAVE_ARCH_STRNCHR
  423. /**
  424. * strnchr - Find a character in a length limited string
  425. * @s: The string to be searched
  426. * @count: The number of characters to be searched
  427. * @c: The character to search for
  428. *
  429. * Note that the %NUL-terminator is considered part of the string, and can
  430. * be searched for.
  431. */
  432. char *strnchr(const char *s, size_t count, int c)
  433. {
  434. while (count--) {
  435. if (*s == (char)c)
  436. return (char *)s;
  437. if (*s++ == '\0')
  438. break;
  439. }
  440. return NULL;
  441. }
  442. EXPORT_SYMBOL(strnchr);
  443. #endif
  444. #ifndef __HAVE_ARCH_STRLEN
  445. /**
  446. * strlen - Find the length of a string
  447. * @s: The string to be sized
  448. */
  449. size_t strlen(const char *s)
  450. {
  451. const char *sc;
  452. for (sc = s; *sc != '\0'; ++sc)
  453. /* nothing */;
  454. return sc - s;
  455. }
  456. EXPORT_SYMBOL(strlen);
  457. #endif
  458. #ifndef __HAVE_ARCH_STRNLEN
  459. /**
  460. * strnlen - Find the length of a length-limited string
  461. * @s: The string to be sized
  462. * @count: The maximum number of bytes to search
  463. */
  464. size_t strnlen(const char *s, size_t count)
  465. {
  466. const char *sc;
  467. for (sc = s; count-- && *sc != '\0'; ++sc)
  468. /* nothing */;
  469. return sc - s;
  470. }
  471. EXPORT_SYMBOL(strnlen);
  472. #endif
  473. #ifndef __HAVE_ARCH_STRSPN
  474. /**
  475. * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
  476. * @s: The string to be searched
  477. * @accept: The string to search for
  478. */
  479. size_t strspn(const char *s, const char *accept)
  480. {
  481. const char *p;
  482. for (p = s; *p != '\0'; ++p) {
  483. if (!strchr(accept, *p))
  484. break;
  485. }
  486. return p - s;
  487. }
  488. EXPORT_SYMBOL(strspn);
  489. #endif
  490. #ifndef __HAVE_ARCH_STRCSPN
  491. /**
  492. * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
  493. * @s: The string to be searched
  494. * @reject: The string to avoid
  495. */
  496. size_t strcspn(const char *s, const char *reject)
  497. {
  498. const char *p;
  499. for (p = s; *p != '\0'; ++p) {
  500. if (strchr(reject, *p))
  501. break;
  502. }
  503. return p - s;
  504. }
  505. EXPORT_SYMBOL(strcspn);
  506. #endif
  507. #ifndef __HAVE_ARCH_STRPBRK
  508. /**
  509. * strpbrk - Find the first occurrence of a set of characters
  510. * @cs: The string to be searched
  511. * @ct: The characters to search for
  512. */
  513. char *strpbrk(const char *cs, const char *ct)
  514. {
  515. const char *sc1, *sc2;
  516. for (sc1 = cs; *sc1 != '\0'; ++sc1) {
  517. for (sc2 = ct; *sc2 != '\0'; ++sc2) {
  518. if (*sc1 == *sc2)
  519. return (char *)sc1;
  520. }
  521. }
  522. return NULL;
  523. }
  524. EXPORT_SYMBOL(strpbrk);
  525. #endif
  526. #ifndef __HAVE_ARCH_STRSEP
  527. /**
  528. * strsep - Split a string into tokens
  529. * @s: The string to be searched
  530. * @ct: The characters to search for
  531. *
  532. * strsep() updates @s to point after the token, ready for the next call.
  533. *
  534. * It returns empty tokens, too, behaving exactly like the libc function
  535. * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
  536. * Same semantics, slimmer shape. ;)
  537. */
  538. char *strsep(char **s, const char *ct)
  539. {
  540. char *sbegin = *s;
  541. char *end;
  542. if (sbegin == NULL)
  543. return NULL;
  544. end = strpbrk(sbegin, ct);
  545. if (end)
  546. *end++ = '\0';
  547. *s = end;
  548. return sbegin;
  549. }
  550. EXPORT_SYMBOL(strsep);
  551. #endif
  552. #ifndef __HAVE_ARCH_MEMSET
  553. /**
  554. * memset - Fill a region of memory with the given value
  555. * @s: Pointer to the start of the area.
  556. * @c: The byte to fill the area with
  557. * @count: The size of the area.
  558. *
  559. * Do not use memset() to access IO space, use memset_io() instead.
  560. */
  561. void *memset(void *s, int c, size_t count)
  562. {
  563. char *xs = s;
  564. while (count--)
  565. *xs++ = c;
  566. return s;
  567. }
  568. EXPORT_SYMBOL(memset);
  569. #endif
  570. #ifndef __HAVE_ARCH_MEMSET16
  571. /**
  572. * memset16() - Fill a memory area with a uint16_t
  573. * @s: Pointer to the start of the area.
  574. * @v: The value to fill the area with
  575. * @count: The number of values to store
  576. *
  577. * Differs from memset() in that it fills with a uint16_t instead
  578. * of a byte. Remember that @count is the number of uint16_ts to
  579. * store, not the number of bytes.
  580. */
  581. void *memset16(uint16_t *s, uint16_t v, size_t count)
  582. {
  583. uint16_t *xs = s;
  584. while (count--)
  585. *xs++ = v;
  586. return s;
  587. }
  588. EXPORT_SYMBOL(memset16);
  589. #endif
  590. #ifndef __HAVE_ARCH_MEMSET32
  591. /**
  592. * memset32() - Fill a memory area with a uint32_t
  593. * @s: Pointer to the start of the area.
  594. * @v: The value to fill the area with
  595. * @count: The number of values to store
  596. *
  597. * Differs from memset() in that it fills with a uint32_t instead
  598. * of a byte. Remember that @count is the number of uint32_ts to
  599. * store, not the number of bytes.
  600. */
  601. void *memset32(uint32_t *s, uint32_t v, size_t count)
  602. {
  603. uint32_t *xs = s;
  604. while (count--)
  605. *xs++ = v;
  606. return s;
  607. }
  608. EXPORT_SYMBOL(memset32);
  609. #endif
  610. #ifndef __HAVE_ARCH_MEMSET64
  611. /**
  612. * memset64() - Fill a memory area with a uint64_t
  613. * @s: Pointer to the start of the area.
  614. * @v: The value to fill the area with
  615. * @count: The number of values to store
  616. *
  617. * Differs from memset() in that it fills with a uint64_t instead
  618. * of a byte. Remember that @count is the number of uint64_ts to
  619. * store, not the number of bytes.
  620. */
  621. void *memset64(uint64_t *s, uint64_t v, size_t count)
  622. {
  623. uint64_t *xs = s;
  624. while (count--)
  625. *xs++ = v;
  626. return s;
  627. }
  628. EXPORT_SYMBOL(memset64);
  629. #endif
  630. #ifndef __HAVE_ARCH_MEMCPY
  631. /**
  632. * memcpy - Copy one area of memory to another
  633. * @dest: Where to copy to
  634. * @src: Where to copy from
  635. * @count: The size of the area.
  636. *
  637. * You should not use this function to access IO space, use memcpy_toio()
  638. * or memcpy_fromio() instead.
  639. */
  640. void *memcpy(void *dest, const void *src, size_t count)
  641. {
  642. char *tmp = dest;
  643. const char *s = src;
  644. while (count--)
  645. *tmp++ = *s++;
  646. return dest;
  647. }
  648. EXPORT_SYMBOL(memcpy);
  649. #endif
  650. #ifndef __HAVE_ARCH_MEMMOVE
  651. /**
  652. * memmove - Copy one area of memory to another
  653. * @dest: Where to copy to
  654. * @src: Where to copy from
  655. * @count: The size of the area.
  656. *
  657. * Unlike memcpy(), memmove() copes with overlapping areas.
  658. */
  659. void *memmove(void *dest, const void *src, size_t count)
  660. {
  661. char *tmp;
  662. const char *s;
  663. if (dest <= src) {
  664. tmp = dest;
  665. s = src;
  666. while (count--)
  667. *tmp++ = *s++;
  668. } else {
  669. tmp = dest;
  670. tmp += count;
  671. s = src;
  672. s += count;
  673. while (count--)
  674. *--tmp = *--s;
  675. }
  676. return dest;
  677. }
  678. EXPORT_SYMBOL(memmove);
  679. #endif
  680. #ifndef __HAVE_ARCH_MEMCMP
  681. /**
  682. * memcmp - Compare two areas of memory
  683. * @cs: One area of memory
  684. * @ct: Another area of memory
  685. * @count: The size of the area.
  686. */
  687. #undef memcmp
  688. __visible int memcmp(const void *cs, const void *ct, size_t count)
  689. {
  690. const unsigned char *su1, *su2;
  691. int res = 0;
  692. #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  693. if (count >= sizeof(unsigned long)) {
  694. const unsigned long *u1 = cs;
  695. const unsigned long *u2 = ct;
  696. do {
  697. if (get_unaligned(u1) != get_unaligned(u2))
  698. break;
  699. u1++;
  700. u2++;
  701. count -= sizeof(unsigned long);
  702. } while (count >= sizeof(unsigned long));
  703. cs = u1;
  704. ct = u2;
  705. }
  706. #endif
  707. for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  708. if ((res = *su1 - *su2) != 0)
  709. break;
  710. return res;
  711. }
  712. EXPORT_SYMBOL(memcmp);
  713. #endif
  714. #ifndef __HAVE_ARCH_BCMP
  715. /**
  716. * bcmp - returns 0 if and only if the buffers have identical contents.
  717. * @a: pointer to first buffer.
  718. * @b: pointer to second buffer.
  719. * @len: size of buffers.
  720. *
  721. * The sign or magnitude of a non-zero return value has no particular
  722. * meaning, and architectures may implement their own more efficient bcmp(). So
  723. * while this particular implementation is a simple (tail) call to memcmp, do
  724. * not rely on anything but whether the return value is zero or non-zero.
  725. */
  726. int bcmp(const void *a, const void *b, size_t len)
  727. {
  728. return memcmp(a, b, len);
  729. }
  730. EXPORT_SYMBOL(bcmp);
  731. #endif
  732. #ifndef __HAVE_ARCH_MEMSCAN
  733. /**
  734. * memscan - Find a character in an area of memory.
  735. * @addr: The memory area
  736. * @c: The byte to search for
  737. * @size: The size of the area.
  738. *
  739. * returns the address of the first occurrence of @c, or 1 byte past
  740. * the area if @c is not found
  741. */
  742. void *memscan(void *addr, int c, size_t size)
  743. {
  744. unsigned char *p = addr;
  745. while (size) {
  746. if (*p == (unsigned char)c)
  747. return (void *)p;
  748. p++;
  749. size--;
  750. }
  751. return (void *)p;
  752. }
  753. EXPORT_SYMBOL(memscan);
  754. #endif
  755. #ifndef __HAVE_ARCH_STRSTR
  756. /**
  757. * strstr - Find the first substring in a %NUL terminated string
  758. * @s1: The string to be searched
  759. * @s2: The string to search for
  760. */
  761. char *strstr(const char *s1, const char *s2)
  762. {
  763. size_t l1, l2;
  764. l2 = strlen(s2);
  765. if (!l2)
  766. return (char *)s1;
  767. l1 = strlen(s1);
  768. while (l1 >= l2) {
  769. l1--;
  770. if (!memcmp(s1, s2, l2))
  771. return (char *)s1;
  772. s1++;
  773. }
  774. return NULL;
  775. }
  776. EXPORT_SYMBOL(strstr);
  777. #endif
  778. #ifndef __HAVE_ARCH_STRNSTR
  779. /**
  780. * strnstr - Find the first substring in a length-limited string
  781. * @s1: The string to be searched
  782. * @s2: The string to search for
  783. * @len: the maximum number of characters to search
  784. */
  785. char *strnstr(const char *s1, const char *s2, size_t len)
  786. {
  787. size_t l2;
  788. l2 = strlen(s2);
  789. if (!l2)
  790. return (char *)s1;
  791. while (len >= l2) {
  792. len--;
  793. if (!memcmp(s1, s2, l2))
  794. return (char *)s1;
  795. s1++;
  796. }
  797. return NULL;
  798. }
  799. EXPORT_SYMBOL(strnstr);
  800. #endif
  801. #ifndef __HAVE_ARCH_MEMCHR
  802. /**
  803. * memchr - Find a character in an area of memory.
  804. * @s: The memory area
  805. * @c: The byte to search for
  806. * @n: The size of the area.
  807. *
  808. * returns the address of the first occurrence of @c, or %NULL
  809. * if @c is not found
  810. */
  811. void *memchr(const void *s, int c, size_t n)
  812. {
  813. const unsigned char *p = s;
  814. while (n-- != 0) {
  815. if ((unsigned char)c == *p++) {
  816. return (void *)(p - 1);
  817. }
  818. }
  819. return NULL;
  820. }
  821. EXPORT_SYMBOL(memchr);
  822. #endif
  823. static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
  824. {
  825. while (bytes) {
  826. if (*start != value)
  827. return (void *)start;
  828. start++;
  829. bytes--;
  830. }
  831. return NULL;
  832. }
  833. /**
  834. * memchr_inv - Find an unmatching character in an area of memory.
  835. * @start: The memory area
  836. * @c: Find a character other than c
  837. * @bytes: The size of the area.
  838. *
  839. * returns the address of the first character other than @c, or %NULL
  840. * if the whole buffer contains just @c.
  841. */
  842. void *memchr_inv(const void *start, int c, size_t bytes)
  843. {
  844. u8 value = c;
  845. u64 value64;
  846. unsigned int words, prefix;
  847. if (bytes <= 16)
  848. return check_bytes8(start, value, bytes);
  849. value64 = value;
  850. #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
  851. value64 *= 0x0101010101010101ULL;
  852. #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
  853. value64 *= 0x01010101;
  854. value64 |= value64 << 32;
  855. #else
  856. value64 |= value64 << 8;
  857. value64 |= value64 << 16;
  858. value64 |= value64 << 32;
  859. #endif
  860. prefix = (unsigned long)start % 8;
  861. if (prefix) {
  862. u8 *r;
  863. prefix = 8 - prefix;
  864. r = check_bytes8(start, value, prefix);
  865. if (r)
  866. return r;
  867. start += prefix;
  868. bytes -= prefix;
  869. }
  870. words = bytes / 8;
  871. while (words) {
  872. if (*(u64 *)start != value64)
  873. return check_bytes8(start, value, 8);
  874. start += 8;
  875. words--;
  876. }
  877. return check_bytes8(start, value, bytes % 8);
  878. }
  879. EXPORT_SYMBOL(memchr_inv);