public Model(Stream stream, string path) { base._data = Data = new GifFormat(this, stream, path); // Arbitrary sanity limit. if (Data.FileSize > 50000000) { IssueModel.Add("File size insanely huge", Severity.Fatal); return; } Data.fBuf = new byte[(int)Data.FileSize]; stream.Position = 0; int got = stream.Read(Data.fBuf, 0, (int)Data.FileSize); if (got < Data.FileSize) { IssueModel.Add("Read failed.", Severity.Fatal); return; } Data.Version = Data.fBuf[4] == '7'? "87a" : "89a"; Data.Width = ConvertTo.FromLit16ToInt32(Data.fBuf, 6); Data.Height = ConvertTo.FromLit16ToInt32(Data.fBuf, 8); }
public Model(Stream stream, string path) { base._data = Data = new IcoFormat(this, stream, path); // Arbitrary sanity limit. if (Data.FileSize > 50000000) { IssueModel.Add("File insanely large", Severity.Fatal); return; } var buf = new byte[Data.FileSize]; stream.Position = 0; var got = stream.Read(buf, 0, (int)Data.FileSize); if (got != Data.FileSize) { IssueModel.Add("Read error", Severity.Fatal); return; } int headerCount = ConvertTo.FromLit16ToInt32(buf, 4); if (headerCount <= 0) { IssueModel.Add("Corrupt header count", Severity.Fatal); return; } int pos = 6; int stop = pos + 16 * headerCount; int actualStart = stop; for (pos = 6; pos < stop; pos += 16) { int width, height, bpp, paletteSize; int storedSize = ConvertTo.FromLit32ToInt32(buf, pos + 8); int storedStart = ConvertTo.FromLit32ToInt32(buf, pos + 12); if (storedStart != actualStart || storedSize <= 0) { IssueModel.Add($"Corrupt header near byte {Data.ValidSize}", Severity.Fatal); return; } bool isPNG = buf[actualStart] == 0x89 && buf[actualStart + 1] == 'P' && buf[actualStart + 2] == 'N' && buf[actualStart + 3] == 'G'; if (isPNG) { width = ConvertTo.FromBig32ToInt32(buf, actualStart + 16); height = ConvertTo.FromBig32ToInt32(buf, actualStart + 20); paletteSize = 0; bpp = buf[actualStart + 24]; } else { width = buf[pos]; if (width == 0) { width = 256; } height = buf[pos + 1]; if (height == 0) { height = 256; } paletteSize = buf[pos + 2]; bpp = buf[pos + 6]; } Data.icons.Add(new IconItem(width, height, paletteSize, bpp, storedStart, storedSize, isPNG)); actualStart += storedSize; } if (actualStart != Data.FileSize) { IssueModel.Add("Incorrect file size"); return; } }