├── README.md ├── die_script.pri ├── LICENSE ├── die_script.cmake ├── xscriptengine.cpp ├── util_script.h ├── xscriptengine.h ├── util_script.cpp ├── global_script.h ├── global_script.cpp ├── die_script.h ├── die_scriptengine.h ├── die_scriptengine.cpp └── die_script.cpp /README.md: -------------------------------------------------------------------------------- 1 | # die_script 2 | 3 | Scripts 4 | -------------------------------------------------------------------------------- /die_script.pri: -------------------------------------------------------------------------------- 1 | QT += concurrent 2 | 3 | lessThan(QT_MAJOR_VERSION, 6): QT += script 4 | greaterThan(QT_MAJOR_VERSION, 5): QT += qml 5 | 6 | INCLUDEPATH += $$PWD 7 | DEPENDPATH += $$PWD 8 | 9 | HEADERS += \ 10 | $$PWD/die_script.h \ 11 | $$PWD/die_scriptengine.h \ 12 | $$PWD/global_script.h \ 13 | $$PWD/util_script.h \ 14 | $$PWD/xscriptengine.h 15 | 16 | SOURCES += \ 17 | $$PWD/die_script.cpp \ 18 | $$PWD/die_scriptengine.cpp \ 19 | $$PWD/global_script.cpp \ 20 | $$PWD/util_script.cpp \ 21 | $$PWD/xscriptengine.cpp 22 | 23 | !contains(XCONFIG, xscanengine) { 24 | XCONFIG += xscanengine 25 | include($$PWD/../XScanEngine/xscanengine.pri) 26 | } 27 | 28 | !contains(XCONFIG, xdisasmcore) { 29 | XCONFIG += xdisasmcore 30 | include($$PWD/../XDisasmCore/xdisasmcore.pri) 31 | } 32 | 33 | DISTFILES += \ 34 | $$PWD/LICENSE \ 35 | $$PWD/README.md \ 36 | $$PWD/die_script.cmake 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019-2025 hors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /die_script.cmake: -------------------------------------------------------------------------------- 1 | include_directories(${CMAKE_CURRENT_LIST_DIR}) 2 | 3 | if (NOT DEFINED XDISASMCORE_SOURCES) 4 | include(${CMAKE_CURRENT_LIST_DIR}/../XDisasmCore/xdisasmcore.cmake) 5 | set(DIE_SCRIPT_SOURCES ${DIE_SCRIPT_SOURCES} ${XDISASMCORE_SOURCES}) 6 | endif() 7 | if (NOT DEFINED XSCANENGINE_SOURCES) 8 | include(${CMAKE_CURRENT_LIST_DIR}/../XScanEngine/xscanengine.cmake) 9 | set(DIE_SCRIPT_SOURCES ${DIE_SCRIPT_SOURCES} ${XSCANENGINE_SOURCES}) 10 | endif() 11 | if (NOT DEFINED DETECTITEASY_SOURCES) 12 | include(${CMAKE_CURRENT_LIST_DIR}/../Detect-It-Easy/detectiteasy.cmake) 13 | endif() 14 | 15 | set(DIE_SCRIPT_SOURCES 16 | ${DIE_SCRIPT_SOURCES} 17 | ${CMAKE_CURRENT_LIST_DIR}/die_script.cpp 18 | ${CMAKE_CURRENT_LIST_DIR}/die_script.h 19 | ${CMAKE_CURRENT_LIST_DIR}/die_scriptengine.cpp 20 | ${CMAKE_CURRENT_LIST_DIR}/die_scriptengine.h 21 | ${CMAKE_CURRENT_LIST_DIR}/global_script.cpp 22 | ${CMAKE_CURRENT_LIST_DIR}/global_script.h 23 | ${CMAKE_CURRENT_LIST_DIR}/xscriptengine.cpp 24 | ${CMAKE_CURRENT_LIST_DIR}/xscriptengine.h 25 | ${CMAKE_CURRENT_LIST_DIR}/util_script.cpp 26 | ${CMAKE_CURRENT_LIST_DIR}/util_script.h 27 | ) 28 | -------------------------------------------------------------------------------- /xscriptengine.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2019-2025 hors 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | * SOFTWARE. 20 | */ 21 | #include "xscriptengine.h" 22 | 23 | XScriptEngine::XScriptEngine() 24 | { 25 | } 26 | 27 | #ifdef QT_SCRIPT_LIB 28 | void XScriptEngine::_addFunction(QScriptEngine::FunctionSignature function, const QString &sFunctionName) 29 | { 30 | QScriptValue func = this->newFunction(function); 31 | this->globalObject().setProperty(sFunctionName, func); 32 | } 33 | #endif 34 | 35 | void XScriptEngine::_addClass(QObject *pClass, QString sClassName) 36 | { 37 | XSCRIPTVALUE objectWnd = this->newQObject(pClass); 38 | this->globalObject().setProperty(sClassName, objectWnd); 39 | } 40 | -------------------------------------------------------------------------------- /util_script.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2019-2025 hors 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | * SOFTWARE. 20 | */ 21 | #ifndef UTIL_SCRIPT_H 22 | #define UTIL_SCRIPT_H 23 | 24 | #include 25 | #include 26 | 27 | class Util_script : public QObject { 28 | Q_OBJECT 29 | 30 | public: 31 | explicit Util_script(QObject *pParent = nullptr); 32 | 33 | public slots: 34 | quint64 shlu64(quint64 nValue, quint64 nShift); 35 | quint64 shru64(quint64 nValue, quint64 nShift); 36 | qint64 shl64(qint64 nValue, qint64 nShift); 37 | qint64 shr64(qint64 nValue, qint64 nShift); 38 | quint64 divu64(quint64 nDividend, quint64 nDivisor); 39 | qint64 div64(qint64 nDividend, qint64 nDivisor); 40 | QString secondsToTimeStr(qint32 nValue); 41 | }; 42 | 43 | #endif // UTIL_SCRIPT_H 44 | -------------------------------------------------------------------------------- /xscriptengine.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2019-2025 hors 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | * SOFTWARE. 20 | */ 21 | #ifndef XSCRIPTENGINE_H 22 | #define XSCRIPTENGINE_H 23 | 24 | #include "global_script.h" 25 | #ifdef QT_SCRIPT_LIB 26 | #include 27 | #else 28 | #include 29 | #endif 30 | 31 | #ifdef QT_SCRIPT_LIB 32 | #define XSCRIPTENGINE QScriptEngine 33 | #define XSCRIPTVALUE QScriptValue 34 | #define XSCRIPTVALUELIST QScriptValueList 35 | #else 36 | #define XSCRIPTENGINE QJSEngine 37 | #define XSCRIPTVALUE QJSValue 38 | #define XSCRIPTVALUELIST QJSValueList 39 | #endif 40 | 41 | class XScriptEngine : public XSCRIPTENGINE { 42 | Q_OBJECT 43 | public: 44 | explicit XScriptEngine(); 45 | 46 | protected: 47 | #ifdef QT_SCRIPT_LIB 48 | void _addFunction(FunctionSignature function, const QString &sFunctionName); 49 | #endif 50 | void _addClass(QObject *pClass, QString sClassName); 51 | 52 | signals: 53 | void errorMessage(const QString &sErrorMessage); 54 | void warningMessage(const QString &sWarningMessage); 55 | void infoMessage(const QString &sInfoMessage); 56 | }; 57 | 58 | #endif // XSCRIPTENGINE_H 59 | -------------------------------------------------------------------------------- /util_script.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2019-2025 hors 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | * SOFTWARE. 20 | */ 21 | #include "util_script.h" 22 | 23 | Util_script::Util_script(QObject *pParent) : QObject(pParent) 24 | { 25 | } 26 | 27 | quint64 Util_script::shlu64(quint64 nValue, quint64 nShift) 28 | { 29 | quint64 nResult = nValue << nShift; 30 | return nResult; 31 | } 32 | 33 | quint64 Util_script::shru64(quint64 nValue, quint64 nShift) 34 | { 35 | quint64 nResult = nValue >> nShift; 36 | return nResult; 37 | } 38 | 39 | qint64 Util_script::shl64(qint64 nValue, qint64 nShift) 40 | { 41 | qint64 nResult = nValue << nShift; 42 | return nResult; 43 | } 44 | 45 | qint64 Util_script::shr64(qint64 nValue, qint64 nShift) 46 | { 47 | qint64 nResult = nValue >> nShift; 48 | return nResult; 49 | } 50 | 51 | quint64 Util_script::divu64(quint64 nDividend, quint64 nDivisor) 52 | { 53 | quint64 nResult = 0; 54 | 55 | if (nDivisor) { 56 | nResult = nDividend / nDivisor; 57 | } else { 58 | nResult = -1; 59 | } 60 | 61 | return nResult; 62 | } 63 | 64 | qint64 Util_script::div64(qint64 nDividend, qint64 nDivisor) 65 | { 66 | qint64 nResult = 0; 67 | 68 | if (nDivisor) { 69 | nResult = nDividend / nDivisor; 70 | } else { 71 | nResult = -1; 72 | } 73 | 74 | return nResult; 75 | } 76 | 77 | QString Util_script::secondsToTimeStr(qint32 nValue) 78 | { 79 | QTime _dt = QTime(0, 0); 80 | _dt = _dt.addSecs(nValue); 81 | QString sResult = _dt.toString(); 82 | return sResult; 83 | } 84 | -------------------------------------------------------------------------------- /global_script.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2019-2025 hors 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | * SOFTWARE. 20 | */ 21 | #ifndef GLOBAL_SCRIPT_H 22 | #define GLOBAL_SCRIPT_H 23 | 24 | // TODO rename to die_global_script 25 | #include "xoptions.h" 26 | 27 | class global_script : public QObject { 28 | Q_OBJECT 29 | 30 | public: 31 | explicit global_script(QObject *pParent = nullptr); 32 | 33 | public slots: 34 | void includeScript(const QString &sScript); 35 | void _log(const QString &sText); 36 | // qint64 _min(qint64 nValue1, qint64 nValue2); 37 | // qint64 _max(qint64 nValue1, qint64 nValue2); 38 | void _setResult(const QString &sType, const QString &sName, const QString &sVersion, const QString &sOptions); 39 | bool _isResultPresent(const QString &sType, const QString &sName); 40 | qint32 _getNumberOfResults(const QString &sType); 41 | void _removeResult(const QString &sType, const QString &sName); 42 | bool _isStop(); 43 | void _encodingList(); 44 | bool _isConsoleMode(); 45 | bool _isLiteMode(); 46 | bool _isGuiMode(); 47 | bool _isLibraryMode(); 48 | void _breakScan(); 49 | QString _getEngineVersion(); 50 | QString _getOS(); 51 | QString _getQtVersion(); 52 | 53 | signals: 54 | void includeScriptSignal(const QString &sScript); 55 | void _logSignal(const QString &sText); 56 | void _setResultSignal(const QString &sType, const QString &sName, const QString &sVersion, const QString &sOptions); 57 | void _isResultPresentSignal(bool *pbResult, const QString &sType, const QString &sName); 58 | void _getNumberOfResultsSignal(qint32 *pnResult, const QString &sType); 59 | void _removeResultSignal(const QString &sType, const QString &sName); 60 | void _isStopSignal(bool *pResult); 61 | void _encodingListSignal(); 62 | void _isConsoleModeSignal(bool *pResult); 63 | void _isLiteModeSignal(bool *pResult); 64 | void _isGuiModeSignal(bool *pResult); 65 | void _isLibraryModeSignal(bool *pResult); 66 | void _breakScanSignal(); 67 | void _getEngineVersionSignal(QString *pResult); 68 | void _getOSSignal(QString *pResult); 69 | void _getQtVersionSignal(QString *pResult); 70 | }; 71 | 72 | #endif // GLOBAL_SCRIPT_H 73 | -------------------------------------------------------------------------------- /global_script.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2019-2025 hors 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | * SOFTWARE. 20 | */ 21 | #include "global_script.h" 22 | global_script::global_script(QObject *pParent) : QObject(pParent) 23 | { 24 | } 25 | 26 | void global_script::includeScript(const QString &sScript) 27 | { 28 | emit includeScriptSignal(sScript); 29 | } 30 | 31 | void global_script::_log(const QString &sText) 32 | { 33 | emit _logSignal(sText); 34 | } 35 | 36 | // qint64 global_script::_min(qint64 nValue1, qint64 nValue2) 37 | //{ 38 | // return qMin(nValue1, nValue2); 39 | // } 40 | 41 | // qint64 global_script::_max(qint64 nValue1, qint64 nValue2) 42 | //{ 43 | // return qMax(nValue1, nValue2); 44 | // } 45 | 46 | void global_script::_setResult(const QString &sType, const QString &sName, const QString &sVersion, const QString &sOptions) 47 | { 48 | emit _setResultSignal(sType, sName, sVersion, sOptions); 49 | } 50 | 51 | bool global_script::_isResultPresent(const QString &sType, const QString &sName) 52 | { 53 | bool bResult = false; 54 | emit _isResultPresentSignal(&bResult, sType, sName); 55 | 56 | return bResult; 57 | } 58 | 59 | qint32 global_script::_getNumberOfResults(const QString &sType) 60 | { 61 | qint32 nResult = 0; 62 | emit _getNumberOfResultsSignal(&nResult, sType); 63 | 64 | return nResult; 65 | } 66 | 67 | void global_script::_removeResult(const QString &sType, const QString &sName) 68 | { 69 | emit _removeResultSignal(sType, sName); 70 | } 71 | 72 | bool global_script::_isStop() 73 | { 74 | bool bResult = false; 75 | emit _isStopSignal(&bResult); 76 | 77 | return bResult; 78 | } 79 | 80 | void global_script::_encodingList() 81 | { 82 | emit _encodingListSignal(); 83 | } 84 | 85 | bool global_script::_isConsoleMode() 86 | { 87 | bool bResult = false; 88 | emit _isConsoleModeSignal(&bResult); 89 | 90 | return bResult; 91 | } 92 | 93 | bool global_script::_isLiteMode() 94 | { 95 | bool bResult = false; 96 | emit _isLiteModeSignal(&bResult); 97 | 98 | return bResult; 99 | } 100 | 101 | bool global_script::_isGuiMode() 102 | { 103 | bool bResult = false; 104 | emit _isGuiModeSignal(&bResult); 105 | 106 | return bResult; 107 | } 108 | 109 | bool global_script::_isLibraryMode() 110 | { 111 | bool bResult = false; 112 | emit _isLibraryModeSignal(&bResult); 113 | 114 | return bResult; 115 | } 116 | 117 | void global_script::_breakScan() 118 | { 119 | emit _breakScanSignal(); 120 | } 121 | 122 | QString global_script::_getEngineVersion() 123 | { 124 | QString sResult; 125 | emit _getEngineVersionSignal(&sResult); 126 | 127 | return sResult; 128 | } 129 | 130 | QString global_script::_getOS() 131 | { 132 | QString sResult; 133 | emit _getOSSignal(&sResult); 134 | 135 | return sResult; 136 | } 137 | 138 | QString global_script::_getQtVersion() 139 | { 140 | QString sResult; 141 | emit _getQtVersionSignal(&sResult); 142 | 143 | return sResult; 144 | } 145 | -------------------------------------------------------------------------------- /die_script.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2019-2025 hors 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | * SOFTWARE. 20 | */ 21 | #ifndef DIE_SCRIPT_H 22 | #define DIE_SCRIPT_H 23 | 24 | #include "die_scriptengine.h" 25 | #ifdef QT_SCRIPTTOOLS_LIB 26 | #include 27 | #include 28 | #endif 29 | 30 | bool sort_signature_prio(const DiE_ScriptEngine::SIGNATURE_RECORD &sr1, const DiE_ScriptEngine::SIGNATURE_RECORD &sr2); 31 | bool sort_signature_name(const DiE_ScriptEngine::SIGNATURE_RECORD &sr1, const DiE_ScriptEngine::SIGNATURE_RECORD &sr2); 32 | 33 | class DiE_Script : public XScanEngine { 34 | Q_OBJECT 35 | 36 | public: 37 | struct STATS { 38 | QMap mapTypes; 39 | }; 40 | 41 | struct SIGNATURE_STATE { 42 | XBinary::FT fileType; 43 | qint32 nNumberOfSignatures; 44 | }; 45 | 46 | explicit DiE_Script(QObject *pParent = nullptr); 47 | DiE_Script(const DiE_Script &other); // Copy constructor declaration 48 | 49 | void initDatabase(); 50 | bool loadDatabase(const QString &sDatabasePath, DiE_ScriptEngine::DT databaseType, XBinary::PDSTRUCT *pPdStruct = nullptr); 51 | 52 | QList getSignatureStates(); 53 | qint32 getNumberOfSignatures(XBinary::FT fileType); 54 | QList *getSignatures(); 55 | 56 | DiE_ScriptEngine::SIGNATURE_RECORD getSignatureByFilePath(const QString &sSignatureFilePath); 57 | bool updateSignature(const QString &sSignatureFilePath, const QString &sText); 58 | STATS getStats(); 59 | bool isSignaturesPresent(XBinary::FT fileType); 60 | 61 | static QString getErrorsString(XScanEngine::SCAN_RESULT *pScanResult); // TODO move to XBinary 62 | static QList getErrorsAndWarningsStringList(XScanEngine::SCAN_RESULT *pScanResult); // TODO move to XBinary 63 | #ifdef QT_SCRIPTTOOLS_LIB 64 | void setDebugger(QScriptEngineDebugger *pDebugger); 65 | void removeDebugger(); 66 | #endif 67 | static QList convert(QList *pListScanStructs); 68 | 69 | bool loadDatabaseFromGlobalOptions(XOptions *pXOptions); 70 | 71 | private: 72 | QList _loadDatabaseFromPath(const QString &sDatabasePath, DiE_ScriptEngine::DT databaseType, XBinary::FT fileType, 73 | XBinary::PDSTRUCT *pPdStruct); 74 | QList _loadDatabaseFromArchive(XArchive *pArchive, QList *pListRecords, DiE_ScriptEngine::DT databaseType, 75 | const QString &sPrefix, XBinary::FT fileType); // TODO pdStruct 76 | void processDetect(XScanEngine::SCANID *pScanID, XScanEngine::SCAN_RESULT *pScanResult, QIODevice *pDevice, const XScanEngine::SCANID &parentId, XBinary::FT fileType, 77 | XScanEngine::SCAN_OPTIONS *pScanOptions, const QString &sSignatureFilePath, bool bAddUnknown, XBinary::PDSTRUCT *pPdStruct); 78 | bool _handleError(DiE_ScriptEngine *pScriptEngine, XSCRIPTVALUE scriptValue, DiE_ScriptEngine::SIGNATURE_RECORD *pSignatureRecord, 79 | XScanEngine::SCAN_RESULT *pScanResult); 80 | 81 | protected: 82 | virtual void _processDetect(XScanEngine::SCANID *pScanID, SCAN_RESULT *pScanResult, QIODevice *pDevice, const SCANID &parentId, XBinary::FT fileType, 83 | SCAN_OPTIONS *pOptions, bool bAddUnknown, XBinary::PDSTRUCT *pPdStruct); 84 | 85 | private: 86 | QList m_listSignatures; 87 | #ifdef QT_SCRIPTTOOLS_LIB 88 | QScriptEngineDebugger *m_pDebugger; 89 | #endif 90 | }; 91 | 92 | #endif // DIE_SCRIPT_H 93 | -------------------------------------------------------------------------------- /die_scriptengine.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2019-2025 hors 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | * SOFTWARE. 20 | */ 21 | #ifndef DIE_SCRIPTENGINE_H 22 | #define DIE_SCRIPTENGINE_H 23 | 24 | #include 25 | #include "xbinary.h" 26 | #include "util_script.h" 27 | #include "xscriptengine.h" 28 | #include "xscanengine.h" 29 | 30 | class DiE_ScriptEngine : public XScriptEngine { 31 | Q_OBJECT 32 | 33 | public: 34 | enum DT { 35 | DT_MAIN = 0, 36 | DT_EXTRA, 37 | DT_CUSTOM 38 | }; 39 | 40 | struct SIGNATURE_RECORD { 41 | XBinary::FT fileType; 42 | QString sName; 43 | QString sFilePath; 44 | DT databaseType; 45 | QString sText; 46 | bool bReadOnly; 47 | }; 48 | 49 | // struct SCAN_HEADER 50 | // { 51 | // XBinary::FT fileType; 52 | // QString sArch; 53 | // XBinary::MODE mode; 54 | // bool bIsBigEndian; 55 | // QString sType; 56 | // qint64 nSize; 57 | // qint64 nOffset; 58 | // XBinary::FILEPART filePart; 59 | // }; 60 | 61 | struct SCAN_STRUCT { 62 | bool bIsUnknown; 63 | XScanEngine::SCANID id; 64 | XScanEngine::SCANID parentId; 65 | // SCAN_HEADER scanHeader; 66 | // XBinary::FT fileType; 67 | // QString sFullString; 68 | QString sType; 69 | // QString sResult; 70 | QString sName; 71 | QString sVersion; 72 | QString sOptions; 73 | QString sSignature; 74 | QString sSignatureFileName; 75 | }; 76 | 77 | struct BLRECORD { 78 | QString sType; 79 | QString sName; 80 | }; 81 | 82 | DiE_ScriptEngine(QList *pSignaturesList, QList *pListScanStructs, QIODevice *pDevice, XBinary::FT fileType, XBinary::FILEPART filePart, 83 | Binary_Script::OPTIONS *pOptions, XBinary::PDSTRUCT *pPdStruct); 84 | ~DiE_ScriptEngine(); 85 | 86 | void _adjustScript(XBinary *pBinary, Binary_Script *pScript, const QString &sName); 87 | 88 | bool handleError(QString sPrefix, XSCRIPTVALUE value, QString *psErrorString); 89 | // QList getListLocalResult(); 90 | // void clearListLocalResult(); 91 | // static RESULT stringToResult(const QString &sString, bool bShowType, bool bShowVersion, bool bShowOptions); 92 | XSCRIPTVALUE evaluateEx(const XScanEngine::SCANID &parentId, const XScanEngine::SCANID &resultId, const QString &sProgram, const QString &sName, 93 | const QString &sFileName); 94 | 95 | private: 96 | #ifdef QT_SCRIPT_LIB 97 | static QScriptValue includeScript(QScriptContext *pContext, QScriptEngine *pEngine); 98 | static QScriptValue _log(QScriptContext *pContext, QScriptEngine *pEngine); 99 | static QScriptValue _setResult(QScriptContext *pContext, QScriptEngine *pEngine); 100 | static QScriptValue _isResultPresent(QScriptContext *pContext, QScriptEngine *pEngine); 101 | static QScriptValue _getNumberOfResults(QScriptContext *pContext, QScriptEngine *pEngine); 102 | static QScriptValue _removeResult(QScriptContext *pContext, QScriptEngine *pEngine); 103 | static QScriptValue _isStop(QScriptContext *pContext, QScriptEngine *pEngine); 104 | static QScriptValue _encodingList(QScriptContext *pContext, QScriptEngine *pEngine); 105 | static QScriptValue _isConsoleMode(QScriptContext *pContext, QScriptEngine *pEngine); 106 | static QScriptValue _isLiteMode(QScriptContext *pContext, QScriptEngine *pEngine); 107 | static QScriptValue _isGuiMode(QScriptContext *pContext, QScriptEngine *pEngine); 108 | static QScriptValue _isLibraryMode(QScriptContext *pContext, QScriptEngine *pEngine); 109 | static QScriptValue _breakScan(QScriptContext *pContext, QScriptEngine *pEngine); 110 | static QScriptValue _getEngineVersion(QScriptContext *pContext, QScriptEngine *pEngine); 111 | static QScriptValue _getOS(QScriptContext *pContext, QScriptEngine *pEngine); 112 | #endif 113 | 114 | private slots: 115 | void includeScriptSlot(const QString &sScript); 116 | void _logSlot(const QString &sText); 117 | void _setResultSlot(const QString &sType, const QString &sName, const QString &sVersion, const QString &sOptions); 118 | void _isResultPresentSlot(bool *pbResult, const QString &sType, const QString &sName); 119 | void _getNumberOfResultsSlot(qint32 *pnResult, const QString &sType); 120 | void _removeResultSlot(const QString &sType, const QString &sName); 121 | void _isStopSlot(bool *pResult); 122 | void _encodingListSlot(); 123 | void _isConsoleModeSlot(bool *pResult); 124 | void _isLiteModeSlot(bool *pResult); 125 | void _isGuiModeSlot(bool *pResult); 126 | void _isLibraryModeSlot(bool *pResult); 127 | void _breakScanSlot(); 128 | void _getEngineVersionSlot(QString *pResult); 129 | void _getOSSlot(QString *pResult); 130 | void _getQtVersionSlot(QString *pResult); 131 | 132 | private: 133 | QList *m_pSignaturesList; 134 | QList *m_pListScanStructs; 135 | QList m_listBinaries; 136 | QList m_listScriptClasses; 137 | XBinary::PDSTRUCT *m_pPdStruct; 138 | 139 | XScanEngine::SCANID m_parentId; 140 | XScanEngine::SCANID m_resultId; // TODO rename 141 | QString m_sName; // TODO rename 142 | QString m_sFileName; // TODO rename 143 | 144 | QList m_listBLRecords; 145 | 146 | // QList m_listResult; // TODO remove 147 | #ifndef QT_SCRIPT_LIB 148 | global_script m_globalScript; 149 | #endif 150 | }; 151 | 152 | #endif // DIE_SCRIPTENGINE_H 153 | -------------------------------------------------------------------------------- /die_scriptengine.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2019-2025 hors 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | * SOFTWARE. 20 | */ 21 | #include "die_scriptengine.h" 22 | 23 | DiE_ScriptEngine::DiE_ScriptEngine(QList *pSignaturesList, QList *pListScanStructs, QIODevice *pDevice, 24 | XBinary::FT fileType, XBinary::FILEPART filePart, Binary_Script::OPTIONS *pOptions, XBinary::PDSTRUCT *pPdStruct) 25 | : XScriptEngine() 26 | { 27 | m_parentId = {}; 28 | m_resultId = {}; 29 | 30 | m_pSignaturesList = pSignaturesList; 31 | m_pListScanStructs = pListScanStructs; 32 | m_pPdStruct = pPdStruct; 33 | 34 | // qRegisterMetaType>("QList"); 35 | 36 | #ifdef QT_SCRIPT_LIB 37 | _addFunction(includeScript, "includeScript"); 38 | _addFunction(_log, "_log"); 39 | _addFunction(_setResult, "_setResult"); 40 | _addFunction(_isResultPresent, "_isResultPresent"); 41 | _addFunction(_getNumberOfResults, "_getNumberOfResults"); 42 | _addFunction(_removeResult, "_removeResult"); 43 | _addFunction(_isStop, "_isStop"); 44 | _addFunction(_encodingList, "_encodingList"); 45 | _addFunction(_isConsoleMode, "_isConsoleMode"); 46 | _addFunction(_isLiteMode, "_isLiteMode"); 47 | _addFunction(_isGuiMode, "_isGuiMode"); 48 | _addFunction(_isLibraryMode, "_isLibraryMode"); 49 | _addFunction(_breakScan, "_breakScan"); 50 | _addFunction(_getEngineVersion, "_getEngineVersion"); 51 | _addFunction(_getOS, "_getOS"); 52 | #else 53 | connect(&m_globalScript, SIGNAL(includeScriptSignal(QString)), this, SLOT(includeScriptSlot(QString)), Qt::DirectConnection); 54 | connect(&m_globalScript, SIGNAL(_logSignal(QString)), this, SLOT(_logSlot(QString)), Qt::DirectConnection); 55 | connect(&m_globalScript, SIGNAL(_setResultSignal(QString, QString, QString, QString)), this, SLOT(_setResultSlot(QString, QString, QString, QString)), 56 | Qt::DirectConnection); 57 | connect(&m_globalScript, SIGNAL(_isResultPresentSignal(bool *, QString, QString)), this, SLOT(_isResultPresentSlot(bool *, QString, QString)), Qt::DirectConnection); 58 | connect(&m_globalScript, SIGNAL(_getNumberOfResultsSignal(qint32 *, QString)), this, SLOT(_getNumberOfResultsSlot(qint32 *, QString)), Qt::DirectConnection); 59 | connect(&m_globalScript, SIGNAL(_removeResultSignal(QString, QString)), this, SLOT(_removeResultSlot(QString, QString)), Qt::DirectConnection); 60 | connect(&m_globalScript, SIGNAL(_isStopSignal(bool *)), this, SLOT(_isStopSlot(bool *)), Qt::DirectConnection); 61 | connect(&m_globalScript, SIGNAL(_encodingListSignal()), this, SLOT(_encodingListSlot()), Qt::DirectConnection); 62 | connect(&m_globalScript, SIGNAL(_isConsoleModeSignal(bool *)), this, SLOT(_isConsoleModeSlot(bool *)), Qt::DirectConnection); 63 | connect(&m_globalScript, SIGNAL(_isLiteModeSignal(bool *)), this, SLOT(_isLiteModeSlot(bool *)), Qt::DirectConnection); 64 | connect(&m_globalScript, SIGNAL(_isGuiModeSignal(bool *)), this, SLOT(_isGuiModeSlot(bool *)), Qt::DirectConnection); 65 | connect(&m_globalScript, SIGNAL(_isLibraryModeSignal(bool *)), this, SLOT(_isLibraryModeSlot(bool *)), Qt::DirectConnection); 66 | connect(&m_globalScript, SIGNAL(_breakScanSignal()), this, SLOT(_breakScanSlot()), Qt::DirectConnection); 67 | connect(&m_globalScript, SIGNAL(_getEngineVersionSignal(QString *)), this, SLOT(_getEngineVersionSlot(QString *)), Qt::DirectConnection); 68 | connect(&m_globalScript, SIGNAL(_getOSSignal(QString *)), this, SLOT(_getOSSlot(QString *)), Qt::DirectConnection); 69 | connect(&m_globalScript, SIGNAL(_getQtVersionSignal(QString *)), this, SLOT(_getQtVersionSlot(QString *)), Qt::DirectConnection); 70 | 71 | QJSValue valueGlobalScript = newQObject(&m_globalScript); 72 | globalObject().setProperty("includeScript", valueGlobalScript.property("includeScript")); 73 | globalObject().setProperty("_log", valueGlobalScript.property("_log")); 74 | globalObject().setProperty("_setResult", valueGlobalScript.property("_setResult")); 75 | globalObject().setProperty("_isResultPresent", valueGlobalScript.property("_isResultPresent")); 76 | globalObject().setProperty("_getNumberOfResults", valueGlobalScript.property("_getNumberOfResults")); 77 | globalObject().setProperty("_removeResult", valueGlobalScript.property("_removeResult")); 78 | globalObject().setProperty("_isStop", valueGlobalScript.property("_isStop")); 79 | globalObject().setProperty("_encodingList", valueGlobalScript.property("_encodingList")); 80 | globalObject().setProperty("_isConsoleMode", valueGlobalScript.property("_isConsoleMode")); 81 | globalObject().setProperty("_isLiteMode", valueGlobalScript.property("_isLiteMode")); 82 | globalObject().setProperty("_isGuiMode", valueGlobalScript.property("_isGuiMode")); 83 | globalObject().setProperty("_isLibraryMode", valueGlobalScript.property("_isLibraryMode")); 84 | globalObject().setProperty("_breakScan", valueGlobalScript.property("_breakScan")); 85 | globalObject().setProperty("_getEngineVersion", valueGlobalScript.property("_getEngineVersion")); 86 | globalObject().setProperty("_getOS", valueGlobalScript.property("_getOS")); 87 | globalObject().setProperty("_getQtVersion", valueGlobalScript.property("_getQtVersion")); 88 | #endif 89 | 90 | Util_script *pUtilScript = new Util_script; 91 | _addClass(pUtilScript, "Util"); 92 | m_listScriptClasses.append(pUtilScript); 93 | 94 | if (XBinary::checkFileType(XBinary::FT_BINARY, fileType)) { 95 | XBinary *pBinary = new XBinary(pDevice); 96 | Binary_Script *pExtraScript = new Binary_Script(pBinary, filePart, pOptions, pPdStruct); 97 | _adjustScript(pBinary, pExtraScript, "Binary"); 98 | } else if (XBinary::checkFileType(XBinary::FT_COM, fileType)) { 99 | XCOM *pCOM = new XCOM(pDevice); 100 | COM_Script *pExtraScript = new COM_Script(pCOM, filePart, pOptions, pPdStruct); 101 | _adjustScript(pCOM, pExtraScript, "COM"); 102 | } else if (XBinary::checkFileType(XBinary::FT_PE, fileType)) { 103 | XPE *pPE = new XPE(pDevice); 104 | PE_Script *pExtraScript = new PE_Script(pPE, filePart, pOptions, pPdStruct); 105 | _adjustScript(pPE, pExtraScript, "PE"); 106 | } else if (XBinary::checkFileType(XBinary::FT_ELF, fileType)) { 107 | XELF *pELF = new XELF(pDevice); 108 | ELF_Script *pExtraScript = new ELF_Script(pELF, filePart, pOptions, pPdStruct); 109 | _adjustScript(pELF, pExtraScript, "ELF"); 110 | } else if (XBinary::checkFileType(XBinary::FT_MACHO, fileType)) { 111 | XMACH *pMACH = new XMACH(pDevice); 112 | MACH_Script *pExtraScript = new MACH_Script(pMACH, filePart, pOptions, pPdStruct); 113 | _adjustScript(pMACH, pExtraScript, "MACH"); 114 | } else if (XBinary::checkFileType(XBinary::FT_NE, fileType)) { 115 | XNE *pNE = new XNE(pDevice); 116 | NE_Script *pExtraScript = new NE_Script(pNE, filePart, pOptions, pPdStruct); 117 | _adjustScript(pNE, pExtraScript, "NE"); 118 | } else if (XBinary::checkFileType(XBinary::FT_LE, fileType)) { 119 | XLE *pLE = new XLE(pDevice); 120 | LE_Script *pExtraScript = new LE_Script(pLE, filePart, pOptions, pPdStruct); 121 | _adjustScript(pLE, pExtraScript, "LE"); 122 | } else if (XBinary::checkFileType(XBinary::FT_LX, fileType)) { 123 | XLE *pLE = new XLE(pDevice); 124 | LX_Script *pExtraScript = new LX_Script(pLE, filePart, pOptions, pPdStruct); 125 | _adjustScript(pLE, pExtraScript, "LX"); 126 | } else if (XBinary::checkFileType(XBinary::FT_MSDOS, fileType)) { 127 | XMSDOS *pXMSDOS = new XMSDOS(pDevice); 128 | MSDOS_Script *pExtraScript = new MSDOS_Script(pXMSDOS, filePart, pOptions, pPdStruct); 129 | _adjustScript(pXMSDOS, pExtraScript, "MSDOS"); 130 | } else if (XBinary::checkFileType(XBinary::FT_ARCHIVE, fileType)) { 131 | Archive_Script *pExtraScript = nullptr; 132 | 133 | QSet fileTypes = XBinary::getFileTypes(pDevice, true); 134 | XBinary::FT _fileType = XBinary::_getPrefFileType(&fileTypes); 135 | 136 | XArchive *_pArchive = XArchives::getClass(_fileType, pDevice); 137 | 138 | if (_pArchive) { 139 | pExtraScript = new Archive_Script(_pArchive, filePart, pOptions, pPdStruct); 140 | _adjustScript(_pArchive, pExtraScript, "Archive"); 141 | } 142 | } else if (XBinary::checkFileType(XBinary::FT_IMAGE, fileType)) { 143 | Image_Script *pExtraScript = nullptr; 144 | 145 | QSet fileTypes = XBinary::getFileTypes(pDevice, true); 146 | 147 | XBinary *_pImage = nullptr; 148 | 149 | if (fileTypes.contains(XBinary::FT_JPEG)) { 150 | _pImage = new XJpeg(pDevice); 151 | pExtraScript = new Jpeg_Script((XJpeg *)_pImage, filePart, pOptions, pPdStruct); 152 | } else if (fileTypes.contains(XBinary::FT_PNG)) { 153 | _pImage = new XPNG(pDevice); 154 | pExtraScript = new PNG_Script((XPNG *)_pImage, filePart, pOptions, pPdStruct); 155 | } 156 | // TODO more 157 | _adjustScript(_pImage, pExtraScript, "Image"); 158 | } else if (XBinary::checkFileType(XBinary::FT_RAR, fileType)) { 159 | XRar *pRAR = new XRar(pDevice); 160 | RAR_Script *pExtraScript = new RAR_Script(pRAR, filePart, pOptions, pPdStruct); 161 | _adjustScript(pRAR, pExtraScript, "RAR"); 162 | } else if (XBinary::checkFileType(XBinary::FT_ZIP, fileType)) { 163 | XZip *pZIP = new XZip(pDevice); 164 | ZIP_Script *pExtraScript = new ZIP_Script(pZIP, filePart, pOptions, pPdStruct); 165 | _adjustScript(pZIP, pExtraScript, "ZIP"); 166 | } else if (XBinary::checkFileType(XBinary::FT_JAR, fileType)) { 167 | XJAR *pJAR = new XJAR(pDevice); 168 | JAR_Script *pExtraScript = new JAR_Script(pJAR, filePart, pOptions, pPdStruct); 169 | _adjustScript(pJAR, pExtraScript, "JAR"); 170 | } else if (XBinary::checkFileType(XBinary::FT_APK, fileType)) { 171 | XAPK *pAPK = new XAPK(pDevice); 172 | APK_Script *pExtraScript = new APK_Script(pAPK, filePart, pOptions, pPdStruct); 173 | _adjustScript(pAPK, pExtraScript, "APK"); 174 | } else if (XBinary::checkFileType(XBinary::FT_IPA, fileType)) { 175 | XIPA *pIPA = new XIPA(pDevice); 176 | IPA_Script *pExtraScript = new IPA_Script(pIPA, filePart, pOptions, pPdStruct); 177 | _adjustScript(pIPA, pExtraScript, "IPA"); 178 | } else if (XBinary::checkFileType(XBinary::FT_NPM, fileType)) { 179 | XNPM *pNPNM = new XNPM(pDevice); 180 | NPM_Script *pExtraScript = new NPM_Script(pNPNM, filePart, pOptions, pPdStruct); 181 | _adjustScript(pNPNM, pExtraScript, "NPM"); 182 | } else if (XBinary::checkFileType(XBinary::FT_MACHOFAT, fileType)) { 183 | XMACHOFat *pMachofat = new XMACHOFat(pDevice); 184 | MACHOFAT_Script *pExtraScript = new MACHOFAT_Script(pMachofat, filePart, pOptions, pPdStruct); 185 | _adjustScript(pMachofat, pExtraScript, "MACHOFAT"); 186 | } else if (XBinary::checkFileType(XBinary::FT_DOS16M, fileType)) { 187 | XDOS16 *pDOS16 = new XDOS16(pDevice); 188 | DOS16M_Script *pExtraScript = new DOS16M_Script(pDOS16, filePart, pOptions, pPdStruct); 189 | _adjustScript(pDOS16, pExtraScript, "DOS16M"); 190 | } else if (XBinary::checkFileType(XBinary::FT_DOS4G, fileType)) { 191 | XDOS16 *pDOS16 = new XDOS16(pDevice); 192 | DOS4G_Script *pExtraScript = new DOS4G_Script(pDOS16, filePart, pOptions, pPdStruct); 193 | _adjustScript(pDOS16, pExtraScript, "DOS4G"); 194 | } else if (XBinary::checkFileType(XBinary::FT_DEX, fileType)) { 195 | XDEX *pDEX = new XDEX(pDevice); 196 | DEX_Script *pExtraScript = new DEX_Script(pDEX, filePart, pOptions, pPdStruct); 197 | _adjustScript(pDEX, pExtraScript, "DEX"); 198 | } else if (XBinary::checkFileType(XBinary::FT_AMIGAHUNK, fileType)) { 199 | XAmigaHunk *pAmiga = new XAmigaHunk(pDevice); 200 | Amiga_Script *pExtraScript = new Amiga_Script(pAmiga, filePart, pOptions, pPdStruct); 201 | _adjustScript(pAmiga, pExtraScript, "Amiga"); 202 | } else if (XBinary::checkFileType(XBinary::FT_ATARIST, fileType)) { 203 | XAtariST *pAtariST = new XAtariST(pDevice); 204 | AtariST_Script *pExtraScript = new AtariST_Script(pAtariST, filePart, pOptions, pPdStruct); 205 | _adjustScript(pAtariST, pExtraScript, "AtariST"); 206 | } else if (XBinary::checkFileType(XBinary::FT_JAVACLASS, fileType)) { 207 | XJavaClass *pAmiga = new XJavaClass(pDevice); 208 | JavaClass_Script *pExtraScript = new JavaClass_Script(pAmiga, filePart, pOptions, pPdStruct); 209 | _adjustScript(pAmiga, pExtraScript, "JavaClass"); 210 | } else if (XBinary::checkFileType(XBinary::FT_PDF, fileType)) { 211 | XPDF *pPDF = new XPDF(pDevice); 212 | PDF_Script *pExtraScript = new PDF_Script(pPDF, filePart, pOptions, pPdStruct); 213 | _adjustScript(pPDF, pExtraScript, "PDF"); 214 | } else if (XBinary::checkFileType(XBinary::FT_CFBF, fileType)) { 215 | XCFBF *pCFBF = new XCFBF(pDevice); 216 | CFBF_Script *pExtraScript = new CFBF_Script(pCFBF, filePart, pOptions, pPdStruct); 217 | _adjustScript(pCFBF, pExtraScript, "CFBF"); 218 | } else if (XBinary::checkFileType(XBinary::FT_JPEG, fileType)) { 219 | XJpeg *pJpeg = new XJpeg(pDevice); 220 | Jpeg_Script *pExtraScript = new Jpeg_Script(pJpeg, filePart, pOptions, pPdStruct); 221 | _adjustScript(pJpeg, pExtraScript, "Jpeg"); 222 | } else if (XBinary::checkFileType(XBinary::FT_PNG, fileType)) { 223 | XPNG *pPNG = new XPNG(pDevice); 224 | PNG_Script *pExtraScript = new PNG_Script(pPNG, filePart, pOptions, pPdStruct); 225 | _adjustScript(pPNG, pExtraScript, "PNG"); 226 | } 227 | 228 | // TODO APKS 229 | } 230 | 231 | DiE_ScriptEngine::~DiE_ScriptEngine() 232 | { 233 | { 234 | qint32 nNumberOfRecords = m_listBinaries.count(); 235 | 236 | for (qint32 i = 0; i < nNumberOfRecords; i++) { 237 | delete m_listBinaries.at(i); 238 | } 239 | } 240 | 241 | { 242 | qint32 nNumberOfRecords = m_listScriptClasses.count(); 243 | 244 | for (qint32 i = 0; i < nNumberOfRecords; i++) { 245 | delete m_listScriptClasses.at(i); 246 | } 247 | } 248 | } 249 | 250 | void DiE_ScriptEngine::_adjustScript(XBinary *pBinary, Binary_Script *pScript, const QString &sName) 251 | { 252 | if (pScript) { 253 | connect(pScript, SIGNAL(errorMessage(QString)), this, SIGNAL(errorMessage(QString))); 254 | connect(pScript, SIGNAL(warningMessage(QString)), this, SIGNAL(warningMessage(QString))); 255 | connect(pScript, SIGNAL(infoMessage(QString)), this, SIGNAL(infoMessage(QString))); 256 | } 257 | 258 | _addClass(pScript, "Binary"); 259 | 260 | if (sName != "Binary") { 261 | _addClass(pScript, sName); 262 | } 263 | 264 | m_listBinaries.append(pBinary); 265 | m_listScriptClasses.append(pScript); 266 | } 267 | 268 | bool DiE_ScriptEngine::handleError(QString sPrefix, XSCRIPTVALUE value, QString *psErrorString) 269 | { 270 | bool bResult = true; 271 | 272 | if (value.isError()) { 273 | // TODO Check more information 274 | *psErrorString = QString("%1: %2: %4").arg(sPrefix, value.property("lineNumber").toString(), value.toString()); 275 | 276 | #ifdef QT_DEBUG 277 | qDebug("%s", (*psErrorString).toUtf8().data()); 278 | #endif 279 | 280 | bResult = false; 281 | } 282 | 283 | return bResult; 284 | } 285 | 286 | XSCRIPTVALUE DiE_ScriptEngine::evaluateEx(const XScanEngine::SCANID &parentId, const XScanEngine::SCANID &resultId, const QString &sProgram, const QString &sName, 287 | const QString &sFileName) 288 | { 289 | m_parentId = parentId; 290 | m_resultId = resultId; 291 | m_sName = sName; 292 | m_sFileName = sFileName; 293 | 294 | return evaluate(sProgram, sFileName); 295 | } 296 | 297 | // QList DiE_ScriptEngine::getListLocalResult() 298 | // { 299 | // return g_listResult; 300 | // } 301 | 302 | // void DiE_ScriptEngine::clearListLocalResult() 303 | // { 304 | // g_listResult.clear(); 305 | // } 306 | 307 | #ifdef QT_SCRIPT_LIB 308 | QScriptValue DiE_ScriptEngine::includeScript(QScriptContext *pContext, QScriptEngine *pEngine) 309 | { 310 | QScriptValue result; 311 | 312 | DiE_ScriptEngine *pScriptEngine = static_cast(pEngine); 313 | 314 | if (pScriptEngine) { 315 | pEngine->currentContext()->setActivationObject(pEngine->currentContext()->parentContext()->activationObject()); 316 | 317 | QString sScript = pContext->argument(0).toString(); 318 | 319 | pScriptEngine->includeScriptSlot(sScript); 320 | } 321 | 322 | return result; 323 | } 324 | #endif 325 | #ifdef QT_SCRIPT_LIB 326 | QScriptValue DiE_ScriptEngine::_log(QScriptContext *pContext, QScriptEngine *pEngine) 327 | { 328 | QScriptValue result; 329 | 330 | DiE_ScriptEngine *pScriptEngine = static_cast(pEngine); 331 | 332 | if (pScriptEngine) { 333 | QString sText = pContext->argument(0).toString(); 334 | 335 | pScriptEngine->_logSlot(sText); 336 | } 337 | 338 | return result; 339 | } 340 | #endif 341 | #ifdef QT_SCRIPT_LIB 342 | QScriptValue DiE_ScriptEngine::_setResult(QScriptContext *pContext, QScriptEngine *pEngine) 343 | { 344 | QScriptValue result; 345 | 346 | DiE_ScriptEngine *pScriptEngine = static_cast(pEngine); 347 | 348 | if (pScriptEngine) { 349 | QString sType = pContext->argument(0).toString(); 350 | QString sName = pContext->argument(1).toString(); 351 | QString sVersion = pContext->argument(2).toString(); 352 | QString sOptions = pContext->argument(3).toString(); 353 | 354 | pScriptEngine->_setResultSlot(sType, sName, sVersion, sOptions); 355 | } 356 | 357 | return result; 358 | } 359 | #endif 360 | #ifdef QT_SCRIPT_LIB 361 | QScriptValue DiE_ScriptEngine::_isResultPresent(QScriptContext *pContext, QScriptEngine *pEngine) 362 | { 363 | QScriptValue result; 364 | 365 | DiE_ScriptEngine *pScriptEngine = static_cast(pEngine); 366 | 367 | if (pScriptEngine) { 368 | QString sType = pContext->argument(0).toString(); 369 | QString sName = pContext->argument(1).toString(); 370 | bool bResult = false; 371 | 372 | pScriptEngine->_isResultPresentSlot(&bResult, sType, sName); 373 | 374 | result = bResult; 375 | } 376 | 377 | return result; 378 | } 379 | #endif 380 | #ifdef QT_SCRIPT_LIB 381 | QScriptValue DiE_ScriptEngine::_getNumberOfResults(QScriptContext *pContext, QScriptEngine *pEngine) 382 | { 383 | QScriptValue result; 384 | 385 | DiE_ScriptEngine *pScriptEngine = static_cast(pEngine); 386 | 387 | if (pScriptEngine) { 388 | QString sType = pContext->argument(0).toString(); 389 | qint32 nResult = 0; 390 | 391 | pScriptEngine->_getNumberOfResultsSlot(&nResult, sType); 392 | 393 | result = nResult; 394 | } 395 | 396 | return result; 397 | } 398 | #endif 399 | #ifdef QT_SCRIPT_LIB 400 | QScriptValue DiE_ScriptEngine::_removeResult(QScriptContext *pContext, QScriptEngine *pEngine) 401 | { 402 | QScriptValue result; 403 | 404 | DiE_ScriptEngine *pScriptEngine = static_cast(pEngine); 405 | 406 | if (pScriptEngine) { 407 | QString sType = pContext->argument(0).toString(); 408 | QString sName = pContext->argument(1).toString(); 409 | 410 | pScriptEngine->_removeResultSlot(sType, sName); 411 | } 412 | 413 | return result; 414 | } 415 | #endif 416 | #ifdef QT_SCRIPT_LIB 417 | QScriptValue DiE_ScriptEngine::_isStop(QScriptContext *pContext, QScriptEngine *pEngine) 418 | { 419 | Q_UNUSED(pContext) 420 | 421 | QScriptValue result; 422 | 423 | DiE_ScriptEngine *pScriptEngine = static_cast(pEngine); 424 | 425 | if (pScriptEngine) { 426 | bool bResult = false; 427 | 428 | pScriptEngine->_isStopSlot(&bResult); 429 | 430 | result = bResult; 431 | } 432 | 433 | return result; 434 | } 435 | #endif 436 | #ifdef QT_SCRIPT_LIB 437 | QScriptValue DiE_ScriptEngine::_encodingList(QScriptContext *pContext, QScriptEngine *pEngine) 438 | { 439 | Q_UNUSED(pContext) 440 | 441 | QScriptValue result; 442 | 443 | DiE_ScriptEngine *pScriptEngine = static_cast(pEngine); 444 | 445 | if (pScriptEngine) { 446 | bool bResult = false; 447 | 448 | pScriptEngine->_encodingListSlot(); 449 | 450 | result = bResult; 451 | } 452 | 453 | return result; 454 | } 455 | #endif 456 | #ifdef QT_SCRIPT_LIB 457 | QScriptValue DiE_ScriptEngine::_isConsoleMode(QScriptContext *pContext, QScriptEngine *pEngine) 458 | { 459 | Q_UNUSED(pContext) 460 | 461 | QScriptValue result; 462 | 463 | DiE_ScriptEngine *pScriptEngine = static_cast(pEngine); 464 | 465 | if (pScriptEngine) { 466 | bool bResult = false; 467 | 468 | pScriptEngine->_isConsoleModeSlot(&bResult); 469 | 470 | result = bResult; 471 | } 472 | 473 | return result; 474 | } 475 | #endif 476 | #ifdef QT_SCRIPT_LIB 477 | QScriptValue DiE_ScriptEngine::_isGuiMode(QScriptContext *pContext, QScriptEngine *pEngine) 478 | { 479 | Q_UNUSED(pContext) 480 | 481 | QScriptValue result; 482 | 483 | DiE_ScriptEngine *pScriptEngine = static_cast(pEngine); 484 | 485 | if (pScriptEngine) { 486 | bool bResult = false; 487 | 488 | pScriptEngine->_isGuiModeSlot(&bResult); 489 | 490 | result = bResult; 491 | } 492 | 493 | return result; 494 | } 495 | #endif 496 | #ifdef QT_SCRIPT_LIB 497 | QScriptValue DiE_ScriptEngine::_isLiteMode(QScriptContext *pContext, QScriptEngine *pEngine) 498 | { 499 | Q_UNUSED(pContext) 500 | 501 | QScriptValue result; 502 | 503 | DiE_ScriptEngine *pScriptEngine = static_cast(pEngine); 504 | 505 | if (pScriptEngine) { 506 | bool bResult = false; 507 | 508 | pScriptEngine->_isLiteModeSlot(&bResult); 509 | 510 | result = bResult; 511 | } 512 | 513 | return result; 514 | } 515 | #endif 516 | #ifdef QT_SCRIPT_LIB 517 | QScriptValue DiE_ScriptEngine::_isLibraryMode(QScriptContext *pContext, QScriptEngine *pEngine) 518 | { 519 | Q_UNUSED(pContext) 520 | 521 | QScriptValue result; 522 | 523 | DiE_ScriptEngine *pScriptEngine = static_cast(pEngine); 524 | 525 | if (pScriptEngine) { 526 | bool bResult = false; 527 | 528 | pScriptEngine->_isLibraryModeSlot(&bResult); 529 | 530 | result = bResult; 531 | } 532 | 533 | return result; 534 | } 535 | #endif 536 | #ifdef QT_SCRIPT_LIB 537 | QScriptValue DiE_ScriptEngine::_breakScan(QScriptContext *pContext, QScriptEngine *pEngine) 538 | { 539 | Q_UNUSED(pContext) 540 | 541 | QScriptValue result; 542 | 543 | DiE_ScriptEngine *pScriptEngine = static_cast(pEngine); 544 | 545 | if (pScriptEngine) { 546 | pScriptEngine->_breakScanSlot(); 547 | } 548 | 549 | return result; 550 | } 551 | #endif 552 | #ifdef QT_SCRIPT_LIB 553 | QScriptValue DiE_ScriptEngine::_getEngineVersion(QScriptContext *pContext, QScriptEngine *pEngine) 554 | { 555 | Q_UNUSED(pContext) 556 | 557 | QScriptValue result; 558 | 559 | DiE_ScriptEngine *pScriptEngine = static_cast(pEngine); 560 | 561 | if (pScriptEngine) { 562 | QString sResult; 563 | 564 | pScriptEngine->_getEngineVersionSlot(&sResult); 565 | 566 | result = sResult; 567 | } 568 | 569 | return result; 570 | } 571 | #endif 572 | #ifdef QT_SCRIPT_LIB 573 | QScriptValue DiE_ScriptEngine::_getOS(QScriptContext *pContext, QScriptEngine *pEngine) 574 | { 575 | Q_UNUSED(pContext) 576 | 577 | QScriptValue result; 578 | 579 | DiE_ScriptEngine *pScriptEngine = static_cast(pEngine); 580 | 581 | if (pScriptEngine) { 582 | QString sResult; 583 | 584 | pScriptEngine->_getOSSlot(&sResult); 585 | 586 | result = sResult; 587 | } 588 | 589 | return result; 590 | } 591 | #endif 592 | 593 | void DiE_ScriptEngine::includeScriptSlot(const QString &sScript) 594 | { 595 | bool bSuccess = false; 596 | 597 | qint32 nNumberOfSignatures = m_pSignaturesList->count(); 598 | 599 | for (qint32 i = 0; i < nNumberOfSignatures; i++) { 600 | if (m_pSignaturesList->at(i).fileType == XBinary::FT_UNKNOWN) { 601 | if (m_pSignaturesList->at(i).sName.toUpper() == sScript.toUpper()) { 602 | XSCRIPTVALUE value = evaluate(m_pSignaturesList->at(i).sText, sScript); 603 | 604 | if (value.isError()) { 605 | emit errorMessage(QString("includeScript %1: %2: %3").arg(sScript, value.property("lineNumber").toString(), value.toString())); 606 | } 607 | 608 | bSuccess = true; 609 | 610 | break; 611 | } 612 | } 613 | } 614 | 615 | if (!bSuccess) { 616 | emit errorMessage(QString("%1: %2").arg(tr("Cannot find"), sScript)); 617 | } 618 | } 619 | 620 | void DiE_ScriptEngine::_logSlot(const QString &sText) 621 | { 622 | #ifdef QT_DEBUG 623 | qDebug("LOG: %s", sText.toUtf8().data()); 624 | #endif 625 | 626 | emit infoMessage(sText); 627 | } 628 | 629 | void DiE_ScriptEngine::_setResultSlot(const QString &sType, const QString &sName, const QString &sVersion, const QString &sOptions) 630 | { 631 | bool bAdd = true; 632 | 633 | qint32 nNumberOfResults = m_listBLRecords.count(); 634 | 635 | for (qint32 i = 0; i < nNumberOfResults; i++) { 636 | if ((m_listBLRecords.at(i).sType.toUpper() == sType.toUpper()) && 637 | ((m_listBLRecords.at(i).sName.toUpper() == sName.toUpper()) || (m_listBLRecords.at(i).sName == ""))) { 638 | bAdd = false; 639 | break; 640 | } 641 | } 642 | 643 | if (bAdd) { 644 | DiE_ScriptEngine::SCAN_STRUCT ssRecord = {}; 645 | 646 | // TODO IDs 647 | ssRecord.id = m_resultId; 648 | ssRecord.parentId = m_parentId; 649 | 650 | ssRecord.sSignature = m_sName; 651 | ssRecord.sSignatureFileName = m_sFileName; 652 | 653 | ssRecord.sType = sType; 654 | ssRecord.sName = sName; 655 | ssRecord.sVersion = sVersion; 656 | ssRecord.sOptions = sOptions; 657 | // ssRecord.sFullString = QString("%1: %2(%3)[%4]").arg(ssRecord.sType, ssRecord.sName, ssRecord.sVersion, ssRecord.sOptions); 658 | // ssRecord.sResult = QString("%1(%2)[%3]").arg(ssRecord.sName, ssRecord.sVersion, ssRecord.sOptions); 659 | 660 | m_pListScanStructs->append(ssRecord); 661 | } 662 | } 663 | 664 | void DiE_ScriptEngine::_isResultPresentSlot(bool *pbResult, const QString &sType, const QString &sName) 665 | { 666 | *pbResult = false; 667 | 668 | qint32 nNumberOfResults = m_pListScanStructs->count(); 669 | 670 | for (qint32 i = 0; i < nNumberOfResults; i++) { 671 | if ((m_pListScanStructs->at(i).sType.toUpper() == sType.toUpper()) && ((m_pListScanStructs->at(i).sName.toUpper() == sName.toUpper()) || (sName == ""))) { 672 | *pbResult = true; 673 | break; 674 | } 675 | } 676 | } 677 | 678 | void DiE_ScriptEngine::_getNumberOfResultsSlot(qint32 *pnResult, const QString &sType) 679 | { 680 | *pnResult = 0; 681 | 682 | qint32 nNumberOfResults = m_pListScanStructs->count(); 683 | 684 | for (qint32 i = 0; i < nNumberOfResults; i++) { 685 | if ((m_pListScanStructs->at(i).sType.toUpper() == sType.toUpper()) || (sType == "")) { 686 | (*pnResult)++; 687 | } 688 | } 689 | } 690 | 691 | void DiE_ScriptEngine::_removeResultSlot(const QString &sType, const QString &sName) 692 | { 693 | BLRECORD blRecord; 694 | blRecord.sType = sType; 695 | blRecord.sName = sName; 696 | 697 | m_listBLRecords.append(blRecord); 698 | 699 | qint32 nNumberOfResults = m_pListScanStructs->count(); 700 | 701 | for (qint32 i = 0; i < nNumberOfResults; i++) { 702 | if ((m_pListScanStructs->at(i).sType.toUpper() == sType.toUpper()) && 703 | ((m_pListScanStructs->at(i).sName.toUpper() == sName.toUpper()) || (m_pListScanStructs->at(i).sName == ""))) { 704 | m_pListScanStructs->removeAt(i); 705 | break; 706 | } 707 | } 708 | } 709 | 710 | void DiE_ScriptEngine::_isStopSlot(bool *pResult) 711 | { 712 | *pResult = XBinary::isPdStructStopped(m_pPdStruct); 713 | } 714 | 715 | void DiE_ScriptEngine::_encodingListSlot() 716 | { 717 | #if (QT_VERSION_MAJOR < 6) || defined(QT_CORE5COMPAT_LIB) 718 | QList listCodePages = XOptions::getCodePages(false); 719 | 720 | qint32 nNumberOfCodePages = listCodePages.count(); 721 | 722 | for (qint32 i = 0; i < nNumberOfCodePages; i++) { 723 | emit infoMessage(listCodePages.at(i)); 724 | } 725 | #endif 726 | } 727 | 728 | void DiE_ScriptEngine::_isConsoleModeSlot(bool *pResult) 729 | { 730 | bool bConsole = false; 731 | #ifndef QT_GUI_LIB 732 | bConsole = true; 733 | #endif 734 | *pResult = bConsole && (qApp->applicationName() == "die"); 735 | } 736 | 737 | void DiE_ScriptEngine::_isLiteModeSlot(bool *pResult) 738 | { 739 | *pResult = (qApp->applicationName() == "diel"); 740 | } 741 | 742 | void DiE_ScriptEngine::_isGuiModeSlot(bool *pResult) 743 | { 744 | bool bGui = false; 745 | #ifdef QT_GUI_LIB 746 | bGui = true; 747 | #endif 748 | *pResult = bGui && (qApp->applicationName() == "die"); 749 | } 750 | 751 | void DiE_ScriptEngine::_isLibraryModeSlot(bool *pResult) 752 | { 753 | *pResult = (qApp->applicationName() == ""); 754 | } 755 | 756 | void DiE_ScriptEngine::_breakScanSlot() 757 | { 758 | XBinary::setPdStructStopped(m_pPdStruct); 759 | } 760 | 761 | void DiE_ScriptEngine::_getEngineVersionSlot(QString *pResult) 762 | { 763 | QDate qDate = QDate::fromString(__DATE__, "MMM dd yyyy"); 764 | 765 | *pResult = QString("%1.%2").arg(qApp->applicationVersion(), qDate.toString("yyyy.MM.dd")); 766 | } 767 | 768 | void DiE_ScriptEngine::_getOSSlot(QString *pResult) 769 | { 770 | *pResult = XOptions::getBundleIdToString(XOptions::getBundle()); 771 | } 772 | 773 | void DiE_ScriptEngine::_getQtVersionSlot(QString *pResult) 774 | { 775 | *pResult = QString("%1.%2.%3").arg(QString::number(QT_VERSION_MAJOR), QString::number(QT_VERSION_MINOR), QString::number(QT_VERSION_PATCH)); 776 | } 777 | 778 | // DiE_ScriptEngine::RESULT DiE_ScriptEngine::stringToResult(const QString &sString, bool bShowType, bool bShowVersion, bool bShowOptions) 779 | // { 780 | // QString sStringTmp = sString; 781 | // RESULT result = {}; 782 | 783 | // if (bShowType) { 784 | // result.sType = sStringTmp.section(": ", 0, 0); 785 | // sStringTmp = sStringTmp.section(": ", 1, -1); 786 | // } 787 | 788 | // QString _sString = sStringTmp; 789 | 790 | // if (bShowOptions) { 791 | // if (_sString.count("[") == 1) { 792 | // result.sName = _sString.section("[", 0, 0); 793 | // result.sOptions = _sString.section("[", 1, -1).section("]", 0, 0); 794 | // _sString = _sString.section("[", 0, 0); 795 | // } 796 | // } 797 | 798 | // if (bShowVersion) { 799 | // if (_sString.count("(") == 1) { 800 | // result.sVersion = _sString.section("(", 1, -1).section(")", 0, 0); 801 | // result.sName = _sString.section("(", 0, 0); 802 | // } 803 | // } 804 | 805 | // return result; 806 | // } 807 | -------------------------------------------------------------------------------- /die_script.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2019-2025 hors 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all 11 | * copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | * SOFTWARE. 20 | */ 21 | #include "die_script.h" 22 | 23 | bool sort_signature_prio(const DiE_ScriptEngine::SIGNATURE_RECORD &sr1, const DiE_ScriptEngine::SIGNATURE_RECORD &sr2) 24 | { 25 | if (sr1.fileType != sr2.fileType) { 26 | return (sr1.fileType < sr2.fileType); 27 | } 28 | 29 | // if ((sr1.sName == "_init") && (sr2.sName == "_init")) { 30 | // return false; 31 | // } 32 | 33 | // if (sr1.sName == "_init") { 34 | // return true; 35 | // } else if (sr2.sName == "_init") { 36 | // return false; 37 | // } 38 | 39 | QString sPrio1 = "9"; 40 | QString sPrio2 = "9"; 41 | 42 | qint32 nPos1 = sr1.sName.count("."); 43 | qint32 nPos2 = sr2.sName.count("."); 44 | 45 | if ((nPos1 > 1) && (nPos2 > 1)) { 46 | sPrio1 = sr1.sName.section(".", nPos1 - 1, nPos1 - 1); 47 | sPrio2 = sr2.sName.section(".", nPos2 - 1, nPos2 - 1); 48 | } 49 | 50 | if ((sPrio1 != "") && (sPrio2 != "")) { 51 | if (sPrio1 > sPrio2) { 52 | return false; 53 | } else if (sPrio1 < sPrio2) { 54 | return true; 55 | } 56 | } 57 | 58 | return (sr1.sName < sr2.sName); 59 | } 60 | 61 | bool sort_signature_name(const DiE_ScriptEngine::SIGNATURE_RECORD &sr1, const DiE_ScriptEngine::SIGNATURE_RECORD &sr2) 62 | { 63 | if ((sr1.sName == "_init") && (sr2.sName == "_init")) { 64 | return false; 65 | } 66 | 67 | if (sr1.sName == "_init") { 68 | return true; 69 | } else if (sr2.sName == "_init") { 70 | return false; 71 | } 72 | 73 | return (sr1.sName < sr2.sName); 74 | } 75 | 76 | DiE_Script::DiE_Script(QObject *pParent) : XScanEngine(pParent) 77 | { 78 | #ifdef QT_SCRIPTTOOLS_LIB 79 | m_pDebugger = nullptr; 80 | #endif 81 | } 82 | 83 | DiE_Script::DiE_Script(const DiE_Script &other) : XScanEngine(other) 84 | { 85 | m_listSignatures = other.m_listSignatures; 86 | #ifdef QT_SCRIPTTOOLS_LIB 87 | m_pDebugger = other.m_pDebugger; 88 | #endif 89 | } 90 | 91 | QList DiE_Script::_loadDatabaseFromPath(const QString &sDatabasePath, DiE_ScriptEngine::DT databaseType, XBinary::FT fileType, 92 | XBinary::PDSTRUCT *pPdStruct) 93 | { 94 | QList listResult; 95 | 96 | QDir dir(sDatabasePath); 97 | 98 | QFileInfoList eil = dir.entryInfoList(); 99 | 100 | qint32 nNumberOfFiles = eil.count(); 101 | 102 | for (qint32 i = 0; (i < nNumberOfFiles) && XBinary::isPdStructNotCanceled(pPdStruct); i++) { 103 | if (eil.at(i).isFile()) { 104 | QString sSuffix = eil.at(i).suffix().toLower(); 105 | 106 | if ((sSuffix == "sg") || (sSuffix == "")) { 107 | DiE_ScriptEngine::SIGNATURE_RECORD record = {}; 108 | 109 | record.fileType = fileType; 110 | record.sName = eil.at(i).fileName(); 111 | record.sText = XBinary::readFile(eil.at(i).absoluteFilePath()); 112 | record.sFilePath = eil.at(i).absoluteFilePath(); 113 | record.databaseType = databaseType; 114 | listResult.append(record); 115 | } 116 | } 117 | } 118 | 119 | return listResult; 120 | } 121 | 122 | QList DiE_Script::_loadDatabaseFromArchive(XArchive *pArchive, QList *pListRecords, 123 | DiE_ScriptEngine::DT databaseType, const QString &sPrefix, XBinary::FT fileType) 124 | { 125 | QList listResult; 126 | 127 | qint32 nNumberOfRecords = pListRecords->count(); 128 | 129 | for (qint32 i = 0; i < nNumberOfRecords; i++) { 130 | XArchive::RECORD _record = pListRecords->at(i); 131 | 132 | if (((sPrefix == "") && (!_record.spInfo.sRecordName.contains("/"))) || 133 | ((_record.spInfo.sRecordName.contains("/")) && (_record.spInfo.sRecordName.section("/", 0, 0) == sPrefix) && 134 | (_record.spInfo.sRecordName.section("/", 1, 1) != ""))) { 135 | QFileInfo fi(_record.spInfo.sRecordName); 136 | 137 | DiE_ScriptEngine::SIGNATURE_RECORD record = {}; 138 | 139 | record.fileType = fileType; 140 | record.sName = fi.fileName(); 141 | record.sText = pArchive->decompress(&_record, nullptr); 142 | record.sFilePath = _record.spInfo.sRecordName; 143 | record.databaseType = databaseType; 144 | record.bReadOnly = true; 145 | 146 | listResult.append(record); 147 | } 148 | } 149 | 150 | return listResult; 151 | } 152 | 153 | void DiE_Script::processDetect(SCANID *pScanID, XScanEngine::SCAN_RESULT *pScanResult, QIODevice *pDevice, const SCANID &parentId, XBinary::FT fileType, 154 | XScanEngine::SCAN_OPTIONS *pScanOptions, const QString &sSignatureFilePath, bool bAddUnknown, XBinary::PDSTRUCT *pPdStruct) 155 | { 156 | QString sDetectFunction = "detect"; 157 | 158 | if (pScanOptions->sDetectFunction != "") { 159 | sDetectFunction = pScanOptions->sDetectFunction; 160 | } 161 | 162 | QList listRecords; 163 | XScanEngine::SCANID resultId = {}; 164 | 165 | XBinary::_MEMORY_MAP memoryMap = XFormats::getMemoryMap(fileType, XBinary::MAPMODE_UNKNOWN, pDevice, false, -1, pPdStruct); 166 | 167 | resultId.fileType = fileType; 168 | resultId.sUuid = XBinary::generateUUID(); 169 | resultId.sArch = memoryMap.sArch; 170 | resultId.mode = memoryMap.mode; 171 | resultId.endian = memoryMap.endian; 172 | resultId.sType = memoryMap.sType; 173 | resultId.nOffset = XIODevice::getInitLocation(pDevice); 174 | resultId.nSize = pDevice->size(); 175 | resultId.filePart = XBinary::FILEPART_HEADER; 176 | 177 | qint32 nNumberOfSignatures = m_listSignatures.count(); 178 | 179 | DiE_ScriptEngine::SIGNATURE_RECORD srGlobalInit = {}; 180 | DiE_ScriptEngine::SIGNATURE_RECORD srInit = {}; 181 | 182 | bool bGlobalInit = false; 183 | bool bInit = false; 184 | 185 | for (qint32 i = 0; (i < nNumberOfSignatures) && XBinary::isPdStructNotCanceled(pPdStruct); i++) { 186 | if (m_listSignatures.at(i).sName == "_init") { 187 | if (m_listSignatures.at(i).fileType == XBinary::FT_UNKNOWN) { 188 | srGlobalInit = m_listSignatures.at(i); 189 | bGlobalInit = true; 190 | } 191 | 192 | if (XBinary::checkFileType(m_listSignatures.at(i).fileType, fileType)) { 193 | srInit = m_listSignatures.at(i); 194 | bInit = true; 195 | } 196 | } 197 | 198 | if (bGlobalInit && bInit) { 199 | break; 200 | } 201 | } 202 | 203 | Binary_Script::OPTIONS _options = {}; 204 | _options.bIsDeepScan = pScanOptions->bIsDeepScan; 205 | _options.bIsHeuristicScan = pScanOptions->bIsHeuristicScan; 206 | _options.bIsAggressiveScan = pScanOptions->bIsAggressiveScan; 207 | _options.bIsRecursiveScan = pScanOptions->bIsRecursiveScan; 208 | _options.bIsVerbose = pScanOptions->bIsVerbose; 209 | _options.bIsProfiling = pScanOptions->bLogProfiling; 210 | 211 | DiE_ScriptEngine scriptEngine(&m_listSignatures, &listRecords, pDevice, fileType, parentId.filePart, &_options, pPdStruct); 212 | 213 | connect(&scriptEngine, SIGNAL(errorMessage(QString)), this, SIGNAL(errorMessage(QString))); 214 | connect(&scriptEngine, SIGNAL(warningMessage(QString)), this, SIGNAL(warningMessage(QString))); 215 | connect(&scriptEngine, SIGNAL(infoMessage(QString)), this, SIGNAL(infoMessage(QString))); 216 | 217 | #ifdef QT_SCRIPTTOOLS_LIB 218 | if (m_pDebugger) { 219 | m_pDebugger->attachTo(&scriptEngine); 220 | } 221 | #endif 222 | 223 | if (nNumberOfSignatures) { 224 | if (bGlobalInit) { 225 | _handleError(&scriptEngine, scriptEngine.evaluate(srGlobalInit.sText, srGlobalInit.sFilePath), &srGlobalInit, pScanResult); 226 | } 227 | 228 | if (bInit) { 229 | _handleError(&scriptEngine, scriptEngine.evaluate(srInit.sText, srInit.sFilePath), &srInit, pScanResult); 230 | } 231 | } 232 | 233 | qint32 _nFreeIndex = XBinary::getFreeIndex(pPdStruct); 234 | XBinary::setPdStructInit(pPdStruct, _nFreeIndex, nNumberOfSignatures); 235 | 236 | for (qint32 i = 0; (i < nNumberOfSignatures) && XBinary::isPdStructNotCanceled(pPdStruct); i++) { 237 | DiE_ScriptEngine::SIGNATURE_RECORD signatureRecord = m_listSignatures.at(i); 238 | 239 | XBinary::setPdStructStatus(pPdStruct, _nFreeIndex, signatureRecord.sName); 240 | 241 | bool bExec = false; 242 | 243 | if (XBinary::checkFileType(signatureRecord.fileType, fileType)) { 244 | bExec = true; 245 | } 246 | 247 | if (bExec) { 248 | if (pScanOptions->sSignatureName != "") { 249 | if (pScanOptions->sSignatureName != signatureRecord.sName) { 250 | bExec = false; 251 | } 252 | } 253 | } 254 | 255 | if (bExec) { 256 | if (!pScanOptions->bIsDeepScan) { 257 | QString sPrefix = signatureRecord.sName.section(".", 0, 0).toUpper(); 258 | 259 | if ((sPrefix == "DS") || (sPrefix == "EP")) { 260 | bExec = false; 261 | } 262 | } 263 | } 264 | 265 | if (bExec) { 266 | if (!pScanOptions->bIsHeuristicScan) { 267 | QString sPrefix = signatureRecord.sName.section(".", 0, 0).toUpper(); 268 | 269 | if (sPrefix == "HEUR") { 270 | bExec = false; 271 | } 272 | } 273 | } 274 | 275 | if (bExec) { 276 | if (sSignatureFilePath != "") // TODO Check! 277 | { 278 | if (sSignatureFilePath != signatureRecord.sFilePath) { 279 | bExec = false; 280 | } 281 | } else { 282 | if (signatureRecord.sName == "_init") { 283 | bExec = false; 284 | } 285 | } 286 | } 287 | 288 | if (bExec) { 289 | if (signatureRecord.databaseType != DiE_ScriptEngine::DT_MAIN) { 290 | bool _bExec = false; 291 | if (pScanOptions->bUseCustomDatabase && (signatureRecord.databaseType == DiE_ScriptEngine::DT_CUSTOM)) { 292 | _bExec = true; 293 | } else if (pScanOptions->bUseExtraDatabase && (signatureRecord.databaseType == DiE_ScriptEngine::DT_EXTRA)) { 294 | _bExec = true; 295 | } 296 | 297 | bExec = _bExec; 298 | } 299 | } 300 | 301 | if (bExec) { 302 | if (pScanOptions->scanEngineCallback) { 303 | if (!pScanOptions->scanEngineCallback(signatureRecord.sName, nNumberOfSignatures, i, pScanOptions->pUserData)) { 304 | XBinary::setPdStructStopped(pPdStruct); 305 | } 306 | } 307 | } 308 | 309 | if (bExec) { 310 | // scriptEngine.clearListLocalResult(); 311 | if (pScanOptions->bLogProfiling) { 312 | emit warningMessage(QString("%1").arg(signatureRecord.sName)); 313 | } 314 | 315 | QElapsedTimer *pElapsedTimer = nullptr; 316 | 317 | if ((pScanOptions->bShowScanTime) || (pScanOptions->bLogProfiling)) { 318 | pElapsedTimer = new QElapsedTimer; 319 | pElapsedTimer->start(); 320 | } 321 | 322 | XSCRIPTVALUE script = scriptEngine.evaluateEx(parentId, resultId, signatureRecord.sText, signatureRecord.sName, signatureRecord.sFilePath); 323 | 324 | if (_handleError(&scriptEngine, script, &signatureRecord, pScanResult)) { 325 | #ifdef QT_SCRIPTTOOLS_LIB 326 | if (m_pDebugger) { 327 | m_pDebugger->action(QScriptEngineDebugger::InterruptAction)->trigger(); 328 | } 329 | #endif 330 | XSCRIPTVALUE _scriptValue = scriptEngine.globalObject().property(sDetectFunction); 331 | 332 | if (_handleError(&scriptEngine, _scriptValue, &signatureRecord, pScanResult)) { 333 | XSCRIPTVALUELIST valuelist; 334 | 335 | if (sDetectFunction == "detect") { 336 | valuelist << pScanOptions->bShowType << pScanOptions->bShowVersion << pScanOptions->bShowInfo; 337 | } 338 | 339 | #ifdef QT_SCRIPT_LIB 340 | QScriptValue result = _scriptValue.call(script, valuelist); 341 | #else 342 | QJSValue result = _scriptValue.callWithInstance(script, valuelist); 343 | #endif 344 | 345 | if (_handleError(&scriptEngine, result, &signatureRecord, pScanResult)) { 346 | // // TODO getResult 347 | // QString sResult = result.toString(); 348 | 349 | // QList listLocalResult = scriptEngine.getListLocalResult(); 350 | // qint32 nNumberOfDetects = listLocalResult.count(); 351 | 352 | // if ((nNumberOfDetects == 0) && (sResult != "")) { 353 | // listLocalResult.append(DiE_ScriptEngine::stringToResult(sResult, pOptions->bShowType, pOptions->bShowVersion, pOptions->bShowOptions)); 354 | // } 355 | 356 | // for (qint32 j = 0; j < nNumberOfDetects; j++) { 357 | // DiE_ScriptEngine::SCAN_STRUCT ssRecord = {}; 358 | 359 | // // TODO IDs 360 | // ssRecord.id = resultId; 361 | // ssRecord.parentId = parentId; 362 | 363 | // ssRecord.sSignature = signatureRecord.sName; 364 | // ssRecord.sSignatureFileName = signatureRecord.sFilePath; 365 | // ssRecord.sType = listLocalResult.at(j).sType; 366 | // ssRecord.sName = listLocalResult.at(j).sName; 367 | // ssRecord.sVersion = listLocalResult.at(j).sVersion; 368 | // ssRecord.sOptions = listLocalResult.at(j).sOptions; 369 | // ssRecord.sFullString = QString("%1: %2(%3)[%4]").arg(ssRecord.sType, ssRecord.sName, ssRecord.sVersion, ssRecord.sOptions); 370 | // ssRecord.sResult = QString("%1(%2)[%3]").arg(ssRecord.sName, ssRecord.sVersion, ssRecord.sOptions); 371 | 372 | // listRecords.append(ssRecord); 373 | // } 374 | } 375 | } 376 | } 377 | 378 | if (pElapsedTimer) { 379 | qint64 nElapsedTime = pElapsedTimer->elapsed(); 380 | delete pElapsedTimer; 381 | 382 | if (pScanOptions->bShowScanTime) { 383 | XScanEngine::DEBUG_RECORD debugRecord = {}; 384 | debugRecord.sScript = signatureRecord.sName; 385 | debugRecord.nElapsedTime = nElapsedTime; 386 | 387 | // #ifdef QT_DEBUG 388 | // qDebug("%s: %lld msec", debugRecord.sScript.toLatin1().data(), debugRecord.nElapsedTime); 389 | // #endif 390 | pScanResult->listDebugRecords.append(debugRecord); 391 | } 392 | 393 | if (pScanOptions->bLogProfiling) { 394 | emit warningMessage(QString("%1: [%2 ms]").arg(signatureRecord.sName, QString::number(nElapsedTime))); 395 | } 396 | } 397 | } 398 | 399 | XBinary::setPdStructCurrentIncrement(pPdStruct, _nFreeIndex); 400 | } 401 | 402 | if (bAddUnknown) { 403 | if (listRecords.count() == 0) { 404 | DiE_ScriptEngine::SCAN_STRUCT ssRecord = {}; 405 | 406 | ssRecord.id = resultId; 407 | ssRecord.parentId = parentId; 408 | ssRecord.sType = tr("Unknown"); 409 | ssRecord.sName = tr("Unknown"); 410 | ssRecord.bIsUnknown = true; 411 | 412 | listRecords.append(ssRecord); 413 | } 414 | } 415 | 416 | QList listScanStruct = convert(&listRecords); 417 | 418 | if (pScanOptions->bIsSort) { 419 | sortRecords(&listScanStruct); 420 | } 421 | 422 | pScanResult->listRecords.append(listScanStruct); 423 | 424 | XBinary::setPdStructFinished(pPdStruct, _nFreeIndex); 425 | 426 | if (pScanID) { 427 | *pScanID = resultId; 428 | } 429 | } 430 | 431 | bool DiE_Script::_handleError(DiE_ScriptEngine *pScriptEngine, XSCRIPTVALUE scriptValue, DiE_ScriptEngine::SIGNATURE_RECORD *pSignatureRecord, 432 | XScanEngine::SCAN_RESULT *pScanResult) 433 | { 434 | bool bResult = false; 435 | 436 | QString sErrorString; 437 | QString sPrefix = QString("%1/%2").arg(XBinary::fileTypeIdToString(pSignatureRecord->fileType), pSignatureRecord->sName); 438 | if (pScriptEngine->handleError(sPrefix, scriptValue, &sErrorString)) { 439 | bResult = true; 440 | } else { 441 | XScanEngine::ERROR_RECORD errorRecord = {}; 442 | errorRecord.sScript = pSignatureRecord->sName; 443 | errorRecord.sErrorString = sErrorString; 444 | 445 | pScanResult->listErrors.append(errorRecord); 446 | } 447 | 448 | return bResult; 449 | } 450 | 451 | void DiE_Script::_processDetect(SCANID *pScanID, SCAN_RESULT *pScanResult, QIODevice *pDevice, const SCANID &parentId, XBinary::FT fileType, SCAN_OPTIONS *pOptions, 452 | bool bAddUnknown, XBinary::PDSTRUCT *pPdStruct) 453 | { 454 | processDetect(pScanID, pScanResult, pDevice, parentId, fileType, pOptions, "", bAddUnknown, pPdStruct); 455 | } 456 | 457 | void DiE_Script::initDatabase() 458 | { 459 | m_listSignatures.clear(); 460 | } 461 | 462 | bool DiE_Script::loadDatabase(const QString &sDatabasePath, DiE_ScriptEngine::DT databaseType, XBinary::PDSTRUCT *pPdStruct) 463 | { 464 | bool bResult = false; 465 | 466 | QString _sDatabasePath = sDatabasePath; 467 | 468 | if (_sDatabasePath == "") { 469 | if (databaseType == DiE_ScriptEngine::DT_MAIN) { 470 | _sDatabasePath = "$app/db"; 471 | } else if (databaseType == DiE_ScriptEngine::DT_EXTRA) { 472 | _sDatabasePath = "$app/db_extra"; 473 | } else if (databaseType == DiE_ScriptEngine::DT_CUSTOM) { 474 | _sDatabasePath = "$app/db_custom"; 475 | } 476 | } 477 | 478 | if (_sDatabasePath != "") { 479 | #ifdef QT_DEBUG 480 | QElapsedTimer *pElapsedTimer = new QElapsedTimer; 481 | pElapsedTimer->start(); 482 | #endif 483 | _sDatabasePath = XBinary::convertPathName(_sDatabasePath); 484 | 485 | if (XBinary::isFileExists(_sDatabasePath)) { 486 | // Load from zip 487 | // TODO load from tar(cache) 488 | QFile file; 489 | file.setFileName(_sDatabasePath); 490 | 491 | if (file.open(QIODevice::ReadOnly)) { 492 | XZip zip(&file); 493 | 494 | if (zip.isValid(pPdStruct)) { 495 | QList listRecords = zip.getRecords(-1, pPdStruct); // TODO Check 496 | 497 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "", XBinary::FT_UNKNOWN)); 498 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "Binary", XBinary::FT_BINARY)); 499 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "COM", XBinary::FT_COM)); 500 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "Archive", XBinary::FT_ARCHIVE)); 501 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "ZIP", XBinary::FT_ZIP)); 502 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "JAR", XBinary::FT_JAR)); 503 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "APK", XBinary::FT_APK)); 504 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "IPA", XBinary::FT_IPA)); 505 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "NPM", XBinary::FT_NPM)); 506 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "MACHOFAT", XBinary::FT_MACHOFAT)); 507 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "DEB", XBinary::FT_DEB)); 508 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "DEX", XBinary::FT_DEX)); 509 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "MSDOS", XBinary::FT_MSDOS)); 510 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "LE", XBinary::FT_LE)); 511 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "LX", XBinary::FT_LX)); // TODO Check 512 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "NE", XBinary::FT_NE)); 513 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "PE", XBinary::FT_PE)); 514 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "ELF", XBinary::FT_ELF)); 515 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "MACH", XBinary::FT_MACHO)); 516 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "DOS16M", XBinary::FT_DOS16M)); 517 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "DOS4G", XBinary::FT_DOS4G)); 518 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "Amiga", XBinary::FT_AMIGAHUNK)); 519 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "AtariST", XBinary::FT_ATARIST)); 520 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "JavaClass", XBinary::FT_JAVACLASS)); 521 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "PDF", XBinary::FT_PDF)); 522 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "CFBF", XBinary::FT_CFBF)); 523 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "Image", XBinary::FT_IMAGE)); 524 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "JPEG", XBinary::FT_JPEG)); 525 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "PNG", XBinary::FT_PNG)); 526 | m_listSignatures.append(_loadDatabaseFromArchive(&zip, &listRecords, databaseType, "RAR", XBinary::FT_RAR)); 527 | 528 | bResult = true; 529 | } 530 | 531 | file.close(); 532 | } 533 | } else if (XBinary::isDirectoryExists(_sDatabasePath)) { 534 | // Load from directory 535 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath, databaseType, XBinary::FT_UNKNOWN, pPdStruct)); 536 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "Binary", databaseType, XBinary::FT_BINARY, pPdStruct)); 537 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "COM", databaseType, XBinary::FT_COM, pPdStruct)); 538 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "Archive", databaseType, XBinary::FT_ARCHIVE, pPdStruct)); 539 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "ZIP", databaseType, XBinary::FT_ZIP, pPdStruct)); 540 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "JAR", databaseType, XBinary::FT_JAR, pPdStruct)); 541 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "APK", databaseType, XBinary::FT_APK, pPdStruct)); 542 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "IPA", databaseType, XBinary::FT_IPA, pPdStruct)); 543 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "NPM", databaseType, XBinary::FT_NPM, pPdStruct)); 544 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "MACHOFAT", databaseType, XBinary::FT_MACHOFAT, pPdStruct)); 545 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "DEB", databaseType, XBinary::FT_DEB, pPdStruct)); 546 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "DEX", databaseType, XBinary::FT_DEX, pPdStruct)); 547 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "MSDOS", databaseType, XBinary::FT_MSDOS, pPdStruct)); 548 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "LE", databaseType, XBinary::FT_LE, pPdStruct)); 549 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "LX", databaseType, XBinary::FT_LX, pPdStruct)); // TODO Check 550 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "NE", databaseType, XBinary::FT_NE, pPdStruct)); 551 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "PE", databaseType, XBinary::FT_PE, pPdStruct)); 552 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "ELF", databaseType, XBinary::FT_ELF, pPdStruct)); 553 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "MACH", databaseType, XBinary::FT_MACHO, pPdStruct)); 554 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "DOS16M", databaseType, XBinary::FT_DOS16M, pPdStruct)); 555 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "DOS4G", databaseType, XBinary::FT_DOS4G, pPdStruct)); 556 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "Amiga", databaseType, XBinary::FT_AMIGAHUNK, pPdStruct)); 557 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "AtariST", databaseType, XBinary::FT_ATARIST, pPdStruct)); 558 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "JavaClass", databaseType, XBinary::FT_JAVACLASS, pPdStruct)); 559 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "PDF", databaseType, XBinary::FT_PDF, pPdStruct)); 560 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "CFBF", databaseType, XBinary::FT_CFBF, pPdStruct)); 561 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "Image", databaseType, XBinary::FT_IMAGE, pPdStruct)); 562 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "JPEG", databaseType, XBinary::FT_JPEG, pPdStruct)); 563 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "PNG", databaseType, XBinary::FT_PNG, pPdStruct)); 564 | m_listSignatures.append(_loadDatabaseFromPath(_sDatabasePath + QDir::separator() + "RAR", databaseType, XBinary::FT_RAR, pPdStruct)); 565 | 566 | bResult = true; 567 | } else { 568 | if (databaseType == DiE_ScriptEngine::DT_MAIN) { 569 | QString sErrorString = QString("%1: %2").arg(tr("Cannot load database"), _sDatabasePath); 570 | 571 | emit errorMessage(sErrorString); 572 | } 573 | } 574 | 575 | std::sort(m_listSignatures.begin(), m_listSignatures.end(), sort_signature_prio); 576 | 577 | #ifdef QT_DEBUG 578 | qDebug("DiE_Script::loadDatabase: %lld ms", pElapsedTimer->elapsed()); 579 | #endif 580 | } 581 | 582 | return bResult; 583 | } 584 | 585 | QList DiE_Script::getSignatureStates() 586 | { 587 | QList listResult; 588 | 589 | QList listFT; 590 | 591 | listFT.append(XBinary::FT_BINARY); 592 | listFT.append(XBinary::FT_COM); 593 | listFT.append(XBinary::FT_MSDOS); 594 | listFT.append(XBinary::FT_NE); 595 | listFT.append(XBinary::FT_LE); 596 | listFT.append(XBinary::FT_LX); 597 | listFT.append(XBinary::FT_PE); 598 | listFT.append(XBinary::FT_ELF); 599 | listFT.append(XBinary::FT_MACHO); 600 | listFT.append(XBinary::FT_PDF); 601 | listFT.append(XBinary::FT_CFBF); 602 | listFT.append(XBinary::FT_IMAGE); 603 | listFT.append(XBinary::FT_JPEG); 604 | listFT.append(XBinary::FT_PNG); 605 | listFT.append(XBinary::FT_RAR); 606 | listFT.append(XBinary::FT_ARCHIVE); 607 | listFT.append(XBinary::FT_ZIP); 608 | listFT.append(XBinary::FT_JAR); 609 | listFT.append(XBinary::FT_APK); 610 | listFT.append(XBinary::FT_IPA); 611 | listFT.append(XBinary::FT_DEB); 612 | listFT.append(XBinary::FT_DEX); 613 | listFT.append(XBinary::FT_NPM); 614 | listFT.append(XBinary::FT_MACHOFAT); 615 | listFT.append(XBinary::FT_AMIGAHUNK); 616 | listFT.append(XBinary::FT_ATARIST); 617 | listFT.append(XBinary::FT_DOS16M); 618 | listFT.append(XBinary::FT_DOS4G); 619 | 620 | qint32 nNumberOfFileTypes = listFT.count(); 621 | 622 | for (qint32 i = 0; i < nNumberOfFileTypes; i++) { 623 | SIGNATURE_STATE state = {}; 624 | state.fileType = listFT.at(i); 625 | state.nNumberOfSignatures = getNumberOfSignatures(state.fileType); 626 | 627 | listResult.append(state); 628 | } 629 | 630 | return listResult; 631 | } 632 | 633 | qint32 DiE_Script::getNumberOfSignatures(XBinary::FT fileType) 634 | { 635 | qint32 nResult = 0; 636 | 637 | qint32 nNumberOfSignatures = m_listSignatures.count(); 638 | 639 | for (qint32 i = 0; (i < nNumberOfSignatures); i++) { 640 | if ((m_listSignatures.at(i).sName != "_init") && (XBinary::checkFileType(m_listSignatures.at(i).fileType, fileType))) { 641 | nResult++; 642 | } 643 | } 644 | 645 | return nResult; 646 | } 647 | 648 | QList *DiE_Script::getSignatures() 649 | { 650 | return &m_listSignatures; 651 | } 652 | 653 | DiE_ScriptEngine::SIGNATURE_RECORD DiE_Script::getSignatureByFilePath(const QString &sSignatureFilePath) 654 | { 655 | DiE_ScriptEngine::SIGNATURE_RECORD result = {}; 656 | 657 | qint32 nNumberOfSignatures = m_listSignatures.count(); 658 | 659 | for (qint32 i = 0; i < nNumberOfSignatures; i++) { 660 | if (m_listSignatures.at(i).sFilePath == sSignatureFilePath) { 661 | result = m_listSignatures.at(i); 662 | 663 | break; 664 | } 665 | } 666 | 667 | return result; 668 | } 669 | 670 | bool DiE_Script::updateSignature(const QString &sSignatureFilePath, const QString &sText) 671 | { 672 | bool bResult = false; 673 | 674 | qint32 nNumberOfSignatures = m_listSignatures.count(); 675 | 676 | for (qint32 i = 0; i < nNumberOfSignatures; i++) { 677 | if (m_listSignatures.at(i).sFilePath == sSignatureFilePath) { 678 | if (XBinary::writeToFile(sSignatureFilePath, QByteArray().append(sText.toUtf8()))) { 679 | m_listSignatures[i].sText = sText; 680 | bResult = true; 681 | } 682 | 683 | break; 684 | } 685 | } 686 | 687 | return bResult; 688 | } 689 | 690 | DiE_Script::STATS DiE_Script::getStats() 691 | { 692 | STATS result = {}; 693 | 694 | qint32 nNumberOfSignatures = m_listSignatures.count(); 695 | 696 | for (qint32 i = 0; i < nNumberOfSignatures; i++) { 697 | QString sText = m_listSignatures.at(i).sText; 698 | 699 | QString sType = XBinary::regExp("init\\(\"(.*?)\",", sText, 1); 700 | 701 | if (sType != "") { 702 | result.mapTypes.insert(sType, result.mapTypes.value(sType, 0) + 1); 703 | } 704 | } 705 | 706 | return result; 707 | } 708 | 709 | bool DiE_Script::isSignaturesPresent(XBinary::FT fileType) 710 | { 711 | bool bResult = false; 712 | 713 | qint32 nNumberOfSignatures = m_listSignatures.count(); 714 | 715 | for (qint32 i = 0; i < nNumberOfSignatures; i++) { 716 | if (m_listSignatures.at(i).fileType == fileType) { 717 | bResult = true; 718 | 719 | break; 720 | } 721 | } 722 | 723 | return bResult; 724 | } 725 | 726 | QString DiE_Script::getErrorsString(XScanEngine::SCAN_RESULT *pScanResult) 727 | { 728 | QString sResult; 729 | 730 | qint32 nNumberOfErrors = pScanResult->listErrors.count(); 731 | 732 | for (qint32 i = 0; i < nNumberOfErrors; i++) { 733 | sResult += QString("%1: %2\n").arg(pScanResult->listErrors.at(i).sScript, pScanResult->listErrors.at(i).sErrorString); 734 | } 735 | 736 | return sResult; 737 | } 738 | 739 | QList DiE_Script::getErrorsAndWarningsStringList(XScanEngine::SCAN_RESULT *pScanResult) 740 | { 741 | QList listResult; 742 | 743 | qint32 nNumberOfErrors = pScanResult->listErrors.count(); 744 | 745 | for (qint32 i = 0; i < nNumberOfErrors; i++) { 746 | listResult.append(QString("%1: %2").arg(pScanResult->listErrors.at(i).sScript, pScanResult->listErrors.at(i).sErrorString)); 747 | } 748 | 749 | return listResult; 750 | } 751 | 752 | QList DiE_Script::convert(QList *pListScanStructs) 753 | { 754 | QList listResult; 755 | 756 | qint32 nNumberOfRecords = pListScanStructs->count(); 757 | 758 | for (qint32 i = 0; i < nNumberOfRecords; i++) { 759 | XScanEngine::SCANSTRUCT record = {}; 760 | 761 | record.bIsHeuristic = isHeurType(pListScanStructs->at(i).sType); 762 | record.bIsAHeuristic = isAHeurType(pListScanStructs->at(i).sType); 763 | record.bIsUnknown = pListScanStructs->at(i).bIsUnknown; 764 | record.id = pListScanStructs->at(i).id; 765 | record.parentId = pListScanStructs->at(i).parentId; 766 | record.sType = pListScanStructs->at(i).sType; 767 | record.sName = pListScanStructs->at(i).sName; 768 | record.sVersion = pListScanStructs->at(i).sVersion; 769 | record.sInfo = pListScanStructs->at(i).sOptions; 770 | record.varInfo = pListScanStructs->at(i).sSignature; 771 | record.varInfo2 = pListScanStructs->at(i).sSignatureFileName; 772 | // record.sResult = pListScanStructs->at(i).sResult; 773 | 774 | record.globalColorRecord = typeToGlobalColorRecord(record.sType); 775 | record.nPrio = typeToPrio(record.sType); 776 | record.bIsProtection = isProtection(record.sType); 777 | record.sType = translateType(record.sType); 778 | 779 | listResult.append(record); 780 | } 781 | 782 | // sortRecords(&listResult); 783 | 784 | return listResult; 785 | } 786 | 787 | bool DiE_Script::loadDatabaseFromGlobalOptions(XOptions *pXOptions) 788 | { 789 | bool bResult = false; 790 | 791 | initDatabase(); 792 | bResult = loadDatabase(pXOptions->getValue(XOptions::ID_SCAN_DATABASE_MAIN_PATH).toString(), DiE_ScriptEngine::DT_MAIN); 793 | loadDatabase(pXOptions->getValue(XOptions::ID_SCAN_DATABASE_EXTRA_PATH).toString(), DiE_ScriptEngine::DT_EXTRA); 794 | loadDatabase(pXOptions->getValue(XOptions::ID_SCAN_DATABASE_CUSTOM_PATH).toString(), DiE_ScriptEngine::DT_CUSTOM); 795 | 796 | return bResult; 797 | } 798 | 799 | #ifdef QT_SCRIPTTOOLS_LIB 800 | void DiE_Script::setDebugger(QScriptEngineDebugger *pDebugger) 801 | { 802 | this->m_pDebugger = pDebugger; 803 | } 804 | #endif 805 | 806 | #ifdef QT_SCRIPTTOOLS_LIB 807 | void DiE_Script::removeDebugger() 808 | { 809 | this->m_pDebugger = nullptr; 810 | } 811 | #endif 812 | --------------------------------------------------------------------------------