Skip to content

Commit

Permalink
Merge pull request #784 from Erior/feature/rar-comment
Browse files Browse the repository at this point in the history
Dont crash on reading rar5 comment #783
  • Loading branch information
adamhathcock authored Dec 11, 2023
2 parents 67be0cd + 7f6f7b1 commit b7ea9dd
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,14 @@ public IEnumerable<IRarHeader> ReadHeaders(Stream stream)
case HeaderCodeV.RAR5_SERVICE_HEADER:
{
var fh = new FileHeader(header, reader, HeaderType.Service);
SkipData(fh, reader);
if (fh.FileName == "CMT")
{
fh.PackedStream = new ReadOnlySubStream(reader.BaseStream, fh.CompressedSize);
}
else
{
SkipData(fh, reader);
}
return fh;
}

Expand Down
7 changes: 2 additions & 5 deletions src/SharpCompress/Common/Rar/RarVolume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using SharpCompress.Common.Rar.Headers;
using SharpCompress.IO;
using SharpCompress.Readers;
Expand Down Expand Up @@ -70,11 +71,7 @@ internal IEnumerable<RarFilePart> GetVolumeFileParts()
var part = CreateFilePart(lastMarkHeader!, fh);
var buffer = new byte[fh.CompressedSize];
part.GetCompressedStream().Read(buffer, 0, buffer.Length);
Comment = System
.Text
.Encoding
.UTF8
.GetString(buffer, 0, buffer.Length - 1);
Comment = Encoding.UTF8.GetString(buffer, 0, buffer.Length - 1);
}
}
break;
Expand Down
6 changes: 6 additions & 0 deletions tests/SharpCompress.Test/Rar/RarReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ public void Rar_Jpg_Reader()
[Fact]
public void Rar_Solid_Reader() => Read("Rar.solid.rar", CompressionType.Rar);

[Fact]
public void Rar_Comment_Reader() => Read("Rar.comment.rar", CompressionType.Rar);

[Fact]
public void Rar5_Comment_Reader() => Read("Rar5.comment.rar", CompressionType.Rar);

[Fact]
public void Rar5_Solid_Reader() => Read("Rar5.solid.rar", CompressionType.Rar);

Expand Down
Binary file added tests/TestArchives/Archives/Rar.comment.rar
Binary file not shown.
Binary file added tests/TestArchives/Archives/Rar5.comment.rar
Binary file not shown.

0 comments on commit b7ea9dd

Please sign in to comment.