Libosmium  2.15.1
Fast and flexible C++ library for working with OpenStreetMap data
gzip_compression.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_IO_GZIP_COMPRESSION_HPP
2 #define OSMIUM_IO_GZIP_COMPRESSION_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2019 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
46 #include <osmium/io/detail/read_write.hpp>
47 #include <osmium/io/error.hpp>
51 
52 #include <zlib.h>
53 
54 #include <cassert>
55 #include <cerrno>
56 #include <cstddef>
57 #include <limits>
58 #include <string>
59 
60 #ifndef _MSC_VER
61 # include <unistd.h>
62 #endif
63 
64 namespace osmium {
65 
70  struct gzip_error : public io_error {
71 
72  int gzip_error_code = 0;
73  int system_errno = 0;
74 
75  explicit gzip_error(const std::string& what) :
76  io_error(what) {
77  }
78 
79  gzip_error(const std::string& what, const int error_code) :
80  io_error(what),
81  gzip_error_code(error_code) {
82  if (error_code == Z_ERRNO) {
83  system_errno = errno;
84  }
85  }
86 
87  }; // struct gzip_error
88 
89  namespace io {
90 
91  namespace detail {
92 
93  OSMIUM_NORETURN inline void throw_gzip_error(gzFile gzfile, const char* msg) {
94  std::string error{"gzip error: "};
95  error += msg;
96  error += ": ";
97  int error_code = 0;
98  if (gzfile) {
99  error += ::gzerror(gzfile, &error_code);
100  }
101  throw osmium::gzip_error{error, error_code};
102  }
103 
104  } // namespace detail
105 
106  class GzipCompressor : public Compressor {
107 
108  int m_fd;
109  gzFile m_gzfile;
110 
111  public:
112 
113  explicit GzipCompressor(const int fd, const fsync sync) :
114  Compressor(sync),
115  m_fd(osmium::io::detail::reliable_dup(fd)) {
116 #ifdef _MSC_VER
117  osmium::detail::disable_invalid_parameter_handler diph;
118 #endif
119  m_gzfile = ::gzdopen(fd, "wb");
120  if (!m_gzfile) {
121  throw gzip_error{"gzip error: write initialization failed"};
122  }
123  }
124 
125  GzipCompressor(const GzipCompressor&) = delete;
126  GzipCompressor& operator=(const GzipCompressor&) = delete;
127 
128  GzipCompressor(GzipCompressor&&) = delete;
130 
131  ~GzipCompressor() noexcept final {
132  try {
133  close();
134  } catch (...) {
135  // Ignore any exceptions because destructor must not throw.
136  }
137  }
138 
139  void write(const std::string& data) final {
140 #ifdef _MSC_VER
141  osmium::detail::disable_invalid_parameter_handler diph;
142 #endif
143  assert(m_gzfile);
144  assert(data.size() < std::numeric_limits<unsigned int>::max());
145  if (!data.empty()) {
146  const int nwrite = ::gzwrite(m_gzfile, data.data(), static_cast<unsigned int>(data.size()));
147  if (nwrite == 0) {
148  detail::throw_gzip_error(m_gzfile, "write failed");
149  }
150  }
151  }
152 
153  void close() final {
154  if (m_gzfile) {
155 #ifdef _MSC_VER
156  osmium::detail::disable_invalid_parameter_handler diph;
157 #endif
158  const int result = ::gzclose_w(m_gzfile);
159  m_gzfile = nullptr;
160  if (result != Z_OK) {
161  throw gzip_error{"gzip error: write close failed", result};
162  }
163  if (do_fsync()) {
164  osmium::io::detail::reliable_fsync(m_fd);
165  }
166  osmium::io::detail::reliable_close(m_fd);
167  }
168  }
169 
170  }; // class GzipCompressor
171 
173 
174  gzFile m_gzfile = nullptr;
175 
176  public:
177 
178  explicit GzipDecompressor(const int fd) {
179 #ifdef _MSC_VER
180  osmium::detail::disable_invalid_parameter_handler diph;
181 #endif
182  m_gzfile = ::gzdopen(fd, "rb");
183  if (!m_gzfile) {
184  try {
185  osmium::io::detail::reliable_close(fd);
186  } catch (...) {
187  }
188  throw gzip_error{"gzip error: read initialization failed"};
189  }
190  }
191 
192  GzipDecompressor(const GzipDecompressor&) = delete;
193  GzipDecompressor& operator=(const GzipDecompressor&) = delete;
194 
197 
198  ~GzipDecompressor() noexcept final {
199  try {
200  close();
201  } catch (...) {
202  // Ignore any exceptions because destructor must not throw.
203  }
204  }
205 
206  std::string read() final {
207 #ifdef _MSC_VER
208  osmium::detail::disable_invalid_parameter_handler diph;
209 #endif
210  assert(m_gzfile);
211  std::string buffer(osmium::io::Decompressor::input_buffer_size, '\0');
212  assert(buffer.size() < std::numeric_limits<unsigned int>::max());
213  int nread = ::gzread(m_gzfile, &*buffer.begin(), static_cast<unsigned int>(buffer.size()));
214  if (nread < 0) {
215  detail::throw_gzip_error(m_gzfile, "read failed");
216  }
217  buffer.resize(static_cast<std::string::size_type>(nread));
218 #if ZLIB_VERNUM >= 0x1240
219  set_offset(static_cast<std::size_t>(::gzoffset(m_gzfile)));
220 #endif
221  return buffer;
222  }
223 
224  void close() final {
225  if (m_gzfile) {
226 #ifdef _MSC_VER
227  osmium::detail::disable_invalid_parameter_handler diph;
228 #endif
229  const int result = ::gzclose_r(m_gzfile);
230  m_gzfile = nullptr;
231  if (result != Z_OK) {
232  throw gzip_error{"gzip error: read close failed", result};
233  }
234  }
235  }
236 
237  }; // class GzipDecompressor
238 
240 
241  const char* m_buffer;
242  std::size_t m_buffer_size;
243  z_stream m_zstream;
244 
245  public:
246 
247  GzipBufferDecompressor(const char* buffer, const std::size_t size) :
248  m_buffer(buffer),
249  m_buffer_size(size),
250  m_zstream() {
251  m_zstream.next_in = reinterpret_cast<unsigned char*>(const_cast<char*>(buffer));
252  assert(size < std::numeric_limits<unsigned int>::max());
253  m_zstream.avail_in = static_cast<unsigned int>(size);
254  const int result = inflateInit2(&m_zstream, MAX_WBITS | 32); // NOLINT(hicpp-signed-bitwise)
255  if (result != Z_OK) {
256  std::string message{"gzip error: decompression init failed: "};
257  if (m_zstream.msg) {
258  message.append(m_zstream.msg);
259  }
260  throw osmium::gzip_error{message, result};
261  }
262  }
263 
266 
269 
270  ~GzipBufferDecompressor() noexcept final {
271  try {
272  close();
273  } catch (...) {
274  // Ignore any exceptions because destructor must not throw.
275  }
276  }
277 
278  std::string read() final {
279  std::string output;
280 
281  if (m_buffer) {
282  const std::size_t buffer_size = 10240;
283  output.append(buffer_size, '\0');
284  m_zstream.next_out = reinterpret_cast<unsigned char*>(&*output.begin());
285  m_zstream.avail_out = buffer_size;
286  const int result = inflate(&m_zstream, Z_SYNC_FLUSH);
287 
288  if (result != Z_OK) {
289  m_buffer = nullptr;
290  m_buffer_size = 0;
291  }
292 
293  if (result != Z_OK && result != Z_STREAM_END) {
294  std::string message{"gzip error: inflate failed: "};
295  if (m_zstream.msg) {
296  message.append(m_zstream.msg);
297  }
298  throw osmium::gzip_error{message, result};
299  }
300 
301  output.resize(static_cast<std::size_t>(m_zstream.next_out - reinterpret_cast<const unsigned char*>(output.data())));
302  }
303 
304  return output;
305  }
306 
307  void close() final {
308  inflateEnd(&m_zstream);
309  }
310 
311  }; // class GzipBufferDecompressor
312 
313  namespace detail {
314 
315  // we want the register_compression() function to run, setting
316  // the variable is only a side-effect, it will never be used
318  [](const int fd, const fsync sync) { return new osmium::io::GzipCompressor{fd, sync}; },
319  [](const int fd) { return new osmium::io::GzipDecompressor{fd}; },
320  [](const char* buffer, const std::size_t size) { return new osmium::io::GzipBufferDecompressor{buffer, size}; }
321  );
322 
323  // dummy function to silence the unused variable warning from above
324  inline bool get_registered_gzip_compression() noexcept {
325  return registered_gzip_compression;
326  }
327 
328  } // namespace detail
329 
330  } // namespace io
331 
332 } // namespace osmium
333 
334 #endif // OSMIUM_IO_GZIP_COMPRESSION_HPP
gzFile m_gzfile
Definition: gzip_compression.hpp:109
Definition: compression.hpp:95
GzipBufferDecompressor & operator=(const GzipBufferDecompressor &)=delete
Definition: gzip_compression.hpp:239
gzFile m_gzfile
Definition: gzip_compression.hpp:174
#define OSMIUM_NORETURN
Definition: compatibility.hpp:41
Definition: gzip_compression.hpp:106
std::size_t m_buffer_size
Definition: gzip_compression.hpp:242
z_stream m_zstream
Definition: gzip_compression.hpp:243
gzip_error(const std::string &what, const int error_code)
Definition: gzip_compression.hpp:79
static CompressionFactory & instance()
Definition: compression.hpp:180
GzipDecompressor & operator=(const GzipDecompressor &)=delete
Definition: gzip_compression.hpp:172
~GzipDecompressor() noexcept final
Definition: gzip_compression.hpp:198
Definition: compression.hpp:87
void set_offset(const std::size_t offset) noexcept
Definition: compression.hpp:124
int m_fd
Definition: gzip_compression.hpp:108
bool do_fsync() const noexcept
Definition: compression.hpp:63
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
GzipCompressor(const int fd, const fsync sync)
Definition: gzip_compression.hpp:113
const char * m_buffer
Definition: gzip_compression.hpp:241
Definition: attr.hpp:333
void close() final
Definition: gzip_compression.hpp:307
fsync
Definition: writer_options.hpp:51
GzipDecompressor(const int fd)
Definition: gzip_compression.hpp:178
void close() final
Definition: gzip_compression.hpp:153
Definition: error.hpp:44
int system_errno
Definition: gzip_compression.hpp:73
gzip_error(const std::string &what)
Definition: gzip_compression.hpp:75
GzipCompressor & operator=(const GzipCompressor &)=delete
Definition: gzip_compression.hpp:70
std::string read() final
Definition: gzip_compression.hpp:206
int gzip_error_code
Definition: gzip_compression.hpp:72
Definition: compression.hpp:57
void write(const std::string &data) final
Definition: gzip_compression.hpp:139
~GzipCompressor() noexcept final
Definition: gzip_compression.hpp:131
~GzipBufferDecompressor() noexcept final
Definition: gzip_compression.hpp:270
bool register_compression(osmium::io::file_compression compression, create_compressor_type create_compressor, create_decompressor_type_fd create_decompressor_fd, create_decompressor_type_buffer create_decompressor_buffer)
Definition: compression.hpp:185
GzipBufferDecompressor(const char *buffer, const std::size_t size)
Definition: gzip_compression.hpp:247
std::string read() final
Definition: gzip_compression.hpp:278
void close() final
Definition: gzip_compression.hpp:224