mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-08-21 04:28:26 -04:00
parent
93585aded7
commit
eb56bd8973
13 changed files with 393 additions and 3 deletions
|
@ -43,7 +43,7 @@ KeePass2Reader::KeePass2Reader()
|
|||
{
|
||||
}
|
||||
|
||||
Database* KeePass2Reader::readDatabase(QIODevice* device, const CompositeKey& key)
|
||||
Database* KeePass2Reader::readDatabase(QIODevice* device, const CompositeKey& key, bool keepDatabase)
|
||||
{
|
||||
QScopedPointer<Database> db(new Database());
|
||||
m_db = db.data();
|
||||
|
@ -178,7 +178,12 @@ Database* KeePass2Reader::readDatabase(QIODevice* device, const CompositeKey& ke
|
|||
|
||||
if (xmlReader.hasError()) {
|
||||
raiseError(xmlReader.errorString());
|
||||
return Q_NULLPTR;
|
||||
if (keepDatabase) {
|
||||
return db.take();
|
||||
}
|
||||
else {
|
||||
return Q_NULLPTR;
|
||||
}
|
||||
}
|
||||
|
||||
Q_ASSERT(version < 0x00030001 || !xmlReader.headerHash().isEmpty());
|
||||
|
@ -232,6 +237,11 @@ QByteArray KeePass2Reader::xmlData()
|
|||
return m_xmlData;
|
||||
}
|
||||
|
||||
QByteArray KeePass2Reader::streamKey()
|
||||
{
|
||||
return m_protectedStreamKey;
|
||||
}
|
||||
|
||||
void KeePass2Reader::raiseError(const QString& errorMessage)
|
||||
{
|
||||
m_error = true;
|
||||
|
|
|
@ -31,12 +31,13 @@ class KeePass2Reader
|
|||
|
||||
public:
|
||||
KeePass2Reader();
|
||||
Database* readDatabase(QIODevice* device, const CompositeKey& key);
|
||||
Database* readDatabase(QIODevice* device, const CompositeKey& key, bool keepDatabase = false);
|
||||
Database* readDatabase(const QString& filename, const CompositeKey& key);
|
||||
bool hasError();
|
||||
QString errorString();
|
||||
void setSaveXml(bool save);
|
||||
QByteArray xmlData();
|
||||
QByteArray streamKey();
|
||||
|
||||
private:
|
||||
void raiseError(const QString& errorMessage);
|
||||
|
|
107
src/format/KeePass2Repair.cpp
Normal file
107
src/format/KeePass2Repair.cpp
Normal file
|
@ -0,0 +1,107 @@
|
|||
/*
|
||||
* Copyright (C) 2016 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 "KeePass2Repair.h"
|
||||
|
||||
#include <QBuffer>
|
||||
#include <QRegExp>
|
||||
|
||||
#include "format/KeePass2RandomStream.h"
|
||||
#include "format/KeePass2Reader.h"
|
||||
#include "format/KeePass2XmlReader.h"
|
||||
|
||||
KeePass2Repair::KeePass2Repair()
|
||||
: m_db(Q_NULLPTR)
|
||||
{
|
||||
}
|
||||
|
||||
KeePass2Repair::RepairResult KeePass2Repair::repairDatabase(QIODevice* device, const CompositeKey& key)
|
||||
{
|
||||
m_db = Q_NULLPTR;
|
||||
m_errorStr.clear();
|
||||
|
||||
KeePass2Reader reader;
|
||||
reader.setSaveXml(true);
|
||||
|
||||
Database* db = reader.readDatabase(device, key, true);
|
||||
if (!reader.hasError()) {
|
||||
delete db;
|
||||
return NothingTodo;
|
||||
}
|
||||
|
||||
QByteArray xmlData = reader.xmlData();
|
||||
if (!db || xmlData.isEmpty()) {
|
||||
delete db;
|
||||
m_errorStr = reader.errorString();
|
||||
return UnableToOpen;
|
||||
}
|
||||
|
||||
bool repairAction = false;
|
||||
|
||||
QString xmlStart = QString::fromLatin1(xmlData.constData(), qMin(100, xmlData.size()));
|
||||
QRegExp encodingRegExp("encoding=\"([^\"]+)\"", Qt::CaseInsensitive, QRegExp::RegExp2);
|
||||
if (encodingRegExp.indexIn(xmlStart) != -1) {
|
||||
if (encodingRegExp.cap(1).compare("utf-8", Qt::CaseInsensitive) != 0
|
||||
&& encodingRegExp.cap(1).compare("utf8", Qt::CaseInsensitive) != 0)
|
||||
{
|
||||
// database is not utf-8 encoded, we don't support repairing that
|
||||
delete db;
|
||||
return RepairFailed;
|
||||
}
|
||||
}
|
||||
|
||||
// try to fix broken databases because of bug #392
|
||||
for (int i = (xmlData.size() - 1); i >= 0; i--) {
|
||||
char ch = xmlData.at(i);
|
||||
if (ch < 0x20 && ch != 0x09 && ch != 0x0A && ch != 0x0D) {
|
||||
xmlData.remove(i, 1);
|
||||
repairAction = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!repairAction) {
|
||||
// we were unable to find the problem
|
||||
delete db;
|
||||
return RepairFailed;
|
||||
}
|
||||
|
||||
KeePass2RandomStream randomStream;
|
||||
randomStream.init(reader.streamKey());
|
||||
KeePass2XmlReader xmlReader;
|
||||
QBuffer buffer(&xmlData);
|
||||
buffer.open(QIODevice::ReadOnly);
|
||||
xmlReader.readDatabase(&buffer, db, &randomStream);
|
||||
|
||||
if (xmlReader.hasError()) {
|
||||
delete db;
|
||||
return RepairFailed;
|
||||
}
|
||||
else {
|
||||
m_db = db;
|
||||
return RepairSuccess;
|
||||
}
|
||||
}
|
||||
|
||||
Database* KeePass2Repair::database() const
|
||||
{
|
||||
return m_db;
|
||||
}
|
||||
|
||||
QString KeePass2Repair::errorString() const
|
||||
{
|
||||
return m_errorStr;
|
||||
}
|
50
src/format/KeePass2Repair.h
Normal file
50
src/format/KeePass2Repair.h
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Copyright (C) 2016 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/>.
|
||||
*/
|
||||
|
||||
#ifndef KEEPASSX_KEEPASS2REPAIR_H
|
||||
#define KEEPASSX_KEEPASS2REPAIR_H
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QIODevice>
|
||||
|
||||
#include "core/Database.h"
|
||||
#include "keys/CompositeKey.h"
|
||||
|
||||
class KeePass2Repair
|
||||
{
|
||||
Q_DECLARE_TR_FUNCTIONS(KeePass2Repair)
|
||||
|
||||
public:
|
||||
enum RepairResult
|
||||
{
|
||||
NothingTodo,
|
||||
UnableToOpen,
|
||||
RepairSuccess,
|
||||
RepairFailed
|
||||
};
|
||||
|
||||
KeePass2Repair();
|
||||
RepairResult repairDatabase(QIODevice* device, const CompositeKey& key);
|
||||
Database* database() const;
|
||||
QString errorString() const;
|
||||
|
||||
private:
|
||||
Database* m_db;
|
||||
QString m_errorStr;
|
||||
};
|
||||
|
||||
#endif // KEEPASSX_KEEPASS2REPAIR_H
|
Loading…
Add table
Add a link
Reference in a new issue