mirror of
https://github.com/davidgiven/fluxengine.git
synced 2025-10-31 11:17:01 -07:00
Hack in compressed flux streams; I don't like this much.
This commit is contained in:
42
lib/bytes.cc
42
lib/bytes.cc
@@ -1,5 +1,7 @@
|
||||
#include "globals.h"
|
||||
#include "bytes.h"
|
||||
#include "fmt/format.h"
|
||||
#include <zlib.h>
|
||||
|
||||
uint8_t toByte(
|
||||
std::vector<bool>::const_iterator start,
|
||||
@@ -69,3 +71,43 @@ void BitAccumulator::finish()
|
||||
_bitcount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<uint8_t> compress(const std::vector<uint8_t>& source)
|
||||
{
|
||||
size_t destsize = compressBound(source.size());
|
||||
std::vector<uint8_t> dest(destsize);
|
||||
if (compress(&dest[0], &destsize, &source[0], source.size()) != Z_OK)
|
||||
Error() << "error compressing data";
|
||||
dest.resize(destsize);
|
||||
return dest;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> decompress(const std::vector<uint8_t>& source)
|
||||
{
|
||||
std::vector<uint8_t> output;
|
||||
std::vector<uint8_t> outputBuffer(1024*1024);
|
||||
|
||||
z_stream stream = {};
|
||||
inflateInit(&stream);
|
||||
stream.avail_in = source.size();
|
||||
stream.next_in = (uint8_t*) &source[0];
|
||||
|
||||
int ret;
|
||||
do
|
||||
{
|
||||
stream.avail_out = outputBuffer.size();
|
||||
stream.next_out = &outputBuffer[0];
|
||||
ret = inflate(&stream, Z_NO_FLUSH);
|
||||
if (ret == Z_BUF_ERROR)
|
||||
ret = Z_OK;
|
||||
if ((ret != Z_OK) && (ret != Z_STREAM_END))
|
||||
Error() << fmt::format(
|
||||
"failed to decompress data: {}", stream.msg ? stream.msg : "(unknown error)");
|
||||
|
||||
output.insert(output.end(), outputBuffer.begin(), outputBuffer.end() - stream.avail_out);
|
||||
}
|
||||
while (ret != Z_STREAM_END);
|
||||
inflateEnd(&stream);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user