Fabcoin Core  0.16.2
P2P Digital Currency
TrieCommon.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #include "TrieCommon.h"
23 
24 namespace dev
25 {
26 
27 /*
28  * Hex-prefix Notation. First nibble has flags: oddness = 2^0 & termination = 2^1
29  * NOTE: the "termination marker" and "leaf-node" specifier are completely equivalent.
30  * [0,0,1,2,3,4,5] 0x10012345
31  * [0,1,2,3,4,5] 0x00012345
32  * [1,2,3,4,5] 0x112345
33  * [0,0,1,2,3,4] 0x00001234
34  * [0,1,2,3,4] 0x101234
35  * [1,2,3,4] 0x001234
36  * [0,0,1,2,3,4,5,T] 0x30012345
37  * [0,0,1,2,3,4,T] 0x20001234
38  * [0,1,2,3,4,5,T] 0x20012345
39  * [1,2,3,4,5,T] 0x312345
40  * [1,2,3,4,T] 0x201234
41  */
42 
43 std::string hexPrefixEncode(bytes const& _hexVector, bool _leaf, int _begin, int _end)
44 {
45  unsigned begin = _begin;
46  unsigned end = _end < 0 ? _hexVector.size() + 1 + _end : _end;
47  bool odd = ((end - begin) % 2) != 0;
48 
49  std::string ret(1, ((_leaf ? 2 : 0) | (odd ? 1 : 0)) * 16);
50  if (odd)
51  {
52  ret[0] |= _hexVector[begin];
53  ++begin;
54  }
55  for (unsigned i = begin; i < end; i += 2)
56  ret += _hexVector[i] * 16 + _hexVector[i + 1];
57  return ret;
58 }
59 
60 std::string hexPrefixEncode(bytesConstRef _data, bool _leaf, int _beginNibble, int _endNibble, unsigned _offset)
61 {
62  unsigned begin = _beginNibble + _offset;
63  unsigned end = (_endNibble < 0 ? ((int)(_data.size() * 2 - _offset) + 1) + _endNibble : _endNibble) + _offset;
64  bool odd = (end - begin) & 1;
65 
66  std::string ret(1, ((_leaf ? 2 : 0) | (odd ? 1 : 0)) * 16);
67  ret.reserve((end - begin) / 2 + 1);
68 
69  unsigned d = odd ? 1 : 2;
70  for (auto i = begin; i < end; ++i, ++d)
71  {
72  byte n = nibble(_data, i);
73  if (d & 1) // odd
74  ret.back() |= n; // or the nibble onto the back
75  else
76  ret.push_back(n << 4); // push the nibble on to the back << 4
77  }
78  return ret;
79 }
80 
81 std::string hexPrefixEncode(bytesConstRef _d1, unsigned _o1, bytesConstRef _d2, unsigned _o2, bool _leaf)
82 {
83  unsigned begin1 = _o1;
84  unsigned end1 = _d1.size() * 2;
85  unsigned begin2 = _o2;
86  unsigned end2 = _d2.size() * 2;
87 
88  bool odd = (end1 - begin1 + end2 - begin2) & 1;
89 
90  std::string ret(1, ((_leaf ? 2 : 0) | (odd ? 1 : 0)) * 16);
91  ret.reserve((end1 - begin1 + end2 - begin2) / 2 + 1);
92 
93  unsigned d = odd ? 1 : 2;
94  for (auto i = begin1; i < end1; ++i, ++d)
95  {
96  byte n = nibble(_d1, i);
97  if (d & 1) // odd
98  ret.back() |= n; // or the nibble onto the back
99  else
100  ret.push_back(n << 4); // push the nibble on to the back << 4
101  }
102  for (auto i = begin2; i < end2; ++i, ++d)
103  {
104  byte n = nibble(_d2, i);
105  if (d & 1) // odd
106  ret.back() |= n; // or the nibble onto the back
107  else
108  ret.push_back(n << 4); // push the nibble on to the back << 4
109  }
110  return ret;
111 }
112 
113 byte uniqueInUse(RLP const& _orig, byte except)
114 {
115  byte used = 255;
116  for (unsigned i = 0; i < 17; ++i)
117  if (i != except && !_orig[i].isEmpty())
118  {
119  if (used == 255)
120  used = (byte)i;
121  else
122  return 255;
123  }
124  return used;
125 }
126 
127 }
byte nibble(bytesConstRef _data, unsigned _i)
Definition: TrieCommon.h:30
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
uint8_t byte
Definition: Common.h:57
std::string hexPrefixEncode(bytes const &_hexVector, bool _leaf, int _begin, int _end)
Definition: TrieCommon.cpp:43
std::vector< byte > bytes
Definition: Common.h:75
size_t size() const
Definition: vector_ref.h:55
byte uniqueInUse(RLP const &_orig, byte except)
Definition: TrieCommon.cpp:113
uint8_t byte
Definition: Common.h:10
#define d(i)
Definition: sha.cpp:732
Class for interpreting Recursive Linear-Prefix Data.
Definition: RLP.h:64