From 2edd9638904af6c7de3011c9d87849da8c2a6065 Mon Sep 17 00:00:00 2001 From: Dennis Bonke Date: Fri, 26 Jul 2024 16:52:06 +0200 Subject: [PATCH 1/2] options/posix: Implement swab --- options/posix/generic/unistd-stubs.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/options/posix/generic/unistd-stubs.cpp b/options/posix/generic/unistd-stubs.cpp index 444ad22da0..1ac4843073 100644 --- a/options/posix/generic/unistd-stubs.cpp +++ b/options/posix/generic/unistd-stubs.cpp @@ -666,9 +666,15 @@ int setuid(uid_t uid) { return 0; } -void swab(const void *__restrict, void *__restrict, ssize_t) { - __ensure(!"Not implemented"); - __builtin_unreachable(); +void swab(const void *_src, void *_dest, ssize_t n) { + const char *src = reinterpret_cast(_src); + char *dest = reinterpret_cast(_dest); + for(; n > 1; n -= 2) { + dest[0] = src[1]; + dest[1] = src[0]; + dest += 2; + src += 2; + } } int symlink(const char *target_path, const char *link_path) { From 55d66a87bbfa62434d0ab82d3c113f745a123b02 Mon Sep 17 00:00:00 2001 From: Dennis Bonke Date: Wed, 9 Oct 2024 01:49:24 +0200 Subject: [PATCH 2/2] tests/posix: Add tests for swab --- tests/meson.build | 1 + tests/posix/swab.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 tests/posix/swab.c diff --git a/tests/meson.build b/tests/meson.build index bbc0cd6491..ec1b860fce 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -98,6 +98,7 @@ all_test_cases = [ 'posix/mkstemp', 'posix/waitid', 'posix/usershell', + 'posix/swab', 'glibc/getopt', 'glibc/ffsl-ffsll', 'glibc/error_message_count', diff --git a/tests/posix/swab.c b/tests/posix/swab.c new file mode 100644 index 0000000000..ee6b45df02 --- /dev/null +++ b/tests/posix/swab.c @@ -0,0 +1,66 @@ +#include +#include +#include + +void test_swab_even_bytes() { + uint8_t input[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t output[4]; + + swab(input, output, sizeof(input)); + + assert(output[0] == 0x02); + assert(output[1] == 0x01); + assert(output[2] == 0x04); + assert(output[3] == 0x03); +} + +void test_swab_odd_bytes() { + uint8_t input[] = {0x01, 0x02, 0x03, 0x04, 0x05}; + uint8_t output[5]; + + swab(input, output, sizeof(input)); + + assert(output[0] == 0x02); + assert(output[1] == 0x01); + assert(output[2] == 0x04); + assert(output[3] == 0x03); + + // Last byte is UB, assume unchanged? + assert(output[4] == 0x05); +} + +void test_swab_negative_bytes() { + uint8_t input[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t output[4]; + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wstringop-overflow" + swab(input, output, -1); // Should change nothing +#pragma GCC diagnostic pop + + assert(output[0] == 0x01); + assert(output[1] == 0x02); + assert(output[2] == 0x03); + assert(output[3] == 0x04); +} + +void test_swab_zero_bytes() { + uint8_t input[] = {0x01, 0x02, 0x03, 0x04}; + uint8_t output[4]; + + swab(input, output, 0); // Should change nothing + + assert(output[0] == 0x01); + assert(output[1] == 0x02); + assert(output[2] == 0x03); + assert(output[3] == 0x04); +} + +int main() { + test_swab_even_bytes(); + test_swab_odd_bytes(); + test_swab_negative_bytes(); + test_swab_zero_bytes(); + + return 0; +}