From: ljdarj Date: Sat, 8 Mar 2025 03:21:23 +0000 (+0100) Subject: Moving the tests' CRC-32 function to test_utils. (#2390) X-Git-Tag: v3.8.0~69 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=de77e6d67166bf667f057bd9515c77bb8dc0f30a;p=thirdparty%2Flibarchive.git Moving the tests' CRC-32 function to test_utils. (#2390) Following up from #2284, putting the tests' CRC-32 implementation in test_utils and having all tests use it. --- diff --git a/libarchive/test/test_read_format_7zip.c b/libarchive/test/test_read_format_7zip.c index 8862ad517..187c02ff8 100644 --- a/libarchive/test/test_read_format_7zip.c +++ b/libarchive/test/test_read_format_7zip.c @@ -34,7 +34,6 @@ #endif #define __LIBARCHIVE_BUILD -#include /* * Extract a non-encoded file. @@ -406,7 +405,7 @@ test_extract_file_zstd_bcj_nobjc(const char *refname) la_ssize_t bytes_read = archive_read_data(a, buff, sizeof(buff)); assert(bytes_read >= 0); if (bytes_read == 0) break; - computed_crc = crc32(computed_crc, buff, bytes_read); + computed_crc = bitcrc32(computed_crc, buff, bytes_read); } assertEqualInt(computed_crc, expected_crc); @@ -1075,7 +1074,7 @@ test_arm_filter(const char *refname) assertEqualString("hw-gnueabihf", archive_entry_pathname(ae)); assertEqualInt(sizeof(buff), archive_entry_size(ae)); assertEqualInt(sizeof(buff), archive_read_data(a, buff, sizeof(buff))); - computed_crc = crc32(computed_crc, buff, sizeof(buff)); + computed_crc = bitcrc32(computed_crc, buff, sizeof(buff)); assertEqualInt(computed_crc, expected_crc); assertEqualInt(1, archive_file_count(a)); @@ -1149,7 +1148,7 @@ test_arm64_filter(const char *refname) assertEqualString("hw-arm64", archive_entry_pathname(ae)); assertEqualInt(sizeof(buff), archive_entry_size(ae)); assertEqualInt(sizeof(buff), archive_read_data(a, buff, sizeof(buff))); - computed_crc = crc32(computed_crc, buff, sizeof(buff)); + computed_crc = bitcrc32(computed_crc, buff, sizeof(buff)); assertEqualInt(computed_crc, expected_crc); assertEqualInt(1, archive_file_count(a)); @@ -1342,7 +1341,7 @@ test_riscv_filter(const char *refname) assertEqualInt(sizeof(buff), archive_entry_size(ae)); assertEqualInt(sizeof(buff), archive_read_data(a, buff, sizeof(buff))); - computed_crc = crc32(computed_crc, buff, sizeof(buff)); + computed_crc = bitcrc32(computed_crc, buff, sizeof(buff)); assertEqualInt(computed_crc, expected_crc); assertEqualInt(1, archive_file_count(a)); @@ -1399,7 +1398,7 @@ test_sparc_filter(const char *refname) assertEqualInt(expected_entry_size, archive_entry_size(ae)); assertEqualInt(expected_entry_size, archive_read_data(a, buff, expected_entry_size)); - computed_crc = crc32(computed_crc, buff, expected_entry_size); + computed_crc = bitcrc32(computed_crc, buff, expected_entry_size); assertEqualInt(computed_crc, expected_crc); assertEqualInt(1, archive_file_count(a)); @@ -1472,7 +1471,7 @@ test_powerpc_filter(const char *refname) assertEqualInt(expected_entry_size, archive_entry_size(ae)); assertEqualInt(expected_entry_size, archive_read_data(a, buff, expected_entry_size)); - computed_crc = crc32(computed_crc, buff, expected_entry_size); + computed_crc = bitcrc32(computed_crc, buff, expected_entry_size); assertEqualInt(computed_crc, expected_crc); assertEqualInt(1, archive_file_count(a)); diff --git a/libarchive/test/test_read_format_rar5.c b/libarchive/test/test_read_format_rar5.c index 0345b1bed..696975354 100644 --- a/libarchive/test/test_read_format_rar5.c +++ b/libarchive/test/test_read_format_rar5.c @@ -27,7 +27,7 @@ /* Some tests will want to calculate some CRC32's, and this header can * help. */ #define __LIBARCHIVE_BUILD -#include +#include #define PROLOGUE(reffile) \ struct archive_entry *ae; \ @@ -106,7 +106,7 @@ int extract_one(struct archive* a, struct archive_entry* ae, uint32_t crc) { goto fn_exit; } - computed_crc = crc32(0, buf, fsize); + computed_crc = bitcrc32(0, buf, fsize); assertEqualInt(computed_crc, crc); ret = 0; @@ -335,7 +335,7 @@ DEFINE_TEST(test_read_format_rar5_blake2) assertA(proper_size == archive_read_data(a, buf, proper_size)); /* To be extra pedantic, let's also check crc32 of the poem. */ - assertEqualInt(crc32(0, buf, proper_size), 0x7E5EC49E); + assertEqualInt(bitcrc32(0, buf, proper_size), 0x7E5EC49E); assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae)); EPILOGUE(); @@ -358,7 +358,7 @@ DEFINE_TEST(test_read_format_rar5_arm_filter) /* Yes, RARv5 unpacker itself should calculate the CRC, but in case * the DONT_FAIL_ON_CRC_ERROR define option is enabled during compilation, * let's still fail the test if the unpacked data is wrong. */ - assertEqualInt(crc32(0, buf, proper_size), 0x886F91EB); + assertEqualInt(bitcrc32(0, buf, proper_size), 0x886F91EB); assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae)); EPILOGUE(); @@ -869,7 +869,7 @@ DEFINE_TEST(test_read_format_rar5_block_by_block) if(bytes_read <= 0) break; - computed_crc = crc32(computed_crc, buf, bytes_read); + computed_crc = bitcrc32(computed_crc, buf, bytes_read); } assertEqualInt(computed_crc, 0x7CCA70CD); diff --git a/libarchive/test/test_read_format_zip.c b/libarchive/test/test_read_format_zip.c index 9e820f509..90d00474b 100644 --- a/libarchive/test/test_read_format_zip.c +++ b/libarchive/test/test_read_format_zip.c @@ -26,7 +26,6 @@ #include "test.h" #define __LIBARCHIVE_BUILD -#include static int extract_one(struct archive* a, struct archive_entry* ae, uint32_t crc) @@ -47,7 +46,7 @@ int extract_one(struct archive* a, struct archive_entry* ae, uint32_t crc) goto fn_exit; } - computed_crc = crc32(0, buf, fsize); + computed_crc = bitcrc32(0, buf, fsize); assertEqualInt(computed_crc, crc); ret = 0; @@ -84,7 +83,7 @@ int extract_one_using_blocks(struct archive* a, int block_size, uint32_t crc) /* ok */ } - computed_crc = crc32(computed_crc, buf, bytes_read); + computed_crc = bitcrc32(computed_crc, buf, bytes_read); } assertEqualInt(computed_crc, crc); diff --git a/libarchive/test/test_write_format_zip64_stream.c b/libarchive/test/test_write_format_zip64_stream.c index 5f650bccb..1d8f1ff7f 100644 --- a/libarchive/test/test_write_format_zip64_stream.c +++ b/libarchive/test/test_write_format_zip64_stream.c @@ -31,34 +31,6 @@ * written in streaming mode with Zip64 extensions enabled. */ -static unsigned long -bitcrc32(unsigned long c, void *_p, size_t s) -{ - /* This is a drop-in replacement for crc32() from zlib. - * Libarchive should be able to correctly generate - * uncompressed zip archives (including correct CRCs) even - * when zlib is unavailable, and this function helps us verify - * that. Yes, this is very, very slow and unsuitable for - * production use, but it's correct, compact, and works well - * enough for this particular usage. Libarchive internally - * uses a much more efficient implementation. */ - const unsigned char *p = _p; - int bitctr; - - if (p == NULL) - return (0); - - for (; s > 0; --s) { - c ^= *p++; - for (bitctr = 8; bitctr > 0; --bitctr) { - if (c & 1) c = (c >> 1); - else c = (c >> 1) ^ 0xedb88320; - c ^= 0x80000000; - } - } - return (c); -} - DEFINE_TEST(test_write_format_zip64_stream) { struct archive *a; diff --git a/libarchive/test/test_write_format_zip_compression_bzip2.c b/libarchive/test/test_write_format_zip_compression_bzip2.c index 969c4ddd9..7ec83309e 100644 --- a/libarchive/test/test_write_format_zip_compression_bzip2.c +++ b/libarchive/test/test_write_format_zip_compression_bzip2.c @@ -7,35 +7,6 @@ #ifdef HAVE_BZLIB_H #include -static unsigned long -bitcrc32(unsigned long c, const void *_p, size_t s) -{ - /* This is a drop-in replacement for crc32() from zlib, given that - * libbzip2 doesn't expose its CRC32 function, as far as I'm aware. - * Libarchive should be able to correctly generate BZIP2-compressed - * zip archives (including correct CRCs) even when zlib is - * unavailable, and this function helps us verify that. Yes, this is - * very, very slow and unsuitable for production use, but it's very, - * very obviously correct, compact, and works well for this - * particular usage. Libarchive internally uses a much more - * efficient implementation when zlib is unavailable */ - const unsigned char *p = _p; - int bitctr; - - if (p == NULL) - return (0); - - for (; s > 0; --s) { - c ^= *p++; - for (bitctr = 8; bitctr > 0; --bitctr) { - if (c & 1) c = (c >> 1); - else c = (c >> 1) ^ 0xedb88320; - c ^= 0x80000000; - } - } - return (c); -} - /* File data */ static const char file_name[] = "file"; static const char file_data1[] = {'1', '2', '3', '4', '5', '6', '7', '8'}; diff --git a/libarchive/test/test_write_format_zip_compression_lzmaxz.c b/libarchive/test/test_write_format_zip_compression_lzmaxz.c index 4b5be2913..c5bb2d659 100644 --- a/libarchive/test/test_write_format_zip_compression_lzmaxz.c +++ b/libarchive/test/test_write_format_zip_compression_lzmaxz.c @@ -9,11 +9,8 @@ /* File data */ static const char file_name[] = "file"; -/* We have liblzma's lzma_crc32() so no need to rely on a handmade - * CRC-32...but it requires a uint8_t* for its data, hence the change - * compared to the usual */ -static const uint8_t file_data1[] = {'.', ';', ':', '!', '?', ',', '"', '\'', ')', '(', '*'}; -static const uint8_t file_data2[] = {'-', '/', '>', '$', '\\', '#', '@', '+', '=', '{', ']', '[', '}', '&', '<', '%'}; +static const char file_data1[] = {'.', ';', ':', '!', '?', ',', '"', '\'', ')', '(', '*'}; +static const char file_data2[] = {'-', '/', '>', '$', '\\', '#', '@', '+', '=', '{', ']', '[', '}', '&', '<', '%'}; static const int file_perm = 00644; static const short file_uid = 10; static const short file_gid = 20; @@ -125,8 +122,8 @@ static void verify_xz_lzma(const char *buff, size_t used, uint16_t id, assertEqualInt(i2le(p + 10), id); /* Compression method */ assertEqualInt(i2le(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */ assertEqualInt(i2le(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */ - crc = lzma_crc32(file_data1, sizeof(file_data1), 0); - crc = lzma_crc32(file_data2, sizeof(file_data2), crc); + crc = bitcrc32(0, file_data1, sizeof(file_data1)); + crc = bitcrc32(crc, file_data2, sizeof(file_data2)); assertEqualInt(i4le(p + 16), crc); /* CRC-32 */ assertEqualInt(i4le(p + 24), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */ assertEqualInt(i2le(p + 28), strlen(file_name)); /* Pathname length */ diff --git a/libarchive/test/test_write_format_zip_compression_store.c b/libarchive/test/test_write_format_zip_compression_store.c index 495509c05..5f08a59e9 100644 --- a/libarchive/test/test_write_format_zip_compression_store.c +++ b/libarchive/test/test_write_format_zip_compression_store.c @@ -45,34 +45,6 @@ static const short folder_gid = 40; static time_t now; -static unsigned long -bitcrc32(unsigned long c, const void *_p, size_t s) -{ - /* This is a drop-in replacement for crc32() from zlib. - * Libarchive should be able to correctly generate - * uncompressed zip archives (including correct CRCs) even - * when zlib is unavailable, and this function helps us verify - * that. Yes, this is very, very slow and unsuitable for - * production use, but it's correct, compact, and works well - * enough for this particular usage. Libarchive internally - * uses a much more efficient implementation. */ - const unsigned char *p = _p; - int bitctr; - - if (p == NULL) - return (0); - - for (; s > 0; --s) { - c ^= *p++; - for (bitctr = 8; bitctr > 0; --bitctr) { - if (c & 1) c = (c >> 1); - else c = (c >> 1) ^ 0xedb88320; - c ^= 0x80000000; - } - } - return (c); -} - static void verify_write_uncompressed(struct archive *a) { struct archive_entry *entry; diff --git a/libarchive/test/test_write_format_zip_compression_zstd.c b/libarchive/test/test_write_format_zip_compression_zstd.c index aeef35535..002026a4c 100644 --- a/libarchive/test/test_write_format_zip_compression_zstd.c +++ b/libarchive/test/test_write_format_zip_compression_zstd.c @@ -7,35 +7,6 @@ #ifdef HAVE_ZSTD_H #include -static unsigned long -bitcrc32(unsigned long c, const void *_p, size_t s) -{ - /* This is a drop-in replacement for crc32() from zlib, given that - * libzstd doesn't use CRC32 in the first place, let alone has a - * function for it. Libarchive should be able to correctly generate - * ZSTD-compressed zip archives (including correct CRCs) even when - * zlib is unavailable, and this function helps us verify that. Yes, - * this is very, very slow and unsuitable for production use, but - * it's very, very obviously correct, compact, and works well for - * this particular usage. Libarchive internally uses a much more - * efficient implementation when zlib is unavailable */ - const unsigned char *p = _p; - int bitctr; - - if (p == NULL) - return (0); - - for (; s > 0; --s) { - c ^= *p++; - for (bitctr = 8; bitctr > 0; --bitctr) { - if (c & 1) c = (c >> 1); - else c = (c >> 1) ^ 0xedb88320; - c ^= 0x80000000; - } - } - return (c); -} - /* File data */ static const char file_name[] = "file"; static const char file_data1[] = {'~', 'Z', '`', '^', 'Y', 'X', 'N', 'W', 'V', 'G', 'H', 'I', 'J'}; diff --git a/libarchive/test/test_write_format_zip_entry_size_unset.c b/libarchive/test/test_write_format_zip_entry_size_unset.c index ed2fb2835..e9a9ca141 100644 --- a/libarchive/test/test_write_format_zip_entry_size_unset.c +++ b/libarchive/test/test_write_format_zip_entry_size_unset.c @@ -42,38 +42,6 @@ static const short folder_gid = 40; #define ZIP_ENTRY_FLAG_LENGTH_AT_END (1 << 3) -static unsigned long -bitcrc32(unsigned long c, const void *_p, size_t s) -{ - /* This is a drop-in replacement for crc32() from zlib. - * Libarchive should be able to correctly generate - * uncompressed zip archives (including correct CRCs) even - * when zlib is unavailable, and this function helps us verify - * that. Yes, this is very, very slow and unsuitable for - * production use, but it's correct, compact, and works well - * enough for this particular usage. Libarchive internally - * uses a much more efficient implementation. */ - const unsigned char *p = _p; - int bitctr; - - if (p == NULL) - return (0); - - for (; s > 0; --s) - { - c ^= *p++; - for (bitctr = 8; bitctr > 0; --bitctr) - { - if (c & 1) - c = (c >> 1); - else - c = (c >> 1) ^ 0xedb88320; - c ^= 0x80000000; - } - } - return (c); -} - static void write_archive(struct archive *a) { struct archive_entry *entry = archive_entry_new(); diff --git a/libarchive/test/test_write_format_zip_file.c b/libarchive/test/test_write_format_zip_file.c index 24b238436..e5314b078 100644 --- a/libarchive/test/test_write_format_zip_file.c +++ b/libarchive/test/test_write_format_zip_file.c @@ -35,34 +35,6 @@ * with a single file written to it. */ -static unsigned long -bitcrc32(unsigned long c, void *_p, size_t s) -{ - /* This is a drop-in replacement for crc32() from zlib. - * Libarchive should be able to correctly generate - * uncompressed zip archives (including correct CRCs) even - * when zlib is unavailable, and this function helps us verify - * that. Yes, this is very, very slow and unsuitable for - * production use, but it's correct, compact, and works well - * enough for this particular usage. Libarchive internally - * uses a much more efficient implementation. */ - const unsigned char *p = _p; - int bitctr; - - if (p == NULL) - return (0); - - for (; s > 0; --s) { - c ^= *p++; - for (bitctr = 8; bitctr > 0; --bitctr) { - if (c & 1) c = (c >> 1); - else c = (c >> 1) ^ 0xedb88320; - c ^= 0x80000000; - } - } - return (c); -} - DEFINE_TEST(test_write_format_zip_file) { struct archive *a; diff --git a/libarchive/test/test_write_format_zip_file_zip64.c b/libarchive/test/test_write_format_zip_file_zip64.c index 9d1bdffbe..28b22a4fa 100644 --- a/libarchive/test/test_write_format_zip_file_zip64.c +++ b/libarchive/test/test_write_format_zip_file_zip64.c @@ -35,34 +35,6 @@ * with a single file written to it that uses Zip64 extensions. */ -static unsigned long -bitcrc32(unsigned long c, void *_p, size_t s) -{ - /* This is a drop-in replacement for crc32() from zlib. - * Libarchive should be able to correctly generate - * uncompressed zip archives (including correct CRCs) even - * when zlib is unavailable, and this function helps us verify - * that. Yes, this is very, very slow and unsuitable for - * production use, but it's correct, compact, and works well - * enough for this particular usage. Libarchive internally - * uses a much more efficient implementation. */ - const unsigned char *p = _p; - int bitctr; - - if (p == NULL) - return (0); - - for (; s > 0; --s) { - c ^= *p++; - for (bitctr = 8; bitctr > 0; --bitctr) { - if (c & 1) c = (c >> 1); - else c = (c >> 1) ^ 0xedb88320; - c ^= 0x80000000; - } - } - return (c); -} - DEFINE_TEST(test_write_format_zip_file_zip64) { struct archive *a; diff --git a/libarchive/test/test_write_format_zip_stream.c b/libarchive/test/test_write_format_zip_stream.c index c9d2db0b0..e67d2b8da 100644 --- a/libarchive/test/test_write_format_zip_stream.c +++ b/libarchive/test/test_write_format_zip_stream.c @@ -31,34 +31,6 @@ * written in streaming mode WITHOUT Zip64 extensions enabled. */ -static unsigned long -bitcrc32(unsigned long c, void *_p, size_t s) -{ - /* This is a drop-in replacement for crc32() from zlib. - * Libarchive should be able to correctly generate - * uncompressed zip archives (including correct CRCs) even - * when zlib is unavailable, and this function helps us verify - * that. Yes, this is very, very slow and unsuitable for - * production use, but it's correct, compact, and works well - * enough for this particular usage. Libarchive internally - * uses a much more efficient implementation. */ - const unsigned char *p = _p; - int bitctr; - - if (p == NULL) - return (0); - - for (; s > 0; --s) { - c ^= *p++; - for (bitctr = 8; bitctr > 0; --bitctr) { - if (c & 1) c = (c >> 1); - else c = (c >> 1) ^ 0xedb88320; - c ^= 0x80000000; - } - } - return (c); -} - DEFINE_TEST(test_write_format_zip_stream) { struct archive *a; diff --git a/test_utils/test_utils.c b/test_utils/test_utils.c index ebb5789c6..be717f4cc 100644 --- a/test_utils/test_utils.c +++ b/test_utils/test_utils.c @@ -110,6 +110,34 @@ fill_with_pseudorandom_data(void *buffer, size_t size) fill_with_pseudorandom_data_seed(seed, buffer, size); } +unsigned long +bitcrc32(unsigned long c, const void *_p, size_t s) +{ + /* This is a drop-in replacement for crc32() from zlib. + * Libarchive should be able to correctly read archives (including + * correct CRCs) even when zlib is unavailable, and this function + * helps us verify that. Yes, this is very, very slow and unsuitable + * for production use, but it's obviously correct, compact, and + * works well enough for this particular usage. Libarchive + * internally uses a much more efficient implementation if zlib is + * unavailable. */ + const unsigned char *p = _p; + char bitctr; + + if (p == NULL) + return (0); + + for (; s > 0; --s) { + c ^= *p++; + for (bitctr = 8; bitctr > 0; --bitctr) { + if (c & 1) c = (c >> 1); + else c = (c >> 1) ^ 0xedb88320; + c ^= 0x80000000; + } + } + return (c); +} + /* Read little-endian integers */ unsigned short i2le(const void* p_) diff --git a/test_utils/test_utils.h b/test_utils/test_utils.h index b0b2a50ef..a398141c6 100644 --- a/test_utils/test_utils.h +++ b/test_utils/test_utils.h @@ -31,7 +31,9 @@ #include /* Fill a buffer with pseudorandom data */ -void fill_with_pseudorandom_data(void* buffer, size_t size); +void fill_with_pseudorandom_data(void*, size_t); +/* A simplistic CRC-32 function for testing purposes */ +unsigned long bitcrc32(unsigned long, const void*, size_t); /* Read little-endian integers */ unsigned short i2le(const void*); unsigned int i4le(const void*);