[Compression] Implement zstd for fastfiles (unfinished)

This commit is contained in:
momo5502 2017-06-26 19:01:52 +02:00
parent 4b75665eb0
commit 19f49a0273
4 changed files with 80 additions and 1 deletions

View File

@ -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";

View File

@ -74,6 +74,11 @@ template <size_t S> class Sizer { };
#pragma warning(disable: 6387)
#include <zlib.h>
#include <zstd.h>
//#define ZWRAP_USE_ZSTD 1
//#include <zstd_zlibwrapper.h>
#include <curses.h>
#include <mongoose.h>
#include <json11.hpp>

View File

@ -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<char>(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<uint8_t>(CHUNK);
const char* dataPtr = data.data();
do
{
stream.avail_in = std::min(static_cast<size_t>(CHUNK), data.size() - (dataPtr - data.data()));
stream.next_in = reinterpret_cast<const uint8_t*>(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<const char*>(dest), CHUNK - stream.avail_out);
} while (stream.avail_out == 0);
} while (ret != Z_STREAM_END);
inflateEnd(&stream);
return buffer;
}
};
}

View File

@ -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);
};
};
}