Skip to content

Commit

Permalink
Merge pull request #785 from Erior/feature/Issue-782
Browse files Browse the repository at this point in the history
Handle tar files generated with tar -H oldgnu that has large uid/gid values
  • Loading branch information
adamhathcock authored Dec 11, 2023
2 parents b7ea9dd + 18c7f58 commit 8a59fc9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/SharpCompress/Common/Tar/Headers/TarHeader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#nullable disable
#nullable disable

using System;
using System.Buffers.Binary;
Expand Down Expand Up @@ -132,8 +132,8 @@ internal bool Read(BinaryReader reader)
Mode |= 0b1_000_000_000;
}

UserId = ReadAsciiInt64Base8(buffer, 108, 7);
GroupId = ReadAsciiInt64Base8(buffer, 116, 7);
UserId = ReadAsciiInt64Base8oldGnu(buffer, 108, 7);
GroupId = ReadAsciiInt64Base8oldGnu(buffer, 116, 7);
var unixTimeStamp = ReadAsciiInt64Base8(buffer, 136, 11);
LastModifiedTime = EPOCH.AddSeconds(unixTimeStamp).ToLocalTime();

Expand Down Expand Up @@ -249,6 +249,24 @@ private static long ReadAsciiInt64Base8(byte[] buffer, int offset, int count)
return Convert.ToInt64(s, 8);
}

private static long ReadAsciiInt64Base8oldGnu(byte[] buffer, int offset, int count)
{
if (buffer[offset] == 0x80 && buffer[offset + 1] == 0x00)
{
return buffer[offset + 4] << 24
| buffer[offset + 5] << 16
| buffer[offset + 6] << 8
| buffer[offset + 7];
}
var s = Encoding.UTF8.GetString(buffer, offset, count).TrimNulls();

if (string.IsNullOrEmpty(s))
{
return 0;
}
return Convert.ToInt64(s, 8);
}

private static long ReadAsciiInt64(byte[] buffer, int offset, int count)
{
var s = Encoding.UTF8.GetString(buffer, offset, count).TrimNulls();
Expand Down
3 changes: 3 additions & 0 deletions tests/SharpCompress.Test/Tar/TarReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public void Tar_Skip()
[Fact]
public void Tar_Xz_Reader() => Read("Tar.tar.xz", CompressionType.Xz);

[Fact]
public void Tar_GZip_OldGnu_Reader() => Read("Tar.oldgnu.tar.gz", CompressionType.GZip);

[Fact]
public void Tar_BZip2_Entry_Stream()
{
Expand Down
Binary file added tests/TestArchives/Archives/Tar.oldgnu.tar.gz
Binary file not shown.

0 comments on commit 8a59fc9

Please sign in to comment.