Skip to content

Commit

Permalink
Changed the hook search method from binary to linear.
Browse files Browse the repository at this point in the history
(It's faster maybe due to the amount of memmove.)
  • Loading branch information
TsudaKageyu committed Jul 20, 2014
1 parent a1bcc24 commit b16c0da
Showing 1 changed file with 14 additions and 40 deletions.
54 changes: 14 additions & 40 deletions src/hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,37 +113,21 @@ struct
} g_threads;

//-------------------------------------------------------------------------
// returns: >= 0 if the element in contained, < 0 (-insertPos-1) if not
// Returns UINT_MAX if not found.
static UINT FindHookEntry(LPVOID pTarget)
{
INT left, right;

if (g_hooks.size == 0)
return UINT_MAX;

// Binary search through the list
left = 0;
right = g_hooks.size - 1;

do
UINT i;
for (i = 0; i < g_hooks.size; ++i)
{
INT center = (left + right) / 2;

if ((ULONG_PTR)g_hooks.pItems[center].pTarget == (ULONG_PTR)pTarget)
return center; // found

if ((ULONG_PTR)g_hooks.pItems[center].pTarget < (ULONG_PTR)pTarget)
left = center + 1; // continue right
else
right = center - 1; // continue left

} while (left <= right);
if ((ULONG_PTR)pTarget == (ULONG_PTR)g_hooks.pItems[i].pTarget)
return i;
}

return (left ^ UINT_MAX); // not found, return insert position
return UINT_MAX;
}

//-------------------------------------------------------------------------
static PHOOK_ENTRY NewHookEntry(UINT pos)
static PHOOK_ENTRY NewHookEntry()
{
if (g_hooks.pItems == NULL)
{
Expand All @@ -165,17 +149,7 @@ static PHOOK_ENTRY NewHookEntry(UINT pos)
g_hooks.pItems = p;
}

// Add the element at the correct position
if (pos < g_hooks.size)
{
RtlMoveMemory(
&g_hooks.pItems[pos + 1],
&g_hooks.pItems[pos],
(g_hooks.size - pos) * sizeof(HOOK_ENTRY));
}

g_hooks.size++;
return &g_hooks.pItems[pos];
return &g_hooks.pItems[g_hooks.size++];
}

//-------------------------------------------------------------------------
Expand Down Expand Up @@ -549,7 +523,7 @@ MH_STATUS WINAPI MH_CreateHook(LPVOID pTarget, LPVOID pDetour, LPVOID *ppOrigina
return MH_ERROR_NOT_EXECUTABLE;

pos = FindHookEntry(pTarget);
if (!(pos >> 31))
if (pos != UINT_MAX)
return MH_ERROR_ALREADY_CREATED;

pBuffer = AllocateBuffer(pTarget);
Expand All @@ -572,7 +546,7 @@ MH_STATUS WINAPI MH_CreateHook(LPVOID pTarget, LPVOID pDetour, LPVOID *ppOrigina
return MH_ERROR_UNSUPPORTED_FUNCTION;
}

pHook = NewHookEntry(pos ^ UINT_MAX);
pHook = NewHookEntry();
if (pHook == NULL)
{
FreeBuffer(pBuffer);
Expand Down Expand Up @@ -634,7 +608,7 @@ MH_STATUS WINAPI MH_RemoveHook(LPVOID pTarget)
return MH_ERROR_NOT_INITIALIZED;

pos = FindHookEntry(pTarget);
if (pos >> 31)
if (pos == UINT_MAX)
return MH_ERROR_NOT_CREATED;

if (g_hooks.pItems[pos].isEnabled)
Expand Down Expand Up @@ -679,7 +653,7 @@ static MH_STATUS EnableHook(LPVOID pTarget, BOOL enable)
else
{
UINT pos = FindHookEntry(pTarget);
if (pos >> 31)
if (pos == UINT_MAX)
return MH_ERROR_NOT_CREATED;

if (g_hooks.pItems[pos].isEnabled == enable)
Expand Down Expand Up @@ -732,7 +706,7 @@ static MH_STATUS QueueHook(LPVOID pTarget, BOOL queueEnable)
else
{
UINT pos = FindHookEntry(pTarget);
if (pos >> 31)
if (pos == UINT_MAX)
return MH_ERROR_NOT_CREATED;

g_hooks.pItems[pos].queueEnable = queueEnable;
Expand Down

0 comments on commit b16c0da

Please # to comment.