2009-11-10 16:48:42 +01:00
|
|
|
#include "rar.hpp"
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
RawRead::RawRead()
|
|
|
|
{
|
|
|
|
RawRead::SrcFile=NULL;
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-10 16:48:42 +01:00
|
|
|
RawRead::RawRead(File *SrcFile)
|
|
|
|
{
|
|
|
|
RawRead::SrcFile=SrcFile;
|
2020-07-15 00:40:26 +02:00
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RawRead::Reset()
|
|
|
|
{
|
|
|
|
Data.SoftReset();
|
2009-11-10 16:48:42 +01:00
|
|
|
ReadPos=0;
|
|
|
|
DataSize=0;
|
|
|
|
Crypt=NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
size_t RawRead::Read(size_t Size)
|
2009-11-10 16:48:42 +01:00
|
|
|
{
|
2020-07-15 00:40:26 +02:00
|
|
|
size_t ReadSize=0;
|
|
|
|
#if !defined(RAR_NOCRYPT)
|
2009-11-10 16:48:42 +01:00
|
|
|
if (Crypt!=NULL)
|
|
|
|
{
|
2020-07-15 00:40:26 +02:00
|
|
|
// Full size of buffer with already read data including data read
|
|
|
|
// for encryption block alignment.
|
|
|
|
size_t FullSize=Data.Size();
|
|
|
|
|
|
|
|
// Data read for alignment and not processed yet.
|
|
|
|
size_t DataLeft=FullSize-DataSize;
|
|
|
|
|
|
|
|
if (Size>DataLeft) // Need to read more than we already have.
|
2009-11-10 16:48:42 +01:00
|
|
|
{
|
2020-07-15 00:40:26 +02:00
|
|
|
size_t SizeToRead=Size-DataLeft;
|
|
|
|
size_t AlignedReadSize=SizeToRead+((~SizeToRead+1) & CRYPT_BLOCK_MASK);
|
2009-11-10 16:48:42 +01:00
|
|
|
Data.Add(AlignedReadSize);
|
2020-07-15 00:40:26 +02:00
|
|
|
ReadSize=SrcFile->Read(&Data[FullSize],AlignedReadSize);
|
|
|
|
Crypt->DecryptBlock(&Data[FullSize],AlignedReadSize);
|
2009-11-10 16:48:42 +01:00
|
|
|
DataSize+=ReadSize==0 ? 0:Size;
|
|
|
|
}
|
2020-07-15 00:40:26 +02:00
|
|
|
else // Use buffered data, no real read.
|
|
|
|
{
|
|
|
|
ReadSize=Size;
|
2009-11-10 16:48:42 +01:00
|
|
|
DataSize+=Size;
|
2020-07-15 00:40:26 +02:00
|
|
|
}
|
2009-11-10 16:48:42 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (Size!=0)
|
|
|
|
{
|
|
|
|
Data.Add(Size);
|
2020-07-15 00:40:26 +02:00
|
|
|
ReadSize=SrcFile->Read(&Data[DataSize],Size);
|
|
|
|
DataSize+=ReadSize;
|
2009-11-10 16:48:42 +01:00
|
|
|
}
|
2020-07-15 00:40:26 +02:00
|
|
|
return ReadSize;
|
2009-11-10 16:48:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RawRead::Read(byte *SrcData,size_t Size)
|
|
|
|
{
|
|
|
|
if (Size!=0)
|
|
|
|
{
|
|
|
|
Data.Add(Size);
|
|
|
|
memcpy(&Data[DataSize],SrcData,Size);
|
|
|
|
DataSize+=Size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
byte RawRead::Get1()
|
2009-11-10 16:48:42 +01:00
|
|
|
{
|
2020-07-15 00:40:26 +02:00
|
|
|
return ReadPos<DataSize ? Data[ReadPos++]:0;
|
2009-11-10 16:48:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
ushort RawRead::Get2()
|
2009-11-10 16:48:42 +01:00
|
|
|
{
|
|
|
|
if (ReadPos+1<DataSize)
|
|
|
|
{
|
2020-07-15 00:40:26 +02:00
|
|
|
ushort Result=Data[ReadPos]+(Data[ReadPos+1]<<8);
|
2009-11-10 16:48:42 +01:00
|
|
|
ReadPos+=2;
|
2020-07-15 00:40:26 +02:00
|
|
|
return Result;
|
2009-11-10 16:48:42 +01:00
|
|
|
}
|
2020-07-15 00:40:26 +02:00
|
|
|
return 0;
|
2009-11-10 16:48:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
uint RawRead::Get4()
|
2009-11-10 16:48:42 +01:00
|
|
|
{
|
|
|
|
if (ReadPos+3<DataSize)
|
|
|
|
{
|
2020-07-15 00:40:26 +02:00
|
|
|
uint Result=Data[ReadPos]+(Data[ReadPos+1]<<8)+(Data[ReadPos+2]<<16)+
|
|
|
|
(Data[ReadPos+3]<<24);
|
2009-11-10 16:48:42 +01:00
|
|
|
ReadPos+=4;
|
2020-07-15 00:40:26 +02:00
|
|
|
return Result;
|
2009-11-10 16:48:42 +01:00
|
|
|
}
|
2020-07-15 00:40:26 +02:00
|
|
|
return 0;
|
2009-11-10 16:48:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
uint64 RawRead::Get8()
|
2009-11-10 16:48:42 +01:00
|
|
|
{
|
2020-07-15 00:40:26 +02:00
|
|
|
uint Low=Get4(),High=Get4();
|
|
|
|
return INT32TO64(High,Low);
|
2009-11-10 16:48:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
uint64 RawRead::GetV()
|
2009-11-10 16:48:42 +01:00
|
|
|
{
|
2020-07-15 00:40:26 +02:00
|
|
|
uint64 Result=0;
|
|
|
|
// Need to check Shift<64, because for shift greater than or equal to
|
|
|
|
// the width of the promoted left operand, the behavior is undefined.
|
|
|
|
for (uint Shift=0;ReadPos<DataSize && Shift<64;Shift+=7)
|
2009-11-10 16:48:42 +01:00
|
|
|
{
|
2020-07-15 00:40:26 +02:00
|
|
|
byte CurByte=Data[ReadPos++];
|
|
|
|
Result+=uint64(CurByte & 0x7f)<<Shift;
|
|
|
|
if ((CurByte & 0x80)==0)
|
|
|
|
return Result; // Decoded successfully.
|
2009-11-10 16:48:42 +01:00
|
|
|
}
|
2020-07-15 00:40:26 +02:00
|
|
|
return 0; // Out of buffer border.
|
2009-11-10 16:48:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
// Return a number of bytes in current variable length integer.
|
|
|
|
uint RawRead::GetVSize(size_t Pos)
|
|
|
|
{
|
|
|
|
for (size_t CurPos=Pos;CurPos<DataSize;CurPos++)
|
|
|
|
if ((Data[CurPos] & 0x80)==0)
|
|
|
|
return int(CurPos-Pos+1);
|
|
|
|
return 0; // Buffer overflow.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t RawRead::GetB(void *Field,size_t Size)
|
|
|
|
{
|
|
|
|
byte *F=(byte *)Field;
|
|
|
|
size_t CopySize=Min(DataSize-ReadPos,Size);
|
|
|
|
if (CopySize>0)
|
|
|
|
memcpy(F,&Data[ReadPos],CopySize);
|
|
|
|
if (Size>CopySize)
|
|
|
|
memset(F+CopySize,0,Size-CopySize);
|
|
|
|
ReadPos+=CopySize;
|
|
|
|
return CopySize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RawRead::GetW(wchar *Field,size_t Size)
|
2009-11-10 16:48:42 +01:00
|
|
|
{
|
|
|
|
if (ReadPos+2*Size-1<DataSize)
|
|
|
|
{
|
|
|
|
RawToWide(&Data[ReadPos],Field,Size);
|
2020-07-15 00:40:26 +02:00
|
|
|
ReadPos+=sizeof(wchar)*Size;
|
2009-11-10 16:48:42 +01:00
|
|
|
}
|
|
|
|
else
|
2020-07-15 00:40:26 +02:00
|
|
|
memset(Field,0,sizeof(wchar)*Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint RawRead::GetCRC15(bool ProcessedOnly) // RAR 1.5 block CRC.
|
|
|
|
{
|
|
|
|
if (DataSize<=2)
|
|
|
|
return 0;
|
|
|
|
uint HeaderCRC=CRC32(0xffffffff,&Data[2],(ProcessedOnly ? ReadPos:DataSize)-2);
|
|
|
|
return ~HeaderCRC & 0xffff;
|
2009-11-10 16:48:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-15 00:40:26 +02:00
|
|
|
uint RawRead::GetCRC50() // RAR 5.0 block CRC.
|
2009-11-10 16:48:42 +01:00
|
|
|
{
|
2020-07-15 00:40:26 +02:00
|
|
|
if (DataSize<=4)
|
|
|
|
return 0xffffffff;
|
|
|
|
return CRC32(0xffffffff,&Data[4],DataSize-4) ^ 0xffffffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Read vint from arbitrary byte array.
|
|
|
|
uint64 RawGetV(const byte *Data,uint &ReadPos,uint DataSize,bool &Overflow)
|
|
|
|
{
|
|
|
|
Overflow=false;
|
|
|
|
uint64 Result=0;
|
|
|
|
for (uint Shift=0;ReadPos<DataSize;Shift+=7)
|
|
|
|
{
|
|
|
|
byte CurByte=Data[ReadPos++];
|
|
|
|
Result+=uint64(CurByte & 0x7f)<<Shift;
|
|
|
|
if ((CurByte & 0x80)==0)
|
|
|
|
return Result; // Decoded successfully.
|
|
|
|
}
|
|
|
|
Overflow=true;
|
|
|
|
return 0; // Out of buffer border.
|
2009-11-10 16:48:42 +01:00
|
|
|
}
|