8bcd4ea Add explicit cast operator from osmium::Timestamp to uint32_t. 0b24814 Fixed the full_queue_sleep_duration const (again). 5e19dd2 Try different workaround for windows... df5d6c9 Fix build error on Windows. 1553adf Workaround for missing support for u8 string literals on Windows. aa5e44a Do not build benchmarks in Appveyor to speed up build. 2b22b31 Remove warning generated by assert by casting to largest type. 148c5e3 Fix inclusion of our own css file into doxygen documentation. 13bce6f Split out test thats fails on Windows and do not run it on appveyor. ca04757 Make CMake add_unit_test() function more flexible. 6c04a63 Tell CMake to output json file with compile commands. c0dd848 Fix indentation in some tests. dfa7e4b Formatting: Consistently use spaces around equal signs. 08fe6db Add change log, release version 2.1.0. 667192e Add XML tests. 28acfc7 Make sorting the PBF stringtables optional. 8184781 Fix PBFInputFormat. Use member variable instead of static variable. 2b48945 Fix comment. 6d37054 Rename m_done to m_quit_input_thread to clarify what it is for. bc23083 Fix race condition in PBF reader. 7fc380e Add various docs, noexcepts, asserts, and tests. aeaf4d7 Not a good idea to how overbroad patterns in .gitignore. c1ef2f9 Bugfix: Multipolygon collector was accessing non-existent NodeRef. 0ef9a13 Add noexcept and docs to some functions in NodeRefList class. da4d764 Workaround for Doxygen bug. e67d5d9 use absolute paths to osmium in YouCompleteMe configuration 38abeac remove template parameter from NodeRefList c47adf0 Add check that osm xml file has <osm> or <osmChange> as top-level element. 5e4af90 Updated version number to 2.0.0. 5b2bc3e Workaround in cmake test for sparsehash size. git-subtree-dir: third_party/libosmium git-subtree-split: 8bcd4ea771696812bbb08ebc58e3ee22d0538070
247 lines
8.3 KiB
C++
247 lines
8.3 KiB
C++
#ifndef OSMIUM_IO_GZIP_COMPRESSION_HPP
|
|
#define OSMIUM_IO_GZIP_COMPRESSION_HPP
|
|
|
|
/*
|
|
|
|
This file is part of Osmium (http://osmcode.org/libosmium).
|
|
|
|
Copyright 2013-2015 Jochen Topf <jochen@topf.org> and others (see README).
|
|
|
|
Boost Software License - Version 1.0 - August 17th, 2003
|
|
|
|
Permission is hereby granted, free of charge, to any person or organization
|
|
obtaining a copy of the software and accompanying documentation covered by
|
|
this license (the "Software") to use, reproduce, display, distribute,
|
|
execute, and transmit the Software, and to prepare derivative works of the
|
|
Software, and to permit third-parties to whom the Software is furnished to
|
|
do so, all subject to the following:
|
|
|
|
The copyright notices in the Software and this entire statement, including
|
|
the above license grant, this restriction and the following disclaimer,
|
|
must be included in all copies of the Software, in whole or in part, and
|
|
all derivative works of the Software, unless such copies or derivative
|
|
works are solely in the form of machine-executable object code generated by
|
|
a source language processor.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
|
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
|
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
|
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
/**
|
|
* @file
|
|
*
|
|
* Include this file if you want to read or write gzip-compressed OSM XML
|
|
* files.
|
|
*
|
|
* @attention If you include this file, you'll need to link with `libz`.
|
|
*/
|
|
|
|
#include <stdexcept>
|
|
#include <string>
|
|
|
|
#include <errno.h>
|
|
#include <zlib.h>
|
|
|
|
#include <osmium/io/compression.hpp>
|
|
#include <osmium/io/file_compression.hpp>
|
|
#include <osmium/util/cast.hpp>
|
|
#include <osmium/util/compatibility.hpp>
|
|
|
|
namespace osmium {
|
|
|
|
/**
|
|
* Exception thrown when there are problems compressing or
|
|
* decompressing gzip files.
|
|
*/
|
|
struct gzip_error : public std::runtime_error {
|
|
|
|
int gzip_error_code;
|
|
int system_errno;
|
|
|
|
gzip_error(const std::string& what, int error_code) :
|
|
std::runtime_error(what),
|
|
gzip_error_code(error_code),
|
|
system_errno(error_code == Z_ERRNO ? errno : 0) {
|
|
}
|
|
|
|
}; // struct gzip_error
|
|
|
|
namespace io {
|
|
|
|
namespace detail {
|
|
|
|
OSMIUM_NORETURN inline void throw_gzip_error(gzFile gzfile, const char* msg, int zlib_error = 0) {
|
|
std::string error("gzip error: ");
|
|
error += msg;
|
|
error += ": ";
|
|
int errnum = zlib_error;
|
|
if (zlib_error) {
|
|
error += std::to_string(zlib_error);
|
|
} else {
|
|
error += ::gzerror(gzfile, &errnum);
|
|
}
|
|
throw osmium::gzip_error(error, errnum);
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
class GzipCompressor : public Compressor {
|
|
|
|
gzFile m_gzfile;
|
|
|
|
public:
|
|
|
|
explicit GzipCompressor(int fd) :
|
|
Compressor(),
|
|
m_gzfile(::gzdopen(fd, "w")) {
|
|
if (!m_gzfile) {
|
|
detail::throw_gzip_error(m_gzfile, "write initialization failed");
|
|
}
|
|
}
|
|
|
|
~GzipCompressor() override final {
|
|
close();
|
|
}
|
|
|
|
void write(const std::string& data) override final {
|
|
if (!data.empty()) {
|
|
int nwrite = ::gzwrite(m_gzfile, data.data(), static_cast_with_assert<unsigned int>(data.size()));
|
|
if (nwrite == 0) {
|
|
detail::throw_gzip_error(m_gzfile, "write failed");
|
|
}
|
|
}
|
|
}
|
|
|
|
void close() override final {
|
|
if (m_gzfile) {
|
|
int result = ::gzclose(m_gzfile);
|
|
m_gzfile = nullptr;
|
|
if (result != Z_OK) {
|
|
detail::throw_gzip_error(m_gzfile, "write close failed", result);
|
|
}
|
|
}
|
|
}
|
|
|
|
}; // class GzipCompressor
|
|
|
|
class GzipDecompressor : public Decompressor {
|
|
|
|
gzFile m_gzfile;
|
|
|
|
public:
|
|
|
|
explicit GzipDecompressor(int fd) :
|
|
Decompressor(),
|
|
m_gzfile(::gzdopen(fd, "r")) {
|
|
if (!m_gzfile) {
|
|
detail::throw_gzip_error(m_gzfile, "read initialization failed");
|
|
}
|
|
}
|
|
|
|
~GzipDecompressor() override final {
|
|
close();
|
|
}
|
|
|
|
std::string read() override final {
|
|
std::string buffer(osmium::io::Decompressor::input_buffer_size, '\0');
|
|
int nread = ::gzread(m_gzfile, const_cast<char*>(buffer.data()), static_cast_with_assert<unsigned int>(buffer.size()));
|
|
if (nread < 0) {
|
|
detail::throw_gzip_error(m_gzfile, "read failed");
|
|
}
|
|
buffer.resize(static_cast<std::string::size_type>(nread));
|
|
return buffer;
|
|
}
|
|
|
|
void close() override final {
|
|
if (m_gzfile) {
|
|
int result = ::gzclose(m_gzfile);
|
|
m_gzfile = nullptr;
|
|
if (result != Z_OK) {
|
|
detail::throw_gzip_error(m_gzfile, "read close failed", result);
|
|
}
|
|
}
|
|
}
|
|
|
|
}; // class GzipDecompressor
|
|
|
|
class GzipBufferDecompressor : public Decompressor {
|
|
|
|
const char* m_buffer;
|
|
size_t m_buffer_size;
|
|
z_stream m_zstream;
|
|
|
|
public:
|
|
|
|
GzipBufferDecompressor(const char* buffer, size_t size) :
|
|
m_buffer(buffer),
|
|
m_buffer_size(size),
|
|
m_zstream() {
|
|
m_zstream.next_in = reinterpret_cast<unsigned char*>(const_cast<char*>(buffer));
|
|
m_zstream.avail_in = static_cast_with_assert<unsigned int>(size);
|
|
int result = inflateInit2(&m_zstream, MAX_WBITS | 32);
|
|
if (result != Z_OK) {
|
|
std::string message("gzip error: decompression init failed: ");
|
|
if (m_zstream.msg) {
|
|
message.append(m_zstream.msg);
|
|
}
|
|
throw osmium::gzip_error(message, result);
|
|
}
|
|
}
|
|
|
|
~GzipBufferDecompressor() override final {
|
|
inflateEnd(&m_zstream);
|
|
}
|
|
|
|
std::string read() override final {
|
|
std::string output;
|
|
|
|
if (m_buffer) {
|
|
const size_t buffer_size = 10240;
|
|
output.append(buffer_size, '\0');
|
|
m_zstream.next_out = reinterpret_cast<unsigned char*>(const_cast<char*>(output.data()));
|
|
m_zstream.avail_out = buffer_size;
|
|
int result = inflate(&m_zstream, Z_SYNC_FLUSH);
|
|
|
|
if (result != Z_OK) {
|
|
m_buffer = nullptr;
|
|
m_buffer_size = 0;
|
|
}
|
|
|
|
if (result != Z_OK && result != Z_STREAM_END) {
|
|
std::string message("gzip error: inflate failed: ");
|
|
if (m_zstream.msg) {
|
|
message.append(m_zstream.msg);
|
|
}
|
|
throw osmium::gzip_error(message, result);
|
|
}
|
|
|
|
output.resize(static_cast<unsigned long>(m_zstream.next_out - reinterpret_cast<const unsigned char*>(output.data())));
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
}; // class GzipBufferDecompressor
|
|
|
|
namespace {
|
|
|
|
const bool registered_gzip_compression = osmium::io::CompressionFactory::instance().register_compression(osmium::io::file_compression::gzip,
|
|
[](int fd) { return new osmium::io::GzipCompressor(fd); },
|
|
[](int fd) { return new osmium::io::GzipDecompressor(fd); },
|
|
[](const char* buffer, size_t size) { return new osmium::io::GzipBufferDecompressor(buffer, size); }
|
|
);
|
|
|
|
} // anonymous namespace
|
|
|
|
} // namespace io
|
|
|
|
} // namespace osmium
|
|
|
|
#endif // OSMIUM_IO_GZIP_COMPRESSION_HPP
|