aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZeex <zeex@rocketmail.com>2013-01-15 03:57:07 +0700
committerZeex <zeex@rocketmail.com>2013-01-15 03:57:07 +0700
commit75f746195d54a90480431c42b126bb9be952bfd8 (patch)
tree192b9ff0a516aaebd1b7e4d33b8bee2710fca31b
parent8347e1a37008e2ae73e97080c4e8455fd2213815 (diff)
downloadsubhook-75f746195d54a90480431c42b126bb9be952bfd8.zip
subhook-75f746195d54a90480431c42b126bb9be952bfd8.tar.gz
subhook-75f746195d54a90480431c42b126bb9be952bfd8.tar.bz2
Move arch initialization/cleanup to subhook_arch_{new,free}
-rw-r--r--subhook.c14
-rw-r--r--subhook_private.h3
-rw-r--r--subhook_x86.c17
3 files changed, 26 insertions, 8 deletions
diff --git a/subhook.c b/subhook.c
index 6c23b17..9a96ea5 100644
--- a/subhook.c
+++ b/subhook.c
@@ -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);