Fixed size types instead of unsigned char for numbers.
authorMASM fan <masmfan@gmail.com>
Sun, 28 Dec 2014 22:32:04 +0000 (14:32 -0800)
committerMASM fan <masmfan@gmail.com>
Sun, 28 Dec 2014 22:32:04 +0000 (14:32 -0800)
* uint8_t instead of unsigned char should be better in terms of compatibility;
* Fix Clang compilation issues by using uint16_t for stack size. The uint16_t type gives us a range of possible values which is more than sufficient for allowed script length.

src/bignum.h
src/script.cpp

index 58da20c..04e564b 100644 (file)
@@ -88,7 +88,7 @@ public:
     CBigNum(uint64_t n) { BN_init(this); setuint64(n); }
 
     explicit CBigNum(uint256 n) { BN_init(this); setuint256(n); }
-    explicit CBigNum(const std::vector<unsigned char>& vch)
+    explicit CBigNum(const std::vector<uint8_t>& vch)
     {
         BN_init(this);
         setvch(vch);
@@ -150,8 +150,8 @@ public:
 
     void setint64(int64_t sn)
     {
-        unsigned char pch[sizeof(sn) + 6];
-        unsigned char* p = pch + 4;
+        uint8_t pch[sizeof(sn) + 6];
+        uint8_t* p = pch + 4;
         bool fNegative;
         uint64_t n;
 
@@ -169,7 +169,7 @@ public:
         bool fLeadingZeroes = true;
         for (int i = 0; i < 8; i++)
         {
-            unsigned char c = (n >> 56) & 0xff;
+            uint8_t c = (n >> 56) & 0xff;
             n <<= 8;
             if (fLeadingZeroes)
             {
@@ -196,31 +196,32 @@ public:
         unsigned int nSize = BN_bn2mpi(this, NULL);
         if (nSize < 4)
             return 0;
-        std::vector<unsigned char> vch(nSize);
+        std::vector<uint8_t> vch(nSize);
         BN_bn2mpi(this, &vch[0]);
         if (vch.size() > 4)
             vch[4] &= 0x7f;
         uint64_t n = 0;
         for (unsigned int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
-            ((unsigned char*)&n)[i] = vch[j];
+            ((uint8_t*)&n)[i] = vch[j];
         return n;
     }
 
     void setuint64(uint64_t n)
     {
-        if (sizeof(n) == sizeof(size_t))
+        // Use BN_set_word if word size is sufficient for uint64_t
+        if (sizeof(n) <= sizeof(BN_ULONG))
         {
             if (!BN_set_word(this, n))
                 throw bignum_error("CBigNum conversion from uint64_t : BN_set_word failed");
             return;
         }
 
-        unsigned char pch[sizeof(n) + 6];
-        unsigned char* p = pch + 4;
+        uint8_t pch[sizeof(n) + 6];
+        uint8_t* p = pch + 4;
         bool fLeadingZeroes = true;
         for (int i = 0; i < 8; i++)
         {
-            unsigned char c = (n >> 56) & 0xff;
+            uint8_t c = (n >> 56) & 0xff;
             n <<= 8;
             if (fLeadingZeroes)
             {
@@ -242,14 +243,14 @@ public:
 
     void setuint160(uint160 n)
     {
-        unsigned char pch[sizeof(n) + 6];
-        unsigned char* p = pch + 4;
+        uint8_t pch[sizeof(n) + 6];
+        uint8_t* p = pch + 4;
         bool fLeadingZeroes = true;
-        unsigned char* pbegin = (unsigned char*)&n;
-        unsigned char* psrc = pbegin + sizeof(n);
+        uint8_t* pbegin = (uint8_t*)&n;
+        uint8_t* psrc = pbegin + sizeof(n);
         while (psrc != pbegin)
         {
-            unsigned char c = *(--psrc);
+            uint8_t c = *(--psrc);
             if (fLeadingZeroes)
             {
                 if (c == 0)
@@ -273,26 +274,26 @@ public:
         unsigned int nSize = BN_bn2mpi(this, NULL);
         if (nSize < 4)
             return 0;
-        std::vector<unsigned char> vch(nSize);
+        std::vector<uint8_t> vch(nSize);
         BN_bn2mpi(this, &vch[0]);
         if (vch.size() > 4)
             vch[4] &= 0x7f;
         uint160 n = 0;
         for (unsigned int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
-            ((unsigned char*)&n)[i] = vch[j];
+            ((uint8_t*)&n)[i] = vch[j];
         return n;
     }
 
     void setuint256(uint256 n)
     {
-        unsigned char pch[sizeof(n) + 6];
-        unsigned char* p = pch + 4;
+        uint8_t pch[sizeof(n) + 6];
+        uint8_t* p = pch + 4;
         bool fLeadingZeroes = true;
-        unsigned char* pbegin = (unsigned char*)&n;
-        unsigned char* psrc = pbegin + sizeof(n);
+        uint8_t* pbegin = (uint8_t*)&n;
+        uint8_t* psrc = pbegin + sizeof(n);
         while (psrc != pbegin)
         {
-            unsigned char c = *(--psrc);
+            uint8_t c = *(--psrc);
             if (fLeadingZeroes)
             {
                 if (c == 0)
@@ -316,26 +317,26 @@ public:
         unsigned int nSize = BN_bn2mpi(this, NULL);
         if (nSize < 4)
             return 0;
-        std::vector<unsigned char> vch(nSize);
+        std::vector<uint8_t> vch(nSize);
         BN_bn2mpi(this, &vch[0]);
         if (vch.size() > 4)
             vch[4] &= 0x7f;
         uint256 n = 0;
         for (unsigned int i = 0, j = vch.size()-1; i < sizeof(n) && j >= 4; i++, j--)
-            ((unsigned char*)&n)[i] = vch[j];
+            ((uint8_t*)&n)[i] = vch[j];
         return n;
     }
 
-    void setBytes(const std::vector<unsigned char>& vchBytes)
+    void setBytes(const std::vector<uint8_t>& vchBytes)
     {
         BN_bin2bn(&vchBytes[0], vchBytes.size(), this);
     }
 
-    std::vector<unsigned char> getBytes() const
+    std::vector<uint8_t> getBytes() const
     {
         int nBytes = BN_num_bytes(this);
 
-        std::vector<unsigned char> vchBytes(nBytes);
+        std::vector<uint8_t> vchBytes(nBytes);
 
         int n = BN_bn2bin(this, &vchBytes[0]);
         if (n != nBytes) {
@@ -345,9 +346,9 @@ public:
         return vchBytes;
     }
 
-    void setvch(const std::vector<unsigned char>& vch)
+    void setvch(const std::vector<uint8_t>& vch)
     {
-        std::vector<unsigned char> vch2(vch.size() + 4);
+        std::vector<uint8_t> vch2(vch.size() + 4);
         uint32_t nSize = vch.size();
         // BIGNUM's byte stream format expects 4 bytes of
         // big endian size data info at the front
@@ -360,12 +361,12 @@ public:
         BN_mpi2bn(&vch2[0], vch2.size(), this);
     }
 
-    std::vector<unsigned char> getvch() const
+    std::vector<uint8_t> getvch() const
     {
         unsigned int nSize = BN_bn2mpi(this, NULL);
         if (nSize <= 4)
-            return std::vector<unsigned char>();
-        std::vector<unsigned char> vch(nSize);
+            return std::vector<uint8_t>();
+        std::vector<uint8_t> vch(nSize);
         BN_bn2mpi(this, &vch[0]);
         vch.erase(vch.begin(), vch.begin() + 4);
         reverse(vch.begin(), vch.end());
@@ -375,7 +376,7 @@ public:
     CBigNum& SetCompact(uint32_t nCompact)
     {
         uint32_t nSize = nCompact >> 24;
-        std::vector<unsigned char> vch(4 + nSize);
+        std::vector<uint8_t> vch(4 + nSize);
         vch[3] = nSize;
         if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff;
         if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff;
@@ -387,7 +388,7 @@ public:
     uint32_t GetCompact() const
     {
         uint32_t nSize = BN_bn2mpi(this, NULL);
-        std::vector<unsigned char> vch(nSize);
+        std::vector<uint8_t> vch(nSize);
         nSize -= 4;
         BN_bn2mpi(this, &vch[0]);
         uint32_t nCompact = nSize << 24;
@@ -420,7 +421,7 @@ public:
         while (isxdigit(*psz))
         {
             *this <<= 4;
-            int n = phexdigit[(unsigned char)*psz++];
+            int n = phexdigit[(uint8_t)*psz++];
             *this += n;
         }
         if (fNegative)
@@ -444,7 +445,7 @@ public:
             if (!BN_div(&dv, &rem, &bn, &bnBase, pctx))
                 throw bignum_error("CBigNum::ToString() : BN_div failed");
             bn = dv;
-            unsigned int c = rem.getuint32();
+            uint32_t c = rem.getuint32();
             str += "0123456789abcdef"[c];
         }
         if (BN_is_negative(this))
@@ -472,7 +473,7 @@ public:
     template<typename Stream>
     void Unserialize(Stream& s, int nType=0, int nVersion=PROTOCOL_VERSION)
     {
-        std::vector<unsigned char> vch;
+        std::vector<uint8_t> vch;
         ::Unserialize(s, vch, nType, nVersion);
         setvch(vch);
     }
@@ -509,7 +510,7 @@ public:
         CBigNum ret;
         if (!BN_mod_mul(&ret, this, &b, &m, pctx))
             throw bignum_error("CBigNum::mul_mod : BN_mod_mul failed");
-        
+
         return ret;
     }
 
index 7b25637..f30f3a2 100644 (file)
@@ -575,7 +575,7 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, co
                 case OP_DEPTH:
                 {
                     // -- stacksize
-                    CBigNum bn(stack.size());
+                    CBigNum bn((uint16_t) stack.size());
                     stack.push_back(bn.getvch());
                 }
                 break;
@@ -673,7 +673,7 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, co
                     // (in -- in size)
                     if (stack.size() < 1)
                         return false;
-                    CBigNum bn(stacktop(-1).size());
+                    CBigNum bn((uint16_t) stacktop(-1).size());
                     stack.push_back(bn.getvch());
                 }
                 break;