Developers.Hash.TigerTree.Sample.Code.Client: Difference between revisions
(Importing page from Tikiwiki) |
m (1 revision) |
Latest revision as of 20:09, 20 June 2009
TigerTree client code
<source lang="c"> // Takes a TigerTree root hash, the whole tree, and the whole file // Shows how a computer downloading the file can use the hash information to verify parts of the file void TigerClient(DWORD maxheight, CBuffer* file, CBuffer* tree) {
// Make a new TigerTree object and give it the whole tree CTigerTree tiger; // This new TigerTree object doesn't know about the file yet BOOL result; // FromBytes will return a result of true if the tiger tree we are giving it is valid result = tiger.FromBytes( // FromBytes takes a tiger tree from memory and loads it into the object tree->m_pBuffer, // Pointer to the tree tree->m_nLength, // Size of the tree, the number of bytes FromBytes should read there maxheight, // Maximum tree height, you can use this to shorten trees that are too tall file->m_nLength); // FromBytes also needs to know how big the file is if (!result) return; // The tree is not valid
// Ask the object about the details of the tree we just loaded DWORD blocksize, blocks, height; blocksize = tiger.GetBlockLength(); blocks = tiger.GetBlockCount(); height = tiger.GetHeight();
// Start block and blocknumber at the beginning of the file byte* block = file->m_pBuffer; // Point block at the start of the file DWORD blocknumber = 0; // This is the first block, a distance 0 blocks into the file DWORD blockfilled; // How much of this block is filled, useful for the last partially filled block
// Loop while block still points to bytes within the file while (block < file->m_pBuffer + file->m_nLength) {
// Calculate how much of this block is filled blockfilled = file->m_pBuffer + file->m_nLength - block; // The number of bytes in the file beyond the block pointer if (blockfilled > blocksize) blockfilled = blocksize; // If this is more than 1 block, make it just 1 block
// Test the block tiger.BeginBlockTest(); // Tell the TigerTree object we want to test a block of file data tiger.AddToTest(block, blockfilled); // Give AddToTest a pointer to the memory, and the number of bytes there result = tiger.FinishBlockTest(blocknumber); // Tell it what block number that was, it returns true if the block is valid
// Move to the next block block += blocksize; // Move the block pointer forward to the next block in the file blocknumber++; // Record this is one block distance further from the start } } </source>