Skip to content

Commit

Permalink
CLI: Fix XML encoding when export database
Browse files Browse the repository at this point in the history
Add write function to TextStream
Fix keepassxreboot#3900
  • Loading branch information
ba32107 authored and droidmonkey committed Jan 30, 2020
1 parent 792c1c9 commit 06e0f38
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cli/Export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int Export::executeWithDatabase(QSharedPointer<Database> database, QSharedPointe
errorTextStream << QObject::tr("Unable to export database to XML: %1").arg(errorMessage) << endl;
return EXIT_FAILURE;
}
outputTextStream << xmlData.constData() << endl;
outputTextStream.write(xmlData.constData());
} else if (format.startsWith(QStringLiteral("csv"), Qt::CaseInsensitive)) {
CsvExporter csvExporter;
outputTextStream << csvExporter.exportDatabase(database);
Expand Down
9 changes: 9 additions & 0 deletions src/cli/TextStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ TextStream::TextStream(const QByteArray& array, QIODevice::OpenMode openMode)
detectCodec();
}

void TextStream::write(const char* str)
{
// Workaround for an issue with QTextStream. Its operator<<(const char *string) will encode the
// string with a non-UTF-8 encoding. We work around this by wrapping the input string into
// a QString, thus enforcing UTF-8. More info:
// https://code.qt.io/cgit/qt/qtbase.git/commit?id=cec8cdba4d1b856e17c8743ba8803349d42dc701
*this << QString(str);
}

void TextStream::detectCodec()
{
QString codecName = "UTF-8";
Expand Down
2 changes: 2 additions & 0 deletions src/cli/TextStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class TextStream : public QTextStream
explicit TextStream(QByteArray* array, QIODevice::OpenMode openMode = QIODevice::ReadWrite);
explicit TextStream(const QByteArray& array, QIODevice::OpenMode openMode = QIODevice::ReadOnly);

void write(const char* str);

private:
void detectCodec();
};
Expand Down

0 comments on commit 06e0f38

Please sign in to comment.