8000 miniz/miniz_tester.cpp at master · barnson/miniz · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
1809 lines (1509 loc) · 50.8 KB

miniz_tester.cpp

File metadata and controls

1809 lines (1509 loc) · 50.8 KB
{
in_file_buf_ofs += (uint)num_in_bytes;
assert(in_file_buf_ofs <= in_file_buf_size);
}
out_file_buf_ofs += (uint)num_out_bytes;
if ((out_file_buf_ofs == out_buf_size) || (status == Z_STREAM_END))
{
if (fwrite(out_file_buf, 1, static_cast<uint>(out_file_buf_ofs), pOutFile) != out_file_buf_ofs)
{
print_error("Failure writing to destination file!\n");
_aligned_free(in_file_buf);
_aligned_free(out_file_buf);
inflateEnd(&zstream);
fclose(pInFile);
fclose(pOutFile);
return false;
}
out_file_buf_ofs = 0;
}
if (num_out_bytes > dst_bytes_left)
{
print_error("Decompressor wrote too many bytes to destination file!\n");
_aligned_free(in_file_buf);
_aligned_free(out_file_buf);
inflateEnd(&zstream);
fclose(pInFile);
fclose(pOutFile);
return false;
}
dst_bytes_left -= num_out_bytes;
if (status != Z_OK)
break;
}
_aligned_free(in_file_buf);
in_file_buf = NULL;
_aligned_free(out_file_buf);
out_file_buf = NULL;
src_bytes_left += (in_file_buf_size - in_file_buf_ofs);
uint32 adler32 = zstream.adler;
inflateEnd(&zstream);
timer_ticks end_time = timer::get_ticks();
double total_time = timer::ticks_to_secs(my_max(1, end_time - start_time));
fclose(pInFile);
pInFile = NULL;
fclose(pOutFile);
pOutFile = NULL;
if (status != Z_STREAM_END)
{
print_error("Decompression FAILED with status %i\n", status);
return false;
}
if ((src_file_size < UINT_MAX) && (orig_file_size < UINT_MAX))
{
if ((((size_t)zstream.total_in + total_header_bytes) != src_file_size) || (zstream.total_out != orig_file_size))
{
print_error("Decompression FAILED to consume all input or write all expected output!\n");
return false;
}
}
if (dst_bytes_left)
{
print_error("Decompressor FAILED to output the entire output file!\n");
return false;
}
if (src_bytes_left)
{
print_error("Decompressor FAILED to read " QUAD_INT_FMT " bytes from input buffer\n", src_bytes_left);
}
printf("Success\n");
printf("Source file size: " QUAD_INT_FMT ", Decompressed file size: " QUAD_INT_FMT "\n", src_file_size, orig_file_size);
if (zlib_header) printf("Decompressed adler32: 0x%08X\n", adler32);
printf("Overall decompression time (decompression init+I/O+decompression): %3.6f\n Consumption rate: %9.1f bytes/sec, Decompression rate: %9.1f bytes/sec\n", total_time, src_file_size / total_time, orig_file_size / total_time);
printf("Decompression only time (not counting decompression init or I/O): %3.6f\n Consumption rate: %9.1f bytes/sec, Decompression rate: %9.1f bytes/sec\n", decomp_only_time, src_file_size / decomp_only_time, orig_file_size / decomp_only_time);
return true;
}
static bool compare_files(const char *pFilename1, const char* pFilename2)
{
FILE* pFile1 = open_file_with_retries(pFilename1, "rb");
if (!pFile1)
{
print_error("Failed opening file: %s\n", pFilename1);
return false;
}
FILE* pFile2 = open_file_with_retries(pFilename2, "rb");
if (!pFile2)
{
print_error("Failed opening file: %s\n", pFilename2);
fclose(pFile1);
return false;
}
_fseeki64(pFile1, 0, SEEK_END);
int64 fileSize1 = _ftelli64(pFile1);
_fseeki64(pFile1, 0, SEEK_SET);
_fseeki64(pFile2, 0, SEEK_END);
int64 fileSize2 = _ftelli64(pFile2);
_fseeki64(pFile2, 0, SEEK_SET);
if (fileSize1 != fileSize2)
{
print_error("Files to compare are not the same size: %I64i vs. %I64i.\n", fileSize1, fileSize2);
fclose(pFile1);
fclose(pFile2);
return false;
}
const uint cBufSize = 1024 * 1024;
std::vector<uint8> buf1(cBufSize);
std::vector<uint8> buf2(cBufSize);
int64 bytes_remaining = fileSize1;
while (bytes_remaining)
{
const uint bytes_to_read = static_cast<uint>(my_min(cBufSize, bytes_remaining));
if (fread(&buf1.front(), bytes_to_read, 1, pFile1) != 1)
{
print_error("Failed reading from file: %s\n", pFilename1);
fclose(pFile1);
fclose(pFile2);
return false;
}
if (fread(&buf2.front(), bytes_to_read, 1, pFile2) != 1)
{
print_error("Failed reading from file: %s\n", pFilename2);
fclose(pFile1);
fclose(pFile2);
return false;
}
if (memcmp(&buf1.front(), &buf2.front(), bytes_to_read) != 0)
{
print_error("File data comparison failed!\n");
fclose(pFile1);
fclose(pFile2);
return false;
}
bytes_remaining -= bytes_to_read;
}
fclose(pFile1);
fclose(pFile2);
return true;
}
static bool zip_create(const char *pZip_filename, const char *pSrc_filename)
{
mz_zip_archive zip;
memset(&zip, 0, sizeof(zip));
if ((rand() % 100) >= 10)
zip.m_file_offset_alignment = 1 << (rand() & 15);
if (!mz_zip_writer_init_file(&zip, pZip_filename, 65537))
{
print_error("Failed creating zip archive \"%s\" (1)!\n", pZip_filename);
return false;
}
mz_bool success = MZ_TRUE;
const char *pStr = "This is a test!This is a test!This is a test!\n";
size_t comp_size;
void *pComp_data = tdefl_compress_mem_to_heap(pStr, strlen(pStr), &comp_size, 256);
success &= mz_zip_writer_add_mem_ex(&zip, "precomp.txt", pComp_data, comp_size, "Comment", (uint16)strlen("Comment"), MZ_ZIP_FLAG_COMPRESSED_DATA, strlen(pStr), mz_crc32(MZ_CRC32_INIT, (const uint8 *)pStr, strlen(pStr)));
success &= mz_zip_writer_add_mem(&zip, "cool/", NULL, 0, 0);
success &= mz_zip_writer_add_mem(&zip, "1.txt", pStr, strlen(pStr), 9);
int n = rand() & 4095;
for (int i = 0; i < n; i++)
{
char name[256], buf[256], comment[256];
sprintf(name, "t%u.txt", i);
sprintf(buf, "%u\n", i*5377);
sprintf(comment, "comment: %u\n", i);
success &= mz_zip_writer_add_mem_ex(&zip, name, buf, strlen(buf), comment, (uint16)strlen(comment), i % 10, 0, 0);
}
const char *pTestComment = "test comment";
success &= mz_zip_writer_add_file(&zip, "test.bin", pSrc_filename, pTestComment, (uint16)strlen(pTestComment), 9);
if (ensure_file_exists_and_is_readable("changelog.txt"))
success &= mz_zip_writer_add_file(&zip, "changelog.txt", "changelog.txt", "This is a comment", (uint16)strlen("This is a comment"), 9);
if (!success)
{
mz_zip_writer_end(&zip);
remove(pZip_filename);
print_error("Failed creating zip archive \"%s\" (2)!\n", pZip_filename);
return false;
}
if (!mz_zip_writer_finalize_archive(&zip))
{
mz_zip_writer_end(&zip);
remove(pZip_filename);
print_error("Failed creating zip archive \"%s\" (3)!\n", pZip_filename);
return false;
}
mz_zip_writer_end(&zip);
struct FILE_STAT_STRUCT stat_buf;
FILE_STAT(pZip_filename, &stat_buf);
uint64 actual_file_size = stat_buf.st_size;
if (zip.m_archive_size != actual_file_size)
{
print_error("Archive's actual size and zip archive object's size differ for file \"%s\"!\n", pZip_filename);
return false;
}
printf("Created zip file \"%s\", file size: " QUAD_INT_FMT "\n", pZip_filename, zip.m_archive_size);
return true;
}
static size_t zip_write_callback(void *pOpaque, uint64 ofs, const void *pBuf, size_t n)
{
(void)pOpaque, (void)ofs, (void)pBuf, (void)n;
return n;
}
static bool zip_extract(const char *pZip_filename, const char *pDst_filename)
{
mz_zip_archive zip;
memset(&zip, 0, sizeof(zip));
if (!mz_zip_reader_init_file(&zip, pZip_filename, 0))
{
print_error("Failed opening zip archive \"%s\"!\n", pZip_filename);
return false;
}
int file_index = mz_zip_reader_locate_file(&zip, "test.bin", "test Comment", 0);
int alt_file_index = mz_zip_reader_locate_file(&zip, "test.bin", "test Comment e", 0);
if ((file_index < 0) || (alt_file_index >= 0))
{
print_error("Archive \"%s\" is missing test.bin file!\n", pZip_filename);
mz_zip_reader_end(&zip);
return false;
}
alt_file_index = mz_zip_reader_locate_file(&zip, "test.bin", NULL, 0);
if (alt_file_index != file_index)
{
print_error("mz_zip_reader_locate_file() failed!\n", pZip_filename);
mz_zip_reader_end(&zip);
return false;
}
if (!mz_zip_reader_extract_to_file(&zip, file_index, pDst_filename, 0))
{
print_error("Failed extracting test.bin from archive \"%s\"!\n", pZip_filename);
mz_zip_reader_end(&zip);
return false;
}
for (uint i = 0; i < mz_zip_reader_get_num_files(&zip); i++)
{
mz_zip_archive_file_stat stat;
if (!mz_zip_reader_file_stat(&zip, i, &stat))
{
print_error("Failed testing archive \"%s\"!\n", pZip_filename);
mz_zip_reader_end(&zip);
return false;
}
//printf("\"%s\" %I64u %I64u\n", stat.m_filename, stat.m_comp_size, stat.m_uncomp_size);
size_t size = 0;
mz_bool status = mz_zip_reader_extract_to_callback(&zip, i, zip_write_callback, NULL, 0);
if (!status)
{
print_error("Failed testing archive \"%s\"!\n", pZip_filename);
mz_zip_reader_end(&zip);
return false;
}
void *p = mz_zip_reader_extract_to_heap(&zip, i, &size, 0);
if (!p)
{
print_error("Failed testing archive \"%s\"!\n", pZip_filename);
mz_zip_reader_end(&zip);
return false;
}
free(p);
}
printf("Verified %u files\n", mz_zip_reader_get_num_files(&zip));
mz_zip_reader_end(&zip);
printf("Extracted file \"%s\"\n", pDst_filename);
return true;
}
0