]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Moving the tests' CRC-32 function to test_utils. (#2390)
authorljdarj <ljd@luigiscorner.mu>
Sat, 8 Mar 2025 03:21:23 +0000 (04:21 +0100)
committerGitHub <noreply@github.com>
Sat, 8 Mar 2025 03:21:23 +0000 (19:21 -0800)
Following up from #2284, putting the tests' CRC-32 implementation in
test_utils and having all tests use it.

14 files changed:
libarchive/test/test_read_format_7zip.c
libarchive/test/test_read_format_rar5.c
libarchive/test/test_read_format_zip.c
libarchive/test/test_write_format_zip64_stream.c
libarchive/test/test_write_format_zip_compression_bzip2.c
libarchive/test/test_write_format_zip_compression_lzmaxz.c
libarchive/test/test_write_format_zip_compression_store.c
libarchive/test/test_write_format_zip_compression_zstd.c
libarchive/test/test_write_format_zip_entry_size_unset.c
libarchive/test/test_write_format_zip_file.c
libarchive/test/test_write_format_zip_file_zip64.c
libarchive/test/test_write_format_zip_stream.c
test_utils/test_utils.c
test_utils/test_utils.h

index 8862ad5172c52c29b1a4a4627b352dfe1ff7a59b..187c02ff869b388ad7c845b67b3419c001f13d1c 100644 (file)
@@ -34,7 +34,6 @@
 #endif
 
 #define __LIBARCHIVE_BUILD
-#include <archive_crc32.h>
 
 /*
  * 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));
index 0345b1bed3465f6b732399dbe8778453b9b75d6b..6969753545654a04efe434afb07d0ded18171e62 100644 (file)
@@ -27,7 +27,7 @@
 /* Some tests will want to calculate some CRC32's, and this header can
  * help. */
 #define __LIBARCHIVE_BUILD
-#include <archive_crc32.h>
+#include <archive_endian.h>
 
 #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);
index 9e820f509c19b1c67875beb78aecad584ba35521..90d00474ba111cdbc42305f00b494d4ec579fdf7 100644 (file)
@@ -26,7 +26,6 @@
 #include "test.h"
 
 #define __LIBARCHIVE_BUILD
-#include <archive_crc32.h>
 
 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);
index 5f650bccb74dc49802a7b1997f625967a1406f51..1d8f1ff7f33f38e9dc4affb15fd2bf291672f94f 100644 (file)
  * 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;
index 969c4ddd92ff968fae792c3c54a59314eae5a9d6..7ec83309ee80d2b54809863eb417fcc3853635f0 100644 (file)
@@ -7,35 +7,6 @@
 #ifdef HAVE_BZLIB_H
 #include <bzlib.h>
 
-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'};
index 4b5be2913979ca39f415cc45d5d95b751cb4e81e..c5bb2d659a045df3da58b500d5d5dfb214e5523d 100644 (file)
@@ -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 */
index 495509c05d102938df13963fc7233a64db85640e..5f08a59e9ff9bdbe6a03727460953fd046cdf367 100644 (file)
@@ -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;
index aeef3553590055186e8a5cd817da8d7b879fee2c..002026a4c0d335c73e5d9c1e196e6d765f2dd2e3 100644 (file)
@@ -7,35 +7,6 @@
 #ifdef HAVE_ZSTD_H
 #include <zstd.h>
 
-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'};
index ed2fb2835c13d3d6a335a8715060d1d6c644f128..e9a9ca141f06b36842ce2733c4d468bd2fde93b5 100644 (file)
@@ -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();
index 24b23843613ae73d68e2b0295e4a99b8fd351abd..e5314b078e856c627233018bc2855abd237cbaca 100644 (file)
  * 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;
index 9d1bdffbe00e94344deb9100b49bc362f8a03620..28b22a4facf5597a2d7b5bf1fcbba76d1b46f86a 100644 (file)
  * 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;
index c9d2db0b0fb48458a068473a5de2f867d0951ee3..e67d2b8dae4a41a63578671c5037507357216022 100644 (file)
  * 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;
index ebb5789c6936274d24e78272c15b55b2a296a3aa..be717f4ccfe09a2bd63f43a1930ee644ff43a7a3 100644 (file)
@@ -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_)
index b0b2a50efae6f2e6f75875c6626b65000097c297..a398141c6f87c18ce2047d9699373e909bc6c71c 100644 (file)
@@ -31,7 +31,9 @@
 #include <stdint.h>
 
 /* 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*);