C#: improved the RLE compressor, even though it's still untested. (should no longer read beyond the end of the input stream)

This commit is contained in:
barubary 2011-04-05 16:18:03 +00:00
parent edc3d2da24
commit bd9c45a89b

View File

@ -137,11 +137,12 @@ namespace DSDecmp.Formats.Nitro
{
bool foundRepetition = false;
while (currentBlockLength < dataBlock.Length)
while (currentBlockLength < dataBlock.Length && readLength < inLength)
{
nextByte = instream.ReadByte();
if (nextByte < 0)
throw new StreamTooShortException();
readLength++;
dataBlock[currentBlockLength++] = (byte)nextByte;
if (currentBlockLength > 1)
@ -188,11 +189,12 @@ namespace DSDecmp.Formats.Nitro
{
// if a repetition was found, continue until the first different byte
// (or until the buffer is full)
while (currentBlockLength < dataBlock.Length)
while (currentBlockLength < dataBlock.Length && readLength < inLength)
{
nextByte = instream.ReadByte();
if (nextByte < 0)
throw new StreamTooShortException();
readLength++;
dataBlock[currentBlockLength++] = (byte)nextByte;
@ -215,6 +217,16 @@ namespace DSDecmp.Formats.Nitro
}
}
// write any reamaining bytes as uncompressed
if (currentBlockLength > 0)
{
byte flag = (byte)(currentBlockLength - 1);
compressedData.Add(flag);
for (int i = 0; i < currentBlockLength; i++)
compressedData.Add(dataBlock[i]);
currentBlockLength = 0;
}
if (compressedData.Count > 0xFFFFFF)
throw new InputTooLargeException();