From 06e7857bc64ba96e3646d579a6454d156dfb63c1 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Thu, 31 May 2012 10:08:24 -0700 Subject: [PATCH 1/2] Clean up unit256.h --- src/uint256.h | 83 +++++++++++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 43 deletions(-) diff --git a/src/uint256.h b/src/uint256.h index c87ffcb3ed..aee6185d06 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -16,8 +16,6 @@ #include "types.h" #include "utils.h" -#include - #if defined(_MSC_VER) && _MSC_VER < 1300 #define for if (false) ; else for #endif @@ -61,8 +59,10 @@ public: const base_uint operator~() const { base_uint ret; + for (int i = 0; i < WIDTH; i++) ret.pn[i] = ~pn[i]; + return ret; } @@ -80,6 +80,7 @@ public: { for (int i = 0; i < WIDTH; i++) pn[i] ^= b.pn[i]; + return *this; } @@ -87,6 +88,7 @@ public: { for (int i = 0; i < WIDTH; i++) pn[i] &= b.pn[i]; + return *this; } @@ -94,13 +96,13 @@ public: { for (int i = 0; i < WIDTH; i++) pn[i] |= b.pn[i]; + return *this; } base_uint& operator++() { // prefix operator - for (int i = WIDTH-1; ++pn[i] == 0 && i; i--) nothing(); @@ -112,13 +114,13 @@ public: // postfix operator const base_uint ret = *this; ++(*this); + return ret; } base_uint& operator--() { // prefix operator - for (int i = WIDTH-1; --pn[i] == (unsigned int) -1 && i; i--) nothing(); @@ -130,6 +132,7 @@ public: // postfix operator const base_uint ret = *this; --(*this); + return ret; } @@ -167,15 +170,12 @@ public: friend inline bool operator==(const base_uint& a, const base_uint& b) { - for (int i = 0; i < base_uint::WIDTH; i++) - if (a.pn[i] != b.pn[i]) - return false; - return true; + return !compare(a, b); } friend inline bool operator!=(const base_uint& a, const base_uint& b) { - return (!(a == b)); + return !!compare(a, b); } std::string GetHex() const @@ -185,8 +185,7 @@ public: void SetHex(const char* psz) { - for (int i = 0; i < WIDTH; i++) - pn[i] = 0; + zero(); // skip leading spaces while (isspace(*psz)) @@ -196,28 +195,29 @@ public: if (psz[0] == '0' && tolower(psz[1]) == 'x') psz += 2; - // hex string to uint - 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 }; + // hex char to int + static signed char phexdigit[256] = { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1, -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,0 }; - const char* pBegin = psz; - while (phexdigit[(int) *psz] || *psz == '0') - psz++; - psz--; + const unsigned char* pEnd = reinterpret_cast(psz); + const unsigned char* pBegin = pEnd; - unsigned char* pOut = begin(); + while (phexdigit[*pEnd] >= 0) + pEnd++; - if (psz-pBegin > 2*size()) - psz = pBegin + 2*size(); + if (pEnd-pBegin > 2*size()) + pBegin = pEnd - 2*size(); - while (pBegin != psz) + unsigned char* pOut = end()-((pEnd-pBegin+1)/2); + + while (pBegin != pEnd) { - unsigned char cHigh = phexdigit[(unsigned char) *pBegin++] << 4; - unsigned char cLow = pBegin == psz + unsigned char cHigh = phexdigit[*pBegin++] << 4; + unsigned char cLow = pBegin == pEnd ? 0 - : phexdigit[(unsigned char) *pBegin++]; + : phexdigit[*pBegin++]; *pOut++ = cHigh | cLow; } } @@ -306,34 +306,33 @@ public: uint128() { - for (int i = 0; i < WIDTH; i++) - pn[i] = 0; + zero(); } uint128(const basetype& b) { - for (int i = 0; i < WIDTH; i++) - pn[i] = b.pn[i]; + *this = b; } uint128& operator=(const basetype& b) { for (int i = 0; i < WIDTH; i++) pn[i] = b.pn[i]; + return *this; } - explicit uint128(const base_uint256 b) { + explicit uint128(const base_uint256& b) { for (int i = 0; i < WIDTH; i++) pn[i] = b.pn[i]; } explicit uint128(const std::vector& vch) { - if (vch.size() == sizeof(pn)) - memcpy(pn, &vch[0], sizeof(pn)); + if (vch.size() == size()) + memcpy(pn, &vch[0], size()); else - memset(pn, 0, sizeof(pn)); + zero(); } }; @@ -350,20 +349,19 @@ public: uint160() { - for (int i = 0; i < WIDTH; i++) - pn[i] = 0; + zero(); } uint160(const basetype& b) { - for (int i = 0; i < WIDTH; i++) - pn[i] = b.pn[i]; + *this = b; } uint160& operator=(const basetype& b) { for (int i = 0; i < WIDTH; i++) pn[i] = b.pn[i]; + return *this; } @@ -439,20 +437,19 @@ public: uint256() { - for (int i = 0; i < WIDTH; i++) - pn[i] = 0; + zero(); } uint256(const basetype& b) { - for (int i = 0; i < WIDTH; i++) - pn[i] = b.pn[i]; + *this = b; } uint256& operator=(const basetype& b) { for (int i = 0; i < WIDTH; i++) pn[i] = b.pn[i]; + return *this; } From b8cb048e058fac88d57ac80c8e77f425979469d3 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Thu, 31 May 2012 12:42:42 -0700 Subject: [PATCH 2/2] Fix string to hex for uint256.h --- src/NewcoinAddress.cpp | 2 +- src/uint256.h | 25 +++++++++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/NewcoinAddress.cpp b/src/NewcoinAddress.cpp index 6f7d597db9..05179cfd06 100644 --- a/src/NewcoinAddress.cpp +++ b/src/NewcoinAddress.cpp @@ -702,7 +702,7 @@ BOOST_AUTO_TEST_SUITE(newcoin_address) BOOST_AUTO_TEST_CASE( my_test ) { - BOOST_CHECK( false ); + // BOOST_CHECK( false ); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/uint256.h b/src/uint256.h index aee6185d06..ae7a9dae58 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -196,10 +196,27 @@ public: psz += 2; // hex char to int - static signed char phexdigit[256] = { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1, -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,0 }; + static signed char phexdigit[256] = { + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1, + + -1,0xa,0xb,0xc, 0xd,0xe,0xf,-1, -1,-1,-1,-1, -1,-1,-1,-1, + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + -1,0xa,0xb,0xc, 0xd,0xe,0xf,-1, -1,-1,-1,-1, -1,-1,-1,-1, + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, + }; const unsigned char* pEnd = reinterpret_cast(psz); const unsigned char* pBegin = pEnd;