Compress game binary diffs using zstd

This commit is contained in:
momo5502 2019-11-30 23:51:13 +01:00
parent 2546d92b92
commit b2546a24e0
6 changed files with 32 additions and 2 deletions

View File

@ -70,7 +70,7 @@ namespace binary_loader
std::string result(reinterpret_cast<char*>(diff.data()), diff.size()); std::string result(reinterpret_cast<char*>(diff.data()), diff.size());
result.append(reinterpret_cast<const char*>(&size), sizeof(size)); result.append(reinterpret_cast<const char*>(&size), sizeof(size));
result = utils::compression::zlib::compress(result); result = utils::compression::zstd::compress(result);
utils::io::write_file(file + ".diff", result); utils::io::write_file(file + ".diff", result);
} }
@ -115,7 +115,7 @@ namespace binary_loader
} }
auto delta = load_delta(mode); auto delta = load_delta(mode);
delta = utils::compression::zlib::decompress(delta); delta = utils::compression::zstd::decompress(delta);
return build_binary(base, delta); return build_binary(base, delta);
} }
} }

Binary file not shown.

Binary file not shown.

View File

@ -51,6 +51,7 @@
#include <filesystem> #include <filesystem>
#include <zlib.h> #include <zlib.h>
#include <zstd.h>
#include <diff.h> #include <diff.h>
#include <patch.h> #include <patch.h>
#include <tomcrypt.h> #include <tomcrypt.h>

View File

@ -67,4 +67,26 @@ namespace utils::compression
inflateEnd(&stream); inflateEnd(&stream);
return buffer; return buffer;
} }
std::string zstd::compress(const std::string& data)
{
memory::allocator allocator;
const auto bound = ZSTD_compressBound(data.size());
auto* buffer = allocator.allocate_array<char>(bound);
const auto size = ZSTD_compress(buffer, bound, data.data(), data.size(), ZSTD_maxCLevel());
return std::string(buffer, size);
}
std::string zstd::decompress(const std::string& data)
{
memory::allocator allocator;
const auto bound = size_t(ZSTD_getFrameContentSize(data.data(), data.size()));
auto* buffer = allocator.allocate_array<char>(bound);
const auto size = ZSTD_decompress(buffer, bound, data.data(), data.size());
return std::string(buffer, size);
}
} }

View File

@ -10,4 +10,11 @@ namespace utils::compression
static std::string compress(const std::string& data); static std::string compress(const std::string& data);
static std::string decompress(const std::string& data); static std::string decompress(const std::string& data);
}; };
class zstd final
{
public:
static std::string compress(const std::string& data);
static std::string decompress(const std::string& data);
};
}; };