diff --git a/cartographer/common/port.h b/cartographer/common/port.h index 338861f..252c566 100644 --- a/cartographer/common/port.h +++ b/cartographer/common/port.h @@ -19,11 +19,12 @@ #include #include +#include #include +#include +#include -#include -#include -#include +#include namespace cartographer { @@ -48,22 +49,54 @@ inline int64 RoundToInt64(const double x) { return std::lround(x); } inline void FastGzipString(const std::string& uncompressed, std::string* compressed) { - boost::iostreams::filtering_ostream out; - out.push( - boost::iostreams::gzip_compressor(boost::iostreams::zlib::best_speed)); - out.push(boost::iostreams::back_inserter(*compressed)); - boost::iostreams::write(out, - reinterpret_cast(uncompressed.data()), - uncompressed.size()); + z_stream zs; + memset(&zs, 0, sizeof(zs)); + + if (deflateInit(&zs, Z_BEST_SPEED) != Z_OK) + throw std::runtime_error("deflateInit failed while compressing."); + + zs.next_in = reinterpret_cast(const_cast(uncompressed.data())); + zs.avail_in = static_cast(uncompressed.size()); + + int ret; + char buffer[4096]; + + do { + zs.next_out = reinterpret_cast(buffer); + zs.avail_out = sizeof(buffer); + + ret = deflate(&zs, Z_FINISH); + + compressed->append(buffer, sizeof(buffer) - zs.avail_out); + } while (zs.avail_out == 0); + + deflateEnd(&zs); } inline void FastGunzipString(const std::string& compressed, std::string* decompressed) { - boost::iostreams::filtering_ostream out; - out.push(boost::iostreams::gzip_decompressor()); - out.push(boost::iostreams::back_inserter(*decompressed)); - boost::iostreams::write(out, reinterpret_cast(compressed.data()), - compressed.size()); + z_stream zs; + memset(&zs, 0, sizeof(zs)); + + if (inflateInit(&zs) != Z_OK) + throw std::runtime_error("inflateInit failed while decompressing."); + + zs.next_in = reinterpret_cast(const_cast(compressed.data())); + zs.avail_in = static_cast(compressed.size()); + + int ret; + char buffer[4096]; + + do { + zs.next_out = reinterpret_cast(buffer); + zs.avail_out = sizeof(buffer); + + ret = inflate(&zs, Z_NO_FLUSH); + + decompressed->append(buffer, sizeof(buffer) - zs.avail_out); + } while (zs.avail_out == 0); + + inflateEnd(&zs); } } // namespace common