2 static unsigned int DecL1[]={0x8000,0xa000,0xc000,0xd000,0xe000,0xea00,
3 0xee00,0xf000,0xf200,0xf200,0xffff};
4 static unsigned int PosL1[]={0,0,0,2,3,5,7,11,16,20,24,32,32};
7 static unsigned int DecL2[]={0xa000,0xc000,0xd000,0xe000,0xea00,0xee00,
8 0xf000,0xf200,0xf240,0xffff};
9 static unsigned int PosL2[]={0,0,0,0,5,7,9,13,18,22,26,34,36};
12 static unsigned int DecHf0[]={0x8000,0xc000,0xe000,0xf200,0xf200,0xf200,
13 0xf200,0xf200,0xffff};
14 static unsigned int PosHf0[]={0,0,0,0,0,8,16,24,33,33,33,33,33};
18 static unsigned int DecHf1[]={0x2000,0xc000,0xe000,0xf000,0xf200,0xf200,
20 static unsigned int PosHf1[]={0,0,0,0,0,0,4,44,60,76,80,80,127};
24 static unsigned int DecHf2[]={0x1000,0x2400,0x8000,0xc000,0xfa00,0xffff,
26 static unsigned int PosHf2[]={0,0,0,0,0,0,2,7,53,117,233,0,0};
30 static unsigned int DecHf3[]={0x800,0x2400,0xee00,0xfe80,0xffff,0xffff,
32 static unsigned int PosHf3[]={0,0,0,0,0,0,0,2,16,218,251,0,0};
36 static unsigned int DecHf4[]={0xff00,0xffff,0xffff,0xffff,0xffff,0xffff};
37 static unsigned int PosHf4[]={0,0,0,0,0,0,0,0,0,255,0,0,0};
40 void Unpack::Unpack15(bool Solid)
47 OldUnpInitData(Solid);
64 while (DestUnpSize>=0)
66 if (UnpIO->hQuit->WaitMSec(1))
71 if (InAddr>ReadTop-30 && !UnpReadBuf())
73 if (((WrPtr-UnpPtr) & MAXWINMASK)<270 && WrPtr!=UnpPtr)
126 void Unpack::OldUnpWriteBuf()
132 UnpIO->UnpWrite(&Window[WrPtr],-WrPtr & MAXWINMASK);
133 UnpIO->UnpWrite(Window,UnpPtr);
137 UnpIO->UnpWrite(&Window[WrPtr],UnpPtr-WrPtr);
142 void Unpack::ShortLZ()
144 static unsigned int ShortLen1[]={1,3,4,4,5,6,7,8,8,4,4,5,6,6,4,0};
145 static unsigned int ShortXor1[]={0,0xa0,0xd0,0xe0,0xf0,0xf8,0xfc,0xfe,
146 0xff,0xc0,0x80,0x90,0x98,0x9c,0xb0};
147 static unsigned int ShortLen2[]={2,3,3,3,4,4,5,6,6,4,4,5,6,6,4,0};
148 static unsigned int ShortXor2[]={0,0x40,0x60,0xa0,0xd0,0xe0,0xf0,0xf8,
149 0xfc,0xc0,0x80,0x90,0x98,0x9c,0xb0};
152 unsigned int Length,SaveLength;
153 unsigned int LastDistance;
154 unsigned int Distance;
158 unsigned int BitField=fgetbits();
162 if (BitField >= 0x8000)
164 OldCopyString((unsigned int)LastDist,LastLength);
173 ShortLen1[1]=ShortLen2[3]=Buf60+3;
177 for (Length=0;;Length++)
178 if (((BitField^ShortXor1[Length]) & (~(0xff>>ShortLen1[Length])))==0)
180 faddbits(ShortLen1[Length]);
184 for (Length=0;;Length++)
185 if (((BitField^ShortXor2[Length]) & (~(0xff>>ShortLen2[Length])))==0)
187 faddbits(ShortLen2[Length]);
195 OldCopyString((unsigned int)LastDist,LastLength);
201 Length=DecodeNum(fgetbits(),STARTL2,DecL2,PosL2)+5;
202 Distance=(fgetbits()>>1) | 0x8000;
206 OldCopyString(Distance,Length);
212 Distance=OldDist[(OldDistPtr-(Length-9)) & 3];
213 Length=DecodeNum(fgetbits(),STARTL1,DecL1,PosL1)+2;
214 if (Length==0x101 && SaveLength==10)
221 if (Distance >= MaxDist3)
224 OldDist[OldDistPtr++]=Distance;
225 OldDistPtr = OldDistPtr & 3;
228 OldCopyString(Distance,Length);
234 AvrLn1 -= AvrLn1 >> 4;
236 DistancePlace=DecodeNum(fgetbits(),STARTHF2,DecHf2,PosHf2) & 0xff;
237 Distance=ChSetA[DistancePlace];
238 if (--DistancePlace != -1)
241 LastDistance=ChSetA[DistancePlace];
242 PlaceA[LastDistance]++;
243 ChSetA[DistancePlace+1]=LastDistance;
244 ChSetA[DistancePlace]=Distance;
247 OldDist[OldDistPtr++] = ++Distance;
248 OldDistPtr = OldDistPtr & 3;
251 OldCopyString(Distance,Length);
255 void Unpack::LongLZ()
258 unsigned int Distance;
259 unsigned int DistancePlace,NewDistancePlace;
260 unsigned int OldAvr2,OldAvr3;
271 unsigned int BitField=fgetbits();
273 Length=DecodeNum(BitField,STARTL2,DecL2,PosL2);
276 Length=DecodeNum(BitField,STARTL1,DecL1,PosL1);
278 if (BitField < 0x100)
285 for (Length=0;((BitField<<Length)&0x8000)==0;Length++)
291 AvrLn2 -= AvrLn2 >> 5;
294 if (AvrPlcB > 0x28ff)
295 DistancePlace=DecodeNum(BitField,STARTHF2,DecHf2,PosHf2);
296 else if (AvrPlcB > 0x6ff)
297 DistancePlace=DecodeNum(BitField,STARTHF1,DecHf1,PosHf1);
299 DistancePlace=DecodeNum(BitField,STARTHF0,DecHf0,PosHf0);
301 AvrPlcB += DistancePlace;
302 AvrPlcB -= AvrPlcB >> 8;
305 Distance = ChSetB[DistancePlace & 0xff];
306 NewDistancePlace = NToPlB[Distance++ & 0xff]++;
307 if (!(Distance & 0xff))
308 CorrHuff(ChSetB,NToPlB);
313 ChSetB[DistancePlace]=ChSetB[NewDistancePlace];
314 ChSetB[NewDistancePlace]=Distance;
316 Distance=((Distance & 0xff00) | (fgetbits() >> 8)) >> 1;
320 if (Length!=1 && Length!=4)
322 if (Length==0 && Distance <= MaxDist3)
325 AvrLn3 -= AvrLn3 >> 8;
331 if (Distance >= MaxDist3)
335 if (OldAvr3 > 0xb0 || (AvrPlc >= 0x2a00 && OldAvr2 < 0x40))
339 OldDist[OldDistPtr++]=Distance;
340 OldDistPtr = OldDistPtr & 3;
343 OldCopyString(Distance,Length);
347 void Unpack::HuffDecode()
349 unsigned int CurByte,NewBytePlace;
351 unsigned int Distance;
354 unsigned int BitField=fgetbits();
357 BytePlace=DecodeNum(BitField,STARTHF4,DecHf4,PosHf4);
358 else if (AvrPlc > 0x5dff)
359 BytePlace=DecodeNum(BitField,STARTHF3,DecHf3,PosHf3);
360 else if (AvrPlc > 0x35ff)
361 BytePlace=DecodeNum(BitField,STARTHF2,DecHf2,PosHf2);
362 else if (AvrPlc > 0x0dff)
363 BytePlace=DecodeNum(BitField,STARTHF1,DecHf1,PosHf1);
365 BytePlace=DecodeNum(BitField,STARTHF0,DecHf0,PosHf0);
369 if (BytePlace==0 && BitField > 0xfff)
375 if (BitField & 0x8000)
382 Length = (BitField & 0x4000) ? 4 : 3;
384 Distance=DecodeNum(fgetbits(),STARTHF2,DecHf2,PosHf2);
385 Distance = (Distance << 5) | (fgetbits() >> 11);
387 OldCopyString(Distance,Length);
393 if (NumHuf++ >= 16 && FlagsCnt==0)
396 AvrPlc -= AvrPlc >> 8;
404 Window[UnpPtr++]=(byte)(ChSet[BytePlace]>>8);
409 CurByte=ChSet[BytePlace];
410 NewBytePlace=NToPl[CurByte++ & 0xff]++;
411 if ((CurByte & 0xff) > 0xa1)
412 CorrHuff(ChSet,NToPl);
417 ChSet[BytePlace]=ChSet[NewBytePlace];
418 ChSet[NewBytePlace]=CurByte;
422 void Unpack::GetFlagsBuf()
424 unsigned int Flags,NewFlagsPlace;
425 unsigned int FlagsPlace=DecodeNum(fgetbits(),STARTHF2,DecHf2,PosHf2);
429 Flags=ChSetC[FlagsPlace];
431 NewFlagsPlace=NToPlC[Flags++ & 0xff]++;
432 if ((Flags & 0xff) != 0)
434 CorrHuff(ChSetC,NToPlC);
437 ChSetC[FlagsPlace]=ChSetC[NewFlagsPlace];
438 ChSetC[NewFlagsPlace]=Flags;
442 void Unpack::OldUnpInitData(int Solid)
446 AvrPlcB=AvrLn1=AvrLn2=AvrLn3=NumHuf=Buf60=0;
459 void Unpack::InitHuff()
461 for (unsigned int I=0;I<256;I++)
463 Place[I]=PlaceA[I]=PlaceB[I]=I;
464 PlaceC[I]=(~I+1) & 0xff;
465 ChSet[I]=ChSetB[I]=I<<8;
467 ChSetC[I]=((~I+1) & 0xff)<<8;
469 memset(NToPl,0,sizeof(NToPl));
470 memset(NToPlB,0,sizeof(NToPlB));
471 memset(NToPlC,0,sizeof(NToPlC));
472 CorrHuff(ChSetB,NToPlB);
476 void Unpack::CorrHuff(unsigned int *CharSet,unsigned int *NumToPlace)
480 for (J=0;J<32;J++,CharSet++)
481 *CharSet=(*CharSet & ~0xff) | I;
482 memset(NumToPlace,0,sizeof(NToPl));
484 NumToPlace[I]=(7-I)*32;
488 void Unpack::OldCopyString(unsigned int Distance,unsigned int Length)
493 Window[UnpPtr]=Window[(UnpPtr-Distance) & MAXWINMASK];
494 UnpPtr=(UnpPtr+1) & MAXWINMASK;
499 unsigned int Unpack::DecodeNum(int Num,unsigned int StartPos,
500 unsigned int *DecTab,unsigned int *PosTab)
503 for (Num&=0xfff0,I=0;(int)DecTab[I]<=Num;I++)
506 return(((Num-(I ? DecTab[I-1]:0))>>(16-StartPos))+PosTab[StartPos]);