Helper.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "Helper.hpp"
  2. #include <tchar.h>
  3. #include <windows.h>
  4. namespace Helper {
  5. Navicat11Crypto NavicatCipher("23970790", 8);
  6. //
  7. // read byte(s) at address `p` as _Type to `out`
  8. // succeed if return true, otherwise return false
  9. //
  10. template<typename _Type>
  11. static __forceinline bool ProbeForRead(const void* p, void* out) {
  12. __try {
  13. *reinterpret_cast<_Type*>(out) = *reinterpret_cast<const _Type*>(p);
  14. return true;
  15. } __except (EXCEPTION_EXECUTE_HANDLER) {
  16. return false;
  17. }
  18. }
  19. void PrintMemory(const void* from, const void* to, const void* base) {
  20. const uint8_t* start = reinterpret_cast<const uint8_t*>(from);
  21. const uint8_t* end = reinterpret_cast<const uint8_t*>(to);
  22. const uint8_t* base_ptr = reinterpret_cast<const uint8_t*>(base);
  23. if (start >= end)
  24. return;
  25. while (reinterpret_cast<uintptr_t>(start) % 16)
  26. start--;
  27. while (reinterpret_cast<uintptr_t>(start) % 16)
  28. end++;
  29. while (start < end) {
  30. uint16_t value[16] = {};
  31. if (base_ptr) {
  32. uintptr_t d = start >= base ? start - base_ptr : base_ptr - start;
  33. if (start >= base) {
  34. if constexpr (sizeof(void*) == 4) {
  35. _tprintf(TEXT("+0x%.8zx "), d);
  36. }
  37. if constexpr (sizeof(void*) == 8) {
  38. _tprintf(TEXT("+0x%.16zx "), d);
  39. }
  40. } else {
  41. if constexpr (sizeof(void*) == 4) {
  42. _tprintf(TEXT("-0x%.8zx "), d);
  43. }
  44. if constexpr (sizeof(void*) == 8) {
  45. _tprintf(TEXT("-0x%.16zx "), d);
  46. }
  47. }
  48. } else {
  49. _tprintf(TEXT("0x%p "), start);
  50. }
  51. for (int i = 0; i < 16; ++i) {
  52. if (ProbeForRead<uint8_t>(start + i, value + i)) {
  53. _tprintf(TEXT("%.2x "), value[i]);
  54. } else {
  55. value[i] = -1;
  56. _tprintf(TEXT("?? "));
  57. }
  58. }
  59. _tprintf(TEXT(" "));
  60. for (int i = 0; i < 16; ++i) {
  61. if (value[i] < 0x20) {
  62. _tprintf(TEXT("."));
  63. } else if (value[i] > 0x7e) {
  64. _tprintf(TEXT("."));
  65. } else {
  66. _tprintf(TEXT("%c"), value[i]);
  67. }
  68. }
  69. _tprintf(TEXT("\n"));
  70. start += 0x10;
  71. }
  72. }
  73. void PrintBytes(const void* p, size_t s) {
  74. const uint8_t* byte_ptr = reinterpret_cast<const uint8_t*>(p);
  75. if (s == 0)
  76. return;
  77. if (s == 1) {
  78. _tprintf_s(TEXT("%.2X"), byte_ptr[0]);
  79. return;
  80. }
  81. s -= 1;
  82. for (size_t i = 0; i < s; ++i)
  83. _tprintf_s(TEXT("%.2X "), byte_ptr[i]);
  84. _tprintf_s(TEXT("%.2X"), byte_ptr[s]);
  85. }
  86. bool IsPrintable(const uint8_t* p, size_t s) {
  87. for (size_t i = 0; i < s; ++i)
  88. if (isprint(p[i]) == false)
  89. return false;
  90. return true;
  91. }
  92. }