2019-09-23 16:46:57 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018-2019 QuasarApp.
|
|
|
|
* Distributed under the lgplv3 software license, see the accompanying
|
|
|
|
* Everyone is permitted to copy and distribute verbatim copies
|
|
|
|
* of this license document, but changing it is not allowed.
|
|
|
|
*/
|
|
|
|
|
2019-03-17 18:42:07 +03:00
|
|
|
#include "pe.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
2019-03-18 10:33:03 +03:00
|
|
|
#include <QFileInfo>
|
2019-03-23 20:40:33 +03:00
|
|
|
#include <QSet>
|
2019-03-22 23:36:22 +03:00
|
|
|
#include <QVector>
|
2019-03-25 20:13:30 +03:00
|
|
|
#include <parser-library/parse.h>
|
2019-03-17 20:16:48 +03:00
|
|
|
|
2019-03-23 20:40:33 +03:00
|
|
|
#include <bits/stl_set.h>
|
2019-03-22 23:36:22 +03:00
|
|
|
|
2019-03-23 20:40:33 +03:00
|
|
|
namespace peparse {
|
2019-03-21 00:34:57 +03:00
|
|
|
|
2019-03-23 20:40:33 +03:00
|
|
|
class section;
|
2019-03-21 00:34:57 +03:00
|
|
|
|
2019-03-23 20:40:33 +03:00
|
|
|
struct importent {
|
|
|
|
VA addr;
|
|
|
|
std::string symbolName;
|
|
|
|
std::string moduleName;
|
|
|
|
};
|
2019-03-21 13:22:18 +03:00
|
|
|
|
2019-03-23 20:40:33 +03:00
|
|
|
class reloc;
|
|
|
|
class exportent;
|
|
|
|
class symbol;
|
2019-03-17 20:16:48 +03:00
|
|
|
|
2019-03-23 20:40:33 +03:00
|
|
|
struct parsed_pe_internal {
|
|
|
|
std::vector<section> secs;
|
|
|
|
std::vector<resource> rsrcs;
|
|
|
|
std::vector<importent> imports;
|
|
|
|
std::vector<reloc> relocs;
|
|
|
|
std::vector<exportent> exports;
|
|
|
|
std::vector<symbol> symbols;
|
|
|
|
};
|
2019-03-17 20:16:48 +03:00
|
|
|
|
2019-03-17 18:42:07 +03:00
|
|
|
}
|
|
|
|
|
2019-08-31 22:22:26 +03:00
|
|
|
bool PE::getDep(peparse::parsed_pe_internal * internal, LibInfo &res) const {
|
2019-03-23 20:40:33 +03:00
|
|
|
auto imports = internal->imports;
|
2019-03-17 20:16:48 +03:00
|
|
|
|
2019-03-23 20:40:33 +03:00
|
|
|
std::set<std::string> filter;
|
2019-03-17 20:16:48 +03:00
|
|
|
|
2019-03-23 20:40:33 +03:00
|
|
|
for ( auto &i : imports) {
|
|
|
|
if (!filter.count(i.moduleName)) {
|
|
|
|
filter.insert(i.moduleName);
|
2019-04-05 14:23:42 +03:00
|
|
|
res.addDependncies(QString::fromStdString(i.moduleName));
|
2019-03-18 10:33:03 +03:00
|
|
|
}
|
|
|
|
}
|
2019-03-17 20:16:48 +03:00
|
|
|
|
2019-04-05 14:23:42 +03:00
|
|
|
return res.getDependncies().size();
|
2019-03-17 18:42:07 +03:00
|
|
|
}
|
|
|
|
|
2019-03-18 16:00:39 +03:00
|
|
|
PE::PE(): IGetLibInfo () {
|
2019-03-18 10:33:03 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-31 22:22:26 +03:00
|
|
|
bool PE::getLibInfo(const QString &lib, LibInfo &info) const {
|
2019-03-23 20:40:33 +03:00
|
|
|
auto parsedPeLib = peparse::ParsePEFromFile(lib.toLatin1());
|
2019-03-18 10:33:03 +03:00
|
|
|
|
2019-09-14 13:59:11 +03:00
|
|
|
if (!parsedPeLib)
|
|
|
|
return false;
|
|
|
|
|
2019-03-23 20:40:33 +03:00
|
|
|
if (static_cast<RunType>(parsedPeLib->peHeader.nt.OptionalMagic) == RunType::_32bit) {
|
2019-04-05 14:23:42 +03:00
|
|
|
info.setPlatform(Platform::Win32);
|
2019-03-23 20:40:33 +03:00
|
|
|
} else if (static_cast<RunType>(parsedPeLib->peHeader.nt.OptionalMagic) == RunType::_64bit) {
|
2019-04-05 14:23:42 +03:00
|
|
|
info.setPlatform(Platform::Win64);
|
2019-03-23 20:40:33 +03:00
|
|
|
} else {
|
2019-04-05 14:23:42 +03:00
|
|
|
info.setPlatform(Platform::UnknownPlatform);
|
2019-03-18 10:33:03 +03:00
|
|
|
}
|
|
|
|
|
2019-04-05 14:23:42 +03:00
|
|
|
info.setName(QFileInfo(lib).fileName());
|
2019-04-05 16:52:53 +03:00
|
|
|
info.setPath(QFileInfo(lib).absolutePath());
|
2019-03-18 10:33:03 +03:00
|
|
|
|
2019-04-05 14:23:42 +03:00
|
|
|
if (!getDep(parsedPeLib->internal, info)) {
|
2019-03-23 20:40:33 +03:00
|
|
|
return false;
|
2019-03-18 10:33:03 +03:00
|
|
|
}
|
|
|
|
|
2019-03-23 20:40:33 +03:00
|
|
|
peparse::DestructParsedPE(parsedPeLib);
|
2019-03-17 18:42:07 +03:00
|
|
|
|
2019-03-19 21:50:05 +03:00
|
|
|
return info.isValid();
|
2019-03-17 18:42:07 +03:00
|
|
|
}
|
2019-03-18 17:12:40 +03:00
|
|
|
|
|
|
|
PE::~PE(){
|
|
|
|
|
|
|
|
}
|