Files
RedBear-OS/recipes/tests/unistd/swab.c
T
vasilito b9874d0941 feat: USB storage read/write proof + full Red Bear OS tree sync
Add redbear-usb-storage-check in-guest binary that validates USB mass
storage read and write I/O: discovers /scheme/disk/ devices, writes a
test pattern to sector 2048, reads it back, verifies match, restores
original content. Updates test-usb-storage-qemu.sh with write-proof
verification step.

Includes all accumulated Red Bear OS work: kernel patches, relibc
patches, driver infrastructure, DRM/GPU, KDE recipes, firmware,
validation tooling, build system hardening, and documentation.
2026-05-03 23:03:24 +01:00

54 lines
1.7 KiB
C

#include <stdio.h>
#include <unistd.h>
int main(void) {
const char source[] = {0, 1, 2, 3, 4, 5, 6};
char destination[] = {0, 0, 0, 0, 0, 0};
const char flipped_source[] = {1, 0, 3, 2, 5, 4};
const char first_two_source_bytes_flipped[] = {1, 0};
#ifndef __GLIBC__
swab(source, destination, /* nbytes */ -3);
for (size_t i = 0; i < sizeof(destination); ++i) {
if (destination[i] != 0) {
puts("If nbytes is negative destionation shouldn't be modified");
return 1;
}
}
#endif
swab(source, destination, /* nbytes */ 0);
for (size_t i = 0; i < sizeof(destination); ++i) {
if (destination[i] != 0) {
puts("If nbytes is zero destionation shouldn't be modified");
return 1;
}
}
swab(source, destination, /* nbytes */ 3);
for (size_t i = 0; i < sizeof(first_two_source_bytes_flipped); ++i) {
if (destination[i] != first_two_source_bytes_flipped[i]) {
puts("copied bytes don't match expected values");
return 1;
}
}
// If nbytes is not even it's not specified what should happen to the
// last byte so the third byte in destination is not checked.
for (size_t i = sizeof(first_two_source_bytes_flipped) + 1; i < sizeof(destination); ++i) {
if (destination[i] != 0) {
puts("swab modified too many bytes in destination");
return 1;
}
}
swab(source, destination, /* nbytes */ sizeof(destination));
for (size_t i = 0; i < sizeof(destination); ++i) {
if (destination[i] != flipped_source[i]) {
puts("copied bytes don't match expected values");
return 1;
}
}
return 0;
}