std/brotli: large-window mode, custom dictionaries and a fuzzer

Completes the decoder's feature set relative to libbrotlidec v1.2.1:

- QUIRK_LARGE_WINDOW mirrors BROTLI_DECODER_PARAM_LARGE_WINDOW: the
  large-window WBITS escape, window bits up to 30, the 544-symbol distance
  alphabet and 64-bit distance arithmetic, with workbuf_len growing to
  (1 << 30) + 542 when the quirk is set.
- decoder.add_dictionary! mirrors BrotliDecoderAttachDictionary for raw
  (LZ77 prefix) and serialized shared dictionaries: compound-dictionary
  distance mapping, the shared-dictionary parser (custom word lists,
  transform lists including the SHIFT transforms, context-dependent
  dictionary selection, prefix dictionaries) and all of its validation.
  Wuffs structs cannot hold slices, so the dictionary bytes are copied
  into the front of the caller's workbuf; the layout is documented in
  the README. Serialized dictionaries are accepted as in a
  BROTLI_EXPERIMENTAL build of libbrotli.
- fuzz/c/std/brotli_fuzzer.c, exercising the quirk, dictionaries and
  random I/O limits.

Tests: new golden vectors for large windows (including a 17 MiB
far-distance stream generated in the test), raw and serialized
dictionaries, nine more malformed inputs, and mimic comparisons that
attach the same dictionaries to libbrotlidec. Differential runs over
2.7 million mutated large-window and dictionary streams found no
divergence from libbrotlidec.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011NoosQNe7DKWnqDCBeqw1p
This commit is contained in:
Claude committed 2026-10-26 23:45:06 +00:00
1 parent 4978207531
commit b9be8feaee
43 files changed
+4253 -460

No files matched your search

+72
View File
@@ -72,3 +72,75 @@ mimic_brotli_decode(wuffs_base__io_buffer* dst,
int error_code = 0;
return mimic_brotli_decode_error_code(dst, src, &error_code);
}
// ---------------- Large Window and Dictionaries
#include "brotli/shared_dictionary.h"
// mimic_brotli_decode_with_options is like mimic_brotli_decode_error_code but
// it also sets BROTLI_DECODER_PARAM_LARGE_WINDOW (if large_window is true) and
// attaches dictionaries (with BrotliDecoderAttachDictionary). Each
// dict_types[i] is a BrotliSharedDictionaryType value: 0 for
// BROTLI_SHARED_DICTIONARY_RAW and 1 for BROTLI_SHARED_DICTIONARY_SERIALIZED.
//
// libbrotlidec only supports serialized dictionaries when built with
// BROTLI_EXPERIMENTAL (which is not the default). When attaching a dictionary
// fails, this function returns
// mimic_brotli_attach_dictionary_failed_message.
const char mimic_brotli_attach_dictionary_failed_message[] =
"libbrotlidec: BrotliDecoderAttachDictionary failed";
const char* //
mimic_brotli_decode_with_options(wuffs_base__io_buffer* dst,
wuffs_base__io_buffer* src,
bool large_window,
size_t num_dicts,
const uint32_t* dict_types,
const wuffs_base__slice_u8* dicts,
int* error_code_ptr) {
*error_code_ptr = 0;
BrotliDecoderState* state = BrotliDecoderCreateInstance(NULL, NULL, NULL);
if (!state) {
return "libbrotlidec: BrotliDecoderCreateInstance failed";
}
if (large_window &&
!BrotliDecoderSetParameter(state, BROTLI_DECODER_PARAM_LARGE_WINDOW,
1u)) {
BrotliDecoderDestroyInstance(state);
return "libbrotlidec: BrotliDecoderSetParameter failed";
}
for (size_t i = 0; i < num_dicts; i++) {
if (!BrotliDecoderAttachDictionary(
state, (BrotliSharedDictionaryType)dict_types[i], dicts[i].len,
dicts[i].ptr)) {
BrotliDecoderDestroyInstance(state);
return mimic_brotli_attach_dictionary_failed_message;
}
}
size_t avail_in = wuffs_base__io_buffer__reader_length(src);
const uint8_t* next_in = wuffs_base__io_buffer__reader_pointer(src);
size_t avail_out = wuffs_base__io_buffer__writer_length(dst);
uint8_t* next_out = wuffs_base__io_buffer__writer_pointer(dst);
size_t total_out = 0;
BrotliDecoderResult result = BrotliDecoderDecompressStream(
state, &avail_in, &next_in, &avail_out, &next_out, &total_out);
*error_code_ptr = (int)BrotliDecoderGetErrorCode(state);
BrotliDecoderDestroyInstance(state);
src->meta.ri += wuffs_base__io_buffer__reader_length(src) - avail_in;
dst->meta.wi += wuffs_base__io_buffer__writer_length(dst) - avail_out;
switch (result) {
case BROTLI_DECODER_RESULT_SUCCESS:
return NULL;
case BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT:
return "libbrotlidec: needs more input";
case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT:
return "libbrotlidec: needs more output";
default:
break;
}
return "libbrotlidec: BrotliDecoderDecompressStream failed";
}