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
22#if defined(__GNUC__)
23#pragma GCC diagnostic push
24#pragma GCC diagnostic ignored "-Wunknown-pragmas"
25#endif
26
27namespace stdex
28{
30 inline void throw_on_zlib_error(int result)
31 {
32 if (result >= 0)
33 return;
34 switch (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");
42 }
43 }
45
50 {
51 public:
52 zlib_writer(_Inout_ stdex::stream::basic& source, _In_ int compression_level = Z_BEST_COMPRESSION, _In_ uInt block_size = 0x10000) :
54 m_block_size(block_size),
55 m_block(new Byte[block_size])
56 {
57 memset(&m_zlib, 0, sizeof(m_zlib));
58 throw_on_zlib_error(deflateInit(&m_zlib, compression_level));
59 }
60
61 virtual ~zlib_writer()
62 {
63 m_zlib.avail_in = 0;
64 m_zlib.next_in = NULL;
65 do {
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"); // Data loss occured
72 } while (m_zlib.avail_out == 0);
73 // m_zlib.avail_out = m_block_size;
74 // m_zlib.next_out = m_block.get();
75 // deflateReset(&m_zlib);
76 deflateEnd(&m_zlib);
77 }
78
79 virtual _Success_(return != 0) size_t write(
80 _In_reads_bytes_opt_(length) const void* data, _In_ size_t length)
81 {
82 _Assume_(data || !length);
83 size_t num_written = 0;
84 while (length) {
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));
88 do {
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;
93 if (num_deflated) {
94 m_source->write(m_block.get(), num_deflated);
95 if (!m_source->ok()) {
96 m_state = m_source->state();
97 return num_written;
98 }
99 }
100 } while (m_zlib.avail_out == 0);
101 num_written += num_inflated;
102 reinterpret_cast<const Bytef*&>(data) += num_inflated;
103 length -= num_inflated;
104 }
105 m_state = stdex::stream::state_t::ok;
106 return num_written;
107 }
108
109 protected:
110 z_stream m_zlib;
111 uInt m_block_size;
112 std::unique_ptr<Byte[]> m_block;
113 };
114
119 {
120 public:
121 zlib_reader(_Inout_ stdex::stream::basic& source, _In_ uInt block_size = 0x10000) :
123 m_block_size(block_size),
124 m_block(new Byte[block_size])
125 {
126 memset(&m_zlib, 0, sizeof(m_zlib));
127 throw_on_zlib_error(inflateInit(&m_zlib));
128 }
129
130 virtual ~zlib_reader()
131 {
132 inflateEnd(&m_zlib);
133 }
134
135#pragma warning(suppress: 6101) // See [1] below
136 virtual _Success_(return != 0 || length == 0) size_t read(
137 _Out_writes_bytes_to_opt_(length, return) void* data, _In_ size_t length)
138 {
139 _Assume_(data || !length);
140 size_t num_read = 0;
141 while (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);
145 do {
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; // [1] Code analysis misses `num_deflated - m_zlib.avail_out` bytes were written to data in previous loop iterations.
151 if (num_read) {
152 m_state = stdex::stream::state_t::ok;
153 return num_read;
154 }
155 m_state = m_source->state();
156 return 0;
157 }
158 }
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;
164 }
165 m_state = stdex::stream::state_t::ok;
166 return num_read;
167 }
168
169 protected:
170 z_stream m_zlib;
171 uInt m_block_size;
172 std::unique_ptr<Byte[]> m_block;
173 };
174}
175
176#if defined(__GNUC__)
177#pragma GCC diagnostic pop
178#endif
‍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