#endif
#define __LIBARCHIVE_BUILD
-#include <archive_crc32.h>
/*
* Extract a non-encoded file.
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);
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));
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));
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));
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));
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));
/* 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; \
goto fn_exit;
}
- computed_crc = crc32(0, buf, fsize);
+ computed_crc = bitcrc32(0, buf, fsize);
assertEqualInt(computed_crc, crc);
ret = 0;
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();
/* 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();
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);
#include "test.h"
#define __LIBARCHIVE_BUILD
-#include <archive_crc32.h>
static
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;
/* ok */
}
- computed_crc = crc32(computed_crc, buf, bytes_read);
+ computed_crc = bitcrc32(computed_crc, buf, bytes_read);
}
assertEqualInt(computed_crc, crc);
* 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;
#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'};
/* 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;
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 */
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;
#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'};
#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();
* 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;
* 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;
* 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;
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_)
#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*);