Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
utils: Convert Windows args to utf-8 string
  • Loading branch information
ken2812221 committed Sep 30, 2018
1 parent 9b8bb5f commit 380c843
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/bitcoin-cli.cpp
Expand Up @@ -17,6 +17,7 @@

#include <memory>
#include <stdio.h>
#include <tuple>

#include <event2/buffer.h>
#include <event2/keyvalq_struct.h>
Expand Down Expand Up @@ -511,6 +512,10 @@ static int CommandLineRPC(int argc, char *argv[])

int main(int argc, char* argv[])
{
#ifdef WIN32
util::WinCmdLineArgs winArgs;
std::tie(argc, argv) = winArgs.get();
#endif
SetupEnvironment();
if (!SetupNetworking()) {
fprintf(stderr, "Error: Initializing networking failed\n");
Expand Down
4 changes: 4 additions & 0 deletions src/bitcoind.cpp
Expand Up @@ -185,6 +185,10 @@ static bool AppInit(int argc, char* argv[])

int main(int argc, char* argv[])
{
#ifdef WIN32
util::WinCmdLineArgs winArgs;
std::tie(argc, argv) = winArgs.get();
#endif
SetupEnvironment();

// Connect bitcoind signal handlers
Expand Down
4 changes: 4 additions & 0 deletions src/qt/bitcoin.cpp
Expand Up @@ -552,6 +552,10 @@ static void SetupUIArgs()
#ifndef BITCOIN_QT_TEST
int main(int argc, char *argv[])
{
#ifdef WIN32
util::WinCmdLineArgs winArgs;
std::tie(argc, argv) = winArgs.get();
#endif
SetupEnvironment();

std::unique_ptr<interfaces::Node> node = interfaces::MakeNode();
Expand Down
32 changes: 32 additions & 0 deletions src/util.cpp
Expand Up @@ -61,6 +61,7 @@
#include <codecvt>

#include <io.h> /* for _commit */
#include <shellapi.h>
#include <shlobj.h>
#endif

Expand Down Expand Up @@ -1200,6 +1201,10 @@ void SetupEnvironment()
} catch (const std::runtime_error&) {
setenv("LC_ALL", "C", 1);
}
#elif defined(WIN32)
// Set the default input/output charset is utf-8
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
#endif
// The path locale is lazy initialized and to avoid deinitialization errors
// in multithreading environments, it is set explicitly by the main thread.
Expand Down Expand Up @@ -1265,3 +1270,30 @@ int ScheduleBatchPriority()
return 1;
#endif
}

namespace util {
#ifdef WIN32
WinCmdLineArgs::WinCmdLineArgs()
{
wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utf8_cvt;
argv = new char*[argc];
args.resize(argc);
for (int i = 0; i < argc; i++) {
args[i] = utf8_cvt.to_bytes(wargv[i]);
argv[i] = &*args[i].begin();
}
LocalFree(wargv);
}

WinCmdLineArgs::~WinCmdLineArgs()
{
delete[] argv;
}

std::pair<int, char**> WinCmdLineArgs::get()
{
return std::make_pair(argc, argv);
}
#endif
} // namespace util
16 changes: 16 additions & 0 deletions src/util.h
Expand Up @@ -29,6 +29,7 @@
#include <stdint.h>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>

#include <boost/thread/condition_variable.hpp> // for boost::thread_interrupted
Expand Down Expand Up @@ -361,6 +362,21 @@ inline void insert(std::set<TsetT>& dst, const Tsrc& src) {
dst.insert(src.begin(), src.end());
}

#ifdef WIN32
class WinCmdLineArgs
{
public:
WinCmdLineArgs();
~WinCmdLineArgs();
std::pair<int, char**> get();

private:
int argc;
char** argv;
std::vector<std::string> args;
};
#endif

} // namespace util

#endif // BITCOIN_UTIL_H
2 changes: 1 addition & 1 deletion test/functional/feature_uacomment.py
Expand Up @@ -31,7 +31,7 @@ def run_test(self):
self.nodes[0].assert_start_raises_init_error(["-uacomment=" + 'a' * 256], expected, match=ErrorMatch.FULL_REGEX)

self.log.info("test -uacomment unsafe characters")
for unsafe_char in ['/', ':', '(', ')']:
for unsafe_char in ['/', ':', '(', ')', '₿', '🏃']:
expected = "Error: User Agent comment \(" + re.escape(unsafe_char) + "\) contains unsafe characters."
self.nodes[0].assert_start_raises_init_error(["-uacomment=" + unsafe_char], expected, match=ErrorMatch.FULL_REGEX)

Expand Down

0 comments on commit 380c843

Please sign in to comment.