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/nvramparser.cpp
../common/ffsparser.cpp ../common/ffsparser.cpp
../common/fitparser.cpp ../common/fitparser.cpp
../common/ffsreport.cpp
../common/ffsutils.cpp
../common/peimage.cpp ../common/peimage.cpp
../common/treeitem.cpp ../common/treeitem.cpp
../common/treemodel.cpp ../common/treemodel.cpp
@ -59,8 +57,6 @@ SET(PROJECT_SOURCES
../common/zlib/zutil.c ../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}) ADD_EXECUTABLE(UEFIFind ${PROJECT_SOURCES})
IF(UNIX) IF(UNIX)

View File

@ -12,9 +12,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
*/ */
#include "uefifind.h" #include "uefifind.h"
#include "../common/ffsutils.h"
#include <fstream> #include <fstream>
#include <set>
UEFIFind::UEFIFind() UEFIFind::UEFIFind()
{ {
@ -46,6 +46,74 @@ USTATUS UEFIFind::init(const UString & path)
return U_SUCCESS; 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) USTATUS UEFIFind::find(const UINT8 mode, const bool count, const UString & hexPattern, UString & result)
{ {
UModelIndex root = model->index(0, 0); UModelIndex root = model->index(0, 0);
@ -53,7 +121,7 @@ USTATUS UEFIFind::find(const UINT8 mode, const bool count, const UString & hexPa
result.clear(); result.clear();
USTATUS returned = FfsUtils::findFileRecursive(model, root, hexPattern, mode, files); USTATUS returned = findFileRecursive(root, hexPattern, mode, files);
if (returned) if (returned)
return returned; return returned;

View File

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

View File

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

View File

@ -21,8 +21,6 @@
USTATUS FfsFinder::findHexPattern(const UModelIndex & index, const UByteArray & hexPattern, const UINT8 mode) USTATUS FfsFinder::findHexPattern(const UModelIndex & index, const UByteArray & hexPattern, const UINT8 mode)
{ {
// TODO: use FfsUtils.
if (!index.isValid()) if (!index.isValid())
return U_SUCCESS; return U_SUCCESS;
@ -57,18 +55,16 @@ USTATUS FfsFinder::findHexPattern(const UModelIndex & index, const UByteArray &
UString hexBody = UString(data.toHex()); UString hexBody = UString(data.toHex());
#if QT_VERSION_MAJOR >= 6 #if QT_VERSION_MAJOR >= 6
QRegularExpression regexp = QRegularExpression(UString(hexPattern)); QRegularExpression regexp = QRegularExpression(UString(hexPattern));
regexp.setPatternOptions((QRegularExpression::PatternOptions)0x1); regexp.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
QRegularExpressionMatch regexpmatch; QRegularExpressionMatch regexpmatch;
INT32 offset = 0; INT32 offset = (INT32)hexBody.indexOf(regexp, 0, &regexpmatch);
while ((offset = (INT32)hexBody.indexOf(regexp, (qsizetype)offset, &regexpmatch)) != -1) {
#else #else
QRegExp regexp = QRegExp(UString(hexPattern), Qt::CaseInsensitive); QRegExp regexp = QRegExp(UString(hexPattern), Qt::CaseInsensitive);
INT32 offset = regexp.indexIn(hexBody); INT32 offset = regexp.indexIn(hexBody);
while (offset >= 0) {
#endif #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 // For patterns that cross header|body boundary, skip patterns entirely located in body, since
// children search above has already found them. // children search above has already found them.
@ -91,7 +87,7 @@ USTATUS FfsFinder::findHexPattern(const UModelIndex & index, const UByteArray &
} }
#if QT_VERSION_MAJOR >= 6 #if QT_VERSION_MAJOR >= 6
offset += 1; offset = (INT32)hexBody.indexOf(regexp, (qsizetype)offset + 1, &regexpmatch);
#else #else
offset = regexp.indexIn(hexBody, offset + 1); offset = regexp.indexIn(hexBody, offset + 1);
#endif #endif
@ -153,17 +149,15 @@ USTATUS FfsFinder::findHexPattern(const UModelIndex & index, const UByteArray &
#if QT_VERSION_MAJOR >= 6 #if QT_VERSION_MAJOR >= 6
QRegularExpression regexp((QString)UString(hexPattern)); QRegularExpression regexp((QString)UString(hexPattern));
regexp.setPatternOptions((QRegularExpression::PatternOptions)0x1); regexp.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
QRegularExpressionMatch regexpmatch; QRegularExpressionMatch regexpmatch;
INT32 offset = 0; INT32 offset = (INT32)hexBody.indexOf(regexp, 0, &regexpmatch);
offset = (INT32)hexBody.indexOf(regexp, (qsizetype)offset, &regexpmatch);
#else #else
QRegExp regexp(UString(hexPattern), Qt::CaseInsensitive); QRegExp regexp(UString(hexPattern), Qt::CaseInsensitive);
INT32 offset = regexp.indexIn(hexBody); INT32 offset = regexp.indexIn(hexBody);
#endif #endif
while (offset >= 0) { while (offset >= 0) {
if (offset % 2 == 0) { if (offset % 2 == 0) {
UModelIndex parentFileIndex = model->findParentOfType(index, Types::File); UModelIndex parentFileIndex = model->findParentOfType(index, Types::File);
@ -219,15 +213,16 @@ USTATUS FfsFinder::findHexPattern(const UModelIndex & index, const UByteArray &
body.append(model->header(index)).append(model->body(index)); body.append(model->header(index)).append(model->body(index));
} }
UString data; UString data = UString::fromLatin1((const char*)body.constData(), body.length());
UString searchPattern;
if (unicode) if (unicode)
data = uFromUcs2(body.constData(), body.length() / 2); searchPattern = UString::fromLatin1((const char*)pattern.utf16(), pattern.length() * 2);
else else
data = UString::fromLatin1((const char*)body.constData(), body.length()); searchPattern = pattern;
int offset = -1; int offset = -1;
while ((offset = (int)data.indexOf(pattern, (int)(offset + 1), caseSensitive)) >= 0) { while ((offset = (int)data.indexOf(searchPattern, (int)(offset + 1), caseSensitive)) >= 0) {
UModelIndex parentFileIndex = model->findParentOfType(index, Types::File); UModelIndex parentFileIndex = model->findParentOfType(index, Types::File);
UString name = model->name(index); UString name = model->name(index);
if (model->parent(index) == parentFileIndex) { if (model->parent(index) == parentFileIndex) {
@ -239,7 +234,7 @@ USTATUS FfsFinder::findHexPattern(const UModelIndex & index, const UByteArray &
msg((unicode ? UString("Unicode") : UString("ASCII")) + UString(" text \"") + UString(pattern) msg((unicode ? UString("Unicode") : UString("ASCII")) + UString(" text \"") + UString(pattern)
+ UString("\" found in ") + name + UString("\" found in ") + name
+ usprintf(" at %s-offset %02Xh", mode == SEARCH_MODE_BODY ? "body" : "header", (unicode ? offset * 2 : offset)), + usprintf(" at %s-offset %02Xh", mode == SEARCH_MODE_BODY ? "body" : "header", offset),
index); index);
} }

View File

@ -37,7 +37,6 @@ HEADERS += uefitool.h \
../common/ffsparser.h \ ../common/ffsparser.h \
../common/ffsreport.h \ ../common/ffsreport.h \
../common/treeitem.h \ ../common/treeitem.h \
../common/ffsutils.h \
../common/intel_fit.h \ ../common/intel_fit.h \
../common/intel_microcode.h \ ../common/intel_microcode.h \
../common/treemodel.h \ ../common/treemodel.h \
@ -86,7 +85,6 @@ SOURCES += uefitool_main.cpp \
../common/ffsbuilder.cpp \ ../common/ffsbuilder.cpp \
../common/ffsparser.cpp \ ../common/ffsparser.cpp \
../common/ffsreport.cpp \ ../common/ffsreport.cpp \
../common/ffsutils.cpp \
../common/treeitem.cpp \ ../common/treeitem.cpp \
../common/treemodel.cpp \ ../common/treemodel.cpp \
../common/LZMA/LzmaCompress.c \ ../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', 'fitparser.cpp',
'ffsparser.cpp', 'ffsparser.cpp',
'ffsreport.cpp', 'ffsreport.cpp',
'ffsutils.cpp',
'peimage.cpp', 'peimage.cpp',
'treeitem.cpp', 'treeitem.cpp',
'treemodel.cpp', 'treemodel.cpp',