string.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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. #include <config.h>
  17. #include <linux/compiler.h>
  18. #include <linux/types.h>
  19. #include <linux/string.h>
  20. #include <linux/ctype.h>
  21. #include <malloc.h>
  22. /**
  23. * strncasecmp - Case insensitive, length-limited string comparison
  24. * @s1: One string
  25. * @s2: The other string
  26. * @len: the maximum number of characters to compare
  27. */
  28. int strncasecmp(const char *s1, const char *s2, size_t len)
  29. {
  30. /* Yes, Virginia, it had better be unsigned */
  31. unsigned char c1, c2;
  32. c1 = 0; c2 = 0;
  33. if (len) {
  34. do {
  35. c1 = *s1; c2 = *s2;
  36. s1++; s2++;
  37. if (!c1)
  38. break;
  39. if (!c2)
  40. break;
  41. if (c1 == c2)
  42. continue;
  43. c1 = tolower(c1);
  44. c2 = tolower(c2);
  45. if (c1 != c2)
  46. break;
  47. } while (--len);
  48. }
  49. return (int)c1 - (int)c2;
  50. }
  51. /**
  52. * strcasecmp - Case insensitive string comparison
  53. * @s1: One string
  54. * @s2: The other string
  55. */
  56. int strcasecmp(const char *s1, const char *s2)
  57. {
  58. return strncasecmp(s1, s2, -1U);
  59. }
  60. char * ___strtok;
  61. #ifndef __HAVE_ARCH_STRCPY
  62. /**
  63. * strcpy - Copy a %NUL terminated string
  64. * @dest: Where to copy the string to
  65. * @src: Where to copy the string from
  66. */
  67. char * strcpy(char * dest,const char *src)
  68. {
  69. char *tmp = dest;
  70. while ((*dest++ = *src++) != '\0')
  71. /* nothing */;
  72. return tmp;
  73. }
  74. #endif
  75. #ifndef __HAVE_ARCH_STRNCPY
  76. /**
  77. * strncpy - Copy a length-limited, %NUL-terminated string
  78. * @dest: Where to copy the string to
  79. * @src: Where to copy the string from
  80. * @count: The maximum number of bytes to copy
  81. *
  82. * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
  83. * However, the result is not %NUL-terminated if the source exceeds
  84. * @count bytes.
  85. */
  86. char * strncpy(char * dest,const char *src,size_t count)
  87. {
  88. char *tmp = dest;
  89. while (count-- && (*dest++ = *src++) != '\0')
  90. /* nothing */;
  91. return tmp;
  92. }
  93. #endif
  94. #ifndef __HAVE_ARCH_STRLCPY
  95. /**
  96. * strlcpy - Copy a C-string into a sized buffer
  97. * @dest: Where to copy the string to
  98. * @src: Where to copy the string from
  99. * @size: size of destination buffer
  100. *
  101. * Compatible with *BSD: the result is always a valid
  102. * NUL-terminated string that fits in the buffer (unless,
  103. * of course, the buffer size is zero). It does not pad
  104. * out the result like strncpy() does.
  105. *
  106. * Return: the number of bytes copied
  107. */
  108. size_t strlcpy(char *dest, const char *src, size_t size)
  109. {
  110. if (size) {
  111. size_t srclen = strlen(src);
  112. size_t len = (srclen >= size) ? size - 1 : srclen;
  113. memcpy(dest, src, len);
  114. dest[len] = '\0';
  115. return len + 1;
  116. }
  117. return 0;
  118. }
  119. #endif
  120. #ifndef __HAVE_ARCH_STRCAT
  121. /**
  122. * strcat - Append one %NUL-terminated string to another
  123. * @dest: The string to be appended to
  124. * @src: The string to append to it
  125. */
  126. char * strcat(char * dest, const char * src)
  127. {
  128. char *tmp = dest;
  129. while (*dest)
  130. dest++;
  131. while ((*dest++ = *src++) != '\0')
  132. ;
  133. return tmp;
  134. }
  135. #endif
  136. #ifndef __HAVE_ARCH_STRNCAT
  137. /**
  138. * strncat - Append a length-limited, %NUL-terminated string to another
  139. * @dest: The string to be appended to
  140. * @src: The string to append to it
  141. * @count: The maximum numbers of bytes to copy
  142. *
  143. * Note that in contrast to strncpy, strncat ensures the result is
  144. * terminated.
  145. */
  146. char * strncat(char *dest, const char *src, size_t count)
  147. {
  148. char *tmp = dest;
  149. if (count) {
  150. while (*dest)
  151. dest++;
  152. while ((*dest++ = *src++)) {
  153. if (--count == 0) {
  154. *dest = '\0';
  155. break;
  156. }
  157. }
  158. }
  159. return tmp;
  160. }
  161. #endif
  162. #ifndef __HAVE_ARCH_STRLCAT
  163. /**
  164. * strlcat - Append a length-limited, %NUL-terminated string to another
  165. * @dest: The string to be appended to
  166. * @src: The string to append to it
  167. * @size: The size of @dest
  168. *
  169. * Compatible with *BSD: the result is always a valid NUL-terminated string that
  170. * fits in the buffer (unless, of course, the buffer size is zero). It does not
  171. * write past @size like strncat() does.
  172. */
  173. size_t strlcat(char *dest, const char *src, size_t size)
  174. {
  175. size_t len = strnlen(dest, size);
  176. return len + strlcpy(dest + len, src, size - len);
  177. }
  178. #endif
  179. #ifndef __HAVE_ARCH_STRCMP
  180. /**
  181. * strcmp - Compare two strings
  182. * @cs: One string
  183. * @ct: Another string
  184. */
  185. int strcmp(const char *cs, const char *ct)
  186. {
  187. int ret;
  188. while (1) {
  189. unsigned char a = *cs++;
  190. unsigned char b = *ct++;
  191. ret = a - b;
  192. if (ret || !b)
  193. break;
  194. }
  195. return ret;
  196. }
  197. #endif
  198. #ifndef __HAVE_ARCH_STRNCMP
  199. /**
  200. * strncmp - Compare two length-limited strings
  201. * @cs: One string
  202. * @ct: Another string
  203. * @count: The maximum number of bytes to compare
  204. */
  205. int strncmp(const char *cs, const char *ct, size_t count)
  206. {
  207. int ret = 0;
  208. while (count--) {
  209. unsigned char a = *cs++;
  210. unsigned char b = *ct++;
  211. ret = a - b;
  212. if (ret || !b)
  213. break;
  214. }
  215. return ret;
  216. }
  217. #endif
  218. #ifndef __HAVE_ARCH_STRCHR
  219. /**
  220. * strchr - Find the first occurrence of a character in a string
  221. * @s: The string to be searched
  222. * @c: The character to search for
  223. */
  224. char * strchr(const char * s, int c)
  225. {
  226. for(; *s != (char) c; ++s)
  227. if (*s == '\0')
  228. return NULL;
  229. return (char *) s;
  230. }
  231. #endif
  232. const char *strchrnul(const char *s, int c)
  233. {
  234. for (; *s != (char)c; ++s)
  235. if (*s == '\0')
  236. break;
  237. return s;
  238. }
  239. #ifndef __HAVE_ARCH_STRRCHR
  240. /**
  241. * strrchr - Find the last occurrence of a character in a string
  242. * @s: The string to be searched
  243. * @c: The character to search for
  244. */
  245. char * strrchr(const char * s, int c)
  246. {
  247. const char *p = s + strlen(s);
  248. do {
  249. if (*p == (char)c)
  250. return (char *)p;
  251. } while (--p >= s);
  252. return NULL;
  253. }
  254. #endif
  255. #ifndef __HAVE_ARCH_STRLEN
  256. /**
  257. * strlen - Find the length of a string
  258. * @s: The string to be sized
  259. */
  260. size_t strlen(const char * s)
  261. {
  262. const char *sc;
  263. for (sc = s; *sc != '\0'; ++sc)
  264. /* nothing */;
  265. return sc - s;
  266. }
  267. #endif
  268. #ifndef __HAVE_ARCH_STRNLEN
  269. /**
  270. * strnlen - Find the length of a length-limited string
  271. * @s: The string to be sized
  272. * @count: The maximum number of bytes to search
  273. */
  274. size_t strnlen(const char * s, size_t count)
  275. {
  276. const char *sc;
  277. for (sc = s; count-- && *sc != '\0'; ++sc)
  278. /* nothing */;
  279. return sc - s;
  280. }
  281. #endif
  282. #ifndef __HAVE_ARCH_STRCSPN
  283. /**
  284. * strcspn - Calculate the length of the initial substring of @s which does
  285. * not contain letters in @reject
  286. * @s: The string to be searched
  287. * @reject: The string to avoid
  288. */
  289. size_t strcspn(const char *s, const char *reject)
  290. {
  291. const char *p;
  292. const char *r;
  293. size_t count = 0;
  294. for (p = s; *p != '\0'; ++p) {
  295. for (r = reject; *r != '\0'; ++r) {
  296. if (*p == *r)
  297. return count;
  298. }
  299. ++count;
  300. }
  301. return count;
  302. }
  303. #endif
  304. #ifndef __HAVE_ARCH_STRDUP
  305. char * strdup(const char *s)
  306. {
  307. char *new;
  308. if ((s == NULL) ||
  309. ((new = malloc (strlen(s) + 1)) == NULL) ) {
  310. return NULL;
  311. }
  312. strcpy (new, s);
  313. return new;
  314. }
  315. char * strndup(const char *s, size_t n)
  316. {
  317. size_t len;
  318. char *new;
  319. if (s == NULL)
  320. return NULL;
  321. len = strlen(s);
  322. if (n < len)
  323. len = n;
  324. new = malloc(len + 1);
  325. if (new == NULL)
  326. return NULL;
  327. strncpy(new, s, len);
  328. new[len] = '\0';
  329. return new;
  330. }
  331. #endif
  332. #ifndef __HAVE_ARCH_STRSPN
  333. /**
  334. * strspn - Calculate the length of the initial substring of @s which only
  335. * contain letters in @accept
  336. * @s: The string to be searched
  337. * @accept: The string to search for
  338. */
  339. size_t strspn(const char *s, const char *accept)
  340. {
  341. const char *p;
  342. const char *a;
  343. size_t count = 0;
  344. for (p = s; *p != '\0'; ++p) {
  345. for (a = accept; *a != '\0'; ++a) {
  346. if (*p == *a)
  347. break;
  348. }
  349. if (*a == '\0')
  350. return count;
  351. ++count;
  352. }
  353. return count;
  354. }
  355. #endif
  356. #ifndef __HAVE_ARCH_STRPBRK
  357. /**
  358. * strpbrk - Find the first occurrence of a set of characters
  359. * @cs: The string to be searched
  360. * @ct: The characters to search for
  361. */
  362. char * strpbrk(const char * cs,const char * ct)
  363. {
  364. const char *sc1,*sc2;
  365. for( sc1 = cs; *sc1 != '\0'; ++sc1) {
  366. for( sc2 = ct; *sc2 != '\0'; ++sc2) {
  367. if (*sc1 == *sc2)
  368. return (char *) sc1;
  369. }
  370. }
  371. return NULL;
  372. }
  373. #endif
  374. #ifndef __HAVE_ARCH_STRTOK
  375. /**
  376. * strtok - Split a string into tokens
  377. * @s: The string to be searched
  378. * @ct: The characters to search for
  379. *
  380. * WARNING: strtok is deprecated, use strsep instead.
  381. */
  382. char * strtok(char * s,const char * ct)
  383. {
  384. char *sbegin, *send;
  385. sbegin = s ? s : ___strtok;
  386. if (!sbegin) {
  387. return NULL;
  388. }
  389. sbegin += strspn(sbegin,ct);
  390. if (*sbegin == '\0') {
  391. ___strtok = NULL;
  392. return( NULL );
  393. }
  394. send = strpbrk( sbegin, ct);
  395. if (send && *send != '\0')
  396. *send++ = '\0';
  397. ___strtok = send;
  398. return (sbegin);
  399. }
  400. #endif
  401. #ifndef __HAVE_ARCH_STRSEP
  402. /**
  403. * strsep - Split a string into tokens
  404. * @s: The string to be searched
  405. * @ct: The characters to search for
  406. *
  407. * strsep() updates @s to point after the token, ready for the next call.
  408. *
  409. * It returns empty tokens, too, behaving exactly like the libc function
  410. * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
  411. * Same semantics, slimmer shape. ;)
  412. */
  413. char * strsep(char **s, const char *ct)
  414. {
  415. char *sbegin = *s, *end;
  416. if (sbegin == NULL)
  417. return NULL;
  418. end = strpbrk(sbegin, ct);
  419. if (end)
  420. *end++ = '\0';
  421. *s = end;
  422. return sbegin;
  423. }
  424. #endif
  425. #ifndef __HAVE_ARCH_STRSWAB
  426. /**
  427. * strswab - swap adjacent even and odd bytes in %NUL-terminated string
  428. * s: address of the string
  429. *
  430. * returns the address of the swapped string or NULL on error. If
  431. * string length is odd, last byte is untouched.
  432. */
  433. char *strswab(const char *s)
  434. {
  435. char *p, *q;
  436. if ((NULL == s) || ('\0' == *s)) {
  437. return (NULL);
  438. }
  439. for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
  440. char tmp;
  441. tmp = *p;
  442. *p = *q;
  443. *q = tmp;
  444. }
  445. return (char *) s;
  446. }
  447. #endif
  448. #ifndef __HAVE_ARCH_MEMSET
  449. /**
  450. * memset - Fill a region of memory with the given value
  451. * @s: Pointer to the start of the area.
  452. * @c: The byte to fill the area with
  453. * @count: The size of the area.
  454. *
  455. * Do not use memset() to access IO space, use memset_io() instead.
  456. */
  457. __used void * memset(void * s,int c,size_t count)
  458. {
  459. unsigned long *sl = (unsigned long *) s;
  460. char *s8;
  461. #if !CONFIG_IS_ENABLED(TINY_MEMSET)
  462. unsigned long cl = 0;
  463. int i;
  464. /* do it one word at a time (32 bits or 64 bits) while possible */
  465. if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
  466. for (i = 0; i < sizeof(*sl); i++) {
  467. cl <<= 8;
  468. cl |= c & 0xff;
  469. }
  470. while (count >= sizeof(*sl)) {
  471. *sl++ = cl;
  472. count -= sizeof(*sl);
  473. }
  474. }
  475. #endif /* fill 8 bits at a time */
  476. s8 = (char *)sl;
  477. while (count--)
  478. *s8++ = c;
  479. return s;
  480. }
  481. #endif
  482. #ifndef __HAVE_ARCH_MEMCPY
  483. /**
  484. * memcpy - Copy one area of memory to another
  485. * @dest: Where to copy to
  486. * @src: Where to copy from
  487. * @count: The size of the area.
  488. *
  489. * You should not use this function to access IO space, use memcpy_toio()
  490. * or memcpy_fromio() instead.
  491. */
  492. __used void * memcpy(void *dest, const void *src, size_t count)
  493. {
  494. unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
  495. char *d8, *s8;
  496. if (src == dest)
  497. return dest;
  498. /* while all data is aligned (common case), copy a word at a time */
  499. if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
  500. while (count >= sizeof(*dl)) {
  501. *dl++ = *sl++;
  502. count -= sizeof(*dl);
  503. }
  504. }
  505. /* copy the reset one byte at a time */
  506. d8 = (char *)dl;
  507. s8 = (char *)sl;
  508. while (count--)
  509. *d8++ = *s8++;
  510. return dest;
  511. }
  512. #endif
  513. #ifndef __HAVE_ARCH_MEMMOVE
  514. /**
  515. * memmove - Copy one area of memory to another
  516. * @dest: Where to copy to
  517. * @src: Where to copy from
  518. * @count: The size of the area.
  519. *
  520. * Unlike memcpy(), memmove() copes with overlapping areas.
  521. */
  522. __used void * memmove(void * dest,const void *src,size_t count)
  523. {
  524. char *tmp, *s;
  525. if (dest <= src || (src + count) <= dest) {
  526. /*
  527. * Use the fast memcpy implementation (ARCH optimized or lib/string.c) when it is possible:
  528. * - when dest is before src (assuming that memcpy is doing forward-copying)
  529. * - when destination don't overlap the source buffer (src + count <= dest)
  530. *
  531. * WARNING: the first optimisation cause an issue, when __HAVE_ARCH_MEMCPY is defined,
  532. * __HAVE_ARCH_MEMMOVE is not defined and if the memcpy ARCH-specific
  533. * implementation is not doing a forward-copying.
  534. *
  535. * No issue today because memcpy is doing a forward-copying in lib/string.c and for ARM32
  536. * architecture; no other arches use __HAVE_ARCH_MEMCPY without __HAVE_ARCH_MEMMOVE.
  537. */
  538. memcpy(dest, src, count);
  539. } else {
  540. tmp = (char *) dest + count;
  541. s = (char *) src + count;
  542. while (count--)
  543. *--tmp = *--s;
  544. }
  545. return dest;
  546. }
  547. #endif
  548. #ifndef __HAVE_ARCH_MEMCMP
  549. /**
  550. * memcmp - Compare two areas of memory
  551. * @cs: One area of memory
  552. * @ct: Another area of memory
  553. * @count: The size of the area.
  554. */
  555. __used int memcmp(const void * cs,const void * ct,size_t count)
  556. {
  557. const unsigned char *su1, *su2;
  558. int res = 0;
  559. for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  560. if ((res = *su1 - *su2) != 0)
  561. break;
  562. return res;
  563. }
  564. #endif
  565. #ifndef __HAVE_ARCH_MEMSCAN
  566. /**
  567. * memscan - Find a character in an area of memory.
  568. * @addr: The memory area
  569. * @c: The byte to search for
  570. * @size: The size of the area.
  571. *
  572. * returns the address of the first occurrence of @c, or 1 byte past
  573. * the area if @c is not found
  574. */
  575. void * memscan(void * addr, int c, size_t size)
  576. {
  577. unsigned char * p = (unsigned char *) addr;
  578. while (size) {
  579. if (*p == c)
  580. return (void *) p;
  581. p++;
  582. size--;
  583. }
  584. return (void *) p;
  585. }
  586. #endif
  587. char *memdup(const void *src, size_t len)
  588. {
  589. char *p;
  590. p = malloc(len);
  591. if (!p)
  592. return NULL;
  593. memcpy(p, src, len);
  594. return p;
  595. }
  596. #ifndef __HAVE_ARCH_STRSTR
  597. /**
  598. * strstr - Find the first substring in a %NUL terminated string
  599. * @s1: The string to be searched
  600. * @s2: The string to search for
  601. */
  602. char * strstr(const char * s1,const char * s2)
  603. {
  604. int l1, l2;
  605. l2 = strlen(s2);
  606. if (!l2)
  607. return (char *) s1;
  608. l1 = strlen(s1);
  609. while (l1 >= l2) {
  610. l1--;
  611. if (!memcmp(s1,s2,l2))
  612. return (char *) s1;
  613. s1++;
  614. }
  615. return NULL;
  616. }
  617. #endif
  618. #ifndef __HAVE_ARCH_MEMCHR
  619. /**
  620. * memchr - Find a character in an area of memory.
  621. * @s: The memory area
  622. * @c: The byte to search for
  623. * @n: The size of the area.
  624. *
  625. * returns the address of the first occurrence of @c, or %NULL
  626. * if @c is not found
  627. */
  628. void *memchr(const void *s, int c, size_t n)
  629. {
  630. const unsigned char *p = s;
  631. while (n-- != 0) {
  632. if ((unsigned char)c == *p++) {
  633. return (void *)(p-1);
  634. }
  635. }
  636. return NULL;
  637. }
  638. #endif
  639. #ifndef __HAVE_ARCH_MEMCHR_INV
  640. static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
  641. {
  642. while (bytes) {
  643. if (*start != value)
  644. return (void *)start;
  645. start++;
  646. bytes--;
  647. }
  648. return NULL;
  649. }
  650. /**
  651. * memchr_inv - Find an unmatching character in an area of memory.
  652. * @start: The memory area
  653. * @c: Find a character other than c
  654. * @bytes: The size of the area.
  655. *
  656. * returns the address of the first character other than @c, or %NULL
  657. * if the whole buffer contains just @c.
  658. */
  659. void *memchr_inv(const void *start, int c, size_t bytes)
  660. {
  661. u8 value = c;
  662. u64 value64;
  663. unsigned int words, prefix;
  664. if (bytes <= 16)
  665. return check_bytes8(start, value, bytes);
  666. value64 = value;
  667. value64 |= value64 << 8;
  668. value64 |= value64 << 16;
  669. value64 |= value64 << 32;
  670. prefix = (unsigned long)start % 8;
  671. if (prefix) {
  672. u8 *r;
  673. prefix = 8 - prefix;
  674. r = check_bytes8(start, value, prefix);
  675. if (r)
  676. return r;
  677. start += prefix;
  678. bytes -= prefix;
  679. }
  680. words = bytes / 8;
  681. while (words) {
  682. if (*(u64 *)start != value64)
  683. return check_bytes8(start, value, 8);
  684. start += 8;
  685. words--;
  686. }
  687. return check_bytes8(start, value, bytes % 8);
  688. }
  689. #endif