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