view src/win32/7zip/7z/CPP/7zip/Compress/Rar3Vm.h @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line source
1 // Rar3Vm.h
2 // According to unRAR license, this code may not be used to develop
3 // a program that creates RAR archives
5 #ifndef __COMPRESS_RAR3_VM_H
6 #define __COMPRESS_RAR3_VM_H
8 #include "../../../C/CpuArch.h"
10 #include "Common/MyVector.h"
11 #include "Common/Types.h"
13 #define RARVM_STANDARD_FILTERS
15 namespace NCompress {
16 namespace NRar3 {
18 class CMemBitDecoder
19 {
20 const Byte *_data;
21 UInt32 _bitSize;
22 UInt32 _bitPos;
23 public:
24 void Init(const Byte *data, UInt32 byteSize)
25 {
26 _data = data;
27 _bitSize = (byteSize << 3);
28 _bitPos = 0;
29 }
30 UInt32 ReadBits(int numBits);
31 UInt32 ReadBit();
32 bool Avail() const { return (_bitPos < _bitSize); }
33 };
35 namespace NVm {
37 inline UInt32 GetValue32(const void *addr) { return GetUi32(addr); }
38 inline void SetValue32(void *addr, UInt32 value) { SetUi32(addr, value); }
40 UInt32 ReadEncodedUInt32(CMemBitDecoder &inp);
42 const int kNumRegBits = 3;
43 const UInt32 kNumRegs = 1 << kNumRegBits;
44 const UInt32 kNumGpRegs = kNumRegs - 1;
46 const UInt32 kSpaceSize = 0x40000;
47 const UInt32 kSpaceMask = kSpaceSize -1;
48 const UInt32 kGlobalOffset = 0x3C000;
49 const UInt32 kGlobalSize = 0x2000;
50 const UInt32 kFixedGlobalSize = 64;
52 namespace NGlobalOffset
53 {
54 const UInt32 kBlockSize = 0x1C;
55 const UInt32 kBlockPos = 0x20;
56 const UInt32 kExecCount = 0x2C;
57 const UInt32 kGlobalMemOutSize = 0x30;
58 }
60 enum ECommand
61 {
62 CMD_MOV, CMD_CMP, CMD_ADD, CMD_SUB, CMD_JZ, CMD_JNZ, CMD_INC, CMD_DEC,
63 CMD_JMP, CMD_XOR, CMD_AND, CMD_OR, CMD_TEST, CMD_JS, CMD_JNS, CMD_JB,
64 CMD_JBE, CMD_JA, CMD_JAE, CMD_PUSH, CMD_POP, CMD_CALL, CMD_RET, CMD_NOT,
65 CMD_SHL, CMD_SHR, CMD_SAR, CMD_NEG, CMD_PUSHA,CMD_POPA, CMD_PUSHF,CMD_POPF,
66 CMD_MOVZX,CMD_MOVSX,CMD_XCHG, CMD_MUL, CMD_DIV, CMD_ADC, CMD_SBB, CMD_PRINT,
68 CMD_MOVB, CMD_CMPB, CMD_ADDB, CMD_SUBB, CMD_INCB, CMD_DECB,
69 CMD_XORB, CMD_ANDB, CMD_ORB, CMD_TESTB,CMD_NEGB,
70 CMD_SHLB, CMD_SHRB, CMD_SARB, CMD_MULB
71 };
73 enum EOpType {OP_TYPE_REG, OP_TYPE_INT, OP_TYPE_REGMEM, OP_TYPE_NONE};
75 // Addr in COperand object can link (point) to CVm object!!!
77 struct COperand
78 {
79 EOpType Type;
80 UInt32 Data;
81 UInt32 Base;
82 COperand(): Type(OP_TYPE_NONE), Data(0), Base(0) {}
83 };
85 struct CCommand
86 {
87 ECommand OpCode;
88 bool ByteMode;
89 COperand Op1, Op2;
90 };
92 struct CBlockRef
93 {
94 UInt32 Offset;
95 UInt32 Size;
96 };
98 struct CProgram
99 {
100 CRecordVector<CCommand> Commands;
101 #ifdef RARVM_STANDARD_FILTERS
102 int StandardFilterIndex;
103 #endif
104 CRecordVector<Byte> StaticData;
105 };
107 struct CProgramInitState
108 {
109 UInt32 InitR[kNumGpRegs];
110 CRecordVector<Byte> GlobalData;
112 void AllocateEmptyFixedGlobal()
113 {
114 GlobalData.Clear();
115 GlobalData.Reserve(NVm::kFixedGlobalSize);
116 for (UInt32 i = 0; i < NVm::kFixedGlobalSize; i++)
117 GlobalData.Add(0);
118 }
119 };
121 class CVm
122 {
123 static UInt32 GetValue(bool byteMode, const void *addr)
124 {
125 if (byteMode)
126 return(*(const Byte *)addr);
127 else
128 return GetUi32(addr);
129 }
131 static void SetValue(bool byteMode, void *addr, UInt32 value)
132 {
133 if (byteMode)
134 *(Byte *)addr = (Byte)value;
135 else
136 SetUi32(addr, value);
137 }
139 UInt32 GetFixedGlobalValue32(UInt32 globalOffset) { return GetValue(false, &Mem[kGlobalOffset + globalOffset]); }
141 void SetBlockSize(UInt32 v) { SetValue(&Mem[kGlobalOffset + NGlobalOffset::kBlockSize], v); }
142 void SetBlockPos(UInt32 v) { SetValue(&Mem[kGlobalOffset + NGlobalOffset::kBlockPos], v); }
143 public:
144 static void SetValue(void *addr, UInt32 value) { SetValue(false, addr, value); }
145 private:
146 UInt32 GetOperand32(const COperand *op) const;
147 void SetOperand32(const COperand *op, UInt32 val);
148 Byte GetOperand8(const COperand *op) const;
149 void SetOperand8(const COperand *op, Byte val);
150 UInt32 GetOperand(bool byteMode, const COperand *op) const;
151 void SetOperand(bool byteMode, const COperand *op, UInt32 val);
153 void DecodeArg(CMemBitDecoder &inp, COperand &op, bool byteMode);
155 bool ExecuteCode(const CProgram *prg);
157 #ifdef RARVM_STANDARD_FILTERS
158 void ExecuteStandardFilter(int filterIndex);
159 #endif
161 Byte *Mem;
162 UInt32 R[kNumRegs + 1]; // R[kNumRegs] = 0 always (speed optimization)
163 UInt32 Flags;
164 void ReadVmProgram(const Byte *code, UInt32 codeSize, CProgram *prg);
165 public:
166 CVm();
167 ~CVm();
168 bool Create();
169 void PrepareProgram(const Byte *code, UInt32 codeSize, CProgram *prg);
170 void SetMemory(UInt32 pos, const Byte *data, UInt32 dataSize);
171 bool Execute(CProgram *prg, const CProgramInitState *initState,
172 CBlockRef &outBlockRef, CRecordVector<Byte> &outGlobalData);
173 const Byte *GetDataPointer(UInt32 offset) const { return Mem + offset; }
175 };
177 #endif
179 }}}