From 07ab90039380344a308d0a9446564e5474697a02 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Fri, 13 Jul 2018 17:02:49 -0700 Subject: [PATCH] Report decompression errors in tippecanoe-decode --- mvt.cpp | 52 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/mvt.cpp b/mvt.cpp index f5028e7..b069302 100644 --- a/mvt.cpp +++ b/mvt.cpp @@ -34,25 +34,49 @@ int decompress(std::string const &input, std::string &output) { inflate_s.avail_in = 0; inflate_s.next_in = Z_NULL; if (inflateInit2(&inflate_s, 32 + 15) != Z_OK) { - fprintf(stderr, "error: %s\n", inflate_s.msg); + fprintf(stderr, "Decompression error: %s\n", inflate_s.msg); } inflate_s.next_in = (Bytef *) input.data(); inflate_s.avail_in = input.size(); - size_t length = 0; - do { - output.resize(length + 2 * input.size()); - inflate_s.avail_out = 2 * input.size(); - inflate_s.next_out = (Bytef *) (output.data() + length); - int ret = inflate(&inflate_s, Z_FINISH); - if (ret != Z_STREAM_END && ret != Z_OK && ret != Z_BUF_ERROR) { - fprintf(stderr, "error: %s\n", inflate_s.msg); + inflate_s.next_out = (Bytef *) output.data(); + inflate_s.avail_out = output.size(); + + while (true) { + size_t existing_output = inflate_s.next_out - (Bytef *) output.data(); + + output.resize(existing_output + 2 * inflate_s.avail_in + 100); + inflate_s.next_out = (Bytef *) output.data() + existing_output; + inflate_s.avail_out = output.size() - existing_output; + + int ret = inflate(&inflate_s, 0); + if (ret < 0) { + fprintf(stderr, "Decompression error: "); + if (ret == Z_DATA_ERROR) { + fprintf(stderr, "data error"); + } + if (ret == Z_STREAM_ERROR) { + fprintf(stderr, "stream error"); + } + if (ret == Z_MEM_ERROR) { + fprintf(stderr, "out of memory"); + } + if (ret == Z_BUF_ERROR) { + fprintf(stderr, "no data in buffer"); + } + fprintf(stderr, "\n"); return 0; } - length += (2 * input.size() - inflate_s.avail_out); - } while (inflate_s.avail_out == 0); + if (ret == Z_STREAM_END) { + break; + } + + // ret must be Z_OK or Z_NEED_DICT; + // continue decompresing + } + + output.resize(inflate_s.next_out - (Bytef *) output.data()); inflateEnd(&inflate_s); - output.resize(length); return 1; } @@ -90,7 +114,9 @@ bool mvt_tile::decode(std::string &message, bool &was_compressed) { if (is_compressed(message)) { std::string uncompressed; - decompress(message, uncompressed); + if (decompress(message, uncompressed) == 0) { + exit(EXIT_FAILURE); + } src = uncompressed; was_compressed = true; } else {