-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathTarExtendedHeaderReader.cs
More file actions
126 lines (105 loc) · 2.66 KB
/
Copy pathTarExtendedHeaderReader.cs
File metadata and controls
126 lines (105 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Collections.Generic;
using System.Text;
namespace ICSharpCode.SharpZipLib.Tar
{
/// <summary>
/// Reads the extended header of a Tar stream
/// </summary>
public class TarExtendedHeaderReader
{
private const byte LENGTH = 0;
private const byte KEY = 1;
private const byte VALUE = 2;
private const byte END = 3;
private readonly Dictionary<string, string> headers = new Dictionary<string, string>();
private string[] headerParts = new string[3];
private int bbIndex;
private byte[] byteBuffer;
private char[] charBuffer;
private readonly StringBuilder sb = new StringBuilder();
private readonly Decoder decoder = Encoding.UTF8.GetDecoder();
private int state = LENGTH;
private int currHeaderLength;
private int currHeaderRead;
private static readonly byte[] StateNext = { (byte)' ', (byte)'=', (byte)'\n' };
/// <summary>
/// Creates a new <see cref="TarExtendedHeaderReader"/>.
/// </summary>
public TarExtendedHeaderReader()
{
ResetBuffers();
}
/// <summary>
/// Read <paramref name="length"/> bytes from <paramref name="buffer"/>
/// </summary>
/// <param name="buffer"></param>
/// <param name="length"></param>
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 (foundStateEnd)
{
Flush();
headerParts[state] = sb.ToString();
sb.Clear();
if (++state == END)
{
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();
}
}
}
private void Flush()
{
decoder.Convert(byteBuffer, 0, bbIndex, charBuffer, 0, 4, false, out int bytesUsed, out int charsUsed, out bool completed);
sb.Append(charBuffer, 0, charsUsed);
ResetBuffers();
}
private void ResetBuffers()
{
charBuffer = new char[4];
byteBuffer = new byte[4];
bbIndex = 0;
}
/// <summary>
/// Returns the parsed headers as key-value strings
/// </summary>
public Dictionary<string, string> Headers
{
get
{
// TODO: Check for invalid state? -NM 2018-07-01
return headers;
}
}
}
}