forked from photron/msys_setup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrewritepython.c
87 lines (70 loc) · 1.85 KB
/
rewritepython.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
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <malloc.h>
#include <string.h>
struct rewrite {
const char *from;
const char *to;
size_t length;
};
struct rewrite rewrites[] = {
{ "python26", "python27", 8 },
{ "_26_python_", "_27_python_", 11 }
};
int
main(int argc, char **argv)
{
char *path;
struct stat st;
char *buffer;
FILE *fp;
int i, k;
int rewrites_count = sizeof (rewrites) / sizeof (struct rewrite);
if (argc != 2) {
printf("usage: %s <libvirtmod.pyd>", argv[0]);
return 1;
}
path = argv[1];
if (stat(path, &st) < 0) {
printf("error: could not stat '%s'\n", path);
return 1;
}
buffer = malloc(st.st_size + 1024);
fp = fopen(path, "rb");
if (fp == NULL) {
printf("error: could not open '%s' for reading\n", path);
return 1;
}
if (fread(buffer, 1, st.st_size, fp) != st.st_size) {
fclose(fp);
free(buffer);
printf("error: could not read from '%s'\n", path);
return 1;
}
fclose(fp);
for (i = 0; i < st.st_size - 100; ++i) {
for (k = 0; k < rewrites_count; ++k) {
if (memcmp(buffer + i, rewrites[k].from, rewrites[k].length) == 0) {
printf("rewriting '%s' at 0x%08x\n", rewrites[k].from, i);
memcpy(buffer + i, rewrites[k].to, rewrites[k].length);
i += rewrites[k].length;
}
}
}
fp = fopen(path, "wb");
if (fp == NULL) {
free(buffer);
printf("error: could not open '%s' for writing\n", path);
return 1;
}
if (fwrite(buffer, 1, st.st_size, fp) != st.st_size) {
fclose(fp);
free(buffer);
printf("error: could not write to '%s'\n", path);
return 1;
}
fclose(fp);
free(buffer);
return 0;
}