Helper.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "def.hpp"
  2. #include "NavicatCrypto.hpp"
  3. namespace Helper {
  4. static Navicat11Crypto NavicatCipher("23970790", 8);
  5. std::string EncryptPublicKey(const std::string& PublicKeyString) {
  6. return NavicatCipher.EncryptString(PublicKeyString.c_str(),
  7. PublicKeyString.length());
  8. }
  9. bool ConvertToUTF8(LPCSTR from, std::string& to) {
  10. bool bSuccess = false;
  11. int RequireLength = 0;
  12. LPWSTR lpUnicodeString = nullptr;
  13. RequireLength = MultiByteToWideChar(CP_ACP, NULL, from, -1, nullptr, 0);
  14. if (RequireLength == 0)
  15. goto ON_ConvertToUTF8_0_ERROR;
  16. lpUnicodeString = reinterpret_cast<LPWSTR>(HeapAlloc(GetProcessHeap(),
  17. HEAP_ZERO_MEMORY,
  18. RequireLength * sizeof(WCHAR)));
  19. if (lpUnicodeString == nullptr)
  20. goto ON_ConvertToUTF8_0_ERROR;
  21. if (!MultiByteToWideChar(CP_ACP, NULL, from, -1, lpUnicodeString, RequireLength))
  22. goto ON_ConvertToUTF8_0_ERROR;
  23. RequireLength = WideCharToMultiByte(CP_UTF8, NULL, lpUnicodeString, -1, nullptr, 0, nullptr, nullptr);
  24. if (RequireLength == 0)
  25. goto ON_ConvertToUTF8_0_ERROR;
  26. to.resize(RequireLength);
  27. if (!WideCharToMultiByte(CP_UTF8, NULL, lpUnicodeString, -1, to.data(), RequireLength, nullptr, nullptr))
  28. goto ON_ConvertToUTF8_0_ERROR;
  29. while (to.back() == 0)
  30. to.pop_back();
  31. bSuccess = true;
  32. ON_ConvertToUTF8_0_ERROR:
  33. if (lpUnicodeString)
  34. HeapFree(GetProcessHeap(), NULL, lpUnicodeString);
  35. return bSuccess;
  36. }
  37. bool ConvertToUTF8(LPCWSTR from, std::string& to) {
  38. bool bSuccess = false;
  39. int RequireLength = 0;
  40. RequireLength = WideCharToMultiByte(CP_UTF8, NULL, from, -1, nullptr, 0, nullptr, nullptr);
  41. if (RequireLength == 0)
  42. goto ON_ConvertToUTF8_1_ERROR;
  43. to.resize(RequireLength);
  44. if (!WideCharToMultiByte(CP_UTF8, NULL, from, -1, to.data(), RequireLength, nullptr, nullptr))
  45. goto ON_ConvertToUTF8_1_ERROR;
  46. while (to.back() == 0)
  47. to.pop_back();
  48. bSuccess = true;
  49. ON_ConvertToUTF8_1_ERROR:
  50. return bSuccess;
  51. }
  52. bool ConvertToUTF8(std::string& str) {
  53. bool bSuccess = false;
  54. std::string temp;
  55. bSuccess = ConvertToUTF8(str.c_str(), temp);
  56. if (bSuccess)
  57. str = temp;
  58. return bSuccess;
  59. }
  60. void ErrorReport(LPCTSTR at, UINT line, LPCTSTR msg) {
  61. _tprintf_s(TEXT("@%s LINE: %u\n"), at, line);
  62. _tprintf_s(TEXT("%s\n"), msg);
  63. }
  64. void ErrorReport(LPCTSTR at, UINT line, LPCTSTR msg, DWORD err_code) {
  65. _tprintf_s(TEXT("@%s LINE: %u\n"), at, line);
  66. _tprintf_s(TEXT("%s CODE: 0x%08X\n"), msg, err_code);
  67. }
  68. //
  69. // read byte(s) at address `p` as _Type to `out`
  70. // succeed if return true, otherwise return false
  71. //
  72. template<typename _Type>
  73. static __forceinline bool ProbeForRead(const void* p, void* out) {
  74. __try {
  75. *reinterpret_cast<_Type*>(out) = *reinterpret_cast<const _Type*>(p);
  76. return true;
  77. } __except (EXCEPTION_EXECUTE_HANDLER) {
  78. return false;
  79. }
  80. }
  81. //
  82. // Print memory data in [from, to) at least
  83. // If `base` is not nullptr, print address as offset. Otherwise, as absolute address.
  84. // NOTICE:
  85. // `base` must >= `from`
  86. //
  87. void PrintMemory(const void* from, const void* to, const void* base) {
  88. const uint8_t* start = reinterpret_cast<const uint8_t*>(from);
  89. const uint8_t* end = reinterpret_cast<const uint8_t*>(to);
  90. const uint8_t* base_ptr = reinterpret_cast<const uint8_t*>(base);
  91. if (start >= end)
  92. return;
  93. while (reinterpret_cast<uintptr_t>(start) % 16)
  94. start--;
  95. while (reinterpret_cast<uintptr_t>(start) % 16)
  96. end++;
  97. while (start < end) {
  98. uint16_t value[16] = {};
  99. if (base_ptr)
  100. _tprintf(TEXT("+0x%p "), reinterpret_cast<const void*>(start - base_ptr));
  101. else
  102. _tprintf(TEXT("0x%p "), start);
  103. for (int i = 0; i < 16; ++i) {
  104. if (ProbeForRead<uint8_t>(start + i, value + i)) {
  105. _tprintf(TEXT("%02x "), value[i]);
  106. } else {
  107. value[i] = -1;
  108. _tprintf(TEXT("?? "));
  109. }
  110. }
  111. _tprintf(TEXT(" "));
  112. for (int i = 0; i < 16; ++i) {
  113. if (value[i] < 0x20) {
  114. _tprintf(TEXT("."));
  115. } else if (value[i] > 0x7e) {
  116. _tprintf(TEXT("."));
  117. } else {
  118. _tprintf(TEXT("%c"), value[i]);
  119. }
  120. }
  121. _tprintf(TEXT("\n"));
  122. start += 0x10;
  123. }
  124. }
  125. PIMAGE_SECTION_HEADER ImageSectionHeader(PVOID lpBase, LPCSTR lpSectionName) {
  126. PIMAGE_DOS_HEADER pFileHeader = NULL;
  127. PIMAGE_NT_HEADERS pNtHeader = NULL;
  128. IMAGE_SECTION_HEADER* pSectionHeaders = NULL;
  129. pFileHeader = (IMAGE_DOS_HEADER*)lpBase;
  130. if (pFileHeader->e_magic != IMAGE_DOS_SIGNATURE)
  131. return NULL;
  132. pNtHeader = (IMAGE_NT_HEADERS*)((BYTE*)lpBase + pFileHeader->e_lfanew);
  133. if (pNtHeader->Signature != IMAGE_NT_SIGNATURE)
  134. return NULL;
  135. pSectionHeaders = (IMAGE_SECTION_HEADER*)((BYTE*)pNtHeader +
  136. offsetof(IMAGE_NT_HEADERS, OptionalHeader) +
  137. pNtHeader->FileHeader.SizeOfOptionalHeader);
  138. for (WORD i = 0; i < pNtHeader->FileHeader.NumberOfSections; ++i)
  139. if (_stricmp((const char*)pSectionHeaders[i].Name, lpSectionName) == 0)
  140. return pSectionHeaders + i;
  141. return NULL;
  142. }
  143. }