| 1 |
//--------------------------------------------------------------------------- |
| 2 |
// PatchGenerator.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 |
#include "tchar.h" |
| 29 |
#include "PatchGenerator.h" |
| 30 |
#include <algorithm> |
| 31 |
|
| 32 |
#define TARGET_BUFFER_SIZE 65536 |
| 33 |
#define TARGET_LOOKAHEAD_SIZE 4096 |
| 34 |
#define MAX_BLOCK_SIZE 16384 |
| 35 |
#define DEFAULT_MAX_MATCHES 500 |
| 36 |
|
| 37 |
PatchGenerator::PatchGenerator(bistream& source, TFileOffset sourceSize, bistream& target, TFileOffset targetSize, bostream& patch) : |
| 38 |
source(source), sourceSize(sourceSize), target(target), targetSize(targetSize), patch(patch), |
| 39 |
targetCData(NULL), targetCDataBaseOffset(0), targetCDataSize(0), blockSize(64), maxMatches(DEFAULT_MAX_MATCHES), beVerbose(false) { |
| 40 |
targetCData = new unsigned char[TARGET_BUFFER_SIZE]; |
| 41 |
} |
| 42 |
|
| 43 |
PatchGenerator::~PatchGenerator() { |
| 44 |
if(targetCData != NULL) delete[] targetCData; |
| 45 |
} |
| 46 |
|
| 47 |
void PatchGenerator::execute(vector<SameBlock*>& sameBlocks) { |
| 48 |
ChunkedFile* sourceTree = new ChunkedFile(source,sourceSize,blockSize); |
| 49 |
|
| 50 |
// sameBlocks: this vector will store blocks that have been found to be the same |
| 51 |
// between files |
| 52 |
|
| 53 |
// the vector needs an 'empty' first block so checking for overlap with the |
| 54 |
// 'previous' block never fails |
| 55 |
SameBlock* firstBlock = new SameBlock; |
| 56 |
firstBlock->sourceOffset = 0; |
| 57 |
firstBlock->targetOffset = 0; |
| 58 |
firstBlock->size = 0; |
| 59 |
sameBlocks.push_back(firstBlock); |
| 60 |
|
| 61 |
targetCDataBaseOffset = 0; |
| 62 |
targetCDataSize = 0; |
| 63 |
bool firstRun = true; |
| 64 |
|
| 65 |
// currentOffset is in the target file |
| 66 |
for(TFileOffset currentOffset = 0; currentOffset < targetSize; ) { |
| 67 |
bool reloadTargetCData = true; |
| 68 |
if((currentOffset >= targetCDataBaseOffset) && |
| 69 |
(currentOffset + TARGET_LOOKAHEAD_SIZE < targetCDataBaseOffset + TARGET_BUFFER_SIZE)) { |
| 70 |
|
| 71 |
if(firstRun) { |
| 72 |
firstRun = false; |
| 73 |
} else { |
| 74 |
reloadTargetCData = false; |
| 75 |
} |
| 76 |
} |
| 77 |
if(reloadTargetCData) { |
| 78 |
// at least support looking back blockSize, if possible (findBlock relies on this!) |
| 79 |
targetCDataBaseOffset = currentOffset - blockSize; |
| 80 |
|
| 81 |
// handle start of file correctly |
| 82 |
if(currentOffset < blockSize) targetCDataBaseOffset = 0; |
| 83 |
|
| 84 |
targetCDataSize = TARGET_BUFFER_SIZE; |
| 85 |
// check if this does not extend beyond EOF |
| 86 |
if(targetCDataBaseOffset + targetCDataSize > targetSize) { |
| 87 |
targetCDataSize = targetSize - targetCDataBaseOffset; |
| 88 |
} |
| 89 |
|
| 90 |
// we need to update the memory cache of target |
| 91 |
if (beVerbose) { |
| 92 |
tout << _T("[CacheReload] File position = ") << static_cast<unsigned int>(targetCDataBaseOffset) << _T("\n"); |
| 93 |
} |
| 94 |
|
| 95 |
target.seekg(targetCDataBaseOffset,ios::beg); |
| 96 |
target.read(reinterpret_cast<char*>(targetCData),targetCDataSize); |
| 97 |
} |
| 98 |
//tout << currentOffset << _T(" "); |
| 99 |
|
| 100 |
SameBlock* currentSameBlock = findBlock(sourceTree,currentOffset); |
| 101 |
|
| 102 |
if(currentSameBlock) { |
| 103 |
// we have a match |
| 104 |
SameBlock* previousBlock = sameBlocks.back(); |
| 105 |
if(previousBlock->targetOffset + previousBlock->size > currentSameBlock->targetOffset) { |
| 106 |
// there is overlap, resolve it |
| 107 |
TFileOffset difference = previousBlock->targetOffset + previousBlock->size - currentSameBlock->targetOffset; |
| 108 |
currentSameBlock->sourceOffset += difference; |
| 109 |
currentSameBlock->targetOffset += difference; |
| 110 |
currentSameBlock->size -= difference; |
| 111 |
} |
| 112 |
sameBlocks.push_back(currentSameBlock); |
| 113 |
|
| 114 |
// debug info |
| 115 |
if(beVerbose) { |
| 116 |
tout << _T("Block found: ") << static_cast<unsigned int>(currentSameBlock->targetOffset) |
| 117 |
<< _T(" ") << static_cast<unsigned int>(currentSameBlock->size) |
| 118 |
<< _T(" (source offset=") << static_cast<unsigned int>(currentSameBlock->sourceOffset) << _T(")\n"); |
| 119 |
} |
| 120 |
|
| 121 |
currentOffset = currentSameBlock->targetOffset + currentSameBlock->size; |
| 122 |
} else { |
| 123 |
// no match, advance one byte |
| 124 |
currentOffset++; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
// add a block at the end, again to prevent bounds checking hassle |
| 129 |
SameBlock* lastBlock = new SameBlock; |
| 130 |
lastBlock->sourceOffset = 0; |
| 131 |
lastBlock->targetOffset = targetSize; |
| 132 |
lastBlock->size = 0; |
| 133 |
sameBlocks.push_back(lastBlock); |
| 134 |
delete sourceTree; |
| 135 |
} |
| 136 |
|
| 137 |
SameBlock* PatchGenerator::findBlock(ChunkedFile* sourceTree, |
| 138 |
TFileOffset targetFileStartOffset) { |
| 139 |
if(targetSize - targetFileStartOffset < blockSize) return NULL; |
| 140 |
|
| 141 |
TFileOffset preDataSize = targetFileStartOffset - targetCDataBaseOffset; |
| 142 |
//unsigned char* p = &(targetCData[preDataSize]); |
| 143 |
// SameBlock* currentSameBlock = findBlock(sourceTree,p,preDataSize, |
| 144 |
// targetCDataSize - preDataSize, |
| 145 |
// currentOffset); |
| 146 |
//unsigned char* targetData, // target data contains a memory part of target stream |
| 147 |
//TFileOffset targetDataPreSize, // this specifies how many bytes we can access in front (required to be at least blockSize) |
| 148 |
//TFileOffset targetDataSize, // this specifies how many bytes we can access in the pointer (must be at least blockSize) |
| 149 |
|
| 150 |
// read the current data part into memory |
| 151 |
TChunkChecksum checksum; |
| 152 |
sourceTree->calculateChecksum(&(targetCData[preDataSize]),blockSize,checksum); |
| 153 |
|
| 154 |
TFileOffset foundIndex; // location into sourceTree chunks array of found chunk |
| 155 |
if(sourceTree->search(checksum,&foundIndex)) { |
| 156 |
// we found something |
| 157 |
SameBlock* bestMatch = new SameBlock; |
| 158 |
bestMatch->sourceOffset = sourceTree->chunks[foundIndex].offset; |
| 159 |
bestMatch->targetOffset = targetFileStartOffset; |
| 160 |
bestMatch->size = 0; // default to 0, because they can all be mismatches as well |
| 161 |
|
| 162 |
// increase match size if possible, also check if it is a match at all |
| 163 |
int matchCount = 0; |
| 164 |
while((sourceTree->chunks[foundIndex].checksum == checksum) && ((maxMatches == 0) || (matchCount < maxMatches))) { |
| 165 |
// check if this one is better than the current match |
| 166 |
SameBlock match; |
| 167 |
match.sourceOffset = sourceTree->chunks[foundIndex].offset; |
| 168 |
match.targetOffset = targetFileStartOffset; |
| 169 |
match.size = 0; // default to 0, could be a mismatch with the same key |
| 170 |
improveSameBlockMatch(match,bestMatch->size); |
| 171 |
if(match.size > bestMatch->size) { |
| 172 |
*bestMatch = match; |
| 173 |
} |
| 174 |
foundIndex++; |
| 175 |
matchCount++; |
| 176 |
} |
| 177 |
if(beVerbose) { |
| 178 |
if(maxMatches != 0) { |
| 179 |
if(matchCount == maxMatches) { |
| 180 |
tout << _T("[FindBlock] Abort due to >") << static_cast<unsigned int>(maxMatches) |
| 181 |
<< _T(" matches; file position = ") << static_cast<unsigned int>(targetFileStartOffset) << _T("\n"); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
if(bestMatch->size == 0) |
| 186 |
return NULL; |
| 187 |
else |
| 188 |
return bestMatch; |
| 189 |
} else { |
| 190 |
return NULL; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
#define COMPARISON_SIZE 2048 |
| 195 |
|
| 196 |
void PatchGenerator::improveSameBlockMatch(SameBlock& match, TFileOffset currentBest) { |
| 197 |
// we should now try to make the match longer by reading big chunks of the |
| 198 |
// files to come |
| 199 |
source.seekg(match.sourceOffset + match.size,ios::beg); |
| 200 |
target.seekg(match.targetOffset + match.size,ios::beg); |
| 201 |
|
| 202 |
while(true) { |
| 203 |
unsigned char sourceData[COMPARISON_SIZE]; |
| 204 |
unsigned char targetData[COMPARISON_SIZE]; |
| 205 |
|
| 206 |
TFileOffset startTarget = match.targetOffset + match.size; |
| 207 |
TFileOffset startSource = match.sourceOffset + match.size; |
| 208 |
TFileOffset checkSize = COMPARISON_SIZE; |
| 209 |
if(checkSize > targetSize - startTarget) checkSize = targetSize - startTarget; |
| 210 |
if(checkSize > sourceSize - startSource) checkSize = sourceSize - startSource; |
| 211 |
if(checkSize == 0) break; |
| 212 |
source.read(reinterpret_cast<char*>(sourceData),checkSize); |
| 213 |
target.read(reinterpret_cast<char*>(targetData),checkSize); |
| 214 |
TFileOffset i = 0; |
| 215 |
while((sourceData[i] == targetData[i]) && (i < checkSize )) { |
| 216 |
match.size++; |
| 217 |
i++; |
| 218 |
} |
| 219 |
// check if we stopped because we had a mismatch |
| 220 |
if(i < checkSize) break; |
| 221 |
} |
| 222 |
if(match.size < blockSize) { |
| 223 |
match.size = 0; |
| 224 |
} else { |
| 225 |
// try to improve before match if this is useful |
| 226 |
if(match.size + blockSize <= currentBest) return; |
| 227 |
// do not do if there is no more data in the target... |
| 228 |
if(match.targetOffset == 0) return; |
| 229 |
|
| 230 |
// we know it is stored in the cache... so we just need the source one |
| 231 |
unsigned char sourceData[MAX_BLOCK_SIZE]; |
| 232 |
|
| 233 |
TFileOffset startSource = match.sourceOffset - blockSize; |
| 234 |
TFileOffset checkSize = blockSize; |
| 235 |
if(checkSize > match.sourceOffset) { |
| 236 |
checkSize = match.sourceOffset; |
| 237 |
startSource = 0; |
| 238 |
} |
| 239 |
if(checkSize == 0) return; |
| 240 |
source.seekg(startSource,ios::beg); |
| 241 |
source.read(reinterpret_cast<char*>(sourceData),checkSize); |
| 242 |
checkSize--; |
| 243 |
while(sourceData[checkSize] == targetCData[match.targetOffset - targetCDataBaseOffset - 1]) { |
| 244 |
match.targetOffset--; |
| 245 |
match.sourceOffset--; |
| 246 |
match.size++; |
| 247 |
checkSize--; |
| 248 |
if(checkSize == 0) break; |
| 249 |
if(match.targetOffset == 0) break; |
| 250 |
} |
| 251 |
} |
| 252 |
} |