# HG changeset patch # User František Kučera # Date 1576003070 -3600 # Node ID 24a506eb97b5c58ada4fcbb83e4a5aac809185c6 # Parent bcff4ebca71da0285d5b81020032746f63e1023a binary format change: encode numbers as ULEB128 instead of original uint8/16/32/64 format diff -r bcff4ebca71d -r 24a506eb97b5 src/types/IntegerDataTypeReader.h --- a/src/types/IntegerDataTypeReader.h Tue Dec 03 16:30:02 2019 +0100 +++ b/src/types/IntegerDataTypeReader.h Tue Dec 10 19:37:50 2019 +0100 @@ -38,61 +38,16 @@ using namespace relpipe::reader; /** - * The prototype does not have various integer and other numeric data types, - * it just works with one type of integer. - * But this integer has variable length -- smaller values occupy only one byte, bigger ones, more bytes 1,2,4,8 + first byte (contains length signalization). - * In the real implementation of relational pipes, there will be DataTypes for particular numeric types. - * - * TODO: support also big endian architectures. - * TODO: throw exception if a value was stored in bigger type than needed (while reading – there should be only one supported way how to encode a single value) - * - * Example of encoded values: - * ------------------------------------------------------------------------------------------------- - * $ for n in 0 1 10 250 251 252 65535 65536 4294967295 4294967296 18446744073709551615; do printf '%20s = ' $n; dist/Debug/GNU-Linux/rp-prototype write integer $n | hd | head -n 1; done - * 0 = 00000000 00 |.| - * 1 = 00000000 01 |.| - * 10 = 00000000 0a |.| - * 250 = 00000000 fa |.| - * 251 = 00000000 fb fb |..| - * 252 = 00000000 fb fc |..| - * 65535 = 00000000 fc ff ff |...| - * 65536 = 00000000 fd 00 00 01 00 |.....| - * 4294967295 = 00000000 fd ff ff ff ff |.....| - * 4294967296 = 00000000 fe 00 00 00 00 01 00 00 00 |.........| - * 18446744073709551615 = 00000000 fe ff ff ff ff ff ff ff ff |.........| - * ------------------------------------------------------------------------------------------------- - * - * Example of decoded values: - * ------------------------------------------------------------------------------------------------- - * $ for n in 0 1 10 250 251 252 65535 65536 4294967295 4294967296 18446744073709551615; do dist/Debug/GNU-Linux/rp-prototype write integer $n | dist/Debug/GNU-Linux/rp-prototype read integer; done; - * 0 - * 1 - * 10 - * 250 - * 251 - * 252 - * 65535 - * 65536 - * 4294967295 - * 4294967296 - * 18446744073709551615 - * ------------------------------------------------------------------------------------------------- - * - * Note: similar format as original idea: https://en.wikipedia.org/wiki/X.690#Length_octets - * + * Unsigned variable-length integer. + * ULEB128 */ class IntegerDataTypeReader : public DataTypeReader { private: - static const uint8_t INTEGER_TYPE_UINT8 = 251; - static const uint8_t INTEGER_TYPE_UINT16 = 252; - static const uint8_t INTEGER_TYPE_UINT32 = 253; - static const uint8_t INTEGER_TYPE_UINT64 = 254; - static const uint8_t INTEGER_TYPE_RESERVED = 255; - template integer_t read(std::istream &input) { - T value = 0; - input.read(reinterpret_cast (&value), sizeof (value)); - return value; + uint8_t readNextOctet(std::istream &input) { + uint8_t value = input.get(); + if (input.good()) return value; + else throw RelpipeReaderException(L"Unable to read next octet of the integer."); } public: @@ -101,19 +56,15 @@ } integer_t readValue(std::istream &input) override { - uint8_t first; - input.read(reinterpret_cast (&first), sizeof (first)); - if (input.good()) { - - if (first < INTEGER_TYPE_UINT8) return first; - else if (first == INTEGER_TYPE_UINT8) return read(input); - else if (first == INTEGER_TYPE_UINT16) return read(input); - else if (first == INTEGER_TYPE_UINT32) return read(input); - else if (first == INTEGER_TYPE_UINT64) return read(input); - else throw RelpipeReaderException(L"Error while parsing integer type: unsupported type"); - } else { - throw RelpipeReaderException(L"Error while reading integer from the stream."); - } + integer_t value = 0; + integer_t shift = 0; + octet_t octet; + do { + octet = readNextOctet(input); + value += integer_t(octet & 0x7F) << shift; + shift += 7; + } while (octet >= 128); + return value; } string_t toString(const integer_t &value) override { @@ -124,4 +75,4 @@ } } -} \ No newline at end of file +}