Review fixes.

This commit is contained in:
Yuri Gorshenin 2016-12-08 12:04:53 +03:00
parent d6816bb4ee
commit f2d282359b
3 changed files with 42 additions and 26 deletions

View file

@ -32,7 +32,7 @@ UNIT_TEST(ZLib_Smoke)
}
TestInflateDeflate("");
TestInflateDeflate("Hello, World");
TestInflateDeflate("Hello, World!");
}
UNIT_TEST(ZLib_Large)

View file

@ -4,7 +4,7 @@ namespace coding
{
namespace
{
int LevelToInt(ZLib::Level level)
int ToInt(ZLib::Level level)
{
switch (level)
{
@ -17,12 +17,17 @@ int LevelToInt(ZLib::Level level)
} // namespace
// ZLib::Processor ---------------------------------------------------------------------------------
ZLib::Processor::Processor(char const * data, size_t size) : m_init(false)
ZLib::Processor::Processor(void const * data, size_t size) noexcept : m_init(false)
{
m_stream.next_in = const_cast<unsigned char *>(reinterpret_cast<unsigned char const *>(data));
// next_in is defined as z_const (see
// http://www.zlib.net/manual.html). Sometimes it's a const (when
// ZLIB_CONST is defined), sometimes not, it depends on the local
// zconf.h. So, for portability, const_cast<...> is used here, but
// in any case, zlib does not modify |data|.
m_stream.next_in = static_cast<unsigned char *>(const_cast<void *>(data));
m_stream.avail_in = size;
m_stream.next_out = reinterpret_cast<unsigned char *>(m_buffer);
m_stream.next_out = m_buffer;
m_stream.avail_out = kBufferSize;
m_stream.zalloc = Z_NULL;
@ -43,14 +48,14 @@ bool ZLib::Processor::BufferIsFull() const
}
// ZLib::Deflate -----------------------------------------------------------------------------------
ZLib::DeflateProcessor::DeflateProcessor(char const * data, size_t size, ZLib::Level level)
ZLib::DeflateProcessor::DeflateProcessor(void const * data, size_t size, ZLib::Level level) noexcept
: Processor(data, size)
{
int const ret = deflateInit(&m_stream, LevelToInt(level));
int const ret = deflateInit(&m_stream, ToInt(level));
m_init = (ret == Z_OK);
}
ZLib::DeflateProcessor::~DeflateProcessor()
ZLib::DeflateProcessor::~DeflateProcessor() noexcept
{
if (m_init)
deflateEnd(&m_stream);
@ -63,13 +68,14 @@ int ZLib::DeflateProcessor::Process(int flush)
}
// ZLib::Inflate -----------------------------------------------------------------------------------
ZLib::InflateProcessor::InflateProcessor(char const * data, size_t size) : Processor(data, size)
ZLib::InflateProcessor::InflateProcessor(void const * data, size_t size) noexcept
: Processor(data, size)
{
int const ret = inflateInit(&m_stream);
m_init = (ret == Z_OK);
}
ZLib::InflateProcessor::~InflateProcessor()
ZLib::InflateProcessor::~InflateProcessor() noexcept
{
if (m_init)
inflateEnd(&m_stream);

View file

@ -1,4 +1,7 @@
#pragma once
#include "base/assert.hpp"
#include "base/macros.hpp"
#include "std/algorithm.hpp"
#include "std/string.hpp"
@ -11,8 +14,8 @@ namespace coding
//
// *NOTE* All Inflate() and Deflate() methods may return false in case
// of errors. In this case the output sequence may be already
// partially formed, so the user needs to implement its own roll-back
// strategy.
// partially formed, so the user needs to implement their own
// roll-back strategy.
class ZLib
{
public:
@ -25,9 +28,9 @@ public:
};
template <typename OutIt>
static bool Deflate(char const * data, size_t size, Level level, OutIt out)
static bool Deflate(void const * data, size_t size, Level level, OutIt out)
{
if (!data)
if (data == nullptr)
return false;
DeflateProcessor processor(data, size, level);
return Process(processor, out);
@ -40,9 +43,9 @@ public:
}
template <typename OutIt>
static bool Inflate(char const * data, size_t size, OutIt out)
static bool Inflate(void const * data, size_t size, OutIt out)
{
if (!data)
if (data == nullptr)
return false;
InflateProcessor processor(data, size);
return Process(processor, out);
@ -60,9 +63,10 @@ private:
public:
static size_t constexpr kBufferSize = 1024;
Processor(char const * data, size_t size);
Processor(void const * data, size_t size) noexcept;
virtual ~Processor() noexcept = default;
inline bool IsInit() const { return m_init; }
inline bool IsInit() const noexcept { return m_init; }
bool ConsumedAll() const;
bool BufferIsFull() const;
@ -71,32 +75,38 @@ private:
{
ASSERT(IsInit(), ());
copy(m_buffer, m_buffer + kBufferSize - m_stream.avail_out, out);
m_stream.next_out = reinterpret_cast<unsigned char *>(m_buffer);
m_stream.next_out = m_buffer;
m_stream.avail_out = kBufferSize;
}
protected:
z_stream m_stream;
bool m_init;
char m_buffer[kBufferSize];
unsigned char m_buffer[kBufferSize];
DISALLOW_COPY_AND_MOVE(Processor);
};
class DeflateProcessor : public Processor
class DeflateProcessor final : public Processor
{
public:
DeflateProcessor(char const * data, size_t size, Level level);
~DeflateProcessor();
DeflateProcessor(void const * data, size_t size, Level level) noexcept;
virtual ~DeflateProcessor() noexcept override;
int Process(int flush);
DISALLOW_COPY_AND_MOVE(DeflateProcessor);
};
class InflateProcessor : public Processor
class InflateProcessor final : public Processor
{
public:
InflateProcessor(char const * data, size_t size);
~InflateProcessor();
InflateProcessor(void const * data, size_t size) noexcept;
virtual ~InflateProcessor() noexcept override;
int Process(int flush);
DISALLOW_COPY_AND_MOVE(InflateProcessor);
};
template <typename Processor, typename OutIt>