diff options
author | Zeex <zeex@rocketmail.com> | 2013-12-07 18:12:05 +0700 |
---|---|---|
committer | Zeex <zeex@rocketmail.com> | 2013-12-07 18:12:05 +0700 |
commit | 2aca600fa7cf73fc9179b6ca24fbd399b6d91353 (patch) | |
tree | cb4224c2e1032269980af1684a52c561b589fb61 | |
parent | 88d878cc687dd4fab537b5684823cd77a797c25a (diff) | |
download | subhook-2aca600fa7cf73fc9179b6ca24fbd399b6d91353.zip subhook-2aca600fa7cf73fc9179b6ca24fbd399b6d91353.tar.gz subhook-2aca600fa7cf73fc9179b6ca24fbd399b6d91353.tar.bz2 |
Allocate whole struct in one malloc() call
-rw-r--r-- | subhook.c | 19 | ||||
-rw-r--r-- | subhook_private.h | 4 | ||||
-rw-r--r-- | subhook_x86.c | 19 |
3 files changed, 10 insertions, 32 deletions
@@ -28,25 +28,6 @@ #include "subhook.h" #include "subhook_private.h" -SUBHOOK_EXPORT subhook_t SUBHOOK_API subhook_new() { - subhook_t hook; - - if ((hook = calloc(1, sizeof(*hook))) == NULL) - return NULL; - - if (subhook_arch_new(hook) < 0) { - free(hook); - return NULL; - } - - return hook; -} - -SUBHOOK_EXPORT void SUBHOOK_API subhook_free(subhook_t hook) { - subhook_arch_free(hook); - free(hook); -} - SUBHOOK_EXPORT void *SUBHOOK_API subhook_get_src(subhook_t hook) { return hook->src; } diff --git a/subhook_private.h b/subhook_private.h index 1764d50..c3de967 100644 --- a/subhook_private.h +++ b/subhook_private.h @@ -30,12 +30,8 @@ struct subhook { int installed: 1; void *src; void *dst; - void *arch; /* architecture-specific information */ }; -int subhook_arch_new(subhook_t hook); -void subhook_arch_free(subhook_t hook); - void *subhook_unprotect(void *address, size_t size); #endif /* SUBHOOK_PRIVATE_H */ diff --git a/subhook_x86.c b/subhook_x86.c index ebded5e..991a290 100644 --- a/subhook_x86.c +++ b/subhook_x86.c @@ -47,20 +47,21 @@ static const unsigned char jmp_opcode = JMP_OPCODE; static const unsigned char jmp_instr[] = { JMP_OPCODE, 0x0, 0x0, 0x0, 0x0 }; struct subhook_x86 { + struct subhook _; unsigned char code[sizeof(jmp_instr)]; }; -int subhook_arch_new(subhook_t hook) { - hook->arch = malloc(sizeof(struct subhook_x86)); +SUBHOOK_EXPORT subhook_t SUBHOOK_API subhook_new() { + struct subhook_x86 *hook; - if (hook->arch == NULL) - return -ENOMEM; + if ((hook = calloc(1, sizeof(*hook))) == NULL) + return NULL; - return 0; + return (subhook_t)hook; } -void subhook_arch_free(subhook_t hook) { - free(hook->arch); +SUBHOOK_EXPORT void SUBHOOK_API subhook_free(subhook_t hook) { + free(hook); } SUBHOOK_EXPORT int SUBHOOK_API subhook_install(subhook_t hook) { @@ -75,7 +76,7 @@ SUBHOOK_EXPORT int SUBHOOK_API subhook_install(subhook_t hook) { dst = subhook_get_dst(hook); subhook_unprotect(src, sizeof(jmp_instr)); - memcpy(((struct subhook_x86 *)hook->arch)->code, src, sizeof(jmp_instr)); + memcpy(((struct subhook_x86 *)hook)->code, src, sizeof(jmp_instr)); memcpy(src, &jmp_instr, sizeof(jmp_instr)); offset = (intptr_t)dst - ((intptr_t)src + sizeof(jmp_instr)); @@ -90,7 +91,7 @@ SUBHOOK_EXPORT int SUBHOOK_API subhook_remove(subhook_t hook) { if (!subhook_is_installed(hook)) return -EINVAL; - memcpy(subhook_get_src(hook), ((struct subhook_x86 *)hook->arch)->code, + memcpy(subhook_get_src(hook), ((struct subhook_x86 *)hook)->code, sizeof(jmp_instr)); hook->installed = 0; |