1 #ifndef _RAR_UNPACK_ 2 #define _RAR_UNPACK_ 3 4 enum BLOCK_TYPES {BLOCK_LZ,BLOCK_PPM}; 5 6 // Maximum allowed number of compressed bits processed in quick mode. 7 #define MAX_QUICK_DECODE_BITS 10 8 9 // Decode compressed bit fields to alphabet numbers. 10 struct DecodeTable 11 { 12 // Real size of DecodeNum table. 13 uint MaxNum; 14 15 // Left aligned start and upper limit codes defining code space 16 // ranges for bit lengths. DecodeLen[BitLength-1] defines the start of 17 // range for bit length and DecodeLen[BitLength] defines next code 18 // after the end of range or in other words the upper limit code 19 // for specified bit length. 20 uint DecodeLen[16]; 21 22 // Every item of this array contains the sum of all preceding items. 23 // So it contains the start position in code list for every bit length. 24 uint DecodePos[16]; 25 26 // Number of compressed bits processed in quick mode. 27 // Must not exceed MAX_QUICK_DECODE_BITS. 28 uint QuickBits; 29 30 // Translates compressed bits (up to QuickBits length) 31 // to bit length in quick mode. 32 byte QuickLen[1<<MAX_QUICK_DECODE_BITS]; 33 34 // Translates compressed bits (up to QuickBits length) 35 // to position in alphabet in quick mode. 36 uint QuickNum[1<<MAX_QUICK_DECODE_BITS]; 37 38 // Translate the position in code list to position in alphabet. 39 // We do not allocate it dynamically to avoid performance overhead 40 // introduced by pointer, so we use the largest possible table size 41 // as array dimension. Real size of this array is defined in MaxNum. 42 // We use this array if compressed bit field is too lengthy 43 // for QuickLen based translation. 44 uint DecodeNum[LARGEST_TABLE_SIZE]; 45 }; 46 47 struct UnpackFilter 48 { 49 unsigned int BlockStart; 50 unsigned int BlockLength; 51 unsigned int ExecCount; 52 bool NextWindow; 53 54 // position of parent filter in Filters array used as prototype for filter 55 // in PrgStack array. Not defined for filters in Filters array. 56 unsigned int ParentFilter; 57 58 VM_PreparedProgram Prg; 59 }; 60 61 62 struct AudioVariables // For RAR 2.0 archives only. 63 { 64 int K1,K2,K3,K4,K5; 65 int D1,D2,D3,D4; 66 int LastDelta; 67 unsigned int Dif[11]; 68 unsigned int ByteCount; 69 int LastChar; 70 }; 71 72 73 class Unpack:private BitInput 74 { 75 private: 76 friend class Pack; 77 78 void Unpack29(bool Solid); 79 bool UnpReadBuf(); 80 void UnpWriteBuf(); 81 void ExecuteCode(VM_PreparedProgram *Prg); 82 void UnpWriteArea(unsigned int StartPtr,unsigned int EndPtr); 83 void UnpWriteData(byte *Data,size_t Size); 84 bool ReadTables(); 85 void MakeDecodeTables(byte *LengthTable,DecodeTable *Dec,uint Size); 86 _forceinline uint DecodeNumber(DecodeTable *Dec); 87 inline int SafePPMDecodeChar(); 88 void CopyString(); 89 inline void InsertOldDist(unsigned int Distance); 90 inline void InsertLastMatch(unsigned int Length,unsigned int Distance); 91 void UnpInitData(int Solid); 92 _forceinline void CopyString(uint Length,uint Distance); 93 bool ReadEndOfBlock(); 94 bool ReadVMCode(); 95 bool ReadVMCodePPM(); 96 bool AddVMCode(unsigned int FirstByte,byte *Code,int CodeSize); 97 void InitFilters(); 98 99 ComprDataIO *UnpIO; 100 ModelPPM PPM; 101 int PPMEscChar; 102 103 // Virtual machine to execute filters code. 104 RarVM VM; 105 106 // Buffer to read VM filters code. We moved it here from AddVMCode 107 // function to reduce time spent in BitInput constructor. 108 BitInput VMCodeInp; 109 110 // Filters code, one entry per filter. 111 Array<UnpackFilter*> Filters; 112 113 // Filters stack, several entrances of same filter are possible. 114 Array<UnpackFilter*> PrgStack; 115 116 // Lengths of preceding data blocks, one length of one last block 117 // for every filter. Used to reduce the size required to write 118 // the data block length if lengths are repeating. 119 Array<int> OldFilterLengths; 120 121 int LastFilter; 122 123 bool TablesRead; 124 125 DecodeTable LD; // Decode literals. 126 DecodeTable DD; // Decode distances. 127 DecodeTable LDD; // Decode lower bits of distances. 128 DecodeTable RD; // Decode repeating distances. 129 DecodeTable BD; // Decod bit lengths in Huffman table. 130 131 unsigned int OldDist[4],OldDistPtr; 132 unsigned int LastDist,LastLength; 133 134 unsigned int UnpPtr,WrPtr; 135 136 // Top border of read packed data. 137 int ReadTop; 138 139 // Border to call UnpReadBuf. We use it instead of (ReadTop-C) 140 // for optimization reasons. Ensures that we have C bytes in buffer 141 // unless we are at the end of file. 142 int ReadBorder; 143 144 unsigned char UnpOldTable[HUFF_TABLE_SIZE]; 145 146 int UnpBlockType; 147 148 byte *Window; 149 bool ExternalWindow; 150 151 152 int64 DestUnpSize; 153 154 bool Suspended; 155 bool UnpAllBuf; 156 bool UnpSomeRead; 157 int64 WrittenFileSize; 158 bool FileExtracted; 159 160 int PrevLowDist,LowDistRepCount; 161 162 /***************************** Unpack v 1.5 *********************************/ 163 void Unpack15(bool Solid); 164 void ShortLZ(); 165 void LongLZ(); 166 void HuffDecode(); 167 void GetFlagsBuf(); 168 void OldUnpInitData(int Solid); 169 void InitHuff(); 170 void CorrHuff(unsigned int *CharSet,unsigned int *NumToPlace); 171 void OldCopyString(unsigned int Distance,unsigned int Length); 172 uint DecodeNum(uint Num,uint StartPos,uint *DecTab,uint *PosTab); 173 void OldUnpWriteBuf(); 174 175 unsigned int ChSet[256],ChSetA[256],ChSetB[256],ChSetC[256]; 176 unsigned int Place[256],PlaceA[256],PlaceB[256],PlaceC[256]; 177 unsigned int NToPl[256],NToPlB[256],NToPlC[256]; 178 unsigned int FlagBuf,AvrPlc,AvrPlcB,AvrLn1,AvrLn2,AvrLn3; 179 int Buf60,NumHuf,StMode,LCount,FlagsCnt; 180 unsigned int Nhfb,Nlzb,MaxDist3; 181 /***************************** Unpack v 1.5 *********************************/ 182 183 /***************************** Unpack v 2.0 *********************************/ 184 void Unpack20(bool Solid); 185 186 DecodeTable MD[4]; // Decode multimedia data, up to 4 channels. 187 188 unsigned char UnpOldTable20[MC20*4]; 189 int UnpAudioBlock,UnpChannels,UnpCurChannel,UnpChannelDelta; 190 void CopyString20(unsigned int Length,unsigned int Distance); 191 bool ReadTables20(); 192 void UnpInitData20(int Solid); 193 void ReadLastTables(); 194 byte DecodeAudio(int Delta); 195 struct AudioVariables AudV[4]; 196 /***************************** Unpack v 2.0 *********************************/ 197 198 public: 199 Unpack(ComprDataIO *DataIO); 200 ~Unpack(); 201 void Init(byte *Window=NULL); 202 void DoUnpack(int Method,bool Solid); IsFileExtracted()203 bool IsFileExtracted() {return(FileExtracted);} SetDestSize(int64 DestSize)204 void SetDestSize(int64 DestSize) {DestUnpSize=DestSize;FileExtracted=false;} SetSuspended(bool Suspended)205 void SetSuspended(bool Suspended) {Unpack::Suspended=Suspended;} 206 GetChar()207 unsigned int GetChar() 208 { 209 if (InAddr>BitInput::MAX_SIZE-30) 210 UnpReadBuf(); 211 return(InBuf[InAddr++]); 212 } 213 }; 214 215 #endif 216