string.c 15 KB

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