⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 urarlib.c

📁 一个解RAR文件的开发库源码
💻 C
📖 第 1 页 / 共 5 页
字号:
	    }      }      WrPtr=UnpPtr;    }    if (UnpAudioBlock)    {      DecodeNumber((struct Decode *)MDPtr[CurChannel]);      if (Number==256)      {        ReadTables();        continue;      }      UnpBuf[UnpPtr++]=DecodeAudio(Number);      if (++CurChannel==UnpChannels)        CurChannel=0;      DestUnpSize--;      continue;    }    DecodeNumber((struct Decode *)&LD);    if (Number<256)    {      UnpBuf[UnpPtr++]=(UBYTE)Number;      DestUnpSize--;      continue;    }    if (Number>269)    {      Length=LDecode[Number-=270]+3;      if ((Bits=LBits[Number])>0)      {        GetBits();        Length+=BitField>>(16-Bits);        AddBits(Bits);      }      DecodeNumber((struct Decode *)&DD);      Distance=DDecode[Number]+1;      if ((Bits=DBits[Number])>0)      {        GetBits();        Distance+=BitField>>(16-Bits);        AddBits(Bits);      }      if (Distance>=0x40000L)        Length++;      if (Distance>=0x2000)        Length++;       LastDist=OldDist[OldDistPtr++ & 3]=Distance;       DestUnpSize-=(LastLength=Length);       while (Length--)       {         UnpBuf[UnpPtr]=UnpBuf[(UnpPtr-Distance) & MAXWINMASK];         UnpPtr=(UnpPtr+1) & MAXWINMASK;       }      continue;    }    if (Number==269)    {      ReadTables();      continue;    }    if (Number==256)    {      Length=LastLength;      Distance=LastDist;       LastDist=OldDist[OldDistPtr++ & 3]=Distance;       DestUnpSize-=(LastLength=Length);       while (Length--)       {         UnpBuf[UnpPtr]=UnpBuf[(UnpPtr-Distance) & MAXWINMASK];         UnpPtr=(UnpPtr+1) & MAXWINMASK;       }      continue;    }    if (Number<261)    {      Distance=OldDist[(OldDistPtr-(Number-256)) & 3];      DecodeNumber((struct Decode *)&RD);      Length=LDecode[Number]+2;      if ((Bits=LBits[Number])>0)      {        GetBits();        Length+=BitField>>(16-Bits);        AddBits(Bits);      }      if (Distance>=0x40000)        Length++;      if (Distance>=0x2000)        Length++;      if (Distance>=0x101)        Length++;       LastDist=OldDist[OldDistPtr++ & 3]=Distance;       DestUnpSize-=(LastLength=Length);       while (Length--)       {         UnpBuf[UnpPtr]=UnpBuf[(UnpPtr-Distance) & MAXWINMASK];         UnpPtr=(UnpPtr+1) & MAXWINMASK;       }      continue;    }    if (Number<270)    {      Distance=SDDecode[Number-=261]+1;      if ((Bits=SDBits[Number])>0)      {        GetBits();        Distance+=BitField>>(16-Bits);        AddBits(Bits);      }      Length=2;       LastDist=OldDist[OldDistPtr++ & 3]=Distance;       DestUnpSize-=(LastLength=Length);       while (Length--)       {         UnpBuf[UnpPtr]=UnpBuf[(UnpPtr-Distance) & MAXWINMASK];         UnpPtr=(UnpPtr+1) & MAXWINMASK;       }      continue;   }  }  ReadLastTables();  if (FileFound)                            /* flush buffer                 */  {    if (UnpPtr<WrPtr)    {	  if((*temp_output_buffer_offset + UnpPtr) > NewLhd.UnpSize)	  {	    debug_log("Fatal! Buffer overrun during decompression!");		DestUnpSize=-1;							  } else	  {        /* copy extracted data to output buffer                             */        memcpy(temp_output_buffer + *temp_output_buffer_offset, &UnpBuf[WrPtr],                (0-WrPtr) & MAXWINMASK);        /* update offset within buffer                                      */        *temp_output_buffer_offset+= (0-WrPtr) & MAXWINMASK;           /* copy extracted data to output buffer                             */        memcpy(temp_output_buffer + *temp_output_buffer_offset, UnpBuf, UnpPtr);        /* update offset within buffer                                      */        *temp_output_buffer_offset+=UnpPtr;	  }    } else    {	  if((*temp_output_buffer_offset + (UnpPtr-WrPtr)) > NewLhd.UnpSize)	  {	 	debug_log("Fatal! Buffer overrun during decompression!");		DestUnpSize=-1;							  } else	  {        /* copy extracted data to output buffer                             */        memcpy(temp_output_buffer + *temp_output_buffer_offset, &UnpBuf[WrPtr],               UnpPtr-WrPtr);           /* update offset within buffer                                      */        *temp_output_buffer_offset+=UnpPtr-WrPtr;	  }    }  }  WrPtr=UnpPtr;}unsigned int UnpRead(unsigned char *Addr,unsigned int Count){  int RetCode=0;  unsigned int I,ReadSize,TotalRead=0;  unsigned char *ReadAddr;  ReadAddr=Addr;  while (Count > 0)  {    ReadSize=(unsigned int)((Count>(unsigned long)UnpPackedSize) ?                                                   UnpPackedSize : Count);#ifdef _USE_MEMORY_TO_MEMORY_DECOMPRESSION
    if(MemRARFile->data == NULL)
      return(0);
    RetCode=tread(MemRARFile, ReadAddr, ReadSize);
#else
    if (ArcPtr==NULL)      return(0);
    RetCode=tread(ArcPtr,ReadAddr,ReadSize);
#endif    CurUnpRead+=RetCode;    ReadAddr+=RetCode;    TotalRead+=RetCode;    Count-=RetCode;    UnpPackedSize-=RetCode;      break;  }  if (RetCode!= -1)  {    RetCode=TotalRead;    if (Encryption)    {      if (Encryption<20)          {            debug_log("Old Crypt() not supported!");          }      else      {        for (I=0;I<(unsigned int)RetCode;I+=16)          DecryptBlock(&Addr[I]);      }    }  }  return(RetCode);}void UnpReadBuf(int FirstBuf){  int RetCode;  if (FirstBuf)  {    ReadTop=UnpRead(InBuf,sizeof(InBuf));    InAddr=0;  }  else  {    memcpy(InBuf,&InBuf[sizeof(InBuf)-32],32);    InAddr&=0x1f;    RetCode=UnpRead(&InBuf[32],sizeof(InBuf)-32);    if (RetCode>0)      ReadTop=RetCode+32;    else      ReadTop=InAddr;  }}void ReadTables(void){  UBYTE BitLength[BC];  unsigned char Table[MC*4];  int TableSize,N,I;  if (InAddr>sizeof(InBuf)-25)    UnpReadBuf(0);  GetBits();  UnpAudioBlock=(BitField & 0x8000);  if (!(BitField & 0x4000))    memset(UnpOldTable,0,sizeof(UnpOldTable));  AddBits(2);  if (UnpAudioBlock)  {    UnpChannels=((BitField>>12) & 3)+1;    if (CurChannel>=UnpChannels)      CurChannel=0;    AddBits(2);    TableSize=MC*UnpChannels;  }  else    TableSize=NC+DC+RC;  for (I=0;I<BC;I++)  {    GetBits();    BitLength[I]=(UBYTE)(BitField >> 12);    AddBits(4);  }  MakeDecodeTables(BitLength,(struct Decode *)&BD,BC);  I=0;  while (I<TableSize)  {    if (InAddr>sizeof(InBuf)-5)      UnpReadBuf(0);    DecodeNumber((struct Decode *)&BD);    if (Number<16)      Table[I++]=(Number+UnpOldTable[I]) & 0xf;    else      if (Number==16)      {        GetBits();        N=(BitField >> 14)+3;        AddBits(2);        while (N-- > 0 && I<TableSize)        {          Table[I]=Table[I-1];          I++;        }      }      else      {        if (Number==17)        {          GetBits();          N=(BitField >> 13)+3;          AddBits(3);        }        else        {          GetBits();          N=(BitField >> 9)+11;          AddBits(7);        }        while (N-- > 0 && I<TableSize)          Table[I++]=0;      }  }  if (UnpAudioBlock)    for (I=0;I<UnpChannels;I++)      MakeDecodeTables(&Table[I*MC],(struct Decode *)MDPtr[I],MC);  else  {    MakeDecodeTables(&Table[0],(struct Decode *)&LD,NC);    MakeDecodeTables(&Table[NC],(struct Decode *)&DD,DC);    MakeDecodeTables(&Table[NC+DC],(struct Decode *)&RD,RC);  }  memcpy(UnpOldTable,Table,sizeof(UnpOldTable));}static void ReadLastTables(void){  if (ReadTop>=InAddr+5)  {    if (UnpAudioBlock)    {      DecodeNumber((struct Decode *)MDPtr[CurChannel]);      if (Number==256)        ReadTables();    }    else    {      DecodeNumber((struct Decode *)&LD);      if (Number==269)        ReadTables();    }  }}static void MakeDecodeTables(unsigned char *LenTab,                             struct Decode *Dec,                             int Size){  int LenCount[16],TmpPos[16],I;  long M,N;  memset(LenCount,0,sizeof(LenCount));  for (I=0;I<Size;I++)    LenCount[LenTab[I] & 0xF]++;  LenCount[0]=0;  for (TmpPos[0]=Dec->DecodePos[0]=Dec->DecodeLen[0]=0,N=0,I=1;I<16;I++)  {    N=2*(N+LenCount[I]);    M=N<<(15-I);    if (M>0xFFFF)      M=0xFFFF;    Dec->DecodeLen[I]=(unsigned int)M;    TmpPos[I]=Dec->DecodePos[I]=Dec->DecodePos[I-1]+LenCount[I-1];  }  for (I=0;I<Size;I++)    if (LenTab[I]!=0)      Dec->DecodeNum[TmpPos[LenTab[I] & 0xF]++]=I;  Dec->MaxNum=Size;}static void DecodeNumber(struct Decode *Deco)/* *** 52.6% of all CPU time is spent within this function!!!               */{  unsigned int I;  register unsigned int N;  GetBits();#ifdef _USE_ASM  #ifdef _WIN_32 __asm {    xor eax, eax    mov eax, BitField                       // N=BitField & 0xFFFE;    and eax, 0xFFFFFFFE    mov [N], eax                                                     mov edx, [Deco]                         // EAX=N, EDX=Deco          cmp  eax, dword ptr[edx + 8*4 + 4]// if (N<Dec->DecodeLen[8])          jae  else_G             cmp  eax, dword ptr[edx + 4*4 + 4]// if (N<Dec->DecodeLen[4])              jae  else_F                cmp  eax, dword ptr[edx + 2*4 + 4]// if (N<Dec->DecodeLen[2])                 jae  else_C                   cmp  eax, dword ptr[edx + 1*4 + 4]// if (N<Dec->DecodeLen[1])                    jae  else_1                   mov  I, 1                         //  I=1;                   jmp  next_1                 else_1:                             // else                   mov  I, 2                         //  I=2;                 next_1:                                jmp  next_C              else_C:                             // else                                      cmp  eax, dword ptr[edx + 3*4 + 4]// if (N<Dec->DecodeLen[3])                    jae  else_2                   mov  I, 3                         //  I=3;                   jmp  next_2                 else_2:                             // else                   mov  I, 4                         //  I=4;                 next_2:              next_C:                             // else             jmp  next_F           else_F:             cmp  eax, dword ptr[edx + 6*4 + 4]// if (N<Dec->DecodeLen[6])              jae  else_E                cmp  eax, dword ptr[edx + 5*4 + 4]// if (N<Dec->DecodeLen[5])                 jae  else_3                mov  I, 5                         //  I=5;                jmp  next_3              else_3:                             // else                mov  I, 6                         //  I=6;              next_3:             jmp  next_E           else_E:                             // else                cmp  eax, dword ptr[edx + 7*4 + 4]// if (N<Dec->DecodeLen[7])                 jae  else_4                mov  I, 7                         //  I=7;                jmp  next_4              else_4:                             // else                mov  I, 8                         //  I=8;              next_4:           next_E:           next_F:          jmp  next_G        else_G:          cmp  eax, dword ptr[edx + 12*4 + 4] // if (N<Dec->DecodeLen[12])           jae  else_D             cmp  eax, dword ptr[edx + 10*4 + 4]// if (N<Dec->DecodeLen[10])              jae  else_B                cmp  eax, dword ptr[edx + 9*4 + 4]// if (N<Dec->DecodeLen[9])                jae  else_5                mov  I, 9                         //  I=9;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -