forked from highperformancecoder/minsky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zStream.h
131 lines (118 loc) · 3.83 KB
/
zStream.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
@copyright Steve Keen 2020
@author Russell Standish
@author Wynand Dednam
This file is part of Minsky.
Minsky is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Minsky is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Minsky. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ZSTREAM_H
#define ZSTREAM_H
#include <zlib.h>
namespace minsky
{
// nice RAII wrappers around zlib's data structures
struct ZStream: public z_stream
{
ZStream(Bytef* input, std::size_t inputSize, Bytef* output, std::size_t outputSize)
{
next_in=input;
avail_in=inputSize;
next_out=output;
avail_out=outputSize;
zfree=Z_NULL;
zalloc=Z_NULL;
}
void throwError() const {
throw std::runtime_error(std::string("compression failure: ")+(msg? msg:""));
}
};
struct DeflateZStream: public ZStream
{
template <class I, class O>
DeflateZStream(const I& input, O& output):
ZStream((Bytef*)input.data(), input.size(),
(Bytef*)output.data(), output.size())
{
if (deflateInit(this,9)!=Z_OK) throwError();
}
~DeflateZStream() {deflateEnd(this);}
void deflate() {
if (::deflate(this,Z_FINISH)!=Z_STREAM_END) throwError();
}
};
struct InflateZStream: public ZStream
{
classdesc::pack_t output{256};
Bytef* inputData;
std::size_t inputSize;
template <class I>
InflateZStream(const I& input):
ZStream((Bytef*)input.data(), input.size(), 0,0),
inputData((Bytef*)input.data()),inputSize(input.size())
{
next_out=(Bytef*)output.data();
avail_out=output.size();
if (inflateInit(this)!=Z_OK) throwError();
}
~InflateZStream() {inflateEnd(this);}
void inflate() {
int err;
while ((err=::inflate(this,Z_SYNC_FLUSH))==Z_OK)
{
// try doubling size
output.resize(2*output.size());
next_out=(Bytef*)(output.data())+total_out;
avail_out=output.size()-total_out;
next_in=inputData+total_in;
avail_in=inputSize-total_in;
}
if (err!=Z_STREAM_END) throwError();
}
void throwError() {
throw std::runtime_error(std::string("compression failure: ")+(msg? msg:""));
}
};
struct InflateFileZStream: public ZStream
{
std::string output;
Bytef* inputData;
std::size_t inputSize;
template <class I>
InflateFileZStream(const I& input):
ZStream((Bytef*)input.data(), input.size(), 0,0),
inputData((Bytef*)input.data()),inputSize(input.size())
{
next_out=(Bytef*)output.data();
avail_out=output.size();
if (inflateInit2(this,-MAX_WBITS)!=Z_OK) throwError(); //none of MAX_WBITS + 16, MAX_WBITS or -MAX_WBITS works. see https://stackoverflow.com/questions/1838699/how-can-i-decompress-a-gzip-stream-with-zlib
}
~InflateFileZStream() {inflateEnd(this);}
void inflate() {
int err;
while ((err=::inflate(this,Z_SYNC_FLUSH))==Z_OK)
{
// try doubling size
output.resize(2*output.size());
next_out=(Bytef*)(output.data())+total_out;
avail_out=output.size()-total_out;
next_in=inputData+total_in;
avail_in=inputSize-total_in;
}
if (err!=Z_STREAM_END) throwError();
}
void throwError() {
throw std::runtime_error(std::string("compression failure: ")+(msg? msg:""));
}
};
}
#include "zStream.cd"
#endif