Skip to content

Commit

Permalink
Check crc on tar header
Browse files Browse the repository at this point in the history
  • Loading branch information
Erior committed Jul 10, 2024
1 parent d18cad6 commit e42d953
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/SharpCompress/Common/Tar/Headers/TarHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ internal bool Read(BinaryReader reader)
hasLongValue = false;
} while (hasLongValue);

var crc = ReadAsciiInt64Base8(buffer, 148, 7);

if (crc != RecalculateChecksum(buffer))
{
return false;
}

Name = longName ?? ArchiveEncoding.Decode(buffer, 0, 100).TrimNulls();
EntryType = entryType;
Size = ReadSize(buffer);
Expand Down
11 changes: 9 additions & 2 deletions tests/SharpCompress.Test/Tar/TarArchiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,7 @@ public void Tar_Empty_Archive()
{
var archiveFullPath = Path.Combine(TEST_ARCHIVES_PATH, "Tar.Empty.tar");
using Stream stream = File.OpenRead(archiveFullPath);
using var archive = ArchiveFactory.Open(stream);
Assert.True(archive.Type == ArchiveType.Tar);
Assert.Throws<InvalidOperationException>(() => ArchiveFactory.Open(stream));
}

[Theory]
Expand Down Expand Up @@ -287,4 +286,12 @@ public void Tar_Read_One_At_A_Time()

Assert.Equal(2, numberOfEntries);
}

[Fact]
public void Tar_Detect_Test()
{
var isTar = TarArchive.IsTarFile(Path.Combine(TEST_ARCHIVES_PATH, "false.positive.tar"));

Assert.False(isTar);
}
}
Binary file not shown.

0 comments on commit e42d953

Please sign in to comment.