From 4c08c515f3c45abde832ce7421794ae6fc14cd21 Mon Sep 17 00:00:00 2001
From: Eugene Kliuchnikov <eustas@chromium.org>
Date: Wed, 26 Aug 2020 10:42:53 +0200
Subject: [PATCH] Update

 * IMPORTANT: decoder: fix overflow when input chunk is >2GiB
 * simplify max Huffman table size calculation
 * eliminate symbol duplicates (static arrays in .h files)
 * minor combing in research/ code
---
 .travis.yml                |   5 +-
 c/common/constants.c       |  15 ++++
 c/common/constants.h       |  16 ++++
 c/common/context.c         | 156 +++++++++++++++++++++++++++++++++++++
 c/common/context.h         | 156 +------------------------------------
 c/common/platform.c        |  22 ++++++
 c/common/platform.h        |  11 +--
 c/dec/bit_reader.c         |  11 +++
 c/dec/bit_reader.h         |  19 ++---
 c/dec/decode.c             |   9 ++-
 c/dec/huffman.h            |   9 +--
 c/dec/prefix.h             |  18 -----
 c/dec/state.c              |   8 +-
 c/enc/brotli_bit_stream.c  |  21 +----
 c/enc/command.c            |  28 +++++++
 c/enc/command.h            |  24 +++---
 c/enc/entropy_encode.c     |   2 +
 c/enc/entropy_encode.h     |   4 +-
 c/enc/fast_log.c           | 105 +++++++++++++++++++++++++
 c/enc/fast_log.h           |  99 ++---------------------
 research/brotli_decoder.c  |   1 +
 research/draw_histogram.cc |  25 +++---
 scripts/.bintray.json      |   2 +-
 scripts/sources.lst        |   5 ++
 setup.py                   |   5 ++
 25 files changed, 431 insertions(+), 345 deletions(-)
 create mode 100644 c/common/constants.c
 create mode 100644 c/common/context.c
 create mode 100644 c/common/platform.c
 create mode 100644 c/enc/command.c
 create mode 100644 c/enc/fast_log.c

Index: brotlipy-0.6.0/libbrotli/common/constants.c
===================================================================
--- /dev/null
+++ brotlipy-0.6.0/libbrotli/common/constants.c
@@ -0,0 +1,15 @@
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+#include "./constants.h"
+
+const BrotliPrefixCodeRange
+    _kBrotliPrefixCodeRanges[BROTLI_NUM_BLOCK_LEN_SYMBOLS] = {
+        {1, 2},     {5, 2},     {9, 2},   {13, 2},    {17, 3},    {25, 3},
+        {33, 3},    {41, 3},    {49, 4},  {65, 4},    {81, 4},    {97, 4},
+        {113, 5},   {145, 5},   {177, 5}, {209, 5},   {241, 6},   {305, 6},
+        {369, 7},   {497, 8},   {753, 9}, {1265, 10}, {2289, 11}, {4337, 12},
+        {8433, 13}, {16625, 24}};
Index: brotlipy-0.6.0/libbrotli/common/constants.h
===================================================================
--- brotlipy-0.6.0.orig/libbrotli/common/constants.h
+++ brotlipy-0.6.0/libbrotli/common/constants.h
@@ -6,6 +6,8 @@
 
 #ifndef BROTLI_COMMON_CONSTANTS_H_
 #define BROTLI_COMMON_CONSTANTS_H_
+#include "./port.h"
+#include <stdint.h>
 
 /* Specification: 7.3. Encoding of the context map */
 #define BROTLI_CONTEXT_MAP_MAX_RLE 16
@@ -37,6 +38,10 @@
                                      BROTLI_MAX_NDIRECT +              \
                                      (24 << (BROTLI_MAX_NPOSTFIX + 1)))
 
+
+/* Specification: 4. Encoding of Literal Insertion Lengths and Copy Lengths */
+#define BROTLI_NUM_INS_COPY_CODES 24
+
 /* 7.1. Context modes and context ID lookup for literals */
 /* "context IDs for literals are in the range of 0..63" */
 #define BROTLI_LITERAL_CONTEXT_BITS 6
@@ -44,4 +49,15 @@
 /* 7.2. Context ID for distances */
 #define BROTLI_DISTANCE_CONTEXT_BITS 2
 
+/* Represents the range of values belonging to a prefix code:
+   [offset, offset + 2^nbits) */
+typedef struct {
+  uint16_t offset;
+  uint8_t nbits;
+} BrotliPrefixCodeRange;
+
+/* "Soft-private", it is exported, but not "advertised" as API. */
+extern const BrotliPrefixCodeRange
+    _kBrotliPrefixCodeRanges[BROTLI_NUM_BLOCK_LEN_SYMBOLS];
+
 #endif  /* BROTLI_COMMON_CONSTANTS_H_ */
Index: brotlipy-0.6.0/libbrotli/common/context.c
===================================================================
--- /dev/null
+++ brotlipy-0.6.0/libbrotli/common/context.c
@@ -0,0 +1,156 @@
+#include "./context.h"
+
+#include <brotli/types.h>
+
+/* Common context lookup table for all context modes. */
+const uint8_t _kBrotliContextLookupTable[2048] = {
+  /* CONTEXT_LSB6, last byte. */
+   0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
+  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
+  32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
+  48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
+   0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
+  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
+  32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
+  48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
+   0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
+  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
+  32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
+  48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
+   0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
+  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
+  32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
+  48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
+
+  /* CONTEXT_LSB6, second last byte, */
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+
+  /* CONTEXT_MSB6, last byte. */
+   0,  0,  0,  0,  1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,
+   4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,  6,  7,  7,  7,  7,
+   8,  8,  8,  8,  9,  9,  9,  9, 10, 10, 10, 10, 11, 11, 11, 11,
+  12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15,
+  16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19,
+  20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23,
+  24, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27,
+  28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31,
+  32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 35, 35, 35, 35,
+  36, 36, 36, 36, 37, 37, 37, 37, 38, 38, 38, 38, 39, 39, 39, 39,
+  40, 40, 40, 40, 41, 41, 41, 41, 42, 42, 42, 42, 43, 43, 43, 43,
+  44, 44, 44, 44, 45, 45, 45, 45, 46, 46, 46, 46, 47, 47, 47, 47,
+  48, 48, 48, 48, 49, 49, 49, 49, 50, 50, 50, 50, 51, 51, 51, 51,
+  52, 52, 52, 52, 53, 53, 53, 53, 54, 54, 54, 54, 55, 55, 55, 55,
+  56, 56, 56, 56, 57, 57, 57, 57, 58, 58, 58, 58, 59, 59, 59, 59,
+  60, 60, 60, 60, 61, 61, 61, 61, 62, 62, 62, 62, 63, 63, 63, 63,
+
+  /* CONTEXT_MSB6, second last byte, */
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+
+  /* CONTEXT_UTF8, last byte. */
+  /* ASCII range. */
+   0,  0,  0,  0,  0,  0,  0,  0,  0,  4,  4,  0,  0,  4,  0,  0,
+   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+   8, 12, 16, 12, 12, 20, 12, 16, 24, 28, 12, 12, 32, 12, 36, 12,
+  44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 32, 32, 24, 40, 28, 12,
+  12, 48, 52, 52, 52, 48, 52, 52, 52, 48, 52, 52, 52, 52, 52, 48,
+  52, 52, 52, 52, 52, 48, 52, 52, 52, 52, 52, 24, 12, 28, 12, 12,
+  12, 56, 60, 60, 60, 56, 60, 60, 60, 56, 60, 60, 60, 60, 60, 56,
+  60, 60, 60, 60, 60, 56, 60, 60, 60, 60, 60, 24, 12, 28, 12,  0,
+  /* UTF8 continuation byte range. */
+  0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
+  0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
+  0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
+  0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
+  /* UTF8 lead byte range. */
+  2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
+  2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
+  2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
+  2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
+
+  /* CONTEXT_UTF8 second last byte. */
+  /* ASCII range. */
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1,
+  1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1,
+  1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
+  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 0,
+  /* UTF8 continuation byte range. */
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  /* UTF8 lead byte range. */
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+
+  /* CONTEXT_SIGNED, last byte, same as the above values shifted by 3 bits. */
+   0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
+  16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
+  16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
+  16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
+  24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+  24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+  24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+  24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
+  32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
+  32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
+  32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
+  32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
+  40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+  40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+  40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
+  48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 56,
+
+  /* CONTEXT_SIGNED, second last byte. */
+  0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
+  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
+  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
+  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
+  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
+  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
+  5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
+  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7,
+};
Index: brotlipy-0.6.0/libbrotli/common/platform.c
===================================================================
--- /dev/null
+++ brotlipy-0.6.0/libbrotli/common/platform.c
@@ -0,0 +1,21 @@
+/* Copyright 2016 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+#include <stdlib.h>
+
+#include "./types.h"
+
+/* Default brotli_alloc_func */
+void* BrotliDefaultAllocFunc(void* opaque, size_t size) {
+  BROTLI_UNUSED(opaque);
+  return malloc(size);
+}
+
+/* Default brotli_free_func */
+void BrotliDefaultFreeFunc(void* opaque, void* address) {
+  BROTLI_UNUSED(opaque);
+  free(address);
+}
Index: brotlipy-0.6.0/libbrotli/dec/bit_reader.c
===================================================================
--- brotlipy-0.6.0.orig/libbrotli/dec/bit_reader.c
+++ brotlipy-0.6.0/libbrotli/dec/bit_reader.c
@@ -15,6 +15,17 @@
 extern "C" {
 #endif
 
+const uint32_t kBrotliBitMask[33] = {   0x00000000,
+    0x00000001, 0x00000003, 0x00000007, 0x0000000F,
+    0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
+    0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
+    0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
+    0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
+    0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
+    0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
+    0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
+};
+
 void BrotliInitBitReader(BrotliBitReader* const br) {
   br->val_ = 0;
   br->bit_pos_ = sizeof(br->val_) << 3;
Index: brotlipy-0.6.0/libbrotli/dec/bit_reader.h
===================================================================
--- brotlipy-0.6.0.orig/libbrotli/dec/bit_reader.h
+++ brotlipy-0.6.0/libbrotli/dec/bit_reader.h
@@ -11,6 +11,7 @@
 
 #include <string.h>  /* memcpy */
 
+#include "../common/constants.h"
 #include "../common/types.h"
 #include "./port.h"
 
@@ -26,16 +27,7 @@ typedef uint64_t reg_t;
 typedef uint32_t reg_t;
 #endif
 
-static const uint32_t kBitMask[33] = { 0x0000,
-    0x00000001, 0x00000003, 0x00000007, 0x0000000F,
-    0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
-    0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
-    0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
-    0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
-    0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
-    0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
-    0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
-};
+BROTLI_INTERNAL extern const uint32_t kBrotliBitMask[33];
 
 static BROTLI_INLINE uint32_t BitMask(uint32_t n) {
   if (IS_CONSTANT(n) || BROTLI_HAS_UBFX) {
@@ -43,7 +35,7 @@ static BROTLI_INLINE uint32_t BitMask(ui
        "Unsigned Bit Field Extract" UBFX instruction on ARM. */
     return ~((0xffffffffU) << n);
   } else {
-    return kBitMask[n];
+    return kBrotliBitMask[n];
   }
 }
 
@@ -92,8 +84,11 @@ static BROTLI_INLINE uint32_t BrotliGetA
 }
 
 /* Returns amount of unread bytes the bit reader still has buffered from the
-   BrotliInput, including whole bytes in br->val_. */
+   BrotliInput, including whole bytes in br->val_. Result is capped with
+   maximal ring-buffer size (larger number won't be utilized anyway). */
 static BROTLI_INLINE size_t BrotliGetRemainingBytes(BrotliBitReader* br) {
+  static const size_t kCap = (size_t)1 << 30;
+  if (br->avail_in > kCap) return kCap;
   return br->avail_in + (BrotliGetAvailableBits(br) >> 3);
 }
 
Index: brotlipy-0.6.0/libbrotli/dec/decode.c
===================================================================
--- brotlipy-0.6.0.orig/libbrotli/dec/decode.c
+++ brotlipy-0.6.0/libbrotli/dec/decode.c
@@ -801,8 +801,8 @@ static BROTLI_INLINE uint32_t ReadBlockL
   uint32_t code;
   uint32_t nbits;
   code = ReadSymbol(table, br);
-  nbits = kBlockLengthPrefixCode[code].nbits; /* nbits == 2..24 */
-  return kBlockLengthPrefixCode[code].offset + BrotliReadBits(br, nbits);
+  nbits = _kBrotliPrefixCodeRanges[code].nbits;  /* nbits == 2..24 */
+  return _kBrotliPrefixCodeRanges[code].offset + BrotliReadBits(br, nbits);
 }
 
 /* WARNING: if state is not BROTLI_STATE_READ_BLOCK_LENGTH_NONE, then
@@ -820,13 +820,14 @@ static BROTLI_INLINE BROTLI_BOOL SafeRea
   }
   {
     uint32_t bits;
-    uint32_t nbits = kBlockLengthPrefixCode[index].nbits; /* nbits == 2..24 */
+    uint32_t nbits = _kBrotliPrefixCodeRanges[index].nbits;
+    uint32_t offset = _kBrotliPrefixCodeRanges[index].offset;
     if (!BrotliSafeReadBits(br, nbits, &bits)) {
       s->block_length_index = index;
       s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX;
       return BROTLI_FALSE;
     }
-    *result = kBlockLengthPrefixCode[index].offset + bits;
+    *result = offset + bits;
     s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
     return BROTLI_TRUE;
   }
Index: brotlipy-0.6.0/libbrotli/dec/huffman.h
===================================================================
--- brotlipy-0.6.0.orig/libbrotli/dec/huffman.h
+++ brotlipy-0.6.0/libbrotli/dec/huffman.h
@@ -18,11 +18,6 @@ extern "C" {
 
 #define BROTLI_HUFFMAN_MAX_CODE_LENGTH 15
 
-/* Maximum possible Huffman table size for an alphabet size of (index * 32),
- * max code length 15 and root table bits 8. */
-static const uint16_t kMaxHuffmanTableSize[] = {
-  256, 402, 436, 468, 500, 534, 566, 598, 630, 662, 694, 726, 758, 790, 822,
-  854, 886, 920, 952, 984, 1016, 1048, 1080};
 /* BROTLI_NUM_BLOCK_LEN_SYMBOLS == 26 */
 #define BROTLI_HUFFMAN_MAX_SIZE_26 396
 /* BROTLI_MAX_BLOCK_TYPE_SYMBOLS == 258 */
@@ -44,7 +39,7 @@ BROTLI_INTERNAL void BrotliBuildCodeLeng
 /* Builds Huffman lookup table assuming code lengths are in symbol order. */
 /* Returns size of resulting table. */
 BROTLI_INTERNAL uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
-    int root_bits, const uint16_t* const symbol_lists, uint16_t* count_arg);
+    int root_bits, const uint16_t* const symbol_lists, uint16_t* count);
 
 /* Builds a simple Huffman table. The num_symbols parameter is to be */
 /* interpreted as follows: 0 means 1 symbol, 1 means 2 symbols, 2 means 3 */
Index: brotlipy-0.6.0/libbrotli/dec/prefix.h
===================================================================
--- brotlipy-0.6.0.orig/libbrotli/dec/prefix.h
+++ brotlipy-0.6.0/libbrotli/dec/prefix.h
@@ -14,24 +14,6 @@
 #include "../common/constants.h"
 #include "../common/types.h"
 
-/* Represents the range of values belonging to a prefix code: */
-/* [offset, offset + 2^nbits) */
-struct PrefixCodeRange {
-  uint16_t offset;
-  uint8_t nbits;
-};
-
-static const struct PrefixCodeRange
-    kBlockLengthPrefixCode[BROTLI_NUM_BLOCK_LEN_SYMBOLS] = {
-  {   1,  2}, {    5,  2}, {  9,   2}, {  13,  2},
-  {  17,  3}, {   25,  3}, {  33,  3}, {  41,  3},
-  {  49,  4}, {   65,  4}, {  81,  4}, {  97,  4},
-  { 113,  5}, {  145,  5}, { 177,  5}, { 209,  5},
-  { 241,  6}, {  305,  6}, { 369,  7}, { 497,  8},
-  { 753,  9}, { 1265, 10}, {2289, 11}, {4337, 12},
-  {8433, 13}, {16625, 24}
-};
-
 typedef struct CmdLutElement {
   uint8_t insert_len_extra_bits;
   uint8_t copy_len_extra_bits;
Index: brotlipy-0.6.0/libbrotli/dec/state.c
===================================================================
--- brotlipy-0.6.0.orig/libbrotli/dec/state.c
+++ brotlipy-0.6.0/libbrotli/dec/state.c
@@ -146,8 +146,11 @@ void BrotliDecoderStateCleanup(BrotliDec
 
 void BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,
     HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t ntrees) {
-  /* Pack two allocations into one */
-  const size_t max_table_size = kMaxHuffmanTableSize[(alphabet_size + 31) >> 5];
+  /* 376 = 256 (1-st level table) + 4 + 7 + 15 + 31 + 63 (2-nd level mix-tables)
+     This number is discovered "unlimited" "enough" calculator; it is actually
+     a wee bigger than required in several cases (especially for alphabets with
+     less than 16 symbols). */
+  const size_t max_table_size = alphabet_size + 376;
   const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;
   const size_t htree_size = sizeof(HuffmanCode*) * ntrees;
   char* p = (char*)BROTLI_ALLOC(s, code_size + htree_size);
Index: brotlipy-0.6.0/libbrotli/enc/brotli_bit_stream.c
===================================================================
--- brotlipy-0.6.0.orig/libbrotli/enc/brotli_bit_stream.c
+++ brotlipy-0.6.0/libbrotli/enc/brotli_bit_stream.c
@@ -28,33 +28,18 @@ extern "C" {
 
 #define MAX_HUFFMAN_TREE_SIZE (2 * BROTLI_NUM_COMMAND_SYMBOLS + 1)
 
-/* Represents the range of values belonging to a prefix code:
-   [offset, offset + 2^nbits) */
-typedef struct PrefixCodeRange {
-  uint32_t offset;
-  uint32_t nbits;
-} PrefixCodeRange;
-
-static const PrefixCodeRange
-    kBlockLengthPrefixCode[BROTLI_NUM_BLOCK_LEN_SYMBOLS] = {
-  { 1, 2}, { 5, 2}, { 9, 2}, {13, 2}, {17, 3}, { 25, 3}, { 33, 3},
-  {41, 3}, {49, 4}, {65, 4}, {81, 4}, {97, 4}, {113, 5}, {145, 5},
-  {177, 5}, { 209,  5}, { 241,  6}, { 305,  6}, { 369,  7}, {  497,  8},
-  {753, 9}, {1265, 10}, {2289, 11}, {4337, 12}, {8433, 13}, {16625, 24}
-};
-
 static BROTLI_INLINE uint32_t BlockLengthPrefixCode(uint32_t len) {
   uint32_t code = (len >= 177) ? (len >= 753 ? 20 : 14) : (len >= 41 ? 7 : 0);
   while (code < (BROTLI_NUM_BLOCK_LEN_SYMBOLS - 1) &&
-      len >= kBlockLengthPrefixCode[code + 1].offset) ++code;
+      len >= _kBrotliPrefixCodeRanges[code + 1].offset) ++code;
   return code;
 }
 
 static BROTLI_INLINE void GetBlockLengthPrefixCode(uint32_t len, size_t* code,
     uint32_t* n_extra, uint32_t* extra) {
   *code = BlockLengthPrefixCode(len);
-  *n_extra = kBlockLengthPrefixCode[*code].nbits;
-  *extra = len - kBlockLengthPrefixCode[*code].offset;
+  *n_extra = _kBrotliPrefixCodeRanges[*code].nbits;
+  *extra = len - _kBrotliPrefixCodeRanges[*code].offset;
 }
 
 typedef struct BlockTypeCodeCalculator {
Index: brotlipy-0.6.0/libbrotli/enc/command.c
===================================================================
--- /dev/null
+++ brotlipy-0.6.0/libbrotli/enc/command.c
@@ -0,0 +1,28 @@
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+#include "./command.h"
+
+#include "./types.h"
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
+const uint32_t kBrotliInsBase[BROTLI_NUM_INS_COPY_CODES] = {
+    0,  1,  2,  3,  4,   5,   6,   8,   10,   14,   18,   26,
+    34, 50, 66, 98, 130, 194, 322, 578, 1090, 2114, 6210, 22594};
+const uint32_t kBrotliInsExtra[BROTLI_NUM_INS_COPY_CODES] = {
+    0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 9, 10, 12, 14, 24};
+const uint32_t kBrotliCopyBase[BROTLI_NUM_INS_COPY_CODES] = {
+    2,  3,  4,  5,  6,  7,   8,   9,   10,  12,  14,   18,
+    22, 30, 38, 54, 70, 102, 134, 198, 326, 582, 1094, 2118};
+const uint32_t kBrotliCopyExtra[BROTLI_NUM_INS_COPY_CODES] = {
+    0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 9, 10, 24};
+
+#if defined(__cplusplus) || defined(c_plusplus)
+}  /* extern "C" */
+#endif
Index: brotlipy-0.6.0/libbrotli/enc/command.h
===================================================================
--- brotlipy-0.6.0.orig/libbrotli/enc/command.h
+++ brotlipy-0.6.0/libbrotli/enc/command.h
@@ -18,14 +18,14 @@
 extern "C" {
 #endif
 
-static uint32_t kInsBase[] =   { 0, 1, 2, 3, 4, 5, 6, 8, 10, 14, 18, 26, 34, 50,
-    66, 98, 130, 194, 322, 578, 1090, 2114, 6210, 22594 };
-static uint32_t kInsExtra[] =  { 0, 0, 0, 0, 0, 0, 1, 1,  2,  2,  3,  3,  4,  4,
-    5,   5,   6,   7,   8,   9,   10,   12,   14,    24 };
-static uint32_t kCopyBase[] =  { 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 18, 22, 30,
-    38, 54,  70, 102, 134, 198, 326,   582, 1094,  2118 };
-static uint32_t kCopyExtra[] = { 0, 0, 0, 0, 0, 0, 0, 0,  1,  1,  2,  2,  3,  3,
-     4,  4,   5,   5,   6,   7,   8,     9,   10,    24 };
+BROTLI_INTERNAL extern const uint32_t
+    kBrotliInsBase[BROTLI_NUM_INS_COPY_CODES];
+BROTLI_INTERNAL extern const uint32_t
+    kBrotliInsExtra[BROTLI_NUM_INS_COPY_CODES];
+BROTLI_INTERNAL extern const uint32_t
+    kBrotliCopyBase[BROTLI_NUM_INS_COPY_CODES];
+BROTLI_INTERNAL extern const uint32_t
+    kBrotliCopyExtra[BROTLI_NUM_INS_COPY_CODES];
 
 static BROTLI_INLINE uint16_t GetInsertLengthCode(size_t insertlen) {
   if (insertlen < 6) {
@@ -81,19 +81,19 @@ static BROTLI_INLINE void GetLengthCode(
 }
 
 static BROTLI_INLINE uint32_t GetInsertBase(uint16_t inscode) {
-  return kInsBase[inscode];
+  return kBrotliInsBase[inscode];
 }
 
 static BROTLI_INLINE uint32_t GetInsertExtra(uint16_t inscode) {
-  return kInsExtra[inscode];
+  return kBrotliInsExtra[inscode];
 }
 
 static BROTLI_INLINE uint32_t GetCopyBase(uint16_t copycode) {
-  return kCopyBase[copycode];
+  return kBrotliCopyBase[copycode];
 }
 
 static BROTLI_INLINE uint32_t GetCopyExtra(uint16_t copycode) {
-  return kCopyExtra[copycode];
+  return kBrotliCopyExtra[copycode];
 }
 
 typedef struct Command {
Index: brotlipy-0.6.0/libbrotli/enc/entropy_encode.c
===================================================================
--- brotlipy-0.6.0.orig/libbrotli/enc/entropy_encode.c
+++ brotlipy-0.6.0/libbrotli/enc/entropy_encode.c
@@ -18,6 +18,8 @@
 extern "C" {
 #endif
 
+const size_t kBrotliShellGaps[] = {132, 57, 23, 10, 4, 1};
+
 BROTLI_BOOL BrotliSetDepth(
     int p0, HuffmanTree* pool, uint8_t* depth, int max_depth) {
   int stack[16];
Index: brotlipy-0.6.0/libbrotli/enc/entropy_encode.h
===================================================================
--- brotlipy-0.6.0.orig/libbrotli/enc/entropy_encode.h
+++ brotlipy-0.6.0/libbrotli/enc/entropy_encode.h
@@ -76,12 +76,12 @@ BROTLI_INTERNAL void BrotliConvertBitDep
                                                      size_t len,
                                                      uint16_t *bits);
 
+BROTLI_INTERNAL extern const size_t kBrotliShellGaps[6];
 /* Input size optimized Shell sort. */
 typedef BROTLI_BOOL (*HuffmanTreeComparator)(
     const HuffmanTree*, const HuffmanTree*);
 static BROTLI_INLINE void SortHuffmanTreeItems(HuffmanTree* items,
     const size_t n, HuffmanTreeComparator comparator) {
-  static const size_t gaps[] = {132, 57, 23, 10, 4, 1};
   if (n < 13) {
     /* Insertion sort. */
     size_t i;
@@ -101,7 +101,7 @@ static BROTLI_INLINE void SortHuffmanTre
     /* Shell sort. */
     int g = n < 57 ? 2 : 0;
     for (; g < 6; ++g) {
-      size_t gap = gaps[g];
+      size_t gap = kBrotliShellGaps[g];
       size_t i;
       for (i = gap; i < n; ++i) {
         size_t j = i;
Index: brotlipy-0.6.0/libbrotli/enc/fast_log.c
===================================================================
--- /dev/null
+++ brotlipy-0.6.0/libbrotli/enc/fast_log.c
@@ -0,0 +1,105 @@
+/* Copyright 2013 Google Inc. All Rights Reserved.
+
+   Distributed under MIT license.
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
+*/
+
+#include "./fast_log.h"
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
+/* ", ".join(["%.16ff" % x for x in [0.0]+[log2(x) for x in range(1, 256)]]) */
+const double kBrotliLog2Table[BROTLI_LOG2_TABLE_SIZE] = {
+  0.0000000000000000f, 0.0000000000000000f, 1.0000000000000000f,
+  1.5849625007211563f, 2.0000000000000000f, 2.3219280948873622f,
+  2.5849625007211561f, 2.8073549220576042f, 3.0000000000000000f,
+  3.1699250014423126f, 3.3219280948873626f, 3.4594316186372978f,
+  3.5849625007211565f, 3.7004397181410922f, 3.8073549220576037f,
+  3.9068905956085187f, 4.0000000000000000f, 4.0874628412503400f,
+  4.1699250014423122f, 4.2479275134435852f, 4.3219280948873626f,
+  4.3923174227787607f, 4.4594316186372973f, 4.5235619560570131f,
+  4.5849625007211570f, 4.6438561897747244f, 4.7004397181410926f,
+  4.7548875021634691f, 4.8073549220576037f, 4.8579809951275728f,
+  4.9068905956085187f, 4.9541963103868758f, 5.0000000000000000f,
+  5.0443941193584534f, 5.0874628412503400f, 5.1292830169449664f,
+  5.1699250014423122f, 5.2094533656289501f, 5.2479275134435852f,
+  5.2854022188622487f, 5.3219280948873626f, 5.3575520046180838f,
+  5.3923174227787607f, 5.4262647547020979f, 5.4594316186372973f,
+  5.4918530963296748f, 5.5235619560570131f, 5.5545888516776376f,
+  5.5849625007211570f, 5.6147098441152083f, 5.6438561897747244f,
+  5.6724253419714961f, 5.7004397181410926f, 5.7279204545631996f,
+  5.7548875021634691f, 5.7813597135246599f, 5.8073549220576046f,
+  5.8328900141647422f, 5.8579809951275719f, 5.8826430493618416f,
+  5.9068905956085187f, 5.9307373375628867f, 5.9541963103868758f,
+  5.9772799234999168f, 6.0000000000000000f, 6.0223678130284544f,
+  6.0443941193584534f, 6.0660891904577721f, 6.0874628412503400f,
+  6.1085244567781700f, 6.1292830169449672f, 6.1497471195046822f,
+  6.1699250014423122f, 6.1898245588800176f, 6.2094533656289510f,
+  6.2288186904958804f, 6.2479275134435861f, 6.2667865406949019f,
+  6.2854022188622487f, 6.3037807481771031f, 6.3219280948873617f,
+  6.3398500028846252f, 6.3575520046180847f, 6.3750394313469254f,
+  6.3923174227787598f, 6.4093909361377026f, 6.4262647547020979f,
+  6.4429434958487288f, 6.4594316186372982f, 6.4757334309663976f,
+  6.4918530963296748f, 6.5077946401986964f, 6.5235619560570131f,
+  6.5391588111080319f, 6.5545888516776376f, 6.5698556083309478f,
+  6.5849625007211561f, 6.5999128421871278f, 6.6147098441152092f,
+  6.6293566200796095f, 6.6438561897747253f, 6.6582114827517955f,
+  6.6724253419714952f, 6.6865005271832185f, 6.7004397181410917f,
+  6.7142455176661224f, 6.7279204545631988f, 6.7414669864011465f,
+  6.7548875021634691f, 6.7681843247769260f, 6.7813597135246599f,
+  6.7944158663501062f, 6.8073549220576037f, 6.8201789624151887f,
+  6.8328900141647422f, 6.8454900509443757f, 6.8579809951275719f,
+  6.8703647195834048f, 6.8826430493618416f, 6.8948177633079437f,
+  6.9068905956085187f, 6.9188632372745955f, 6.9307373375628867f,
+  6.9425145053392399f, 6.9541963103868758f, 6.9657842846620879f,
+  6.9772799234999168f, 6.9886846867721664f, 7.0000000000000000f,
+  7.0112272554232540f, 7.0223678130284544f, 7.0334230015374501f,
+  7.0443941193584534f, 7.0552824355011898f, 7.0660891904577721f,
+  7.0768155970508317f, 7.0874628412503400f, 7.0980320829605272f,
+  7.1085244567781700f, 7.1189410727235076f, 7.1292830169449664f,
+  7.1395513523987937f, 7.1497471195046822f, 7.1598713367783891f,
+  7.1699250014423130f, 7.1799090900149345f, 7.1898245588800176f,
+  7.1996723448363644f, 7.2094533656289492f, 7.2191685204621621f,
+  7.2288186904958804f, 7.2384047393250794f, 7.2479275134435861f,
+  7.2573878426926521f, 7.2667865406949019f, 7.2761244052742384f,
+  7.2854022188622487f, 7.2946207488916270f, 7.3037807481771031f,
+  7.3128829552843557f, 7.3219280948873617f, 7.3309168781146177f,
+  7.3398500028846243f, 7.3487281542310781f, 7.3575520046180847f,
+  7.3663222142458151f, 7.3750394313469254f, 7.3837042924740528f,
+  7.3923174227787607f, 7.4008794362821844f, 7.4093909361377026f,
+  7.4178525148858991f, 7.4262647547020979f, 7.4346282276367255f,
+  7.4429434958487288f, 7.4512111118323299f, 7.4594316186372973f,
+  7.4676055500829976f, 7.4757334309663976f, 7.4838157772642564f,
+  7.4918530963296748f, 7.4998458870832057f, 7.5077946401986964f,
+  7.5156998382840436f, 7.5235619560570131f, 7.5313814605163119f,
+  7.5391588111080319f, 7.5468944598876373f, 7.5545888516776376f,
+  7.5622424242210728f, 7.5698556083309478f, 7.5774288280357487f,
+  7.5849625007211561f, 7.5924570372680806f, 7.5999128421871278f,
+  7.6073303137496113f, 7.6147098441152075f, 7.6220518194563764f,
+  7.6293566200796095f, 7.6366246205436488f, 7.6438561897747244f,
+  7.6510516911789290f, 7.6582114827517955f, 7.6653359171851765f,
+  7.6724253419714952f, 7.6794800995054464f, 7.6865005271832185f,
+  7.6934869574993252f, 7.7004397181410926f, 7.7073591320808825f,
+  7.7142455176661224f, 7.7210991887071856f, 7.7279204545631996f,
+  7.7347096202258392f, 7.7414669864011465f, 7.7481928495894596f,
+  7.7548875021634691f, 7.7615512324444795f, 7.7681843247769260f,
+  7.7747870596011737f, 7.7813597135246608f, 7.7879025593914317f,
+  7.7944158663501062f, 7.8008998999203047f, 7.8073549220576037f,
+  7.8137811912170374f, 7.8201789624151887f, 7.8265484872909159f,
+  7.8328900141647422f, 7.8392037880969445f, 7.8454900509443757f,
+  7.8517490414160571f, 7.8579809951275719f, 7.8641861446542798f,
+  7.8703647195834048f, 7.8765169465650002f, 7.8826430493618425f,
+  7.8887432488982601f, 7.8948177633079446f, 7.9008668079807496f,
+  7.9068905956085187f, 7.9128893362299619f, 7.9188632372745955f,
+  7.9248125036057813f, 7.9307373375628867f, 7.9366379390025719f,
+  7.9425145053392399f, 7.9483672315846778f, 7.9541963103868758f,
+  7.9600019320680806f, 7.9657842846620870f, 7.9715435539507720f,
+  7.9772799234999168f, 7.9829935746943104f, 7.9886846867721664f,
+  7.9943534368588578f
+};
+
+#if defined(__cplusplus) || defined(c_plusplus)
+}  /* extern "C" */
+#endif
Index: brotlipy-0.6.0/libbrotli/enc/fast_log.h
===================================================================
--- brotlipy-0.6.0.orig/libbrotli/enc/fast_log.h
+++ brotlipy-0.6.0/libbrotli/enc/fast_log.h
@@ -28,105 +28,18 @@ static BROTLI_INLINE uint32_t Log2FloorN
 #endif
 }
 
-/* A lookup table for small values of log2(int) to be used in entropy
-   computation.
+#define BROTLI_LOG2_TABLE_SIZE 256
 
-   ", ".join(["%.16ff" % x for x in [0.0]+[log2(x) for x in range(1, 256)]]) */
-static const float kLog2Table[] = {
-  0.0000000000000000f, 0.0000000000000000f, 1.0000000000000000f,
-  1.5849625007211563f, 2.0000000000000000f, 2.3219280948873622f,
-  2.5849625007211561f, 2.8073549220576042f, 3.0000000000000000f,
-  3.1699250014423126f, 3.3219280948873626f, 3.4594316186372978f,
-  3.5849625007211565f, 3.7004397181410922f, 3.8073549220576037f,
-  3.9068905956085187f, 4.0000000000000000f, 4.0874628412503400f,
-  4.1699250014423122f, 4.2479275134435852f, 4.3219280948873626f,
-  4.3923174227787607f, 4.4594316186372973f, 4.5235619560570131f,
-  4.5849625007211570f, 4.6438561897747244f, 4.7004397181410926f,
-  4.7548875021634691f, 4.8073549220576037f, 4.8579809951275728f,
-  4.9068905956085187f, 4.9541963103868758f, 5.0000000000000000f,
-  5.0443941193584534f, 5.0874628412503400f, 5.1292830169449664f,
-  5.1699250014423122f, 5.2094533656289501f, 5.2479275134435852f,
-  5.2854022188622487f, 5.3219280948873626f, 5.3575520046180838f,
-  5.3923174227787607f, 5.4262647547020979f, 5.4594316186372973f,
-  5.4918530963296748f, 5.5235619560570131f, 5.5545888516776376f,
-  5.5849625007211570f, 5.6147098441152083f, 5.6438561897747244f,
-  5.6724253419714961f, 5.7004397181410926f, 5.7279204545631996f,
-  5.7548875021634691f, 5.7813597135246599f, 5.8073549220576046f,
-  5.8328900141647422f, 5.8579809951275719f, 5.8826430493618416f,
-  5.9068905956085187f, 5.9307373375628867f, 5.9541963103868758f,
-  5.9772799234999168f, 6.0000000000000000f, 6.0223678130284544f,
-  6.0443941193584534f, 6.0660891904577721f, 6.0874628412503400f,
-  6.1085244567781700f, 6.1292830169449672f, 6.1497471195046822f,
-  6.1699250014423122f, 6.1898245588800176f, 6.2094533656289510f,
-  6.2288186904958804f, 6.2479275134435861f, 6.2667865406949019f,
-  6.2854022188622487f, 6.3037807481771031f, 6.3219280948873617f,
-  6.3398500028846252f, 6.3575520046180847f, 6.3750394313469254f,
-  6.3923174227787598f, 6.4093909361377026f, 6.4262647547020979f,
-  6.4429434958487288f, 6.4594316186372982f, 6.4757334309663976f,
-  6.4918530963296748f, 6.5077946401986964f, 6.5235619560570131f,
-  6.5391588111080319f, 6.5545888516776376f, 6.5698556083309478f,
-  6.5849625007211561f, 6.5999128421871278f, 6.6147098441152092f,
-  6.6293566200796095f, 6.6438561897747253f, 6.6582114827517955f,
-  6.6724253419714952f, 6.6865005271832185f, 6.7004397181410917f,
-  6.7142455176661224f, 6.7279204545631988f, 6.7414669864011465f,
-  6.7548875021634691f, 6.7681843247769260f, 6.7813597135246599f,
-  6.7944158663501062f, 6.8073549220576037f, 6.8201789624151887f,
-  6.8328900141647422f, 6.8454900509443757f, 6.8579809951275719f,
-  6.8703647195834048f, 6.8826430493618416f, 6.8948177633079437f,
-  6.9068905956085187f, 6.9188632372745955f, 6.9307373375628867f,
-  6.9425145053392399f, 6.9541963103868758f, 6.9657842846620879f,
-  6.9772799234999168f, 6.9886846867721664f, 7.0000000000000000f,
-  7.0112272554232540f, 7.0223678130284544f, 7.0334230015374501f,
-  7.0443941193584534f, 7.0552824355011898f, 7.0660891904577721f,
-  7.0768155970508317f, 7.0874628412503400f, 7.0980320829605272f,
-  7.1085244567781700f, 7.1189410727235076f, 7.1292830169449664f,
-  7.1395513523987937f, 7.1497471195046822f, 7.1598713367783891f,
-  7.1699250014423130f, 7.1799090900149345f, 7.1898245588800176f,
-  7.1996723448363644f, 7.2094533656289492f, 7.2191685204621621f,
-  7.2288186904958804f, 7.2384047393250794f, 7.2479275134435861f,
-  7.2573878426926521f, 7.2667865406949019f, 7.2761244052742384f,
-  7.2854022188622487f, 7.2946207488916270f, 7.3037807481771031f,
-  7.3128829552843557f, 7.3219280948873617f, 7.3309168781146177f,
-  7.3398500028846243f, 7.3487281542310781f, 7.3575520046180847f,
-  7.3663222142458151f, 7.3750394313469254f, 7.3837042924740528f,
-  7.3923174227787607f, 7.4008794362821844f, 7.4093909361377026f,
-  7.4178525148858991f, 7.4262647547020979f, 7.4346282276367255f,
-  7.4429434958487288f, 7.4512111118323299f, 7.4594316186372973f,
-  7.4676055500829976f, 7.4757334309663976f, 7.4838157772642564f,
-  7.4918530963296748f, 7.4998458870832057f, 7.5077946401986964f,
-  7.5156998382840436f, 7.5235619560570131f, 7.5313814605163119f,
-  7.5391588111080319f, 7.5468944598876373f, 7.5545888516776376f,
-  7.5622424242210728f, 7.5698556083309478f, 7.5774288280357487f,
-  7.5849625007211561f, 7.5924570372680806f, 7.5999128421871278f,
-  7.6073303137496113f, 7.6147098441152075f, 7.6220518194563764f,
-  7.6293566200796095f, 7.6366246205436488f, 7.6438561897747244f,
-  7.6510516911789290f, 7.6582114827517955f, 7.6653359171851765f,
-  7.6724253419714952f, 7.6794800995054464f, 7.6865005271832185f,
-  7.6934869574993252f, 7.7004397181410926f, 7.7073591320808825f,
-  7.7142455176661224f, 7.7210991887071856f, 7.7279204545631996f,
-  7.7347096202258392f, 7.7414669864011465f, 7.7481928495894596f,
-  7.7548875021634691f, 7.7615512324444795f, 7.7681843247769260f,
-  7.7747870596011737f, 7.7813597135246608f, 7.7879025593914317f,
-  7.7944158663501062f, 7.8008998999203047f, 7.8073549220576037f,
-  7.8137811912170374f, 7.8201789624151887f, 7.8265484872909159f,
-  7.8328900141647422f, 7.8392037880969445f, 7.8454900509443757f,
-  7.8517490414160571f, 7.8579809951275719f, 7.8641861446542798f,
-  7.8703647195834048f, 7.8765169465650002f, 7.8826430493618425f,
-  7.8887432488982601f, 7.8948177633079446f, 7.9008668079807496f,
-  7.9068905956085187f, 7.9128893362299619f, 7.9188632372745955f,
-  7.9248125036057813f, 7.9307373375628867f, 7.9366379390025719f,
-  7.9425145053392399f, 7.9483672315846778f, 7.9541963103868758f,
-  7.9600019320680806f, 7.9657842846620870f, 7.9715435539507720f,
-  7.9772799234999168f, 7.9829935746943104f, 7.9886846867721664f,
-  7.9943534368588578f
-};
+/* A lookup table for small values of log2(int) to be used in entropy
+   computation. */
+BROTLI_INTERNAL extern const double kBrotliLog2Table[BROTLI_LOG2_TABLE_SIZE];
 
 #define LOG_2_INV 1.4426950408889634
 
 /* Faster logarithm for small integers, with the property of log2(0) == 0. */
 static BROTLI_INLINE double FastLog2(size_t v) {
-  if (v < sizeof(kLog2Table) / sizeof(kLog2Table[0])) {
-    return kLog2Table[v];
+  if (v < BROTLI_LOG2_TABLE_SIZE) {
+    return kBrotliLog2Table[v];
   }
 #if (defined(_MSC_VER) && _MSC_VER <= 1700) || \
     (defined(__ANDROID_API__) && __ANDROID_API__ < 18)
Index: brotlipy-0.6.0/setup.py
===================================================================
--- brotlipy-0.6.0.orig/setup.py
+++ brotlipy-0.6.0/setup.py
@@ -45,6 +45,8 @@ setup(
             ],
             "sources": [
                 "libbrotli/common/dictionary.c",
+                "libbrotli/common/constants.c",
+                "libbrotli/common/platform.c",
                 "libbrotli/dec/bit_reader.c",
                 "libbrotli/dec/decode.c",
                 "libbrotli/dec/huffman.c",
@@ -67,6 +69,8 @@ setup(
                 "libbrotli/enc/compressor.cc",
                 "libbrotli/enc/encode_parallel.cc",
                 "libbrotli/enc/streams.cc",
+                "libbrotli/enc/command.c",
+                "libbrotli/enc/fast_log.c",
             ],
         }),
     ],
