4 #include "suballoc.cpp"
7 #include "unpack15.cpp"
8 #include "unpack20.cpp"
11 Unpack::Unpack(ComprDataIO *DataIO)
24 if (Window!=NULL && !ExternalWindow)
30 void Unpack::Init(byte *Window)
34 if (UnpIO->UnpackToMemorySize > -1)
35 Unpack::Window = new byte[MAXWINMEMSIZE];
37 Unpack::Window=new byte[MAXWINSIZE];
38 #ifndef ALLOW_EXCEPTIONS
39 if (Unpack::Window==NULL)
40 ErrHandler.MemoryError();
45 Unpack::Window=Window;
52 void Unpack::DoUnpack(int Method,bool Solid)
72 inline void Unpack::InsertOldDist(unsigned int Distance)
74 OldDist[3]=OldDist[2];
75 OldDist[2]=OldDist[1];
76 OldDist[1]=OldDist[0];
81 inline void Unpack::InsertLastMatch(unsigned int Length,unsigned int Distance)
88 void Unpack::CopyString(unsigned int Length,unsigned int Distance)
90 unsigned int DestPtr=UnpPtr-Distance;
91 if (DestPtr<MAXWINSIZE-260 && UnpPtr<MAXWINSIZE-260)
93 Window[UnpPtr++]=Window[DestPtr++];
95 Window[UnpPtr++]=Window[DestPtr++];
100 Window[UnpPtr]=Window[DestPtr++ & MAXWINMASK];
101 UnpPtr=(UnpPtr+1) & MAXWINMASK;
106 int Unpack::DecodeNumber(struct Decode *Dec)
109 unsigned int BitField=getbits() & 0xfffe;
110 if (BitField<Dec->DecodeLen[8])
111 if (BitField<Dec->DecodeLen[4])
112 if (BitField<Dec->DecodeLen[2])
113 if (BitField<Dec->DecodeLen[1])
118 if (BitField<Dec->DecodeLen[3])
123 if (BitField<Dec->DecodeLen[6])
124 if (BitField<Dec->DecodeLen[5])
129 if (BitField<Dec->DecodeLen[7])
134 if (BitField<Dec->DecodeLen[12])
135 if (BitField<Dec->DecodeLen[10])
136 if (BitField<Dec->DecodeLen[9])
141 if (BitField<Dec->DecodeLen[11])
146 if (BitField<Dec->DecodeLen[14])
147 if (BitField<Dec->DecodeLen[13])
155 unsigned int N=Dec->DecodePos[Bits]+((BitField-Dec->DecodeLen[Bits-1])>>(16-Bits));
158 return(Dec->DecodeNum[N]);
162 void Unpack::Unpack29(bool Solid)
164 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};
165 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};
166 static int DDecode[DC];
167 static byte DBits[DC];
168 static unsigned int DBitLengthCounts[]= {4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,14,0,12};
169 static unsigned char SDDecode[]={0,4,8,16,32,64,128,192};
170 static unsigned char SDBits[]= {2,2,3, 4, 5, 6, 6, 6};
175 int Dist=0,BitLength=0,Slot=0;
176 for (unsigned int I=0;I<sizeof(DBitLengthCounts)/sizeof(DBitLengthCounts[0]);I++,BitLength++)
177 for (unsigned int J=0;J<DBitLengthCounts[I];J++,Slot++,Dist+=(1<<BitLength))
180 DBits[Slot]=BitLength;
194 // if (!TablesRead && Solid)
195 // if (!ReadTables())
197 //if ((!Solid || !TablesRead) && !ReadTables())
214 if (InAddr>ReadBorder)
219 if (((WrPtr-UnpPtr) & MAXWINMASK)<260 && WrPtr!=UnpPtr)
222 if (WrittenFileSize>DestUnpSize)
230 if (UnpBlockType==BLOCK_PPM)
232 int Ch=PPM.DecodeChar();
240 int NextCh=PPM.DecodeChar();
247 if (NextCh==2 || NextCh==-1)
251 if (!ReadVMCodePPM())
257 unsigned int Distance=0,Length=0;
259 for (int I=0;I<4 && !Failed;I++)
261 int Ch=PPM.DecodeChar();
268 Distance=(Distance<<8)+(byte)Ch;
272 CopyString(Length+32,Distance+2);
277 int Length=PPM.DecodeChar();
280 CopyString(Length+4,1);
288 int Number=DecodeNumber((struct Decode *)&LD);
291 Window[UnpPtr++]=(byte)Number;
296 int Length=LDecode[Number-=271]+3;
297 if ((Bits=LBits[Number])>0)
299 Length+=getbits()>>(16-Bits);
303 int DistNumber=DecodeNumber((struct Decode *)&DD);
304 unsigned int Distance=DDecode[DistNumber]+1;
305 if ((Bits=DBits[DistNumber])>0)
311 Distance+=((getbits()>>(20-Bits))<<4);
314 if (LowDistRepCount>0)
317 Distance+=PrevLowDist;
321 int LowDist=DecodeNumber((struct Decode *)&LDD);
324 LowDistRepCount=LOW_DIST_REP_COUNT-1;
325 Distance+=PrevLowDist;
336 Distance+=getbits()>>(16-Bits);
341 if (Distance>=0x2000)
344 if (Distance>=0x40000L)
348 InsertOldDist(Distance);
349 InsertLastMatch(Length,Distance);
350 CopyString(Length,Distance);
355 if (!ReadEndOfBlock())
368 CopyString(LastLength,LastDist);
373 int DistNum=Number-259;
374 unsigned int Distance=OldDist[DistNum];
375 for (int I=DistNum;I>0;I--)
376 OldDist[I]=OldDist[I-1];
379 int LengthNumber=DecodeNumber((struct Decode *)&RD);
380 int Length=LDecode[LengthNumber]+2;
381 if ((Bits=LBits[LengthNumber])>0)
383 Length+=getbits()>>(16-Bits);
386 InsertLastMatch(Length,Distance);
387 CopyString(Length,Distance);
392 unsigned int Distance=SDDecode[Number-=263]+1;
393 if ((Bits=SDBits[Number])>0)
395 Distance+=getbits()>>(16-Bits);
398 InsertOldDist(Distance);
399 InsertLastMatch(2,Distance);
400 CopyString(2,Distance);
406 if (UnpIO->UnpackToMemorySize > -1)
408 UnpIO->hBufferEmpty->Set();
409 while (! UnpIO->hBufferFilled->WaitMSec(1))
410 if (UnpIO->hQuit->WaitMSec(1))
415 bool Unpack::ReadEndOfBlock()
417 unsigned int BitField=getbits();
418 bool NewTable,NewFile=false;
419 if (BitField & 0x8000)
427 NewTable=(BitField & 0x4000);
430 TablesRead=!NewTable;
431 return !(NewFile || (NewTable && !ReadTables()));
435 bool Unpack::ReadVMCode()
437 unsigned int FirstByte=getbits()>>8;
439 int Length=(FirstByte & 7)+1;
442 Length=(getbits()>>8)+7;
451 Array<byte> VMCode(Length);
452 for (int I=0;I<Length;I++)
454 if (InAddr>=ReadTop-1 && !UnpReadBuf() && I<Length-1)
456 VMCode[I]=getbits()>>8;
459 return(AddVMCode(FirstByte,&VMCode[0],Length));
463 bool Unpack::ReadVMCodePPM()
465 unsigned int FirstByte=PPM.DecodeChar();
466 if ((int)FirstByte==-1)
468 int Length=(FirstByte & 7)+1;
471 int B1=PPM.DecodeChar();
479 int B1=PPM.DecodeChar();
482 int B2=PPM.DecodeChar();
487 Array<byte> VMCode(Length);
488 for (int I=0;I<Length;I++)
490 int Ch=PPM.DecodeChar();
495 return(AddVMCode(FirstByte,&VMCode[0],Length));
499 bool Unpack::AddVMCode(unsigned int FirstByte,byte *Code,int CodeSize)
503 memcpy(Inp.InBuf,Code,Min(BitInput::MAX_SIZE,CodeSize));
507 if (FirstByte & 0x80)
509 FiltPos=RarVM::ReadData(Inp);
517 if (FiltPos>Filters.Size() || FiltPos>OldFilterLengths.Size())
520 bool NewFilter=(FiltPos==Filters.Size());
522 UnpackFilter *Filter;
526 Filters[Filters.Size()-1]=Filter=new UnpackFilter;
527 OldFilterLengths.Add(1);
532 Filter=Filters[FiltPos];
536 UnpackFilter *StackFilter=new UnpackFilter;
539 for (int I=0;I<PrgStack.Size();I++)
541 PrgStack[I-EmptyCount]=PrgStack[I];
542 if (PrgStack[I]==NULL)
552 int StackPos=PrgStack.Size()-EmptyCount;
553 PrgStack[StackPos]=StackFilter;
554 StackFilter->ExecCount=Filter->ExecCount;
556 uint BlockStart=RarVM::ReadData(Inp);
557 if (FirstByte & 0x40)
559 StackFilter->BlockStart=(BlockStart+UnpPtr)&MAXWINMASK;
560 if (FirstByte & 0x20)
561 StackFilter->BlockLength=RarVM::ReadData(Inp);
563 StackFilter->BlockLength=FiltPos<OldFilterLengths.Size() ? OldFilterLengths[FiltPos]:0;
564 StackFilter->NextWindow=WrPtr!=UnpPtr && ((WrPtr-UnpPtr)&MAXWINMASK)<=BlockStart;
566 // DebugLog("\nNextWindow: UnpPtr=%08x WrPtr=%08x BlockStart=%08x",UnpPtr,WrPtr,BlockStart);
568 OldFilterLengths[FiltPos]=StackFilter->BlockLength;
570 memset(StackFilter->Prg.InitR,0,sizeof(StackFilter->Prg.InitR));
571 StackFilter->Prg.InitR[3]=VM_GLOBALMEMADDR;
572 StackFilter->Prg.InitR[4]=StackFilter->BlockLength;
573 StackFilter->Prg.InitR[5]=StackFilter->ExecCount;
574 if (FirstByte & 0x10)
576 unsigned int InitMask=Inp.fgetbits()>>9;
578 for (int I=0;I<7;I++)
579 if (InitMask & (1<<I))
580 StackFilter->Prg.InitR[I]=RarVM::ReadData(Inp);
584 uint VMCodeSize=RarVM::ReadData(Inp);
585 if (VMCodeSize>=0x10000 || VMCodeSize==0)
587 Array<byte> VMCode(VMCodeSize);
588 for (uint I=0;I<VMCodeSize;I++)
590 VMCode[I]=Inp.fgetbits()>>8;
593 VM.Prepare(&VMCode[0],VMCodeSize,&Filter->Prg);
595 StackFilter->Prg.AltCmd=&Filter->Prg.Cmd[0];
596 StackFilter->Prg.CmdCount=Filter->Prg.CmdCount;
598 int StaticDataSize=Filter->Prg.StaticData.Size();
599 if (StaticDataSize>0 && StaticDataSize<VM_GLOBALMEMSIZE)
601 StackFilter->Prg.StaticData.Add(StaticDataSize);
602 memcpy(&StackFilter->Prg.StaticData[0],&Filter->Prg.StaticData[0],StaticDataSize);
605 if (StackFilter->Prg.GlobalData.Size()<VM_FIXEDGLOBALSIZE)
607 StackFilter->Prg.GlobalData.Reset();
608 StackFilter->Prg.GlobalData.Add(VM_FIXEDGLOBALSIZE);
610 byte *GlobalData=&StackFilter->Prg.GlobalData[0];
611 for (int I=0;I<7;I++)
612 VM.SetValue((uint *)&GlobalData[I*4],StackFilter->Prg.InitR[I]);
613 VM.SetValue((uint *)&GlobalData[0x1c],StackFilter->BlockLength);
614 VM.SetValue((uint *)&GlobalData[0x20],0);
615 VM.SetValue((uint *)&GlobalData[0x2c],StackFilter->ExecCount);
616 memset(&GlobalData[0x30],0,16);
620 uint DataSize=RarVM::ReadData(Inp);
621 if (DataSize>=0x10000)
623 unsigned int CurSize=StackFilter->Prg.GlobalData.Size();
624 if (CurSize<DataSize+VM_FIXEDGLOBALSIZE)
625 StackFilter->Prg.GlobalData.Add(DataSize+VM_FIXEDGLOBALSIZE-CurSize);
626 byte *GlobalData=&StackFilter->Prg.GlobalData[VM_FIXEDGLOBALSIZE];
627 for (uint I=0;I<DataSize;I++)
629 GlobalData[I]=Inp.fgetbits()>>8;
637 bool Unpack::UnpReadBuf()
639 int DataSize=ReadTop-InAddr;
642 if (InAddr>BitInput::MAX_SIZE/2)
645 memmove(InBuf,InBuf+InAddr,DataSize);
651 int ReadCode=UnpIO->UnpRead(InBuf+DataSize,(BitInput::MAX_SIZE-DataSize)&~0xf);
654 ReadBorder=ReadTop-30;
655 return(ReadCode!=-1);
659 void Unpack::UnpWriteBuf()
661 unsigned int WrittenBorder=WrPtr;
662 unsigned int WriteSize=(UnpPtr-WrittenBorder)&MAXWINMASK;
663 for (int I=0;I<PrgStack.Size();I++)
665 UnpackFilter *flt=PrgStack[I];
670 flt->NextWindow=false;
673 unsigned int BlockStart=flt->BlockStart;
674 unsigned int BlockLength=flt->BlockLength;
675 if (((BlockStart-WrittenBorder)&MAXWINMASK)<WriteSize)
677 if (WrittenBorder!=BlockStart)
679 UnpWriteArea(WrittenBorder,BlockStart);
680 WrittenBorder=BlockStart;
681 WriteSize=(UnpPtr-WrittenBorder)&MAXWINMASK;
683 if (BlockLength<=WriteSize)
685 unsigned int BlockEnd=(BlockStart+BlockLength)&MAXWINMASK;
686 if (BlockStart<BlockEnd || BlockEnd==0)
687 VM.SetMemory(0,Window+BlockStart,BlockLength);
690 unsigned int FirstPartLength=MAXWINSIZE-BlockStart;
691 VM.SetMemory(0,Window+BlockStart,FirstPartLength);
692 VM.SetMemory(FirstPartLength,Window,BlockEnd);
694 VM_PreparedProgram *Prg=&flt->Prg;
697 byte *FilteredData=Prg->FilteredData;
698 unsigned int FilteredDataSize=Prg->FilteredDataSize;
702 while (I+1<PrgStack.Size())
704 UnpackFilter *NextFilter=PrgStack[I+1];
705 if (NextFilter==NULL || NextFilter->BlockStart!=BlockStart ||
706 NextFilter->BlockLength!=FilteredDataSize || NextFilter->NextWindow)
708 VM.SetMemory(0,FilteredData,FilteredDataSize);
709 VM_PreparedProgram *NextPrg=&PrgStack[I+1]->Prg;
710 ExecuteCode(NextPrg);
711 FilteredData=NextPrg->FilteredData;
712 FilteredDataSize=NextPrg->FilteredDataSize;
717 UnpIO->UnpWrite(FilteredData,FilteredDataSize);
719 WrittenFileSize+=FilteredDataSize;
720 WrittenBorder=BlockEnd;
721 WriteSize=(UnpPtr-WrittenBorder)&MAXWINMASK;
725 for (int J=I;J<PrgStack.Size();J++)
727 UnpackFilter *flt=PrgStack[J];
728 if (flt!=NULL && flt->NextWindow)
729 flt->NextWindow=false;
737 UnpWriteArea(WrittenBorder,UnpPtr);
742 void Unpack::ExecuteCode(VM_PreparedProgram *Prg)
744 if (Prg->GlobalData.Size()>0)
746 Prg->InitR[6]=int64to32(WrittenFileSize);
747 VM.SetValue((uint *)&Prg->GlobalData[0x24],int64to32(WrittenFileSize));
748 VM.SetValue((uint *)&Prg->GlobalData[0x28],int64to32(WrittenFileSize>>32));
754 void Unpack::UnpWriteArea(unsigned int StartPtr,unsigned int EndPtr)
756 if (EndPtr!=StartPtr)
760 UnpWriteData(&Window[StartPtr],-StartPtr & MAXWINMASK);
761 UnpWriteData(Window,EndPtr);
765 UnpWriteData(&Window[StartPtr],EndPtr-StartPtr);
769 void Unpack::UnpWriteData(byte *Data,int Size)
771 if (WrittenFileSize>=DestUnpSize)
774 Int64 LeftToWrite=DestUnpSize-WrittenFileSize;
775 if (WriteSize>LeftToWrite)
776 WriteSize=int64to32(LeftToWrite);
777 UnpIO->UnpWrite(Data,WriteSize);
778 WrittenFileSize+=Size;
782 bool Unpack::ReadTables()
785 unsigned char Table[HUFF_TABLE_SIZE];
786 if (InAddr>ReadTop-25)
789 faddbits((8-InBit)&7);
790 unsigned int BitField=fgetbits();
791 if (BitField & 0x8000)
793 UnpBlockType=BLOCK_PPM;
794 return(PPM.DecodeInit(this,PPMEscChar));
796 UnpBlockType=BLOCK_LZ;
801 if (!(BitField & 0x4000))
802 memset(UnpOldTable,0,sizeof(UnpOldTable));
805 for (uint I=0;I<BC;I++)
807 int Length=(byte)(fgetbits() >> 12);
811 int ZeroCount=(byte)(fgetbits() >> 12);
818 while (ZeroCount-- > 0 && I<sizeof(BitLength)/sizeof(BitLength[0]))
826 MakeDecodeTables(BitLength,(struct Decode *)&BD,BC);
828 const int TableSize=HUFF_TABLE_SIZE;
829 for (int I=0;I<TableSize;)
831 if (InAddr>ReadTop-5)
834 int Number=DecodeNumber((struct Decode *)&BD);
837 Table[I]=(Number+UnpOldTable[I]) & 0xf;
846 N=(fgetbits() >> 13)+3;
851 N=(fgetbits() >> 9)+11;
854 while (N-- > 0 && I<TableSize)
865 N=(fgetbits() >> 13)+3;
870 N=(fgetbits() >> 9)+11;
873 while (N-- > 0 && I<TableSize)
880 MakeDecodeTables(&Table[0],(struct Decode *)&LD,NC);
881 MakeDecodeTables(&Table[NC],(struct Decode *)&DD,DC);
882 MakeDecodeTables(&Table[NC+DC],(struct Decode *)&LDD,LDC);
883 MakeDecodeTables(&Table[NC+DC+LDC],(struct Decode *)&RD,RC);
884 memcpy(UnpOldTable,Table,sizeof(UnpOldTable));
889 void Unpack::UnpInitData(int Solid)
894 memset(OldDist,0,sizeof(OldDist));
896 LastDist=LastLength=0;
897 if (UnpIO->UnpackToMemorySize > -1)
898 memset(Window,0,MAXWINMEMSIZE);
900 memset(Window,0,MAXWINSIZE);
901 memset(UnpOldTable,0,sizeof(UnpOldTable));
913 UnpInitData20(Solid);
918 void Unpack::InitFilters()
920 OldFilterLengths.Reset();
923 for (int I=0;I<Filters.Size();I++)
926 for (int I=0;I<PrgStack.Size();I++)
932 void Unpack::MakeDecodeTables(unsigned char *LenTab,struct Decode *Dec,int Size)
934 int LenCount[16],TmpPos[16],I;
936 memset(LenCount,0,sizeof(LenCount));
937 memset(Dec->DecodeNum,0,Size*sizeof(*Dec->DecodeNum));
939 LenCount[LenTab[I] & 0xF]++;
942 for (TmpPos[0]=Dec->DecodePos[0]=Dec->DecodeLen[0]=0,N=0,I=1;I<16;I++)
948 Dec->DecodeLen[I]=(unsigned int)M;
949 TmpPos[I]=Dec->DecodePos[I]=Dec->DecodePos[I-1]+LenCount[I-1];
954 Dec->DecodeNum[TmpPos[LenTab[I] & 0xF]++]=I;