string.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. /*
  2. * linux/lib/string.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * stupid library routines.. The optimized versions should generally be found
  8. * as inline code in <asm-xx/string.h>
  9. *
  10. * These are buggy as well..
  11. *
  12. * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
  13. * - Added strsep() which will replace strtok() soon (because strsep() is
  14. * reentrant and should be faster). Use only strsep() in new code, please.
  15. *
  16. * * Sat Feb 09 2002, Jason Thomas <jason@topic.com.au>,
  17. * Matthew Hawkins <matt@mh.dropbear.id.au>
  18. * - Kissed strtok() goodbye
  19. */
  20. #include <linux/types.h>
  21. #include <linux/string.h>
  22. #include <linux/ctype.h>
  23. #include <linux/kernel.h>
  24. #include <linux/export.h>
  25. #include <linux/bug.h>
  26. #include <linux/errno.h>
  27. #include <asm/byteorder.h>
  28. #include <asm/word-at-a-time.h>
  29. #include <asm/page.h>
  30. #ifndef __HAVE_ARCH_STRNCASECMP
  31. /**
  32. * strncasecmp - Case insensitive, length-limited string comparison
  33. * @s1: One string
  34. * @s2: The other string
  35. * @len: the maximum number of characters to compare
  36. */
  37. int strncasecmp(const char *s1, const char *s2, size_t len)
  38. {
  39. /* Yes, Virginia, it had better be unsigned */
  40. unsigned char c1, c2;
  41. if (!len)
  42. return 0;
  43. do {
  44. c1 = *s1++;
  45. c2 = *s2++;
  46. if (!c1 || !c2)
  47. break;
  48. if (c1 == c2)
  49. continue;
  50. c1 = tolower(c1);
  51. c2 = tolower(c2);
  52. if (c1 != c2)
  53. break;
  54. } while (--len);
  55. return (int)c1 - (int)c2;
  56. }
  57. EXPORT_SYMBOL(strncasecmp);
  58. #endif
  59. #ifndef __HAVE_ARCH_STRCASECMP
  60. int strcasecmp(const char *s1, const char *s2)
  61. {
  62. int c1, c2;
  63. do {
  64. c1 = tolower(*s1++);
  65. c2 = tolower(*s2++);
  66. } while (c1 == c2 && c1 != 0);
  67. return c1 - c2;
  68. }
  69. EXPORT_SYMBOL(strcasecmp);
  70. #endif
  71. #ifndef __HAVE_ARCH_STRCPY
  72. /**
  73. * strcpy - Copy a %NUL terminated string
  74. * @dest: Where to copy the string to
  75. * @src: Where to copy the string from
  76. */
  77. #undef strcpy
  78. char *strcpy(char *dest, const char *src)
  79. {
  80. char *tmp = dest;
  81. while ((*dest++ = *src++) != '\0')
  82. /* nothing */;
  83. return tmp;
  84. }
  85. EXPORT_SYMBOL(strcpy);
  86. #endif
  87. #ifndef __HAVE_ARCH_STRNCPY
  88. /**
  89. * strncpy - Copy a length-limited, C-string
  90. * @dest: Where to copy the string to
  91. * @src: Where to copy the string from
  92. * @count: The maximum number of bytes to copy
  93. *
  94. * The result is not %NUL-terminated if the source exceeds
  95. * @count bytes.
  96. *
  97. * In the case where the length of @src is less than that of
  98. * count, the remainder of @dest will be padded with %NUL.
  99. *
  100. */
  101. char *strncpy(char *dest, const char *src, size_t count)
  102. {
  103. char *tmp = dest;
  104. while (count) {
  105. if ((*tmp = *src) != 0)
  106. src++;
  107. tmp++;
  108. count--;
  109. }
  110. return dest;
  111. }
  112. EXPORT_SYMBOL(strncpy);
  113. #endif
  114. #ifndef __HAVE_ARCH_STRLCPY
  115. /**
  116. * strlcpy - Copy a C-string into a sized buffer
  117. * @dest: Where to copy the string to
  118. * @src: Where to copy the string from
  119. * @size: size of destination buffer
  120. *
  121. * Compatible with ``*BSD``: the result is always a valid
  122. * NUL-terminated string that fits in the buffer (unless,
  123. * of course, the buffer size is zero). It does not pad
  124. * out the result like strncpy() does.
  125. */
  126. size_t strlcpy(char *dest, const char *src, size_t size)
  127. {
  128. size_t ret = strlen(src);
  129. if (size) {
  130. size_t len = (ret >= size) ? size - 1 : ret;
  131. memcpy(dest, src, len);
  132. dest[len] = '\0';
  133. }
  134. return ret;
  135. }
  136. EXPORT_SYMBOL(strlcpy);
  137. #endif
  138. #ifndef __HAVE_ARCH_STRSCPY
  139. /**
  140. * strscpy - Copy a C-string into a sized buffer
  141. * @dest: Where to copy the string to
  142. * @src: Where to copy the string from
  143. * @count: Size of destination buffer
  144. *
  145. * Copy the string, or as much of it as fits, into the dest buffer.
  146. * The routine returns the number of characters copied (not including
  147. * the trailing NUL) or -E2BIG if the destination buffer wasn't big enough.
  148. * The behavior is undefined if the string buffers overlap.
  149. * The destination buffer is always NUL terminated, unless it's zero-sized.
  150. *
  151. * Preferred to strlcpy() since the API doesn't require reading memory
  152. * from the src string beyond the specified "count" bytes, and since
  153. * the return value is easier to error-check than strlcpy()'s.
  154. * In addition, the implementation is robust to the string changing out
  155. * from underneath it, unlike the current strlcpy() implementation.
  156. *
  157. * Preferred to strncpy() since it always returns a valid string, and
  158. * doesn't unnecessarily force the tail of the destination buffer to be
  159. * zeroed. If the zeroing is desired, it's likely cleaner to use strscpy()
  160. * with an overflow test, then just memset() the tail of the dest buffer.
  161. */
  162. ssize_t strscpy(char *dest, const char *src, size_t count)
  163. {
  164. const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
  165. size_t max = count;
  166. long res = 0;
  167. if (count == 0)
  168. return -E2BIG;
  169. #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  170. /*
  171. * If src is unaligned, don't cross a page boundary,
  172. * since we don't know if the next page is mapped.
  173. */
  174. if ((long)src & (sizeof(long) - 1)) {
  175. size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
  176. if (limit < max)
  177. max = limit;
  178. }
  179. #else
  180. /* If src or dest is unaligned, don't do word-at-a-time. */
  181. if (((long) dest | (long) src) & (sizeof(long) - 1))
  182. max = 0;
  183. #endif
  184. while (max >= sizeof(unsigned long)) {
  185. unsigned long c, data;
  186. c = *(unsigned long *)(src+res);
  187. 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_MEMSET16
  658. /**
  659. * memset16() - Fill a memory area with a uint16_t
  660. * @s: Pointer to the start of the area.
  661. * @v: The value to fill the area with
  662. * @count: The number of values to store
  663. *
  664. * Differs from memset() in that it fills with a uint16_t instead
  665. * of a byte. Remember that @count is the number of uint16_ts to
  666. * store, not the number of bytes.
  667. */
  668. void *memset16(uint16_t *s, uint16_t v, size_t count)
  669. {
  670. uint16_t *xs = s;
  671. while (count--)
  672. *xs++ = v;
  673. return s;
  674. }
  675. EXPORT_SYMBOL(memset16);
  676. #endif
  677. #ifndef __HAVE_ARCH_MEMSET32
  678. /**
  679. * memset32() - Fill a memory area with a uint32_t
  680. * @s: Pointer to the start of the area.
  681. * @v: The value to fill the area with
  682. * @count: The number of values to store
  683. *
  684. * Differs from memset() in that it fills with a uint32_t instead
  685. * of a byte. Remember that @count is the number of uint32_ts to
  686. * store, not the number of bytes.
  687. */
  688. void *memset32(uint32_t *s, uint32_t v, size_t count)
  689. {
  690. uint32_t *xs = s;
  691. while (count--)
  692. *xs++ = v;
  693. return s;
  694. }
  695. EXPORT_SYMBOL(memset32);
  696. #endif
  697. #ifndef __HAVE_ARCH_MEMSET64
  698. /**
  699. * memset64() - Fill a memory area with a uint64_t
  700. * @s: Pointer to the start of the area.
  701. * @v: The value to fill the area with
  702. * @count: The number of values to store
  703. *
  704. * Differs from memset() in that it fills with a uint64_t instead
  705. * of a byte. Remember that @count is the number of uint64_ts to
  706. * store, not the number of bytes.
  707. */
  708. void *memset64(uint64_t *s, uint64_t v, size_t count)
  709. {
  710. uint64_t *xs = s;
  711. while (count--)
  712. *xs++ = v;
  713. return s;
  714. }
  715. EXPORT_SYMBOL(memset64);
  716. #endif
  717. #ifndef __HAVE_ARCH_MEMCPY
  718. /**
  719. * memcpy - Copy one area of memory to another
  720. * @dest: Where to copy to
  721. * @src: Where to copy from
  722. * @count: The size of the area.
  723. *
  724. * You should not use this function to access IO space, use memcpy_toio()
  725. * or memcpy_fromio() instead.
  726. */
  727. void *memcpy(void *dest, const void *src, size_t count)
  728. {
  729. char *tmp = dest;
  730. const char *s = src;
  731. while (count--)
  732. *tmp++ = *s++;
  733. return dest;
  734. }
  735. EXPORT_SYMBOL(memcpy);
  736. #endif
  737. #ifndef __HAVE_ARCH_MEMMOVE
  738. /**
  739. * memmove - Copy one area of memory to another
  740. * @dest: Where to copy to
  741. * @src: Where to copy from
  742. * @count: The size of the area.
  743. *
  744. * Unlike memcpy(), memmove() copes with overlapping areas.
  745. */
  746. void *memmove(void *dest, const void *src, size_t count)
  747. {
  748. char *tmp;
  749. const char *s;
  750. if (dest <= src) {
  751. tmp = dest;
  752. s = src;
  753. while (count--)
  754. *tmp++ = *s++;
  755. } else {
  756. tmp = dest;
  757. tmp += count;
  758. s = src;
  759. s += count;
  760. while (count--)
  761. *--tmp = *--s;
  762. }
  763. return dest;
  764. }
  765. EXPORT_SYMBOL(memmove);
  766. #endif
  767. #ifndef __HAVE_ARCH_MEMCMP
  768. /**
  769. * memcmp - Compare two areas of memory
  770. * @cs: One area of memory
  771. * @ct: Another area of memory
  772. * @count: The size of the area.
  773. */
  774. #undef memcmp
  775. __visible int memcmp(const void *cs, const void *ct, size_t count)
  776. {
  777. const unsigned char *su1, *su2;
  778. int res = 0;
  779. for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  780. if ((res = *su1 - *su2) != 0)
  781. break;
  782. return res;
  783. }
  784. EXPORT_SYMBOL(memcmp);
  785. #endif
  786. #ifndef __HAVE_ARCH_MEMSCAN
  787. /**
  788. * memscan - Find a character in an area of memory.
  789. * @addr: The memory area
  790. * @c: The byte to search for
  791. * @size: The size of the area.
  792. *
  793. * returns the address of the first occurrence of @c, or 1 byte past
  794. * the area if @c is not found
  795. */
  796. void *memscan(void *addr, int c, size_t size)
  797. {
  798. unsigned char *p = addr;
  799. while (size) {
  800. if (*p == c)
  801. return (void *)p;
  802. p++;
  803. size--;
  804. }
  805. return (void *)p;
  806. }
  807. EXPORT_SYMBOL(memscan);
  808. #endif
  809. #ifndef __HAVE_ARCH_STRSTR
  810. /**
  811. * strstr - Find the first substring in a %NUL terminated string
  812. * @s1: The string to be searched
  813. * @s2: The string to search for
  814. */
  815. char *strstr(const char *s1, const char *s2)
  816. {
  817. size_t l1, l2;
  818. l2 = strlen(s2);
  819. if (!l2)
  820. return (char *)s1;
  821. l1 = strlen(s1);
  822. while (l1 >= l2) {
  823. l1--;
  824. if (!memcmp(s1, s2, l2))
  825. return (char *)s1;
  826. s1++;
  827. }
  828. return NULL;
  829. }
  830. EXPORT_SYMBOL(strstr);
  831. #endif
  832. #ifndef __HAVE_ARCH_STRNSTR
  833. /**
  834. * strnstr - Find the first substring in a length-limited string
  835. * @s1: The string to be searched
  836. * @s2: The string to search for
  837. * @len: the maximum number of characters to search
  838. */
  839. char *strnstr(const char *s1, const char *s2, size_t len)
  840. {
  841. size_t l2;
  842. l2 = strlen(s2);
  843. if (!l2)
  844. return (char *)s1;
  845. while (len >= l2) {
  846. len--;
  847. if (!memcmp(s1, s2, l2))
  848. return (char *)s1;
  849. s1++;
  850. }
  851. return NULL;
  852. }
  853. EXPORT_SYMBOL(strnstr);
  854. #endif
  855. #ifndef __HAVE_ARCH_MEMCHR
  856. /**
  857. * memchr - Find a character in an area of memory.
  858. * @s: The memory area
  859. * @c: The byte to search for
  860. * @n: The size of the area.
  861. *
  862. * returns the address of the first occurrence of @c, or %NULL
  863. * if @c is not found
  864. */
  865. void *memchr(const void *s, int c, size_t n)
  866. {
  867. const unsigned char *p = s;
  868. while (n-- != 0) {
  869. if ((unsigned char)c == *p++) {
  870. return (void *)(p - 1);
  871. }
  872. }
  873. return NULL;
  874. }
  875. EXPORT_SYMBOL(memchr);
  876. #endif
  877. static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
  878. {
  879. while (bytes) {
  880. if (*start != value)
  881. return (void *)start;
  882. start++;
  883. bytes--;
  884. }
  885. return NULL;
  886. }
  887. /**
  888. * memchr_inv - Find an unmatching character in an area of memory.
  889. * @start: The memory area
  890. * @c: Find a character other than c
  891. * @bytes: The size of the area.
  892. *
  893. * returns the address of the first character other than @c, or %NULL
  894. * if the whole buffer contains just @c.
  895. */
  896. void *memchr_inv(const void *start, int c, size_t bytes)
  897. {
  898. u8 value = c;
  899. u64 value64;
  900. unsigned int words, prefix;
  901. if (bytes <= 16)
  902. return check_bytes8(start, value, bytes);
  903. value64 = value;
  904. #if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
  905. value64 *= 0x0101010101010101ULL;
  906. #elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
  907. value64 *= 0x01010101;
  908. value64 |= value64 << 32;
  909. #else
  910. value64 |= value64 << 8;
  911. value64 |= value64 << 16;
  912. value64 |= value64 << 32;
  913. #endif
  914. prefix = (unsigned long)start % 8;
  915. if (prefix) {
  916. u8 *r;
  917. prefix = 8 - prefix;
  918. r = check_bytes8(start, value, prefix);
  919. if (r)
  920. return r;
  921. start += prefix;
  922. bytes -= prefix;
  923. }
  924. words = bytes / 8;
  925. while (words) {
  926. if (*(u64 *)start != value64)
  927. return check_bytes8(start, value, 8);
  928. start += 8;
  929. words--;
  930. }
  931. return check_bytes8(start, value, bytes % 8);
  932. }
  933. EXPORT_SYMBOL(memchr_inv);
  934. /**
  935. * strreplace - Replace all occurrences of character in string.
  936. * @s: The string to operate on.
  937. * @old: The character being replaced.
  938. * @new: The character @old is replaced with.
  939. *
  940. * Returns pointer to the nul byte at the end of @s.
  941. */
  942. char *strreplace(char *s, char old, char new)
  943. {
  944. for (; *s; ++s)
  945. if (*s == old)
  946. *s = new;
  947. return s;
  948. }
  949. EXPORT_SYMBOL(strreplace);
  950. void fortify_panic(const char *name)
  951. {
  952. pr_emerg("detected buffer overflow in %s\n", name);
  953. BUG();
  954. }
  955. EXPORT_SYMBOL(fortify_panic);
  956. #ifdef CONFIG_STRING_SELFTEST
  957. #include <linux/slab.h>
  958. #include <linux/module.h>
  959. static __init int memset16_selftest(void)
  960. {
  961. unsigned i, j, k;
  962. u16 v, *p = kmalloc(256 * 2 * 2, GFP_KERNEL);
  963. for (i = 0; i < 256; i++) {
  964. for (j = 0; j < 256; j++) {
  965. memset(p, 0xa1, 256 * 2 * sizeof(v));
  966. memset16(p + i, 0xb1b2, j);
  967. for (k = 0; k < 512; k++) {
  968. v = p[k];
  969. if (k < i) {
  970. if (v != 0xa1a1)
  971. goto fail;
  972. } else if (k < i + j) {
  973. if (v != 0xb1b2)
  974. goto fail;
  975. } else {
  976. if (v != 0xa1a1)
  977. goto fail;
  978. }
  979. }
  980. }
  981. }
  982. fail:
  983. kfree(p);
  984. if (i < 256)
  985. return (i << 24) | (j << 16) | k;
  986. return 0;
  987. }
  988. static __init int memset32_selftest(void)
  989. {
  990. unsigned i, j, k;
  991. u32 v, *p = kmalloc(256 * 2 * 4, GFP_KERNEL);
  992. for (i = 0; i < 256; i++) {
  993. for (j = 0; j < 256; j++) {
  994. memset(p, 0xa1, 256 * 2 * sizeof(v));
  995. memset32(p + i, 0xb1b2b3b4, j);
  996. for (k = 0; k < 512; k++) {
  997. v = p[k];
  998. if (k < i) {
  999. if (v != 0xa1a1a1a1)
  1000. goto fail;
  1001. } else if (k < i + j) {
  1002. if (v != 0xb1b2b3b4)
  1003. goto fail;
  1004. } else {
  1005. if (v != 0xa1a1a1a1)
  1006. goto fail;
  1007. }
  1008. }
  1009. }
  1010. }
  1011. fail:
  1012. kfree(p);
  1013. if (i < 256)
  1014. return (i << 24) | (j << 16) | k;
  1015. return 0;
  1016. }
  1017. static __init int memset64_selftest(void)
  1018. {
  1019. unsigned i, j, k;
  1020. u64 v, *p = kmalloc(256 * 2 * 8, GFP_KERNEL);
  1021. for (i = 0; i < 256; i++) {
  1022. for (j = 0; j < 256; j++) {
  1023. memset(p, 0xa1, 256 * 2 * sizeof(v));
  1024. memset64(p + i, 0xb1b2b3b4b5b6b7b8ULL, j);
  1025. for (k = 0; k < 512; k++) {
  1026. v = p[k];
  1027. if (k < i) {
  1028. if (v != 0xa1a1a1a1a1a1a1a1ULL)
  1029. goto fail;
  1030. } else if (k < i + j) {
  1031. if (v != 0xb1b2b3b4b5b6b7b8ULL)
  1032. goto fail;
  1033. } else {
  1034. if (v != 0xa1a1a1a1a1a1a1a1ULL)
  1035. goto fail;
  1036. }
  1037. }
  1038. }
  1039. }
  1040. fail:
  1041. kfree(p);
  1042. if (i < 256)
  1043. return (i << 24) | (j << 16) | k;
  1044. return 0;
  1045. }
  1046. static __init int string_selftest_init(void)
  1047. {
  1048. int test, subtest;
  1049. test = 1;
  1050. subtest = memset16_selftest();
  1051. if (subtest)
  1052. goto fail;
  1053. test = 2;
  1054. subtest = memset32_selftest();
  1055. if (subtest)
  1056. goto fail;
  1057. test = 3;
  1058. subtest = memset64_selftest();
  1059. if (subtest)
  1060. goto fail;
  1061. pr_info("String selftests succeeded\n");
  1062. return 0;
  1063. fail:
  1064. pr_crit("String selftest failure %d.%08x\n", test, subtest);
  1065. return 0;
  1066. }
  1067. module_init(string_selftest_init);
  1068. #endif /* CONFIG_STRING_SELFTEST */