diff options
author | Daniel Wilhelm <shieldwed@outlook.com> | 2017-04-20 16:55:28 -0600 |
---|---|---|
committer | Daniel Wilhelm <shieldwed@outlook.com> | 2017-04-20 16:59:56 -0600 |
commit | 823740e1ffa2b3bd39f8dea8062f5c5a0d9c741b (patch) | |
tree | 63222010af3b90a36f29c1c9c360116973cdff38 /wx+/zlib_wrap.h | |
parent | add .gitattributes for less line ending hassles (diff) | |
download | FreeFileSync-823740e1ffa2b3bd39f8dea8062f5c5a0d9c741b.tar.gz FreeFileSync-823740e1ffa2b3bd39f8dea8062f5c5a0d9c741b.tar.bz2 FreeFileSync-823740e1ffa2b3bd39f8dea8062f5c5a0d9c741b.zip |
normalize most lineendings
Diffstat (limited to 'wx+/zlib_wrap.h')
-rwxr-xr-x | wx+/zlib_wrap.h | 228 |
1 files changed, 114 insertions, 114 deletions
diff --git a/wx+/zlib_wrap.h b/wx+/zlib_wrap.h index 11d8227b..ae2e6d0e 100755 --- a/wx+/zlib_wrap.h +++ b/wx+/zlib_wrap.h @@ -1,114 +1,114 @@ -// *****************************************************************************
-// * This file is part of the FreeFileSync project. It is distributed under *
-// * GNU General Public License: http://www.gnu.org/licenses/gpl-3.0 *
-// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
-// *****************************************************************************
-
-#ifndef ZLIB_WRAP_H_428597064566
-#define ZLIB_WRAP_H_428597064566
-
-#include <zen/serialize.h>
-
-
-namespace zen
-{
-class ZlibInternalError {};
-
-// 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 ZlibInternalError
-//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 ZlibInternalError
-
-
-
-
-
-
-
-
-
-
-
-//######################## 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 ZlibInternalError
-size_t zlib_decompress(const void* src, size_t srcLen, void* trg, size_t trgLen); //throw ZlibInternalError
-}
-
-
-template <class BinContainer>
-BinContainer compress(const BinContainer& stream, int level) //throw ZlibInternalError
-{
- 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::copy(reinterpret_cast<const char*>(&uncompressedSize),
- reinterpret_cast<const char*>(&uncompressedSize) + sizeof(uncompressedSize),
- &*contOut.begin());
-
- 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 ZlibInternalError
- 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 ZlibInternalError
-{
- 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 ZlibInternalError();
- std::copy(&*stream.begin(),
- &*stream.begin() + sizeof(uncompressedSize),
- reinterpret_cast<char*>(&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 ZlibInternalError();
-
- try
- {
- contOut.resize(static_cast<size_t>(uncompressedSize)); //throw std::bad_alloc
- }
- catch (std::bad_alloc&) //most likely due to data corruption!
- {
- throw ZlibInternalError();
- }
-
- const size_t bytesWritten = impl::zlib_decompress(&*stream.begin() + sizeof(uncompressedSize),
- stream.size() - sizeof(uncompressedSize),
- &*contOut.begin(),
- static_cast<size_t>(uncompressedSize)); //throw ZlibInternalError
- if (bytesWritten != static_cast<size_t>(uncompressedSize))
- throw ZlibInternalError();
- }
- return contOut;
-}
-}
-
-#endif //ZLIB_WRAP_H_428597064566
+// ***************************************************************************** +// * This file is part of the FreeFileSync project. It is distributed under * +// * GNU General Public License: http://www.gnu.org/licenses/gpl-3.0 * +// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved * +// ***************************************************************************** + +#ifndef ZLIB_WRAP_H_428597064566 +#define ZLIB_WRAP_H_428597064566 + +#include <zen/serialize.h> + + +namespace zen +{ +class ZlibInternalError {}; + +// 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 ZlibInternalError +//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 ZlibInternalError + + + + + + + + + + + +//######################## 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 ZlibInternalError +size_t zlib_decompress(const void* src, size_t srcLen, void* trg, size_t trgLen); //throw ZlibInternalError +} + + +template <class BinContainer> +BinContainer compress(const BinContainer& stream, int level) //throw ZlibInternalError +{ + 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::copy(reinterpret_cast<const char*>(&uncompressedSize), + reinterpret_cast<const char*>(&uncompressedSize) + sizeof(uncompressedSize), + &*contOut.begin()); + + 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 ZlibInternalError + 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 ZlibInternalError +{ + 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 ZlibInternalError(); + std::copy(&*stream.begin(), + &*stream.begin() + sizeof(uncompressedSize), + reinterpret_cast<char*>(&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 ZlibInternalError(); + + try + { + contOut.resize(static_cast<size_t>(uncompressedSize)); //throw std::bad_alloc + } + catch (std::bad_alloc&) //most likely due to data corruption! + { + throw ZlibInternalError(); + } + + const size_t bytesWritten = impl::zlib_decompress(&*stream.begin() + sizeof(uncompressedSize), + stream.size() - sizeof(uncompressedSize), + &*contOut.begin(), + static_cast<size_t>(uncompressedSize)); //throw ZlibInternalError + if (bytesWritten != static_cast<size_t>(uncompressedSize)) + throw ZlibInternalError(); + } + return contOut; +} +} + +#endif //ZLIB_WRAP_H_428597064566 |