string.cpp 12 KB

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