Work toward big endian

This commit is contained in:
Arthur Britto
2012-05-31 01:51:58 -07:00
parent 61e075bff3
commit dad9568f4f
6 changed files with 100 additions and 108 deletions

View File

@@ -16,7 +16,7 @@ bool STAmount::currencyFromString(uint160& uDstCurrency, const std::string& sCur
if (sCurrency.empty() || !sCurrency.compare(SYSTEM_CURRENCY_CODE))
{
uDstCurrency = 0;
uDstCurrency.zero();
}
else if (3 == sCurrency.size())
{

View File

@@ -62,8 +62,8 @@ public:
}
bool addHeldTransaction(Transaction::pointer trans);
uint64 getBalance(std::string& strAcctID, const uint160 currency = 0);
uint64 getBalance(const NewcoinAddress& acctID, const uint160 currency = 0);
uint64 getBalance(std::string& strAcctID, const uint160 currency);
uint64 getBalance(const NewcoinAddress& acctID, const uint160 currency);
};
#endif

View File

@@ -80,9 +80,14 @@ class hash_SMN
{ // These must be randomized for release
public:
std::size_t operator() (const SHAMapNode& mn) const
#if 0
{ return mn.getDepth() ^ static_cast<std::size_t>(mn.getNodeID().GetAt(0)); }
#else
{ return mn.getDepth() ^ *reinterpret_cast<const std::size_t *>(mn.getNodeID().begin()); }
#endif
std::size_t operator() (const uint256& u) const
{ return static_cast<std::size_t>(u.GetAt(0)); }
{ return *reinterpret_cast<const std::size_t *>(u.begin()); }
};
class SHAMapItem

View File

@@ -2,6 +2,7 @@
#include <cstring>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/smart_ptr/make_shared.hpp>
@@ -13,12 +14,9 @@
std::string SHAMapNode::getString() const
{
std::string ret="NodeID(";
ret+=boost::lexical_cast<std::string>(mDepth);
ret+=",";
ret+=mNodeID.GetHex();
ret+=")";
return ret;
return str(boost::format("NodeID(%s,%s)")
% boost::lexical_cast<std::string>(mDepth)
% mNodeID.GetHex());
}
uint256 SHAMapNode::smMasks[64];
@@ -77,7 +75,7 @@ void SHAMapNode::ClassInit()
for(int i = 0; i < 64; i += 2)
{
smMasks[i] = selector;
*(selector.begin() + (i / 2)) = 0x0F;
*(selector.begin() + (i / 2)) = 0xF0;
smMasks[i + 1]=selector;
*(selector.begin() + (i / 2)) = 0xFF;
}
@@ -123,13 +121,14 @@ SHAMapNode SHAMapNode::getChildNodeID(int m) const
assert((m >= 0) && (m < 16));
uint256 child(mNodeID);
child.PeekAt(mDepth / 8) |= m << (4 * (mDepth % 8));
child.begin()[mDepth/2] |= (mDepth & 1) ? m : m << 4;
return SHAMapNode(mDepth + 1, child);
}
int SHAMapNode::selectBranch(const uint256& hash) const
{ // Which branch would contain the specified hash
#ifdef DEBUG
if (mDepth == 63)
{
@@ -147,8 +146,10 @@ int SHAMapNode::selectBranch(const uint256& hash) const
#endif
int branch = *(hash.begin() + (mDepth / 2));
if (mDepth % 2) branch >>= 4;
else branch &= 0xf;
if (mDepth & 1)
branch &= 0xf;
else
branch >>= 4;
assert((branch >= 0) && (branch < 16));
return branch;
@@ -259,6 +260,7 @@ void SHAMapTreeNode::addRaw(Serializer &s)
for (int i = 0; i < 16; ++i)
s.add256(mHashes[i]);
s.add8(2);
}

View File

@@ -14,6 +14,7 @@
#include <cassert>
#include "types.h"
#include "utils.h"
#include <openssl/bn.h>
@@ -33,6 +34,9 @@ class base_uint
{
protected:
enum { WIDTH=BITS/32 };
// This is really big-endian in byte order.
// We use unsigned int for speed.
unsigned int pn[WIDTH];
public:
@@ -61,16 +65,17 @@ public:
ret.pn[i] = ~pn[i];
return ret;
}
#if 0
base_uint& operator=(uint64 b)
{
pn[0] = (unsigned int)b;
pn[1] = (unsigned int)(b >> 32);
for (int i = 2; i < WIDTH; i++)
pn[i] = 0;
zero();
// Put in least significant bits.
((uint64_t *) end())[-1] = htobe64(b);
return *this;
}
#endif
base_uint& operator^=(const base_uint& b)
{
for (int i = 0; i < WIDTH; i++)
@@ -95,9 +100,10 @@ public:
base_uint& operator++()
{
// prefix operator
int i = 0;
while (++pn[i] == 0 && i < WIDTH-1)
i++;
for (int i = WIDTH-1; ++pn[i] == 0 && i; i--)
nothing();
return *this;
}
@@ -112,9 +118,10 @@ public:
base_uint& operator--()
{
// prefix operator
unsigned i = 0;
while (--pn[i] == -1 && i < WIDTH-1)
i++;
for (int i = WIDTH-1; --pn[i] == (unsigned int) -1 && i; i--)
nothing();
return *this;
}
@@ -126,52 +133,36 @@ public:
return ret;
}
friend inline int compare(const base_uint& a, const base_uint& b)
{
const unsigned char* pA = a.begin();
const unsigned char* pAEnd = a.end();
const unsigned char* pB = b.begin();
while (pA != pAEnd && *pA == *pB)
pA++, pB++;
return pA == pAEnd ? 0 : *pA < *pB ? -1 : *pA > *pB ? 1 : 0;
}
friend inline bool operator<(const base_uint& a, const base_uint& b)
{
for (int i = base_uint::WIDTH-1; i >= 0; i--)
{
if (a.pn[i] < b.pn[i])
return true;
else if (a.pn[i] > b.pn[i])
return false;
}
return false;
return compare(a, b) < 0;
}
friend inline bool operator<=(const base_uint& a, const base_uint& b)
{
for (int i = base_uint::WIDTH-1; i >= 0; i--)
{
if (a.pn[i] < b.pn[i])
return true;
else if (a.pn[i] > b.pn[i])
return false;
}
return true;
return compare(a, b) <= 0;
}
friend inline bool operator>(const base_uint& a, const base_uint& b)
{
for (int i = base_uint::WIDTH-1; i >= 0; i--)
{
if (a.pn[i] > b.pn[i])
return true;
else if (a.pn[i] < b.pn[i])
return false;
}
return false;
return compare(a, b) > 0;
}
friend inline bool operator>=(const base_uint& a, const base_uint& b)
{
for (int i = base_uint::WIDTH-1; i >= 0; i--)
{
if (a.pn[i] > b.pn[i])
return true;
else if (a.pn[i] < b.pn[i])
return false;
}
return true;
return compare(a, b) >= 0;
}
friend inline bool operator==(const base_uint& a, const base_uint& b)
@@ -186,23 +177,10 @@ public:
{
return (!(a == b));
}
#if 1
unsigned int GetAt(int j) const
{
return pn[j];
}
unsigned int& PeekAt(int j)
{
return pn[j];
}
#endif
std::string GetHex() const
{
char psz[sizeof(pn)*2 + 1];
for (int i = 0; i < sizeof(pn); i++)
sprintf(psz + i*2, "%02X", ((unsigned char*)pn)[sizeof(pn) - i - 1]);
return std::string(psz, psz + sizeof(pn)*2);
return strHex(begin(), size());
}
void SetHex(const char* psz)
@@ -224,20 +202,23 @@ public:
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 };
const char* pbegin = psz;
const char* pBegin = psz;
while (phexdigit[(int) *psz] || *psz == '0')
psz++;
psz--;
unsigned char* p1 = (unsigned char*)pn;
unsigned char* pend = p1 + WIDTH * 4;
while (psz >= pbegin && p1 < pend)
unsigned char* pOut = begin();
if (psz-pBegin > 2*size())
psz = pBegin + 2*size();
while (pBegin != psz)
{
*p1 = phexdigit[(unsigned char)*psz--];
if (psz >= pbegin)
{
*p1 |= (phexdigit[(unsigned char)*psz--] << 4);
p1++;
}
unsigned char cHigh = phexdigit[(unsigned char) *pBegin++] << 4;
unsigned char cLow = pBegin == psz
? 0
: phexdigit[(unsigned char) *pBegin++];
*pOut++ = cHigh | cLow;
}
}
@@ -253,22 +234,22 @@ public:
unsigned char* begin()
{
return (unsigned char*)&pn[0];
return (unsigned char*) &pn[0];
}
unsigned char* end()
{
return (unsigned char*)&pn[WIDTH];
return (unsigned char*) &pn[WIDTH];
}
const unsigned char* begin() const
{
return (const unsigned char*)&pn[0];
return (const unsigned char*) &pn[0];
}
const unsigned char* end() const
{
return (unsigned char*)&pn[WIDTH];
return (unsigned char*) &pn[WIDTH];
}
unsigned int size() const
@@ -388,18 +369,16 @@ public:
uint160(uint64 b)
{
pn[0] = (unsigned int)(b & 0xffffffffu);
pn[1] = (unsigned int)(b >> 32);
for (int i = 2; i < WIDTH; i++)
pn[i] = 0;
*this = b;
}
uint160& operator=(uint64 b)
{
pn[0] = (unsigned int)(b & 0xffffffffu);
pn[1] = (unsigned int)(b >> 32);
for (int i = 2; i < WIDTH; i++)
pn[i] = 0;
zero();
// Put in least significant bits.
((uint64_t *) end())[-1] = htobe64(b);
return *this;
}
@@ -413,7 +392,7 @@ public:
if (vch.size() == sizeof(pn))
memcpy(pn, &vch[0], sizeof(pn));
else
*this = 0;
zero();
}
base_uint256 to256() const;
@@ -443,6 +422,11 @@ inline const uint160 operator^(const uint160& a, const uint160& b) { return (b
inline const uint160 operator&(const uint160& a, const uint160& b) { return (base_uint160)a & (base_uint160)b; }
inline const uint160 operator|(const uint160& a, const uint160& b) { return (base_uint160)a | (base_uint160)b; }
inline const std::string strHex(const uint160& ui)
{
return strHex(ui.begin(), ui.size());
}
//////////////////////////////////////////////////////////////////////////////
//
// uint256
@@ -474,18 +458,16 @@ public:
uint256(uint64 b)
{
pn[0] = (unsigned int)(b & 0xffffffff);
pn[1] = (unsigned int)(b >> 32);
for (int i = 2; i < WIDTH; i++)
pn[i] = 0;
*this = b;
}
uint256& operator=(uint64 b)
{
pn[0] = (unsigned int)(b & 0xffffffff);
pn[1] = (unsigned int)(b >> 32);
for (int i = 2; i < WIDTH; i++)
pn[i] = 0;
zero();
// Put in least significant bits.
((uint64_t *) end())[-1] = htobe64(b);
return *this;
}
@@ -501,7 +483,7 @@ public:
else
{
assert(false);
*this = 0;
zero();
}
}

View File

@@ -6,7 +6,8 @@
#include <openssl/dh.h>
#include "uint256.h"
#include "types.h"
// #include "uint256.h"
#define nothing() do {} while (0)
@@ -64,9 +65,11 @@ inline std::string strHex(const std::vector<unsigned char>& vucData)
return strHex(vucData.begin(), vucData.size());
}
inline const std::string strHex(const uint160& ui)
inline std::string strHex(const uint64 uiHost)
{
return strHex(ui.begin(), ui.size());
uint64_t uBig = htobe64(uiHost);
return strHex((unsigned char*) &uBig, sizeof(uBig));
}
template<class Iterator>