| 186 | }; |
| 187 | |
| 188 | class CScriptNum |
| 189 | { |
| 190 | /** |
| 191 | * Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers. |
| 192 | * The semantics are subtle, though: operands must be in the range [-2^31 +1...2^31 -1], |
| 193 | * but results may overflow (and are valid as long as they are not used in a subsequent |
| 194 | * numeric operation). CScriptNum enforces those semantics by storing results as |
| 195 | * an int64 and allowing out-of-range values to be returned as a vector of bytes but |
| 196 | * throwing an exception if arithmetic is done or the result is interpreted as an integer. |
| 197 | */ |
| 198 | public: |
| 199 | |
| 200 | explicit CScriptNum(const int64_t& n) |
| 201 | { |
| 202 | m_value = n; |
| 203 | } |
| 204 | |
| 205 | static const size_t nDefaultMaxNumSize = 4; |
| 206 | |
| 207 | explicit CScriptNum(const std::vector<unsigned char>& vch, bool fRequireMinimal, |
| 208 | const size_t nMaxNumSize = nDefaultMaxNumSize) |
| 209 | { |
| 210 | if (vch.size() > nMaxNumSize) { |
| 211 | throw scriptnum_error("script number overflow"); |
| 212 | } |
| 213 | if (fRequireMinimal && vch.size() > 0) { |
| 214 | // Check that the number is encoded with the minimum possible |
| 215 | // number of bytes. |
| 216 | // |
| 217 | // If the most-significant-byte - excluding the sign bit - is zero |
| 218 | // then we're not minimal. Note how this test also rejects the |
| 219 | // negative-zero encoding, 0x80. |
| 220 | if ((vch.back() & 0x7f) == 0) { |
| 221 | // One exception: if there's more than one byte and the most |
| 222 | // significant bit of the second-most-significant-byte is set |
| 223 | // it would conflict with the sign bit. An example of this case |
| 224 | // is +-255, which encode to 0xff00 and 0xff80 respectively. |
| 225 | // (big-endian). |
| 226 | if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) { |
| 227 | throw scriptnum_error("non-minimally encoded script number"); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | m_value = set_vch(vch); |
| 232 | } |
| 233 | |
| 234 | inline bool operator==(const int64_t& rhs) const { return m_value == rhs; } |
| 235 | inline bool operator!=(const int64_t& rhs) const { return m_value != rhs; } |
| 236 | inline bool operator<=(const int64_t& rhs) const { return m_value <= rhs; } |
| 237 | inline bool operator< (const int64_t& rhs) const { return m_value < rhs; } |
| 238 | inline bool operator>=(const int64_t& rhs) const { return m_value >= rhs; } |
| 239 | inline bool operator> (const int64_t& rhs) const { return m_value > rhs; } |
| 240 | |
| 241 | inline bool operator==(const CScriptNum& rhs) const { return operator==(rhs.m_value); } |
| 242 | inline bool operator!=(const CScriptNum& rhs) const { return operator!=(rhs.m_value); } |
| 243 | inline bool operator<=(const CScriptNum& rhs) const { return operator<=(rhs.m_value); } |
| 244 | inline bool operator< (const CScriptNum& rhs) const { return operator< (rhs.m_value); } |
| 245 | inline bool operator>=(const CScriptNum& rhs) const { return operator>=(rhs.m_value); } |
no outgoing calls