unpack.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. #include "rar.hpp"
  2. #include "coder.cpp"
  3. #include "suballoc.cpp"
  4. #include "model.cpp"
  5. #ifndef SFX_MODULE
  6. #include "unpack15.cpp"
  7. #include "unpack20.cpp"
  8. #endif
  9. Unpack::Unpack(ComprDataIO *DataIO)
  10. {
  11. UnpIO=DataIO;
  12. Window=NULL;
  13. ExternalWindow=false;
  14. Suspended=false;
  15. UnpAllBuf=false;
  16. UnpSomeRead=false;
  17. }
  18. Unpack::~Unpack()
  19. {
  20. if (Window!=NULL && !ExternalWindow)
  21. delete[] Window;
  22. InitFilters();
  23. }
  24. void Unpack::Init(byte *Window)
  25. {
  26. if (Window==NULL)
  27. {
  28. Unpack::Window=new byte[MAXWINSIZE];
  29. // Clean the window to generate the same output when unpacking corrupt
  30. // RAR files, which may access to unused areas of sliding dictionary.
  31. memset(Unpack::Window,0,MAXWINSIZE);
  32. #ifndef ALLOW_EXCEPTIONS
  33. if (Unpack::Window==NULL)
  34. ErrHandler.MemoryError();
  35. #endif
  36. }
  37. else
  38. {
  39. Unpack::Window=Window;
  40. ExternalWindow=true;
  41. }
  42. UnpInitData(false);
  43. #ifndef SFX_MODULE
  44. // RAR 1.5 decompression initialization
  45. OldUnpInitData(false);
  46. InitHuff();
  47. #endif
  48. }
  49. void Unpack::DoUnpack(int Method,bool Solid)
  50. {
  51. switch(Method)
  52. {
  53. #ifndef SFX_MODULE
  54. case 15: // rar 1.5 compression
  55. Unpack15(Solid);
  56. break;
  57. case 20: // rar 2.x compression
  58. case 26: // files larger than 2GB
  59. Unpack20(Solid);
  60. break;
  61. #endif
  62. case 29: // rar 3.x compression
  63. case 36: // alternative hash
  64. Unpack29(Solid);
  65. break;
  66. }
  67. }
  68. inline void Unpack::InsertOldDist(unsigned int Distance)
  69. {
  70. OldDist[3]=OldDist[2];
  71. OldDist[2]=OldDist[1];
  72. OldDist[1]=OldDist[0];
  73. OldDist[0]=Distance;
  74. }
  75. inline void Unpack::InsertLastMatch(unsigned int Length,unsigned int Distance)
  76. {
  77. LastDist=Distance;
  78. LastLength=Length;
  79. }
  80. _forceinline void Unpack::CopyString(uint Length,uint Distance)
  81. {
  82. uint SrcPtr=UnpPtr-Distance;
  83. if (SrcPtr<MAXWINSIZE-MAX_LZ_MATCH && UnpPtr<MAXWINSIZE-MAX_LZ_MATCH)
  84. {
  85. // If we are not close to end of window, we do not need to waste time
  86. // to "& MAXWINMASK" pointer protection.
  87. byte *Src=Window+SrcPtr;
  88. byte *Dest=Window+UnpPtr;
  89. UnpPtr+=Length;
  90. while (Length>=8)
  91. {
  92. // Unroll the loop for 8 byte and longer strings.
  93. Dest[0]=Src[0];
  94. Dest[1]=Src[1];
  95. Dest[2]=Src[2];
  96. Dest[3]=Src[3];
  97. Dest[4]=Src[4];
  98. Dest[5]=Src[5];
  99. Dest[6]=Src[6];
  100. Dest[7]=Src[7];
  101. Src+=8;
  102. Dest+=8;
  103. Length-=8;
  104. }
  105. // Unroll the loop for 0 - 7 bytes left. Note that we use nested "if"s.
  106. if (Length>0) { Dest[0]=Src[0];
  107. if (Length>1) { Dest[1]=Src[1];
  108. if (Length>2) { Dest[2]=Src[2];
  109. if (Length>3) { Dest[3]=Src[3];
  110. if (Length>4) { Dest[4]=Src[4];
  111. if (Length>5) { Dest[5]=Src[5];
  112. if (Length>6) { Dest[6]=Src[6]; } } } } } } } // Close all nested "if"s.
  113. }
  114. else
  115. while (Length--) // Slow copying with all possible precautions.
  116. {
  117. Window[UnpPtr]=Window[SrcPtr++ & MAXWINMASK];
  118. UnpPtr=(UnpPtr+1) & MAXWINMASK;
  119. }
  120. }
  121. _forceinline uint Unpack::DecodeNumber(DecodeTable *Dec)
  122. {
  123. // Left aligned 15 bit length raw bit field.
  124. uint BitField=getbits() & 0xfffe;
  125. if (BitField<Dec->DecodeLen[Dec->QuickBits])
  126. {
  127. uint Code=BitField>>(16-Dec->QuickBits);
  128. addbits(Dec->QuickLen[Code]);
  129. return Dec->QuickNum[Code];
  130. }
  131. // Detect the real bit length for current code.
  132. uint Bits=15;
  133. for (uint I=Dec->QuickBits+1;I<15;I++)
  134. if (BitField<Dec->DecodeLen[I])
  135. {
  136. Bits=I;
  137. break;
  138. }
  139. addbits(Bits);
  140. // Calculate the distance from the start code for current bit length.
  141. uint Dist=BitField-Dec->DecodeLen[Bits-1];
  142. // Start codes are left aligned, but we need the normal right aligned
  143. // number. So we shift the distance to the right.
  144. Dist>>=(16-Bits);
  145. // Now we can calculate the position in the code list. It is the sum
  146. // of first position for current bit length and right aligned distance
  147. // between our bit field and start code for current bit length.
  148. uint Pos=Dec->DecodePos[Bits]+Dist;
  149. // Out of bounds safety check required for damaged archives.
  150. if (Pos>=Dec->MaxNum)
  151. Pos=0;
  152. // Convert the position in the code list to position in alphabet
  153. // and return it.
  154. return(Dec->DecodeNum[Pos]);
  155. }
  156. // We use it instead of direct PPM.DecodeChar call to be sure that
  157. // we reset PPM structures in case of corrupt data. It is important,
  158. // because these structures can be invalid after PPM.DecodeChar returned -1.
  159. inline int Unpack::SafePPMDecodeChar()
  160. {
  161. int Ch=PPM.DecodeChar();
  162. if (Ch==-1) // Corrupt PPM data found.
  163. {
  164. PPM.CleanUp(); // Reset possibly corrupt PPM data structures.
  165. UnpBlockType=BLOCK_LZ; // Set faster and more fail proof LZ mode.
  166. }
  167. return(Ch);
  168. }
  169. void Unpack::Unpack29(bool Solid)
  170. {
  171. static unsigned char LDecode[]={0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,32,40,48,56,64,80,96,112,128,160,192,224};
  172. static unsigned char LBits[]= {0,0,0,0,0,0,0,0,1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5};
  173. static int DDecode[DC];
  174. static byte DBits[DC];
  175. static int DBitLengthCounts[]= {4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,14,0,12};
  176. static unsigned char SDDecode[]={0,4,8,16,32,64,128,192};
  177. static unsigned char SDBits[]= {2,2,3, 4, 5, 6, 6, 6};
  178. unsigned int Bits;
  179. if (DDecode[1]==0)
  180. {
  181. int Dist=0,BitLength=0,Slot=0;
  182. for (int I=0;I<sizeof(DBitLengthCounts)/sizeof(DBitLengthCounts[0]);I++,BitLength++)
  183. for (int J=0;J<DBitLengthCounts[I];J++,Slot++,Dist+=(1<<BitLength))
  184. {
  185. DDecode[Slot]=Dist;
  186. DBits[Slot]=BitLength;
  187. }
  188. }
  189. FileExtracted=true;
  190. if (!Suspended)
  191. {
  192. UnpInitData(Solid);
  193. if (!UnpReadBuf())
  194. return;
  195. if ((!Solid || !TablesRead) && !ReadTables())
  196. return;
  197. }
  198. while (true)
  199. {
  200. UnpPtr&=MAXWINMASK;
  201. if (InAddr>ReadBorder)
  202. {
  203. if (!UnpReadBuf())
  204. break;
  205. }
  206. if (((WrPtr-UnpPtr) & MAXWINMASK)<260 && WrPtr!=UnpPtr)
  207. {
  208. UnpWriteBuf();
  209. if (WrittenFileSize>DestUnpSize)
  210. return;
  211. if (Suspended)
  212. {
  213. FileExtracted=false;
  214. return;
  215. }
  216. }
  217. if (UnpBlockType==BLOCK_PPM)
  218. {
  219. // Here speed is critical, so we do not use SafePPMDecodeChar,
  220. // because sometimes even the inline function can introduce
  221. // some additional penalty.
  222. int Ch=PPM.DecodeChar();
  223. if (Ch==-1) // Corrupt PPM data found.
  224. {
  225. PPM.CleanUp(); // Reset possibly corrupt PPM data structures.
  226. UnpBlockType=BLOCK_LZ; // Set faster and more fail proof LZ mode.
  227. break;
  228. }
  229. if (Ch==PPMEscChar)
  230. {
  231. int NextCh=SafePPMDecodeChar();
  232. if (NextCh==0) // End of PPM encoding.
  233. {
  234. if (!ReadTables())
  235. break;
  236. continue;
  237. }
  238. if (NextCh==-1) // Corrupt PPM data found.
  239. break;
  240. if (NextCh==2) // End of file in PPM mode..
  241. break;
  242. if (NextCh==3) // Read VM code.
  243. {
  244. if (!ReadVMCodePPM())
  245. break;
  246. continue;
  247. }
  248. if (NextCh==4) // LZ inside of PPM.
  249. {
  250. unsigned int Distance=0,Length;
  251. bool Failed=false;
  252. for (int I=0;I<4 && !Failed;I++)
  253. {
  254. int Ch=SafePPMDecodeChar();
  255. if (Ch==-1)
  256. Failed=true;
  257. else
  258. if (I==3)
  259. Length=(byte)Ch;
  260. else
  261. Distance=(Distance<<8)+(byte)Ch;
  262. }
  263. if (Failed)
  264. break;
  265. CopyString(Length+32,Distance+2);
  266. continue;
  267. }
  268. if (NextCh==5) // One byte distance match (RLE) inside of PPM.
  269. {
  270. int Length=SafePPMDecodeChar();
  271. if (Length==-1)
  272. break;
  273. CopyString(Length+4,1);
  274. continue;
  275. }
  276. // If we are here, NextCh must be 1, what means that current byte
  277. // is equal to our 'escape' byte, so we just store it to Window.
  278. }
  279. Window[UnpPtr++]=Ch;
  280. continue;
  281. }
  282. int Number=DecodeNumber(&LD);
  283. if (Number<256)
  284. {
  285. Window[UnpPtr++]=(byte)Number;
  286. continue;
  287. }
  288. if (Number>=271)
  289. {
  290. int Length=LDecode[Number-=271]+3;
  291. if ((Bits=LBits[Number])>0)
  292. {
  293. Length+=getbits()>>(16-Bits);
  294. addbits(Bits);
  295. }
  296. int DistNumber=DecodeNumber(&DD);
  297. unsigned int Distance=DDecode[DistNumber]+1;
  298. if ((Bits=DBits[DistNumber])>0)
  299. {
  300. if (DistNumber>9)
  301. {
  302. if (Bits>4)
  303. {
  304. Distance+=((getbits()>>(20-Bits))<<4);
  305. addbits(Bits-4);
  306. }
  307. if (LowDistRepCount>0)
  308. {
  309. LowDistRepCount--;
  310. Distance+=PrevLowDist;
  311. }
  312. else
  313. {
  314. int LowDist=DecodeNumber(&LDD);
  315. if (LowDist==16)
  316. {
  317. LowDistRepCount=LOW_DIST_REP_COUNT-1;
  318. Distance+=PrevLowDist;
  319. }
  320. else
  321. {
  322. Distance+=LowDist;
  323. PrevLowDist=LowDist;
  324. }
  325. }
  326. }
  327. else
  328. {
  329. Distance+=getbits()>>(16-Bits);
  330. addbits(Bits);
  331. }
  332. }
  333. if (Distance>=0x2000)
  334. {
  335. Length++;
  336. if (Distance>=0x40000L)
  337. Length++;
  338. }
  339. InsertOldDist(Distance);
  340. InsertLastMatch(Length,Distance);
  341. CopyString(Length,Distance);
  342. continue;
  343. }
  344. if (Number==256)
  345. {
  346. if (!ReadEndOfBlock())
  347. break;
  348. continue;
  349. }
  350. if (Number==257)
  351. {
  352. if (!ReadVMCode())
  353. break;
  354. continue;
  355. }
  356. if (Number==258)
  357. {
  358. if (LastLength!=0)
  359. CopyString(LastLength,LastDist);
  360. continue;
  361. }
  362. if (Number<263)
  363. {
  364. int DistNum=Number-259;
  365. unsigned int Distance=OldDist[DistNum];
  366. for (int I=DistNum;I>0;I--)
  367. OldDist[I]=OldDist[I-1];
  368. OldDist[0]=Distance;
  369. int LengthNumber=DecodeNumber(&RD);
  370. int Length=LDecode[LengthNumber]+2;
  371. if ((Bits=LBits[LengthNumber])>0)
  372. {
  373. Length+=getbits()>>(16-Bits);
  374. addbits(Bits);
  375. }
  376. InsertLastMatch(Length,Distance);
  377. CopyString(Length,Distance);
  378. continue;
  379. }
  380. if (Number<272)
  381. {
  382. unsigned int Distance=SDDecode[Number-=263]+1;
  383. if ((Bits=SDBits[Number])>0)
  384. {
  385. Distance+=getbits()>>(16-Bits);
  386. addbits(Bits);
  387. }
  388. InsertOldDist(Distance);
  389. InsertLastMatch(2,Distance);
  390. CopyString(2,Distance);
  391. continue;
  392. }
  393. }
  394. UnpWriteBuf();
  395. }
  396. bool Unpack::ReadEndOfBlock()
  397. {
  398. unsigned int BitField=getbits();
  399. bool NewTable,NewFile=false;
  400. if (BitField & 0x8000)
  401. {
  402. NewTable=true;
  403. addbits(1);
  404. }
  405. else
  406. {
  407. NewFile=true;
  408. NewTable=(BitField & 0x4000)!=0;
  409. addbits(2);
  410. }
  411. TablesRead=!NewTable;
  412. return !(NewFile || NewTable && !ReadTables());
  413. }
  414. bool Unpack::ReadVMCode()
  415. {
  416. unsigned int FirstByte=getbits()>>8;
  417. addbits(8);
  418. int Length=(FirstByte & 7)+1;
  419. if (Length==7)
  420. {
  421. Length=(getbits()>>8)+7;
  422. addbits(8);
  423. }
  424. else
  425. if (Length==8)
  426. {
  427. Length=getbits();
  428. addbits(16);
  429. }
  430. Array<byte> VMCode(Length);
  431. for (int I=0;I<Length;I++)
  432. {
  433. // Try to read the new buffer if only one byte is left.
  434. // But if we read all bytes except the last, one byte is enough.
  435. if (InAddr>=ReadTop-1 && !UnpReadBuf() && I<Length-1)
  436. return(false);
  437. VMCode[I]=getbits()>>8;
  438. addbits(8);
  439. }
  440. return(AddVMCode(FirstByte,&VMCode[0],Length));
  441. }
  442. bool Unpack::ReadVMCodePPM()
  443. {
  444. unsigned int FirstByte=SafePPMDecodeChar();
  445. if ((int)FirstByte==-1)
  446. return(false);
  447. int Length=(FirstByte & 7)+1;
  448. if (Length==7)
  449. {
  450. int B1=SafePPMDecodeChar();
  451. if (B1==-1)
  452. return(false);
  453. Length=B1+7;
  454. }
  455. else
  456. if (Length==8)
  457. {
  458. int B1=SafePPMDecodeChar();
  459. if (B1==-1)
  460. return(false);
  461. int B2=SafePPMDecodeChar();
  462. if (B2==-1)
  463. return(false);
  464. Length=B1*256+B2;
  465. }
  466. Array<byte> VMCode(Length);
  467. for (int I=0;I<Length;I++)
  468. {
  469. int Ch=SafePPMDecodeChar();
  470. if (Ch==-1)
  471. return(false);
  472. VMCode[I]=Ch;
  473. }
  474. return(AddVMCode(FirstByte,&VMCode[0],Length));
  475. }
  476. bool Unpack::AddVMCode(unsigned int FirstByte,byte *Code,int CodeSize)
  477. {
  478. VMCodeInp.InitBitInput();
  479. memcpy(VMCodeInp.InBuf,Code,Min(BitInput::MAX_SIZE,CodeSize));
  480. VM.Init();
  481. uint FiltPos;
  482. if (FirstByte & 0x80)
  483. {
  484. FiltPos=RarVM::ReadData(VMCodeInp);
  485. if (FiltPos==0)
  486. InitFilters();
  487. else
  488. FiltPos--;
  489. }
  490. else
  491. FiltPos=LastFilter; // Use the same filter as last time.
  492. if (FiltPos>Filters.Size() || FiltPos>OldFilterLengths.Size())
  493. return(false);
  494. LastFilter=FiltPos;
  495. bool NewFilter=(FiltPos==Filters.Size());
  496. UnpackFilter *StackFilter=new UnpackFilter; // New filter for PrgStack.
  497. UnpackFilter *Filter;
  498. if (NewFilter) // New filter code, never used before since VM reset.
  499. {
  500. // Too many different filters, corrupt archive.
  501. if (FiltPos>1024)
  502. {
  503. delete StackFilter;
  504. return false;
  505. }
  506. Filters.Add(1);
  507. Filters[Filters.Size()-1]=Filter=new UnpackFilter;
  508. StackFilter->ParentFilter=(uint)(Filters.Size()-1);
  509. // Reserve one item, where we store the data block length of our new
  510. // filter entry. We'll set it to real block length below, after reading
  511. // it. But we need to initialize it now, because when processing corrupt
  512. // data, we can access this item even before we set it to real value.
  513. OldFilterLengths.Push(0);
  514. Filter->ExecCount=0;
  515. }
  516. else // Filter was used in the past.
  517. {
  518. Filter=Filters[FiltPos];
  519. StackFilter->ParentFilter=FiltPos;
  520. Filter->ExecCount++;
  521. }
  522. int EmptyCount=0;
  523. for (uint I=0;I<PrgStack.Size();I++)
  524. {
  525. PrgStack[I-EmptyCount]=PrgStack[I];
  526. if (PrgStack[I]==NULL)
  527. EmptyCount++;
  528. if (EmptyCount>0)
  529. PrgStack[I]=NULL;
  530. }
  531. if (EmptyCount==0)
  532. {
  533. PrgStack.Add(1);
  534. EmptyCount=1;
  535. }
  536. int StackPos=(int)(PrgStack.Size()-EmptyCount);
  537. PrgStack[StackPos]=StackFilter;
  538. StackFilter->ExecCount=Filter->ExecCount;
  539. uint BlockStart=RarVM::ReadData(VMCodeInp);
  540. if (FirstByte & 0x40)
  541. BlockStart+=258;
  542. StackFilter->BlockStart=(BlockStart+UnpPtr)&MAXWINMASK;
  543. if (FirstByte & 0x20)
  544. {
  545. StackFilter->BlockLength=RarVM::ReadData(VMCodeInp);
  546. // Store the last data block length for current filter.
  547. OldFilterLengths[FiltPos]=StackFilter->BlockLength;
  548. }
  549. else
  550. {
  551. // Set the data block size to same value as the previous block size
  552. // for same filter. It is possible on corrupt data to access here a new
  553. // and not filled yet item of OldFilterLengths array. This is why above
  554. // we set new OldFilterLengths items to zero.
  555. StackFilter->BlockLength=FiltPos<OldFilterLengths.Size() ? OldFilterLengths[FiltPos]:0;
  556. }
  557. StackFilter->NextWindow=WrPtr!=UnpPtr && ((WrPtr-UnpPtr)&MAXWINMASK)<=BlockStart;
  558. // DebugLog("\nNextWindow: UnpPtr=%08x WrPtr=%08x BlockStart=%08x",UnpPtr,WrPtr,BlockStart);
  559. memset(StackFilter->Prg.InitR,0,sizeof(StackFilter->Prg.InitR));
  560. StackFilter->Prg.InitR[3]=VM_GLOBALMEMADDR;
  561. StackFilter->Prg.InitR[4]=StackFilter->BlockLength;
  562. StackFilter->Prg.InitR[5]=StackFilter->ExecCount;
  563. if (FirstByte & 0x10) // set registers to optional parameters if any
  564. {
  565. unsigned int InitMask=VMCodeInp.fgetbits()>>9;
  566. VMCodeInp.faddbits(7);
  567. for (int I=0;I<7;I++)
  568. if (InitMask & (1<<I))
  569. StackFilter->Prg.InitR[I]=RarVM::ReadData(VMCodeInp);
  570. }
  571. if (NewFilter)
  572. {
  573. uint VMCodeSize=RarVM::ReadData(VMCodeInp);
  574. if (VMCodeSize>=0x10000 || VMCodeSize==0)
  575. return(false);
  576. Array<byte> VMCode(VMCodeSize);
  577. for (uint I=0;I<VMCodeSize;I++)
  578. {
  579. if (VMCodeInp.Overflow(3))
  580. return(false);
  581. VMCode[I]=VMCodeInp.fgetbits()>>8;
  582. VMCodeInp.faddbits(8);
  583. }
  584. VM.Prepare(&VMCode[0],VMCodeSize,&Filter->Prg);
  585. }
  586. StackFilter->Prg.AltCmd=&Filter->Prg.Cmd[0];
  587. StackFilter->Prg.CmdCount=Filter->Prg.CmdCount;
  588. size_t StaticDataSize=Filter->Prg.StaticData.Size();
  589. if (StaticDataSize>0 && StaticDataSize<VM_GLOBALMEMSIZE)
  590. {
  591. // read statically defined data contained in DB commands
  592. StackFilter->Prg.StaticData.Add(StaticDataSize);
  593. memcpy(&StackFilter->Prg.StaticData[0],&Filter->Prg.StaticData[0],StaticDataSize);
  594. }
  595. if (StackFilter->Prg.GlobalData.Size()<VM_FIXEDGLOBALSIZE)
  596. {
  597. StackFilter->Prg.GlobalData.Reset();
  598. StackFilter->Prg.GlobalData.Add(VM_FIXEDGLOBALSIZE);
  599. }
  600. byte *GlobalData=&StackFilter->Prg.GlobalData[0];
  601. for (int I=0;I<7;I++)
  602. VM.SetLowEndianValue((uint *)&GlobalData[I*4],StackFilter->Prg.InitR[I]);
  603. VM.SetLowEndianValue((uint *)&GlobalData[0x1c],StackFilter->BlockLength);
  604. VM.SetLowEndianValue((uint *)&GlobalData[0x20],0);
  605. VM.SetLowEndianValue((uint *)&GlobalData[0x2c],StackFilter->ExecCount);
  606. memset(&GlobalData[0x30],0,16);
  607. if (FirstByte & 8) // Put the data block passed as parameter if any.
  608. {
  609. if (VMCodeInp.Overflow(3))
  610. return(false);
  611. uint DataSize=RarVM::ReadData(VMCodeInp);
  612. if (DataSize>VM_GLOBALMEMSIZE-VM_FIXEDGLOBALSIZE)
  613. return(false);
  614. size_t CurSize=StackFilter->Prg.GlobalData.Size();
  615. if (CurSize<DataSize+VM_FIXEDGLOBALSIZE)
  616. StackFilter->Prg.GlobalData.Add(DataSize+VM_FIXEDGLOBALSIZE-CurSize);
  617. byte *GlobalData=&StackFilter->Prg.GlobalData[VM_FIXEDGLOBALSIZE];
  618. for (uint I=0;I<DataSize;I++)
  619. {
  620. if (VMCodeInp.Overflow(3))
  621. return(false);
  622. GlobalData[I]=VMCodeInp.fgetbits()>>8;
  623. VMCodeInp.faddbits(8);
  624. }
  625. }
  626. return(true);
  627. }
  628. bool Unpack::UnpReadBuf()
  629. {
  630. int DataSize=ReadTop-InAddr; // Data left to process.
  631. if (DataSize<0)
  632. return(false);
  633. if (InAddr>BitInput::MAX_SIZE/2)
  634. {
  635. // If we already processed more than half of buffer, let's move
  636. // remaining data into beginning to free more space for new data.
  637. if (DataSize>0)
  638. memmove(InBuf,InBuf+InAddr,DataSize);
  639. InAddr=0;
  640. ReadTop=DataSize;
  641. }
  642. else
  643. DataSize=ReadTop;
  644. int ReadCode=UnpIO->UnpRead(InBuf+DataSize,(BitInput::MAX_SIZE-DataSize)&~0xf);
  645. if (ReadCode>0)
  646. ReadTop+=ReadCode;
  647. ReadBorder=ReadTop-30;
  648. return(ReadCode!=-1);
  649. }
  650. void Unpack::UnpWriteBuf()
  651. {
  652. unsigned int WrittenBorder=WrPtr;
  653. unsigned int WriteSize=(UnpPtr-WrittenBorder)&MAXWINMASK;
  654. for (size_t I=0;I<PrgStack.Size();I++)
  655. {
  656. // Here we apply filters to data which we need to write.
  657. // We always copy data to virtual machine memory before processing.
  658. // We cannot process them just in place in Window buffer, because
  659. // these data can be used for future string matches, so we must
  660. // preserve them in original form.
  661. UnpackFilter *flt=PrgStack[I];
  662. if (flt==NULL)
  663. continue;
  664. if (flt->NextWindow)
  665. {
  666. flt->NextWindow=false;
  667. continue;
  668. }
  669. unsigned int BlockStart=flt->BlockStart;
  670. unsigned int BlockLength=flt->BlockLength;
  671. if (((BlockStart-WrittenBorder)&MAXWINMASK)<WriteSize)
  672. {
  673. if (WrittenBorder!=BlockStart)
  674. {
  675. UnpWriteArea(WrittenBorder,BlockStart);
  676. WrittenBorder=BlockStart;
  677. WriteSize=(UnpPtr-WrittenBorder)&MAXWINMASK;
  678. }
  679. if (BlockLength<=WriteSize)
  680. {
  681. unsigned int BlockEnd=(BlockStart+BlockLength)&MAXWINMASK;
  682. if (BlockStart<BlockEnd || BlockEnd==0)
  683. VM.SetMemory(0,Window+BlockStart,BlockLength);
  684. else
  685. {
  686. unsigned int FirstPartLength=MAXWINSIZE-BlockStart;
  687. VM.SetMemory(0,Window+BlockStart,FirstPartLength);
  688. VM.SetMemory(FirstPartLength,Window,BlockEnd);
  689. }
  690. VM_PreparedProgram *ParentPrg=&Filters[flt->ParentFilter]->Prg;
  691. VM_PreparedProgram *Prg=&flt->Prg;
  692. if (ParentPrg->GlobalData.Size()>VM_FIXEDGLOBALSIZE)
  693. {
  694. // Copy global data from previous script execution if any.
  695. Prg->GlobalData.Alloc(ParentPrg->GlobalData.Size());
  696. memcpy(&Prg->GlobalData[VM_FIXEDGLOBALSIZE],&ParentPrg->GlobalData[VM_FIXEDGLOBALSIZE],ParentPrg->GlobalData.Size()-VM_FIXEDGLOBALSIZE);
  697. }
  698. ExecuteCode(Prg);
  699. if (Prg->GlobalData.Size()>VM_FIXEDGLOBALSIZE)
  700. {
  701. // Save global data for next script execution.
  702. if (ParentPrg->GlobalData.Size()<Prg->GlobalData.Size())
  703. ParentPrg->GlobalData.Alloc(Prg->GlobalData.Size());
  704. memcpy(&ParentPrg->GlobalData[VM_FIXEDGLOBALSIZE],&Prg->GlobalData[VM_FIXEDGLOBALSIZE],Prg->GlobalData.Size()-VM_FIXEDGLOBALSIZE);
  705. }
  706. else
  707. ParentPrg->GlobalData.Reset();
  708. byte *FilteredData=Prg->FilteredData;
  709. unsigned int FilteredDataSize=Prg->FilteredDataSize;
  710. delete PrgStack[I];
  711. PrgStack[I]=NULL;
  712. while (I+1<PrgStack.Size())
  713. {
  714. UnpackFilter *NextFilter=PrgStack[I+1];
  715. if (NextFilter==NULL || NextFilter->BlockStart!=BlockStart ||
  716. NextFilter->BlockLength!=FilteredDataSize || NextFilter->NextWindow)
  717. break;
  718. // Apply several filters to same data block.
  719. VM.SetMemory(0,FilteredData,FilteredDataSize);
  720. VM_PreparedProgram *ParentPrg=&Filters[NextFilter->ParentFilter]->Prg;
  721. VM_PreparedProgram *NextPrg=&NextFilter->Prg;
  722. if (ParentPrg->GlobalData.Size()>VM_FIXEDGLOBALSIZE)
  723. {
  724. // Copy global data from previous script execution if any.
  725. NextPrg->GlobalData.Alloc(ParentPrg->GlobalData.Size());
  726. memcpy(&NextPrg->GlobalData[VM_FIXEDGLOBALSIZE],&ParentPrg->GlobalData[VM_FIXEDGLOBALSIZE],ParentPrg->GlobalData.Size()-VM_FIXEDGLOBALSIZE);
  727. }
  728. ExecuteCode(NextPrg);
  729. if (NextPrg->GlobalData.Size()>VM_FIXEDGLOBALSIZE)
  730. {
  731. // Save global data for next script execution.
  732. if (ParentPrg->GlobalData.Size()<NextPrg->GlobalData.Size())
  733. ParentPrg->GlobalData.Alloc(NextPrg->GlobalData.Size());
  734. memcpy(&ParentPrg->GlobalData[VM_FIXEDGLOBALSIZE],&NextPrg->GlobalData[VM_FIXEDGLOBALSIZE],NextPrg->GlobalData.Size()-VM_FIXEDGLOBALSIZE);
  735. }
  736. else
  737. ParentPrg->GlobalData.Reset();
  738. FilteredData=NextPrg->FilteredData;
  739. FilteredDataSize=NextPrg->FilteredDataSize;
  740. I++;
  741. delete PrgStack[I];
  742. PrgStack[I]=NULL;
  743. }
  744. UnpIO->UnpWrite(FilteredData,FilteredDataSize);
  745. UnpSomeRead=true;
  746. WrittenFileSize+=FilteredDataSize;
  747. WrittenBorder=BlockEnd;
  748. WriteSize=(UnpPtr-WrittenBorder)&MAXWINMASK;
  749. }
  750. else
  751. {
  752. for (size_t J=I;J<PrgStack.Size();J++)
  753. {
  754. UnpackFilter *flt=PrgStack[J];
  755. if (flt!=NULL && flt->NextWindow)
  756. flt->NextWindow=false;
  757. }
  758. WrPtr=WrittenBorder;
  759. return;
  760. }
  761. }
  762. }
  763. UnpWriteArea(WrittenBorder,UnpPtr);
  764. WrPtr=UnpPtr;
  765. }
  766. void Unpack::ExecuteCode(VM_PreparedProgram *Prg)
  767. {
  768. if (Prg->GlobalData.Size()>0)
  769. {
  770. Prg->InitR[6]=(uint)WrittenFileSize;
  771. VM.SetLowEndianValue((uint *)&Prg->GlobalData[0x24],(uint)WrittenFileSize);
  772. VM.SetLowEndianValue((uint *)&Prg->GlobalData[0x28],(uint)(WrittenFileSize>>32));
  773. VM.Execute(Prg);
  774. }
  775. }
  776. void Unpack::UnpWriteArea(unsigned int StartPtr,unsigned int EndPtr)
  777. {
  778. if (EndPtr!=StartPtr)
  779. UnpSomeRead=true;
  780. if (EndPtr<StartPtr)
  781. {
  782. UnpWriteData(&Window[StartPtr],-(int)StartPtr & MAXWINMASK);
  783. UnpWriteData(Window,EndPtr);
  784. UnpAllBuf=true;
  785. }
  786. else
  787. UnpWriteData(&Window[StartPtr],EndPtr-StartPtr);
  788. }
  789. void Unpack::UnpWriteData(byte *Data,size_t Size)
  790. {
  791. if (WrittenFileSize>=DestUnpSize)
  792. return;
  793. size_t WriteSize=Size;
  794. int64 LeftToWrite=DestUnpSize-WrittenFileSize;
  795. if ((int64)WriteSize>LeftToWrite)
  796. WriteSize=(size_t)LeftToWrite;
  797. UnpIO->UnpWrite(Data,WriteSize);
  798. WrittenFileSize+=Size;
  799. }
  800. bool Unpack::ReadTables()
  801. {
  802. byte BitLength[BC];
  803. byte Table[HUFF_TABLE_SIZE];
  804. if (InAddr>ReadTop-25)
  805. if (!UnpReadBuf())
  806. return(false);
  807. faddbits((8-InBit)&7);
  808. uint BitField=fgetbits();
  809. if (BitField & 0x8000)
  810. {
  811. UnpBlockType=BLOCK_PPM;
  812. return(PPM.DecodeInit(this,PPMEscChar));
  813. }
  814. UnpBlockType=BLOCK_LZ;
  815. PrevLowDist=0;
  816. LowDistRepCount=0;
  817. if (!(BitField & 0x4000))
  818. memset(UnpOldTable,0,sizeof(UnpOldTable));
  819. faddbits(2);
  820. for (int I=0;I<BC;I++)
  821. {
  822. int Length=(byte)(fgetbits() >> 12);
  823. faddbits(4);
  824. if (Length==15)
  825. {
  826. int ZeroCount=(byte)(fgetbits() >> 12);
  827. faddbits(4);
  828. if (ZeroCount==0)
  829. BitLength[I]=15;
  830. else
  831. {
  832. ZeroCount+=2;
  833. while (ZeroCount-- > 0 && I<sizeof(BitLength)/sizeof(BitLength[0]))
  834. BitLength[I++]=0;
  835. I--;
  836. }
  837. }
  838. else
  839. BitLength[I]=Length;
  840. }
  841. MakeDecodeTables(BitLength,&BD,BC);
  842. const int TableSize=HUFF_TABLE_SIZE;
  843. for (int I=0;I<TableSize;)
  844. {
  845. if (InAddr>ReadTop-5)
  846. if (!UnpReadBuf())
  847. return(false);
  848. int Number=DecodeNumber(&BD);
  849. if (Number<16)
  850. {
  851. Table[I]=(Number+UnpOldTable[I]) & 0xf;
  852. I++;
  853. }
  854. else
  855. if (Number<18)
  856. {
  857. int N;
  858. if (Number==16)
  859. {
  860. N=(fgetbits() >> 13)+3;
  861. faddbits(3);
  862. }
  863. else
  864. {
  865. N=(fgetbits() >> 9)+11;
  866. faddbits(7);
  867. }
  868. while (N-- > 0 && I<TableSize)
  869. {
  870. Table[I]=Table[I-1];
  871. I++;
  872. }
  873. }
  874. else
  875. {
  876. int N;
  877. if (Number==18)
  878. {
  879. N=(fgetbits() >> 13)+3;
  880. faddbits(3);
  881. }
  882. else
  883. {
  884. N=(fgetbits() >> 9)+11;
  885. faddbits(7);
  886. }
  887. while (N-- > 0 && I<TableSize)
  888. Table[I++]=0;
  889. }
  890. }
  891. TablesRead=true;
  892. if (InAddr>ReadTop)
  893. return(false);
  894. MakeDecodeTables(&Table[0],&LD,NC);
  895. MakeDecodeTables(&Table[NC],&DD,DC);
  896. MakeDecodeTables(&Table[NC+DC],&LDD,LDC);
  897. MakeDecodeTables(&Table[NC+DC+LDC],&RD,RC);
  898. memcpy(UnpOldTable,Table,sizeof(UnpOldTable));
  899. return(true);
  900. }
  901. void Unpack::UnpInitData(int Solid)
  902. {
  903. if (!Solid)
  904. {
  905. TablesRead=false;
  906. memset(OldDist,0,sizeof(OldDist));
  907. OldDistPtr=0;
  908. LastDist=LastLength=0;
  909. // memset(Window,0,MAXWINSIZE);
  910. memset(UnpOldTable,0,sizeof(UnpOldTable));
  911. memset(&LD,0,sizeof(LD));
  912. memset(&DD,0,sizeof(DD));
  913. memset(&LDD,0,sizeof(LDD));
  914. memset(&RD,0,sizeof(RD));
  915. memset(&BD,0,sizeof(BD));
  916. UnpPtr=WrPtr=0;
  917. PPMEscChar=2;
  918. UnpBlockType=BLOCK_LZ;
  919. InitFilters();
  920. }
  921. InitBitInput();
  922. WrittenFileSize=0;
  923. ReadTop=0;
  924. ReadBorder=0;
  925. #ifndef SFX_MODULE
  926. UnpInitData20(Solid);
  927. #endif
  928. }
  929. void Unpack::InitFilters()
  930. {
  931. OldFilterLengths.Reset();
  932. LastFilter=0;
  933. for (size_t I=0;I<Filters.Size();I++)
  934. delete Filters[I];
  935. Filters.Reset();
  936. for (size_t I=0;I<PrgStack.Size();I++)
  937. delete PrgStack[I];
  938. PrgStack.Reset();
  939. }
  940. // LengthTable contains the length in bits for every element of alphabet.
  941. // Dec is the structure to decode Huffman code/
  942. // Size is size of length table and DecodeNum field in Dec structure,
  943. void Unpack::MakeDecodeTables(byte *LengthTable,DecodeTable *Dec,uint Size)
  944. {
  945. // Size of alphabet and DecodePos array.
  946. Dec->MaxNum=Size;
  947. // Calculate how many entries for every bit length in LengthTable we have.
  948. uint LengthCount[16];
  949. memset(LengthCount,0,sizeof(LengthCount));
  950. for (size_t I=0;I<Size;I++)
  951. LengthCount[LengthTable[I] & 0xf]++;
  952. // We must not calculate the number of zero length codes.
  953. LengthCount[0]=0;
  954. // Set the entire DecodeNum to zero.
  955. memset(Dec->DecodeNum,0,Size*sizeof(*Dec->DecodeNum));
  956. // Initialize not really used entry for zero length code.
  957. Dec->DecodePos[0]=0;
  958. // Start code for bit length 1 is 0.
  959. Dec->DecodeLen[0]=0;
  960. // Right aligned upper limit code for current bit length.
  961. uint UpperLimit=0;
  962. for (size_t I=1;I<16;I++)
  963. {
  964. // Adjust the upper limit code.
  965. UpperLimit+=LengthCount[I];
  966. // Left aligned upper limit code.
  967. uint LeftAligned=UpperLimit<<(16-I);
  968. // Prepare the upper limit code for next bit length.
  969. UpperLimit*=2;
  970. // Store the left aligned upper limit code.
  971. Dec->DecodeLen[I]=(uint)LeftAligned;
  972. // Every item of this array contains the sum of all preceding items.
  973. // So it contains the start position in code list for every bit length.
  974. Dec->DecodePos[I]=Dec->DecodePos[I-1]+LengthCount[I-1];
  975. }
  976. // Prepare the copy of DecodePos. We'll modify this copy below,
  977. // so we cannot use the original DecodePos.
  978. uint CopyDecodePos[16];
  979. memcpy(CopyDecodePos,Dec->DecodePos,sizeof(CopyDecodePos));
  980. // For every bit length in the bit length table and so for every item
  981. // of alphabet.
  982. for (uint I=0;I<Size;I++)
  983. {
  984. // Get the current bit length.
  985. byte CurBitLength=LengthTable[I] & 0xf;
  986. if (CurBitLength!=0)
  987. {
  988. // Last position in code list for current bit length.
  989. uint LastPos=CopyDecodePos[CurBitLength];
  990. // Prepare the decode table, so this position in code list will be
  991. // decoded to current alphabet item number.
  992. Dec->DecodeNum[LastPos]=I;
  993. // We'll use next position number for this bit length next time.
  994. // So we pass through the entire range of positions available
  995. // for every bit length.
  996. CopyDecodePos[CurBitLength]++;
  997. }
  998. }
  999. // Define the number of bits to process in quick mode. We use more bits
  1000. // for larger alphabets. More bits means that more codes will be processed
  1001. // in quick mode, but also that more time will be spent to preparation
  1002. // of tables for quick decode.
  1003. switch (Size)
  1004. {
  1005. case NC:
  1006. case NC20:
  1007. Dec->QuickBits=MAX_QUICK_DECODE_BITS;
  1008. break;
  1009. default:
  1010. Dec->QuickBits=MAX_QUICK_DECODE_BITS-3;
  1011. break;
  1012. }
  1013. // Size of tables for quick mode.
  1014. uint QuickDataSize=1<<Dec->QuickBits;
  1015. // Bit length for current code, start from 1 bit codes. It is important
  1016. // to use 1 bit instead of 0 for minimum code length, so we are moving
  1017. // forward even when processing a corrupt archive.
  1018. uint CurBitLength=1;
  1019. // For every right aligned bit string which supports the quick decoding.
  1020. for (uint Code=0;Code<QuickDataSize;Code++)
  1021. {
  1022. // Left align the current code, so it will be in usual bit field format.
  1023. uint BitField=Code<<(16-Dec->QuickBits);
  1024. // Prepare the table for quick decoding of bit lengths.
  1025. // Find the upper limit for current bit field and adjust the bit length
  1026. // accordingly if necessary.
  1027. while (BitField>=Dec->DecodeLen[CurBitLength] && CurBitLength<ASIZE(Dec->DecodeLen))
  1028. CurBitLength++;
  1029. // Translation of right aligned bit string to bit length.
  1030. Dec->QuickLen[Code]=CurBitLength;
  1031. // Prepare the table for quick translation of position in code list
  1032. // to position in alphabet.
  1033. // Calculate the distance from the start code for current bit length.
  1034. uint Dist=BitField-Dec->DecodeLen[CurBitLength-1];
  1035. // Right align the distance.
  1036. Dist>>=(16-CurBitLength);
  1037. // Now we can calculate the position in the code list. It is the sum
  1038. // of first position for current bit length and right aligned distance
  1039. // between our bit field and start code for current bit length.
  1040. uint Pos=Dec->DecodePos[CurBitLength]+Dist;
  1041. if (Pos<Size) // Safety check for damaged archives.
  1042. {
  1043. // Define the code to alphabet number translation.
  1044. Dec->QuickNum[Code]=Dec->DecodeNum[Pos];
  1045. }
  1046. else
  1047. Dec->QuickNum[Code]=0;
  1048. }
  1049. }