string.c 25 KB

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