🌏 UTF8NoBOM Support

This commit is contained in:
bitcookies 2025-06-29 10:26:43 +08:00
parent f037df898c
commit f84a00c672
5 changed files with 58 additions and 51 deletions

View file

@ -1,71 +1,78 @@
#include <tchar.h>
#include <stdio.h>
#include <windows.h> #include <windows.h>
#include <fcntl.h>
#include <io.h>
#include <locale.h> #include <locale.h>
#include <iostream>
#include <string>
#include <codecvt>
#include "WinRarConfig.hpp" #include "WinRarConfig.hpp"
#include "WinRarKeygen.hpp" #include "WinRarKeygen.hpp"
#include <system_error> #include <system_error>
std::string WideToUtf8(const std::wstring& wstr) {
if (wstr.empty()) return {};
int size = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), nullptr, 0, nullptr, nullptr);
std::string result(size, 0);
WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), &result[0], size, nullptr, nullptr);
return result;
}
std::wstring Utf8ToWide(const std::string& str) {
if (str.empty()) return {};
int size = MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), nullptr, 0);
std::wstring result(size, 0);
MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), &result[0], size);
return result;
}
void Help() { void Help() {
_putts(TEXT("Please use the following command:\n")); std::wcout << L"Please use the following command:\n";
_putts(TEXT("Usage:")); std::wcout << L"Usage:\n";
_putts(TEXT(" winrar-keygen.exe <Username> <License Name>")); std::wcout << L" winrar-keygen.exe <Username> <License Name>\n\n";
_putts(TEXT("")); std::wcout << L"Example:\n";
_putts(TEXT("Example:")); std::wcout << L" winrar-keygen.exe \"GitHub\" \"Single PC usage license\"\n";
_putts(TEXT(" winrar-keygen.exe \"Github\" \"Single PC usage license\"\n"));
} }
void PrintRegisterInfo(const WinRarKeygen<WinRarConfig>::RegisterInfo& Info) { void PrintRegisterInfo(const WinRarKeygen<WinRarConfig>::RegisterInfo& Info) {
_tprintf_s(TEXT("%hs\n"), "RAR registration data"); std::wstring user = Utf8ToWide(Info.UserName);
_tprintf_s(TEXT("%hs\n"), Info.UserName.c_str()); std::wstring license = Utf8ToWide(Info.LicenseType);
_tprintf_s(TEXT("%hs\n"), Info.LicenseType.c_str()); std::wstring uid = Utf8ToWide(Info.UID);
_tprintf_s(TEXT("UID=%hs\n"), Info.UID.c_str()); std::wstring data = Utf8ToWide(Info.HexData);
for (size_t i = 0; i < Info.HexData.length(); i += 54) {
_tprintf_s(TEXT("%.54hs\n"), Info.HexData.c_str() + i); std::wcout << L"RAR registration data\n";
std::wcout << user << L"\n";
std::wcout << license << L"\n";
std::wcout << L"UID=" << uid << L"\n";
for (size_t i = 0; i < data.length(); i += 54) {
std::wcout << data.substr(i, 54) << L"\n";
} }
} }
std::string ToACP(PCWSTR lpszUnicodeString) { int wmain(int argc, wchar_t* argv[]) {
int len; SetConsoleOutputCP(CP_UTF8); // powershell code page set to 65001
if (_setmode(_fileno(stdout), _O_U8TEXT) == -1) {
len = WideCharToMultiByte(CP_ACP, 0, lpszUnicodeString, -1, NULL, 0, NULL, NULL); std::wcerr << L"Failed to set console to UTF-8 output mode.\n";
if (len == 0) {
auto err = GetLastError();
throw std::system_error(err, std::system_category());
} }
std::string Result(len, '\x00');
len = WideCharToMultiByte(CP_ACP, 0, lpszUnicodeString, -1, Result.data(), static_cast<int>(Result.length()), NULL, NULL);
if (len == 0) {
auto err = GetLastError();
throw std::system_error(err, std::system_category());
}
while (Result.back() == '\x00') {
Result.pop_back();
}
return Result;
}
int _tmain(int argc, PTSTR argv[]) {
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
if (argc == 3) { if (argc == 3) {
try { try {
std::string user = WideToUtf8(argv[1]);
std::string license = WideToUtf8(argv[2]);
PrintRegisterInfo( PrintRegisterInfo(
#if defined(_UNICODE) || defined(UNICODE) WinRarKeygen<WinRarConfig>::GenerateRegisterInfo(user.c_str(), license.c_str())
WinRarKeygen<WinRarConfig>::GenerateRegisterInfo(ToACP(argv[1]).c_str(), ToACP(argv[2]).c_str())
#else
WinRarKeygen<WinRarConfig>::GenerateRegisterInfo(argv[1], argv[2])
#endif
); );
} catch (std::exception& e) { }
_tprintf_s(TEXT("%hs\n"), e.what()); catch (std::exception& e) {
std::wcerr << L"Error: " << e.what() << L"\n";
return -1; return -1;
} }
} else { }
else {
Help(); Help();
} }
return 0; return 0;
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -51,8 +51,8 @@ END
// //
VS_VERSION_INFO VERSIONINFO VS_VERSION_INFO VERSIONINFO
FILEVERSION 3,0,0,0 FILEVERSION 3,1,0,0
PRODUCTVERSION 3,0,0,0 PRODUCTVERSION 3,1,0,0
FILEFLAGSMASK 0x3fL FILEFLAGSMASK 0x3fL
#ifdef _DEBUG #ifdef _DEBUG
FILEFLAGS 0x1L FILEFLAGS 0x1L
@ -69,12 +69,12 @@ BEGIN
BEGIN BEGIN
VALUE "CompanyName", "WinRAR Keygen" VALUE "CompanyName", "WinRAR Keygen"
VALUE "FileDescription", "WinRAR Key Generation Tool" VALUE "FileDescription", "WinRAR Key Generation Tool"
VALUE "FileVersion", "3.0.0.0" VALUE "FileVersion", "3.1.0.0"
VALUE "InternalName", "winrar-keygen.exe" VALUE "InternalName", "winrar-keygen.exe"
VALUE "LegalCopyright", "Copyright (C) 2024 MIT License" VALUE "LegalCopyright", "Copyright (C) 2021 Bitcookies"
VALUE "OriginalFilename", "winrar-keygen.exe" VALUE "OriginalFilename", "winrar-keygen.exe"
VALUE "ProductName", "WinRAR Keygen" VALUE "ProductName", "WinRAR Keygen"
VALUE "ProductVersion", "3.0.0.0" VALUE "ProductVersion", "3.1.0.0"
END END
END END
BLOCK "VarFileInfo" BLOCK "VarFileInfo"