| 1 |
//--------------------------------------------------------------------------- |
| 2 |
// main.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 |
#ifdef __BORLANDC__ |
| 29 |
#pragma argsused |
| 30 |
#endif |
| 31 |
|
| 32 |
#if !defined(__BORLANDC__) && !defined(_MSC_VER) |
| 33 |
#include <unistd.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
#include "tchar.h" |
| 37 |
|
| 38 |
#if defined(__WIN32__) || defined(_WIN32) |
| 39 |
#define OPT_CHAR _T('/') |
| 40 |
#else |
| 41 |
#define OPT_CHAR _T('-') |
| 42 |
#endif |
| 43 |
const TCHAR OPT_CHARSTR[] = {OPT_CHAR, _T('\0')} ; |
| 44 |
// Win32 now supports "/" AND "-" switches (like makensis) but the filename warning only makes sense for "-" |
| 45 |
#define OPT_FSCONFLICTCHARSTR _T("-") |
| 46 |
|
| 47 |
inline bool ISSINGLESWITCHCHAR(const TCHAR c) { return ( OPT_CHAR==(c) || (OPT_CHAR!=_T('-') && _T('-')==(c)) ); } |
| 48 |
#undef OPT_CHAR |
| 49 |
|
| 50 |
#include "GlobalTypes.h" |
| 51 |
#include "POSIXUtil.h" |
| 52 |
#include "Checksums.h" |
| 53 |
#include "PatchGenerator.h" |
| 54 |
#include "FileFormat1.h" |
| 55 |
|
| 56 |
#include <fstream> |
| 57 |
#include <sstream> |
| 58 |
|
| 59 |
#ifdef _WIN32 |
| 60 |
#include "../../../../Source/Platform.h" |
| 61 |
NSIS_ENTRYPOINT_TMAIN |
| 62 |
#endif |
| 63 |
int _tmain( int argc, TCHAR * argv[] ) { |
| 64 |
tout << _T("GenPat v3.1\n"); |
| 65 |
tout << _T("===========\n\n(c) 2001-2005 Van de Sande Productions\n"); |
| 66 |
tout << _T("Website: http://www.tibed.net/vpatch\n\n"); |
| 67 |
|
| 68 |
tstring sourceFileName; |
| 69 |
tstring targetFileName; |
| 70 |
tstring patchFileName; |
| 71 |
|
| 72 |
bool showHelp = true; |
| 73 |
|
| 74 |
int blockSize = 64; |
| 75 |
int maxMatches = 500; |
| 76 |
bool beVerbose = false; |
| 77 |
bool beOptimal = false; |
| 78 |
bool existenceIsError = true; // flag error if patch already has source's MD5/CRC32? |
| 79 |
int fileNameArgument = 0; |
| 80 |
if(argc > 1) { |
| 81 |
for(int i = 1; i < argc; i++) { |
| 82 |
tstring s(argv[i]); |
| 83 |
if(s.size() > 0) { |
| 84 |
if(ISSINGLESWITCHCHAR(s[0])) { |
| 85 |
if(s.size() > 1) { |
| 86 |
if((s[1] == _T('v')) || (s[1] == _T('V'))) { |
| 87 |
beVerbose = true; |
| 88 |
} |
| 89 |
if((s[1] == _T('o')) || (s[1] == _T('O'))) { |
| 90 |
beOptimal = true; |
| 91 |
} |
| 92 |
if((s[1] == _T('r')) || (s[1] == _T('R'))) { |
| 93 |
existenceIsError = false; |
| 94 |
} |
| 95 |
} |
| 96 |
if(s.size() > 2) { |
| 97 |
if((s[1] == _T('b')) || (s[1] == _T('B'))) { |
| 98 |
if(s[2] == _T('=')) { |
| 99 |
tistringstream ss(s.substr(3)); |
| 100 |
ss >> blockSize; |
| 101 |
} |
| 102 |
} |
| 103 |
if((s[1] == _T('a')) || (s[1] == _T('A'))) { |
| 104 |
if(s[2] == _T('=')) { |
| 105 |
tistringstream ss(s.substr(3)); |
| 106 |
ss >> maxMatches; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} else { |
| 111 |
switch (fileNameArgument) { |
| 112 |
case 0: |
| 113 |
sourceFileName = s; |
| 114 |
break; |
| 115 |
case 1: |
| 116 |
targetFileName = s; |
| 117 |
break; |
| 118 |
case 2: |
| 119 |
patchFileName = s; |
| 120 |
showHelp = false; |
| 121 |
break; |
| 122 |
default: |
| 123 |
terr << _T("WARNING: extra filename argument not used: ") << s << _T("\n"); |
| 124 |
} |
| 125 |
fileNameArgument++; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
if(beOptimal) { |
| 131 |
maxMatches = 0; |
| 132 |
} |
| 133 |
if(showHelp) { |
| 134 |
tout << _T("This program will take (sourcefile) as input and create a (patchfile).\n"); |
| 135 |
tout << _T("With this patchfile, you can convert a (sourcefile) into (targetfile).\n\n"); |
| 136 |
tout << _T("Command line info:\n"); |
| 137 |
tout << _T(" GENPAT (sourcefile) (targetfile) (patchfile)\n\n"); |
| 138 |
|
| 139 |
tout << _T("Command line option (optional):\n"); |
| 140 |
tout << OPT_CHARSTR << _T("R Replace a patch with same contents as source silently if it\n already exists.\n"); |
| 141 |
tout << OPT_CHARSTR << _T("B=64 Set blocksize (default=64), multiple of 2 is required.\n"); |
| 142 |
tout << OPT_CHARSTR << _T("V More verbose information during patch creation.\n"); |
| 143 |
tout << OPT_CHARSTR << _T("O Deactivate match limit of the ") << OPT_CHARSTR << _T("A switch (sometimes smaller patches).\n"); |
| 144 |
tout << OPT_CHARSTR << _T("A=500 Maximum number of block matches per block (improves performance).\n"); |
| 145 |
tout << _T(" Default is 500, larger is slower. Use ") << OPT_CHARSTR << _T("V to see the cut-off aborts.\n\n"); |
| 146 |
tout << _T("Note: filenames should never start with ") << OPT_FSCONFLICTCHARSTR << _T(" character!\n\n"); |
| 147 |
tout << _T("Possible exit codes:\n"); |
| 148 |
tout << _T(" 0 Success\n"); |
| 149 |
tout << _T(" 1 Arguments missing\n"); |
| 150 |
tout << _T(" 2 Other error\n"); |
| 151 |
tout << _T(" 3 Source file already has a patch in specified patch file (=error)\n"); |
| 152 |
return 1; |
| 153 |
} |
| 154 |
|
| 155 |
tout << _T("[Source] ") << sourceFileName.c_str() << _T("\n"); |
| 156 |
tout << _T("[Target] ") << targetFileName.c_str() << _T("\n[PatchFile] ") << patchFileName.c_str() << _T("\n"); |
| 157 |
|
| 158 |
// get the file sizes |
| 159 |
TFileOffset sourceSize = 0; |
| 160 |
try { |
| 161 |
sourceSize = POSIX::getFileSize(sourceFileName.c_str()); |
| 162 |
} |
| 163 |
catch(TCHAR* s) { |
| 164 |
terr << _T("Source file size reading failed: ") << s << _T("\n"); |
| 165 |
return 2; |
| 166 |
} |
| 167 |
TFileOffset targetSize; |
| 168 |
try { |
| 169 |
targetSize = POSIX::getFileSize(targetFileName.c_str()); |
| 170 |
} |
| 171 |
catch(const TCHAR* s) { |
| 172 |
terr << _T("Target file size reading failed: ") << s << _T("\n"); |
| 173 |
return 2; |
| 174 |
} |
| 175 |
|
| 176 |
// calculate CRCs |
| 177 |
TChecksum* sourceCRC = new TChecksum(sourceFileName); |
| 178 |
sourceCRC->mode = TChecksum::MD5; // default |
| 179 |
TChecksum* targetCRC = new TChecksum(targetFileName); |
| 180 |
targetCRC->mode = TChecksum::MD5; // default |
| 181 |
|
| 182 |
tstring tempFileName = POSIX::getTempFile(); |
| 183 |
if (tempFileName == _T("")) |
| 184 |
return 2; |
| 185 |
|
| 186 |
// open the files |
| 187 |
bifstream source; |
| 188 |
source.open(sourceFileName.c_str(), std::ios_base::binary | std::ios_base::in); |
| 189 |
bifstream target; |
| 190 |
target.open(targetFileName.c_str(), std::ios_base::binary | std::ios_base::in); |
| 191 |
bofstream patch; |
| 192 |
patch.open(tempFileName.c_str(), std::ios_base::binary | std::ios_base::out); |
| 193 |
|
| 194 |
// remove existing patch with sourceCRC |
| 195 |
TFileOffset fileCount = 0; |
| 196 |
while(true) { |
| 197 |
TFileOffset previousPatchSize = 0; |
| 198 |
try { |
| 199 |
previousPatchSize = POSIX::getFileSize(patchFileName.c_str()); |
| 200 |
} catch(const TCHAR* s) { |
| 201 |
tout << _T("Patch file does not yet exist: ") << s << _T(", it will be created.\n"); |
| 202 |
bofstream newfile; |
| 203 |
newfile.open(patchFileName.c_str(), std::ios_base::binary | std::ios_base::out); |
| 204 |
newfile.close(); |
| 205 |
} |
| 206 |
bifstream previousPatch; |
| 207 |
previousPatch.open(patchFileName.c_str(), std::ios_base::binary | std::ios_base::in); |
| 208 |
|
| 209 |
try { |
| 210 |
// this will copy the contents of previousPatch to patch, but without sourceCRC |
| 211 |
fileCount = FileFormat1::removeExistingPatch(previousPatch,previousPatchSize,patch,sourceCRC,existenceIsError); |
| 212 |
} catch(const TCHAR* s) { |
| 213 |
terr << _T("ERROR: ") << s << _T("\n"); |
| 214 |
patch.close(); |
| 215 |
_tunlink(tempFileName.c_str()); |
| 216 |
return 3; |
| 217 |
} |
| 218 |
|
| 219 |
// set them to the same checksum mode |
| 220 |
targetCRC->mode = sourceCRC->mode; |
| 221 |
tout << _T("[Checksum] Kind of checksums used: "); |
| 222 |
if(targetCRC->mode == TChecksum::MD5) tout << _T("MD5\n"); |
| 223 |
if(targetCRC->mode == TChecksum::CRC32) tout << _T("CRC32\n"); |
| 224 |
break; |
| 225 |
} |
| 226 |
|
| 227 |
if(source.good() && target.good() && patch.good()) { |
| 228 |
PatchGenerator* gen = new PatchGenerator(source,sourceSize,target,targetSize,patch); |
| 229 |
try { |
| 230 |
// clean up the blocksize to be a multiple of 2 |
| 231 |
int orgBlockSize = blockSize; |
| 232 |
int bs_counter = 0; |
| 233 |
while(blockSize != 0) { |
| 234 |
bs_counter++; |
| 235 |
blockSize >>= 1; |
| 236 |
} |
| 237 |
blockSize = 1; |
| 238 |
while(bs_counter != 0) { |
| 239 |
blockSize <<= 1; |
| 240 |
bs_counter--; |
| 241 |
} |
| 242 |
if((blockSize >> 1) == orgBlockSize) blockSize = orgBlockSize; |
| 243 |
if(blockSize != orgBlockSize) { |
| 244 |
tout << _T("[BlockSizeFix] Your blocksize had to be fixed since it is not a multiple of 2\n"); |
| 245 |
} |
| 246 |
if(blockSize < 16) { |
| 247 |
blockSize = 16; |
| 248 |
tout << _T("[BlockSizeFix] Your blocksize had to be fixed since it is smaller than 16\n"); |
| 249 |
} |
| 250 |
|
| 251 |
gen->blockSize = blockSize; |
| 252 |
tout << _T("[BlockSize] ") << static_cast<unsigned int>(gen->blockSize) << _T(" bytes\n"); |
| 253 |
|
| 254 |
gen->maxMatches = maxMatches; |
| 255 |
if(gen->maxMatches == 0) { |
| 256 |
tout << _T("[FindBlockMatchLimit] Unlimited matches\n"); |
| 257 |
} else { |
| 258 |
tout << _T("[FindBlockMatchLimit] ") << gen->maxMatches << _T(" matches\n"); |
| 259 |
} |
| 260 |
|
| 261 |
gen->beVerbose = beVerbose; |
| 262 |
if(beVerbose) { |
| 263 |
tout << _T("[Debug] Verbose output during patch generation activated.\n"); |
| 264 |
} |
| 265 |
|
| 266 |
// create sameBlock storage |
| 267 |
vector<SameBlock*> sameBlocks; |
| 268 |
// run the patch generator to find similar blocks |
| 269 |
gen->execute(sameBlocks); |
| 270 |
// construct the actual patch in FileFormat1 |
| 271 |
FileFormat1::writePatch(patch,target,sameBlocks,sourceCRC,targetCRC,fileCount,POSIX::getFileTime(targetFileName.c_str())); |
| 272 |
// cleanup sameblocks |
| 273 |
for(vector<SameBlock*>::iterator iter = sameBlocks.begin(); iter != sameBlocks.end(); iter++) { |
| 274 |
delete *iter; |
| 275 |
*iter = NULL; |
| 276 |
} |
| 277 |
|
| 278 |
patch.close(); // Close the temporary patch file so we can open it again for reading |
| 279 |
TFileOffset patchSize = POSIX::getFileSize(tempFileName.c_str()); |
| 280 |
char* buf = new char[patchSize]; |
| 281 |
if (!buf) throw _T("Out of memory"); // In case we switch to nothrow_t |
| 282 |
|
| 283 |
// finally: copy the temporary file to the actual patch |
| 284 |
bifstream tempF; |
| 285 |
tempF.open(tempFileName.c_str(), std::ios_base::binary | std::ios_base::in); |
| 286 |
tempF.read(buf,patchSize); |
| 287 |
if (tempF.fail()) throw _T("Could not read temp file"); |
| 288 |
tempF.close(); |
| 289 |
bofstream().open(tempFileName.c_str(), std::ios_base::binary | std::ios_base::out); // empty the temporary file |
| 290 |
|
| 291 |
bofstream patchF; |
| 292 |
patchF.open(patchFileName.c_str(), std::ios_base::binary | std::ios_base::out); |
| 293 |
patchF.write(buf,patchSize); |
| 294 |
if (patchF.fail()) throw _T("Could not write patch file"); |
| 295 |
delete[] buf; |
| 296 |
} |
| 297 |
catch(tstring s) { |
| 298 |
terr << _T("Error thrown: ") << s.c_str(); |
| 299 |
return 2; |
| 300 |
} |
| 301 |
catch(const TCHAR* s) { |
| 302 |
terr << _T("Error thrown: ") << s; |
| 303 |
return 2; |
| 304 |
} |
| 305 |
catch(...) { |
| 306 |
terr << _T("Error thrown: Unknown error!\n"); |
| 307 |
return 2; |
| 308 |
} |
| 309 |
} else { |
| 310 |
terr << _T("There was a problem opening the files.\n"); |
| 311 |
return 2; |
| 312 |
} |
| 313 |
if(*sourceCRC == *targetCRC) |
| 314 |
terr << _T("WARNING: source and target file have equal CRCs!"); |
| 315 |
delete sourceCRC; |
| 316 |
delete targetCRC; |
| 317 |
_tunlink(tempFileName.c_str()); |
| 318 |
return 0; |
| 319 |
} |