Reorganize source files and modules

This commit is contained in:
Vinnie Falco
2013-09-22 17:26:40 -07:00
parent 2d65ab4021
commit 19eff08e16
77 changed files with 1905 additions and 1574 deletions

View File

@@ -1,188 +0,0 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2011 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.
char const* Base58::s_currentAlphabet = Base58::getRippleAlphabet ();
char const* Base58::getCurrentAlphabet ()
{
return s_currentAlphabet;
}
void Base58::setCurrentAlphabet (char const* alphabet)
{
s_currentAlphabet = alphabet;
}
char const* Base58::getBitcoinAlphabet ()
{
return "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
}
char const* Base58::getRippleAlphabet ()
{
return "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz";
}
char const* Base58::getTestnetAlphabet ()
{
return "RPShNAF39wBUDnEGHJKLM4pQrsT7VWXYZ2bcdeCg65jkm8ofqi1tuvaxyz";
}
std::string Base58::encode (const unsigned char* pbegin, const unsigned char* pend)
{
char const* alphabet = getCurrentAlphabet ();
CAutoBN_CTX pctx;
CBigNum bn58 = 58;
CBigNum bn0 = 0;
// Convert big endian data to little endian
// Extra zero at the end make sure bignum will interpret as a positive number
Blob vchTmp (pend - pbegin + 1, 0);
std::reverse_copy (pbegin, pend, vchTmp.begin ());
// Convert little endian data to bignum
CBigNum bn (vchTmp);
// Convert bignum to std::string
std::string str;
// Expected size increase from base58 conversion is approximately 137%
// use 138% to be safe
str.reserve ((pend - pbegin) * 138 / 100 + 1);
CBigNum dv;
CBigNum rem;
while (bn > bn0)
{
if (!BN_div (&dv, &rem, &bn, &bn58, pctx))
throw bignum_error ("EncodeBase58 : BN_div failed");
bn = dv;
unsigned int c = rem.getuint ();
str += alphabet [c];
}
// Leading zeroes encoded as base58 zeros
for (const unsigned char* p = pbegin; p < pend && *p == 0; p++)
str += alphabet [0];
// Convert little endian std::string to big endian
reverse (str.begin (), str.end ());
return str;
}
std::string Base58::encode (Blob const& vch)
{
return encode (&vch[0], &vch[0] + vch.size ());
}
std::string Base58::encodeWithCheck (Blob const& vchIn)
{
// add 4-byte hash check to the end
Blob vch (vchIn);
uint256 hash = SHA256Hash (vch.begin (), vch.end ());
vch.insert (vch.end (), (unsigned char*)&hash, (unsigned char*)&hash + 4);
return encode (vch);
}
bool Base58::decode (const char* psz, Blob& vchRet, const char* pAlpha)
{
assert (pAlpha != 0);
CAutoBN_CTX pctx;
vchRet.clear ();
CBigNum bn58 = 58;
CBigNum bn = 0;
CBigNum bnChar;
while (isspace (*psz))
psz++;
// Convert big endian string to bignum
for (const char* p = psz; *p; p++)
{
const char* p1 = strchr (pAlpha, *p);
if (p1 == NULL)
{
while (isspace (*p))
p++;
if (*p != '\0')
return false;
break;
}
bnChar.setuint (p1 - pAlpha);
if (!BN_mul (&bn, &bn, &bn58, pctx))
throw bignum_error ("DecodeBase58 : BN_mul failed");
bn += bnChar;
}
// Get bignum as little endian data
Blob vchTmp = bn.getvch ();
// Trim off sign byte if present
if (vchTmp.size () >= 2 && vchTmp.end ()[-1] == 0 && vchTmp.end ()[-2] >= 0x80)
vchTmp.erase (vchTmp.end () - 1);
// Restore leading zeros
int nLeadingZeros = 0;
for (const char* p = psz; *p == pAlpha[0]; p++)
nLeadingZeros++;
vchRet.assign (nLeadingZeros + vchTmp.size (), 0);
// Convert little endian data to big endian
std::reverse_copy (vchTmp.begin (), vchTmp.end (), vchRet.end () - vchTmp.size ());
return true;
}
bool Base58::decode (const std::string& str, Blob& vchRet)
{
return decode (str.c_str (), vchRet);
}
bool Base58::decodeWithCheck (const char* psz, Blob& vchRet, const char* pAlphabet)
{
assert (pAlphabet != NULL);
if (!decode (psz, vchRet, pAlphabet))
return false;
if (vchRet.size () < 4)
{
vchRet.clear ();
return false;
}
uint256 hash = SHA256Hash (vchRet.begin (), vchRet.end () - 4);
if (memcmp (&hash, &vchRet.end ()[-4], 4) != 0)
{
vchRet.clear ();
return false;
}
vchRet.resize (vchRet.size () - 4);
return true;
}
bool Base58::decodeWithCheck (const std::string& str, Blob& vchRet, const char* pAlphabet)
{
return decodeWithCheck (str.c_str (), vchRet, pAlphabet);
}
// vim:ts=4

View File

@@ -1,49 +0,0 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2011 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.
//
// Why base-58 instead of standard base-64 encoding?
// - Don't want 0OIl characters that look the same in some fonts and
// could be used to create visually identical looking account numbers.
// - A string with non-alphanumeric characters is not as easily accepted as an account number.
// - E-mail usually won't line-break if there's no punctuation to break at.
// - Doubleclicking selects the whole number as one word if it's all alphanumeric.
//
#ifndef RIPPLE_BASE58_H
#define RIPPLE_BASE58_H
/** Performs Base 58 encoding and decoding.
*/
class Base58
{
public:
// VFALCO TODO clean up this poor API
static char const* getCurrentAlphabet ();
static void setCurrentAlphabet (char const* alphabet);
static char const* getBitcoinAlphabet ();
static char const* getRippleAlphabet ();
static char const* getTestnetAlphabet ();
static std::string encode (const unsigned char* pbegin, const unsigned char* pend);
static std::string encode (Blob const& vch);
static std::string encodeWithCheck (Blob const& vchIn);
static bool decode (const char* psz, Blob& vchRet, const char* pAlphabet = getCurrentAlphabet ());
static bool decode (const std::string& str, Blob& vchRet);
static bool decodeWithCheck (const char* psz, Blob& vchRet, const char* pAlphabet = getCurrentAlphabet ());
static bool decodeWithCheck (const std::string& str, Blob& vchRet, const char* pAlphabet);
private:
static char const* s_currentAlphabet;
};
#endif
// vim:ts=4

View File

@@ -1,643 +0,0 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2011 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.
CBigNum::CBigNum ()
{
BN_init (this);
}
CBigNum::CBigNum (const CBigNum& b)
{
BN_init (this);
if (!BN_copy (this, &b))
{
BN_clear_free (this);
throw bignum_error ("CBigNum::CBigNum(const CBigNum&) : BN_copy failed");
}
}
CBigNum& CBigNum::operator= (const CBigNum& b)
{
if (!BN_copy (this, &b))
throw bignum_error ("CBigNum::operator= : BN_copy failed");
return (*this);
}
CBigNum::~CBigNum ()
{
BN_clear_free (this);
}
CBigNum::CBigNum (char n)
{
BN_init (this);
if (n >= 0) setulong (n);
else setint64 (n);
}
CBigNum::CBigNum (short n)
{
BN_init (this);
if (n >= 0) setulong (n);
else setint64 (n);
}
CBigNum::CBigNum (int n)
{
BN_init (this);
if (n >= 0) setulong (n);
else setint64 (n);
}
CBigNum::CBigNum (long n)
{
BN_init (this);
if (n >= 0) setulong (n);
else setint64 (n);
}
CBigNum::CBigNum (int64 n)
{
BN_init (this);
setint64 (n);
}
CBigNum::CBigNum (unsigned char n)
{
BN_init (this);
setulong (n);
}
CBigNum::CBigNum (unsigned short n)
{
BN_init (this);
setulong (n);
}
CBigNum::CBigNum (unsigned int n)
{
BN_init (this);
setulong (n);
}
CBigNum::CBigNum (uint64 n)
{
BN_init (this);
setuint64 (n);
}
CBigNum::CBigNum (uint256 n)
{
BN_init (this);
setuint256 (n);
}
CBigNum::CBigNum (Blob const& vch)
{
BN_init (this);
setvch (vch);
}
void CBigNum::setuint (unsigned int n)
{
setulong (static_cast<unsigned long> (n));
}
unsigned int CBigNum::getuint () const
{
return BN_get_word (this);
}
int CBigNum::getint () const
{
unsigned long n = BN_get_word (this);
if (!BN_is_negative (this))
return (n > INT_MAX ? INT_MAX : n);
else
return (n > INT_MAX ? INT_MIN : - (int)n);
}
void CBigNum::setint64 (int64 n)
{
unsigned char pch[sizeof (n) + 6];
unsigned char* p = pch + 4;
bool fNegative = false;
if (n < (int64)0)
{
n = -n;
fNegative = true;
}
bool fLeadingZeroes = true;
for (int i = 0; i < 8; i++)
{
unsigned char c = (n >> 56) & 0xff;
n <<= 8;
if (fLeadingZeroes)
{
if (c == 0)
continue;
if (c & 0x80)
*p++ = (fNegative ? 0x80 : 0);
else if (fNegative)
c |= 0x80;
fLeadingZeroes = false;
}
*p++ = c;
}
unsigned int nSize = p - (pch + 4);
pch[0] = (nSize >> 24) & 0xff;
pch[1] = (nSize >> 16) & 0xff;
pch[2] = (nSize >> 8) & 0xff;
pch[3] = (nSize) & 0xff;
BN_mpi2bn (pch, p - pch, this);
}
uint64 CBigNum::getuint64 () const
{
#if (ULONG_MAX > UINT_MAX)
return static_cast<uint64> (getulong ());
#else
int len = BN_num_bytes (this);
if (len > 8)
throw std::runtime_error ("BN getuint64 overflow");
unsigned char buf[8];
memset (buf, 0, sizeof (buf));
BN_bn2bin (this, buf + 8 - len);
return
static_cast<uint64> (buf[0]) << 56 | static_cast<uint64> (buf[1]) << 48 |
static_cast<uint64> (buf[2]) << 40 | static_cast<uint64> (buf[3]) << 32 |
static_cast<uint64> (buf[4]) << 24 | static_cast<uint64> (buf[5]) << 16 |
static_cast<uint64> (buf[6]) << 8 | static_cast<uint64> (buf[7]);
#endif
}
void CBigNum::setuint64 (uint64 n)
{
#if (ULONG_MAX > UINT_MAX)
setulong (static_cast<unsigned long> (n));
#else
unsigned char buf[8];
buf[0] = static_cast<unsigned char> ((n >> 56) & 0xff);
buf[1] = static_cast<unsigned char> ((n >> 48) & 0xff);
buf[2] = static_cast<unsigned char> ((n >> 40) & 0xff);
buf[3] = static_cast<unsigned char> ((n >> 32) & 0xff);
buf[4] = static_cast<unsigned char> ((n >> 24) & 0xff);
buf[5] = static_cast<unsigned char> ((n >> 16) & 0xff);
buf[6] = static_cast<unsigned char> ((n >> 8) & 0xff);
buf[7] = static_cast<unsigned char> ((n) & 0xff);
BN_bin2bn (buf, 8, this);
#endif
}
void CBigNum::setuint256 (uint256 const& n)
{
BN_bin2bn (n.begin (), n.size (), NULL);
}
uint256 CBigNum::getuint256 ()
{
uint256 ret;
unsigned int size = BN_num_bytes (this);
if (size > ret.size ())
return ret;
BN_bn2bin (this, ret.begin () + (ret.size () - BN_num_bytes (this)));
return ret;
}
void CBigNum::setvch (Blob const& vch)
{
Blob 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
std::reverse_copy (vch.begin (), vch.end (), vch2.begin () + 4);
BN_mpi2bn (&vch2[0], vch2.size (), this);
}
Blob CBigNum::getvch () const
{
unsigned int nSize = BN_bn2mpi (this, NULL);
if (nSize < 4)
return Blob ();
Blob vch (nSize);
BN_bn2mpi (this, &vch[0]);
vch.erase (vch.begin (), vch.begin () + 4);
reverse (vch.begin (), vch.end ());
return vch;
}
CBigNum& CBigNum::SetCompact (unsigned int nCompact)
{
unsigned int nSize = nCompact >> 24;
Blob vch (4 + nSize);
vch[3] = nSize;
if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff;
if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff;
if (nSize >= 3) vch[6] = (nCompact >> 0) & 0xff;
BN_mpi2bn (&vch[0], vch.size (), this);
return *this;
}
unsigned int CBigNum::GetCompact () const
{
unsigned int nSize = BN_bn2mpi (this, NULL);
Blob vch (nSize);
nSize -= 4;
BN_bn2mpi (this, &vch[0]);
unsigned int nCompact = nSize << 24;
if (nSize >= 1) nCompact |= (vch[4] << 16);
if (nSize >= 2) nCompact |= (vch[5] << 8);
if (nSize >= 3) nCompact |= (vch[6] << 0);
return nCompact;
}
void CBigNum::SetHex (const std::string& str)
{
// skip 0x
const char* psz = str.c_str ();
while (isspace (*psz))
psz++;
bool fNegative = false;
if (*psz == '-')
{
fNegative = true;
psz++;
}
if (psz[0] == '0' && tolower (psz[1]) == 'x')
psz += 2;
while (isspace (*psz))
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
};
*this = 0;
while (isxdigit (*psz))
{
*this <<= 4;
int n = phexdigit[ (int) * psz++];
*this += n;
}
if (fNegative)
*this = 0 - *this;
}
std::string CBigNum::ToString (int nBase) const
{
CAutoBN_CTX pctx;
CBigNum bnBase = nBase;
CBigNum bn0 = 0;
std::string str;
CBigNum bn = *this;
BN_set_negative (&bn, false);
CBigNum dv;
CBigNum rem;
if (BN_cmp (&bn, &bn0) == 0)
return "0";
while (BN_cmp (&bn, &bn0) > 0)
{
if (!BN_div (&dv, &rem, &bn, &bnBase, pctx))
throw bignum_error ("CBigNum::ToString() : BN_div failed");
bn = dv;
unsigned int c = rem.getuint ();
str += "0123456789abcdef"[c];
}
if (BN_is_negative (this))
str += "-";
reverse (str.begin (), str.end ());
return str;
}
std::string CBigNum::GetHex () const
{
return ToString (16);
}
bool CBigNum::operator! () const
{
return BN_is_zero (this);
}
CBigNum& CBigNum::operator+= (const CBigNum& b)
{
if (!BN_add (this, this, &b))
throw bignum_error ("CBigNum::operator+= : BN_add failed");
return *this;
}
CBigNum& CBigNum::operator-= (const CBigNum& b)
{
*this = *this - b;
return *this;
}
CBigNum& CBigNum::operator*= (const CBigNum& b)
{
CAutoBN_CTX pctx;
if (!BN_mul (this, this, &b, pctx))
throw bignum_error ("CBigNum::operator*= : BN_mul failed");
return *this;
}
CBigNum& CBigNum::operator/= (const CBigNum& b)
{
*this = *this / b;
return *this;
}
CBigNum& CBigNum::operator%= (const CBigNum& b)
{
*this = *this % b;
return *this;
}
CBigNum& CBigNum::operator<<= (unsigned int shift)
{
if (!BN_lshift (this, this, shift))
throw bignum_error ("CBigNum:operator<<= : BN_lshift failed");
return *this;
}
CBigNum& CBigNum::operator>>= (unsigned int shift)
{
// Note: BN_rshift segfaults on 64-bit if 2^shift is greater than the number
// if built on ubuntu 9.04 or 9.10, probably depends on version of openssl
CBigNum a = 1;
a <<= shift;
if (BN_cmp (&a, this) > 0)
{
*this = 0;
return *this;
}
if (!BN_rshift (this, this, shift))
throw bignum_error ("CBigNum:operator>>= : BN_rshift failed");
return *this;
}
CBigNum& CBigNum::operator++ ()
{
// prefix operator
if (!BN_add (this, this, BN_value_one ()))
throw bignum_error ("CBigNum::operator++ : BN_add failed");
return *this;
}
const CBigNum CBigNum::operator++ (int)
{
// postfix operator
const CBigNum ret = *this;
++ (*this);
return ret;
}
CBigNum& CBigNum::operator-- ()
{
// prefix operator
CBigNum r;
if (!BN_sub (&r, this, BN_value_one ()))
throw bignum_error ("CBigNum::operator-- : BN_sub failed");
*this = r;
return *this;
}
const CBigNum CBigNum::operator-- (int)
{
// postfix operator
const CBigNum ret = *this;
-- (*this);
return ret;
}
void CBigNum::setulong (unsigned long n)
{
if (!BN_set_word (this, n))
throw bignum_error ("CBigNum conversion from unsigned long : BN_set_word failed");
}
unsigned long CBigNum::getulong () const
{
return BN_get_word (this);
}
const CBigNum operator+ (const CBigNum& a, const CBigNum& b)
{
CBigNum r;
if (!BN_add (&r, &a, &b))
throw bignum_error ("CBigNum::operator+ : BN_add failed");
return r;
}
const CBigNum operator- (const CBigNum& a, const CBigNum& b)
{
CBigNum r;
if (!BN_sub (&r, &a, &b))
throw bignum_error ("CBigNum::operator- : BN_sub failed");
return r;
}
const CBigNum operator- (const CBigNum& a)
{
CBigNum r (a);
BN_set_negative (&r, !BN_is_negative (&r));
return r;
}
const CBigNum operator* (const CBigNum& a, const CBigNum& b)
{
CAutoBN_CTX pctx;
CBigNum r;
if (!BN_mul (&r, &a, &b, pctx))
throw bignum_error ("CBigNum::operator* : BN_mul failed");
return r;
}
const CBigNum operator/ (const CBigNum& a, const CBigNum& b)
{
CAutoBN_CTX pctx;
CBigNum r;
if (!BN_div (&r, NULL, &a, &b, pctx))
throw bignum_error ("CBigNum::operator/ : BN_div failed");
return r;
}
const CBigNum operator% (const CBigNum& a, const CBigNum& b)
{
CAutoBN_CTX pctx;
CBigNum r;
if (!BN_mod (&r, &a, &b, pctx))
throw bignum_error ("CBigNum::operator% : BN_div failed");
return r;
}
const CBigNum operator<< (const CBigNum& a, unsigned int shift)
{
CBigNum r;
if (!BN_lshift (&r, &a, shift))
throw bignum_error ("CBigNum:operator<< : BN_lshift failed");
return r;
}
const CBigNum operator>> (const CBigNum& a, unsigned int shift)
{
CBigNum r = a;
r >>= shift;
return r;
}
bool operator== (const CBigNum& a, const CBigNum& b)
{
return (BN_cmp (&a, &b) == 0);
}
bool operator!= (const CBigNum& a, const CBigNum& b)
{
return (BN_cmp (&a, &b) != 0);
}
bool operator<= (const CBigNum& a, const CBigNum& b)
{
return (BN_cmp (&a, &b) <= 0);
}
bool operator>= (const CBigNum& a, const CBigNum& b)
{
return (BN_cmp (&a, &b) >= 0);
}
bool operator< (const CBigNum& a, const CBigNum& b)
{
return (BN_cmp (&a, &b) < 0);
}
bool operator> (const CBigNum& a, const CBigNum& b)
{
return (BN_cmp (&a, &b) > 0);
}
#if (ULONG_MAX > UINT_MAX)
int BN_add_word64 (BIGNUM* bn, uint64 word)
{
return BN_add_word (bn, word);
}
int BN_sub_word64 (BIGNUM* bn, uint64 word)
{
return BN_sub_word (bn, word);
}
int BN_mul_word64 (BIGNUM* bn, uint64 word)
{
return BN_mul_word (bn, word);
}
uint64 BN_div_word64 (BIGNUM* bn, uint64 word)
{
return BN_div_word (bn, word);
}
#else
int BN_add_word64 (BIGNUM* a, uint64 w)
{
CBigNum bn (w);
return BN_add (a, &bn, a);
}
int BN_sub_word64 (BIGNUM* a, uint64 w)
{
CBigNum bn (w);
return BN_sub (a, &bn, a);
}
int BN_mul_word64 (BIGNUM* a, uint64 w)
{
CBigNum bn (w);
CAutoBN_CTX ctx;
return BN_mul (a, &bn, a, ctx);
}
uint64 BN_div_word64 (BIGNUM* a, uint64 w)
{
CBigNum bn (w);
CAutoBN_CTX ctx;
return (BN_div (a, NULL, a, &bn, ctx) == 1) ? 0 : ((uint64) - 1);
}
#endif

View File

@@ -1,171 +0,0 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2011 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.
#ifndef RIPPLE_CBIGNUM_H
#define RIPPLE_CBIGNUM_H
//------------------------------------------------------------------------------
class bignum_error : public std::runtime_error
{
public:
explicit bignum_error (const std::string& str) : std::runtime_error (str) {}
};
//------------------------------------------------------------------------------
class CAutoBN_CTX
{
private:
CAutoBN_CTX (const CAutoBN_CTX&); // no implementation
CAutoBN_CTX& operator= (const CAutoBN_CTX&); // no implementation
protected:
BN_CTX* pctx;
CAutoBN_CTX& operator= (BN_CTX* pnew)
{
pctx = pnew;
return *this;
}
public:
CAutoBN_CTX ()
{
pctx = BN_CTX_new ();
if (pctx == NULL)
throw bignum_error ("CAutoBN_CTX : BN_CTX_new() returned NULL");
}
~CAutoBN_CTX ()
{
if (pctx != NULL)
BN_CTX_free (pctx);
}
operator BN_CTX* ()
{
return pctx;
}
BN_CTX& operator* ()
{
return *pctx;
}
BN_CTX** operator& ()
{
return &pctx;
}
bool operator! ()
{
return (pctx == NULL);
}
};
//------------------------------------------------------------------------------
// VFALCO TODO figure out a way to remove the dependency on openssl in the
// header. Maybe rewrite this to use cryptopp.
class CBigNum : public BIGNUM
{
public:
CBigNum ();
CBigNum (const CBigNum& b);
CBigNum& operator= (const CBigNum& b);
CBigNum (char n);
CBigNum (short n);
CBigNum (int n);
CBigNum (long n);
CBigNum (int64 n);
CBigNum (unsigned char n);
CBigNum (unsigned short n);
CBigNum (unsigned int n);
CBigNum (uint64 n);
explicit CBigNum (uint256 n);
explicit CBigNum (Blob const& vch);
~CBigNum ();
void setuint (unsigned int n);
unsigned int getuint () const;
int getint () const;
void setint64 (int64 n);
uint64 getuint64 () const;
void setuint64 (uint64 n);
void setuint256 (uint256 const& n);
uint256 getuint256 ();
void setvch (Blob const& vch);
Blob getvch () const;
CBigNum& SetCompact (unsigned int nCompact);
unsigned int GetCompact () const;
void SetHex (const std::string& str);
std::string ToString (int nBase = 10) const;
std::string GetHex () const;
bool operator! () const;
CBigNum& operator+= (const CBigNum& b);
CBigNum& operator-= (const CBigNum& b);
CBigNum& operator*= (const CBigNum& b);
CBigNum& operator/= (const CBigNum& b);
CBigNum& operator%= (const CBigNum& b);
CBigNum& operator<<= (unsigned int shift);
CBigNum& operator>>= (unsigned int shift);
CBigNum& operator++ ();
CBigNum& operator-- ();
const CBigNum operator++ (int);
const CBigNum operator-- (int);
friend inline const CBigNum operator- (const CBigNum& a, const CBigNum& b);
friend inline const CBigNum operator/ (const CBigNum& a, const CBigNum& b);
friend inline const CBigNum operator% (const CBigNum& a, const CBigNum& b);
private:
// private because the size of an unsigned long varies by platform
void setulong (unsigned long n);
unsigned long getulong () const;
};
const CBigNum operator+ (const CBigNum& a, const CBigNum& b);
const CBigNum operator- (const CBigNum& a, const CBigNum& b);
const CBigNum operator- (const CBigNum& a);
const CBigNum operator* (const CBigNum& a, const CBigNum& b);
const CBigNum operator/ (const CBigNum& a, const CBigNum& b);
const CBigNum operator% (const CBigNum& a, const CBigNum& b);
const CBigNum operator<< (const CBigNum& a, unsigned int shift);
const CBigNum operator>> (const CBigNum& a, unsigned int shift);
bool operator== (const CBigNum& a, const CBigNum& b);
bool operator!= (const CBigNum& a, const CBigNum& b);
bool operator<= (const CBigNum& a, const CBigNum& b);
bool operator>= (const CBigNum& a, const CBigNum& b);
bool operator< (const CBigNum& a, const CBigNum& b);
bool operator> (const CBigNum& a, const CBigNum& b);
//------------------------------------------------------------------------------
// VFALCO NOTE this seems as good a place as any for this.
// Here's the old implementation using macros, in case something broke
//#if (ULONG_MAX > UINT_MAX)
//#define BN_add_word64(bn, word) BN_add_word(bn, word)
//#define BN_sub_word64(bn, word) BN_sub_word(bn, word)
//#define BN_mul_word64(bn, word) BN_mul_word(bn, word)
//#define BN_div_word64(bn, word) BN_div_word(bn, word)
//#endif
// VFALCO I believe only STAmount uses these
extern int BN_add_word64 (BIGNUM* a, uint64 w);
extern int BN_sub_word64 (BIGNUM* a, uint64 w);
extern int BN_mul_word64 (BIGNUM* a, uint64 w);
extern uint64 BN_div_word64 (BIGNUM* a, uint64 w);
#endif
// vim:ts=4

View File

@@ -1,74 +0,0 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
PackedMessage::PackedMessage (::google::protobuf::Message const& message, int type)
{
unsigned const messageBytes = message.ByteSize ();
assert (messageBytes != 0);
mBuffer.resize (kHeaderBytes + messageBytes);
encodeHeader (messageBytes, type);
if (messageBytes != 0)
{
message.SerializeToArray (&mBuffer [PackedMessage::kHeaderBytes], messageBytes);
#ifdef BEAST_DEBUG
//Log::out() << "PackedMessage: type=" << type << ", datalen=" << msg_size;
#endif
}
}
bool PackedMessage::operator== (PackedMessage const& other) const
{
return mBuffer == other.mBuffer;
}
unsigned PackedMessage::getLength (std::vector <uint8_t> const& buf)
{
unsigned result;
if (buf.size () >= PackedMessage::kHeaderBytes)
{
result = buf [0];
result <<= 8;
result |= buf [1];
result <<= 8;
result |= buf [2];
result <<= 8;
result |= buf [3];
}
else
{
result = 0;
}
return result;
}
int PackedMessage::getType (std::vector<uint8_t> const& buf)
{
if (buf.size () < PackedMessage::kHeaderBytes)
return 0;
int ret = buf[4];
ret <<= 8;
ret |= buf[5];
return ret;
}
void PackedMessage::encodeHeader (unsigned size, int type)
{
assert (mBuffer.size () >= PackedMessage::kHeaderBytes);
mBuffer[0] = static_cast<boost::uint8_t> ((size >> 24) & 0xFF);
mBuffer[1] = static_cast<boost::uint8_t> ((size >> 16) & 0xFF);
mBuffer[2] = static_cast<boost::uint8_t> ((size >> 8) & 0xFF);
mBuffer[3] = static_cast<boost::uint8_t> (size & 0xFF);
mBuffer[4] = static_cast<boost::uint8_t> ((type >> 8) & 0xFF);
mBuffer[5] = static_cast<boost::uint8_t> (type & 0xFF);
}

View File

@@ -1,60 +0,0 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
//
// packaging of messages into length/type-prepended buffers
// ready for transmission.
#ifndef RIPPLE_PACKEDMESSAGE_H
#define RIPPLE_PACKEDMESSAGE_H
// PackedMessage implements simple "packing" of protocol buffers Messages into
// a string prepended by a header specifying the message length.
// MessageType should be a Message class generated by the protobuf compiler.
//
class PackedMessage : public boost::enable_shared_from_this <PackedMessage>
{
public:
typedef boost::shared_ptr< ::google::protobuf::Message > MessagePointer;
typedef boost::shared_ptr<PackedMessage> pointer;
public:
/** Number of bytes in a message header.
*/
static unsigned const kHeaderBytes = 6;
PackedMessage (::google::protobuf::Message const& message, int type);
/** Retrieve the packed message data.
*/
// VFALCO TODO shouldn't this be const?
std::vector <uint8_t>& getBuffer ()
{
return mBuffer;
}
/** Determine bytewise equality.
*/
bool operator == (PackedMessage const& other) const;
/** Calculate the length of a packed message.
*/
static unsigned getLength (std::vector <uint8_t> const& buf);
/** Determine the type of a packed message.
*/
static int getType (std::vector <uint8_t> const& buf);
private:
// Encodes the size and type into a header at the beginning of buf
//
void encodeHeader (unsigned size, int type);
std::vector <uint8_t> mBuffer;
};
#endif /* PACKEDMESSAGE_H */

View File

@@ -210,4 +210,3 @@ public:
};
#endif
// vim:ts=4

View File

@@ -10,7 +10,7 @@
//#include <cmath>
#include "beast/modules/beast_core/system/BeforeBoost.h" // must come first
#include "beast/modules/beast_core/system/BeforeBoost.h"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/trim.hpp>
@@ -28,9 +28,10 @@
#include <openssl/ecdsa.h>
#include <openssl/pem.h>
#include <openssl/hmac.h>
//#include <openssl/rand.h> // includes <windows.h> and causes errors due to #define GetMessage
#include <openssl/err.h>
#include "../ripple/sslutil/ripple_sslutil.h"
// VFALCO TODO fix these warnings!
#if BEAST_MSVC
#pragma warning (push)
@@ -44,22 +45,18 @@
namespace ripple
{
#include "crypto/Base58.h" // for RippleAddress
#include "crypto/CKey.h" // needs RippleAddress VFALCO TODO remove this dependency cycle
#include "crypto/RFC1751.h"
#include "crypto/CBigNum.cpp"
#include "crypto/CKey.cpp"
#include "crypto/CKeyDeterministic.cpp"
#include "crypto/CKeyECIES.cpp"
#include "crypto/Base58.cpp"
#include "crypto/Base58Data.cpp"
#include "crypto/RFC1751.cpp"
#include "protocol/BuildInfo.cpp"
#include "protocol/FieldNames.cpp"
#include "protocol/LedgerFormats.cpp"
#include "protocol/PackedMessage.cpp"
#include "protocol/RippleAddress.cpp"
#include "protocol/SerializedTypes.cpp"
#include "protocol/Serializer.cpp"

View File

@@ -4,43 +4,23 @@
*/
//==============================================================================
/** Include this to get the @ref ripple_data module.
#ifndef RIPPLE_DATA_H_INCLUDED
#define RIPPLE_DATA_H_INCLUDED
@file ripple_data.h
@ingroup ripple_data
*/
/** Ripple specific data representation and manipulation.
These form the building blocks of Ripple data.
@defgroup ripple_data
*/
#ifndef RIPPLE_DATA_RIPPLEHEADER
#define RIPPLE_DATA_RIPPLEHEADER
// VFALCO TODO try to reduce these dependencies
#include "../ripple_basics/ripple_basics.h"
// VFALCO TODO figure out a good place for this file, perhaps give it some
// additional hierarchy via directories.
#include "ripple.pb.h"
#include "../ripple/json/ripple_json.h"
namespace ripple
{
struct bignum_st;
typedef struct bignum_st BIGNUM;
namespace ripple {
#include "crypto/CBigNum.h"
#include "crypto/Base58.h" // VFALCO TODO Can be moved to .cpp if we clean up setAlphabet stuff
#include "crypto/Base58Data.h"
#include "crypto/RFC1751.h"
#include "protocol/BuildInfo.h"
#include "protocol/FieldNames.h"
#include "protocol/HashPrefix.h"
#include "protocol/PackedMessage.h"
#include "protocol/Protocol.h"
#include "protocol/RippleAddress.h"
#include "protocol/RippleSystem.h"