diff options
author | Daniel Wilhelm <shieldwed@outlook.com> | 2019-02-13 21:05:15 +0000 |
---|---|---|
committer | Daniel Wilhelm <shieldwed@outlook.com> | 2019-02-13 21:05:15 +0000 |
commit | 2e618740c10a6dc7f5a8ee031a196b4ac95b1294 (patch) | |
tree | 475a67b0b138f2b1cd5f02eaab8e413f7eee62c6 /zen/zlib_wrap.h | |
parent | Merge branch '10.8' into 'master' (diff) | |
parent | 10.9 (diff) | |
download | FreeFileSync-10.9.tar.gz FreeFileSync-10.9.tar.bz2 FreeFileSync-10.9.zip |
Merge branch '10.9' into 'master'10.9
10.9
See merge request opensource-tracking/FreeFileSync!6
Diffstat (limited to 'zen/zlib_wrap.h')
-rw-r--r--[-rwxr-xr-x] | zen/zlib_wrap.h | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/zen/zlib_wrap.h b/zen/zlib_wrap.h index b92a8eba..c8647baf 100755..100644 --- a/zen/zlib_wrap.h +++ b/zen/zlib_wrap.h @@ -25,8 +25,19 @@ template <class BinContainer> BinContainer decompress(const BinContainer& stream); //throw ZlibInternalError +class InputStreamAsGzip //convert input stream into gzip on the fly +{ +public: + InputStreamAsGzip( //throw ZlibInternalError + const std::function<size_t(void* buffer, size_t bytesToRead)>& readBlock /*throw X*/); //returning 0 signals EOF: Posix read() semantics + ~InputStreamAsGzip(); + size_t read(void* buffer, size_t bytesToRead); //throw ZlibInternalError, X; return "bytesToRead" bytes unless end of stream! +private: + class Impl; + const std::unique_ptr<Impl> pimpl_; +}; @@ -52,9 +63,7 @@ BinContainer compress(const BinContainer& stream, int level) //throw ZlibInterna //save uncompressed stream size for decompression const uint64_t uncompressedSize = stream.size(); //use portable number type! contOut.resize(sizeof(uncompressedSize)); - std::copy(reinterpret_cast<const std::byte*>(&uncompressedSize), - reinterpret_cast<const std::byte*>(&uncompressedSize) + sizeof(uncompressedSize), - &*contOut.begin()); + std::memcpy(&*contOut.begin(), &uncompressedSize, sizeof(uncompressedSize)); const size_t bufferEstimate = impl::zlib_compressBound(stream.size()); //upper limit for buffer size, larger than input size!!! @@ -83,9 +92,9 @@ BinContainer decompress(const BinContainer& stream) //throw ZlibInternalError uint64_t uncompressedSize = 0; //use portable number type! if (stream.size() < sizeof(uncompressedSize)) throw ZlibInternalError(); - std::copy(&*stream.begin(), - &*stream.begin() + sizeof(uncompressedSize), - reinterpret_cast<std::byte*>(&uncompressedSize)); + + 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! |