Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
Update Shared.cs (#234)
Browse files Browse the repository at this point in the history
* Update Shared.cs

I think, I found the cause for the issue #139
This approach uses the byte buffer for looking for the signature, instead of reading it once with 64k, and then with 4byte steps again, if one byte matches the signature

* Update Shared.cs

* Update Shared.cs

make sure, splitted signatures, which won't fit as a whole in the buffer will be read again
  • Loading branch information
exbachi authored Dec 2, 2022
1 parent 4ab8c0e commit 168ae0e
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/Zip.Shared/Shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,31 +350,26 @@ internal static long FindSignature(System.IO.Stream stream, int SignatureToFind)
do
{
n = stream.Read(batch, 0, batch.Length);
if (n != 0)
if (n >= 4)
{
for (int i = 0; i < n; i++)
for (int i = 0; i < n - 3; i++)
{
if (batch[i] == targetBytes[3])
if (batch[i] == targetBytes[3]
&& batch[i + 1] == targetBytes[2]
&& batch[i + 2] == targetBytes[1]
&& batch[i + 3] == targetBytes[0])
{
long curPosition = stream.Position;
stream.Seek(i - n, System.IO.SeekOrigin.Current);

// workitem 7711
int sig = ReadSignature(stream);

success = (sig == SignatureToFind);
if (!success)
{
stream.Seek(curPosition, System.IO.SeekOrigin.Begin);
}
else
break; // out of for loop
stream.Seek(i - n + 4, System.IO.SeekOrigin.Current);
success = true;
break; // out of for loop
}
}
}
else break;
if (success) break;

//Move back 3 bytes, to make sure incomplete signatures will be read as a whole
stream.Seek(-3, System.IO.SeekOrigin.Current);
} while (true);

if (!success)
Expand Down

0 comments on commit 168ae0e

Please sign in to comment.