public static Quetzal FromStream(Stream stream)
{
Quetzal result = new Quetzal();
uint type = BigEndian.ReadInt32(stream);
if (type != FORM && type != LIST && type != CAT_)
throw new ArgumentException("Invalid IFF type");
int length = (int)BigEndian.ReadInt32(stream);
byte[] buffer = new byte[length];
int amountRead = stream.Read(buffer, 0, (int)length);
if (amountRead < length)
throw new ArgumentException("Quetzal file is too short");
stream = new MemoryStream(buffer);
type = BigEndian.ReadInt32(stream);
if (type != IFZS)
throw new ArgumentException("Wrong IFF sub-type: not a Quetzal file");
while (stream.Position < stream.Length)
{
type = BigEndian.ReadInt32(stream);
length = (int)BigEndian.ReadInt32(stream);
byte[] chunk = new byte[length];
amountRead = stream.Read(chunk, 0, length);
if (amountRead < length)
throw new ArgumentException("Chunk extends past end of file");
result.chunks[type] = chunk;
}
return result;
}