Files
wuffs/test/c/mimiclib/brotli.c
T
Claude 4978207531 std/brotli: add a brotli decoder with parity to libbrotlidec v1.2.1
This is the first milestone of a byte-exact rewrite of brotli (reference
implementation tag v1.2.1) in Wuffs. The decoder mirrors c/dec/decode.c
function by function, including its validation checks: the same output for
valid input and the same verdict, with each BROTLI_DECODER_ERROR_FORMAT_*
code mapped to a Wuffs status (see std/brotli/README.md). The C state
machine is replaced by Wuffs coroutines, so decoding works with arbitrarily
small src and dst buffers.

The history window lives in the caller's workbuf. Huffman table groups,
context maps and the static dictionary scratch space live in the decoder
struct. The static dictionary, transforms, context lookup tables and prefix
code tables in common_tables.wuffs are generated from the brotli source by
script/print-brotli-tables.go.

Tests: golden tests on the upstream tests/testdata vectors and on streams
produced by the v1.2.1 CLI at several qualities and window sizes, 1-byte
I/O limits, truncation at every prefix, malformed inputs per error code,
trailing data and workbuf boundaries. With -mimic, every vector and every
malformed input is also compared against libbrotlidec, including the exact
error code. A differential run over about a million mutated inputs found
no divergence.

Not yet supported (left as TODOs): large-window brotli, custom/shared
dictionaries, metadata callbacks and fast paths.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011NoosQNe7DKWnqDCBeqw1p
2026-10-26 22:46:34 +00:00

75 lines
2.8 KiB
C

// Copyright 2026 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
// ----------------
// The reference implementation for std/brotli is libbrotlidec v1.2.1 (from
// github.com/google/brotli). Older versions (such as v1.1.0) differ slightly
// in which error code some invalid inputs produce.
#include <limits.h>
#include <stdint.h>
#include "brotli/decode.h"
// mimic_brotli_decode_error_code is like mimic_brotli_decode but also stores
// the libbrotlidec error code (a BrotliDecoderErrorCode value) in
// *error_code_ptr. A BROTLI_DECODER_NEEDS_MORE_INPUT (2) error code means that
// the input was truncated.
const char* //
mimic_brotli_decode_error_code(wuffs_base__io_buffer* dst,
wuffs_base__io_buffer* src,
int* error_code_ptr) {
*error_code_ptr = 0;
BrotliDecoderState* state = BrotliDecoderCreateInstance(NULL, NULL, NULL);
if (!state) {
return "libbrotlidec: BrotliDecoderCreateInstance failed";
}
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";
}
const char* //
mimic_brotli_decode(wuffs_base__io_buffer* dst,
wuffs_base__io_buffer* src,
uint32_t wuffs_initialize_flags,
uint64_t wlimit,
uint64_t rlimit) {
if ((wlimit < UINT64_MAX) || (rlimit < UINT64_MAX)) {
// It's simpler if we only assume one-shot decompression.
return "unsupported I/O limit";
}
int error_code = 0;
return mimic_brotli_decode_error_code(dst, src, &error_code);
}