X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fbignum.h;h=f09c4aaf31dd074c8a409399eca7783b8a0c4746;hb=0561bbd1c69263dceb24ffacf850788e6e961a13;hp=c7c2ff173046d4b2ce06a4165cd8d0b80de9b866;hpb=6b8de05d0a6696dff4b7dccec5b74889f3cdc486;p=novacoin.git diff --git a/src/bignum.h b/src/bignum.h index c7c2ff1..f09c4aa 100644 --- a/src/bignum.h +++ b/src/bignum.h @@ -1,7 +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 @@ -9,7 +9,7 @@ #include #include -#include "util.h" +#include "util.h" // for uint64 /** Errors thrown by the bignum class */ class bignum_error : public std::runtime_error @@ -47,7 +47,7 @@ public: }; -/** C++ wrapper for BIGNUM (OpenSSl bignum) */ +/** C++ wrapper for BIGNUM (OpenSSL bignum) */ class CBigNum : public BIGNUM { public: @@ -78,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); } @@ -116,9 +117,9 @@ public: { unsigned long n = BN_get_word(this); if (!BN_is_negative(this)) - return (n > std::numeric_limits::max() ? std::numeric_limits::max() : n); + return (n > (unsigned long)std::numeric_limits::max() ? std::numeric_limits::max() : n); else - return (n > std::numeric_limits::max() ? std::numeric_limits::min() : -(int)n); + return (n > (unsigned long)std::numeric_limits::max() ? std::numeric_limits::min() : -(int)n); } void setint64(int64 n) @@ -183,6 +184,21 @@ public: BN_mpi2bn(pch, p - pch, this); } + uint64 getuint64() + { + unsigned int nSize = BN_bn2mpi(this, NULL); + if (nSize < 4) + return 0; + std::vector vch(nSize); + BN_bn2mpi(this, &vch[0]); + if (vch.size() > 4) + vch[4] &= 0x7f; + uint64 n = 0; + for (int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--) + ((unsigned char*)&n)[i] = vch[j]; + return n; + } + void setuint256(uint256 n) { unsigned char pch[sizeof(n) + 6]; @@ -221,7 +237,7 @@ public: if (vch.size() > 4) vch[4] &= 0x7f; uint256 n = 0; - for (int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--) + for (unsigned int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--) ((unsigned char*)&n)[i] = vch[j]; return n; } @@ -296,12 +312,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)