-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy path_tmain.cpp
More file actions
820 lines (745 loc) · 28.1 KB
/
_tmain.cpp
File metadata and controls
820 lines (745 loc) · 28.1 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
#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 "WinRarConfig.hpp"
#include "WinRarKeygen.hpp"
#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);
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;
}
std::string WideToAnsi(const std::wstring& wstr) {
if (wstr.empty()) return {};
BOOL usedDefaultChar = FALSE;
int size = WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wstr.data(), (int)wstr.size(),
nullptr, 0, nullptr, &usedDefaultChar);
if (size == 0 || usedDefaultChar) {
throw std::runtime_error(
"Input contains characters not representable in the current ANSI code page. "
"Use '-e utf8' for Unicode characters.");
}
std::string result(size, 0);
WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wstr.data(), (int)wstr.size(),
&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) {
std::wstring arg = argv[i];
if (arg == L"-v" || arg == L"--version" || _wcsicmp(arg.c_str(), L"ver") == 0) {
opts.showVersion = true; return true;
}
if (arg == L"-h" || arg == L"--help" || _wcsicmp(arg.c_str(), L"help") == 0) {
opts.showHelp = true; return true;
}
if (arg == L"-t" || arg == L"--text") {
opts.textOnly = true; continue;
}
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";
return false;
}
std::wstring val = argv[i];
if (_wcsicmp(val.c_str(), L"ascii") == 0) opts.encoding = Encoding::ASCII;
else if (_wcsicmp(val.c_str(), L"ansi") == 0) opts.encoding = Encoding::ANSI;
else if (_wcsicmp(val.c_str(), L"utf8") == 0 || _wcsicmp(val.c_str(), L"utf-8") == 0)
opts.encoding = Encoding::UTF8;
else {
std::wcerr << L"Error: Unknown encoding '" << val << L"'. Use: ascii, ansi, utf8\n";
return false;
}
continue;
}
if (arg == L"-o" || arg == L"--output") {
if (++i >= argc) {
std::wcerr << L"Error: Missing value for " << arg << L"\n";
return false;
}
opts.outputFile = argv[i]; continue;
}
if (!arg.empty() && arg[0] == L'-') {
std::wcerr << L"Error: Unknown option '" << arg << L"'\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::wcerr << L"Error: Expected 2 arguments (Username, LicenseName), got "
<< 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";
std::wcout << L" winrar-keygen.exe <Username> <LicenseName> [options]\n";
std::wcout << L" winrar-keygen.exe -v | --version\n";
std::wcout << L" winrar-keygen.exe -h | --help\n\n";
std::wcout << L"Options:\n";
std::wcout << L" -e, --encoding <enc> utf8 (default), ascii, ansi\n";
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 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);
if (pathLen == 0) {
return L"unknown";
}
DWORD handle = 0;
DWORD versionSize = GetFileVersionInfoSizeW(exePath, &handle);
if (versionSize == 0) {
return L"unknown";
}
std::vector<BYTE> versionData(versionSize);
if (!GetFileVersionInfoW(exePath, 0, versionSize, versionData.data())) {
return L"unknown";
}
VS_FIXEDFILEINFO* fixedInfo = nullptr;
UINT len = 0;
if (!VerQueryValueW(versionData.data(), L"\\",
reinterpret_cast<LPVOID*>(&fixedInfo),
&len) ||
fixedInfo == nullptr) {
return L"unknown";
}
return std::to_wstring(HIWORD(fixedInfo->dwFileVersionMS)) + L"." +
std::to_wstring(LOWORD(fixedInfo->dwFileVersionMS)) + L"." +
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);
std::wstring data = Utf8ToWide(Info.HexData);
std::wcout << L"RAR registration data\n";
std::wcout << wUser << L"\n";
std::wcout << wLicense << 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";
}
}
#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;
s += "RAR registration data\r\n";
s += Info.UserName + "\r\n";
s += Info.LicenseType + "\r\n";
s += "UID=" + Info.UID + "\r\n";
for (size_t i = 0; i < Info.HexData.length(); i += 54) {
s += Info.HexData.substr(i, 54) + "\r\n";
}
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)
return false;
size_t written = fwrite(content.data(), 1, content.size(), fp);
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;
}
DWORD mode = 0;
return GetConsoleMode(handle, &mode) != 0;
}
void ConfigureConsoleOutput() {
HANDLE stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE stderrHandle = GetStdHandle(STD_ERROR_HANDLE);
// console use UTF-16, file use UTF-8
if (IsConsoleHandle(stdoutHandle)) {
if (_setmode(_fileno(stdout), _O_WTEXT) == -1)
std::wcerr << L"Failed to set stdout _O_WTEXT\n";
}
else {
// stdout is redirected to a file, use UTF-8
if (_setmode(_fileno(stdout), _O_U8TEXT) == -1)
std::wcerr << L"Failed to set stdout _O_U8TEXT\n";
}
if (IsConsoleHandle(stderrHandle)) {
if (_setmode(_fileno(stderr), _O_WTEXT) == -1)
std::wcerr << L"Failed to set stderr _O_WTEXT\n";
}
else {
if (_setmode(_fileno(stderr), _O_U8TEXT) == -1)
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();
Options opts;
if (!ParseArguments(argc, argv, opts)) {
return -1;
}
std::wstring version = GetExecutableVersion();
std::string versionUtf8 = WideToUtf8(version);
if (opts.showVersion) {
std::wcout << L"winrar-keygen v" << version << L"\n";
return 0;
}
if (opts.showHelp) {
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";
return -1;
}
if (opts.activate && opts.textOnly) {
std::wcerr << L"Error: --activate and -t cannot be used together.\n";
return -1;
}
if (opts.activate) {
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";
return -1;
}
CreateDirectoryW(appdata, nullptr);
opts.outputFile = std::wstring(appdata) + L"\\rarreg.key";
}
try {
if (opts.username.empty() || opts.license.empty()) {
std::wcerr << L"Error: Username and License Name must not be empty.\n";
return -1;
}
if (opts.username.length() > 200 || opts.license.length() > 200) {
std::wcerr << L"Error: Username and License Name must not exceed 200 characters.\n";
return -1;
}
std::wstring wDisplayUser = opts.username;
std::wstring wDisplayLicense = opts.license;
std::string user, license;
switch (opts.encoding) {
case Encoding::UTF8: {
auto hasNonAscii = [](const std::wstring& s) {
for (wchar_t c : s)
if (static_cast<unsigned>(c) > 127) return true;
return false;
};
if (hasNonAscii(wDisplayUser) &&
(wDisplayUser.length() < 5 || wDisplayUser.substr(0, 5) != L"utf8:"))
wDisplayUser = L"utf8:" + wDisplayUser;
if (hasNonAscii(wDisplayLicense) &&
(wDisplayLicense.length() < 5 || wDisplayLicense.substr(0, 5) != L"utf8:"))
wDisplayLicense = L"utf8:" + wDisplayLicense;
user = WideToUtf8(wDisplayUser);
license = WideToUtf8(wDisplayLicense);
break;
}
case Encoding::ANSI:
user = WideToAnsi(wDisplayUser);
license = WideToAnsi(wDisplayLicense);
break;
case Encoding::ASCII:
default:
user = WideToAnsi(wDisplayUser);
license = WideToAnsi(wDisplayLicense);
for (unsigned char c : user)
if (c > 127)
throw std::runtime_error(
"Username contains non-ASCII characters. Use '-e ansi' or '-e utf8'.");
for (unsigned char c : license)
if (c > 127)
throw std::runtime_error(
"License name contains non-ASCII characters. Use '-e ansi' or '-e utf8'.");
break;
}
auto Info = WinRarKeygen<WinRarConfig>::GenerateRegisterInfo(user.c_str(), license.c_str());
if (opts.textOnly) {
PrintRegisterInfo(Info, wDisplayUser, wDisplayLicense);
} else {
std::string content = BuildRegFileContent(Info);
if (!WriteRegFile(opts.outputFile, content)) {
std::wcerr << L"Error: Failed to write file: " << opts.outputFile << L"\n";
return -1;
}
const wchar_t* encName = (opts.encoding == Encoding::UTF8) ? L"UTF-8" :
(opts.encoding == Encoding::ANSI) ? L"ANSI" : L"ASCII";
std::wcout << L"\n";
PrintRegisterInfo(Info, wDisplayUser, wDisplayLicense);
std::wcout << L"\nDone! " << opts.outputFile << L" has been generated. ("
<< encName << L")\n";
}
}
catch (std::exception& e) {
std::wcerr << L"Error: " << Utf8ToWide(e.what()) << L"\n";
return -1;
}
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