RarCrcStream.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System.IO;
  2. using SharpCompress.Common;
  3. using SharpCompress.Common.Rar.Headers;
  4. namespace SharpCompress.Compressors.Rar
  5. {
  6. internal class RarCrcStream : RarStream
  7. {
  8. private readonly MultiVolumeReadOnlyStream readStream;
  9. private uint currentCrc;
  10. public RarCrcStream(IRarUnpack unpack, FileHeader fileHeader, MultiVolumeReadOnlyStream readStream)
  11. : base(unpack, fileHeader, readStream)
  12. {
  13. this.readStream = readStream;
  14. ResetCrc();
  15. }
  16. public uint GetCrc()
  17. {
  18. return ~currentCrc;
  19. }
  20. public void ResetCrc()
  21. {
  22. currentCrc = 0xffffffff;
  23. }
  24. public override int Read(byte[] buffer, int offset, int count)
  25. {
  26. var result = base.Read(buffer, offset, count);
  27. if (result != 0)
  28. {
  29. currentCrc = RarCRC.CheckCrc(currentCrc, buffer, offset, result);
  30. }
  31. else if (GetCrc() != readStream.CurrentCrc && count != 0)
  32. {
  33. // NOTE: we use the last FileHeader in a multipart volume to check CRC
  34. throw new InvalidFormatException("file crc mismatch");
  35. }
  36. return result;
  37. }
  38. }
  39. }