Hash :
b4913eb1
Author :
Thomas de Grivel
Date :
2025-07-22T23:18:41
wip crc32c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
/* ext4fs
* Copyright 2025 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <ext4fs.h>
#include <crc32c.h>
#include <uuid.h>
long g_test_ko = 0;
long g_test_ok = 0;
long g_test_total = 0;
#define TEST_ASSERT(test) \
do { \
if (! (test)) { \
fprintf(stderr, "KO: %s\n", # test); \
g_test_ko++; \
g_test_total++; \
} \
else { \
g_test_ok++; \
g_test_total++; \
} \
} while (0)
#define TEST_EQ(test, expected) \
do { \
int64_t i_test = (test); \
int64_t i_expected = (expected); \
if (! ((i_test) == (i_expected))) { \
fprintf(stderr, "KO: expected %s == %ld, got %ld instead\n", \
# test, i_expected, i_test); \
g_test_ko++; \
g_test_total++; \
} \
else { \
g_test_ok++; \
g_test_total++; \
} \
} while (0)
void test_summary (void)
{
fflush(stdout);
fprintf(stderr, "OK: %ld\tKO: %ld\tTotal: %ld\n",
g_test_ok, g_test_ko, g_test_total);
fflush(stderr);
}
int main (int argc, char **argv)
{
uint8_t b[32];
struct ext4fs_super_block sb = {0};
uint32_t u32;
(void) argc;
(void) argv;
TEST_EQ(offsetof(struct ext4fs_super_block, sb_default_mount_opts),
0x100);
TEST_EQ(offsetof(struct ext4fs_super_block, sb_mount_opts),
0x200);
TEST_EQ(offsetof(struct ext4fs_super_block, sb_orphan_file_inode),
0x280);
TEST_EQ(offsetof(struct ext4fs_super_block, sb_checksum),
0x3FC);
TEST_EQ(sizeof(sb.sb_uuid), 16);
TEST_EQ(sizeof(struct ext4fs_super_block),
1024);
TEST_EQ(offsetof(struct ext4fs_block_group_descriptor, bgd_checksum),
0x1E);
TEST_EQ(sizeof(struct ext4fs_block_group_descriptor),
64);
u32 = 0;
TEST_EQ(crc32c(0, &u32, 4), 0x48674BC7U);
bzero(b, sizeof(b));
TEST_EQ(crc32c(0, b, sizeof(b)), 0x8A9136AAU);
memset(b, -1, sizeof(b));
TEST_EQ(crc32c(0, b, sizeof(b)), 0x62A8AB43U);
TEST_EQ(crc32c(0, "123456789", 9), 0xE3069283U);
TEST_EQ(crc32c(crc32c(0, "1", 1), "23456789", 8),
0xE3069283U);
TEST_EQ(crc32c(crc32c(0, "12", 2), "3456789", 7),
0xE3069283U);
TEST_EQ(crc32c(crc32c(0, "123", 3), "456789", 6),
0xE3069283U);
TEST_EQ(crc32c(crc32c(0, "1234", 4), "56789", 5),
0xE3069283U);
TEST_EQ(crc32c(crc32c(0, "12345", 5), "6789", 4),
0xE3069283U);
TEST_EQ(crc32c(crc32c(0, "123456", 6), "789", 3),
0xE3069283U);
TEST_EQ(crc32c(crc32c(0, "1234567", 7), "89", 2),
0xE3069283U);
TEST_EQ(crc32c(crc32c(0, "12345678", 8), "9", 1),
0xE3069283U);
TEST_EQ(crc32c(crc32c(crc32c(0, "123", 3), "456", 3), "789", 3),
0xE3069283U);
test_summary();
return g_test_ko ? 1 : 0;
}