Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.8 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Module: detours.lib
  4. // File: disasm.h
  5. // Author: Doug Brubacher
  6. //
  7. // Detours for binary functions. Version 1.2. (Build 35)
  8. // Includes support for all x86 chips prior to the Pentium III.
  9. //
  10. // Copyright 1999, Microsoft Corporation
  11. //
  12. // http://research.microsoft.com/sn/detours
  13. //
  14. #pragma once
  15. #ifndef _DISASM_H_
  16. #define _DISASM_H_
  17. class CDetourDis
  18. {
  19. public:
  20. CDetourDis(PBYTE *ppbTarget, LONG *plExtra);
  21. PBYTE CopyInstruction(PBYTE pbDst, PBYTE pbSrc);
  22. static BOOL SanityCheckSystem();
  23. public:
  24. struct COPYENTRY;
  25. typedef const COPYENTRY * REFCOPYENTRY;
  26. typedef PBYTE (CDetourDis::* COPYFUNC)(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
  27. enum {
  28. DYNAMIC = 0x1u,
  29. ADDRESS = 0x2u,
  30. NOENLARGE = 0x4u,
  31. SIB = 0x10u,
  32. NOTSIB = 0x0fu,
  33. };
  34. struct COPYENTRY
  35. {
  36. ULONG nOpcode : 8; // Opcode
  37. ULONG nFixedSize : 3; // Fixed size of opcode
  38. ULONG nFixedSize16 : 3; // Fixed size when 16 bit operand
  39. ULONG nModOffset : 3; // Offset to mod/rm byte (0=none)
  40. LONG nRelOffset : 3; // Offset to relative target.
  41. ULONG nFlagBits : 4; // Flags for DYNAMIC, etc.
  42. COPYFUNC pfCopy; // Function pointer.
  43. };
  44. protected:
  45. #define ENTRY_CopyBytes1 1, 1, 0, 0, 0, CopyBytes
  46. #define ENTRY_CopyBytes1Dynamic 1, 1, 0, 0, DYNAMIC, CopyBytes
  47. #define ENTRY_CopyBytes2 2, 2, 0, 0, 0, CopyBytes
  48. #define ENTRY_CopyBytes2Jump 2, 2, 0, 1, 0, CopyBytes
  49. #define ENTRY_CopyBytes2CantJump 2, 2, 0, 1, NOENLARGE, CopyBytes
  50. #define ENTRY_CopyBytes2Dynamic 2, 2, 0, 0, DYNAMIC, CopyBytes
  51. #define ENTRY_CopyBytes3 3, 3, 0, 0, 0, CopyBytes
  52. #define ENTRY_CopyBytes3Dynamic 3, 3, 0, 0, DYNAMIC, CopyBytes
  53. #define ENTRY_CopyBytes3Or5 5, 3, 0, 0, 0, CopyBytes
  54. #define ENTRY_CopyBytes3Or5Target 5, 3, 0, 1, 0, CopyBytes
  55. #define ENTRY_CopyBytes5Or7Dynamic 7, 5, 0, 0, DYNAMIC, CopyBytes
  56. #define ENTRY_CopyBytes3Or5Address 5, 3, 0, 0, ADDRESS, CopyBytes
  57. #define ENTRY_CopyBytes4 4, 4, 0, 0, 0, CopyBytes
  58. #define ENTRY_CopyBytes5 5, 5, 0, 0, 0, CopyBytes
  59. #define ENTRY_CopyBytes7 7, 7, 0, 0, 0, CopyBytes
  60. #define ENTRY_CopyBytes2Mod 2, 2, 1, 0, 0, CopyBytes
  61. #define ENTRY_CopyBytes2Mod1 3, 3, 1, 0, 0, CopyBytes
  62. #define ENTRY_CopyBytes2ModOperand 6, 4, 1, 0, 0, CopyBytes
  63. #define ENTRY_CopyBytes3Mod 3, 3, 2, 0, 0, CopyBytes
  64. #define ENTRY_CopyBytesPrefix 1, 1, 0, 0, 0, CopyBytesPrefix
  65. #define ENTRY_Copy0F 1, 1, 0, 0, 0, Copy0F
  66. #define ENTRY_Copy66 1, 1, 0, 0, 0, Copy66
  67. #define ENTRY_Copy67 1, 1, 0, 0, 0, Copy67
  68. #define ENTRY_CopyF6 0, 0, 0, 0, 0, CopyF6
  69. #define ENTRY_CopyF7 0, 0, 0, 0, 0, CopyF7
  70. #define ENTRY_CopyFF 0, 0, 0, 0, 0, CopyFF
  71. #define ENTRY_Invalid 1, 1, 0, 0, 0, Invalid
  72. #define ENTRY_End 0, 0, 0, 0, 0, NULL
  73. PBYTE CopyBytes(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
  74. PBYTE CopyBytesPrefix(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
  75. PBYTE Invalid(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
  76. PBYTE AdjustTarget(PBYTE pbDst, PBYTE pbSrc, LONG cbOp, LONG cbTargetOffset);
  77. VOID Set16BitOperand();
  78. VOID Set32BitOperand();
  79. VOID Set16BitAddress();
  80. VOID Set32BitAddress();
  81. protected:
  82. PBYTE Copy0F(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
  83. PBYTE Copy66(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
  84. PBYTE Copy67(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
  85. PBYTE CopyF6(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
  86. PBYTE CopyF7(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
  87. PBYTE CopyFF(REFCOPYENTRY pEntry, PBYTE pbDst, PBYTE pbSrc);
  88. protected:
  89. static const COPYENTRY s_rceCopyTable[257];
  90. static const COPYENTRY s_rceCopyTable0F[257];
  91. static const BYTE s_rbModRm[256];
  92. protected:
  93. BOOL m_b16BitOperand;
  94. BOOL m_b16BitAddress;
  95. PBYTE * m_ppbTarget;
  96. LONG * m_plExtra;
  97. LONG m_lScratchExtra;
  98. PBYTE m_pbScratchTarget;
  99. BYTE m_rbScratchDst[64];
  100. };
  101. #endif //_DISASM_H_