1 #ifndef OSMIUM_IO_BZIP2_COMPRESSION_HPP
2 #define OSMIUM_IO_BZIP2_COMPRESSION_HPP
46 #include <osmium/io/detail/read_write.hpp>
60 #include <system_error>
80 if (error_code == BZ_IO_ERROR) {
91 OSMIUM_NORETURN inline void throw_bzip2_error(BZFILE* bzfile,
const char* msg,
const int bzlib_error) {
92 std::string error{
"bzip2 error: "};
95 int errnum = bzlib_error;
97 error += std::to_string(bzlib_error);
99 error += ::BZ2_bzerror(bzfile, &errnum);
106 FILE* m_file =
nullptr;
110 file_wrapper() noexcept =
default;
112 file_wrapper(
const int fd,
const char* mode) {
114 osmium::detail::disable_invalid_parameter_handler diph;
116 m_file = fdopen(fd, mode);
119 throw std::system_error{errno, std::system_category(),
"fdopen failed"};
123 file_wrapper(
const file_wrapper&) =
delete;
124 file_wrapper& operator=(
const file_wrapper&) =
delete;
126 file_wrapper(file_wrapper&&) =
delete;
127 file_wrapper& operator=(file_wrapper&&) =
delete;
129 ~file_wrapper() noexcept {
131 osmium::detail::disable_invalid_parameter_handler diph;
138 FILE* file()
const noexcept {
144 osmium::detail::disable_invalid_parameter_handler diph;
147 if (fclose(m_file) != 0) {
149 throw std::system_error{errno, std::system_category(),
"fclose failed"};
170 osmium::detail::disable_invalid_parameter_handler diph;
175 throw bzip2_error{
"bzip2 error: write open failed", bzerror};
193 void write(
const std::string& data)
final {
194 assert(data.size() < std::numeric_limits<int>::max());
197 osmium::detail::disable_invalid_parameter_handler diph;
200 ::BZ2_bzWrite(&bzerror,
m_bzfile,
const_cast<char*
>(data.data()),
static_cast<int>(data.size()));
201 if (bzerror != BZ_OK && bzerror != BZ_STREAM_END) {
202 detail::throw_bzip2_error(
m_bzfile,
"write failed", bzerror);
209 osmium::detail::disable_invalid_parameter_handler diph;
212 ::BZ2_bzWriteClose(&bzerror,
m_bzfile, 0,
nullptr,
nullptr);
215 osmium::io::detail::reliable_fsync(fileno(
m_file.file()));
218 if (bzerror != BZ_OK) {
219 throw bzip2_error{
"bzip2 error: write close failed", bzerror};
237 osmium::detail::disable_invalid_parameter_handler diph;
240 m_bzfile = ::BZ2_bzReadOpen(&bzerror,
m_file.file(), 0, 0,
nullptr, 0);
242 throw bzip2_error{
"bzip2 error: read open failed", bzerror};
262 osmium::detail::disable_invalid_parameter_handler diph;
270 assert(buffer.size() < std::numeric_limits<int>::max());
271 const int nread = ::BZ2_bzRead(&bzerror,
m_bzfile, &*buffer.begin(),
static_cast<int>(buffer.size()));
272 if (bzerror != BZ_OK && bzerror != BZ_STREAM_END) {
273 detail::throw_bzip2_error(
m_bzfile,
"read failed", bzerror);
275 if (bzerror == BZ_STREAM_END) {
278 if (!feof(
m_file.file())) {
279 ::BZ2_bzReadGetUnused(&bzerror,
m_bzfile, &unused, &nunused);
280 if (bzerror != BZ_OK) {
281 detail::throw_bzip2_error(
m_bzfile,
"get unused failed", bzerror);
283 std::string unused_data{
static_cast<const char*
>(unused),
static_cast<std::string::size_type
>(nunused)};
284 ::BZ2_bzReadClose(&bzerror,
m_bzfile);
285 if (bzerror != BZ_OK) {
286 throw bzip2_error{
"bzip2 error: read close failed", bzerror};
288 assert(unused_data.size() < std::numeric_limits<int>::max());
289 m_bzfile = ::BZ2_bzReadOpen(&bzerror,
m_file.file(), 0, 0, &*unused_data.begin(),
static_cast<int>(unused_data.size()));
291 throw bzip2_error{
"bzip2 error: read open failed", bzerror};
297 buffer.resize(
static_cast<std::string::size_type
>(nread));
308 osmium::detail::disable_invalid_parameter_handler diph;
311 ::BZ2_bzReadClose(&bzerror,
m_bzfile);
314 if (bzerror != BZ_OK) {
315 throw bzip2_error{
"bzip2 error: read close failed", bzerror};
334 m_bzstream.next_in =
const_cast<char*
>(buffer);
335 assert(size < std::numeric_limits<unsigned int>::max());
336 m_bzstream.avail_in =
static_cast<unsigned int>(size);
337 const int result = BZ2_bzDecompressInit(&
m_bzstream, 0, 0);
338 if (result != BZ_OK) {
339 throw bzip2_error{
"bzip2 error: decompression init failed: ", result};
361 const std::size_t buffer_size = 10240;
362 output.resize(buffer_size);
365 const int result = BZ2_bzDecompress(&
m_bzstream);
367 if (result != BZ_OK) {
372 if (result != BZ_OK && result != BZ_STREAM_END) {
373 throw bzip2_error{
"bzip2 error: decompress failed: ", result};
376 output.resize(
static_cast<std::size_t
>(
m_bzstream.next_out - output.data()));
399 inline bool get_registered_bzip2_compression() noexcept {
400 return registered_bzip2_compression;
409 #endif // OSMIUM_IO_BZIP2_COMPRESSION_HPP