Unpack50.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. #if true
  2. using System;
  3. using System.Collections.Generic;
  4. using SharpCompress.Compressors.Rar.UnpackV1.Decode;
  5. using SharpCompress.Compressors.Rar.VM;
  6. using size_t=System.UInt32;
  7. using UnpackBlockHeader = SharpCompress.Compressors.Rar.UnpackV1;
  8. namespace SharpCompress.Compressors.Rar.UnpackV1
  9. {
  10. internal partial class Unpack
  11. {
  12. // Maximum allowed number of compressed bits processed in quick mode.
  13. private const int MAX_QUICK_DECODE_BITS = 10;
  14. // Maximum number of filters per entire data block. Must be at least
  15. // twice more than MAX_PACK_FILTERS to store filters from two data blocks.
  16. private const int MAX_UNPACK_FILTERS = 8192;
  17. // Maximum number of filters per entire data block for RAR3 unpack.
  18. // Must be at least twice more than v3_MAX_PACK_FILTERS to store filters
  19. // from two data blocks.
  20. private const int MAX3_UNPACK_FILTERS = 8192;
  21. // Limit maximum number of channels in RAR3 delta filter to some reasonable
  22. // value to prevent too slow processing of corrupt archives with invalid
  23. // channels number. Must be equal or larger than v3_MAX_FILTER_CHANNELS.
  24. // No need to provide it for RAR5, which uses only 5 bits to store channels.
  25. private const int MAX3_UNPACK_CHANNELS = 1024;
  26. // Maximum size of single filter block. We restrict it to limit memory
  27. // allocation. Must be equal or larger than MAX_ANALYZE_SIZE.
  28. private const int MAX_FILTER_BLOCK_SIZE = 0x400000;
  29. // Write data in 4 MB or smaller blocks. Must not exceed PACK_MAX_WRITE,
  30. // so we keep number of buffered filter in unpacker reasonable.
  31. private const int UNPACK_MAX_WRITE = 0x400000;
  32. // Decode compressed bit fields to alphabet numbers.
  33. // struct DecodeTable
  34. // {
  35. // // Real size of DecodeNum table.
  36. // public uint MaxNum;
  37. //
  38. // // Left aligned start and upper limit codes defining code space
  39. // // ranges for bit lengths. DecodeLen[BitLength-1] defines the start of
  40. // // range for bit length and DecodeLen[BitLength] defines next code
  41. // // after the end of range or in other words the upper limit code
  42. // // for specified bit length.
  43. // //uint DecodeLen[16];
  44. // public uint [] DecodeLen = new uint[16];
  45. //
  46. // // Every item of this array contains the sum of all preceding items.
  47. // // So it contains the start position in code list for every bit length.
  48. // public uint DecodePos[16];
  49. //
  50. // // Number of compressed bits processed in quick mode.
  51. // // Must not exceed MAX_QUICK_DECODE_BITS.
  52. // public uint QuickBits;
  53. //
  54. // // Translates compressed bits (up to QuickBits length)
  55. // // to bit length in quick mode.
  56. // public byte QuickLen[1<<MAX_QUICK_DECODE_BITS];
  57. //
  58. // // Translates compressed bits (up to QuickBits length)
  59. // // to position in alphabet in quick mode.
  60. // // 'ushort' saves some memory and even provides a little speed gain
  61. // // comparting to 'uint' here.
  62. // public ushort QuickNum[1<<MAX_QUICK_DECODE_BITS];
  63. //
  64. // // Translate the position in code list to position in alphabet.
  65. // // We do not allocate it dynamically to avoid performance overhead
  66. // // introduced by pointer, so we use the largest possible table size
  67. // // as array dimension. Real size of this array is defined in MaxNum.
  68. // // We use this array if compressed bit field is too lengthy
  69. // // for QuickLen based translation.
  70. // // 'ushort' saves some memory and even provides a little speed gain
  71. // // comparting to 'uint' here.
  72. // public ushort DecodeNum[LARGEST_TABLE_SIZE];
  73. // }
  74. // struct UnpackBlockHeader
  75. // {
  76. // public int BlockSize;
  77. // public int BlockBitSize;
  78. // public int BlockStart;
  79. // public int HeaderSize;
  80. // public bool LastBlockInFile;
  81. // public bool TablePresent;
  82. // }
  83. // struct UnpackBlockTables
  84. // {
  85. // public DecodeTable LD; // Decode literals.
  86. // public DecodeTable DD; // Decode distances.
  87. // public DecodeTable LDD; // Decode lower bits of distances.
  88. // public DecodeTable RD; // Decode repeating distances.
  89. // public DecodeTable BD; // Decode bit lengths in Huffman table.
  90. // }
  91. // private UnpackBlockHeader BlockHeader;
  92. private bool TablesRead5;
  93. private int WriteBorder;
  94. // TODO: see logic in unpack.cpp Unpack::Init()
  95. private const int MaxWinSize = PackDef.MAXWINSIZE;
  96. private const int MaxWinMask = PackDef.MAXWINMASK;
  97. // TODO: rename var
  98. private int UnpPtr { get { return unpPtr; } set { unpPtr = value; } }
  99. private int ReadBorder { get { return readBorder; } set { readBorder = value; } }
  100. private long DestUnpSize { get { return destUnpSize; } set { destUnpSize = value; } }
  101. private long WrittenFileSize { get { return writtenFileSize; } set { writtenFileSize = value; } }
  102. private byte[] Window{ get { return window; } }
  103. private uint LastLength { get { return (uint)lastLength; } set { lastLength = (int)value; } }
  104. private uint OldDistN(int i) { return (uint)oldDist[i]; }
  105. private void SetOldDistN(int i, uint value) { oldDist[i] = (int)value; }
  106. private int WrPtr { get { return wrPtr; } set { wrPtr = value; } }
  107. private Unpack BlockHeader { get { return this; } }
  108. private Unpack Header { get { return this; } }
  109. private int ReadTop { get { return readTop; } set { readTop = value; } }
  110. private List<UnpackFilter> Filters { get { return filters; } }
  111. // TODO: make sure these aren't already somewhere else
  112. public int BlockSize;
  113. public int BlockBitSize;
  114. public int BlockStart;
  115. public int HeaderSize;
  116. public bool LastBlockInFile;
  117. public bool TablePresent;
  118. public void Unpack5(bool Solid) {
  119. FileExtracted=true;
  120. if (!Suspended)
  121. {
  122. UnpInitData(Solid);
  123. if (!UnpReadBuf())
  124. return;
  125. // Check TablesRead5 to be sure that we read tables at least once
  126. // regardless of current block header TablePresent flag.
  127. // So we can safefly use these tables below.
  128. if (!ReadBlockHeader() ||
  129. !ReadTables() || !TablesRead5)
  130. return;
  131. }
  132. while (true)
  133. {
  134. UnpPtr &= MaxWinMask;
  135. if (Inp.InAddr>=ReadBorder)
  136. {
  137. bool FileDone=false;
  138. // We use 'while', because for empty block containing only Huffman table,
  139. // we'll be on the block border once again just after reading the table.
  140. while (Inp.InAddr>BlockHeader.BlockStart+BlockHeader.BlockSize-1 ||
  141. Inp.InAddr==BlockHeader.BlockStart+BlockHeader.BlockSize-1 &&
  142. Inp.InBit>=BlockHeader.BlockBitSize)
  143. {
  144. if (BlockHeader.LastBlockInFile)
  145. {
  146. FileDone=true;
  147. break;
  148. }
  149. if (!ReadBlockHeader() || !ReadTables())
  150. return;
  151. }
  152. if (FileDone || !UnpReadBuf())
  153. break;
  154. }
  155. if (((WriteBorder-UnpPtr) & MaxWinMask)<PackDef.MAX_LZ_MATCH+3 && WriteBorder!=UnpPtr)
  156. {
  157. UnpWriteBuf();
  158. if (WrittenFileSize>DestUnpSize)
  159. return;
  160. if (Suspended)
  161. {
  162. FileExtracted=false;
  163. return;
  164. }
  165. }
  166. //uint MainSlot=DecodeNumber(Inp,LD);
  167. uint MainSlot= this.DecodeNumber(LD);
  168. if (MainSlot<256)
  169. {
  170. // if (Fragmented)
  171. // FragWindow[UnpPtr++]=(byte)MainSlot;
  172. // else
  173. Window[UnpPtr++]=(byte)MainSlot;
  174. continue;
  175. }
  176. if (MainSlot>=262)
  177. {
  178. uint Length=SlotToLength(MainSlot-262);
  179. //uint DBits,Distance=1,DistSlot=DecodeNumber(Inp,&BlockTables.DD);
  180. int DBits;
  181. uint Distance=1,DistSlot=this.DecodeNumber(DD);
  182. if (DistSlot<4)
  183. {
  184. DBits=0;
  185. Distance+=DistSlot;
  186. }
  187. else
  188. {
  189. //DBits=DistSlot/2 - 1;
  190. DBits=(int)(DistSlot/2 - 1);
  191. Distance+=(2 | (DistSlot & 1)) << DBits;
  192. }
  193. if (DBits>0)
  194. {
  195. if (DBits>=4)
  196. {
  197. if (DBits>4)
  198. {
  199. Distance+=((Inp.getbits()>>(36-DBits))<<4);
  200. Inp.AddBits(DBits-4);
  201. }
  202. //uint LowDist=DecodeNumber(Inp,&BlockTables.LDD);
  203. uint LowDist=this.DecodeNumber(LDD);
  204. Distance+=LowDist;
  205. }
  206. else
  207. {
  208. Distance+=Inp.getbits()>>(32-DBits);
  209. Inp.AddBits(DBits);
  210. }
  211. }
  212. if (Distance>0x100)
  213. {
  214. Length++;
  215. if (Distance>0x2000)
  216. {
  217. Length++;
  218. if (Distance>0x40000)
  219. Length++;
  220. }
  221. }
  222. InsertOldDist(Distance);
  223. LastLength=Length;
  224. // if (Fragmented)
  225. // FragWindow.CopyString(Length,Distance,UnpPtr,MaxWinMask);
  226. // else
  227. CopyString(Length,Distance);
  228. continue;
  229. }
  230. if (MainSlot==256)
  231. {
  232. UnpackFilter Filter = new UnpackFilter();
  233. if (!ReadFilter(Filter) || !AddFilter(Filter))
  234. break;
  235. continue;
  236. }
  237. if (MainSlot==257)
  238. {
  239. if (LastLength!=0)
  240. // if (Fragmented)
  241. // FragWindow.CopyString(LastLength,OldDist[0],UnpPtr,MaxWinMask);
  242. // else
  243. //CopyString(LastLength,OldDist[0]);
  244. CopyString(LastLength,OldDistN(0));
  245. continue;
  246. }
  247. if (MainSlot<262)
  248. {
  249. //uint DistNum=MainSlot-258;
  250. int DistNum=(int)(MainSlot-258);
  251. //uint Distance=OldDist[DistNum];
  252. uint Distance=OldDistN(DistNum);
  253. //for (uint I=DistNum;I>0;I--)
  254. for (int I=DistNum;I>0;I--)
  255. //OldDistN[I]=OldDistN(I-1);
  256. SetOldDistN(I, OldDistN(I-1));
  257. //OldDistN[0]=Distance;
  258. SetOldDistN(0, Distance);
  259. uint LengthSlot=this.DecodeNumber(RD);
  260. uint Length=SlotToLength(LengthSlot);
  261. LastLength=Length;
  262. // if (Fragmented)
  263. // FragWindow.CopyString(Length,Distance,UnpPtr,MaxWinMask);
  264. // else
  265. CopyString(Length,Distance);
  266. continue;
  267. }
  268. }
  269. UnpWriteBuf();
  270. }
  271. private uint ReadFilterData()
  272. {
  273. uint ByteCount=(Inp.fgetbits()>>14)+1;
  274. Inp.AddBits(2);
  275. uint Data=0;
  276. //for (uint I=0;I<ByteCount;I++)
  277. for (int I=0;I<ByteCount;I++)
  278. {
  279. Data+=(Inp.fgetbits()>>8)<<(I*8);
  280. Inp.AddBits(8);
  281. }
  282. return Data;
  283. }
  284. private bool ReadFilter(UnpackFilter Filter)
  285. {
  286. if (!Inp.ExternalBuffer && Inp.InAddr>ReadTop-16)
  287. if (!UnpReadBuf())
  288. return false;
  289. Filter.uBlockStart=ReadFilterData();
  290. Filter.uBlockLength=ReadFilterData();
  291. if (Filter.BlockLength>MAX_FILTER_BLOCK_SIZE)
  292. Filter.BlockLength=0;
  293. //Filter.Type=Inp.fgetbits()>>13;
  294. Filter.Type=(byte)(Inp.fgetbits()>>13);
  295. Inp.faddbits(3);
  296. if (Filter.Type==(byte)FilterType.FILTER_DELTA)
  297. {
  298. //Filter.Channels=(Inp.fgetbits()>>11)+1;
  299. Filter.Channels=(byte)((Inp.fgetbits()>>11)+1);
  300. Inp.faddbits(5);
  301. }
  302. return true;
  303. }
  304. private bool AddFilter(UnpackFilter Filter)
  305. {
  306. if (Filters.Count>=MAX_UNPACK_FILTERS)
  307. {
  308. UnpWriteBuf(); // Write data, apply and flush filters.
  309. if (Filters.Count>=MAX_UNPACK_FILTERS)
  310. InitFilters(); // Still too many filters, prevent excessive memory use.
  311. }
  312. // If distance to filter start is that large that due to circular dictionary
  313. // mode now it points to old not written yet data, then we set 'NextWindow'
  314. // flag and process this filter only after processing that older data.
  315. Filter.NextWindow=WrPtr!=UnpPtr && ((WrPtr-UnpPtr)&MaxWinMask)<=Filter.BlockStart;
  316. Filter.uBlockStart=(uint)((Filter.BlockStart+UnpPtr)&MaxWinMask);
  317. Filters.Add(Filter);
  318. return true;
  319. }
  320. private bool UnpReadBuf()
  321. {
  322. int DataSize=ReadTop-Inp.InAddr; // Data left to process.
  323. if (DataSize<0)
  324. return false;
  325. BlockHeader.BlockSize-=Inp.InAddr-BlockHeader.BlockStart;
  326. if (Inp.InAddr>MAX_SIZE/2)
  327. {
  328. // If we already processed more than half of buffer, let's move
  329. // remaining data into beginning to free more space for new data
  330. // and ensure that calling function does not cross the buffer border
  331. // even if we did not read anything here. Also it ensures that read size
  332. // is not less than CRYPT_BLOCK_SIZE, so we can align it without risk
  333. // to make it zero.
  334. if (DataSize>0)
  335. //memmove(Inp.InBuf,Inp.InBuf+Inp.InAddr,DataSize);
  336. Array.Copy(InBuf, inAddr, InBuf, 0, DataSize);
  337. // TODO: perf
  338. //Buffer.BlockCopy(InBuf, inAddr, InBuf, 0, DataSize);
  339. Inp.InAddr=0;
  340. ReadTop=DataSize;
  341. }
  342. else
  343. DataSize=ReadTop;
  344. int ReadCode=0;
  345. if (MAX_SIZE!=DataSize)
  346. //ReadCode=UnpIO->UnpRead(Inp.InBuf+DataSize,BitInput.MAX_SIZE-DataSize);
  347. ReadCode = readStream.Read(InBuf, DataSize, MAX_SIZE-DataSize);
  348. if (ReadCode>0) // Can be also -1.
  349. ReadTop+=ReadCode;
  350. ReadBorder=ReadTop-30;
  351. BlockHeader.BlockStart=Inp.InAddr;
  352. if (BlockHeader.BlockSize!=-1) // '-1' means not defined yet.
  353. {
  354. // We may need to quit from main extraction loop and read new block header
  355. // and trees earlier than data in input buffer ends.
  356. ReadBorder=Math.Min(ReadBorder,BlockHeader.BlockStart+BlockHeader.BlockSize-1);
  357. }
  358. return ReadCode!=-1;
  359. }
  360. //?
  361. // void UnpWriteBuf()
  362. // {
  363. // size_t WrittenBorder=WrPtr;
  364. // size_t FullWriteSize=(UnpPtr-WrittenBorder)&MaxWinMask;
  365. // size_t WriteSizeLeft=FullWriteSize;
  366. // bool NotAllFiltersProcessed=false;
  367. // for (size_t I=0;I<Filters.Size();I++)
  368. // {
  369. // // Here we apply filters to data which we need to write.
  370. // // We always copy data to another memory block before processing.
  371. // // We cannot process them just in place in Window buffer, because
  372. // // these data can be used for future string matches, so we must
  373. // // preserve them in original form.
  374. //
  375. // UnpackFilter *flt=&Filters[I];
  376. // if (flt->Type==FilterType.FILTER_NONE)
  377. // continue;
  378. // if (flt->NextWindow)
  379. // {
  380. // // Here we skip filters which have block start in current data range
  381. // // due to address wrap around in circular dictionary, but actually
  382. // // belong to next dictionary block. If such filter start position
  383. // // is included to current write range, then we reset 'NextWindow' flag.
  384. // // In fact we can reset it even without such check, because current
  385. // // implementation seems to guarantee 'NextWindow' flag reset after
  386. // // buffer writing for all existing filters. But let's keep this check
  387. // // just in case. Compressor guarantees that distance between
  388. // // filter block start and filter storing position cannot exceed
  389. // // the dictionary size. So if we covered the filter block start with
  390. // // our write here, we can safely assume that filter is applicable
  391. // // to next block on no further wrap arounds is possible.
  392. // if (((flt->BlockStart-WrPtr)&MaxWinMask)<=FullWriteSize)
  393. // flt->NextWindow=false;
  394. // continue;
  395. // }
  396. // uint BlockStart=flt->BlockStart;
  397. // uint BlockLength=flt->BlockLength;
  398. // if (((BlockStart-WrittenBorder)&MaxWinMask)<WriteSizeLeft)
  399. // {
  400. // if (WrittenBorder!=BlockStart)
  401. // {
  402. // UnpWriteArea(WrittenBorder,BlockStart);
  403. // WrittenBorder=BlockStart;
  404. // WriteSizeLeft=(UnpPtr-WrittenBorder)&MaxWinMask;
  405. // }
  406. // if (BlockLength<=WriteSizeLeft)
  407. // {
  408. // if (BlockLength>0) // We set it to 0 also for invalid filters.
  409. // {
  410. // uint BlockEnd=(BlockStart+BlockLength)&MaxWinMask;
  411. //
  412. // FilterSrcMemory.Alloc(BlockLength);
  413. // byte *Mem=&FilterSrcMemory[0];
  414. // if (BlockStart<BlockEnd || BlockEnd==0)
  415. // {
  416. // if (Fragmented)
  417. // FragWindow.CopyData(Mem,BlockStart,BlockLength);
  418. // else
  419. // memcpy(Mem,Window+BlockStart,BlockLength);
  420. // }
  421. // else
  422. // {
  423. // size_t FirstPartLength=size_t(MaxWinSize-BlockStart);
  424. // if (Fragmented)
  425. // {
  426. // FragWindow.CopyData(Mem,BlockStart,FirstPartLength);
  427. // FragWindow.CopyData(Mem+FirstPartLength,0,BlockEnd);
  428. // }
  429. // else
  430. // {
  431. // memcpy(Mem,Window+BlockStart,FirstPartLength);
  432. // memcpy(Mem+FirstPartLength,Window,BlockEnd);
  433. // }
  434. // }
  435. //
  436. // byte *OutMem=ApplyFilter(Mem,BlockLength,flt);
  437. //
  438. // Filters[I].Type=FilterType.FILTER_NONE;
  439. //
  440. // if (OutMem!=NULL)
  441. // UnpIO->UnpWrite(OutMem,BlockLength);
  442. //
  443. // UnpSomeRead=true;
  444. // WrittenFileSize+=BlockLength;
  445. // WrittenBorder=BlockEnd;
  446. // WriteSizeLeft=(UnpPtr-WrittenBorder)&MaxWinMask;
  447. // }
  448. // }
  449. // else
  450. // {
  451. // // Current filter intersects the window write border, so we adjust
  452. // // the window border to process this filter next time, not now.
  453. // WrPtr=WrittenBorder;
  454. //
  455. // // Since Filter start position can only increase, we quit processing
  456. // // all following filters for this data block and reset 'NextWindow'
  457. // // flag for them.
  458. // for (size_t J=I;J<Filters.Size();J++)
  459. // {
  460. // UnpackFilter *flt=&Filters[J];
  461. // if (flt->Type!=FilterType.FILTER_NONE)
  462. // flt->NextWindow=false;
  463. // }
  464. //
  465. // // Do not write data left after current filter now.
  466. // NotAllFiltersProcessed=true;
  467. // break;
  468. // }
  469. // }
  470. // }
  471. //
  472. // // Remove processed filters from queue.
  473. // size_t EmptyCount=0;
  474. // for (size_t I=0;I<Filters.Size();I++)
  475. // {
  476. // if (EmptyCount>0)
  477. // Filters[I-EmptyCount]=Filters[I];
  478. // if (Filters[I].Type==FilterType.FILTER_NONE)
  479. // EmptyCount++;
  480. // }
  481. // if (EmptyCount>0)
  482. // Filters.Alloc(Filters.Size()-EmptyCount);
  483. //
  484. // if (!NotAllFiltersProcessed) // Only if all filters are processed.
  485. // {
  486. // // Write data left after last filter.
  487. // UnpWriteArea(WrittenBorder,UnpPtr);
  488. // WrPtr=UnpPtr;
  489. // }
  490. //
  491. // // We prefer to write data in blocks not exceeding UNPACK_MAX_WRITE
  492. // // instead of potentially huge MaxWinSize blocks. It also allows us
  493. // // to keep the size of Filters array reasonable.
  494. // WriteBorder=(UnpPtr+Min(MaxWinSize,UNPACK_MAX_WRITE))&MaxWinMask;
  495. //
  496. // // Choose the nearest among WriteBorder and WrPtr actual written border.
  497. // // If border is equal to UnpPtr, it means that we have MaxWinSize data ahead.
  498. // if (WriteBorder==UnpPtr ||
  499. // WrPtr!=UnpPtr && ((WrPtr-UnpPtr)&MaxWinMask)<((WriteBorder-UnpPtr)&MaxWinMask))
  500. // WriteBorder=WrPtr;
  501. // }
  502. // unused
  503. //x byte* ApplyFilter(byte *Data,uint DataSize,UnpackFilter *Flt)
  504. // byte[] ApplyFilter(byte []Data, uint DataSize, UnpackFilter Flt)
  505. // {
  506. // //x byte *SrcData=Data;
  507. // byte []SrcData=Data;
  508. // switch(Flt.Type)
  509. // {
  510. // case (byte)FilterType.FILTER_E8:
  511. // case (byte)FilterType.FILTER_E8E9:
  512. // {
  513. // uint FileOffset=(uint)WrittenFileSize;
  514. //
  515. // const uint FileSize=0x1000000;
  516. // byte CmpByte2=Flt.Type==(byte)FilterType.FILTER_E8E9 ? (byte)0xe9 : (byte)0xe8;
  517. // // DataSize is unsigned, so we use "CurPos+4" and not "DataSize-4"
  518. // // to avoid overflow for DataSize<4.
  519. // for (uint CurPos=0;CurPos+4<DataSize;)
  520. // {
  521. // byte CurByte=*(Data++);
  522. // CurPos++;
  523. // if (CurByte==0xe8 || CurByte==CmpByte2)
  524. // {
  525. // uint Offset=(CurPos+FileOffset)%FileSize;
  526. // uint Addr=RawGet4(Data);
  527. //
  528. // // We check 0x80000000 bit instead of '< 0' comparison
  529. // // not assuming int32 presence or uint size and endianness.
  530. // if ((Addr & 0x80000000)!=0) // Addr<0
  531. // {
  532. // if (((Addr+Offset) & 0x80000000)==0) // Addr+Offset>=0
  533. // RawPut4(Addr+FileSize,Data);
  534. // }
  535. // else
  536. // if (((Addr-FileSize) & 0x80000000)!=0) // Addr<FileSize
  537. // RawPut4(Addr-Offset,Data);
  538. //
  539. // Data+=4;
  540. // CurPos+=4;
  541. // }
  542. // }
  543. // }
  544. // return SrcData;
  545. // case (byte)FilterType.FILTER_ARM:
  546. // {
  547. // uint FileOffset=(uint)WrittenFileSize;
  548. // // DataSize is unsigned, so we use "CurPos+3" and not "DataSize-3"
  549. // // to avoid overflow for DataSize<3.
  550. // for (uint CurPos=0;CurPos+3<DataSize;CurPos+=4)
  551. // {
  552. // byte *D=Data+CurPos;
  553. // if (D[3]==0xeb) // BL command with '1110' (Always) condition.
  554. // {
  555. // uint Offset=D[0]+uint(D[1])*0x100+uint(D[2])*0x10000;
  556. // Offset-=(FileOffset+CurPos)/4;
  557. // D[0]=(byte)Offset;
  558. // D[1]=(byte)(Offset>>8);
  559. // D[2]=(byte)(Offset>>16);
  560. // }
  561. // }
  562. // }
  563. // return SrcData;
  564. // case (byte)FilterType.FILTER_DELTA:
  565. // {
  566. // // Unlike RAR3, we do not need to reject excessive channel
  567. // // values here, since RAR5 uses only 5 bits to store channel.
  568. // uint Channels=Flt->Channels,SrcPos=0;
  569. //
  570. // FilterDstMemory.Alloc(DataSize);
  571. // byte *DstData=&FilterDstMemory[0];
  572. //
  573. // // Bytes from same channels are grouped to continual data blocks,
  574. // // so we need to place them back to their interleaving positions.
  575. // for (uint CurChannel=0;CurChannel<Channels;CurChannel++)
  576. // {
  577. // byte PrevByte=0;
  578. // for (uint DestPos=CurChannel;DestPos<DataSize;DestPos+=Channels)
  579. // DstData[DestPos]=(PrevByte-=Data[SrcPos++]);
  580. // }
  581. // return DstData;
  582. // }
  583. //
  584. // }
  585. // return null;
  586. // }
  587. // unused
  588. // void UnpWriteArea(size_t StartPtr,size_t EndPtr)
  589. // {
  590. // if (EndPtr!=StartPtr)
  591. // UnpSomeRead=true;
  592. // if (EndPtr<StartPtr)
  593. // UnpAllBuf=true;
  594. //
  595. //// if (Fragmented)
  596. //// {
  597. //// size_t SizeToWrite=(EndPtr-StartPtr) & MaxWinMask;
  598. //// while (SizeToWrite>0)
  599. //// {
  600. //// size_t BlockSize=FragWindow.GetBlockSize(StartPtr,SizeToWrite);
  601. //// UnpWriteData(&FragWindow[StartPtr],BlockSize);
  602. //// SizeToWrite-=BlockSize;
  603. //// StartPtr=(StartPtr+BlockSize) & MaxWinMask;
  604. //// }
  605. //// }
  606. //// else
  607. // if (EndPtr<StartPtr)
  608. // {
  609. // UnpWriteData(Window+StartPtr,MaxWinSize-StartPtr);
  610. // UnpWriteData(Window,EndPtr);
  611. // }
  612. // else
  613. // UnpWriteData(Window+StartPtr,EndPtr-StartPtr);
  614. // }
  615. // unused
  616. // void UnpWriteData(byte *Data,size_t Size)
  617. // {
  618. // if (WrittenFileSize>=DestUnpSize)
  619. // return;
  620. // size_t WriteSize=Size;
  621. // long LeftToWrite=DestUnpSize-WrittenFileSize;
  622. // if ((long)WriteSize>LeftToWrite)
  623. // WriteSize=(size_t)LeftToWrite;
  624. // UnpIO->UnpWrite(Data,WriteSize);
  625. // WrittenFileSize+=Size;
  626. // }
  627. private void UnpInitData50(bool Solid)
  628. {
  629. if (!Solid)
  630. TablesRead5=false;
  631. }
  632. private bool ReadBlockHeader()
  633. {
  634. Header.HeaderSize=0;
  635. if (!Inp.ExternalBuffer && Inp.InAddr>ReadTop-7)
  636. if (!UnpReadBuf())
  637. return false;
  638. //Inp.faddbits((8-Inp.InBit)&7);
  639. Inp.faddbits((uint)((8-Inp.InBit)&7));
  640. byte BlockFlags=(byte)(Inp.fgetbits()>>8);
  641. Inp.faddbits(8);
  642. //uint ByteCount=((BlockFlags>>3)&3)+1; // Block size byte count.
  643. uint ByteCount=(uint)(((BlockFlags>>3)&3)+1); // Block size byte count.
  644. if (ByteCount==4)
  645. return false;
  646. //Header.HeaderSize=2+ByteCount;
  647. Header.HeaderSize=(int)(2+ByteCount);
  648. Header.BlockBitSize=(BlockFlags&7)+1;
  649. byte SavedCheckSum=(byte)(Inp.fgetbits()>>8);
  650. Inp.faddbits(8);
  651. int BlockSize=0;
  652. //for (uint I=0;I<ByteCount;I++)
  653. for (int I=0;I<ByteCount;I++)
  654. {
  655. //BlockSize+=(Inp.fgetbits()>>8)<<(I*8);
  656. BlockSize+=(int)(Inp.fgetbits()>>8)<<(I*8);
  657. Inp.AddBits(8);
  658. }
  659. Header.BlockSize=BlockSize;
  660. byte CheckSum=(byte)(0x5a^BlockFlags^BlockSize^(BlockSize>>8)^(BlockSize>>16));
  661. if (CheckSum!=SavedCheckSum)
  662. return false;
  663. Header.BlockStart=Inp.InAddr;
  664. ReadBorder=Math.Min(ReadBorder,Header.BlockStart+Header.BlockSize-1);
  665. Header.LastBlockInFile=(BlockFlags & 0x40)!=0;
  666. Header.TablePresent=(BlockFlags & 0x80)!=0;
  667. return true;
  668. }
  669. //?
  670. // bool ReadTables(BitInput Inp, ref UnpackBlockHeader Header, ref UnpackBlockTables Tables)
  671. // {
  672. // if (!Header.TablePresent)
  673. // return true;
  674. //
  675. // if (!Inp.ExternalBuffer && Inp.InAddr>ReadTop-25)
  676. // if (!UnpReadBuf())
  677. // return false;
  678. //
  679. // byte BitLength[BC];
  680. // for (uint I=0;I<BC;I++)
  681. // {
  682. // uint Length=(byte)(Inp.fgetbits() >> 12);
  683. // Inp.faddbits(4);
  684. // if (Length==15)
  685. // {
  686. // uint ZeroCount=(byte)(Inp.fgetbits() >> 12);
  687. // Inp.faddbits(4);
  688. // if (ZeroCount==0)
  689. // BitLength[I]=15;
  690. // else
  691. // {
  692. // ZeroCount+=2;
  693. // while (ZeroCount-- > 0 && I<ASIZE(BitLength))
  694. // BitLength[I++]=0;
  695. // I--;
  696. // }
  697. // }
  698. // else
  699. // BitLength[I]=Length;
  700. // }
  701. //
  702. // MakeDecodeTables(BitLength,&Tables.BD,BC);
  703. //
  704. // byte Table[HUFF_TABLE_SIZE];
  705. // const uint TableSize=HUFF_TABLE_SIZE;
  706. // for (uint I=0;I<TableSize;)
  707. // {
  708. // if (!Inp.ExternalBuffer && Inp.InAddr>ReadTop-5)
  709. // if (!UnpReadBuf())
  710. // return false;
  711. // uint Number=DecodeNumber(Inp,&Tables.BD);
  712. // if (Number<16)
  713. // {
  714. // Table[I]=Number;
  715. // I++;
  716. // }
  717. // else
  718. // if (Number<18)
  719. // {
  720. // uint N;
  721. // if (Number==16)
  722. // {
  723. // N=(Inp.fgetbits() >> 13)+3;
  724. // Inp.faddbits(3);
  725. // }
  726. // else
  727. // {
  728. // N=(Inp.fgetbits() >> 9)+11;
  729. // Inp.faddbits(7);
  730. // }
  731. // if (I==0)
  732. // {
  733. // // We cannot have "repeat previous" code at the first position.
  734. // // Multiple such codes would shift Inp position without changing I,
  735. // // which can lead to reading beyond of Inp boundary in mutithreading
  736. // // mode, where Inp.ExternalBuffer disables bounds check and we just
  737. // // reserve a lot of buffer space to not need such check normally.
  738. // return false;
  739. // }
  740. // else
  741. // while (N-- > 0 && I<TableSize)
  742. // {
  743. // Table[I]=Table[I-1];
  744. // I++;
  745. // }
  746. // }
  747. // else
  748. // {
  749. // uint N;
  750. // if (Number==18)
  751. // {
  752. // N=(Inp.fgetbits() >> 13)+3;
  753. // Inp.faddbits(3);
  754. // }
  755. // else
  756. // {
  757. // N=(Inp.fgetbits() >> 9)+11;
  758. // Inp.faddbits(7);
  759. // }
  760. // while (N-- > 0 && I<TableSize)
  761. // Table[I++]=0;
  762. // }
  763. // }
  764. // TablesRead5=true;
  765. // if (!Inp.ExternalBuffer && Inp.InAddr>ReadTop)
  766. // return false;
  767. // MakeDecodeTables(&Table[0],&Tables.LD,NC);
  768. // MakeDecodeTables(&Table[NC],&Tables.DD,DC);
  769. // MakeDecodeTables(&Table[NC+DC],&Tables.LDD,LDC);
  770. // MakeDecodeTables(&Table[NC+DC+LDC],&Tables.RD,RC);
  771. // return true;
  772. // }
  773. //?
  774. // void InitFilters()
  775. // {
  776. // Filters.SoftReset();
  777. // }
  778. }
  779. }
  780. #endif