Skip to content

Commit

Permalink
Merge pull request #29 from visuve/master
Browse files Browse the repository at this point in the history
Small code improvements
  • Loading branch information
hasherezade authored Apr 6, 2024
2 parents 5baea08 + d5d823e commit 8339763
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 44 deletions.
3 changes: 1 addition & 2 deletions commander/Commander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Command* Commander::getCommand(const std::string& line)
return cmd;
}

bool Commander::addCommand(std::string name, Command *cmd, bool overwrite)
bool Commander::addCommand(const std::string& name, Command *cmd, bool overwrite)
{
if ( cmds.find(name) != cmds.end() ) {
if (!overwrite) return false; // already exist
Expand All @@ -59,7 +59,6 @@ bool Commander::addCommand(std::string name, Command *cmd, bool overwrite)
void Commander::parseCommands()
{
const std::string PROMPT = "$ ";
Command *cmd = NULL;

while (true) {
if (this->context == NULL) throw CmdException("Uninitialized commander context!");
Expand Down
5 changes: 3 additions & 2 deletions commander/Commander.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ friend class Commander;
class Command
{
public:
Command(std::string v_desc = "") : desc(v_desc) {}
Command() {}
Command(const std::string& v_desc) : desc(v_desc) {}
virtual ~Command() {}

virtual std::string getDescription() { return desc; }
Expand Down Expand Up @@ -76,7 +77,7 @@ class Commander
virtual ~Commander() { clearCommands(); }

void printHelp();
bool addCommand(std::string name, Command *cmd, bool overwrite = true);
bool addCommand(const std::string& name, Command *cmd, bool overwrite = true);

virtual void parseCommands(); // main loop

Expand Down
2 changes: 1 addition & 1 deletion commander/ExeCommander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ offset_t cmd_util::readOffset(Executable::addr_type aType)
return offset;
}

size_t cmd_util::readNumber(std::string prompt, bool read_hex)
size_t cmd_util::readNumber(const std::string& prompt, bool read_hex)
{
unsigned int num = 0;
std::cout << prompt.c_str() << ": ";
Expand Down
27 changes: 12 additions & 15 deletions commander/ExeCommander.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace cmd_util {
std::string addrTypeToStr(Executable::addr_type type);

offset_t readOffset(Executable::addr_type aType);
size_t readNumber(std::string prompt, bool read_hex=false);
size_t readNumber(const std::string& prompt, bool read_hex=false);

void fetch(Executable *exe, offset_t offset, Executable::addr_type aType, bool hex);
void printWrapperNames(MappedExe *exe);
Expand Down Expand Up @@ -60,7 +60,7 @@ class ExeCommander : public Commander
class ConvertAddrCommand : public Command
{
public:
ConvertAddrCommand(Executable::addr_type v_from, Executable::addr_type v_to, std::string desc)
ConvertAddrCommand(Executable::addr_type v_from, Executable::addr_type v_to, const std::string& desc)
: Command(desc), addrFrom(v_from), addrTo(v_to) {}

virtual void execute(CmdParams *params, CmdContext *context);
Expand All @@ -74,7 +74,7 @@ class ConvertAddrCommand : public Command
class FetchCommand : public Command
{
public:
FetchCommand(bool v_isHex, Executable::addr_type v_addrType, std::string desc)
FetchCommand(bool v_isHex, Executable::addr_type v_addrType, const std::string& desc)
: Command(desc), isHex(v_isHex), addrType(v_addrType) {}

virtual void execute(CmdParams *params, CmdContext *context)
Expand All @@ -92,7 +92,7 @@ class FetchCommand : public Command
class ExeInfoCommand : public Command
{
public:
ExeInfoCommand(std::string desc = "Exe Info")
ExeInfoCommand(const std::string& desc = "Exe Info")
: Command(desc) {}

virtual void execute(CmdParams *params, CmdContext *context);
Expand All @@ -101,7 +101,7 @@ class ExeInfoCommand : public Command
class WrapperCommand : public Command
{
public:
WrapperCommand(std::string desc, size_t v_wrapperId = INVALID_WRAPPER)
WrapperCommand(const std::string& desc, size_t v_wrapperId = INVALID_WRAPPER)
: Command(desc), wrapperId(v_wrapperId) {}

virtual void wrapperAction(ExeElementWrapper *wrapper) = 0;
Expand Down Expand Up @@ -130,7 +130,7 @@ class WrapperCommand : public Command
class AddEntryCommand : public WrapperCommand
{
public:
AddEntryCommand(std::string desc, size_t v_wrapperId = INVALID_WRAPPER)
AddEntryCommand(const std::string& desc, size_t v_wrapperId = INVALID_WRAPPER)
: WrapperCommand(desc, v_wrapperId) {}

virtual void wrapperAction(ExeElementWrapper *wrapper)
Expand Down Expand Up @@ -159,7 +159,7 @@ class AddEntryCommand : public WrapperCommand
class DumpWrapperCommand : public WrapperCommand
{
public:
DumpWrapperCommand(std::string desc, size_t v_wrapperId = INVALID_WRAPPER)
DumpWrapperCommand(const std::string& desc, size_t v_wrapperId = INVALID_WRAPPER)
: WrapperCommand(desc, v_wrapperId) {}

virtual void wrapperAction(ExeElementWrapper *wrapper)
Expand All @@ -173,8 +173,8 @@ class DumpWrapperCommand : public WrapperCommand
class DumpWrapperEntriesCommand : public WrapperCommand
{
public:
DumpWrapperEntriesCommand(std::string desc, size_t v_wrapperId = INVALID_WRAPPER)
: WrapperCommand(desc), wrapperId(v_wrapperId) {}
DumpWrapperEntriesCommand(const std::string& desc, size_t v_wrapperId = INVALID_WRAPPER)
: WrapperCommand(desc, v_wrapperId) {}

virtual void wrapperAction(ExeElementWrapper *wrapper)
{
Expand All @@ -196,16 +196,13 @@ class DumpWrapperEntriesCommand : public WrapperCommand
cmd_util::dumpEntryInfo(lib);
cmd_util::dumpNodeInfo(lib);
}

protected:
size_t wrapperId; //TODO: fetch it from params!
};


class ClearWrapperCommand : public WrapperCommand
{
public:
ClearWrapperCommand(std::string desc, size_t v_wrapperId = INVALID_WRAPPER)
ClearWrapperCommand(const std::string& desc, size_t v_wrapperId = INVALID_WRAPPER)
: WrapperCommand(desc, v_wrapperId) {}

virtual void wrapperAction(ExeElementWrapper *wrapper)
Expand All @@ -228,7 +225,7 @@ class ClearWrapperCommand : public WrapperCommand
class DumpWrapperToFileCommand : public WrapperCommand
{
public:
DumpWrapperToFileCommand(std::string desc, size_t v_wrapperId = INVALID_WRAPPER)
DumpWrapperToFileCommand(const std::string& desc, size_t v_wrapperId = INVALID_WRAPPER)
: WrapperCommand(desc, v_wrapperId)
{
}
Expand Down Expand Up @@ -261,7 +258,7 @@ class DumpWrapperToFileCommand : public WrapperCommand
class SaveExeToFileCommand : public Command
{
public:
SaveExeToFileCommand(std::string desc = "Save exe to file")
SaveExeToFileCommand(const std::string& desc = "Save exe to file")
: Command(desc)
{
}
Expand Down
4 changes: 2 additions & 2 deletions commander/PECommander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ void cmd_util::printStrings(PEFile *pe, size_t limit)
}
size_t count = wrapper->getResStringsCount();

for (size_t i = 0; i < count; i++) {
for (size_t j = 0; j < count; j++) {
if (limit != 0 && limCount >= limit) return;

ResString *resStr = wrapper->getResStringAt(i);
ResString *resStr = wrapper->getResStringAt(j);
if (resStr != NULL) {
OUT_PADDED_OFFSET(std::cout, resStr->offset);
std::cout << " [" << std::dec << resStr->getSize() << "]" << std::endl;
Expand Down
19 changes: 8 additions & 11 deletions commander/PECommander.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class PECommander : public ExeCommander
class SectionByAddrCommand : public Command
{
public:
SectionByAddrCommand(Executable::addr_type v_addrType, std::string desc)
SectionByAddrCommand(Executable::addr_type v_addrType, const std::string& desc)
: Command(desc), addrType(v_addrType) {}

virtual void execute(CmdParams *params, CmdContext *context)
Expand Down Expand Up @@ -79,7 +79,7 @@ class SectionByAddrCommand : public Command
class PrintStringsCommand : public Command
{
public:
PrintStringsCommand(std::string desc)
PrintStringsCommand(const std::string& desc)
: Command(desc) {}

virtual void execute(CmdParams *params, CmdContext *context)
Expand All @@ -101,14 +101,12 @@ class PrintStringsCommand : public Command

cmd_util::printStrings(pe, limit);
}
protected:
int wrapperId; //TODO: fetch it from params!
};

class PrintWrapperTypesCommand : public Command
{
public:
PrintWrapperTypesCommand(std::string desc)
PrintWrapperTypesCommand(const std::string& desc)
: Command(desc) {}

virtual void execute(CmdParams *params, CmdContext *context)
Expand All @@ -125,7 +123,7 @@ class PrintWrapperTypesCommand : public Command
class WrapperInfoCommand : public Command
{
public:
WrapperInfoCommand(std::string desc)
WrapperInfoCommand(const std::string& desc)
: Command(desc) {}

virtual void execute(CmdParams *params, CmdContext *context)
Expand Down Expand Up @@ -166,7 +164,7 @@ class WrapperInfoCommand : public Command
class MoveDataDirEntryCommand : public Command
{
public:
MoveDataDirEntryCommand(std::string desc)
MoveDataDirEntryCommand(const std::string& desc)
: Command(desc) {}

virtual void execute(CmdParams *params, CmdContext *context)
Expand Down Expand Up @@ -200,7 +198,7 @@ class MoveDataDirEntryCommand : public Command
class SectionDumpCommand : public Command
{
public:
SectionDumpCommand(std::string desc, bool v_saveToFile = false)
SectionDumpCommand(const std::string& desc, bool v_saveToFile = false)
: Command(desc), saveToFile(v_saveToFile)
{
}
Expand All @@ -210,7 +208,6 @@ class SectionDumpCommand : public Command
PEFile *pe = cmd_util::getPEFromContext(context);
if (!pe) return;

size_t sectHdrCount = pe->getSectionsCount(false);
size_t sectCount = pe->getSectionsCount(true);
std:: cout << "Sections count = " << std::dec << sectCount << "\n";
if (sectCount == 0) {
Expand Down Expand Up @@ -274,7 +271,7 @@ class SectionDumpCommand : public Command
class ExportsListCommand : public Command
{
public:
ExportsListCommand(std::string desc)
ExportsListCommand(const std::string& desc)
: Command(desc) {}

virtual void execute(CmdParams *params, CmdContext *context)
Expand Down Expand Up @@ -310,7 +307,7 @@ class ExportsListCommand : public Command
class ImportsListCommand : public Command
{
public:
ImportsListCommand(std::string desc)
ImportsListCommand(const std::string& desc)
: Command(desc) {}

virtual void execute(CmdParams *params, CmdContext *context)
Expand Down
1 change: 0 additions & 1 deletion parser/pe/ExportDirWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ size_t ExportDirWrapper::mapNames()
bool ExportDirWrapper::wrap()
{
clear();
size_t mapNum = mapNames();

IMAGE_EXPORT_DIRECTORY* exp = exportDir();
if (exp == NULL) return 0;
Expand Down
3 changes: 1 addition & 2 deletions parser/pe/ImportBaseDirWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ void ImportBaseDirWrapper::reloadMapping()
for (size_t i = 0; i < entriesCount; i++) {
ImportBaseEntryWrapper* lib = dynamic_cast<ImportBaseEntryWrapper*> (this->getEntryAt(i));
if (lib == NULL) continue;
size_t libId = lib->entryNum;

size_t funcCount = lib->getEntriesCount();
for (size_t fI = 0; fI < funcCount; fI++) {
Expand Down Expand Up @@ -254,7 +253,7 @@ bool ImportBaseFuncWrapper::isValid()

if (!isByOrdinal()) {
char *fName = this->getFunctionName();
if (!imports_util::isNameValid(m_Exe, libName)) return false;
if (!imports_util::isNameValid(m_Exe, fName)) return false;
}
return true;
}
6 changes: 0 additions & 6 deletions parser/pe/ImportDirWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ uint64_t ImportedFuncWrapper::getThunkValue()

bool ImportedFuncWrapper::isByOrdinal()
{
bool isOk = false;

void *p = getValuePtr(ImportEntryWrapper::ORIG_FIRST_THUNK);
if (!p) p = getValuePtr(ImportEntryWrapper::FIRST_THUNK);
if (!p) return false;
Expand Down Expand Up @@ -122,13 +120,9 @@ bufsize_t ImportedFuncWrapper::getSize()

void* ImportedFuncWrapper::getFieldPtr(size_t fId, size_t subField)
{
bool is64 = isBit64();
void *entryPtr = this->getPtr();
if (entryPtr == NULL) return NULL;

IMAGE_THUNK_DATA32* en32 = is64 ? NULL : (IMAGE_THUNK_DATA32*) entryPtr;
IMAGE_THUNK_DATA64* en64 = is64 ? (IMAGE_THUNK_DATA64*) entryPtr : NULL;

switch (fId) {
case ORIG_THUNK: return (void*) getValuePtr(ImportEntryWrapper::ORIG_FIRST_THUNK);
case THUNK: return (void*) getValuePtr(ImportEntryWrapper::FIRST_THUNK);
Expand Down
3 changes: 1 addition & 2 deletions parser/pe/SectHdrsWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ bufsize_t SectionHdrWrapper::getMappedRawSize()
if (secEnd > peSize) {
const bufsize_t trimmedSize = peSize - secOffset; // trim to the file size
if (virtualSize) {
bufsize_t unit = m_PE->getAlignment(Executable::RVA);
unit = m_PE->getAlignment(Executable::RVA);
if (unit != 0) {
virtualSize = roundupToUnit(virtualSize, unit);
}
Expand Down Expand Up @@ -534,7 +534,6 @@ QString SectHdrsWrapper::getFieldName(size_t fieldId)

SectionHdrWrapper* SectHdrsWrapper::getSecHdrAtOffset(offset_t offset, Executable::addr_type addrType, bool recalculate, bool verbose)
{
size_t size = this->entries.size();
std::map<offset_t, SectionHdrWrapper*> *secMap = NULL;

if (addrType == Executable::RAW) {
Expand Down

0 comments on commit 8339763

Please sign in to comment.