From 435bb8c899fb3f1a4b24d90130058d01dc220486 Mon Sep 17 00:00:00 2001 From: Jerboa-app Date: Thu, 8 Feb 2024 06:02:57 +0000 Subject: [PATCH] use z level --- include/Util/z.h | 7 +++++-- src/Util/z.cpp | 25 +++++++++++++++++-------- tools/z/main.cpp | 11 ++++++++--- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/include/Util/z.h b/include/Util/z.h index 77bcc734..3230e1f4 100644 --- a/include/Util/z.h +++ b/include/Util/z.h @@ -5,6 +5,8 @@ #include #include #include +#include + #include namespace Hop::Util::Z @@ -30,7 +32,7 @@ namespace Hop::Util::Z std::vector inflate(std::vector & cdata, long unsigned int decompressedSize); - std::vector deflate(std::vector & data); + std::vector deflate(std::vector & data, uint8_t level = Z_DEFAULT_COMPRESSION); std::vector load(std::string file); @@ -38,7 +40,8 @@ namespace Hop::Util::Z ( std::string file, std::vector data, - std::string header = "compressed file, next line is uncompressed size" + std::string header = "compressed file, next line is uncompressed size", + uint8_t level = Z_DEFAULT_COMPRESSION ); } diff --git a/src/Util/z.cpp b/src/Util/z.cpp index 6dec5332..49ccf307 100644 --- a/src/Util/z.cpp +++ b/src/Util/z.cpp @@ -12,12 +12,14 @@ namespace Hop::Util::Z rawData.push_back(0); } - int result = uncompress( + std::unique_ptr n = std::make_unique(cdata.size()); + + int result = uncompress2( &rawData[0], &decompressedSize, &cdata[0], - cdata.size() + n.get() ); switch (result) @@ -67,21 +69,27 @@ namespace Hop::Util::Z } - std::vector deflate(std::vector & data) + std::vector deflate(std::vector & data, uint8_t level) { + + if (level > Z_BEST_COMPRESSION) + { + level = Z_BEST_COMPRESSION; + } + std::vector compressedData(data.size()*1.1+12); uint64_t dataSize = data.size(); // long unsigned int for windows rather than uint64_t long unsigned int bufferSize = compressedData.size(); - int result = compress( + int result = compress2( &compressedData[0], &bufferSize, &data[0], - dataSize - + dataSize, + level ); switch (result) @@ -103,12 +111,13 @@ namespace Hop::Util::Z ( std::string file, std::vector data, - std::string header + std::string header, + uint8_t level ) { uint64_t dataSize = data.size(); - std::vector compressedData = deflate(data); + std::vector compressedData = deflate(data, level); std::ofstream out(file,std::ios::binary); if (out.is_open()) diff --git a/tools/z/main.cpp b/tools/z/main.cpp index 070041fa..607bb686 100644 --- a/tools/z/main.cpp +++ b/tools/z/main.cpp @@ -37,14 +37,19 @@ int main(int argc, char ** argv) bytes.push_back(c); } - std::vector zd = Hop::Util::Z::deflate(bytes); + uint8_t level = Z_DEFAULT_COMPRESSION; + if (argc >= 4) + { + level = std::stoul(argv[3]); + } + + std::vector zd = Hop::Util::Z::deflate(bytes, level); std::ofstream out(file+".z",std::ios::binary); - if (argc == 3) + if (argc >= 3) { std::string option = argv[2]; - if (option == "-dump") { for (uint8_t c : zd)