1#ifndef OSMIUM_IO_BZIP2_COMPRESSION_HPP
2#define OSMIUM_IO_BZIP2_COMPRESSION_HPP
46#include <osmium/io/detail/read_write.hpp>
59#include <system_error>
79 if (error_code == BZ_IO_ERROR) {
90 [[noreturn]]
inline void throw_bzip2_error(BZFILE* bzfile,
const char* msg,
const int bzlib_error) {
91 std::string error{
"bzip2 error: "};
94 int errnum = bzlib_error;
96 error += std::to_string(bzlib_error);
98 error += ::BZ2_bzerror(bzfile, &errnum);
105 FILE* m_file =
nullptr;
109 file_wrapper()
noexcept =
default;
111 file_wrapper(
const int fd,
const char* mode) {
113 osmium::detail::disable_invalid_parameter_handler diph;
115 m_file = fdopen(fd, mode);
122 throw std::system_error{errno, std::system_category(),
"fdopen failed"};
126 file_wrapper(
const file_wrapper&) =
delete;
127 file_wrapper& operator=(
const file_wrapper&) =
delete;
129 file_wrapper(file_wrapper&&) =
delete;
130 file_wrapper& operator=(file_wrapper&&) =
delete;
132 ~file_wrapper()
noexcept {
134 osmium::detail::disable_invalid_parameter_handler diph;
137 (void)fclose(m_file);
141 FILE* file()
const noexcept {
147 osmium::detail::disable_invalid_parameter_handler diph;
150 FILE* wrapped_file = m_file;
154 if (fileno(wrapped_file) == 1) {
158 if (fclose(wrapped_file) != 0) {
159 throw std::system_error{errno, std::system_category(),
"fclose failed"};
180 osmium::detail::disable_invalid_parameter_handler diph;
185 throw bzip2_error{
"bzip2 error: write open failed", bzerror};
203 void write(
const std::string& data)
override {
204 assert(data.size() < std::numeric_limits<int>::max());
207 osmium::detail::disable_invalid_parameter_handler diph;
210 ::BZ2_bzWrite(&bzerror,
m_bzfile,
const_cast<char*
>(data.data()),
static_cast<int>(data.size()));
211 if (bzerror != BZ_OK && bzerror != BZ_STREAM_END) {
212 detail::throw_bzip2_error(
m_bzfile,
"write failed", bzerror);
219 osmium::detail::disable_invalid_parameter_handler diph;
222 unsigned int nbytes_out_lo32 = 0;
223 unsigned int nbytes_out_hi32 = 0;
224 ::BZ2_bzWriteClose64(&bzerror,
m_bzfile, 0,
nullptr,
nullptr, &nbytes_out_lo32, &nbytes_out_hi32);
227 osmium::io::detail::reliable_fsync(fileno(
m_file.file()));
230 if (bzerror != BZ_OK) {
231 throw bzip2_error{
"bzip2 error: write close failed", bzerror};
233 m_file_size =
static_cast<std::size_t
>(
static_cast<uint64_t
>(nbytes_out_hi32) << 32U | nbytes_out_lo32);
254 osmium::detail::disable_invalid_parameter_handler diph;
257 m_bzfile = ::BZ2_bzReadOpen(&bzerror,
m_file.file(), 0, 0,
nullptr, 0);
259 throw bzip2_error{
"bzip2 error: read open failed", bzerror};
278 const auto offset = ftell(
m_file.file());
280 osmium::io::detail::remove_buffered_pages(fileno(
m_file.file()),
static_cast<std::size_t
>(offset));
283 osmium::detail::disable_invalid_parameter_handler diph;
291 assert(buffer.size() < std::numeric_limits<int>::max());
292 const int nread = ::BZ2_bzRead(&bzerror,
m_bzfile, &*buffer.begin(),
static_cast<int>(buffer.size()));
293 if (bzerror != BZ_OK && bzerror != BZ_STREAM_END) {
294 detail::throw_bzip2_error(
m_bzfile,
"read failed", bzerror);
296 if (bzerror == BZ_STREAM_END) {
297 void* unused =
nullptr;
299 if (!feof(
m_file.file())) {
300 ::BZ2_bzReadGetUnused(&bzerror,
m_bzfile, &unused, &nunused);
301 if (bzerror != BZ_OK) {
302 detail::throw_bzip2_error(
m_bzfile,
"get unused failed", bzerror);
304 std::string unused_data{
static_cast<const char*
>(unused),
static_cast<std::string::size_type
>(nunused)};
305 ::BZ2_bzReadClose(&bzerror,
m_bzfile);
306 if (bzerror != BZ_OK) {
307 throw bzip2_error{
"bzip2 error: read close failed", bzerror};
309 assert(unused_data.size() < std::numeric_limits<int>::max());
310 m_bzfile = ::BZ2_bzReadOpen(&bzerror,
m_file.file(), 0, 0, &*unused_data.begin(),
static_cast<int>(unused_data.size()));
312 throw bzip2_error{
"bzip2 error: read open failed", bzerror};
318 buffer.resize(
static_cast<std::string::size_type
>(nread));
329 osmium::io::detail::remove_buffered_pages(fileno(
m_file.file()));
332 osmium::detail::disable_invalid_parameter_handler diph;
335 ::BZ2_bzReadClose(&bzerror,
m_bzfile);
338 if (bzerror != BZ_OK) {
339 throw bzip2_error{
"bzip2 error: read close failed", bzerror};
358 m_bzstream.next_in =
const_cast<char*
>(buffer);
359 assert(size < std::numeric_limits<unsigned int>::max());
360 m_bzstream.avail_in =
static_cast<unsigned int>(size);
361 const int result = BZ2_bzDecompressInit(&
m_bzstream, 0, 0);
362 if (result != BZ_OK) {
363 throw bzip2_error{
"bzip2 error: decompression init failed: ", result};
385 const std::size_t buffer_size = 10240;
386 output.resize(buffer_size);
389 const int result = BZ2_bzDecompress(&
m_bzstream);
391 if (result != BZ_OK) {
396 if (result != BZ_OK && result != BZ_STREAM_END) {
397 throw bzip2_error{
"bzip2 error: decompress failed: ", result};
400 output.resize(
static_cast<std::size_t
>(
m_bzstream.next_out - output.data()));
423 inline bool get_registered_bzip2_compression() noexcept {
424 return registered_bzip2_compression;
Definition: bzip2_compression.hpp:346
Bzip2BufferDecompressor(const Bzip2BufferDecompressor &)=delete
Bzip2BufferDecompressor(const char *buffer, const std::size_t size)
Definition: bzip2_compression.hpp:354
Bzip2BufferDecompressor & operator=(Bzip2BufferDecompressor &&)=delete
Bzip2BufferDecompressor & operator=(const Bzip2BufferDecompressor &)=delete
Bzip2BufferDecompressor(Bzip2BufferDecompressor &&)=delete
std::string read() override
Definition: bzip2_compression.hpp:381
~Bzip2BufferDecompressor() noexcept override
Definition: bzip2_compression.hpp:373
void close() override
Definition: bzip2_compression.hpp:406
bz_stream m_bzstream
Definition: bzip2_compression.hpp:350
std::size_t m_buffer_size
Definition: bzip2_compression.hpp:349
const char * m_buffer
Definition: bzip2_compression.hpp:348
Definition: bzip2_compression.hpp:168
BZFILE * m_bzfile
Definition: bzip2_compression.hpp:172
Bzip2Compressor(const Bzip2Compressor &)=delete
Bzip2Compressor(Bzip2Compressor &&)=delete
Bzip2Compressor & operator=(const Bzip2Compressor &)=delete
~Bzip2Compressor() noexcept override
Definition: bzip2_compression.hpp:195
std::size_t file_size() const override
Definition: bzip2_compression.hpp:237
Bzip2Compressor(const int fd, const fsync sync)
Definition: bzip2_compression.hpp:176
Bzip2Compressor & operator=(Bzip2Compressor &&)=delete
std::size_t m_file_size
Definition: bzip2_compression.hpp:170
void close() override
Definition: bzip2_compression.hpp:216
void write(const std::string &data) override
Definition: bzip2_compression.hpp:203
detail::file_wrapper m_file
Definition: bzip2_compression.hpp:171
Definition: bzip2_compression.hpp:243
~Bzip2Decompressor() noexcept override
Definition: bzip2_compression.hpp:269
BZFILE * m_bzfile
Definition: bzip2_compression.hpp:246
Bzip2Decompressor & operator=(const Bzip2Decompressor &)=delete
detail::file_wrapper m_file
Definition: bzip2_compression.hpp:245
Bzip2Decompressor & operator=(Bzip2Decompressor &&)=delete
bool m_stream_end
Definition: bzip2_compression.hpp:247
Bzip2Decompressor(Bzip2Decompressor &&)=delete
void close() override
Definition: bzip2_compression.hpp:326
Bzip2Decompressor(const int fd)
Definition: bzip2_compression.hpp:251
Bzip2Decompressor(const Bzip2Decompressor &)=delete
std::string read() override
Definition: bzip2_compression.hpp:277
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
fsync
Definition: writer_options.hpp:51
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: bzip2_compression.hpp:71
bzip2_error(const std::string &what, const int error_code)
Definition: bzip2_compression.hpp:76
int bzip2_error_code
Definition: bzip2_compression.hpp:73
int system_errno
Definition: bzip2_compression.hpp:74