18
#include "format/KeePass2Reader.h"
19
#include "format/Kdbx3Reader.h"
20
#include "format/Kdbx4Reader.h"
21
#include "format/KeePass1.h"
22
#include "keys/CompositeKey.h"
34
bool KeePass2Reader::readDatabase(const QString& filename, QSharedPointer<const CompositeKey> key, Database* db)
37
if (!file.open(QFile::ReadOnly)) {
38
raiseError(file.errorString());
42
bool ok = readDatabase(&file, std::move(key), db);
44
if (file.error() != QFile::NoError) {
45
raiseError(file.errorString());
60
bool KeePass2Reader::readDatabase(QIODevice* device, QSharedPointer<const CompositeKey> key, Database* db)
65
quint32 signature1, signature2;
66
bool ok = KdbxReader::readMagicNumbers(device, signature1, signature2, m_version);
69
raiseError(tr("Failed to read database file."));
73
if (signature1 == KeePass1::SIGNATURE_1 && signature2 == KeePass1::SIGNATURE_2) {
74
raiseError(tr("The selected file is an old KeePass 1 database (.kdb).\n\n"
75
"You can import it by clicking on Database > 'Import KeePass 1 database…'.\n"
76
"This is a one-way migration. You won't be able to open the imported "
77
"database with the old KeePassX 0.4 version."));
79
} else if (!(signature1 == KeePass2::SIGNATURE_1 && signature2 == KeePass2::SIGNATURE_2)) {
80
raiseError(tr("Not a KeePass database."));
84
if (m_version < KeePass2::FILE_VERSION_MIN
85
|| (m_version & KeePass2::FILE_VERSION_CRITICAL_MASK) > KeePass2::FILE_VERSION_MAX) {
86
raiseError(tr("Unsupported KeePass 2 database version."));
91
if (m_version < KeePass2::FILE_VERSION_4) {
92
m_reader.reset(new Kdbx3Reader());
94
m_reader.reset(new Kdbx4Reader());
97
return m_reader->readDatabase(device, std::move(key), db);
100
bool KeePass2Reader::hasError() const
102
return m_error || (!m_reader.isNull() && m_reader->hasError());
105
QString KeePass2Reader::errorString() const
107
return !m_reader.isNull() ? m_reader->errorString() : m_errorStr;
113
quint32 KeePass2Reader::version() const
121
QSharedPointer<KdbxReader> KeePass2Reader::reader() const
131
void KeePass2Reader::raiseError(const QString& errorMessage)
134
m_errorStr = errorMessage;