Skip to content

Commit 8bbec34

Browse files
committed
fix(tar): read full extended headers
1 parent e1e1a91 commit 8bbec34

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

src/ICSharpCode.SharpZipLib/Tar/TarExtendedHeaderReader.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Text;
34

45
namespace ICSharpCode.SharpZipLib.Tar
@@ -26,7 +27,10 @@ public class TarExtendedHeaderReader
2627

2728
private int state = LENGTH;
2829

29-
private static readonly byte[] StateNext = new[] { (byte)' ', (byte)'=', (byte)'\n' };
30+
private int currHeaderLength;
31+
private int currHeaderRead;
32+
33+
private static readonly byte[] StateNext = { (byte)' ', (byte)'=', (byte)'\n' };
3034

3135
/// <summary>
3236
/// Creates a new <see cref="TarExtendedHeaderReader"/>.
@@ -46,23 +50,47 @@ public void Read(byte[] buffer, int length)
4650
for (int i = 0; i < length; i++)
4751
{
4852
byte next = buffer[i];
53+
54+
var foundStateEnd = state == VALUE
55+
? currHeaderRead == currHeaderLength -1
56+
: next == StateNext[state];
4957

50-
if (next == StateNext[state])
58+
if (foundStateEnd)
5159
{
5260
Flush();
5361
headerParts[state] = sb.ToString();
5462
sb.Clear();
55-
63+
5664
if (++state == END)
5765
{
58-
headers.Add(headerParts[KEY], headerParts[VALUE]);
66+
Console.WriteLine($"KEY: {headerParts[KEY]}");
67+
if (!headers.ContainsKey(headerParts[KEY]))
68+
{
69+
headers.Add(headerParts[KEY], headerParts[VALUE]);
70+
}
71+
5972
headerParts = new string[3];
73+
currHeaderLength = 0;
74+
currHeaderRead = 0;
6075
state = LENGTH;
6176
}
77+
else
78+
{
79+
currHeaderRead++;
80+
}
81+
82+
83+
if (state != VALUE) continue;
84+
85+
if (int.TryParse(headerParts[LENGTH], out var vl))
86+
{
87+
currHeaderLength = vl;
88+
}
6289
}
6390
else
6491
{
6592
byteBuffer[bbIndex++] = next;
93+
currHeaderRead++;
6694
if (bbIndex == 4)
6795
Flush();
6896
}

0 commit comments

Comments
 (0)