Skip to content

Commit

Permalink
use z level
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerboa-app committed Feb 8, 2024
1 parent 6ea4903 commit 435bb8c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
7 changes: 5 additions & 2 deletions include/Util/z.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <vector>
#include <exception>
#include <fstream>
#include <memory>

#include <zlib.h>

namespace Hop::Util::Z
Expand All @@ -30,15 +32,16 @@ namespace Hop::Util::Z

std::vector<uint8_t> inflate(std::vector<uint8_t> & cdata, long unsigned int decompressedSize);

std::vector<uint8_t> deflate(std::vector<uint8_t> & data);
std::vector<uint8_t> deflate(std::vector<uint8_t> & data, uint8_t level = Z_DEFAULT_COMPRESSION);

std::vector<uint8_t> load(std::string file);

void save
(
std::string file,
std::vector<uint8_t> 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
);
}

Expand Down
25 changes: 17 additions & 8 deletions src/Util/z.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ namespace Hop::Util::Z
rawData.push_back(0);
}

int result = uncompress(
std::unique_ptr<uLong> n = std::make_unique<uLong>(cdata.size());

int result = uncompress2(

&rawData[0],
&decompressedSize,
&cdata[0],
cdata.size()
n.get()
);

switch (result)
Expand Down Expand Up @@ -67,21 +69,27 @@ namespace Hop::Util::Z

}

std::vector<uint8_t> deflate(std::vector<uint8_t> & data)
std::vector<uint8_t> deflate(std::vector<uint8_t> & data, uint8_t level)
{

if (level > Z_BEST_COMPRESSION)
{
level = Z_BEST_COMPRESSION;
}

std::vector<uint8_t> 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)
Expand All @@ -103,12 +111,13 @@ namespace Hop::Util::Z
(
std::string file,
std::vector<uint8_t> data,
std::string header
std::string header,
uint8_t level
)
{
uint64_t dataSize = data.size();

std::vector<uint8_t> compressedData = deflate(data);
std::vector<uint8_t> compressedData = deflate(data, level);

std::ofstream out(file,std::ios::binary);
if (out.is_open())
Expand Down
11 changes: 8 additions & 3 deletions tools/z/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@ int main(int argc, char ** argv)
bytes.push_back(c);
}

std::vector<uint8_t> zd = Hop::Util::Z::deflate(bytes);
uint8_t level = Z_DEFAULT_COMPRESSION;
if (argc >= 4)
{
level = std::stoul(argv[3]);
}

std::vector<uint8_t> 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)
Expand Down

0 comments on commit 435bb8c

Please sign in to comment.