rawread.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "rar.hpp"
  2. RawRead::RawRead(File *SrcFile)
  3. {
  4. RawRead::SrcFile=SrcFile;
  5. ReadPos=0;
  6. DataSize=0;
  7. #ifndef SHELL_EXT
  8. Crypt=NULL;
  9. #endif
  10. }
  11. void RawRead::Read(size_t Size)
  12. {
  13. #if !defined(SHELL_EXT) && !defined(RAR_NOCRYPT)
  14. if (Crypt!=NULL)
  15. {
  16. size_t CurSize=Data.Size();
  17. size_t SizeToRead=Size-(CurSize-DataSize);
  18. if (SizeToRead>0)
  19. {
  20. size_t AlignedReadSize=SizeToRead+((~SizeToRead+1)&0xf);
  21. Data.Add(AlignedReadSize);
  22. size_t ReadSize=SrcFile->Read(&Data[CurSize],AlignedReadSize);
  23. Crypt->DecryptBlock(&Data[CurSize],AlignedReadSize);
  24. DataSize+=ReadSize==0 ? 0:Size;
  25. }
  26. else
  27. DataSize+=Size;
  28. }
  29. else
  30. #endif
  31. if (Size!=0)
  32. {
  33. Data.Add(Size);
  34. DataSize+=SrcFile->Read(&Data[DataSize],Size);
  35. }
  36. }
  37. void RawRead::Read(byte *SrcData,size_t Size)
  38. {
  39. if (Size!=0)
  40. {
  41. Data.Add(Size);
  42. memcpy(&Data[DataSize],SrcData,Size);
  43. DataSize+=Size;
  44. }
  45. }
  46. void RawRead::Get(byte &Field)
  47. {
  48. if (ReadPos<DataSize)
  49. {
  50. Field=Data[ReadPos];
  51. ReadPos++;
  52. }
  53. else
  54. Field=0;
  55. }
  56. void RawRead::Get(ushort &Field)
  57. {
  58. if (ReadPos+1<DataSize)
  59. {
  60. Field=Data[ReadPos]+(Data[ReadPos+1]<<8);
  61. ReadPos+=2;
  62. }
  63. else
  64. Field=0;
  65. }
  66. void RawRead::Get(uint &Field)
  67. {
  68. if (ReadPos+3<DataSize)
  69. {
  70. Field=Data[ReadPos]+(Data[ReadPos+1]<<8)+(Data[ReadPos+2]<<16)+
  71. (Data[ReadPos+3]<<24);
  72. ReadPos+=4;
  73. }
  74. else
  75. Field=0;
  76. }
  77. void RawRead::Get8(int64 &Field)
  78. {
  79. uint Low,High;
  80. Get(Low);
  81. Get(High);
  82. Field=INT32TO64(High,Low);
  83. }
  84. void RawRead::Get(byte *Field,size_t Size)
  85. {
  86. if (ReadPos+Size-1<DataSize)
  87. {
  88. memcpy(Field,&Data[ReadPos],Size);
  89. ReadPos+=Size;
  90. }
  91. else
  92. memset(Field,0,Size);
  93. }
  94. void RawRead::Get(wchar *Field,size_t Size)
  95. {
  96. if (ReadPos+2*Size-1<DataSize)
  97. {
  98. RawToWide(&Data[ReadPos],Field,Size);
  99. ReadPos+=sizeof(wchar)*Size;
  100. }
  101. else
  102. memset(Field,0,sizeof(wchar)*Size);
  103. }
  104. uint RawRead::GetCRC(bool ProcessedOnly)
  105. {
  106. return(DataSize>2 ? CRC(0xffffffff,&Data[2],(ProcessedOnly ? ReadPos:DataSize)-2):0xffffffff);
  107. }