diff options
author | B. Stack <bgstack15@gmail.com> | 2022-10-11 15:17:59 +0000 |
---|---|---|
committer | B. Stack <bgstack15@gmail.com> | 2022-10-11 15:17:59 +0000 |
commit | 38c826621a39831d1bdc78aa9e45cc592db3e77f (patch) | |
tree | a49cfd729d9793681a57fa6f7409b0f0848e9ede /zen/zlib_wrap.h | |
parent | Merge branch 'b11.25' into 'master' (diff) | |
parent | add upstream 11.26 (diff) | |
download | FreeFileSync-38c826621a39831d1bdc78aa9e45cc592db3e77f.tar.gz FreeFileSync-38c826621a39831d1bdc78aa9e45cc592db3e77f.tar.bz2 FreeFileSync-38c826621a39831d1bdc78aa9e45cc592db3e77f.zip |
Merge branch 'b11.26' into 'master'11.26
add upstream 11.26
See merge request opensource-tracking/FreeFileSync!49
Diffstat (limited to 'zen/zlib_wrap.h')
-rw-r--r-- | zen/zlib_wrap.h | 94 |
1 files changed, 8 insertions, 86 deletions
diff --git a/zen/zlib_wrap.h b/zen/zlib_wrap.h index 41d7428a..d672707b 100644 --- a/zen/zlib_wrap.h +++ b/zen/zlib_wrap.h @@ -7,7 +7,7 @@ #ifndef ZLIB_WRAP_H_428597064566 #define ZLIB_WRAP_H_428597064566 -#include "serialize.h" +#include <functional> #include "sys_error.h" @@ -16,21 +16,21 @@ namespace zen // compression level must be between 0 and 9: // 0: no compression // 9: best compression -template <class BinContainer> //as specified in serialize.h -BinContainer compress(const BinContainer& stream, int level); //throw SysError +std::string compress(const std::string_view& stream, int level); //throw SysError //caveat: output stream is physically larger than input! => strip additional reserved space if needed: "BinContainer(output.begin(), output.end())" -template <class BinContainer> -BinContainer decompress(const BinContainer& stream); //throw SysError +std::string decompress(const std::string_view& stream); //throw SysError class InputStreamAsGzip //convert input stream into gzip on the fly { public: - explicit InputStreamAsGzip( //throw SysError - const std::function<size_t(void* buffer, size_t bytesToRead)>& readBlock /*throw X; returning 0 signals EOF: Posix read() semantics*/); + explicit InputStreamAsGzip(const std::function<size_t(void* buffer, size_t bytesToRead)>& tryReadBlock /*throw X; may return short, only 0 means EOF!*/, + size_t blockSize); //throw SysError ~InputStreamAsGzip(); + size_t getBlockSize() const; + size_t read(void* buffer, size_t bytesToRead); //throw SysError, X; return "bytesToRead" bytes unless end of stream! private: @@ -38,85 +38,7 @@ private: const std::unique_ptr<Impl> pimpl_; }; -std::string compressAsGzip(const void* buffer, size_t bufSize); //throw SysError - - - - - - -//######################## implementation ########################## -namespace impl -{ -size_t zlib_compressBound(size_t len); -size_t zlib_compress (const void* src, size_t srcLen, void* trg, size_t trgLen, int level); //throw SysError -size_t zlib_decompress(const void* src, size_t srcLen, void* trg, size_t trgLen); //throw SysError -} - - -template <class BinContainer> -BinContainer compress(const BinContainer& stream, int level) //throw SysError -{ - BinContainer contOut; - if (!stream.empty()) //don't dereference iterator into empty container! - { - //save uncompressed stream size for decompression - const uint64_t uncompressedSize = stream.size(); //use portable number type! - contOut.resize(sizeof(uncompressedSize)); - std::memcpy(&contOut[0], &uncompressedSize, sizeof(uncompressedSize)); - - const size_t bufferEstimate = impl::zlib_compressBound(stream.size()); //upper limit for buffer size, larger than input size!!! - - contOut.resize(contOut.size() + bufferEstimate); - - const size_t bytesWritten = impl::zlib_compress(&*stream.begin(), - stream.size(), - &*contOut.begin() + contOut.size() - bufferEstimate, - bufferEstimate, - level); //throw SysError - if (bytesWritten < bufferEstimate) - contOut.resize(contOut.size() - (bufferEstimate - bytesWritten)); //caveat: unsigned arithmetics - //caveat: physical memory consumption still *unchanged*! - } - return contOut; -} - - -template <class BinContainer> -BinContainer decompress(const BinContainer& stream) //throw SysError -{ - BinContainer contOut; - if (!stream.empty()) //don't dereference iterator into empty container! - { - //retrieve size of uncompressed data - uint64_t uncompressedSize = 0; //use portable number type! - if (stream.size() < sizeof(uncompressedSize)) - throw SysError(L"zlib error: stream size < 8"); - - std::memcpy(&uncompressedSize, &*stream.begin(), sizeof(uncompressedSize)); - - //attention: contOut MUST NOT be empty! Else it will pass a nullptr to zlib_decompress() => Z_STREAM_ERROR although "uncompressedSize == 0"!!! - //secondary bug: don't dereference iterator into empty container! - if (uncompressedSize == 0) //cannot be 0: compress() directly maps empty -> empty container skipping zlib! - throw SysError(L"zlib error: uncompressed size == 0"); - - try - { - contOut.resize(static_cast<size_t>(uncompressedSize)); //throw std::bad_alloc - } - //most likely this is due to data corruption: - catch (const std::length_error& e) { throw SysError(L"zlib error: " + _("Out of memory.") + L' ' + utfTo<std::wstring>(e.what())); } - catch (const std::bad_alloc& e) { throw SysError(L"zlib error: " + _("Out of memory.") + L' ' + utfTo<std::wstring>(e.what())); } - - const size_t bytesWritten = impl::zlib_decompress(&*stream.begin() + sizeof(uncompressedSize), - stream.size() - sizeof(uncompressedSize), - &*contOut.begin(), - static_cast<size_t>(uncompressedSize)); //throw SysError - if (bytesWritten != static_cast<size_t>(uncompressedSize)) - throw SysError(formatSystemError("zlib_decompress", L"", L"bytes written != uncompressed size.")); - } - return contOut; -} +std::string compressAsGzip(const std::string_view& stream); //throw SysError } #endif //ZLIB_WRAP_H_428597064566 |