home *** CD-ROM | disk | FTP | other *** search
/ ftp.rarlab.com / 2014.05.ftp.rarlab.com.tar / ftp.rarlab.com / rar / sunrar.zip / unpack.c < prev    next >
C/C++ Source or Header  |  1998-07-01  |  12KB  |  592 lines

  1. #define NC 298  /* alphabet = {0, 1, 2, ..., NC - 1} */
  2. #define DC 48
  3. #define RC 28
  4. #define BC 19
  5. #define MC 257
  6.  
  7. struct Decode
  8. {
  9.   unsigned int MaxNum;
  10.   unsigned int DecodeLen[16];
  11.   unsigned int DecodePos[16];
  12.   unsigned int DecodeNum[2];
  13. };
  14.  
  15. struct AudioVariables AudV[4];
  16.  
  17. #define GetBits()                                                 \
  18.         BitField = ( ( ( (UDWORD)InBuf[InAddr]   << 16 ) |        \
  19.                        ( (UWORD) InBuf[InAddr+1] <<  8 ) |        \
  20.                        (         InBuf[InAddr+2]       ) )        \
  21.                        >> (8-InBit) ) & 0xffff;
  22.  
  23.  
  24. #define AddBits(Bits)                          \
  25.         InAddr += ( InBit + (Bits) ) >> 3;     \
  26.         InBit  =  ( InBit + (Bits) ) &  7;
  27.  
  28. int Suspend;
  29.  
  30. static unsigned char *UnpBuf;
  31. unsigned int UnpPtr,WrPtr;
  32. static unsigned int BitField;
  33. static unsigned int Number;
  34.  
  35. unsigned char InBuf[8192];
  36.  
  37. unsigned char UnpOldTable[MC*4];
  38.  
  39. unsigned int InAddr,InBit,ReadTop;
  40.  
  41. unsigned int LastDist,LastLength;
  42. static unsigned int Length,Distance;
  43.  
  44. unsigned int OldDist[4],OldDistPtr;
  45.  
  46.  
  47. struct LitDecode
  48. {
  49.   unsigned int MaxNum;
  50.   unsigned int DecodeLen[16];
  51.   unsigned int DecodePos[16];
  52.   unsigned int DecodeNum[NC];
  53. } LD;
  54.  
  55. struct DistDecode
  56. {
  57.   unsigned int MaxNum;
  58.   unsigned int DecodeLen[16];
  59.   unsigned int DecodePos[16];
  60.   unsigned int DecodeNum[DC];
  61. } DD;
  62.  
  63. struct RepDecode
  64. {
  65.   unsigned int MaxNum;
  66.   unsigned int DecodeLen[16];
  67.   unsigned int DecodePos[16];
  68.   unsigned int DecodeNum[RC];
  69. } RD;
  70.  
  71. struct MultDecode
  72. {
  73.   unsigned int MaxNum;
  74.   unsigned int DecodeLen[16];
  75.   unsigned int DecodePos[16];
  76.   unsigned int DecodeNum[MC];
  77. } MD[4];
  78.  
  79. struct BitDecode
  80. {
  81.   unsigned int MaxNum;
  82.   unsigned int DecodeLen[16];
  83.   unsigned int DecodePos[16];
  84.   unsigned int DecodeNum[BC];
  85. } BD;
  86.  
  87. static struct MultDecode *MDPtr[4]={&MD[0],&MD[1],&MD[2],&MD[3]};
  88.  
  89. int UnpAudioBlock,UnpChannels,CurChannel,ChannelDelta;
  90.  
  91.  
  92. extern void SetPackAudioVars(struct AudioVariables *AudV,int CurChannel,int ChnDelta,int UnpChannels);
  93. extern long DestUnpSize;
  94.  
  95. void Unpack(unsigned char *UnpAddr,int Solid);
  96. void UnpReadBuf(int FirstBuf);
  97. void UnpWriteBuf(void);
  98. void ReadTables(void);
  99. static void ReadLastTables(void);
  100. static void MakeDecodeTables(unsigned char *LenTab,struct Decode *Dec,int Size);
  101. static void DecodeNumber(struct Decode *Dec);
  102. void CopyString(void);
  103. void UnpInitData(int Solid);
  104. UBYTE DecodeAudio(int Delta);
  105.  
  106. void Unpack(unsigned char *UnpAddr,int Solid)
  107. {
  108.   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};
  109.   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};
  110.   static int DDecode[]={0,1,2,3,4,6,8,12,16,24,32,48,64,96,128,192,256,384,512,768,1024,1536,2048,3072,4096,6144,8192,12288,16384,24576,32768U,49152U,65536,98304,131072,196608,262144,327680,393216,458752,524288,589824,655360,720896,786432,851968,917504,983040};
  111.   static unsigned char DBits[]=  {0,0,0,0,1,1,2, 2, 3, 3, 4, 4, 5, 5,  6,  6,  7,  7,  8,  8,   9,   9,  10,  10,  11,  11,  12,   12,   13,   13,    14,    14,   15,   15,    16,    16,    16,    16,    16,    16,    16,    16,    16,    16,    16,    16,    16,    16};
  112.   static unsigned char SDDecode[]={0,4,8,16,32,64,128,192};
  113.   static unsigned char SDBits[]={2,2,3, 4, 5, 6,  6,  6};
  114.   unsigned int Bits;
  115.  
  116.   UnpBuf=UnpAddr;
  117. #ifndef SFX
  118.   if (Suspend)
  119.     UnpPtr=WrPtr;
  120.   else
  121. #endif
  122.  
  123.   {
  124.     UnpInitData(Solid);
  125.     UnpReadBuf(1);
  126.     if (!Solid)
  127.       ReadTables();
  128.     DestUnpSize--;
  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. #ifndef SFX
  141.       if (Suspend)
  142.         return;
  143. #endif
  144.     }
  145.     if (UnpAudioBlock)
  146.     {
  147.       DecodeNumber((struct Decode *)MDPtr[CurChannel]);
  148.       if (Number==256)
  149.       {
  150.         ReadTables();
  151.         continue;
  152.       }
  153.       UnpBuf[UnpPtr++]=DecodeAudio(Number);
  154.       if (++CurChannel==UnpChannels)
  155.         CurChannel=0;
  156.       DestUnpSize--;
  157.       continue;
  158.     }
  159.  
  160.     DecodeNumber((struct Decode *)&LD);
  161.     if (Number<256)
  162.     {
  163.       UnpBuf[UnpPtr++]=(UBYTE)Number;
  164.       DestUnpSize--;
  165.       continue;
  166.     }
  167.     if (Number>269)
  168.     {
  169.       Length=LDecode[Number-=270]+3;
  170.       if ((Bits=LBits[Number])>0)
  171.       {
  172.         GetBits();
  173.         Length+=BitField>>(16-Bits);
  174.         AddBits(Bits);
  175.       }
  176.  
  177.       DecodeNumber((struct Decode *)&DD);
  178.       Distance=DDecode[Number]+1;
  179.       if ((Bits=DBits[Number])>0)
  180.       {
  181.         GetBits();
  182.         Distance+=BitField>>(16-Bits);
  183.         AddBits(Bits);
  184.       }
  185.  
  186.       if (Distance>=0x40000L)
  187.         Length++;
  188.  
  189.       if (Distance>=0x2000)
  190.         Length++;
  191.  
  192.       CopyString();
  193.       continue;
  194.     }
  195.     if (Number==269)
  196.     {
  197.       ReadTables();
  198.       continue;
  199.     }
  200.     if (Number==256)
  201.     {
  202.       Length=LastLength;
  203.       Distance=LastDist;
  204.       CopyString();
  205.       continue;
  206.     }
  207.     if (Number<261)
  208.     {
  209.       Distance=OldDist[(OldDistPtr-(Number-256)) & 3];
  210.       DecodeNumber((struct Decode *)&RD);
  211.       Length=LDecode[Number]+2;
  212.       if ((Bits=LBits[Number])>0)
  213.       {
  214.         GetBits();
  215.         Length+=BitField>>(16-Bits);
  216.         AddBits(Bits);
  217.       }
  218.       if (Distance>=0x40000)
  219.         Length++;
  220.       if (Distance>=0x2000)
  221.         Length++;
  222.       if (Distance>=0x101)
  223.         Length++;
  224.       CopyString();
  225.       continue;
  226.     }
  227.     if (Number<270)
  228.     {
  229.       Distance=SDDecode[Number-=261]+1;
  230.       if ((Bits=SDBits[Number])>0)
  231.       {
  232.         GetBits();
  233.         Distance+=BitField>>(16-Bits);
  234.         AddBits(Bits);
  235.       }
  236.       Length=2;
  237.       CopyString();
  238.       continue;
  239.    }
  240.   }
  241.   ReadLastTables();
  242.   UnpWriteBuf();
  243. }
  244.  
  245.  
  246. void UnpReadBuf(int FirstBuf)
  247. {
  248.   int RetCode;
  249.   if (FirstBuf)
  250.   {
  251.     ReadTop=UnpRead(InBuf,sizeof(InBuf));
  252.     InAddr=0;
  253.   }
  254.   else
  255.   {
  256.     memcpy(InBuf,&InBuf[sizeof(InBuf)-32],32);
  257.     InAddr&=0x1f;
  258.     RetCode=UnpRead(&InBuf[32],sizeof(InBuf)-32);
  259.     if (RetCode>0)
  260.       ReadTop=RetCode+32;
  261.     else
  262.       ReadTop=InAddr;
  263.   }
  264. }
  265.  
  266.  
  267. void UnpWriteBuf(void)
  268. {
  269.   if (UnpPtr<WrPtr)
  270.   {
  271.     UnpWrite(&UnpBuf[WrPtr],-WrPtr & MAXWINMASK);
  272.     UnpWrite(UnpBuf,UnpPtr);
  273.   }
  274.   else
  275.     UnpWrite(&UnpBuf[WrPtr],UnpPtr-WrPtr);
  276.   WrPtr=UnpPtr;
  277. }
  278.  
  279.  
  280.  
  281. void ReadTables(void)
  282. {
  283.   UBYTE BitLength[BC];
  284.   unsigned char Table[MC*4];
  285.   int TableSize,N,I;
  286.   if (InAddr>sizeof(InBuf)-25)
  287.     UnpReadBuf(0);
  288.   GetBits();
  289.   UnpAudioBlock=(BitField & 0x8000);
  290.  
  291.   if (!(BitField & 0x4000))
  292.     memset(UnpOldTable,0,sizeof(UnpOldTable));
  293.   AddBits(2);
  294.  
  295.  
  296.   if (UnpAudioBlock)
  297.   {
  298.     UnpChannels=((BitField>>12) & 3)+1;
  299.     if (CurChannel>=UnpChannels)
  300.       CurChannel=0;
  301.     AddBits(2);
  302.     TableSize=MC*UnpChannels;
  303.   }
  304.   else
  305.     TableSize=NC+DC+RC;
  306.  
  307.  
  308.   for (I=0;I<BC;I++)
  309.   {
  310.     GetBits();
  311.     BitLength[I]=(UBYTE)(BitField >> 12);
  312.     AddBits(4);
  313.   }
  314.   MakeDecodeTables(BitLength,(struct Decode *)&BD,BC);
  315.   I=0;
  316.   while (I<TableSize)
  317.   {
  318.     if (InAddr>sizeof(InBuf)-5)
  319.       UnpReadBuf(0);
  320.     DecodeNumber((struct Decode *)&BD);
  321.     if (Number<16)
  322.       Table[I++]=(Number+UnpOldTable[I]) & 0xf;
  323.     else
  324.       if (Number==16)
  325.       {
  326.         GetBits();
  327.         N=(BitField >> 14)+3;
  328.         AddBits(2);
  329.         while (N-- > 0 && I<TableSize)
  330.         {
  331.           Table[I]=Table[I-1];
  332.           I++;
  333.         }
  334.       }
  335.       else
  336.       {
  337.         if (Number==17)
  338.         {
  339.           GetBits();
  340.           N=(BitField >> 13)+3;
  341.           AddBits(3);
  342.         }
  343.         else
  344.         {
  345.           GetBits();
  346.           N=(BitField >> 9)+11;
  347.           AddBits(7);
  348.         }
  349.         while (N-- > 0 && I<TableSize)
  350.           Table[I++]=0;
  351.       }
  352.   }
  353.   if (UnpAudioBlock)
  354.     for (I=0;I<UnpChannels;I++)
  355.       MakeDecodeTables(&Table[I*MC],(struct Decode *)MDPtr[I],MC);
  356.   else
  357.   {
  358.     MakeDecodeTables(&Table[0],(struct Decode *)&LD,NC);
  359.     MakeDecodeTables(&Table[NC],(struct Decode *)&DD,DC);
  360.     MakeDecodeTables(&Table[NC+DC],(struct Decode *)&RD,RC);
  361.   }
  362.   memcpy(UnpOldTable,Table,sizeof(UnpOldTable));
  363. }
  364.  
  365.  
  366. static void ReadLastTables(void)
  367. {
  368.   if (ReadTop>=InAddr+5)
  369.     if (UnpAudioBlock)
  370.     {
  371.       DecodeNumber((struct Decode *)MDPtr[CurChannel]);
  372.       if (Number==256)
  373.         ReadTables();
  374.     }
  375.     else
  376.     {
  377.       DecodeNumber((struct Decode *)&LD);
  378.       if (Number==269)
  379.         ReadTables();
  380.     }
  381. }
  382.  
  383.  
  384. static void MakeDecodeTables(unsigned char *LenTab,struct Decode *Dec,int Size)
  385. {
  386.   int LenCount[16],TmpPos[16],I;
  387.   long M,N;
  388.   memset(LenCount,0,sizeof(LenCount));
  389.   for (I=0;I<Size;I++)
  390.     LenCount[LenTab[I] & 0xF]++;
  391.  
  392.   LenCount[0]=0;
  393.   for (TmpPos[0]=Dec->DecodePos[0]=Dec->DecodeLen[0]=0,N=0,I=1;I<16;I++)
  394.   {
  395.     N=2*(N+LenCount[I]);
  396.     M=N<<(15-I);
  397.     if (M>0xFFFF)
  398.       M=0xFFFF;
  399.     Dec->DecodeLen[I]=(unsigned int)M;
  400.     TmpPos[I]=Dec->DecodePos[I]=Dec->DecodePos[I-1]+LenCount[I-1];
  401.   }
  402.  
  403.   for (I=0;I<Size;I++)
  404.     if (LenTab[I]!=0)
  405.       Dec->DecodeNum[TmpPos[LenTab[I] & 0xF]++]=I;
  406.   Dec->MaxNum=Size;
  407. }
  408.  
  409.  
  410. static void DecodeNumber(struct Decode *Dec)
  411. {
  412.   unsigned int I;
  413.   register unsigned int N;
  414.   GetBits();
  415.   N=BitField & 0xFFFE;
  416.   if (N<Dec->DecodeLen[8])
  417.     if (N<Dec->DecodeLen[4])
  418.       if (N<Dec->DecodeLen[2])
  419.         if (N<Dec->DecodeLen[1])
  420.           I=1;
  421.         else
  422.           I=2;
  423.       else
  424.         if (N<Dec->DecodeLen[3])
  425.           I=3;
  426.         else
  427.           I=4;
  428.     else
  429.       if (N<Dec->DecodeLen[6])
  430.         if (N<Dec->DecodeLen[5])
  431.           I=5;
  432.         else
  433.           I=6;
  434.       else
  435.         if (N<Dec->DecodeLen[7])
  436.           I=7;
  437.         else
  438.           I=8;
  439.   else
  440.     if (N<Dec->DecodeLen[12])
  441.       if (N<Dec->DecodeLen[10])
  442.         if (N<Dec->DecodeLen[9])
  443.           I=9;
  444.         else
  445.           I=10;
  446.       else
  447.         if (N<Dec->DecodeLen[11])
  448.           I=11;
  449.         else
  450.           I=12;
  451.     else
  452.       if (N<Dec->DecodeLen[14])
  453.         if (N<Dec->DecodeLen[13])
  454.           I=13;
  455.         else
  456.           I=14;
  457.       else
  458.         I=15;
  459.  
  460.   AddBits(I);
  461.   if ((N=Dec->DecodePos[I]+((N-Dec->DecodeLen[I-1])>>(16-I)))>=Dec->MaxNum)
  462.     N=0;
  463.   Number=Dec->DecodeNum[N];
  464. }
  465.  
  466.  
  467. void CopyString(void)
  468. {
  469.   LastDist=OldDist[OldDistPtr++ & 3]=Distance;
  470.   DestUnpSize-=(LastLength=Length);
  471.   while (Length--)
  472.   {
  473.     UnpBuf[UnpPtr]=UnpBuf[(UnpPtr-Distance) & MAXWINMASK];
  474.     UnpPtr=(UnpPtr+1) & MAXWINMASK;
  475.   }
  476. }
  477.  
  478.  
  479. void UnpInitData(int Solid)
  480. {
  481.   InAddr=InBit=0;
  482.   if (!Solid)
  483.   {
  484.     ChannelDelta=CurChannel=0;
  485.     memset(AudV,0,sizeof(AudV));
  486.     memset(OldDist,0,sizeof(OldDist));
  487.     OldDistPtr=0;
  488.     LastDist=LastLength=0;
  489.     memset(UnpBuf,0,MAXWINSIZE);
  490.     memset(UnpOldTable,0,sizeof(UnpOldTable));
  491.     UnpPtr=WrPtr=0;
  492.   }
  493. }
  494.  
  495.  
  496. UBYTE DecodeAudio(int Delta)
  497. {
  498.   struct AudioVariables *V;
  499.   unsigned int Ch;
  500.   unsigned int NumMinDif,MinDif;
  501.   int PCh,I;
  502.  
  503.   V=&AudV[CurChannel];
  504.   V->ByteCount++;
  505.   V->D4=V->D3;
  506.   V->D3=V->D2;
  507.   V->D2=V->LastDelta-V->D1;
  508.   V->D1=V->LastDelta;
  509.   PCh=8*V->LastChar+V->K1*V->D1+V->K2*V->D2+V->K3*V->D3+V->K4*V->D4+V->K5*ChannelDelta;
  510.   PCh=(PCh>>3) & 0xFF;
  511.  
  512.   Ch=PCh-Delta;
  513.  
  514.   I=((signed char)Delta)<<3;
  515.  
  516.   V->Dif[0]+=abs(I);
  517.   V->Dif[1]+=abs(I-V->D1);
  518.   V->Dif[2]+=abs(I+V->D1);
  519.   V->Dif[3]+=abs(I-V->D2);
  520.   V->Dif[4]+=abs(I+V->D2);
  521.   V->Dif[5]+=abs(I-V->D3);
  522.   V->Dif[6]+=abs(I+V->D3);
  523.   V->Dif[7]+=abs(I-V->D4);
  524.   V->Dif[8]+=abs(I+V->D4);
  525.   V->Dif[9]+=abs(I-ChannelDelta);
  526.   V->Dif[10]+=abs(I+ChannelDelta);
  527.  
  528.   ChannelDelta=V->LastDelta=(signed char)(Ch-V->LastChar);
  529.   V->LastChar=Ch;
  530.  
  531.   if ((V->ByteCount & 0x1F)==0)
  532.   {
  533.     MinDif=V->Dif[0];
  534.     NumMinDif=0;
  535.     V->Dif[0]=0;
  536.     for (I=1;I<sizeof(V->Dif)/sizeof(V->Dif[0]);I++)
  537.     {
  538.       if (V->Dif[I]<MinDif)
  539.       {
  540.         MinDif=V->Dif[I];
  541.         NumMinDif=I;
  542.       }
  543.       V->Dif[I]=0;
  544.     }
  545.     switch(NumMinDif)
  546.     {
  547.       case 1:
  548.         if (V->K1>=-16)
  549.           V->K1--;
  550.         break;
  551.       case 2:
  552.         if (V->K1<16)
  553.           V->K1++;
  554.         break;
  555.       case 3:
  556.         if (V->K2>=-16)
  557.           V->K2--;
  558.         break;
  559.       case 4:
  560.         if (V->K2<16)
  561.           V->K2++;
  562.         break;
  563.       case 5:
  564.         if (V->K3>=-16)
  565.           V->K3--;
  566.         break;
  567.       case 6:
  568.         if (V->K3<16)
  569.           V->K3++;
  570.         break;
  571.       case 7:
  572.         if (V->K4>=-16)
  573.           V->K4--;
  574.         break;
  575.       case 8:
  576.         if (V->K4<16)
  577.           V->K4++;
  578.         break;
  579.       case 9:
  580.         if (V->K5>=-16)
  581.           V->K5--;
  582.         break;
  583.       case 10:
  584.         if (V->K5<16)
  585.           V->K5++;
  586.         break;
  587.     }
  588.   }
  589.   return((UBYTE)Ch);
  590. }
  591.  
  592.