mirror of
https://github.com/Nezumi-2711/winrar-keygen.git
synced 2026-09-22 05:41:48 +00:00
🐧 Cross-platform build support
This commit is contained in:
+470
-7
@@ -1,17 +1,35 @@
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include <winhttp.h>
|
||||
#include <fcntl.h>
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <strings.h>
|
||||
#endif
|
||||
|
||||
#include <locale.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <codecvt>
|
||||
#include "WinRarConfig.hpp"
|
||||
#include "WinRarKeygen.hpp"
|
||||
#include <system_error>
|
||||
#include <stdexcept>
|
||||
|
||||
#ifdef _WIN32
|
||||
#pragma comment(lib, "Version.lib")
|
||||
#pragma comment(lib, "winhttp.lib")
|
||||
#endif
|
||||
|
||||
#ifndef APP_VERSION
|
||||
#define APP_VERSION "4.1.0.0"
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
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);
|
||||
@@ -43,20 +61,30 @@ std::string WideToAnsi(const std::wstring& wstr) {
|
||||
&result[0], size, nullptr, nullptr);
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
enum class Encoding { ASCII, ANSI, UTF8 };
|
||||
|
||||
struct Options {
|
||||
#ifdef _WIN32
|
||||
std::wstring username;
|
||||
std::wstring license;
|
||||
Encoding encoding = Encoding::UTF8;
|
||||
std::wstring outputFile = L"rarreg.key";
|
||||
#else
|
||||
std::string username;
|
||||
std::string license;
|
||||
Encoding encoding = Encoding::UTF8;
|
||||
std::string outputFile = "rarreg.key";
|
||||
#endif
|
||||
bool textOnly = false;
|
||||
bool activate = false;
|
||||
bool showVersion = false;
|
||||
bool showHelp = false;
|
||||
bool checkUpdate = false;
|
||||
};
|
||||
|
||||
#ifdef _WIN32
|
||||
bool ParseArguments(int argc, wchar_t* argv[], Options& opts) {
|
||||
std::vector<std::wstring> positional;
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
@@ -73,6 +101,9 @@ bool ParseArguments(int argc, wchar_t* argv[], Options& opts) {
|
||||
if (arg == L"-a" || arg == L"--activate") {
|
||||
opts.activate = true; continue;
|
||||
}
|
||||
if (arg == L"-u" || arg == L"--update") {
|
||||
opts.checkUpdate = true; return true;
|
||||
}
|
||||
if (arg == L"-e" || arg == L"--encoding") {
|
||||
if (++i >= argc) {
|
||||
std::wcerr << L"Error: Missing value for " << arg << L"\n";
|
||||
@@ -114,7 +145,70 @@ bool ParseArguments(int argc, wchar_t* argv[], Options& opts) {
|
||||
<< positional.size() << L"\n";
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
bool ParseArguments(int argc, char* argv[], Options& opts) {
|
||||
std::vector<std::string> positional;
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
std::string arg = argv[i];
|
||||
if (arg == "-v" || arg == "--version" || strcasecmp(arg.c_str(), "ver") == 0) {
|
||||
opts.showVersion = true; return true;
|
||||
}
|
||||
if (arg == "-h" || arg == "--help" || strcasecmp(arg.c_str(), "help") == 0) {
|
||||
opts.showHelp = true; return true;
|
||||
}
|
||||
if (arg == "-t" || arg == "--text") {
|
||||
opts.textOnly = true; continue;
|
||||
}
|
||||
if (arg == "-a" || arg == "--activate") {
|
||||
opts.activate = true; continue;
|
||||
}
|
||||
if (arg == "-u" || arg == "--update") {
|
||||
opts.checkUpdate = true; return true;
|
||||
}
|
||||
if (arg == "-e" || arg == "--encoding") {
|
||||
if (++i >= argc) {
|
||||
std::cerr << "Error: Missing value for " << arg << "\n";
|
||||
return false;
|
||||
}
|
||||
std::string val = argv[i];
|
||||
if (strcasecmp(val.c_str(), "ascii") == 0) opts.encoding = Encoding::ASCII;
|
||||
else if (strcasecmp(val.c_str(), "ansi") == 0) opts.encoding = Encoding::ANSI;
|
||||
else if (strcasecmp(val.c_str(), "utf8") == 0 || strcasecmp(val.c_str(), "utf-8") == 0)
|
||||
opts.encoding = Encoding::UTF8;
|
||||
else {
|
||||
std::cerr << "Error: Unknown encoding '" << val << "'. Use: ascii, ansi, utf8\n";
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (arg == "-o" || arg == "--output") {
|
||||
if (++i >= argc) {
|
||||
std::cerr << "Error: Missing value for " << arg << "\n";
|
||||
return false;
|
||||
}
|
||||
opts.outputFile = argv[i]; continue;
|
||||
}
|
||||
if (!arg.empty() && arg[0] == '-') {
|
||||
std::cerr << "Error: Unknown option '" << arg << "'\n";
|
||||
return false;
|
||||
}
|
||||
positional.push_back(arg);
|
||||
}
|
||||
if (positional.size() == 2) {
|
||||
opts.username = positional[0];
|
||||
opts.license = positional[1];
|
||||
return true;
|
||||
}
|
||||
if (positional.empty()) {
|
||||
opts.showHelp = true; return true;
|
||||
}
|
||||
std::cerr << "Error: Expected 2 arguments (Username, LicenseName), got "
|
||||
<< positional.size() << "\n";
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
void ShowHelp(const std::wstring& version) {
|
||||
std::wcout << L"WinRAR Keygen v" << version << L"\n\n";
|
||||
std::wcout << L"Usage:\n";
|
||||
@@ -126,17 +220,43 @@ void ShowHelp(const std::wstring& version) {
|
||||
std::wcout << L" -o, --output <file> Output file (default: rarreg.key)\n";
|
||||
std::wcout << L" -a, --activate Write to %APPDATA%\\WinRAR\\rarreg.key\n";
|
||||
std::wcout << L" -t, --text Print to console only, don't write file\n";
|
||||
std::wcout << L" -u, --update Check for updates on GitHub\n";
|
||||
std::wcout << L" -v, --version Show version\n";
|
||||
std::wcout << L" -h, --help Show this help\n\n";
|
||||
std::wcout << L"Examples:\n";
|
||||
std::wcout << L" winrar-keygen.exe \"Github\" \"Single PC usage license\"\n";
|
||||
std::wcout << L" winrar-keygen.exe \"Github\" \"Single PC usage license\" -e ansi\n";
|
||||
std::wcout << L" winrar-keygen.exe \"YourName\" \"Single PC usage license\" -e utf8\n";
|
||||
std::wcout << L" winrar-keygen.exe \"Github\" \"Single PC usage license\" -o \"D:\\keys\\rarreg.key\"\n";
|
||||
std::wcout << L" winrar-keygen.exe \"Github\" \"Single PC usage license\" --activate\n";
|
||||
std::wcout << L" winrar-keygen.exe \"Github\" \"Single PC usage license\" -e ascii\n";
|
||||
std::wcout << L" winrar-keygen.exe \"Github\" \"Single PC usage license\" -a\n";
|
||||
std::wcout << L" winrar-keygen.exe \"Github\" \"Single PC usage license\" -t\n";
|
||||
}
|
||||
#else
|
||||
void ShowHelp(const std::string& version) {
|
||||
std::cout << "WinRAR Keygen v" << version << "\n\n";
|
||||
std::cout << "Usage:\n";
|
||||
std::cout << " winrar-keygen <Username> <LicenseName> [options]\n";
|
||||
std::cout << " winrar-keygen -v | --version\n";
|
||||
std::cout << " winrar-keygen -h | --help\n\n";
|
||||
std::cout << "Options:\n";
|
||||
std::cout << " -e, --encoding <enc> utf8 (default), ascii\n";
|
||||
std::cout << " -o, --output <file> Output file (default: rarreg.key)\n";
|
||||
#ifdef __APPLE__
|
||||
std::cout << " -a, --activate Write to ~/Library/Application Support/com.rarlab.WinRAR/rarreg.key\n";
|
||||
#else
|
||||
std::cout << " -a, --activate Write to ~/.rarkey\n";
|
||||
#endif
|
||||
std::cout << " -t, --text Print to console only, don't write file\n";
|
||||
std::cout << " -u, --update Check for updates on GitHub\n";
|
||||
std::cout << " -v, --version Show version\n";
|
||||
std::cout << " -h, --help Show this help\n\n";
|
||||
std::cout << "Examples:\n";
|
||||
std::cout << " winrar-keygen \"Github\" \"Single PC usage license\"\n";
|
||||
std::cout << " winrar-keygen \"Github\" \"Single PC usage license\" -e ascii\n";
|
||||
std::cout << " winrar-keygen \"Github\" \"Single PC usage license\" -a\n";
|
||||
std::cout << " winrar-keygen \"Github\" \"Single PC usage license\" -t\n";
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
std::wstring GetExecutableVersion() {
|
||||
wchar_t exePath[MAX_PATH] = {};
|
||||
DWORD pathLen = GetModuleFileNameW(nullptr, exePath, MAX_PATH);
|
||||
@@ -169,7 +289,13 @@ std::wstring GetExecutableVersion() {
|
||||
std::to_wstring(HIWORD(fixedInfo->dwFileVersionLS)) + L"." +
|
||||
std::to_wstring(LOWORD(fixedInfo->dwFileVersionLS));
|
||||
}
|
||||
#else
|
||||
std::string GetAppVersion() {
|
||||
return APP_VERSION;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
void PrintRegisterInfo(const WinRarKeygen<WinRarConfig>::RegisterInfo& Info,
|
||||
const std::wstring& wUser, const std::wstring& wLicense) {
|
||||
std::wstring uid = Utf8ToWide(Info.UID);
|
||||
@@ -184,6 +310,19 @@ void PrintRegisterInfo(const WinRarKeygen<WinRarConfig>::RegisterInfo& Info,
|
||||
std::wcout << data.substr(i, 54) << L"\n";
|
||||
}
|
||||
}
|
||||
#else
|
||||
void PrintRegisterInfo(const WinRarKeygen<WinRarConfig>::RegisterInfo& Info,
|
||||
const std::string& user, const std::string& license) {
|
||||
std::cout << "RAR registration data\n";
|
||||
std::cout << user << "\n";
|
||||
std::cout << license << "\n";
|
||||
std::cout << "UID=" << Info.UID << "\n";
|
||||
|
||||
for (size_t i = 0; i < Info.HexData.length(); i += 54) {
|
||||
std::cout << Info.HexData.substr(i, 54) << "\n";
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
std::string BuildRegFileContent(const WinRarKeygen<WinRarConfig>::RegisterInfo& Info) {
|
||||
std::string s;
|
||||
@@ -197,6 +336,7 @@ std::string BuildRegFileContent(const WinRarKeygen<WinRarConfig>::RegisterInfo&
|
||||
return s;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
bool WriteRegFile(const std::wstring& filePath, const std::string& content) {
|
||||
FILE* fp = nullptr;
|
||||
if (_wfopen_s(&fp, filePath.c_str(), L"wb") != 0 || !fp)
|
||||
@@ -205,7 +345,17 @@ bool WriteRegFile(const std::wstring& filePath, const std::string& content) {
|
||||
fclose(fp);
|
||||
return written == content.size();
|
||||
}
|
||||
#else
|
||||
bool WriteRegFile(const std::string& filePath, const std::string& content) {
|
||||
FILE* fp = fopen(filePath.c_str(), "wb");
|
||||
if (!fp) return false;
|
||||
size_t written = fwrite(content.data(), 1, content.size(), fp);
|
||||
fclose(fp);
|
||||
return written == content.size();
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
bool IsConsoleHandle(HANDLE handle) {
|
||||
if (handle == nullptr || handle == INVALID_HANDLE_VALUE) {
|
||||
return false;
|
||||
@@ -238,7 +388,187 @@ void ConfigureConsoleOutput() {
|
||||
std::wcerr << L"Failed to set stderr _O_U8TEXT\n";
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
struct Version {
|
||||
int major = 0, minor = 0, patch = 0;
|
||||
bool valid = false;
|
||||
};
|
||||
|
||||
Version ParseVersion(const std::string& verStr) {
|
||||
Version v;
|
||||
std::string s = verStr;
|
||||
if (!s.empty() && (s[0] == 'v' || s[0] == 'V')) s = s.substr(1);
|
||||
#ifdef _MSC_VER
|
||||
int count = sscanf_s(s.c_str(), "%d.%d.%d", &v.major, &v.minor, &v.patch);
|
||||
#else
|
||||
int count = sscanf(s.c_str(), "%d.%d.%d", &v.major, &v.minor, &v.patch);
|
||||
#endif
|
||||
v.valid = (count >= 2);
|
||||
return v;
|
||||
}
|
||||
|
||||
bool IsNewer(const Version& remote, const Version& local) {
|
||||
if (remote.major != local.major) return remote.major > local.major;
|
||||
if (remote.minor != local.minor) return remote.minor > local.minor;
|
||||
return remote.patch > local.patch;
|
||||
}
|
||||
|
||||
std::string ExtractTagFromJson(const std::string& responseBody) {
|
||||
std::string tagKey = "\"tag_name\"";
|
||||
size_t pos = responseBody.find(tagKey);
|
||||
if (pos == std::string::npos) return "";
|
||||
pos = responseBody.find('\"', pos + tagKey.length());
|
||||
if (pos == std::string::npos) return "";
|
||||
size_t end = responseBody.find('\"', pos + 1);
|
||||
if (end == std::string::npos) return "";
|
||||
return responseBody.substr(pos + 1, end - pos - 1);
|
||||
}
|
||||
|
||||
int CompareAndPrintUpdate(const std::string& currentVersion, const std::string& remoteTag) {
|
||||
Version local = ParseVersion(currentVersion);
|
||||
Version remote = ParseVersion(remoteTag);
|
||||
|
||||
if (!remote.valid) {
|
||||
#ifdef _WIN32
|
||||
std::wcerr << L"Error: Could not parse remote version '" << Utf8ToWide(remoteTag) << L"'.\n";
|
||||
#else
|
||||
std::cerr << "Error: Could not parse remote version '" << remoteTag << "'.\n";
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (IsNewer(remote, local)) {
|
||||
#ifdef _WIN32
|
||||
std::wcout << L"\n New version available: " << Utf8ToWide(remoteTag)
|
||||
<< L" (current: v" << Utf8ToWide(currentVersion) << L")\n";
|
||||
std::wcout << L" Download: https://github.com/bitcookies/winrar-keygen/releases/latest\n\n";
|
||||
#else
|
||||
std::cout << "\n New version available: " << remoteTag
|
||||
<< " (current: v" << currentVersion << ")\n";
|
||||
std::cout << " Download: https://github.com/bitcookies/winrar-keygen/releases/latest\n\n";
|
||||
#endif
|
||||
} else {
|
||||
#ifdef _WIN32
|
||||
std::wcout << L"Already up to date. (v" << Utf8ToWide(currentVersion) << L")\n";
|
||||
#else
|
||||
std::cout << "Already up to date. (v" << currentVersion << ")\n";
|
||||
#endif
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
int CheckForUpdate(const std::string& currentVersion) {
|
||||
std::wcout << L"Checking for updates...\n";
|
||||
|
||||
HINTERNET hSession = WinHttpOpen(L"winrar-keygen-updater",
|
||||
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
|
||||
if (!hSession) {
|
||||
std::wcerr << L"Error: Failed to initialize WinHTTP.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
HINTERNET hConnect = WinHttpConnect(hSession, L"api.github.com",
|
||||
INTERNET_DEFAULT_HTTPS_PORT, 0);
|
||||
if (!hConnect) {
|
||||
std::wcerr << L"Error: Failed to connect to api.github.com.\n";
|
||||
WinHttpCloseHandle(hSession);
|
||||
return -1;
|
||||
}
|
||||
|
||||
HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"GET",
|
||||
L"/repos/bitcookies/winrar-keygen/releases/latest",
|
||||
nullptr, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE);
|
||||
if (!hRequest) {
|
||||
std::wcerr << L"Error: Failed to create HTTP request.\n";
|
||||
WinHttpCloseHandle(hConnect);
|
||||
WinHttpCloseHandle(hSession);
|
||||
return -1;
|
||||
}
|
||||
|
||||
BOOL bResult = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0,
|
||||
WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
|
||||
if (!bResult) {
|
||||
std::wcerr << L"Error: Failed to send HTTP request. Check your network connection.\n";
|
||||
WinHttpCloseHandle(hRequest);
|
||||
WinHttpCloseHandle(hConnect);
|
||||
WinHttpCloseHandle(hSession);
|
||||
return -1;
|
||||
}
|
||||
|
||||
bResult = WinHttpReceiveResponse(hRequest, nullptr);
|
||||
if (!bResult) {
|
||||
std::wcerr << L"Error: No response from GitHub API.\n";
|
||||
WinHttpCloseHandle(hRequest);
|
||||
WinHttpCloseHandle(hConnect);
|
||||
WinHttpCloseHandle(hSession);
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string responseBody;
|
||||
DWORD dwSize = 0;
|
||||
DWORD dwDownloaded = 0;
|
||||
do {
|
||||
dwSize = 0;
|
||||
WinHttpQueryDataAvailable(hRequest, &dwSize);
|
||||
if (dwSize == 0) break;
|
||||
std::vector<char> buffer(dwSize);
|
||||
WinHttpReadData(hRequest, buffer.data(), dwSize, &dwDownloaded);
|
||||
responseBody.append(buffer.data(), dwDownloaded);
|
||||
} while (dwSize > 0);
|
||||
|
||||
WinHttpCloseHandle(hRequest);
|
||||
WinHttpCloseHandle(hConnect);
|
||||
WinHttpCloseHandle(hSession);
|
||||
|
||||
if (responseBody.empty()) {
|
||||
std::wcerr << L"Error: Empty response from GitHub API.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string remoteTag = ExtractTagFromJson(responseBody);
|
||||
if (remoteTag.empty()) {
|
||||
std::wcerr << L"Error: Could not find version info in GitHub response.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
return CompareAndPrintUpdate(currentVersion, remoteTag);
|
||||
}
|
||||
#else
|
||||
int CheckForUpdate(const std::string& currentVersion) {
|
||||
std::cout << "Checking for updates...\n";
|
||||
|
||||
FILE* pipe = popen("curl -s https://api.github.com/repos/bitcookies/winrar-keygen/releases/latest 2>/dev/null", "r");
|
||||
if (!pipe) {
|
||||
std::cerr << "Error: Failed to run curl. Make sure curl is installed.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string responseBody;
|
||||
char buf[4096];
|
||||
while (fgets(buf, sizeof(buf), pipe)) {
|
||||
responseBody += buf;
|
||||
}
|
||||
int status = pclose(pipe);
|
||||
|
||||
if (status != 0 || responseBody.empty()) {
|
||||
std::cerr << "Error: Failed to fetch update info. Check your network connection.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string remoteTag = ExtractTagFromJson(responseBody);
|
||||
if (remoteTag.empty()) {
|
||||
std::cerr << "Error: Could not find version info in GitHub response.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
return CompareAndPrintUpdate(currentVersion, remoteTag);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
int wmain(int argc, wchar_t* argv[]) {
|
||||
ConfigureConsoleOutput();
|
||||
|
||||
@@ -248,6 +578,7 @@ int wmain(int argc, wchar_t* argv[]) {
|
||||
}
|
||||
|
||||
std::wstring version = GetExecutableVersion();
|
||||
std::string versionUtf8 = WideToUtf8(version);
|
||||
|
||||
if (opts.showVersion) {
|
||||
std::wcout << L"winrar-keygen v" << version << L"\n";
|
||||
@@ -257,6 +588,9 @@ int wmain(int argc, wchar_t* argv[]) {
|
||||
ShowHelp(version);
|
||||
return 0;
|
||||
}
|
||||
if (opts.checkUpdate) {
|
||||
return CheckForUpdate(versionUtf8);
|
||||
}
|
||||
|
||||
if (opts.activate && opts.outputFile != L"rarreg.key") {
|
||||
std::wcerr << L"Error: --activate and -o cannot be used together.\n";
|
||||
@@ -271,7 +605,7 @@ int wmain(int argc, wchar_t* argv[]) {
|
||||
wchar_t appdata[MAX_PATH] = {};
|
||||
DWORD len = ExpandEnvironmentStringsW(L"%APPDATA%\\WinRAR", appdata, MAX_PATH);
|
||||
if (len == 0 || len > MAX_PATH) {
|
||||
std::wcerr << L"Error: Failed to resolve %%APPDATA%% path.\n";
|
||||
std::wcerr << L"Error: Failed to resolve %APPDATA% path.\n";
|
||||
return -1;
|
||||
}
|
||||
CreateDirectoryW(appdata, nullptr);
|
||||
@@ -355,3 +689,132 @@ int wmain(int argc, wchar_t* argv[]) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
int main(int argc, char* argv[]) {
|
||||
Options opts;
|
||||
if (!ParseArguments(argc, argv, opts)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string version = GetAppVersion();
|
||||
|
||||
if (opts.showVersion) {
|
||||
std::cout << "winrar-keygen v" << version << "\n";
|
||||
return 0;
|
||||
}
|
||||
if (opts.showHelp) {
|
||||
ShowHelp(version);
|
||||
return 0;
|
||||
}
|
||||
if (opts.checkUpdate) {
|
||||
return CheckForUpdate(version);
|
||||
}
|
||||
|
||||
if (opts.activate && opts.outputFile != "rarreg.key") {
|
||||
std::cerr << "Error: --activate and -o cannot be used together.\n";
|
||||
return -1;
|
||||
}
|
||||
if (opts.activate && opts.textOnly) {
|
||||
std::cerr << "Error: --activate and -t cannot be used together.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (opts.activate) {
|
||||
#ifdef __APPLE__
|
||||
const char* home = getenv("HOME");
|
||||
if (!home) {
|
||||
std::cerr << "Error: Failed to resolve $HOME path.\n";
|
||||
return -1;
|
||||
}
|
||||
std::string activateDir = std::string(home) + "/Library/Application Support/com.rarlab.WinRAR";
|
||||
mkdir(activateDir.c_str(), 0755);
|
||||
opts.outputFile = activateDir + "/rarreg.key";
|
||||
#else
|
||||
const char* home = getenv("HOME");
|
||||
if (!home) {
|
||||
std::cerr << "Error: Failed to resolve $HOME path.\n";
|
||||
return -1;
|
||||
}
|
||||
opts.outputFile = std::string(home) + "/.rarkey";
|
||||
#endif
|
||||
}
|
||||
|
||||
try {
|
||||
if (opts.username.empty() || opts.license.empty()) {
|
||||
std::cerr << "Error: Username and License Name must not be empty.\n";
|
||||
return -1;
|
||||
}
|
||||
if (opts.username.length() > 200 || opts.license.length() > 200) {
|
||||
std::cerr << "Error: Username and License Name must not exceed 200 characters.\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string displayUser = opts.username;
|
||||
std::string displayLicense = opts.license;
|
||||
std::string user, license;
|
||||
|
||||
switch (opts.encoding) {
|
||||
case Encoding::UTF8: {
|
||||
auto hasNonAscii = [](const std::string& s) {
|
||||
for (unsigned char c : s)
|
||||
if (c > 127) return true;
|
||||
return false;
|
||||
};
|
||||
if (hasNonAscii(displayUser) &&
|
||||
(displayUser.length() < 5 || displayUser.substr(0, 5) != "utf8:"))
|
||||
displayUser = "utf8:" + displayUser;
|
||||
if (hasNonAscii(displayLicense) &&
|
||||
(displayLicense.length() < 5 || displayLicense.substr(0, 5) != "utf8:"))
|
||||
displayLicense = "utf8:" + displayLicense;
|
||||
user = displayUser;
|
||||
license = displayLicense;
|
||||
break;
|
||||
}
|
||||
case Encoding::ANSI:
|
||||
std::cerr << "Warning: ANSI encoding is not supported on this platform. Using UTF-8.\n";
|
||||
user = displayUser;
|
||||
license = displayLicense;
|
||||
break;
|
||||
case Encoding::ASCII:
|
||||
default:
|
||||
user = displayUser;
|
||||
license = displayLicense;
|
||||
for (unsigned char c : user)
|
||||
if (c > 127)
|
||||
throw std::runtime_error(
|
||||
"Username contains non-ASCII characters. Use '-e utf8'.");
|
||||
for (unsigned char c : license)
|
||||
if (c > 127)
|
||||
throw std::runtime_error(
|
||||
"License name contains non-ASCII characters. Use '-e utf8'.");
|
||||
break;
|
||||
}
|
||||
|
||||
auto Info = WinRarKeygen<WinRarConfig>::GenerateRegisterInfo(user.c_str(), license.c_str());
|
||||
|
||||
if (opts.textOnly) {
|
||||
PrintRegisterInfo(Info, displayUser, displayLicense);
|
||||
} else {
|
||||
std::string content = BuildRegFileContent(Info);
|
||||
if (!WriteRegFile(opts.outputFile, content)) {
|
||||
std::cerr << "Error: Failed to write file: " << opts.outputFile << "\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char* encName = (opts.encoding == Encoding::UTF8) ? "UTF-8" :
|
||||
(opts.encoding == Encoding::ANSI) ? "ANSI" : "ASCII";
|
||||
|
||||
std::cout << "\n";
|
||||
PrintRegisterInfo(Info, displayUser, displayLicense);
|
||||
std::cout << "\nDone! " << opts.outputFile << " has been generated. ("
|
||||
<< encName << ")\n";
|
||||
}
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
std::cerr << "Error: " << e.what() << "\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user