stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
zlib.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2016-2024 Amebis
4*/
5
6#pragma once
7
8#include "compat.hpp"
9#include "stream.hpp"
10#if _MSC_VER
11#include <CodeAnalysis/Warnings.h>
12#pragma warning(push)
13#pragma warning(disable: ALL_CODE_ANALYSIS_WARNINGS)
14#endif
15#include <zlib.h>
16#if _MSC_VER
17#pragma warning(pop)
18#endif
19#include <memory>
20#include <stdexcept>
21
22namespace stdex
23{
25 inline void throw_on_zlib_error(int result)
26 {
27 if (result >= 0)
28 return;
29 switch (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");
37 }
38 }
40
45 {
46 public:
47 zlib_writer(_Inout_ stdex::stream::basic& source, _In_ int compression_level = Z_BEST_COMPRESSION, _In_ uInt block_size = 0x10000) :
49 m_block_size(block_size),
50 m_block(new Byte[block_size])
51 {
52 memset(&m_zlib, 0, sizeof(m_zlib));
53 throw_on_zlib_error(deflateInit(&m_zlib, compression_level));
54 }
55
56 virtual ~zlib_writer()
57 {
58 m_zlib.avail_in = 0;
59 m_zlib.next_in = NULL;
60 do {
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"); // Data loss occured
67 } while (m_zlib.avail_out == 0);
68 // m_zlib.avail_out = m_block_size;
69 // m_zlib.next_out = m_block.get();
70 // deflateReset(&m_zlib);
71 deflateEnd(&m_zlib);
72 }
73
74 virtual _Success_(return != 0) size_t write(
75 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
76 {
77 _Assume_(data || !length);
78 size_t num_written = 0;
79 while (length) {
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));
83 do {
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;
88 if (num_deflated) {
89 m_source->write(m_block.get(), num_deflated);
90 if (!m_source->ok()) {
91 m_state = m_source->state();
92 return num_written;
93 }
94 }
95 } while (m_zlib.avail_out == 0);
96 num_written += num_inflated;
97 reinterpret_cast<const Bytef*&>(data) += num_inflated;
98 length -= num_inflated;
99 }
100 m_state = stdex::stream::state_t::ok;
101 return num_written;
102 }
103
104 protected:
105 z_stream m_zlib;
106 uInt m_block_size;
107 std::unique_ptr<Byte[]> m_block;
108 };
109
114 {
115 public:
116 zlib_reader(_Inout_ stdex::stream::basic& source, _In_ uInt block_size = 0x10000) :
118 m_block_size(block_size),
119 m_block(new Byte[block_size])
120 {
121 memset(&m_zlib, 0, sizeof(m_zlib));
122 throw_on_zlib_error(inflateInit(&m_zlib));
123 }
124
125 virtual ~zlib_reader()
126 {
127 inflateEnd(&m_zlib);
128 }
129
130#pragma warning(suppress: 6101) // See [1] below
131 virtual _Success_(return != 0 || length == 0) size_t read(
132 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
133 {
134 _Assume_(data || !length);
135 size_t num_read = 0;
136 while (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);
140 do {
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; // [1] Code analysis misses `num_deflated - m_zlib.avail_out` bytes were written to data in previous loop iterations.
146 if (num_read) {
147 m_state = stdex::stream::state_t::ok;
148 return num_read;
149 }
150 m_state = m_source->state();
151 return 0;
152 }
153 }
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;
159 }
160 m_state = stdex::stream::state_t::ok;
161 return num_read;
162 }
163
164 protected:
165 z_stream m_zlib;
166 uInt m_block_size;
167 std::unique_ptr<Byte[]> m_block;
168 };
169}
‍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