From 4d50d581fa84b9ca944dc12181f4fdb97e4c039c Mon Sep 17 00:00:00 2001 From: vit9696 Date: Thu, 2 Aug 2018 03:41:11 +0300 Subject: [PATCH] Remove Qt deps from UEFIFind and fix issues --- .gitignore | 3 + UEFIDump/CMakeLists.txt | 2 + UEFIDump/uefidump.cpp | 16 +++--- UEFIDump/uefidump_main.cpp | 5 +- UEFIExtract/CMakeLists.txt | 2 + UEFIExtract/ffsdumper.cpp | 40 ++++++------- UEFIExtract/uefiextract_main.cpp | 25 ++++---- UEFIFind/uefifind.cpp | 84 ++++++++++++++------------- UEFIFind/uefifind.h | 20 +++---- UEFIFind/uefifind.pro | 47 --------------- UEFIFind/uefifind_main.cpp | 95 ++++++++++++++++--------------- common/Tiano/EfiTianoDecompress.c | 10 +--- common/basetypes.h | 1 + common/bstrlib/bstrlib.c | 16 +++--- common/bstrlib/bstrwrap.cpp | 7 ++- common/bstrlib/bstrwrap.h | 2 +- common/filesystem.h | 12 ++-- common/nvramparser.cpp | 30 +++++++--- common/treemodel.cpp | 6 +- common/utility.cpp | 68 ++++++++++++++++++++++ common/utility.h | 9 +++ 21 files changed, 275 insertions(+), 225 deletions(-) delete mode 100644 UEFIFind/uefifind.pro diff --git a/.gitignore b/.gitignore index 161f117..d197107 100644 --- a/.gitignore +++ b/.gitignore @@ -243,3 +243,6 @@ CMakeFiles cmake_install.cmake DerivedData *.xcodeproj +compile_commands.json +CMakeLists.txt +CMakeScripts diff --git a/UEFIDump/CMakeLists.txt b/UEFIDump/CMakeLists.txt index 945509f..cf8d1ce 100644 --- a/UEFIDump/CMakeLists.txt +++ b/UEFIDump/CMakeLists.txt @@ -1,3 +1,5 @@ +cmake_minimum_required(VERSION 3.0) + PROJECT(UEFIDump) SET(PROJECT_SOURCES diff --git a/UEFIDump/uefidump.cpp b/UEFIDump/uefidump.cpp index c72bba7..e2830b8 100644 --- a/UEFIDump/uefidump.cpp +++ b/UEFIDump/uefidump.cpp @@ -54,12 +54,12 @@ USTATUS UEFIDumper::dump(const UByteArray & buffer, const UString & inPath, cons std::cout << " Address | Size | Ver | CS | Type / Info " << std::endl; std::cout << "-------------------------------------------------------------------------" << std::endl; for (size_t i = 0; i < fitTable.size(); i++) { - std::cout << (const char*)fitTable[i].first[0].toLocal8Bit() << " | " - << (const char*)fitTable[i].first[1].toLocal8Bit() << " | " - << (const char*)fitTable[i].first[2].toLocal8Bit() << " | " - << (const char*)fitTable[i].first[3].toLocal8Bit() << " | " - << (const char*)fitTable[i].first[4].toLocal8Bit() << " | " - << (const char*)fitTable[i].first[5].toLocal8Bit() << std::endl; + std::cout << fitTable[i].first[0].toLocal8Bit() << " | " + << fitTable[i].first[1].toLocal8Bit() << " | " + << fitTable[i].first[2].toLocal8Bit() << " | " + << fitTable[i].first[3].toLocal8Bit() << " | " + << fitTable[i].first[4].toLocal8Bit() << " | " + << fitTable[i].first[5].toLocal8Bit() << std::endl; } } @@ -68,9 +68,9 @@ USTATUS UEFIDumper::dump(const UByteArray & buffer, const UString & inPath, cons std::vector report = ffsReport.generate(); if (report.size()) { std::ofstream ofs; - ofs.open((const char*)reportPath, std::ofstream::out); + ofs.open(reportPath, std::ofstream::out); for (size_t i = 0; i < report.size(); i++) { - ofs << (const char*)report[i].toLocal8Bit() << std::endl; + ofs << report[i].toLocal8Bit() << std::endl; } ofs.close(); } diff --git a/UEFIDump/uefidump_main.cpp b/UEFIDump/uefidump_main.cpp index 0ccd4b0..3a61505 100644 --- a/UEFIDump/uefidump_main.cpp +++ b/UEFIDump/uefidump_main.cpp @@ -20,8 +20,9 @@ int main(int argc, char *argv[]) { if (argc > 1) { - std::ifstream inputFile; - inputFile.open(argv[1], std::ios::in | std::ios::binary); + std::ifstream inputFile(argv[1], std::ios::in | std::ios::binary); + if (!inputFile) + return U_FILE_OPEN; std::vector buffer(std::istreambuf_iterator(inputFile), (std::istreambuf_iterator())); inputFile.close(); diff --git a/UEFIExtract/CMakeLists.txt b/UEFIExtract/CMakeLists.txt index c26f5ac..29f23ba 100644 --- a/UEFIExtract/CMakeLists.txt +++ b/UEFIExtract/CMakeLists.txt @@ -1,3 +1,5 @@ +cmake_minimum_required(VERSION 3.0) + PROJECT(UEFIExtract) SET(PROJECT_SOURCES diff --git a/UEFIExtract/ffsdumper.cpp b/UEFIExtract/ffsdumper.cpp index b8dd12c..42cdac2 100644 --- a/UEFIExtract/ffsdumper.cpp +++ b/UEFIExtract/ffsdumper.cpp @@ -53,11 +53,11 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path if (!model->header(index).isEmpty() && (sectionType == IgnoreSectionType || model->subtype(index) == sectionType)) { UString filename; if (counterHeader == 0) - filename = usprintf("%s/header.bin", (const char *)path.toLocal8Bit()); + filename = usprintf("%s/header.bin", path.toLocal8Bit()); else - filename = usprintf("%s/header_%d.bin", (const char *)path.toLocal8Bit(), counterHeader); + filename = usprintf("%s/header_%d.bin", path.toLocal8Bit(), counterHeader); counterHeader++; - file.open((const char *)filename.toLocal8Bit(), std::ofstream::binary); + file.open(filename.toLocal8Bit(), std::ofstream::binary); const UByteArray &data = model->header(index); file.write(data.constData(), data.size()); file.close(); @@ -68,11 +68,11 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path if (!model->body(index).isEmpty() && (sectionType == IgnoreSectionType || model->subtype(index) == sectionType)) { UString filename; if (counterBody == 0) - filename = usprintf("%s/body.bin", (const char *)path.toLocal8Bit()); + filename = usprintf("%s/body.bin", path.toLocal8Bit()); else - filename = usprintf("%s/body_%d.bin", (const char *)path.toLocal8Bit(), counterBody); + filename = usprintf("%s/body_%d.bin", path.toLocal8Bit(), counterBody); counterBody++; - file.open((const char *)filename.toLocal8Bit(), std::ofstream::binary); + file.open(filename.toLocal8Bit(), std::ofstream::binary); const UByteArray &data = model->body(index); file.write(data.constData(), data.size()); file.close(); @@ -85,11 +85,11 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path fileIndex = index; UString filename; if (counterRaw == 0) - filename = usprintf("%s/file.ffs", (const char *)path.toLocal8Bit()); + filename = usprintf("%s/file.ffs", path.toLocal8Bit()); else - filename = usprintf("%s/file_%d.bin", (const char *)path.toLocal8Bit(), counterRaw); + filename = usprintf("%s/file_%d.bin", path.toLocal8Bit(), counterRaw); counterRaw++; - file.open((const char *)filename.toLocal8Bit(), std::ofstream::binary); + file.open(filename.toLocal8Bit(), std::ofstream::binary); const UByteArray &headerData = model->header(index); const UByteArray &bodyData = model->body(index); const UByteArray &tailData = model->tail(index); @@ -104,19 +104,19 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path if ((dumpMode == DUMP_ALL || dumpMode == DUMP_CURRENT || dumpMode == DUMP_INFO) && (sectionType == IgnoreSectionType || model->subtype(index) == sectionType)) { UString info = usprintf("Type: %s\nSubtype: %s\n%s%s\n", - (const char *)itemTypeToUString(model->type(index)).toLocal8Bit(), - (const char *)itemSubtypeToUString(model->type(index), model->subtype(index)).toLocal8Bit(), - (const char *)(model->text(index).isEmpty() ? UString("") : - usprintf("Text: %s\n", (const char *)model->text(index).toLocal8Bit())).toLocal8Bit(), - (const char *)model->info(index).toLocal8Bit()); + itemTypeToUString(model->type(index)).toLocal8Bit(), + itemSubtypeToUString(model->type(index), model->subtype(index)).toLocal8Bit(), + (model->text(index).isEmpty() ? UString("") : + usprintf("Text: %s\n", model->text(index).toLocal8Bit())).toLocal8Bit(), + model->info(index).toLocal8Bit()); UString filename; if (counterInfo == 0) - filename = usprintf("%s/info.txt", (const char *)path.toLocal8Bit()); + filename = usprintf("%s/info.txt", path.toLocal8Bit()); else - filename = usprintf("%s/info_%d.txt", (const char *)path.toLocal8Bit(), counterInfo); + filename = usprintf("%s/info_%d.txt", path.toLocal8Bit(), counterInfo); counterInfo++; - file.open((const char *)filename.toLocal8Bit()); - file << (const char *)info.toLocal8Bit(); + file.open(filename.toLocal8Bit()); + file << info.toLocal8Bit(); file.close(); } @@ -132,8 +132,8 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path UString childPath = path; if (dumpMode == DUMP_ALL || dumpMode == DUMP_CURRENT) - childPath = usprintf("%s/%d %s", (const char *)path.toLocal8Bit(), i, - (const char *)(useText ? model->text(childIndex) : model->name(childIndex)).toLocal8Bit()); + childPath = usprintf("%s/%d %s", path.toLocal8Bit(), i, + (useText ? model->text(childIndex) : model->name(childIndex)).toLocal8Bit()); result = recursiveDump(childIndex, childPath, dumpMode, sectionType, guid); if (result) return result; diff --git a/UEFIExtract/uefiextract_main.cpp b/UEFIExtract/uefiextract_main.cpp index 672a3a3..928f2a0 100644 --- a/UEFIExtract/uefiextract_main.cpp +++ b/UEFIExtract/uefiextract_main.cpp @@ -37,8 +37,9 @@ int main(int argc, char *argv[]) return U_FILE_OPEN; // Open the input file - std::ifstream inputFile; - inputFile.open(argv[1], std::ios::in | std::ios::binary); + std::ifstream inputFile(argv[1], std::ios::in | std::ios::binary); + if (!inputFile) + return U_FILE_OPEN; std::vector buffer(std::istreambuf_iterator(inputFile), (std::istreambuf_iterator())); inputFile.close(); @@ -54,7 +55,7 @@ int main(int argc, char *argv[]) // Show ffsParser's messages std::vector > messages = ffsParser.getMessages(); for (size_t i = 0; i < messages.size(); i++) { - std::cout << (const char *)messages[i].first.toLocal8Bit() << std::endl; + std::cout << messages[i].first.toLocal8Bit() << std::endl; } // Get last VTF @@ -64,12 +65,12 @@ int main(int argc, char *argv[]) std::cout << " Address | Size | Ver | CS | Type / Info " << std::endl; std::cout << "---------------------------------------------------------------------------" << std::endl; for (size_t i = 0; i < fitTable.size(); i++) { - std::cout << (const char *)fitTable[i].first[0].toLocal8Bit() << " | " - << (const char *)fitTable[i].first[1].toLocal8Bit() << " | " - << (const char *)fitTable[i].first[2].toLocal8Bit() << " | " - << (const char *)fitTable[i].first[3].toLocal8Bit() << " | " - << (const char *)fitTable[i].first[4].toLocal8Bit() << " | " - << (const char *)fitTable[i].first[5].toLocal8Bit() << std::endl; + std::cout << fitTable[i].first[0].toLocal8Bit() << " | " + << fitTable[i].first[1].toLocal8Bit() << " | " + << fitTable[i].first[2].toLocal8Bit() << " | " + << fitTable[i].first[3].toLocal8Bit() << " | " + << fitTable[i].first[4].toLocal8Bit() << " | " + << fitTable[i].first[5].toLocal8Bit() << std::endl; } } @@ -140,7 +141,7 @@ int main(int argc, char *argv[]) UINT8 type = sectionTypes.empty() ? FfsDumper::IgnoreSectionType : sectionTypes[i]; result = ffsDumper.dump(model.index(0, 0), outPath, mode, type, inputs[i]); if (result) { - std::cout << "Guid " << (const char *)inputs[i].toLocal8Bit() << " failed with " << result << " code!" << std::endl; + std::cout << "Guid " << inputs[i].toLocal8Bit() << " failed with " << result << " code!" << std::endl; lastError = result; } } @@ -153,9 +154,9 @@ int main(int argc, char *argv[]) std::vector report = ffsReport.generate(); if (report.size()) { std::ofstream file; - file.open((const char *)(path + UString(".report.txt")).toLocal8Bit()); + file.open((path + UString(".report.txt")).toLocal8Bit()); for (size_t i = 0; i < report.size(); i++) - file << (const char *)report[i].toLocal8Bit() << '\n'; + file << report[i].toLocal8Bit() << '\n'; } // Dump all non-leaf elements, with report, default diff --git a/UEFIFind/uefifind.cpp b/UEFIFind/uefifind.cpp index 563b066..141ee78 100644 --- a/UEFIFind/uefifind.cpp +++ b/UEFIFind/uefifind.cpp @@ -13,6 +13,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "uefifind.h" +#include + 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 buffer(std::istreambuf_iterator(inputFile), + (std::istreambuf_iterator())); 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 > 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 >::const_iterator citer = files.begin(); citer != files.end(); ++citer) { - QByteArray data(16, '\x00'); + UByteArray data(16, '\x00'); std::pair 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 > & files) +USTATUS UEFIFind::findFileRecursive(const UModelIndex index, const UString & hexPattern, const UINT8 mode, std::set > & 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 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(ffs, index)); - else - files.insert(std::pair(ffs, UModelIndex())); - } + const UINT8 *rawData = reinterpret_cast(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(ffs, index)); else - files.insert(std::pair(index, UModelIndex())); - - break; + files.insert(std::pair(ffs, UModelIndex())); } - offset = regexp.indexIn(hexBody, offset + 1); + else + files.insert(std::pair(index, UModelIndex())); + } return U_SUCCESS; -} \ No newline at end of file +} diff --git a/UEFIFind/uefifind.h b/UEFIFind/uefifind.h index 39b052c..420446e 100644 --- a/UEFIFind/uefifind.h +++ b/UEFIFind/uefifind.h @@ -17,17 +17,12 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include -#include -#include -#include -#include -#include -#include -#include - #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 > & files); - QString guidToQString(const UINT8* guid); + USTATUS findFileRecursive(const UModelIndex index, const UString & hexPattern, const UINT8 mode, std::set > & files); + UString guidToQString(const UINT8* guid); FfsParser* ffsParser; TreeModel* model; - QFileInfo fileInfo; bool initDone; }; diff --git a/UEFIFind/uefifind.pro b/UEFIFind/uefifind.pro deleted file mode 100644 index 4880eec..0000000 --- a/UEFIFind/uefifind.pro +++ /dev/null @@ -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 diff --git a/UEFIFind/uefifind_main.cpp b/UEFIFind/uefifind_main.cpp index f4e7a35..2b4aa81 100644 --- a/UEFIFind/uefifind_main.cpp +++ b/UEFIFind/uefifind_main.cpp @@ -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 #include +#include +#include +#include #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 list = line.split(' '); - if (list.count() < 3) { - std::cout << line.constData() << "skipped, too few arguments" << std::endl << std::endl; + std::vector 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; } } - diff --git a/common/Tiano/EfiTianoDecompress.c b/common/Tiano/EfiTianoDecompress.c index 2e58f64..eb59272 100644 --- a/common/Tiano/EfiTianoDecompress.c +++ b/common/Tiano/EfiTianoDecompress.c @@ -646,13 +646,9 @@ Decode ( SCRATCH_DATA *Sd ) { - UINT16 BytesRemain; - UINT32 DataIdx; - UINT16 CharC; - - BytesRemain = (UINT16)(-1); - - DataIdx = 0; + UINT16 BytesRemain = (UINT16)(-1); + UINT32 DataIdx = 0; + UINT16 CharC = 0; for (;;) { CharC = DecodeC(Sd); diff --git a/common/basetypes.h b/common/basetypes.h index 6afc5f8..0a8407d 100644 --- a/common/basetypes.h +++ b/common/basetypes.h @@ -81,6 +81,7 @@ typedef uint64_t UINT64; typedef char CHAR8; typedef uint16_t CHAR16; typedef size_t UINTN; +typedef ptrdiff_t INTN; #define CONST const #define VOID void diff --git a/common/bstrlib/bstrlib.c b/common/bstrlib/bstrlib.c index 82fbc05..5eaada2 100644 --- a/common/bstrlib/bstrlib.c +++ b/common/bstrlib/bstrlib.c @@ -2432,7 +2432,7 @@ int i, c, v; * NULL is returned, otherwise a bstring with the correct result is returned. */ bstring bjoin (const struct bstrList * bl, const_bstring sep) { - if (sep != NULL && (sep->slen < 0 || sep->data == NULL)) return NULL; + if (sep == NULL || (sep->slen < 0 || sep->data == NULL)) return NULL; return bjoinblk (bl, sep->data, sep->slen); } @@ -2458,9 +2458,9 @@ bstring bjoin (const struct bstrList * bl, const_bstring sep) { */ int bssplitscb (struct bStream * s, const_bstring splitStr, int (* cb) (void * parm, int ofs, const_bstring entry), void * parm) { -struct charField chrs; -bstring buff; -int i, p, ret; + struct charField chrs; + bstring buff; + int i = 0, p = 0, ret = 0; if (cb == NULL || s == NULL || s->readFnPtr == NULL || splitStr == NULL || splitStr->slen < 0) return BSTR_ERR; @@ -2473,7 +2473,6 @@ int i, p, ret; ret = 0; } else { buildCharField (&chrs, splitStr); - ret = p = i = 0; for (;;) { if (i >= buff->slen) { bsreada (buff, s, BSSSC_BUFF_LEN); @@ -2525,8 +2524,8 @@ int i, p, ret; */ int bssplitstrcb (struct bStream * s, const_bstring splitStr, int (* cb) (void * parm, int ofs, const_bstring entry), void * parm) { -bstring buff; -int i, p, ret; + bstring buff; + int i = 0, p = 0, ret = 0; if (cb == NULL || s == NULL || s->readFnPtr == NULL || splitStr == NULL || splitStr->slen < 0) return BSTR_ERR; @@ -2545,8 +2544,7 @@ int i, p, ret; } return BSTR_OK; } else { - ret = p = i = 0; - for (i=p=0;;) { + for (;;) { if ((ret = binstr (buff, 0, splitStr)) >= 0) { struct tagbstring t; blk2tbstr (t, buff->data, ret); diff --git a/common/bstrlib/bstrwrap.cpp b/common/bstrlib/bstrwrap.cpp index 2ec38c1..d0b2a83 100644 --- a/common/bstrlib/bstrwrap.cpp +++ b/common/bstrlib/bstrwrap.cpp @@ -1490,9 +1490,10 @@ int p, i; } static int streamSplitCb (void * parm, int ofs, const_bstring entry) { -CBStringList * r = (CBStringList *) parm; - - ofs = ofs; + CBStringList * r = (CBStringList *) parm; + //FIXME: this is broken! + // ofs = ofs; + (void)ofs; r->push_back (CBString (*entry)); return 0; } diff --git a/common/bstrlib/bstrwrap.h b/common/bstrlib/bstrwrap.h index e672d4d..781c018 100644 --- a/common/bstrlib/bstrwrap.h +++ b/common/bstrlib/bstrwrap.h @@ -362,7 +362,7 @@ struct CBString : public tagbstring { int read (bNread readPtr, void * parm); // QString compatibility methods - CBString toLocal8Bit() const { return *this; } + const char *toLocal8Bit() const { return *this; } bool isEmpty() const { return slen == 0; } void clear() { *this = ""; } CBString left(int len) const { return midstr(0, len); } diff --git a/common/filesystem.h b/common/filesystem.h index 904cab6..1685a08 100644 --- a/common/filesystem.h +++ b/common/filesystem.h @@ -22,29 +22,29 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include static inline bool isExistOnFs(const UString & path) { struct _stat buf; - return (_stat((const char*)path.toLocal8Bit(), &buf) == 0); + return (_stat(path.toLocal8Bit(), &buf) == 0); } static inline bool makeDirectory(const UString & dir) { - return (_mkdir((const char*)dir.toLocal8Bit()) == 0); + return (_mkdir(dir.toLocal8Bit()) == 0); } static inline bool changeDirectory(const UString & dir) { - return (_chdir((const char*)dir.toLocal8Bit()) == 0); + return (_chdir(dir.toLocal8Bit()) == 0); } #else #include static inline bool isExistOnFs(const UString & path) { struct stat buf; - return (stat((const char*)path.toLocal8Bit(), &buf) == 0); + return (stat(path.toLocal8Bit(), &buf) == 0); } static inline bool makeDirectory(const UString & dir) { - return (mkdir((const char*)dir.toLocal8Bit(), ACCESSPERMS) == 0); + return (mkdir(dir.toLocal8Bit(), ACCESSPERMS) == 0); } static inline bool changeDirectory(const UString & dir) { - return (chdir((const char*)dir.toLocal8Bit()) == 0); + return (chdir(dir.toLocal8Bit()) == 0); } #endif diff --git a/common/nvramparser.cpp b/common/nvramparser.cpp index 6a09052..bced44f 100644 --- a/common/nvramparser.cpp +++ b/common/nvramparser.cpp @@ -501,12 +501,26 @@ USTATUS NvramParser::parseNvramVolumeBody(const UModelIndex & index) for (int i = 0; i < model->rowCount(index); i++) { UModelIndex current = index.child(i, 0); switch (model->type(current)) { - case Types::FdcStore: return parseFdcStoreBody(current); - case Types::VssStore: return parseVssStoreBody(current, 0); - case Types::Vss2Store: return parseVssStoreBody(current, 4); - case Types::FsysStore: return parseFsysStoreBody(current); - case Types::EvsaStore: return parseEvsaStoreBody(current); - case Types::FlashMapStore: return parseFlashMapBody(current); + case Types::FdcStore: + parseFdcStoreBody(current); + break; + case Types::VssStore: + parseVssStoreBody(current, 0); + break; + case Types::Vss2Store: + parseVssStoreBody(current, 4); + break; + case Types::FsysStore: + parseFsysStoreBody(current); + break; + case Types::EvsaStore: + parseEvsaStoreBody(current); + break; + case Types::FlashMapStore: + parseFlashMapBody(current); + break; + default: + // Ignore unknown! } } @@ -1197,8 +1211,8 @@ USTATUS NvramParser::parseSlicMarkerHeader(const UByteArray & store, const UINT3 markerHeader->Size, markerHeader->Size, header.size(), header.size(), markerHeader->Version, - (const char*)UString((const char*)&(markerHeader->OemId)).left(6).toLocal8Bit(), - (const char*)UString((const char*)&(markerHeader->OemTableId)).left(8).toLocal8Bit(), + UString((const char*)&(markerHeader->OemId)).left(6).toLocal8Bit(), + UString((const char*)&(markerHeader->OemTableId)).left(8).toLocal8Bit(), markerHeader->SlicVersion); diff --git a/common/treemodel.cpp b/common/treemodel.cpp index e34f3fd..aa34478 100644 --- a/common/treemodel.cpp +++ b/common/treemodel.cpp @@ -24,7 +24,7 @@ QVariant TreeModel::data(const UModelIndex &index, int role) const TreeItem *item = static_cast(index.internalPointer()); if (role == Qt::DisplayRole) { - return (const char*)item->data(index.column()).toLocal8Bit(); + return item->data(index.column()).toLocal8Bit(); } #if defined (QT_GUI_LIB) else if (role == Qt::BackgroundRole) { @@ -34,7 +34,7 @@ QVariant TreeModel::data(const UModelIndex &index, int role) const } #endif else if (role == Qt::UserRole) { - return (const char*)item->info().toLocal8Bit(); + return item->info().toLocal8Bit(); } return QVariant(); @@ -556,4 +556,4 @@ goDeeper: } return (parentIndex == index(0, 0) ? UModelIndex() : parentIndex); -} \ No newline at end of file +} diff --git a/common/utility.cpp b/common/utility.cpp index 2c07879..2042836 100644 --- a/common/utility.cpp +++ b/common/utility.cpp @@ -11,6 +11,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. */ +#include +#include + #include "treemodel.h" #include "utility.h" #include "ffs.h" @@ -375,3 +378,68 @@ UINT8 getPaddingType(const UByteArray & padding) return Subtypes::OnePadding; return Subtypes::DataPadding; } + +static inline int char2hex(char c) { + if (c >= '0' && c <= '9') + return c - '0'; + if (c >= 'A' && c <= 'F') + return c - 'A'; + if (c == '.') + return -2; + return -1; +} + +INTN findPattern(const UINT8 *pattern, const UINT8 *patternMask, UINTN patternSize, + const UINT8 *data, UINTN dataSize, UINTN dataOff) { + if (patternSize == 0 || dataSize == 0 || dataOff >= dataSize || dataSize - dataOff < patternSize) + return -1; + + while (dataOff + patternSize < dataSize) { + BOOLEAN matches = TRUE; + for (UINTN i = 0; i < patternSize; i++) { + if ((data[dataOff + i] & patternMask[i]) != pattern[i]) { + matches = FALSE; + break; + } + } + + if (matches) + return static_cast(dataOff); + + dataOff++; + } + + return -1; +} + +BOOLEAN makePattern(const CHAR8 *textPattern, std::vector &pattern, std::vector &patternMask) { + UINTN len = std::strlen(textPattern); + + if (len == 0 || len % 2 != 0) + return FALSE; + + len /= 2; + + pattern.resize(len); + patternMask.resize(len); + + for (UINTN i = 0; i < len; i++) { + int v1 = char2hex(std::toupper(textPattern[i * 2])); + int v2 = char2hex(std::toupper(textPattern[i * 2 + 1])); + + if (v1 == -1 || v2 == -1) + return FALSE; + + if (v1 != -2) { + patternMask[i] = 0xF0; + pattern[i] = static_cast(v1) << 4; + } + + if (v2 != -2) { + patternMask[i] |= 0x0F; + pattern[i] |= static_cast(v2); + } + } + + return TRUE; +} diff --git a/common/utility.h b/common/utility.h index 792bca8..ef768aa 100644 --- a/common/utility.h +++ b/common/utility.h @@ -14,6 +14,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef UTILITY_H #define UTILITY_H +#include + #include "basetypes.h" #include "ustring.h" #include "treemodel.h" @@ -46,4 +48,11 @@ UINT16 calculateChecksum16(const UINT16* buffer, UINT32 bufferSize); // Return padding type from it's contents UINT8 getPaddingType(const UByteArray & padding); +// Make pattern from a hexstring with an assumption of . being any char +BOOLEAN makePattern(const CHAR8 *textPattern, std::vector &pattern, std::vector &patternMask); + +// Find pattern in a binary blob +INTN findPattern(const UINT8 *pattern, const UINT8 *patternMask, UINTN patternSize, + const UINT8 *data, UINTN dataSize, UINTN dataOff); + #endif // UTILITY_H