equal
deleted
inserted
replaced
52 BMPString, |
52 BMPString, |
53 }; |
53 }; |
54 |
54 |
55 class Integer { |
55 class Integer { |
56 private: |
56 private: |
|
57 // TODO: use std::string (of octets, not ASCII) instead of std::vector? |
|
58 // TODO: use this class as BigInteger across Relational pipes? |
57 std::vector<uint8_t> data; |
59 std::vector<uint8_t> data; |
58 public: |
60 public: |
59 |
61 |
60 /** |
62 /** |
61 * @param data integer octets as in BER encoding |
63 * @param data integer octets as in BER encoding |
83 |
85 |
84 const std::string toString() const { |
86 const std::string toString() const { |
85 try { |
87 try { |
86 return std::to_string(toInt64()); |
88 return std::to_string(toInt64()); |
87 } catch (...) { |
89 } catch (...) { |
|
90 // integer has more than 64 bits → only HEX form value will be available |
88 // TODO: support longer values than 64 bits |
91 // TODO: support longer values than 64 bits |
89 // integer has more than 64 bits → only HEX form value will be available |
92 // TODO: do not ignore zero-length error? |
90 return ""; |
93 return ""; |
91 } |
94 } |
92 } |
95 } |
93 |
96 |
94 const int64_t toInt64() const { |
97 const int64_t toInt64() const { |
95 int64_t value = 0; |
98 int64_t value = 0; |
96 |
99 |
97 if (data.size() > sizeof (value)) throw std::invalid_argument("integer is too long"); |
100 if (data.size() > sizeof (value)) throw std::invalid_argument("Integer is too long"); |
|
101 else if (data.size() == 0) throw std::invalid_argument("Integer has zero length"); |
98 |
102 |
99 for (size_t i = 0, limit = data.size(), negative = 0; i < limit; i++) { |
103 value = data[0]; |
100 uint8_t b = data[i]; |
104 bool negative = data[0] & 0x80; |
101 if (i == 0 && b & 0x80) negative = true; |
105 |
102 value = (value << 8) | b; |
106 for (size_t i = 1, limit = data.size(); i < limit; i++) value = (value << 8) | data[i]; |
103 if (i == (limit - 1) && negative) value -= std::pow(256, data.size()); |
107 |
104 } |
108 if (negative) value -= std::pow(256, data.size()); |
105 |
109 |
106 return value; |
110 return value; |
107 } |
111 } |
108 |
112 |
109 }; |
113 }; |