aboutsummaryrefslogtreecommitdiff
path: root/client/src/util/util.cpp
blob: 1847780ec40214174e3ab15ab49398958213551e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "../include.h"
#include "util.h"
#include "io.h"
#include "syscalls.h"

std::string util::wide_to_multibyte(const std::wstring& str) {
	std::string ret;
	size_t str_len;

	// check if not empty str
	if (str.empty())
		return{};

	// count size
	str_len = WideCharToMultiByte(CP_UTF8, 0, &str[0], str.size(), 0, 0, 0, 0);

	// setup return value
	ret = std::string(str_len, 0);

	// final conversion
	WideCharToMultiByte(CP_UTF8, 0, &str[0], str.size(), &ret[0], str_len, 0, 0);

	return ret;
}

std::wstring util::multibyte_to_wide(const std::string& str) {
	size_t      size;
	std::wstring out;

	// get size
	size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.size() + 1, 0, 0);

	out.resize(size);

	// finally convert
	MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.size() + 1, &out[0], size);

	return out;
}

bool util::close_handle(HANDLE handle) {
	if (!handle) {
		io::log_error("invalid handle specified to close.");
		return false;
	}

	static auto nt_close = g_syscalls.get<native::NtClose>("NtClose");

	auto status = nt_close(handle);
	if (!NT_SUCCESS(status)) {
		io::log_error("failed to close {}, status {:#X}.", handle, (status & 0xFFFFFFFF));
		return false;
	}

	return true;
}