OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / user / unrar / rarvm.hpp
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 {
62   VM_PreparedProgram() {AltCmd=NULL;}
63
64   Array<VM_PreparedCommand> Cmd;
65   VM_PreparedCommand *AltCmd;
66   int CmdCount;
67
68   Array<byte> GlobalData;
69   Array<byte> StaticData;
70   uint InitR[7];
71
72   byte *FilteredData;
73   unsigned int FilteredDataSize;
74 };
75
76 class RarVM:private BitInput
77 {
78   private:
79     inline uint GetValue(bool ByteMode,uint *Addr);
80     inline void SetValue(bool ByteMode,uint *Addr,uint Value);
81     inline uint* GetOperand(VM_PreparedOperand *CmdOp);
82     void PrintState(uint IP);
83     void DecodeArg(VM_PreparedOperand &Op,bool ByteMode);
84 #ifdef VM_OPTIMIZE
85     void Optimize(VM_PreparedProgram *Prg);
86 #endif
87     bool ExecuteCode(VM_PreparedCommand *PreparedCode,int CodeSize);
88 #ifdef VM_STANDARDFILTERS
89     VM_StandardFilters IsStandardFilter(byte *Code,int CodeSize);
90     void ExecuteStandardFilter(VM_StandardFilters FilterType);
91     unsigned int FilterItanium_GetBits(byte *Data,int BitPos,int BitCount);
92     void FilterItanium_SetBits(byte *Data,unsigned int BitField,int BitPos,
93       int BitCount);
94 #endif
95
96     byte *Mem;
97     uint R[8];
98     uint Flags;
99   public:
100     RarVM();
101     ~RarVM();
102     void Init();
103     void Prepare(byte *Code,int CodeSize,VM_PreparedProgram *Prg);
104     void Execute(VM_PreparedProgram *Prg);
105     void SetValue(uint *Addr,uint Value);
106     void SetMemory(unsigned int Pos,byte *Data,unsigned int DataSize);
107     static uint ReadData(BitInput &Inp);
108 };
109
110 #endif