Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

options/posix: Implement swab #1096

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions options/posix/generic/unistd-stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char *>(_src);
char *dest = reinterpret_cast<char *>(_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) {
Expand Down
1 change: 1 addition & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ all_test_cases = [
'posix/mkstemp',
'posix/waitid',
'posix/usershell',
'posix/swab',
'glibc/getopt',
'glibc/ffsl-ffsll',
'glibc/error_message_count',
Expand Down
66 changes: 66 additions & 0 deletions tests/posix/swab.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <assert.h>
#include <stdint.h>
#include <unistd.h>

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;
}
Loading