11#include <CodeAnalysis/Warnings.h>
13#pragma warning(disable: ALL_CODE_ANALYSIS_WARNINGS)
23#pragma GCC diagnostic push
24#pragma GCC diagnostic ignored "-Wunknown-pragmas"
30 inline void throw_on_zlib_error(
int result)
35 case Z_ERRNO:
throw std::system_error(errno, std::system_category(),
"zlib failed with errno");
36 case Z_STREAM_ERROR:
throw std::runtime_error(
"zlib stream error");
37 case Z_DATA_ERROR:
throw std::runtime_error(
"zlib data error");
38 case Z_MEM_ERROR:
throw std::bad_alloc();
39 case Z_BUF_ERROR:
throw std::runtime_error(
"zlib buffer error");
40 case Z_VERSION_ERROR:
throw std::runtime_error(
"zlib version error");
41 default:
throw std::runtime_error(
"zlib unknown error");
54 m_block_size(block_size),
55 m_block(
new Byte[block_size])
57 memset(&m_zlib, 0,
sizeof(m_zlib));
58 throw_on_zlib_error(deflateInit(&m_zlib, compression_level));
64 m_zlib.next_in = NULL;
66 m_zlib.avail_out = m_block_size;
67 m_zlib.next_out = m_block.get();
68 throw_on_zlib_error(deflate(&m_zlib, Z_FINISH));
69 m_source->
write(m_block.get(), m_block_size - m_zlib.avail_out);
70 if (!m_source->
ok()) _Unlikely_
71 throw std::system_error(sys_error(), std::system_category(),
"failed to flush compressed stream");
72 }
while (m_zlib.avail_out == 0);
79 virtual _Success_(
return != 0) size_t
write(
80 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
82 _Assume_(data || !length);
83 size_t num_written = 0;
85 uInt num_inflated =
static_cast<uInt
>(std::min<size_t>(length, UINT_MAX));
86 m_zlib.avail_in = num_inflated;
87 m_zlib.next_in =
const_cast<Bytef*
>(
reinterpret_cast<const Bytef*
>(data));
89 m_zlib.avail_out = m_block_size;
90 m_zlib.next_out = m_block.get();
91 throw_on_zlib_error(deflate(&m_zlib, Z_NO_FLUSH));
92 size_t num_deflated = m_block_size - m_zlib.avail_out;
94 m_source->
write(m_block.get(), num_deflated);
95 if (!m_source->
ok()) {
96 m_state = m_source->
state();
100 }
while (m_zlib.avail_out == 0);
101 num_written += num_inflated;
102 reinterpret_cast<const Bytef*&
>(data) += num_inflated;
103 length -= num_inflated;
105 m_state = stdex::stream::state_t::ok;
112 std::unique_ptr<Byte[]> m_block;
123 m_block_size(block_size),
124 m_block(
new Byte[block_size])
126 memset(&m_zlib, 0,
sizeof(m_zlib));
127 throw_on_zlib_error(inflateInit(&m_zlib));
135#pragma warning(suppress: 6101)
136 virtual _Success_(
return != 0 || length == 0) size_t
read(
137 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
139 _Assume_(data || !length);
142 uInt num_deflated =
static_cast<uInt
>(std::min<size_t>(length, UINT_MAX));
143 m_zlib.avail_out = num_deflated;
144 m_zlib.next_out =
reinterpret_cast<Bytef*
>(data);
146 if (m_zlib.avail_in == 0) {
147 m_zlib.next_in = m_block.get();
148 m_zlib.avail_in =
static_cast<uInt
>(m_source->
read(m_block.get(), m_block_size));
149 if (!m_zlib.avail_in) {
150 num_read += num_deflated - m_zlib.avail_out;
152 m_state = stdex::stream::state_t::ok;
155 m_state = m_source->
state();
159 throw_on_zlib_error(inflate(&m_zlib, Z_NO_FLUSH));
160 }
while (m_zlib.avail_out);
161 num_read += num_deflated;
162 reinterpret_cast<Bytef*&
>(data) += num_deflated;
163 length -= num_deflated;
165 m_state = stdex::stream::state_t::ok;
172 std::unique_ptr<Byte[]> m_block;
177#pragma GCC diagnostic pop
UTF-8 byte-order-mark
Definition stream.hpp:84
bool ok() const
Returns true if the stream state is clean i.e. previous operation was succesful.
Definition stream.hpp:180
state_t state() const
Returns stream state after last operation.
Definition stream.hpp:175
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition stream.hpp:101
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition stream.hpp:119
Modifies data on the fly when reading from/writing to a source stream. Could also be used to modify r...
Definition stream.hpp:1022
Decompresses data when reading from a stream.
Definition zlib.hpp:119
virtual size_t read(_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
Reads block of data from the stream.
Definition zlib.hpp:136
Compresses data when writing to a stream.
Definition zlib.hpp:50
virtual size_t write(_In_reads_bytes_opt_(length) const void *data, size_t length)
Writes block of data to the stream.
Definition zlib.hpp:79