crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
Zlib.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2020 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it 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 in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * Zlib.hpp
24  *
25  * Namespace for functions for zlib compression.
26  *
27  * Created on: May 2, 2019
28  * Author: ans
29  */
30 
31 #ifndef DATA_ZLIB_HPP_
32 #define DATA_ZLIB_HPP_
33 
34 #include "../../Main/Exception.hpp"
35 
36 #include <zlib.h>
37 
38 #include <array> // std::array
39 #include <string> // std::string
40 
43 
44  /*
45  * DECLARATION
46  */
47 
50 
51  std::string compress(const std::string& content);
52  std::string decompress(const std::string& compressedContent);
53 
57 
59  inline constexpr auto bufferSize{65536};
60 
62 
63  /*
64  * CLASS FOR ZLIB EXCEPTIONS
65  */
66 
68 
78 
79  /*
80  * IMPLEMENTATION
81  */
82 
84 
100  inline std::string compress(const std::string& content) {
101  if(content.empty()) {
102  return "";
103  }
104 
105  z_stream control{z_stream()};
106  int returnValue{};
107  std::array<char, bufferSize> buffer{};
108  std::string result;
109 
110  result.reserve(content.size());
111 
112  if(deflateInit(&control, Z_BEST_COMPRESSION) != Z_OK) {
113  throw Exception(
114  "Data::Compression::Zlib::compress():"
115  " Could not initialize zlib compression"
116  );
117  }
118 
119  control.next_in = reinterpret_cast<const Bytef *>(
120  content.data()
121  );
122  control.avail_in = content.size();
123 
124  do {
125  control.next_out = reinterpret_cast<Bytef *>(buffer.data());
126  control.avail_out = buffer.size();
127 
128  returnValue = deflate(&control, Z_FINISH);
129 
130  if(result.size() < control.total_out) {
131  result.append(buffer.data(), control.total_out - result.size());
132  }
133  }
134  while(returnValue == Z_OK);
135 
136  deflateEnd(&control);
137 
138  if(returnValue != Z_STREAM_END) {
139  throw Exception(
140  "Data::Compression::Zlib::compress():"
141  " zlib error: "
142  + std::string(control.msg)
143  );
144  }
145 
146  return result;
147  }
148 
150 
165  inline std::string decompress(const std::string& compressedContent) {
166  if(compressedContent.empty()) {
167  return "";
168  }
169 
170  z_stream control{z_stream()};
171  int returnValue{};
172  std::array<char, bufferSize> buffer{};
173  std::string result;
174 
175  if(inflateInit(&control) != Z_OK) {
176  throw Exception(
177  "Data::Compression::Zlib::decompress():"
178  " Could not initialize zlib decompression"
179  );
180  }
181 
182  control.next_in = reinterpret_cast<const Bytef *>(
183  compressedContent.data()
184  );
185  control.avail_in = compressedContent.size();
186 
187  do {
188  control.next_out = reinterpret_cast<Bytef *>(buffer.data());
189  control.avail_out = buffer.size();
190 
191  returnValue = inflate(&control, 0);
192 
193  if(result.size() < control.total_out) {
194  result.append(buffer.data(), control.total_out - result.size());
195  }
196  }
197  while(returnValue == Z_OK);
198 
199  inflateEnd(&control);
200 
201  if(returnValue != Z_STREAM_END) {
202  throw Exception(
203  "Data::Compression::Zlib::decompress():"
204  " zlib error: "
205  + std::string(control.msg)
206  );
207  }
208 
209  return result;
210  }
211 
212 } /* namespace crawlservpp::Data::Compression::Zlib */
213 
214 #endif /* DATA_ZLIB_HPP_ */
#define MAIN_EXCEPTION_CLASS()
Macro used to easily define classes for general exceptions.
Definition: Exception.hpp:50
Namespace for compressing and decompressing zlib.
Definition: Zlib.hpp:42
constexpr auto bufferSize
The maximum buffer size for zlib compression and decompression.
Definition: Zlib.hpp:59
std::string decompress(const std::string &compressedContent)
Decompresses zlib-compressed content.
Definition: Zlib.hpp:165
Class for zlib exceptions.
Definition: Zlib.hpp:77
std::string compress(const std::string &content)
Compresses content using zlib.
Definition: Zlib.hpp:100