std/jpeg: allow DHT markers before SOF

This commit is contained in:
Nigel Tao committed 2024-08-24 16:13:31 +10:00
1 parent 60e3b8cb1c
commit a824c55c3d
6 files changed
+122 -23

No files matched your search

+24 -10
View File
@@ -49085,6 +49085,14 @@ wuffs_jpeg__decoder__do_decode_image_config(
} else if (self->private_impl.f_sof_marker != 0u) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_sof_marker);
goto exit;
} else if (v_marker == 192u) {
if (self->private_impl.f_seen_dht[2u] ||
self->private_impl.f_seen_dht[3u] ||
self->private_impl.f_seen_dht[6u] ||
self->private_impl.f_seen_dht[7u]) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_sof_marker);
goto exit;
}
}
self->private_impl.f_sof_marker = v_marker;
if (a_src) {
@@ -49103,8 +49111,18 @@ wuffs_jpeg__decoder__do_decode_image_config(
status = wuffs_base__make_status(wuffs_jpeg__error__unsupported_lossless_coding);
goto exit;
} else if (v_marker == 196u) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_dht_marker);
goto exit;
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(8);
status = wuffs_jpeg__decoder__decode_dht(self, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
}
if (status.repr) {
goto suspend;
}
continue;
} else if ((197u <= v_marker) && (v_marker <= 199u)) {
status = wuffs_base__make_status(wuffs_jpeg__error__unsupported_hierarchical_coding);
goto exit;
@@ -49126,7 +49144,7 @@ wuffs_jpeg__decoder__do_decode_image_config(
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(8);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(9);
status = wuffs_jpeg__decoder__decode_dqt(self, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
@@ -49139,7 +49157,7 @@ wuffs_jpeg__decoder__do_decode_image_config(
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(9);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(10);
status = wuffs_jpeg__decoder__decode_dri(self, a_src);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
@@ -49156,7 +49174,7 @@ wuffs_jpeg__decoder__do_decode_image_config(
if (a_src) {
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
}
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(10);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(11);
status = wuffs_jpeg__decoder__decode_appn(self, a_src, v_marker);
if (a_src) {
iop_a_src = a_src->data.ptr + a_src->meta.ri;
@@ -49173,7 +49191,7 @@ wuffs_jpeg__decoder__do_decode_image_config(
}
}
self->private_data.s_do_decode_image_config.scratch = self->private_impl.f_payload_length;
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(11);
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(12);
if (self->private_data.s_do_decode_image_config.scratch > ((uint64_t)(io2_a_src - iop_a_src))) {
self->private_data.s_do_decode_image_config.scratch -= ((uint64_t)(io2_a_src - iop_a_src));
iop_a_src = io2_a_src;
@@ -50521,10 +50539,6 @@ wuffs_jpeg__decoder__decode_dht(
switch (coro_susp_point) {
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
if (self->private_impl.f_sof_marker == 0u) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_dht_marker);
goto exit;
}
while (self->private_impl.f_payload_length > 0u) {
if (self->private_impl.f_payload_length < 17u) {
status = wuffs_base__make_status(wuffs_jpeg__error__bad_dht_marker);
+9 -8
View File
@@ -40,11 +40,11 @@ func main1() error {
pos := 0
if len(src) < 2 {
return posError(pos)
return posError(pos, len(src))
}
for pos <= (len(src) - 2) {
if src[pos] != 0xFF {
return posError(pos)
return posError(pos, len(src))
}
marker := src[pos+1]
fmt.Printf("pos = 0x%08X = %10d marker = 0xFF 0x%02X %s\n", pos, pos, marker, names[marker])
@@ -59,12 +59,12 @@ func main1() error {
}
if pos >= (len(src) - 2) {
return posError(pos)
return posError(pos, len(src))
}
payloadLength := (int(src[pos]) << 8) | int(src[pos+1])
pos += payloadLength
if (payloadLength < 2) || (pos < 0) || (pos > len(src)) {
return posError(pos)
return posError(pos, len(src))
}
if marker != 0xDA { // SOS (Start Of Scan) marker.
@@ -74,7 +74,7 @@ func main1() error {
if src[pos] != 0xFF {
pos += 1
} else if pos >= (len(src) - 1) {
return posError(pos)
return posError(pos, len(src))
} else if m := src[pos+1]; (m != 0x00) && ((m < 0xD0) || (0xD7 < m)) {
break
} else {
@@ -82,11 +82,12 @@ func main1() error {
}
}
}
return posError(pos)
return posError(pos, len(src))
}
func posError(pos int) error {
return fmt.Errorf("invalid JPEG at pos = 0x%08X = %10d", pos, pos)
func posError(pos int, lenSrc int) error {
return fmt.Errorf("bad JPEG, pos = 0x%08X = %10d, len(src) is 0x%08X = %10d",
pos, pos, lenSrc, lenSrc)
}
var names = [256]string{
+8 -5
View File
@@ -441,6 +441,12 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
return "#rejected progressive JPEG"
} else if this.sof_marker <> 0 {
return "#bad SOF marker"
} else if (marker == 0xC0) {
// SOF0 (which is 0xC0) means a baseline JPEG. The baseline
// (th <= 1) restriction is specified in table B.5.
if this.seen_dht[2] or this.seen_dht[3] or this.seen_dht[6] or this.seen_dht[7] {
return "#bad SOF marker"
}
}
this.sof_marker = marker
this.decode_sof?(src: args.src)
@@ -450,8 +456,8 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
return "#unsupported lossless coding"
} else if marker == 0xC4 { // DHT (Define Huffman Table).
// We shouldn't see DHT before SOF.
return "#bad DHT marker"
this.decode_dht?(src: args.src)
continue
} else if (0xC5 <= marker) and (marker <= 0xC7) {
return "#unsupported hierarchical coding"
@@ -1104,9 +1110,6 @@ pri func decoder.decode_dht?(src: base.io_reader) {
var i : base.u32
var failed : base.bool
if this.sof_marker == 0 {
return "#bad DHT marker"
}
while this.payload_length > 0 {
if this.payload_length < 17 {
return "#bad DHT marker"
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

@@ -0,0 +1,80 @@
// Copyright 2024 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
//go:build ignore
// +build ignore
package main
// Usage: go run make-hippopotamus-sof-dht-swap.go
//
// The test/data/hippopotamus.jpeg file has one SOF0 (Start Of Frame type 0)
// and then four DHT (Define Huffman Table) markers. This program swaps the
// order of the SOF0 and first DHT fragments.
//
// $ go run script/print-jpeg-markers.go test/data/hippopotamus.jpeg
// pos = 0x00000000 = 0 marker = 0xFF 0xD8 SOI
// pos = 0x00000002 = 2 marker = 0xFF 0xE0 APP0
// pos = 0x00000014 = 20 marker = 0xFF 0xDB DQT
// pos = 0x00000059 = 89 marker = 0xFF 0xDB DQT
// pos = 0x0000009E = 158 marker = 0xFF 0xC0 SOF0 (Sequential/Baseline)
// pos = 0x000000B1 = 177 marker = 0xFF 0xC4 DHT
// pos = 0x000000CC = 204 marker = 0xFF 0xC4 DHT
// pos = 0x000000FC = 252 marker = 0xFF 0xC4 DHT
// pos = 0x00000119 = 281 marker = 0xFF 0xC4 DHT
// pos = 0x0000014B = 331 marker = 0xFF 0xDA SOS
// pos = 0x000004C1 = 1217 marker = 0xFF 0xD9 EOI
//
// $ go run script/print-jpeg-markers.go test/data/artificial-jpeg/hippopotamus-sof-dht-swap.jpeg
// pos = 0x00000000 = 0 marker = 0xFF 0xD8 SOI
// pos = 0x00000002 = 2 marker = 0xFF 0xE0 APP0
// pos = 0x00000014 = 20 marker = 0xFF 0xDB DQT
// pos = 0x00000059 = 89 marker = 0xFF 0xDB DQT
// pos = 0x0000009E = 158 marker = 0xFF 0xC4 DHT
// pos = 0x000000B9 = 185 marker = 0xFF 0xC0 SOF0 (Sequential/Baseline)
// pos = 0x000000CC = 204 marker = 0xFF 0xC4 DHT
// pos = 0x000000FC = 252 marker = 0xFF 0xC4 DHT
// pos = 0x00000119 = 281 marker = 0xFF 0xC4 DHT
// pos = 0x0000014B = 331 marker = 0xFF 0xDA SOS
// pos = 0x000004C1 = 1217 marker = 0xFF 0xD9 EOI
import (
"fmt"
"os"
)
func main() {
if err := main1(); err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
}
func main1() error {
src, err := os.ReadFile("../hippopotamus.jpeg")
if err != nil {
return err
} else if len(src) != 0x4C3 {
return fmt.Errorf("bad input length: 0x%X", len(src))
}
part0 := src[0x000:0x09E]
part1 := src[0x09E:0x0B1] // SOF0
part2 := src[0x0B1:0x0CC] // DHT
part3 := src[0x0CC:0x4C3]
dst := []byte(nil)
dst = append(dst, part0...)
dst = append(dst, part2...) // Note the re-order: it's not part1.
dst = append(dst, part1...) // Note the re-order: it's not part2.
dst = append(dst, part3...)
return os.WriteFile("hippopotamus-sof-dht-swap.jpeg", dst, 0666)
}
+1
View File
@@ -21,6 +21,7 @@ BAD 2342e3fc test/data/artificial-gif/pixel-data-too-much-good-lzw.gif
OK. 39a71dce test/data/artificial-gif/small-frame-interlaced.gif
OK. beaec397 test/data/artificial-gif/transparent-index.gif
BAD f33b9bc1 test/data/artificial-gif/zero-width-frame.gif
OK. 96bdbbb3 test/data/artificial-jpeg/hippopotamus-sof-dht-swap.jpeg
OK. 0564b364 test/data/artificial-png/apng-skip-idat.png
OK. e08a7cc8 test/data/artificial-png/exif.png
OK. e08a7cc8 test/data/artificial-png/key-value-pairs.png