| 1 |
//--------------------------------------------------------------------------- |
| 2 |
// apply_patch.c |
| 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 "apply_patch.h" |
| 29 |
#include "checksum.h" |
| 30 |
|
| 31 |
/* ------------------------ patch application ----------------- */ |
| 32 |
|
| 33 |
#define BLOCKSIZE 16384 |
| 34 |
|
| 35 |
int DoPatch(HANDLE hPatch, HANDLE hSource, HANDLE hDest) { |
| 36 |
|
| 37 |
static char block[BLOCKSIZE]; |
| 38 |
|
| 39 |
unsigned long temp = 0; |
| 40 |
unsigned long read; |
| 41 |
unsigned long source_crc = 0; |
| 42 |
md5_byte_t source_md5[16]; |
| 43 |
unsigned long patch_dest_crc = 0; |
| 44 |
md5_byte_t patch_dest_md5[16]; |
| 45 |
int MD5Mode = 0; |
| 46 |
unsigned long patches = 0; |
| 47 |
int already_uptodate = 0; |
| 48 |
|
| 49 |
FILETIME targetModifiedTime; |
| 50 |
|
| 51 |
// special 'addition' for the dll: since the patch file is now |
| 52 |
// in a separate file, the VPAT header might be right at the start |
| 53 |
// of the file, and a pointer at the end of the file is probably missing |
| 54 |
// (because all patch generator versions don't append it, the linker/gui |
| 55 |
// does this). |
| 56 |
SetFilePointer(hPatch, 0, NULL, FILE_BEGIN); |
| 57 |
ReadFile(hPatch, &temp, 4, &read, NULL); |
| 58 |
// it's not at the start of file -> there must be a pointer at the end of |
| 59 |
// file then |
| 60 |
if (temp != 0x54415056) { |
| 61 |
SetFilePointer(hPatch, -4, NULL, FILE_END); |
| 62 |
ReadFile(hPatch, &temp, 4, &read, NULL); |
| 63 |
|
| 64 |
SetFilePointer(hPatch, temp, NULL, FILE_BEGIN); |
| 65 |
ReadFile(hPatch, &temp, 4, &read, NULL); |
| 66 |
if (temp != 0x54415056) |
| 67 |
return PATCH_CORRUPT; |
| 68 |
} |
| 69 |
|
| 70 |
// target file date is by default the current system time |
| 71 |
GetSystemTimeAsFileTime(&targetModifiedTime); |
| 72 |
|
| 73 |
// read the number of patches in the file |
| 74 |
ReadFile(hPatch, &patches, 4, &read, NULL); |
| 75 |
if(patches & 0x80000000) MD5Mode = 1; |
| 76 |
// MSB is now reserved for future extensions, anyone wanting more than |
| 77 |
// 16 million patches in a single file is nuts anyway |
| 78 |
patches = patches & 0x00FFFFFF; |
| 79 |
|
| 80 |
if(!MD5Mode) { |
| 81 |
if (!FileCRC(hSource, &source_crc)) |
| 82 |
return PATCH_ERROR; |
| 83 |
} else { |
| 84 |
if (!FileMD5(hSource, source_md5)) |
| 85 |
return PATCH_ERROR; |
| 86 |
} |
| 87 |
|
| 88 |
while (patches--) { |
| 89 |
long patch_blocks = 0, patch_size = 0; |
| 90 |
|
| 91 |
// flag which needs to be set by one of the checksum checks |
| 92 |
int currentPatchMatchesChecksum = 0; |
| 93 |
|
| 94 |
// read the number of blocks this patch has |
| 95 |
if(!ReadFile(hPatch, &patch_blocks, 4, &read, NULL)) { |
| 96 |
return PATCH_CORRUPT; |
| 97 |
} |
| 98 |
|
| 99 |
// read checksums |
| 100 |
if(!MD5Mode) { |
| 101 |
unsigned long patch_source_crc = 0; |
| 102 |
if(!ReadFile(hPatch, &patch_source_crc, 4, &read, NULL)) { |
| 103 |
return PATCH_CORRUPT; |
| 104 |
} |
| 105 |
if(!ReadFile(hPatch, &patch_dest_crc, 4, &read, NULL)) { |
| 106 |
return PATCH_CORRUPT; |
| 107 |
} |
| 108 |
// check to see if it's already up-to-date for some patch |
| 109 |
if (source_crc == patch_dest_crc) { |
| 110 |
already_uptodate = 1; |
| 111 |
} |
| 112 |
if (source_crc == patch_source_crc) { |
| 113 |
currentPatchMatchesChecksum = 1; |
| 114 |
} |
| 115 |
} else { |
| 116 |
int md5index; |
| 117 |
md5_byte_t patch_source_md5[16]; |
| 118 |
if(!ReadFile(hPatch, patch_source_md5, 16, &read, NULL)) { |
| 119 |
return PATCH_CORRUPT; |
| 120 |
} |
| 121 |
if(!ReadFile(hPatch, patch_dest_md5, 16, &read, NULL)) { |
| 122 |
return PATCH_CORRUPT; |
| 123 |
} |
| 124 |
// check to see if it's already up-to-date for some patch |
| 125 |
for(md5index = 0; md5index < 16; md5index++) { |
| 126 |
if(source_md5[md5index] != patch_dest_md5[md5index]) break; |
| 127 |
if(md5index == 15) already_uptodate = 1; |
| 128 |
} |
| 129 |
for(md5index = 0; md5index < 16; md5index++) { |
| 130 |
if(source_md5[md5index] != patch_source_md5[md5index]) break; |
| 131 |
if(md5index == 15) currentPatchMatchesChecksum = 1; |
| 132 |
} |
| 133 |
} |
| 134 |
// read the size of the patch, we can use this to skip over it |
| 135 |
if(!ReadFile(hPatch, &patch_size, 4, &read, NULL)) { |
| 136 |
return PATCH_CORRUPT; |
| 137 |
} |
| 138 |
|
| 139 |
if(currentPatchMatchesChecksum) { |
| 140 |
while (patch_blocks--) { |
| 141 |
unsigned char blocktype = 0; |
| 142 |
unsigned long blocksize = 0; |
| 143 |
if(!ReadFile(hPatch, &blocktype, 1, &read, NULL)) { |
| 144 |
return PATCH_CORRUPT; |
| 145 |
} |
| 146 |
switch (blocktype) { |
| 147 |
case 1: |
| 148 |
case 2: |
| 149 |
case 3: |
| 150 |
if (blocktype == 1) |
| 151 |
{ unsigned char x; blocksize = ReadFile(hPatch,&x,1,&read,NULL)? x: 0; } |
| 152 |
else if (blocktype == 2) |
| 153 |
{ unsigned short x; blocksize = ReadFile(hPatch,&x,2,&read,NULL)? x: 0; } |
| 154 |
else |
| 155 |
{ unsigned long x; blocksize = ReadFile(hPatch,&x,4,&read,NULL)? x:0; } |
| 156 |
|
| 157 |
if (!blocksize || !ReadFile(hPatch, &temp, 4, &read, NULL) || read != 4) |
| 158 |
return PATCH_CORRUPT; |
| 159 |
|
| 160 |
SetFilePointer(hSource, temp, 0, FILE_BEGIN); |
| 161 |
|
| 162 |
do { |
| 163 |
if(!ReadFile(hSource, block, min(BLOCKSIZE, blocksize), &read, NULL)) { |
| 164 |
return PATCH_ERROR; |
| 165 |
} |
| 166 |
WriteFile(hDest, block, read, &temp, NULL); |
| 167 |
if (temp != min(BLOCKSIZE, blocksize)) |
| 168 |
return PATCH_ERROR; |
| 169 |
blocksize -= temp; |
| 170 |
} while (temp); |
| 171 |
|
| 172 |
break; |
| 173 |
|
| 174 |
case 5: |
| 175 |
case 6: |
| 176 |
case 7: |
| 177 |
if (blocktype == 5) |
| 178 |
{ unsigned char x; blocksize = ReadFile(hPatch,&x,1,&read,NULL)? x:0; } |
| 179 |
else if (blocktype == 6) |
| 180 |
{ unsigned short x; blocksize = ReadFile(hPatch,&x,2,&read,NULL)? x:0; } |
| 181 |
else |
| 182 |
{ unsigned long x; blocksize = ReadFile(hPatch,&x,4,&read,NULL)? x:0; } |
| 183 |
|
| 184 |
if (!blocksize) |
| 185 |
return PATCH_CORRUPT; |
| 186 |
|
| 187 |
do { |
| 188 |
if(!ReadFile(hPatch, block, min(BLOCKSIZE, blocksize), &read, NULL)) { |
| 189 |
return PATCH_CORRUPT; |
| 190 |
} |
| 191 |
WriteFile(hDest, block, read, &temp, NULL); |
| 192 |
if (temp != min(BLOCKSIZE, blocksize)) |
| 193 |
return PATCH_ERROR; |
| 194 |
blocksize -= temp; |
| 195 |
} while (temp); |
| 196 |
|
| 197 |
break; |
| 198 |
|
| 199 |
case 255: // read the file modified time from the patch |
| 200 |
if(!ReadFile(hPatch,&targetModifiedTime,sizeof(targetModifiedTime),&read,NULL)) { |
| 201 |
return PATCH_CORRUPT; |
| 202 |
} |
| 203 |
break; |
| 204 |
|
| 205 |
default: |
| 206 |
return PATCH_CORRUPT; |
| 207 |
} |
| 208 |
} |
| 209 |
if(!MD5Mode) { |
| 210 |
unsigned long dest_crc = 0; |
| 211 |
if(!FileCRC(hDest, &dest_crc)) { |
| 212 |
return PATCH_ERROR; |
| 213 |
} |
| 214 |
if (dest_crc != patch_dest_crc) |
| 215 |
return PATCH_ERROR; |
| 216 |
} else { |
| 217 |
int md5index; |
| 218 |
md5_byte_t dest_md5[16]; |
| 219 |
if(!FileMD5(hDest, dest_md5)) { |
| 220 |
return PATCH_ERROR; |
| 221 |
} |
| 222 |
for(md5index = 0; md5index < 16; md5index++) { |
| 223 |
if(dest_md5[md5index] != patch_dest_md5[md5index]) return PATCH_ERROR; |
| 224 |
} |
| 225 |
} |
| 226 |
// set file time |
| 227 |
SetFileTime(hDest, NULL, NULL, &targetModifiedTime); |
| 228 |
|
| 229 |
return PATCH_SUCCESS; |
| 230 |
} else { |
| 231 |
SetFilePointer(hPatch, patch_size, NULL, FILE_CURRENT); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
// if already up to date, it doesn't matter that we didn't match |
| 236 |
if(already_uptodate) { |
| 237 |
return PATCH_UPTODATE; |
| 238 |
} else { |
| 239 |
return PATCH_NOMATCH; |
| 240 |
} |
| 241 |
} |