string.c 21 KB

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