std/brotli: add QUIRK_REPORT_METADATA

The decoder can now report metadata blocks, like libbrotlidec's
BrotliDecoderSetMetadataCallbacks: transform_io returns
"@metadata reported", metadata_length gives the block's length and the
block's contents are written to dst. The differential fuzzer compares
the reported metadata with libbrotlidec's callbacks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UZ978Fjdsyig4v3wYAZRg5
This commit is contained in:
Claude committed 2026-10-27 17:06:08 +00:00
1 parent 5d6de9880f
commit bc1b5fcd49
5 files changed
+774 -27

No files matched your search

+70 -10
View File
@@ -15,12 +15,12 @@
// particular its c/dec/decode.c file. Comments like "Mirrors Foo in decode.c"
// name the C function whose logic (and validation checks) a Wuffs function
// mirrors. The C code's explicit BROTLI_STATE_ETC state machine is replaced by
// Wuffs coroutines. The C code's "safe" and "unsafe" (fast path) variants are
// implemented once, as the "safe" (read as few bits as possible) variant.
// Wuffs coroutines. The C code's "safe" variants (read as few bits as
// possible, so that decoding can suspend anywhere) are coroutines and its
// "unsafe" fast path (ProcessCommands) is process_commands_fast, which reads
// ahead and gives back the unused bytes (see README.md).
//
// See README.md for how C error codes map to Wuffs statuses.
//
// TODO: support metadata callbacks and fast paths.
pub status "#bad block length"
pub status "#bad block switch"
@@ -245,6 +245,17 @@ pub struct decoder? implements base.io_transformer(
quirk_large_window : base.bool,
large_window : base.bool,
// quirk_report_metadata is the QUIRK_REPORT_METADATA quirk.
// md_length is the length of the last reported metadata block.
// md_note_pending is whether do_transform_io has suspended (with
// "$short write") so that transform_io returns "@metadata reported".
// md_copying is whether do_transform_io is writing a metadata block's
// contents to args.dst.
quirk_report_metadata : base.bool,
md_length : base.u32[..= 0x100_0000],
md_note_pending : base.bool,
md_copying : base.bool,
// started is whether the C code's s->state would no longer be
// BROTLI_STATE_UNINITED. Quirks and dictionaries can only be set or
// added before then.
@@ -355,6 +366,8 @@ pub struct decoder? implements base.io_transformer(
pub func decoder.get_quirk(key: base.u32) base.u64 {
if (args.key == QUIRK_LARGE_WINDOW) and this.quirk_large_window {
return 1
} else if (args.key == QUIRK_REPORT_METADATA) and this.quirk_report_metadata {
return 1
}
return 0
}
@@ -368,10 +381,22 @@ pub func decoder.set_quirk!(key: base.u32, value: base.u64) base.status {
}
this.quirk_large_window = args.value > 0
return ok
} else if args.key == QUIRK_REPORT_METADATA {
if this.started {
return base."#bad call sequence"
}
this.quirk_report_metadata = args.value > 0
return ok
}
return base."#unsupported option"
}
// metadata_length returns the length of the metadata block that was last
// reported (see QUIRK_REPORT_METADATA).
pub func decoder.metadata_length() base.u64 {
return this.md_length as base.u64
}
pub func decoder.dst_history_retain_length() base.optional_u63 {
return this.util.make_optional_u63(has_value: true, value: 0)
}
@@ -409,7 +434,7 @@ pub func decoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf
if this.dict_total > args.workbuf.length() {
return base."#bad workbuf length"
}
status =? this.do_transform_io?(src: args.src,
status =? this.do_transform_io?(dst: args.dst, src: args.src,
workbuf: args.workbuf[this.dict_total ..],
dictbuf: args.workbuf[.. this.dict_total])
if this.dict_total > args.workbuf.length() {
@@ -428,7 +453,10 @@ pub func decoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf
return ok
} else if status == base."$short write" {
if this.is_fully_flushed() {
if this.md_note_pending {
this.md_note_pending = false
return base."@metadata reported"
} else if this.is_fully_flushed() and (not this.md_copying) {
continue
}
@@ -471,7 +499,7 @@ pri func decoder.flush!(dst: base.io_writer, workbuf: slice base.u8) {
// Mirrors the BROTLI_STATE_ETC state machine in BrotliDecoderDecompressStream
// in decode.c.
pri func decoder.do_transform_io?(src: base.io_reader, workbuf: slice base.u8, dictbuf: roslice base.u8) {
pri func decoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8, dictbuf: roslice base.u8) {
var w : base.u64
var aligned : base.bool
@@ -508,9 +536,11 @@ pri func decoder.do_transform_io?(src: base.io_reader, workbuf: slice base.u8, d
if this.is_metadata {
// BROTLI_STATE_METADATA. Mirrors SkipMetadataBlock.
//
// TODO: support metadata callbacks.
args.src.skip?(n: this.mb_remaining as base.u64)
if this.quirk_report_metadata {
this.report_metadata?(dst: args.dst, src: args.src)
} else {
args.src.skip?(n: this.mb_remaining as base.u64)
}
this.mb_remaining = 0
} else if this.mb_remaining > 0 {
@@ -538,6 +568,36 @@ pri func decoder.do_transform_io?(src: base.io_reader, workbuf: slice base.u8, d
}
}
// report_metadata mirrors the C code's metadata callbacks (the start callback
// in BROTLI_STATE_METABLOCK_HEADER and the chunk callbacks in
// SkipMetadataBlock). It waits until every decoded byte has been written to
// args.dst, then has transform_io return "@metadata reported" and then copies
// the metadata block's contents from args.src to args.dst.
pri func decoder.report_metadata?(dst: base.io_writer, src: base.io_reader) {
var n : base.u32
while not this.is_fully_flushed() {
yield? base."$short write"
}
this.md_length = this.mb_remaining.min(no_more_than: 0x100_0000)
this.md_note_pending = true
yield? base."$short write"
this.md_copying = true
while this.mb_remaining > 0 {
if args.dst.length() <= 0 {
yield? base."$short write"
continue
} else if args.src.length() <= 0 {
yield? base."$short read"
continue
}
n = args.dst.limited_copy_u32_from_reader!(up_to: this.mb_remaining as base.u32, r: args.src)
this.mb_remaining ~mod-= n
}
this.md_copying = false
}
// initialize_codes sets up the decoder state that the C code sets in
// BrotliDecoderStateInit (in state.c). It also builds the fixed table for
// decoding the code length code lengths (kCodeLengthPrefixLength and
+16
View File
@@ -30,3 +30,19 @@ pri const QUIRKS_BASE : base.u32 = 0x2AB7_9000
// is rejected with "#unsupported large window". With this quirk, a stream that
// does not start with that escape is decoded as standard brotli.
pub const QUIRK_LARGE_WINDOW : base.u32 = 0x2AB7_9000 | 0x00
// When this quirk is set, a positive value makes the decoder report metadata
// blocks, like the C code's BrotliDecoderSetMetadataCallbacks. Zero (the
// default) means that metadata blocks are skipped, like the C code without
// callbacks.
//
// When reporting, at each metadata block, after all of the previously decoded
// bytes have been written to dst, transform_io returns
// base."@metadata reported" and metadata_length returns the block's length
// (the C code's start callback's size argument), which can be zero. The next
// metadata_length bytes that transform_io writes to dst (in subsequent calls)
// are the metadata block's contents (the C code's chunk callbacks' data),
// after which decoded bytes follow as usual.
//
// It must be set before decoding starts. This quirk is decoder-only.
pub const QUIRK_REPORT_METADATA : base.u32 = 0x2AB7_9000 | 0x0A