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,18 +55,16 @@ 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);
while (offset >= 0) {
#endif
while (offset >= 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.
@ -91,17 +87,17 @@ USTATUS FfsFinder::findHexPattern(const UModelIndex & index, const UByteArray &
}
#if QT_VERSION_MAJOR >= 6
offset += 1;
offset = (INT32)hexBody.indexOf(regexp, (qsizetype)offset + 1, &regexpmatch);
#else
offset = regexp.indexIn(hexBody, offset + 1);
#endif
}
return U_SUCCESS;
}
}
USTATUS FfsFinder::findGuidPattern(const UModelIndex & index, const UByteArray & guidPattern, const UINT8 mode)
{
USTATUS FfsFinder::findGuidPattern(const UModelIndex & index, const UByteArray & guidPattern, const UINT8 mode)
{
if (guidPattern.isEmpty())
return U_INVALID_PARAMETER;
@ -153,17 +149,15 @@ USTATUS FfsFinder::findHexPattern(const UModelIndex & index, const UByteArray &
#if QT_VERSION_MAJOR >= 6
QRegularExpression regexp((QString)UString(hexPattern));
regexp.setPatternOptions((QRegularExpression::PatternOptions)0x1);
regexp.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
QRegularExpressionMatch regexpmatch;
INT32 offset = 0;
offset = (INT32)hexBody.indexOf(regexp, (qsizetype)offset, &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);
@ -190,10 +184,10 @@ USTATUS FfsFinder::findHexPattern(const UModelIndex & index, const UByteArray &
}
return U_SUCCESS;
}
}
USTATUS FfsFinder::findTextPattern(const UModelIndex & index, const UString & pattern, const UINT8 mode, const bool unicode, const Qt::CaseSensitivity caseSensitive)
{
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;
@ -219,15 +213,16 @@ USTATUS FfsFinder::findHexPattern(const UModelIndex & index, const UByteArray &
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)
data = uFromUcs2(body.constData(), body.length() / 2);
searchPattern = UString::fromLatin1((const char*)pattern.utf16(), pattern.length() * 2);
else
data = UString::fromLatin1((const char*)body.constData(), body.length());
searchPattern = pattern;
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);
UString name = model->name(index);
if (model->parent(index) == parentFileIndex) {
@ -239,9 +234,9 @@ USTATUS FfsFinder::findHexPattern(const UModelIndex & index, const UByteArray &
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)),
+ 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',