diff options
-rw-r--r-- | subhook.c | 14 | ||||
-rw-r--r-- | subhook_private.h | 3 | ||||
-rw-r--r-- | subhook_x86.c | 17 |
3 files changed, 26 insertions, 8 deletions
@@ -29,11 +29,21 @@ #include "subhook_private.h" SUBHOOK_EXPORT struct subhook *SUBHOOK_API subhook_new() { - return (struct subhook *)calloc(1, sizeof(struct subhook)); + struct subhook *hook; + + if ((hook = (struct subhook *)calloc(1, sizeof(struct subhook))) == NULL) + return NULL; + + if (subhook_arch_new(hook) < 0) { + free(hook); + return NULL; + } + + return hook; } SUBHOOK_EXPORT void SUBHOOK_API subhook_free(struct subhook *hook) { - free(hook->arch); + subhook_arch_free(hook); free(hook); } diff --git a/subhook_private.h b/subhook_private.h index 1de11cf..bd66e2f 100644 --- a/subhook_private.h +++ b/subhook_private.h @@ -33,4 +33,7 @@ struct subhook { void *arch; /* architecture-specific information */ }; +int subhook_arch_new(struct subhook *hook); +void subhook_arch_free(struct subhook *hook); + #endif /* SUBHOOK_PRIVATE_H */ diff --git a/subhook_x86.c b/subhook_x86.c index a040cb3..a838ed8 100644 --- a/subhook_x86.c +++ b/subhook_x86.c @@ -38,6 +38,17 @@ struct subhook_x86 { unsigned char code[SUBHOOK_JUMP_SIZE]; }; +int subhook_arch_new(struct subhook *hook) { + if ((hook->arch = malloc(sizeof(struct subhook_x86))) == NULL) + return -ENOMEM; + + return 0; +} + +void subhook_arch_free(struct subhook *hook) { + free(hook->arch); +} + SUBHOOK_EXPORT int SUBHOOK_API subhook_install(struct subhook *hook) { static const unsigned char jmp = 0xE9; void *src, *dst; @@ -49,12 +60,6 @@ SUBHOOK_EXPORT int SUBHOOK_API subhook_install(struct subhook *hook) { src = subhook_get_source(hook); dst = subhook_get_destination(hook); - /* allocate machine-specific data on frist install */ - if (hook->arch == NULL) { - if ((hook->arch = malloc(sizeof(struct subhook_x86))) == NULL) - return -ENOMEM; - } - subhook_unprotect(src, SUBHOOK_JUMP_SIZE); memcpy(((struct subhook_x86 *)hook->arch)->code, src, SUBHOOK_JUMP_SIZE); |