Skip to content

Commit

Permalink
[REFACT] Moved exporting disasm to PeHandler (Issue #19)
Browse files Browse the repository at this point in the history
  • Loading branch information
hasherezade committed Feb 21, 2023
1 parent 9cb571d commit 3011e80
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 48 deletions.
50 changes: 2 additions & 48 deletions pe-bear/PEDockedWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,7 @@ void SectionMenu::dumpSelectedSection()

void SectionMenu::exportSectionDisasm()
{
if (!peHndl) return;
PEFile *pe = peHndl->getPe();
if (!pe) return;
if (!selectedSection) return;
if (!peHndl || !selectedSection) return;

QString outDir = mainSettings.dirDump;
if (outDir == "") outDir = peHndl->getDirPath();
Expand All @@ -356,53 +353,10 @@ void SectionMenu::exportSectionDisasm()
const offset_t startOff = selectedSection->getContentOffset(Executable::RAW, true);
const size_t previewSize = selectedSection->getContentSize(Executable::RAW, true);

pe_bear::PeDisasm myDisasm(pe, previewSize);
myDisasm.init(startOff, pe->getBitMode());
myDisasm.fillTable();

QFile fOut(path);
if (fOut.open(QFile::WriteOnly | QFile::Text) == false) {
if (!peHndl->exportDisasm(path, startOff, previewSize)) {
QMessageBox::warning(this, "Error", "Dumping section failed!");
return;
}
QTextStream disasmStream(&fOut);
for (int index = 0; index < myDisasm.chunksCount(); ++index ) {
QString str = myDisasm.mnemStr(index);
if (myDisasm.isBranching(index)) {
str = myDisasm.translateBranching(index);
}

//resolve target functions:
bool isOk = false;
const offset_t tRva = myDisasm.getTargetRVA(index, isOk);
QString funcName = "";
QString refStr = "";
if (isOk) {
funcName = peHndl->importDirWrapper.thunkToFuncName(tRva, false);
if (funcName.length() == 0 ) {
funcName = peHndl->delayImpDirWrapper.thunkToFuncName(tRva, false);
}
refStr = myDisasm.getStringAt(tRva);
}

offset_t VA = pe->rvaToVa(myDisasm.getRvaAt(index));
QString vaStr = QString::number(VA, 16);

// stream to the file:
disasmStream << vaStr << " : " << str;
if (funcName.length()) {
disasmStream << " : " << funcName;
}
else if (refStr.length()) {
disasmStream << " : " << refStr;
}
disasmStream << "\n";
if (myDisasm.isBranching(index)) {
disasmStream << "\n"; // add a separator line
}
}
fOut.close();

QMessageBox::information(this, "Done!", "Dumped section disasembly: "+ selectedSection->mappedName +"\ninto: " + path);
return;
}
Expand Down
59 changes: 59 additions & 0 deletions pe-bear/base/PeHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "../base/PeHandlersManager.h"
#include <bearparser/bearparser.h>
#include "../disasm/PeDisasm.h"

using namespace sig_ma;
using namespace pe;
Expand Down Expand Up @@ -1194,3 +1195,61 @@ bool PeHandler::markedBranching(offset_t cRva, offset_t tRva)
emit marked();
return true;
}

bool PeHandler::exportDisasm(const QString &path, const offset_t startOff, const size_t previewSize)
{
PEFile *pe = this->getPe();
if (!pe) return false;

if (!pe->getContentAt(startOff, previewSize)) {
return false;
}

QFile fOut(path);
if (fOut.open(QFile::WriteOnly | QFile::Text) == false) {
return false;
}

pe_bear::PeDisasm myDisasm(pe, previewSize);
myDisasm.init(startOff, pe->getBitMode());
myDisasm.fillTable();

QTextStream disasmStream(&fOut);
for (int index = 0; index < myDisasm.chunksCount(); ++index ) {
QString str = myDisasm.mnemStr(index);
if (myDisasm.isBranching(index)) {
str = myDisasm.translateBranching(index);
}

//resolve target functions:
bool isOk = false;
const offset_t tRva = myDisasm.getTargetRVA(index, isOk);
QString funcName = "";
QString refStr = "";
if (isOk) {
funcName = importDirWrapper.thunkToFuncName(tRva, false);
if (funcName.length() == 0 ) {
funcName = delayImpDirWrapper.thunkToFuncName(tRva, false);
}
refStr = myDisasm.getStringAt(tRva);
}

offset_t VA = pe->rvaToVa(myDisasm.getRvaAt(index));
QString vaStr = QString::number(VA, 16);

// stream to the file:
disasmStream << vaStr << " : " << str;
if (funcName.length()) {
disasmStream << " : " << funcName;
}
else if (refStr.length()) {
disasmStream << " : " << refStr;
}
disasmStream << "\n";
if (myDisasm.isBranching(index)) {
disasmStream << "\n"; // add a separator line
}
}
fOut.close();
return true;
}
3 changes: 3 additions & 0 deletions pe-bear/base/PeHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ class PeHandler : public QObject, public Releasable
bool setDisplayedEP();
void undoDisplayOffset();

bool exportDisasm(const QString &path, const offset_t startOff, const size_t previewSize);

/* File name wrappers */
QString getFullName() { return this->m_fileBuffer->getFileName(); }

Expand All @@ -204,6 +206,7 @@ class PeHandler : public QObject, public Releasable
QFileInfo fileInfo(path);
return fileInfo.absoluteDir().absolutePath();
}

//--------
/* wrappers for PE structures */
DosHdrWrapper dosHdrWrapper;
Expand Down

0 comments on commit 3011e80

Please sign in to comment.