(MAI4): be tolerant to premature end-of-file.

This commit is contained in:
morkt 2017-01-27 13:17:43 +04:00
parent c0a8ea8165
commit 65a8bce839

View File

@ -52,7 +52,6 @@ namespace GameRes.Formats.ShiinaRio
public override ImageData Read (IBinaryStream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
stream.Position = 0x10;
using (var reader = new Reader (stream, (int)info.Width, (int)info.Height)) using (var reader = new Reader (stream, (int)info.Width, (int)info.Height))
{ {
reader.Unpack (); reader.Unpack ();
@ -80,13 +79,19 @@ namespace GameRes.Formats.ShiinaRio
m_output = new byte[m_stride*height]; m_output = new byte[m_stride*height];
} }
uint m_bit_count; int m_bit_count;
uint m_bits; uint m_bits;
void ResetBits () void LoadBits ()
{ {
m_bits = m_input.ReadUInt32(); for (int i = 0; i < 4; ++i)
m_bit_count = 32; {
int b = m_input.ReadByte();
if (-1 == b)
break;
m_bits = (m_bits >> 8) | (uint)(b << 24);
m_bit_count += 8;
}
} }
uint GetBit () uint GetBit ()
@ -95,8 +100,7 @@ namespace GameRes.Formats.ShiinaRio
m_bits <<= 1; m_bits <<= 1;
if (0 == --m_bit_count) if (0 == --m_bit_count)
{ {
m_bits = m_input.ReadUInt32(); LoadBits();
m_bit_count = 32;
} }
return bit; return bit;
} }
@ -113,7 +117,9 @@ namespace GameRes.Formats.ShiinaRio
public void Unpack () public void Unpack ()
{ {
ResetBits(); m_input.Position = 0x10;
m_bit_count = 0;
LoadBits();
int dst = 0; int dst = 0;
byte b = 0, g = 0, r = 0; byte b = 0, g = 0, r = 0;
while (dst < m_output.Length) while (dst < m_output.Length)
@ -122,9 +128,12 @@ namespace GameRes.Formats.ShiinaRio
{ {
if (GetBit() != 0) if (GetBit() != 0)
{ {
b = m_input.ReadUInt8(); if (m_input.PeekByte() != -1)
g = m_input.ReadUInt8(); {
r = m_input.ReadUInt8(); b = m_input.ReadUInt8();
g = m_input.ReadUInt8();
r = m_input.ReadUInt8();
}
} }
else if (GetBit() != 0) else if (GetBit() != 0)
{ {