-
Notifications
You must be signed in to change notification settings - Fork 2
/
Search.cpp
324 lines (310 loc) · 11.5 KB
/
Search.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
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
/************************************************************************
* search.cpp *
* These are the functions which handle searching. I still have more *
* work to do on searching (particularly regarding strings - unicode, *
* etc), but the basic functionality is there. Other ideas for the *
* future include wildcard searching. (Byte wildcard searching would be *
* nice :)) *
************************************************************************/
#include <windows.h>
#include <stdio.h>
#include "dasm.h"
#include "schedule.h"
#include "srch.h"
#include "data.h"
#include "disasm.h"
#include "debug.h"
#include "resource.h"
enum search_type {SEARCH_STR=1,SEARCH_HEX,SEARCH_DEC,SEARCH_BYTES};
#define MAX_SEARCHLEN 200
/************************************************************************
* global variables *
* - these just save the search dialog status, and allow us to fill in *
* the old options in the dialog box, and also do a second search, etc *
************************************************************************/
char oldsearchtext[MAX_SEARCHLEN+1]="";
search_type lastsearchtype=SEARCH_STR;
bool lastfromstart=false;
/************************************************************************
* forward declarations *
************************************************************************/
BOOL CALLBACK searchbox(HWND hdwnd,UINT message,WPARAM wParam,LPARAM lParam);
BOOL CALLBACK searchingbox(HWND hdwnd,UINT message,WPARAM wParam,LPARAM lParam);
/************************************************************************
* searchengine *
* - simply stops the secondary thread, puts up the search dialog box, *
* and restarts the thread again at the end *
************************************************************************/
void searchengine(void)
{ scheduler.stopthread();
DialogBox(hInst,MAKEINTRESOURCE(Search_Dialog),mainwindow,(DLGPROC)searchbox);
scheduler.continuethread();
}
/************************************************************************
* parsestring *
* - parses a string into from the input text into the search array *
************************************************************************/
int parsestring(byte *match,char *srchtext)
{ strcpy((char *)match,srchtext);
return strlen(srchtext);
}
/************************************************************************
* parsehex *
* - parses a string for a hex value and puts it in the search array *
************************************************************************/
int parsehex(byte *match,char *srchtext)
{ int matchlen;
dword mtch;
sscanf(srchtext,"%x",&mtch);
if(mtch<256)
{ matchlen=1;
match[0]=(byte)mtch;
}
else if(mtch<65536)
{ matchlen=2;
match[0]=(byte)(mtch&0xff);
match[1]=(byte)(mtch/0x100);
}
else
{ matchlen=4;
match[0]=(byte)(mtch&0xff);
match[1]=(byte)(mtch/0x100);
match[2]=(byte)(mtch/0x10000);
match[3]=(byte)(mtch/0x1000000);
}
return matchlen;
}
/************************************************************************
* parsedecimal *
* - parses a string for a decimal value and puts it in the search array *
************************************************************************/
int parsedec(byte *match,char *srchtext)
{ int matchlen;
dword mtch;
sscanf(srchtext,"%d",&mtch);
if(mtch<256)
{ matchlen=1;
match[0]=(byte)mtch;
}
else if(mtch<65536)
{ matchlen=2;
match[0]=(byte)(mtch&0xff);
match[1]=(byte)(mtch/0x100);
}
else
{ matchlen=4;
match[0]=(byte)(mtch&0xff);
match[1]=(byte)(mtch/0x100);
match[2]=(byte)(mtch/0x10000);
match[3]=(byte)(mtch/0x1000000);
}
return matchlen;
}
/************************************************************************
* parsebytes *
* - parses a string for a series of bytes and puts them in the search *
* array *
************************************************************************/
int parsebytes(byte *match,char *srchtext)
{ int matchlen,i;
byte tmpbyte;
matchlen=strlen(srchtext)/2;
for(i=0;i<matchlen;i++)
{ if(srchtext[i*2]>='a')
tmpbyte=(byte)((srchtext[i*2]-'a'+10));
else
tmpbyte=(byte)((srchtext[i*2]-'0'));
if(srchtext[i*2+1]>='a')
tmpbyte=(byte)(tmpbyte*16+(srchtext[i*2+1]-'a'+10));
else
tmpbyte=(byte)(tmpbyte*16+(srchtext[i*2+1]-'0'));
match[i]=tmpbyte;
}
return matchlen;
}
/************************************************************************
* dosearch *
* - main search routine ripped from other routines in previous versions *
************************************************************************/
void dosearch(HWND hdwnd,search_type searchtype,bool fromstart,lptr s_seg)
{ bool found; // did we find it ?
int matchlen; // length of string to try and match
int i; // loop counter
HWND sbox; // 'searching' box displayed during search
dsegitem *srchseg; // current segment of search
byte match[MAX_SEARCHLEN+1]; // what we're looking for.... filled in by parse routine
byte *segmtch; // segment data pointer.... want segmtch[x]==match[x]
sbox=CreateDialog(hInst,MAKEINTRESOURCE(S_Box),hdwnd,(DLGPROC)searchingbox);
switch(searchtype)
{ case SEARCH_STR:
matchlen=parsestring(match,oldsearchtext);
break;
case SEARCH_HEX:
matchlen=parsehex(match,oldsearchtext);
break;
case SEARCH_DEC:
matchlen=parsedec(match,oldsearchtext);
break;
case SEARCH_BYTES:
matchlen=parsebytes(match,oldsearchtext);
break;
default:
MessageBox(hdwnd,"Internal Error:Search Unknown Option","Borg",MB_OK);
return;
}
// string->data,
// for each seg do .....
found=false;
if(fromstart)
{ dta.resetiterator();
srchseg=dta.nextiterator();
}
else
{ srchseg=dta.findseg(s_seg);
}
lastfromstart=fromstart;
while(srchseg!=NULL)
{ s_seg.segm=srchseg->addr.segm;
if(fromstart)
s_seg=srchseg->addr;
fromstart=false;
if(s_seg<srchseg->addr)
s_seg=srchseg->addr;
while(s_seg<=srchseg->addr+srchseg->size-matchlen)
{ segmtch=srchseg->data+(s_seg-srchseg->addr);
found=true;
for(i=0;i<matchlen;i++)
{ if(segmtch[i]!=match[i])
{ found=false;
break;
}
}
if(found)
break;
s_seg++;
}
if(found)
break;
s_seg.offs=0;
srchseg=dta.nextiterator();
}
if(found)
scheduler.addtask(user_jumptoaddr,priority_userrequest,s_seg,NULL);
DestroyWindow(sbox);
}
/************************************************************************
* searchmore *
* - Rewritten v2.22...... just calls the search function with search *
* again now. *
************************************************************************/
void searchmore(void)
{ lptr s_seg;
if(!strlen(oldsearchtext))
{ searchengine();
return;
}
scheduler.stopthread();
dio.findcurrentaddr(&s_seg);
s_seg+=dsm.getlength(s_seg);
dosearch(mainwindow,lastsearchtype,false,s_seg);
scheduler.continuethread();
}
/************************************************************************
* searchbox *
* - this is the main search dialog box. *
* - it performs the search when we press ok, and the state of controls *
* is saved to global variables *
* - search function separated out v2.22 *
************************************************************************/
#ifdef __BORLANDC__
#pragma warn -par
#endif
BOOL CALLBACK searchbox(HWND hdwnd,UINT message,WPARAM wParam,LPARAM lParam)
{ search_type searchtype;
lptr s_seg;
bool fromstart; // added plus code, bug fix build 14
switch(message)
{ case WM_COMMAND:
{ switch(wParam)
{ case IDOK:
if(SendDlgItemMessage(hdwnd,search_string,BM_GETCHECK,(WPARAM)0,(LPARAM)0))
searchtype=SEARCH_STR;
else if(SendDlgItemMessage(hdwnd,search_hex,BM_GETCHECK,(WPARAM)0,(LPARAM)0))
searchtype=SEARCH_HEX;
else if(SendDlgItemMessage(hdwnd,search_decimal,BM_GETCHECK,(WPARAM)0,(LPARAM)0))
searchtype=SEARCH_DEC;
else
searchtype=SEARCH_BYTES;
if(SendDlgItemMessage(hdwnd,search_fromstart,BM_GETCHECK,(WPARAM)0,(LPARAM)0))
{ s_seg=options.loadaddr;
fromstart=true;
}
else
{ dio.findcurrentaddr(&s_seg);
s_seg+=dsm.getlength(s_seg);
fromstart=false;
}
SendDlgItemMessage(hdwnd,search_edit,WM_GETTEXT,(WPARAM)18,(LPARAM)oldsearchtext);
dosearch(hdwnd,searchtype,fromstart,s_seg);
lastsearchtype=searchtype;
EndDialog(hdwnd,NULL);
return true;
case IDCANCEL:
EndDialog(hdwnd,NULL);
return true;
default:
break;
}
}
break;
case WM_INITDIALOG:
CenterWindow(hdwnd);
fromstart=lastfromstart;
searchtype=lastsearchtype;
if(searchtype==SEARCH_STR)
SendDlgItemMessage(hdwnd,search_string,BM_SETCHECK,(WPARAM)1,(LPARAM)0);
else if(searchtype==SEARCH_HEX)
SendDlgItemMessage(hdwnd,search_hex,BM_SETCHECK,(WPARAM)1,(LPARAM)0);
else if(searchtype==SEARCH_DEC)
SendDlgItemMessage(hdwnd,search_decimal,BM_SETCHECK,(WPARAM)1,(LPARAM)0);
else
SendDlgItemMessage(hdwnd,search_bytes,BM_SETCHECK,(WPARAM)1,(LPARAM)0);
if(fromstart)
SendDlgItemMessage(hdwnd,search_fromstart,BM_SETCHECK,(WPARAM)1,(LPARAM)0);
else
SendDlgItemMessage(hdwnd,search_fromcurr,BM_SETCHECK,(WPARAM)1,(LPARAM)0);
SendDlgItemMessage(hdwnd,search_edit,WM_SETTEXT,(WPARAM)0,(LPARAM)oldsearchtext);
SetFocus(GetDlgItem(hdwnd,search_edit));
return false;
default:
break;
}
return false;
}
#ifdef __BORLANDC__
#pragma warn +par
#endif
/************************************************************************
* searchingbox *
* - this is simply a small dialog box with only the text message *
* 'searching' which is displayed while the search takes place. Note *
* that while a search is being done we are in the primary thread and *
* that the secondary thread is stopped, so nothing else happens *
* within Borg at all. *
************************************************************************/
#ifdef __BORLANDC__
#pragma warn -par
#endif
BOOL CALLBACK searchingbox(HWND hdwnd,UINT message,WPARAM wParam,LPARAM lParam)
{ switch(message)
{ case WM_INITDIALOG:
CenterWindow(hdwnd);
return false;
default:
break;
}
return false;
}
#ifdef __BORLANDC__
#pragma warn +par
#endif