11#include <CodeAnalysis/Warnings.h>
13#pragma warning(disable: ALL_CODE_ANALYSIS_WARNINGS)
25 inline void throw_on_zlib_error(
int result)
30 case Z_ERRNO:
throw std::system_error(errno, std::system_category(),
"zlib failed with errno");
31 case Z_STREAM_ERROR:
throw std::runtime_error(
"zlib stream error");
32 case Z_DATA_ERROR:
throw std::runtime_error(
"zlib data error");
33 case Z_MEM_ERROR:
throw std::bad_alloc();
34 case Z_BUF_ERROR:
throw std::runtime_error(
"zlib buffer error");
35 case Z_VERSION_ERROR:
throw std::runtime_error(
"zlib version error");
36 default:
throw std::runtime_error(
"zlib unknown error");
49 m_block_size(block_size),
50 m_block(
new Byte[block_size])
52 memset(&m_zlib, 0,
sizeof(m_zlib));
53 throw_on_zlib_error(deflateInit(&m_zlib, compression_level));
59 m_zlib.next_in = NULL;
61 m_zlib.avail_out = m_block_size;
62 m_zlib.next_out = m_block.get();
63 throw_on_zlib_error(deflate(&m_zlib, Z_FINISH));
64 m_source->
write(m_block.get(), m_block_size - m_zlib.avail_out);
65 if (!m_source->
ok()) _Unlikely_
66 throw std::system_error(sys_error(), std::system_category(),
"failed to flush compressed stream");
67 }
while (m_zlib.avail_out == 0);
74 virtual _Success_(
return != 0) size_t
write(
75 _In_reads_bytes_opt_(length) const
void* data, _In_
size_t length)
77 _Assume_(data || !length);
78 size_t num_written = 0;
80 uInt num_inflated =
static_cast<uInt
>(std::min<size_t>(length, UINT_MAX));
81 m_zlib.avail_in = num_inflated;
82 m_zlib.next_in =
const_cast<Bytef*
>(
reinterpret_cast<const Bytef*
>(data));
84 m_zlib.avail_out = m_block_size;
85 m_zlib.next_out = m_block.get();
86 throw_on_zlib_error(deflate(&m_zlib, Z_NO_FLUSH));
87 size_t num_deflated = m_block_size - m_zlib.avail_out;
89 m_source->
write(m_block.get(), num_deflated);
90 if (!m_source->
ok()) {
91 m_state = m_source->
state();
95 }
while (m_zlib.avail_out == 0);
96 num_written += num_inflated;
97 reinterpret_cast<const Bytef*&
>(data) += num_inflated;
98 length -= num_inflated;
100 m_state = stdex::stream::state_t::ok;
107 std::unique_ptr<Byte[]> m_block;
118 m_block_size(block_size),
119 m_block(
new Byte[block_size])
121 memset(&m_zlib, 0,
sizeof(m_zlib));
122 throw_on_zlib_error(inflateInit(&m_zlib));
130#pragma warning(suppress: 6101)
131 virtual _Success_(
return != 0 || length == 0) size_t
read(
132 _Out_writes_bytes_to_opt_(length, return)
void* data, _In_
size_t length)
134 _Assume_(data || !length);
137 uInt num_deflated =
static_cast<uInt
>(std::min<size_t>(length, UINT_MAX));
138 m_zlib.avail_out = num_deflated;
139 m_zlib.next_out =
reinterpret_cast<Bytef*
>(data);
141 if (m_zlib.avail_in == 0) {
142 m_zlib.next_in = m_block.get();
143 m_zlib.avail_in =
static_cast<uInt
>(m_source->
read(m_block.get(), m_block_size));
144 if (!m_zlib.avail_in) {
145 num_read += num_deflated - m_zlib.avail_out;
147 m_state = stdex::stream::state_t::ok;
150 m_state = m_source->
state();
154 throw_on_zlib_error(inflate(&m_zlib, Z_NO_FLUSH));
155 }
while (m_zlib.avail_out);
156 num_read += num_deflated;
157 reinterpret_cast<Bytef*&
>(data) += num_deflated;
158 length -= num_deflated;
160 m_state = stdex::stream::state_t::ok;
167 std::unique_ptr<Byte[]> m_block;
UTF-8 byte-order-mark
Definition stream.hpp:79
bool ok() const
Returns true if the stream state is clean i.e. previous operation was succesful.
Definition stream.hpp:175
state_t state() const
Returns stream state after last operation.
Definition stream.hpp:170
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:96
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:114
Modifies data on the fly when reading from/writing to a source stream. Could also be used to modify r...
Definition stream.hpp:1012
Decompresses data when reading from a stream.
Definition zlib.hpp:114
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:131
Compresses data when writing to a stream.
Definition zlib.hpp:45
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:74