diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/7zip/7z/CPP/7zip/Compress/Rar3Vm.h	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,179 @@
     1.4 +// Rar3Vm.h
     1.5 +// According to unRAR license, this code may not be used to develop
     1.6 +// a program that creates RAR archives
     1.7 +
     1.8 +#ifndef __COMPRESS_RAR3_VM_H
     1.9 +#define __COMPRESS_RAR3_VM_H
    1.10 +
    1.11 +#include "../../../C/CpuArch.h"
    1.12 +
    1.13 +#include "Common/MyVector.h"
    1.14 +#include "Common/Types.h"
    1.15 +
    1.16 +#define RARVM_STANDARD_FILTERS
    1.17 +
    1.18 +namespace NCompress {
    1.19 +namespace NRar3 {
    1.20 +
    1.21 +class CMemBitDecoder
    1.22 +{
    1.23 +  const Byte *_data;
    1.24 +  UInt32 _bitSize;
    1.25 +  UInt32 _bitPos;
    1.26 +public:
    1.27 +  void Init(const Byte *data, UInt32 byteSize)
    1.28 +  {
    1.29 +    _data = data;
    1.30 +    _bitSize = (byteSize << 3);
    1.31 +    _bitPos = 0;
    1.32 +  }
    1.33 +  UInt32 ReadBits(int numBits);
    1.34 +  UInt32 ReadBit();
    1.35 +  bool Avail() const { return (_bitPos < _bitSize); }
    1.36 +};
    1.37 +
    1.38 +namespace NVm {
    1.39 +
    1.40 +inline UInt32 GetValue32(const void *addr) { return GetUi32(addr); }
    1.41 +inline void SetValue32(void *addr, UInt32 value) { SetUi32(addr, value); }
    1.42 +
    1.43 +UInt32 ReadEncodedUInt32(CMemBitDecoder &inp);
    1.44 +
    1.45 +const int kNumRegBits = 3;
    1.46 +const UInt32 kNumRegs = 1 << kNumRegBits;
    1.47 +const UInt32 kNumGpRegs = kNumRegs - 1;
    1.48 +
    1.49 +const UInt32 kSpaceSize = 0x40000;
    1.50 +const UInt32 kSpaceMask = kSpaceSize -1;
    1.51 +const UInt32 kGlobalOffset = 0x3C000;
    1.52 +const UInt32 kGlobalSize = 0x2000;
    1.53 +const UInt32 kFixedGlobalSize = 64;
    1.54 +
    1.55 +namespace NGlobalOffset
    1.56 +{
    1.57 +  const UInt32 kBlockSize = 0x1C;
    1.58 +  const UInt32 kBlockPos  = 0x20;
    1.59 +  const UInt32 kExecCount = 0x2C;
    1.60 +  const UInt32 kGlobalMemOutSize = 0x30;
    1.61 +}
    1.62 +
    1.63 +enum ECommand
    1.64 +{
    1.65 +  CMD_MOV,  CMD_CMP,  CMD_ADD,  CMD_SUB,  CMD_JZ,   CMD_JNZ,  CMD_INC,  CMD_DEC,
    1.66 +  CMD_JMP,  CMD_XOR,  CMD_AND,  CMD_OR,   CMD_TEST, CMD_JS,   CMD_JNS,  CMD_JB,
    1.67 +  CMD_JBE,  CMD_JA,   CMD_JAE,  CMD_PUSH, CMD_POP,  CMD_CALL, CMD_RET,  CMD_NOT,
    1.68 +  CMD_SHL,  CMD_SHR,  CMD_SAR,  CMD_NEG,  CMD_PUSHA,CMD_POPA, CMD_PUSHF,CMD_POPF,
    1.69 +  CMD_MOVZX,CMD_MOVSX,CMD_XCHG, CMD_MUL,  CMD_DIV,  CMD_ADC,  CMD_SBB,  CMD_PRINT,
    1.70 +
    1.71 +  CMD_MOVB, CMD_CMPB, CMD_ADDB, CMD_SUBB, CMD_INCB, CMD_DECB,
    1.72 +  CMD_XORB, CMD_ANDB, CMD_ORB,  CMD_TESTB,CMD_NEGB,
    1.73 +  CMD_SHLB, CMD_SHRB, CMD_SARB, CMD_MULB
    1.74 +};
    1.75 +
    1.76 +enum EOpType {OP_TYPE_REG, OP_TYPE_INT, OP_TYPE_REGMEM, OP_TYPE_NONE};
    1.77 +
    1.78 +// Addr in COperand object can link (point) to CVm object!!!
    1.79 +
    1.80 +struct COperand
    1.81 +{
    1.82 +  EOpType Type;
    1.83 +  UInt32 Data;
    1.84 +  UInt32 Base;
    1.85 +  COperand(): Type(OP_TYPE_NONE), Data(0), Base(0) {}
    1.86 +};
    1.87 +
    1.88 +struct CCommand
    1.89 +{
    1.90 +  ECommand OpCode;
    1.91 +  bool ByteMode;
    1.92 +  COperand Op1, Op2;
    1.93 +};
    1.94 +
    1.95 +struct CBlockRef
    1.96 +{
    1.97 +  UInt32 Offset;
    1.98 +  UInt32 Size;
    1.99 +};
   1.100 +
   1.101 +struct CProgram
   1.102 +{
   1.103 +  CRecordVector<CCommand> Commands;
   1.104 +  #ifdef RARVM_STANDARD_FILTERS
   1.105 +  int StandardFilterIndex;
   1.106 +  #endif
   1.107 +  CRecordVector<Byte> StaticData;
   1.108 +};
   1.109 +
   1.110 +struct CProgramInitState
   1.111 +{
   1.112 +  UInt32 InitR[kNumGpRegs];
   1.113 +  CRecordVector<Byte> GlobalData;
   1.114 +
   1.115 +  void AllocateEmptyFixedGlobal()
   1.116 +  {
   1.117 +    GlobalData.Clear();
   1.118 +    GlobalData.Reserve(NVm::kFixedGlobalSize);
   1.119 +    for (UInt32 i = 0; i < NVm::kFixedGlobalSize; i++)
   1.120 +      GlobalData.Add(0);
   1.121 +  }
   1.122 +};
   1.123 +
   1.124 +class CVm
   1.125 +{
   1.126 +  static UInt32 GetValue(bool byteMode, const void *addr)
   1.127 +  {
   1.128 +    if (byteMode)
   1.129 +      return(*(const Byte *)addr);
   1.130 +    else
   1.131 +      return GetUi32(addr);
   1.132 +  }
   1.133 +
   1.134 +  static void SetValue(bool byteMode, void *addr, UInt32 value)
   1.135 +  {
   1.136 +    if (byteMode)
   1.137 +      *(Byte *)addr = (Byte)value;
   1.138 +    else
   1.139 +      SetUi32(addr, value);
   1.140 +  }
   1.141 +
   1.142 +  UInt32 GetFixedGlobalValue32(UInt32 globalOffset) { return GetValue(false, &Mem[kGlobalOffset + globalOffset]); }
   1.143 +
   1.144 +  void SetBlockSize(UInt32 v) { SetValue(&Mem[kGlobalOffset + NGlobalOffset::kBlockSize], v); }
   1.145 +  void SetBlockPos(UInt32 v) { SetValue(&Mem[kGlobalOffset + NGlobalOffset::kBlockPos], v); }
   1.146 +public:
   1.147 +  static void SetValue(void *addr, UInt32 value) { SetValue(false, addr, value); }
   1.148 +private:
   1.149 +  UInt32 GetOperand32(const COperand *op) const;
   1.150 +  void SetOperand32(const COperand *op, UInt32 val);
   1.151 +  Byte GetOperand8(const COperand *op) const;
   1.152 +  void SetOperand8(const COperand *op, Byte val);
   1.153 +  UInt32 GetOperand(bool byteMode, const COperand *op) const;
   1.154 +  void SetOperand(bool byteMode, const COperand *op, UInt32 val);
   1.155 +
   1.156 +  void DecodeArg(CMemBitDecoder &inp, COperand &op, bool byteMode);
   1.157 +  
   1.158 +  bool ExecuteCode(const CProgram *prg);
   1.159 +  
   1.160 +  #ifdef RARVM_STANDARD_FILTERS
   1.161 +  void ExecuteStandardFilter(int filterIndex);
   1.162 +  #endif
   1.163 +  
   1.164 +  Byte *Mem;
   1.165 +  UInt32 R[kNumRegs + 1]; // R[kNumRegs] = 0 always (speed optimization)
   1.166 +  UInt32 Flags;
   1.167 +  void ReadVmProgram(const Byte *code, UInt32 codeSize, CProgram *prg);
   1.168 +public:
   1.169 +  CVm();
   1.170 +  ~CVm();
   1.171 +  bool Create();
   1.172 +  void PrepareProgram(const Byte *code, UInt32 codeSize, CProgram *prg);
   1.173 +  void SetMemory(UInt32 pos, const Byte *data, UInt32 dataSize);
   1.174 +  bool Execute(CProgram *prg, const CProgramInitState *initState,
   1.175 +      CBlockRef &outBlockRef, CRecordVector<Byte> &outGlobalData);
   1.176 +  const Byte *GetDataPointer(UInt32 offset) const { return Mem + offset; }
   1.177 +
   1.178 +};
   1.179 +
   1.180 +#endif
   1.181 +
   1.182 +}}}