Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check crc on tar header #855

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Erior marked this conversation as resolved.
Show resolved Hide resolved
}

[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.