Developers.Hash.TigerTree.Sample.Code.Test

From Shareaza Wiki
Jump to navigation Jump to search

TigerTree test code

<source lang="c"> // Test function void Test() {

// Hash a file with TigerTree, and then check in TigerTest(_T("C:\\Documents\\4.txt"]]; TigerTest(_T("C:\\Documents\\4a.txt"]]; }

// Takes a path to a file on the disk // Shows how TigerTree is used to share and reassemble the file void TigerTest(LPCTSTR path) {

// Open the file and read its contents into a buffer CBuffer file, tree; FileToBuffer(path, &file);

// Hash the file with TigerTree as a computer that is sharing the file would TigerServer(9, &file, &tree);

// Now, pass the tree and file to a function that will simulate downloading it in parts TigerClient(9, &file, &tree); }

// Takes a path to a file // Opens it, and reads its contents into the given buffer void FileToBuffer(LPCTSTR path, CBuffer* buffer) {

// Open the file and get its size HANDLE file = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); DWORD size = GetFileSize(file, NULL);

// Prepare that much room in the buffer, and read the file into the buffer buffer->EnsureBuffer(size); ReadFile(file, buffer->m_pBuffer, size, &size, NULL); buffer->m_nLength += size;

// Close the file CloseHandle(file); } </source>