Refactor Endian namespace

This commit is contained in:
Janek Bevendorff 2017-12-16 17:32:39 +01:00 committed by Jonathan White
parent 3461cbfb06
commit d1a19a1009
No known key found for this signature in database
GPG Key ID: 440FC65F2E0C6E01
9 changed files with 99 additions and 320 deletions

View File

@ -42,7 +42,6 @@ set(keepassx_SOURCES
core/CsvParser.cpp
core/Database.cpp
core/DatabaseIcons.cpp
core/Endian.cpp
core/Entry.cpp
core/EntryAttachments.cpp
core/EntryAttributes.cpp

View File

@ -1,244 +0,0 @@
/*
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Endian.h"
#include <QtEndian>
#include <QIODevice>
namespace Endian {
qint16 bytesToInt16(const QByteArray& ba, QSysInfo::Endian byteOrder)
{
Q_ASSERT(ba.size() == 2);
if (byteOrder == QSysInfo::LittleEndian) {
return qFromLittleEndian<qint16>(reinterpret_cast<const uchar*>(ba.constData()));
}
else {
return qFromBigEndian<qint16>(reinterpret_cast<const uchar*>(ba.constData()));
}
}
qint32 bytesToInt32(const QByteArray& ba, QSysInfo::Endian byteOrder)
{
Q_ASSERT(ba.size() == 4);
if (byteOrder == QSysInfo::LittleEndian) {
return qFromLittleEndian<qint32>(reinterpret_cast<const uchar*>(ba.constData()));
}
else {
return qFromBigEndian<qint32>(reinterpret_cast<const uchar*>(ba.constData()));
}
}
qint64 bytesToInt64(const QByteArray& ba, QSysInfo::Endian byteOrder)
{
Q_ASSERT(ba.size() == 8);
if (byteOrder == QSysInfo::LittleEndian) {
return qFromLittleEndian<qint64>(reinterpret_cast<const uchar*>(ba.constData()));
}
else {
return qFromBigEndian<qint64>(reinterpret_cast<const uchar*>(ba.constData()));
}
}
quint16 bytesToUInt16(const QByteArray& ba, QSysInfo::Endian byteOrder)
{
return static_cast<quint16>(bytesToInt16(ba, byteOrder));
}
quint32 bytesToUInt32(const QByteArray& ba, QSysInfo::Endian byteOrder)
{
return static_cast<quint32>(bytesToInt32(ba, byteOrder));
}
quint64 bytesToUInt64(const QByteArray& ba, QSysInfo::Endian byteOrder)
{
return static_cast<quint64>(bytesToInt64(ba, byteOrder));
}
qint16 readInt16(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok)
{
QByteArray ba = device->read(2);
if (ba.size() != 2) {
*ok = false;
return 0;
}
else {
*ok = true;
return bytesToInt16(ba, byteOrder);
}
}
qint32 readInt32(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok)
{
QByteArray ba = device->read(4);
if (ba.size() != 4) {
*ok = false;
return 0;
}
else {
*ok = true;
return bytesToInt32(ba, byteOrder);
}
}
qint64 readInt64(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok)
{
QByteArray ba = device->read(8);
if (ba.size() != 8) {
*ok = false;
return 0;
}
else {
*ok = true;
return bytesToInt64(ba, byteOrder);
}
}
quint16 readUInt16(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok)
{
return static_cast<quint16>(readInt16(device, byteOrder, ok));
}
quint32 readUInt32(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok)
{
return static_cast<quint32>(readInt32(device, byteOrder, ok));
}
quint64 readUInt64(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok)
{
return static_cast<quint64>(readInt64(device, byteOrder, ok));
}
QByteArray int16ToBytes(qint16 num, QSysInfo::Endian byteOrder)
{
QByteArray ba;
ba.resize(2);
if (byteOrder == QSysInfo::LittleEndian) {
qToLittleEndian<qint16>(num, reinterpret_cast<uchar*>(ba.data()));
}
else {
qToBigEndian<qint16>(num, reinterpret_cast<uchar*>(ba.data()));
}
return ba;
}
QByteArray int32ToBytes(qint32 num, QSysInfo::Endian byteOrder)
{
QByteArray ba;
ba.resize(4);
if (byteOrder == QSysInfo::LittleEndian) {
qToLittleEndian<qint32>(num, reinterpret_cast<uchar*>(ba.data()));
}
else {
qToBigEndian<qint32>(num, reinterpret_cast<uchar*>(ba.data()));
}
return ba;
}
QByteArray int64ToBytes(qint64 num, QSysInfo::Endian byteOrder)
{
QByteArray ba;
ba.resize(8);
if (byteOrder == QSysInfo::LittleEndian) {
qToLittleEndian<qint64>(num, reinterpret_cast<uchar*>(ba.data()));
}
else {
qToBigEndian<qint64>(num, reinterpret_cast<uchar*>(ba.data()));
}
return ba;
}
QByteArray uint16ToBytes(quint16 num, QSysInfo::Endian byteOrder)
{
QByteArray ba;
ba.resize(2);
if (byteOrder == QSysInfo::LittleEndian) {
qToLittleEndian<quint16>(num, reinterpret_cast<uchar*>(ba.data()));
}
else {
qToBigEndian<quint16>(num, reinterpret_cast<uchar*>(ba.data()));
}
return ba;
}
QByteArray uint32ToBytes(quint32 num, QSysInfo::Endian byteOrder)
{
QByteArray ba;
ba.resize(4);
if (byteOrder == QSysInfo::LittleEndian) {
qToLittleEndian<quint32>(num, reinterpret_cast<uchar*>(ba.data()));
}
else {
qToBigEndian<quint32>(num, reinterpret_cast<uchar*>(ba.data()));
}
return ba;
}
QByteArray uint64ToBytes(quint64 num, QSysInfo::Endian byteOrder)
{
QByteArray ba;
ba.resize(8);
if (byteOrder == QSysInfo::LittleEndian) {
qToLittleEndian<quint64>(num, reinterpret_cast<uchar*>(ba.data()));
}
else {
qToBigEndian<quint64>(num, reinterpret_cast<uchar*>(ba.data()));
}
return ba;
}
bool writeInt16(qint16 num, QIODevice* device, QSysInfo::Endian byteOrder)
{
QByteArray ba = int16ToBytes(num, byteOrder);
int bytesWritten = device->write(ba);
return (bytesWritten == ba.size());
}
bool writeInt32(qint32 num, QIODevice* device, QSysInfo::Endian byteOrder)
{
QByteArray ba = int32ToBytes(num, byteOrder);
int bytesWritten = device->write(ba);
return (bytesWritten == ba.size());
}
bool writeInt64(qint64 num, QIODevice* device, QSysInfo::Endian byteOrder)
{
QByteArray ba = int64ToBytes(num, byteOrder);
int bytesWritten = device->write(ba);
return (bytesWritten == ba.size());
}
} // namespace Endian

View File

@ -1,4 +1,5 @@
/*
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
@ -20,35 +21,58 @@
#include <QByteArray>
#include <QSysInfo>
#include <QtEndian>
#include <QIODevice>
class QIODevice;
namespace Endian
{
namespace Endian {
template<typename SizedQInt>
SizedQInt bytesToSizedInt(const QByteArray& ba, QSysInfo::Endian byteOrder)
{
Q_ASSERT(ba.size() == sizeof(SizedQInt));
qint16 bytesToInt16(const QByteArray& ba, QSysInfo::Endian byteOrder);
quint16 bytesToUInt16(const QByteArray& ba, QSysInfo::Endian byteOrder);
qint32 bytesToInt32(const QByteArray& ba, QSysInfo::Endian byteOrder);
quint32 bytesToUInt32(const QByteArray& ba, QSysInfo::Endian byteOrder);
qint64 bytesToInt64(const QByteArray& ba, QSysInfo::Endian byteOrder);
quint64 bytesToUInt64(const QByteArray& ba, QSysInfo::Endian byteOrder);
if (byteOrder == QSysInfo::LittleEndian) {
return qFromLittleEndian<SizedQInt>(reinterpret_cast<const uchar*>(ba.constData()));
}
return qFromBigEndian<SizedQInt>(reinterpret_cast<const uchar*>(ba.constData()));
}
qint16 readInt16(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok);
quint16 readUInt16(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok);
qint32 readInt32(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok);
quint32 readUInt32(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok);
qint64 readInt64(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok);
quint64 readUInt64(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok);
template<typename SizedQInt>
SizedQInt readSizedInt(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok)
{
QByteArray ba = device->read(sizeof(SizedQInt));
QByteArray int16ToBytes(qint16 num, QSysInfo::Endian byteOrder);
QByteArray int32ToBytes(qint32 num, QSysInfo::Endian byteOrder);
QByteArray int64ToBytes(qint64 num, QSysInfo::Endian byteOrder);
QByteArray uint16ToBytes(quint16 num, QSysInfo::Endian byteOrder);
QByteArray uint32ToBytes(quint32 num, QSysInfo::Endian byteOrder);
QByteArray uint64ToBytes(quint64 num, QSysInfo::Endian byteOrder);
if (ba.size() != sizeof(SizedQInt)) {
*ok = false;
return 0;
}
*ok = true;
return bytesToSizedInt<SizedQInt>(ba, byteOrder);
}
bool writeInt16(qint16 num, QIODevice* device, QSysInfo::Endian byteOrder);
bool writeInt32(qint32 num, QIODevice* device, QSysInfo::Endian byteOrder);
bool writeInt64(qint64 num, QIODevice* device, QSysInfo::Endian byteOrder);
template<typename SizedQInt>
QByteArray sizedIntToBytes(SizedQInt num, QSysInfo::Endian byteOrder)
{
QByteArray ba;
ba.resize(sizeof(SizedQInt));
if (byteOrder == QSysInfo::LittleEndian) {
qToLittleEndian<SizedQInt>(num, reinterpret_cast<uchar*>(ba.data()));
} else {
qToBigEndian<SizedQInt>(num, reinterpret_cast<uchar*>(ba.data()));
}
return ba;
}
template<typename SizedQInt>
bool writeSizedInt(SizedQInt num, QIODevice* device, QSysInfo::Endian byteOrder)
{
QByteArray ba = sizedIntToBytes<SizedQInt>(num, byteOrder);
qint64 bytesWritten = device->write(ba);
return (bytesWritten == ba.size());
}
} // namespace Endian

View File

@ -62,13 +62,13 @@ Database* Kdbx3Reader::readDatabase(QIODevice* device, const CompositeKey& key,
bool ok;
quint32 signature1 = Endian::readUInt32(m_headerStream, KeePass2::BYTEORDER, &ok);
quint32 signature1 = Endian::readSizedInt<quint32>(m_headerStream, KeePass2::BYTEORDER, &ok);
if (!ok || signature1 != KeePass2::SIGNATURE_1) {
raiseError(tr("Not a KeePass database."));
return nullptr;
}
quint32 signature2 = Endian::readUInt32(m_headerStream, KeePass2::BYTEORDER, &ok);
quint32 signature2 = Endian::readSizedInt<quint32>(m_headerStream, KeePass2::BYTEORDER, &ok);
if (ok && signature2 == KeePass1::SIGNATURE_2) {
raiseError(tr("The selected file is an old KeePass 1 database (.kdb).\n\n"
"You can import it by clicking on Database > 'Import KeePass 1 database...'.\n"
@ -81,7 +81,7 @@ Database* Kdbx3Reader::readDatabase(QIODevice* device, const CompositeKey& key,
return nullptr;
}
quint32 version = Endian::readUInt32(m_headerStream, KeePass2::BYTEORDER, &ok)
quint32 version = Endian::readSizedInt<quint32>(m_headerStream, KeePass2::BYTEORDER, &ok)
& KeePass2::FILE_VERSION_CRITICAL_MASK;
quint32 maxVersion = KeePass2::FILE_VERSION & KeePass2::FILE_VERSION_CRITICAL_MASK;
if (!ok || (version < KeePass2::FILE_VERSION_MIN) || (version > maxVersion)) {
@ -214,7 +214,7 @@ bool Kdbx3Reader::readHeaderField()
quint8 fieldID = fieldIDArray.at(0);
bool ok;
quint16 fieldLen = Endian::readUInt16(m_headerStream, KeePass2::BYTEORDER, &ok);
quint16 fieldLen = Endian::readSizedInt<quint16>(m_headerStream, KeePass2::BYTEORDER, &ok);
if (!ok) {
raiseError("Invalid header field length");
return false;
@ -299,7 +299,7 @@ void Kdbx3Reader::setCompressionFlags(const QByteArray& data)
raiseError("Invalid compression flags length");
}
else {
quint32 id = Endian::bytesToUInt32(data, KeePass2::BYTEORDER);
quint32 id = Endian::bytesToSizedInt<quint32>(data, KeePass2::BYTEORDER);
if (id > Database::CompressionAlgorithmMax) {
raiseError("Unsupported compression algorithm");
@ -344,7 +344,7 @@ void Kdbx3Reader::setTransformRounds(const QByteArray& data)
raiseError("Invalid transform rounds size");
}
else {
quint64 rounds = Endian::bytesToUInt64(data, KeePass2::BYTEORDER);
quint64 rounds = Endian::bytesToSizedInt<quint64>(data, KeePass2::BYTEORDER);
AesKdf* aesKdf;
if (m_db->kdf()->type() == Kdf::Type::AES) {
@ -383,7 +383,7 @@ void Kdbx3Reader::setInnerRandomStreamID(const QByteArray& data)
if (data.size() != 4) {
raiseError("Invalid random stream id size");
} else {
quint32 id = Endian::bytesToUInt32(data, KeePass2::BYTEORDER);
quint32 id = Endian::bytesToSizedInt<quint32>(data, KeePass2::BYTEORDER);
KeePass2::ProtectedStreamAlgo irsAlgo = KeePass2::idToProtectedStreamAlgo(id);
if (irsAlgo == KeePass2::InvalidProtectedStreamAlgo || irsAlgo == KeePass2::ArcFourVariant) {
raiseError("Invalid inner random stream cipher");

View File

@ -72,25 +72,25 @@ bool Kdbx3Writer::writeDatabase(QIODevice* device, Database* db)
header.open(QIODevice::WriteOnly);
m_device = &header;
CHECK_RETURN_FALSE(writeData(Endian::int32ToBytes(KeePass2::SIGNATURE_1, KeePass2::BYTEORDER)));
CHECK_RETURN_FALSE(writeData(Endian::int32ToBytes(KeePass2::SIGNATURE_2, KeePass2::BYTEORDER)));
CHECK_RETURN_FALSE(writeData(Endian::int32ToBytes(KeePass2::FILE_VERSION, KeePass2::BYTEORDER)));
CHECK_RETURN_FALSE(writeData(Endian::sizedIntToBytes<qint32>(KeePass2::SIGNATURE_1, KeePass2::BYTEORDER)));
CHECK_RETURN_FALSE(writeData(Endian::sizedIntToBytes<qint32>(KeePass2::SIGNATURE_2, KeePass2::BYTEORDER)));
CHECK_RETURN_FALSE(writeData(Endian::sizedIntToBytes<qint32>(KeePass2::FILE_VERSION, KeePass2::BYTEORDER)));
CHECK_RETURN_FALSE(writeHeaderField(KeePass2::CipherID, db->cipher().toByteArray()));
CHECK_RETURN_FALSE(writeHeaderField(KeePass2::CompressionFlags,
Endian::int32ToBytes(db->compressionAlgo(),
Endian::sizedIntToBytes<qint32>(db->compressionAlgo(),
KeePass2::BYTEORDER)));
AesKdf* kdf = static_cast<AesKdf*>(db->kdf());
CHECK_RETURN_FALSE(writeHeaderField(KeePass2::MasterSeed, masterSeed));
CHECK_RETURN_FALSE(writeHeaderField(KeePass2::TransformSeed, kdf->seed()));
CHECK_RETURN_FALSE(writeHeaderField(KeePass2::TransformRounds,
Endian::int64ToBytes(kdf->rounds(),
Endian::sizedIntToBytes<qint64>(kdf->rounds(),
KeePass2::BYTEORDER)));
CHECK_RETURN_FALSE(writeHeaderField(KeePass2::EncryptionIV, encryptionIV));
CHECK_RETURN_FALSE(writeHeaderField(KeePass2::ProtectedStreamKey, protectedStreamKey));
CHECK_RETURN_FALSE(writeHeaderField(KeePass2::StreamStartBytes, startBytes));
CHECK_RETURN_FALSE(writeHeaderField(KeePass2::InnerRandomStreamID,
Endian::int32ToBytes(KeePass2::Salsa20,
Endian::sizedIntToBytes<qint32>(KeePass2::Salsa20,
KeePass2::BYTEORDER)));
CHECK_RETURN_FALSE(writeHeaderField(KeePass2::EndOfHeader, endOfHeader));
@ -179,7 +179,7 @@ bool Kdbx3Writer::writeHeaderField(KeePass2::HeaderFieldID fieldId, const QByteA
QByteArray fieldIdArr;
fieldIdArr[0] = fieldId;
CHECK_RETURN_FALSE(writeData(fieldIdArr));
CHECK_RETURN_FALSE(writeData(Endian::int16ToBytes(static_cast<quint16>(data.size()),
CHECK_RETURN_FALSE(writeData(Endian::sizedIntToBytes<qint16>(static_cast<quint16>(data.size()),
KeePass2::BYTEORDER)));
CHECK_RETURN_FALSE(writeData(data));

View File

@ -94,25 +94,25 @@ Database* KeePass1Reader::readDatabase(QIODevice* device, const QString& passwor
bool ok;
quint32 signature1 = Endian::readUInt32(m_device, KeePass1::BYTEORDER, &ok);
quint32 signature1 = Endian::readSizedInt<quint32>(m_device, KeePass1::BYTEORDER, &ok);
if (!ok || signature1 != KeePass1::SIGNATURE_1) {
raiseError(tr("Not a KeePass database."));
return nullptr;
}
quint32 signature2 = Endian::readUInt32(m_device, KeePass1::BYTEORDER, &ok);
quint32 signature2 = Endian::readSizedInt<quint32>(m_device, KeePass1::BYTEORDER, &ok);
if (!ok || signature2 != KeePass1::SIGNATURE_2) {
raiseError(tr("Not a KeePass database."));
return nullptr;
}
m_encryptionFlags = Endian::readUInt32(m_device, KeePass1::BYTEORDER, &ok);
m_encryptionFlags = Endian::readSizedInt<quint32>(m_device, KeePass1::BYTEORDER, &ok);
if (!ok || !(m_encryptionFlags & KeePass1::Rijndael || m_encryptionFlags & KeePass1::Twofish)) {
raiseError(tr("Unsupported encryption algorithm."));
return nullptr;
}
quint32 version = Endian::readUInt32(m_device, KeePass1::BYTEORDER, &ok);
quint32 version = Endian::readSizedInt<quint32>(m_device, KeePass1::BYTEORDER, &ok);
if (!ok || (version & KeePass1::FILE_VERSION_CRITICAL_MASK)
!= (KeePass1::FILE_VERSION & KeePass1::FILE_VERSION_CRITICAL_MASK)) {
raiseError(tr("Unsupported KeePass database version."));
@ -131,13 +131,13 @@ Database* KeePass1Reader::readDatabase(QIODevice* device, const QString& passwor
return nullptr;
}
quint32 numGroups = Endian::readUInt32(m_device, KeePass1::BYTEORDER, &ok);
quint32 numGroups = Endian::readSizedInt<quint32>(m_device, KeePass1::BYTEORDER, &ok);
if (!ok) {
raiseError("Invalid number of groups");
return nullptr;
}
quint32 numEntries = Endian::readUInt32(m_device, KeePass1::BYTEORDER, &ok);
quint32 numEntries = Endian::readSizedInt<quint32>(m_device, KeePass1::BYTEORDER, &ok);
if (!ok) {
raiseError("Invalid number of entries");
return nullptr;
@ -155,7 +155,7 @@ Database* KeePass1Reader::readDatabase(QIODevice* device, const QString& passwor
return nullptr;
}
m_transformRounds = Endian::readUInt32(m_device, KeePass1::BYTEORDER, &ok);
m_transformRounds = Endian::readSizedInt<quint32>(m_device, KeePass1::BYTEORDER, &ok);
if (!ok) {
raiseError("Invalid number of transform rounds");
return nullptr;
@ -444,13 +444,13 @@ Group* KeePass1Reader::readGroup(QIODevice* cipherStream)
bool reachedEnd = false;
do {
quint16 fieldType = Endian::readUInt16(cipherStream, KeePass1::BYTEORDER, &ok);
quint16 fieldType = Endian::readSizedInt<quint16>(cipherStream, KeePass1::BYTEORDER, &ok);
if (!ok) {
raiseError("Invalid group field type number");
return nullptr;
}
int fieldSize = static_cast<int>(Endian::readUInt32(cipherStream, KeePass1::BYTEORDER, &ok));
int fieldSize = static_cast<int>(Endian::readSizedInt<quint32>(cipherStream, KeePass1::BYTEORDER, &ok));
if (!ok) {
raiseError("Invalid group field size");
return nullptr;
@ -471,7 +471,7 @@ Group* KeePass1Reader::readGroup(QIODevice* cipherStream)
raiseError("Incorrect group id field size");
return nullptr;
}
groupId = Endian::bytesToUInt32(fieldData, KeePass1::BYTEORDER);
groupId = Endian::bytesToSizedInt<quint32>(fieldData, KeePass1::BYTEORDER);
groupIdSet = true;
break;
case 0x0002:
@ -530,7 +530,7 @@ Group* KeePass1Reader::readGroup(QIODevice* cipherStream)
raiseError("Incorrect group icon field size");
return nullptr;
}
quint32 iconNumber = Endian::bytesToUInt32(fieldData, KeePass1::BYTEORDER);
quint32 iconNumber = Endian::bytesToSizedInt<quint32>(fieldData, KeePass1::BYTEORDER);
group->setIcon(iconNumber);
break;
}
@ -540,7 +540,7 @@ Group* KeePass1Reader::readGroup(QIODevice* cipherStream)
raiseError("Incorrect group level field size");
return nullptr;
}
groupLevel = Endian::bytesToUInt16(fieldData, KeePass1::BYTEORDER);
groupLevel = Endian::bytesToSizedInt<quint16>(fieldData, KeePass1::BYTEORDER);
groupLevelSet = true;
break;
}
@ -582,13 +582,13 @@ Entry* KeePass1Reader::readEntry(QIODevice* cipherStream)
bool reachedEnd = false;
do {
quint16 fieldType = Endian::readUInt16(cipherStream, KeePass1::BYTEORDER, &ok);
quint16 fieldType = Endian::readSizedInt<quint16>(cipherStream, KeePass1::BYTEORDER, &ok);
if (!ok) {
raiseError("Missing entry field type number");
return nullptr;
}
int fieldSize = static_cast<int>(Endian::readUInt32(cipherStream, KeePass1::BYTEORDER, &ok));
int fieldSize = static_cast<int>(Endian::readSizedInt<quint32>(cipherStream, KeePass1::BYTEORDER, &ok));
if (!ok) {
raiseError("Invalid entry field size");
return nullptr;
@ -617,7 +617,7 @@ Entry* KeePass1Reader::readEntry(QIODevice* cipherStream)
raiseError("Invalid entry group id field size");
return nullptr;
}
quint32 groupId = Endian::bytesToUInt32(fieldData, KeePass1::BYTEORDER);
quint32 groupId = Endian::bytesToSizedInt<quint32>(fieldData, KeePass1::BYTEORDER);
m_entryGroupIds.insert(entry.data(), groupId);
break;
}
@ -627,7 +627,7 @@ Entry* KeePass1Reader::readEntry(QIODevice* cipherStream)
raiseError("Invalid entry icon field size");
return nullptr;
}
quint32 iconNumber = Endian::bytesToUInt32(fieldData, KeePass1::BYTEORDER);
quint32 iconNumber = Endian::bytesToSizedInt<quint32>(fieldData, KeePass1::BYTEORDER);
entry->setIcon(iconNumber);
break;
}
@ -837,7 +837,7 @@ bool KeePass1Reader::parseGroupTreeState(const QByteArray& data)
}
int pos = 0;
quint32 num = Endian::bytesToUInt32(data.mid(pos, 4), KeePass1::BYTEORDER);
quint32 num = Endian::bytesToSizedInt<quint32>(data.mid(pos, 4), KeePass1::BYTEORDER);
pos += 4;
if (static_cast<quint32>(data.size() - 4) != (num * 5)) {
@ -845,7 +845,7 @@ bool KeePass1Reader::parseGroupTreeState(const QByteArray& data)
}
for (quint32 i = 0; i < num; i++) {
quint32 groupId = Endian::bytesToUInt32(data.mid(pos, 4), KeePass1::BYTEORDER);
quint32 groupId = Endian::bytesToSizedInt<quint32>(data.mid(pos, 4), KeePass1::BYTEORDER);
pos += 4;
bool expanded = data.at(pos);
@ -867,13 +867,13 @@ bool KeePass1Reader::parseCustomIcons4(const QByteArray& data)
int pos = 0;
quint32 numIcons = Endian::bytesToUInt32(data.mid(pos, 4), KeePass1::BYTEORDER);
quint32 numIcons = Endian::bytesToSizedInt<quint32>(data.mid(pos, 4), KeePass1::BYTEORDER);
pos += 4;
quint32 numEntries = Endian::bytesToUInt32(data.mid(pos, 4), KeePass1::BYTEORDER);
quint32 numEntries = Endian::bytesToSizedInt<quint32>(data.mid(pos, 4), KeePass1::BYTEORDER);
pos += 4;
quint32 numGroups = Endian::bytesToUInt32(data.mid(pos, 4), KeePass1::BYTEORDER);
quint32 numGroups = Endian::bytesToSizedInt<quint32>(data.mid(pos, 4), KeePass1::BYTEORDER);
pos += 4;
QList<Uuid> iconUuids;
@ -882,7 +882,7 @@ bool KeePass1Reader::parseCustomIcons4(const QByteArray& data)
if (data.size() < (pos + 4)) {
return false;
}
quint32 iconSize = Endian::bytesToUInt32(data.mid(pos, 4), KeePass1::BYTEORDER);
quint32 iconSize = Endian::bytesToSizedInt<quint32>(data.mid(pos, 4), KeePass1::BYTEORDER);
pos += 4;
if (static_cast<quint32>(data.size()) < (pos + iconSize)) {
@ -908,7 +908,7 @@ bool KeePass1Reader::parseCustomIcons4(const QByteArray& data)
QByteArray entryUuid = data.mid(pos, 16);
pos += 16;
quint32 iconId = Endian::bytesToUInt32(data.mid(pos, 4), KeePass1::BYTEORDER);
quint32 iconId = Endian::bytesToSizedInt<quint32>(data.mid(pos, 4), KeePass1::BYTEORDER);
pos += 4;
if (m_entryUuids.contains(entryUuid) && (iconId < static_cast<quint32>(iconUuids.size()))) {
@ -921,10 +921,10 @@ bool KeePass1Reader::parseCustomIcons4(const QByteArray& data)
}
for (quint32 i = 0; i < numGroups; i++) {
quint32 groupId = Endian::bytesToUInt32(data.mid(pos, 4), KeePass1::BYTEORDER);
quint32 groupId = Endian::bytesToSizedInt<quint32>(data.mid(pos, 4), KeePass1::BYTEORDER);
pos += 4;
quint32 iconId = Endian::bytesToUInt32(data.mid(pos, 4), KeePass1::BYTEORDER);
quint32 iconId = Endian::bytesToSizedInt<quint32>(data.mid(pos, 4), KeePass1::BYTEORDER);
pos += 4;
if (m_groupIds.contains(groupId) && (iconId < static_cast<quint32>(iconUuids.size()))) {

View File

@ -99,13 +99,13 @@ Database* KeePass2Reader::readDatabase(QIODevice* device, const CompositeKey& ke
bool ok;
quint32 signature1 = Endian::readUInt32(device, KeePass2::BYTEORDER, &ok);
quint32 signature1 = Endian::readSizedInt<quint32>(device, KeePass2::BYTEORDER, &ok);
if (!ok || signature1 != KeePass2::SIGNATURE_1) {
raiseError(tr("Not a KeePass database."));
return nullptr;
}
quint32 signature2 = Endian::readUInt32(device, KeePass2::BYTEORDER, &ok);
quint32 signature2 = Endian::readSizedInt<quint32>(device, KeePass2::BYTEORDER, &ok);
if (ok && signature2 == KeePass1::SIGNATURE_2) {
raiseError(tr("The selected file is an old KeePass 1 database (.kdb).\n\n"
"You can import it by clicking on Database > 'Import KeePass 1 database...'.\n"
@ -118,7 +118,7 @@ Database* KeePass2Reader::readDatabase(QIODevice* device, const CompositeKey& ke
return nullptr;
}
m_version = Endian::readUInt32(device, KeePass2::BYTEORDER, &ok)
m_version = Endian::readSizedInt<quint32>(device, KeePass2::BYTEORDER, &ok)
& KeePass2::FILE_VERSION_CRITICAL_MASK;
quint32 maxVersion = KeePass2::FILE_VERSION & KeePass2::FILE_VERSION_CRITICAL_MASK;
if (!ok || (m_version < KeePass2::FILE_VERSION_MIN) || (m_version > maxVersion)) {

View File

@ -130,7 +130,7 @@ bool HashedBlockStream::readHashedBlock()
{
bool ok;
quint32 index = Endian::readUInt32(m_baseDevice, ByteOrder, &ok);
quint32 index = Endian::readSizedInt<quint32>(m_baseDevice, ByteOrder, &ok);
if (!ok || index != m_blockIndex) {
m_error = true;
setErrorString("Invalid block index.");
@ -144,7 +144,7 @@ bool HashedBlockStream::readHashedBlock()
return false;
}
m_blockSize = Endian::readInt32(m_baseDevice, ByteOrder, &ok);
m_blockSize = Endian::readSizedInt<qint32>(m_baseDevice, ByteOrder, &ok);
if (!ok || m_blockSize < 0) {
m_error = true;
setErrorString("Invalid block size.");
@ -217,7 +217,7 @@ qint64 HashedBlockStream::writeData(const char* data, qint64 maxSize)
bool HashedBlockStream::writeHashedBlock()
{
if (!Endian::writeInt32(m_blockIndex, m_baseDevice, ByteOrder)) {
if (!Endian::writeSizedInt<qint32>(m_blockIndex, m_baseDevice, ByteOrder)) {
m_error = true;
setErrorString(m_baseDevice->errorString());
return false;
@ -238,7 +238,7 @@ bool HashedBlockStream::writeHashedBlock()
return false;
}
if (!Endian::writeInt32(m_buffer.size(), m_baseDevice, ByteOrder)) {
if (!Endian::writeSizedInt<qint32>(m_buffer.size(), m_baseDevice, ByteOrder)) {
m_error = true;
setErrorString(m_baseDevice->errorString());
return false;

View File

@ -144,7 +144,7 @@ bool HmacBlockStream::readHashedBlock()
setErrorString("Invalid block size size.");
return false;
}
qint32 blockSize = Endian::bytesToInt32(blockSizeBytes, ByteOrder);
qint32 blockSize = Endian::bytesToSizedInt<qint32>(blockSizeBytes, ByteOrder);
if (blockSize < 0) {
m_error = true;
setErrorString("Invalid block size.");
@ -160,7 +160,7 @@ bool HmacBlockStream::readHashedBlock()
CryptoHash hasher(CryptoHash::Sha256, true);
hasher.setKey(getCurrentHmacKey());
hasher.addData(Endian::uint64ToBytes(m_blockIndex, ByteOrder));
hasher.addData(Endian::sizedIntToBytes<quint64>(m_blockIndex, ByteOrder));
hasher.addData(blockSizeBytes);
hasher.addData(m_buffer);
@ -219,8 +219,8 @@ bool HmacBlockStream::writeHashedBlock()
{
CryptoHash hasher(CryptoHash::Sha256, true);
hasher.setKey(getCurrentHmacKey());
hasher.addData(Endian::uint64ToBytes(m_blockIndex, ByteOrder));
hasher.addData(Endian::int32ToBytes(m_buffer.size(), ByteOrder));
hasher.addData(Endian::sizedIntToBytes<quint64>(m_blockIndex, ByteOrder));
hasher.addData(Endian::sizedIntToBytes<qint32>(m_buffer.size(), ByteOrder));
hasher.addData(m_buffer);
QByteArray hash = hasher.result();
@ -230,7 +230,7 @@ bool HmacBlockStream::writeHashedBlock()
return false;
}
if (!Endian::writeInt32(m_buffer.size(), m_baseDevice, ByteOrder)) {
if (!Endian::writeSizedInt<qint32>(m_buffer.size(), m_baseDevice, ByteOrder)) {
m_error = true;
setErrorString(m_baseDevice->errorString());
return false;
@ -255,7 +255,7 @@ QByteArray HmacBlockStream::getCurrentHmacKey() const {
QByteArray HmacBlockStream::getHmacKey(quint64 blockIndex, QByteArray key) {
Q_ASSERT(key.size() == 64);
QByteArray indexBytes = Endian::uint64ToBytes(blockIndex, ByteOrder);
QByteArray indexBytes = Endian::sizedIntToBytes<quint64>(blockIndex, ByteOrder);
CryptoHash hasher(CryptoHash::Sha512);
hasher.addData(indexBytes);
hasher.addData(key);