-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
patch.c
604 lines (537 loc) · 15.8 KB
/
patch.c
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/******************************************************************************
* Copyright (c) 2022 Jaroslav Hensl *
* *
* Permission is hereby granted, free of charge, to any person *
* obtaining a copy of this software and associated documentation *
* files (the "Software"), to deal in the Software without *
* restriction, including without limitation the rights to use, *
* copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be *
* included in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, *
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
* OTHER DEALINGS IN THE SOFTWARE. *
* *
*******************************************************************************/
#include "patcher9x.h"
#include <bpatcher.h>
#include "vmm_patch.h"
#include "vmm_patch_v2.h"
#include "vmm_patch_me1.h"
#include "vmm_patch_me2.h"
#include "vmm_patch_old.h"
#include "vmm_patch_old_v2.h"
#include "vmm_patch_simple.h"
#include "vmm_patch_simple_v2.h"
#include "cpuspeed_patch_v1.h"
#include "cpuspeed_patch_v2.h"
#include "cpuspeed_patch_v3.h"
#include "cpuspeed_patch_v4.h"
#include "cpuspeed_patch_v5.h"
#include "cpuspeed_patch_v6.h"
#include "cpuspeed_patch_v7.h"
#include "cpuspeed_patch_v8.h"
#include "cpuspeed_ndis_patch_v1.h"
#include "cpuspeed_ndis_patch_v2.h"
#include "cpuspeed_ndis_patch_v3.h"
typedef struct _ppatch_t
{
int id;
const char *name;
const uint8_t *patch_data;
size_t patch_size;
const uint8_t *orig_data;
size_t orig_size;
const uint8_t *check_data;
size_t check_size;
const uint8_t *modif_data;
size_t modif_size;
} ppatch_t;
#define PPATCH_FILL(_name) _name, sizeof(_name), \
_name##_orig, sizeof(_name##_orig), \
_name##_orig_check, sizeof(_name##_orig_check), \
_name##_modif, sizeof(_name##_modif)
ppatch_t ppathes[] = {
{PATCH_VMM98, "W98 TLB patch #1 (for SE, updated)", PPATCH_FILL(vmm_patch)},
{PATCH_VMM98_V2, "W98 TLB patch #2 (for SE, Q242161, Q288430)", PPATCH_FILL(vmm_patch_v2)},
{PATCH_VMMME, "WinMe TLB patch", PPATCH_FILL(vmm_patch_me1)},
/* ^ only first part, for this case is there special function */
{PATCH_CPU_SPEED_V1, "CPU Speed #1 (1 000 000 LOOPs)", PPATCH_FILL(cpuspeed_patch_v1)},
{PATCH_CPU_SPEED_V2, "CPU Speed #2 (2 000 000 LOOPs)", PPATCH_FILL(cpuspeed_patch_v2)},
{PATCH_CPU_SPEED_V3, "CPU Speed #3 (10 000 000 LOOPs, FIX95)", PPATCH_FILL(cpuspeed_patch_v3)},
{PATCH_CPU_SPEED_V4, "CPU Speed #4 (10 000 000 LOOPs, rloew's patch)", PPATCH_FILL(cpuspeed_patch_v4)},
{PATCH_CPU_SPEED_V5, "CPU Speed #5 (1 000 000 LOOPs)", PPATCH_FILL(cpuspeed_patch_v5)},
{PATCH_CPU_SPEED_V6, "CPU Speed #6 (2 000 000 LOOPs)", PPATCH_FILL(cpuspeed_patch_v6)},
{PATCH_CPU_SPEED_V7, "CPU Speed #7 (10 000 000 LOOPs, FIX95)", PPATCH_FILL(cpuspeed_patch_v7)},
{PATCH_CPU_SPEED_V8, "CPU Speed #8 (10 000 000 LOOPs, rloew's patch)", PPATCH_FILL(cpuspeed_patch_v8)},
{PATCH_CPU_SPEED_NDIS_V1, "CPU Speed NDIS.VXD #1 (1 048 576 LOOPs, W95+W98FE)", PPATCH_FILL(cpuspeed_ndis_patch_v1)},
{PATCH_CPU_SPEED_NDIS_V2, "CPU Speed NDIS.VXD #2 (1 048 576 LOOPs, W98SE)", PPATCH_FILL(cpuspeed_ndis_patch_v2)},
{PATCH_CPU_SPEED_NDIS_V3, "CPU Speed NDIS.VXD #3 (10 485 760, LOOPs, rloew's patch)", PPATCH_FILL(cpuspeed_ndis_patch_v3)},
{PATCH_VMM98_OLD, "W98 TLB patch #1 UPGRADE", PPATCH_FILL(vmm_patch_old)},
{PATCH_VMM98_OLD_V2, "W98 TLB patch #2 UPGRADE", PPATCH_FILL(vmm_patch_old_v2)},
{PATCH_VMM98_SIMPLE, "W98 TLB patch #1 (simple version)", PPATCH_FILL(vmm_patch_simple)},
{PATCH_VMM98_SIMPLE_V2, "W98 TLB patch #2 (simple version, Q242161, Q288430)", PPATCH_FILL(vmm_patch_simple_v2)},
{0, NULL, NULL, 0, NULL, 0, NULL, 0, NULL, 0}
};
/* special case for ME patch */
static int patch_select_me(FILE *fp, const char *dstfile, int *file_copied, uint32_t *applied, uint32_t *exists);
/**
* Apply selected set of patches
*
* @param fp: pointer to original file, source must support binary read and seeking
* @param dstfile: file name of destination, destination will be overwriten
* @param to_apply: constants PATCH_* from above, use binary OR for specifing multiple values
* @param out_applied: if is not NULL, it will be filled with PATCH_* constants which indicated successfull patching
* @param out_exists: if is not NULL, it will be filled with PATCH_* constants which indicated already patched original file
*
* @return: PATCH_OK on success (at last one patch will be applied) or one of PATCH_E_* codes
*
**/
int patch_selected(FILE *fp, const char *dstfile, uint32_t to_apply, uint32_t *out_applied, uint32_t *out_exists)
{
int status = PATCH_OK;
ppatch_t *patch;
uint32_t applied = 0;
uint32_t exists = 0;
int file_copied = 0;
for(patch = ppathes; patch->patch_data != NULL; patch++)
{
//printf("patch: %X\n", patch->id);
if((to_apply & patch->id) != 0 && patch->id == PATCH_VMMME)
{
status = patch_select_me(fp, dstfile, &file_copied, &applied, &exists);
}
else if((to_apply & patch->id) != 0)
{
ssize_t pos;
bitstream_t bs_check, bs_patch;
bs_mem(&bs_check, (uint8_t*)patch->check_data, patch->check_size);
/* if already applied updated patch ignore simple version */
if((applied & (PATCH_VMM98 | PATCH_VMM98_V2)) != 0)
{
if(patch->id == PATCH_VMM98_SIMPLE || patch->id == PATCH_VMM98_SIMPLE_V2)
{
continue;
}
}
fseek(fp, 0, SEEK_SET);
pos = search_sieve_file(fp, patch->orig_data, patch->orig_size, &bs_check);
if(pos >= 0)
{
applied |= patch->id;
if((to_apply & PATCH_DRY) == 0)
{
FILE *fw;
if(file_copied == 0)
{
fw = FOPEN_LOG(dstfile, "wb");
fseek(fp, 0, SEEK_SET);
fs_file_copy(fp, fw, 0);
file_copied = 1;
}
else
{
fw = FOPEN_LOG(dstfile, "r+b");
}
if(fw != NULL)
{
void *buf = malloc(patch->patch_size);
if(buf != NULL)
{
do
{
fseek(fp, pos, SEEK_SET);
if(fread(buf, 1, patch->patch_size, fp) == patch->patch_size)
{
bs_mem(&bs_patch, (uint8_t*)patch->modif_data, patch->modif_size);
patch_sieve(buf, patch->patch_data, patch->patch_size, &bs_patch);
fseek(fw, pos, SEEK_SET);
fwrite(buf, 1, patch->patch_size, fw);
}
else
{
status = PATCH_E_READ;
}
/* some files has to be patched more times */
bs_reset(&bs_check);
pos = search_sieve_file(fp, patch->orig_data, patch->orig_size, &bs_check);
//printf("another pos: %zd\n", pos);
} while(pos >= 0);
free(buf);
}
else
{
status = PATCH_E_MEM;
}
fclose(fw);
}
else
{
status = PATCH_E_WRITE;
}
} // ! dry run
} //
else
{
/* original data not found, but we'll is pach isn't applied */
fseek(fp, 0, SEEK_SET);
bs_reset(&bs_check);
pos = search_sieve_file(fp, patch->patch_data, patch->patch_size, &bs_check);
if(pos >= 0)
{
exists |= patch->id;
}
}
} // if apply
} // for patch in patches
if(out_applied != NULL)
{
*out_applied = applied;
}
if(out_exists != NULL)
{
*out_exists = exists;
}
if(status == PATCH_OK)
{
if(applied == 0)
{
if(exists == 0)
{
return PATCH_E_CHECK;
}
else
{
return PATCH_E_PATCHED;
}
}
}
return status;
}
#define ME_BLOCK_DISTANCE 16148
#define SIZEOF_MAX(_a, _b) (sizeof(_a) > sizeof(_b) ? sizeof(_a) : sizeof(_b))
static int patch_select_me(FILE *fp, const char *dstfile, int *file_copied, uint32_t *applied, uint32_t *exists)
{
int status = PATCH_OK;
ssize_t pos_a, pos_b;
bitstream_t bs_check, bs_patch;
fseek(fp, 0, SEEK_SET);
bs_mem(&bs_check, (uint8_t*)vmm_patch_me1_orig_check, sizeof(vmm_patch_me1_orig_check));
pos_a = search_sieve_file(fp, vmm_patch_me1_orig, sizeof(vmm_patch_me1_orig), &bs_check);
fseek(fp, 0, SEEK_SET);
bs_mem(&bs_check, (uint8_t*)vmm_patch_me2_orig_check, sizeof(vmm_patch_me2_orig_check));
pos_b = search_sieve_file(fp, vmm_patch_me2_orig, sizeof(vmm_patch_me2_orig), &bs_check);
if(pos_a >= 0 && (pos_b - pos_a) == ME_BLOCK_DISTANCE)
{
FILE *fw;
if(*file_copied == 0)
{
fw = FOPEN_LOG(dstfile, "wb");
fseek(fp, 0, SEEK_SET);
fs_file_copy(fp, fw, 0);
*file_copied = 1;
}
else
{
fw = FOPEN_LOG(dstfile, "r+b");
}
if(fw != NULL)
{
void *buf = malloc(SIZEOF_MAX(vmm_patch_me1, vmm_patch_me2));
if(buf != NULL)
{
fseek(fp, pos_a, SEEK_SET);
if(fread(buf, 1, sizeof(vmm_patch_me1), fp) == sizeof(vmm_patch_me1))
{
bs_mem(&bs_patch, (uint8_t*)vmm_patch_me1_modif, sizeof(vmm_patch_me1_modif));
patch_sieve(buf, vmm_patch_me1, sizeof(vmm_patch_me1), &bs_patch);
fseek(fw, pos_a, SEEK_SET);
fwrite(buf, 1, sizeof(vmm_patch_me1), fw);
}
else
{
status = PATCH_E_READ;
}
fseek(fp, pos_b, SEEK_SET);
if(fread(buf, 1, sizeof(vmm_patch_me2), fp) == sizeof(vmm_patch_me2))
{
bs_mem(&bs_patch, (uint8_t*)vmm_patch_me2_modif, sizeof(vmm_patch_me2_modif));
patch_sieve(buf, vmm_patch_me2, sizeof(vmm_patch_me2), &bs_patch);
fseek(fw, pos_b, SEEK_SET);
fwrite(buf, 1, sizeof(vmm_patch_me2), fw);
}
else
{
status = PATCH_E_READ;
}
free(buf);
}
else
{
status = PATCH_E_MEM;
}
fclose(fw);
}
else
{
status = PATCH_E_WRITE;
}
if(status == PATCH_OK)
{
*applied |= PATCH_VMMME;
}
}
else
{
fseek(fp, 0, SEEK_SET);
bs_mem(&bs_check, (uint8_t*)vmm_patch_me1_orig_check, sizeof(vmm_patch_me1_orig_check));
pos_a = search_sieve_file(fp, vmm_patch_me1, sizeof(vmm_patch_me1), &bs_check);
if(pos_a >= 0)
{
*exists |= PATCH_VMMME;
}
}
return status;
}
/**
* Apply patch and if check failure check if applied.
*
**/
int patch_apply(const char *srcfile, const char *dstfile, int flags, int *applied)
{
int status = PATCH_E_NOTFOUND;
FILE *fp;
fp = FOPEN_LOG(srcfile, "rb");
if(fp)
{
status = patch_selected(fp, dstfile, flags, NULL, NULL);
fclose(fp);
}
else
{
status = PATCH_E_READ;
}
return status;
}
/**
* Apply patch on W3/W4 file and compress/decompress if it is W4 file
*
**/
int patch_apply_wx(const char *srcfile, const char *dstfile, const char *tmpname, int flags)
{
int status = PATCH_OK;
int w4_decompres = 0;
int is_w3 = 0;
dos_header_t dos;
pe_header_t pe;
pe_w4_t *w4;
pe_w3_t *w3;
FILE *fp;
int t;
fp = FOPEN_LOG(srcfile, "rb");
if(fp)
{
t = pe_read(&dos, &pe, fp);
if(t == PE_W4)
{
w4 = pe_w4_read(&dos, &pe, fp);
if(w4 != NULL)
{
if(pe_w4_to_w3(w4, tmpname) == PE_OK)
{
w4_decompres = 1;
}
else
{
status = PATCH_E_CONVERT;
}
pe_w4_free(w4);
}
else
{
status = PATCH_E_READ;
}
}
else if(t == PE_W3)
{
is_w3 = 1;
}
fclose(fp);
}
if(status == PATCH_OK)
{
if(w4_decompres)
{
status = patch_apply(tmpname, dstfile, flags, NULL);
if((flags & PATCH_FORCE_W3) == 0)
{
fs_rename(dstfile, tmpname);
}
else
{
w4_decompres = 0;
}
}
else
{
if(is_w3 && (flags & PATCH_FORCE_W4) != 0)
{
status = patch_apply(srcfile, tmpname, flags, NULL);
w4_decompres = 1;
}
else
{
status = patch_apply(srcfile, dstfile, flags, NULL);
}
}
}
if(status == PATCH_OK && w4_decompres)
{
fp = FOPEN_LOG(tmpname, "rb");
if(fp)
{
t = pe_read(&dos, &pe, fp);
if(t == PE_W3)
{
w3 = pe_w3_read(&dos, &pe, fp);
if(w3 != NULL)
{
printf("Compressing file, please wait...\n");
if(pe_w3_to_w4(w3, dstfile) != PE_OK)
{
status = PATCH_E_CONVERT;
}
pe_w3_free(w3);
}
else
{
status = PATCH_E_READ;
}
}
else
{
status = PATCH_E_READ;
}
fclose(fp);
}
if(status == PATCH_OK)
{
w4 = NULL;
fp = FOPEN_LOG(dstfile, "rb");
if(fp != NULL)
{
t = pe_read(&dos, &pe, fp);
if(t == PE_W4)
{
w4 = pe_w4_read(&dos, &pe, fp);
if(w4 != NULL)
{
if(pe_w4_check(w4) != PE_OK)
{
printf("Warning: W4 file is not loadable, leaving in W3 format!\n");
pe_w4_free(w4);
w4 = NULL;
fclose(fp);
fp = NULL;
fs_unlink(dstfile);
fs_rename(tmpname, dstfile);
}
else
{
/* file is valid, remove temp */
fs_unlink(tmpname);
}
}
}
else
{
status = PATCH_E_READ;
}
}
else
{
status = PATCH_E_READ;
}
if(w4 != NULL) pe_w4_free(w4);
if(fp != NULL) fclose(fp);
}
}
return status;
}
/**
* Backup file
*
**/
int patch_backup_file(const char *path, int nobackup)
{
FILE *fr, *fw;
int status = -1;
char *backupname;
if(nobackup)
{
return PATCH_OK;
}
backupname = fs_path_get2(path, NULL, "bak");
if(backupname)
{
if(!fs_file_exists(backupname))
{
fr = FOPEN_LOG(path, "rb");
if(fr != NULL)
{
fw = FOPEN_LOG(backupname, "wb");
if(fw != NULL)
{
fs_file_copy(fr, fw, 0);
status = PATCH_OK;
fclose(fw);
}
else
{
status = PATCH_E_WRITE;
}
fclose(fr);
}
else
{
status = PATCH_E_READ;
}
}
else
{
status = PATCH_OK;
}
fs_path_free(backupname);
}
else
{
status = PATCH_E_MEM;
}
return status;
}
void patch_print(uint32_t patches)
{
ppatch_t *patch;
int cnt = 0;
for(patch = ppathes; patch->patch_data != NULL; patch++)
{
if((patch->id & patches) != 0)
{
if(cnt++ > 0)
{
printf(", ");
}
printf("%s", patch->name);
}
}
}