diff --git a/src/format/Kdbx4Writer.cpp b/src/format/Kdbx4Writer.cpp index 70bfa2d5b..7ce8bab1a 100644 --- a/src/format/Kdbx4Writer.cpp +++ b/src/format/Kdbx4Writer.cpp @@ -152,6 +152,9 @@ bool Kdbx4Writer::writeDatabase(QIODevice* device, Database* db) CHECK_RETURN_FALSE(writeInnerHeaderField(outputDevice, KeePass2::InnerHeaderFieldID::InnerRandomStreamKey, protectedStreamKey)); + // Write attachments to the inner header + writeAttachments(outputDevice, db); + CHECK_RETURN_FALSE(writeInnerHeaderField(outputDevice, KeePass2::InnerHeaderFieldID::End, QByteArray())); KeePass2RandomStream randomStream(KeePass2::ProtectedStreamAlgo::ChaCha20); @@ -204,24 +207,17 @@ bool Kdbx4Writer::writeInnerHeaderField(QIODevice* device, KeePass2::InnerHeader return true; } -/** - * Write binary header field.. - * - * @param device output device - * @param fieldId field identifier - * @param data header payload - * @return true on success - */ -bool Kdbx4Writer::writeBinary(QIODevice* device, const QByteArray& data) +void Kdbx4Writer::writeAttachments(QIODevice* device, Database* db) { - QByteArray fieldIdArr; - fieldIdArr[0] = static_cast(KeePass2::InnerHeaderFieldID::Binary); - CHECK_RETURN_FALSE(writeData(device, fieldIdArr)); - CHECK_RETURN_FALSE(writeData(device, Endian::sizedIntToBytes(static_cast(data.size() + 1), KeePass2::BYTEORDER))); - CHECK_RETURN_FALSE(writeData(device, QByteArray(1, '\1'))); - CHECK_RETURN_FALSE(writeData(device, data)); - - return true; + const QList allEntries = db->rootGroup()->entriesRecursive(true); + for (Entry* entry : allEntries) { + const QList attachmentKeys = entry->attachments()->keys(); + for (const QString& key : attachmentKeys) { + QByteArray data = entry->attachments()->value(key); + data.prepend("\x01"); + writeInnerHeaderField(device, KeePass2::InnerHeaderFieldID::Binary, data); + } + } } /** diff --git a/src/format/Kdbx4Writer.h b/src/format/Kdbx4Writer.h index 097a7864a..f05ad21a3 100644 --- a/src/format/Kdbx4Writer.h +++ b/src/format/Kdbx4Writer.h @@ -30,7 +30,7 @@ public: private: bool writeInnerHeaderField(QIODevice* device, KeePass2::InnerHeaderFieldID fieldId, const QByteArray& data); - bool writeBinary(QIODevice* device, const QByteArray& data); + void writeAttachments(QIODevice* device, Database* db); static bool serializeVariantMap(const QVariantMap& map, QByteArray& outputBytes); };