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;
141 FILE* file()
const noexcept {
147 osmium::detail::disable_invalid_parameter_handler diph;
154 if (fileno(file) == 1) {
158 if (fclose(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
>(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};
279 osmium::detail::disable_invalid_parameter_handler diph;
287 assert(buffer.size() < std::numeric_limits<int>::max());
288 const int nread = ::BZ2_bzRead(&bzerror,
m_bzfile, &*buffer.begin(),
static_cast<int>(buffer.size()));
289 if (bzerror != BZ_OK && bzerror != BZ_STREAM_END) {
290 detail::throw_bzip2_error(
m_bzfile,
"read failed", bzerror);
292 if (bzerror == BZ_STREAM_END) {
293 void* unused =
nullptr;
295 if (!feof(
m_file.file())) {
296 ::BZ2_bzReadGetUnused(&bzerror,
m_bzfile, &unused, &nunused);
297 if (bzerror != BZ_OK) {
298 detail::throw_bzip2_error(
m_bzfile,
"get unused failed", bzerror);
300 std::string unused_data{
static_cast<const char*
>(unused),
static_cast<std::string::size_type
>(nunused)};
301 ::BZ2_bzReadClose(&bzerror,
m_bzfile);
302 if (bzerror != BZ_OK) {
303 throw bzip2_error{
"bzip2 error: read close failed", bzerror};
305 assert(unused_data.size() < std::numeric_limits<int>::max());
306 m_bzfile = ::BZ2_bzReadOpen(&bzerror,
m_file.file(), 0, 0, &*unused_data.begin(),
static_cast<int>(unused_data.size()));
308 throw bzip2_error{
"bzip2 error: read open failed", bzerror};
314 buffer.resize(
static_cast<std::string::size_type
>(nread));
325 osmium::detail::disable_invalid_parameter_handler diph;
328 ::BZ2_bzReadClose(&bzerror,
m_bzfile);
331 if (bzerror != BZ_OK) {
332 throw bzip2_error{
"bzip2 error: read close failed", bzerror};
351 m_bzstream.next_in =
const_cast<char*
>(buffer);
352 assert(size < std::numeric_limits<unsigned int>::max());
353 m_bzstream.avail_in =
static_cast<unsigned int>(size);
354 const int result = BZ2_bzDecompressInit(&
m_bzstream, 0, 0);
355 if (result != BZ_OK) {
356 throw bzip2_error{
"bzip2 error: decompression init failed: ", result};
378 const std::size_t buffer_size = 10240;
379 output.resize(buffer_size);
382 const int result = BZ2_bzDecompress(&
m_bzstream);
384 if (result != BZ_OK) {
389 if (result != BZ_OK && result != BZ_STREAM_END) {
390 throw bzip2_error{
"bzip2 error: decompress failed: ", result};
393 output.resize(
static_cast<std::size_t
>(
m_bzstream.next_out - output.data()));
416 inline bool get_registered_bzip2_compression() noexcept {
417 return registered_bzip2_compression;
426 #endif // OSMIUM_IO_BZIP2_COMPRESSION_HPP