| 1 |
//--------------------------------------------------------------------------- |
| 2 |
// FileFormat1.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 |
// Reviewed for Unicode support by Jim Park -- 08/29/2007 |
| 27 |
|
| 28 |
#include "FileFormat1.h" |
| 29 |
#include "GlobalTypes.h" |
| 30 |
|
| 31 |
#define MAGIC_VPAT 0x54415056 |
| 32 |
|
| 33 |
namespace FileFormat1 { |
| 34 |
void writeByte(bostream& patch, TFileOffset dw) { |
| 35 |
unsigned char b = dw & 0xFF; |
| 36 |
patch.write(reinterpret_cast<char*>(&b),sizeof(b)); |
| 37 |
} |
| 38 |
void writeWord(bostream& patch, TFileOffset dw) { |
| 39 |
unsigned char b = dw & 0xFF; |
| 40 |
patch.write(reinterpret_cast<char*>(&b),sizeof(b)); |
| 41 |
b = (dw & 0xFF00) >> 8; |
| 42 |
patch.write(reinterpret_cast<char*>(&b),sizeof(b)); |
| 43 |
} |
| 44 |
void writeDword(bostream& patch, TFileOffset dw) { |
| 45 |
unsigned char b = dw & 0xFF; |
| 46 |
patch.write(reinterpret_cast<char*>(&b),sizeof(b)); |
| 47 |
b = (dw & 0xFF00) >> 8; |
| 48 |
patch.write(reinterpret_cast<char*>(&b),sizeof(b)); |
| 49 |
b = (dw & 0xFF0000) >> 16; |
| 50 |
patch.write(reinterpret_cast<char*>(&b),sizeof(b)); |
| 51 |
b = (dw & 0xFF000000) >> 24; |
| 52 |
patch.write(reinterpret_cast<char*>(&b),sizeof(b)); |
| 53 |
} |
| 54 |
|
| 55 |
void writeMD5(bostream& patch, md5_byte_t digest[16]) { |
| 56 |
for(int i = 0; i < 16; i++) { |
| 57 |
writeByte(patch, digest[i]); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
TFileOffset readDword(bistream& patch) { |
| 62 |
unsigned char b; |
| 63 |
patch.read(reinterpret_cast<char*>(&b),sizeof(b)); |
| 64 |
TFileOffset dw = b; |
| 65 |
patch.read(reinterpret_cast<char*>(&b),sizeof(b)); |
| 66 |
dw = dw | (b << 8); |
| 67 |
patch.read(reinterpret_cast<char*>(&b),sizeof(b)); |
| 68 |
dw = dw | (b << 16); |
| 69 |
patch.read(reinterpret_cast<char*>(&b),sizeof(b)); |
| 70 |
dw = dw | (b << 24); |
| 71 |
return dw; |
| 72 |
} |
| 73 |
|
| 74 |
void readMD5(bistream& patch, md5_byte_t digest[16]) { |
| 75 |
unsigned char b; |
| 76 |
for(int i = 0; i < 16; i++) { |
| 77 |
patch.read(reinterpret_cast<char*>(&b),sizeof(b)); |
| 78 |
digest[i] = b; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
void writeFileCount(bostream& f, TFileOffset currentCount) { |
| 83 |
f.seekp(4,ios::beg); |
| 84 |
writeDword(f,currentCount); |
| 85 |
} |
| 86 |
|
| 87 |
TFileOffset removeExistingPatch(bistream& in, TFileOffset inSize, bostream& out, TChecksum* removeCRC, bool existenceIsError) { |
| 88 |
TFileOffset fileCount = 0x80000000; // MD5 mode |
| 89 |
if(in.bad() || in.eof() || (inSize == 0)) { // empty file/does not yet exist |
| 90 |
writeDword(out,MAGIC_VPAT); |
| 91 |
writeDword(out,fileCount); // noFiles |
| 92 |
return fileCount; |
| 93 |
} |
| 94 |
// copy and do stuff |
| 95 |
if(readDword(in) != MAGIC_VPAT) { |
| 96 |
writeDword(out,MAGIC_VPAT); |
| 97 |
writeDword(out,fileCount); // noFiles |
| 98 |
return fileCount; |
| 99 |
} |
| 100 |
fileCount = readDword(in); |
| 101 |
writeDword(out,MAGIC_VPAT); |
| 102 |
writeDword(out,fileCount); // noFiles |
| 103 |
bool MD5Mode = (fileCount & 0x80000000) != 0; |
| 104 |
|
| 105 |
if(MD5Mode) removeCRC->mode = TChecksum::MD5; |
| 106 |
if(!MD5Mode) removeCRC->mode = TChecksum::CRC32; |
| 107 |
|
| 108 |
// top byte is reserved for extensions |
| 109 |
fileCount = fileCount & 0x00FFFFFF; |
| 110 |
|
| 111 |
TFileOffset tempCount = fileCount; |
| 112 |
for(TFileOffset i = 0; i < tempCount; i++) { |
| 113 |
TFileOffset startOffset = in.tellg(); |
| 114 |
readDword(in); // noBlocks |
| 115 |
TChecksum sourceChecksum; |
| 116 |
if(!MD5Mode) { |
| 117 |
crc32_t sourceCRC = readDword(in); // SourceCRC |
| 118 |
readDword(in); // TargetCRC |
| 119 |
sourceChecksum.loadCRC32(sourceCRC); |
| 120 |
} else { |
| 121 |
md5_byte_t digest[16]; |
| 122 |
readMD5(in, digest); // SourceCRC |
| 123 |
sourceChecksum.loadMD5(digest); |
| 124 |
readMD5(in, digest); // TargetCRC |
| 125 |
} |
| 126 |
TFileOffset bodySize = readDword(in); // bodySize |
| 127 |
in.seekg(bodySize,ios::cur); |
| 128 |
TFileOffset endOffset = in.tellg(); |
| 129 |
if(sourceChecksum == *removeCRC) { |
| 130 |
if(existenceIsError) { |
| 131 |
throw _T("Source file with the exact same contents already exists in patch!\nUse /R option (replace) to replace it with this patch!"); |
| 132 |
} |
| 133 |
fileCount--; |
| 134 |
} else { |
| 135 |
// copy this patch to out |
| 136 |
in.seekg(startOffset,ios::beg); |
| 137 |
TFileOffset size = endOffset-startOffset; |
| 138 |
char* buffer = new char[size]; |
| 139 |
in.read(buffer,size); |
| 140 |
out.write(buffer,size); |
| 141 |
delete[] buffer; |
| 142 |
} |
| 143 |
} |
| 144 |
TFileOffset curPos = out.tellp(); |
| 145 |
if(MD5Mode) fileCount = fileCount | 0x80000000; |
| 146 |
writeFileCount(out,fileCount); |
| 147 |
out.seekp(curPos,ios::beg); |
| 148 |
return fileCount; |
| 149 |
} |
| 150 |
|
| 151 |
void writePatch(bostream& patch, bistream& target, vector<SameBlock*>& sameBlocks, TChecksum* sourceCRC, TChecksum* targetCRC, TFileOffset currentFileCount, POSIX::ALT_FILETIME targetTime) { |
| 152 |
TFileOffset bodySize = 0; |
| 153 |
TFileOffset noBlocks = 0; |
| 154 |
TFileOffset noBlocksOffset = patch.tellp(); |
| 155 |
writeDword(patch,noBlocks); |
| 156 |
if(sourceCRC->mode == TChecksum::MD5) { |
| 157 |
writeMD5(patch,sourceCRC->digest); // sourceCRC |
| 158 |
writeMD5(patch,targetCRC->digest); // targetCRC |
| 159 |
} else { |
| 160 |
writeDword(patch,sourceCRC->crc); // sourceCRC |
| 161 |
writeDword(patch,targetCRC->crc); // targetCRC |
| 162 |
} |
| 163 |
TFileOffset bodySizeOffset = patch.tellp(); |
| 164 |
writeDword(patch,bodySize); |
| 165 |
|
| 166 |
for(vector<SameBlock*>::iterator iter = sameBlocks.begin(); iter != sameBlocks.end(); iter++) { |
| 167 |
SameBlock* current = *iter; |
| 168 |
|
| 169 |
// store current block |
| 170 |
if(current->size > 0) { |
| 171 |
// copy block from sourceFile |
| 172 |
if(current->size < 256) { |
| 173 |
writeByte(patch,1); |
| 174 |
writeByte(patch,current->size); |
| 175 |
bodySize += 2; |
| 176 |
} else if(current->size < 65536) { |
| 177 |
writeByte(patch,2); |
| 178 |
writeWord(patch,current->size); |
| 179 |
bodySize += 3; |
| 180 |
} else { |
| 181 |
writeByte(patch,3); |
| 182 |
writeDword(patch,current->size); |
| 183 |
bodySize += 5; |
| 184 |
} |
| 185 |
writeDword(patch,current->sourceOffset); |
| 186 |
bodySize += 4; |
| 187 |
noBlocks++; |
| 188 |
} |
| 189 |
iter++; |
| 190 |
if(iter == sameBlocks.end()) break; |
| 191 |
SameBlock* next = *iter; |
| 192 |
iter--; |
| 193 |
|
| 194 |
// calculate area inbetween this block and the next |
| 195 |
TFileOffset notFoundStart = current->targetOffset+current->size; |
| 196 |
if(notFoundStart > next->targetOffset) { |
| 197 |
throw _T("makeBinaryPatch input problem: there was overlap"); |
| 198 |
} |
| 199 |
TFileOffset notFoundSize = next->targetOffset - notFoundStart; |
| 200 |
if(notFoundSize > 0) { |
| 201 |
// we need to include this area in the patch directly |
| 202 |
if(notFoundSize < 256) { |
| 203 |
writeByte(patch,5); |
| 204 |
writeByte(patch,notFoundSize); |
| 205 |
bodySize += 2; |
| 206 |
} else if(notFoundSize < 65536) { |
| 207 |
writeByte(patch,6); |
| 208 |
writeWord(patch,notFoundSize); |
| 209 |
bodySize += 3; |
| 210 |
} else { |
| 211 |
writeByte(patch,7); |
| 212 |
writeDword(patch,notFoundSize); |
| 213 |
bodySize += 5; |
| 214 |
} |
| 215 |
// copy from target... |
| 216 |
target.seekg(notFoundStart,ios::beg); |
| 217 |
#define COPY_BUF_SIZE 4096 |
| 218 |
char copyBuffer[COPY_BUF_SIZE]; |
| 219 |
for(TFileOffset i = 0; i < notFoundSize; i += COPY_BUF_SIZE) { |
| 220 |
TFileOffset j = notFoundSize - i; |
| 221 |
if(j > COPY_BUF_SIZE) j = COPY_BUF_SIZE; |
| 222 |
target.read(copyBuffer,j); |
| 223 |
patch.write(copyBuffer,j); |
| 224 |
} |
| 225 |
bodySize += notFoundSize; |
| 226 |
noBlocks++; |
| 227 |
} |
| 228 |
} |
| 229 |
// we are done, now add just one extra block with the target file time |
| 230 |
writeByte(patch,255); |
| 231 |
writeDword(patch,targetTime.dwLowDateTime); |
| 232 |
writeDword(patch,targetTime.dwHighDateTime); |
| 233 |
noBlocks++; |
| 234 |
bodySize += 9; |
| 235 |
|
| 236 |
TFileOffset curPos = patch.tellp(); |
| 237 |
patch.seekp(noBlocksOffset,ios::beg); |
| 238 |
writeDword(patch,noBlocks); |
| 239 |
patch.seekp(bodySizeOffset,ios::beg); |
| 240 |
writeDword(patch,bodySize); |
| 241 |
// do this at the end because it messes up file position |
| 242 |
writeFileCount(patch,++currentFileCount); |
| 243 |
patch.seekp(curPos,ios::beg); |
| 244 |
} |
| 245 |
} |