Source file: /~heha/hsn/borg.zip/DLG_LDOP.CPP

/************************************************************************
*		   dlg_ldopt.cpp					*
* Contains the dialog routines for the load file dialogboxes		*
************************************************************************/

#include <windows.h>
#include <stdio.h>		//sscanf...


#include "resource.h"
#include "exeload.h"
#include "proctab.h"
#include "dasm.h"
#include "disasm.h"
#include "help.h"
#include "debug.h"
#include "data.h"
//#include "gname.h"
//#include "relocs.h"
//#include "xref.h"

/************************************************************************
* moreoptions								*
* - advanced loading options						*
************************************************************************/
BOOL CALLBACK moreoptions(HWND Wnd,UINT Msg,WPARAM wParam,LPARAM) {

 switch (Msg) {
  case WM_INITDIALOG: {
   CheckDlgButton(Wnd,advanced_pushbp,    options.codedetect&CD_PUSHBP);
   CheckDlgButton(Wnd,advanced_aggressive,options.codedetect&CD_AGGRESSIVE);
   CheckDlgButton(Wnd,advanced_enter,     options.codedetect&CD_ENTER);
   CheckDlgButton(Wnd,advanced_movbx,     options.codedetect&CD_MOVBX);
   CheckDlgButton(Wnd,advanced_moveax,    options.codedetect&CD_MOVEAX);
   CheckDlgButton(Wnd,advanced_eaxfromesp,options.codedetect&CD_EAXFROMESP);
  }return TRUE;
  case WM_COMMAND: switch (LOWORD(wParam)) {
   case IDOK: {
    options.codedetect=0;
    if (IsDlgButtonChecked(Wnd,advanced_pushbp))     options.codedetect|=CD_PUSHBP;
    if (IsDlgButtonChecked(Wnd,advanced_aggressive)) options.codedetect|=CD_AGGRESSIVE;
    if (IsDlgButtonChecked(Wnd,advanced_enter))      options.codedetect|=CD_ENTER;
    if (IsDlgButtonChecked(Wnd,advanced_movbx))      options.codedetect|=CD_MOVBX;
    if (IsDlgButtonChecked(Wnd,advanced_moveax))     options.codedetect|=CD_MOVEAX;
    if (IsDlgButtonChecked(Wnd,advanced_eaxfromesp)) options.codedetect|=CD_EAXFROMESP;
   }nobreak;
   case IDCANCEL: EndDialog(Wnd,wParam);
  }
 }
 return FALSE;
}

/************************************************************************
* 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.								*
************************************************************************/
//dialog proc for verifying type and
// initial options
BOOL CALLBACK checktypebox(HWND Wnd,UINT Msg,WPARAM wParam,LPARAM lParam) {
 static int exetype;
 int i;
 TCHAR *db;	// Text for DefaultButton

 switch (Msg) {
  case WM_INITDIALOG: {
   exetype=floader.getexetype();
   i=0;
   options.loadaddr.segm=0x1000;
   options.loadaddr.offs=0;
   CheckDlgButton(Wnd,IDC_DEFAULTBUTTON,true);
   switch (exetype) {
    case NE_EXE:
     db=T("NE Executable");
     options.processor=PROC_80486;
     options.mode16=true;
    break;
    case COM_EXE:
     db=T("COM File");
     options.processor=PROC_80386;
     options.mode16=true;
     options.loadaddr.offs=0x100;
    break;
    case SYS_EXE:
     db=T("SYS File");
     options.processor=PROC_80386;
     options.mode16=true;
     options.loadaddr.offs=0x00;
    break;
    case PE_EXE:
     db=T("PE Executable");
     options.processor=PROC_PENTIUM;
     options.mode16=false;
    break;
    case OS2_EXE:
     db=T("OS2 Executable");
     options.processor=PROC_PENTIUM;
     options.mode16=false;
    break;
    case LE_EXE:
     db=T("LE Executable");
     options.processor=PROC_80486;
     options.mode16=false;
    break;
    case MZ_EXE:
     db=T("COM File");
     CheckRadioButton(Wnd,IDC_DEFAULTBUTTON,IDC_BINBUTTON,IDC_DOSBUTTON);
     options.processor=PROC_80386;
     options.mode16=true;
     break;
    case BIN_EXE:
    default:
     db=T("COM File");
     CheckRadioButton(Wnd,IDC_DEFAULTBUTTON,IDC_BINBUTTON,IDC_BINBUTTON);
     options.processor=PROC_8086;
     options.mode16=true;
    break;
   }
   SetDlgItemText(Wnd,IDC_DEFAULTBUTTON,db);

   options.mode32=!options.mode16;
   CheckDlgButton(Wnd,load_debug,options.loaddebug);
   CheckDlgButton(Wnd,demangle_names,options.demangle);
   CheckDlgButton(Wnd,IDC_16DASM,options.mode16);
   CheckDlgButton(Wnd,IDC_32DASM,options.mode32);
   CheckDlgButton(Wnd,IDC_LOADDATA,options.loaddata);
   CheckDlgButton(Wnd,IDC_LOADRESOURCES,options.loadresources);
   HWND w=GetDlgItem(Wnd,IDC_LISTBOX1);
   while (procnames[i].num) {
    SendMessage(w,LB_ADDSTRING,0,(LPARAM)(LPCTSTR)procnames[i].name);
    if(options.processor==procnames[i].num)
    SendMessage(w,LB_SETCURSEL,i,0);
    i++;
   }
   TCHAR s[20];
   wsprintf(s,"%x",options.loadaddr.segm);
   SetDlgItemText(Wnd,IDC_SEGEDIT,s);
   wsprintf(s,"%lx",options.loadaddr.offs);
   SetDlgItemText(Wnd,IDC_OFFSEDIT,s);
  }return TRUE;

  case WM_COMMAND: switch (LOWORD(wParam)) {
   case IDC_SEGEDIT: if (HIWORD(wParam)==EN_CHANGE) {
    TCHAR s[20];
    GetWindowText((HWND)lParam,s,elemof(s));
    sscanf(s,"%x",&options.loadaddr.segm);
   }break;
   case IDC_OFFSEDIT: if (HIWORD(wParam)==EN_CHANGE) {
    TCHAR s[20];
    GetWindowText((HWND)lParam,s,elemof(s));
    sscanf(s,"%lx",&options.loadaddr.offs);
   }break;
   case IDC_HELPBUTTON1: {
    DialogBox(hInst,MAKEINTRESOURCE(HELPDIALOG_1),Wnd,helpbox1);
   }break;
   case more_options: {
    DialogBox(hInst,MAKEINTRESOURCE(Advanced_Options),Wnd,moreoptions);
   }break;
   case load_debug: {
    options.loaddebug=!options.loaddebug;
    CheckDlgButton(Wnd,load_debug,options.loaddebug);
   }break;
   case demangle_names: {
    options.demangle=!options.demangle;
    CheckDlgButton(Wnd,demangle_names,options.demangle);
   }break;
   case IDC_16DASM: {
    options.mode16=!options.mode16;
    CheckDlgButton(Wnd,IDC_16DASM,options.mode16);
   }break;
   case IDC_32DASM: {
    options.mode32=!options.mode32;
    CheckDlgButton(Wnd,IDC_32DASM,options.mode32);
   }break;
   case IDC_LOADDATA: {
    options.loaddata=!options.loaddata;
    CheckDlgButton(Wnd,IDC_LOADDATA,options.loaddata);
   }break;
   case IDC_LOADRESOURCES: {
    options.loadresources=!options.loadresources;
    CheckDlgButton(Wnd,IDC_LOADRESOURCES,options.loadresources);
   }break;
   case IDOK: {
    if(!IsDlgButtonChecked(Wnd,IDC_DEFAULTBUTTON)) {
     if(IsDlgButtonChecked(Wnd,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(Wnd,IDC_LISTBOX1,LB_GETCURSEL,0,0)].num;
   }nobreak;
   case IDCANCEL: EndDialog(Wnd,wParam);
  }
 }
 return FALSE;
}

/************************************************************************
* 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(LPCTSTR 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(MainWnd,"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(MainWnd,"Couldn't obtain write permission to file\n"
      "File 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(lstrlen(fname)>3)
      { if(!lstrcmpi(fname+lstrlen(fname)-3,"com"))
	{ SetFilePointer(floader.efile,0,NULL,FILE_BEGIN);
	  floader.exetype=COM_EXE;
	}
	else if(!lstrcmpi(fname+lstrlen(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);
  if (DialogBox(hInst,MAKEINTRESOURCE(D_checktype),MainWnd,checktypebox)
    !=IDOK) {
   CloseHandle(floader.efile);
   return false;
  }
  if (!options.loadaddr.segm) {
   options.loadaddr.segm=0x1000;
   MessageBox(MainWnd,"Sorry - Can't use a zero segment base.\n"
     "Segment 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) {
 UINT FilterIndex;
 if (getfiletoload(current_exe_name, elemof(current_exe_name),IDS_FILTERSTRING,FilterIndex)) {
  changemenus(true);	// Fenster zwecks Ausgabe erzeugen
  if (loadexefile(current_exe_name)) {
   TCHAR s[128];
   StatusMessage("File Opened");
   wsprintf(s,T("%s: %s"),winname,current_exe_name);
   SetWindowText(MainWnd,s);
//   InThread=true;
//   ThreadHandle=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Thread,0,0,&ThreadId);
  }else{
   changemenus(false);
   MessageBox(MainWnd,"File open failed?",program_name,MB_OK|MB_ICONEXCLAMATION);
  }
 }
 return 0;
}

Detected encoding: ASCII (7 bit)2