Blame SOURCES/rhbz1881156.patch

944952
From ce3b0936620b9abb2d6d21d3e608f37f3c7302d9 Mon Sep 17 00:00:00 2001
944952
From: rpm-build <rpm-build>
944952
Date: Thu, 1 Oct 2020 10:34:27 +0200
944952
Subject: [PATCH] rhbz1881156.patch
944952
944952
---
944952
 c/common/constants.c      |  15 +
944952
 c/common/constants.h      | 136 ++++++++
944952
 c/common/platform.h       |   8 +-
944952
 c/dec/bit_reader.c        |  28 ++
944952
 c/dec/bit_reader.h        |  74 +++-
944952
 c/dec/decode.c            | 695 ++++++++++++++++++++++----------------
944952
 c/dec/huffman.h           |  71 +++-
944952
 c/dec/prefix.h            |  18 -
944952
 c/dec/state.c             |  23 +-
944952
 c/dec/state.h             | 181 ++++++++--
944952
 c/enc/brotli_bit_stream.c |  21 +-
944952
 research/brotli_decoder.c |   1 +
944952
 scripts/sources.lst       |   1 +
944952
 setup.py                  |   1 +
944952
 14 files changed, 865 insertions(+), 408 deletions(-)
944952
 create mode 100644 c/common/constants.c
944952
944952
diff --git a/c/common/constants.c b/c/common/constants.c
944952
new file mode 100644
944952
index 0000000..6bad9f6
944952
--- /dev/null
944952
+++ b/c/common/constants.c
944952
@@ -0,0 +1,15 @@
944952
+/* Copyright 2013 Google Inc. All Rights Reserved.
944952
+
944952
+   Distributed under MIT license.
944952
+   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
944952
+*/
944952
+
944952
+#include "./constants.h"
944952
+
944952
+const BrotliPrefixCodeRange
944952
+    _kBrotliPrefixCodeRanges[BROTLI_NUM_BLOCK_LEN_SYMBOLS] = {
944952
+        {1, 2},     {5, 2},     {9, 2},   {13, 2},    {17, 3},    {25, 3},
944952
+        {33, 3},    {41, 3},    {49, 4},  {65, 4},    {81, 4},    {97, 4},
944952
+        {113, 5},   {145, 5},   {177, 5}, {209, 5},   {241, 6},   {305, 6},
944952
+        {369, 7},   {497, 8},   {753, 9}, {1265, 10}, {2289, 11}, {4337, 12},
944952
+        {8433, 13}, {16625, 24}};
944952
diff --git a/c/common/constants.h b/c/common/constants.h
944952
index d1b88d1..e848195 100644
944952
--- a/c/common/constants.h
944952
+++ b/c/common/constants.h
944952
@@ -4,9 +4,18 @@
944952
    See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
944952
 */
944952
 
944952
+/**
944952
+ * @file
944952
+ * Common constants used in decoder and encoder API.
944952
+ */
944952
+
944952
 #ifndef BROTLI_COMMON_CONSTANTS_H_
944952
 #define BROTLI_COMMON_CONSTANTS_H_
944952
 
944952
+#include "./platform.h"
944952
+#include <brotli/port.h>
944952
+#include <brotli/types.h>
944952
+
944952
 /* Specification: 7.3. Encoding of the context map */
944952
 #define BROTLI_CONTEXT_MAP_MAX_RLE 16
944952
 
944952
@@ -29,12 +38,31 @@
944952
 #define BROTLI_INITIAL_REPEATED_CODE_LENGTH 8
944952
 
944952
 /* "Large Window Brotli" */
944952
+
944952
+/**
944952
+ * The theoretical maximum number of distance bits specified for large window
944952
+ * brotli, for 64-bit encoders and decoders. Even when in practice 32-bit
944952
+ * encoders and decoders only support up to 30 max distance bits, the value is
944952
+ * set to 62 because it affects the large window brotli file format.
944952
+ * Specifically, it affects the encoding of simple huffman tree for distances,
944952
+ * see Specification RFC 7932 chapter 3.4.
944952
+ */
944952
 #define BROTLI_LARGE_MAX_DISTANCE_BITS 62U
944952
 #define BROTLI_LARGE_MIN_WBITS 10
944952
+/**
944952
+ * The maximum supported large brotli window bits by the encoder and decoder.
944952
+ * Large window brotli allows up to 62 bits, however the current encoder and
944952
+ * decoder, designed for 32-bit integers, only support up to 30 bits maximum.
944952
+ */
944952
 #define BROTLI_LARGE_MAX_WBITS 30
944952
 
944952
 /* Specification: 4. Encoding of distances */
944952
 #define BROTLI_NUM_DISTANCE_SHORT_CODES 16
944952
+/**
944952
+ * Maximal number of "postfix" bits.
944952
+ *
944952
+ * Number of "postfix" bits is stored as 2 bits in meta-block header.
944952
+ */
944952
 #define BROTLI_MAX_NPOSTFIX 3
944952
 #define BROTLI_MAX_NDIRECT 120
944952
 #define BROTLI_MAX_DISTANCE_BITS 24U
944952
@@ -45,9 +73,22 @@
944952
 #define BROTLI_NUM_DISTANCE_SYMBOLS \
944952
     BROTLI_DISTANCE_ALPHABET_SIZE(  \
944952
         BROTLI_MAX_NDIRECT, BROTLI_MAX_NPOSTFIX, BROTLI_LARGE_MAX_DISTANCE_BITS)
944952
+
944952
+/* ((1 << 26) - 4) is the maximal distance that can be expressed in RFC 7932
944952
+   brotli stream using NPOSTFIX = 0 and NDIRECT = 0. With other NPOSTFIX and
944952
+   NDIRECT values distances up to ((1 << 29) + 88) could be expressed. */
944952
 #define BROTLI_MAX_DISTANCE 0x3FFFFFC
944952
+
944952
+/* ((1 << 31) - 4) is the safe distance limit. Using this number as a limit
944952
+   allows safe distance calculation without overflows, given the distance
944952
+   alphabet size is limited to corresponding size
944952
+   (see kLargeWindowDistanceCodeLimits). */
944952
 #define BROTLI_MAX_ALLOWED_DISTANCE 0x7FFFFFFC
944952
 
944952
+
944952
+/* Specification: 4. Encoding of Literal Insertion Lengths and Copy Lengths */
944952
+#define BROTLI_NUM_INS_COPY_CODES 24
944952
+
944952
 /* 7.1. Context modes and context ID lookup for literals */
944952
 /* "context IDs for literals are in the range of 0..63" */
944952
 #define BROTLI_LITERAL_CONTEXT_BITS 6
944952
@@ -61,4 +102,99 @@
944952
 #define BROTLI_WINDOW_GAP 16
944952
 #define BROTLI_MAX_BACKWARD_LIMIT(W) (((size_t)1 << (W)) - BROTLI_WINDOW_GAP)
944952
 
944952
+typedef struct BrotliDistanceCodeLimit {
944952
+  uint32_t max_alphabet_size;
944952
+  uint32_t max_distance;
944952
+} BrotliDistanceCodeLimit;
944952
+
944952
+/* This function calculates maximal size of distance alphabet, such that the
944952
+   distances greater than the given values can not be represented.
944952
+
944952
+   This limits are designed to support fast and safe 32-bit decoders.
944952
+   "32-bit" means that signed integer values up to ((1 << 31) - 1) could be
944952
+   safely expressed.
944952
+
944952
+   Brotli distance alphabet symbols do not represent consecutive distance
944952
+   ranges. Each distance alphabet symbol (excluding direct distances and short
944952
+   codes), represent interleaved (for NPOSTFIX > 0) range of distances.
944952
+   A "group" of consecutive (1 << NPOSTFIX) symbols represent non-interleaved
944952
+   range. Two consecutive groups require the same amount of "extra bits".
944952
+
944952
+   It is important that distance alphabet represents complete "groups".
944952
+   To avoid complex logic on encoder side about interleaved ranges
944952
+   it was decided to restrict both sides to complete distance code "groups".
944952
+ */
944952
+BROTLI_UNUSED_FUNCTION BrotliDistanceCodeLimit BrotliCalculateDistanceCodeLimit(
944952
+    uint32_t max_distance, uint32_t npostfix, uint32_t ndirect) {
944952
+  BrotliDistanceCodeLimit result;
944952
+  /* Marking this function as unused, because not all files
944952
+     including "constants.h" use it -> compiler warns about that. */
944952
+  BROTLI_UNUSED(&BrotliCalculateDistanceCodeLimit);
944952
+  if (max_distance <= ndirect) {
944952
+    /* This case never happens / exists only for the sake of completeness. */
944952
+    result.max_alphabet_size = max_distance + BROTLI_NUM_DISTANCE_SHORT_CODES;
944952
+    result.max_distance = max_distance;
944952
+    return result;
944952
+  } else {
944952
+    /* The first prohibited value. */
944952
+    uint32_t forbidden_distance = max_distance + 1;
944952
+    /* Subtract "directly" encoded region. */
944952
+    uint32_t offset = forbidden_distance - ndirect - 1;
944952
+    uint32_t ndistbits = 0;
944952
+    uint32_t tmp;
944952
+    uint32_t half;
944952
+    uint32_t group;
944952
+    /* Postfix for the last dcode in the group. */
944952
+    uint32_t postfix = (1u << npostfix) - 1;
944952
+    uint32_t extra;
944952
+    uint32_t start;
944952
+    /* Remove postfix and "head-start". */
944952
+    offset = (offset >> npostfix) + 4;
944952
+    /* Calculate the number of distance bits. */
944952
+    tmp = offset / 2;
944952
+    /* Poor-man's log2floor, to avoid extra dependencies. */
944952
+    while (tmp != 0) {ndistbits++; tmp = tmp >> 1;}
944952
+    /* One bit is covered with subrange addressing ("half"). */
944952
+    ndistbits--;
944952
+    /* Find subrange. */
944952
+    half = (offset >> ndistbits) & 1;
944952
+    /* Calculate the "group" part of dcode. */
944952
+    group = ((ndistbits - 1) << 1) | half;
944952
+    /* Calculated "group" covers the prohibited distance value. */
944952
+    if (group == 0) {
944952
+      /* This case is added for correctness; does not occur for limit > 128. */
944952
+      result.max_alphabet_size = ndirect + BROTLI_NUM_DISTANCE_SHORT_CODES;
944952
+      result.max_distance = ndirect;
944952
+      return result;
944952
+    }
944952
+    /* Decrement "group", so it is the last permitted "group". */
944952
+    group--;
944952
+    /* After group was decremented, ndistbits and half must be recalculated. */
944952
+    ndistbits = (group >> 1) + 1;
944952
+    /* The last available distance in the subrange has all extra bits set. */
944952
+    extra = (1u << ndistbits) - 1;
944952
+    /* Calculate region start. NB: ndistbits >= 1. */
944952
+    start = (1u << (ndistbits + 1)) - 4;
944952
+    /* Move to subregion. */
944952
+    start += (group & 1) << ndistbits;
944952
+    /* Calculate the alphabet size. */
944952
+    result.max_alphabet_size = ((group << npostfix) | postfix) + ndirect +
944952
+        BROTLI_NUM_DISTANCE_SHORT_CODES + 1;
944952
+    /* Calculate the maximal distance representable by alphabet. */
944952
+    result.max_distance = ((start + extra) << npostfix) + postfix + ndirect + 1;
944952
+    return result;
944952
+  }
944952
+}
944952
+
944952
+/* Represents the range of values belonging to a prefix code:
944952
+   [offset, offset + 2^nbits) */
944952
+typedef struct {
944952
+  uint16_t offset;
944952
+  uint8_t nbits;
944952
+} BrotliPrefixCodeRange;
944952
+
944952
+/* "Soft-private", it is exported, but not "advertised" as API. */
944952
+BROTLI_COMMON_API extern const BrotliPrefixCodeRange
944952
+    _kBrotliPrefixCodeRanges[BROTLI_NUM_BLOCK_LEN_SYMBOLS];
944952
+
944952
 #endif  /* BROTLI_COMMON_CONSTANTS_H_ */
944952
diff --git a/c/common/platform.h b/c/common/platform.h
944952
index 0d84b19..94bf773 100755
944952
--- a/c/common/platform.h
944952
+++ b/c/common/platform.h
944952
@@ -197,6 +197,10 @@ OR:
944952
 
944952
 #endif  /* ARMv8 */
944952
 
944952
+#if defined(__ARM_NEON__) || defined(__ARM_NEON)
944952
+#define BROTLI_TARGET_NEON
944952
+#endif
944952
+
944952
 #if defined(__i386) || defined(_M_IX86)
944952
 #define BROTLI_TARGET_X86
944952
 #endif
944952
@@ -456,20 +460,20 @@ static BROTLI_INLINE void BROTLI_UNALIGNED_STORE64LE(void* p, uint64_t v) {
944952
 #endif
944952
 
944952
 #if defined(BROTLI_ENABLE_LOG)
944952
-#define BROTLI_DCHECK(x) assert(x)
944952
 #define BROTLI_LOG(x) printf x
944952
 #else
944952
-#define BROTLI_DCHECK(x)
944952
 #define BROTLI_LOG(x)
944952
 #endif
944952
 
944952
 #if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG)
944952
+#define BROTLI_DCHECK(x) assert(x)
944952
 static BROTLI_INLINE void BrotliDump(const char* f, int l, const char* fn) {
944952
   fprintf(stderr, "%s:%d (%s)\n", f, l, fn);
944952
   fflush(stderr);
944952
 }
944952
 #define BROTLI_DUMP() BrotliDump(__FILE__, __LINE__, __FUNCTION__)
944952
 #else
944952
+#define BROTLI_DCHECK(x)
944952
 #define BROTLI_DUMP() (void)(0)
944952
 #endif
944952
 
944952
diff --git a/c/dec/bit_reader.c b/c/dec/bit_reader.c
944952
index 722fd90..7f7b256 100644
944952
--- a/c/dec/bit_reader.c
944952
+++ b/c/dec/bit_reader.c
944952
@@ -15,6 +15,17 @@
944952
 extern "C" {
944952
 #endif
944952
 
944952
+const uint32_t kBrotliBitMask[33] = {   0x00000000,
944952
+    0x00000001, 0x00000003, 0x00000007, 0x0000000F,
944952
+    0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
944952
+    0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
944952
+    0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
944952
+    0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
944952
+    0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
944952
+    0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
944952
+    0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
944952
+};
944952
+
944952
 void BrotliInitBitReader(BrotliBitReader* const br) {
944952
   br->val_ = 0;
944952
   br->bit_pos_ = sizeof(br->val_) << 3;
944952
@@ -43,6 +54,23 @@ BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br) {
944952
   return BROTLI_TRUE;
944952
 }
944952
 
944952
+BROTLI_BOOL BrotliSafeReadBits32Slow(BrotliBitReader* const br,
944952
+    uint32_t n_bits, uint32_t* val) {
944952
+  uint32_t low_val;
944952
+  uint32_t high_val;
944952
+  BrotliBitReaderState memento;
944952
+  BROTLI_DCHECK(n_bits <= 32);
944952
+  BROTLI_DCHECK(n_bits > 24);
944952
+  BrotliBitReaderSaveState(br, &memento);
944952
+  if (!BrotliSafeReadBits(br, 16, &low_val) ||
944952
+      !BrotliSafeReadBits(br, n_bits - 16, &high_val)) {
944952
+    BrotliBitReaderRestoreState(br, &memento);
944952
+    return BROTLI_FALSE;
944952
+  }
944952
+  *val = low_val | (high_val << 16);
944952
+  return BROTLI_TRUE;
944952
+}
944952
+
944952
 #if defined(__cplusplus) || defined(c_plusplus)
944952
 }  /* extern "C" */
944952
 #endif
944952
diff --git a/c/dec/bit_reader.h b/c/dec/bit_reader.h
944952
index c06e914..22bc060 100644
944952
--- a/c/dec/bit_reader.h
944952
+++ b/c/dec/bit_reader.h
944952
@@ -11,6 +11,7 @@
944952
 
944952
 #include <string.h>  /* memcpy */
944952
 
944952
+#include "../common/constants.h"
944952
 #include "../common/platform.h"
944952
 #include <brotli/types.h>
944952
 
944952
@@ -20,16 +21,7 @@ extern "C" {
944952
 
944952
 #define BROTLI_SHORT_FILL_BIT_WINDOW_READ (sizeof(brotli_reg_t) >> 1)
944952
 
944952
-static const uint32_t kBitMask[33] = {  0x00000000,
944952
-    0x00000001, 0x00000003, 0x00000007, 0x0000000F,
944952
-    0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
944952
-    0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
944952
-    0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
944952
-    0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
944952
-    0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
944952
-    0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
944952
-    0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
944952
-};
944952
+BROTLI_INTERNAL extern const uint32_t kBrotliBitMask[33];
944952
 
944952
 static BROTLI_INLINE uint32_t BitMask(uint32_t n) {
944952
   if (BROTLI_IS_CONSTANT(n) || BROTLI_HAS_UBFX) {
944952
@@ -37,7 +29,7 @@ static BROTLI_INLINE uint32_t BitMask(uint32_t n) {
944952
        "Unsigned Bit Field Extract" UBFX instruction on ARM. */
944952
     return ~((0xFFFFFFFFu) << n);
944952
   } else {
944952
-    return kBitMask[n];
944952
+    return kBrotliBitMask[n];
944952
   }
944952
 }
944952
 
944952
@@ -65,6 +57,12 @@ BROTLI_INTERNAL void BrotliInitBitReader(BrotliBitReader* const br);
944952
    reading. */
944952
 BROTLI_INTERNAL BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br);
944952
 
944952
+/* Fallback for BrotliSafeReadBits32. Extracted as noninlined method to unburden
944952
+   the main code-path. Never called for RFC brotli streams, required only for
944952
+   "large-window" mode and other extensions. */
944952
+BROTLI_INTERNAL BROTLI_NOINLINE BROTLI_BOOL BrotliSafeReadBits32Slow(
944952
+    BrotliBitReader* const br, uint32_t n_bits, uint32_t* val);
944952
+
944952
 static BROTLI_INLINE void BrotliBitReaderSaveState(
944952
     BrotliBitReader* const from, BrotliBitReaderState* to) {
944952
   to->val_ = from->val_;
944952
@@ -87,8 +85,11 @@ static BROTLI_INLINE uint32_t BrotliGetAvailableBits(
944952
 }
944952
 
944952
 /* Returns amount of unread bytes the bit reader still has buffered from the
944952
-   BrotliInput, including whole bytes in br->val_. */
944952
+   BrotliInput, including whole bytes in br->val_. Result is capped with
944952
+   maximal ring-buffer size (larger number won't be utilized anyway). */
944952
 static BROTLI_INLINE size_t BrotliGetRemainingBytes(BrotliBitReader* br) {
944952
+  static const size_t kCap = (size_t)1 << BROTLI_LARGE_MAX_WBITS;
944952
+  if (br->avail_in > kCap) return kCap;
944952
   return br->avail_in + (BrotliGetAvailableBits(br) >> 3);
944952
 }
944952
 
944952
@@ -237,15 +238,17 @@ static BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) {
944952
 static BROTLI_INLINE void BrotliTakeBits(
944952
   BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
944952
   *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
944952
-  BROTLI_LOG(("[BrotliReadBits]  %d %d %d val: %6x\n",
944952
+  BROTLI_LOG(("[BrotliTakeBits]  %d %d %d val: %6x\n",
944952
       (int)br->avail_in, (int)br->bit_pos_, (int)n_bits, (int)*val));
944952
   BrotliDropBits(br, n_bits);
944952
 }
944952
 
944952
 /* Reads the specified number of bits from |br| and advances the bit pos.
944952
-   Assumes that there is enough input to perform BrotliFillBitWindow. */
944952
-static BROTLI_INLINE uint32_t BrotliReadBits(
944952
+   Assumes that there is enough input to perform BrotliFillBitWindow.
944952
+   Up to 24 bits are allowed to be requested from this method. */
944952
+static BROTLI_INLINE uint32_t BrotliReadBits24(
944952
     BrotliBitReader* const br, uint32_t n_bits) {
944952
+  BROTLI_DCHECK(n_bits <= 24);
944952
   if (BROTLI_64_BITS || (n_bits <= 16)) {
944952
     uint32_t val;
944952
     BrotliFillBitWindow(br, n_bits);
944952
@@ -262,10 +265,32 @@ static BROTLI_INLINE uint32_t BrotliReadBits(
944952
   }
944952
 }
944952
 
944952
+/* Same as BrotliReadBits24, but allows reading up to 32 bits. */
944952
+static BROTLI_INLINE uint32_t BrotliReadBits32(
944952
+    BrotliBitReader* const br, uint32_t n_bits) {
944952
+  BROTLI_DCHECK(n_bits <= 32);
944952
+  if (BROTLI_64_BITS || (n_bits <= 16)) {
944952
+    uint32_t val;
944952
+    BrotliFillBitWindow(br, n_bits);
944952
+    BrotliTakeBits(br, n_bits, &val;;
944952
+    return val;
944952
+  } else {
944952
+    uint32_t low_val;
944952
+    uint32_t high_val;
944952
+    BrotliFillBitWindow(br, 16);
944952
+    BrotliTakeBits(br, 16, &low_val);
944952
+    BrotliFillBitWindow(br, 16);
944952
+    BrotliTakeBits(br, n_bits - 16, &high_val);
944952
+    return low_val | (high_val << 16);
944952
+  }
944952
+}
944952
+
944952
 /* Tries to read the specified amount of bits. Returns BROTLI_FALSE, if there
944952
-   is not enough input. |n_bits| MUST be positive. */
944952
+   is not enough input. |n_bits| MUST be positive.
944952
+   Up to 24 bits are allowed to be requested from this method. */
944952
 static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits(
944952
     BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
944952
+  BROTLI_DCHECK(n_bits <= 24);
944952
   while (BrotliGetAvailableBits(br) < n_bits) {
944952
     if (!BrotliPullByte(br)) {
944952
       return BROTLI_FALSE;
944952
@@ -275,6 +300,23 @@ static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits(
944952
   return BROTLI_TRUE;
944952
 }
944952
 
944952
+/* Same as BrotliSafeReadBits, but allows reading up to 32 bits. */
944952
+static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits32(
944952
+    BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
944952
+  BROTLI_DCHECK(n_bits <= 32);
944952
+  if (BROTLI_64_BITS || (n_bits <= 24)) {
944952
+    while (BrotliGetAvailableBits(br) < n_bits) {
944952
+      if (!BrotliPullByte(br)) {
944952
+        return BROTLI_FALSE;
944952
+      }
944952
+    }
944952
+    BrotliTakeBits(br, n_bits, val);
944952
+    return BROTLI_TRUE;
944952
+  } else {
944952
+    return BrotliSafeReadBits32Slow(br, n_bits, val);
944952
+  }
944952
+}
944952
+
944952
 /* Advances the bit reader position to the next byte boundary and verifies
944952
    that any skipped bits are set to zero. */
944952
 static BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) {
944952
diff --git a/c/dec/decode.c b/c/dec/decode.c
944952
index 674b829..ae5a3d3 100644
944952
--- a/c/dec/decode.c
944952
+++ b/c/dec/decode.c
944952
@@ -6,10 +6,6 @@
944952
 
944952
 #include <brotli/decode.h>
944952
 
944952
-#if defined(__ARM_NEON__)
944952
-#include <arm_neon.h>
944952
-#endif
944952
-
944952
 #include <stdlib.h>  /* free, malloc */
944952
 #include <string.h>  /* memcpy, memset */
944952
 
944952
@@ -24,6 +20,10 @@
944952
 #include "./prefix.h"
944952
 #include "./state.h"
944952
 
944952
+#if defined(BROTLI_TARGET_NEON)
944952
+#include <arm_neon.h>
944952
+#endif
944952
+
944952
 #if defined(__cplusplus) || defined(c_plusplus)
944952
 extern "C" {
944952
 #endif
944952
@@ -41,7 +41,8 @@ extern "C" {
944952
 
944952
 /* We need the slack region for the following reasons:
944952
     - doing up to two 16-byte copies for fast backward copying
944952
-    - inserting transformed dictionary word (5 prefix + 24 base + 8 suffix) */
944952
+    - inserting transformed dictionary word:
944952
+        5 prefix + 24 base + 8 suffix */
944952
 static const uint32_t kRingBufferWriteAheadSlack = 42;
944952
 
944952
 static const uint8_t kCodeLengthCodeOrder[BROTLI_CODE_LENGTH_CODES] = {
944952
@@ -167,7 +168,7 @@ static BrotliDecoderErrorCode DecodeWindowBits(BrotliDecoderState* s,
944952
 }
944952
 
944952
 static BROTLI_INLINE void memmove16(uint8_t* dst, uint8_t* src) {
944952
-#if defined(__ARM_NEON__)
944952
+#if defined(BROTLI_TARGET_NEON)
944952
   vst1q_u8(dst, vld1q_u8(src));
944952
 #else
944952
   uint32_t buffer[4];
944952
@@ -274,7 +275,8 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
944952
             s->loop_counter = i;
944952
             return BROTLI_DECODER_NEEDS_MORE_INPUT;
944952
           }
944952
-          if (i + 1 == s->size_nibbles && s->size_nibbles > 4 && bits == 0) {
944952
+          if (i + 1 == (int)s->size_nibbles && s->size_nibbles > 4 &&
944952
+              bits == 0) {
944952
             return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE);
944952
           }
944952
           s->meta_block_remaining_len |= (int)(bits << (i * 4));
944952
@@ -323,7 +325,8 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
944952
             s->loop_counter = i;
944952
             return BROTLI_DECODER_NEEDS_MORE_INPUT;
944952
           }
944952
-          if (i + 1 == s->size_nibbles && s->size_nibbles > 1 && bits == 0) {
944952
+          if (i + 1 == (int)s->size_nibbles && s->size_nibbles > 1 &&
944952
+              bits == 0) {
944952
             return BROTLI_FAILURE(
944952
                 BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE);
944952
           }
944952
@@ -347,15 +350,17 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
944952
 static BROTLI_INLINE uint32_t DecodeSymbol(uint32_t bits,
944952
                                            const HuffmanCode* table,
944952
                                            BrotliBitReader* br) {
944952
-  table += bits & HUFFMAN_TABLE_MASK;
944952
-  if (table->bits > HUFFMAN_TABLE_BITS) {
944952
-    uint32_t nbits = table->bits - HUFFMAN_TABLE_BITS;
944952
+  BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table);
944952
+  BROTLI_HC_ADJUST_TABLE_INDEX(table, bits & HUFFMAN_TABLE_MASK);
944952
+  if (BROTLI_HC_FAST_LOAD_BITS(table) > HUFFMAN_TABLE_BITS) {
944952
+    uint32_t nbits = BROTLI_HC_FAST_LOAD_BITS(table) - HUFFMAN_TABLE_BITS;
944952
     BrotliDropBits(br, HUFFMAN_TABLE_BITS);
944952
-    table += table->value;
944952
-    table += (bits >> HUFFMAN_TABLE_BITS) & BitMask(nbits);
944952
+    BROTLI_HC_ADJUST_TABLE_INDEX(table,
944952
+        BROTLI_HC_FAST_LOAD_VALUE(table) +
944952
+        ((bits >> HUFFMAN_TABLE_BITS) & BitMask(nbits)));
944952
   }
944952
-  BrotliDropBits(br, table->bits);
944952
-  return table->value;
944952
+  BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(table));
944952
+  return BROTLI_HC_FAST_LOAD_VALUE(table);
944952
 }
944952
 
944952
 /* Reads and decodes the next Huffman code from bit-stream.
944952
@@ -371,19 +376,20 @@ static BROTLI_NOINLINE BROTLI_BOOL SafeDecodeSymbol(
944952
     const HuffmanCode* table, BrotliBitReader* br, uint32_t* result) {
944952
   uint32_t val;
944952
   uint32_t available_bits = BrotliGetAvailableBits(br);
944952
+  BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table);
944952
   if (available_bits == 0) {
944952
-    if (table->bits == 0) {
944952
-      *result = table->value;
944952
+    if (BROTLI_HC_FAST_LOAD_BITS(table) == 0) {
944952
+      *result = BROTLI_HC_FAST_LOAD_VALUE(table);
944952
       return BROTLI_TRUE;
944952
     }
944952
     return BROTLI_FALSE;  /* No valid bits at all. */
944952
   }
944952
   val = (uint32_t)BrotliGetBitsUnmasked(br);
944952
-  table += val & HUFFMAN_TABLE_MASK;
944952
-  if (table->bits <= HUFFMAN_TABLE_BITS) {
944952
-    if (table->bits <= available_bits) {
944952
-      BrotliDropBits(br, table->bits);
944952
-      *result = table->value;
944952
+  BROTLI_HC_ADJUST_TABLE_INDEX(table, val & HUFFMAN_TABLE_MASK);
944952
+  if (BROTLI_HC_FAST_LOAD_BITS(table) <= HUFFMAN_TABLE_BITS) {
944952
+    if (BROTLI_HC_FAST_LOAD_BITS(table) <= available_bits) {
944952
+      BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(table));
944952
+      *result = BROTLI_HC_FAST_LOAD_VALUE(table);
944952
       return BROTLI_TRUE;
944952
     } else {
944952
       return BROTLI_FALSE;  /* Not enough bits for the first level. */
944952
@@ -394,15 +400,15 @@ static BROTLI_NOINLINE BROTLI_BOOL SafeDecodeSymbol(
944952
   }
944952
 
944952
   /* Speculatively drop HUFFMAN_TABLE_BITS. */
944952
-  val = (val & BitMask(table->bits)) >> HUFFMAN_TABLE_BITS;
944952
+  val = (val & BitMask(BROTLI_HC_FAST_LOAD_BITS(table))) >> HUFFMAN_TABLE_BITS;
944952
   available_bits -= HUFFMAN_TABLE_BITS;
944952
-  table += table->value + val;
944952
-  if (available_bits < table->bits) {
944952
+  BROTLI_HC_ADJUST_TABLE_INDEX(table, BROTLI_HC_FAST_LOAD_VALUE(table) + val);
944952
+  if (available_bits < BROTLI_HC_FAST_LOAD_BITS(table)) {
944952
     return BROTLI_FALSE;  /* Not enough bits for the second level. */
944952
   }
944952
 
944952
-  BrotliDropBits(br, HUFFMAN_TABLE_BITS + table->bits);
944952
-  *result = table->value;
944952
+  BrotliDropBits(br, HUFFMAN_TABLE_BITS + BROTLI_HC_FAST_LOAD_BITS(table));
944952
+  *result = BROTLI_HC_FAST_LOAD_VALUE(table);
944952
   return BROTLI_TRUE;
944952
 }
944952
 
944952
@@ -425,9 +431,10 @@ static BROTLI_INLINE void PreloadSymbol(int safe,
944952
   if (safe) {
944952
     return;
944952
   }
944952
-  table += BrotliGetBits(br, HUFFMAN_TABLE_BITS);
944952
-  *bits = table->bits;
944952
-  *value = table->value;
944952
+  BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table);
944952
+  BROTLI_HC_ADJUST_TABLE_INDEX(table, BrotliGetBits(br, HUFFMAN_TABLE_BITS));
944952
+  *bits = BROTLI_HC_FAST_LOAD_BITS(table);
944952
+  *value = BROTLI_HC_FAST_LOAD_VALUE(table);
944952
 }
944952
 
944952
 /* Decodes the next Huffman code using data prepared by PreloadSymbol.
944952
@@ -441,10 +448,11 @@ static BROTLI_INLINE uint32_t ReadPreloadedSymbol(const HuffmanCode* table,
944952
     uint32_t val = BrotliGet16BitsUnmasked(br);
944952
     const HuffmanCode* ext = table + (val & HUFFMAN_TABLE_MASK) + *value;
944952
     uint32_t mask = BitMask((*bits - HUFFMAN_TABLE_BITS));
944952
+    BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(ext);
944952
     BrotliDropBits(br, HUFFMAN_TABLE_BITS);
944952
-    ext += (val >> HUFFMAN_TABLE_BITS) & mask;
944952
-    BrotliDropBits(br, ext->bits);
944952
-    result = ext->value;
944952
+    BROTLI_HC_ADJUST_TABLE_INDEX(ext, (val >> HUFFMAN_TABLE_BITS) & mask);
944952
+    BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(ext));
944952
+    result = BROTLI_HC_FAST_LOAD_VALUE(ext);
944952
   } else {
944952
     BrotliDropBits(br, *bits);
944952
   }
944952
@@ -465,32 +473,34 @@ static BROTLI_INLINE uint32_t Log2Floor(uint32_t x) {
944952
    Totally 1..4 symbols are read, 1..11 bits each.
944952
    The list of symbols MUST NOT contain duplicates. */
944952
 static BrotliDecoderErrorCode ReadSimpleHuffmanSymbols(
944952
-    uint32_t alphabet_size, uint32_t max_symbol, BrotliDecoderState* s) {
944952
+    uint32_t alphabet_size_max, uint32_t alphabet_size_limit,
944952
+    BrotliDecoderState* s) {
944952
   /* max_bits == 1..11; symbol == 0..3; 1..44 bits will be read. */
944952
   BrotliBitReader* br = &s->br;
944952
-  uint32_t max_bits = Log2Floor(alphabet_size - 1);
944952
-  uint32_t i = s->sub_loop_counter;
944952
-  uint32_t num_symbols = s->symbol;
944952
+  BrotliMetablockHeaderArena* h = &s->arena.header;
944952
+  uint32_t max_bits = Log2Floor(alphabet_size_max - 1);
944952
+  uint32_t i = h->sub_loop_counter;
944952
+  uint32_t num_symbols = h->symbol;
944952
   while (i <= num_symbols) {
944952
     uint32_t v;
944952
     if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, max_bits, &v))) {
944952
-      s->sub_loop_counter = i;
944952
-      s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_READ;
944952
+      h->sub_loop_counter = i;
944952
+      h->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_READ;
944952
       return BROTLI_DECODER_NEEDS_MORE_INPUT;
944952
     }
944952
-    if (v >= max_symbol) {
944952
+    if (v >= alphabet_size_limit) {
944952
       return
944952
           BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET);
944952
     }
944952
-    s->symbols_lists_array[i] = (uint16_t)v;
944952
-    BROTLI_LOG_UINT(s->symbols_lists_array[i]);
944952
+    h->symbols_lists_array[i] = (uint16_t)v;
944952
+    BROTLI_LOG_UINT(h->symbols_lists_array[i]);
944952
     ++i;
944952
   }
944952
 
944952
   for (i = 0; i < num_symbols; ++i) {
944952
     uint32_t k = i + 1;
944952
     for (; k <= num_symbols; ++k) {
944952
-      if (s->symbols_lists_array[i] == s->symbols_lists_array[k]) {
944952
+      if (h->symbols_lists_array[i] == h->symbols_lists_array[k]) {
944952
         return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME);
944952
       }
944952
     }
944952
@@ -583,33 +593,35 @@ static BROTLI_INLINE void ProcessRepeatedCodeLength(uint32_t code_len,
944952
 static BrotliDecoderErrorCode ReadSymbolCodeLengths(
944952
     uint32_t alphabet_size, BrotliDecoderState* s) {
944952
   BrotliBitReader* br = &s->br;
944952
-  uint32_t symbol = s->symbol;
944952
-  uint32_t repeat = s->repeat;
944952
-  uint32_t space = s->space;
944952
-  uint32_t prev_code_len = s->prev_code_len;
944952
-  uint32_t repeat_code_len = s->repeat_code_len;
944952
-  uint16_t* symbol_lists = s->symbol_lists;
944952
-  uint16_t* code_length_histo = s->code_length_histo;
944952
-  int* next_symbol = s->next_symbol;
944952
+  BrotliMetablockHeaderArena* h = &s->arena.header;
944952
+  uint32_t symbol = h->symbol;
944952
+  uint32_t repeat = h->repeat;
944952
+  uint32_t space = h->space;
944952
+  uint32_t prev_code_len = h->prev_code_len;
944952
+  uint32_t repeat_code_len = h->repeat_code_len;
944952
+  uint16_t* symbol_lists = h->symbol_lists;
944952
+  uint16_t* code_length_histo = h->code_length_histo;
944952
+  int* next_symbol = h->next_symbol;
944952
   if (!BrotliWarmupBitReader(br)) {
944952
     return BROTLI_DECODER_NEEDS_MORE_INPUT;
944952
   }
944952
   while (symbol < alphabet_size && space > 0) {
944952
-    const HuffmanCode* p = s->table;
944952
+    const HuffmanCode* p = h->table;
944952
     uint32_t code_len;
944952
+    BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(p);
944952
     if (!BrotliCheckInputAmount(br, BROTLI_SHORT_FILL_BIT_WINDOW_READ)) {
944952
-      s->symbol = symbol;
944952
-      s->repeat = repeat;
944952
-      s->prev_code_len = prev_code_len;
944952
-      s->repeat_code_len = repeat_code_len;
944952
-      s->space = space;
944952
+      h->symbol = symbol;
944952
+      h->repeat = repeat;
944952
+      h->prev_code_len = prev_code_len;
944952
+      h->repeat_code_len = repeat_code_len;
944952
+      h->space = space;
944952
       return BROTLI_DECODER_NEEDS_MORE_INPUT;
944952
     }
944952
     BrotliFillBitWindow16(br);
944952
-    p += BrotliGetBitsUnmasked(br) &
944952
-        BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH);
944952
-    BrotliDropBits(br, p->bits);  /* Use 1..5 bits. */
944952
-    code_len = p->value;  /* code_len == 0..17 */
944952
+    BROTLI_HC_ADJUST_TABLE_INDEX(p, BrotliGetBitsUnmasked(br) &
944952
+        BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH));
944952
+    BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(p));  /* Use 1..5 bits. */
944952
+    code_len = BROTLI_HC_FAST_LOAD_VALUE(p);  /* code_len == 0..17 */
944952
     if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) {
944952
       ProcessSingleCodeLength(code_len, &symbol, &repeat, &space,
944952
           &prev_code_len, symbol_lists, code_length_histo, next_symbol);
944952
@@ -624,48 +636,52 @@ static BrotliDecoderErrorCode ReadSymbolCodeLengths(
944952
           symbol_lists, code_length_histo, next_symbol);
944952
     }
944952
   }
944952
-  s->space = space;
944952
+  h->space = space;
944952
   return BROTLI_DECODER_SUCCESS;
944952
 }
944952
 
944952
 static BrotliDecoderErrorCode SafeReadSymbolCodeLengths(
944952
     uint32_t alphabet_size, BrotliDecoderState* s) {
944952
   BrotliBitReader* br = &s->br;
944952
+  BrotliMetablockHeaderArena* h = &s->arena.header;
944952
   BROTLI_BOOL get_byte = BROTLI_FALSE;
944952
-  while (s->symbol < alphabet_size && s->space > 0) {
944952
-    const HuffmanCode* p = s->table;
944952
+  while (h->symbol < alphabet_size && h->space > 0) {
944952
+    const HuffmanCode* p = h->table;
944952
     uint32_t code_len;
944952
     uint32_t available_bits;
944952
     uint32_t bits = 0;
944952
+    BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(p);
944952
     if (get_byte && !BrotliPullByte(br)) return BROTLI_DECODER_NEEDS_MORE_INPUT;
944952
     get_byte = BROTLI_FALSE;
944952
     available_bits = BrotliGetAvailableBits(br);
944952
     if (available_bits != 0) {
944952
       bits = (uint32_t)BrotliGetBitsUnmasked(br);
944952
     }
944952
-    p += bits & BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH);
944952
-    if (p->bits > available_bits) {
944952
+    BROTLI_HC_ADJUST_TABLE_INDEX(p,
944952
+        bits & BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH));
944952
+    if (BROTLI_HC_FAST_LOAD_BITS(p) > available_bits) {
944952
       get_byte = BROTLI_TRUE;
944952
       continue;
944952
     }
944952
-    code_len = p->value;  /* code_len == 0..17 */
944952
+    code_len = BROTLI_HC_FAST_LOAD_VALUE(p);  /* code_len == 0..17 */
944952
     if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) {
944952
-      BrotliDropBits(br, p->bits);
944952
-      ProcessSingleCodeLength(code_len, &s->symbol, &s->repeat, &s->space,
944952
-          &s->prev_code_len, s->symbol_lists, s->code_length_histo,
944952
-          s->next_symbol);
944952
+      BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(p));
944952
+      ProcessSingleCodeLength(code_len, &h->symbol, &h->repeat, &h->space,
944952
+          &h->prev_code_len, h->symbol_lists, h->code_length_histo,
944952
+          h->next_symbol);
944952
     } else {  /* code_len == 16..17, extra_bits == 2..3 */
944952
       uint32_t extra_bits = code_len - 14U;
944952
-      uint32_t repeat_delta = (bits >> p->bits) & BitMask(extra_bits);
944952
-      if (available_bits < p->bits + extra_bits) {
944952
+      uint32_t repeat_delta = (bits >> BROTLI_HC_FAST_LOAD_BITS(p)) &
944952
+          BitMask(extra_bits);
944952
+      if (available_bits < BROTLI_HC_FAST_LOAD_BITS(p) + extra_bits) {
944952
         get_byte = BROTLI_TRUE;
944952
         continue;
944952
       }
944952
-      BrotliDropBits(br, p->bits + extra_bits);
944952
+      BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(p) + extra_bits);
944952
       ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size,
944952
-          &s->symbol, &s->repeat, &s->space, &s->prev_code_len,
944952
-          &s->repeat_code_len, s->symbol_lists, s->code_length_histo,
944952
-          s->next_symbol);
944952
+          &h->symbol, &h->repeat, &h->space, &h->prev_code_len,
944952
+          &h->repeat_code_len, h->symbol_lists, h->code_length_histo,
944952
+          h->next_symbol);
944952
     }
944952
   }
944952
   return BROTLI_DECODER_SUCCESS;
944952
@@ -675,9 +691,10 @@ static BrotliDecoderErrorCode SafeReadSymbolCodeLengths(
944952
    Each code is 2..4 bits long. In total 30..72 bits are used. */
944952
 static BrotliDecoderErrorCode ReadCodeLengthCodeLengths(BrotliDecoderState* s) {
944952
   BrotliBitReader* br = &s->br;
944952
-  uint32_t num_codes = s->repeat;
944952
-  unsigned space = s->space;
944952
-  uint32_t i = s->sub_loop_counter;
944952
+  BrotliMetablockHeaderArena* h = &s->arena.header;
944952
+  uint32_t num_codes = h->repeat;
944952
+  unsigned space = h->space;
944952
+  uint32_t i = h->sub_loop_counter;
944952
   for (; i < BROTLI_CODE_LENGTH_CODES; ++i) {
944952
     const uint8_t code_len_idx = kCodeLengthCodeOrder[i];
944952
     uint32_t ix;
944952
@@ -690,21 +707,21 @@ static BrotliDecoderErrorCode ReadCodeLengthCodeLengths(BrotliDecoderState* s) {
944952
         ix = 0;
944952
       }
944952
       if (kCodeLengthPrefixLength[ix] > available_bits) {
944952
-        s->sub_loop_counter = i;
944952
-        s->repeat = num_codes;
944952
-        s->space = space;
944952
-        s->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX;
944952
+        h->sub_loop_counter = i;
944952
+        h->repeat = num_codes;
944952
+        h->space = space;
944952
+        h->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX;
944952
         return BROTLI_DECODER_NEEDS_MORE_INPUT;
944952
       }
944952
     }
944952
     v = kCodeLengthPrefixValue[ix];
944952
     BrotliDropBits(br, kCodeLengthPrefixLength[ix]);
944952
-    s->code_length_code_lengths[code_len_idx] = (uint8_t)v;
944952
-    BROTLI_LOG_ARRAY_INDEX(s->code_length_code_lengths, code_len_idx);
944952
+    h->code_length_code_lengths[code_len_idx] = (uint8_t)v;
944952
+    BROTLI_LOG_ARRAY_INDEX(h->code_length_code_lengths, code_len_idx);
944952
     if (v != 0) {
944952
       space = space - (32U >> v);
944952
       ++num_codes;
944952
-      ++s->code_length_histo[v];
944952
+      ++h->code_length_histo[v];
944952
       if (space - 1U >= 32U) {
944952
         /* space is 0 or wrapped around. */
944952
         break;
944952
@@ -728,49 +745,48 @@ static BrotliDecoderErrorCode ReadCodeLengthCodeLengths(BrotliDecoderState* s) {
944952
          encoded with predefined entropy code. 32 - 74 bits are used.
944952
     B.2) Decoded table is used to decode code lengths of symbols in resulting
944952
          Huffman table. In worst case 3520 bits are read. */
944952
-static BrotliDecoderErrorCode ReadHuffmanCode(uint32_t alphabet_size,
944952
-                                              uint32_t max_symbol,
944952
+static BrotliDecoderErrorCode ReadHuffmanCode(uint32_t alphabet_size_max,
944952
+                                              uint32_t alphabet_size_limit,
944952
                                               HuffmanCode* table,
944952
                                               uint32_t* opt_table_size,
944952
                                               BrotliDecoderState* s) {
944952
   BrotliBitReader* br = &s->br;
944952
-  /* Unnecessary masking, but might be good for safety. */
944952
-  alphabet_size &= 0x7FF;
944952
+  BrotliMetablockHeaderArena* h = &s->arena.header;
944952
   /* State machine. */
944952
   for (;;) {
944952
-    switch (s->substate_huffman) {
944952
+    switch (h->substate_huffman) {
944952
       case BROTLI_STATE_HUFFMAN_NONE:
944952
-        if (!BrotliSafeReadBits(br, 2, &s->sub_loop_counter)) {
944952
+        if (!BrotliSafeReadBits(br, 2, &h->sub_loop_counter)) {
944952
           return BROTLI_DECODER_NEEDS_MORE_INPUT;
944952
         }
944952
-        BROTLI_LOG_UINT(s->sub_loop_counter);
944952
+        BROTLI_LOG_UINT(h->sub_loop_counter);
944952
         /* The value is used as follows:
944952
            1 for simple code;
944952
            0 for no skipping, 2 skips 2 code lengths, 3 skips 3 code lengths */
944952
-        if (s->sub_loop_counter != 1) {
944952
-          s->space = 32;
944952
-          s->repeat = 0;  /* num_codes */
944952
-          memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo[0]) *
944952
+        if (h->sub_loop_counter != 1) {
944952
+          h->space = 32;
944952
+          h->repeat = 0;  /* num_codes */
944952
+          memset(&h->code_length_histo[0], 0, sizeof(h->code_length_histo[0]) *
944952
               (BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1));
944952
-          memset(&s->code_length_code_lengths[0], 0,
944952
-              sizeof(s->code_length_code_lengths));
944952
-          s->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX;
944952
+          memset(&h->code_length_code_lengths[0], 0,
944952
+              sizeof(h->code_length_code_lengths));
944952
+          h->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX;
944952
           continue;
944952
         }
944952
       /* Fall through. */
944952
 
944952
       case BROTLI_STATE_HUFFMAN_SIMPLE_SIZE:
944952
         /* Read symbols, codes & code lengths directly. */
944952
-        if (!BrotliSafeReadBits(br, 2, &s->symbol)) {  /* num_symbols */
944952
-          s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_SIZE;
944952
+        if (!BrotliSafeReadBits(br, 2, &h->symbol)) {  /* num_symbols */
944952
+          h->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_SIZE;
944952
           return BROTLI_DECODER_NEEDS_MORE_INPUT;
944952
         }
944952
-        s->sub_loop_counter = 0;
944952
+        h->sub_loop_counter = 0;
944952
       /* Fall through. */
944952
 
944952
       case BROTLI_STATE_HUFFMAN_SIMPLE_READ: {
944952
         BrotliDecoderErrorCode result =
944952
-            ReadSimpleHuffmanSymbols(alphabet_size, max_symbol, s);
944952
+            ReadSimpleHuffmanSymbols(alphabet_size_max, alphabet_size_limit, s);
944952
         if (result != BROTLI_DECODER_SUCCESS) {
944952
           return result;
944952
         }
944952
@@ -779,21 +795,21 @@ static BrotliDecoderErrorCode ReadHuffmanCode(uint32_t alphabet_size,
944952
 
944952
       case BROTLI_STATE_HUFFMAN_SIMPLE_BUILD: {
944952
         uint32_t table_size;
944952
-        if (s->symbol == 3) {
944952
+        if (h->symbol == 3) {
944952
           uint32_t bits;
944952
           if (!BrotliSafeReadBits(br, 1, &bits)) {
944952
-            s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_BUILD;
944952
+            h->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_BUILD;
944952
             return BROTLI_DECODER_NEEDS_MORE_INPUT;
944952
           }
944952
-          s->symbol += bits;
944952
+          h->symbol += bits;
944952
         }
944952
-        BROTLI_LOG_UINT(s->symbol);
944952
+        BROTLI_LOG_UINT(h->symbol);
944952
         table_size = BrotliBuildSimpleHuffmanTable(
944952
-            table, HUFFMAN_TABLE_BITS, s->symbols_lists_array, s->symbol);
944952
+            table, HUFFMAN_TABLE_BITS, h->symbols_lists_array, h->symbol);
944952
         if (opt_table_size) {
944952
           *opt_table_size = table_size;
944952
         }
944952
-        s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
944952
+        h->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
944952
         return BROTLI_DECODER_SUCCESS;
944952
       }
944952
 
944952
@@ -804,44 +820,45 @@ static BrotliDecoderErrorCode ReadHuffmanCode(uint32_t alphabet_size,
944952
         if (result != BROTLI_DECODER_SUCCESS) {
944952
           return result;
944952
         }
944952
-        BrotliBuildCodeLengthsHuffmanTable(s->table,
944952
-                                           s->code_length_code_lengths,
944952
-                                           s->code_length_histo);
944952
-        memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo));
944952
+        BrotliBuildCodeLengthsHuffmanTable(h->table,
944952
+                                           h->code_length_code_lengths,
944952
+                                           h->code_length_histo);
944952
+        memset(&h->code_length_histo[0], 0, sizeof(h->code_length_histo));
944952
         for (i = 0; i <= BROTLI_HUFFMAN_MAX_CODE_LENGTH; ++i) {
944952
-          s->next_symbol[i] = (int)i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
944952
-          s->symbol_lists[s->next_symbol[i]] = 0xFFFF;
944952
+          h->next_symbol[i] = (int)i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
944952
+          h->symbol_lists[h->next_symbol[i]] = 0xFFFF;
944952
         }
944952
 
944952
-        s->symbol = 0;
944952
-        s->prev_code_len = BROTLI_INITIAL_REPEATED_CODE_LENGTH;
944952
-        s->repeat = 0;
944952
-        s->repeat_code_len = 0;
944952
-        s->space = 32768;
944952
-        s->substate_huffman = BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS;
944952
+        h->symbol = 0;
944952
+        h->prev_code_len = BROTLI_INITIAL_REPEATED_CODE_LENGTH;
944952
+        h->repeat = 0;
944952
+        h->repeat_code_len = 0;
944952
+        h->space = 32768;
944952
+        h->substate_huffman = BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS;
944952
       }
944952
       /* Fall through. */
944952
 
944952
       case BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS: {
944952
         uint32_t table_size;
944952
-        BrotliDecoderErrorCode result = ReadSymbolCodeLengths(max_symbol, s);
944952
+        BrotliDecoderErrorCode result = ReadSymbolCodeLengths(
944952
+            alphabet_size_limit, s);
944952
         if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) {
944952
-          result = SafeReadSymbolCodeLengths(max_symbol, s);
944952
+          result = SafeReadSymbolCodeLengths(alphabet_size_limit, s);
944952
         }
944952
         if (result != BROTLI_DECODER_SUCCESS) {
944952
           return result;
944952
         }
944952
 
944952
-        if (s->space != 0) {
944952
-          BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", (int)s->space));
944952
+        if (h->space != 0) {
944952
+          BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", (int)h->space));
944952
           return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE);
944952
         }
944952
         table_size = BrotliBuildHuffmanTable(
944952
-            table, HUFFMAN_TABLE_BITS, s->symbol_lists, s->code_length_histo);
944952
+            table, HUFFMAN_TABLE_BITS, h->symbol_lists, h->code_length_histo);
944952
         if (opt_table_size) {
944952
           *opt_table_size = table_size;
944952
         }
944952
-        s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
944952
+        h->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
944952
         return BROTLI_DECODER_SUCCESS;
944952
       }
944952
 
944952
@@ -858,8 +875,8 @@ static BROTLI_INLINE uint32_t ReadBlockLength(const HuffmanCode* table,
944952
   uint32_t code;
944952
   uint32_t nbits;
944952
   code = ReadSymbol(table, br);
944952
-  nbits = kBlockLengthPrefixCode[code].nbits;  /* nbits == 2..24 */
944952
-  return kBlockLengthPrefixCode[code].offset + BrotliReadBits(br, nbits);
944952
+  nbits = _kBrotliPrefixCodeRanges[code].nbits;  /* nbits == 2..24 */
944952
+  return _kBrotliPrefixCodeRanges[code].offset + BrotliReadBits24(br, nbits);
944952
 }
944952
 
944952
 /* WARNING: if state is not BROTLI_STATE_READ_BLOCK_LENGTH_NONE, then
944952
@@ -877,13 +894,14 @@ static BROTLI_INLINE BROTLI_BOOL SafeReadBlockLength(
944952
   }
944952
   {
944952
     uint32_t bits;
944952
-    uint32_t nbits = kBlockLengthPrefixCode[index].nbits;  /* nbits == 2..24 */
944952
+    uint32_t nbits = _kBrotliPrefixCodeRanges[index].nbits;
944952
+    uint32_t offset = _kBrotliPrefixCodeRanges[index].offset;
944952
     if (!BrotliSafeReadBits(br, nbits, &bits)) {
944952
       s->block_length_index = index;
944952
       s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX;
944952
       return BROTLI_FALSE;
944952
     }
944952
-    *result = kBlockLengthPrefixCode[index].offset + bits;
944952
+    *result = offset + bits;
944952
     s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
944952
     return BROTLI_TRUE;
944952
   }
944952
@@ -943,22 +961,22 @@ static BROTLI_NOINLINE void InverseMoveToFrontTransform(
944952
 /* Decodes a series of Huffman table using ReadHuffmanCode function. */
944952
 static BrotliDecoderErrorCode HuffmanTreeGroupDecode(
944952
     HuffmanTreeGroup* group, BrotliDecoderState* s) {
944952
-  if (s->substate_tree_group != BROTLI_STATE_TREE_GROUP_LOOP) {
944952
-    s->next = group->codes;
944952
-    s->htree_index = 0;
944952
-    s->substate_tree_group = BROTLI_STATE_TREE_GROUP_LOOP;
944952
+  BrotliMetablockHeaderArena* h = &s->arena.header;
944952
+  if (h->substate_tree_group != BROTLI_STATE_TREE_GROUP_LOOP) {
944952
+    h->next = group->codes;
944952
+    h->htree_index = 0;
944952
+    h->substate_tree_group = BROTLI_STATE_TREE_GROUP_LOOP;
944952
   }
944952
-  while (s->htree_index < group->num_htrees) {
944952
+  while (h->htree_index < group->num_htrees) {
944952
     uint32_t table_size;
944952
-    BrotliDecoderErrorCode result =
944952
-        ReadHuffmanCode(group->alphabet_size, group->max_symbol,
944952
-                        s->next, &table_size, s);
944952
+    BrotliDecoderErrorCode result = ReadHuffmanCode(group->alphabet_size_max,
944952
+        group->alphabet_size_limit, h->next, &table_size, s);
944952
     if (result != BROTLI_DECODER_SUCCESS) return result;
944952
-    group->htrees[s->htree_index] = s->next;
944952
-    s->next += table_size;
944952
-    ++s->htree_index;
944952
+    group->htrees[h->htree_index] = h->next;
944952
+    h->next += table_size;
944952
+    ++h->htree_index;
944952
   }
944952
-  s->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;
944952
+  h->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;
944952
   return BROTLI_DECODER_SUCCESS;
944952
 }
944952
 
944952
@@ -976,15 +994,16 @@ static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size,
944952
                                                BrotliDecoderState* s) {
944952
   BrotliBitReader* br = &s->br;
944952
   BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS;
944952
+  BrotliMetablockHeaderArena* h = &s->arena.header;
944952
 
944952
-  switch ((int)s->substate_context_map) {
944952
+  switch ((int)h->substate_context_map) {
944952
     case BROTLI_STATE_CONTEXT_MAP_NONE:
944952
       result = DecodeVarLenUint8(s, br, num_htrees);
944952
       if (result != BROTLI_DECODER_SUCCESS) {
944952
         return result;
944952
       }
944952
       (*num_htrees)++;
944952
-      s->context_index = 0;
944952
+      h->context_index = 0;
944952
       BROTLI_LOG_UINT(context_map_size);
944952
       BROTLI_LOG_UINT(*num_htrees);
944952
       *context_map_arg =
944952
@@ -996,7 +1015,7 @@ static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size,
944952
         memset(*context_map_arg, 0, (size_t)context_map_size);
944952
         return BROTLI_DECODER_SUCCESS;
944952
       }
944952
-      s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_READ_PREFIX;
944952
+      h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_READ_PREFIX;
944952
     /* Fall through. */
944952
 
944952
     case BROTLI_STATE_CONTEXT_MAP_READ_PREFIX: {
944952
@@ -1007,38 +1026,38 @@ static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size,
944952
         return BROTLI_DECODER_NEEDS_MORE_INPUT;
944952
       }
944952
       if ((bits & 1) != 0) { /* Use RLE for zeros. */
944952
-        s->max_run_length_prefix = (bits >> 1) + 1;
944952
+        h->max_run_length_prefix = (bits >> 1) + 1;
944952
         BrotliDropBits(br, 5);
944952
       } else {
944952
-        s->max_run_length_prefix = 0;
944952
+        h->max_run_length_prefix = 0;
944952
         BrotliDropBits(br, 1);
944952
       }
944952
-      BROTLI_LOG_UINT(s->max_run_length_prefix);
944952
-      s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_HUFFMAN;
944952
+      BROTLI_LOG_UINT(h->max_run_length_prefix);
944952
+      h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_HUFFMAN;
944952
     }
944952
     /* Fall through. */
944952
 
944952
     case BROTLI_STATE_CONTEXT_MAP_HUFFMAN: {
944952
-      uint32_t alphabet_size = *num_htrees + s->max_run_length_prefix;
944952
+      uint32_t alphabet_size = *num_htrees + h->max_run_length_prefix;
944952
       result = ReadHuffmanCode(alphabet_size, alphabet_size,
944952
-                               s->context_map_table, NULL, s);
944952
+                               h->context_map_table, NULL, s);
944952
       if (result != BROTLI_DECODER_SUCCESS) return result;
944952
-      s->code = 0xFFFF;
944952
-      s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_DECODE;
944952
+      h->code = 0xFFFF;
944952
+      h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_DECODE;
944952
     }
944952
     /* Fall through. */
944952
 
944952
     case BROTLI_STATE_CONTEXT_MAP_DECODE: {
944952
-      uint32_t context_index = s->context_index;
944952
-      uint32_t max_run_length_prefix = s->max_run_length_prefix;
944952
+      uint32_t context_index = h->context_index;
944952
+      uint32_t max_run_length_prefix = h->max_run_length_prefix;
944952
       uint8_t* context_map = *context_map_arg;
944952
-      uint32_t code = s->code;
944952
+      uint32_t code = h->code;
944952
       BROTLI_BOOL skip_preamble = (code != 0xFFFF);
944952
       while (context_index < context_map_size || skip_preamble) {
944952
         if (!skip_preamble) {
944952
-          if (!SafeReadSymbol(s->context_map_table, br, &code)) {
944952
-            s->code = 0xFFFF;
944952
-            s->context_index = context_index;
944952
+          if (!SafeReadSymbol(h->context_map_table, br, &code)) {
944952
+            h->code = 0xFFFF;
944952
+            h->context_index = context_index;
944952
             return BROTLI_DECODER_NEEDS_MORE_INPUT;
944952
           }
944952
           BROTLI_LOG_UINT(code);
944952
@@ -1059,8 +1078,8 @@ static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size,
944952
         {
944952
           uint32_t reps;
944952
           if (!BrotliSafeReadBits(br, code, &reps)) {
944952
-            s->code = code;
944952
-            s->context_index = context_index;
944952
+            h->code = code;
944952
+            h->context_index = context_index;
944952
             return BROTLI_DECODER_NEEDS_MORE_INPUT;
944952
           }
944952
           reps += 1U << code;
944952
@@ -1080,13 +1099,13 @@ static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size,
944952
     case BROTLI_STATE_CONTEXT_MAP_TRANSFORM: {
944952
       uint32_t bits;
944952
       if (!BrotliSafeReadBits(br, 1, &bits)) {
944952
-        s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_TRANSFORM;
944952
+        h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_TRANSFORM;
944952
         return BROTLI_DECODER_NEEDS_MORE_INPUT;
944952
       }
944952
       if (bits != 0) {
944952
         InverseMoveToFrontTransform(*context_map_arg, context_map_size, s);
944952
       }
944952
-      s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;
944952
+      h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;
944952
       return BROTLI_DECODER_SUCCESS;
944952
     }
944952
 
944952
@@ -1448,32 +1467,28 @@ static BrotliDecoderErrorCode ReadContextModes(BrotliDecoderState* s) {
944952
 }
944952
 
944952
 static BROTLI_INLINE void TakeDistanceFromRingBuffer(BrotliDecoderState* s) {
944952
-  if (s->distance_code == 0) {
944952
-    --s->dist_rb_idx;
944952
-    s->distance_code = s->dist_rb[s->dist_rb_idx & 3];
944952
+  int offset = s->distance_code - 3;
944952
+  if (s->distance_code <= 3) {
944952
     /* Compensate double distance-ring-buffer roll for dictionary items. */
944952
-    s->distance_context = 1;
944952
+    s->distance_context = 1 >> s->distance_code;
944952
+    s->distance_code = s->dist_rb[(s->dist_rb_idx - offset) & 3];
944952
+    s->dist_rb_idx -= s->distance_context;
944952
   } else {
944952
-    int distance_code = s->distance_code << 1;
944952
-    /* kDistanceShortCodeIndexOffset has 2-bit values from LSB:
944952
-        3, 2, 1, 0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2 */
944952
-    const uint32_t kDistanceShortCodeIndexOffset = 0xAAAFFF1B;
944952
-    /* kDistanceShortCodeValueOffset has 2-bit values from LSB:
944952
-       -0, 0,-0, 0,-1, 1,-2, 2,-3, 3,-1, 1,-2, 2,-3, 3 */
944952
-    const uint32_t kDistanceShortCodeValueOffset = 0xFA5FA500;
944952
-    int v = (s->dist_rb_idx +
944952
-        (int)(kDistanceShortCodeIndexOffset >> distance_code)) & 0x3;
944952
-    s->distance_code = s->dist_rb[v];
944952
-    v = (int)(kDistanceShortCodeValueOffset >> distance_code) & 0x3;
944952
-    if ((distance_code & 0x3) != 0) {
944952
-      s->distance_code += v;
944952
+    int index_delta = 3;
944952
+    int delta;
944952
+    int base = s->distance_code - 10;
944952
+    if (s->distance_code < 10) {
944952
+      base = s->distance_code - 4;
944952
     } else {
944952
-      s->distance_code -= v;
944952
-      if (s->distance_code <= 0) {
944952
-        /* A huge distance will cause a BROTLI_FAILURE() soon.
944952
-           This is a little faster than failing here. */
944952
-        s->distance_code = 0x7FFFFFFF;
944952
-      }
944952
+      index_delta = 2;
944952
+    }
944952
+    /* Unpack one of six 4-bit values. */
944952
+    delta = ((0x605142 >> (4 * base)) & 0xF) - 3;
944952
+    s->distance_code = s->dist_rb[(s->dist_rb_idx + index_delta) & 0x3] + delta;
944952
+    if (s->distance_code <= 0) {
944952
+      /* A huge distance will cause a BROTLI_FAILURE() soon.
944952
+         This is a little faster than failing here. */
944952
+      s->distance_code = 0x7FFFFFFF;
944952
     }
944952
   }
944952
 }
944952
@@ -1488,62 +1503,153 @@ static BROTLI_INLINE BROTLI_BOOL SafeReadBits(
944952
   }
944952
 }
944952
 
944952
+static BROTLI_INLINE BROTLI_BOOL SafeReadBits32(
944952
+    BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
944952
+  if (n_bits != 0) {
944952
+    return BrotliSafeReadBits32(br, n_bits, val);
944952
+  } else {
944952
+    *val = 0;
944952
+    return BROTLI_TRUE;
944952
+  }
944952
+}
944952
+
944952
+/*
944952
+   RFC 7932 Section 4 with "..." shortenings and "[]" emendations.
944952
+
944952
+   Each distance ... is represented with a pair <distance code, extra bits>...
944952
+   The distance code is encoded using a prefix code... The number of extra bits
944952
+   can be 0..24... Two additional parameters: NPOSTFIX (0..3), and ...
944952
+   NDIRECT (0..120) ... are encoded in the meta-block header...
944952
+
944952
+   The first 16 distance symbols ... reference past distances... ring buffer ...
944952
+   Next NDIRECT distance symbols ... represent distances from 1 to NDIRECT...
944952
+   [For] distance symbols 16 + NDIRECT and greater ... the number of extra bits
944952
+   ... is given by the following formula:
944952
+
944952
+   [ xcode = dcode - NDIRECT - 16 ]
944952
+   ndistbits = 1 + [ xcode ] >> (NPOSTFIX + 1)
944952
+
944952
+   ...
944952
+*/
944952
+
944952
+/*
944952
+   RFC 7932 Section 9.2 with "..." shortenings and "[]" emendations.
944952
+
944952
+   ... to get the actual value of the parameter NDIRECT, left-shift this
944952
+   four-bit number by NPOSTFIX bits ...
944952
+*/
944952
+
944952
+/* Remaining formulas from RFC 7932 Section 4 could be rewritten as following:
944952
+
944952
+     alphabet_size = 16 + NDIRECT + (max_distbits << (NPOSTFIX + 1))
944952
+
944952
+     half = ((xcode >> NPOSTFIX) & 1) << ndistbits
944952
+     postfix = xcode & ((1 << NPOSTFIX) - 1)
944952
+     range_start = 2 * (1 << ndistbits - 1 - 1)
944952
+
944952
+     distance = (range_start + half + extra) << NPOSTFIX + postfix + NDIRECT + 1
944952
+
944952
+   NB: ndistbits >= 1 -> range_start >= 0
944952
+   NB: range_start has factor 2, as the range is covered by 2 "halves"
944952
+   NB: extra -1 offset in range_start formula covers the absence of
944952
+       ndistbits = 0 case
944952
+   NB: when NPOSTFIX = 0, NDIRECT is not greater than 15
944952
+
944952
+   In other words, xcode has the following binary structure - XXXHPPP:
944952
+    - XXX represent the number of extra distance bits
944952
+    - H selects upper / lower range of distances
944952
+    - PPP represent "postfix"
944952
+
944952
+  "Regular" distance encoding has NPOSTFIX = 0; omitting the postfix part
944952
+  simplifies distance calculation.
944952
+
944952
+  Using NPOSTFIX > 0 allows cheaper encoding of regular structures, e.g. where
944952
+  most of distances have the same reminder of division by 2/4/8. For example,
944952
+  the table of int32_t values that come from different sources; if it is likely
944952
+  that 3 highest bytes of values from the same source are the same, then
944952
+  copy distance often looks like 4x + y.
944952
+
944952
+  Distance calculation could be rewritten to:
944952
+
944952
+    ndistbits = NDISTBITS(NDIRECT, NPOSTFIX)[dcode]
944952
+    distance = OFFSET(NDIRECT, NPOSTFIX)[dcode] + extra << NPOSTFIX
944952
+
944952
+  NDISTBITS and OFFSET could be pre-calculated, as NDIRECT and NPOSTFIX could
944952
+  change only once per meta-block.
944952
+*/
944952
+
944952
+/* Calculates distance lookup table.
944952
+   NB: it is possible to have all 64 tables precalculated. */
944952
+static void CalculateDistanceLut(BrotliDecoderState* s) {
944952
+  BrotliMetablockBodyArena* b = &s->arena.body;
944952
+  uint32_t npostfix = s->distance_postfix_bits;
944952
+  uint32_t ndirect = s->num_direct_distance_codes;
944952
+  uint32_t alphabet_size_limit = s->distance_hgroup.alphabet_size_limit;
944952
+  uint32_t postfix = 1u << npostfix;
944952
+  uint32_t j;
944952
+  uint32_t bits = 1;
944952
+  uint32_t half = 0;
944952
+
944952
+  /* Skip short codes. */
944952
+  uint32_t i = BROTLI_NUM_DISTANCE_SHORT_CODES;
944952
+
944952
+  /* Fill direct codes. */
944952
+  for (j = 0; j < ndirect; ++j) {
944952
+    b->dist_extra_bits[i] = 0;
944952
+    b->dist_offset[i] = j + 1;
944952
+    ++i;
944952
+  }
944952
+
944952
+  /* Fill regular distance codes. */
944952
+  while (i < alphabet_size_limit) {
944952
+    uint32_t base = ndirect + ((((2 + half) << bits) - 4) << npostfix) + 1;
944952
+    /* Always fill the complete group. */
944952
+    for (j = 0; j < postfix; ++j) {
944952
+      b->dist_extra_bits[i] = (uint8_t)bits;
944952
+      b->dist_offset[i] = base + j;
944952
+      ++i;
944952
+    }
944952
+    bits = bits + half;
944952
+    half = half ^ 1;
944952
+  }
944952
+}
944952
+
944952
 /* Precondition: s->distance_code < 0. */
944952
 static BROTLI_INLINE BROTLI_BOOL ReadDistanceInternal(
944952
     int safe, BrotliDecoderState* s, BrotliBitReader* br) {
944952
-  int distval;
944952
+  BrotliMetablockBodyArena* b = &s->arena.body;
944952
+  uint32_t code;
944952
+  uint32_t bits;
944952
   BrotliBitReaderState memento;
944952
   HuffmanCode* distance_tree = s->distance_hgroup.htrees[s->dist_htree_index];
944952
   if (!safe) {
944952
-    s->distance_code = (int)ReadSymbol(distance_tree, br);
944952
+    code = ReadSymbol(distance_tree, br);
944952
   } else {
944952
-    uint32_t code;
944952
     BrotliBitReaderSaveState(br, &memento);
944952
     if (!SafeReadSymbol(distance_tree, br, &code)) {
944952
       return BROTLI_FALSE;
944952
     }
944952
-    s->distance_code = (int)code;
944952
   }
944952
+  --s->block_length[2];
944952
   /* Convert the distance code to the actual distance by possibly
944952
-     looking up past distances from the s->ringbuffer. */
944952
+     looking up past distances from the s->dist_rb. */
944952
   s->distance_context = 0;
944952
-  if ((s->distance_code & ~0xF) == 0) {
944952
+  if ((code & ~0xFu) == 0) {
944952
+    s->distance_code = (int)code;
944952
     TakeDistanceFromRingBuffer(s);
944952
-    --s->block_length[2];
944952
     return BROTLI_TRUE;
944952
   }
944952
-  distval = s->distance_code - (int)s->num_direct_distance_codes;
944952
-  if (distval >= 0) {
944952
-    uint32_t nbits;
944952
-    int postfix;
944952
-    int offset;
944952
-    if (!safe && (s->distance_postfix_bits == 0)) {
944952
-      nbits = ((uint32_t)distval >> 1) + 1;
944952
-      offset = ((2 + (distval & 1)) << nbits) - 4;
944952
-      s->distance_code = (int)s->num_direct_distance_codes + offset +
944952
-                         (int)BrotliReadBits(br, nbits);
944952
-    } else {
944952
-      /* This branch also works well when s->distance_postfix_bits == 0. */
944952
-      uint32_t bits;
944952
-      postfix = distval & s->distance_postfix_mask;
944952
-      distval >>= s->distance_postfix_bits;
944952
-      nbits = ((uint32_t)distval >> 1) + 1;
944952
-      if (safe) {
944952
-        if (!SafeReadBits(br, nbits, &bits)) {
944952
-          s->distance_code = -1;  /* Restore precondition. */
944952
-          BrotliBitReaderRestoreState(br, &memento);
944952
-          return BROTLI_FALSE;
944952
-        }
944952
-      } else {
944952
-        bits = BrotliReadBits(br, nbits);
944952
-      }
944952
-      offset = ((2 + (distval & 1)) << nbits) - 4;
944952
-      s->distance_code = (int)s->num_direct_distance_codes +
944952
-          ((offset + (int)bits) << s->distance_postfix_bits) + postfix;
944952
+  if (!safe) {
944952
+    bits = BrotliReadBits32(br, b->dist_extra_bits[code]);
944952
+  } else {
944952
+    if (!SafeReadBits32(br, b->dist_extra_bits[code], &bits)) {
944952
+      ++s->block_length[2];
944952
+      BrotliBitReaderRestoreState(br, &memento);
944952
+      return BROTLI_FALSE;
944952
     }
944952
   }
944952
-  s->distance_code = s->distance_code - BROTLI_NUM_DISTANCE_SHORT_CODES + 1;
944952
-  --s->block_length[2];
944952
+  s->distance_code =
944952
+      (int)(b->dist_offset[code] + (bits << s->distance_postfix_bits));
944952
   return BROTLI_TRUE;
944952
 }
944952
 
944952
@@ -1579,9 +1685,9 @@ static BROTLI_INLINE BROTLI_BOOL ReadCommandInternal(
944952
   *insert_length = v.insert_len_offset;
944952
   if (!safe) {
944952
     if (BROTLI_PREDICT_FALSE(v.insert_len_extra_bits != 0)) {
944952
-      insert_len_extra = BrotliReadBits(br, v.insert_len_extra_bits);
944952
+      insert_len_extra = BrotliReadBits24(br, v.insert_len_extra_bits);
944952
     }
944952
-    copy_length = BrotliReadBits(br, v.copy_len_extra_bits);
944952
+    copy_length = BrotliReadBits24(br, v.copy_len_extra_bits);
944952
   } else {
944952
     if (!SafeReadBits(br, v.insert_len_extra_bits, &insert_len_extra) ||
944952
         !SafeReadBits(br, v.copy_len_extra_bits, &copy_length)) {
944952
@@ -1926,21 +2032,6 @@ static BROTLI_NOINLINE BrotliDecoderErrorCode SafeProcessCommands(
944952
   return ProcessCommandsInternal(1, s);
944952
 }
944952
 
944952
-/* Returns the maximum number of distance symbols which can only represent
944952
-   distances not exceeding BROTLI_MAX_ALLOWED_DISTANCE. */
944952
-static uint32_t BrotliMaxDistanceSymbol(uint32_t ndirect, uint32_t npostfix) {
944952
-  static const uint32_t bound[BROTLI_MAX_NPOSTFIX + 1] = {0, 4, 12, 28};
944952
-  static const uint32_t diff[BROTLI_MAX_NPOSTFIX + 1] = {73, 126, 228, 424};
944952
-  uint32_t postfix = 1U << npostfix;
944952
-  if (ndirect < bound[npostfix]) {
944952
-    return ndirect + diff[npostfix] + postfix;
944952
-  } else if (ndirect > bound[npostfix] + postfix) {
944952
-    return ndirect + diff[npostfix];
944952
-  } else {
944952
-    return bound[npostfix] + diff[npostfix] + postfix;
944952
-  }
944952
-}
944952
-
944952
 BrotliDecoderResult BrotliDecoderDecompress(
944952
     size_t encoded_size, const uint8_t* encoded_buffer, size_t* decoded_size,
944952
     uint8_t* decoded_buffer) {
944952
@@ -2158,33 +2249,23 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
944952
           s->state = BROTLI_STATE_UNCOMPRESSED;
944952
           break;
944952
         }
944952
+        s->state = BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_HEADER;
944952
+      /* Fall through. */
944952
+
944952
+      case BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_HEADER: {
944952
+        BrotliMetablockHeaderArena* h = &s->arena.header;
944952
         s->loop_counter = 0;
944952
+        /* Initialize compressed metablock header arena. */
944952
+        h->sub_loop_counter = 0;
944952
+        /* Make small negative indexes addressable. */
944952
+        h->symbol_lists =
944952
+            &h->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1];
944952
+        h->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
944952
+        h->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;
944952
+        h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;
944952
         s->state = BROTLI_STATE_HUFFMAN_CODE_0;
944952
-        break;
944952
-
944952
-      case BROTLI_STATE_UNCOMPRESSED: {
944952
-        result = CopyUncompressedBlockToOutput(
944952
-            available_out, next_out, total_out, s);
944952
-        if (result != BROTLI_DECODER_SUCCESS) {
944952
-          break;
944952
-        }
944952
-        s->state = BROTLI_STATE_METABLOCK_DONE;
944952
-        break;
944952
       }
944952
-
944952
-      case BROTLI_STATE_METADATA:
944952
-        for (; s->meta_block_remaining_len > 0; --s->meta_block_remaining_len) {
944952
-          uint32_t bits;
944952
-          /* Read one byte and ignore it. */
944952
-          if (!BrotliSafeReadBits(br, 8, &bits)) {
944952
-            result = BROTLI_DECODER_NEEDS_MORE_INPUT;
944952
-            break;
944952
-          }
944952
-        }
944952
-        if (result == BROTLI_DECODER_SUCCESS) {
944952
-          s->state = BROTLI_STATE_METABLOCK_DONE;
944952
-        }
944952
-        break;
944952
+      /* Fall through. */
944952
 
944952
       case BROTLI_STATE_HUFFMAN_CODE_0:
944952
         if (s->loop_counter >= 3) {
944952
@@ -2238,6 +2319,30 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
944952
         break;
944952
       }
944952
 
944952
+      case BROTLI_STATE_UNCOMPRESSED: {
944952
+        result = CopyUncompressedBlockToOutput(
944952
+            available_out, next_out, total_out, s);
944952
+        if (result != BROTLI_DECODER_SUCCESS) {
944952
+          break;
944952
+        }
944952
+        s->state = BROTLI_STATE_METABLOCK_DONE;
944952
+        break;
944952
+      }
944952
+
944952
+      case BROTLI_STATE_METADATA:
944952
+        for (; s->meta_block_remaining_len > 0; --s->meta_block_remaining_len) {
944952
+          uint32_t bits;
944952
+          /* Read one byte and ignore it. */
944952
+          if (!BrotliSafeReadBits(br, 8, &bits)) {
944952
+            result = BROTLI_DECODER_NEEDS_MORE_INPUT;
944952
+            break;
944952
+          }
944952
+        }
944952
+        if (result == BROTLI_DECODER_SUCCESS) {
944952
+          s->state = BROTLI_STATE_METABLOCK_DONE;
944952
+        }
944952
+        break;
944952
+
944952
       case BROTLI_STATE_METABLOCK_HEADER_2: {
944952
         uint32_t bits;
944952
         if (!BrotliSafeReadBits(br, 6, &bits)) {
944952
@@ -2246,11 +2351,9 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
944952
         }
944952
         s->distance_postfix_bits = bits & BitMask(2);
944952
         bits >>= 2;
944952
-        s->num_direct_distance_codes = BROTLI_NUM_DISTANCE_SHORT_CODES +
944952
-            (bits << s->distance_postfix_bits);
944952
+        s->num_direct_distance_codes = bits << s->distance_postfix_bits;
944952
         BROTLI_LOG_UINT(s->num_direct_distance_codes);
944952
         BROTLI_LOG_UINT(s->distance_postfix_bits);
944952
-        s->distance_postfix_mask = (int)BitMask(s->distance_postfix_bits);
944952
         s->context_modes =
944952
             (uint8_t*)BROTLI_DECODER_ALLOC(s, (size_t)s->num_block_types[0]);
944952
         if (s->context_modes == 0) {
944952
@@ -2282,17 +2385,19 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
944952
       /* Fall through. */
944952
 
944952
       case BROTLI_STATE_CONTEXT_MAP_2: {
944952
-        uint32_t num_direct_codes =
944952
-            s->num_direct_distance_codes - BROTLI_NUM_DISTANCE_SHORT_CODES;
944952
-        uint32_t num_distance_codes = BROTLI_DISTANCE_ALPHABET_SIZE(
944952
-            s->distance_postfix_bits, num_direct_codes,
944952
-            (s->large_window ? BROTLI_LARGE_MAX_DISTANCE_BITS :
944952
-                               BROTLI_MAX_DISTANCE_BITS));
944952
-        uint32_t max_distance_symbol = (s->large_window ?
944952
-            BrotliMaxDistanceSymbol(
944952
-                num_direct_codes, s->distance_postfix_bits) :
944952
-            num_distance_codes);
944952
+        uint32_t npostfix = s->distance_postfix_bits;
944952
+        uint32_t ndirect = s->num_direct_distance_codes;
944952
+        uint32_t distance_alphabet_size_max = BROTLI_DISTANCE_ALPHABET_SIZE(
944952
+            npostfix, ndirect, BROTLI_MAX_DISTANCE_BITS);
944952
+        uint32_t distance_alphabet_size_limit = distance_alphabet_size_max;
944952
         BROTLI_BOOL allocation_success = BROTLI_TRUE;
944952
+        if (s->large_window) {
944952
+          BrotliDistanceCodeLimit limit = BrotliCalculateDistanceCodeLimit(
944952
+              BROTLI_MAX_ALLOWED_DISTANCE, npostfix, ndirect);
944952
+          distance_alphabet_size_max = BROTLI_DISTANCE_ALPHABET_SIZE(
944952
+              npostfix, ndirect, BROTLI_LARGE_MAX_DISTANCE_BITS);
944952
+          distance_alphabet_size_limit = limit.max_alphabet_size;
944952
+        }
944952
         result = DecodeContextMap(
944952
             s->num_block_types[2] << BROTLI_DISTANCE_CONTEXT_BITS,
944952
             &s->num_dist_htrees, &s->dist_context_map, s);
944952
@@ -2306,8 +2411,8 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
944952
             s, &s->insert_copy_hgroup, BROTLI_NUM_COMMAND_SYMBOLS,
944952
             BROTLI_NUM_COMMAND_SYMBOLS, s->num_block_types[1]);
944952
         allocation_success &= BrotliDecoderHuffmanTreeGroupInit(
944952
-            s, &s->distance_hgroup, num_distance_codes,
944952
-            max_distance_symbol, s->num_dist_htrees);
944952
+            s, &s->distance_hgroup, distance_alphabet_size_max,
944952
+            distance_alphabet_size_limit, s->num_dist_htrees);
944952
         if (!allocation_success) {
944952
           return SaveErrorCode(s,
944952
               BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS));
944952
@@ -2329,18 +2434,24 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
944952
         result = HuffmanTreeGroupDecode(hgroup, s);
944952
         if (result != BROTLI_DECODER_SUCCESS) break;
944952
         s->loop_counter++;
944952
-        if (s->loop_counter >= 3) {
944952
-          PrepareLiteralDecoding(s);
944952
-          s->dist_context_map_slice = s->dist_context_map;
944952
-          s->htree_command = s->insert_copy_hgroup.htrees[0];
944952
-          if (!BrotliEnsureRingBuffer(s)) {
944952
-            result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2);
944952
-            break;
944952
-          }
944952
-          s->state = BROTLI_STATE_COMMAND_BEGIN;
944952
+        if (s->loop_counter < 3) {
944952
+          break;
944952
         }
944952
-        break;
944952
+        s->state = BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_BODY;
944952
       }
944952
+      /* Fall through. */
944952
+
944952
+      case BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_BODY:
944952
+        PrepareLiteralDecoding(s);
944952
+        s->dist_context_map_slice = s->dist_context_map;
944952
+        s->htree_command = s->insert_copy_hgroup.htrees[0];
944952
+        if (!BrotliEnsureRingBuffer(s)) {
944952
+          result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2);
944952
+          break;
944952
+        }
944952
+        CalculateDistanceLut(s);
944952
+        s->state = BROTLI_STATE_COMMAND_BEGIN;
944952
+      /* Fall through. */
944952
 
944952
       case BROTLI_STATE_COMMAND_BEGIN:
944952
       /* Fall through. */
944952
diff --git a/c/dec/huffman.h b/c/dec/huffman.h
944952
index 521ec6e..a8fbc45 100644
944952
--- a/c/dec/huffman.h
944952
+++ b/c/dec/huffman.h
944952
@@ -18,12 +18,6 @@ extern "C" {
944952
 
944952
 #define BROTLI_HUFFMAN_MAX_CODE_LENGTH 15
944952
 
944952
-/* Maximum possible Huffman table size for an alphabet size of (index * 32),
944952
-   max code length 15 and root table bits 8. */
944952
-static const uint16_t kMaxHuffmanTableSize[] = {
944952
-  256, 402, 436, 468, 500, 534, 566, 598, 630, 662, 694, 726, 758, 790, 822,
944952
-  854, 886, 920, 952, 984, 1016, 1048, 1080, 1112, 1144, 1176, 1208, 1240, 1272,
944952
-  1304, 1336, 1368, 1400, 1432, 1464, 1496, 1528};
944952
 /* BROTLI_NUM_BLOCK_LEN_SYMBOLS == 26 */
944952
 #define BROTLI_HUFFMAN_MAX_SIZE_26 396
944952
 /* BROTLI_MAX_BLOCK_TYPE_SYMBOLS == 258 */
944952
@@ -33,11 +27,66 @@ static const uint16_t kMaxHuffmanTableSize[] = {
944952
 
944952
 #define BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH 5
944952
 
944952
+#if ((defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_32)) && \
944952
+  BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0))
944952
+#define BROTLI_HUFFMAN_CODE_FAST_LOAD
944952
+#endif
944952
+
944952
+#if !defined(BROTLI_HUFFMAN_CODE_FAST_LOAD)
944952
+/* Do not create this struct directly - use the ConstructHuffmanCode
944952
+ * constructor below! */
944952
 typedef struct {
944952
   uint8_t bits;    /* number of bits used for this symbol */
944952
   uint16_t value;  /* symbol value or table offset */
944952
 } HuffmanCode;
944952
 
944952
+static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits,
944952
+    const uint16_t value) {
944952
+  HuffmanCode h;
944952
+  h.bits = bits;
944952
+  h.value = value;
944952
+  return h;
944952
+}
944952
+
944952
+/* Please use the following macros to optimize HuffmanCode accesses in hot
944952
+ * paths.
944952
+ *
944952
+ * For example, assuming |table| contains a HuffmanCode pointer:
944952
+ *
944952
+ *   BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table);
944952
+ *   BROTLI_HC_ADJUST_TABLE_INDEX(table, index_into_table);
944952
+ *   *bits = BROTLI_HC_GET_BITS(table);
944952
+ *   *value = BROTLI_HC_GET_VALUE(table);
944952
+ *   BROTLI_HC_ADJUST_TABLE_INDEX(table, offset);
944952
+ *   *bits2 = BROTLI_HC_GET_BITS(table);
944952
+ *   *value2 = BROTLI_HC_GET_VALUE(table);
944952
+ *
944952
+ */
944952
+
944952
+#define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H)
944952
+#define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V)
944952
+
944952
+/* These must be given a HuffmanCode pointer! */
944952
+#define BROTLI_HC_FAST_LOAD_BITS(H) (H->bits)
944952
+#define BROTLI_HC_FAST_LOAD_VALUE(H) (H->value)
944952
+
944952
+#else /* BROTLI_HUFFMAN_CODE_FAST_LOAD */
944952
+
944952
+typedef BROTLI_ALIGNED(4) uint32_t HuffmanCode;
944952
+
944952
+static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits,
944952
+    const uint16_t value) {
944952
+  return (HuffmanCode) ((value & 0xFFFF) << 16) | (bits & 0xFF);
944952
+}
944952
+
944952
+#define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H) uint32_t __fastload_##H = (*H)
944952
+#define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V); __fastload_##H = (*H)
944952
+
944952
+/* These must be given a HuffmanCode pointer! */
944952
+#define BROTLI_HC_FAST_LOAD_BITS(H) ((__fastload_##H) & 0xFF)
944952
+#define BROTLI_HC_FAST_LOAD_VALUE(H) ((__fastload_##H) >> 16)
944952
+#endif /* BROTLI_HUFFMAN_CODE_FAST_LOAD */
944952
+
944952
 /* Builds Huffman lookup table assuming code lengths are in symbol order. */
944952
 BROTLI_INTERNAL void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table,
944952
     const uint8_t* const code_lengths, uint16_t* count);
944952
@@ -45,7 +94,7 @@ BROTLI_INTERNAL void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table,
944952
 /* Builds Huffman lookup table assuming code lengths are in symbol order.
944952
    Returns size of resulting table. */
944952
 BROTLI_INTERNAL uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
944952
-    int root_bits, const uint16_t* const symbol_lists, uint16_t* count_arg);
944952
+    int root_bits, const uint16_t* const symbol_lists, uint16_t* count);
944952
 
944952
 /* Builds a simple Huffman table. The |num_symbols| parameter is to be
944952
    interpreted as follows: 0 means 1 symbol, 1 means 2 symbols,
944952
@@ -55,13 +104,13 @@ BROTLI_INTERNAL uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
944952
     int root_bits, uint16_t* symbols, uint32_t num_symbols);
944952
 
944952
 /* Contains a collection of Huffman trees with the same alphabet size. */
944952
-/* max_symbol is needed due to simple codes since log2(alphabet_size) could be
944952
-   greater than log2(max_symbol). */
944952
+/* alphabet_size_limit is needed due to simple codes, since
944952
+   log2(alphabet_size_max) could be greater than log2(alphabet_size_limit). */
944952
 typedef struct {
944952
   HuffmanCode** htrees;
944952
   HuffmanCode* codes;
944952
-  uint16_t alphabet_size;
944952
-  uint16_t max_symbol;
944952
+  uint16_t alphabet_size_max;
944952
+  uint16_t alphabet_size_limit;
944952
   uint16_t num_htrees;
944952
 } HuffmanTreeGroup;
944952
 
944952
diff --git a/c/dec/prefix.h b/c/dec/prefix.h
944952
index 3ea062d..481a2c7 100644
944952
--- a/c/dec/prefix.h
944952
+++ b/c/dec/prefix.h
944952
@@ -13,24 +13,6 @@
944952
 #include "../common/constants.h"
944952
 #include <brotli/types.h>
944952
 
944952
-/* Represents the range of values belonging to a prefix code:
944952
-   [offset, offset + 2^nbits) */
944952
-struct PrefixCodeRange {
944952
-  uint16_t offset;
944952
-  uint8_t nbits;
944952
-};
944952
-
944952
-static const struct PrefixCodeRange
944952
-    kBlockLengthPrefixCode[BROTLI_NUM_BLOCK_LEN_SYMBOLS] = {
944952
-  {   1,  2}, {    5,  2}, {  9,   2}, {  13,  2},
944952
-  {  17,  3}, {   25,  3}, {  33,  3}, {  41,  3},
944952
-  {  49,  4}, {   65,  4}, {  81,  4}, {  97,  4},
944952
-  { 113,  5}, {  145,  5}, { 177,  5}, { 209,  5},
944952
-  { 241,  6}, {  305,  6}, { 369,  7}, { 497,  8},
944952
-  { 753,  9}, { 1265, 10}, {2289, 11}, {4337, 12},
944952
-  {8433, 13}, {16625, 24}
944952
-};
944952
-
944952
 typedef struct CmdLutElement {
944952
   uint8_t insert_len_extra_bits;
944952
   uint8_t copy_len_extra_bits;
944952
diff --git a/c/dec/state.c b/c/dec/state.c
944952
index e0b37c2..f847836 100644
944952
--- a/c/dec/state.c
944952
+++ b/c/dec/state.c
944952
@@ -33,10 +33,7 @@ BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s,
944952
   s->state = BROTLI_STATE_UNINITED;
944952
   s->large_window = 0;
944952
   s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
944952
-  s->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;
944952
-  s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;
944952
   s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE;
944952
-  s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
944952
   s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
944952
   s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
944952
 
944952
@@ -59,8 +56,6 @@ BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s,
944952
   s->context_map_slice = NULL;
944952
   s->dist_context_map_slice = NULL;
944952
 
944952
-  s->sub_loop_counter = 0;
944952
-
944952
   s->literal_hgroup.codes = NULL;
944952
   s->literal_hgroup.htrees = NULL;
944952
   s->insert_copy_hgroup.codes = NULL;
944952
@@ -84,9 +79,6 @@ BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s,
944952
   s->block_type_trees = NULL;
944952
   s->block_len_trees = NULL;
944952
 
944952
-  /* Make small negative indexes addressable. */
944952
-  s->symbol_lists = &s->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1];
944952
-
944952
   s->mtf_upper_bound = 63;
944952
 
944952
   s->dictionary = BrotliGetDictionary();
944952
@@ -142,17 +134,20 @@ void BrotliDecoderStateCleanup(BrotliDecoderState* s) {
944952
 }
944952
 
944952
 BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,
944952
-    HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t max_symbol,
944952
-    uint32_t ntrees) {
944952
-  /* Pack two allocations into one */
944952
-  const size_t max_table_size = kMaxHuffmanTableSize[(alphabet_size + 31) >> 5];
944952
+    HuffmanTreeGroup* group, uint32_t alphabet_size_max,
944952
+    uint32_t alphabet_size_limit, uint32_t ntrees) {
944952
+  /* 376 = 256 (1-st level table) + 4 + 7 + 15 + 31 + 63 (2-nd level mix-tables)
944952
+     This number is discovered "unlimited" "enough" calculator; it is actually
944952
+     a wee bigger than required in several cases (especially for alphabets with
944952
+     less than 16 symbols). */
944952
+  const size_t max_table_size = alphabet_size_limit + 376;
944952
   const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;
944952
   const size_t htree_size = sizeof(HuffmanCode*) * ntrees;
944952
   /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */
944952
   HuffmanCode** p = (HuffmanCode**)BROTLI_DECODER_ALLOC(s,
944952
       code_size + htree_size);
944952
-  group->alphabet_size = (uint16_t)alphabet_size;
944952
-  group->max_symbol = (uint16_t)max_symbol;
944952
+  group->alphabet_size_max = (uint16_t)alphabet_size_max;
944952
+  group->alphabet_size_limit = (uint16_t)alphabet_size_limit;
944952
   group->num_htrees = (uint16_t)ntrees;
944952
   group->htrees = p;
944952
   group->codes = (HuffmanCode*)(&p[ntrees]);
944952
diff --git a/c/dec/state.h b/c/dec/state.h
944952
index d28b639..54dab69 100644
944952
--- a/c/dec/state.h
944952
+++ b/c/dec/state.h
944952
@@ -21,6 +21,95 @@
944952
 extern "C" {
944952
 #endif
944952
 
944952
+/* Graphviz diagram that describes state transitions:
944952
+
944952
+digraph States {
944952
+  graph [compound=true]
944952
+  concentrate=true
944952
+  node [shape="box"]
944952
+
944952
+  UNINITED -> {LARGE_WINDOW_BITS -> INITIALIZE}
944952
+  subgraph cluster_metablock_workflow {
944952
+    style="rounded"
944952
+    label=< METABLOCK CYCLE >
944952
+    METABLOCK_BEGIN -> METABLOCK_HEADER
944952
+    METABLOCK_HEADER:sw -> METADATA
944952
+    METABLOCK_HEADER:s -> UNCOMPRESSED
944952
+    METABLOCK_HEADER:se -> METABLOCK_DONE:ne
944952
+    METADATA:s -> METABLOCK_DONE:w
944952
+    UNCOMPRESSED:s -> METABLOCK_DONE:n
944952
+    METABLOCK_DONE:e -> METABLOCK_BEGIN:e [constraint="false"]
944952
+  }
944952
+  INITIALIZE -> METABLOCK_BEGIN
944952
+  METABLOCK_DONE -> DONE
944952
+
944952
+  subgraph cluster_compressed_metablock {
944952
+    style="rounded"
944952
+    label=< COMPRESSED METABLOCK >
944952
+
944952
+    subgraph cluster_command {
944952
+      style="rounded"
944952
+      label=< HOT LOOP >
944952
+
944952
+      _METABLOCK_DONE_PORT_ [shape=point style=invis]
944952
+
944952
+      {
944952
+        // Set different shape for nodes returning from "compressed metablock".
944952
+        node [shape=invhouse]; CMD_INNER CMD_POST_DECODE_LITERALS;
944952
+        CMD_POST_WRAP_COPY; CMD_INNER_WRITE; CMD_POST_WRITE_1;
944952
+      }
944952
+
944952
+      CMD_BEGIN -> CMD_INNER -> CMD_POST_DECODE_LITERALS -> CMD_POST_WRAP_COPY
944952
+
944952
+      // IO ("write") nodes are not in the hot loop!
944952
+      CMD_INNER_WRITE [style=dashed]
944952
+      CMD_INNER -> CMD_INNER_WRITE
944952
+      CMD_POST_WRITE_1 [style=dashed]
944952
+      CMD_POST_DECODE_LITERALS -> CMD_POST_WRITE_1
944952
+      CMD_POST_WRITE_2 [style=dashed]
944952
+      CMD_POST_WRAP_COPY -> CMD_POST_WRITE_2
944952
+
944952
+      CMD_POST_WRITE_1 -> CMD_BEGIN:s [constraint="false"]
944952
+      CMD_INNER_WRITE -> {CMD_INNER CMD_POST_DECODE_LITERALS}
944952
+          [constraint="false"]
944952
+      CMD_BEGIN:ne -> CMD_POST_DECODE_LITERALS [constraint="false"]
944952
+      CMD_POST_WRAP_COPY -> CMD_BEGIN [constraint="false"]
944952
+      CMD_POST_DECODE_LITERALS -> CMD_BEGIN:ne [constraint="false"]
944952
+      CMD_POST_WRITE_2 -> CMD_POST_WRAP_COPY [constraint="false"]
944952
+      {rank=same; CMD_BEGIN; CMD_INNER; CMD_POST_DECODE_LITERALS;
944952
+          CMD_POST_WRAP_COPY}
944952
+      {rank=same; CMD_INNER_WRITE; CMD_POST_WRITE_1; CMD_POST_WRITE_2}
944952
+
944952
+      {CMD_INNER CMD_POST_DECODE_LITERALS CMD_POST_WRAP_COPY} ->
944952
+          _METABLOCK_DONE_PORT_ [style=invis]
944952
+      {CMD_INNER_WRITE CMD_POST_WRITE_1} -> _METABLOCK_DONE_PORT_
944952
+          [constraint="false" style=invis]
944952
+    }
944952
+
944952
+    BEFORE_COMPRESSED_METABLOCK_HEADER:s -> HUFFMAN_CODE_0:n
944952
+    HUFFMAN_CODE_0 -> HUFFMAN_CODE_1 -> HUFFMAN_CODE_2 -> HUFFMAN_CODE_3
944952
+    HUFFMAN_CODE_0 -> METABLOCK_HEADER_2 -> CONTEXT_MODES -> CONTEXT_MAP_1
944952
+    CONTEXT_MAP_1 -> CONTEXT_MAP_2 -> TREE_GROUP
944952
+    TREE_GROUP -> BEFORE_COMPRESSED_METABLOCK_BODY:e
944952
+    BEFORE_COMPRESSED_METABLOCK_BODY:s -> CMD_BEGIN:n
944952
+
944952
+    HUFFMAN_CODE_3:e -> HUFFMAN_CODE_0:ne [constraint="false"]
944952
+    {rank=same; HUFFMAN_CODE_0; HUFFMAN_CODE_1; HUFFMAN_CODE_2; HUFFMAN_CODE_3}
944952
+    {rank=same; METABLOCK_HEADER_2; CONTEXT_MODES; CONTEXT_MAP_1; CONTEXT_MAP_2;
944952
+        TREE_GROUP}
944952
+  }
944952
+  METABLOCK_HEADER:e -> BEFORE_COMPRESSED_METABLOCK_HEADER:n
944952
+
944952
+  _METABLOCK_DONE_PORT_ -> METABLOCK_DONE:se
944952
+      [constraint="false" ltail=cluster_command]
944952
+
944952
+  UNINITED [shape=Mdiamond];
944952
+  DONE [shape=Msquare];
944952
+}
944952
+
944952
+
944952
+ */
944952
+
944952
 typedef enum {
944952
   BROTLI_STATE_UNINITED,
944952
   BROTLI_STATE_LARGE_WINDOW_BITS,
944952
@@ -39,6 +128,7 @@ typedef enum {
944952
   BROTLI_STATE_METABLOCK_DONE,
944952
   BROTLI_STATE_COMMAND_POST_WRITE_1,
944952
   BROTLI_STATE_COMMAND_POST_WRITE_2,
944952
+  BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_HEADER,
944952
   BROTLI_STATE_HUFFMAN_CODE_0,
944952
   BROTLI_STATE_HUFFMAN_CODE_1,
944952
   BROTLI_STATE_HUFFMAN_CODE_2,
944952
@@ -46,6 +136,7 @@ typedef enum {
944952
   BROTLI_STATE_CONTEXT_MAP_1,
944952
   BROTLI_STATE_CONTEXT_MAP_2,
944952
   BROTLI_STATE_TREE_GROUP,
944952
+  BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_BODY,
944952
   BROTLI_STATE_DONE
944952
 } BrotliRunningState;
944952
 
944952
@@ -98,6 +189,50 @@ typedef enum {
944952
   BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX
944952
 } BrotliRunningReadBlockLengthState;
944952
 
944952
+typedef struct BrotliMetablockHeaderArena {
944952
+  BrotliRunningTreeGroupState substate_tree_group;
944952
+  BrotliRunningContextMapState substate_context_map;
944952
+  BrotliRunningHuffmanState substate_huffman;
944952
+
944952
+  uint32_t sub_loop_counter;
944952
+
944952
+  uint32_t repeat_code_len;
944952
+  uint32_t prev_code_len;
944952
+
944952
+  /* For ReadHuffmanCode. */
944952
+  uint32_t symbol;
944952
+  uint32_t repeat;
944952
+  uint32_t space;
944952
+
944952
+  /* Huffman table for "histograms". */
944952
+  HuffmanCode table[32];
944952
+  /* List of heads of symbol chains. */
944952
+  uint16_t* symbol_lists;
944952
+  /* Storage from symbol_lists. */
944952
+  uint16_t symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1 +
944952
+                               BROTLI_NUM_COMMAND_SYMBOLS];
944952
+  /* Tails of symbol chains. */
944952
+  int next_symbol[32];
944952
+  uint8_t code_length_code_lengths[BROTLI_CODE_LENGTH_CODES];
944952
+  /* Population counts for the code lengths. */
944952
+  uint16_t code_length_histo[16];
944952
+
944952
+  /* For HuffmanTreeGroupDecode. */
944952
+  int htree_index;
944952
+  HuffmanCode* next;
944952
+
944952
+  /* For DecodeContextMap. */
944952
+  uint32_t context_index;
944952
+  uint32_t max_run_length_prefix;
944952
+  uint32_t code;
944952
+  HuffmanCode context_map_table[BROTLI_HUFFMAN_MAX_SIZE_272];
944952
+} BrotliMetablockHeaderArena;
944952
+
944952
+typedef struct BrotliMetablockBodyArena {
944952
+  uint8_t dist_extra_bits[544];
944952
+  uint32_t dist_offset[544];
944952
+} BrotliMetablockBodyArena;
944952
+
944952
 struct BrotliDecoderStateStruct {
944952
   BrotliRunningState state;
944952
 
944952
@@ -110,7 +245,8 @@ struct BrotliDecoderStateStruct {
944952
   brotli_free_func free_func;
944952
   void* memory_manager_opaque;
944952
 
944952
-  /* Temporary storage for remaining input. */
944952
+  /* Temporary storage for remaining input. Brotli stream format is designed in
944952
+     a way, that 64 bits are enough to make progress in decoding. */
944952
   union {
944952
     uint64_t u64;
944952
     uint8_t u8[8];
944952
@@ -125,7 +261,6 @@ struct BrotliDecoderStateStruct {
944952
   int dist_rb_idx;
944952
   int dist_rb[4];
944952
   int error_code;
944952
-  uint32_t sub_loop_counter;
944952
   uint8_t* ringbuffer;
944952
   uint8_t* ringbuffer_end;
944952
   HuffmanCode* htree_command;
944952
@@ -153,13 +288,10 @@ struct BrotliDecoderStateStruct {
944952
   uint32_t block_type_rb[6];
944952
   uint32_t distance_postfix_bits;
944952
   uint32_t num_direct_distance_codes;
944952
-  int distance_postfix_mask;
944952
   uint32_t num_dist_htrees;
944952
   uint8_t* dist_context_map;
944952
   HuffmanCode* literal_htree;
944952
   uint8_t dist_htree_index;
944952
-  uint32_t repeat_code_len;
944952
-  uint32_t prev_code_len;
944952
 
944952
   int copy_length;
944952
   int distance_code;
944952
@@ -168,33 +300,6 @@ struct BrotliDecoderStateStruct {
944952
   size_t rb_roundtrips;  /* how many times we went around the ring-buffer */
944952
   size_t partial_pos_out;  /* how much output to the user in total */
944952
 
944952
-  /* For ReadHuffmanCode. */
944952
-  uint32_t symbol;
944952
-  uint32_t repeat;
944952
-  uint32_t space;
944952
-
944952
-  HuffmanCode table[32];
944952
-  /* List of heads of symbol chains. */
944952
-  uint16_t* symbol_lists;
944952
-  /* Storage from symbol_lists. */
944952
-  uint16_t symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1 +
944952
-                               BROTLI_NUM_COMMAND_SYMBOLS];
944952
-  /* Tails of symbol chains. */
944952
-  int next_symbol[32];
944952
-  uint8_t code_length_code_lengths[BROTLI_CODE_LENGTH_CODES];
944952
-  /* Population counts for the code lengths. */
944952
-  uint16_t code_length_histo[16];
944952
-
944952
-  /* For HuffmanTreeGroupDecode. */
944952
-  int htree_index;
944952
-  HuffmanCode* next;
944952
-
944952
-  /* For DecodeContextMap. */
944952
-  uint32_t context_index;
944952
-  uint32_t max_run_length_prefix;
944952
-  uint32_t code;
944952
-  HuffmanCode context_map_table[BROTLI_HUFFMAN_MAX_SIZE_272];
944952
-
944952
   /* For InverseMoveToFrontTransform. */
944952
   uint32_t mtf_upper_bound;
944952
   uint32_t mtf[64 + 1];
944952
@@ -203,10 +308,7 @@ struct BrotliDecoderStateStruct {
944952
 
944952
   /* States inside function calls. */
944952
   BrotliRunningMetablockHeaderState substate_metablock_header;
944952
-  BrotliRunningTreeGroupState substate_tree_group;
944952
-  BrotliRunningContextMapState substate_context_map;
944952
   BrotliRunningUncompressedState substate_uncompressed;
944952
-  BrotliRunningHuffmanState substate_huffman;
944952
   BrotliRunningDecodeUint8State substate_decode_uint8;
944952
   BrotliRunningReadBlockLengthState substate_read_block_length;
944952
 
944952
@@ -229,6 +331,11 @@ struct BrotliDecoderStateStruct {
944952
   const BrotliTransforms* transforms;
944952
 
944952
   uint32_t trivial_literal_contexts[8];  /* 256 bits */
944952
+
944952
+  union {
944952
+    BrotliMetablockHeaderArena header;
944952
+    BrotliMetablockBodyArena body;
944952
+  } arena;
944952
 };
944952
 
944952
 typedef struct BrotliDecoderStateStruct BrotliDecoderStateInternal;
944952
@@ -241,8 +348,8 @@ BROTLI_INTERNAL void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s);
944952
 BROTLI_INTERNAL void BrotliDecoderStateCleanupAfterMetablock(
944952
     BrotliDecoderState* s);
944952
 BROTLI_INTERNAL BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(
944952
-    BrotliDecoderState* s, HuffmanTreeGroup* group, uint32_t alphabet_size,
944952
-    uint32_t max_symbol, uint32_t ntrees);
944952
+    BrotliDecoderState* s, HuffmanTreeGroup* group, uint32_t alphabet_size_max,
944952
+    uint32_t alphabet_size_limit, uint32_t ntrees);
944952
 
944952
 #define BROTLI_DECODER_ALLOC(S, L) S->alloc_func(S->memory_manager_opaque, L)
944952
 
944952
diff --git a/c/enc/brotli_bit_stream.c b/c/enc/brotli_bit_stream.c
944952
index aaf2dad..8e68059 100644
944952
--- a/c/enc/brotli_bit_stream.c
944952
+++ b/c/enc/brotli_bit_stream.c
944952
@@ -34,33 +34,18 @@ extern "C" {
944952
   BROTLI_DISTANCE_ALPHABET_SIZE(0, 0, BROTLI_LARGE_MAX_DISTANCE_BITS)
944952
 /* MAX_SIMPLE_DISTANCE_ALPHABET_SIZE == 140 */
944952
 
944952
-/* Represents the range of values belonging to a prefix code:
944952
-   [offset, offset + 2^nbits) */
944952
-typedef struct PrefixCodeRange {
944952
-  uint32_t offset;
944952
-  uint32_t nbits;
944952
-} PrefixCodeRange;
944952
-
944952
-static const PrefixCodeRange
944952
-    kBlockLengthPrefixCode[BROTLI_NUM_BLOCK_LEN_SYMBOLS] = {
944952
-  { 1, 2}, { 5, 2}, { 9, 2}, {13, 2}, {17, 3}, { 25, 3}, { 33, 3},
944952
-  {41, 3}, {49, 4}, {65, 4}, {81, 4}, {97, 4}, {113, 5}, {145, 5},
944952
-  {177, 5}, { 209,  5}, { 241,  6}, { 305,  6}, { 369,  7}, {  497,  8},
944952
-  {753, 9}, {1265, 10}, {2289, 11}, {4337, 12}, {8433, 13}, {16625, 24}
944952
-};
944952
-
944952
 static BROTLI_INLINE uint32_t BlockLengthPrefixCode(uint32_t len) {
944952
   uint32_t code = (len >= 177) ? (len >= 753 ? 20 : 14) : (len >= 41 ? 7 : 0);
944952
   while (code < (BROTLI_NUM_BLOCK_LEN_SYMBOLS - 1) &&
944952
-      len >= kBlockLengthPrefixCode[code + 1].offset) ++code;
944952
+      len >= _kBrotliPrefixCodeRanges[code + 1].offset) ++code;
944952
   return code;
944952
 }
944952
 
944952
 static BROTLI_INLINE void GetBlockLengthPrefixCode(uint32_t len, size_t* code,
944952
     uint32_t* n_extra, uint32_t* extra) {
944952
   *code = BlockLengthPrefixCode(len);
944952
-  *n_extra = kBlockLengthPrefixCode[*code].nbits;
944952
-  *extra = len - kBlockLengthPrefixCode[*code].offset;
944952
+  *n_extra = _kBrotliPrefixCodeRanges[*code].nbits;
944952
+  *extra = len - _kBrotliPrefixCodeRanges[*code].offset;
944952
 }
944952
 
944952
 typedef struct BlockTypeCodeCalculator {
944952
diff --git a/research/brotli_decoder.c b/research/brotli_decoder.c
944952
index b1d556d..4b0bc4a 100644
944952
--- a/research/brotli_decoder.c
944952
+++ b/research/brotli_decoder.c
944952
@@ -38,6 +38,7 @@ void cleanup(Context* ctx) {
944952
 
944952
 void fail(Context* ctx, const char* message) {
944952
   fprintf(stderr, "%s\n", message);
944952
+  cleanup(ctx);
944952
   exit(1);
944952
 }
944952
 
944952
diff --git a/scripts/sources.lst b/scripts/sources.lst
944952
index 5e8e817..cd0b343 100644
944952
--- a/scripts/sources.lst
944952
+++ b/scripts/sources.lst
944952
@@ -5,6 +5,7 @@ BROTLI_CLI_C = \
944952
   c/tools/brotli.c
944952
 
944952
 BROTLI_COMMON_C = \
944952
+  c/common/constants.c \
944952
   c/common/dictionary.c \
944952
   c/common/transform.c
944952
 
944952
diff --git a/setup.py b/setup.py
944952
index 1491db3..7bd6314 100644
944952
--- a/setup.py
944952
+++ b/setup.py
944952
@@ -181,6 +181,7 @@ EXT_MODULES = [
944952
         '_brotli',
944952
         sources=[
944952
             'python/_brotli.cc',
944952
+            'c/common/constants.c',
944952
             'c/common/dictionary.c',
944952
             'c/common/transform.c',
944952
             'c/dec/bit_reader.c',
944952
-- 
944952
2.25.4
944952