_tmain.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifdef _WIN32
  2. #include <tchar.h>
  3. #include <windows.h>
  4. #ifdef __GNUC__
  5. #define MINGW_HAS_SECURE_API
  6. #include <sec_api/stdio_s.h>
  7. #define _tprintf_s printf_s
  8. #endif
  9. #define HS "hs"
  10. #else
  11. #define PTSTR char*
  12. #define _putts puts
  13. #define _tprintf_s printf
  14. #define TEXT(s) s
  15. #define HS "s"
  16. #endif
  17. #include <stdio.h>
  18. #include <locale.h>
  19. #include "WinRarConfig.hpp"
  20. #include "WinRarKeygen.hpp"
  21. #include <system_error>
  22. void Help() {
  23. _putts(TEXT("Usage:"));
  24. _putts(TEXT(" winrar-keygen.exe <your name> <license type>"));
  25. _putts(TEXT(""));
  26. _putts(TEXT("Example:"));
  27. _putts(TEXT(""));
  28. _putts(TEXT(" winrar-keygen.exe \"Rebecca Morrison\" \"Single PC usage license\""));
  29. _putts(TEXT(" or:"));
  30. _putts(TEXT(" winrar-keygen.exe \"Rebecca Morrison\" \"Single PC usage license\" > rarreg.key\n"));
  31. }
  32. void PrintRegisterInfo(const WinRarKeygen<WinRarConfig>::RegisterInfo& Info) {
  33. _tprintf_s(TEXT("%" HS "\n"), "RAR registration data");
  34. _tprintf_s(TEXT("%" HS "\n"), Info.UserName.c_str());
  35. _tprintf_s(TEXT("%" HS "\n"), Info.LicenseType.c_str());
  36. _tprintf_s(TEXT("UID=%" HS "\n"), Info.UID.c_str());
  37. for (size_t i = 0; i < Info.HexData.length(); i += 54) {
  38. _tprintf_s(TEXT("%.54" HS "\n"), Info.HexData.c_str() + i);
  39. }
  40. }
  41. #ifdef _WIN32
  42. std::string ToACP(PCWSTR lpszUnicodeString) {
  43. int len;
  44. len = WideCharToMultiByte(CP_ACP, 0, lpszUnicodeString, -1, NULL, 0, NULL, NULL);
  45. if (len == 0) {
  46. auto err = GetLastError();
  47. throw std::system_error(err, std::system_category());
  48. }
  49. std::string Result(len, '\x00');
  50. len = WideCharToMultiByte(CP_ACP, 0, lpszUnicodeString, -1, Result.data(), static_cast<int>(Result.length()), NULL, NULL);
  51. if (len == 0) {
  52. auto err = GetLastError();
  53. throw std::system_error(err, std::system_category());
  54. }
  55. while (Result.back() == '\x00') {
  56. Result.pop_back();
  57. }
  58. return Result;
  59. }
  60. #endif
  61. #ifdef WIN32
  62. int _tmain(int argc, PTSTR argv[]) {
  63. #else
  64. int main(int argc, PTSTR argv[]) {
  65. #endif
  66. setlocale(LC_ALL, "");
  67. if (argc == 3) {
  68. try {
  69. PrintRegisterInfo(
  70. #if defined(_UNICODE) || defined(UNICODE)
  71. WinRarKeygen<WinRarConfig>::GenerateRegisterInfo(ToACP(argv[1]).c_str(), ToACP(argv[2]).c_str())
  72. #else
  73. WinRarKeygen<WinRarConfig>::GenerateRegisterInfo(argv[1], argv[2])
  74. #endif
  75. );
  76. } catch (std::exception& e) {
  77. _tprintf_s(TEXT("%" HS "\n"), e.what());
  78. return -1;
  79. }
  80. } else {
  81. Help();
  82. }
  83. return 0;
  84. }