string.c 23 KB

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