| 1 |
//--------------------------------------------------------------------------- |
| 2 |
// ChunkedFile.cpp |
| 3 |
//--------------------------------------------------------------------------- |
| 4 |
// -=* VPatch *=- |
| 5 |
//--------------------------------------------------------------------------- |
| 6 |
// Copyright (C) 2001-2005 Koen van de Sande / Van de Sande Productions |
| 7 |
//--------------------------------------------------------------------------- |
| 8 |
// Website: http://www.tibed.net/vpatch |
| 9 |
// |
| 10 |
// This software is provided 'as-is', without any express or implied |
| 11 |
// warranty. In no event will the authors be held liable for any damages |
| 12 |
// arising from the use of this software. |
| 13 |
// |
| 14 |
// Permission is granted to anyone to use this software for any purpose, |
| 15 |
// including commercial applications, and to alter it and redistribute it |
| 16 |
// freely, subject to the following restrictions: |
| 17 |
// |
| 18 |
// 1. The origin of this software must not be misrepresented; you must not |
| 19 |
// claim that you wrote the original software. If you use this software |
| 20 |
// in a product, an acknowledgment in the product documentation would be |
| 21 |
// appreciated but is not required. |
| 22 |
// 2. Altered source versions must be plainly marked as such, and must not be |
| 23 |
// misrepresented as being the original software. |
| 24 |
// 3. This notice may not be removed or altered from any source distribution. |
| 25 |
// |
| 26 |
// Unicode support by Jim Park -- 08/29/2007 |
| 27 |
|
| 28 |
|
| 29 |
#include "ChunkedFile.h" |
| 30 |
#include "tchar.h" |
| 31 |
|
| 32 |
using namespace std; |
| 33 |
|
| 34 |
ChunkedFile::ChunkedFile(bistream& f, TFileOffset fSize, TFileOffset chunkSize) : |
| 35 |
chunks(NULL) { |
| 36 |
|
| 37 |
chunkCount = fSize / chunkSize; |
| 38 |
tout << _T("[ChunkedFile] Filesize of ") << static_cast<unsigned int>(fSize) << _T(" gives ") << static_cast<unsigned int>(chunkCount) << _T(" chunks.\n"); |
| 39 |
|
| 40 |
tout << _T("[ChunkedFile] Memory to be used by those chunks: ") << sizeof(FileChunk) * chunkCount << _T(" bytes..."); |
| 41 |
if(chunkCount == 0) { |
| 42 |
chunks = NULL; |
| 43 |
return; |
| 44 |
} |
| 45 |
chunks = new FileChunk[chunkCount]; |
| 46 |
tout << _T(" allocated.\n"); |
| 47 |
|
| 48 |
unsigned char* data = new unsigned char[chunkSize]; |
| 49 |
for(TFileOffset i = 0; i < chunkCount; i++) { |
| 50 |
f.read(reinterpret_cast<char*>(data),chunkSize); |
| 51 |
chunks[i].offset = i * chunkSize; |
| 52 |
calculateChecksum(data,chunkSize,chunks[i].checksum); |
| 53 |
} |
| 54 |
delete[] data; |
| 55 |
|
| 56 |
tout << _T("[ChunkedFile] Sorting chunks... "); |
| 57 |
std::sort(chunks,chunks + chunkCount); |
| 58 |
tout << _T("done.\n"); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
bool ChunkedFile::search(TChunkChecksum key, TFileOffset* start) { |
| 63 |
// function: |
| 64 |
// Searches sortedArray[first]..sortedArray[last] for key. |
| 65 |
// returns: index of the matching element if it finds key, |
| 66 |
// otherwise -(index where it could be inserted)-1. |
| 67 |
// parameters: |
| 68 |
// sortedArray in array of sorted (ascending) values. |
| 69 |
// first, last in lower and upper subscript bounds |
| 70 |
// key in value to search for. |
| 71 |
// returns: |
| 72 |
// index of key, or -insertion_position -1 if key is not |
| 73 |
// in the array. This value can easily be |
| 74 |
// transformed into the position to insert it. |
| 75 |
if(chunkCount == 0) return false; |
| 76 |
int first = 0; |
| 77 |
int last = chunkCount - 1; |
| 78 |
while (first <= last) { |
| 79 |
int mid = (first + last) / 2; // compute mid point. |
| 80 |
if(key == chunks[mid].checksum) { |
| 81 |
while(true) { |
| 82 |
if(mid == 0) break; |
| 83 |
mid--; |
| 84 |
if(!(key == chunks[mid].checksum)) { |
| 85 |
mid++; |
| 86 |
break; |
| 87 |
} |
| 88 |
} |
| 89 |
*start = mid; |
| 90 |
return true; // found it. return position |
| 91 |
} |
| 92 |
if (key < chunks[mid].checksum) |
| 93 |
last = mid - 1; // repeat search in bottom half. |
| 94 |
else |
| 95 |
first = mid + 1; // repeat search in top half. |
| 96 |
} |
| 97 |
return false; // failed to find key |
| 98 |
} |