mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-25 08:55:11 +01:00
std/jpeg: allow DHT markers before SOF
This commit is contained in:
1 parent
60e3b8cb1c
commit
a824c55c3d
6 files changed
+122
-23
No files matched your search
@@ -49085,6 +49085,14 @@ wuffs_jpeg__decoder__do_decode_image_config(
|
|||||||
} else if (self->private_impl.f_sof_marker != 0u) {
|
} else if (self->private_impl.f_sof_marker != 0u) {
|
||||||
status = wuffs_base__make_status(wuffs_jpeg__error__bad_sof_marker);
|
status = wuffs_base__make_status(wuffs_jpeg__error__bad_sof_marker);
|
||||||
goto exit;
|
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;
|
self->private_impl.f_sof_marker = v_marker;
|
||||||
if (a_src) {
|
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);
|
status = wuffs_base__make_status(wuffs_jpeg__error__unsupported_lossless_coding);
|
||||||
goto exit;
|
goto exit;
|
||||||
} else if (v_marker == 196u) {
|
} else if (v_marker == 196u) {
|
||||||
status = wuffs_base__make_status(wuffs_jpeg__error__bad_dht_marker);
|
if (a_src) {
|
||||||
goto exit;
|
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)) {
|
} else if ((197u <= v_marker) && (v_marker <= 199u)) {
|
||||||
status = wuffs_base__make_status(wuffs_jpeg__error__unsupported_hierarchical_coding);
|
status = wuffs_base__make_status(wuffs_jpeg__error__unsupported_hierarchical_coding);
|
||||||
goto exit;
|
goto exit;
|
||||||
@@ -49126,7 +49144,7 @@ wuffs_jpeg__decoder__do_decode_image_config(
|
|||||||
if (a_src) {
|
if (a_src) {
|
||||||
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
|
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);
|
status = wuffs_jpeg__decoder__decode_dqt(self, a_src);
|
||||||
if (a_src) {
|
if (a_src) {
|
||||||
iop_a_src = a_src->data.ptr + a_src->meta.ri;
|
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) {
|
if (a_src) {
|
||||||
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
|
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);
|
status = wuffs_jpeg__decoder__decode_dri(self, a_src);
|
||||||
if (a_src) {
|
if (a_src) {
|
||||||
iop_a_src = a_src->data.ptr + a_src->meta.ri;
|
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) {
|
if (a_src) {
|
||||||
a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr));
|
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);
|
status = wuffs_jpeg__decoder__decode_appn(self, a_src, v_marker);
|
||||||
if (a_src) {
|
if (a_src) {
|
||||||
iop_a_src = a_src->data.ptr + a_src->meta.ri;
|
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;
|
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))) {
|
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));
|
self->private_data.s_do_decode_image_config.scratch -= ((uint64_t)(io2_a_src - iop_a_src));
|
||||||
iop_a_src = io2_a_src;
|
iop_a_src = io2_a_src;
|
||||||
@@ -50521,10 +50539,6 @@ wuffs_jpeg__decoder__decode_dht(
|
|||||||
switch (coro_susp_point) {
|
switch (coro_susp_point) {
|
||||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
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) {
|
while (self->private_impl.f_payload_length > 0u) {
|
||||||
if (self->private_impl.f_payload_length < 17u) {
|
if (self->private_impl.f_payload_length < 17u) {
|
||||||
status = wuffs_base__make_status(wuffs_jpeg__error__bad_dht_marker);
|
status = wuffs_base__make_status(wuffs_jpeg__error__bad_dht_marker);
|
||||||
|
|||||||
@@ -40,11 +40,11 @@ func main1() error {
|
|||||||
|
|
||||||
pos := 0
|
pos := 0
|
||||||
if len(src) < 2 {
|
if len(src) < 2 {
|
||||||
return posError(pos)
|
return posError(pos, len(src))
|
||||||
}
|
}
|
||||||
for pos <= (len(src) - 2) {
|
for pos <= (len(src) - 2) {
|
||||||
if src[pos] != 0xFF {
|
if src[pos] != 0xFF {
|
||||||
return posError(pos)
|
return posError(pos, len(src))
|
||||||
}
|
}
|
||||||
marker := src[pos+1]
|
marker := src[pos+1]
|
||||||
fmt.Printf("pos = 0x%08X = %10d marker = 0xFF 0x%02X %s\n", pos, pos, marker, names[marker])
|
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) {
|
if pos >= (len(src) - 2) {
|
||||||
return posError(pos)
|
return posError(pos, len(src))
|
||||||
}
|
}
|
||||||
payloadLength := (int(src[pos]) << 8) | int(src[pos+1])
|
payloadLength := (int(src[pos]) << 8) | int(src[pos+1])
|
||||||
pos += payloadLength
|
pos += payloadLength
|
||||||
if (payloadLength < 2) || (pos < 0) || (pos > len(src)) {
|
if (payloadLength < 2) || (pos < 0) || (pos > len(src)) {
|
||||||
return posError(pos)
|
return posError(pos, len(src))
|
||||||
}
|
}
|
||||||
|
|
||||||
if marker != 0xDA { // SOS (Start Of Scan) marker.
|
if marker != 0xDA { // SOS (Start Of Scan) marker.
|
||||||
@@ -74,7 +74,7 @@ func main1() error {
|
|||||||
if src[pos] != 0xFF {
|
if src[pos] != 0xFF {
|
||||||
pos += 1
|
pos += 1
|
||||||
} else if pos >= (len(src) - 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)) {
|
} else if m := src[pos+1]; (m != 0x00) && ((m < 0xD0) || (0xD7 < m)) {
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
@@ -82,11 +82,12 @@ func main1() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return posError(pos)
|
return posError(pos, len(src))
|
||||||
}
|
}
|
||||||
|
|
||||||
func posError(pos int) error {
|
func posError(pos int, lenSrc int) error {
|
||||||
return fmt.Errorf("invalid JPEG at pos = 0x%08X = %10d", pos, pos)
|
return fmt.Errorf("bad JPEG, pos = 0x%08X = %10d, len(src) is 0x%08X = %10d",
|
||||||
|
pos, pos, lenSrc, lenSrc)
|
||||||
}
|
}
|
||||||
|
|
||||||
var names = [256]string{
|
var names = [256]string{
|
||||||
|
|||||||
@@ -441,6 +441,12 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
|
|||||||
return "#rejected progressive JPEG"
|
return "#rejected progressive JPEG"
|
||||||
} else if this.sof_marker <> 0 {
|
} else if this.sof_marker <> 0 {
|
||||||
return "#bad SOF marker"
|
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.sof_marker = marker
|
||||||
this.decode_sof?(src: args.src)
|
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"
|
return "#unsupported lossless coding"
|
||||||
|
|
||||||
} else if marker == 0xC4 { // DHT (Define Huffman Table).
|
} else if marker == 0xC4 { // DHT (Define Huffman Table).
|
||||||
// We shouldn't see DHT before SOF.
|
this.decode_dht?(src: args.src)
|
||||||
return "#bad DHT marker"
|
continue
|
||||||
|
|
||||||
} else if (0xC5 <= marker) and (marker <= 0xC7) {
|
} else if (0xC5 <= marker) and (marker <= 0xC7) {
|
||||||
return "#unsupported hierarchical coding"
|
return "#unsupported hierarchical coding"
|
||||||
@@ -1104,9 +1110,6 @@ pri func decoder.decode_dht?(src: base.io_reader) {
|
|||||||
var i : base.u32
|
var i : base.u32
|
||||||
var failed : base.bool
|
var failed : base.bool
|
||||||
|
|
||||||
if this.sof_marker == 0 {
|
|
||||||
return "#bad DHT marker"
|
|
||||||
}
|
|
||||||
while this.payload_length > 0 {
|
while this.payload_length > 0 {
|
||||||
if this.payload_length < 17 {
|
if this.payload_length < 17 {
|
||||||
return "#bad DHT marker"
|
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)
|
||||||
|
}
|
||||||
@@ -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. 39a71dce test/data/artificial-gif/small-frame-interlaced.gif
|
||||||
OK. beaec397 test/data/artificial-gif/transparent-index.gif
|
OK. beaec397 test/data/artificial-gif/transparent-index.gif
|
||||||
BAD f33b9bc1 test/data/artificial-gif/zero-width-frame.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. 0564b364 test/data/artificial-png/apng-skip-idat.png
|
||||||
OK. e08a7cc8 test/data/artificial-png/exif.png
|
OK. e08a7cc8 test/data/artificial-png/exif.png
|
||||||
OK. e08a7cc8 test/data/artificial-png/key-value-pairs.png
|
OK. e08a7cc8 test/data/artificial-png/key-value-pairs.png
|
||||||
|
|||||||
Reference in new issue
Block a user