diff --git a/src/core/Tools.cpp b/src/core/Tools.cpp index 341159546..77a7df910 100644 --- a/src/core/Tools.cpp +++ b/src/core/Tools.cpp @@ -17,6 +17,7 @@ #include "Tools.h" +#include #include #include @@ -51,4 +52,27 @@ bool hasChild(const QObject* parent, const QObject* child) return false; } +bool readAllFromDevice(QIODevice* device, QByteArray& data) +{ + QByteArray result; + qint64 readBytes = 0; + qint64 readResult; + do { + result.resize(result.size() + 16384); + readResult = device->read(result.data() + readBytes, result.size() - readBytes); + if (readResult > 0) { + readBytes += readResult; + } + } while (readResult > 0); + + if (readResult == -1) { + return false; + } + else { + result.resize(static_cast(readBytes)); + data = result; + return true; + } +} + } // namespace Tools diff --git a/src/core/Tools.h b/src/core/Tools.h index 1241a6c39..75cd6dd67 100644 --- a/src/core/Tools.h +++ b/src/core/Tools.h @@ -21,10 +21,13 @@ #include #include +class QIODevice; + namespace Tools { QString humanReadableFileSize(qint64 bytes); bool hasChild(const QObject* parent, const QObject* child); +bool readAllFromDevice(QIODevice* device, QByteArray& data); } // namespace Tools diff --git a/src/format/KeePass2XmlReader.cpp b/src/format/KeePass2XmlReader.cpp index d8bd9c975..97e4725a2 100644 --- a/src/format/KeePass2XmlReader.cpp +++ b/src/format/KeePass2XmlReader.cpp @@ -24,6 +24,7 @@ #include "core/DatabaseIcons.h" #include "core/Group.h" #include "core/Metadata.h" +#include "core/Tools.h" #include "format/KeePass2RandomStream.h" #include "streams/QtIOCompressor" @@ -873,24 +874,10 @@ QByteArray KeePass2XmlReader::readCompressedBinary() compressor.open(QIODevice::ReadOnly); QByteArray result; - qint64 readBytes = 0; - qint64 readResult; - do { - result.resize(result.size() + 16384); - readResult = compressor.read(result.data() + readBytes, result.size() - readBytes); - if (readResult > 0) { - readBytes += readResult; - } - } while (readResult > 0); - - if (readResult == -1) { + if (!Tools::readAllFromDevice(&compressor, result)) { raiseError(16); - return QByteArray(); - } - else { - result.resize(static_cast(readBytes)); - return result; } + return result; } Group* KeePass2XmlReader::getGroup(const Uuid& uuid)