From 819742cfac8090693e3b4562011891b6ff1792b9 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 24 Aug 2024 17:01:28 +1000 Subject: [PATCH] std/jpeg: add "#short SOS bitstream" --- release/c/wuffs-unsupported-snapshot.c | 4 +++- script/confirm-matches-djpeg.sh | 12 ++++++++++-- std/jpeg/decode_jpeg.wuffs | 3 ++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 0f669ad9..91cb81e6 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -10034,6 +10034,7 @@ extern const char wuffs_jpeg__error__bad_scan_count[]; extern const char wuffs_jpeg__error__missing_huffman_table[]; extern const char wuffs_jpeg__error__missing_quantization_table[]; extern const char wuffs_jpeg__error__rejected_progressive_jpeg[]; +extern const char wuffs_jpeg__error__short_sos_bitstream[]; extern const char wuffs_jpeg__error__truncated_input[]; extern const char wuffs_jpeg__error__unsupported_arithmetic_coding[]; extern const char wuffs_jpeg__error__unsupported_color_model[]; @@ -46681,6 +46682,7 @@ const char wuffs_jpeg__error__bad_scan_count[] = "#jpeg: bad scan count"; const char wuffs_jpeg__error__missing_huffman_table[] = "#jpeg: missing Huffman table"; const char wuffs_jpeg__error__missing_quantization_table[] = "#jpeg: missing Quantization table"; const char wuffs_jpeg__error__rejected_progressive_jpeg[] = "#jpeg: rejected progressive JPEG"; +const char wuffs_jpeg__error__short_sos_bitstream[] = "#jpeg: short SOS bitstream"; const char wuffs_jpeg__error__truncated_input[] = "#jpeg: truncated input"; const char wuffs_jpeg__error__unsupported_arithmetic_coding[] = "#jpeg: unsupported arithmetic coding"; const char wuffs_jpeg__error__unsupported_color_model[] = "#jpeg: unsupported color model"; @@ -50862,7 +50864,7 @@ wuffs_jpeg__decoder__decode_sos( if (v_bitstream_length < ((uint32_t)(self->private_impl.f_bitstream_wi - self->private_impl.f_bitstream_ri))) { break; } else if (self->private_impl.f_bitstream_padding == 0u) { - status = wuffs_base__make_status(wuffs_jpeg__error__bad_sos_marker); + status = wuffs_base__make_status(wuffs_jpeg__error__short_sos_bitstream); goto exit; } else if ((a_src && a_src->meta.closed) && ! self->private_impl.f_bitstream_is_closed) { if (self->private_impl.f_bitstream_wi < 1024u) { diff --git a/script/confirm-matches-djpeg.sh b/script/confirm-matches-djpeg.sh index 70004f75..0401cfcb 100755 --- a/script/confirm-matches-djpeg.sh +++ b/script/confirm-matches-djpeg.sh @@ -72,10 +72,18 @@ handle() { # Image file format specifications cover how to correctly decode complete # input but usually do not mandate exactly how to decode truncated input. - # Print "trunc" vs "other" to distinguish these cases. + # + # A short SOS (Start Of Scan) bitstream is similar. Strictly speaking, such + # JPEG images are invalid. In practice, decoders have some discretion in how + # many implicit zero bits to produce before giving up. Giving up effectively + # truncates the input, even if we haven't reached the actual end of file. + # + # Print "trunc" vs "sSOSb" vs "other" to distinguish these cases. local error=$(gen/bin/example-convert-to-nia -output-netpbm <$1 2>&1 >/dev/null) - if [[ $error =~ :\ truncated.input$ ]]; then + if [[ $error =~ :\ truncated\ input$ ]]; then echo "Differ (trunc) $1" + elif [[ $error =~ :\ short\ SOS\ bitstream$ ]]; then + echo "Differ (sSOSb) $1" else echo "Differ (other) $1" result=1 diff --git a/std/jpeg/decode_jpeg.wuffs b/std/jpeg/decode_jpeg.wuffs index bf5c27fb..872fa298 100644 --- a/std/jpeg/decode_jpeg.wuffs +++ b/std/jpeg/decode_jpeg.wuffs @@ -19,6 +19,7 @@ pub status "#bad scan count" pub status "#missing Huffman table" pub status "#missing Quantization table" pub status "#rejected progressive JPEG" +pub status "#short SOS bitstream" pub status "#truncated input" pub status "#unsupported arithmetic coding" pub status "#unsupported color model" @@ -1370,7 +1371,7 @@ pri func decoder.decode_sos?(dst: ptr base.pixel_buffer, src: base.io_reader, wo if bitstream_length < (this.bitstream_wi ~mod- this.bitstream_ri) { break.fill_bitstream } else if this.bitstream_padding == 0 { - return "#bad SOS marker" + return "#short SOS bitstream" } else if args.src.is_closed() and (not this.bitstream_is_closed) { if this.bitstream_wi < (0x800 / 2) { assert this.bitstream_wi <= (this.bitstream_wi + 264) via "a <= (a + b): 0 <= b"()