Skip to content

Commit

Permalink
Optionally check a binary hack against an expected sequence of bytes.
Browse files Browse the repository at this point in the history
This can be used to selectively disable certain binary hacks if they would crash a particular variety of a game build.

... well, *actually* this should have been the job of the SHA-256 hash. However, it turns out that certain other patches (*cough*gensokyo.org*cough*) have statically modified the binary to such an extent as to make unpatching their modifications annoying enough.
  • Loading branch information
nmlgc committed Apr 16, 2014
1 parent 5558f7e commit 1377d71
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions thcrap/src/binhack.c
Expand Up @@ -145,14 +145,17 @@ int binhacks_apply(json_t *binhacks)
json_object_foreach(binhacks, key, hack) {
const char *title = json_object_get_string(hack, "title");
const char *code = json_object_get_string(hack, "code");
const char *expected = json_object_get_string(hack, "expected");
// Addresses can be an array, too
json_t *json_addr = json_object_get(hack, "addr");
size_t i;
json_t *addr_val;

// calculated byte size of the hack
size_t asm_size = binhack_calc_size(code);
size_t exp_size = binhack_calc_size(expected);
VLA(BYTE, asm_buf, asm_size);
VLA(BYTE, exp_buf, exp_size);

if(!asm_size) {
continue;
Expand All @@ -171,10 +174,20 @@ int binhacks_apply(json_t *binhacks)
if(binhack_render(asm_buf, addr, code)) {
continue;
}
PatchRegion((void*)addr, NULL, asm_buf, asm_size);
log_printf("OK\n");
if(exp_size > 0 && exp_size != asm_size) {
log_printf("different sizes for expected and new code, skipping verification... ");
exp_size = 0;
} else if(binhack_render(exp_buf, addr, expected)) {
exp_size = 0;
}
log_printf(
PatchRegion((void*)addr, exp_size ? exp_buf : NULL, asm_buf, asm_size)
? "OK\n"
: "expected bytes not matched, skipping...\n"
);
}
VLA_FREE(asm_buf);
VLA_FREE(exp_buf);
}
log_printf("------------------------\n");
return 0;
Expand Down

0 comments on commit 1377d71

Please sign in to comment.