Skip to content

Commit 7411f3a

Browse files
authored
fix(tar): read full extended headers (#675)
* fix(tar): read full extended headers * Update src/ICSharpCode.SharpZipLib/Tar/TarExtendedHeaderReader.cs
1 parent a41e066 commit 7411f3a

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

src/ICSharpCode.SharpZipLib/Tar/TarExtendedHeaderReader.cs

Lines changed: 32 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,46 @@ 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+
if (!headers.ContainsKey(headerParts[KEY]))
67+
{
68+
headers.Add(headerParts[KEY], headerParts[VALUE]);
69+
}
70+
5971
headerParts = new string[3];
72+
currHeaderLength = 0;
73+
currHeaderRead = 0;
6074
state = LENGTH;
6175
}
76+
else
77+
{
78+
currHeaderRead++;
79+
}
80+
81+
82+
if (state != VALUE) continue;
83+
84+
if (int.TryParse(headerParts[LENGTH], out var vl))
85+
{
86+
currHeaderLength = vl;
87+
}
6288
}
6389
else
6490
{
6591
byteBuffer[bbIndex++] = next;
92+
currHeaderRead++;
6693
if (bbIndex == 4)
6794
Flush();
6895
}

0 commit comments

Comments
 (0)