Paweł Bylica
8 years ago
7 changed files with 2 additions and 354 deletions
@ -1,62 +0,0 @@ |
|||
/*
|
|||
base64.cpp and base64.h |
|||
|
|||
Copyright (C) 2004-2008 René Nyffenegger |
|||
|
|||
This source code is provided 'as-is', without any express or implied |
|||
warranty. In no event will the author be held liable for any damages |
|||
arising from the use of this software. |
|||
|
|||
Permission is granted to anyone to use this software for any purpose, |
|||
including commercial applications, and to alter it and redistribute it |
|||
freely, subject to the following restrictions: |
|||
|
|||
1. The origin of this source code must not be misrepresented; you must not |
|||
claim that you wrote the original source code. If you use this source code |
|||
in a product, an acknowledgment in the product documentation would be |
|||
appreciated but is not required. |
|||
|
|||
2. Altered source versions must be plainly marked as such, and must not be |
|||
misrepresented as being the original source code. |
|||
|
|||
3. This notice may not be removed or altered from any source distribution. |
|||
|
|||
René Nyffenegger rene.nyffenegger@adp-gmbh.ch |
|||
*/ |
|||
/// Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c
|
|||
/// Originally by René Nyffenegger.
|
|||
/// DEVified by Gav Wood.
|
|||
#pragma once |
|||
|
|||
#include <string> |
|||
#include "Common.h" |
|||
#include "FixedHash.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
|
|||
std::string toBase64(bytesConstRef _in); |
|||
bytes fromBase64(std::string const& _in); |
|||
|
|||
template <size_t N> inline std::string toBase36(FixedHash<N> const& _h) |
|||
{ |
|||
static char const* c_alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|||
typename FixedHash<N>::Arith a = _h; |
|||
std::string ret; |
|||
for (; a > 0; a /= 36) |
|||
{ |
|||
unsigned r = (unsigned)(a - a / 36 * 36); // boost's % is broken
|
|||
ret = c_alphabet[r] + ret; |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
template <size_t N> inline FixedHash<N> fromBase36(std::string const& _h) |
|||
{ |
|||
typename FixedHash<N>::Arith ret = 0; |
|||
for (char c: _h) |
|||
ret = ret * 36 + (c < 'A' ? c - '0' : (c - 'A' + 10)); |
|||
return ret; |
|||
} |
|||
|
|||
} |
@ -1,22 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file RangeMask.cpp
|
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#include "RangeMask.h" |
@ -1,127 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file TrieCommon.cpp
|
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#include "TrieCommon.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
|
|||
/*
|
|||
* Hex-prefix Notation. First nibble has flags: oddness = 2^0 & termination = 2^1 |
|||
* NOTE: the "termination marker" and "leaf-node" specifier are completely equivalent. |
|||
* [0,0,1,2,3,4,5] 0x10012345 |
|||
* [0,1,2,3,4,5] 0x00012345 |
|||
* [1,2,3,4,5] 0x112345 |
|||
* [0,0,1,2,3,4] 0x00001234 |
|||
* [0,1,2,3,4] 0x101234 |
|||
* [1,2,3,4] 0x001234 |
|||
* [0,0,1,2,3,4,5,T] 0x30012345 |
|||
* [0,0,1,2,3,4,T] 0x20001234 |
|||
* [0,1,2,3,4,5,T] 0x20012345 |
|||
* [1,2,3,4,5,T] 0x312345 |
|||
* [1,2,3,4,T] 0x201234 |
|||
*/ |
|||
|
|||
std::string hexPrefixEncode(bytes const& _hexVector, bool _leaf, int _begin, int _end) |
|||
{ |
|||
unsigned begin = _begin; |
|||
unsigned end = _end < 0 ? _hexVector.size() + 1 + _end : _end; |
|||
bool odd = ((end - begin) % 2) != 0; |
|||
|
|||
std::string ret(1, ((_leaf ? 2 : 0) | (odd ? 1 : 0)) * 16); |
|||
if (odd) |
|||
{ |
|||
ret[0] |= _hexVector[begin]; |
|||
++begin; |
|||
} |
|||
for (unsigned i = begin; i < end; i += 2) |
|||
ret += _hexVector[i] * 16 + _hexVector[i + 1]; |
|||
return ret; |
|||
} |
|||
|
|||
std::string hexPrefixEncode(bytesConstRef _data, bool _leaf, int _beginNibble, int _endNibble, unsigned _offset) |
|||
{ |
|||
unsigned begin = _beginNibble + _offset; |
|||
unsigned end = (_endNibble < 0 ? ((int)(_data.size() * 2 - _offset) + 1) + _endNibble : _endNibble) + _offset; |
|||
bool odd = (end - begin) & 1; |
|||
|
|||
std::string ret(1, ((_leaf ? 2 : 0) | (odd ? 1 : 0)) * 16); |
|||
ret.reserve((end - begin) / 2 + 1); |
|||
|
|||
unsigned d = odd ? 1 : 2; |
|||
for (auto i = begin; i < end; ++i, ++d) |
|||
{ |
|||
byte n = nibble(_data, i); |
|||
if (d & 1) // odd
|
|||
ret.back() |= n; // or the nibble onto the back
|
|||
else |
|||
ret.push_back(n << 4); // push the nibble on to the back << 4
|
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
std::string hexPrefixEncode(bytesConstRef _d1, unsigned _o1, bytesConstRef _d2, unsigned _o2, bool _leaf) |
|||
{ |
|||
unsigned begin1 = _o1; |
|||
unsigned end1 = _d1.size() * 2; |
|||
unsigned begin2 = _o2; |
|||
unsigned end2 = _d2.size() * 2; |
|||
|
|||
bool odd = (end1 - begin1 + end2 - begin2) & 1; |
|||
|
|||
std::string ret(1, ((_leaf ? 2 : 0) | (odd ? 1 : 0)) * 16); |
|||
ret.reserve((end1 - begin1 + end2 - begin2) / 2 + 1); |
|||
|
|||
unsigned d = odd ? 1 : 2; |
|||
for (auto i = begin1; i < end1; ++i, ++d) |
|||
{ |
|||
byte n = nibble(_d1, i); |
|||
if (d & 1) // odd
|
|||
ret.back() |= n; // or the nibble onto the back
|
|||
else |
|||
ret.push_back(n << 4); // push the nibble on to the back << 4
|
|||
} |
|||
for (auto i = begin2; i < end2; ++i, ++d) |
|||
{ |
|||
byte n = nibble(_d2, i); |
|||
if (d & 1) // odd
|
|||
ret.back() |= n; // or the nibble onto the back
|
|||
else |
|||
ret.push_back(n << 4); // push the nibble on to the back << 4
|
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
byte uniqueInUse(RLP const& _orig, byte except) |
|||
{ |
|||
byte used = 255; |
|||
for (unsigned i = 0; i < 17; ++i) |
|||
if (i != except && !_orig[i].isEmpty()) |
|||
{ |
|||
if (used == 255) |
|||
used = (byte)i; |
|||
else |
|||
return 255; |
|||
} |
|||
return used; |
|||
} |
|||
|
|||
} |
@ -1,133 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file TrieCommon.h
|
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <libdevcore/Common.h> |
|||
#include <libdevcore/RLP.h> |
|||
|
|||
namespace dev |
|||
{ |
|||
|
|||
inline byte nibble(bytesConstRef _data, unsigned _i) |
|||
{ |
|||
return (_i & 1) ? (_data[_i / 2] & 15) : (_data[_i / 2] >> 4); |
|||
} |
|||
|
|||
/// Interprets @a _first and @a _second as vectors of nibbles and returns the length of the longest common
|
|||
/// prefix of _first[_beginFirst..._endFirst] and _second[_beginSecond..._endSecond].
|
|||
inline unsigned sharedNibbles(bytesConstRef _first, unsigned _beginFirst, unsigned _endFirst, bytesConstRef _second, unsigned _beginSecond, unsigned _endSecond) |
|||
{ |
|||
unsigned ret = 0; |
|||
while (_beginFirst < _endFirst && _beginSecond < _endSecond && nibble(_first, _beginFirst) == nibble(_second, _beginSecond)) |
|||
{ |
|||
++_beginFirst; |
|||
++_beginSecond; |
|||
++ret; |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
/**
|
|||
* Nibble-based view on a bytesConstRef. |
|||
*/ |
|||
struct NibbleSlice |
|||
{ |
|||
bytesConstRef data; |
|||
unsigned offset; |
|||
|
|||
NibbleSlice(bytesConstRef _data = bytesConstRef(), unsigned _offset = 0): data(_data), offset(_offset) {} |
|||
byte operator[](unsigned _index) const { return nibble(data, offset + _index); } |
|||
unsigned size() const { return data.size() * 2 - offset; } |
|||
bool empty() const { return !size(); } |
|||
NibbleSlice mid(unsigned _index) const { return NibbleSlice(data, offset + _index); } |
|||
void clear() { data.reset(); offset = 0; } |
|||
|
|||
/// @returns true iff _k is a prefix of this.
|
|||
bool contains(NibbleSlice _k) const { return shared(_k) == _k.size(); } |
|||
/// @returns the number of shared nibbles at the beginning of this and _k.
|
|||
unsigned shared(NibbleSlice _k) const { return sharedNibbles(data, offset, offset + size(), _k.data, _k.offset, _k.offset + _k.size()); } |
|||
/**
|
|||
* @brief Determine if we, a full key, are situated prior to a particular key-prefix. |
|||
* @param _k The prefix. |
|||
* @return true if we are strictly prior to the prefix. |
|||
*/ |
|||
bool isEarlierThan(NibbleSlice _k) const |
|||
{ |
|||
unsigned i = 0; |
|||
for (; i < _k.size() && i < size(); ++i) |
|||
if (operator[](i) < _k[i]) // Byte is lower - we're earlier..
|
|||
return true; |
|||
else if (operator[](i) > _k[i]) // Byte is higher - we're not earlier.
|
|||
return false; |
|||
if (i >= _k.size()) // Ran past the end of the prefix - we're == for the entire prefix - we're not earlier.
|
|||
return false; |
|||
return true; // Ran out before the prefix had finished - we're earlier.
|
|||
} |
|||
bool operator==(NibbleSlice _k) const { return _k.size() == size() && shared(_k) == _k.size(); } |
|||
bool operator!=(NibbleSlice _s) const { return !operator==(_s); } |
|||
}; |
|||
|
|||
inline std::ostream& operator<<(std::ostream& _out, NibbleSlice const& _m) |
|||
{ |
|||
for (unsigned i = 0; i < _m.size(); ++i) |
|||
_out << std::hex << (int)_m[i] << std::dec; |
|||
return _out; |
|||
} |
|||
|
|||
inline bool isLeaf(RLP const& _twoItem) |
|||
{ |
|||
assert(_twoItem.isList() && _twoItem.itemCount() == 2); |
|||
auto pl = _twoItem[0].payload(); |
|||
return (pl[0] & 0x20) != 0; |
|||
} |
|||
|
|||
inline NibbleSlice keyOf(bytesConstRef _hpe) |
|||
{ |
|||
if (!_hpe.size()) |
|||
return NibbleSlice(_hpe, 0); |
|||
if (_hpe[0] & 0x10) |
|||
return NibbleSlice(_hpe, 1); |
|||
else |
|||
return NibbleSlice(_hpe, 2); |
|||
} |
|||
|
|||
inline NibbleSlice keyOf(RLP const& _twoItem) |
|||
{ |
|||
return keyOf(_twoItem[0].payload()); |
|||
} |
|||
|
|||
byte uniqueInUse(RLP const& _orig, byte except); |
|||
std::string hexPrefixEncode(bytes const& _hexVector, bool _leaf = false, int _begin = 0, int _end = -1); |
|||
std::string hexPrefixEncode(bytesConstRef _data, bool _leaf, int _beginNibble, int _endNibble, unsigned _offset); |
|||
std::string hexPrefixEncode(bytesConstRef _d1, unsigned _o1, bytesConstRef _d2, unsigned _o2, bool _leaf); |
|||
|
|||
inline std::string hexPrefixEncode(NibbleSlice _s, bool _leaf, int _begin = 0, int _end = -1) |
|||
{ |
|||
return hexPrefixEncode(_s.data, _leaf, _begin, _end, _s.offset); |
|||
} |
|||
|
|||
inline std::string hexPrefixEncode(NibbleSlice _s1, NibbleSlice _s2, bool _leaf) |
|||
{ |
|||
return hexPrefixEncode(_s1.data, _s1.offset, _s2.data, _s2.offset, _leaf); |
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue