1#ifndef OSMIUM_IO_GZIP_COMPRESSION_HPP
2#define OSMIUM_IO_GZIP_COMPRESSION_HPP
46#include <osmium/io/detail/read_write.hpp>
78 gzip_error(
const std::string& what,
const int error_code) :
81 if (error_code == Z_ERRNO) {
92 [[noreturn]]
inline void throw_gzip_error(gzFile gzfile,
const char* msg) {
93 std::string error{
"gzip error: "};
98 error += ::gzerror(gzfile, &error_code);
117 osmium::detail::disable_invalid_parameter_handler diph;
119 m_gzfile = ::gzdopen(osmium::io::detail::reliable_dup(fd),
"wb");
121 throw gzip_error{
"gzip error: write initialization failed"};
139 void write(
const std::string& data)
override {
141 osmium::detail::disable_invalid_parameter_handler diph;
144 assert(data.size() < std::numeric_limits<unsigned int>::max());
146 const int nwrite = ::gzwrite(
m_gzfile, data.data(),
static_cast<unsigned int>(data.size()));
148 detail::throw_gzip_error(
m_gzfile,
"write failed");
156 osmium::detail::disable_invalid_parameter_handler diph;
158 const int result = ::gzclose_w(
m_gzfile);
160 if (result != Z_OK) {
161 throw gzip_error{
"gzip error: write close failed", result};
172 osmium::io::detail::reliable_fsync(
m_fd);
174 osmium::io::detail::reliable_close(
m_fd);
193 osmium::detail::disable_invalid_parameter_handler diph;
198 osmium::io::detail::reliable_close(fd);
201 throw gzip_error{
"gzip error: read initialization failed"};
222 osmium::detail::disable_invalid_parameter_handler diph;
224# if ZLIB_VERNUM >= 0x1240
225 const auto offset = ::gzoffset(
m_gzfile);
227 osmium::io::detail::remove_buffered_pages(
m_fd,
static_cast<std::size_t
>(offset));
232 assert(buffer.size() < std::numeric_limits<unsigned int>::max());
233 const int nread = ::gzread(
m_gzfile, &*buffer.begin(),
static_cast<unsigned int>(buffer.size()));
235 detail::throw_gzip_error(
m_gzfile,
"read failed");
237 buffer.resize(
static_cast<std::string::size_type
>(nread));
238#if ZLIB_VERNUM >= 0x1240
247 osmium::io::detail::remove_buffered_pages(
m_fd);
250 osmium::detail::disable_invalid_parameter_handler diph;
252 const int result = ::gzclose_r(
m_gzfile);
254 if (result != Z_OK) {
255 throw gzip_error{
"gzip error: read close failed", result};
274 m_zstream.next_in =
reinterpret_cast<unsigned char*
>(
const_cast<char*
>(buffer));
275 assert(size < std::numeric_limits<unsigned int>::max());
276 m_zstream.avail_in =
static_cast<unsigned int>(size);
277 const int result = inflateInit2(&
m_zstream, MAX_WBITS | 32);
278 if (result != Z_OK) {
279 std::string message{
"gzip error: decompression init failed: "};
305 const std::size_t buffer_size = 10240;
306 output.append(buffer_size,
'\0');
307 m_zstream.next_out =
reinterpret_cast<unsigned char*
>(&*output.begin());
309 const int result = inflate(&
m_zstream, Z_SYNC_FLUSH);
311 if (result != Z_OK) {
316 if (result != Z_OK && result != Z_STREAM_END) {
317 std::string message{
"gzip error: inflate failed: "};
324 output.resize(
static_cast<std::size_t
>(
m_zstream.next_out -
reinterpret_cast<const unsigned char*
>(output.data())));
347 inline bool get_registered_gzip_compression() noexcept {
348 return registered_gzip_compression;
bool register_compression(osmium::io::file_compression compression, const create_compressor_type &create_compressor, const create_decompressor_type_fd &create_decompressor_fd, const create_decompressor_type_buffer &create_decompressor_buffer)
Definition: compression.hpp:196
static CompressionFactory & instance()
Definition: compression.hpp:191
Definition: compression.hpp:57
bool do_fsync() const noexcept
Definition: compression.hpp:63
Definition: compression.hpp:91
@ input_buffer_size
Definition: compression.hpp:100
void set_offset(const std::size_t offset) noexcept
Definition: compression.hpp:125
bool want_buffered_pages_removed() const noexcept
Definition: compression.hpp:131
Definition: gzip_compression.hpp:262
std::size_t m_buffer_size
Definition: gzip_compression.hpp:265
GzipBufferDecompressor(GzipBufferDecompressor &&)=delete
GzipBufferDecompressor(const GzipBufferDecompressor &)=delete
const char * m_buffer
Definition: gzip_compression.hpp:264
GzipBufferDecompressor & operator=(GzipBufferDecompressor &&)=delete
z_stream m_zstream
Definition: gzip_compression.hpp:266
std::string read() override
Definition: gzip_compression.hpp:301
~GzipBufferDecompressor() noexcept override
Definition: gzip_compression.hpp:293
GzipBufferDecompressor(const char *buffer, const std::size_t size)
Definition: gzip_compression.hpp:270
GzipBufferDecompressor & operator=(const GzipBufferDecompressor &)=delete
void close() override
Definition: gzip_compression.hpp:330
Definition: gzip_compression.hpp:105
gzFile m_gzfile
Definition: gzip_compression.hpp:109
GzipCompressor & operator=(const GzipCompressor &)=delete
GzipCompressor(GzipCompressor &&)=delete
std::size_t file_size() const override
Definition: gzip_compression.hpp:178
GzipCompressor & operator=(GzipCompressor &&)=delete
~GzipCompressor() noexcept override
Definition: gzip_compression.hpp:131
int m_fd
Definition: gzip_compression.hpp:108
GzipCompressor(const GzipCompressor &)=delete
std::size_t m_file_size
Definition: gzip_compression.hpp:107
void close() override
Definition: gzip_compression.hpp:153
void write(const std::string &data) override
Definition: gzip_compression.hpp:139
GzipCompressor(const int fd, const fsync sync)
Definition: gzip_compression.hpp:113
Definition: gzip_compression.hpp:184
GzipDecompressor & operator=(GzipDecompressor &&)=delete
GzipDecompressor(const GzipDecompressor &)=delete
GzipDecompressor(const int fd)
Definition: gzip_compression.hpp:191
int m_fd
Definition: gzip_compression.hpp:187
~GzipDecompressor() noexcept override
Definition: gzip_compression.hpp:211
gzFile m_gzfile
Definition: gzip_compression.hpp:186
std::string read() override
Definition: gzip_compression.hpp:219
GzipDecompressor(GzipDecompressor &&)=delete
void close() override
Definition: gzip_compression.hpp:244
GzipDecompressor & operator=(const GzipDecompressor &)=delete
fsync
Definition: writer_options.hpp:51
std::size_t file_size(int fd)
Definition: file.hpp:109
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: gzip_compression.hpp:69
gzip_error(const std::string &what, const int error_code)
Definition: gzip_compression.hpp:78
int gzip_error_code
Definition: gzip_compression.hpp:71
gzip_error(const std::string &what)
Definition: gzip_compression.hpp:74
int system_errno
Definition: gzip_compression.hpp:72