Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tar): read full extended headers #675

Merged
merged 2 commits into from
Aug 14, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 33 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,47 @@ 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]);
Console.WriteLine($"KEY: {headerParts[KEY]}");
piksel marked this conversation as resolved.
Show resolved Hide resolved
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