unpack50mt.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. #define UNP_READ_SIZE_MT 0x400000
  2. #define UNP_BLOCKS_PER_THREAD 2
  3. struct UnpackThreadDataList
  4. {
  5. UnpackThreadData *D;
  6. uint BlockCount;
  7. };
  8. THREAD_PROC(UnpackDecodeThread)
  9. {
  10. UnpackThreadDataList *DL=(UnpackThreadDataList *)Data;
  11. for (uint I=0;I<DL->BlockCount;I++)
  12. DL->D->UnpackPtr->UnpackDecode(DL->D[I]);
  13. }
  14. void Unpack::InitMT()
  15. {
  16. if (ReadBufMT==NULL)
  17. {
  18. // Even getbits32 can read up to 3 additional bytes after current
  19. // and our block header and table reading code can look much further.
  20. // Let's allocate the additional space here, so we do not need to check
  21. // bounds for every bit field access.
  22. const size_t Overflow=1024;
  23. ReadBufMT=new byte[UNP_READ_SIZE_MT+Overflow];
  24. memset(ReadBufMT,0,UNP_READ_SIZE_MT+Overflow);
  25. }
  26. if (UnpThreadData==NULL)
  27. {
  28. uint MaxItems=MaxUserThreads*UNP_BLOCKS_PER_THREAD;
  29. UnpThreadData=new UnpackThreadData[MaxItems];
  30. memset(UnpThreadData,0,sizeof(UnpackThreadData)*MaxItems);
  31. for (uint I=0;I<MaxItems;I++)
  32. {
  33. UnpackThreadData *CurData=UnpThreadData+I;
  34. if (CurData->Decoded==NULL)
  35. {
  36. // Typical number of items in RAR blocks does not exceed 0x4000.
  37. CurData->DecodedAllocated=0x4100;
  38. // It will be freed in the object destructor, not in this file.
  39. CurData->Decoded=(UnpackDecodedItem *)malloc(CurData->DecodedAllocated*sizeof(UnpackDecodedItem));
  40. if (CurData->Decoded==NULL)
  41. ErrHandler.MemoryError();
  42. }
  43. }
  44. }
  45. }
  46. void Unpack::Unpack5MT(bool Solid)
  47. {
  48. InitMT();
  49. UnpInitData(Solid);
  50. for (uint I=0;I<MaxUserThreads*UNP_BLOCKS_PER_THREAD;I++)
  51. {
  52. UnpackThreadData *CurData=UnpThreadData+I;
  53. CurData->LargeBlock=false;
  54. CurData->Incomplete=false;
  55. }
  56. UnpThreadData[0].BlockHeader=BlockHeader;
  57. UnpThreadData[0].BlockTables=BlockTables;
  58. uint LastBlockNum=0;
  59. int DataSize=0;
  60. int BlockStart=0;
  61. // 'true' if we found a block too large for multithreaded extraction,
  62. // so we switched to single threaded mode until the end of file.
  63. // Large blocks could cause too high memory use in multithreaded mode.
  64. bool LargeBlock=false;
  65. bool Done=false;
  66. while (!Done)
  67. {
  68. // Data amount, which is guaranteed to fit block header and tables,
  69. // so we can safely read them without additional checks.
  70. const int TooSmallToProcess=1024;
  71. int ReadSize=UnpIO->UnpRead(ReadBufMT+DataSize,(UNP_READ_SIZE_MT-DataSize)&~0xf);
  72. if (ReadSize<0)
  73. break;
  74. DataSize+=ReadSize;
  75. if (DataSize==0)
  76. break;
  77. // First read chunk can be small if we are near the end of volume
  78. // and we want it to fit block header and tables.
  79. if (ReadSize>0 && DataSize<TooSmallToProcess)
  80. continue;
  81. while (BlockStart<DataSize && !Done)
  82. {
  83. uint BlockNumber=0,BlockNumberMT=0;
  84. while (BlockNumber<MaxUserThreads*UNP_BLOCKS_PER_THREAD)
  85. {
  86. UnpackThreadData *CurData=UnpThreadData+BlockNumber;
  87. LastBlockNum=BlockNumber;
  88. CurData->UnpackPtr=this;
  89. // 'Incomplete' thread is present. This is a thread processing block
  90. // in the end of buffer, split between two read operations.
  91. if (CurData->Incomplete)
  92. CurData->DataSize=DataSize;
  93. else
  94. {
  95. CurData->Inp.SetExternalBuffer(ReadBufMT+BlockStart);
  96. CurData->Inp.InitBitInput();
  97. CurData->DataSize=DataSize-BlockStart;
  98. if (CurData->DataSize==0)
  99. break;
  100. CurData->DamagedData=false;
  101. CurData->HeaderRead=false;
  102. CurData->TableRead=false;
  103. }
  104. // We should not use 'last block in file' block flag here unless
  105. // we'll check the block size, because even if block is last in file,
  106. // it can exceed the current buffer and require more reading.
  107. CurData->NoDataLeft=(ReadSize==0);
  108. CurData->Incomplete=false;
  109. CurData->ThreadNumber=BlockNumber;
  110. if (!CurData->HeaderRead)
  111. {
  112. CurData->HeaderRead=true;
  113. if (!ReadBlockHeader(CurData->Inp,CurData->BlockHeader) ||
  114. !CurData->BlockHeader.TablePresent && !TablesRead5)
  115. {
  116. Done=true;
  117. break;
  118. }
  119. TablesRead5=true;
  120. }
  121. // To prevent too high memory use we switch to single threaded mode
  122. // if block exceeds this size. Typically RAR blocks do not exceed
  123. // 64 KB, so this protection should not affect most of valid archives.
  124. const int LargeBlockSize=0x20000;
  125. if (LargeBlock || CurData->BlockHeader.BlockSize>LargeBlockSize)
  126. LargeBlock=CurData->LargeBlock=true;
  127. else
  128. BlockNumberMT++; // Number of normal blocks processed in MT mode.
  129. BlockStart+=CurData->BlockHeader.HeaderSize+CurData->BlockHeader.BlockSize;
  130. BlockNumber++;
  131. int DataLeft=DataSize-BlockStart;
  132. if (DataLeft>=0 && CurData->BlockHeader.LastBlockInFile)
  133. break;
  134. // For second and following threads we move smaller blocks to buffer
  135. // start to ensure that we have enough data to fit block header
  136. // and tables.
  137. if (DataLeft<TooSmallToProcess)
  138. break;
  139. }
  140. //#undef USE_THREADS
  141. UnpackThreadDataList UTDArray[MaxPoolThreads];
  142. uint UTDArrayPos=0;
  143. uint MaxBlockPerThread=BlockNumberMT/MaxUserThreads;
  144. if (BlockNumberMT%MaxUserThreads!=0)
  145. MaxBlockPerThread++;
  146. // Decode all normal blocks until the first 'large' if any.
  147. for (uint CurBlock=0;CurBlock<BlockNumberMT;CurBlock+=MaxBlockPerThread)
  148. {
  149. UnpackThreadDataList *UTD=UTDArray+UTDArrayPos++;
  150. UTD->D=UnpThreadData+CurBlock;
  151. UTD->BlockCount=Min(MaxBlockPerThread,BlockNumberMT-CurBlock);
  152. #ifdef USE_THREADS
  153. if (BlockNumber==1)
  154. UnpackDecode(*UTD->D);
  155. else
  156. UnpThreadPool->AddTask(UnpackDecodeThread,(void*)UTD);
  157. #else
  158. for (uint I=0;I<UTD->BlockCount;I++)
  159. UnpackDecode(UTD->D[I]);
  160. #endif
  161. }
  162. if (BlockNumber==0)
  163. break;
  164. #ifdef USE_THREADS
  165. UnpThreadPool->WaitDone();
  166. #endif
  167. bool IncompleteThread=false;
  168. for (uint Block=0;Block<BlockNumber;Block++)
  169. {
  170. UnpackThreadData *CurData=UnpThreadData+Block;
  171. if (!CurData->LargeBlock && !ProcessDecoded(*CurData) ||
  172. CurData->LargeBlock && !UnpackLargeBlock(*CurData) ||
  173. CurData->DamagedData)
  174. {
  175. Done=true;
  176. break;
  177. }
  178. if (CurData->Incomplete)
  179. {
  180. int BufPos=int(CurData->Inp.InBuf+CurData->Inp.InAddr-ReadBufMT);
  181. if (DataSize<=BufPos) // Thread exceeded input buffer boundary.
  182. {
  183. Done=true;
  184. break;
  185. }
  186. IncompleteThread=true;
  187. memmove(ReadBufMT,ReadBufMT+BufPos,DataSize-BufPos);
  188. CurData->BlockHeader.BlockSize-=CurData->Inp.InAddr-CurData->BlockHeader.BlockStart;
  189. CurData->BlockHeader.HeaderSize=0;
  190. CurData->BlockHeader.BlockStart=0;
  191. CurData->Inp.InBuf=ReadBufMT;
  192. CurData->Inp.InAddr=0;
  193. if (Block!=0)
  194. {
  195. // Move the incomplete thread entry to the first position,
  196. // so we'll start processing from it. Preserve the original
  197. // buffer for decoded data.
  198. UnpackDecodedItem *Decoded=UnpThreadData[0].Decoded;
  199. uint DecodedAllocated=UnpThreadData[0].DecodedAllocated;
  200. UnpThreadData[0]=*CurData;
  201. UnpThreadData[0].Decoded=Decoded;
  202. UnpThreadData[0].DecodedAllocated=DecodedAllocated;
  203. CurData->Incomplete=false;
  204. }
  205. BlockStart=0;
  206. DataSize-=BufPos;
  207. break;
  208. }
  209. else
  210. if (CurData->BlockHeader.LastBlockInFile)
  211. {
  212. Done=true;
  213. break;
  214. }
  215. }
  216. if (IncompleteThread || Done)
  217. break; // Current buffer is done, read more data or quit.
  218. else
  219. {
  220. int DataLeft=DataSize-BlockStart;
  221. if (DataLeft<TooSmallToProcess)
  222. {
  223. if (DataLeft<0) // Invalid data, must not happen in valid archive.
  224. {
  225. Done=true;
  226. break;
  227. }
  228. // If we do not have incomplete thread and have some data
  229. // in the end of buffer, too small for single thread,
  230. // let's move it to beginning of next buffer.
  231. if (DataLeft>0)
  232. memmove(ReadBufMT,ReadBufMT+BlockStart,DataLeft);
  233. DataSize=DataLeft;
  234. BlockStart=0;
  235. break; // Current buffer is done, try to read more data.
  236. }
  237. }
  238. }
  239. }
  240. UnpPtr&=MaxWinMask; // ProcessDecoded and maybe others can leave UnpPtr > MaxWinMask here.
  241. UnpWriteBuf();
  242. BlockHeader=UnpThreadData[LastBlockNum].BlockHeader;
  243. BlockTables=UnpThreadData[LastBlockNum].BlockTables;
  244. }
  245. // Decode Huffman block and save decoded data to memory.
  246. void Unpack::UnpackDecode(UnpackThreadData &D)
  247. {
  248. if (!D.TableRead)
  249. {
  250. D.TableRead=true;
  251. if (!ReadTables(D.Inp,D.BlockHeader,D.BlockTables))
  252. {
  253. D.DamagedData=true;
  254. return;
  255. }
  256. }
  257. if (D.Inp.InAddr>D.BlockHeader.HeaderSize+D.BlockHeader.BlockSize)
  258. {
  259. D.DamagedData=true;
  260. return;
  261. }
  262. D.DecodedSize=0;
  263. int BlockBorder=D.BlockHeader.BlockStart+D.BlockHeader.BlockSize-1;
  264. // Reserve enough space even for filter entry.
  265. int DataBorder=D.DataSize-16;
  266. int ReadBorder=Min(BlockBorder,DataBorder);
  267. while (true)
  268. {
  269. if (D.Inp.InAddr>=ReadBorder)
  270. {
  271. if (D.Inp.InAddr>BlockBorder || D.Inp.InAddr==BlockBorder &&
  272. D.Inp.InBit>=D.BlockHeader.BlockBitSize)
  273. break;
  274. // If we do not have any more data in file to read, we must process
  275. // what we have until last byte. Otherwise we can return and append
  276. // more data to unprocessed few bytes.
  277. if ((D.Inp.InAddr>=DataBorder) && !D.NoDataLeft || D.Inp.InAddr>=D.DataSize)
  278. {
  279. D.Incomplete=true;
  280. break;
  281. }
  282. }
  283. if (D.DecodedSize>D.DecodedAllocated-8) // Filter can use several slots.
  284. {
  285. D.DecodedAllocated=D.DecodedAllocated*2;
  286. void *Decoded=realloc(D.Decoded,D.DecodedAllocated*sizeof(UnpackDecodedItem));
  287. if (Decoded==NULL)
  288. ErrHandler.MemoryError(); // D.Decoded will be freed in the destructor.
  289. D.Decoded=(UnpackDecodedItem *)Decoded;
  290. }
  291. UnpackDecodedItem *CurItem=D.Decoded+D.DecodedSize++;
  292. uint MainSlot=DecodeNumber(D.Inp,&D.BlockTables.LD);
  293. if (MainSlot<256)
  294. {
  295. if (D.DecodedSize>1)
  296. {
  297. UnpackDecodedItem *PrevItem=CurItem-1;
  298. if (PrevItem->Type==UNPDT_LITERAL && PrevItem->Length<3)
  299. {
  300. PrevItem->Length++;
  301. PrevItem->Literal[PrevItem->Length]=(byte)MainSlot;
  302. D.DecodedSize--;
  303. continue;
  304. }
  305. }
  306. CurItem->Type=UNPDT_LITERAL;
  307. CurItem->Literal[0]=(byte)MainSlot;
  308. CurItem->Length=0;
  309. continue;
  310. }
  311. if (MainSlot>=262)
  312. {
  313. uint Length=SlotToLength(D.Inp,MainSlot-262);
  314. uint DBits,Distance=1,DistSlot=DecodeNumber(D.Inp,&D.BlockTables.DD);
  315. if (DistSlot<4)
  316. {
  317. DBits=0;
  318. Distance+=DistSlot;
  319. }
  320. else
  321. {
  322. DBits=DistSlot/2 - 1;
  323. Distance+=(2 | (DistSlot & 1)) << DBits;
  324. }
  325. if (DBits>0)
  326. {
  327. if (DBits>=4)
  328. {
  329. if (DBits>4)
  330. {
  331. Distance+=((D.Inp.getbits32()>>(36-DBits))<<4);
  332. D.Inp.addbits(DBits-4);
  333. }
  334. uint LowDist=DecodeNumber(D.Inp,&D.BlockTables.LDD);
  335. Distance+=LowDist;
  336. }
  337. else
  338. {
  339. Distance+=D.Inp.getbits32()>>(32-DBits);
  340. D.Inp.addbits(DBits);
  341. }
  342. }
  343. if (Distance>0x100)
  344. {
  345. Length++;
  346. if (Distance>0x2000)
  347. {
  348. Length++;
  349. if (Distance>0x40000)
  350. Length++;
  351. }
  352. }
  353. CurItem->Type=UNPDT_MATCH;
  354. CurItem->Length=(ushort)Length;
  355. CurItem->Distance=Distance;
  356. continue;
  357. }
  358. if (MainSlot==256)
  359. {
  360. UnpackFilter Filter;
  361. ReadFilter(D.Inp,Filter);
  362. CurItem->Type=UNPDT_FILTER;
  363. CurItem->Length=Filter.Type;
  364. CurItem->Distance=Filter.BlockStart;
  365. CurItem=D.Decoded+D.DecodedSize++;
  366. CurItem->Type=UNPDT_FILTER;
  367. CurItem->Length=Filter.Channels;
  368. CurItem->Distance=Filter.BlockLength;
  369. continue;
  370. }
  371. if (MainSlot==257)
  372. {
  373. CurItem->Type=UNPDT_FULLREP;
  374. continue;
  375. }
  376. if (MainSlot<262)
  377. {
  378. CurItem->Type=UNPDT_REP;
  379. CurItem->Distance=MainSlot-258;
  380. uint LengthSlot=DecodeNumber(D.Inp,&D.BlockTables.RD);
  381. uint Length=SlotToLength(D.Inp,LengthSlot);
  382. CurItem->Length=(ushort)Length;
  383. continue;
  384. }
  385. }
  386. }
  387. // Process decoded Huffman block data.
  388. bool Unpack::ProcessDecoded(UnpackThreadData &D)
  389. {
  390. UnpackDecodedItem *Item=D.Decoded,*Border=D.Decoded+D.DecodedSize;
  391. while (Item<Border)
  392. {
  393. UnpPtr&=MaxWinMask;
  394. if (((WriteBorder-UnpPtr) & MaxWinMask)<MAX_INC_LZ_MATCH && WriteBorder!=UnpPtr)
  395. {
  396. UnpWriteBuf();
  397. if (WrittenFileSize>DestUnpSize)
  398. return false;
  399. }
  400. if (Item->Type==UNPDT_LITERAL)
  401. {
  402. #if defined(LITTLE_ENDIAN) && defined(ALLOW_MISALIGNED)
  403. if (Item->Length==3 && UnpPtr<MaxWinSize-4)
  404. {
  405. *(uint32 *)(Window+UnpPtr)=*(uint32 *)Item->Literal;
  406. UnpPtr+=4;
  407. }
  408. else
  409. #endif
  410. for (uint I=0;I<=Item->Length;I++)
  411. Window[UnpPtr++ & MaxWinMask]=Item->Literal[I];
  412. }
  413. else
  414. if (Item->Type==UNPDT_MATCH)
  415. {
  416. InsertOldDist(Item->Distance);
  417. LastLength=Item->Length;
  418. CopyString(Item->Length,Item->Distance);
  419. }
  420. else
  421. if (Item->Type==UNPDT_REP)
  422. {
  423. uint Distance=OldDist[Item->Distance];
  424. for (uint I=Item->Distance;I>0;I--)
  425. OldDist[I]=OldDist[I-1];
  426. OldDist[0]=Distance;
  427. LastLength=Item->Length;
  428. CopyString(Item->Length,Distance);
  429. }
  430. else
  431. if (Item->Type==UNPDT_FULLREP)
  432. {
  433. if (LastLength!=0)
  434. CopyString(LastLength,OldDist[0]);
  435. }
  436. else
  437. if (Item->Type==UNPDT_FILTER)
  438. {
  439. UnpackFilter Filter;
  440. Filter.Type=(byte)Item->Length;
  441. Filter.BlockStart=Item->Distance;
  442. Item++;
  443. Filter.Channels=(byte)Item->Length;
  444. Filter.BlockLength=Item->Distance;
  445. AddFilter(Filter);
  446. }
  447. Item++;
  448. }
  449. return true;
  450. }
  451. // For large blocks we decode and process in same function in single threaded
  452. // mode, so we do not need to store intermediate data in memory.
  453. bool Unpack::UnpackLargeBlock(UnpackThreadData &D)
  454. {
  455. if (!D.TableRead)
  456. {
  457. D.TableRead=true;
  458. if (!ReadTables(D.Inp,D.BlockHeader,D.BlockTables))
  459. {
  460. D.DamagedData=true;
  461. return false;
  462. }
  463. }
  464. if (D.Inp.InAddr>D.BlockHeader.HeaderSize+D.BlockHeader.BlockSize)
  465. {
  466. D.DamagedData=true;
  467. return false;
  468. }
  469. int BlockBorder=D.BlockHeader.BlockStart+D.BlockHeader.BlockSize-1;
  470. // Reserve enough space even for filter entry.
  471. int DataBorder=D.DataSize-16;
  472. int ReadBorder=Min(BlockBorder,DataBorder);
  473. while (true)
  474. {
  475. UnpPtr&=MaxWinMask;
  476. if (D.Inp.InAddr>=ReadBorder)
  477. {
  478. if (D.Inp.InAddr>BlockBorder || D.Inp.InAddr==BlockBorder &&
  479. D.Inp.InBit>=D.BlockHeader.BlockBitSize)
  480. break;
  481. // If we do not have any more data in file to read, we must process
  482. // what we have until last byte. Otherwise we can return and append
  483. // more data to unprocessed few bytes.
  484. if ((D.Inp.InAddr>=DataBorder) && !D.NoDataLeft || D.Inp.InAddr>=D.DataSize)
  485. {
  486. D.Incomplete=true;
  487. break;
  488. }
  489. }
  490. if (((WriteBorder-UnpPtr) & MaxWinMask)<MAX_INC_LZ_MATCH && WriteBorder!=UnpPtr)
  491. {
  492. UnpWriteBuf();
  493. if (WrittenFileSize>DestUnpSize)
  494. return false;
  495. }
  496. uint MainSlot=DecodeNumber(D.Inp,&D.BlockTables.LD);
  497. if (MainSlot<256)
  498. {
  499. Window[UnpPtr++]=(byte)MainSlot;
  500. continue;
  501. }
  502. if (MainSlot>=262)
  503. {
  504. uint Length=SlotToLength(D.Inp,MainSlot-262);
  505. uint DBits,Distance=1,DistSlot=DecodeNumber(D.Inp,&D.BlockTables.DD);
  506. if (DistSlot<4)
  507. {
  508. DBits=0;
  509. Distance+=DistSlot;
  510. }
  511. else
  512. {
  513. DBits=DistSlot/2 - 1;
  514. Distance+=(2 | (DistSlot & 1)) << DBits;
  515. }
  516. if (DBits>0)
  517. {
  518. if (DBits>=4)
  519. {
  520. if (DBits>4)
  521. {
  522. Distance+=((D.Inp.getbits32()>>(36-DBits))<<4);
  523. D.Inp.addbits(DBits-4);
  524. }
  525. uint LowDist=DecodeNumber(D.Inp,&D.BlockTables.LDD);
  526. Distance+=LowDist;
  527. }
  528. else
  529. {
  530. Distance+=D.Inp.getbits32()>>(32-DBits);
  531. D.Inp.addbits(DBits);
  532. }
  533. }
  534. if (Distance>0x100)
  535. {
  536. Length++;
  537. if (Distance>0x2000)
  538. {
  539. Length++;
  540. if (Distance>0x40000)
  541. Length++;
  542. }
  543. }
  544. InsertOldDist(Distance);
  545. LastLength=Length;
  546. CopyString(Length,Distance);
  547. continue;
  548. }
  549. if (MainSlot==256)
  550. {
  551. UnpackFilter Filter;
  552. if (!ReadFilter(D.Inp,Filter) || !AddFilter(Filter))
  553. break;
  554. continue;
  555. }
  556. if (MainSlot==257)
  557. {
  558. if (LastLength!=0)
  559. CopyString(LastLength,OldDist[0]);
  560. continue;
  561. }
  562. if (MainSlot<262)
  563. {
  564. uint DistNum=MainSlot-258;
  565. uint Distance=OldDist[DistNum];
  566. for (uint I=DistNum;I>0;I--)
  567. OldDist[I]=OldDist[I-1];
  568. OldDist[0]=Distance;
  569. uint LengthSlot=DecodeNumber(D.Inp,&D.BlockTables.RD);
  570. uint Length=SlotToLength(D.Inp,LengthSlot);
  571. LastLength=Length;
  572. CopyString(Length,Distance);
  573. continue;
  574. }
  575. }
  576. return true;
  577. }