From c94c6f805c881b2a67540f5953a59d963d87a7f8 Mon Sep 17 00:00:00 2001 From: Alexey Ivanov Date: Tue, 2 Oct 2018 07:28:37 -0700 Subject: tools/brotli: improve window size autodetect (#710) Window size is defined as: `(1 << BROTLI_PARAM_LGWIN) - 16` in `c/include/brotli/encode.h` Therefore we should probably take these 16 bytes into account. Done basic manual testing: $ python3 -c 'print ("A"*2046)' > t $ bazel run -- //:brotli -w 0 -f -o $(realpath t).br $(realpath ./t) $ python3 research/brotlidump.py t.br |& fgrep WSIZE 0000 c1 1000001 WSIZE windowsize=(1<<12)-16=4080 New version properly detects window size of `4080`, while previous one used `2032`: $ python3 research/brotlidump.py t.br |& fgrep WSIZE 0000 b1 0110001 WSIZE windowsize=(1<<11)-16=2032 --- c/tools/brotli.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/c/tools/brotli.c b/c/tools/brotli.c index ea4fdac..ce05b64 100644 --- a/c/tools/brotli.c +++ b/c/tools/brotli.c @@ -943,10 +943,9 @@ static BROTLI_BOOL CompressFiles(Context* context) { uint32_t lgwin = DEFAULT_LGWIN; /* Use file size to limit lgwin. */ if (context->input_file_length >= 0) { - int32_t size = 1 << BROTLI_MIN_WINDOW_BITS; lgwin = BROTLI_MIN_WINDOW_BITS; - while (size < context->input_file_length) { - size <<= 1; + while (BROTLI_MAX_BACKWARD_LIMIT(lgwin) < + (uint64_t)context->input_file_length) { lgwin++; if (lgwin == BROTLI_MAX_WINDOW_BITS) break; } -- cgit v1.1