Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cHookJump.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cHookJump_H
7 #define _INC_cHookJump_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cThreadLock.h"
13 #include "cUnitTestDecl.h"
14 
15 namespace Gray
16 {
18  {
23 
24  public:
25  static const BYTE k_I_NULL = 0x00;
26 #if USE_INTEL
27  static const BYTE k_I_JUMP = 0xe9; // X86
28 #endif
29  static const int k_LEN_J = 1;
30 
31  static const int k_LEN_P = 4;
32 #ifdef USE_64BIT
33  static const int k_LEN_A = 16;
34 #else
35  static const int k_LEN_A = 8;
36 #endif
37  static const int k_LEN_D = k_LEN_J + k_LEN_P;
38 
39  friend class cHookSwapLock;
40  friend class cHookSwapChain;
41 
42  protected:
43  FARPROC m_pFuncOrig;
44  BYTE m_OldCode[k_LEN_D];
45  BYTE m_Jump[k_LEN_D];
47 
48  protected:
49  bool SwapOld() noexcept
50  {
53  if (!isHookValid())
54  return false;
55  cMem::Copy((void*)m_pFuncOrig, m_OldCode, sizeof(m_OldCode));
56  return true;
57  }
58  void SwapReset() noexcept
59  {
62  if (!isHookInstalled() || m_pFuncOrig == nullptr) // hook has since been destroyed!
63  return;
64  cMem::Copy((void*)m_pFuncOrig, m_Jump, sizeof(m_Jump));
65  }
66 
67  public:
68  cHookJump() noexcept
69  : m_pFuncOrig(nullptr)
70  {
71  m_OldCode[0] = k_I_NULL;
72  m_Jump[0] = k_I_NULL;
73  }
75  {
76  RemoveHook();
77  }
78 
79  bool isHookInstalled() const noexcept
80  {
81  return m_Jump[0] == k_I_JUMP;
82  }
83  bool isHookValid() const noexcept
84  {
86  if (!isHookInstalled() || m_pFuncOrig == nullptr)
87  return false;
88  if (::memcmp((const void*)m_pFuncOrig, m_Jump, sizeof(m_Jump)))
89  return false; // NOT set !!
90  return true;
91  }
92 
93  bool isChainable() const noexcept
94  {
95  // The jump i inserted is just on top of another jump?
96  // I don't need to lock and swap to call the old code. I can just chain to it
97  return m_OldCode[0] == k_I_JUMP; // the old code was just a jump as well.
98  }
99 
100  FARPROC GetChainFunc() const;
101 
102  bool InstallHook(FARPROC pFuncOrig, FARPROC pFuncNew);
103  void RemoveHook();
104 
106  };
107 
109  {
112  public:
114  bool m_bSwapOld; // has Old. Must be locked.
115 
116  public:
117  cHookSwapLock(cHookJump& rJump, bool swap = true)
118  : cThreadGuardFast(rJump.m_Lock) // MUST lock while we do this. single thread.
119  , m_rJump(rJump)
120  {
121  m_bSwapOld = swap ? m_rJump.SwapOld() : false;
122  }
123  ~cHookSwapLock() noexcept
124  {
125  if (m_bSwapOld)
126  {
127  m_rJump.SwapReset();
128  }
129  }
130  };
131 
133  {
134  public:
135  FARPROC m_pFuncChain; // chained version of m_pFuncOrig. or fallback to m_pFuncOrig.
136  public:
138  : cHookSwapLock(rJump, !rJump.isChainable())
139  {
140  m_pFuncChain = rJump.GetChainFunc();
141  }
142  };
143 }
144 
145 #endif // _INC_cHookJump_H
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
Definition: cHookJump.h:18
bool isHookValid() const noexcept
Definition: cHookJump.h:83
bool isChainable() const noexcept
Definition: cHookJump.h:93
cHookJump() noexcept
Definition: cHookJump.h:68
void SwapReset() noexcept
Definition: cHookJump.h:58
FARPROC GetChainFunc() const
Definition: cHookJump.cpp:15
~cHookJump()
Definition: cHookJump.h:74
cThreadLockFast m_Lock
prevent multiple threads from using this at the same time.
Definition: cHookJump.h:46
FARPROC m_pFuncOrig
Pointer to the original/old function. The one i will replace. Inject code here.
Definition: cHookJump.h:43
bool SwapOld() noexcept
Definition: cHookJump.h:49
bool isHookInstalled() const noexcept
Definition: cHookJump.h:79
Definition: cHookJump.h:133
FARPROC m_pFuncChain
Definition: cHookJump.h:135
cHookSwapChain(cHookJump &rJump)
Definition: cHookJump.h:137
Definition: cHookJump.h:109
cHookSwapLock(cHookJump &rJump, bool swap=true)
Definition: cHookJump.h:117
~cHookSwapLock() noexcept
Definition: cHookJump.h:123
bool m_bSwapOld
Definition: cHookJump.h:114
cHookJump & m_rJump
The code we are locking for use.
Definition: cHookJump.h:113
Definition: cLocker.h:72
Definition: cThreadLock.h:205
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
static void Copy(void *pDst, const void *pSrc, size_t nSizeBlock) noexcept
Definition: cMem.h:132