void verify_compression()

in src/native/tools/zstd_compress_file/compress_utility.cpp [105:164]


void verify_compression(fs::path uncompressed_path, fs::path *delta_basis_path, fs::path compressed_path)
{
	auto uncompressed_hash = get_file_hash(uncompressed_path);
	auto uncompressed_hash_string =
		archive_diff::hashing::data_to_hexstring(uncompressed_hash.data(), uncompressed_hash.size());
	printf("Uncompressed file hash: %s\n", uncompressed_hash_string.c_str());

#ifdef WIN32
	std::vector<char> buffer;
	const size_t buffer_capacity = 1024;
	buffer.reserve(buffer_capacity);
	auto err = tmpnam_s(buffer.data(), buffer.capacity());
	if (err)
	{
		std::cout << "tmpnam_s() failed with " << err << std::endl;
		throw std::exception();
	}
	auto test_uncompressed_path = fs::path(buffer.data());
#else
	char name_template[] = "compress_verify_XXXXXX";
	int fd               = mkstemp(name_template);
	if (fd == -1)
	{
		std::cout << "Couldn't open temp file to verify compression. Errno: " << std::to_string(errno) << std::endl;
		throw std::exception();
	}
	close(fd);
	auto test_uncompressed_path = fs::temp_directory_path() / name_template;
#endif

	std::cout << "Testing decompression to temp path: " << test_uncompressed_path << std::endl;

	if (delta_basis_path)
	{
		decompress_file(compressed_path, *delta_basis_path, test_uncompressed_path);
	}
	else
	{
		decompress_file(compressed_path, L"", test_uncompressed_path);
	}

	auto test_uncompressed_hash = get_file_hash(test_uncompressed_path);
	auto test_uncompressed_hash_string =
		archive_diff::hashing::data_to_hexstring(test_uncompressed_hash.data(), test_uncompressed_hash.size());
	printf("Test Uncompressed file hash: %s\n", test_uncompressed_hash_string.c_str());

	fs::remove(test_uncompressed_path);

	if (test_uncompressed_hash.size() != uncompressed_hash.size())
	{
		printf("Hashes of uncompressed data has mismatch size!\n");
		throw std::exception();
	}

	if (0 != memcmp(test_uncompressed_hash.data(), uncompressed_hash.data(), uncompressed_hash.size()))
	{
		printf("Hashes of uncompressed data has mismatch!\n");
		throw std::exception();
	}
}