using Titano.Core.Codecs.Jpeg; using Titano.Testing; using static Titano.Core.Tests.Codecs.CodecTestData; namespace Titano.Core.Tests.Codecs; /// Corrupt, truncated and hostile JPEG input: only may escape. public sealed class JpegRobustnessTests(TestContext context) { private static byte[] LosslessSample() { var samples = new ushort[24 * 10 * 2]; for (var i = 0; i < samples.Length; i++) samples[i] = (ushort)(i * 37 % 4096); return JpegLosslessEncoder.Encode(samples, 24, 10, 2, 12, predictor: 6, restartInterval: 24); } [Theory] [Case("jpeg/b420.jpg")] [Case("jpeg/b411.jpg")] [Case("jpeg/p420.jpg")] [Case("jpeg/p444.jpg")] [Case("jpeg/r420.jpg")] [Case("jpeg/pgray.jpg")] [Case("jpeg/cmyk.jpg")] [Case("lossless")] public void FuzzedInputOnlyThrowsInvalidData(string source) { var valid = source == "lossless" ? LosslessSample() : Fixture(source); var options = new[] { JpegDecodeOptions.Default, new JpegDecodeOptions { MaxLongEdge = 16, FancyUpsampling = false } }; int decoded = 0, rejected = 0; var i = 0; foreach (var input in Corruptions(valid, 120, seed: source.Sum(c => c))) { try { JpegDecoder.ReadInfo(input); if (source == "lossless") { var raw = JpegLosslessDecoder.Decode(input); Assert.Equal(raw.Width * raw.Height * raw.Components, raw.Samples.Length); } else { var image = JpegDecoder.Decode(input, options[i++ % 2]); Assert.True(image.Width > 0 && image.Height > 0); } decoded++; } catch (InvalidDataException) { rejected++; } } context.Log($"{source}: {decoded} decoded (possibly partial), {rejected} rejected"); } [Test] public void CorruptRestartIntervalIsResynchronised() { var data = Fixture("jpeg/r420.jpg"); var reference = ReadPnm(Fixture("jpeg/r420.ppm")).Samples; var rst0 = data.AsSpan().IndexOf([(byte)0xFF, (byte)0xD0]); var rst1 = data.AsSpan().IndexOf([(byte)0xFF, (byte)0xD1]); Assert.True(rst0 > 0 && rst1 > rst0, "fixture has restart markers"); var corrupt = (byte[])data.Clone(); var rng = new Random(5); for (var p = rst0 + 6; p < rst1 - 4; p += 3) { if (corrupt[p - 1] != 0xFF) corrupt[p] = (byte)rng.Next(0xFF); } // A bogus marker inside the damaged interval must be skipped, as libjpeg's resync does. corrupt[rst0 + 10] = 0xFF; corrupt[rst0 + 11] = 0x3A; var image = JpegDecoder.Decode(corrupt); context.Log(string.Join("; ", image.Warnings)); Assert.NotEmpty(image.Warnings); AssertRowsMatch(image.Pixels8!, reference, 0, 14); AssertRowsMatch(image.Pixels8!, reference, 34, 45); } [Test] public void MissingRestartMarkerSkipsTheLostInterval() { var data = Fixture("jpeg/r420.jpg"); var reference = ReadPnm(Fixture("jpeg/r420.ppm")).Samples; var rst0 = data.AsSpan().IndexOf([(byte)0xFF, (byte)0xD0]); var cut = data.AsSpan(0, rst0).ToArray().Concat(data.AsSpan(rst0 + 2).ToArray()).ToArray(); var image = JpegDecoder.Decode(cut); Assert.NotEmpty(image.Warnings); AssertRowsMatch(image.Pixels8!, reference, 0, 14); AssertRowsMatch(image.Pixels8!, reference, 34, 45); } [Theory] [Case("jpeg/p420.jpg")] [Case("jpeg/b444.jpg")] public void TruncatedScanDataYieldsPartialImage(string name) { var data = Fixture(name); var image = JpegDecoder.Decode(data.AsSpan(0, data.Length * 2 / 3)); Assert.Equal(61, image.Width); Assert.Equal(45, image.Height); Assert.NotEmpty(image.Warnings); } [Test] public void StructuralErrorsAreRejected() { Assert.Throws(() => JpegDecoder.Decode([])); Assert.Throws(() => JpegDecoder.Decode([0xFF, 0xD8, 0xFF, 0xD9])); Assert.Throws(() => JpegDecoder.ReadInfo([0x89, 0x50, 0x4E, 0x47])); var arithmetic = Fixture("jpeg/b420.jpg"); var sof = arithmetic.AsSpan().IndexOf([(byte)0xFF, (byte)0xC0]); arithmetic[sof + 1] = 0xC9; Assert.Throws(() => JpegDecoder.Decode(arithmetic)); var huge = Fixture("jpeg/b420.jpg"); huge[sof + 5] = huge[sof + 6] = huge[sof + 7] = huge[sof + 8] = 0xFF; Assert.Throws(() => JpegDecoder.Decode(huge)); } [Test] public void TablesStreamFeedsAbbreviatedImage() { // TIFF JPEGTables layout: tables in one SOI..EOI stream, the image without DQT/DHT. var full = Fixture("jpeg/b420.jpg"); var tables = new MemoryStream(); var image = new MemoryStream(); tables.Write([0xFF, 0xD8]); image.Write([0xFF, 0xD8]); var pos = 2; while (pos < full.Length) { var marker = full[pos + 1]; if (marker == 0xDA) { image.Write(full.AsSpan(pos)); break; } var length = full[pos + 2] << 8 | full[pos + 3]; (marker is 0xDB or 0xC4 ? tables : image).Write(full.AsSpan(pos, length + 2)); pos += length + 2; } tables.Write([0xFF, 0xD9]); Assert.Throws(() => JpegDecoder.Decode(image.ToArray())); var decoded = JpegDecoder.Decode(image.ToArray(), new JpegDecodeOptions { Tables = tables.ToArray() }); Assert.BytesEqual(JpegDecoder.Decode(full).Pixels8, decoded.Pixels8); } private static void AssertRowsMatch(byte[] actual, int[] reference, int fromRow, int toRow) { var rowBytes = 61 * 3; for (var i = fromRow * rowBytes; i < toRow * rowBytes; i++) { if (Math.Abs(actual[i] - reference[i]) > 2) Assert.Fail($"row {i / rowBytes} differs from the reference"); } } }