Misc.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. #include <stddef.h>
  3. #include <tchar.h>
  4. #include <windows.h>
  5. #include <type_traits>
  6. #include <xstring.hpp>
  7. #define LOG_SUCCESS(tab, fmt, ...) tab ? _tprintf_s(TEXT("%*c[+] " fmt "\n"), tab, ' ', __VA_ARGS__) : _tprintf_s(TEXT("[+] " fmt "\n"), __VA_ARGS__)
  8. #define LOG_FAILURE(tab, fmt, ...) tab ? _tprintf_s(TEXT("%*c[-] " fmt "\n"), tab, ' ', __VA_ARGS__) : _tprintf_s(TEXT("[-] " fmt "\n"), __VA_ARGS__)
  9. #define LOG_HINT(tab, fmt, ...) tab ? _tprintf_s(TEXT("%*c[*] " fmt "\n"), tab, ' ', __VA_ARGS__) : _tprintf_s(TEXT("[*] " fmt "\n"), __VA_ARGS__)
  10. #define LOG_SELECT(tab, fmt, ...) tab ? _tprintf_s(TEXT("%*c[?] " fmt " "), tab, ' ', __VA_ARGS__) : _tprintf_s(TEXT("[?] " fmt " "), __VA_ARGS__)
  11. namespace nkg {
  12. template<size_t __Len>
  13. constexpr size_t literal_length(const char (&)[__Len]) noexcept {
  14. return __Len - 1;
  15. }
  16. template<typename __PtrType1, typename __PtrType2>
  17. constexpr auto address_delta(__PtrType1 p1, __PtrType2 p2) noexcept {
  18. static_assert(std::is_pointer_v<__PtrType1>);
  19. static_assert(std::is_pointer_v<__PtrType2>);
  20. return reinterpret_cast<const volatile char*>(p1) - reinterpret_cast<const volatile char*>(p2);
  21. }
  22. template<typename __PtrType>
  23. constexpr __PtrType address_offset(__PtrType p, decltype(static_cast<char*>(nullptr) - static_cast<char*>(nullptr)) offset) noexcept {
  24. static_assert(std::is_pointer_v<__PtrType>);
  25. return reinterpret_cast<__PtrType>(
  26. const_cast<char*>(reinterpret_cast<const volatile char*>(p)) + offset
  27. );
  28. }
  29. template<typename __ReturnType, typename __PtrType>
  30. constexpr __ReturnType address_offset_cast(__PtrType p, decltype(static_cast<char*>(nullptr) - static_cast<char*>(nullptr)) offset) noexcept {
  31. static_assert(std::is_pointer_v<__ReturnType>);
  32. static_assert(std::is_pointer_v<__PtrType>);
  33. return reinterpret_cast<__ReturnType>(
  34. const_cast<char*>(reinterpret_cast<const volatile char*>(p)) + offset
  35. );
  36. }
  37. //
  38. // Print memory data in [lpMemBegin, lpMemEnd)
  39. // If `base` is not nullptr, print address as offset. Otherwise, as absolute address.
  40. // NOTICE:
  41. // `base` must >= `from`
  42. //
  43. void PrintMemory(const void* lpMemBegin, const void* lpMemEnd, const void* lpBase) noexcept;
  44. //
  45. // Print memory data in [lpMem, lpMem + cbMem)
  46. // If `base` is not nullptr, print address as offset. Otherwise, as absolute address.
  47. // NOTICE:
  48. // `base` must >= `from`
  49. //
  50. void PrintMemory(const void* lpMem, size_t cbMem, const void* lpBase) noexcept;
  51. void PrintBytes(const void* lpMemBegin, const void* lpMemEnd) noexcept;
  52. void PrintBytes(const void* lpMem, size_t cbMem) noexcept;
  53. [[nodiscard]]
  54. bool IsValidDirectoryPath(PCTSTR lpszDirectoryPath) noexcept;
  55. [[nodiscard]]
  56. bool IsValidFilePath(PCTSTR lpszFilePath) noexcept;
  57. [[nodiscard]]
  58. bool IsWineEnvironment() noexcept;
  59. std::xstring GetCurrentWorkingDirectory();
  60. }