2015-09-13 20:15:26 +08:00
|
|
|
/* uefifind_main.cpp
|
|
|
|
|
2018-04-30 13:33:19 +08:00
|
|
|
Copyright (c) 2018, LongSoft. All rights reserved.
|
2015-09-13 20:15:26 +08:00
|
|
|
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 <QCoreApplication>
|
|
|
|
#include <iostream>
|
2018-05-22 05:59:23 +08:00
|
|
|
|
|
|
|
#include "../version.h"
|
2015-09-13 20:15:26 +08:00
|
|
|
#include "uefifind.h"
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
QCoreApplication a(argc, argv);
|
2017-12-11 09:56:00 +08:00
|
|
|
a.setOrganizationName("LongSoft");
|
|
|
|
a.setOrganizationDomain("longsoft.me");
|
2015-09-13 20:15:26 +08:00
|
|
|
a.setApplicationName("UEFIFind");
|
|
|
|
|
|
|
|
UEFIFind w;
|
|
|
|
UINT8 result;
|
|
|
|
|
|
|
|
if (a.arguments().length() == 5) {
|
2018-06-03 00:43:29 +08:00
|
|
|
const QString &inputArg = a.arguments().at(1);
|
|
|
|
const QString &modeArg = a.arguments().at(2);
|
|
|
|
const QString &subModeArg = a.arguments().at(3);
|
|
|
|
const QString &patternArg = a.arguments().at(4);
|
|
|
|
|
2015-09-13 20:15:26 +08:00
|
|
|
// Get search mode
|
|
|
|
UINT8 mode;
|
2018-06-03 00:43:29 +08:00
|
|
|
if (modeArg == QString("header"))
|
2015-09-13 20:15:26 +08:00
|
|
|
mode = SEARCH_MODE_HEADER;
|
2018-06-03 00:43:29 +08:00
|
|
|
else if (modeArg == QString("body"))
|
2015-09-13 20:15:26 +08:00
|
|
|
mode = SEARCH_MODE_BODY;
|
2018-06-03 00:43:29 +08:00
|
|
|
else if (modeArg == QString("all"))
|
2015-09-13 20:15:26 +08:00
|
|
|
mode = SEARCH_MODE_ALL;
|
|
|
|
else
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_INVALID_PARAMETER;
|
2015-09-13 20:15:26 +08:00
|
|
|
|
|
|
|
// Get result type
|
|
|
|
bool count;
|
2018-06-03 00:43:29 +08:00
|
|
|
if (subModeArg == QString("list"))
|
2015-09-13 20:15:26 +08:00
|
|
|
count = false;
|
2018-06-03 00:43:29 +08:00
|
|
|
else if (subModeArg == QString("count"))
|
2015-09-13 20:15:26 +08:00
|
|
|
count = true;
|
|
|
|
else
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_INVALID_PARAMETER;
|
2015-09-13 20:15:26 +08:00
|
|
|
|
|
|
|
// Parse input file
|
2018-06-03 00:43:29 +08:00
|
|
|
result = w.init(inputArg);
|
2015-09-13 20:15:26 +08:00
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
// Go find the supplied pattern
|
|
|
|
QString found;
|
2018-06-03 00:43:29 +08:00
|
|
|
result = w.find(mode, count, patternArg, found);
|
2015-09-13 20:15:26 +08:00
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
// Nothing is found
|
|
|
|
if (found.isEmpty())
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_ITEM_NOT_FOUND;
|
2015-09-13 20:15:26 +08:00
|
|
|
|
|
|
|
// Print result
|
|
|
|
std::cout << found.toStdString();
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_SUCCESS;
|
2015-09-13 20:15:26 +08:00
|
|
|
}
|
|
|
|
else if (a.arguments().length() == 4) {
|
2018-06-03 00:43:29 +08:00
|
|
|
const QString &inputArg = a.arguments().at(1);
|
|
|
|
const QString &modeArg = a.arguments().at(2);
|
|
|
|
const QString &patternArg = a.arguments().at(3);
|
|
|
|
|
2015-09-13 20:15:26 +08:00
|
|
|
// Get search mode
|
2018-06-03 00:43:29 +08:00
|
|
|
if (modeArg != QString("file"))
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_INVALID_PARAMETER;
|
2015-09-13 20:15:26 +08:00
|
|
|
|
|
|
|
// Open patterns file
|
2018-06-03 00:43:29 +08:00
|
|
|
QFileInfo fileInfo(patternArg);
|
2015-09-13 20:15:26 +08:00
|
|
|
if (!fileInfo.exists())
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_FILE_OPEN;
|
2015-09-13 20:15:26 +08:00
|
|
|
|
|
|
|
QFile patternsFile;
|
2018-06-03 00:43:29 +08:00
|
|
|
patternsFile.setFileName(patternArg);
|
2015-09-13 20:15:26 +08:00
|
|
|
if (!patternsFile.open(QFile::ReadOnly))
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_FILE_OPEN;
|
2015-09-13 20:15:26 +08:00
|
|
|
|
|
|
|
// Parse input file
|
2018-06-03 00:43:29 +08:00
|
|
|
result = w.init(inputArg);
|
2015-09-13 20:15:26 +08:00
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
// Perform searches
|
|
|
|
bool somethingFound = false;
|
|
|
|
while (!patternsFile.atEnd()) {
|
|
|
|
QByteArray line = patternsFile.readLine();
|
|
|
|
// Use sharp symbol as commentary
|
|
|
|
if (line.count() == 0 || line[0] == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Split the read line
|
|
|
|
QList<QByteArray> list = line.split(' ');
|
|
|
|
if (list.count() < 3) {
|
2015-12-13 03:28:44 +08:00
|
|
|
std::cout << line.constData() << "skipped, too few arguments" << std::endl << std::endl;
|
2015-09-13 20:15:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Get search mode
|
|
|
|
UINT8 mode;
|
|
|
|
if (list.at(0) == QString("header"))
|
|
|
|
mode = SEARCH_MODE_HEADER;
|
|
|
|
else if (list.at(0) == QString("body"))
|
|
|
|
mode = SEARCH_MODE_BODY;
|
|
|
|
else if (list.at(0) == QString("all"))
|
|
|
|
mode = SEARCH_MODE_ALL;
|
|
|
|
else {
|
2015-12-13 03:28:44 +08:00
|
|
|
std::cout << line.constData() << "skipped, invalid search mode" << std::endl << std::endl;
|
2015-09-13 20:15:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get result type
|
|
|
|
bool count;
|
|
|
|
if (list.at(1) == QString("list"))
|
|
|
|
count = false;
|
|
|
|
else if (list.at(1) == QString("count"))
|
|
|
|
count = true;
|
|
|
|
else {
|
2015-12-13 03:28:44 +08:00
|
|
|
std::cout << line.constData() << "skipped, invalid result type" << std::endl << std::endl;
|
2015-09-13 20:15:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go find the supplied pattern
|
|
|
|
QString found;
|
|
|
|
result = w.find(mode, count, list.at(2), found);
|
|
|
|
if (result) {
|
2015-12-13 03:28:44 +08:00
|
|
|
std::cout << line.constData() << "skipped, find failed with error " << (UINT32)result << std::endl << std::endl;
|
2015-09-13 20:15:26 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found.isEmpty()) {
|
|
|
|
// Nothing is found
|
2015-12-13 03:28:44 +08:00
|
|
|
std::cout << line.constData() << "nothing found" << std::endl << std::endl;
|
2015-09-13 20:15:26 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Print result
|
2015-12-13 03:28:44 +08:00
|
|
|
std::cout << line.constData() << found.toStdString() << std::endl;
|
2015-09-13 20:15:26 +08:00
|
|
|
somethingFound = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing is found
|
|
|
|
if (!somethingFound)
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_ITEM_NOT_FOUND;
|
2015-09-13 20:15:26 +08:00
|
|
|
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_SUCCESS;
|
2015-09-13 20:15:26 +08:00
|
|
|
}
|
|
|
|
else {
|
2018-05-22 05:59:23 +08:00
|
|
|
std::cout << "UEFIFind " PROGRAM_VERSION << std::endl << std::endl <<
|
2018-06-03 00:43:29 +08:00
|
|
|
"Usage: UEFIFind imagefile {header | body | all} {list | count} pattern" << std::endl <<
|
|
|
|
" or UEFIFind imagefile file patternsfile" << std::endl;
|
2016-07-09 16:01:41 +08:00
|
|
|
return U_INVALID_PARAMETER;
|
2015-09-13 20:15:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|