/usr/include/osmium/io/compression.hpp is in libosmium2-dev 2.13.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | #ifndef OSMIUM_IO_COMPRESSION_HPP
#define OSMIUM_IO_COMPRESSION_HPP
/*
This file is part of Osmium (http://osmcode.org/libosmium).
Copyright 2013-2017 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.
*/
#include <atomic>
#include <cerrno>
#include <cstddef>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <system_error>
#include <tuple>
#include <utility>
#ifndef _MSC_VER
# include <unistd.h>
#else
# include <io.h>
#endif
#include <osmium/io/detail/read_write.hpp>
#include <osmium/io/error.hpp>
#include <osmium/io/file_compression.hpp>
#include <osmium/io/writer_options.hpp>
#include <osmium/util/file.hpp>
namespace osmium {
namespace io {
class Compressor {
fsync m_fsync;
protected:
bool do_fsync() const {
return m_fsync == fsync::yes;
}
public:
explicit Compressor(fsync sync) :
m_fsync(sync) {
}
virtual ~Compressor() noexcept = default;
virtual void write(const std::string& data) = 0;
virtual void close() = 0;
}; // class Compressor
class Decompressor {
std::atomic<std::size_t> m_file_size{0};
std::atomic<std::size_t> m_offset{0};
public:
static constexpr unsigned int input_buffer_size = 1024 * 1024;
Decompressor() = default;
Decompressor(const Decompressor&) = delete;
Decompressor& operator=(const Decompressor&) = delete;
Decompressor(Decompressor&&) = delete;
Decompressor& operator=(Decompressor&&) = delete;
virtual ~Decompressor() noexcept = default;
virtual std::string read() = 0;
virtual void close() = 0;
std::size_t file_size() const noexcept {
return m_file_size;
}
void set_file_size(std::size_t size) noexcept {
m_file_size = size;
}
std::size_t offset() const noexcept {
return m_offset;
}
void set_offset(std::size_t offset) noexcept {
m_offset = offset;
}
}; // class Decompressor
/**
* This singleton factory class is used to register compression
* algorithms used for reading and writing OSM files.
*
* For each algorithm we store two functions that construct
* a compressor and decompressor object, respectively.
*/
class CompressionFactory {
public:
using create_compressor_type = std::function<osmium::io::Compressor*(int, fsync)>;
using create_decompressor_type_fd = std::function<osmium::io::Decompressor*(int)>;
using create_decompressor_type_buffer = std::function<osmium::io::Decompressor*(const char*, std::size_t)>;
private:
using callbacks_type = std::tuple<create_compressor_type,
create_decompressor_type_fd,
create_decompressor_type_buffer>;
using compression_map_type = std::map<const osmium::io::file_compression, callbacks_type>;
compression_map_type m_callbacks;
CompressionFactory() = default;
CompressionFactory(const CompressionFactory&) = delete;
CompressionFactory& operator=(const CompressionFactory&) = delete;
CompressionFactory(CompressionFactory&&) = delete;
CompressionFactory& operator=(CompressionFactory&&) = delete;
const callbacks_type& find_callbacks(osmium::io::file_compression compression) const {
const auto it = m_callbacks.find(compression);
if (it != m_callbacks.end()) {
return it->second;
}
std::string error_message{"Support for compression '"};
error_message += as_string(compression);
error_message += "' not compiled into this binary";
throw unsupported_file_format_error{error_message};
}
public:
static CompressionFactory& instance() {
static CompressionFactory factory;
return factory;
}
bool register_compression(
osmium::io::file_compression compression,
create_compressor_type create_compressor,
create_decompressor_type_fd create_decompressor_fd,
create_decompressor_type_buffer create_decompressor_buffer) {
compression_map_type::value_type cc{compression,
std::make_tuple(create_compressor,
create_decompressor_fd,
create_decompressor_buffer)};
return m_callbacks.insert(cc).second;
}
template <typename... TArgs>
std::unique_ptr<osmium::io::Compressor> create_compressor(osmium::io::file_compression compression, TArgs&&... args) const {
const auto callbacks = find_callbacks(compression);
return std::unique_ptr<osmium::io::Compressor>(std::get<0>(callbacks)(std::forward<TArgs>(args)...));
}
std::unique_ptr<osmium::io::Decompressor> create_decompressor(osmium::io::file_compression compression, int fd) const {
const auto callbacks = find_callbacks(compression);
auto p = std::unique_ptr<osmium::io::Decompressor>(std::get<1>(callbacks)(fd));
p->set_file_size(osmium::util::file_size(fd));
return p;
}
std::unique_ptr<osmium::io::Decompressor> create_decompressor(osmium::io::file_compression compression, const char* buffer, std::size_t size) const {
const auto callbacks = find_callbacks(compression);
return std::unique_ptr<osmium::io::Decompressor>(std::get<2>(callbacks)(buffer, size));
}
}; // class CompressionFactory
class NoCompressor : public Compressor {
int m_fd;
public:
NoCompressor(int fd, fsync sync) :
Compressor(sync),
m_fd(fd) {
}
~NoCompressor() noexcept final {
try {
close();
} catch (...) {
// Ignore any exceptions because destructor must not throw.
}
}
void write(const std::string& data) final {
osmium::io::detail::reliable_write(m_fd, data.data(), data.size());
}
void close() final {
if (m_fd >= 0) {
const int fd = m_fd;
m_fd = -1;
if (do_fsync()) {
osmium::io::detail::reliable_fsync(fd);
}
osmium::io::detail::reliable_close(fd);
}
}
}; // class NoCompressor
class NoDecompressor : public Decompressor {
int m_fd;
const char *m_buffer;
std::size_t m_buffer_size;
std::size_t m_offset = 0;
public:
explicit NoDecompressor(int fd) :
Decompressor(),
m_fd(fd),
m_buffer(nullptr),
m_buffer_size(0) {
}
NoDecompressor(const char* buffer, std::size_t size) :
Decompressor(),
m_fd(-1),
m_buffer(buffer),
m_buffer_size(size) {
}
~NoDecompressor() noexcept final {
try {
close();
} catch (...) {
// Ignore any exceptions because destructor must not throw.
}
}
std::string read() final {
std::string buffer;
if (m_buffer) {
if (m_buffer_size != 0) {
const std::size_t size = m_buffer_size;
m_buffer_size = 0;
buffer.append(m_buffer, size);
}
} else {
buffer.resize(osmium::io::Decompressor::input_buffer_size);
const auto nread = ::read(m_fd, const_cast<char*>(buffer.data()), osmium::io::Decompressor::input_buffer_size);
if (nread < 0) {
throw std::system_error{errno, std::system_category(), "Read failed"};
}
buffer.resize(std::string::size_type(nread));
}
m_offset += buffer.size();
set_offset(m_offset);
return buffer;
}
void close() final {
if (m_fd >= 0) {
const int fd = m_fd;
m_fd = -1;
osmium::io::detail::reliable_close(fd);
}
}
}; // class NoDecompressor
namespace detail {
// we want the register_compression() function to run, setting
// the variable is only a side-effect, it will never be used
const bool registered_no_compression = osmium::io::CompressionFactory::instance().register_compression(osmium::io::file_compression::none,
[](int fd, fsync sync) { return new osmium::io::NoCompressor{fd, sync}; },
[](int fd) { return new osmium::io::NoDecompressor{fd}; },
[](const char* buffer, std::size_t size) { return new osmium::io::NoDecompressor{buffer, size}; }
);
// dummy function to silence the unused variable warning from above
inline bool get_registered_no_compression() noexcept {
return registered_no_compression;
}
} // namespace detail
} // namespace io
} // namespace osmium
#endif // OSMIUM_IO_COMPRESSION_HPP
|