mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-11-18 18:32:34 +00:00
Handle partial LZip headers and validate dictionary sizes consistently
This commit is contained in:
@@ -129,11 +129,7 @@ public sealed partial class LZipStream
|
||||
var header = ArrayPool<byte>.Shared.Rent(6);
|
||||
try
|
||||
{
|
||||
var n = await stream.ReadAsync(header, 0, 6, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// TODO: Handle reading only part of the header?
|
||||
|
||||
if (n != 6)
|
||||
if (!await stream.ReadFullyAsync(header, 0, 6, cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@@ -148,13 +144,7 @@ public sealed partial class LZipStream
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
var basePower = header[5] & 0x1F;
|
||||
var subtractionNumerator = (header[5] & 0xE0) >> 5;
|
||||
if (basePower < 4 || basePower > 30)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return (1 << basePower) - (subtractionNumerator * (1 << (basePower - 4)));
|
||||
return DecodeDictionarySize(header[5]);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
@@ -253,11 +253,7 @@ public sealed partial class LZipStream : Stream, IFinishable
|
||||
{
|
||||
// Read the header
|
||||
Span<byte> header = stackalloc byte[6];
|
||||
var n = stream.Read(header);
|
||||
|
||||
// TODO: Handle reading only part of the header?
|
||||
|
||||
if (n != 6)
|
||||
if (!stream.ReadFully(header))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@@ -272,9 +268,18 @@ public sealed partial class LZipStream : Stream, IFinishable
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
var basePower = header[5] & 0x1F;
|
||||
var subtractionNumerator = (header[5] & 0xE0) >> 5;
|
||||
return (1 << basePower) - (subtractionNumerator * (1 << (basePower - 4)));
|
||||
return DecodeDictionarySize(header[5]);
|
||||
}
|
||||
|
||||
private static int DecodeDictionarySize(byte encoded)
|
||||
{
|
||||
var basePower = encoded & 0x1F;
|
||||
if (basePower < 12 || basePower > 29)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
var size = (1 << basePower) - ((encoded >> 5) * (1 << (basePower - 4)));
|
||||
return size >= 4096 ? size : 0;
|
||||
}
|
||||
|
||||
// Async methods moved to LZipStream.Async.cs
|
||||
|
||||
Reference in New Issue
Block a user