-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch.c
65 lines (59 loc) · 1.74 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
#include "patch.h"
#include "bin.h"
patch_status_t get_patch_status(FILE *f, const patch_t *patch)
{
if (!bin_check(f, patch->offset, patch->match, patch->len)) {
return PATCH_STATUS_ORIGINAL;
}
else if (patch->replace) {
if (!bin_check(f, patch->offset, patch->replace, patch->len)) {
return PATCH_STATUS_PATCHED;
}
} else {
if (!bin_check_nop(f, patch->offset, patch->len)) {
return PATCH_STATUS_PATCHED;
}
}
return PATCH_STATUS_INVALID;
}
void apply_patch(FILE *f, const patch_t *patch)
{
if (patch->replace) {
bin_replace(f, patch->offset, patch->replace, patch->len);
} else {
bin_set_nop(f, patch->offset, patch->len);
}
}
void remove_patch(FILE *f, const patch_t *patch)
{
bin_replace(f, patch->offset, patch->match, patch->len);
}
patch_status_t get_patch_set_status(FILE *f, const patch_t *patch_set)
{
patch_status_t result = PATCH_STATUS_INVALID;
for (int ci = 0; patch_set[ci].match != NULL; ++ci) {
const patch_t *patch = &patch_set[ci];
const patch_status_t status = get_patch_status(f, patch);
if (status == PATCH_STATUS_INVALID) {
return PATCH_STATUS_INVALID;
}
if (result == PATCH_STATUS_INVALID) {
result = status;
} else if (result != status) {
return PATCH_STATUS_INVALID;
}
}
return result;
}
void apply_patch_set(FILE *f, const patch_t *patch_set)
{
for (int ci = 0; patch_set[ci].match != NULL; ++ci) {
apply_patch(f, &patch_set[ci]);
}
}
void remove_patch_set(FILE *f, const patch_t *patch_set)
{
for (int ci = 0; patch_set[ci].match != NULL; ++ci) {
remove_patch(f, &patch_set[ci]);
}
}