From 19f49a02735dac2886928fa568d869dff9059ce1 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Mon, 26 Jun 2017 19:01:52 +0200 Subject: [PATCH] [Compression] Implement zstd for fastfiles (unfinished) --- src/Components/Modules/ZoneBuilder.cpp | 2 +- src/STDInclude.hpp | 5 ++ src/Utils/Compression.cpp | 67 ++++++++++++++++++++++++++ src/Utils/Compression.hpp | 7 +++ 4 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index 86c909ae..80013bae 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -407,7 +407,7 @@ namespace Components } #endif - zoneBuffer = Utils::Compression::ZLib::Compress(zoneBuffer); + zoneBuffer = Utils::Compression::ZStd::Compress(zoneBuffer); outBuffer.append(zoneBuffer); std::string outFile = "zone/" + this->zoneName + ".ff"; diff --git a/src/STDInclude.hpp b/src/STDInclude.hpp index a0c9b0cf..31650b36 100644 --- a/src/STDInclude.hpp +++ b/src/STDInclude.hpp @@ -74,6 +74,11 @@ template class Sizer { }; #pragma warning(disable: 6387) #include +#include + +//#define ZWRAP_USE_ZSTD 1 +//#include + #include #include #include diff --git a/src/Utils/Compression.cpp b/src/Utils/Compression.cpp index c35837b6..de213981 100644 --- a/src/Utils/Compression.cpp +++ b/src/Utils/Compression.cpp @@ -70,5 +70,72 @@ namespace Utils inflateEnd(&stream); return buffer; } + + + + + std::string ZStd::Compress(std::string data) + { + Utils::Memory::Allocator allocator; + + size_t length = (ZSTD_compressBound(data.size() + 1) + 1) * 2; + char* buffer = allocator.allocateArray(length); + + length = ZSTD_compress(buffer, length, data.data(), data.size(), ZSTD_maxCLevel()); + if (length <= 0/* || ZSTD_isError()*/) + { + return ""; + } + + data.clear(); + data.append(buffer, length); + + return data; + } + + std::string ZStd::Decompress(std::string data) + { + z_stream stream; + ZeroMemory(&stream, sizeof(stream)); + std::string buffer; + + if (inflateInit(&stream) != Z_OK) + { + return ""; + } + + int ret; + Utils::Memory::Allocator allocator; + + uint8_t* dest = allocator.allocateArray(CHUNK); + const char* dataPtr = data.data(); + + do + { + stream.avail_in = std::min(static_cast(CHUNK), data.size() - (dataPtr - data.data())); + stream.next_in = reinterpret_cast(dataPtr); + dataPtr += stream.avail_in; + + do + { + stream.avail_out = CHUNK; + stream.next_out = dest; + + ret = inflate(&stream, Z_NO_FLUSH); + if (ret != Z_OK && ret != Z_STREAM_END) + { + inflateEnd(&stream); + return ""; + } + + buffer.append(reinterpret_cast(dest), CHUNK - stream.avail_out); + + } while (stream.avail_out == 0); + + } while (ret != Z_STREAM_END); + + inflateEnd(&stream); + return buffer; + } }; } diff --git a/src/Utils/Compression.hpp b/src/Utils/Compression.hpp index 35ee73e9..2ab684ad 100644 --- a/src/Utils/Compression.hpp +++ b/src/Utils/Compression.hpp @@ -12,5 +12,12 @@ namespace Utils static std::string Compress(std::string data); static std::string Decompress(std::string data); }; + + class ZStd + { + public: + static std::string Compress(std::string data); + static std::string Decompress(std::string data); + }; }; }