Skip to content
Merged
Changes from all commits
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
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