fix(codecs): harden JPEG restart resync and Huffman table checks

Port libjpeg's resync_to_restart decision rules (skip bogus markers and
stale RSTs, account for lost intervals), reject oversubscribed Huffman
tables before filling the lookahead table (found by fuzzing), and keep
triangle chroma upsampling at 1/8 scale for better thumbnails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 09:44:20 +00:00
1 parent 64e4fb3df6
commit a1d8287faa
4 files changed
+39 -17

No files matched your search

+32 -1
View File
@@ -31,11 +31,11 @@ internal sealed class JpegHuffmanTable
Delta[len] = k - code;
for (var i = 0; i < counts[len - 1]; i++, code++, k++)
{
if (code >= 1 << len) throw new InvalidDataException("JPEG: Huffman table oversubscribed");
if (len > FastBits) continue;
var first = code << (FastBits - len);
Fast.AsSpan(first, 1 << (FastBits - len)).Fill((ushort)(len << 8 | values[k]));
}
if (code > 1 << len) throw new InvalidDataException("JPEG: Huffman table oversubscribed");
MaxCode[len] = code << (16 - len);
code <<= 1;
}
@@ -238,4 +238,35 @@ internal ref struct JpegBitReader
_pos = _markerPos + 2;
_marker = 0;
}
/// <summary>
/// Restart processing with libjpeg's resync_to_restart rules: skips invalid markers and stale RSTs, accepts the
/// expected RSTn, and reports how many whole intervals were lost when RSTn+1/RSTn+2 shows up instead.
/// Returns false when a non-restart marker (or the end of data) ends the scan.
/// </summary>
public bool NextRestart(ref int expected, out int lostIntervals)
{
lostIntervals = 0;
while (true)
{
var marker = FindMarker();
if (marker < 0) return false;
if (marker < 0xC0)
{
AcceptMarker(); // corrupt data that happened to look like a marker
continue;
}
if (marker is < JpegTables.Rst0 or > JpegTables.Rst0 + 7) return false;
var delta = (marker - JpegTables.Rst0 - expected) & 7;
if (delta is 6 or 7)
{
AcceptMarker(); // a restart we already passed
continue;
}
if (delta is 1 or 2) lostIntervals = delta;
AcceptMarker();
expected = (marker - JpegTables.Rst0 + 1) & 7;
return true;
}
}
}
@@ -100,21 +100,17 @@ public static class JpegLosslessDecoder
}
if (mcu >= total || interval == 0) break;
var marker = reader.FindMarker();
if (marker is < JpegTables.Rst0 or > JpegTables.Rst0 + 7)
if (!reader.NextRestart(ref expectedRst, out var lost))
{
parser.Warn("JPEG: restart marker missing; scan truncated");
break;
}
var found = marker - JpegTables.Rst0;
if (found != expectedRst)
if (lost > 0)
{
var lost = (found - expectedRst) & 7;
// Intervals between the expected and the found marker were lost: leave their samples untouched.
parser.Warn($"JPEG: {lost} restart interval(s) lost");
mcu += (long)lost * interval;
}
reader.AcceptMarker();
expectedRst = (found + 1) & 7;
x = (int)(mcu % width);
y = (int)(mcu / width);
restartRow = y;
+1 -1
View File
@@ -31,7 +31,7 @@ internal sealed class JpegRenderer
_frame = parser.Frame!;
var options = parser.Options;
_n = BlockSize(_frame, options.MaxLongEdge);
_fancy = options.FancyUpsampling && _n > 1;
_fancy = options.FancyUpsampling;
_max = (1 << _frame.Precision) - 1;
_colorSpace = parser.ColorSpace();
_invertedCmyk = parser.AdobeTransform >= 0 || _colorSpace == JpegColorSpace.Ycck;
@@ -74,22 +74,17 @@ internal static class JpegScanDecoder
}
if (mcu >= total || interval == 0) break;
var marker = reader.FindMarker();
if (marker is < JpegTables.Rst0 or > JpegTables.Rst0 + 7)
if (!reader.NextRestart(ref expectedRst, out var lost))
{
parser.Warn("JPEG: restart marker missing; scan truncated");
break;
}
var found = marker - JpegTables.Rst0;
if (found != expectedRst)
if (lost > 0)
{
// Intervals between the expected and the found marker were lost: leave their blocks untouched.
var lost = (found - expectedRst) & 7;
// Intervals between the expected and the found marker were lost: leave their samples untouched.
parser.Warn($"JPEG: {lost} restart interval(s) lost");
mcu += (long)lost * interval;
}
reader.AcceptMarker();
expectedRst = (found + 1) & 7;
state.EobRun = 0;
foreach (var c in comps) c.DcPred = 0;
}