zlib: add
Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
parent
790c26789a
commit
9f3d65d910
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
[submodule "UnitTests/zlib"]
|
||||||
|
path = UnitTests/zlib
|
||||||
|
url = https://github.com/madler/zlib.git
|
@ -79,7 +79,7 @@
|
|||||||
<WarningLevel>Level4</WarningLevel>
|
<WarningLevel>Level4</WarningLevel>
|
||||||
<EnablePREfast>true</EnablePREfast>
|
<EnablePREfast>true</EnablePREfast>
|
||||||
<SDLCheck>true</SDLCheck>
|
<SDLCheck>true</SDLCheck>
|
||||||
<AdditionalIncludeDirectories>..\include;$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
<AdditionalIncludeDirectories>..\include;$(VCInstallDir)UnitTest\include;zlib;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||||
<PreprocessorDefinitions>WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
<UseFullPaths>true</UseFullPaths>
|
<UseFullPaths>true</UseFullPaths>
|
||||||
<PrecompiledHeaderFile>pch.hpp</PrecompiledHeaderFile>
|
<PrecompiledHeaderFile>pch.hpp</PrecompiledHeaderFile>
|
||||||
@ -131,6 +131,48 @@
|
|||||||
<ClCompile Include="string.cpp" />
|
<ClCompile Include="string.cpp" />
|
||||||
<ClCompile Include="unicode.cpp" />
|
<ClCompile Include="unicode.cpp" />
|
||||||
<ClCompile Include="watchdog.cpp" />
|
<ClCompile Include="watchdog.cpp" />
|
||||||
|
<ClCompile Include="zlib.cpp" />
|
||||||
|
<ClCompile Include="zlib\adler32.c">
|
||||||
|
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||||
|
<WarningLevel>TurnOffAllWarnings</WarningLevel>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\compress.c">
|
||||||
|
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||||
|
<WarningLevel>TurnOffAllWarnings</WarningLevel>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\crc32.c">
|
||||||
|
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||||
|
<WarningLevel>TurnOffAllWarnings</WarningLevel>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\deflate.c">
|
||||||
|
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||||
|
<WarningLevel>TurnOffAllWarnings</WarningLevel>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\inffast.c">
|
||||||
|
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||||
|
<WarningLevel>TurnOffAllWarnings</WarningLevel>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\inflate.c">
|
||||||
|
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||||
|
<WarningLevel>TurnOffAllWarnings</WarningLevel>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\inftrees.c">
|
||||||
|
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||||
|
<WarningLevel>TurnOffAllWarnings</WarningLevel>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\trees.c">
|
||||||
|
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||||
|
<WarningLevel>TurnOffAllWarnings</WarningLevel>
|
||||||
|
<DisableSpecificWarnings>6385</DisableSpecificWarnings>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\uncompr.c">
|
||||||
|
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||||
|
<WarningLevel>TurnOffAllWarnings</WarningLevel>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\zutil.c">
|
||||||
|
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||||
|
<WarningLevel>TurnOffAllWarnings</WarningLevel>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="compat.hpp" />
|
<ClInclude Include="compat.hpp" />
|
||||||
|
@ -13,6 +13,9 @@
|
|||||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||||
</Filter>
|
</Filter>
|
||||||
|
<Filter Include="Source Files\zlib">
|
||||||
|
<UniqueIdentifier>{c793ce2d-1a0b-43b6-b0b4-60025ceafa56}</UniqueIdentifier>
|
||||||
|
</Filter>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="pch.cpp">
|
<ClCompile Include="pch.cpp">
|
||||||
@ -48,6 +51,39 @@
|
|||||||
<ClCompile Include="string.cpp">
|
<ClCompile Include="string.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\adler32.c">
|
||||||
|
<Filter>Source Files\zlib</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\compress.c">
|
||||||
|
<Filter>Source Files\zlib</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\crc32.c">
|
||||||
|
<Filter>Source Files\zlib</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\deflate.c">
|
||||||
|
<Filter>Source Files\zlib</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\inffast.c">
|
||||||
|
<Filter>Source Files\zlib</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\inflate.c">
|
||||||
|
<Filter>Source Files\zlib</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\inftrees.c">
|
||||||
|
<Filter>Source Files\zlib</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\trees.c">
|
||||||
|
<Filter>Source Files\zlib</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\uncompr.c">
|
||||||
|
<Filter>Source Files\zlib</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="zlib\zutil.c">
|
||||||
|
<Filter>Source Files\zlib</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="pch.hpp">
|
<ClInclude Include="pch.hpp">
|
||||||
|
@ -31,6 +31,7 @@
|
|||||||
#include <stdex/vector_queue.hpp>
|
#include <stdex/vector_queue.hpp>
|
||||||
#include <stdex/watchdog.hpp>
|
#include <stdex/watchdog.hpp>
|
||||||
#include <stdex/wav.hpp>
|
#include <stdex/wav.hpp>
|
||||||
|
#include <stdex/zlib.hpp>
|
||||||
|
|
||||||
#include "compat.hpp"
|
#include "compat.hpp"
|
||||||
|
|
||||||
|
1
UnitTests/zlib
Submodule
1
UnitTests/zlib
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 643e17b7498d12ab8d15565662880579692f769d
|
40
UnitTests/zlib.cpp
Normal file
40
UnitTests/zlib.cpp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
SPDX-License-Identifier: MIT
|
||||||
|
Copyright © 2024 Amebis
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "pch.hpp"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
#ifdef _WIN32
|
||||||
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace UnitTests
|
||||||
|
{
|
||||||
|
TEST_CLASS(hash)
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TEST_METHOD(zlib)
|
||||||
|
{
|
||||||
|
static const char inflated[] = "This is a test.";
|
||||||
|
stdex::stream::memory_file dat_deflated;
|
||||||
|
{
|
||||||
|
stdex::zlib_writer zlib(dat_deflated, 9, 4);
|
||||||
|
zlib.write(inflated, sizeof(inflated) - sizeof(*inflated));
|
||||||
|
}
|
||||||
|
static const uint8_t deflated[] = { 0x78, 0xda, 0x0b, 0xc9, 0xc8, 0x2c, 0x56, 0x00, 0xa2, 0x44, 0x85, 0x92, 0xd4, 0xe2, 0x12, 0x3d, 0x00, 0x29, 0x97, 0x05, 0x24 };
|
||||||
|
Assert::AreEqual(sizeof(deflated), dat_deflated.size());
|
||||||
|
Assert::AreEqual(0, memcmp(deflated, dat_deflated.data(), sizeof(deflated)));
|
||||||
|
|
||||||
|
dat_deflated.seekbeg(0);
|
||||||
|
stdex::stream::memory_file dat_inflated;
|
||||||
|
{
|
||||||
|
stdex::zlib_reader zlib(dat_deflated, 3);
|
||||||
|
dat_inflated.write_stream(zlib);
|
||||||
|
}
|
||||||
|
Assert::AreEqual(sizeof(inflated) - sizeof(*inflated), dat_inflated.size());
|
||||||
|
Assert::AreEqual(0, memcmp(inflated, dat_inflated.data(), sizeof(inflated) - sizeof(*inflated)));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
169
include/stdex/zlib.hpp
Normal file
169
include/stdex/zlib.hpp
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
/*
|
||||||
|
SPDX-License-Identifier: MIT
|
||||||
|
Copyright © 2016-2024 Amebis
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "compat.hpp"
|
||||||
|
#include "stream.hpp"
|
||||||
|
#if _MSC_VER
|
||||||
|
#include <CodeAnalysis/Warnings.h>
|
||||||
|
#pragma warning(push)
|
||||||
|
#pragma warning(disable: ALL_CODE_ANALYSIS_WARNINGS)
|
||||||
|
#endif
|
||||||
|
#include <zlib.h>
|
||||||
|
#if _MSC_VER
|
||||||
|
#pragma warning(pop)
|
||||||
|
#endif
|
||||||
|
#include <memory>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace stdex
|
||||||
|
{
|
||||||
|
/// \cond internal
|
||||||
|
inline void throw_on_zlib_error(int result)
|
||||||
|
{
|
||||||
|
if (result >= 0)
|
||||||
|
return;
|
||||||
|
switch (result) {
|
||||||
|
case Z_ERRNO: throw std::system_error(errno, std::system_category(), "zlib failed with errno");
|
||||||
|
case Z_STREAM_ERROR: throw std::runtime_error("zlib stream error");
|
||||||
|
case Z_DATA_ERROR: throw std::runtime_error("zlib data error");
|
||||||
|
case Z_MEM_ERROR: throw std::bad_alloc();
|
||||||
|
case Z_BUF_ERROR: throw std::runtime_error("zlib buffer error");
|
||||||
|
case Z_VERSION_ERROR: throw std::runtime_error("zlib version error");
|
||||||
|
default: throw std::runtime_error("zlib unknown error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// \endcond
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Compresses data when writing to a stream
|
||||||
|
///
|
||||||
|
class zlib_writer : public stdex::stream::converter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
zlib_writer(_Inout_ stdex::stream::basic& source, _In_ int compression_level = Z_BEST_COMPRESSION, _In_ uInt block_size = 0x10000) :
|
||||||
|
stdex::stream::converter(source),
|
||||||
|
m_block_size(block_size),
|
||||||
|
m_block(new Byte[block_size])
|
||||||
|
{
|
||||||
|
memset(&m_zlib, 0, sizeof(m_zlib));
|
||||||
|
throw_on_zlib_error(deflateInit(&m_zlib, compression_level));
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~zlib_writer()
|
||||||
|
{
|
||||||
|
m_zlib.avail_in = 0;
|
||||||
|
m_zlib.next_in = NULL;
|
||||||
|
do {
|
||||||
|
m_zlib.avail_out = m_block_size;
|
||||||
|
m_zlib.next_out = m_block.get();
|
||||||
|
throw_on_zlib_error(deflate(&m_zlib, Z_FINISH));
|
||||||
|
m_source->write(m_block.get(), m_block_size - m_zlib.avail_out);
|
||||||
|
if (!m_source->ok()) _Unlikely_
|
||||||
|
throw std::system_error(sys_error(), std::system_category(), "failed to flush compressed stream"); // Data loss occured
|
||||||
|
} while (m_zlib.avail_out == 0);
|
||||||
|
// m_zlib.avail_out = m_block_size;
|
||||||
|
// m_zlib.next_out = m_block.get();
|
||||||
|
// deflateReset(&m_zlib);
|
||||||
|
deflateEnd(&m_zlib);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual _Success_(return != 0) size_t write(
|
||||||
|
_In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
|
||||||
|
{
|
||||||
|
_Assume_(data || !length);
|
||||||
|
size_t num_written = 0;
|
||||||
|
while (length) {
|
||||||
|
uInt num_inflated = static_cast<uInt>(std::min<size_t>(length, UINT_MAX));
|
||||||
|
m_zlib.avail_in = num_inflated;
|
||||||
|
m_zlib.next_in = const_cast<Bytef*>(reinterpret_cast<const Bytef*>(data));
|
||||||
|
do {
|
||||||
|
m_zlib.avail_out = m_block_size;
|
||||||
|
m_zlib.next_out = m_block.get();
|
||||||
|
throw_on_zlib_error(deflate(&m_zlib, Z_NO_FLUSH));
|
||||||
|
size_t num_deflated = m_block_size - m_zlib.avail_out;
|
||||||
|
if (num_deflated) {
|
||||||
|
m_source->write(m_block.get(), num_deflated);
|
||||||
|
if (!m_source->ok()) {
|
||||||
|
m_state = m_source->state();
|
||||||
|
return num_written;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (m_zlib.avail_out == 0);
|
||||||
|
num_written += num_inflated;
|
||||||
|
reinterpret_cast<const Bytef*&>(data) += num_inflated;
|
||||||
|
length -= num_inflated;
|
||||||
|
}
|
||||||
|
m_state = stdex::stream::state_t::ok;
|
||||||
|
return num_written;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
z_stream m_zlib;
|
||||||
|
uInt m_block_size;
|
||||||
|
std::unique_ptr<Byte[]> m_block;
|
||||||
|
};
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Decompresses data when reading from a stream
|
||||||
|
///
|
||||||
|
class zlib_reader : public stdex::stream::converter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
zlib_reader(_Inout_ stdex::stream::basic& source, _In_ uInt block_size = 0x10000) :
|
||||||
|
stdex::stream::converter(source),
|
||||||
|
m_block_size(block_size),
|
||||||
|
m_block(new Byte[block_size])
|
||||||
|
{
|
||||||
|
memset(&m_zlib, 0, sizeof(m_zlib));
|
||||||
|
throw_on_zlib_error(inflateInit(&m_zlib));
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~zlib_reader()
|
||||||
|
{
|
||||||
|
inflateEnd(&m_zlib);
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma warning(suppress: 6101) // See [1] below
|
||||||
|
virtual _Success_(return != 0 || length == 0) size_t read(
|
||||||
|
_Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
|
||||||
|
{
|
||||||
|
_Assume_(data || !length);
|
||||||
|
size_t num_read = 0;
|
||||||
|
while (length) {
|
||||||
|
uInt num_deflated = static_cast<uInt>(std::min<size_t>(length, UINT_MAX));
|
||||||
|
m_zlib.avail_out = num_deflated;
|
||||||
|
m_zlib.next_out = reinterpret_cast<Bytef*>(data);
|
||||||
|
do {
|
||||||
|
if (m_zlib.avail_in == 0) {
|
||||||
|
m_zlib.next_in = m_block.get();
|
||||||
|
m_zlib.avail_in = static_cast<uInt>(m_source->read(m_block.get(), m_block_size));
|
||||||
|
if (!m_zlib.avail_in) {
|
||||||
|
num_read += num_deflated - m_zlib.avail_out; // [1] Code analysis misses `num_deflated - m_zlib.avail_out` bytes were written to data in previous loop iterations.
|
||||||
|
if (num_read) {
|
||||||
|
m_state = stdex::stream::state_t::ok;
|
||||||
|
return num_read;
|
||||||
|
}
|
||||||
|
m_state = m_source->state();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw_on_zlib_error(inflate(&m_zlib, Z_NO_FLUSH));
|
||||||
|
} while (m_zlib.avail_out);
|
||||||
|
num_read += num_deflated;
|
||||||
|
reinterpret_cast<Bytef*&>(data) += num_deflated;
|
||||||
|
length -= num_deflated;
|
||||||
|
}
|
||||||
|
m_state = stdex::stream::state_t::ok;
|
||||||
|
return num_read;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
z_stream m_zlib;
|
||||||
|
uInt m_block_size;
|
||||||
|
std::unique_ptr<Byte[]> m_block;
|
||||||
|
};
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user