-
Notifications
You must be signed in to change notification settings - Fork 12
/
TableSigs.cpp
175 lines (156 loc) · 5.31 KB
/
TableSigs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// TableSigs.cpp: implementation of the TableSigs class.
//
//////////////////////////////////////////////////////////////////////
//Standard Defs
typedef int BOOL;
#define FALSE 0
#define TRUE 1
#define NULL 0
#include <pro.h>
#include <kernwin.hpp>
#include <bytes.hpp>
//#include <funcs.hpp>
#include <lines.hpp>
#include <name.hpp>
#include "TableSigs.h" //Has all the Table signatures stored within it
//////////////////////////////////////////////////////////////////////
// Table Signatures
//////////////////////////////////////////////////////////////////////
#define NUM_DATA_SIGS 3
unsigned char ts_Tablenames[NUM_DATA_SIGS][255] =
{
{"LineariseMAF"},
{"LineariseEGT"},
{"LineariseCTS"}
};
unsigned char ts_Tablecomments[NUM_DATA_SIGS][255] =
{
{"Table to linearise the Hot Wire MAF output"},
{"Table to linearise the EGT output"},
{"Table to linearise the CTS output"}
};
unsigned char ts_sigs[NUM_DATA_SIGS][310] =
{
//Linearise MAF
{0x93,0x05,0x9B,0x05,0xA2,0x05,0xAA,0x05,0xB2,0x05,0xB9,0x05,0xC1,0x05,0xC8,0x05},
//Linearise EGT
{0x12,0x00,0x00,0x00,0x9A,0x19,0x17,0x28,0x39,0x36,0x0B,0x44,0x9D,0x51,0xFA,0x5E},
//Linearise CTS
{0x14,0x00,0x00,0x00,0x24,0x00,0x2C,0x00,0x38,0x00,0x44,0x00,0x58,0x00,0x70,0x00}
};
unsigned int ts_siglen[NUM_DATA_SIGS] = {16, 16, 16};
//////////////////////////////////////////////////////////////////////
//,Construction/Destruction
//////////////////////////////////////////////////////////////////////
TableSigs::TableSigs()
{
}
TableSigs::~TableSigs()
{
}
//////////////////////////////////////////////////////////////////////
// Helpers
//////////////////////////////////////////////////////////////////////
//Finds the given binary string in the given binary. If 0xff then this is don't care
ea_t TableSigs::FindBinaryWithDontCare(uchar* ubinstr,unsigned __int32 nSLength,ea_t eaStartAddress,ea_t eaEndAddr)
{
ea_t eaAddr = BADADDR,eaSearchAddr=0,nIndexA=0,nIndexB=0;
uchar nRead=0;
int iBit = 0;
ulong v=0;
// ea1stAddr = find_binary(ea_t startea,ea_t endea,ubinstr,16,sflag);
// msg("FindBinaryWithDontCare()");
for(nIndexA=eaStartAddress; nIndexA < eaEndAddr;)
{
eaAddr = nIndexA;//Store where we are.
nRead = get_8bit(nIndexA,v,iBit);//Read 8bits but remember that nIndexA is automatically incremented
if (nIndexA==BADADDR)//Have we ran out of bytes?
{
return BADADDR;
}
if (nIndexA>= (eaEndAddr-1))
return BADADDR;
if(nRead == *ubinstr)
{// We're matched for the 1st char,now check the rest
// msg("FindBinaryWithDontCare() found 0x%x at 0x%x with length %lu\n",nRead, nIndexA, nSLength);
for(nIndexB=1; nIndexB < nSLength; nIndexB++)
{
eaSearchAddr= eaAddr + nIndexB;
if(*(ubinstr+nIndexB) != 0xff) //check for don't care flag
{
nRead = get_8bit(eaSearchAddr,v,iBit);
// msg(": found 0x%x\n",nRead);
if (eaSearchAddr==BADADDR)//Have we ran out of bytes?
{
eaAddr=BADADDR;
// msg(" not found, ran beyond binary address space\n");
return BADADDR;
}
if(nRead != *(ubinstr+nIndexB))
{
// msg(" no match.\n");
eaAddr=BADADDR;
break;// No longer matched,exit this for(..) loop
}
else if(nIndexB==(nSLength-1))
{
// msg(" binary string matched at 0x%x.\n",eaAddr);
return eaAddr; // String completely matched
}
}
// else
// msg(": ignoring 0xff\n");
}
}
}
// msg("\n");
return eaAddr;
}
//////////////////////////////////////////////////////////////////////
// Implementation
//////////////////////////////////////////////////////////////////////
//Looks for specific binary patterns and then makes a table and comments it
void TableSigs::FindTablesAndComment(ea_t eaStartAddr, ea_t eaEndAddr)
{
msg("Finding Table signatures....\n");
unsigned int uIndex=0, iTry=0, iTries=0;
ea_t eaAddr;
char ucBuf[255];
for(uIndex=0; uIndex < NUM_DATA_SIGS; uIndex++)
{
msg("\nSearching for %s, len = %lu\n",ts_Tablenames[uIndex], ts_siglen[uIndex]);
for(iTry=eaStartAddr, iTries=0; iTry<eaEndAddr; iTry=eaAddr,iTries++)//We may have duplicates
{
//Search from the last place until the end of the database
eaAddr = FindBinaryWithDontCare(ts_sigs[uIndex], ts_siglen[uIndex], iTry, eaEndAddr);
if (eaAddr != BADADDR)
{
qsnprintf(ucBuf, 40, "%s_%x", (char*)ts_Tablenames[uIndex], eaAddr);//Create a unique name
set_name(eaAddr, ucBuf, SN_PUBLIC);//set the name of the Table
//We now want to comment the Table
// func_t* functTable = get_func(eaAddr); // get a pointer to the Table chunk
// del_func_cmt(functTable,1);//delete the existing comment
// update_func(functTable);
// set_func_cmt(functTable,(char*)ts_Tablecomments[uIndex],1);//Make a repeatable comment
// update_func(functTable);
// msg("Table finishes at: 0x0%x\n", functTable->endEA);
//Try the next set of addresses
// eaAddr=functTable->endEA;// the end of the Table just created.
}
else if(iTries==0)
{
msg(" Try %d, nothing found\n", iTries);
eaAddr+=0x1;//TO DO: Find the length of the Table just created.
break;
}
else
{
msg(" Try %d, no further Tables found\n", iTries);
eaAddr+=0x1;//TO DO: Find the length of the Table just created.
break;
}
// eaAddr+=0x10;//TO DO: Find the length of the Table just created.
// eaAddr=functTable->endEA;
}
}
}