string.c 26 KB

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