diff --git a/configure.ac b/configure.ac index 86018e9cc..35270ccca 100644 --- a/configure.ac +++ b/configure.ac @@ -3,7 +3,7 @@ # Process this file with autoconf to produce a configure script. AC_PREREQ([2.63]) -AC_INIT([leech], [0.1.10], [https://github.com/larsewi/leech/issues], [leech], +AC_INIT([leech], [0.1.11], [https://github.com/larsewi/leech/issues], [leech], [https://github.com/larsewi/leech]) AC_CONFIG_SRCDIR([lib/leech.h]) diff --git a/lib/buffer.c b/lib/buffer.c index f067d1efc..26db88c95 100644 --- a/lib/buffer.c +++ b/lib/buffer.c @@ -189,76 +189,6 @@ bool LCH_BufferBytesToHex(LCH_Buffer *const hex, return true; } -bool LCH_BufferHexToBytes(LCH_Buffer *const bytes, - const LCH_Buffer *const hex) { - assert(bytes != NULL); - assert(hex != NULL); - assert(hex->length % 2 == 0); // Illegal: Odd number of hexadecimals - - size_t num_bytes = hex->length / 2; - if (!EnsureCapacity(bytes, num_bytes)) { - return false; - } - - for (size_t i = 0; i < num_bytes; i++) { - if (sscanf(hex->buffer + (i * 2), "%2hhx", - bytes->buffer + (bytes->length + i)) != 1) { - bytes->buffer[bytes->length] = '\0'; - return false; - } - } - bytes->length += num_bytes; - bytes->buffer[bytes->length] = '\0'; - - return true; -} - -bool LCH_BufferUnicodeToUTF8(LCH_Buffer *const buffer, const char *const in) { - LCH_Buffer *hex = LCH_BufferCreate(); - for (size_t i = 0; i < 4; i++) { - if (!isxdigit(in[i])) { - LCH_LOG_ERROR( - "Failed to convert unicode escape sequence to UTF8:\n" - "%.4s\n%*s^ Not a hexadecimal number!", - in, i); - LCH_BufferDestroy(hex); - return false; - } - if (!LCH_BufferAppend(hex, in[i])) { - LCH_BufferDestroy(hex); - return false; - } - } - - LCH_Buffer *bytes = LCH_BufferCreateWithCapacity(4); - if (!LCH_BufferHexToBytes(bytes, hex)) { - LCH_BufferDestroy(hex); - return false; - } - LCH_BufferDestroy(hex); - - uint16_t *host = (uint16_t *)LCH_BufferData(bytes); - assert(host != NULL); - uint16_t code_point = htons(*host); - LCH_BufferDestroy(bytes); - - if (code_point < 0x80) { - if (!LCH_BufferAppend(buffer, (unsigned char)code_point)) { - return false; - } - } else if (code_point < 0x800) { - if (!LCH_BufferAppend(buffer, 192 + code_point / 64)) { - return false; - } - if (!LCH_BufferAppend(buffer, 128 + code_point % 64)) { - return false; - } - } else { - return false; - } - return true; -} - void LCH_BufferChop(LCH_Buffer *const self, size_t offset) { assert(self != NULL); assert(self->buffer != NULL); diff --git a/lib/buffer.h b/lib/buffer.h index 47b4e3afa..072212623 100644 --- a/lib/buffer.h +++ b/lib/buffer.h @@ -37,20 +37,6 @@ void LCH_BufferSet(LCH_Buffer *buffer, size_t offset, const void *value, */ bool LCH_BufferBytesToHex(LCH_Buffer *hex, const LCH_Buffer *bytes); -/** - * @brief Converts hexadecimal string representation into bytes - * @param bytes Byte buffer - * @param hex Hexadecimal buffer - * @return True on success, false on error - */ -bool LCH_BufferHexToBytes(LCH_Buffer *bytes, const LCH_Buffer *hex); - -/** - * @warning Makes the assumption that the input string is at least four bytes - * long (excluding the termination null-byte). - */ -bool LCH_BufferUnicodeToUTF8(LCH_Buffer *const buffer, const char *in); - const LCH_Buffer *LCH_BufferStaticFromString(const char *str); void LCH_BufferTrim(LCH_Buffer *buffer, char ch); diff --git a/tests/check_buffer.c b/tests/check_buffer.c index b6146aa2f..dd386e620 100644 --- a/tests/check_buffer.c +++ b/tests/check_buffer.c @@ -189,55 +189,6 @@ START_TEST(test_LCH_BufferBytesToHex) { } END_TEST -START_TEST(test_LCH_BufferHexToBytes) { - LCH_Buffer *hex = LCH_BufferCreate(); - ck_assert_ptr_nonnull(hex); - ck_assert(LCH_BufferPrintFormat(hex, "0123456789abcdef")); - - LCH_Buffer *bytes = LCH_BufferCreate(); - ck_assert_ptr_nonnull(bytes); - ck_assert(LCH_BufferHexToBytes(bytes, hex)); - - const unsigned char data[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}; - for (size_t i = 0; i < sizeof(data); i++) { - ck_assert_int_eq(*((unsigned char *)(LCH_BufferData(bytes) + i)), data[i]); - } - - LCH_BufferDestroy(hex); - LCH_BufferDestroy(bytes); -} -END_TEST - -START_TEST(test_LCH_BufferUnicodeToUTF8) { - const char *code_points = "0041"; - LCH_Buffer *buffer = LCH_BufferCreate(); - ck_assert_ptr_nonnull(buffer); - ck_assert(LCH_BufferUnicodeToUTF8(buffer, code_points)); - char *str = LCH_BufferToString(buffer); - ck_assert_ptr_nonnull(str); - ck_assert_str_eq(str, "A"); - free(str); - - code_points = "0100"; - buffer = LCH_BufferCreate(); - ck_assert_ptr_nonnull(buffer); - ck_assert(LCH_BufferUnicodeToUTF8(buffer, code_points)); - str = LCH_BufferToString(buffer); - ck_assert_ptr_nonnull(str); - ck_assert_str_eq(str, "Ā"); - free(str); - - code_points = "0101"; - buffer = LCH_BufferCreate(); - ck_assert_ptr_nonnull(buffer); - ck_assert(LCH_BufferUnicodeToUTF8(buffer, code_points)); - str = LCH_BufferToString(buffer); - ck_assert_ptr_nonnull(str); - ck_assert_str_eq(str, "ā"); - free(str); -} -END_TEST - Suite *BufferSuite(void) { Suite *s = suite_create("buffer.c"); { @@ -265,15 +216,5 @@ Suite *BufferSuite(void) { tcase_add_test(tc, test_LCH_BufferBytesToHex); suite_add_tcase(s, tc); } - { - TCase *tc = tcase_create("LCH_BufferHexToBytes"); - tcase_add_test(tc, test_LCH_BufferHexToBytes); - suite_add_tcase(s, tc); - } - { - TCase *tc = tcase_create("LCH_BufferUnicodeToUTF8"); - tcase_add_test(tc, test_LCH_BufferUnicodeToUTF8); - suite_add_tcase(s, tc); - } return s; }