Skip to content

Commit

Permalink
Merge pull request #64 from larsewi/hex
Browse files Browse the repository at this point in the history
Removed unused functions that caused Windows build to fail
  • Loading branch information
larsewi authored May 2, 2024
2 parents 31f3df4 + dd78cae commit 1a344fc
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 144 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
70 changes: 0 additions & 70 deletions lib/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 0 additions & 14 deletions lib/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
59 changes: 0 additions & 59 deletions tests/check_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
{
Expand Down Expand Up @@ -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;
}

0 comments on commit 1a344fc

Please sign in to comment.