dcpp/MerkleTree.h

filter: Statementstmt 85.5% 59 / 69 Branchbran 48.6% 37 / 76 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 66.2% 96 / 145 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« dcpp/Encoder.cpp ↑ Summary TigerHash.xs »
1 /*
2 * Copyright (C) 2001-2012 Jacek Sieka, arnetheduck on gmail point com
3 *
4 * This program 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 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program 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 this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18  
19 #pragma once
20  
21 #include "debug.h"
22 #include "typedefs.h"
23 #include "TigerHash.h"
24 #include "Encoder.h"
25 #include "HashValue.h"
26  
27 namespace dcpp {
28  
29 /**
30 * A class that represents a Merkle Tree hash. Storing
31 * only the leaves of the tree, it is rather memory efficient,
32 * but can still take a significant amount of memory during / after
33 * hash generation.
34 * The root hash produced can be used like any
35 * other hash to verify the integrity of a whole file, while
36 * the leaves provide checking of smaller parts of the file.
37 */
38 template<class Hasher, size_t baseBlockSize = 1024>
39 class MerkleTree {
40 public:
41 static const size_t BITS = Hasher::BITS;
42 static const size_t BYTES = Hasher::BYTES;
43 static const size_t BASE_BLOCK_SIZE = baseBlockSize;
44  
45 typedef HashValue<Hasher> MerkleValue;
46 typedef vector<MerkleValue> MerkleList;
47 typedef typename MerkleList::iterator MerkleIter;
48  
49 MerkleTree() : fileSize(0), blockSize(baseBlockSize) { }
50 10 MerkleTree(int64_t aBlockSize) : fileSize(0), blockSize(aBlockSize) { }
51  
52 /**
53 * Loads a set of leaf hashes, calculating the root
54 * @param data Pointer to (aFileSize + aBlockSize - 1) / aBlockSize) hash values,
55 * stored consecutively left to right
56 */
57 MerkleTree(int64_t aFileSize, int64_t aBlockSize, uint8_t* aData) :
58 fileSize(aFileSize), blockSize(aBlockSize)
59 {
60 size_t n = calcBlocks(aFileSize, aBlockSize);
61 for(size_t i = 0; i < n; i++)
62 leaves.push_back(MerkleValue(aData + i * Hasher::BYTES));
63  
64 calcRoot();
65 }
66  
67 /** Initialise a single root tree */
68 MerkleTree(int64_t aFileSize, int64_t aBlockSize, const MerkleValue& aRoot) : root(aRoot), fileSize(aFileSize), blockSize(aBlockSize) {
69 leaves.push_back(root);
70 }
71  
72 10 ~MerkleTree() { }
73  
74 10 static int64_t calcBlockSize(int64_t aFileSize, int maxLevels) {
75 10 int64_t tmp = baseBlockSize;
76 10 int64_t maxHashes = ((int64_t)1) << (maxLevels - 1);
77 10 ▶ while((maxHashes * tmp) < aFileSize)
Branch: 1 branch
BranchTrueFalseTotal
while((maxHashes * tmp) < aFileSize) 0 10 10
78 0 tmp *= 2;
79 10 return tmp;
80 }
81  
82 static size_t calcBlocks(int64_t aFileSize, int64_t aBlockSize) {
83 return max((size_t)((aFileSize + aBlockSize - 1) / aBlockSize), (size_t)1);
84 }
85  
86 /**
87 * Update the merkle tree.
88 * @param len Length of data, must be a multiple of baseBlockSize, unless it's
89 * the last block.
90 */
91 19 void update(const void* data, size_t len) {
92 19 uint8_t* buf = (uint8_t*)data;
93 19 uint8_t zero = 0;
94 19 size_t i = 0;
95  
96 // Skip empty data sets if we already added at least one of them...
97 19 ▶ if(len == 0 && !(leaves.empty() && blocks.empty()))
Branch: 4 branches
BranchTrueFalseTotal
if(len == 0 && !(leaves.empty() && blocks.empty())) 10 9 19
if(len == 0 && !(leaves.empty() && blocks.empty())) 10 0 10
if(len == 0 && !(leaves.empty() && blocks.empty())) 9 1 10
if(len == 0 && !(leaves.empty() && blocks.empty())) 9 10 19
98 9 return;
99  
100 do {
101 46 size_t n = min(baseBlockSize, len-i);
102 46 Hasher h;
103 46 ▶ h.update(&zero, 1);
Branch: 1 branch
BranchTrueFalseTotal
h.update(&zero, 1); 46 0 46
104 46 ▶ h.update(buf + i, n);
Branch: 1 branch
BranchTrueFalseTotal
h.update(buf + i, n); 46 0 46
105 46 ▶ if((int64_t)baseBlockSize < blockSize) {
Branch: 1 branch
BranchTrueFalseTotal
if((int64_t)baseBlockSize < blockSize) { 46 0 46
106 46 ▶ blocks.push_back(make_pair(MerkleValue(h.finalize()), baseBlockSize));
Branch: 4 branches
BranchTrueFalseTotal
blocks.push_back(make_pair(MerkleValue(h.finalize()), baseBlockSize)); 46 0 46
blocks.push_back(make_pair(MerkleValue(h.finalize()), baseBlockSize)); 46 0 46
blocks.push_back(make_pair(MerkleValue(h.finalize()), baseBlockSize)); 46 0 46
blocks.push_back(make_pair(MerkleValue(h.finalize()), baseBlockSize)); 46 0 46
107 46 ▶ reduceBlocks();
Branch: 1 branch
BranchTrueFalseTotal
reduceBlocks(); 46 0 46
108 } else {
109 0 ▶ leaves.push_back(MerkleValue(h.finalize()));
Branch: 2 branches
BranchTrueFalseTotal
leaves.push_back(MerkleValue(h.finalize())); 0 0 0
leaves.push_back(MerkleValue(h.finalize())); 0 0 0
110 }
111 46 i += n;
112 46 ▶ } while(i < len);
Branch: 1 branch
BranchTrueFalseTotal
} while(i < len); 36 10 46
113 10 fileSize += len;
114 }
115  
116 10 uint8_t* finalize() {
117 // No updates yet, make sure we have at least one leaf for 0-length files...
118 10 ▶ if(leaves.empty() && blocks.empty()) {
Branch: 3 branches
BranchTrueFalseTotal
if(leaves.empty() && blocks.empty()) { 10 0 10
if(leaves.empty() && blocks.empty()) { 0 10 10
if(leaves.empty() && blocks.empty()) { 0 10 10
119 0 update(0, 0);
120 }
121 14 ▶ while(blocks.size() > 1) {
Branch: 1 branch
BranchTrueFalseTotal
while(blocks.size() > 1) { 4 10 14
122 4 MerkleBlock& a = blocks[blocks.size()-2];
123 4 MerkleBlock& b = blocks[blocks.size()-1];
124 4 ▶ a.first = combine(a.first, b.first);
Branch: 1 branch
BranchTrueFalseTotal
a.first = combine(a.first, b.first); 4 0 4
125 4 blocks.pop_back();
126 }
127 dcassert(blocks.size() == 0 || blocks.size() == 1);
128 10 ▶ if(!blocks.empty()) {
Branch: 1 branch
BranchTrueFalseTotal
if(!blocks.empty()) { 10 0 10
129 10 leaves.push_back(blocks[0].first);
130 }
131 10 calcRoot();
132 10 return root.data;
133 }
134  
135 MerkleValue& getRoot() { return root; }
136 const MerkleValue& getRoot() const { return root; }
137 MerkleList& getLeaves() { return leaves; }
138 const MerkleList& getLeaves() const { return leaves; }
139  
140 int64_t getBlockSize() const { return blockSize; }
141 void setBlockSize(int64_t aSize) { blockSize = aSize; }
142  
143 int64_t getFileSize() const { return fileSize; }
144 void setFileSize(int64_t aSize) { fileSize = aSize; }
145  
146 bool verifyRoot(const uint8_t* aRoot) {
147 return memcmp(aRoot, getRoot().data(), BYTES) == 0;
148 }
149  
150 10 void calcRoot() {
151 10 ▶ root = getHash(0, fileSize);
Branch: 1 branch
BranchTrueFalseTotal
root = getHash(0, fileSize); 10 0 10
152 10 }
153  
154 ByteVector getLeafData() {
155 ByteVector buf(getLeaves().size() * BYTES);
156 uint8_t* p = &buf[0];
157 for(size_t i = 0; i < getLeaves().size(); ++i) {
158 memcpy(p + i * BYTES, &getLeaves()[i], BYTES);
159 }
160 return buf;
161 }
162  
163 private:
164 typedef pair<MerkleValue, int64_t> MerkleBlock;
165 typedef vector<MerkleBlock> MBList;
166  
167 MBList blocks;
168  
169 MerkleList leaves;
170  
171 MerkleValue root;
172 /** Total size of hashed data */
173 int64_t fileSize;
174 /** Final block size */
175 int64_t blockSize;
176  
177 10 MerkleValue getHash(int64_t start, int64_t length) {
178 dcassert((start % blockSize) == 0);
179 10 ▶ if(length <= blockSize) {
Branch: 1 branch
BranchTrueFalseTotal
if(length <= blockSize) { 10 0 10
180 dcassert((start / blockSize) < (int64_t)leaves.size());
181 10 return leaves[(uint32_t)(start / blockSize)];
182 } else {
183 0 int64_t l = blockSize;
184 0 ▶ while(l * 2 < length)
Branch: 1 branch
BranchTrueFalseTotal
while(l * 2 < length) 0 0 0
185 0 l *= 2;
186 0 ▶ return combine(getHash(start, l), getHash(start+l, length - l));
Branch: 3 branches
BranchTrueFalseTotal
return combine(getHash(start, l), getHash(start+l, length - l)); 0 0 0
return combine(getHash(start, l), getHash(start+l, length - l)); 0 0 0
return combine(getHash(start, l), getHash(start+l, length - l)); 0 0 0
187 }
188 }
189  
190 36 MerkleValue combine(const MerkleValue& a, const MerkleValue& b) {
191 36 uint8_t one = 1;
192 36 Hasher h;
193 36 ▶ h.update(&one, 1);
Branch: 1 branch
BranchTrueFalseTotal
h.update(&one, 1); 36 0 36
194 36 ▶ h.update(a.data, MerkleValue::BYTES);
Branch: 1 branch
BranchTrueFalseTotal
h.update(a.data, MerkleValue::BYTES); 36 0 36
195 36 ▶ h.update(b.data, MerkleValue::BYTES);
Branch: 1 branch
BranchTrueFalseTotal
h.update(b.data, MerkleValue::BYTES); 36 0 36
196 72 ▶ return MerkleValue(h.finalize());
Branch: 1 branch
BranchTrueFalseTotal
return MerkleValue(h.finalize()); 36 0 36
197 36 }
198  
199 46 void reduceBlocks() {
200 78 ▶ while(blocks.size() > 1) {
Branch: 1 branch
BranchTrueFalseTotal
while(blocks.size() > 1) { 56 22 78
201 56 MerkleBlock& a = blocks[blocks.size()-2];
202 56 MerkleBlock& b = blocks[blocks.size()-1];
203 56 ▶ if(a.second == b.second) {
Branch: 1 branch
BranchTrueFalseTotal
if(a.second == b.second) { 32 24 56
204 32 ▶ if(a.second*2 == blockSize) {
Branch: 1 branch
BranchTrueFalseTotal
if(a.second*2 == blockSize) { 0 32 32
205 0 ▶ leaves.push_back(combine(a.first, b.first));
Branch: 2 branches
BranchTrueFalseTotal
leaves.push_back(combine(a.first, b.first)); 0 0 0
leaves.push_back(combine(a.first, b.first)); 0 0 0
206 0 blocks.pop_back();
207 0 blocks.pop_back();
208 } else {
209 32 a.second *= 2;
210 32 ▶ a.first = combine(a.first, b.first);
Branch: 1 branch
BranchTrueFalseTotal
a.first = combine(a.first, b.first); 32 0 32
211 32 blocks.pop_back();
212 }
213 } else {
214 24 break;
215 }
216 }
217 46 }
218 };
219  
220 typedef MerkleTree<TigerHash> TigerTree;
221 typedef TigerTree::MerkleValue TTHValue;
222  
223 template<int64_t aBlockSize>
224 struct TTFilter {
225 TTFilter() : tt(aBlockSize) { }
226 void operator()(const void* data, size_t len) { tt.update(data, len); }
227 TigerTree& getTree() { return tt; }
228 private:
229 TigerTree tt;
230 };
231  
232 } // namespace dcpp
« dcpp/Encoder.cpp ↑ Summary TigerHash.xs »