From a637ad54735493c76cb72c92baa0755f3d76a16f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Tue, 21 Jan 2014 16:30:28 +0100 Subject: [PATCH] move pbf parsing to use exceptions --- include/llmr/util/pbf.hpp | 238 ++++++++++++++------------------------ src/map/tile.cpp | 26 ++--- 2 files changed, 102 insertions(+), 162 deletions(-) diff --git a/include/llmr/util/pbf.hpp b/include/llmr/util/pbf.hpp index f8a57a68d..f49940839 100644 --- a/include/llmr/util/pbf.hpp +++ b/include/llmr/util/pbf.hpp @@ -12,190 +12,132 @@ #include #include #include -// #include -#define PBF_USE_SETJMP - -#ifdef PBF_USE_SETJMP -#include -#endif namespace llmr { struct pbf { - inline pbf(const unsigned char *data, uint32_t length); + struct exception : std::exception {}; + struct unterminated_varint_exception : exception {}; + struct varint_too_long_exception : exception {}; + struct unknown_field_type_exception : exception {}; + struct end_of_buffer_exception : exception {}; - inline bool next(); - template inline T varint(); - template inline T svarint(); - inline std::string string(); - inline float float32(); - inline double float64(); - // inline int64_t int64(); - inline bool boolean(); + inline pbf(const unsigned char *data, uint32_t length); - inline void skip(); - inline void skipValue(uint32_t val); - inline void skipBytes(uint32_t bytes); + inline bool next(); + template inline T varint(); + template inline T svarint(); + inline std::string string(); + inline float float32(); + inline double float64(); + inline bool boolean(); - const uint8_t *data; - const uint8_t *end; - uint32_t value; - uint32_t tag; + inline void skip(); + inline void skipValue(uint32_t val); + inline void skipBytes(uint32_t bytes); - enum error_code { - UNTERMINATED_VARINT = 1, - UNKNOWN_TYPE = 2, - END_OF_BUFFER = 3 - }; - - #ifdef PBF_USE_SETJMP - std::jmp_buf jump_buffer; - std::string msg; - #endif + const uint8_t *data; + const uint8_t *end; + uint32_t value; + uint32_t tag; }; pbf::pbf(const unsigned char *data, uint32_t length) - : data(data), - end(data + length) -{} + : data(data), + end(data + length) { +} -// pbf::pbf(const std::string& buffer) -// : data((const unsigned char *)buffer.data()), -// end((const unsigned char *)buffer.data() + buffer.size()) -// {} - -bool pbf::next() -{ - if (data < end) { - value = (uint32_t)varint(); - tag = value >> 3; - return true; - } - return false; +bool pbf::next() { + if (data < end) { + value = (uint32_t)varint(); + tag = value >> 3; + return true; + } + return false; } template -T pbf::varint() -{ - uint8_t byte = 0x80; - T result = 0; - int bitpos; - for (bitpos = 0; bitpos < 70 && (byte & 0x80); bitpos += 7) { - if (data >= end) { - #ifdef PBF_USE_SETJMP - msg = "unterminated varint, unexpected end of buffer"; - std::longjmp(jump_buffer, UNTERMINATED_VARINT); - #endif - exit(1); - } - result |= ((T)(byte = *data) & 0x7F) << bitpos; +T pbf::varint() { + uint8_t byte = 0x80; + T result = 0; + int bitpos; + for (bitpos = 0; bitpos < 70 && (byte & 0x80); bitpos += 7) { + if (data >= end) { + throw unterminated_varint_exception(); + } + result |= ((T)(byte = *data) & 0x7F) << bitpos; - data++; - } - if (bitpos == 70 && (byte & 0x80)) { - #ifdef PBF_USE_SETJMP - msg = "unterminated varint (too long)"; - std::longjmp(jump_buffer, UNTERMINATED_VARINT); - #endif - exit(1); - } + data++; + } + if (bitpos == 70 && (byte & 0x80)) { + throw varint_too_long_exception(); + } - return result; + return result; } template -T pbf::svarint() -{ - T n = varint(); - return (n >> 1) ^ -(T)(n & 1); +T pbf::svarint() { + T n = varint(); + return (n >> 1) ^ -(T)(n & 1); } -std::string pbf::string() -{ - uint32_t bytes = (uint32_t)varint(); - const char *string = (const char *)data; - skipBytes(bytes); - return std::string(string, bytes); +std::string pbf::string() { + uint32_t bytes = (uint32_t)varint(); + const char *string = (const char *)data; + skipBytes(bytes); + return std::string(string, bytes); } -float pbf::float32() -{ - skipBytes(4); - float result; - memcpy(&result, data - 4, 4); - return result; +float pbf::float32() { + skipBytes(4); + float result; + memcpy(&result, data - 4, 4); + return result; } -double pbf::float64() -{ - skipBytes(8); - double result; - memcpy(&result, data - 8, 8); - return result; +double pbf::float64() { + skipBytes(8); + double result; + memcpy(&result, data - 8, 8); + return result; } -// int64_t pbf::int64() -// { -// return (int64_t)varint(); -// } - -bool pbf::boolean() -{ - skipBytes(1); - return *(bool *)(data - 1); +bool pbf::boolean() { + skipBytes(1); + return *(bool *)(data - 1); } -// template T *pbf::message(const Args&... args) -// { -// uint32_t bytes = (uint32_t)varint(); -// T *result = new T(data, bytes, args...); -// skipBytes(bytes); -// return result; -// } - -void pbf::skip() -{ - skipValue(value); +void pbf::skip() { + skipValue(value); } -void pbf::skipValue(uint32_t val) -{ - switch (val & 0x7) { - case 0: // varint - varint(); - break; - case 1: // 64 bit - skipBytes(8); - break; - case 2: // string/message - skipBytes((uint32_t)varint()); - break; - case 5: // 32 bit - skipBytes(4); - break; - default: - char text[32]; - snprintf(text, 32, "cannot skip unknown type %d", val & 0x7); - #ifdef PBF_USE_SETJMP - msg = text; - std::longjmp(jump_buffer, UNKNOWN_TYPE); - #endif - exit(1); - } +void pbf::skipValue(uint32_t val) { + switch (val & 0x7) { + case 0: // varint + varint(); + break; + case 1: // 64 bit + skipBytes(8); + break; + case 2: // string/message + skipBytes((uint32_t)varint()); + break; + case 5: // 32 bit + skipBytes(4); + break; + default: + throw unknown_field_type_exception(); + } } -void pbf::skipBytes(uint32_t bytes) -{ - data += bytes; - if (data > end) { - #ifdef PBF_USE_SETJMP - msg = "unexpected end of buffer"; - std::longjmp(jump_buffer, END_OF_BUFFER); - #endif - exit(1); - } +void pbf::skipBytes(uint32_t bytes) { + data += bytes; + if (data > end) { + throw end_of_buffer_exception(); + } } } // end namespace llmr diff --git a/src/map/tile.cpp b/src/map/tile.cpp index 59cf2e4c6..fb21f9d40 100644 --- a/src/map/tile.cpp +++ b/src/map/tile.cpp @@ -85,24 +85,22 @@ bool Tile::parse() { // fprintf(stderr, "[%p] parsing tile [%d/%d/%d]...\n", this, z, x, y); pbf tile(data, bytes); - - int code = setjmp(tile.jump_buffer); - if (code > 0) { - fprintf(stderr, "[%p] parsing tile [%d/%d/%d]... failed: %s\n", this, id.z, id.x, id.y, tile.msg.c_str()); + try { + while (tile.next()) { + if (tile.tag == 3) { // layer + uint32_t bytes = (uint32_t)tile.varint(); + parseLayer(tile.data, bytes); + tile.skipBytes(bytes); + } else { + tile.skip(); + } + } + } catch(const pbf::exception& ex) { + fprintf(stderr, "[%p] parsing tile [%d/%d/%d]... failed: %s\n", this, id.z, id.x, id.y, ex.what()); cancel(); return false; } - while (tile.next()) { - if (tile.tag == 3) { // layer - uint32_t bytes = (uint32_t)tile.varint(); - parseLayer(tile.data, bytes); - tile.skipBytes(bytes); - } else { - tile.skip(); - } - } - if (state == obsolete) { return false; } else {