Repository navigation
Expand file tree
/
Copy pathpatch.cpp
More file actions
309 lines (255 loc) · 8.17 KB
/
Copy pathpatch.cpp
File metadata and controls
309 lines (255 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "patch.h"
#include <fstream.h>
// Author: Steel Rat, 2010
PatchClass::PatchClass(unsigned char *buf)
{
process=0;
current=0;
// Pinfo=(PatchInfo *) buf; // ñíà÷àëà â ôàéëå ïàò÷à èä¸ò çàãîëîâîê (1)
memcpy(description, buf,128*sizeof(char));
unsigned char size1[4];
memcpy(&size1,buf+128,4*sizeof(unsigned char));
unsigned long nnum=size1[3]*256*256*256+size1[2]*256*256+size1[1]*256+size1[0];
num=nnum;
//printf("Patchinfo size=%d num=%d\n",sizeof(PatchInfo),nnum);
//printf("Patchinfo size=%d name=%s\n",sizeof(PatchInfo),Pinfo->description);
mainbuf=buf;
offset=128+4;//sizeof(PatchInfo);
GetString(regpath); // çàòåì èä¸ò ïóòü â ðååñòðå ê ïåðåìåííîé èãðû (2)
GetString(regkey); // èìÿ ïåðåìåííîé ñ ïóò¸ì ê èãðå (3)
startoffset=offset;
gamepath[0]=0;
srcbuf=(unsigned char *) malloc(1024);
destbuf=(unsigned char *) malloc(1024);
nocrc=false;
patchanyway=false;
}
PatchClass::~PatchClass()
{
if (srcbuf!=NULL) free(srcbuf);
if (destbuf!=NULL) free(destbuf);
}
void PatchClass::GetString(char *buf)
{
unsigned long size;
unsigned char size1[4];
buf[0]=0; // íà âñÿêèé ñëó÷àé, âäðóã size áóäåò 0
// memcpy(&size, (mainbuf+offset), sizeof(size));
memcpy(&size1,(mainbuf+offset),4);
size=size1[3]*256*256*256+size1[2]*256*256+size1[1]*256+size1[0];
//printf("size=%d sizeof=%d, address=%d\n",size,sizeof(size),*(mainbuf+offset+3));
offset+=sizeof(size);
memcpy(buf, (mainbuf+offset), size);
offset+=size;
}
bool PatchClass::getGamePath(char *buf)
{
if (strlen(gamepath)==0) return false;
strcpy(buf, gamepath);
return true;
}
bool PatchClass::check(char *path)
{
// ýòî ìåñòî íàäî ïåðåðàáîòàòü
int len=strlen(path);
if ((len>1)&&(process==0)) {
++process;
}
switch (process) {
case INITT:
strcpy(gamepath, path);
--len;
// åñëè íà êîíöå \ - óäàëÿåì åãî, patchmaker äîëæåí áûë ñîçäàòü èìåíà ñî ñëýøåì
if (gamepath[len]==0x5C) gamepath[len]=0;
++process;
nocrc=false;
return true;
break;
case CHECKK:
if (!CheckNextCRC()){
// abortProcess();
printf("CRC not equal\n");
process=ERRORR;
}
return true;
break;
case APPLY:
if (!PatchNext()) {
process=ERRORR;
// MessageBox(hwnd, "Error while patching!\nGame files maybe corrupted!\nProcess continues...", "Error!", MB_OK);
}
return true;
break;
case DONE:
return true;
break;
case ERRORR:
return true;
break;
}
return false;
}
bool PatchClass::CheckNextCRC()
{
char tmp[512];
unsigned long psize;
// çàêîí÷èòü ïðîâåðêó ïî îêîí÷àíèþ, ïåðåéòè ê ïðèìåíåíèþ ïàò÷à
if (current>=num) {
current=0;
offset=startoffset;
++process;
return true;
};
// êîïèðóåì FileInfo èç ïàìÿòè (4)
// memcpy(&FI, mainbuf+offset, sizeof(FileInfo));
unsigned char size1[4];
memcpy(&size1,(mainbuf+offset),4);
FI.psize=size1[3]*256*256*256+size1[2]*256*256+size1[1]*256+size1[0];
offset+=4;
memcpy(&size1,(mainbuf+offset),4);
FI.osize=size1[3]*256*256*256+size1[2]*256*256+size1[1]*256+size1[0];
offset+=4;
memcpy(&size1,(mainbuf+offset),4);
FI.crc32=size1[3]*256*256*256+size1[2]*256*256+size1[1]*256+size1[0];
offset+=4;
memcpy(&size1,(mainbuf+offset),4);
FI.packed=size1[3]*256*256*256+size1[2]*256*256+size1[1]*256+size1[0];
offset+=4;
// offset+=sizeof(FileInfo);
// ñ÷èòûâàåì èìÿ ôàéëà (5)
GetString(tmp);
strcpy(currentpath, gamepath);
//printf("psize=%d osize=%d crc32=%d packed=%d filename=%s\n",FI.psize,FI.osize,FI.crc32,FI.packed,tmp);
if(tmp[0]=='\\')tmp[0]='/';
strcat(currentpath, tmp);
// çàòåì èä¸ò ðàçìåð äàííûõ ïàò÷à (6)
// unsigned char size1[4];
memcpy(&size1,(mainbuf+offset),4);
psize=size1[3]*256*256*256+size1[2]*256*256+size1[1]*256+size1[0];
// memcpy(&psize, mainbuf+offset, sizeof(psize));
// åñëè ðàçìåð îðèãèíàëà - 0, òî ôàéëà íå áûëî â îðèãèíàëå, êîïèðóåòñÿ öåëèêîì
offset+=psize+sizeof(psize);
if (FI.osize==0) {
++current;
return true;
}
FILE *file;
file=fopen(currentpath, "rb");
if (file==NULL) {
// sprintf(tmp, "%s:\nUnable to open!", currentpath);
// MessageBox(hwnd, tmp, "Error!", MB_OK);
printf("Unable to open %s\n", currentpath);
return false;
}
fseek(file, 0, SEEK_END);
long fsize=ftell(file);
fseek(file, 0, SEEK_SET);
if ((!patchanyway)&&(fsize!=FI.osize)) {
printf("Size is not equal to original: your size: %d, original: %d\n",fsize,FI.osize);
//sprintf(tmp, "%s:\nFile size is not equal to original!\nYour size: %d, original size: %d\nPatch game ANY WAY? (NO recommended)", currentpath, fsize, FI.osize);
//if (MessageBox(hwnd, tmp, "Error!", MB_YESNO)==IDYES) patchanyway=true;
// else {
fclose(file);
return false;
// }
}
srcbuf=(unsigned char *) realloc(srcbuf, fsize);
fread(srcbuf, fsize, 1, file);
fclose(file);
if ((!nocrc)&&(Crc32(srcbuf, fsize)!=FI.crc32)) {
// sprintf(tmp, "%s:\nBad CRC! Your: %X, original: %X\nTurn off CRC check? (NO recommended)", currentpath, Crc32(srcbuf, fsize), FI.crc32);
// if (MessageBox(hwnd, tmp, "Error!", MB_YESNO)==IDYES) nocrc=true;
// else
return false;
}
++current;
return true;
}
//======================================PATCHING
bool PatchClass::PatchNext(){
char tmp[512];
unsigned long patchsize=0;
FILE *file;
if (current>=num) {
abortProcess();
process=DONE;
return true;
};
unsigned char size1[4];
memcpy(&size1,(mainbuf+offset),4);
FI.psize=size1[3]*256*256*256+size1[2]*256*256+size1[1]*256+size1[0];
offset+=4;
memcpy(&size1,(mainbuf+offset),4);
FI.osize=size1[3]*256*256*256+size1[2]*256*256+size1[1]*256+size1[0];
offset+=4;
memcpy(&size1,(mainbuf+offset),4);
FI.crc32=size1[3]*256*256*256+size1[2]*256*256+size1[1]*256+size1[0];
offset+=4;
memcpy(&size1,(mainbuf+offset),4);
FI.packed=size1[3]*256*256*256+size1[2]*256*256+size1[1]*256+size1[0];
offset+=4;
// memcpy(&FI, mainbuf+offset, sizeof(FileInfo));
// offset+=sizeof(FileInfo);
GetString(tmp);
if(tmp[0]=='\\')tmp[0]='/';
strcpy(currentpath, gamepath);
strcat(currentpath, tmp);
// unsigned char size1[4];
memcpy(&size1,(mainbuf+offset),4);
patchsize=size1[3]*256*256*256+size1[2]*256*256+size1[1]*256+size1[0];
// memcpy(&patchsize, mainbuf+offset, sizeof(patchsize));
offset+=sizeof(patchsize);
// åñëè ðàçìåð îðèãèíàëà - 0, òî ôàéëà íå áûëî â îðèãèíàëå, êîïèðóåòñÿ öåëèêîì
if (FI.osize==0) {
file=fopen(currentpath, "wb");
fwrite(mainbuf+offset, FI.psize, 1, file);
fclose(file);
offset+=FI.psize;
++current;
return true;
}
srcbuf=(unsigned char *) realloc(srcbuf, patchsize);
destbuf=(unsigned char *) realloc(destbuf, FI.psize);
//
memcpy(srcbuf, mainbuf+offset, patchsize);
offset+=patchsize;
//
file=fopen(currentpath, "rb");
if (file==NULL) {
// sprintf(tmp, "%s:\nUnable to open!", currentpath);
// MessageBox(hwnd, tmp, "Error!", MB_OK);
printf("Unable to open!\n");
return false;
}
fread(destbuf, FI.psize, 1, file);
fclose(file);
//
//printf("Create diff\n");
createDiff(patchsize);
//
//printf("Create diff\n");
file=fopen(currentpath, "wb");
fwrite(destbuf, FI.psize, 1, file);
printf("File %s patched\n", currentpath);
fclose(file);
++current;
return true;
}
//
void PatchClass::createDiff(unsigned long size)
{
unsigned long ofs, len, count=0;
while (count<size) {
unsigned char size1[4];
memcpy(&size1,(srcbuf+count),4);
len=size1[3]*256*256*256+size1[2]*256*256+size1[1]*256+size1[0];
// memcpy(&len, srcbuf+count, sizeof(len));
count+=sizeof(len);
memcpy(&size1,(srcbuf+count),4);
ofs=size1[3]*256*256*256+size1[2]*256*256+size1[1]*256+size1[0];
// memcpy(&ofs, srcbuf+count, sizeof(ofs));
count+=sizeof(ofs);
memcpy(destbuf+ofs, srcbuf+count, len);
count+=len;
}
}