// Copyright 2026 The Wuffs Authors. // // Licensed under the Apache License, Version 2.0 or the MIT license // , 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 // -------- // Mirrors the BROTLI_STATE_HUFFMAN_CODE_0 through // BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_BODY states of // BrotliDecoderDecompressStream in decode.c, followed by ProcessCommands. pri func decoder.decode_compressed_metablock?(src: base.io_reader, workbuf: slice base.u8) { var t : base.u32 var n : base.u32[..= 256] var n0 : base.u32[..= 256] var n2 : base.u32[..= 256] var bits : base.u32 var npostfix : base.u32[..= 3] var ndirect : base.u32[..= 120] var alphabet : base.u32[..= 520] var i : base.u32 var k : base.u32 // BROTLI_STATE_HUFFMAN_CODE_0 ..= BROTLI_STATE_HUFFMAN_CODE_3. t = 0 while t < 3 { this.decode_var_len_uint8?(src: args.src) n = (this.bits_value & 0xFF) + 1 this.num_block_types[t] = n if n >= 2 { this.read_huffman_code?(src: args.src, alphabet_size_max: n + 2, alphabet_size_limit: n + 2, hoff: BLOCK_TYPE_HUFFS_BASE + (t * BLOCK_TYPE_HUFFS_STRIDE), table_size_max: BLOCK_TYPE_HUFFS_STRIDE) this.read_huffman_code?(src: args.src, alphabet_size_max: 26, alphabet_size_limit: 26, hoff: BLOCK_LEN_HUFFS_BASE + (t * BLOCK_LEN_HUFFS_STRIDE), table_size_max: BLOCK_LEN_HUFFS_STRIDE) this.read_block_length?(src: args.src, t: t) this.block_length[t] = this.bits_value } t += 1 } // BROTLI_STATE_METABLOCK_HEADER_2. this.read_bits?(src: args.src, n: 6) bits = this.bits_value npostfix = bits & 3 ndirect = ((bits >> 2) & 15) << npostfix this.distance_postfix_bits = npostfix this.num_direct_distance_codes = ndirect // BROTLI_STATE_CONTEXT_MODES. Mirrors ReadContextModes. n0 = this.num_block_types[0] i = 0 while i < n0 { assert i < 256 via "a < b: a < c; c <= b"(c: n0) this.read_bits?(src: args.src, n: 2) this.context_modes[i] = (this.bits_value & 3) as base.u8 i += 1 } // BROTLI_STATE_CONTEXT_MAP_1. this.decode_context_map?(src: args.src, size: n0 << 6, offset: 0) this.num_literal_htrees = this.bits_value.min(no_more_than: 256) // BROTLI_STATE_CONTEXT_MAP_2. n2 = this.num_block_types[2] this.decode_context_map?(src: args.src, size: n2 << 2, offset: DIST_CONTEXT_MAP_BASE) this.num_dist_htrees = this.bits_value.min(no_more_than: 256) // BROTLI_DISTANCE_ALPHABET_SIZE(npostfix, ndirect, BROTLI_MAX_DISTANCE_BITS). alphabet = 16 + ndirect + ((24 as base.u32) << (npostfix + 1)) this.distance_alphabet_size = alphabet // BROTLI_STATE_TREE_GROUP. Mirrors HuffmanTreeGroupDecode, three times. k = 0 while k < this.num_literal_htrees { assert k < 256 via "a < b: a < c; c <= b"(c: this.num_literal_htrees) this.read_huffman_code?(src: args.src, alphabet_size_max: 256, alphabet_size_limit: 256, hoff: LIT_HUFFS_BASE + (k * LIT_HUFFS_STRIDE), table_size_max: LIT_HUFFS_STRIDE) k += 1 } k = 0 while k < this.num_block_types[1] { assert k < 256 via "a < b: a < c; c <= b"(c: this.num_block_types[1]) this.read_huffman_code?(src: args.src, alphabet_size_max: 704, alphabet_size_limit: 704, hoff: CMD_HUFFS_BASE + (k * CMD_HUFFS_STRIDE), table_size_max: CMD_HUFFS_STRIDE) k += 1 } k = 0 while k < this.num_dist_htrees { assert k < 256 via "a < b: a < c; c <= b"(c: this.num_dist_htrees) this.read_huffman_code?(src: args.src, alphabet_size_max: alphabet, alphabet_size_limit: alphabet, hoff: DIST_HUFFS_BASE + (k * DIST_HUFFS_STRIDE), table_size_max: DIST_HUFFS_STRIDE) k += 1 } // BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_BODY. this.prepare_literal_decoding!() this.dist_context_map_slice = 0 this.htree_command = 0 this.calculate_distance_lut!() this.process_commands?(src: args.src, workbuf: args.workbuf) } // Mirrors DecodeContextMap in decode.c. The context map is written to // this.context_maps[args.offset .. args.offset + args.size] and the number // of Huffman trees (in [1 ..= 256]) is stored in this.bits_value. pri func decoder.decode_context_map?(src: base.io_reader, size: base.u32[..= 16384], offset: base.u32[..= 16384]) { var size : base.u32[..= 16384] var offset : base.u32[..= 16384] var num_htrees : base.u32[..= 256] var max_rle : base.u32[..= 16] var alphabet : base.u32[..= 272] var ci : base.u32 var code : base.u32 var reps : base.u32 var i : base.u32 size = args.size offset = args.offset this.decode_var_len_uint8?(src: args.src) num_htrees = (this.bits_value & 0xFF) + 1 if num_htrees <= 1 { ci = 0 while ci < size, inv num_htrees <= 1, { i = offset ~mod+ ci if i >= 17408 { return "#internal error: inconsistent ring buffer" } this.context_maps[i] = 0 ci ~mod+= 1 } this.bits_value = num_htrees return ok } // BROTLI_STATE_CONTEXT_MAP_READ_PREFIX. max_rle = 0 this.read_bits?(src: args.src, n: 1) if this.bits_value <> 0 { this.read_bits?(src: args.src, n: 4) max_rle = (this.bits_value & 15) + 1 } // BROTLI_STATE_CONTEXT_MAP_HUFFMAN. alphabet = num_htrees + max_rle this.read_huffman_code?(src: args.src, alphabet_size_max: alphabet, alphabet_size_limit: alphabet, hoff: CONTEXT_MAP_HUFFS_BASE, table_size_max: 646) // BROTLI_STATE_CONTEXT_MAP_DECODE. ci = 0 while ci < size { this.read_symbol?(src: args.src, hoff: CONTEXT_MAP_HUFFS_BASE) code = this.symbol if (code == 0) or (code > max_rle) { i = offset ~mod+ ci if i >= 17408 { return "#internal error: inconsistent ring buffer" } if code == 0 { this.context_maps[i] = 0 } else { this.context_maps[i] = ((code ~mod- max_rle) & 0xFF) as base.u8 } ci ~mod+= 1 continue } // RLE sub-stage. code is in [1 ..= max_rle] and max_rle <= 16. this.read_bits?(src: args.src, n: code.min(no_more_than: 16)) reps = this.bits_value ~mod+ ((1 as base.u32) << code.min(no_more_than: 16)) if reps > (size ~mod- ci) { return "#bad context map repeat" } while reps > 0 { i = offset ~mod+ ci if i >= 17408 { return "#internal error: inconsistent ring buffer" } this.context_maps[i] = 0 ci ~mod+= 1 reps -= 1 } } // BROTLI_STATE_CONTEXT_MAP_TRANSFORM. this.read_bits?(src: args.src, n: 1) if this.bits_value <> 0 { this.inverse_move_to_front_transform!(offset: offset, size: size) } this.bits_value = num_htrees } // Mirrors InverseMoveToFrontTransform in decode.c. That C function // re-initializes only part of its move-to-front list, but that's an // optimization: it's equivalent to re-initializing all of it. pri func decoder.inverse_move_to_front_transform!(offset: base.u32[..= 16384], size: base.u32[..= 16384]) { var i : base.u32 var j : base.u32 var index : base.u32[..= 0xFF] var value : base.u8 i = 0 while i < 256 { this.mtf[i] = (i & 0xFF) as base.u8 i += 1 } i = 0 while i < args.size { j = args.offset ~mod+ i if j >= 17408 { return nothing } index = this.context_maps[j] as base.u32 value = this.mtf[index] this.context_maps[j] = value while index > 0 { this.mtf[index] = this.mtf[index - 1] index -= 1 } this.mtf[0] = value i ~mod+= 1 } } // read_block_length reads a block length, using the args.t'th block length // prefix code, and stores it in this.bits_value. Mirrors SafeReadBlockLength // in decode.c. pri func decoder.read_block_length?(src: base.io_reader, t: base.u32[..= 2]) { var code : base.u32[..= 25] this.read_symbol?(src: args.src, hoff: BLOCK_LEN_HUFFS_BASE + (args.t * BLOCK_LEN_HUFFS_STRIDE)) if this.symbol >= 26 { return "#internal error: inconsistent Huffman table" } code = this.symbol.min(no_more_than: 25) this.read_bits?(src: args.src, n: BLOCK_LENGTH_NBITS[code]) this.bits_value = BLOCK_LENGTH_OFFSETS[code] ~mod+ this.bits_value } // Mirrors DecodeBlockTypeAndLength in decode.c. pri func decoder.decode_block_switch?(src: base.io_reader, t: base.u32[..= 2]) { var t : base.u32[..= 2] var max_block_type : base.u32 var block_type : base.u32 t = args.t if this.num_block_types[t] <= 1 { return "#bad block switch" } this.read_symbol?(src: args.src, hoff: BLOCK_TYPE_HUFFS_BASE + (t * BLOCK_TYPE_HUFFS_STRIDE)) block_type = this.symbol this.read_block_length?(src: args.src, t: t) this.block_length[t] = this.bits_value max_block_type = this.num_block_types[t] if block_type == 1 { block_type = this.block_type_rb[(2 * t) + 1] ~mod+ 1 } else if block_type == 0 { block_type = this.block_type_rb[2 * t] } else { block_type ~mod-= 2 } if block_type >= max_block_type { block_type -= max_block_type } this.block_type_rb[2 * t] = this.block_type_rb[(2 * t) + 1] this.block_type_rb[(2 * t) + 1] = block_type } // Mirrors PrepareLiteralDecoding in decode.c. pri func decoder.prepare_literal_decoding!() { var block_type : base.u32[..= 0xFF] block_type = this.block_type_rb[1] & 0xFF this.context_map_slice = block_type << 6 this.context_mode = (this.context_modes[block_type] & 3) as base.u32 } // Mirrors CalculateDistanceLut in decode.c. pri func decoder.calculate_distance_lut!() { var npostfix : base.u32[..= 3] var ndirect : base.u32[..= 120] var limit : base.u32[..= 520] var postfix : base.u32[..= 8] var i : base.u32 var j : base.u32 var bits : base.u32 var half : base.u32 var dbase : base.u32 npostfix = this.distance_postfix_bits ndirect = this.num_direct_distance_codes limit = this.distance_alphabet_size postfix = (1 as base.u32) << npostfix // Skip short codes. i = 16 // Fill direct codes. j = 0 while j < ndirect { if i >= 1128 { return nothing } this.dist_extra_bits[i] = 0 this.dist_offset[i] = j ~mod+ 1 i ~mod+= 1 j ~mod+= 1 } // Fill regular distance codes. bits = 1 half = 0 while i < limit { dbase = (((2 ~mod+ half) ~mod<< (bits & 31)) ~mod- 4) ~mod<< npostfix dbase = (ndirect ~mod+ dbase) ~mod+ 1 // Always fill the complete group. j = 0 while j < postfix { if i >= 1128 { return nothing } this.dist_extra_bits[i] = (bits & 0xFF) as base.u8 this.dist_offset[i] = dbase ~mod+ j i ~mod+= 1 j ~mod+= 1 } bits = bits ~mod+ half half = half ^ 1 } } // Mirrors ProcessCommandsInternal in decode.c. pri func decoder.process_commands?(src: base.io_reader, workbuf: slice base.u8) { var cmd_code : base.u32[..= 703] var insert_length : base.u32 var p1 : base.u32[..= 0xFF] var p2 : base.u32[..= 0xFF] var mode_offset : base.u32[..= 0x600] var ctx : base.u32[..= 63] var tree : base.u32[..= 0xFF] var max_distance : base.u32 var distance : base.u32 var i : base.u32 var n : base.u32 var k : base.u32 var b : base.u8 while true { // CommandBegin. if this.block_length[1] == 0 { // Mirrors DecodeCommandBlockSwitch. this.decode_block_switch?(src: args.src, t: 1) this.htree_command = this.block_type_rb[3] & 0xFF continue } // Mirrors ReadCommandInternal. this.read_symbol?(src: args.src, hoff: CMD_HUFFS_BASE + (this.htree_command * CMD_HUFFS_STRIDE)) if this.symbol >= 704 { return "#internal error: inconsistent Huffman table" } cmd_code = this.symbol.min(no_more_than: 703) this.implicit_zero_distance = CMD_IMPLICIT_ZERO_DISTANCE[cmd_code] <> 0 this.distance_context = CMD_DISTANCE_CONTEXT[cmd_code] as base.u32 this.update_dist_htree_index!() this.read_bits?(src: args.src, n: CMD_INSERT_LEN_EXTRA_BITS[cmd_code] as base.u32) insert_length = CMD_INSERT_LEN_OFFSET[cmd_code] ~mod+ this.bits_value this.read_bits?(src: args.src, n: CMD_COPY_LEN_EXTRA_BITS[cmd_code] as base.u32) this.copy_length = CMD_COPY_LEN_OFFSET[cmd_code] ~mod+ this.bits_value this.block_length[1] ~mod-= 1 if insert_length > 0 { this.mb_remaining ~mod-= insert_length // CommandInner. Read the literals in the command. while insert_length > 0 { if this.block_length[0] == 0 { // Mirrors DecodeLiteralBlockSwitch. this.decode_block_switch?(src: args.src, t: 0) this.prepare_literal_decoding!() continue } p1 = this.get_byte(workbuf: args.workbuf, distance: 1) as base.u32 p2 = this.get_byte(workbuf: args.workbuf, distance: 2) as base.u32 mode_offset = this.context_mode << 9 // BROTLI_CONTEXT(p1, p2, s->context_lookup). ctx = (CONTEXT_LOOKUP[mode_offset | p1] | CONTEXT_LOOKUP[mode_offset | 0x100 | p2]) as base.u32 tree = this.context_maps[this.context_map_slice + ctx] as base.u32 this.read_symbol?(src: args.src, hoff: LIT_HUFFS_BASE + (tree * LIT_HUFFS_STRIDE)) this.put_byte!(workbuf: args.workbuf, b: (this.symbol & 0xFF) as base.u8) this.block_length[0] ~mod-= 1 insert_length -= 1 if this.pos == this.rb_size { this.ring_event?() } } if (this.mb_remaining == 0) or (this.mb_remaining >= 0x8000_0000) { // BROTLI_STATE_METABLOCK_DONE. return ok } } // CommandPostDecodeLiterals. if this.implicit_zero_distance { // Implicit distance case. this.distance_context = 1 this.dist_rb_idx ~mod-= 1 this.distance_code = this.dist_rb[this.dist_rb_idx & 3] } else { // Read distance code in the command, unless it was implicitly // zero. if this.block_length[2] == 0 { // Mirrors DecodeDistanceBlockSwitch. this.decode_block_switch?(src: args.src, t: 2) this.dist_context_map_slice = (this.block_type_rb[5] & 0xFF) << 2 this.update_dist_htree_index!() } this.read_distance?(src: args.src) } if this.max_distance <> this.max_backward_distance { this.max_distance = this.pos.min(no_more_than: this.max_backward_distance) } max_distance = this.max_distance i = this.copy_length distance = this.distance_code if distance > max_distance { // Apply a static dictionary reference, since the distance is // larger than the max LZ77 distance. // // The maximum allowed distance is BROTLI_MAX_ALLOWED_DISTANCE. if distance > 0x7FFF_FFFC { return "#bad distance" } // The C code checks for SHARED_BROTLI_MIN_DICTIONARY_WORD_LENGTH // (4) and SHARED_BROTLI_MAX_DICTIONARY_WORD_LENGTH (31) but the // RFC 7932 dictionary's size_bits_by_length is zero for lengths // 25 ..= 31, which also fails with "#bad dictionary". // // TODO: support custom dictionaries. if (i < 4) or (24 < i) { return "#bad dictionary" } this.dist_rb_idx ~mod+= this.distance_context n = this.copy_dictionary_word!( address: (distance ~mod- max_distance) ~mod- 1, len: i.min(no_more_than: 24)) if n == 0xFFFF_FFFF { return "#bad transform" } else if (n == 0) and (distance <= 120) { return "#bad transform" } this.mb_remaining ~mod-= n k = 0 while k < n { this.put_byte!(workbuf: args.workbuf, b: this.transformed_word[k & 63]) k ~mod+= 1 if this.pos == this.rb_size { this.ring_event?() } } } else { // Apply an LZ77 copy. Update the recent distances cache. this.dist_rb[this.dist_rb_idx & 3] = distance this.dist_rb_idx ~mod+= 1 this.mb_remaining ~mod-= i while i > 0 { b = this.get_byte(workbuf: args.workbuf, distance: distance) this.put_byte!(workbuf: args.workbuf, b: b) i -= 1 if this.pos == this.rb_size { this.ring_event?() } } } if (this.mb_remaining == 0) or (this.mb_remaining >= 0x8000_0000) { // BROTLI_STATE_METABLOCK_DONE. return ok } } } // update_dist_htree_index sets this.dist_htree_index from the distance // context map. Mirrors part of ReadCommandInternal and // DecodeDistanceBlockSwitch in decode.c. pri func decoder.update_dist_htree_index!() { this.dist_htree_index = this.context_maps[ DIST_CONTEXT_MAP_BASE + this.dist_context_map_slice + this.distance_context] as base.u32 } // Mirrors ReadDistanceInternal in decode.c. pri func decoder.read_distance?(src: base.io_reader) { var code : base.u32 this.read_symbol?(src: args.src, hoff: DIST_HUFFS_BASE + (this.dist_htree_index * DIST_HUFFS_STRIDE)) code = this.symbol this.block_length[2] ~mod-= 1 // Convert the distance code to the actual distance by possibly looking up // past distances from the this.dist_rb. this.distance_context = 0 if code < 16 { this.take_distance_from_ring_buffer!(code: code) return ok } if (code >= this.distance_alphabet_size) or (code >= 1128) { return "#internal error: inconsistent Huffman table" } this.read_bits?(src: args.src, n: this.dist_extra_bits[code].min(no_more_than: 24) as base.u32) if code >= 1128 { return "#internal error: inconsistent Huffman table" } this.distance_code = this.dist_offset[code] ~mod+ (this.bits_value ~mod<< this.distance_postfix_bits) } // Mirrors TakeDistanceFromRingBuffer in decode.c. pri func decoder.take_distance_from_ring_buffer!(code: base.u32[..= 15]) { var index_delta : base.u32[..= 3] var nibble : base.u32[..= 5] var v : base.u32 if args.code <= 3 { // Compensate double distance-ring-buffer roll for dictionary items. this.distance_context = 0 if args.code == 0 { this.distance_context = 1 } this.distance_code = this.dist_rb[((this.dist_rb_idx ~mod+ 3) ~mod- args.code) & 3] this.dist_rb_idx ~mod-= this.distance_context return nothing } index_delta = 3 if args.code < 10 { nibble = args.code - 4 } else { nibble = args.code - 10 index_delta = 2 } // Unpack one of six 4-bit values, and subtract 3. v = this.dist_rb[(this.dist_rb_idx ~mod+ index_delta) & 3] ~mod+ (((0x60_5142 as base.u32) >> (4 * nibble)) & 15) if v <= 3 { // A huge distance will cause a "#bad distance" soon. this.distance_code = 0x7FFF_FFFF } else { this.distance_code = v - 3 } } // copy_dictionary_word writes a transformed static dictionary word to // this.transformed_word and returns its length. It returns 0xFFFF_FFFF if the // transform index is out of range. Mirrors part of ProcessCommandsInternal in // decode.c and BrotliTransformDictionaryWord in transform.c. pri func decoder.copy_dictionary_word!(address: base.u32, len: base.u32[..= 24]) base.u32 { var shift : base.u32[..= 24] var word_idx : base.u32 var transform_idx : base.u32 var offset : base.u32 var t : base.u32[..= 120] var t_type : base.u32[..= 22] var affix : base.u32[..= 216] var affix_len : base.u32 var idx : base.u32 var word_start : base.u32 var word_len : base.u32 var skip : base.u32[..= 9] var i : base.u32 var j : base.u32 var step : base.u32 shift = DICTIONARY_SIZE_BITS_BY_LENGTH[args.len] word_idx = args.address & (((1 as base.u32) << shift) - 1) transform_idx = args.address >> shift if transform_idx >= NUM_TRANSFORMS { return 0xFFFF_FFFF } t = transform_idx.min(no_more_than: 120) offset = DICTIONARY_OFFSETS_BY_LENGTH[args.len] ~mod+ (word_idx ~mod* args.len) // The prefix. idx = 0 affix = PREFIX_SUFFIX_MAP[TRANSFORM_PREFIX_ID[t]] affix_len = PREFIX_SUFFIX[affix] as base.u32 i = 0 while i < affix_len { j = (affix ~mod+ 1) ~mod+ i if (j >= 217) or (idx >= 64) { break } this.transformed_word[idx] = PREFIX_SUFFIX[j] idx ~mod+= 1 i ~mod+= 1 } // The word, possibly with its first or last few bytes omitted. t_type = TRANSFORM_TYPE[t] as base.u32 word_len = args.len skip = 0 if t_type <= 9 { // BROTLI_TRANSFORM_OMIT_LAST_N. if t_type < args.len { word_len = args.len ~mod- t_type } else { word_len = 0 } } else if (12 <= t_type) and (t_type <= 20) { // BROTLI_TRANSFORM_OMIT_FIRST_N. skip = t_type - 11 if skip < args.len { word_len = args.len ~mod- skip } else { word_len = 0 } } word_start = idx i = 0 while i < word_len { j = (offset ~mod+ skip) ~mod+ i if (j >= 122784) or (idx >= 64) { break } this.transformed_word[idx] = DICTIONARY[j] idx ~mod+= 1 i ~mod+= 1 } // Upper-casing. The C code's ToUpperCase can modify bytes after the // word's end, but those bytes are either overwritten by the suffix or are // past the transformed word's length, so we don't bother. if t_type == 10 { // BROTLI_TRANSFORM_UPPERCASE_FIRST. this.to_upper_case!(p: word_start, end: idx) } else if t_type == 11 { // BROTLI_TRANSFORM_UPPERCASE_ALL. i = word_start while i < idx { step = this.to_upper_case!(p: i, end: idx) i ~mod+= step } } // TODO: support BROTLI_TRANSFORM_SHIFT_FIRST and _SHIFT_ALL (used only by // custom dictionaries' transforms). // The suffix. affix = PREFIX_SUFFIX_MAP[TRANSFORM_SUFFIX_ID[t]] affix_len = PREFIX_SUFFIX[affix] as base.u32 i = 0 while i < affix_len { j = (affix ~mod+ 1) ~mod+ i if (j >= 217) or (idx >= 64) { break } this.transformed_word[idx] = PREFIX_SUFFIX[j] idx ~mod+= 1 i ~mod+= 1 } return idx } // Mirrors ToUpperCase in transform.c, except that it does not modify bytes at // or after args.end. It returns the step: the number of bytes in the // (overly simplified model of a) UTF-8 encoded character. pri func decoder.to_upper_case!(p: base.u32, end: base.u32) base.u32 { var c : base.u8 if args.p >= 64 { return 1 } c = this.transformed_word[args.p] if c < 0xC0 { if ('a' <= c) and (c <= 'z') { this.transformed_word[args.p] = c ^ 32 } return 1 } // An overly simplified uppercasing model for UTF-8. if c < 0xE0 { if ((args.p + 1) < args.end) and ((args.p + 1) < 64) { this.transformed_word[args.p + 1] ^= 32 } return 2 } // An arbitrary transform for three byte characters. if ((args.p + 2) < args.end) and ((args.p + 2) < 64) { this.transformed_word[args.p + 2] ^= 5 } return 3 }