rarvm.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef _RAR_VM_
  2. #define _RAR_VM_
  3. #define VM_STANDARDFILTERS
  4. #ifndef SFX_MODULE
  5. #define VM_OPTIMIZE
  6. #endif
  7. #define VM_MEMSIZE 0x40000
  8. #define VM_MEMMASK (VM_MEMSIZE-1)
  9. #define VM_GLOBALMEMADDR 0x3C000
  10. #define VM_GLOBALMEMSIZE 0x2000
  11. #define VM_FIXEDGLOBALSIZE 64
  12. enum VM_Commands
  13. {
  14. VM_MOV, VM_CMP, VM_ADD, VM_SUB, VM_JZ, VM_JNZ, VM_INC, VM_DEC,
  15. VM_JMP, VM_XOR, VM_AND, VM_OR, VM_TEST, VM_JS, VM_JNS, VM_JB,
  16. VM_JBE, VM_JA, VM_JAE, VM_PUSH, VM_POP, VM_CALL, VM_RET, VM_NOT,
  17. VM_SHL, VM_SHR, VM_SAR, VM_NEG, VM_PUSHA,VM_POPA, VM_PUSHF,VM_POPF,
  18. VM_MOVZX,VM_MOVSX,VM_XCHG, VM_MUL, VM_DIV, VM_ADC, VM_SBB, VM_PRINT,
  19. #ifdef VM_OPTIMIZE
  20. VM_MOVB, VM_MOVD, VM_CMPB, VM_CMPD,
  21. VM_ADDB, VM_ADDD, VM_SUBB, VM_SUBD, VM_INCB, VM_INCD, VM_DECB, VM_DECD,
  22. VM_NEGB, VM_NEGD,
  23. #endif
  24. VM_STANDARD
  25. };
  26. enum VM_StandardFilters {
  27. VMSF_NONE, VMSF_E8, VMSF_E8E9, VMSF_ITANIUM, VMSF_RGB, VMSF_AUDIO,
  28. VMSF_DELTA, VMSF_UPCASE
  29. };
  30. enum VM_Flags {VM_FC=1,VM_FZ=2,VM_FS=0x80000000};
  31. enum VM_OpType {VM_OPREG,VM_OPINT,VM_OPREGMEM,VM_OPNONE};
  32. struct VM_PreparedOperand
  33. {
  34. VM_OpType Type;
  35. uint Data;
  36. uint Base;
  37. uint *Addr;
  38. };
  39. struct VM_PreparedCommand
  40. {
  41. VM_Commands OpCode;
  42. bool ByteMode;
  43. VM_PreparedOperand Op1,Op2;
  44. };
  45. struct VM_PreparedProgram
  46. {
  47. VM_PreparedProgram()
  48. {
  49. AltCmd=NULL;
  50. FilteredDataSize=0;
  51. CmdCount=0;
  52. }
  53. Array<VM_PreparedCommand> Cmd;
  54. VM_PreparedCommand *AltCmd;
  55. int CmdCount;
  56. Array<byte> GlobalData;
  57. Array<byte> StaticData; // static data contained in DB operators
  58. uint InitR[7];
  59. byte *FilteredData;
  60. uint FilteredDataSize;
  61. };
  62. class RarVM:private BitInput
  63. {
  64. private:
  65. inline uint GetValue(bool ByteMode,uint *Addr);
  66. inline void SetValue(bool ByteMode,uint *Addr,uint Value);
  67. inline uint* GetOperand(VM_PreparedOperand *CmdOp);
  68. void DecodeArg(VM_PreparedOperand &Op,bool ByteMode);
  69. #ifdef VM_OPTIMIZE
  70. void Optimize(VM_PreparedProgram *Prg);
  71. #endif
  72. bool ExecuteCode(VM_PreparedCommand *PreparedCode,uint CodeSize);
  73. #ifdef VM_STANDARDFILTERS
  74. VM_StandardFilters IsStandardFilter(byte *Code,uint CodeSize);
  75. void ExecuteStandardFilter(VM_StandardFilters FilterType);
  76. uint FilterItanium_GetBits(byte *Data,int BitPos,int BitCount);
  77. void FilterItanium_SetBits(byte *Data,uint BitField,int BitPos,int BitCount);
  78. #endif
  79. byte *Mem;
  80. uint R[8];
  81. uint Flags;
  82. public:
  83. RarVM();
  84. ~RarVM();
  85. void Init();
  86. void Prepare(byte *Code,uint CodeSize,VM_PreparedProgram *Prg);
  87. void Execute(VM_PreparedProgram *Prg);
  88. void SetLowEndianValue(uint *Addr,uint Value);
  89. void SetMemory(uint Pos,byte *Data,uint DataSize);
  90. static uint ReadData(BitInput &Inp);
  91. };
  92. #endif