string.c 22 KB

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