forked from crypto2011/IDR
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKnowledgeBase.h
More file actions
384 lines (349 loc) · 14.2 KB
/
KnowledgeBase.h
File metadata and controls
384 lines (349 loc) · 14.2 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//---------------------------------------------------------------------------
#ifndef KnowledgeBaseH
#define KnowledgeBaseH
//---------------------------------------------------------------------------
#include <stdio.h>
//---------------------------------------------------------------------------
// Информация о смещениях имен и данных | Information about name and data offsets
typedef struct {
DWORD Offset;
DWORD Size;
int ModId; // Modules
int NamId; // Names
} OFFSETSINFO, *POFFSETSINFO;
//Fixup info
typedef struct {
BYTE Type; // 'A' - ADR, 'J' - JMP, 'D' - DAT
DWORD Ofs; // Смещение относительно начала дампа | Offset relative to the start of the dump
char *Name;
} FIXUPINFO, *PFIXUPINFO;
/*
ModuleDataTable
---------------
//Состоит из ModuleCount записей вида
WORD ID;
PSTR ModuleName;
PSTR Filename;
WORD UsesNum;
WORD UsesID[UsesNum]; //Массив идентификаторов модулей
PSTR UsesNames[UsesNum]; //Массив имен модулей
ConstDataTable
--------------
//Состоит из ModuleCount записей вида
WORD ModuleID;
PSTR ConstName;
BYTE Type; //'C'-ConstDecl, 'P'-PDecl (VMT), 'V'-VarCDecl
PSTR TypeDef; //Тип
PSTR Value; //Значение
DWORD DumpTotal; //Общий размер дампа (дамп+релоки+фиксапы)
DWORD DumpSize; //Размер бинарного дампа (RTTI)
DWORD FixupNum; //Количество фиксапов дампа
BYTE Dump[DumpSize]; //Бинарный дамп (RTTI)
BYTE Relocs[DumpSize];
FIXUPINFO Fixups[FixupNum]; //Массив фиксапов
TypeDataTable
-------------
//Состоит из TypeCount записей вида
DWORD Size; //Size of Type
WORD ModuleID;
PSTR TypeName;
BYTE Kind; //drArrayDef,...,drVariantDef (см. начало)
DWORD VMCnt; //Количество элементов VMT (начиная с 0)
PSTR Decl; //Декларация
DWORD DumpTotal; //Общий размер дампа (дамп+релоки+фиксапы)
DWORD DumpSize; //Размер бинарного дампа (RTTI)
DWORD FixupNum; //Количество фиксапов дампа
BYTE Dump[DumpSize]; //Бинарный дамп (RTTI)
BYTE Relocs[DumpSize];
FIXUPINFO Fixups[FixupNum]; //Фиксапы
DWORD FieldsTotal; //Общий размер данных полей
WORD FieldsNum; //Количество полей (class, interface, record)
FIELDINFO Fields[FieldNum]; //Поля
DWORD PropsTotal; //Общий размер данных свойств
WORD PropsNum; //Количество свойств (class, interface)
PROPERTYINFO Props[PropNum]; //Свойства
DWORD MethodsTotal; //Общий размер данных методов
WORD MethodsNum; //Количество методов (class, interface)
METHODINFO Methods[MethodNum]; //Методы
VarDataTable
------------
//Состоит из VarCount записей вида
WORD ModuleID;
PSTR VarName;
BYTE Type; //'V'-Var;'A'-AbsVar;'S'-SpecVar;'T'-ThreadVar
PSTR TypeDef;
PSTR AbsName; //Для ключевого слова absolute
ResStrDataTable
---------------
//Состоит из ResStrCount записей вида
WORD ModuleID;
PSTR ResStrName;
PSTR TypeDef;
PSTR Context;
ProcDataTable
-------------
//Contains ProcCount structures:
WORD ModuleID;
PSTR ProcName;
BYTE Embedded; //Contains embedded procs
BYTE DumpType; //'C' - code, 'D' - data
BYTE MethodKind; //'M'-method,'P'-procedure,'F'-function,'C'-constructor,'D'-destructor
BYTE CallKind; //1-'cdecl', 2-'pascal', 3-'stdcall', 4-'safecall'
int VProc; //Flag for "overload" (if Delphi version > verD3 and VProc&0x1000 != 0)
PSTR TypeDef; //Type of Result for function
DWORD DumpTotal; //Total size of dump (dump+relocs+fixups)
DWORD DumpSz; //Dump size
DWORD FixupNum; //Dump fixups number
BYTE Dump[DumpSz]; //Binary dump
BYTE Relocs[DumpSize];
FIXUPINFO Fixups[FixupNum]; //Fixups
DWORD ArgsTotal; //Total size of arguments
WORD ArgsNum; //Arguments number
ARGINFO Args[ArgNum]; //Arguments
DWORD LocalsTotal; //Total size of local vars
WORD LocalsNum; //Local vars number
LOCALINFO Locals[LocalNum]; //Local vars
*/
#define SCOPE_TMP 32 // Temp struct FIELDINFO, to be deleted
typedef struct FIELDINFO {
FIELDINFO() : xrefs(0) {}
~FIELDINFO();
BYTE Scope; // 9-private, 10-protected, 11-public, 12-published
int Offset; // Offset in class instance
int Case; // Case for record (in other cases 0xFFFFFFFF)
String Name; // Field Name
String Type; // Field Type
TList *xrefs; // Xrefs from code
} FIELDINFO, *PFIELDINFO;
typedef struct {
BYTE Scope; // 9-private, 10-protected, 11-public, 12-published
int Index; // readonly, writeonly в зависимости от установки бит 1 и 2 | depending on the setting of bits 1 and 2
int DispID; // ???
String Name; // Имя свойства | Property name
String TypeDef; // Тип свойства | Property type
String ReadName; // Процедура для чтения свойства или соответствующее поле
String WriteName; // Процедура для записи свойства или соответствующее поле
String StoredName; // Процедура для проверки свойства или соответствующее значение
} PROPINFO, *PPROPINFO;
typedef struct {
BYTE Scope; // 9-private, 10-protected, 11-public, 12-published
BYTE MethodKind; // 'M'-method, 'P'-procedure, 'F'-function, 'C'-constructor, 'D'-destructor
String Prototype; // Prototype full name
} METHODINFO, *PMETHODINFO;
typedef struct {
BYTE Tag; // 0x21-"val", 0x22-"var"
bool Register; // If true - argument is in register, else - in stack
int Ndx; // Register number and offset (XX-number, XXXXXX-offset) (0-EAX, 1-ECX, 2-EDX)
int Size; // Argument Size
String Name; // Argument Name
String TypeDef; // Argument Type
} ARGINFO, *PARGINFO;
typedef struct {
int Ofs; // Offset of local var (from ebp or EP)
int Size; // Size of local var
String Name; // Local var Name
String TypeDef; // Local var Type
} LOCALINFO, *PLOCALINFO;
typedef struct {
char type; // 'C'-call; 'J'-jmp; 'D'-data
DWORD adr; // address of procedure
int offset; // offset in procedure
} XrefRec, *PXrefRec;
//Флажки для заполнения членов классов
#define INFO_DUMP 1
#define INFO_ARGS 2
#define INFO_LOCALS 4
#define INFO_FIELDS 8
#define INFO_PROPS 16
#define INFO_METHODS 32
#define INFO_ABSNAME 64
class MConstInfo {
public:
MConstInfo();
~MConstInfo();
public:
WORD ModuleID;
String ConstName;
BYTE Type; // 'C'-ConstDecl, 'P'-PDecl (VMT), 'V'-VarCDecl
String TypeDef; // Тип | Type
String Value; // Значение
DWORD DumpSz; // Размер бинарного дампа | Binary dump size
DWORD FixupNum; // Количество фиксапов дампа | Number of dump fixups
BYTE *Dump; // Бинарный дамп | Binary dump
};
// Значения байта Kind информации о типе
// Byte type kind information values
#define drArrayDef 0x4C //'L'
#define drClassDef 0x46 //'F'
#define drFileDef 0x4F //'O'
#define drFloatDef 0x49 //'I'
#define drInterfaceDef 0x54 //'T'
#define drObjVMTDef 0x47 //'G'
#define drProcTypeDef 0x48 //'H'
#define drPtrDef 0x45 //'E'
#define drRangeDef 0x44 //'D'
#define drRecDef 0x4D //'M'
#define drSetDef 0x4A //'J'
#define drShortStrDef 0x4B //'K'
#define drStringDef 0x52 //'R'
#define drTextDef 0x50 //'P'
#define drVariantDef 0x53 //'S'
#define drAliasDef 0x41 //'Z'
class MTypeInfo {
public:
MTypeInfo();
~MTypeInfo();
public:
DWORD Size;
WORD ModuleID;
String TypeName;
BYTE Kind; //drArrayDef,...,drVariantDef
WORD VMCnt; //VMT elements number (from 0)
String Decl; //Declaration
DWORD DumpSz; //Binary dump size
DWORD FixupNum; //Binary dump fixup number
BYTE *Dump; //Binary dump
WORD FieldsNum; //Fields number (class, interface, record)
BYTE *Fields;
WORD PropsNum; //Properties number (class, interface)
BYTE *Props;
WORD MethodsNum; //Methods number (class, interface)
BYTE *Methods;
};
//Var Type field
#define VT_VAR 'V'
#define VT_ABSVAR 'A'
#define VT_SPECVAR 'S'
#define VT_THREADVAR 'T'
class MVarInfo {
public:
MVarInfo();
~MVarInfo();
public:
WORD ModuleID;
String VarName;
BYTE Type; //'V'-Var;'A'-AbsVar;'S'-SpecVar;'T'-ThreadVar
String TypeDef;
String AbsName; //Для ключевого слова absolute
};
class MResStrInfo {
public:
MResStrInfo();
~MResStrInfo();
public:
WORD ModuleID;
String ResStrName;
String TypeDef;
//String Context;
};
class MProcInfo {
public:
MProcInfo();
~MProcInfo();
public:
WORD ModuleID;
String ProcName;
bool Embedded; //true-содержит вложенные процедуры
char DumpType; //'C' - код, 'D' - данные
char MethodKind; //'M'-method,'P'-procedure,'F'-function,'C'-constructor,'D'-destructor
BYTE CallKind; //1-'cdecl', 2-'pascal', 3-'stdcall', 4-'safecall'
int VProc; //флажок для "overload" (если версия Дельфи > verD3 и VProc&0x1000 != 0)
String TypeDef; //Тип
DWORD DumpSz; //Размер бинарного дампа
DWORD FixupNum; //Количество фиксапов дампа
BYTE *Dump; //Бинарный дамп (включает в себя собственно дамп, релоки и фиксапы)
WORD ArgsNum; //Количество аргументов процедуры
BYTE *Args; //Список аргументов
//WORD LocalsNum; //Количество локальных переменных процедуры
//BYTE *Locals; //Список локальных переменных
};
//Секции базы знаний
#define KB_NO_SECTION 0
#define KB_CONST_SECTION 1
#define KB_TYPE_SECTION 2
#define KB_VAR_SECTION 4
#define KB_RESSTR_SECTION 8
#define KB_PROC_SECTION 16
class MKnowledgeBase {
public:
MKnowledgeBase();
~MKnowledgeBase();
bool __fastcall Open(char *filename);
void __fastcall Close();
const BYTE * __fastcall GetKBCachePtr(DWORD Offset, DWORD Size);
WORD __fastcall GetModuleID(const char *ModuleName);
String __fastcall GetModuleName(WORD ModuleID);
void __fastcall GetModuleIdsByProcName(const char *ProcName);
int __fastcall GetItemSection(WORD *ModuleIDs, char *ItemName);
int __fastcall GetConstIdx(WORD *ModuleID, const char *ConstName);
int __fastcall GetConstIdxs(const char *ConstName, int *ConstIdx);
int __fastcall GetTypeIdxByModuleIds(WORD *ModuleIDs, const char *TypeName);
int __fastcall GetTypeIdxsByName(const char *TypeName, int *TypeIdx);
int __fastcall GetTypeIdxByUID(char *UID);
int __fastcall GetVarIdx(WORD *ModuleIDs, const char *VarName);
int __fastcall GetResStrIdx(int from, const char *ResStrContext);
int __fastcall GetResStrIdx(WORD ModuleID, const char *ResStrContext);
int __fastcall GetResStrIdx(WORD *ModuleIDs, const char *ResStrName);
int __fastcall GetProcIdx(WORD ModuleID, const char *ProcName);
int __fastcall GetProcIdx(WORD ModuleID, const char *ProcName, BYTE *code);
int __fastcall GetProcIdx(WORD *ModuleIDs, const char *ProcName, BYTE *code);
bool __fastcall GetProcIdxs(WORD ModuleID, int *FirstProcIdx, int *LastProcIdx);
bool __fastcall GetProcIdxs(WORD ModuleID, int *FirstProcIdx, int *LastProcIdx, int *DumpSize);
MConstInfo * __fastcall GetConstInfo(int AConstIdx, DWORD AFlags, MConstInfo *cInfo);
MProcInfo * __fastcall GetProcInfo(const char *ProcName, DWORD AFlags, MProcInfo *pInfo, int *procIdx);
MProcInfo * __fastcall GetProcInfo(int AProcIdx, DWORD AFlags, MProcInfo *pInfo);
MTypeInfo * __fastcall GetTypeInfo(int ATypeIdx, DWORD AFlags, MTypeInfo *tInfo);
MVarInfo * __fastcall GetVarInfo(int AVarIdx, DWORD AFlags, MVarInfo *vInfo);
MResStrInfo * __fastcall GetResStrInfo(int AResStrIdx, DWORD AFlags, MResStrInfo *rsInfo);
int __fastcall ScanCode(BYTE *code, DWORD *CodeFlags, DWORD CodeSz, MProcInfo *pInfo);
WORD * __fastcall GetModuleUses(WORD ModuleID);
int __fastcall GetProcUses(const char *ProcName, WORD *uses);
WORD * __fastcall GetTypeUses(const char *TypeName);
WORD * __fastcall GetConstUses(const char *ConstName);
String __fastcall GetProcPrototype(int ProcIdx);
String __fastcall GetProcPrototype(MProcInfo *pInfo);
bool __fastcall IsUsedProc(int AIdx);
void __fastcall SetUsedProc(int AIdx);
bool __fastcall GetKBProcInfo(String typeName, MProcInfo *procInfo, int *procIdx);
bool __fastcall GetKBTypeInfo(String typeName, MTypeInfo *typeInfo);
bool __fastcall GetKBPropertyInfo(String className, String propName, MTypeInfo *typeInfo);
String __fastcall IsPropFunction(String className, String procName);
DWORD Version;
int ModuleCount;
OFFSETSINFO *ModuleOffsets;
WORD *Mods;
BYTE *UsedProcs;
const OFFSETSINFO *ConstOffsets;
const OFFSETSINFO *TypeOffsets;
const OFFSETSINFO *VarOffsets;
const OFFSETSINFO *ResStrOffsets;
const OFFSETSINFO *ProcOffsets;
private:
bool Inited;
FILE *Handle;
bool __fastcall CheckKBFile();
long SectionsOffset;
//Modules
int MaxModuleDataSize;
//Consts
int ConstCount;
int MaxConstDataSize;
//Types
int TypeCount;
int MaxTypeDataSize;
//Vars
int VarCount;
int MaxVarDataSize;
//ResStr
int ResStrCount;
int MaxResStrDataSize;
//Procs
int MaxProcDataSize;
int ProcCount;
//as temp test (global KB file cache in mem)
const BYTE *KBCache;
long SizeKBFile;
String NameKBFile;
};
//---------------------------------------------------------------------------
#endif