string.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Brian Gianforcaro <bgianf@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Format.h>
  8. #include <AK/MemMem.h>
  9. #include <AK/Memory.h>
  10. #include <AK/Platform.h>
  11. #include <AK/StdLibExtras.h>
  12. #include <AK/Types.h>
  13. #include <assert.h>
  14. #include <ctype.h>
  15. #include <errno.h>
  16. #include <signal.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. extern "C" {
  21. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strspn.html
  22. size_t strspn(char const* s, char const* accept)
  23. {
  24. char const* p = s;
  25. cont:
  26. char ch = *p++;
  27. char ac;
  28. for (char const* ap = accept; (ac = *ap++) != '\0';) {
  29. if (ac == ch)
  30. goto cont;
  31. }
  32. return p - 1 - s;
  33. }
  34. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strcspn.html
  35. size_t strcspn(char const* s, char const* reject)
  36. {
  37. for (auto* p = s;;) {
  38. char c = *p++;
  39. auto* rp = reject;
  40. char rc;
  41. do {
  42. if ((rc = *rp++) == c)
  43. return p - 1 - s;
  44. } while (rc);
  45. }
  46. }
  47. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strlen.html
  48. size_t strlen(char const* str)
  49. {
  50. size_t len = 0;
  51. while (*(str++))
  52. ++len;
  53. return len;
  54. }
  55. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strnlen.html
  56. size_t strnlen(char const* str, size_t maxlen)
  57. {
  58. size_t len = 0;
  59. for (; len < maxlen && *str; str++)
  60. len++;
  61. return len;
  62. }
  63. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strdup.html
  64. char* strdup(char const* str)
  65. {
  66. size_t len = strlen(str);
  67. char* new_str = (char*)malloc(len + 1);
  68. memcpy(new_str, str, len);
  69. new_str[len] = '\0';
  70. return new_str;
  71. }
  72. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strndup.html
  73. char* strndup(char const* str, size_t maxlen)
  74. {
  75. size_t len = strnlen(str, maxlen);
  76. char* new_str = (char*)malloc(len + 1);
  77. memcpy(new_str, str, len);
  78. new_str[len] = 0;
  79. return new_str;
  80. }
  81. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strcmp.html
  82. int strcmp(char const* s1, char const* s2)
  83. {
  84. while (*s1 == *s2++)
  85. if (*s1++ == 0)
  86. return 0;
  87. return *(unsigned char const*)s1 - *(unsigned char const*)--s2;
  88. }
  89. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strncmp.html
  90. int strncmp(char const* s1, char const* s2, size_t n)
  91. {
  92. if (!n)
  93. return 0;
  94. do {
  95. if (*s1 != *s2++)
  96. return *(unsigned char const*)s1 - *(unsigned char const*)--s2;
  97. if (*s1++ == 0)
  98. break;
  99. } while (--n);
  100. return 0;
  101. }
  102. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/memcmp.html
  103. int memcmp(void const* v1, void const* v2, size_t n)
  104. {
  105. auto* s1 = (uint8_t const*)v1;
  106. auto* s2 = (uint8_t const*)v2;
  107. while (n-- > 0) {
  108. if (*s1++ != *s2++)
  109. return s1[-1] < s2[-1] ? -1 : 1;
  110. }
  111. return 0;
  112. }
  113. int timingsafe_memcmp(void const* b1, void const* b2, size_t len)
  114. {
  115. return AK::timing_safe_compare(b1, b2, len) ? 1 : 0;
  116. }
  117. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/memcpy.html
  118. void* memcpy(void* dest_ptr, void const* src_ptr, size_t n)
  119. {
  120. void* original_dest = dest_ptr;
  121. asm volatile(
  122. "rep movsb"
  123. : "+D"(dest_ptr), "+S"(src_ptr), "+c"(n)::"memory");
  124. return original_dest;
  125. }
  126. #if ARCH(I386)
  127. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/memset.html
  128. //
  129. // For x86-64, an optimized ASM implementation is found in ./arch/x86_64/memset.S
  130. void* memset(void* dest_ptr, int c, size_t n)
  131. {
  132. size_t dest = (size_t)dest_ptr;
  133. // FIXME: Support starting at an unaligned address.
  134. if (!(dest & 0x3) && n >= 12) {
  135. size_t size_ts = n / sizeof(size_t);
  136. size_t expanded_c = explode_byte((u8)c);
  137. asm volatile(
  138. "rep stosl\n"
  139. : "=D"(dest)
  140. : "D"(dest), "c"(size_ts), "a"(expanded_c)
  141. : "memory");
  142. n -= size_ts * sizeof(size_t);
  143. if (n == 0)
  144. return dest_ptr;
  145. }
  146. asm volatile(
  147. "rep stosb\n"
  148. : "=D"(dest), "=c"(n)
  149. : "0"(dest), "1"(n), "a"(c)
  150. : "memory");
  151. return dest_ptr;
  152. }
  153. #endif
  154. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/memmove.html
  155. void* memmove(void* dest, void const* src, size_t n)
  156. {
  157. if (((FlatPtr)dest - (FlatPtr)src) >= n)
  158. return memcpy(dest, src, n);
  159. u8* pd = (u8*)dest;
  160. u8 const* ps = (u8 const*)src;
  161. for (pd += n, ps += n; n--;)
  162. *--pd = *--ps;
  163. return dest;
  164. }
  165. void const* memmem(void const* haystack, size_t haystack_length, void const* needle, size_t needle_length)
  166. {
  167. return AK::memmem(haystack, haystack_length, needle, needle_length);
  168. }
  169. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strcpy.html
  170. char* strcpy(char* dest, char const* src)
  171. {
  172. char* original_dest = dest;
  173. while ((*dest++ = *src++) != '\0')
  174. ;
  175. return original_dest;
  176. }
  177. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strncpy.html
  178. char* strncpy(char* dest, char const* src, size_t n)
  179. {
  180. size_t i;
  181. for (i = 0; i < n && src[i] != '\0'; ++i)
  182. dest[i] = src[i];
  183. for (; i < n; ++i)
  184. dest[i] = '\0';
  185. return dest;
  186. }
  187. size_t strlcpy(char* dest, char const* src, size_t n)
  188. {
  189. size_t i;
  190. // Would like to test i < n - 1 here, but n might be 0.
  191. for (i = 0; i + 1 < n && src[i] != '\0'; ++i)
  192. dest[i] = src[i];
  193. if (n)
  194. dest[i] = '\0';
  195. for (; src[i] != '\0'; ++i)
  196. ; // Determine the length of src, don't copy.
  197. return i;
  198. }
  199. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strchr.html
  200. char* strchr(char const* str, int c)
  201. {
  202. char ch = c;
  203. for (;; ++str) {
  204. if (*str == ch)
  205. return const_cast<char*>(str);
  206. if (!*str)
  207. return nullptr;
  208. }
  209. }
  210. // https://pubs.opengroup.org/onlinepubs/9699959399/functions/index.html
  211. char* index(char const* str, int c)
  212. {
  213. return strchr(str, c);
  214. }
  215. char* strchrnul(char const* str, int c)
  216. {
  217. char ch = c;
  218. for (;; ++str) {
  219. if (*str == ch || !*str)
  220. return const_cast<char*>(str);
  221. }
  222. }
  223. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/memchr.html
  224. void* memchr(void const* ptr, int c, size_t size)
  225. {
  226. char ch = c;
  227. auto* cptr = (char const*)ptr;
  228. for (size_t i = 0; i < size; ++i) {
  229. if (cptr[i] == ch)
  230. return const_cast<char*>(cptr + i);
  231. }
  232. return nullptr;
  233. }
  234. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strrchr.html
  235. char* strrchr(char const* str, int ch)
  236. {
  237. char* last = nullptr;
  238. char c;
  239. for (; (c = *str); ++str) {
  240. if (c == ch)
  241. last = const_cast<char*>(str);
  242. }
  243. return last;
  244. }
  245. // https://pubs.opengroup.org/onlinepubs/9699959399/functions/rindex.html
  246. char* rindex(char const* str, int ch)
  247. {
  248. return strrchr(str, ch);
  249. }
  250. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strcat.html
  251. char* strcat(char* dest, char const* src)
  252. {
  253. size_t dest_length = strlen(dest);
  254. size_t i;
  255. for (i = 0; src[i] != '\0'; i++)
  256. dest[dest_length + i] = src[i];
  257. dest[dest_length + i] = '\0';
  258. return dest;
  259. }
  260. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strncat.html
  261. char* strncat(char* dest, char const* src, size_t n)
  262. {
  263. size_t dest_length = strlen(dest);
  264. size_t i;
  265. for (i = 0; i < n && src[i] != '\0'; i++)
  266. dest[dest_length + i] = src[i];
  267. dest[dest_length + i] = '\0';
  268. return dest;
  269. }
  270. char const* const sys_errlist[] = {
  271. #define __ENUMERATE_ERRNO_CODE(c, s) s,
  272. ENUMERATE_ERRNO_CODES(__ENUMERATE_ERRNO_CODE)
  273. #undef __ENUMERATE_ERRNO_CODE
  274. };
  275. static_assert(array_size(sys_errlist) == (EMAXERRNO + 1));
  276. int sys_nerr = EMAXERRNO;
  277. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strerror_r.html
  278. int strerror_r(int errnum, char* buf, size_t buflen)
  279. {
  280. auto saved_errno = errno;
  281. if (errnum < 0 || errnum >= EMAXERRNO) {
  282. auto rc = strlcpy(buf, "unknown error", buflen);
  283. if (rc >= buflen)
  284. dbgln("strerror_r(): Invalid error number '{}' specified and the output buffer is too small.", errnum);
  285. errno = saved_errno;
  286. return EINVAL;
  287. }
  288. auto text = strerror(errnum);
  289. auto rc = strlcpy(buf, text, buflen);
  290. if (rc >= buflen) {
  291. errno = saved_errno;
  292. return ERANGE;
  293. }
  294. errno = saved_errno;
  295. return 0;
  296. }
  297. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strerror.html
  298. char* strerror(int errnum)
  299. {
  300. if (errnum < 0 || errnum >= EMAXERRNO) {
  301. return const_cast<char*>("Unknown error");
  302. }
  303. return const_cast<char*>(sys_errlist[errnum]);
  304. }
  305. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strsignal.html
  306. char* strsignal(int signum)
  307. {
  308. if (signum >= NSIG) {
  309. dbgln("strsignal() missing string for signum={}", signum);
  310. return const_cast<char*>("Unknown signal");
  311. }
  312. return const_cast<char*>(sys_siglist[signum]);
  313. }
  314. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strstr.html
  315. char* strstr(char const* haystack, char const* needle)
  316. {
  317. char nch;
  318. char hch;
  319. if ((nch = *needle++) != 0) {
  320. size_t len = strlen(needle);
  321. do {
  322. do {
  323. if ((hch = *haystack++) == 0)
  324. return nullptr;
  325. } while (hch != nch);
  326. } while (strncmp(haystack, needle, len) != 0);
  327. --haystack;
  328. }
  329. return const_cast<char*>(haystack);
  330. }
  331. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strpbrk.html
  332. char* strpbrk(char const* s, char const* accept)
  333. {
  334. while (*s)
  335. if (strchr(accept, *s++))
  336. return const_cast<char*>(--s);
  337. return nullptr;
  338. }
  339. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strtok_r.html
  340. char* strtok_r(char* str, char const* delim, char** saved_str)
  341. {
  342. if (!str) {
  343. if (!saved_str || *saved_str == nullptr)
  344. return nullptr;
  345. str = *saved_str;
  346. }
  347. size_t token_start = 0;
  348. size_t token_end = 0;
  349. size_t str_len = strlen(str);
  350. size_t delim_len = strlen(delim);
  351. for (size_t i = 0; i < str_len; ++i) {
  352. bool is_proper_delim = false;
  353. for (size_t j = 0; j < delim_len; ++j) {
  354. if (str[i] == delim[j]) {
  355. // Skip beginning delimiters
  356. if (token_end - token_start == 0) {
  357. ++token_start;
  358. break;
  359. }
  360. is_proper_delim = true;
  361. }
  362. }
  363. ++token_end;
  364. if (is_proper_delim && token_end > 0) {
  365. --token_end;
  366. break;
  367. }
  368. }
  369. if (str[token_start] == '\0') {
  370. *saved_str = nullptr;
  371. return nullptr;
  372. }
  373. if (token_end == 0) {
  374. *saved_str = nullptr;
  375. return &str[token_start];
  376. }
  377. if (str[token_end] == '\0')
  378. *saved_str = &str[token_end];
  379. else
  380. *saved_str = &str[token_end + 1];
  381. str[token_end] = '\0';
  382. return &str[token_start];
  383. }
  384. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strtok.html
  385. char* strtok(char* str, char const* delim)
  386. {
  387. static char* saved_str;
  388. return strtok_r(str, delim, &saved_str);
  389. }
  390. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strcoll.html
  391. int strcoll(char const* s1, char const* s2)
  392. {
  393. return strcmp(s1, s2);
  394. }
  395. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strxfrm.html
  396. size_t strxfrm(char* dest, char const* src, size_t n)
  397. {
  398. size_t i;
  399. for (i = 0; i < n && src[i] != '\0'; ++i)
  400. dest[i] = src[i];
  401. for (; i < n; ++i)
  402. dest[i] = '\0';
  403. return i;
  404. }
  405. // Not in POSIX, originated in BSD but also supported on Linux.
  406. // https://man.openbsd.org/strsep.3
  407. char* strsep(char** str, char const* delim)
  408. {
  409. if (*str == nullptr)
  410. return nullptr;
  411. auto* begin = *str;
  412. auto* end = begin + strcspn(begin, delim);
  413. if (*end) {
  414. *end = '\0';
  415. *str = ++end;
  416. } else {
  417. *str = nullptr;
  418. }
  419. return begin;
  420. }
  421. void explicit_bzero(void* ptr, size_t size)
  422. {
  423. secure_zero(ptr, size);
  424. }
  425. }