| 117 | } |
| 118 | |
| 119 | static std::vector<unsigned char> serialize(const int64_t& value) |
| 120 | { |
| 121 | if(value == 0) |
| 122 | return std::vector<unsigned char>(); |
| 123 | |
| 124 | std::vector<unsigned char> result; |
| 125 | const bool neg = value < 0; |
| 126 | uint64_t absvalue = neg ? -value : value; |
| 127 | |
| 128 | while(absvalue) |
| 129 | { |
| 130 | result.push_back(absvalue & 0xff); |
| 131 | absvalue >>= 8; |
| 132 | } |
| 133 | |
| 134 | // - If the most significant byte is >= 0x80 and the value is positive, push a |
| 135 | // new zero-byte to make the significant byte < 0x80 again. |
| 136 | |
| 137 | // - If the most significant byte is >= 0x80 and the value is negative, push a |
| 138 | // new 0x80 byte that will be popped off when converting to an integral. |
| 139 | |
| 140 | // - If the most significant byte is < 0x80 and the value is negative, add |
| 141 | // 0x80 to it, since it will be subtracted and interpreted as a negative when |
| 142 | // converting to an integral. |
| 143 | |
| 144 | if (result.back() & 0x80) |
| 145 | result.push_back(neg ? 0x80 : 0); |
| 146 | else if (neg) |
| 147 | result.back() |= 0x80; |
| 148 | |
| 149 | return result; |
| 150 | } |
| 151 | |
| 152 | private: |
| 153 | static int64_t set_vch(const std::vector<unsigned char>& vch) |