mutil.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. #include "mutil.h"
  2. #include <Windows.h>
  3. #include <shellapi.h>
  4. #include <commdlg.h>
  5. #include <shtypes.h>
  6. #include <shobjidl_core.h>
  7. #include <shlobj_core.h>
  8. #include <sstream>
  9. #include <string>
  10. #include <iomanip>
  11. #include <codecvt>
  12. #include <filesystem>
  13. #include <iostream>
  14. namespace mutil {
  15. std::vector<uint8_t> from_hex_string(std::string string)
  16. {
  17. std::vector<uint8_t> ret;
  18. auto HexCharToByte = [](char ch)
  19. {
  20. if (ch <= 0x40)
  21. return ch - 48;
  22. else
  23. return ch - 55;
  24. };
  25. for (int i = 0; i < string.length(); i += 2)
  26. {
  27. char firstchar = HexCharToByte(string[i]);
  28. char secondchar = HexCharToByte(string[i + 1]);
  29. char result = (16 * firstchar) | secondchar;
  30. ret.push_back(result);
  31. }
  32. return ret;
  33. }
  34. std::string to_hex_string(uint8_t* barray, int length)
  35. {
  36. if (barray == nullptr || length == 0)
  37. return std::string();
  38. std::stringstream stream;
  39. for (size_t i = 0; i < length; i++)
  40. stream << std::setfill('0') << std::setw(2) << std::hex << (int)barray[i];
  41. return stream.str();
  42. }
  43. bool IsLittleEndian()
  44. {
  45. unsigned int i = 1;
  46. char* c = (char*)&i;
  47. return (*c);
  48. }
  49. static std::filesystem::path _currentPath;
  50. void SetCurrentPath(const std::filesystem::path& current_path)
  51. {
  52. _currentPath = current_path;
  53. }
  54. std::filesystem::path GetCurrentPath()
  55. {
  56. return _currentPath;
  57. }
  58. std::optional<std::string> SelectDirectory(const char* title)
  59. {
  60. auto currPath = std::filesystem::current_path();
  61. if (!SUCCEEDED(CoInitialize(nullptr)))
  62. return {};
  63. IFileDialog* pfd;
  64. if (!SUCCEEDED(CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd))))
  65. return {};
  66. const size_t titleSize = strlen(title) + 1;
  67. wchar_t* wcTitle = new wchar_t[titleSize];
  68. mbstowcs(wcTitle, title, titleSize);
  69. DWORD dwOptions;
  70. IShellItem* psi;
  71. if (!SUCCEEDED(pfd->GetOptions(&dwOptions)) ||
  72. !SUCCEEDED(pfd->SetOptions(dwOptions | FOS_PICKFOLDERS)) ||
  73. !SUCCEEDED(pfd->SetTitle(wcTitle)) ||
  74. !SUCCEEDED(pfd->Show(NULL)) ||
  75. !SUCCEEDED(pfd->GetResult(&psi)))
  76. {
  77. pfd->Release();
  78. return {};
  79. }
  80. WCHAR* folderName;
  81. if (!SUCCEEDED(psi->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &folderName)))
  82. {
  83. pfd->Release();
  84. psi->Release();
  85. return {};
  86. }
  87. pfd->Release();
  88. psi->Release();
  89. std::filesystem::current_path(currPath);
  90. std::u16string u16(reinterpret_cast<const char16_t*>(folderName));
  91. return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u16);
  92. }
  93. std::optional<std::string> SelectFile(const char* filter, const char* title)
  94. {
  95. auto currPath = std::filesystem::current_path();
  96. // common dialog box structure, setting all fields to 0 is important
  97. OPENFILENAME ofn = { 0 };
  98. TCHAR szFile[260] = { 0 };
  99. // Initialize remaining fields of OPENFILENAME structure
  100. ofn.lStructSize = sizeof(ofn);
  101. ofn.hwndOwner = NULL;
  102. ofn.lpstrFile = szFile;
  103. ofn.nMaxFile = sizeof(szFile);
  104. ofn.lpstrFilter = filter;
  105. ofn.lpstrTitle = title;
  106. ofn.nFilterIndex = 1;
  107. ofn.lpstrFileTitle = NULL;
  108. ofn.nMaxFileTitle = 0;
  109. ofn.lpstrInitialDir = NULL;
  110. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  111. std::optional<std::string> result = {};
  112. if (GetOpenFileName(&ofn) == TRUE)
  113. result = std::string(szFile);
  114. std::filesystem::current_path(currPath);
  115. return result;
  116. }
  117. int64_t GetCurrentTimeMillisec()
  118. {
  119. return std::chrono::duration_cast<std::chrono::milliseconds>(
  120. std::chrono::system_clock::now().time_since_epoch()).count();
  121. }
  122. std::vector<std::string> StringSplit(const std::string& delimiter, const std::string& content)
  123. {
  124. std::vector<std::string> tokens;
  125. size_t pos = 0;
  126. size_t prevPos = 0;
  127. std::string token;
  128. while ((pos = content.find(delimiter, prevPos)) != std::string::npos)
  129. {
  130. token = content.substr(prevPos, pos - prevPos);
  131. tokens.push_back(token);
  132. prevPos = pos + delimiter.length();
  133. }
  134. tokens.push_back(content.substr(prevPos));
  135. return tokens;
  136. }
  137. std::string SplitWords(const std::string& value)
  138. {
  139. std::stringstream outStream;
  140. std::stringstream inStream(value);
  141. char ch;
  142. inStream >> ch;
  143. outStream << ch;
  144. while (inStream >> ch)
  145. {
  146. if (isupper(ch))
  147. outStream << " ";
  148. outStream << ch;
  149. }
  150. return outStream.str();
  151. }
  152. std::string Unsplit(const std::string& value)
  153. {
  154. std::stringstream in(value);
  155. std::stringstream out;
  156. std::string word;
  157. while (in >> word)
  158. out << word;
  159. return out.str();
  160. }
  161. std::string MakeCapital(std::string value)
  162. {
  163. if (islower(value[0]))
  164. value[0] = toupper(value[0]);
  165. return value;
  166. }
  167. static const std::string base64_chars =
  168. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  169. "abcdefghijklmnopqrstuvwxyz"
  170. "0123456789+/";
  171. static inline bool is_base64(BYTE c)
  172. {
  173. return (isalnum(c) || (c == '+') || (c == '/'));
  174. }
  175. std::string base64_encode(BYTE const* buf, unsigned int bufLen)
  176. {
  177. std::string ret;
  178. int i = 0;
  179. int j = 0;
  180. BYTE char_array_3[3];
  181. BYTE char_array_4[4];
  182. while (bufLen--)
  183. {
  184. char_array_3[i++] = *(buf++);
  185. if (i == 3)
  186. {
  187. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  188. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  189. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  190. char_array_4[3] = char_array_3[2] & 0x3f;
  191. for (i = 0; (i < 4); i++)
  192. ret += base64_chars[char_array_4[i]];
  193. i = 0;
  194. }
  195. }
  196. if (i)
  197. {
  198. for (j = i; j < 3; j++)
  199. char_array_3[j] = '\0';
  200. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  201. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  202. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  203. char_array_4[3] = char_array_3[2] & 0x3f;
  204. for (j = 0; (j < i + 1); j++)
  205. ret += base64_chars[char_array_4[j]];
  206. while ((i++ < 3))
  207. ret += '=';
  208. }
  209. return ret;
  210. }
  211. std::vector<BYTE> base64_decode(std::string const& encoded_string)
  212. {
  213. size_t in_len = encoded_string.size();
  214. int i = 0;
  215. int j = 0;
  216. int in_ = 0;
  217. BYTE char_array_4[4], char_array_3[3];
  218. std::vector<BYTE> ret;
  219. while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_]))
  220. {
  221. char_array_4[i++] = encoded_string[in_];
  222. in_++;
  223. if (i == 4)
  224. {
  225. for (i = 0; i < 4; i++)
  226. char_array_4[i] = static_cast<BYTE>(base64_chars.find(char_array_4[i])); // base64_chars len < 255
  227. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  228. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  229. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  230. for (i = 0; (i < 3); i++)
  231. ret.push_back(char_array_3[i]);
  232. i = 0;
  233. }
  234. }
  235. if (i)
  236. {
  237. for (j = i; j < 4; j++)
  238. char_array_4[j] = 0;
  239. for (j = 0; j < 4; j++)
  240. char_array_4[j] = static_cast<BYTE>(base64_chars.find(char_array_4[j]));
  241. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  242. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  243. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  244. for (j = 0; (j < i - 1); j++) ret.push_back(char_array_3[j]);
  245. }
  246. return ret;
  247. }
  248. int64_t GetTimezoneBias()
  249. {
  250. _TIME_ZONE_INFORMATION timezoneInfo{};
  251. if (GetTimeZoneInformation(&timezoneInfo) == TIME_ZONE_ID_INVALID)
  252. LOG_LAST_ERROR("Failed to get timezone.");
  253. return static_cast<int64_t>(timezoneInfo.Bias) * 60;
  254. }
  255. void OpenConsole()
  256. {
  257. AllocConsole();
  258. freopen_s((FILE**)stdout, "CONOUT$", "w", stdout);
  259. freopen_s((FILE**)stderr, "CONOUT$", "w", stderr);
  260. auto consoleWindow = GetConsoleWindow();
  261. SetForegroundWindow(consoleWindow);
  262. ShowWindow(consoleWindow, SW_RESTORE);
  263. ShowWindow(consoleWindow, SW_SHOW);
  264. }
  265. void OpenURL(const char* url)
  266. {
  267. ShellExecute(nullptr, nullptr, url, nullptr, nullptr, SW_SHOW);
  268. }
  269. std::string SelectFolder(const std::filesystem::path& dir)
  270. {
  271. std::string folderPath;
  272. IFileDialog* pFileDialog;
  273. HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileDialog));
  274. if (SUCCEEDED(hr))
  275. {
  276. DWORD options;
  277. pFileDialog->GetOptions(&options);
  278. pFileDialog->SetOptions(options | FOS_PICKFOLDERS);
  279. if (!dir.empty())
  280. {
  281. PIDLIST_ABSOLUTE pidl;
  282. hr = SHParseDisplayName(dir.c_str(), NULL, &pidl, 0, NULL);
  283. if (SUCCEEDED(hr))
  284. {
  285. IShellItem* pDefaultFolder;
  286. hr = SHCreateShellItem(NULL, NULL, pidl, &pDefaultFolder);
  287. if (SUCCEEDED(hr))
  288. {
  289. pFileDialog->SetFolder(pDefaultFolder);
  290. pDefaultFolder->Release();
  291. }
  292. CoTaskMemFree(pidl);
  293. }
  294. }
  295. hr = pFileDialog->Show(NULL);
  296. if (SUCCEEDED(hr))
  297. {
  298. IShellItem* pItem;
  299. hr = pFileDialog->GetResult(&pItem);
  300. if (SUCCEEDED(hr))
  301. {
  302. PWSTR pszFolderPath;
  303. hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFolderPath);
  304. if (SUCCEEDED(hr))
  305. {
  306. folderPath = std::filesystem::path(pszFolderPath).string();
  307. CoTaskMemFree(pszFolderPath);
  308. }
  309. pItem->Release();
  310. }
  311. }
  312. pFileDialog->Release();
  313. }
  314. return folderPath;
  315. }
  316. void OpenFolder(const std::filesystem::path& folderPath)
  317. {
  318. std::string command = "explorer \"" + folderPath.string() + "\"";
  319. int result = std::system(command.c_str());
  320. if (result == 0)
  321. {
  322. std::cout << "Successfully opened folder." << std::endl;
  323. }
  324. else
  325. {
  326. std::cerr << "Failed to open folder." << std::endl;
  327. }
  328. }
  329. void backup_and_rename_file(std::string filepath, bool restore)
  330. {
  331. const std::filesystem::path file_path(filepath);
  332. std::filesystem::path backup_path = file_path.string() + ".bak";
  333. if (!restore)
  334. {
  335. if (exists(file_path))
  336. std::filesystem::rename(file_path, backup_path);
  337. }
  338. else
  339. {
  340. if (exists(backup_path))
  341. std::filesystem::rename(backup_path, file_path);
  342. }
  343. }
  344. }