FireBirdLib - Topfield TMS PVR TAP Programming Library
FindInstructionSequence.c
Go to the documentation of this file.
1#include <stdlib.h>
2#include <string.h>
3#include "libFireBird.h"
4
5dword FindInstructionSequence(char *SearchPattern, char *SearchMask, dword StartAddress, dword EndAddress, int EntryPointOffset, bool SearchForPrevADDIUSP)
6{
8
9 dword SP[50], SM[50];
10 dword i, p, NrOfInstr;
11 bool OK;
12
13 if(!StartAddress || !EndAddress || (strlen(SearchPattern) != strlen(SearchMask)))
14 {
15 TRACEEXIT();
16 return 0;
17 }
18
19 //Copy the hex string into the dword arrays
20 NrOfInstr = 0;
21 for(i = 0; i < strlen(SearchPattern); i += 9)
22 {
23 SP[NrOfInstr] = strtoul(&SearchPattern[i], NULL, 16);
24 SM[NrOfInstr] = strtoul(&SearchMask[i], NULL, 16);
25 SP[NrOfInstr] &= SM[NrOfInstr];
26
27 NrOfInstr++;
28 }
29
30 for(p = StartAddress; p < EndAddress; p += 4)
31 {
32 OK = TRUE;
33 for(i = 0; i < NrOfInstr; i++)
34 {
35 if(((* (dword *) (p + (i << 2))) & SM[i]) != SP[i])
36 {
37 OK = FALSE;
38 break;
39 }
40 }
41
42 if(OK)
43 {
44 if(SearchForPrevADDIUSP)
45 {
46 //FireBird 2006-11-28: also stop on JMP to catch hooked functions
47 while((((*(dword *) p) & 0xffff0000) != 0x27bd0000) && (((*(dword *) p) & CMD_MASK) != JMP_CMD))
48 {
49 p -= 4;
50
51 //ibbi 2007-01-07: at which address should be stopped anyway if there is no PrevADDIUSP?
52 if(p < StartAddress)
53 {
54 TRACEEXIT();
55 return 0;
56 }
57 }
58 }
59
60 TRACEEXIT();
61 return p + (EntryPointOffset << 2);
62 }
63 }
64
65 TRACEEXIT();
66 return 0;
67}
dword FindInstructionSequence(char *SearchPattern, char *SearchMask, dword StartAddress, dword EndAddress, int EntryPointOffset, bool SearchForPrevADDIUSP)
#define TRACEEXIT()
Definition: libFireBird.h:1244
#define TRACEENTER()
Definition: libFireBird.h:1243
#define JMP_CMD
Definition: libFireBird.h:3090
#define CMD_MASK
Definition: libFireBird.h:3087