-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
verify RAR crc on header and file data
- Loading branch information
Showing
6 changed files
with
101 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.IO; | ||
using SharpCompress.Compressors.Rar; | ||
using SharpCompress.IO; | ||
|
||
namespace SharpCompress.Common.Rar { | ||
internal class RarCrcBinaryReader : MarkingBinaryReader { | ||
private uint currentCrc; | ||
|
||
public RarCrcBinaryReader(Stream stream) : base(stream) | ||
{ | ||
} | ||
|
||
public ushort GetCrc() | ||
{ | ||
return (ushort)~this.currentCrc; | ||
} | ||
|
||
public void ResetCrc() | ||
{ | ||
this.currentCrc = 0xffffffff; | ||
} | ||
|
||
public override byte[] ReadBytes(int count) | ||
{ | ||
var result = base.ReadBytes(count); | ||
this.currentCrc = RarCRC.CheckCrc(this.currentCrc, result, 0, result.Length); | ||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.IO; | ||
using SharpCompress.Common; | ||
using SharpCompress.Common.Rar.Headers; | ||
|
||
namespace SharpCompress.Compressors.Rar { | ||
internal class RarCrcStream : RarStream { | ||
private readonly uint expectedCrc; | ||
private uint currentCrc; | ||
|
||
public RarCrcStream(Unpack unpack, FileHeader fileHeader, Stream readStream) : base(unpack, fileHeader, readStream) | ||
{ | ||
this.expectedCrc = fileHeader.FileCRC; | ||
ResetCrc(); | ||
} | ||
|
||
public uint GetCrc() | ||
{ | ||
return ~this.currentCrc; | ||
} | ||
|
||
public void ResetCrc() | ||
{ | ||
this.currentCrc = 0xffffffff; | ||
} | ||
|
||
|
||
public override int Read(byte[] buffer, int offset, int count) | ||
{ | ||
var result = base.Read(buffer, offset, count); | ||
if (result != 0) | ||
{ | ||
this.currentCrc = RarCRC.CheckCrc(this.currentCrc, buffer, offset, result); | ||
} else if (GetCrc() != this.expectedCrc) | ||
{ | ||
throw new InvalidFormatException("file crc mismatch"); | ||
} | ||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters