string.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. #if ARCH(I386) || ARCH(X86_64)
  121. void* original_dest = dest_ptr;
  122. asm volatile(
  123. "rep movsb"
  124. : "+D"(dest_ptr), "+S"(src_ptr), "+c"(n)::"memory");
  125. return original_dest;
  126. #elif ARCH(AARCH64)
  127. (void)dest_ptr;
  128. (void)src_ptr;
  129. (void)n;
  130. TODO_AARCH64();
  131. #else
  132. # error Unknown architecture
  133. #endif
  134. }
  135. #if ARCH(I386)
  136. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/memset.html
  137. void* memset(void* dest_ptr, int c, size_t n)
  138. {
  139. size_t dest = (size_t)dest_ptr;
  140. // FIXME: Support starting at an unaligned address.
  141. if (!(dest & 0x3) && n >= 12) {
  142. size_t size_ts = n / sizeof(size_t);
  143. size_t expanded_c = explode_byte((u8)c);
  144. asm volatile(
  145. "rep stosl\n"
  146. : "=D"(dest)
  147. : "D"(dest), "c"(size_ts), "a"(expanded_c)
  148. : "memory");
  149. n -= size_ts * sizeof(size_t);
  150. if (n == 0)
  151. return dest_ptr;
  152. }
  153. asm volatile(
  154. "rep stosb\n"
  155. : "=D"(dest), "=c"(n)
  156. : "0"(dest), "1"(n), "a"(c)
  157. : "memory");
  158. return dest_ptr;
  159. }
  160. #elif ARCH(X86_64)
  161. // For x86-64, an optimized ASM implementation is found in ./arch/x86_64/memset.S
  162. #elif ARCH(AARCH64)
  163. void* memset(void* dest_ptr, int c, size_t n)
  164. {
  165. (void)dest_ptr;
  166. (void)c;
  167. (void)n;
  168. TODO_AARCH64();
  169. }
  170. #else
  171. # error Unknown architecture
  172. #endif
  173. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/memmove.html
  174. void* memmove(void* dest, void const* src, size_t n)
  175. {
  176. if (((FlatPtr)dest - (FlatPtr)src) >= n)
  177. return memcpy(dest, src, n);
  178. u8* pd = (u8*)dest;
  179. u8 const* ps = (u8 const*)src;
  180. for (pd += n, ps += n; n--;)
  181. *--pd = *--ps;
  182. return dest;
  183. }
  184. void const* memmem(void const* haystack, size_t haystack_length, void const* needle, size_t needle_length)
  185. {
  186. return AK::memmem(haystack, haystack_length, needle, needle_length);
  187. }
  188. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strcpy.html
  189. char* strcpy(char* dest, char const* src)
  190. {
  191. char* original_dest = dest;
  192. while ((*dest++ = *src++) != '\0')
  193. ;
  194. return original_dest;
  195. }
  196. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strncpy.html
  197. char* strncpy(char* dest, char const* src, size_t n)
  198. {
  199. size_t i;
  200. for (i = 0; i < n && src[i] != '\0'; ++i)
  201. dest[i] = src[i];
  202. for (; i < n; ++i)
  203. dest[i] = '\0';
  204. return dest;
  205. }
  206. size_t strlcpy(char* dest, char const* src, size_t n)
  207. {
  208. size_t i;
  209. // Would like to test i < n - 1 here, but n might be 0.
  210. for (i = 0; i + 1 < n && src[i] != '\0'; ++i)
  211. dest[i] = src[i];
  212. if (n)
  213. dest[i] = '\0';
  214. for (; src[i] != '\0'; ++i)
  215. ; // Determine the length of src, don't copy.
  216. return i;
  217. }
  218. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strchr.html
  219. char* strchr(char const* str, int c)
  220. {
  221. char ch = c;
  222. for (;; ++str) {
  223. if (*str == ch)
  224. return const_cast<char*>(str);
  225. if (!*str)
  226. return nullptr;
  227. }
  228. }
  229. // https://pubs.opengroup.org/onlinepubs/9699959399/functions/index.html
  230. char* index(char const* str, int c)
  231. {
  232. return strchr(str, c);
  233. }
  234. char* strchrnul(char const* str, int c)
  235. {
  236. char ch = c;
  237. for (;; ++str) {
  238. if (*str == ch || !*str)
  239. return const_cast<char*>(str);
  240. }
  241. }
  242. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/memchr.html
  243. void* memchr(void const* ptr, int c, size_t size)
  244. {
  245. char ch = c;
  246. auto* cptr = (char const*)ptr;
  247. for (size_t i = 0; i < size; ++i) {
  248. if (cptr[i] == ch)
  249. return const_cast<char*>(cptr + i);
  250. }
  251. return nullptr;
  252. }
  253. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strrchr.html
  254. char* strrchr(char const* str, int ch)
  255. {
  256. char* last = nullptr;
  257. char c;
  258. for (; (c = *str); ++str) {
  259. if (c == ch)
  260. last = const_cast<char*>(str);
  261. }
  262. return last;
  263. }
  264. // https://pubs.opengroup.org/onlinepubs/9699959399/functions/rindex.html
  265. char* rindex(char const* str, int ch)
  266. {
  267. return strrchr(str, ch);
  268. }
  269. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strcat.html
  270. char* strcat(char* dest, char const* src)
  271. {
  272. size_t dest_length = strlen(dest);
  273. size_t i;
  274. for (i = 0; src[i] != '\0'; i++)
  275. dest[dest_length + i] = src[i];
  276. dest[dest_length + i] = '\0';
  277. return dest;
  278. }
  279. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strncat.html
  280. char* strncat(char* dest, char const* src, size_t n)
  281. {
  282. size_t dest_length = strlen(dest);
  283. size_t i;
  284. for (i = 0; i < n && src[i] != '\0'; i++)
  285. dest[dest_length + i] = src[i];
  286. dest[dest_length + i] = '\0';
  287. return dest;
  288. }
  289. char const* const sys_errlist[] = {
  290. #define __ENUMERATE_ERRNO_CODE(c, s) s,
  291. ENUMERATE_ERRNO_CODES(__ENUMERATE_ERRNO_CODE)
  292. #undef __ENUMERATE_ERRNO_CODE
  293. };
  294. static_assert(array_size(sys_errlist) == (EMAXERRNO + 1));
  295. int sys_nerr = EMAXERRNO;
  296. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strerror_r.html
  297. int strerror_r(int errnum, char* buf, size_t buflen)
  298. {
  299. auto saved_errno = errno;
  300. if (errnum < 0 || errnum >= EMAXERRNO) {
  301. auto rc = strlcpy(buf, "unknown error", buflen);
  302. if (rc >= buflen)
  303. dbgln("strerror_r(): Invalid error number '{}' specified and the output buffer is too small.", errnum);
  304. errno = saved_errno;
  305. return EINVAL;
  306. }
  307. auto text = strerror(errnum);
  308. auto rc = strlcpy(buf, text, buflen);
  309. if (rc >= buflen) {
  310. errno = saved_errno;
  311. return ERANGE;
  312. }
  313. errno = saved_errno;
  314. return 0;
  315. }
  316. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strerror.html
  317. char* strerror(int errnum)
  318. {
  319. if (errnum < 0 || errnum >= EMAXERRNO) {
  320. return const_cast<char*>("Unknown error");
  321. }
  322. return const_cast<char*>(sys_errlist[errnum]);
  323. }
  324. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strsignal.html
  325. char* strsignal(int signum)
  326. {
  327. if (signum >= NSIG) {
  328. dbgln("strsignal() missing string for signum={}", signum);
  329. return const_cast<char*>("Unknown signal");
  330. }
  331. return const_cast<char*>(sys_siglist[signum]);
  332. }
  333. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strstr.html
  334. char* strstr(char const* haystack, char const* needle)
  335. {
  336. char nch;
  337. char hch;
  338. if ((nch = *needle++) != 0) {
  339. size_t len = strlen(needle);
  340. do {
  341. do {
  342. if ((hch = *haystack++) == 0)
  343. return nullptr;
  344. } while (hch != nch);
  345. } while (strncmp(haystack, needle, len) != 0);
  346. --haystack;
  347. }
  348. return const_cast<char*>(haystack);
  349. }
  350. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strpbrk.html
  351. char* strpbrk(char const* s, char const* accept)
  352. {
  353. while (*s)
  354. if (strchr(accept, *s++))
  355. return const_cast<char*>(--s);
  356. return nullptr;
  357. }
  358. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strtok_r.html
  359. char* strtok_r(char* str, char const* delim, char** saved_str)
  360. {
  361. if (!str) {
  362. if (!saved_str || *saved_str == nullptr)
  363. return nullptr;
  364. str = *saved_str;
  365. }
  366. size_t token_start = 0;
  367. size_t token_end = 0;
  368. size_t str_len = strlen(str);
  369. size_t delim_len = strlen(delim);
  370. for (size_t i = 0; i < str_len; ++i) {
  371. bool is_proper_delim = false;
  372. for (size_t j = 0; j < delim_len; ++j) {
  373. if (str[i] == delim[j]) {
  374. // Skip beginning delimiters
  375. if (token_end - token_start == 0) {
  376. ++token_start;
  377. break;
  378. }
  379. is_proper_delim = true;
  380. }
  381. }
  382. ++token_end;
  383. if (is_proper_delim && token_end > 0) {
  384. --token_end;
  385. break;
  386. }
  387. }
  388. if (str[token_start] == '\0') {
  389. *saved_str = nullptr;
  390. return nullptr;
  391. }
  392. if (token_end == 0) {
  393. *saved_str = nullptr;
  394. return &str[token_start];
  395. }
  396. if (str[token_end] == '\0')
  397. *saved_str = &str[token_end];
  398. else
  399. *saved_str = &str[token_end + 1];
  400. str[token_end] = '\0';
  401. return &str[token_start];
  402. }
  403. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strtok.html
  404. char* strtok(char* str, char const* delim)
  405. {
  406. static char* saved_str;
  407. return strtok_r(str, delim, &saved_str);
  408. }
  409. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strcoll.html
  410. int strcoll(char const* s1, char const* s2)
  411. {
  412. return strcmp(s1, s2);
  413. }
  414. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strxfrm.html
  415. size_t strxfrm(char* dest, char const* src, size_t n)
  416. {
  417. size_t i;
  418. for (i = 0; i < n && src[i] != '\0'; ++i)
  419. dest[i] = src[i];
  420. for (; i < n; ++i)
  421. dest[i] = '\0';
  422. return i;
  423. }
  424. // Not in POSIX, originated in BSD but also supported on Linux.
  425. // https://man.openbsd.org/strsep.3
  426. char* strsep(char** str, char const* delim)
  427. {
  428. if (*str == nullptr)
  429. return nullptr;
  430. auto* begin = *str;
  431. auto* end = begin + strcspn(begin, delim);
  432. if (*end) {
  433. *end = '\0';
  434. *str = ++end;
  435. } else {
  436. *str = nullptr;
  437. }
  438. return begin;
  439. }
  440. void explicit_bzero(void* ptr, size_t size)
  441. {
  442. secure_zero(ptr, size);
  443. }
  444. }