Skip to content

Commit 168ae0e

Browse files
authored
Update Shared.cs (haf#234)
* Update Shared.cs I think, I found the cause for the issue haf#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
1 parent 4ab8c0e commit 168ae0e

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

src/Zip.Shared/Shared.cs

+11-16
Original file line numberDiff line numberDiff line change
@@ -350,31 +350,26 @@ internal static long FindSignature(System.IO.Stream stream, int SignatureToFind)
350350
do
351351
{
352352
n = stream.Read(batch, 0, batch.Length);
353-
if (n != 0)
353+
if (n >= 4)
354354
{
355-
for (int i = 0; i < n; i++)
355+
for (int i = 0; i < n - 3; i++)
356356
{
357-
if (batch[i] == targetBytes[3])
357+
if (batch[i] == targetBytes[3]
358+
&& batch[i + 1] == targetBytes[2]
359+
&& batch[i + 2] == targetBytes[1]
360+
&& batch[i + 3] == targetBytes[0])
358361
{
359-
long curPosition = stream.Position;
360-
stream.Seek(i - n, System.IO.SeekOrigin.Current);
361-
362-
// workitem 7711
363-
int sig = ReadSignature(stream);
364-
365-
success = (sig == SignatureToFind);
366-
if (!success)
367-
{
368-
stream.Seek(curPosition, System.IO.SeekOrigin.Begin);
369-
}
370-
else
371-
break; // out of for loop
362+
stream.Seek(i - n + 4, System.IO.SeekOrigin.Current);
363+
success = true;
364+
break; // out of for loop
372365
}
373366
}
374367
}
375368
else break;
376369
if (success) break;
377370

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

380375
if (!success)

0 commit comments

Comments
 (0)