string.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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. register signed char __res;
  188. while (1) {
  189. if ((__res = *cs - *ct++) != 0 || !*cs++)
  190. break;
  191. }
  192. return __res;
  193. }
  194. #endif
  195. #ifndef __HAVE_ARCH_STRNCMP
  196. /**
  197. * strncmp - Compare two length-limited strings
  198. * @cs: One string
  199. * @ct: Another string
  200. * @count: The maximum number of bytes to compare
  201. */
  202. int strncmp(const char * cs,const char * ct,size_t count)
  203. {
  204. register signed char __res = 0;
  205. while (count) {
  206. if ((__res = *cs - *ct++) != 0 || !*cs++)
  207. break;
  208. count--;
  209. }
  210. return __res;
  211. }
  212. #endif
  213. #ifndef __HAVE_ARCH_STRCHR
  214. /**
  215. * strchr - Find the first occurrence of a character in a string
  216. * @s: The string to be searched
  217. * @c: The character to search for
  218. */
  219. char * strchr(const char * s, int c)
  220. {
  221. for(; *s != (char) c; ++s)
  222. if (*s == '\0')
  223. return NULL;
  224. return (char *) s;
  225. }
  226. #endif
  227. const char *strchrnul(const char *s, int c)
  228. {
  229. for (; *s != (char)c; ++s)
  230. if (*s == '\0')
  231. break;
  232. return s;
  233. }
  234. #ifndef __HAVE_ARCH_STRRCHR
  235. /**
  236. * strrchr - Find the last occurrence of a character in a string
  237. * @s: The string to be searched
  238. * @c: The character to search for
  239. */
  240. char * strrchr(const char * s, int c)
  241. {
  242. const char *p = s + strlen(s);
  243. do {
  244. if (*p == (char)c)
  245. return (char *)p;
  246. } while (--p >= s);
  247. return NULL;
  248. }
  249. #endif
  250. #ifndef __HAVE_ARCH_STRLEN
  251. /**
  252. * strlen - Find the length of a string
  253. * @s: The string to be sized
  254. */
  255. size_t strlen(const char * s)
  256. {
  257. const char *sc;
  258. for (sc = s; *sc != '\0'; ++sc)
  259. /* nothing */;
  260. return sc - s;
  261. }
  262. #endif
  263. #ifndef __HAVE_ARCH_STRNLEN
  264. /**
  265. * strnlen - Find the length of a length-limited string
  266. * @s: The string to be sized
  267. * @count: The maximum number of bytes to search
  268. */
  269. size_t strnlen(const char * s, size_t count)
  270. {
  271. const char *sc;
  272. for (sc = s; count-- && *sc != '\0'; ++sc)
  273. /* nothing */;
  274. return sc - s;
  275. }
  276. #endif
  277. #ifndef __HAVE_ARCH_STRCSPN
  278. /**
  279. * strcspn - Calculate the length of the initial substring of @s which does
  280. * not contain letters in @reject
  281. * @s: The string to be searched
  282. * @reject: The string to avoid
  283. */
  284. size_t strcspn(const char *s, const char *reject)
  285. {
  286. const char *p;
  287. const char *r;
  288. size_t count = 0;
  289. for (p = s; *p != '\0'; ++p) {
  290. for (r = reject; *r != '\0'; ++r) {
  291. if (*p == *r)
  292. return count;
  293. }
  294. ++count;
  295. }
  296. return count;
  297. }
  298. #endif
  299. #ifndef __HAVE_ARCH_STRDUP
  300. char * strdup(const char *s)
  301. {
  302. char *new;
  303. if ((s == NULL) ||
  304. ((new = malloc (strlen(s) + 1)) == NULL) ) {
  305. return NULL;
  306. }
  307. strcpy (new, s);
  308. return new;
  309. }
  310. char * strndup(const char *s, size_t n)
  311. {
  312. size_t len;
  313. char *new;
  314. if (s == NULL)
  315. return NULL;
  316. len = strlen(s);
  317. if (n < len)
  318. len = n;
  319. new = malloc(len + 1);
  320. if (new == NULL)
  321. return NULL;
  322. strncpy(new, s, len);
  323. new[len] = '\0';
  324. return new;
  325. }
  326. #endif
  327. #ifndef __HAVE_ARCH_STRSPN
  328. /**
  329. * strspn - Calculate the length of the initial substring of @s which only
  330. * contain letters in @accept
  331. * @s: The string to be searched
  332. * @accept: The string to search for
  333. */
  334. size_t strspn(const char *s, const char *accept)
  335. {
  336. const char *p;
  337. const char *a;
  338. size_t count = 0;
  339. for (p = s; *p != '\0'; ++p) {
  340. for (a = accept; *a != '\0'; ++a) {
  341. if (*p == *a)
  342. break;
  343. }
  344. if (*a == '\0')
  345. return count;
  346. ++count;
  347. }
  348. return count;
  349. }
  350. #endif
  351. #ifndef __HAVE_ARCH_STRPBRK
  352. /**
  353. * strpbrk - Find the first occurrence of a set of characters
  354. * @cs: The string to be searched
  355. * @ct: The characters to search for
  356. */
  357. char * strpbrk(const char * cs,const char * ct)
  358. {
  359. const char *sc1,*sc2;
  360. for( sc1 = cs; *sc1 != '\0'; ++sc1) {
  361. for( sc2 = ct; *sc2 != '\0'; ++sc2) {
  362. if (*sc1 == *sc2)
  363. return (char *) sc1;
  364. }
  365. }
  366. return NULL;
  367. }
  368. #endif
  369. #ifndef __HAVE_ARCH_STRTOK
  370. /**
  371. * strtok - Split a string into tokens
  372. * @s: The string to be searched
  373. * @ct: The characters to search for
  374. *
  375. * WARNING: strtok is deprecated, use strsep instead.
  376. */
  377. char * strtok(char * s,const char * ct)
  378. {
  379. char *sbegin, *send;
  380. sbegin = s ? s : ___strtok;
  381. if (!sbegin) {
  382. return NULL;
  383. }
  384. sbegin += strspn(sbegin,ct);
  385. if (*sbegin == '\0') {
  386. ___strtok = NULL;
  387. return( NULL );
  388. }
  389. send = strpbrk( sbegin, ct);
  390. if (send && *send != '\0')
  391. *send++ = '\0';
  392. ___strtok = send;
  393. return (sbegin);
  394. }
  395. #endif
  396. #ifndef __HAVE_ARCH_STRSEP
  397. /**
  398. * strsep - Split a string into tokens
  399. * @s: The string to be searched
  400. * @ct: The characters to search for
  401. *
  402. * strsep() updates @s to point after the token, ready for the next call.
  403. *
  404. * It returns empty tokens, too, behaving exactly like the libc function
  405. * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
  406. * Same semantics, slimmer shape. ;)
  407. */
  408. char * strsep(char **s, const char *ct)
  409. {
  410. char *sbegin = *s, *end;
  411. if (sbegin == NULL)
  412. return NULL;
  413. end = strpbrk(sbegin, ct);
  414. if (end)
  415. *end++ = '\0';
  416. *s = end;
  417. return sbegin;
  418. }
  419. #endif
  420. #ifndef __HAVE_ARCH_STRSWAB
  421. /**
  422. * strswab - swap adjacent even and odd bytes in %NUL-terminated string
  423. * s: address of the string
  424. *
  425. * returns the address of the swapped string or NULL on error. If
  426. * string length is odd, last byte is untouched.
  427. */
  428. char *strswab(const char *s)
  429. {
  430. char *p, *q;
  431. if ((NULL == s) || ('\0' == *s)) {
  432. return (NULL);
  433. }
  434. for (p=(char *)s, q=p+1; (*p != '\0') && (*q != '\0'); p+=2, q+=2) {
  435. char tmp;
  436. tmp = *p;
  437. *p = *q;
  438. *q = tmp;
  439. }
  440. return (char *) s;
  441. }
  442. #endif
  443. #ifndef __HAVE_ARCH_MEMSET
  444. /**
  445. * memset - Fill a region of memory with the given value
  446. * @s: Pointer to the start of the area.
  447. * @c: The byte to fill the area with
  448. * @count: The size of the area.
  449. *
  450. * Do not use memset() to access IO space, use memset_io() instead.
  451. */
  452. __used void * memset(void * s,int c,size_t count)
  453. {
  454. unsigned long *sl = (unsigned long *) s;
  455. char *s8;
  456. #if !CONFIG_IS_ENABLED(TINY_MEMSET)
  457. unsigned long cl = 0;
  458. int i;
  459. /* do it one word at a time (32 bits or 64 bits) while possible */
  460. if ( ((ulong)s & (sizeof(*sl) - 1)) == 0) {
  461. for (i = 0; i < sizeof(*sl); i++) {
  462. cl <<= 8;
  463. cl |= c & 0xff;
  464. }
  465. while (count >= sizeof(*sl)) {
  466. *sl++ = cl;
  467. count -= sizeof(*sl);
  468. }
  469. }
  470. #endif /* fill 8 bits at a time */
  471. s8 = (char *)sl;
  472. while (count--)
  473. *s8++ = c;
  474. return s;
  475. }
  476. #endif
  477. #ifndef __HAVE_ARCH_MEMCPY
  478. /**
  479. * memcpy - Copy one area of memory to another
  480. * @dest: Where to copy to
  481. * @src: Where to copy from
  482. * @count: The size of the area.
  483. *
  484. * You should not use this function to access IO space, use memcpy_toio()
  485. * or memcpy_fromio() instead.
  486. */
  487. __used void * memcpy(void *dest, const void *src, size_t count)
  488. {
  489. unsigned long *dl = (unsigned long *)dest, *sl = (unsigned long *)src;
  490. char *d8, *s8;
  491. if (src == dest)
  492. return dest;
  493. /* while all data is aligned (common case), copy a word at a time */
  494. if ( (((ulong)dest | (ulong)src) & (sizeof(*dl) - 1)) == 0) {
  495. while (count >= sizeof(*dl)) {
  496. *dl++ = *sl++;
  497. count -= sizeof(*dl);
  498. }
  499. }
  500. /* copy the reset one byte at a time */
  501. d8 = (char *)dl;
  502. s8 = (char *)sl;
  503. while (count--)
  504. *d8++ = *s8++;
  505. return dest;
  506. }
  507. #endif
  508. #ifndef __HAVE_ARCH_MEMMOVE
  509. /**
  510. * memmove - Copy one area of memory to another
  511. * @dest: Where to copy to
  512. * @src: Where to copy from
  513. * @count: The size of the area.
  514. *
  515. * Unlike memcpy(), memmove() copes with overlapping areas.
  516. */
  517. __used void * memmove(void * dest,const void *src,size_t count)
  518. {
  519. char *tmp, *s;
  520. if (dest <= src || (src + count) <= dest) {
  521. /*
  522. * Use the fast memcpy implementation (ARCH optimized or lib/string.c) when it is possible:
  523. * - when dest is before src (assuming that memcpy is doing forward-copying)
  524. * - when destination don't overlap the source buffer (src + count <= dest)
  525. *
  526. * WARNING: the first optimisation cause an issue, when __HAVE_ARCH_MEMCPY is defined,
  527. * __HAVE_ARCH_MEMMOVE is not defined and if the memcpy ARCH-specific
  528. * implementation is not doing a forward-copying.
  529. *
  530. * No issue today because memcpy is doing a forward-copying in lib/string.c and for ARM32
  531. * architecture; no other arches use __HAVE_ARCH_MEMCPY without __HAVE_ARCH_MEMMOVE.
  532. */
  533. memcpy(dest, src, count);
  534. } else {
  535. tmp = (char *) dest + count;
  536. s = (char *) src + count;
  537. while (count--)
  538. *--tmp = *--s;
  539. }
  540. return dest;
  541. }
  542. #endif
  543. #ifndef __HAVE_ARCH_MEMCMP
  544. /**
  545. * memcmp - Compare two areas of memory
  546. * @cs: One area of memory
  547. * @ct: Another area of memory
  548. * @count: The size of the area.
  549. */
  550. __used int memcmp(const void * cs,const void * ct,size_t count)
  551. {
  552. const unsigned char *su1, *su2;
  553. int res = 0;
  554. for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  555. if ((res = *su1 - *su2) != 0)
  556. break;
  557. return res;
  558. }
  559. #endif
  560. #ifndef __HAVE_ARCH_MEMSCAN
  561. /**
  562. * memscan - Find a character in an area of memory.
  563. * @addr: The memory area
  564. * @c: The byte to search for
  565. * @size: The size of the area.
  566. *
  567. * returns the address of the first occurrence of @c, or 1 byte past
  568. * the area if @c is not found
  569. */
  570. void * memscan(void * addr, int c, size_t size)
  571. {
  572. unsigned char * p = (unsigned char *) addr;
  573. while (size) {
  574. if (*p == c)
  575. return (void *) p;
  576. p++;
  577. size--;
  578. }
  579. return (void *) p;
  580. }
  581. #endif
  582. #ifndef __HAVE_ARCH_STRSTR
  583. /**
  584. * strstr - Find the first substring in a %NUL terminated string
  585. * @s1: The string to be searched
  586. * @s2: The string to search for
  587. */
  588. char * strstr(const char * s1,const char * s2)
  589. {
  590. int l1, l2;
  591. l2 = strlen(s2);
  592. if (!l2)
  593. return (char *) s1;
  594. l1 = strlen(s1);
  595. while (l1 >= l2) {
  596. l1--;
  597. if (!memcmp(s1,s2,l2))
  598. return (char *) s1;
  599. s1++;
  600. }
  601. return NULL;
  602. }
  603. #endif
  604. #ifndef __HAVE_ARCH_MEMCHR
  605. /**
  606. * memchr - Find a character in an area of memory.
  607. * @s: The memory area
  608. * @c: The byte to search for
  609. * @n: The size of the area.
  610. *
  611. * returns the address of the first occurrence of @c, or %NULL
  612. * if @c is not found
  613. */
  614. void *memchr(const void *s, int c, size_t n)
  615. {
  616. const unsigned char *p = s;
  617. while (n-- != 0) {
  618. if ((unsigned char)c == *p++) {
  619. return (void *)(p-1);
  620. }
  621. }
  622. return NULL;
  623. }
  624. #endif
  625. #ifndef __HAVE_ARCH_MEMCHR_INV
  626. static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
  627. {
  628. while (bytes) {
  629. if (*start != value)
  630. return (void *)start;
  631. start++;
  632. bytes--;
  633. }
  634. return NULL;
  635. }
  636. /**
  637. * memchr_inv - Find an unmatching character in an area of memory.
  638. * @start: The memory area
  639. * @c: Find a character other than c
  640. * @bytes: The size of the area.
  641. *
  642. * returns the address of the first character other than @c, or %NULL
  643. * if the whole buffer contains just @c.
  644. */
  645. void *memchr_inv(const void *start, int c, size_t bytes)
  646. {
  647. u8 value = c;
  648. u64 value64;
  649. unsigned int words, prefix;
  650. if (bytes <= 16)
  651. return check_bytes8(start, value, bytes);
  652. value64 = value;
  653. value64 |= value64 << 8;
  654. value64 |= value64 << 16;
  655. value64 |= value64 << 32;
  656. prefix = (unsigned long)start % 8;
  657. if (prefix) {
  658. u8 *r;
  659. prefix = 8 - prefix;
  660. r = check_bytes8(start, value, prefix);
  661. if (r)
  662. return r;
  663. start += prefix;
  664. bytes -= prefix;
  665. }
  666. words = bytes / 8;
  667. while (words) {
  668. if (*(u64 *)start != value64)
  669. return check_bytes8(start, value, 8);
  670. start += 8;
  671. words--;
  672. }
  673. return check_bytes8(start, value, bytes % 8);
  674. }
  675. #endif