27static std::map<std::pair<size_t, std::string>,
TTCacheEntry> ttCache;
29[[nodiscard]]
static constexpr size_t calcNumNodes(
size_t dataSize)
32 return (numBlocks == 0) ? 1 : 2 * numBlocks - 1;
35[[nodiscard]]
static TTCacheEntry& getCacheEntry(
36 TTData& data,
size_t dataSize,
const std::string& name)
38 auto& result = ttCache[std::pair(dataSize, name)];
39 if (!data.isCacheStillValid(result.time)) {
40 size_t numNodes = calcNumNodes(dataSize);
41 result.nodes.resize(numNodes);
42 for (
auto& i : result.nodes) i.valid = false;
43 result.numNodesValid = 0;
51 , entry(getCacheEntry(data, dataSize, name))
57 return calcHash(getTop(), progressCallback);
64 assert((offset + len) <= dataSize);
67 if (entry.
nodes[getTop().n].valid) {
68 entry.
nodes[getTop().n].valid =
false;
73 assert(first <= last);
75 auto node = getLeaf(first);
76 while (entry.
nodes[node.n].valid) {
77 entry.
nodes[node.n].valid =
false;
79 node = getParent(node);
81 }
while (++first <= last);
87 auto& nod = entry.
nodes[n];
91 auto left = getLeftChild (node);
92 auto right = getRightChild(node);
93 const auto& h1 =
calcHash(left, progressCallback);
94 const auto& h2 =
calcHash(right, progressCallback);
99 size_t l = dataSize - b;
106 auto* d = data.getData(b, l);
108 tiger(std::span{d - 1, l + 1}, nod.hash);
113 if (progressCallback) {
143TigerTree::Node TigerTree::getTop()
const
149TigerTree::Node TigerTree::getLeaf(
size_t block)
const
151 assert((2 * block) < entry.
nodes.size());
152 return {2 * block, 1};
155TigerTree::Node TigerTree::getParent(Node node)
const
157 assert(node.n < entry.
nodes.size());
159 node.n = (node.n & ~(2 * node.l)) + node.l;
161 }
while (node.n >= entry.
nodes.size());
165TigerTree::Node TigerTree::getLeftChild(Node node)
const
167 assert(node.n < entry.
nodes.size());
174TigerTree::Node TigerTree::getRightChild(Node node)
const
176 assert(node.n < entry.
nodes.size());
180 auto r = node.n + node.l;
181 if (r < entry.
nodes.size())
return {r, node.l};
Assign new value to some variable and restore the original value when this object goes out of scope.
This class manages the lifetime of a block of memory.
The TigerTree class will query the to-be-hashed data via this abstract interface.
const TigerHash & calcHash(const std::function< void(size_t, size_t)> &progressCallback)
Calculate the hash value.
TigerTree(TTData &data, size_t dataSize, const std::string &name)
Create TigerTree calculator for the given (abstract) data block of given size.
void notifyChange(size_t offset, size_t len, time_t time)
Inform this calculator about changes in the input data.
static constexpr size_t BLOCK_SIZE
constexpr auto floodRight(std::unsigned_integral auto x) noexcept
Returns the smallest number of the form 2^n-1 that is greater or equal to the given number.
This file implemented 3 utility functions:
void tiger(std::span< const uint8_t > input, TigerHash &result)
Generic function to calculate a tiger-hash.
void tiger_int(const TigerHash &h0, const TigerHash &h1, TigerHash &result)
Use for tiger-tree internal node hash calculations.
void tiger_leaf(std::span< uint8_t > data, TigerHash &result)
Use for tiger-tree leaf node hash calculations.
This struct represents the result of a tiger-hash.