forked from JHRobotics/wine9x
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathddreplacer.c
More file actions
91 lines (74 loc) · 2.12 KB
/
ddreplacer.c
File metadata and controls
91 lines (74 loc) · 2.12 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdint.h>
const char defname[] = "ddraw.dll";
const uint8_t search[] = {
0x44, 0x44, 0x52, 0x41, 0x57, 0x31, 0x36, 0x2E, 0x44, 0x4C, 0x4C, 0x00, 0x44, 0x44, 0x52, 0x41, // ; DDRAW16.DLL.DDRA
0x57, 0x2E, 0x44, 0x4C, 0x4C, 0x00 // ; W.DLL.
};
size_t replace_pos = 12;
int main(int argc, char **argv)
{
int r = -1;
int found = 0;
if(argc < 3)
{
printf("Usage %s <path to ddraw.dll> <new name>\n", argv[0]);
return 0;
}
const char *src = argv[1];
const char *dest = argv[2];
if(strlen(dest) != strlen(defname))
{
fprintf(stderr, "the length of name to replace must match original name, %u (%s) != %u (%s)\n",
strlen(dest), dest, strlen(defname), defname);
return 1;
}
FILE *fr = fopen(src, "rb");
if(fr != NULL)
{
size_t fs = 0;
uint8_t *mem = NULL;
fseek(fr, 0, SEEK_END);
fs = ftell(fr);
fseek(fr, 0, SEEK_SET);
mem = (uint8_t*)malloc(fs);
if(mem != NULL)
{
if(fread(mem, 1, fs, fr) == fs)
{
fclose(fr); // for case, we replacing file
fr = NULL;
size_t i, j;
for(i = 0; i < fs - sizeof(search); i++)
{
if(memcmp(mem+i, search, sizeof(search)) == 0)
{
for(j = 0; j < sizeof(defname)-1; j++)
{
mem[i+replace_pos+j] = toupper(dest[j]);
}
found++;
}
}
if(found > 0)
{
FILE *fw = fopen(dest, "wb");
if(fw)
{
if(fwrite(mem, 1, fs, fw) == fs)
{
r = 0;
} else fprintf(stderr, "Write file failure");
fclose(fw);
} else fprintf(stderr, "Failed open %s for write\n", dest);
} else fprintf(stderr, "String to replace not found! (wrong DLL, or file already patched)\n");
} else fprintf(stderr, "Read file failure\n");
free(mem);
} else fprintf(stderr, "Failed to allocate memory (%u bytes)\n", fs);
if(fr != NULL) fclose(fr);
} else fprintf(stderr, "Failed open %s for read\n", src);
return r;
}