Skip to content

Commit

Permalink
Revert "Use ansi codepage for internal multibyte strings on windows (f…
Browse files Browse the repository at this point in the history
…ixes debauchee#976, fixes debauchee#974, fixes debauchee#444)"

This reverts commit 402801e.
  • Loading branch information
p12tic committed Oct 29, 2021
1 parent dd8c2a1 commit bd6c490
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 151 deletions.
21 changes: 10 additions & 11 deletions src/gui/src/CommandProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,34 @@ CommandProcess::CommandProcess(QString cmd, QStringList arguments, QString input
QString CommandProcess::run()
{
QProcess process;
QString standardOutput, standardError;
process.setReadChannel(QProcess::StandardOutput);
process.start(m_Command, m_Arguments);
bool success = process.waitForStarted();

QString output, error;
if (success)
{
if (!m_Input.isEmpty()) {
process.write(m_Input.toLocal8Bit());
process.write(m_Input.toStdString().c_str());
}

if (process.waitForFinished()) {
standardOutput = QString::fromLocal8Bit(process.readAllStandardOutput().trimmed());
standardError = QString::fromLocal8Bit(process.readAllStandardError().trimmed());
output = process.readAllStandardOutput().trimmed();
error = process.readAllStandardError().trimmed();
}
}

int code = process.exitCode();
if (!standardError.isEmpty() || !success || code != 0)
if (!error.isEmpty() || !success || code != 0)
{
throw std::runtime_error(
std::string(
QString("Code: %1\nError: %2")
.arg(process.exitCode())
.arg(standardError.isEmpty() ? "Unknown" : standardError)
.toLocal8Bit().constData()));
QString("Code: %1\nError: %2")
.arg(process.exitCode())
.arg(error.isEmpty() ? "Unknown" : error)
.toStdString());
}

emit finished();

return standardOutput;
return output;
}
7 changes: 5 additions & 2 deletions src/gui/src/Fingerprint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
*/

#include "Fingerprint.h"
#include "QUtility.h"

#include "common/DataDirectories.h"

#include <QDir>
#include <QTextStream>
Expand Down Expand Up @@ -123,8 +124,10 @@ void Fingerprint::persistDirectory()

QString Fingerprint::directoryPath()
{
auto profileDir = QString::fromStdString(DataDirectories::profile());

return QString("%1/%2")
.arg(profilePath())
.arg(profileDir)
.arg(kDirName);
}

Expand Down
8 changes: 5 additions & 3 deletions src/gui/src/IpcClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,14 @@ void IpcClient::sendCommand(const QString& command, ElevateMode const elevate)

stream.writeRawData(kIpcMsgCommand, 4);

QByteArray utf8Command = command.toUtf8();
std::string stdStringCommand = command.toStdString();
const char* charCommand = stdStringCommand.c_str();
int length = (int)strlen(charCommand);

char lenBuf[4];
intToBytes(utf8Command.size(), lenBuf, 4);
intToBytes(length, lenBuf, 4);
stream.writeRawData(lenBuf, 4);
stream.writeRawData(utf8Command.constData(), utf8Command.size());
stream.writeRawData(charCommand, length);

char elevateBuf[1];
// Refer to enum ElevateMode documentation for why this flag is mapped this way
Expand Down
3 changes: 2 additions & 1 deletion src/gui/src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "ProcessorArch.h"
#include "SslCertificate.h"
#include "ShutdownCh.h"
#include "common/DataDirectories.h"

#include <QtCore>
#include <QtGui>
Expand Down Expand Up @@ -524,7 +525,7 @@ void MainWindow::startBarrier()
// launched the process (e.g. when launched with elevation). setting the
// profile dir on launch ensures it uses the same profile dir is used
// no matter how its relaunched.
args << "--profile-dir" << QString("\"%1\"").arg(profilePath());
args << "--profile-dir" << QString::fromStdString("\"" + DataDirectories::profile() + "\"");
#endif

if ((barrierType() == barrierClient && !clientArgs(args, app))
Expand Down
9 changes: 0 additions & 9 deletions src/gui/src/QUtility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include "ProcessorArch.h"
#include "CommandProcess.h"
#include "common/DataDirectories.h"

#if defined(Q_OS_LINUX)
#include <QProcess>
Expand Down Expand Up @@ -113,11 +112,3 @@ QString getOSInformation()

return result;
}

QString profilePath()
{
// Get path to current profile directory, properly converted
// from an OS locale std::string to Unicode QString.
auto localePath = DataDirectories::profile();
return QString::fromLocal8Bit(localePath.c_str(), localePath.size());
}
1 change: 0 additions & 1 deletion src/gui/src/QUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ QString hash(const QString& string);
QString getFirstMacAddress();
qProcessorArch getProcessorArch();
QString getOSInformation();
QString profilePath();
47 changes: 26 additions & 21 deletions src/gui/src/SslCertificate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include "SslCertificate.h"
#include "Fingerprint.h"
#include "QUtility.h"
#include "common/DataDirectories.h"

#include <QProcess>
#include <QDir>
Expand All @@ -43,13 +43,13 @@ static const char kConfigFile[] = "barrier.conf";
SslCertificate::SslCertificate(QObject *parent) :
QObject(parent)
{
m_ProfileDir = profilePath();
if (m_ProfileDir.isEmpty()) {
m_ProfileDir = DataDirectories::profile();
if (m_ProfileDir.empty()) {
emit error(tr("Failed to get profile directory."));
}
}

std::pair<bool, QString> SslCertificate::runTool(const QStringList& args)
std::pair<bool, std::string> SslCertificate::runTool(const QStringList& args)
{
QString program;
#if defined(Q_OS_WIN)
Expand All @@ -68,15 +68,17 @@ std::pair<bool, QString> SslCertificate::runTool(const QStringList& args)
#endif

QProcess process;
QString standardOutput, standardError;
process.setEnvironment(environment);
process.start(program, args);

bool success = process.waitForStarted();
std::string output;

QString standardError;
if (success && process.waitForFinished())
{
standardOutput = QString::fromLocal8Bit(process.readAllStandardOutput().trimmed());
standardError = QString::fromLocal8Bit(process.readAllStandardError().trimmed());
output = process.readAllStandardOutput().trimmed().toStdString();
standardError = process.readAllStandardError().trimmed();
}

int code = process.exitCode();
Expand All @@ -87,15 +89,15 @@ std::pair<bool, QString> SslCertificate::runTool(const QStringList& args)
.arg(program)
.arg(process.exitCode())
.arg(standardError.isEmpty() ? "Unknown" : standardError));
return {false, standardOutput};
return {false, output};
}

return {true, standardOutput};
return {true, output};
}

void SslCertificate::generateCertificate()
{
auto filename = getCertificatePath();
auto filename = QString::fromStdString(getCertificatePath());

QFile file(filename);
if (!file.exists() || !isCertificateValid(filename)) {
Expand All @@ -120,7 +122,7 @@ void SslCertificate::generateCertificate()
arguments.append("-newkey");
arguments.append("rsa:2048");

QDir sslDir(getCertificateDirectory());
QDir sslDir(QString::fromStdString(getCertificateDirectory()));
if (!sslDir.exists()) {
sslDir.mkpath(".");
}
Expand Down Expand Up @@ -157,32 +159,35 @@ void SslCertificate::generateFingerprint(const QString& certificateFilename)

auto ret = runTool(arguments);
bool success = ret.first;
std::string output = ret.second;

if (!success) {
return;
}

// find the fingerprint from the tool output
QString fingerprint = ret.second;
auto i = fingerprint.indexOf('=');
if (i != -1) {
fingerprint.remove(0, i+1);
auto i = output.find_first_of('=');
if (i != std::string::npos) {
i++;
auto fingerprint = output.substr(
i, output.size() - i);

Fingerprint::local().trust(fingerprint, false);
Fingerprint::local().trust(QString::fromStdString(fingerprint), false);
emit info(tr("SSL fingerprint generated."));
}
else {
emit error(tr("Failed to find SSL fingerprint."));
}
}

QString SslCertificate::getCertificatePath()
std::string SslCertificate::getCertificatePath()
{
return getCertificateDirectory() + QDir::separator() + kCertificateFilename;
return getCertificateDirectory() + QDir::separator().toLatin1() + kCertificateFilename;
}

QString SslCertificate::getCertificateDirectory()
std::string SslCertificate::getCertificateDirectory()
{
return m_ProfileDir + QDir::separator() + kSslDir;
return m_ProfileDir + QDir::separator().toLatin1() + kSslDir;
}

bool SslCertificate::isCertificateValid(const QString& path)
Expand All @@ -193,7 +198,7 @@ bool SslCertificate::isCertificateValid(const QString& path)

BIO* bio = BIO_new(BIO_s_file());

auto ret = BIO_read_filename(bio, path.toLocal8Bit().constData());
auto ret = BIO_read_filename(bio, path.toStdString().c_str());
if (!ret) {
emit info(tr("Could not read from default certificate file."));
BIO_free_all(bio);
Expand Down
8 changes: 4 additions & 4 deletions src/gui/src/SslCertificate.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public slots:
void generateFinished();

private:
std::pair<bool, QString> runTool(const QStringList& args);
std::pair<bool, std::string> runTool(const QStringList& args);
void generateFingerprint(const QString& certificateFilename);

QString getCertificatePath();
QString getCertificateDirectory();
std::string getCertificatePath();
std::string getCertificateDirectory();

bool isCertificateValid(const QString& path);
private:
QString m_ProfileDir;
std::string m_ProfileDir;
};
3 changes: 1 addition & 2 deletions src/lib/barrier/win32/DaemonApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "base/log_outputters.h"
#include "base/Log.h"
#include "common/DataDirectories.h"
#include "base/Unicode.h"

#include "arch/win32/ArchMiscWindows.h"
#include "arch/win32/XArchWindows.h"
Expand Down Expand Up @@ -258,7 +257,7 @@ DaemonApp::handleIpcMessage(const Event& e, void*)
switch (m->type()) {
case kIpcCommand: {
IpcCommandMessage* cm = static_cast<IpcCommandMessage*>(m);
String command = Unicode::UTF8ToText(cm->command());
String command = cm->command();

// if empty quotes, clear.
if (command == "\"\"") {
Expand Down
28 changes: 25 additions & 3 deletions src/lib/common/win32/DataDirectories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,34 @@
*/

#include "../DataDirectories.h"
#include "KnownFolderPaths.h"

#include <Shlobj.h>

std::string unicode_to_mb(const WCHAR* utfStr)
{
int utfLength = lstrlenW(utfStr);
int mbLength = WideCharToMultiByte(CP_UTF8, 0, utfStr, utfLength, NULL, 0, NULL, NULL);
std::string mbStr(mbLength, 0);
WideCharToMultiByte(CP_UTF8, 0, utfStr, utfLength, &mbStr[0], mbLength, NULL, NULL);
return mbStr;
}

std::string known_folder_path(const KNOWNFOLDERID& id)
{
std::string path;
WCHAR* buffer;
HRESULT result = SHGetKnownFolderPath(id, 0, NULL, &buffer);
if (result == S_OK) {
path = unicode_to_mb(buffer);
CoTaskMemFree(buffer);
}
return path;
}

const std::string& DataDirectories::profile()
{
if (_profile.empty())
_profile = localAppDataPath() + "\\Barrier";
_profile = known_folder_path(FOLDERID_LocalAppData) + "\\Barrier";
return _profile;
}
const std::string& DataDirectories::profile(const std::string& path)
Expand All @@ -33,7 +55,7 @@ const std::string& DataDirectories::profile(const std::string& path)
const std::string& DataDirectories::global()
{
if (_global.empty())
_global = programDataPath() + "\\Barrier";
_global = known_folder_path(FOLDERID_ProgramData) + "\\Barrier";
return _global;
}
const std::string& DataDirectories::global(const std::string& path)
Expand Down
Loading

0 comments on commit bd6c490

Please sign in to comment.