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