home *** CD-ROM | disk | FTP | other *** search
/ ftp.rarlab.com / 2014.05.ftp.rarlab.com.tar / ftp.rarlab.com / rar / sunrar.zip / unpold.c < prev    next >
Text File  |  1998-07-01  |  12KB  |  560 lines

  1. void OldUnpack(UBYTE *UnpAddr,int Solid);
  2. void ShortLZ(void);
  3. void LongLZ(void);
  4. void HuffDecode(void);
  5. void GetFlagsBuf(void);
  6. void OldUnpInitData(int Solid);
  7. void InitHuff(void);
  8. void CorrHuff(unsigned int *CharSet,unsigned int *NumToPlace);
  9. void OldCopyString(unsigned int Distance,unsigned int Length);
  10. unsigned int DecodeNum(int Num,unsigned int StartPos,unsigned int *DecTab,
  11.                        unsigned int *PosTab);
  12.  
  13.  
  14. unsigned int FlagBuf;
  15.  
  16. int LCount;
  17. int FlagsCnt;
  18.  
  19. unsigned int ChSet[256];
  20. unsigned int Place[256];
  21. unsigned int NToPl[256];
  22.  
  23. unsigned int ChSetA[256];
  24. unsigned int PlaceA[256];
  25.  
  26. unsigned int ChSetB[256];
  27. unsigned int PlaceB[256];
  28. unsigned int NToPlB[256];
  29.  
  30. unsigned int ChSetC[256];
  31. unsigned int PlaceC[256];
  32. unsigned int NToPlC[256];
  33.  
  34. unsigned int AvrPlc;
  35. unsigned int AvrPlcB;
  36. unsigned int AvrLn1;
  37. unsigned int AvrLn2;
  38. unsigned int AvrLn3;
  39.  
  40. int NumHuf;
  41. int StMode;
  42.  
  43. unsigned int Nhfb,Nlzb;
  44.  
  45. unsigned int MaxDist3;
  46. int Buf60;
  47.  
  48.  
  49. static unsigned int ShortLen1[]={1,3,4,4,5,6,7,8,8,4,4,5,6,6,4,0};
  50. static unsigned int ShortXor1[]={0,0xa0,0xd0,0xe0,0xf0,0xf8,0xfc,0xfe,
  51.                                  0xff,0xc0,0x80,0x90,0x98,0x9c,0xb0};
  52. static unsigned int ShortLen2[]={2,3,3,3,4,4,5,6,6,4,4,5,6,6,4,0};
  53. static unsigned int ShortXor2[]={0,0x40,0x60,0xa0,0xd0,0xe0,0xf0,0xf8,
  54.                                  0xfc,0xc0,0x80,0x90,0x98,0x9c,0xb0};
  55.  
  56. #define STARTL1  2
  57. static unsigned int DecL1[]={0x8000,0xa000,0xc000,0xd000,0xe000,0xea00,
  58.                              0xee00,0xf000,0xf200,0xf200,0xffff};
  59. static unsigned int PosL1[]={0,0,0,2,3,5,7,11,16,20,24,32,32};
  60.  
  61. #define STARTL2  3
  62. static unsigned int DecL2[]={0xa000,0xc000,0xd000,0xe000,0xea00,0xee00,
  63.                              0xf000,0xf200,0xf240,0xffff};
  64. static unsigned int PosL2[]={0,0,0,0,5,7,9,13,18,22,26,34,36};
  65.  
  66. #define STARTHF0  4
  67. static unsigned int DecHf0[]={0x8000,0xc000,0xe000,0xf200,0xf200,0xf200,
  68.                               0xf200,0xf200,0xffff};
  69. static unsigned int PosHf0[]={0,0,0,0,0,8,16,24,33,33,33,33,33};
  70.  
  71.  
  72. #define STARTHF1  5
  73. static unsigned int DecHf1[]={0x2000,0xc000,0xe000,0xf000,0xf200,0xf200,
  74.                               0xf7e0,0xffff};
  75. static unsigned int PosHf1[]={0,0,0,0,0,0,4,44,60,76,80,80,127};
  76.  
  77.  
  78. #define STARTHF2  5
  79. static unsigned int DecHf2[]={0x1000,0x2400,0x8000,0xc000,0xfa00,0xffff,
  80.                               0xffff,0xffff};
  81. static unsigned int PosHf2[]={0,0,0,0,0,0,2,7,53,117,233,0,0};
  82.  
  83.  
  84. #define STARTHF3  6
  85. static unsigned int DecHf3[]={0x800,0x2400,0xee00,0xfe80,0xffff,0xffff,
  86.                               0xffff};
  87. static unsigned int PosHf3[]={0,0,0,0,0,0,0,2,16,218,251,0,0};
  88.  
  89.  
  90. #define STARTHF4  8
  91. static unsigned int DecHf4[]={0xff00,0xffff,0xffff,0xffff,0xffff,0xffff};
  92. static unsigned int PosHf4[]={0,0,0,0,0,0,0,0,0,255,0,0,0};
  93.  
  94.  
  95. void OldUnpack(UBYTE *UnpAddr,int Solid)
  96. {
  97.   int SetStMode;
  98.   UnpBuf=UnpAddr;
  99.   if (Solid==0x1000)
  100.   {
  101.     Solid=0;
  102.     SetStMode=1;
  103.   }
  104.   else
  105.     SetStMode=0;
  106.   if (Suspend)
  107.     UnpPtr=WrPtr;
  108.   else
  109.   {
  110.     UnpInitData(Solid);
  111.     OldUnpInitData(Solid);
  112.     UnpReadBuf(1);
  113.     if (!Solid)
  114.     {
  115.       InitHuff();
  116.       UnpPtr=0;
  117.     }
  118.     else
  119.       UnpPtr=WrPtr;
  120.     DestUnpSize--;
  121.   }
  122.   if (SetStMode)
  123.     StMode=1;
  124.   else
  125.     if (DestUnpSize>=0)
  126.     {
  127.       GetFlagsBuf();
  128.       FlagsCnt=8;
  129.     }
  130.  
  131.   while (DestUnpSize>=0)
  132.   {
  133.     UnpPtr&=MAXWINMASK;
  134.  
  135.     if (InAddr>sizeof(InBuf)-30)
  136.       UnpReadBuf(0);
  137.     if (((WrPtr-UnpPtr) & MAXWINMASK)<270 && WrPtr!=UnpPtr)
  138.     {
  139.       UnpWriteBuf();
  140.       if (Suspend)
  141.         return;
  142.     }
  143.     if (StMode)
  144.     {
  145.       HuffDecode();
  146.       continue;
  147.     }
  148.  
  149.     if (--FlagsCnt < 0)
  150.     {
  151.       GetFlagsBuf();
  152.       FlagsCnt=7;
  153.     }
  154.  
  155.     if (FlagBuf & 0x80)
  156.     {
  157.       FlagBuf<<=1;
  158.       if (Nlzb > Nhfb)
  159.         LongLZ();
  160.       else
  161.         HuffDecode();
  162.     }
  163.     else
  164.     {
  165.       FlagBuf<<=1;
  166.       if (--FlagsCnt < 0)
  167.       {
  168.         GetFlagsBuf();
  169.         FlagsCnt=7;
  170.       }
  171.       if (FlagBuf & 0x80)
  172.       {
  173.         FlagBuf<<=1;
  174.         if (Nlzb > Nhfb)
  175.           HuffDecode();
  176.         else
  177.           LongLZ();
  178.       }
  179.       else
  180.       {
  181.         FlagBuf<<=1;
  182.         ShortLZ();
  183.       }
  184.     }
  185.   }
  186.   UnpWriteBuf();
  187. }
  188.  
  189.  
  190. void ShortLZ(void)
  191. {
  192.   unsigned int Length,SaveLength;
  193.   unsigned int LastDistance;
  194.   unsigned int Distance;
  195.   int DistancePlace;
  196.   NumHuf=0;
  197.   GetBits();
  198.   if (LCount==2)
  199.   {
  200.     AddBits(1);
  201.     if (BitField >= 0x8000)
  202.     {
  203.       OldCopyString((unsigned int)LastDist,LastLength);
  204.       return;
  205.     }
  206.     BitField <<= 1;
  207.     LCount=0;
  208.   }
  209.  
  210.   BitField>>=8;
  211.  
  212.   ShortLen1[1]=ShortLen2[3]=Buf60+3;
  213.  
  214.   if (AvrLn1<37)
  215.   {
  216.     for (Length=0;;Length++)
  217.       if (((BitField^ShortXor1[Length]) & (~(0xff>>ShortLen1[Length])))==0)
  218.         break;
  219.     AddBits(ShortLen1[Length]);
  220.   }
  221.   else
  222.   {
  223.     for (Length=0;;Length++)
  224.       if (((BitField^ShortXor2[Length]) & (~(0xff>>ShortLen2[Length])))==0)
  225.         break;
  226.     AddBits(ShortLen2[Length]);
  227.   }
  228.  
  229.   if (Length >= 9)
  230.   {
  231.     if (Length == 9)
  232.     {
  233.       LCount++;
  234.       OldCopyString((unsigned int)LastDist,LastLength);
  235.       return;
  236.     }
  237.     if (Length == 14)
  238.     {
  239.       LCount=0;
  240.       GetBits();
  241.       Length=DecodeNum(BitField,STARTL2,DecL2,PosL2)+5;
  242.       GetBits();
  243.       Distance=(BitField>>1) | 0x8000;
  244.       AddBits(15);
  245.       LastLength=Length;
  246.       LastDist=Distance;
  247.       OldCopyString(Distance,Length);
  248.       return;
  249.     }
  250.  
  251.     LCount=0;
  252.     SaveLength=Length;
  253.     Distance=OldDist[(OldDistPtr-(Length-9)) & 3];
  254.     GetBits();
  255.     Length=DecodeNum(BitField,STARTL1,DecL1,PosL1)+2;
  256.     if (Length==0x101 && SaveLength==10)
  257.     {
  258.       Buf60 ^= 1;
  259.       return;
  260.     }
  261.     if (Distance > 256)
  262.       Length++;
  263.     if (Distance >= MaxDist3)
  264.       Length++;
  265.  
  266.     OldDist[OldDistPtr++]=Distance;
  267.     OldDistPtr = OldDistPtr & 3;
  268.     LastLength=Length;
  269.     LastDist=Distance;
  270.     OldCopyString(Distance,Length);
  271.     return;
  272.   }
  273.  
  274.   LCount=0;
  275.   AvrLn1 += Length;
  276.   AvrLn1 -= AvrLn1 >> 4;
  277.  
  278.   GetBits();
  279.   DistancePlace=DecodeNum(BitField,STARTHF2,DecHf2,PosHf2);
  280.   Distance=ChSetA[DistancePlace];
  281.   if (--DistancePlace != -1)
  282.   {
  283.     PlaceA[Distance]--;
  284.     LastDistance=ChSetA[DistancePlace];
  285.     PlaceA[LastDistance]++;
  286.     ChSetA[DistancePlace+1]=LastDistance;
  287.     ChSetA[DistancePlace]=Distance;
  288.   }
  289.   Length+=2;
  290.   OldDist[OldDistPtr++] = ++Distance;
  291.   OldDistPtr = OldDistPtr & 3;
  292.   LastLength=Length;
  293.   LastDist=Distance;
  294.   OldCopyString(Distance,Length);
  295.   return;
  296. }
  297.  
  298.  
  299. void LongLZ(void)
  300. {
  301.   unsigned int Length;
  302.   unsigned int Distance;
  303.   unsigned int DistancePlace,NewDistancePlace;
  304.   unsigned int OldAvr2,OldAvr3;
  305.  
  306.   NumHuf=0;
  307.   Nlzb+=16;
  308.   if (Nlzb > 0xff)
  309.   {
  310.     Nlzb=0x90;
  311.     Nhfb >>= 1;
  312.   }
  313.   OldAvr2=AvrLn2;
  314.  
  315.   GetBits();
  316.   if (AvrLn2 >= 122)
  317.     Length=DecodeNum(BitField,STARTL2,DecL2,PosL2);
  318.   else
  319.     if (AvrLn2 >= 64)
  320.       Length=DecodeNum(BitField,STARTL1,DecL1,PosL1);
  321.     else
  322.       if (BitField < 0x100)
  323.       {
  324.         Length=BitField;
  325.         AddBits(16);
  326.       }
  327.       else
  328.       {
  329.         for (Length=0;((BitField<<Length)&0x8000)==0;Length++)
  330.           ;
  331.         AddBits(Length+1);
  332.       }
  333.  
  334.   AvrLn2 += Length;
  335.   AvrLn2 -= AvrLn2 >> 5;
  336.  
  337.   GetBits();
  338.   if (AvrPlcB > 0x28ff)
  339.     DistancePlace=DecodeNum(BitField,STARTHF2,DecHf2,PosHf2);
  340.   else
  341.     if (AvrPlcB > 0x6ff)
  342.       DistancePlace=DecodeNum(BitField,STARTHF1,DecHf1,PosHf1);
  343.     else
  344.       DistancePlace=DecodeNum(BitField,STARTHF0,DecHf0,PosHf0);
  345.  
  346.   AvrPlcB += DistancePlace;
  347.   AvrPlcB -= AvrPlcB >> 8;
  348.   while (1)
  349.   {
  350.     Distance = ChSetB[DistancePlace];
  351.     NewDistancePlace = NToPlB[Distance++ & 0xff]++;
  352.     if (!(Distance & 0xff))
  353.       CorrHuff(ChSetB,NToPlB);
  354.     else
  355.       break;
  356.   }
  357.  
  358.   ChSetB[DistancePlace]=ChSetB[NewDistancePlace];
  359.   ChSetB[NewDistancePlace]=Distance;
  360.  
  361.   GetBits();
  362.   Distance=((UWORD)((Distance & 0xff00) | (BitField >> 8))) >> 1;
  363.   AddBits(7);
  364.  
  365.   OldAvr3=AvrLn3;
  366.   if (Length!=1 && Length!=4)
  367.     if (Length==0 && Distance <= MaxDist3)
  368.     {
  369.       AvrLn3++;
  370.       AvrLn3 -= AvrLn3 >> 8;
  371.     }
  372.     else
  373.       if (AvrLn3 > 0)
  374.         AvrLn3--;
  375.   Length+=3;
  376.   if (Distance >= MaxDist3)
  377.     Length++;
  378.   if (Distance <= 256)
  379.     Length+=8;
  380.   if (OldAvr3 > 0xb0 || AvrPlc >= 0x2a00 && OldAvr2 < 0x40)
  381.     MaxDist3=0x7f00;
  382.   else
  383.     MaxDist3=0x2001;
  384.   OldDist[OldDistPtr++]=Distance;
  385.   OldDistPtr = OldDistPtr & 3;
  386.   LastLength=Length;
  387.   LastDist=Distance;
  388.   OldCopyString(Distance,Length);
  389. }
  390.  
  391.  
  392. void HuffDecode(void)
  393. {
  394.   unsigned int CurByte,NewBytePlace;
  395.   unsigned int Length;
  396.   unsigned int Distance;
  397.   int BytePlace;
  398.  
  399.   GetBits();
  400.  
  401.   if (AvrPlc > 0x75ff)
  402.     BytePlace=DecodeNum(BitField,STARTHF4,DecHf4,PosHf4);
  403.   else
  404.     if (AvrPlc > 0x5dff)
  405.       BytePlace=DecodeNum(BitField,STARTHF3,DecHf3,PosHf3);
  406.     else
  407.       if (AvrPlc > 0x35ff)
  408.         BytePlace=DecodeNum(BitField,STARTHF2,DecHf2,PosHf2);
  409.       else
  410.         if (AvrPlc > 0x0dff)
  411.           BytePlace=DecodeNum(BitField,STARTHF1,DecHf1,PosHf1);
  412.         else
  413.           BytePlace=DecodeNum(BitField,STARTHF0,DecHf0,PosHf0);
  414.   if (StMode)
  415.   {
  416.     if (BytePlace==0 && BitField > 0xfff)
  417.       BytePlace=0x100;
  418.     if (--BytePlace==-1)
  419.     {
  420.       GetBits();
  421.       AddBits(1);
  422.       if (BitField & 0x8000)
  423.       {
  424.         NumHuf=StMode=0;
  425.         return;
  426.       }
  427.       else
  428.       {
  429.         Length = (BitField & 0x4000) ? 4 : 3;
  430.         AddBits(1);
  431.         GetBits();
  432.         Distance=DecodeNum(BitField,STARTHF2,DecHf2,PosHf2);
  433.         GetBits();
  434.         Distance = (Distance << 5) | (BitField >> 11);
  435.         AddBits(5);
  436.         OldCopyString(Distance,Length);
  437.         return;
  438.       }
  439.     }
  440.   }
  441.   else
  442.     if (NumHuf++ >= 16 && FlagsCnt==0)
  443.       StMode=1;
  444.   AvrPlc += BytePlace;
  445.   AvrPlc -= AvrPlc >> 8;
  446.   Nhfb+=16;
  447.   if (Nhfb > 0xff)
  448.   {
  449.     Nhfb=0x90;
  450.     Nlzb >>= 1;
  451.   }
  452.  
  453.   UnpBuf[UnpPtr++]=(UBYTE)(ChSet[BytePlace]>>8);
  454.   DestUnpSize--;
  455.  
  456.   while (1)
  457.   {
  458.     CurByte=ChSet[BytePlace];
  459.     NewBytePlace=NToPl[CurByte++ & 0xff]++;
  460.     if ((CurByte & 0xff) > 0xa1)
  461.       CorrHuff(ChSet,NToPl);
  462.     else
  463.       break;
  464.   }
  465.  
  466.   ChSet[BytePlace]=ChSet[NewBytePlace];
  467.   ChSet[NewBytePlace]=CurByte;
  468. }
  469.  
  470.  
  471. void GetFlagsBuf(void)
  472. {
  473.   unsigned int Flags,FlagsPlace,NewFlagsPlace;
  474.  
  475.   GetBits();
  476.   FlagsPlace=DecodeNum(BitField,STARTHF2,DecHf2,PosHf2);
  477.  
  478.   while (1)
  479.   {
  480.     Flags=ChSetC[FlagsPlace];
  481.     FlagBuf=Flags>>8;
  482.     NewFlagsPlace=NToPlC[Flags++ & 0xff]++;
  483.     if ((Flags & 0xff) == 0)
  484.       CorrHuff(ChSetC,NToPlC);
  485.     else
  486.       break;
  487.   }
  488.  
  489.   ChSetC[FlagsPlace]=ChSetC[NewFlagsPlace];
  490.   ChSetC[NewFlagsPlace]=Flags;
  491. }
  492.  
  493.  
  494. void OldUnpInitData(int Solid)
  495. {
  496.   if (!Solid)
  497.   {
  498.     AvrPlcB=AvrLn1=AvrLn2=AvrLn3=NumHuf=Buf60=0;
  499.     AvrPlc=0x3500;
  500.     MaxDist3=0x2001;
  501.     Nhfb=Nlzb=0x80;
  502.   }
  503.   FlagsCnt=0;
  504.   FlagBuf=0;
  505.   StMode=0;
  506.   LCount=0;
  507. }
  508.  
  509.  
  510. void InitHuff(void)
  511. {
  512.   unsigned int I;
  513.   for (I=0;I<256;I++)
  514.   {
  515.     Place[I]=PlaceA[I]=PlaceB[I]=I;
  516.     PlaceC[I]=(~I+1) & 0xff;
  517.     ChSet[I]=ChSetB[I]=I<<8;
  518.     ChSetA[I]=I;
  519.     ChSetC[I]=((~I+1) & 0xff)<<8;
  520.   }
  521.   memset(NToPl,0,sizeof(NToPl));
  522.   memset(NToPlB,0,sizeof(NToPlB));
  523.   memset(NToPlC,0,sizeof(NToPlC));
  524.   CorrHuff(ChSetB,NToPlB);
  525. }
  526.  
  527.  
  528. void CorrHuff(unsigned int *CharSet,unsigned int *NumToPlace)
  529. {
  530.   int I,J;
  531.   for (I=7;I>=0;I--)
  532.     for (J=0;J<32;J++,CharSet++)
  533.       *CharSet=(*CharSet & ~0xff) | I;
  534.   memset(NumToPlace,0,sizeof(NToPl));
  535.   for (I=6;I>=0;I--)
  536.     NumToPlace[I]=(7-I)*32;
  537. }
  538.  
  539.  
  540. void OldCopyString(unsigned int Distance,unsigned int Length)
  541. {
  542.   DestUnpSize-=Length;
  543.   while (Length--)
  544.   {
  545.     UnpBuf[UnpPtr]=UnpBuf[(UnpPtr-Distance) & MAXWINMASK];
  546.     UnpPtr=(UnpPtr+1) & MAXWINMASK;
  547.   }
  548. }
  549.  
  550.  
  551. unsigned int DecodeNum(int Num,unsigned int StartPos,unsigned int *DecTab,
  552.                        unsigned int *PosTab)
  553. {
  554.   int I;
  555.   for (Num&=0xfff0,I=0;DecTab[I]<=Num;I++)
  556.     StartPos++;
  557.   AddBits(StartPos);
  558.   return(((Num-(I ? DecTab[I-1]:0))>>(16-StartPos))+PosTab[StartPos]);
  559. }
  560.