UByteArray integrated

- another Qt class can be replaced for non-Qt builds
This commit is contained in:
Nikolaj Schlej 2016-07-05 17:22:03 +02:00
parent 804a55ba64
commit 71ba5fe582
10 changed files with 56 additions and 28 deletions

View File

@ -56,7 +56,8 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path
if (!file.open(QFile::WriteOnly))
return U_FILE_OPEN;
file.write(model->header(index));
QByteArray ba(model->header(index).constData(), model->header(index).size());
file.write(ba);
file.close();
}
@ -65,7 +66,8 @@ USTATUS FfsDumper::recursiveDump(const UModelIndex & index, const UString & path
if (!file.open(QFile::WriteOnly))
return U_FILE_OPEN;
file.write(model->body(index));
QByteArray ba(model->body(index).constData(), model->body(index).size());
file.write(ba);
file.close();
}
}

View File

@ -48,9 +48,11 @@ int main(int argc, char *argv[])
return U_FILE_OPEN;
// Read and close the file
UByteArray buffer = inputFile.readAll();
QByteArray b = inputFile.readAll();
inputFile.close();
UByteArray buffer(b.constData(), b.size());
// Create model and ffsParser
TreeModel model;
FfsParser ffsParser(&model);

View File

@ -365,9 +365,9 @@ struct CBString : public tagbstring {
CBString toLocal8Bit() { return *this; }
bool isEmpty() const { return slen == 0; }
void clear() { *this = ""; }
CBString left(int len) { return midstr(0, len); }
CBString mid(int pos, int len) { return midstr(pos, len); }
static CBString fromUtf16(const ushort* str) { // Naive implementation assuming that only ASCII part of UCS2 is used
CBString left(int len) const { return midstr(0, len); }
CBString mid(int pos, int len) const { return midstr(pos, len); }
static CBString fromUtf16(const unsigned short* str) { // Naive implementation assuming that only ASCII part of UCS2 is used
CBString msg; while (*str) { msg += *(char*)str; str++; } return msg;
}
CBString leftJustified(int length) { if (length > slen) { return *this + CBString(' ', length - slen); } return *this; }

View File

@ -112,7 +112,7 @@ USTATUS FfsParser::performFirstPass(const UByteArray & buffer, UModelIndex & ind
capsuleHeaderSize = capsuleHeader->HeaderSize;
UByteArray header = buffer.left(capsuleHeaderSize);
UByteArray body = buffer.right(buffer.size() - capsuleHeaderSize);
UByteArray body = buffer.mid(capsuleHeaderSize);
UString name("Toshiba capsule");
UString info = UString("Capsule GUID: ") + guidToUString(capsuleHeader->CapsuleGuid) +
usprintf("\nFull size: %Xh (%u)\nHeader size: %Xh (%u)\nImage size: %Xh (%u)\nFlags: %08Xh",
@ -669,7 +669,7 @@ USTATUS FfsParser::parseMeRegion(const UByteArray & me, const UINT32 parentOffse
bool versionFound = true;
bool emptyRegion = false;
// Check for empty region
if (me.count() == me.count('\xFF') || me.count() == me.count('\x00')) {
if (me.size() == me.count('\xFF') || me.size() == me.count('\x00')) {
// Further parsing not needed
emptyRegion = true;
info += ("\nState: empty");
@ -791,9 +791,9 @@ USTATUS FfsParser::parseBiosRegion(const UByteArray & bios, const UINT32 parentO
UINT8 FfsParser::getPaddingType(const UByteArray & padding)
{
if (padding.count('\x00') == padding.count())
if (padding.count('\x00') == padding.size())
return Subtypes::ZeroPadding;
if (padding.count('\xFF') == padding.count())
if (padding.count('\xFF') == padding.size())
return Subtypes::OnePadding;
return Subtypes::DataPadding;
}
@ -1339,7 +1339,7 @@ USTATUS FfsParser::parseVolumeBody(const UModelIndex & index)
if (header.count(pdata.emptyByte) == header.size()) { //Empty space
// Check free space to be actually free
UByteArray freeSpace = volumeBody.mid(fileOffset);
if (freeSpace.count(pdata.emptyByte) != freeSpace.count()) {
if (freeSpace.count(pdata.emptyByte) != freeSpace.size()) {
// Search for the first non-empty byte
UINT32 i;
UINT32 size = freeSpace.size();
@ -2481,7 +2481,7 @@ USTATUS FfsParser::parseGuidedSectionBody(const UModelIndex & index)
}
info += UString("\nCompression algorithm: ") + compressionTypeToUString(algorithm);
info += usprintf("\nDecompressed size: %Xh (%u)", processed.length(), processed.length());
info += usprintf("\nDecompressed size: %Xh (%u)", processed.size(), processed.size());
}
// LZMA compressed section
else if (UByteArray((const char*)&guid, sizeof(EFI_GUID)) == EFI_GUIDED_SECTION_LZMA) {
@ -2495,7 +2495,7 @@ USTATUS FfsParser::parseGuidedSectionBody(const UModelIndex & index)
if (algorithm == COMPRESSION_ALGORITHM_LZMA) {
info += UString("\nCompression algorithm: LZMA");
info += usprintf("\nDecompressed size: %Xh (%u)", processed.length(), processed.length());
info += usprintf("\nDecompressed size: %Xh (%u)", processed.size(), processed.size());
}
else {
info += UString("\nCompression algorithm: unknown");
@ -3276,7 +3276,7 @@ parsing_done:
// Authentication data
if (hasTimestampAndHash) {
info += usprintf("\nTimestamp: %"PRIX64"h\nHash: ",
timestamp) + UString(hash.toHex().toUpper());
timestamp) + UString(hash.toHex().constData());
}
}
@ -3890,7 +3890,7 @@ USTATUS FfsParser::parseFsysStoreHeader(const UByteArray & store, const UINT32 p
UByteArray body = store.mid(sizeof(APPLE_FSYS_STORE_HEADER), fsysStoreHeader->Size - sizeof(APPLE_FSYS_STORE_HEADER) - sizeof(UINT32));
// Check store checksum
UINT32 storedCrc = *(UINT32*)store.right(sizeof(UINT32)).constBegin();
UINT32 storedCrc = *(UINT32*)store.right(sizeof(UINT32)).constData();
UINT32 calculatedCrc = crc32(0, (const UINT8*)store.constData(), (const UINT32)store.size() - sizeof(UINT32));
// Add info
@ -4488,7 +4488,7 @@ USTATUS FfsParser::parseFsysStoreBody(const UModelIndex & index)
pdata.offset = parentOffset + offset;
// Add EOF tree item
model->addItem(Types::FsysEntry, 0, UString(name), UString(), info, header, UByteArray(), UByteArray(), false, parsingDataToUByteArray(pdata), index);
model->addItem(Types::FsysEntry, 0, UString("EOF"), UString(), info, header, UByteArray(), UByteArray(), false, parsingDataToUByteArray(pdata), index);
// Add free space
offset += header.size();
@ -4542,7 +4542,7 @@ USTATUS FfsParser::parseFsysStoreBody(const UModelIndex & index)
pdata.offset = parentOffset + offset;
// Add tree item
model->addItem(Types::FsysEntry, 0, UString(name), UString(), info, header, body, UByteArray(), false, parsingDataToUByteArray(pdata), index);
model->addItem(Types::FsysEntry, 0, UString(name.constData(), name.size()), UString(), info, header, body, UByteArray(), false, parsingDataToUByteArray(pdata), index);
// Move to next variable
offset += variableSize;

View File

@ -13,8 +13,8 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#ifndef FIT_H
#define FIT_H
#include <QByteArray>
#include "basetypes.h"
#include "ubytearray.h"
// Make sure we use right packing rules
#pragma pack(push, 1)
@ -37,7 +37,7 @@ WITHWARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#define FIT_HEADER_VERSION 0x0100
#define FIT_MICROCODE_VERSION 0x0100
const QByteArray FIT_SIGNATURE
const UByteArray FIT_SIGNATURE
("\x5F\x46\x49\x54\x5F\x20\x20\x20", 8);
typedef struct FIT_ENTRY_ {

View File

@ -17,7 +17,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
TreeItem::TreeItem(const UINT8 type, const UINT8 subtype,
const UString & name, const UString & text, const UString & info,
const UByteArray & header, const UByteArray & body, const UByteArray & tail,
const BOOLEAN fixed, const BOOLEAN compressed, const UByteArray & parsingData,
const bool fixed, const bool compressed, const UByteArray & parsingData,
TreeItem *parent) :
itemAction(Actions::NoAction),
itemType(type),

View File

@ -25,7 +25,7 @@ class TreeItem
public:
TreeItem(const UINT8 type, const UINT8 subtype, const UString &name, const UString &text, const UString &info,
const UByteArray & header, const UByteArray & body, const UByteArray & tail,
const BOOLEAN fixed, const BOOLEAN compressed, const UByteArray & parsingData,
const bool fixed, const bool compressed, const UByteArray & parsingData,
TreeItem *parent = 0);
~TreeItem(); // Non-trivial implementation in CPP file
@ -70,16 +70,16 @@ public:
void setParsingData(const UByteArray & data) { itemParsingData = data; }
UString info() const { return itemInfo; }
void addInfo(const UString &info, const BOOLEAN append) { if (append) itemInfo += info; else itemInfo = info + itemInfo; }
void addInfo(const UString &info, const bool append) { if (append) itemInfo += info; else itemInfo = info + itemInfo; }
void setInfo(const UString &info) { itemInfo = info; }
UINT8 action() const {return itemAction; }
void setAction(const UINT8 action) { itemAction = action; }
BOOLEAN fixed() const { return itemFixed; }
bool fixed() const { return itemFixed; }
void setFixed(const bool fixed) { itemFixed = fixed; }
BOOLEAN compressed() const { return itemCompressed; }
bool compressed() const { return itemCompressed; }
void setCompressed(const bool compressed) { itemCompressed = compressed; }
private:

View File

@ -349,7 +349,7 @@ void TreeModel::addInfo(const UModelIndex &index, const UString &data, const boo
return;
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
item->addInfo(data, (BOOLEAN)append);
item->addInfo(data, append);
emit dataChanged(index, index);
}

View File

@ -13,7 +13,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#ifndef UBYTEARRAY_H
#define UBYTEARRAY_H
#ifdef QT_CORE_LIB
//#define U_USE_QBYTEARRAY
#if defined(QT_CORE_LIB) && defined(U_USE_QBYTEARRAY)
// Use Qt class, if Qt is available
#include <QByteArray>
#define UByteArray QByteArray
@ -54,14 +56,33 @@ public:
UByteArray right(int32_t len) const { return d.substr(d.size() - 1 - len, len); };
UByteArray mid(int32_t pos, int32_t len = -1) const { return d.substr(pos, len); };
UByteArray &operator=(const UByteArray & ba) { d = ba.d; return *this; }
UByteArray & operator=(const UByteArray & ba) { d = ba.d; return *this; }
UByteArray & operator+=(const UByteArray & ba) { d += ba.d; return *this; }
bool operator== (const UByteArray & ba) const { return d == ba.d; }
bool operator!= (const UByteArray & ba) const { return d != ba.d; }
inline void swap(UByteArray &other) { std::swap(d, other.d); }
UByteArray toHex() {
std::basic_string<char> hex(size() * 2, '\x00');
for (int32_t i = 0; i < size(); ++i) {
uint8_t low = d[i] & 0x0F;
uint8_t high = (d[i] & 0xF0) >> 4;
low += (low < 10 ? 'a' : '0');
high += (high < 10 ? 'a' : '0');
hex[i] = low;
hex[i + 1] = high;
}
std::reverse(hex.begin(), hex.end());
return UByteArray(hex);
}
private:
std::basic_string<char> d;
};
inline const UByteArray operator+(const UByteArray &a1, const UByteArray &a2)
{
return UByteArray(a1) += a2;
}
#endif // QT_CORE_LIB
#endif // UBYTEARRAY_H

View File

@ -13,11 +13,14 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#ifndef UMODELINDEX_H
#define UMODELINDEX_H
#ifdef QT_CORE_LIB
#if defined(QT_CORE_LIB)
// Use Qt class, if Qt is available
#include <QModelIndex>
#define UModelIndex QModelIndex
#else
// Use own implementation
#endif // QT_CORE_LIB
#endif // UMODELINDEX_H