diff options
author | Zeex <zeex@rocketmail.com> | 2013-12-05 02:04:59 +0700 |
---|---|---|
committer | Zeex <zeex@rocketmail.com> | 2013-12-05 02:11:39 +0700 |
commit | bab4da2a39751366b73dae704b7a04425efa7f13 (patch) | |
tree | 7f7a751bf8b6dc35393f4f7c8d0fdf5a575703cd | |
parent | 57712a37181e8430a2a74eece1e9b25019e05843 (diff) | |
download | subhook-bab4da2a39751366b73dae704b7a04425efa7f13.zip subhook-bab4da2a39751366b73dae704b7a04425efa7f13.tar.gz subhook-bab4da2a39751366b73dae704b7a04425efa7f13.tar.bz2 |
Add subhook_t typedef
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | subhook.c | 20 | ||||
-rw-r--r-- | subhook.h | 31 | ||||
-rw-r--r-- | subhook_private.h | 4 | ||||
-rw-r--r-- | subhook_x86.c | 10 | ||||
-rw-r--r-- | test/test.c | 2 |
6 files changed, 38 insertions, 35 deletions
@@ -14,7 +14,7 @@ as `my_foo` (depends on compiler). #include <stdio.h> #include <subhook.h> -struct subhook *foo_hook; +subhook_t *foo_hook; void my_foo(int x) { /* Sometimes you want to call the original function. */ @@ -38,9 +38,9 @@ int main() { subhook_set_dst((void*)my_foo); /* Install our newly created hook so from now on any call to foo() - * will be redirected to my_foo(). */ + * will be redirected to my_foo(). */ subhook_install(foo_hook); - + /* Free the memory when you're done. */ subhook_free(foo_hook); } @@ -28,10 +28,10 @@ #include "subhook.h" #include "subhook_private.h" -SUBHOOK_EXPORT struct subhook *SUBHOOK_API subhook_new() { - struct subhook *hook; - - if ((hook = (struct subhook *)calloc(1, sizeof(struct subhook))) == NULL) +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) { @@ -42,28 +42,28 @@ SUBHOOK_EXPORT struct subhook *SUBHOOK_API subhook_new() { return hook; } -SUBHOOK_EXPORT void SUBHOOK_API subhook_free(struct subhook *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(struct subhook *hook) { +SUBHOOK_EXPORT void *SUBHOOK_API subhook_get_src(subhook_t hook) { return hook->src; } -SUBHOOK_EXPORT void *SUBHOOK_API subhook_get_dst(struct subhook *hook) { +SUBHOOK_EXPORT void *SUBHOOK_API subhook_get_dst(subhook_t hook) { return hook->dst; } -SUBHOOK_EXPORT void SUBHOOK_API subhook_set_src(struct subhook *hook, void *src) { +SUBHOOK_EXPORT void SUBHOOK_API subhook_set_src(subhook_t hook, void *src) { hook->src = src; } -SUBHOOK_EXPORT void SUBHOOK_API subhook_set_dst(struct subhook *hook, void *dst) { +SUBHOOK_EXPORT void SUBHOOK_API subhook_set_dst(subhook_t hook, void *dst) { hook->dst = dst; } -SUBHOOK_EXPORT int SUBHOOK_API subhook_is_installed(struct subhook *hook) { +SUBHOOK_EXPORT int SUBHOOK_API subhook_is_installed(subhook_t hook) { return hook->installed; } @@ -85,21 +85,24 @@ #endif #endif -SUBHOOK_EXPORT struct subhook *SUBHOOK_API subhook_new(); -SUBHOOK_EXPORT void SUBHOOK_API subhook_free(struct subhook *hook); +struct subhook; +typedef struct subhook *subhook_t; -SUBHOOK_EXPORT void SUBHOOK_API subhook_set_src(struct subhook *hook, void *src); -SUBHOOK_EXPORT void *SUBHOOK_API subhook_get_src(struct subhook *hook); +SUBHOOK_EXPORT subhook_t SUBHOOK_API subhook_new(); +SUBHOOK_EXPORT void SUBHOOK_API subhook_free(subhook_t hook); -SUBHOOK_EXPORT void SUBHOOK_API subhook_set_dst(struct subhook *hook, void *dst); -SUBHOOK_EXPORT void *SUBHOOK_API subhook_get_dst(struct subhook *hook); +SUBHOOK_EXPORT void SUBHOOK_API subhook_set_src(subhook_t hook, void *src); +SUBHOOK_EXPORT void *SUBHOOK_API subhook_get_src(subhook_t hook); + +SUBHOOK_EXPORT void SUBHOOK_API subhook_set_dst(subhook_t hook, void *dst); +SUBHOOK_EXPORT void *SUBHOOK_API subhook_get_dst(subhook_t hook); /* These return 0 on failure and 1 on success. */ -SUBHOOK_EXPORT int SUBHOOK_API subhook_install(struct subhook *hook); -SUBHOOK_EXPORT int SUBHOOK_API subhook_remove(struct subhook *hook); +SUBHOOK_EXPORT int SUBHOOK_API subhook_install(subhook_t hook); +SUBHOOK_EXPORT int SUBHOOK_API subhook_remove(subhook_t hook); /* Checks whether the hook is installed. */ -SUBHOOK_EXPORT int SUBHOOK_API subhook_is_installed(struct subhook *hook); +SUBHOOK_EXPORT int SUBHOOK_API subhook_is_installed(subhook_t hook); /* Reads hook destination address from code. * @@ -176,7 +179,7 @@ public: class ScopedRemove { public: - ScopedRemove(SubHook *hook) + ScopedRemove(subhook_t hook) : hook_(hook) , removed_(hook_->Remove()) { @@ -193,13 +196,13 @@ public: void operator=(const ScopedRemove &); private: - SubHook *hook_; + subhook_t hook_; bool removed_; }; class ScopedInstall { public: - ScopedInstall(SubHook *hook) + ScopedInstall(subhook_t hook) : hook_(hook) , installed_(hook_->Install()) { @@ -216,7 +219,7 @@ public: void operator=(const ScopedInstall &); private: - SubHook *hook_; + subhook_t hook_; bool installed_; }; @@ -229,7 +232,7 @@ private: void operator=(const SubHook &); private: - subhook *hook_; + subhook_t hook_; bool installed_; }; diff --git a/subhook_private.h b/subhook_private.h index 9c57bf8..1764d50 100644 --- a/subhook_private.h +++ b/subhook_private.h @@ -33,8 +33,8 @@ struct subhook { void *arch; /* architecture-specific information */ }; -int subhook_arch_new(struct subhook *hook); -void subhook_arch_free(struct subhook *hook); +int subhook_arch_new(subhook_t hook); +void subhook_arch_free(subhook_t hook); void *subhook_unprotect(void *address, size_t size); diff --git a/subhook_x86.c b/subhook_x86.c index fdcfcf7..f106a67 100644 --- a/subhook_x86.c +++ b/subhook_x86.c @@ -47,18 +47,18 @@ struct subhook_x86 { unsigned char code[SUBHOOK_JUMP_SIZE]; }; -int subhook_arch_new(struct subhook *hook) { +int subhook_arch_new(subhook_t hook) { if ((hook->arch = malloc(sizeof(struct subhook_x86))) == NULL) return -ENOMEM; return 0; } -void subhook_arch_free(struct subhook *hook) { +void subhook_arch_free(subhook_t hook) { free(hook->arch); } -SUBHOOK_EXPORT int SUBHOOK_API subhook_install(struct subhook *hook) { +SUBHOOK_EXPORT int SUBHOOK_API subhook_install(subhook_t hook) { static const unsigned char jmp = 0xE9; void *src, *dst; intptr_t offset; @@ -72,7 +72,7 @@ SUBHOOK_EXPORT int SUBHOOK_API subhook_install(struct subhook *hook) { subhook_unprotect(src, SUBHOOK_JUMP_SIZE); memcpy(((struct subhook_x86 *)hook->arch)->code, src, SUBHOOK_JUMP_SIZE); - /* E9 - jump near, relative */ + /* E9 - jump near, relative */ memcpy(src, &jmp, sizeof(jmp)); /* jump address is relative to next instruction */ @@ -84,7 +84,7 @@ SUBHOOK_EXPORT int SUBHOOK_API subhook_install(struct subhook *hook) { return 0; } -SUBHOOK_EXPORT int SUBHOOK_API subhook_remove(struct subhook *hook) { +SUBHOOK_EXPORT int SUBHOOK_API subhook_remove(subhook_t hook) { if (!subhook_is_installed(hook)) return -EINVAL; diff --git a/test/test.c b/test/test.c index 6caa263..58265b0 100644 --- a/test/test.c +++ b/test/test.c @@ -1,7 +1,7 @@ #include <stdio.h> #include <subhook.h> -struct subhook *hfoo, *hbar; +subhook_t hfoo, hbar; void foo() { printf("foo() called\n"); |