📄 database.cpp
字号:
/************************************************************************
* savedbcoord *
* - coordinates saving of the databases when save as database file is *
* chosen in Borg *
************************************************************************/
void savedbcoord(char *fname,char *exename)
{ savefile sf;
dword flen;
dword bver;
HWND sbox;
// open file
sbox=CreateDialog(hInst,MAKEINTRESOURCE(save_box),mainwindow,(DLGPROC)savemessbox);
if(!sf.sopen(fname,GENERIC_WRITE,1,CREATE_ALWAYS,0))
{ DestroyWindow(sbox);
return;
}
// save header to identify as a database file
if(!sf.swrite("BORG",4))
{ DestroyWindow(sbox);
MessageBox(mainwindow,"Header Info[1]:File write failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return;
}
// save BORG_VERSION
bver=BORG_VER;
if(!sf.swrite(&bver,sizeof(bver)))
{ DestroyWindow(sbox);
MessageBox(mainwindow,"Header Info[2]:File write failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return;
}
// save filename of exe file.
flen=strlen(exename)+1;
if(!sf.swrite(&flen,sizeof(dword)))
{ DestroyWindow(sbox);
MessageBox(mainwindow,"Header Info[3]:File write failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return;
}
if(!sf.swrite(exename,flen))
{ DestroyWindow(sbox);
MessageBox(mainwindow,"Header Info[4]:File write failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return;
}
// save options.
if(!sf.swrite(&options,sizeof(globaloptions)))
{ DestroyWindow(sbox);
MessageBox(mainwindow,"Options:File write failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return;
}
if(!sf.swrite(&floader.exetype,sizeof(int)))
{ DestroyWindow(sbox);
MessageBox(mainwindow,"Exetype:File write failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return;
}
// save segment info
if(!savedatasegitems(&sf,floader.fbuff))
{ DestroyWindow(sbox);
MessageBox(mainwindow,"Segments:File write failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return;
}
// save import info
if(!gnamesavedb(&import,&sf))
{ DestroyWindow(sbox);
MessageBox(mainwindow,"Imports:File write failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return;
}
// save export info
if(!gnamesavedb(&expt,&sf))
{ DestroyWindow(sbox);
MessageBox(mainwindow,"Exports:File write failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return;
}
// save names
if(!gnamesavedb(&name,&sf))
{ DestroyWindow(sbox);
MessageBox(mainwindow,"Names:File write failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return;
}
// save relocs
if(!saverelocitems(&sf))
{ DestroyWindow(sbox);
MessageBox(mainwindow,"Relocs:File write failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return;
}
// save xrefs
if(!xrefsavedb(&sf))
{ DestroyWindow(sbox);
MessageBox(mainwindow,"Xrefs:File write failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return;
}
// save asm database
if(!dissavedb(&sf,floader.fbuff))
{ DestroyWindow(sbox);
MessageBox(mainwindow,"Database:File write failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return;
}
// save decrypter list
if(!savedecryptitems(&sf))
{ DestroyWindow(sbox);
MessageBox(mainwindow,"Decryptors:File write failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return;
}
sf.flushfilewrite();
// close file
DestroyWindow(sbox);
}
/************************************************************************
* loaddbcoord *
* - coordinates loading of the databases when load database file is *
* chosen in Borg *
************************************************************************/
bool loaddbcoord(char *fname,char *exename)
{ savefile sf;
char tbuff[20];
dword num;
dword flen,fsize,gsize;
dword bver;
HWND lbox;
// open file
lbox=CreateDialog(hInst,MAKEINTRESOURCE(load_box),mainwindow,(DLGPROC)loadmessbox);
if(!sf.sopen(fname,GENERIC_READ,1,OPEN_EXISTING,0))
{ DestroyWindow(lbox);
return false;
}
// load header check its a database file
tbuff[4]=0;
if(!sf.sread(tbuff,4,&num))
{ DestroyWindow(lbox);
MessageBox(mainwindow,"File read failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return false;
}
if(strcmp(tbuff,"BORG"))
{ DestroyWindow(lbox);
MessageBox(mainwindow,"Not A Borg Database File",fname,MB_OK|MB_ICONEXCLAMATION);
return false;
}
// read BORG_VERSION
if(!sf.sread(&bver,sizeof(bver),&num))
{ DestroyWindow(lbox);
MessageBox(mainwindow,"File read failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return false;
}
if(bver>BORG_VER)
{ DestroyWindow(lbox);
MessageBox(mainwindow,"Savefile from a later version [or very early version]!",fname,MB_OK|MB_ICONEXCLAMATION);
return false;
}
if(bver<BORG_VER)
{ MessageBox(mainwindow,"Warning:earlier version savefile [will attempt load]",fname,MB_OK|MB_ICONEXCLAMATION);
#ifdef DEBUG
DebugMessage("Detected version:%d.%d Savefile",bver/100,bver%100);
#endif
}
// load filename of exe file.
flen=0;
if(!sf.sread(&flen,sizeof(dword),&num))
{ DestroyWindow(lbox);
MessageBox(mainwindow,"File read failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return false;
}
if(!sf.sread(exename,flen,&num))
{ DestroyWindow(lbox);
MessageBox(mainwindow,"File read failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return false;
}
#ifdef DEBUG
DebugMessage("Filename:%s",exename);
#endif
// added 226 to allow load of older databases
gsize=sizeof(globaloptions);
if(bver<222)
{ DestroyWindow(lbox);
MessageBox(mainwindow,"Older databases are incompatible, please reuse the older version of Borg","Prior to 2.22",MB_OK|MB_ICONEXCLAMATION);
return false;
}
// load options.
if(!sf.sread(&options,gsize,&num))
{ DestroyWindow(lbox);
MessageBox(mainwindow,"File read failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return false;
}
#ifdef DEBUG
DebugMessage("Global Options Size:%d",gsize);
#endif
if(!sf.sread(&floader.exetype,sizeof(int),&num))
{ DestroyWindow(lbox);
MessageBox(mainwindow,"File read failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
return false;
}
// load exe file.
// - any errors from here on will now be fatal, and Borg will need to exit
floader.efile=CreateFile(exename,GENERIC_READ|GENERIC_WRITE,1,NULL,OPEN_EXISTING,0,NULL);
if(floader.efile==INVALID_HANDLE_VALUE)
{ floader.efile=CreateFile(exename,GENERIC_READ,1,NULL,OPEN_EXISTING,0,NULL);
if(floader.efile==INVALID_HANDLE_VALUE)
{ DestroyWindow(lbox);
MessageBox(mainwindow,"File open failed ?",exename,MB_OK|MB_ICONEXCLAMATION);
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)
{ DestroyWindow(lbox);
MessageBox(mainwindow,"File open failed ?",exename,MB_OK|MB_ICONEXCLAMATION);
CloseHandle(floader.efile);
return false;
}
fsize=GetFileSize(floader.efile,NULL);
floader.fbuff=new byte[fsize];
SetFilePointer(floader.efile,0x00,NULL,FILE_BEGIN);
if(!ReadFile(floader.efile,floader.fbuff,fsize,&num,NULL))
{ DestroyWindow(lbox);
MessageBox(mainwindow,"File read failed ?",exename,MB_OK|MB_ICONEXCLAMATION);
CloseHandle(floader.efile);
delete floader.fbuff;
return false;
}
// load segment info
if(!loaddatasegitems(&sf,floader.fbuff))
{ DestroyWindow(lbox);
MessageBox(mainwindow,"Fatal Error\nSegments:File read failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
DestroyWindow(mainwindow);
return false;
}
#ifdef DEBUG
DebugMessage("Loading Imports");
#endif
// load import info
if(!gnameloaddb(&import,&sf))
{ DestroyWindow(lbox);
MessageBox(mainwindow,"Fatal Error\nImports:File read failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
DestroyWindow(mainwindow);
return false;
}
#ifdef DEBUG
DebugMessage("Loading Exports");
#endif
// load export info
if(!gnameloaddb(&expt,&sf))
{ DestroyWindow(lbox);
MessageBox(mainwindow,"Fatal Error\nExports:File read failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
DestroyWindow(mainwindow);
return false;
}
#ifdef DEBUG
DebugMessage("Loading Names");
#endif
// load names
if(!gnameloaddb(&name,&sf))
{ DestroyWindow(lbox);
MessageBox(mainwindow,"Fatal Error\nNames:File read failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
DestroyWindow(mainwindow);
return false;
}
#ifdef DEBUG
DebugMessage("Loading Relocs");
#endif
// load relocs
if(!loadrelocitems(&sf))
{ DestroyWindow(lbox);
MessageBox(mainwindow,"Fatal Error\nRelocs:File read failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
DestroyWindow(mainwindow);
return false;
}
#ifdef DEBUG
DebugMessage("Loading Xrefs");
#endif
// load xrefs
if(!xrefloaddb(&sf))
{ DestroyWindow(lbox);
MessageBox(mainwindow,"Fatal Error\nXrefs:File read failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
DestroyWindow(mainwindow);
return false;
}
#ifdef DEBUG
DebugMessage("Loading Asm database");
#endif
// load asm database
if(!disloaddb(&sf,floader.fbuff))
{ DestroyWindow(lbox);
MessageBox(mainwindow,"Fatal Error\nDatabase:File read failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
DestroyWindow(mainwindow);
return false;
}
#ifdef DEBUG
DebugMessage("Relocating File");
#endif
if(!reloc.relocfile())
{ DestroyWindow(lbox);
MessageBox(mainwindow,"Fatal Error\nRelocating File",fname,MB_OK|MB_ICONEXCLAMATION);
DestroyWindow(mainwindow);
return false;
}
#ifdef DEBUG
DebugMessage("Loading Decryptors");
#endif
if(!loaddecryptitems(&sf))
{ DestroyWindow(lbox);
MessageBox(mainwindow,"Fatal Error\nDecryptors:File read failed ?",fname,MB_OK|MB_ICONEXCLAMATION);
DestroyWindow(mainwindow);
return false;
}
DestroyWindow(lbox);
return true;
}
/************************************************************************
* savedb *
* - the first place of call when save as database is selected. *
* - asks the user to select a file before calling the fileloader savedb *
* which is where the save to database is controlled from *
************************************************************************/
void savedb(void)
{ char szFile[MAX_PATH*2];
if(scheduler.sizelist())
{ MessageBox(mainwindow,"There are still items to process yet","Borg Warning",MB_OK|MB_ICONEXCLAMATION);
return;
}
getfiletosave(szFile);
if(szFile[0])
{ savedbcoord(szFile,current_exe_name);
}
}
/************************************************************************
* loaddb *
* - the first place of call when load from database is selected. *
* - asks the user to select a file before calling the fileloader loaddb *
* which is where the load from database is controlled from *
* - starts up the secondary thread when the file is loaded *
************************************************************************/
void loaddb(void)
{ char szFile[MAX_PATH*2];
getfiletoload(szFile);
if(szFile[0])
{ if(loaddbcoord(szFile,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();
scheduler.addtask(scrolling,priority_userrequest,nlptr,NULL);
}
else
MessageBox(mainwindow,"File open failed ?",program_name,MB_OK|MB_ICONEXCLAMATION);
}
}
/************************************************************************
* savemessbox *
* - A small dialog box which contains the message 'saving' to be shown *
* as a database file is saved *
************************************************************************/
#ifdef __BORLANDC__
#pragma warn -par
#endif
BOOL CALLBACK savemessbox(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
/************************************************************************
* loadmessbox *
* - A small dialog box which contains the message 'loading' to be shown *
* as a database file is loaded *
************************************************************************/
#ifdef __BORLANDC__
#pragma warn -par
#endif
BOOL CALLBACK loadmessbox(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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -