1
- using System . Collections . Generic ;
1
+ using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Text ;
3
4
4
5
namespace ICSharpCode . SharpZipLib . Tar
@@ -26,7 +27,10 @@ public class TarExtendedHeaderReader
26
27
27
28
private int state = LENGTH ;
28
29
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 ' } ;
30
34
31
35
/// <summary>
32
36
/// Creates a new <see cref="TarExtendedHeaderReader"/>.
@@ -46,23 +50,46 @@ public void Read(byte[] buffer, int length)
46
50
for ( int i = 0 ; i < length ; i ++ )
47
51
{
48
52
byte next = buffer [ i ] ;
53
+
54
+ var foundStateEnd = state == VALUE
55
+ ? currHeaderRead == currHeaderLength - 1
56
+ : next == StateNext [ state ] ;
49
57
50
- if ( next == StateNext [ state ] )
58
+ if ( foundStateEnd )
51
59
{
52
60
Flush ( ) ;
53
61
headerParts [ state ] = sb . ToString ( ) ;
54
62
sb . Clear ( ) ;
55
-
63
+
56
64
if ( ++ state == END )
57
65
{
58
- headers . Add ( headerParts [ KEY ] , headerParts [ VALUE ] ) ;
66
+ if ( ! headers . ContainsKey ( headerParts [ KEY ] ) )
67
+ {
68
+ headers . Add ( headerParts [ KEY ] , headerParts [ VALUE ] ) ;
69
+ }
70
+
59
71
headerParts = new string [ 3 ] ;
72
+ currHeaderLength = 0 ;
73
+ currHeaderRead = 0 ;
60
74
state = LENGTH ;
61
75
}
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
+ }
62
88
}
63
89
else
64
90
{
65
91
byteBuffer [ bbIndex ++ ] = next ;
92
+ currHeaderRead ++ ;
66
93
if ( bbIndex == 4 )
67
94
Flush ( ) ;
68
95
}
0 commit comments