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