X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fbignum.h;h=abcc052efecda7d9831db5b496451d4535277ed5;hb=d11488abd05cb39a9f481e7c4c35f780197a3d28;hp=5b4c78e7faf6490f14550e89146c7427a043ebe0;hpb=223b6f1ba4819e9a146e7aa451d546726d0bc714;p=novacoin.git diff --git a/src/bignum.h b/src/bignum.h index 5b4c78e..abcc052 100644 --- a/src/bignum.h +++ b/src/bignum.h @@ -1,6 +1,7 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2009-2012 The Bitcoin developers // Distributed under the MIT/X11 software license, see the accompanying -// file license.txt or http://www.opensource.org/licenses/mit-license.php. +// file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_BIGNUM_H #define BITCOIN_BIGNUM_H @@ -10,6 +11,7 @@ #include "util.h" +/** Errors thrown by the bignum class */ class bignum_error : public std::runtime_error { public: @@ -17,7 +19,7 @@ public: }; - +/** RAII encapsulated BN_CTX (OpenSSL bignum context) */ class CAutoBN_CTX { protected: @@ -45,7 +47,7 @@ public: }; - +/** C++ wrapper for BIGNUM (OpenSSl bignum) */ class CBigNum : public BIGNUM { public: @@ -76,7 +78,8 @@ public: BN_clear_free(this); } - CBigNum(char n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } + //CBigNum(char n) is not portable. Use 'signed char' or 'unsigned char'. + CBigNum(signed char n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } CBigNum(short n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } CBigNum(int n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } CBigNum(long n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } @@ -114,9 +117,9 @@ public: { unsigned long n = BN_get_word(this); if (!BN_is_negative(this)) - return (n > INT_MAX ? INT_MAX : n); + return (n > std::numeric_limits::max() ? std::numeric_limits::max() : n); else - return (n > INT_MAX ? INT_MIN : -(int)n); + return (n > std::numeric_limits::max() ? std::numeric_limits::min() : -(int)n); } void setint64(int64 n) @@ -228,10 +231,13 @@ public: { std::vector vch2(vch.size() + 4); unsigned int nSize = vch.size(); + // BIGNUM's byte stream format expects 4 bytes of + // big endian size data info at the front vch2[0] = (nSize >> 24) & 0xff; vch2[1] = (nSize >> 16) & 0xff; vch2[2] = (nSize >> 8) & 0xff; vch2[3] = (nSize >> 0) & 0xff; + // swap data to big endian reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4); BN_mpi2bn(&vch2[0], vch2.size(), this); } @@ -239,7 +245,7 @@ public: std::vector getvch() const { unsigned int nSize = BN_bn2mpi(this, NULL); - if (nSize < 4) + if (nSize <= 4) return std::vector(); std::vector vch(nSize); BN_bn2mpi(this, &vch[0]); @@ -291,12 +297,12 @@ public: psz++; // hex string to bignum - static char phexdigit[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0 }; + static signed char phexdigit[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0 }; *this = 0; while (isxdigit(*psz)) { *this <<= 4; - int n = phexdigit[*psz++]; + int n = phexdigit[(unsigned char)*psz++]; *this += n; } if (fNegative) @@ -334,19 +340,19 @@ public: return ToString(16); } - unsigned int GetSerializeSize(int nType=0, int nVersion=VERSION) const + unsigned int GetSerializeSize(int nType=0, int nVersion=PROTOCOL_VERSION) const { return ::GetSerializeSize(getvch(), nType, nVersion); } template - void Serialize(Stream& s, int nType=0, int nVersion=VERSION) const + void Serialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION) const { ::Serialize(s, getvch(), nType, nVersion); } template - void Unserialize(Stream& s, int nType=0, int nVersion=VERSION) + void Unserialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION) { std::vector vch; ::Unserialize(s, vch, nType, nVersion);