Add Tools::readAllFromDevice().

Make KeePass2XmlReader::readCompressedBinary() use the new function.
This commit is contained in:
Felix Geyer 2012-05-02 11:06:24 +02:00
parent f8f52419c8
commit f89ffa10e6
3 changed files with 30 additions and 16 deletions

View File

@ -17,6 +17,7 @@
#include "Tools.h" #include "Tools.h"
#include <QtCore/QIODevice>
#include <QtCore/QLocale> #include <QtCore/QLocale>
#include <QtCore/QStringList> #include <QtCore/QStringList>
@ -51,4 +52,27 @@ bool hasChild(const QObject* parent, const QObject* child)
return false; 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<int>(readBytes));
data = result;
return true;
}
}
} // namespace Tools } // namespace Tools

View File

@ -21,10 +21,13 @@
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QtCore/QString> #include <QtCore/QString>
class QIODevice;
namespace Tools { namespace Tools {
QString humanReadableFileSize(qint64 bytes); QString humanReadableFileSize(qint64 bytes);
bool hasChild(const QObject* parent, const QObject* child); bool hasChild(const QObject* parent, const QObject* child);
bool readAllFromDevice(QIODevice* device, QByteArray& data);
} // namespace Tools } // namespace Tools

View File

@ -24,6 +24,7 @@
#include "core/DatabaseIcons.h" #include "core/DatabaseIcons.h"
#include "core/Group.h" #include "core/Group.h"
#include "core/Metadata.h" #include "core/Metadata.h"
#include "core/Tools.h"
#include "format/KeePass2RandomStream.h" #include "format/KeePass2RandomStream.h"
#include "streams/QtIOCompressor" #include "streams/QtIOCompressor"
@ -873,24 +874,10 @@ QByteArray KeePass2XmlReader::readCompressedBinary()
compressor.open(QIODevice::ReadOnly); compressor.open(QIODevice::ReadOnly);
QByteArray result; QByteArray result;
qint64 readBytes = 0; if (!Tools::readAllFromDevice(&compressor, result)) {
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) {
raiseError(16); raiseError(16);
return QByteArray();
}
else {
result.resize(static_cast<int>(readBytes));
return result;
} }
return result;
} }
Group* KeePass2XmlReader::getGroup(const Uuid& uuid) Group* KeePass2XmlReader::getGroup(const Uuid& uuid)