Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion IonKiwi.lz4/LZ4RawUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,23 @@ public static unsafe int LZ41_Stream_Decompress(Stream inputStream, Stream outpu
/* Num Offsets */
inputStream.Position = initialPosition + compressedFileSize.Value - 12;
int numOffsets = br.ReadInt32();
if (numOffsets <= 0 || numOffsets > (compressedFileSize.Value - 12) / 4)
throw new Exception($"Invalid numOffsets in LZ41 footer: {numOffsets}");

/* File Size */
if(!length.HasValue)
if(!length.HasValue)
{
length = br.ReadInt32();
if (length <= 0)
throw new Exception($"Invalid uncompressed size in LZ41 footer: {length}");
}
else
inputStream.Position += 4;

/* Block Size */
int blockSize = br.ReadInt32();
if (blockSize <= 0)
throw new Exception($"Invalid block size in LZ41 footer: {blockSize}");

/* Read the offsets tail */
inputStream.Position = initialPosition + (compressedFileSize.Value - 12 - (numOffsets * 4));
Expand All @@ -315,6 +323,9 @@ public static unsafe int LZ41_Stream_Decompress(Stream inputStream, Stream outpu
int currentBlock = offset.Value / blockSize;
int endBlock = ((offset.Value + length.Value - 1) / blockSize) + 1;

if (currentBlock < 0 || endBlock >= numOffsets)
throw new Exception($"Requested range exceeds block table (blocks {currentBlock}–{endBlock}, numOffsets={numOffsets})");

/* Seek to the first block to read */
inputStream.Position = initialPosition + offsets[currentBlock];

Expand Down