From 12029c768c52af41f4c41fb811096c0686898dd3 Mon Sep 17 00:00:00 2001 From: Nikolaj Schlej Date: Tue, 5 Jul 2016 18:19:04 +0200 Subject: [PATCH] UModelIndex integrated - can be used instead of QModelIndex for non-Qt builds --- UEFIExtract/ffsdumper.h | 1 - UEFIExtract/uefiextract.pro | 1 - common/ffsparser.cpp | 15 ++--- common/ffsparser.h | 1 - common/ffsreport.h | 1 - common/fitparser.h | 1 - common/treemodel.cpp | 67 ++++++++++++++++------- common/treemodel.h | 106 +++++++++++++++++++++++++++++++++--- common/ubytearray.h | 2 - common/umodelindex.h | 26 --------- common/utility.h | 1 - 11 files changed, 153 insertions(+), 69 deletions(-) delete mode 100644 common/umodelindex.h diff --git a/UEFIExtract/ffsdumper.h b/UEFIExtract/ffsdumper.h index 23481e3..007e9f0 100644 --- a/UEFIExtract/ffsdumper.h +++ b/UEFIExtract/ffsdumper.h @@ -18,7 +18,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include "../common/ubytearray.h" #include "../common/ustring.h" -#include "../common/umodelindex.h" #include "../common/basetypes.h" #include "../common/treemodel.h" #include "../common/ffs.h" diff --git a/UEFIExtract/uefiextract.pro b/UEFIExtract/uefiextract.pro index 3deb59b..4cc6273 100644 --- a/UEFIExtract/uefiextract.pro +++ b/UEFIExtract/uefiextract.pro @@ -45,7 +45,6 @@ HEADERS += \ ../common/utility.h \ ../common/LZMA/LzmaDecompress.h \ ../common/Tiano/EfiTianoDecompress.h \ - ../common/umodelindex.h \ ../common/ubytearray.h \ ../common/ustring.h \ ../bstrlib/bstrlib.h \ diff --git a/common/ffsparser.cpp b/common/ffsparser.cpp index 35c936a..faaa127 100644 --- a/common/ffsparser.cpp +++ b/common/ffsparser.cpp @@ -12,6 +12,7 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "ffsparser.h" +#include #include #include #include @@ -1027,7 +1028,7 @@ USTATUS FfsParser::parseVolumeHeader(const UByteArray & volume, const UINT32 par UINT32 alignment = 65536; // Default volume alignment is 64K if (volumeHeader->Revision == 1) { // Acquire alignment capability bit - bool alignmentCap = volumeHeader->Attributes & EFI_FVB_ALIGNMENT_CAP; + BOOLEAN alignmentCap = volumeHeader->Attributes & EFI_FVB_ALIGNMENT_CAP; if (!alignmentCap) { if ((volumeHeader->Attributes & 0xFFFF0000)) msgAlignmentBitsSet = true; @@ -1230,7 +1231,7 @@ USTATUS FfsParser::getVolumeSize(const UByteArray & bios, UINT32 volumeOffset, U entry += 1; } - volumeSize = volumeHeader->FvLength; + volumeSize = (UINT32)volumeHeader->FvLength; bmVolumeSize = calcVolumeSize; if (volumeSize == 0) @@ -1604,7 +1605,7 @@ USTATUS FfsParser::parseFileHeader(const UByteArray & file, const UINT32 parentO } // Construct parsing data - bool fixed = fileHeader->Attributes & FFS_ATTRIB_FIXED; + bool fixed(fileHeader->Attributes & FFS_ATTRIB_FIXED); pdata.offset += parentOffset; @@ -2863,8 +2864,8 @@ USTATUS FfsParser::parseTeImageSectionBody(const UModelIndex & index) // Get data from parsing data PARSING_DATA pdata = parsingDataFromUModelIndex(index); - pdata.section.teImage.imageBase = teHeader->ImageBase; - pdata.section.teImage.adjustedImageBase = teHeader->ImageBase + teHeader->StrippedSize - sizeof(EFI_IMAGE_TE_HEADER); + pdata.section.teImage.imageBase = (UINT32)teHeader->ImageBase; + pdata.section.teImage.adjustedImageBase = (UINT32)(teHeader->ImageBase + teHeader->StrippedSize - sizeof(EFI_IMAGE_TE_HEADER)); // Update parsing data model->setParsingData(index, parsingDataToUByteArray(pdata)); @@ -3647,7 +3648,7 @@ USTATUS FfsParser::getStoreSize(const UByteArray & data, const UINT32 storeOffse } else { // Header with 64 bit WriteQueueSize const EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64* ftw64Header = (const EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64*)signature; - storeSize = sizeof(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64) + ftw64Header->WriteQueueSize; + storeSize = sizeof(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64) + (UINT32)ftw64Header->WriteQueueSize; } } else if (*signature == NVRAM_PHOENIX_FLASH_MAP_SIGNATURE_PART1) { // Phoenix SCT flash map @@ -3743,7 +3744,7 @@ USTATUS FfsParser::parseFtwStoreHeader(const UByteArray & store, const UINT32 pa has32bitHeader = true; } else { // Header with 64 bit WriteQueueSize - ftwBlockSize = sizeof(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64) + ftw64BlockHeader->WriteQueueSize; + ftwBlockSize = sizeof(EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER64) + (UINT32)ftw64BlockHeader->WriteQueueSize; has32bitHeader = false; } if (dataSize < ftwBlockSize) { diff --git a/common/ffsparser.h b/common/ffsparser.h index 5e4be11..e809e2b 100644 --- a/common/ffsparser.h +++ b/common/ffsparser.h @@ -17,7 +17,6 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "ustring.h" #include "ubytearray.h" -#include "umodelindex.h" #include "basetypes.h" #include "treemodel.h" #include "utility.h" diff --git a/common/ffsreport.h b/common/ffsreport.h index 5d671c6..c7415f9 100644 --- a/common/ffsreport.h +++ b/common/ffsreport.h @@ -18,7 +18,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "../common/ubytearray.h" #include "../common/ustring.h" -#include "../common/umodelindex.h" #include "basetypes.h" #include "treemodel.h" #include "ffs.h" diff --git a/common/fitparser.h b/common/fitparser.h index 4ebf663..3b4dac8 100644 --- a/common/fitparser.h +++ b/common/fitparser.h @@ -17,7 +17,6 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "ustring.h" #include "ubytearray.h" -#include "umodelindex.h" #include "treemodel.h" #include "utility.h" #include "parsingdata.h" diff --git a/common/treemodel.cpp b/common/treemodel.cpp index 486d8cc..f7969ab 100644 --- a/common/treemodel.cpp +++ b/common/treemodel.cpp @@ -14,25 +14,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "treeitem.h" #include "treemodel.h" -TreeModel::TreeModel(QObject *parent) - : QAbstractItemModel(parent) -{ - rootItem = new TreeItem(Types::Root, 0, UString(), UString(), UString(), UByteArray(), UByteArray(), UByteArray(), TRUE, FALSE, UByteArray()); -} - -TreeModel::~TreeModel() -{ - delete rootItem; -} - -int TreeModel::columnCount(const UModelIndex &parent) const -{ - if (parent.isValid()) - return static_cast(parent.internalPointer())->columnCount(); - else - return rootItem->columnCount(); -} - +#if defined(QT_CORE_LIB) && defined(U_USE_QITEMMODEL) QVariant TreeModel::data(const UModelIndex &index, int role) const { if (!index.isValid()) @@ -78,6 +60,53 @@ QVariant TreeModel::headerData(int section, Qt::Orientation orientation, return QVariant(); } +#else +UString TreeModel::data(const UModelIndex &index, int role) const +{ + if (!index.isValid()) + return UString(); + + if (role != 0 && role != 0x0100) + return UString(); + + TreeItem *item = static_cast(index.internalPointer()); + + if (role == 0) + return item->data(index.column()); + else + return item->info(); +} + +UString TreeModel::headerData(int section, int orientation, + int role) const +{ + if (orientation == 1 && role == 0) { + switch (section) + { + case 0: + return UString("Name"); + case 1: + return UString("Action"); + case 2: + return UString("Type"); + case 3: + return UString("Subtype"); + case 4: + return UString("Text"); + } + } + + return UString(); +} +#endif + +int TreeModel::columnCount(const UModelIndex &parent) const +{ + if (parent.isValid()) + return static_cast(parent.internalPointer())->columnCount(); + else + return rootItem->columnCount(); +} UModelIndex TreeModel::index(int row, int column, const UModelIndex &parent) const { diff --git a/common/treemodel.h b/common/treemodel.h index 58ed9d2..747be23 100644 --- a/common/treemodel.h +++ b/common/treemodel.h @@ -14,7 +14,10 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef TREEMODEL_H #define TREEMODEL_H +#if defined(QT_CORE_LIB) && defined(U_USE_QITEMMODEL) +// Use Qt classes #include +#include #include #include @@ -22,22 +25,103 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "ubytearray.h" #include "basetypes.h" #include "types.h" -#include "umodelindex.h" +#include "treeitem.h" -class TreeItem; +#define UModelIndex QModelIndex +#else +// Use own implementation +#include "ustring.h" +#include "ubytearray.h" +#include "basetypes.h" +#include "types.h" +#include "treeitem.h" +class UModelIndex +{ + friend class TreeModel; + +public: + inline UModelIndex() : r(-1), c(-1), i(0), m(0) {} + // compiler-generated copy/move ctors/assignment operators are fine! + inline int row() const { return r; } + inline int column() const { return c; } + inline uint64_t internalId() const { return i; } + inline void *internalPointer() const { return reinterpret_cast(i); } + inline UModelIndex parent() const; + inline UModelIndex child(int row, int column) const; + inline CBString data(int role) const; + inline const TreeModel *model() const { return m; } + inline bool isValid() const { return (r >= 0) && (c >= 0) && (m != 0); } + inline bool operator==(const UModelIndex &other) const { return (other.r == r) && (other.i == i) && (other.c == c) && (other.m == m); } + inline bool operator!=(const UModelIndex &other) const { return !(*this == other); } + inline bool operator<(const UModelIndex &other) const + { + return r < other.r + || (r == other.r && (c < other.c + || (c == other.c && (i < other.i + || (i == other.i && m < other.m))))); + } + +private: + inline UModelIndex(int arow, int acolumn, void *ptr, const TreeModel *amodel) + : r(arow), c(acolumn), i(reinterpret_cast(ptr)), m(amodel) {} + inline UModelIndex(int arow, int acolumn, uint64_t id, const TreeModel *amodel) + : r(arow), c(acolumn), i(id), m(amodel) {} + int r, c; + uint64_t i; + const TreeModel *m; +}; +#endif + +#if defined(QT_CORE_LIB) && defined(U_USE_QITEMMODEL) class TreeModel : public QAbstractItemModel { Q_OBJECT +private: + TreeItem *rootItem; public: - TreeModel(QObject *parent = 0); - ~TreeModel(); - QVariant data(const UModelIndex &index, int role) const; Qt::ItemFlags flags(const UModelIndex &index) const; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + TreeModel(QObject *parent = 0) : QAbstractItemModel(parent) { + rootItem = new TreeItem(Types::Root, 0, UString(), UString(), UString(), UByteArray(), UByteArray(), UByteArray(), TRUE, FALSE, UByteArray()); + } + +#else +#define emit + +class TreeModel +{ +private: + TreeItem *rootItem; + void dataChanged(const UModelIndex &, const UModelIndex &) {} + void layoutAboutToBeChanged() {} + void layoutChanged() {} + +public: + UString data(const UModelIndex &index, int role) const; + UString headerData(int section, int orientation, int role = 0) const; + + TreeModel() { + rootItem = new TreeItem(Types::Root, 0, UString(), UString(), UString(), UByteArray(), UByteArray(), UByteArray(), TRUE, FALSE, UByteArray()); + } + + bool hasIndex(int row, int column, const UModelIndex &parent = UModelIndex()) const { + if (row < 0 || column < 0) + return false; + return row < rowCount(parent) && column < columnCount(parent); + } + + UModelIndex createIndex(int row, int column, void *data) const { return UModelIndex(row, column, data, this); } +#endif + + + ~TreeModel() { + delete rootItem; + } + UModelIndex index(int row, int column, const UModelIndex &parent = UModelIndex()) const; UModelIndex parent(const UModelIndex &index) const; @@ -70,7 +154,6 @@ public: bool hasEmptyParsingData(const UModelIndex &index) const; UINT8 action(const UModelIndex &index) const; bool fixed(const UModelIndex &index) const; - bool compressed(const UModelIndex &index) const; UModelIndex addItem(const UINT8 type, const UINT8 subtype, @@ -80,9 +163,14 @@ public: const UModelIndex & parent = UModelIndex(), const UINT8 mode = CREATE_MODE_APPEND); UModelIndex findParentOfType(const UModelIndex & index, UINT8 type) const; - -private: - TreeItem *rootItem; }; +#if defined(QT_CORE_LIB) && defined(U_USE_QITEMMODEL) +// Nothing required here +#else +inline UModelIndex UModelIndex::parent() const { return m ? m->parent(*this) : UModelIndex(); } +inline UModelIndex UModelIndex::child(int row, int column) const { return m ? m->index(row, column, *this) : UModelIndex(); } +inline UString UModelIndex::data(int role) const { return m ? m->data(*this, role) : UString(); } +#endif + #endif // TREEMODEL_H diff --git a/common/ubytearray.h b/common/ubytearray.h index 03cd42f..9c1631f 100644 --- a/common/ubytearray.h +++ b/common/ubytearray.h @@ -13,8 +13,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #ifndef UBYTEARRAY_H #define UBYTEARRAY_H -//#define U_USE_QBYTEARRAY - #if defined(QT_CORE_LIB) && defined(U_USE_QBYTEARRAY) // Use Qt class, if Qt is available #include diff --git a/common/umodelindex.h b/common/umodelindex.h deleted file mode 100644 index 4f4c705..0000000 --- a/common/umodelindex.h +++ /dev/null @@ -1,26 +0,0 @@ -/* umodelindex.h - -Copyright (c) 2016, Nikolaj Schlej. All rights reserved. -This program and the accompanying materials -are licensed and made available under the terms and conditions of the BSD License -which accompanies this distribution. The full text of the license may be found at -http://opensource.org/licenses/bsd-license.php - -THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -*/ - -#ifndef UMODELINDEX_H -#define UMODELINDEX_H - -#if defined(QT_CORE_LIB) -// Use Qt class, if Qt is available -#include -#define UModelIndex QModelIndex -#else -// Use own implementation - - -#endif // QT_CORE_LIB -#endif // UMODELINDEX_H - diff --git a/common/utility.h b/common/utility.h index 7378924..8e02035 100644 --- a/common/utility.h +++ b/common/utility.h @@ -15,7 +15,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #define UTILITY_H #include "ustring.h" -#include "umodelindex.h" #include "basetypes.h" #include "parsingdata.h"