Skip to content

Commit

Permalink
fix(tar): read full extended headers (#675)
Browse files Browse the repository at this point in the history
* fix(tar): read full extended headers

* Update src/ICSharpCode.SharpZipLib/Tar/TarExtendedHeaderReader.cs
  • Loading branch information
piksel committed Aug 14, 2022
1 parent a41e066 commit 7411f3a
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/ICSharpCode.SharpZipLib/Tar/TarExtendedHeaderReader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Text;

namespace ICSharpCode.SharpZipLib.Tar
Expand Down Expand Up @@ -26,7 +27,10 @@ public class TarExtendedHeaderReader

private int state = LENGTH;

private static readonly byte[] StateNext = new[] { (byte)' ', (byte)'=', (byte)'\n' };
private int currHeaderLength;
private int currHeaderRead;

private static readonly byte[] StateNext = { (byte)' ', (byte)'=', (byte)'\n' };

/// <summary>
/// Creates a new <see cref="TarExtendedHeaderReader"/>.
Expand All @@ -46,23 +50,46 @@ public void Read(byte[] buffer, int length)
for (int i = 0; i < length; i++)
{
byte next = buffer[i];

var foundStateEnd = state == VALUE
? currHeaderRead == currHeaderLength -1
: next == StateNext[state];

if (next == StateNext[state])
if (foundStateEnd)
{
Flush();
headerParts[state] = sb.ToString();
sb.Clear();

if (++state == END)
{
headers.Add(headerParts[KEY], headerParts[VALUE]);
if (!headers.ContainsKey(headerParts[KEY]))
{
headers.Add(headerParts[KEY], headerParts[VALUE]);
}

headerParts = new string[3];
currHeaderLength = 0;
currHeaderRead = 0;
state = LENGTH;
}
else
{
currHeaderRead++;
}


if (state != VALUE) continue;

if (int.TryParse(headerParts[LENGTH], out var vl))
{
currHeaderLength = vl;
}
}
else
{
byteBuffer[bbIndex++] = next;
currHeaderRead++;
if (bbIndex == 4)
Flush();
}
Expand Down

0 comments on commit 7411f3a

Please sign in to comment.