-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdlg_ldop.cpp
375 lines (364 loc) · 12.9 KB
/
dlg_ldop.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
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
/************************************************************************
* dlg_ldopt.cpp *
* Contains the dialog routines for the load file dialogboxes *
************************************************************************/
#include <windows.h>
#include <stdio.h>
#include "resource.h"
#include "exeload.h"
#include "proctab.h"
#include "dasm.h"
#include "disasm.h"
#include "help.h"
#include "debug.h"
/************************************************************************
* forward declarations *
************************************************************************/
BOOL CALLBACK checktypebox(HWND hdwnd,UINT message,WPARAM wParam,LPARAM lParam);
BOOL CALLBACK moreoptions(HWND hdwnd,UINT message,WPARAM wParam,LPARAM lParam);
/************************************************************************
* checktypebox *
* - after a file has been chosen to load and before the file is loaded *
* this is displayed for the user to set options for analysis, file *
* type, etc. *
************************************************************************/
#ifdef __BORLANDC__
#pragma warn -par
#endif
//dialog proc for verifying type and
// initial options
BOOL CALLBACK checktypebox(HWND hdwnd,UINT message,WPARAM wParam,LPARAM lParam)
{ char segtext[20],offstext[20];
static int exetype;
int i;
dword segd;
switch(message)
{ case WM_INITDIALOG:
exetype=floader.getexetype();
i=0;
options.loadaddr.segm=0x1000;
options.loadaddr.offs=0x00;
switch(exetype)
{ case NE_EXE:
SetDlgItemText(hdwnd,IDC_DEFAULTBUTTON,"NE Executable");
CheckDlgButton(hdwnd,IDC_DEFAULTBUTTON,true);
options.processor=PROC_80486;
options.mode16=true;
break;
case COM_EXE:
SetDlgItemText(hdwnd,IDC_DEFAULTBUTTON,"COM File");
CheckDlgButton(hdwnd,IDC_DEFAULTBUTTON,true);
options.processor=PROC_80386;
options.mode16=true;
options.loadaddr.offs=0x100;
break;
case SYS_EXE:
SetDlgItemText(hdwnd,IDC_DEFAULTBUTTON,"SYS File");
CheckDlgButton(hdwnd,IDC_DEFAULTBUTTON,true);
options.processor=PROC_80386;
options.mode16=true;
options.loadaddr.offs=0x00;
break;
case PE_EXE:
SetDlgItemText(hdwnd,IDC_DEFAULTBUTTON,"PE Executable");
CheckDlgButton(hdwnd,IDC_DEFAULTBUTTON,true);
options.processor=PROC_PENTIUM;
options.mode16=false;
break;
case OS2_EXE:
SetDlgItemText(hdwnd,IDC_DEFAULTBUTTON,"OS2 Executable");
CheckDlgButton(hdwnd,IDC_DEFAULTBUTTON,true);
options.processor=PROC_PENTIUM;
options.mode16=false;
break;
case LE_EXE:
SetDlgItemText(hdwnd,IDC_DEFAULTBUTTON,"LE Executable");
CheckDlgButton(hdwnd,IDC_DEFAULTBUTTON,true);
options.processor=PROC_80486;
options.mode16=false;
break;
case MZ_EXE:
SetDlgItemText(hdwnd,IDC_DEFAULTBUTTON,"COM File");
CheckDlgButton(hdwnd,IDC_DOSBUTTON,true);
options.processor=PROC_80386;
options.mode16=true;
break;
default:
case BIN_EXE:
SetDlgItemText(hdwnd,IDC_DEFAULTBUTTON,"COM File");
CheckDlgButton(hdwnd,IDC_BINBUTTON,true);
options.processor=PROC_8086;
options.mode16=true;
break;
}
options.mode32=!options.mode16;
CheckDlgButton(hdwnd,load_debug,options.loaddebug);
CheckDlgButton(hdwnd,demangle_names,options.demangle);
CheckDlgButton(hdwnd,IDC_16DASM,options.mode16);
CheckDlgButton(hdwnd,IDC_32DASM,options.mode32);
CheckDlgButton(hdwnd,IDC_LOADDATA,options.loaddata);
CheckDlgButton(hdwnd,IDC_LOADRESOURCES,options.loadresources);
while(procnames[i].num)
{ SendDlgItemMessage(hdwnd,IDC_LISTBOX1,LB_ADDSTRING,0,(LPARAM) (LPCTSTR)procnames[i].name);
if(options.processor==procnames[i].num)
SendDlgItemMessage(hdwnd,IDC_LISTBOX1,LB_SETCURSEL,i,0);
i++;
}
segd=options.loadaddr.segm;
wsprintf(segtext,"%x",segd);
wsprintf(offstext,"%lx",options.loadaddr.offs);
SendDlgItemMessage(hdwnd,IDC_SEGEDIT,WM_SETTEXT,0,(LPARAM)segtext);
SendDlgItemMessage(hdwnd,IDC_OFFSEDIT,WM_SETTEXT,0,(LPARAM)offstext);
return false;
case WM_COMMAND:
switch(LOWORD(wParam))
{ case IDOK:
if(!IsDlgButtonChecked(hdwnd,IDC_DEFAULTBUTTON))
{ if(IsDlgButtonChecked(hdwnd,IDC_DOSBUTTON))
floader.setexetype(MZ_EXE);
else
floader.setexetype(BIN_EXE);
}
else if((exetype==BIN_EXE)||(exetype==MZ_EXE))
floader.setexetype(COM_EXE);
options.processor=procnames[SendDlgItemMessage(hdwnd,IDC_LISTBOX1,LB_GETCURSEL,0,0)].num;
EndDialog(hdwnd,NULL);
return true;
case IDC_SEGEDIT:
if(HIWORD(wParam)==EN_CHANGE)
{ SendDlgItemMessage(hdwnd,IDC_SEGEDIT,WM_GETTEXT,(WPARAM)18,(LPARAM)segtext);
sscanf(segtext,"%x",&options.loadaddr.segm);
}
return true;
case IDC_OFFSEDIT:
if(HIWORD(wParam)==EN_CHANGE)
{ SendDlgItemMessage(hdwnd,IDC_OFFSEDIT,WM_GETTEXT,(WPARAM)18,(LPARAM)offstext);
sscanf(offstext,"%lx",&options.loadaddr.offs);
}
return true;
case IDC_HELPBUTTON1:
DialogBox(hInst,MAKEINTRESOURCE(HELPDIALOG_1),hdwnd,(DLGPROC)helpbox1);
return true;
case more_options:
DialogBox(hInst,MAKEINTRESOURCE(Advanced_Options),hdwnd,(DLGPROC)moreoptions);
return true;
case load_debug:
options.loaddebug=!options.loaddebug;
CheckDlgButton(hdwnd,load_debug,options.loaddebug);
return true;
case demangle_names:
options.demangle=!options.demangle;
CheckDlgButton(hdwnd,demangle_names,options.demangle);
return true;
case IDC_16DASM:
options.mode16=!options.mode16;
CheckDlgButton(hdwnd,IDC_16DASM,options.mode16);
return true;
case IDC_32DASM:
options.mode32=!options.mode32;
CheckDlgButton(hdwnd,IDC_32DASM,options.mode32);
return true;
case IDC_LOADDATA:
options.loaddata=!options.loaddata;
CheckDlgButton(hdwnd,IDC_LOADDATA,options.loaddata);
return true;
case IDC_LOADRESOURCES:
options.loadresources=!options.loadresources;
CheckDlgButton(hdwnd,IDC_LOADRESOURCES,options.loadresources);
return true;
default:
break;
}
}
return false;
}
#ifdef __BORLANDC__
#pragma warn +par
#endif
/************************************************************************
* moreoptions *
* - advanced loading options *
************************************************************************/
#ifdef __BORLANDC__
#pragma warn -par
#endif
BOOL CALLBACK moreoptions(HWND hdwnd,UINT message,WPARAM wParam,LPARAM lParam)
{ switch(message)
{ case WM_COMMAND:
{ switch(wParam)
{ case IDOK:
options.codedetect=0;
if(IsDlgButtonChecked(hdwnd,advanced_pushbp))
options.codedetect|=CD_PUSHBP;
if(IsDlgButtonChecked(hdwnd,advanced_aggressive))
options.codedetect|=CD_AGGRESSIVE;
if(IsDlgButtonChecked(hdwnd,advanced_enter))
options.codedetect|=CD_ENTER;
if(IsDlgButtonChecked(hdwnd,advanced_movbx))
options.codedetect|=CD_MOVBX;
if(IsDlgButtonChecked(hdwnd,advanced_moveax))
options.codedetect|=CD_MOVEAX;
if(IsDlgButtonChecked(hdwnd,advanced_eaxfromesp))
options.codedetect|=CD_EAXFROMESP;
EndDialog(hdwnd,NULL);
return true;
default:
break;
}
}
break;
case WM_INITDIALOG:
CheckDlgButton(hdwnd,advanced_pushbp,options.codedetect&CD_PUSHBP);
CheckDlgButton(hdwnd,advanced_aggressive,options.codedetect&CD_AGGRESSIVE);
CheckDlgButton(hdwnd,advanced_enter,options.codedetect&CD_ENTER);
CheckDlgButton(hdwnd,advanced_movbx,options.codedetect&CD_MOVBX);
CheckDlgButton(hdwnd,advanced_moveax,options.codedetect&CD_MOVEAX);
CheckDlgButton(hdwnd,advanced_eaxfromesp,options.codedetect&CD_EAXFROMESP);
return false;
default:
break;
}
return false;
}
#ifdef __BORLANDC__
#pragma warn +par
#endif
/************************************************************************
* load *
* - checks file header info, identifies the possible types of files, *
* gets the users file loading options and calls the specific exe *
* format loading routines. *
************************************************************************/
bool loadexefile(char *fname)
{ char mzhead[2],exthead[2];
dword pe_offset;
dword num;
dword fsize;
if(floader.efile!=INVALID_HANDLE_VALUE)
return false;
// just grab the file size first
floader.efile=CreateFile(fname,GENERIC_READ,1,NULL,OPEN_EXISTING,0,NULL);
fsize=GetFileSize(floader.efile,NULL);
CloseHandle(floader.efile);
if(!fsize)
{ MessageBox(mainwindow,"File appears to be of zero length ???",
"Borg Message",MB_OK);
return false;
}
floader.efile=CreateFile(fname,GENERIC_READ|GENERIC_WRITE,1,NULL,OPEN_EXISTING,0,NULL);
if(floader.efile==INVALID_HANDLE_VALUE)
{ floader.efile=CreateFile(fname,GENERIC_READ,1,NULL,OPEN_EXISTING,0,NULL);
if(floader.efile==INVALID_HANDLE_VALUE)
return false;
options.readonly=true;
MessageBox(mainwindow,"Couldn't obtain write permission to file\nFile opened readonly - will not be able to apply any patches",
"Borg Message",MB_OK);
}
if(GetFileType(floader.efile)!=FILE_TYPE_DISK)
return false;
floader.exetype=BIN_EXE;
if(ReadFile(floader.efile,mzhead,2,&num,NULL))
{ if((num==2)&&(((mzhead[0]=='M')&&(mzhead[1]=='Z'))||
((mzhead[0]=='Z')&&(mzhead[1]=='M'))))
{ SetFilePointer(floader.efile,0x3c,NULL,FILE_BEGIN);
if(ReadFile(floader.efile,&pe_offset,4,&num,NULL))
SetFilePointer(floader.efile,pe_offset,NULL,FILE_BEGIN);
if(ReadFile(floader.efile,exthead,2,&num,NULL))
{ if(((short int *)exthead)[0]==0x4550)
floader.exetype=PE_EXE;
else if(((short int *)exthead)[0]==0x454e)
floader.exetype=NE_EXE;
else if(((short int *)exthead)[0]==0x454c)
floader.exetype=LE_EXE;
else if(((short int *)exthead)[0]==0x584c)
floader.exetype=OS2_EXE;
else
floader.exetype=MZ_EXE;
}
}
else
{ if(strlen(fname)>3)
{ if(!lstrcmpi(fname+strlen(fname)-3,"com"))
{ SetFilePointer(floader.efile,0,NULL,FILE_BEGIN);
floader.exetype=COM_EXE;
}
else if(!lstrcmpi(fname+strlen(fname)-3,"sys"))
{ SetFilePointer(floader.efile,0,NULL,FILE_BEGIN);
floader.exetype=SYS_EXE;
}
}
}
}
floader.fbuff=new byte[fsize];
SetFilePointer(floader.efile,0x00,NULL,FILE_BEGIN);
ReadFile(floader.efile,floader.fbuff,fsize,&num,NULL);
DialogBox(hInst,MAKEINTRESOURCE(D_checktype),mainwindow,(DLGPROC)checktypebox);
if(!options.loadaddr.segm)
{ options.loadaddr.segm=0x1000;
MessageBox(mainwindow,"Sorry - Can't use a zero segment base.\nSegment Base has been set to 0x1000"
,"Borg Message",MB_OK);
}
dsm.dissettable();
switch(floader.exetype)
{ case BIN_EXE:
floader.readbinfile(fsize);
break;
case PE_EXE:
floader.readpefile(pe_offset);
break;
case MZ_EXE:
floader.readmzfile(fsize);
break;
case OS2_EXE:
floader.reados2file();
CloseHandle(floader.efile);
floader.efile=INVALID_HANDLE_VALUE;
floader.exetype=0;
return false; // at the moment;
case COM_EXE:
floader.readcomfile(fsize);
break;
case SYS_EXE:
floader.readsysfile(fsize);
break;
case LE_EXE:
floader.readlefile();
CloseHandle(floader.efile);
floader.efile=INVALID_HANDLE_VALUE;
floader.exetype=0;
return false; // at the moment;
case NE_EXE:
floader.readnefile(pe_offset);
break;
default:
CloseHandle(floader.efile);
floader.efile=INVALID_HANDLE_VALUE;
floader.exetype=0;
return false;
}
return true;
}
/************************************************************************
* newfile *
* - handles selecting a new file and its messages, using the standard *
* routine GetOpenFileName *
* - starts up the secondary thread when the file is loaded *
************************************************************************/
bool newfile(void)
{ // factor of 2 added for nt unicode
getfiletoload(current_exe_name);
if(current_exe_name[0])
{ if(loadexefile(current_exe_name))
{ StatusMessage("File Opened");
strcat(winname," : ");
strcat(winname,current_exe_name);
SetWindowText(mainwindow,winname);
InThread=true;
ThreadHandle=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Thread,0,0,&ThreadId);
changemenus();
}
else
MessageBox(mainwindow,"File open failed ?",program_name,MB_OK|MB_ICONEXCLAMATION);
}
return 0;
}