unpack.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #include "rar.hpp"
  2. #include "coder.cpp"
  3. #include "suballoc.cpp"
  4. #include "model.cpp"
  5. #include "unpackinline.cpp"
  6. #ifdef RAR_SMP
  7. #include "unpack50mt.cpp"
  8. #endif
  9. #ifndef SFX_MODULE
  10. #include "unpack15.cpp"
  11. #include "unpack20.cpp"
  12. #endif
  13. #include "unpack30.cpp"
  14. #include "unpack50.cpp"
  15. #include "unpack50frag.cpp"
  16. Unpack::Unpack(ComprDataIO *DataIO)
  17. :Inp(true),VMCodeInp(true)
  18. {
  19. UnpIO=DataIO;
  20. Window=NULL;
  21. Fragmented=false;
  22. Suspended=false;
  23. UnpAllBuf=false;
  24. UnpSomeRead=false;
  25. #ifdef RAR_SMP
  26. MaxUserThreads=1;
  27. UnpThreadPool=NULL;
  28. ReadBufMT=NULL;
  29. UnpThreadData=NULL;
  30. #endif
  31. MaxWinSize=0;
  32. MaxWinMask=0;
  33. // Perform initialization, which should be done only once for all files.
  34. // It prevents crash if first DoUnpack call is later made with wrong
  35. // (true) 'Solid' value.
  36. UnpInitData(false);
  37. #ifndef SFX_MODULE
  38. // RAR 1.5 decompression initialization
  39. UnpInitData15(false);
  40. InitHuff();
  41. #endif
  42. }
  43. Unpack::~Unpack()
  44. {
  45. InitFilters30(false);
  46. if (Window!=NULL)
  47. free(Window);
  48. #ifdef RAR_SMP
  49. delete UnpThreadPool;
  50. delete[] ReadBufMT;
  51. delete[] UnpThreadData;
  52. #endif
  53. }
  54. #ifdef RAR_SMP
  55. void Unpack::SetThreads(uint Threads)
  56. {
  57. // More than 8 threads are unlikely to provide noticeable gain
  58. // for unpacking, but would use the additional memory.
  59. MaxUserThreads=Min(Threads,8);
  60. UnpThreadPool=new ThreadPool(MaxUserThreads);
  61. }
  62. #endif
  63. void Unpack::Init(size_t WinSize,bool Solid)
  64. {
  65. // If 32-bit RAR unpacks an archive with 4 GB dictionary, the window size
  66. // will be 0 because of size_t overflow. Let's issue the memory error.
  67. if (WinSize==0)
  68. ErrHandler.MemoryError();
  69. // Minimum window size must be at least twice more than maximum possible
  70. // size of filter block, which is 0x10000 in RAR now. If window size is
  71. // smaller, we can have a block with never cleared flt->NextWindow flag
  72. // in UnpWriteBuf(). Minimum window size 0x20000 would be enough, but let's
  73. // use 0x40000 for extra safety and possible filter area size expansion.
  74. const size_t MinAllocSize=0x40000;
  75. if (WinSize<MinAllocSize)
  76. WinSize=MinAllocSize;
  77. if (WinSize<=MaxWinSize) // Use the already allocated window.
  78. return;
  79. if ((WinSize>>16)>0x10000) // Window size must not exceed 4 GB.
  80. return;
  81. // Archiving code guarantees that window size does not grow in the same
  82. // solid stream. So if we are here, we are either creating a new window
  83. // or increasing the size of non-solid window. So we could safely reject
  84. // current window data without copying them to a new window, though being
  85. // extra cautious, we still handle the solid window grow case below.
  86. bool Grow=Solid && (Window!=NULL || Fragmented);
  87. // We do not handle growth for existing fragmented window.
  88. if (Grow && Fragmented)
  89. throw std::bad_alloc();
  90. byte *NewWindow=Fragmented ? NULL : (byte *)malloc(WinSize);
  91. if (NewWindow==NULL)
  92. if (Grow || WinSize<0x1000000)
  93. {
  94. // We do not support growth for new fragmented window.
  95. // Also exclude RAR4 and small dictionaries.
  96. throw std::bad_alloc();
  97. }
  98. else
  99. {
  100. if (Window!=NULL) // If allocated by preceding files.
  101. {
  102. free(Window);
  103. Window=NULL;
  104. }
  105. FragWindow.Init(WinSize);
  106. Fragmented=true;
  107. }
  108. if (!Fragmented)
  109. {
  110. // Clean the window to generate the same output when unpacking corrupt
  111. // RAR files, which may access unused areas of sliding dictionary.
  112. memset(NewWindow,0,WinSize);
  113. // If Window is not NULL, it means that window size has grown.
  114. // In solid streams we need to copy data to a new window in such case.
  115. // RAR archiving code does not allow it in solid streams now,
  116. // but let's implement it anyway just in case we'll change it sometimes.
  117. if (Grow)
  118. for (size_t I=1;I<=MaxWinSize;I++)
  119. NewWindow[(UnpPtr-I)&(WinSize-1)]=Window[(UnpPtr-I)&(MaxWinSize-1)];
  120. if (Window!=NULL)
  121. free(Window);
  122. Window=NewWindow;
  123. }
  124. MaxWinSize=WinSize;
  125. MaxWinMask=MaxWinSize-1;
  126. }
  127. void Unpack::DoUnpack(uint Method,bool Solid)
  128. {
  129. // Methods <50 will crash in Fragmented mode when accessing NULL Window.
  130. // They cannot be called in such mode now, but we check it below anyway
  131. // just for extra safety.
  132. switch(Method)
  133. {
  134. #ifndef SFX_MODULE
  135. case 15: // rar 1.5 compression
  136. if (!Fragmented)
  137. Unpack15(Solid);
  138. break;
  139. case 20: // rar 2.x compression
  140. case 26: // files larger than 2GB
  141. if (!Fragmented)
  142. Unpack20(Solid);
  143. break;
  144. #endif
  145. case 29: // rar 3.x compression
  146. if (!Fragmented)
  147. Unpack29(Solid);
  148. break;
  149. case 50: // RAR 5.0 compression algorithm.
  150. #ifdef RAR_SMP
  151. if (MaxUserThreads>1)
  152. {
  153. // We do not use the multithreaded unpack routine to repack RAR archives
  154. // in 'suspended' mode, because unlike the single threaded code it can
  155. // write more than one dictionary for same loop pass. So we would need
  156. // larger buffers of unknown size. Also we do not support multithreading
  157. // in fragmented window mode.
  158. if (!Fragmented)
  159. {
  160. Unpack5MT(Solid);
  161. break;
  162. }
  163. }
  164. #endif
  165. Unpack5(Solid);
  166. break;
  167. }
  168. }
  169. void Unpack::UnpInitData(bool Solid)
  170. {
  171. if (!Solid)
  172. {
  173. memset(OldDist,0,sizeof(OldDist));
  174. OldDistPtr=0;
  175. LastDist=LastLength=0;
  176. // memset(Window,0,MaxWinSize);
  177. memset(&BlockTables,0,sizeof(BlockTables));
  178. UnpPtr=WrPtr=0;
  179. WriteBorder=Min(MaxWinSize,UNPACK_MAX_WRITE)&MaxWinMask;
  180. }
  181. // Filters never share several solid files, so we can safely reset them
  182. // even in solid archive.
  183. InitFilters();
  184. Inp.InitBitInput();
  185. WrittenFileSize=0;
  186. ReadTop=0;
  187. ReadBorder=0;
  188. memset(&BlockHeader,0,sizeof(BlockHeader));
  189. BlockHeader.BlockSize=-1; // '-1' means not defined yet.
  190. #ifndef SFX_MODULE
  191. UnpInitData20(Solid);
  192. #endif
  193. UnpInitData30(Solid);
  194. UnpInitData50(Solid);
  195. }
  196. // LengthTable contains the length in bits for every element of alphabet.
  197. // Dec is the structure to decode Huffman code/
  198. // Size is size of length table and DecodeNum field in Dec structure,
  199. void Unpack::MakeDecodeTables(byte *LengthTable,DecodeTable *Dec,uint Size)
  200. {
  201. // Size of alphabet and DecodePos array.
  202. Dec->MaxNum=Size;
  203. // Calculate how many entries for every bit length in LengthTable we have.
  204. uint LengthCount[16];
  205. memset(LengthCount,0,sizeof(LengthCount));
  206. for (size_t I=0;I<Size;I++)
  207. LengthCount[LengthTable[I] & 0xf]++;
  208. // We must not calculate the number of zero length codes.
  209. LengthCount[0]=0;
  210. // Set the entire DecodeNum to zero.
  211. memset(Dec->DecodeNum,0,Size*sizeof(*Dec->DecodeNum));
  212. // Initialize not really used entry for zero length code.
  213. Dec->DecodePos[0]=0;
  214. // Start code for bit length 1 is 0.
  215. Dec->DecodeLen[0]=0;
  216. // Right aligned upper limit code for current bit length.
  217. uint UpperLimit=0;
  218. for (size_t I=1;I<16;I++)
  219. {
  220. // Adjust the upper limit code.
  221. UpperLimit+=LengthCount[I];
  222. // Left aligned upper limit code.
  223. uint LeftAligned=UpperLimit<<(16-I);
  224. // Prepare the upper limit code for next bit length.
  225. UpperLimit*=2;
  226. // Store the left aligned upper limit code.
  227. Dec->DecodeLen[I]=(uint)LeftAligned;
  228. // Every item of this array contains the sum of all preceding items.
  229. // So it contains the start position in code list for every bit length.
  230. Dec->DecodePos[I]=Dec->DecodePos[I-1]+LengthCount[I-1];
  231. }
  232. // Prepare the copy of DecodePos. We'll modify this copy below,
  233. // so we cannot use the original DecodePos.
  234. uint CopyDecodePos[ASIZE(Dec->DecodePos)];
  235. memcpy(CopyDecodePos,Dec->DecodePos,sizeof(CopyDecodePos));
  236. // For every bit length in the bit length table and so for every item
  237. // of alphabet.
  238. for (uint I=0;I<Size;I++)
  239. {
  240. // Get the current bit length.
  241. byte CurBitLength=LengthTable[I] & 0xf;
  242. if (CurBitLength!=0)
  243. {
  244. // Last position in code list for current bit length.
  245. uint LastPos=CopyDecodePos[CurBitLength];
  246. // Prepare the decode table, so this position in code list will be
  247. // decoded to current alphabet item number.
  248. Dec->DecodeNum[LastPos]=(ushort)I;
  249. // We'll use next position number for this bit length next time.
  250. // So we pass through the entire range of positions available
  251. // for every bit length.
  252. CopyDecodePos[CurBitLength]++;
  253. }
  254. }
  255. // Define the number of bits to process in quick mode. We use more bits
  256. // for larger alphabets. More bits means that more codes will be processed
  257. // in quick mode, but also that more time will be spent to preparation
  258. // of tables for quick decode.
  259. switch (Size)
  260. {
  261. case NC:
  262. case NC20:
  263. case NC30:
  264. Dec->QuickBits=MAX_QUICK_DECODE_BITS;
  265. break;
  266. default:
  267. Dec->QuickBits=MAX_QUICK_DECODE_BITS-3;
  268. break;
  269. }
  270. // Size of tables for quick mode.
  271. uint QuickDataSize=1<<Dec->QuickBits;
  272. // Bit length for current code, start from 1 bit codes. It is important
  273. // to use 1 bit instead of 0 for minimum code length, so we are moving
  274. // forward even when processing a corrupt archive.
  275. uint CurBitLength=1;
  276. // For every right aligned bit string which supports the quick decoding.
  277. for (uint Code=0;Code<QuickDataSize;Code++)
  278. {
  279. // Left align the current code, so it will be in usual bit field format.
  280. uint BitField=Code<<(16-Dec->QuickBits);
  281. // Prepare the table for quick decoding of bit lengths.
  282. // Find the upper limit for current bit field and adjust the bit length
  283. // accordingly if necessary.
  284. while (CurBitLength<ASIZE(Dec->DecodeLen) && BitField>=Dec->DecodeLen[CurBitLength])
  285. CurBitLength++;
  286. // Translation of right aligned bit string to bit length.
  287. Dec->QuickLen[Code]=CurBitLength;
  288. // Prepare the table for quick translation of position in code list
  289. // to position in alphabet.
  290. // Calculate the distance from the start code for current bit length.
  291. uint Dist=BitField-Dec->DecodeLen[CurBitLength-1];
  292. // Right align the distance.
  293. Dist>>=(16-CurBitLength);
  294. // Now we can calculate the position in the code list. It is the sum
  295. // of first position for current bit length and right aligned distance
  296. // between our bit field and start code for current bit length.
  297. uint Pos;
  298. if (CurBitLength<ASIZE(Dec->DecodePos) &&
  299. (Pos=Dec->DecodePos[CurBitLength]+Dist)<Size)
  300. {
  301. // Define the code to alphabet number translation.
  302. Dec->QuickNum[Code]=Dec->DecodeNum[Pos];
  303. }
  304. else
  305. {
  306. // Can be here for length table filled with zeroes only (empty).
  307. Dec->QuickNum[Code]=0;
  308. }
  309. }
  310. }