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