mirror of
https://github.com/LongSoft/UEFITool.git
synced 2025-05-09 13:52:01 -04:00
Remove Qt deps from UEFIFind and fix issues
This commit is contained in:
parent
7d16c1d48d
commit
4d50d581fa
21 changed files with 275 additions and 225 deletions
|
@ -13,6 +13,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|||
|
||||
#include "uefifind.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
UEFIFind::UEFIFind()
|
||||
{
|
||||
model = new TreeModel();
|
||||
|
@ -27,22 +29,18 @@ UEFIFind::~UEFIFind()
|
|||
model = NULL;
|
||||
}
|
||||
|
||||
USTATUS UEFIFind::init(const QString & path)
|
||||
USTATUS UEFIFind::init(const UString & path)
|
||||
{
|
||||
USTATUS result;
|
||||
|
||||
fileInfo = QFileInfo(path);
|
||||
|
||||
if (!fileInfo.exists())
|
||||
if (!isExistOnFs(path))
|
||||
return U_FILE_OPEN;
|
||||
|
||||
QFile inputFile;
|
||||
inputFile.setFileName(path);
|
||||
|
||||
if (!inputFile.open(QFile::ReadOnly))
|
||||
std::ifstream inputFile(path.toLocal8Bit(), std::ios::in | std::ios::binary);
|
||||
if (!inputFile)
|
||||
return U_FILE_OPEN;
|
||||
|
||||
QByteArray buffer = inputFile.readAll();
|
||||
std::vector<char> buffer(std::istreambuf_iterator<char>(inputFile),
|
||||
(std::istreambuf_iterator<char>()));
|
||||
inputFile.close();
|
||||
|
||||
result = ffsParser->parse(buffer);
|
||||
|
@ -53,7 +51,7 @@ USTATUS UEFIFind::init(const QString & path)
|
|||
return U_SUCCESS;
|
||||
}
|
||||
|
||||
USTATUS UEFIFind::find(const UINT8 mode, const bool count, const QString & hexPattern, QString & result)
|
||||
USTATUS UEFIFind::find(const UINT8 mode, const bool count, const UString & hexPattern, UString & result)
|
||||
{
|
||||
UModelIndex root = model->index(0, 0);
|
||||
std::set<std::pair<UModelIndex, UModelIndex> > files;
|
||||
|
@ -66,29 +64,29 @@ USTATUS UEFIFind::find(const UINT8 mode, const bool count, const QString & hexPa
|
|||
|
||||
if (count) {
|
||||
if (!files.empty())
|
||||
result.append(QString("%1\n").arg(files.size()));
|
||||
result += usprintf("%d\n", files.size());
|
||||
return U_SUCCESS;
|
||||
}
|
||||
|
||||
for (std::set<std::pair<UModelIndex, UModelIndex> >::const_iterator citer = files.begin(); citer != files.end(); ++citer) {
|
||||
QByteArray data(16, '\x00');
|
||||
UByteArray data(16, '\x00');
|
||||
std::pair<UModelIndex, UModelIndex> indexes = *citer;
|
||||
if (!model->hasEmptyHeader(indexes.first))
|
||||
data = model->header(indexes.first).left(16);
|
||||
result.append(guidToUString(*(const EFI_GUID*)data.constData()));
|
||||
result += guidToUString(*(const EFI_GUID*)data.constData());
|
||||
|
||||
// Special case of freeform subtype GUID files
|
||||
if (indexes.second.isValid() && model->subtype(indexes.second) == EFI_SECTION_FREEFORM_SUBTYPE_GUID) {
|
||||
data = model->header(indexes.second);
|
||||
result.append(" ").append(guidToUString(*(const EFI_GUID*)(data.constData() + sizeof(EFI_COMMON_SECTION_HEADER))));
|
||||
result += UString(" ") + (guidToUString(*(const EFI_GUID*)(data.constData() + sizeof(EFI_COMMON_SECTION_HEADER))));
|
||||
}
|
||||
|
||||
result.append("\n");
|
||||
result += UString("\n");
|
||||
}
|
||||
return U_SUCCESS;
|
||||
}
|
||||
|
||||
USTATUS UEFIFind::findFileRecursive(const UModelIndex index, const QString & hexPattern, const UINT8 mode, std::set<std::pair<UModelIndex, UModelIndex> > & files)
|
||||
USTATUS UEFIFind::findFileRecursive(const UModelIndex index, const UString & hexPattern, const UINT8 mode, std::set<std::pair<UModelIndex, UModelIndex> > & files)
|
||||
{
|
||||
if (!index.isValid())
|
||||
return U_SUCCESS;
|
||||
|
@ -96,48 +94,54 @@ USTATUS UEFIFind::findFileRecursive(const UModelIndex index, const QString & hex
|
|||
if (hexPattern.isEmpty())
|
||||
return U_INVALID_PARAMETER;
|
||||
|
||||
const char *hexPatternRaw = hexPattern.toLocal8Bit();
|
||||
std::vector<UINT8> pattern, patternMask;
|
||||
if (!makePattern(hexPatternRaw, pattern, patternMask))
|
||||
return U_INVALID_PARAMETER;
|
||||
|
||||
// Check for "all substrings" pattern
|
||||
if (hexPattern.count('.') == hexPattern.length())
|
||||
size_t count = 0;
|
||||
for (size_t i = 0; i < patternMask.size(); i++)
|
||||
if (patternMask[i] == 0)
|
||||
count++;
|
||||
if (count == patternMask.size())
|
||||
return U_SUCCESS;
|
||||
|
||||
|
||||
|
||||
bool hasChildren = (model->rowCount(index) > 0);
|
||||
for (int i = 0; i < model->rowCount(index); i++) {
|
||||
findFileRecursive(index.child(i, index.column()), hexPattern, mode, files);
|
||||
}
|
||||
|
||||
QByteArray data;
|
||||
UByteArray data;
|
||||
if (hasChildren) {
|
||||
if (mode == SEARCH_MODE_HEADER || mode == SEARCH_MODE_ALL)
|
||||
data.append(model->header(index));
|
||||
data += model->header(index);
|
||||
}
|
||||
else {
|
||||
if (mode == SEARCH_MODE_HEADER)
|
||||
data.append(model->header(index));
|
||||
data += model->header(index);
|
||||
else if (mode == SEARCH_MODE_BODY)
|
||||
data.append(model->body(index));
|
||||
data += model->body(index);
|
||||
else
|
||||
data.append(model->header(index)).append(model->body(index));
|
||||
data += model->header(index) + model->body(index);
|
||||
}
|
||||
|
||||
QString hexBody = QString(data.toHex());
|
||||
QRegExp regexp = QRegExp(QString(hexPattern), Qt::CaseInsensitive);
|
||||
INT32 offset = regexp.indexIn(hexBody);
|
||||
while (offset >= 0) {
|
||||
if (offset % 2 == 0) {
|
||||
if (model->type(index) != Types::File) {
|
||||
UModelIndex ffs = model->findParentOfType(index, Types::File);
|
||||
if (model->type(index) == Types::Section && model->subtype(index) == EFI_SECTION_FREEFORM_SUBTYPE_GUID)
|
||||
files.insert(std::pair<UModelIndex, UModelIndex>(ffs, index));
|
||||
else
|
||||
files.insert(std::pair<UModelIndex, UModelIndex>(ffs, UModelIndex()));
|
||||
}
|
||||
const UINT8 *rawData = reinterpret_cast<const UINT8 *>(data.constData());
|
||||
INTN offset = findPattern(pattern.data(), patternMask.data(), pattern.size(), rawData, data.size(), 0);
|
||||
if (offset >= 0) {
|
||||
if (model->type(index) != Types::File) {
|
||||
UModelIndex ffs = model->findParentOfType(index, Types::File);
|
||||
if (model->type(index) == Types::Section && model->subtype(index) == EFI_SECTION_FREEFORM_SUBTYPE_GUID)
|
||||
files.insert(std::pair<UModelIndex, UModelIndex>(ffs, index));
|
||||
else
|
||||
files.insert(std::pair<UModelIndex, UModelIndex>(index, UModelIndex()));
|
||||
|
||||
break;
|
||||
files.insert(std::pair<UModelIndex, UModelIndex>(ffs, UModelIndex()));
|
||||
}
|
||||
offset = regexp.indexIn(hexBody, offset + 1);
|
||||
else
|
||||
files.insert(std::pair<UModelIndex, UModelIndex>(index, UModelIndex()));
|
||||
|
||||
}
|
||||
|
||||
return U_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,17 +17,12 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|||
#include <iterator>
|
||||
#include <set>
|
||||
|
||||
#include <QObject>
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
#include <QString>
|
||||
#include <QUuid>
|
||||
|
||||
#include "../common/basetypes.h"
|
||||
#include "../common/ustring.h"
|
||||
#include "../common/filesystem.h"
|
||||
#include "../common/ffsparser.h"
|
||||
#include "../common/ffs.h"
|
||||
#include "../common/utility.h"
|
||||
|
||||
class UEFIFind
|
||||
{
|
||||
|
@ -35,16 +30,15 @@ public:
|
|||
explicit UEFIFind();
|
||||
~UEFIFind();
|
||||
|
||||
USTATUS init(const QString & path);
|
||||
USTATUS find(const UINT8 mode, const bool count, const QString & hexPattern, QString & result);
|
||||
USTATUS init(const UString & path);
|
||||
USTATUS find(const UINT8 mode, const bool count, const UString & hexPattern, UString & result);
|
||||
|
||||
private:
|
||||
USTATUS findFileRecursive(const UModelIndex index, const QString & hexPattern, const UINT8 mode, std::set<std::pair<UModelIndex, UModelIndex> > & files);
|
||||
QString guidToQString(const UINT8* guid);
|
||||
USTATUS findFileRecursive(const UModelIndex index, const UString & hexPattern, const UINT8 mode, std::set<std::pair<UModelIndex, UModelIndex> > & files);
|
||||
UString guidToQString(const UINT8* guid);
|
||||
|
||||
FfsParser* ffsParser;
|
||||
TreeModel* model;
|
||||
QFileInfo fileInfo;
|
||||
bool initDone;
|
||||
};
|
||||
|
||||
|
|
|
@ -1,47 +0,0 @@
|
|||
QT += core
|
||||
QT -= gui
|
||||
|
||||
TARGET = UEFIFind
|
||||
TEMPLATE = app
|
||||
CONFIG += console
|
||||
CONFIG -= app_bundle
|
||||
|
||||
SOURCES += uefifind_main.cpp \
|
||||
uefifind.cpp \
|
||||
../common/guiddatabase.cpp \
|
||||
../common/types.cpp \
|
||||
../common/descriptor.cpp \
|
||||
../common/ffs.cpp \
|
||||
../common/nvram.cpp \
|
||||
../common/ffsparser.cpp \
|
||||
../common/peimage.cpp \
|
||||
../common/treeitem.cpp \
|
||||
../common/treemodel.cpp \
|
||||
../common/utility.cpp \
|
||||
../common/LZMA/LzmaDecompress.c \
|
||||
../common/LZMA/SDK/C/LzmaDec.c \
|
||||
../common/Tiano/EfiTianoDecompress.c \
|
||||
../common/ustring.cpp \
|
||||
../common/sha256.c
|
||||
|
||||
HEADERS += uefifind.h \
|
||||
../common/guiddatabase.h \
|
||||
../common/basetypes.h \
|
||||
../common/descriptor.h \
|
||||
../common/gbe.h \
|
||||
../common/me.h \
|
||||
../common/ffs.h \
|
||||
../common/nvram.h \
|
||||
../common/ffsparser.h \
|
||||
../common/peimage.h \
|
||||
../common/types.h \
|
||||
../common/treeitem.h \
|
||||
../common/treemodel.h \
|
||||
../common/utility.h \
|
||||
../common/LZMA/LzmaDecompress.h \
|
||||
../common/Tiano/EfiTianoDecompress.h \
|
||||
../common/ustring.h \
|
||||
../common/ubytearray.h \
|
||||
../common/bootguard.h \
|
||||
../common/sha256.h \
|
||||
../version.h
|
|
@ -10,44 +10,41 @@ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|||
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
||||
|
||||
*/
|
||||
#include <QCoreApplication>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "../version.h"
|
||||
#include "uefifind.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication a(argc, argv);
|
||||
a.setOrganizationName("LongSoft");
|
||||
a.setOrganizationDomain("longsoft.me");
|
||||
a.setApplicationName("UEFIFind");
|
||||
|
||||
UEFIFind w;
|
||||
UINT8 result;
|
||||
|
||||
if (a.arguments().length() == 5) {
|
||||
QString inputArg = a.arguments().at(1);
|
||||
QString modeArg = a.arguments().at(2);
|
||||
QString subModeArg = a.arguments().at(3);
|
||||
QString patternArg = a.arguments().at(4);
|
||||
if (argc == 5) {
|
||||
UString inputArg = argv[1];
|
||||
UString modeArg = argv[2];
|
||||
UString subModeArg = argv[3];
|
||||
UString patternArg = argv[4];
|
||||
|
||||
// Get search mode
|
||||
UINT8 mode;
|
||||
if (modeArg == QString("header"))
|
||||
if (modeArg == UString("header"))
|
||||
mode = SEARCH_MODE_HEADER;
|
||||
else if (modeArg == QString("body"))
|
||||
else if (modeArg == UString("body"))
|
||||
mode = SEARCH_MODE_BODY;
|
||||
else if (modeArg == QString("all"))
|
||||
else if (modeArg == UString("all"))
|
||||
mode = SEARCH_MODE_ALL;
|
||||
else
|
||||
return U_INVALID_PARAMETER;
|
||||
|
||||
// Get result type
|
||||
bool count;
|
||||
if (subModeArg == QString("list"))
|
||||
if (subModeArg == UString("list"))
|
||||
count = false;
|
||||
else if (subModeArg == QString("count"))
|
||||
else if (subModeArg == UString("count"))
|
||||
count = true;
|
||||
else
|
||||
return U_INVALID_PARAMETER;
|
||||
|
@ -58,7 +55,7 @@ int main(int argc, char *argv[])
|
|||
return result;
|
||||
|
||||
// Go find the supplied pattern
|
||||
QString found;
|
||||
UString found;
|
||||
result = w.find(mode, count, patternArg, found);
|
||||
if (result)
|
||||
return result;
|
||||
|
@ -68,26 +65,24 @@ int main(int argc, char *argv[])
|
|||
return U_ITEM_NOT_FOUND;
|
||||
|
||||
// Print result
|
||||
std::cout << found.toStdString();
|
||||
std::cout << found.toLocal8Bit();
|
||||
return U_SUCCESS;
|
||||
}
|
||||
else if (a.arguments().length() == 4) {
|
||||
QString inputArg = a.arguments().at(1);
|
||||
QString modeArg = a.arguments().at(2);
|
||||
QString patternArg = a.arguments().at(3);
|
||||
else if (argc == 4) {
|
||||
UString inputArg = argv[1];
|
||||
UString modeArg = argv[2];
|
||||
UString patternArg = argv[3];
|
||||
|
||||
// Get search mode
|
||||
if (modeArg != QString("file"))
|
||||
if (modeArg != UString("file"))
|
||||
return U_INVALID_PARAMETER;
|
||||
|
||||
// Open patterns file
|
||||
QFileInfo fileInfo(patternArg);
|
||||
if (!fileInfo.exists())
|
||||
if (!isExistOnFs(patternArg))
|
||||
return U_FILE_OPEN;
|
||||
|
||||
QFile patternsFile;
|
||||
patternsFile.setFileName(patternArg);
|
||||
if (!patternsFile.open(QFile::ReadOnly))
|
||||
std::ifstream patternsFile(patternArg.toLocal8Bit());
|
||||
if (!patternsFile)
|
||||
return U_FILE_OPEN;
|
||||
|
||||
// Parse input file
|
||||
|
@ -97,57 +92,66 @@ int main(int argc, char *argv[])
|
|||
|
||||
// Perform searches
|
||||
bool somethingFound = false;
|
||||
while (!patternsFile.atEnd()) {
|
||||
QByteArray line = patternsFile.readLine();
|
||||
while (!patternsFile.eof()) {
|
||||
std::string line;
|
||||
std::getline(patternsFile, line);
|
||||
// Use sharp symbol as commentary
|
||||
if (line.count() == 0 || line[0] == '#')
|
||||
if (line.size() == 0 || line[0] == '#')
|
||||
continue;
|
||||
|
||||
// Split the read line
|
||||
QList<QByteArray> list = line.split(' ');
|
||||
if (list.count() < 3) {
|
||||
std::cout << line.constData() << "skipped, too few arguments" << std::endl << std::endl;
|
||||
std::vector<UString> list;
|
||||
std::string::size_type prev = 0, curr = 0;
|
||||
while ((curr = line.find(' ', curr)) != std::string::npos) {
|
||||
std::string substring( line.substr(prev, curr-prev) );
|
||||
list.push_back(UString(substring.c_str()));
|
||||
prev = ++curr;
|
||||
}
|
||||
list.push_back(UString(line.substr(prev, curr-prev).c_str()));
|
||||
|
||||
if (list.size() < 3) {
|
||||
std::cout << line << std::endl << "skipped, too few arguments" << std::endl << std::endl;
|
||||
continue;
|
||||
}
|
||||
// Get search mode
|
||||
UINT8 mode;
|
||||
if (list.at(0) == QString("header"))
|
||||
if (list.at(0) == UString("header"))
|
||||
mode = SEARCH_MODE_HEADER;
|
||||
else if (list.at(0) == QString("body"))
|
||||
else if (list.at(0) == UString("body"))
|
||||
mode = SEARCH_MODE_BODY;
|
||||
else if (list.at(0) == QString("all"))
|
||||
else if (list.at(0) == UString("all"))
|
||||
mode = SEARCH_MODE_ALL;
|
||||
else {
|
||||
std::cout << line.constData() << "skipped, invalid search mode" << std::endl << std::endl;
|
||||
std::cout << line << std::endl << "skipped, invalid search mode" << std::endl << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get result type
|
||||
bool count;
|
||||
if (list.at(1) == QString("list"))
|
||||
if (list.at(1) == UString("list"))
|
||||
count = false;
|
||||
else if (list.at(1) == QString("count"))
|
||||
else if (list.at(1) == UString("count"))
|
||||
count = true;
|
||||
else {
|
||||
std::cout << line.constData() << "skipped, invalid result type" << std::endl << std::endl;
|
||||
std::cout << line << std::endl << "skipped, invalid result type" << std::endl << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Go find the supplied pattern
|
||||
QString found;
|
||||
UString found;
|
||||
result = w.find(mode, count, list.at(2), found);
|
||||
if (result) {
|
||||
std::cout << line.constData() << "skipped, find failed with error " << (UINT32)result << std::endl << std::endl;
|
||||
std::cout << line << std::endl << "skipped, find failed with error " << (UINT32)result << std::endl << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (found.isEmpty()) {
|
||||
// Nothing is found
|
||||
std::cout << line.constData() << "nothing found" << std::endl << std::endl;
|
||||
std::cout << line << std::endl << "nothing found" << std::endl << std::endl;
|
||||
}
|
||||
else {
|
||||
// Print result
|
||||
std::cout << line.constData() << found.toStdString() << std::endl;
|
||||
std::cout << line << std::endl << found.toLocal8Bit() << std::endl;
|
||||
somethingFound = true;
|
||||
}
|
||||
}
|
||||
|
@ -165,4 +169,3 @@ int main(int argc, char *argv[])
|
|||
return U_INVALID_PARAMETER;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue