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"};
179 osmium::detail::disable_invalid_parameter_handler diph;
184 throw bzip2_error{
"bzip2 error: write open failed", bzerror};
202 void write(
const std::string& data)
override {
203 assert(data.size() < std::numeric_limits<int>::max());
206 osmium::detail::disable_invalid_parameter_handler diph;
209 ::BZ2_bzWrite(&bzerror,
m_bzfile,
const_cast<char*
>(data.data()),
static_cast<int>(data.size()));
210 if (bzerror != BZ_OK && bzerror != BZ_STREAM_END) {
211 detail::throw_bzip2_error(
m_bzfile,
"write failed", bzerror);
218 osmium::detail::disable_invalid_parameter_handler diph;
221 ::BZ2_bzWriteClose(&bzerror,
m_bzfile, 0,
nullptr,
nullptr);
224 osmium::io::detail::reliable_fsync(fileno(
m_file.file()));
227 if (bzerror != BZ_OK) {
228 throw bzip2_error{
"bzip2 error: write close failed", bzerror};
246 osmium::detail::disable_invalid_parameter_handler diph;
249 m_bzfile = ::BZ2_bzReadOpen(&bzerror,
m_file.file(), 0, 0,
nullptr, 0);
251 throw bzip2_error{
"bzip2 error: read open failed", bzerror};
271 osmium::detail::disable_invalid_parameter_handler diph;
279 assert(buffer.size() < std::numeric_limits<int>::max());
280 const int nread = ::BZ2_bzRead(&bzerror,
m_bzfile, &*buffer.begin(),
static_cast<int>(buffer.size()));
281 if (bzerror != BZ_OK && bzerror != BZ_STREAM_END) {
282 detail::throw_bzip2_error(
m_bzfile,
"read failed", bzerror);
284 if (bzerror == BZ_STREAM_END) {
287 if (!feof(
m_file.file())) {
288 ::BZ2_bzReadGetUnused(&bzerror,
m_bzfile, &unused, &nunused);
289 if (bzerror != BZ_OK) {
290 detail::throw_bzip2_error(
m_bzfile,
"get unused failed", bzerror);
292 std::string unused_data{
static_cast<const char*
>(unused),
static_cast<std::string::size_type
>(nunused)};
293 ::BZ2_bzReadClose(&bzerror,
m_bzfile);
294 if (bzerror != BZ_OK) {
295 throw bzip2_error{
"bzip2 error: read close failed", bzerror};
297 assert(unused_data.size() < std::numeric_limits<int>::max());
298 m_bzfile = ::BZ2_bzReadOpen(&bzerror,
m_file.file(), 0, 0, &*unused_data.begin(),
static_cast<int>(unused_data.size()));
300 throw bzip2_error{
"bzip2 error: read open failed", bzerror};
306 buffer.resize(
static_cast<std::string::size_type
>(nread));
317 osmium::detail::disable_invalid_parameter_handler diph;
320 ::BZ2_bzReadClose(&bzerror,
m_bzfile);
323 if (bzerror != BZ_OK) {
324 throw bzip2_error{
"bzip2 error: read close failed", bzerror};
343 m_bzstream.next_in =
const_cast<char*
>(buffer);
344 assert(size < std::numeric_limits<unsigned int>::max());
345 m_bzstream.avail_in =
static_cast<unsigned int>(size);
346 const int result = BZ2_bzDecompressInit(&
m_bzstream, 0, 0);
347 if (result != BZ_OK) {
348 throw bzip2_error{
"bzip2 error: decompression init failed: ", result};
370 const std::size_t buffer_size = 10240;
371 output.resize(buffer_size);
374 const int result = BZ2_bzDecompress(&
m_bzstream);
376 if (result != BZ_OK) {
381 if (result != BZ_OK && result != BZ_STREAM_END) {
382 throw bzip2_error{
"bzip2 error: decompress failed: ", result};
385 output.resize(
static_cast<std::size_t
>(
m_bzstream.next_out - output.data()));
408 inline bool get_registered_bzip2_compression() noexcept {
409 return registered_bzip2_compression;
418 #endif // OSMIUM_IO_BZIP2_COMPRESSION_HPP