string.cpp 12 KB

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