string.c 14 KB

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