Fix Unicode search

This commit is contained in:
Nikolaj Schlej 2023-02-16 22:11:39 -08:00
parent cb6ef45d0c
commit 6f9dc0ab88
9 changed files with 222 additions and 283 deletions

View File

@ -17,8 +17,6 @@ SET(PROJECT_SOURCES
../common/nvramparser.cpp
../common/ffsparser.cpp
../common/fitparser.cpp
../common/ffsreport.cpp
../common/ffsutils.cpp
../common/peimage.cpp
../common/treeitem.cpp
../common/treemodel.cpp
@ -59,8 +57,6 @@ SET(PROJECT_SOURCES
../common/zlib/zutil.c
)
ADD_DEFINITIONS(-DU_ENABLE_NVRAM_PARSING_SUPPORT -DU_ENABLE_FIT_PARSING_SUPPORT -DU_ENABLE_GUID_DATABASE_SUPPORT)
ADD_EXECUTABLE(UEFIFind ${PROJECT_SOURCES})
IF(UNIX)

View File

@ -12,9 +12,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
*/
#include "uefifind.h"
#include "../common/ffsutils.h"
#include <fstream>
#include <set>
UEFIFind::UEFIFind()
{
@ -46,6 +46,74 @@ USTATUS UEFIFind::init(const UString & path)
return U_SUCCESS;
}
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;
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
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.model()->index(i, index.column(), index), hexPattern, mode, files);
}
// TODO: handle a case where an item has both compressed and uncompressed bodies
UByteArray data;
if (hasChildren) {
if (mode == SEARCH_MODE_HEADER)
data = model->header(index);
else if (mode == SEARCH_MODE_ALL)
data = model->header(index) + model->body(index);
}
else {
if (mode == SEARCH_MODE_HEADER)
data = model->header(index);
else if (mode == SEARCH_MODE_BODY)
data = model->body(index);
else
data = model->header(index) + model->body(index);
}
const UINT8 *rawData = reinterpret_cast<const UINT8 *>(data.constData());
INTN offset = findPattern(pattern.data(), patternMask.data(), pattern.size(), rawData, data.size(), 0);
// For patterns that cross header|body boundary, skip patterns entirely located in body, since
// children search above has already found them.
if (hasChildren && mode == SEARCH_MODE_ALL && offset >= model->header(index).size()) {
offset = -1;
}
if (offset >= 0) {
if (model->type(index) != Types::File) {
UModelIndex parentFile = 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>(parentFile, index));
else
files.insert(std::pair<UModelIndex, UModelIndex>(parentFile, UModelIndex()));
}
else {
files.insert(std::pair<UModelIndex, UModelIndex>(index, UModelIndex()));
}
}
return U_SUCCESS;
}
USTATUS UEFIFind::find(const UINT8 mode, const bool count, const UString & hexPattern, UString & result)
{
UModelIndex root = model->index(0, 0);
@ -53,7 +121,7 @@ USTATUS UEFIFind::find(const UINT8 mode, const bool count, const UString & hexPa
result.clear();
USTATUS returned = FfsUtils::findFileRecursive(model, root, hexPattern, mode, files);
USTATUS returned = findFileRecursive(root, hexPattern, mode, files);
if (returned)
return returned;

View File

@ -34,6 +34,8 @@ public:
USTATUS find(const UINT8 mode, const bool count, const UString & hexPattern, UString & result);
private:
USTATUS findFileRecursive(const UModelIndex index, const UString & hexPattern, const UINT8 mode, std::set<std::pair<UModelIndex, UModelIndex> > & files);
FfsParser* ffsParser;
TreeModel* model;
bool initDone;

View File

@ -52,7 +52,6 @@ SET(PROJECT_SOURCES
../common/ffsbuilder.cpp
../common/ffsparser.cpp
../common/ffsreport.cpp
../common/ffsutils.cpp
../common/treeitem.cpp
../common/treemodel.cpp
../common/LZMA/LzmaCompress.c

View File

@ -21,8 +21,6 @@
USTATUS FfsFinder::findHexPattern(const UModelIndex & index, const UByteArray & hexPattern, const UINT8 mode)
{
// TODO: use FfsUtils.
if (!index.isValid())
return U_SUCCESS;
@ -57,115 +55,20 @@ USTATUS FfsFinder::findHexPattern(const UModelIndex & index, const UByteArray &
UString hexBody = UString(data.toHex());
#if QT_VERSION_MAJOR >= 6
QRegularExpression regexp = QRegularExpression(UString(hexPattern));
regexp.setPatternOptions((QRegularExpression::PatternOptions)0x1);
regexp.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
QRegularExpressionMatch regexpmatch;
INT32 offset = 0;
while ((offset = (INT32)hexBody.indexOf(regexp, (qsizetype)offset, &regexpmatch)) != -1) {
INT32 offset = (INT32)hexBody.indexOf(regexp, 0, &regexpmatch);
#else
QRegExp regexp = QRegExp(UString(hexPattern), Qt::CaseInsensitive);
INT32 offset = regexp.indexIn(hexBody);
#endif
while (offset >= 0) {
#endif
if (offset % 2 == 0) {
// For patterns that cross header|body boundary, skip patterns entirely located in body, since
// children search above has already found them.
if (!(hasChildren && mode == SEARCH_MODE_ALL && offset/2 >= model->header(index).size())) {
UModelIndex parentFileIndex = model->findParentOfType(index, Types::File);
UString name = model->name(index);
if (model->parent(index) == parentFileIndex) {
name = model->name(parentFileIndex) + UString("/") + name;
}
else if (parentFileIndex.isValid()) {
name = model->name(parentFileIndex) + UString("/.../") + name;
}
msg(UString("Hex pattern \"") + UString(hexPattern)
+ UString("\" found as \"") + hexBody.mid(offset, hexPattern.length()).toUpper()
+ UString("\" in ") + name
+ usprintf(" at %s-offset %02Xh", mode == SEARCH_MODE_BODY ? "body" : "header", offset / 2),
index);
}
}
#if QT_VERSION_MAJOR >= 6
offset += 1;
#else
offset = regexp.indexIn(hexBody, offset + 1);
#endif
}
return U_SUCCESS;
}
USTATUS FfsFinder::findGuidPattern(const UModelIndex & index, const UByteArray & guidPattern, const UINT8 mode)
{
if (guidPattern.isEmpty())
return U_INVALID_PARAMETER;
if (!index.isValid())
return U_SUCCESS;
bool hasChildren = (model->rowCount(index) > 0);
for (int i = 0; i < model->rowCount(index); i++) {
findGuidPattern(index.model()->index(i, index.column(), index), guidPattern, mode);
}
UByteArray data;
if (hasChildren) {
if (mode != SEARCH_MODE_BODY)
data = model->header(index);
}
else {
if (mode == SEARCH_MODE_HEADER)
data.append(model->header(index));
else if (mode == SEARCH_MODE_BODY)
data.append(model->body(index));
else
data.append(model->header(index)).append(model->body(index));
}
UString hexBody = UString(data.toHex());
QList<UByteArray> list = guidPattern.split('-');
if (list.count() != 5)
return U_INVALID_PARAMETER;
UByteArray hexPattern;
// Reverse first GUID block
hexPattern.append(list.at(0).mid(6, 2));
hexPattern.append(list.at(0).mid(4, 2));
hexPattern.append(list.at(0).mid(2, 2));
hexPattern.append(list.at(0).mid(0, 2));
// Reverse second GUID block
hexPattern.append(list.at(1).mid(2, 2));
hexPattern.append(list.at(1).mid(0, 2));
// Reverse third GUID block
hexPattern.append(list.at(2).mid(2, 2));
hexPattern.append(list.at(2).mid(0, 2));
// Append fourth and fifth GUID blocks as is
hexPattern.append(list.at(3)).append(list.at(4));
// Check for "all substrings" pattern
if (hexPattern.count('.') == hexPattern.length())
return U_SUCCESS;
#if QT_VERSION_MAJOR >= 6
QRegularExpression regexp((QString)UString(hexPattern));
regexp.setPatternOptions((QRegularExpression::PatternOptions)0x1);
QRegularExpressionMatch regexpmatch;
INT32 offset = 0;
offset = (INT32)hexBody.indexOf(regexp, (qsizetype)offset, &regexpmatch);
#else
QRegExp regexp(UString(hexPattern), Qt::CaseInsensitive);
INT32 offset = regexp.indexIn(hexBody);
#endif
while (offset >= 0) {
if (offset % 2 == 0) {
if (offset % 2 == 0) {
// For patterns that cross header|body boundary, skip patterns entirely located in body, since
// children search above has already found them.
if (!(hasChildren && mode == SEARCH_MODE_ALL && offset/2 >= model->header(index).size())) {
UModelIndex parentFileIndex = model->findParentOfType(index, Types::File);
UString name = model->name(index);
if (model->parent(index) == parentFileIndex) {
@ -175,59 +78,88 @@ USTATUS FfsFinder::findHexPattern(const UModelIndex & index, const UByteArray &
name = model->name(parentFileIndex) + UString("/.../") + name;
}
msg(UString("GUID pattern \"") + UString(guidPattern)
msg(UString("Hex pattern \"") + UString(hexPattern)
+ UString("\" found as \"") + hexBody.mid(offset, hexPattern.length()).toUpper()
+ UString("\" in ") + name
+ usprintf(" at %s-offset %02Xh", mode == SEARCH_MODE_BODY ? "body" : "header", offset / 2),
index);
}
}
#if QT_VERSION_MAJOR >= 6
offset = (INT32)hexBody.indexOf(regexp, (qsizetype)offset + 1, &regexpmatch);
offset = (INT32)hexBody.indexOf(regexp, (qsizetype)offset + 1, &regexpmatch);
#else
offset = regexp.indexIn(hexBody, offset + 1);
offset = regexp.indexIn(hexBody, offset + 1);
#endif
}
return U_SUCCESS;
}
USTATUS FfsFinder::findTextPattern(const UModelIndex & index, const UString & pattern, const UINT8 mode, const bool unicode, const Qt::CaseSensitivity caseSensitive)
{
if (pattern.isEmpty())
return U_INVALID_PARAMETER;
return U_SUCCESS;
}
if (!index.isValid())
return U_SUCCESS;
USTATUS FfsFinder::findGuidPattern(const UModelIndex & index, const UByteArray & guidPattern, const UINT8 mode)
{
if (guidPattern.isEmpty())
return U_INVALID_PARAMETER;
bool hasChildren = (model->rowCount(index) > 0);
for (int i = 0; i < model->rowCount(index); i++) {
findTextPattern(index.model()->index(i, index.column(), index), pattern, mode, unicode, caseSensitive);
}
if (!index.isValid())
return U_SUCCESS;
UByteArray body;
if (hasChildren) {
if (mode != SEARCH_MODE_BODY)
body = model->header(index);
}
else {
if (mode == SEARCH_MODE_HEADER)
body.append(model->header(index));
else if (mode == SEARCH_MODE_BODY)
body.append(model->body(index));
else
body.append(model->header(index)).append(model->body(index));
}
bool hasChildren = (model->rowCount(index) > 0);
for (int i = 0; i < model->rowCount(index); i++) {
findGuidPattern(index.model()->index(i, index.column(), index), guidPattern, mode);
}
UString data;
if (unicode)
data = uFromUcs2(body.constData(), body.length() / 2);
UByteArray data;
if (hasChildren) {
if (mode != SEARCH_MODE_BODY)
data = model->header(index);
}
else {
if (mode == SEARCH_MODE_HEADER)
data.append(model->header(index));
else if (mode == SEARCH_MODE_BODY)
data.append(model->body(index));
else
data = UString::fromLatin1((const char*)body.constData(), body.length());
data.append(model->header(index)).append(model->body(index));
}
int offset = -1;
while ((offset = (int)data.indexOf(pattern, (int)(offset + 1), caseSensitive)) >= 0) {
UString hexBody = UString(data.toHex());
QList<UByteArray> list = guidPattern.split('-');
if (list.count() != 5)
return U_INVALID_PARAMETER;
UByteArray hexPattern;
// Reverse first GUID block
hexPattern.append(list.at(0).mid(6, 2));
hexPattern.append(list.at(0).mid(4, 2));
hexPattern.append(list.at(0).mid(2, 2));
hexPattern.append(list.at(0).mid(0, 2));
// Reverse second GUID block
hexPattern.append(list.at(1).mid(2, 2));
hexPattern.append(list.at(1).mid(0, 2));
// Reverse third GUID block
hexPattern.append(list.at(2).mid(2, 2));
hexPattern.append(list.at(2).mid(0, 2));
// Append fourth and fifth GUID blocks as is
hexPattern.append(list.at(3)).append(list.at(4));
// Check for "all substrings" pattern
if (hexPattern.count('.') == hexPattern.length())
return U_SUCCESS;
#if QT_VERSION_MAJOR >= 6
QRegularExpression regexp((QString)UString(hexPattern));
regexp.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
QRegularExpressionMatch regexpmatch;
INT32 offset = (INT32)hexBody.indexOf(regexp, 0, &regexpmatch);
#else
QRegExp regexp(UString(hexPattern), Qt::CaseInsensitive);
INT32 offset = regexp.indexIn(hexBody);
#endif
while (offset >= 0) {
if (offset % 2 == 0) {
UModelIndex parentFileIndex = model->findParentOfType(index, Types::File);
UString name = model->name(index);
if (model->parent(index) == parentFileIndex) {
@ -237,11 +169,74 @@ USTATUS FfsFinder::findHexPattern(const UModelIndex & index, const UByteArray &
name = model->name(parentFileIndex) + UString("/.../") + name;
}
msg((unicode ? UString("Unicode") : UString("ASCII")) + UString(" text \"") + UString(pattern)
+ UString("\" found in ") + name
+ usprintf(" at %s-offset %02Xh", mode == SEARCH_MODE_BODY ? "body" : "header", (unicode ? offset * 2 : offset)),
msg(UString("GUID pattern \"") + UString(guidPattern)
+ UString("\" found as \"") + hexBody.mid(offset, hexPattern.length()).toUpper()
+ UString("\" in ") + name
+ usprintf(" at %s-offset %02Xh", mode == SEARCH_MODE_BODY ? "body" : "header", offset / 2),
index);
}
return U_SUCCESS;
#if QT_VERSION_MAJOR >= 6
offset = (INT32)hexBody.indexOf(regexp, (qsizetype)offset + 1, &regexpmatch);
#else
offset = regexp.indexIn(hexBody, offset + 1);
#endif
}
return U_SUCCESS;
}
USTATUS FfsFinder::findTextPattern(const UModelIndex & index, const UString & pattern, const UINT8 mode, const bool unicode, const Qt::CaseSensitivity caseSensitive)
{
if (pattern.isEmpty())
return U_INVALID_PARAMETER;
if (!index.isValid())
return U_SUCCESS;
bool hasChildren = (model->rowCount(index) > 0);
for (int i = 0; i < model->rowCount(index); i++) {
findTextPattern(index.model()->index(i, index.column(), index), pattern, mode, unicode, caseSensitive);
}
UByteArray body;
if (hasChildren) {
if (mode != SEARCH_MODE_BODY)
body = model->header(index);
}
else {
if (mode == SEARCH_MODE_HEADER)
body.append(model->header(index));
else if (mode == SEARCH_MODE_BODY)
body.append(model->body(index));
else
body.append(model->header(index)).append(model->body(index));
}
UString data = UString::fromLatin1((const char*)body.constData(), body.length());
UString searchPattern;
if (unicode)
searchPattern = UString::fromLatin1((const char*)pattern.utf16(), pattern.length() * 2);
else
searchPattern = pattern;
int offset = -1;
while ((offset = (int)data.indexOf(searchPattern, (int)(offset + 1), caseSensitive)) >= 0) {
UModelIndex parentFileIndex = model->findParentOfType(index, Types::File);
UString name = model->name(index);
if (model->parent(index) == parentFileIndex) {
name = model->name(parentFileIndex) + UString("/") + name;
}
else if (parentFileIndex.isValid()) {
name = model->name(parentFileIndex) + UString("/.../") + name;
}
msg((unicode ? UString("Unicode") : UString("ASCII")) + UString(" text \"") + UString(pattern)
+ UString("\" found in ") + name
+ usprintf(" at %s-offset %02Xh", mode == SEARCH_MODE_BODY ? "body" : "header", offset),
index);
}
return U_SUCCESS;
}

View File

@ -37,7 +37,6 @@ HEADERS += uefitool.h \
../common/ffsparser.h \
../common/ffsreport.h \
../common/treeitem.h \
../common/ffsutils.h \
../common/intel_fit.h \
../common/intel_microcode.h \
../common/treemodel.h \
@ -86,7 +85,6 @@ SOURCES += uefitool_main.cpp \
../common/ffsbuilder.cpp \
../common/ffsparser.cpp \
../common/ffsreport.cpp \
../common/ffsutils.cpp \
../common/treeitem.cpp \
../common/treemodel.cpp \
../common/LZMA/LzmaCompress.c \

View File

@ -1,88 +0,0 @@
/* ffsutils.cpp
Copyright (c) 2019, LongSoft. All rights reserved.
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
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 "ffsutils.h"
#include "utility.h"
#include "ffs.h"
namespace FfsUtils {
USTATUS findFileRecursive(TreeModel *model, const UModelIndex index, const UString & hexPattern, const UINT8 mode, std::set<std::pair<UModelIndex, UModelIndex> > & files)
{
if (!index.isValid())
return U_SUCCESS;
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
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(model, index.model()->index(i, index.column(), index), hexPattern, mode, files);
}
UByteArray data;
if (hasChildren) {
if (mode == SEARCH_MODE_HEADER)
data = model->header(index);
else if (mode == SEARCH_MODE_ALL)
data = model->header(index) + model->body(index);
}
else {
if (mode == SEARCH_MODE_HEADER)
data = model->header(index);
else if (mode == SEARCH_MODE_BODY)
data = model->body(index);
else
data = model->header(index) + model->body(index);
}
const UINT8 *rawData = reinterpret_cast<const UINT8 *>(data.constData());
INTN offset = findPattern(pattern.data(), patternMask.data(), pattern.size(), rawData, data.size(), 0);
// For patterns that cross header|body boundary, skip patterns entirely located in body, since
// children search above has already found them.
if (hasChildren && mode == SEARCH_MODE_ALL && offset >= model->header(index).size()) {
offset = -1;
}
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>(ffs, UModelIndex()));
}
else {
files.insert(std::pair<UModelIndex, UModelIndex>(index, UModelIndex()));
}
}
return U_SUCCESS;
}
};

View File

@ -1,30 +0,0 @@
/* fssreport.h
Copyright (c) 2019, LongSoft. All rights reserved.
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
*/
#ifndef FFSUTILS_H
#define FFSUTILS_H
#include <set>
#include "basetypes.h"
#include "ubytearray.h"
#include "ustring.h"
#include "treemodel.h"
namespace FfsUtils {
USTATUS findFileRecursive(TreeModel *model, const UModelIndex index, const UString & hexPattern, const UINT8 mode, std::set<std::pair<UModelIndex, UModelIndex> > & files);
};
#endif // FFSUTILS_H

View File

@ -26,7 +26,6 @@ uefitoolcommon = static_library('uefitoolcommon',
'fitparser.cpp',
'ffsparser.cpp',
'ffsreport.cpp',
'ffsutils.cpp',
'peimage.cpp',
'treeitem.cpp',
'treemodel.cpp',