string.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. #ifndef __HAVE_ARCH_STRNICMP
  28. /**
  29. * strnicmp - Case insensitive, length-limited string comparison
  30. * @s1: One string
  31. * @s2: The other string
  32. * @len: the maximum number of characters to compare
  33. */
  34. int strnicmp(const char *s1, const char *s2, size_t len)
  35. {
  36. /* Yes, Virginia, it had better be unsigned */
  37. unsigned char c1, c2;
  38. if (!len)
  39. return 0;
  40. do {
  41. c1 = *s1++;
  42. c2 = *s2++;
  43. if (!c1 || !c2)
  44. break;
  45. if (c1 == c2)
  46. continue;
  47. c1 = tolower(c1);
  48. c2 = tolower(c2);
  49. if (c1 != c2)
  50. break;
  51. } while (--len);
  52. return (int)c1 - (int)c2;
  53. }
  54. EXPORT_SYMBOL(strnicmp);
  55. #endif
  56. #ifndef __HAVE_ARCH_STRCASECMP
  57. int strcasecmp(const char *s1, const char *s2)
  58. {
  59. int c1, c2;
  60. do {
  61. c1 = tolower(*s1++);
  62. c2 = tolower(*s2++);
  63. } while (c1 == c2 && c1 != 0);
  64. return c1 - c2;
  65. }
  66. EXPORT_SYMBOL(strcasecmp);
  67. #endif
  68. #ifndef __HAVE_ARCH_STRNCASECMP
  69. int strncasecmp(const char *s1, const char *s2, size_t n)
  70. {
  71. int c1, c2;
  72. do {
  73. c1 = tolower(*s1++);
  74. c2 = tolower(*s2++);
  75. } while ((--n > 0) && c1 == c2 && c1 != 0);
  76. return c1 - c2;
  77. }
  78. EXPORT_SYMBOL(strncasecmp);
  79. #endif
  80. #ifndef __HAVE_ARCH_STRCPY
  81. /**
  82. * strcpy - Copy a %NUL terminated string
  83. * @dest: Where to copy the string to
  84. * @src: Where to copy the string from
  85. */
  86. #undef strcpy
  87. char *strcpy(char *dest, const char *src)
  88. {
  89. char *tmp = dest;
  90. while ((*dest++ = *src++) != '\0')
  91. /* nothing */;
  92. return tmp;
  93. }
  94. EXPORT_SYMBOL(strcpy);
  95. #endif
  96. #ifndef __HAVE_ARCH_STRNCPY
  97. /**
  98. * strncpy - Copy a length-limited, %NUL-terminated string
  99. * @dest: Where to copy the string to
  100. * @src: Where to copy the string from
  101. * @count: The maximum number of bytes to copy
  102. *
  103. * The result is not %NUL-terminated if the source exceeds
  104. * @count bytes.
  105. *
  106. * In the case where the length of @src is less than that of
  107. * count, the remainder of @dest will be padded with %NUL.
  108. *
  109. */
  110. char *strncpy(char *dest, const char *src, size_t count)
  111. {
  112. char *tmp = dest;
  113. while (count) {
  114. if ((*tmp = *src) != 0)
  115. src++;
  116. tmp++;
  117. count--;
  118. }
  119. return dest;
  120. }
  121. EXPORT_SYMBOL(strncpy);
  122. #endif
  123. #ifndef __HAVE_ARCH_STRLCPY
  124. /**
  125. * strlcpy - Copy a %NUL terminated string into a sized buffer
  126. * @dest: Where to copy the string to
  127. * @src: Where to copy the string from
  128. * @size: size of destination buffer
  129. *
  130. * Compatible with *BSD: the result is always a valid
  131. * NUL-terminated string that fits in the buffer (unless,
  132. * of course, the buffer size is zero). It does not pad
  133. * out the result like strncpy() does.
  134. */
  135. size_t strlcpy(char *dest, const char *src, size_t size)
  136. {
  137. size_t ret = strlen(src);
  138. if (size) {
  139. size_t len = (ret >= size) ? size - 1 : ret;
  140. memcpy(dest, src, len);
  141. dest[len] = '\0';
  142. }
  143. return ret;
  144. }
  145. EXPORT_SYMBOL(strlcpy);
  146. #endif
  147. #ifndef __HAVE_ARCH_STRSCPY
  148. /**
  149. * strscpy - Copy a C-string into a sized buffer, but only if it fits
  150. * @dest: Where to copy the string to
  151. * @src: Where to copy the string from
  152. * @size: size of destination buffer
  153. *
  154. * Use this routine to avoid copying too-long strings.
  155. * The routine returns the total number of bytes copied
  156. * (including the trailing NUL) or zero if the buffer wasn't
  157. * big enough. To ensure that programmers pay attention
  158. * to the return code, the destination has a single NUL
  159. * written at the front (if size is non-zero) when the
  160. * buffer is not big enough.
  161. */
  162. static size_t strscpy(char *dest, const char *src, size_t size)
  163. {
  164. size_t len = strnlen(src, size) + 1;
  165. if (len > size) {
  166. if (size)
  167. dest[0] = '\0';
  168. return 0;
  169. }
  170. memcpy(dest, src, len);
  171. return len;
  172. }
  173. EXPORT_SYMBOL(strscpy);
  174. #endif
  175. #ifndef __HAVE_ARCH_STRCAT
  176. /**
  177. * strcat - Append one %NUL-terminated string to another
  178. * @dest: The string to be appended to
  179. * @src: The string to append to it
  180. */
  181. #undef strcat
  182. char *strcat(char *dest, const char *src)
  183. {
  184. char *tmp = dest;
  185. while (*dest)
  186. dest++;
  187. while ((*dest++ = *src++) != '\0')
  188. ;
  189. return tmp;
  190. }
  191. EXPORT_SYMBOL(strcat);
  192. #endif
  193. #ifndef __HAVE_ARCH_STRNCAT
  194. /**
  195. * strncat - Append a length-limited, %NUL-terminated string to another
  196. * @dest: The string to be appended to
  197. * @src: The string to append to it
  198. * @count: The maximum numbers of bytes to copy
  199. *
  200. * Note that in contrast to strncpy(), strncat() ensures the result is
  201. * terminated.
  202. */
  203. char *strncat(char *dest, const char *src, size_t count)
  204. {
  205. char *tmp = dest;
  206. if (count) {
  207. while (*dest)
  208. dest++;
  209. while ((*dest++ = *src++) != 0) {
  210. if (--count == 0) {
  211. *dest = '\0';
  212. break;
  213. }
  214. }
  215. }
  216. return tmp;
  217. }
  218. EXPORT_SYMBOL(strncat);
  219. #endif
  220. #ifndef __HAVE_ARCH_STRLCAT
  221. /**
  222. * strlcat - Append a length-limited, %NUL-terminated string to another
  223. * @dest: The string to be appended to
  224. * @src: The string to append to it
  225. * @count: The size of the destination buffer.
  226. */
  227. size_t strlcat(char *dest, const char *src, size_t count)
  228. {
  229. size_t dsize = strlen(dest);
  230. size_t len = strlen(src);
  231. size_t res = dsize + len;
  232. /* This would be a bug */
  233. BUG_ON(dsize >= count);
  234. dest += dsize;
  235. count -= dsize;
  236. if (len >= count)
  237. len = count-1;
  238. memcpy(dest, src, len);
  239. dest[len] = 0;
  240. return res;
  241. }
  242. EXPORT_SYMBOL(strlcat);
  243. #endif
  244. #ifndef __HAVE_ARCH_STRCMP
  245. /**
  246. * strcmp - Compare two strings
  247. * @cs: One string
  248. * @ct: Another string
  249. */
  250. #undef strcmp
  251. int strcmp(const char *cs, const char *ct)
  252. {
  253. unsigned char c1, c2;
  254. while (1) {
  255. c1 = *cs++;
  256. c2 = *ct++;
  257. if (c1 != c2)
  258. return c1 < c2 ? -1 : 1;
  259. if (!c1)
  260. break;
  261. }
  262. return 0;
  263. }
  264. EXPORT_SYMBOL(strcmp);
  265. #endif
  266. #ifndef __HAVE_ARCH_STRNCMP
  267. /**
  268. * strncmp - Compare two length-limited strings
  269. * @cs: One string
  270. * @ct: Another string
  271. * @count: The maximum number of bytes to compare
  272. */
  273. int strncmp(const char *cs, const char *ct, size_t count)
  274. {
  275. unsigned char c1, c2;
  276. while (count) {
  277. c1 = *cs++;
  278. c2 = *ct++;
  279. if (c1 != c2)
  280. return c1 < c2 ? -1 : 1;
  281. if (!c1)
  282. break;
  283. count--;
  284. }
  285. return 0;
  286. }
  287. EXPORT_SYMBOL(strncmp);
  288. #endif
  289. #ifndef __HAVE_ARCH_STRCHR
  290. /**
  291. * strchr - Find the first occurrence of a character in a string
  292. * @s: The string to be searched
  293. * @c: The character to search for
  294. */
  295. char *strchr(const char *s, int c)
  296. {
  297. for (; *s != (char)c; ++s)
  298. if (*s == '\0')
  299. return NULL;
  300. return (char *)s;
  301. }
  302. EXPORT_SYMBOL(strchr);
  303. #endif
  304. #ifndef __HAVE_ARCH_STRRCHR
  305. /**
  306. * strrchr - Find the last occurrence of a character in a string
  307. * @s: The string to be searched
  308. * @c: The character to search for
  309. */
  310. char *strrchr(const char *s, int c)
  311. {
  312. const char *p = s + strlen(s);
  313. do {
  314. if (*p == (char)c)
  315. return (char *)p;
  316. } while (--p >= s);
  317. return NULL;
  318. }
  319. EXPORT_SYMBOL(strrchr);
  320. #endif
  321. #ifndef __HAVE_ARCH_STRNCHR
  322. /**
  323. * strnchr - Find a character in a length limited string
  324. * @s: The string to be searched
  325. * @count: The number of characters to be searched
  326. * @c: The character to search for
  327. */
  328. char *strnchr(const char *s, size_t count, int c)
  329. {
  330. for (; count-- && *s != '\0'; ++s)
  331. if (*s == (char)c)
  332. return (char *)s;
  333. return NULL;
  334. }
  335. EXPORT_SYMBOL(strnchr);
  336. #endif
  337. /**
  338. * skip_spaces - Removes leading whitespace from @str.
  339. * @str: The string to be stripped.
  340. *
  341. * Returns a pointer to the first non-whitespace character in @str.
  342. */
  343. char *skip_spaces(const char *str)
  344. {
  345. while (isspace(*str))
  346. ++str;
  347. return (char *)str;
  348. }
  349. EXPORT_SYMBOL(skip_spaces);
  350. /**
  351. * strim - Removes leading and trailing whitespace from @s.
  352. * @s: The string to be stripped.
  353. *
  354. * Note that the first trailing whitespace is replaced with a %NUL-terminator
  355. * in the given string @s. Returns a pointer to the first non-whitespace
  356. * character in @s.
  357. */
  358. char *strim(char *s)
  359. {
  360. size_t size;
  361. char *end;
  362. size = strlen(s);
  363. if (!size)
  364. return s;
  365. end = s + size - 1;
  366. while (end >= s && isspace(*end))
  367. end--;
  368. *(end + 1) = '\0';
  369. return skip_spaces(s);
  370. }
  371. EXPORT_SYMBOL(strim);
  372. #ifndef __HAVE_ARCH_STRLEN
  373. /**
  374. * strlen - Find the length of a string
  375. * @s: The string to be sized
  376. */
  377. size_t strlen(const char *s)
  378. {
  379. const char *sc;
  380. for (sc = s; *sc != '\0'; ++sc)
  381. /* nothing */;
  382. return sc - s;
  383. }
  384. EXPORT_SYMBOL(strlen);
  385. #endif
  386. #ifndef __HAVE_ARCH_STRNLEN
  387. /**
  388. * strnlen - Find the length of a length-limited string
  389. * @s: The string to be sized
  390. * @count: The maximum number of bytes to search
  391. */
  392. size_t strnlen(const char *s, size_t count)
  393. {
  394. const char *sc;
  395. for (sc = s; count-- && *sc != '\0'; ++sc)
  396. /* nothing */;
  397. return sc - s;
  398. }
  399. EXPORT_SYMBOL(strnlen);
  400. #endif
  401. #ifndef __HAVE_ARCH_STRSPN
  402. /**
  403. * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
  404. * @s: The string to be searched
  405. * @accept: The string to search for
  406. */
  407. size_t strspn(const char *s, const char *accept)
  408. {
  409. const char *p;
  410. const char *a;
  411. size_t count = 0;
  412. for (p = s; *p != '\0'; ++p) {
  413. for (a = accept; *a != '\0'; ++a) {
  414. if (*p == *a)
  415. break;
  416. }
  417. if (*a == '\0')
  418. return count;
  419. ++count;
  420. }
  421. return count;
  422. }
  423. EXPORT_SYMBOL(strspn);
  424. #endif
  425. #ifndef __HAVE_ARCH_STRCSPN
  426. /**
  427. * strcspn - Calculate the length of the initial substring of @s which does not contain letters in @reject
  428. * @s: The string to be searched
  429. * @reject: The string to avoid
  430. */
  431. size_t strcspn(const char *s, const char *reject)
  432. {
  433. const char *p;
  434. const char *r;
  435. size_t count = 0;
  436. for (p = s; *p != '\0'; ++p) {
  437. for (r = reject; *r != '\0'; ++r) {
  438. if (*p == *r)
  439. return count;
  440. }
  441. ++count;
  442. }
  443. return count;
  444. }
  445. EXPORT_SYMBOL(strcspn);
  446. #endif
  447. #ifndef __HAVE_ARCH_STRPBRK
  448. /**
  449. * strpbrk - Find the first occurrence of a set of characters
  450. * @cs: The string to be searched
  451. * @ct: The characters to search for
  452. */
  453. char *strpbrk(const char *cs, const char *ct)
  454. {
  455. const char *sc1, *sc2;
  456. for (sc1 = cs; *sc1 != '\0'; ++sc1) {
  457. for (sc2 = ct; *sc2 != '\0'; ++sc2) {
  458. if (*sc1 == *sc2)
  459. return (char *)sc1;
  460. }
  461. }
  462. return NULL;
  463. }
  464. EXPORT_SYMBOL(strpbrk);
  465. #endif
  466. #ifndef __HAVE_ARCH_STRSEP
  467. /**
  468. * strsep - Split a string into tokens
  469. * @s: The string to be searched
  470. * @ct: The characters to search for
  471. *
  472. * strsep() updates @s to point after the token, ready for the next call.
  473. *
  474. * It returns empty tokens, too, behaving exactly like the libc function
  475. * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
  476. * Same semantics, slimmer shape. ;)
  477. */
  478. char *strsep(char **s, const char *ct)
  479. {
  480. char *sbegin = *s;
  481. char *end;
  482. if (sbegin == NULL)
  483. return NULL;
  484. end = strpbrk(sbegin, ct);
  485. if (end)
  486. *end++ = '\0';
  487. *s = end;
  488. return sbegin;
  489. }
  490. EXPORT_SYMBOL(strsep);
  491. #endif
  492. /**
  493. * sysfs_streq - return true if strings are equal, modulo trailing newline
  494. * @s1: one string
  495. * @s2: another string
  496. *
  497. * This routine returns true iff two strings are equal, treating both
  498. * NUL and newline-then-NUL as equivalent string terminations. It's
  499. * geared for use with sysfs input strings, which generally terminate
  500. * with newlines but are compared against values without newlines.
  501. */
  502. bool sysfs_streq(const char *s1, const char *s2)
  503. {
  504. while (*s1 && *s1 == *s2) {
  505. s1++;
  506. s2++;
  507. }
  508. if (*s1 == *s2)
  509. return true;
  510. if (!*s1 && *s2 == '\n' && !s2[1])
  511. return true;
  512. if (*s1 == '\n' && !s1[1] && !*s2)
  513. return true;
  514. return false;
  515. }
  516. EXPORT_SYMBOL(sysfs_streq);
  517. /**
  518. * strtobool - convert common user inputs into boolean values
  519. * @s: input string
  520. * @res: result
  521. *
  522. * This routine returns 0 iff the first character is one of 'Yy1Nn0'.
  523. * Otherwise it will return -EINVAL. Value pointed to by res is
  524. * updated upon finding a match.
  525. */
  526. int strtobool(const char *s, bool *res)
  527. {
  528. switch (s[0]) {
  529. case 'y':
  530. case 'Y':
  531. case '1':
  532. *res = true;
  533. break;
  534. case 'n':
  535. case 'N':
  536. case '0':
  537. *res = false;
  538. break;
  539. default:
  540. return -EINVAL;
  541. }
  542. return 0;
  543. }
  544. EXPORT_SYMBOL(strtobool);
  545. #ifndef __HAVE_ARCH_MEMSET
  546. /**
  547. * memset - Fill a region of memory with the given value
  548. * @s: Pointer to the start of the area.
  549. * @c: The byte to fill the area with
  550. * @count: The size of the area.
  551. *
  552. * Do not use memset() to access IO space, use memset_io() instead.
  553. */
  554. void *memset(void *s, int c, size_t count)
  555. {
  556. char *xs = s;
  557. while (count--)
  558. *xs++ = c;
  559. return s;
  560. }
  561. EXPORT_SYMBOL(memset);
  562. #endif
  563. /**
  564. * memzero_explicit - Fill a region of memory (e.g. sensitive
  565. * keying data) with 0s.
  566. * @s: Pointer to the start of the area.
  567. * @count: The size of the area.
  568. *
  569. * memzero_explicit() doesn't need an arch-specific version as
  570. * it just invokes the one of memset() implicitly.
  571. */
  572. void memzero_explicit(void *s, size_t count)
  573. {
  574. memset(s, 0, count);
  575. OPTIMIZER_HIDE_VAR(s);
  576. }
  577. EXPORT_SYMBOL(memzero_explicit);
  578. #ifndef __HAVE_ARCH_MEMCPY
  579. /**
  580. * memcpy - Copy one area of memory to another
  581. * @dest: Where to copy to
  582. * @src: Where to copy from
  583. * @count: The size of the area.
  584. *
  585. * You should not use this function to access IO space, use memcpy_toio()
  586. * or memcpy_fromio() instead.
  587. */
  588. void *memcpy(void *dest, const void *src, size_t count)
  589. {
  590. char *tmp = dest;
  591. const char *s = src;
  592. while (count--)
  593. *tmp++ = *s++;
  594. return dest;
  595. }
  596. EXPORT_SYMBOL(memcpy);
  597. #endif
  598. #ifndef __HAVE_ARCH_MEMMOVE
  599. /**
  600. * memmove - Copy one area of memory to another
  601. * @dest: Where to copy to
  602. * @src: Where to copy from
  603. * @count: The size of the area.
  604. *
  605. * Unlike memcpy(), memmove() copes with overlapping areas.
  606. */
  607. void *memmove(void *dest, const void *src, size_t count)
  608. {
  609. char *tmp;
  610. const char *s;
  611. if (dest <= src) {
  612. tmp = dest;
  613. s = src;
  614. while (count--)
  615. *tmp++ = *s++;
  616. } else {
  617. tmp = dest;
  618. tmp += count;
  619. s = src;
  620. s += count;
  621. while (count--)
  622. *--tmp = *--s;
  623. }
  624. return dest;
  625. }
  626. EXPORT_SYMBOL(memmove);
  627. #endif
  628. #ifndef __HAVE_ARCH_MEMCMP
  629. /**
  630. * memcmp - Compare two areas of memory
  631. * @cs: One area of memory
  632. * @ct: Another area of memory
  633. * @count: The size of the area.
  634. */
  635. #undef memcmp
  636. int memcmp(const void *cs, const void *ct, size_t count)
  637. {
  638. const unsigned char *su1, *su2;
  639. int res = 0;
  640. for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  641. if ((res = *su1 - *su2) != 0)
  642. break;
  643. return res;
  644. }
  645. EXPORT_SYMBOL(memcmp);
  646. #endif
  647. #ifndef __HAVE_ARCH_MEMSCAN
  648. /**
  649. * memscan - Find a character in an area of memory.
  650. * @addr: The memory area
  651. * @c: The byte to search for
  652. * @size: The size of the area.
  653. *
  654. * returns the address of the first occurrence of @c, or 1 byte past
  655. * the area if @c is not found
  656. */
  657. void *memscan(void *addr, int c, size_t size)
  658. {
  659. unsigned char *p = addr;
  660. while (size) {
  661. if (*p == c)
  662. return (void *)p;
  663. p++;
  664. size--;
  665. }
  666. return (void *)p;
  667. }
  668. EXPORT_SYMBOL(memscan);
  669. #endif
  670. #ifndef __HAVE_ARCH_STRSTR
  671. /**
  672. * strstr - Find the first substring in a %NUL terminated string
  673. * @s1: The string to be searched
  674. * @s2: The string to search for
  675. */
  676. char *strstr(const char *s1, const char *s2)
  677. {
  678. size_t l1, l2;
  679. l2 = strlen(s2);
  680. if (!l2)
  681. return (char *)s1;
  682. l1 = strlen(s1);
  683. while (l1 >= l2) {
  684. l1--;
  685. if (!memcmp(s1, s2, l2))
  686. return (char *)s1;
  687. s1++;
  688. }
  689. return NULL;
  690. }
  691. EXPORT_SYMBOL(strstr);
  692. #endif
  693. #ifndef __HAVE_ARCH_STRNSTR
  694. /**
  695. * strnstr - Find the first substring in a length-limited string
  696. * @s1: The string to be searched
  697. * @s2: The string to search for
  698. * @len: the maximum number of characters to search
  699. */
  700. char *strnstr(const char *s1, const char *s2, size_t len)
  701. {
  702. size_t l2;
  703. l2 = strlen(s2);
  704. if (!l2)
  705. return (char *)s1;
  706. while (len >= l2) {
  707. len--;
  708. if (!memcmp(s1, s2, l2))
  709. return (char *)s1;
  710. s1++;
  711. }
  712. return NULL;
  713. }
  714. EXPORT_SYMBOL(strnstr);
  715. #endif
  716. #ifndef __HAVE_ARCH_MEMCHR
  717. /**
  718. * memchr - Find a character in an area of memory.
  719. * @s: The memory area
  720. * @c: The byte to search for
  721. * @n: The size of the area.
  722. *
  723. * returns the address of the first occurrence of @c, or %NULL
  724. * if @c is not found
  725. */
  726. void *memchr(const void *s, int c, size_t n)
  727. {
  728. const unsigned char *p = s;
  729. while (n-- != 0) {
  730. if ((unsigned char)c == *p++) {
  731. return (void *)(p - 1);
  732. }
  733. }
  734. return NULL;
  735. }
  736. EXPORT_SYMBOL(memchr);
  737. #endif
  738. static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
  739. {
  740. while (bytes) {
  741. if (*start != value)
  742. return (void *)start;
  743. start++;
  744. bytes--;
  745. }
  746. return NULL;
  747. }
  748. /**
  749. * memchr_inv - Find an unmatching character in an area of memory.
  750. * @start: The memory area
  751. * @c: Find a character other than c
  752. * @bytes: The size of the area.
  753. *
  754. * returns the address of the first character other than @c, or %NULL
  755. * if the whole buffer contains just @c.
  756. */
  757. void *memchr_inv(const void *start, int c, size_t bytes)
  758. {
  759. u8 value = c;
  760. u64 value64;
  761. unsigned int words, prefix;
  762. if (bytes <= 16)
  763. return check_bytes8(start, value, bytes);
  764. value64 = value;
  765. #if defined(ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
  766. value64 *= 0x0101010101010101;
  767. #elif defined(ARCH_HAS_FAST_MULTIPLIER)
  768. value64 *= 0x01010101;
  769. value64 |= value64 << 32;
  770. #else
  771. value64 |= value64 << 8;
  772. value64 |= value64 << 16;
  773. value64 |= value64 << 32;
  774. #endif
  775. prefix = (unsigned long)start % 8;
  776. if (prefix) {
  777. u8 *r;
  778. prefix = 8 - prefix;
  779. r = check_bytes8(start, value, prefix);
  780. if (r)
  781. return r;
  782. start += prefix;
  783. bytes -= prefix;
  784. }
  785. words = bytes / 8;
  786. while (words) {
  787. if (*(u64 *)start != value64)
  788. return check_bytes8(start, value, 8);
  789. start += 8;
  790. words--;
  791. }
  792. return check_bytes8(start, value, bytes % 8);
  793. }
  794. EXPORT_SYMBOL(memchr_inv);