unpack.hpp 6.7 KB

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