Skip to content

Commit

Permalink
Gives ANSI path to curl CURLOPT_CAINFO
Browse files Browse the repository at this point in the history
  • Loading branch information
RipleyTom committed Mar 27, 2020
1 parent 2aac46e commit 39cb4f8
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
41 changes: 41 additions & 0 deletions Utilities/StrFmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,47 @@
#include <errno.h>
#endif

#ifdef _WIN32
std::string wchar_to_utf8(wchar_t *src)
{
std::string utf8_string;
auto tmp_size = WideCharToMultiByte(CP_UTF8, 0, buf_short.data(), -1, nullptr, 0, nullptr, nullptr);
utf8_string.resize(tmp_size);
WideCharToMultiByte(CP_UTF8, 0, buf_short.data(), -1, utf8_string.data(), tmp_size, nullptr, nullptr);
return utf8_string;
}

std::string wchar_path_to_ansi_path(const std::wstring& src)
{
std::wstring buf_short;
std::string buf_final;

// Get the short path from the wide char path(short path should only contain ansi characters)
tmp_size = GetShortPathNameW(src.data(), nullptr, 0);
buf_short.resize(tmp_size);
GetShortPathNameW(buf_wide.data(), buf_short.data(), tmp_size);

// Convert wide char to ansi
tmp_size = WideCharToMultiByte(CP_ACP, 0, buf_short.data(), -1, nullptr, 0, nullptr, nullptr);
buf_final.resize(tmp_size);
WideCharToMultiByte(CP_ACP, 0, buf_short.data(), -1, buf_final.data(), tmp_size, nullptr, nullptr);

return buf_final;
}

std::string utf8_path_to_ansi_path(const std::string& src)
{
std::wstring buf_wide;

// Converts the utf-8 path to wide char
auto tmp_size = MultiByteToWideChar(CP_UTF8, 0, src.c_str(), -1, nullptr, 0);
buf_wide.resize(tmp_size);
MultiByteToWideChar(CP_UTF8, 0, src.c_str(), -1, buf_wide.data(), tmp_size);

return wchar_path_to_ansi_path(buf_wide);
}
#endif

template <>
void fmt_class_string<std::pair<const fmt_type_info*, u64>>::format(std::string& out, u64 arg)
{
Expand Down
6 changes: 6 additions & 0 deletions Utilities/StrUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#include <functional>
#include <string_view>

#ifdef _WIN32
std::string wchar_to_utf8(wchar_t *src);
std::string wchar_path_to_ansi_path(const std::wstring& src);
std::string utf8_path_to_ansi_path(const std::string& src);
#endif

// Copy null-terminated string from a std::string or a char array to a char array with truncation
template <typename D, typename T>
inline void strcpy_trunc(D& dst, const T& src)
Expand Down
12 changes: 12 additions & 0 deletions rpcs3/Emu/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,18 @@ std::string Emulator::GetHdd1Dir()
return fmt::replace_all(g_cfg.vfs.dev_hdd1, "$(EmulatorDir)", GetEmuDir());
}

#ifdef _WIN32
std::string Emulator:GetExeDir()
{
wchar_t buffer[32767];
GetModuleFileNameW(nullptr, buffer, sizeof(buffer)/2);

std::string path_to_exe = wchar_to_utf8(buffer);
size_t marker = path_to_exe.find_last_of("/\\");
return last == std::string::npos ? "" : path_to_exe.substr(0, last+1);
}
#endif

std::string Emulator::GetSfoDirFromGamePath(const std::string& game_path, const std::string& user, const std::string& title_id)
{
if (fs::is_file(game_path + "/PS3_DISC.SFB"))
Expand Down
4 changes: 4 additions & 0 deletions rpcs3/Emu/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ class Emulator final
void LimitCacheSize();

public:
#ifdef _WIN32
static std::string GetExeDir();
#endif

static std::string GetEmuDir();
static std::string GetHddDir();
static std::string GetHdd1Dir();
Expand Down
10 changes: 8 additions & 2 deletions rpcs3/rpcs3qt/curl_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
#include "curl_handle.h"
#include "Emu/System.h"

#ifdef _WIN32
#include "Utilities/StrUtil.h"
#endif

curl_handle::curl_handle(QObject* parent) : QObject(parent)
{
m_curl = curl_easy_init();

#ifdef _WIN32
// This shouldn't be needed on linux
const std::string path_to_cert = Emulator::GetEmuDir() + "cacert.pem";
curl_easy_setopt(m_curl, CURLOPT_CAINFO, path_to_cert.c_str());
const std::string path_to_cert = Emulator::GetExeDir() + "cacert.pem";
const std::string ansi_path = utf8_path_to_ansi_path(path_to_cert);

curl_easy_setopt(m_curl, CURLOPT_CAINFO, ansi_path.data());
#endif
}

Expand Down

0 comments on commit 39cb4f8

Please sign in to comment.