Home | History | Annotate | Download | only in enc
      1 /* Copyright 2014 Google Inc. All Rights Reserved.
      2 
      3    Distributed under MIT license.
      4    See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
      5 */
      6 
      7 /* Brotli bit stream functions to support the low level format. There are no
      8    compression algorithms here, just the right ordering of bits to match the
      9    specs. */
     10 
     11 #include "./brotli_bit_stream.h"
     12 
     13 #include <string.h>  /* memcpy, memset */
     14 
     15 #include "../common/constants.h"
     16 #include <brotli/types.h>
     17 #include "./context.h"
     18 #include "./entropy_encode.h"
     19 #include "./entropy_encode_static.h"
     20 #include "./fast_log.h"
     21 #include "./memory.h"
     22 #include "./port.h"
     23 #include "./write_bits.h"
     24 
     25 #if defined(__cplusplus) || defined(c_plusplus)
     26 extern "C" {
     27 #endif
     28 
     29 #define MAX_HUFFMAN_TREE_SIZE (2 * BROTLI_NUM_COMMAND_SYMBOLS + 1)
     30 /* The size of Huffman dictionary for distances assuming that NPOSTFIX = 0 and
     31  NDIRECT = 0. */
     32 #define SIMPLE_DISTANCE_ALPHABET_SIZE (BROTLI_NUM_DISTANCE_SHORT_CODES + \
     33                                        (2 * BROTLI_MAX_DISTANCE_BITS))
     34 /* SIMPLE_DISTANCE_ALPHABET_SIZE == 64 */
     35 #define SIMPLE_DISTANCE_ALPHABET_BITS 6
     36 
     37 /* Represents the range of values belonging to a prefix code:
     38    [offset, offset + 2^nbits) */
     39 typedef struct PrefixCodeRange {
     40   uint32_t offset;
     41   uint32_t nbits;
     42 } PrefixCodeRange;
     43 
     44 static const PrefixCodeRange
     45     kBlockLengthPrefixCode[BROTLI_NUM_BLOCK_LEN_SYMBOLS] = {
     46   { 1, 2}, { 5, 2}, { 9, 2}, {13, 2}, {17, 3}, { 25, 3}, { 33, 3},
     47   {41, 3}, {49, 4}, {65, 4}, {81, 4}, {97, 4}, {113, 5}, {145, 5},
     48   {177, 5}, { 209,  5}, { 241,  6}, { 305,  6}, { 369,  7}, {  497,  8},
     49   {753, 9}, {1265, 10}, {2289, 11}, {4337, 12}, {8433, 13}, {16625, 24}
     50 };
     51 
     52 static BROTLI_INLINE uint32_t BlockLengthPrefixCode(uint32_t len) {
     53   uint32_t code = (len >= 177) ? (len >= 753 ? 20 : 14) : (len >= 41 ? 7 : 0);
     54   while (code < (BROTLI_NUM_BLOCK_LEN_SYMBOLS - 1) &&
     55       len >= kBlockLengthPrefixCode[code + 1].offset) ++code;
     56   return code;
     57 }
     58 
     59 static BROTLI_INLINE void GetBlockLengthPrefixCode(uint32_t len, size_t* code,
     60     uint32_t* n_extra, uint32_t* extra) {
     61   *code = BlockLengthPrefixCode(len);
     62   *n_extra = kBlockLengthPrefixCode[*code].nbits;
     63   *extra = len - kBlockLengthPrefixCode[*code].offset;
     64 }
     65 
     66 typedef struct BlockTypeCodeCalculator {
     67   size_t last_type;
     68   size_t second_last_type;
     69 } BlockTypeCodeCalculator;
     70 
     71 static void InitBlockTypeCodeCalculator(BlockTypeCodeCalculator* self) {
     72   self->last_type = 1;
     73   self->second_last_type = 0;
     74 }
     75 
     76 static BROTLI_INLINE size_t NextBlockTypeCode(
     77     BlockTypeCodeCalculator* calculator, uint8_t type) {
     78   size_t type_code = (type == calculator->last_type + 1) ? 1u :
     79       (type == calculator->second_last_type) ? 0u : type + 2u;
     80   calculator->second_last_type = calculator->last_type;
     81   calculator->last_type = type;
     82   return type_code;
     83 }
     84 
     85 /* |nibblesbits| represents the 2 bits to encode MNIBBLES (0-3)
     86    REQUIRES: length > 0
     87    REQUIRES: length <= (1 << 24) */
     88 static void BrotliEncodeMlen(size_t length, uint64_t* bits,
     89                              size_t* numbits, uint64_t* nibblesbits) {
     90   size_t lg = (length == 1) ? 1 : Log2FloorNonZero((uint32_t)(length - 1)) + 1;
     91   size_t mnibbles = (lg < 16 ? 16 : (lg + 3)) / 4;
     92   assert(length > 0);
     93   assert(length <= (1 << 24));
     94   assert(lg <= 24);
     95   *nibblesbits = mnibbles - 4;
     96   *numbits = mnibbles * 4;
     97   *bits = length - 1;
     98 }
     99 
    100 static BROTLI_INLINE void StoreCommandExtra(
    101     const Command* cmd, size_t* storage_ix, uint8_t* storage) {
    102   uint32_t copylen_code = CommandCopyLenCode(cmd);
    103   uint16_t inscode = GetInsertLengthCode(cmd->insert_len_);
    104   uint16_t copycode = GetCopyLengthCode(copylen_code);
    105   uint32_t insnumextra = GetInsertExtra(inscode);
    106   uint64_t insextraval = cmd->insert_len_ - GetInsertBase(inscode);
    107   uint64_t copyextraval = copylen_code - GetCopyBase(copycode);
    108   uint64_t bits = (copyextraval << insnumextra) | insextraval;
    109   BrotliWriteBits(
    110       insnumextra + GetCopyExtra(copycode), bits, storage_ix, storage);
    111 }
    112 
    113 /* Data structure that stores almost everything that is needed to encode each
    114    block switch command. */
    115 typedef struct BlockSplitCode {
    116   BlockTypeCodeCalculator type_code_calculator;
    117   uint8_t type_depths[BROTLI_MAX_BLOCK_TYPE_SYMBOLS];
    118   uint16_t type_bits[BROTLI_MAX_BLOCK_TYPE_SYMBOLS];
    119   uint8_t length_depths[BROTLI_NUM_BLOCK_LEN_SYMBOLS];
    120   uint16_t length_bits[BROTLI_NUM_BLOCK_LEN_SYMBOLS];
    121 } BlockSplitCode;
    122 
    123 /* Stores a number between 0 and 255. */
    124 static void StoreVarLenUint8(size_t n, size_t* storage_ix, uint8_t* storage) {
    125   if (n == 0) {
    126     BrotliWriteBits(1, 0, storage_ix, storage);
    127   } else {
    128     size_t nbits = Log2FloorNonZero(n);
    129     BrotliWriteBits(1, 1, storage_ix, storage);
    130     BrotliWriteBits(3, nbits, storage_ix, storage);
    131     BrotliWriteBits(nbits, n - ((size_t)1 << nbits), storage_ix, storage);
    132   }
    133 }
    134 
    135 /* Stores the compressed meta-block header.
    136    REQUIRES: length > 0
    137    REQUIRES: length <= (1 << 24) */
    138 static void StoreCompressedMetaBlockHeader(BROTLI_BOOL is_final_block,
    139                                            size_t length,
    140                                            size_t* storage_ix,
    141                                            uint8_t* storage) {
    142   uint64_t lenbits;
    143   size_t nlenbits;
    144   uint64_t nibblesbits;
    145 
    146   /* Write ISLAST bit. */
    147   BrotliWriteBits(1, (uint64_t)is_final_block, storage_ix, storage);
    148   /* Write ISEMPTY bit. */
    149   if (is_final_block) {
    150     BrotliWriteBits(1, 0, storage_ix, storage);
    151   }
    152 
    153   BrotliEncodeMlen(length, &lenbits, &nlenbits, &nibblesbits);
    154   BrotliWriteBits(2, nibblesbits, storage_ix, storage);
    155   BrotliWriteBits(nlenbits, lenbits, storage_ix, storage);
    156 
    157   if (!is_final_block) {
    158     /* Write ISUNCOMPRESSED bit. */
    159     BrotliWriteBits(1, 0, storage_ix, storage);
    160   }
    161 }
    162 
    163 /* Stores the uncompressed meta-block header.
    164    REQUIRES: length > 0
    165    REQUIRES: length <= (1 << 24) */
    166 static void BrotliStoreUncompressedMetaBlockHeader(size_t length,
    167                                                    size_t* storage_ix,
    168                                                    uint8_t* storage) {
    169   uint64_t lenbits;
    170   size_t nlenbits;
    171   uint64_t nibblesbits;
    172 
    173   /* Write ISLAST bit.
    174      Uncompressed block cannot be the last one, so set to 0. */
    175   BrotliWriteBits(1, 0, storage_ix, storage);
    176   BrotliEncodeMlen(length, &lenbits, &nlenbits, &nibblesbits);
    177   BrotliWriteBits(2, nibblesbits, storage_ix, storage);
    178   BrotliWriteBits(nlenbits, lenbits, storage_ix, storage);
    179   /* Write ISUNCOMPRESSED bit. */
    180   BrotliWriteBits(1, 1, storage_ix, storage);
    181 }
    182 
    183 static void BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask(
    184     const int num_codes, const uint8_t* code_length_bitdepth,
    185     size_t* storage_ix, uint8_t* storage) {
    186   static const uint8_t kStorageOrder[BROTLI_CODE_LENGTH_CODES] = {
    187     1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15
    188   };
    189   /* The bit lengths of the Huffman code over the code length alphabet
    190      are compressed with the following static Huffman code:
    191        Symbol   Code
    192        ------   ----
    193        0          00
    194        1        1110
    195        2         110
    196        3          01
    197        4          10
    198        5        1111 */
    199   static const uint8_t kHuffmanBitLengthHuffmanCodeSymbols[6] = {
    200      0, 7, 3, 2, 1, 15
    201   };
    202   static const uint8_t kHuffmanBitLengthHuffmanCodeBitLengths[6] = {
    203     2, 4, 3, 2, 2, 4
    204   };
    205 
    206   size_t skip_some = 0;  /* skips none. */
    207 
    208   /* Throw away trailing zeros: */
    209   size_t codes_to_store = BROTLI_CODE_LENGTH_CODES;
    210   if (num_codes > 1) {
    211     for (; codes_to_store > 0; --codes_to_store) {
    212       if (code_length_bitdepth[kStorageOrder[codes_to_store - 1]] != 0) {
    213         break;
    214       }
    215     }
    216   }
    217   if (code_length_bitdepth[kStorageOrder[0]] == 0 &&
    218       code_length_bitdepth[kStorageOrder[1]] == 0) {
    219     skip_some = 2;  /* skips two. */
    220     if (code_length_bitdepth[kStorageOrder[2]] == 0) {
    221       skip_some = 3;  /* skips three. */
    222     }
    223   }
    224   BrotliWriteBits(2, skip_some, storage_ix, storage);
    225   {
    226     size_t i;
    227     for (i = skip_some; i < codes_to_store; ++i) {
    228       size_t l = code_length_bitdepth[kStorageOrder[i]];
    229       BrotliWriteBits(kHuffmanBitLengthHuffmanCodeBitLengths[l],
    230           kHuffmanBitLengthHuffmanCodeSymbols[l], storage_ix, storage);
    231     }
    232   }
    233 }
    234 
    235 static void BrotliStoreHuffmanTreeToBitMask(
    236     const size_t huffman_tree_size, const uint8_t* huffman_tree,
    237     const uint8_t* huffman_tree_extra_bits, const uint8_t* code_length_bitdepth,
    238     const uint16_t* code_length_bitdepth_symbols,
    239     size_t* BROTLI_RESTRICT storage_ix, uint8_t* BROTLI_RESTRICT storage) {
    240   size_t i;
    241   for (i = 0; i < huffman_tree_size; ++i) {
    242     size_t ix = huffman_tree[i];
    243     BrotliWriteBits(code_length_bitdepth[ix], code_length_bitdepth_symbols[ix],
    244                     storage_ix, storage);
    245     /* Extra bits */
    246     switch (ix) {
    247       case BROTLI_REPEAT_PREVIOUS_CODE_LENGTH:
    248         BrotliWriteBits(2, huffman_tree_extra_bits[i], storage_ix, storage);
    249         break;
    250       case BROTLI_REPEAT_ZERO_CODE_LENGTH:
    251         BrotliWriteBits(3, huffman_tree_extra_bits[i], storage_ix, storage);
    252         break;
    253     }
    254   }
    255 }
    256 
    257 static void StoreSimpleHuffmanTree(const uint8_t* depths,
    258                                    size_t symbols[4],
    259                                    size_t num_symbols,
    260                                    size_t max_bits,
    261                                    size_t *storage_ix, uint8_t *storage) {
    262   /* value of 1 indicates a simple Huffman code */
    263   BrotliWriteBits(2, 1, storage_ix, storage);
    264   BrotliWriteBits(2, num_symbols - 1, storage_ix, storage);  /* NSYM - 1 */
    265 
    266   {
    267     /* Sort */
    268     size_t i;
    269     for (i = 0; i < num_symbols; i++) {
    270       size_t j;
    271       for (j = i + 1; j < num_symbols; j++) {
    272         if (depths[symbols[j]] < depths[symbols[i]]) {
    273           BROTLI_SWAP(size_t, symbols, j, i);
    274         }
    275       }
    276     }
    277   }
    278 
    279   if (num_symbols == 2) {
    280     BrotliWriteBits(max_bits, symbols[0], storage_ix, storage);
    281     BrotliWriteBits(max_bits, symbols[1], storage_ix, storage);
    282   } else if (num_symbols == 3) {
    283     BrotliWriteBits(max_bits, symbols[0], storage_ix, storage);
    284     BrotliWriteBits(max_bits, symbols[1], storage_ix, storage);
    285     BrotliWriteBits(max_bits, symbols[2], storage_ix, storage);
    286   } else {
    287     BrotliWriteBits(max_bits, symbols[0], storage_ix, storage);
    288     BrotliWriteBits(max_bits, symbols[1], storage_ix, storage);
    289     BrotliWriteBits(max_bits, symbols[2], storage_ix, storage);
    290     BrotliWriteBits(max_bits, symbols[3], storage_ix, storage);
    291     /* tree-select */
    292     BrotliWriteBits(1, depths[symbols[0]] == 1 ? 1 : 0, storage_ix, storage);
    293   }
    294 }
    295 
    296 /* num = alphabet size
    297    depths = symbol depths */
    298 void BrotliStoreHuffmanTree(const uint8_t* depths, size_t num,
    299                             HuffmanTree* tree,
    300                             size_t *storage_ix, uint8_t *storage) {
    301   /* Write the Huffman tree into the brotli-representation.
    302      The command alphabet is the largest, so this allocation will fit all
    303      alphabets. */
    304   uint8_t huffman_tree[BROTLI_NUM_COMMAND_SYMBOLS];
    305   uint8_t huffman_tree_extra_bits[BROTLI_NUM_COMMAND_SYMBOLS];
    306   size_t huffman_tree_size = 0;
    307   uint8_t code_length_bitdepth[BROTLI_CODE_LENGTH_CODES] = { 0 };
    308   uint16_t code_length_bitdepth_symbols[BROTLI_CODE_LENGTH_CODES];
    309   uint32_t huffman_tree_histogram[BROTLI_CODE_LENGTH_CODES] = { 0 };
    310   size_t i;
    311   int num_codes = 0;
    312   size_t code = 0;
    313 
    314   assert(num <= BROTLI_NUM_COMMAND_SYMBOLS);
    315 
    316   BrotliWriteHuffmanTree(depths, num, &huffman_tree_size, huffman_tree,
    317                          huffman_tree_extra_bits);
    318 
    319   /* Calculate the statistics of the Huffman tree in brotli-representation. */
    320   for (i = 0; i < huffman_tree_size; ++i) {
    321     ++huffman_tree_histogram[huffman_tree[i]];
    322   }
    323 
    324   for (i = 0; i < BROTLI_CODE_LENGTH_CODES; ++i) {
    325     if (huffman_tree_histogram[i]) {
    326       if (num_codes == 0) {
    327         code = i;
    328         num_codes = 1;
    329       } else if (num_codes == 1) {
    330         num_codes = 2;
    331         break;
    332       }
    333     }
    334   }
    335 
    336   /* Calculate another Huffman tree to use for compressing both the
    337      earlier Huffman tree with. */
    338   BrotliCreateHuffmanTree(huffman_tree_histogram, BROTLI_CODE_LENGTH_CODES,
    339                           5, tree, code_length_bitdepth);
    340   BrotliConvertBitDepthsToSymbols(code_length_bitdepth,
    341                                   BROTLI_CODE_LENGTH_CODES,
    342                                   code_length_bitdepth_symbols);
    343 
    344   /* Now, we have all the data, let's start storing it */
    345   BrotliStoreHuffmanTreeOfHuffmanTreeToBitMask(num_codes, code_length_bitdepth,
    346                                                storage_ix, storage);
    347 
    348   if (num_codes == 1) {
    349     code_length_bitdepth[code] = 0;
    350   }
    351 
    352   /* Store the real Huffman tree now. */
    353   BrotliStoreHuffmanTreeToBitMask(huffman_tree_size,
    354                                   huffman_tree,
    355                                   huffman_tree_extra_bits,
    356                                   code_length_bitdepth,
    357                                   code_length_bitdepth_symbols,
    358                                   storage_ix, storage);
    359 }
    360 
    361 /* Builds a Huffman tree from histogram[0:length] into depth[0:length] and
    362    bits[0:length] and stores the encoded tree to the bit stream. */
    363 static void BuildAndStoreHuffmanTree(const uint32_t *histogram,
    364                                      const size_t length,
    365                                      HuffmanTree* tree,
    366                                      uint8_t* depth,
    367                                      uint16_t* bits,
    368                                      size_t* storage_ix,
    369                                      uint8_t* storage) {
    370   size_t count = 0;
    371   size_t s4[4] = { 0 };
    372   size_t i;
    373   size_t max_bits = 0;
    374   for (i = 0; i < length; i++) {
    375     if (histogram[i]) {
    376       if (count < 4) {
    377         s4[count] = i;
    378       } else if (count > 4) {
    379         break;
    380       }
    381       count++;
    382     }
    383   }
    384 
    385   {
    386     size_t max_bits_counter = length - 1;
    387     while (max_bits_counter) {
    388       max_bits_counter >>= 1;
    389       ++max_bits;
    390     }
    391   }
    392 
    393   if (count <= 1) {
    394     BrotliWriteBits(4, 1, storage_ix, storage);
    395     BrotliWriteBits(max_bits, s4[0], storage_ix, storage);
    396     depth[s4[0]] = 0;
    397     bits[s4[0]] = 0;
    398     return;
    399   }
    400 
    401   memset(depth, 0, length * sizeof(depth[0]));
    402   BrotliCreateHuffmanTree(histogram, length, 15, tree, depth);
    403   BrotliConvertBitDepthsToSymbols(depth, length, bits);
    404 
    405   if (count <= 4) {
    406     StoreSimpleHuffmanTree(depth, s4, count, max_bits, storage_ix, storage);
    407   } else {
    408     BrotliStoreHuffmanTree(depth, length, tree, storage_ix, storage);
    409   }
    410 }
    411 
    412 static BROTLI_INLINE BROTLI_BOOL SortHuffmanTree(
    413     const HuffmanTree* v0, const HuffmanTree* v1) {
    414   return TO_BROTLI_BOOL(v0->total_count_ < v1->total_count_);
    415 }
    416 
    417 void BrotliBuildAndStoreHuffmanTreeFast(MemoryManager* m,
    418                                         const uint32_t* histogram,
    419                                         const size_t histogram_total,
    420                                         const size_t max_bits,
    421                                         uint8_t* depth, uint16_t* bits,
    422                                         size_t* storage_ix,
    423                                         uint8_t* storage) {
    424   size_t count = 0;
    425   size_t symbols[4] = { 0 };
    426   size_t length = 0;
    427   size_t total = histogram_total;
    428   while (total != 0) {
    429     if (histogram[length]) {
    430       if (count < 4) {
    431         symbols[count] = length;
    432       }
    433       ++count;
    434       total -= histogram[length];
    435     }
    436     ++length;
    437   }
    438 
    439   if (count <= 1) {
    440     BrotliWriteBits(4, 1, storage_ix, storage);
    441     BrotliWriteBits(max_bits, symbols[0], storage_ix, storage);
    442     depth[symbols[0]] = 0;
    443     bits[symbols[0]] = 0;
    444     return;
    445   }
    446 
    447   memset(depth, 0, length * sizeof(depth[0]));
    448   {
    449     const size_t max_tree_size = 2 * length + 1;
    450     HuffmanTree* tree = BROTLI_ALLOC(m, HuffmanTree, max_tree_size);
    451     uint32_t count_limit;
    452     if (BROTLI_IS_OOM(m)) return;
    453     for (count_limit = 1; ; count_limit *= 2) {
    454       HuffmanTree* node = tree;
    455       size_t l;
    456       for (l = length; l != 0;) {
    457         --l;
    458         if (histogram[l]) {
    459           if (BROTLI_PREDICT_TRUE(histogram[l] >= count_limit)) {
    460             InitHuffmanTree(node, histogram[l], -1, (int16_t)l);
    461           } else {
    462             InitHuffmanTree(node, count_limit, -1, (int16_t)l);
    463           }
    464           ++node;
    465         }
    466       }
    467       {
    468         const int n = (int)(node - tree);
    469         HuffmanTree sentinel;
    470         int i = 0;      /* Points to the next leaf node. */
    471         int j = n + 1;  /* Points to the next non-leaf node. */
    472         int k;
    473 
    474         SortHuffmanTreeItems(tree, (size_t)n, SortHuffmanTree);
    475         /* The nodes are:
    476            [0, n): the sorted leaf nodes that we start with.
    477            [n]: we add a sentinel here.
    478            [n + 1, 2n): new parent nodes are added here, starting from
    479                         (n+1). These are naturally in ascending order.
    480            [2n]: we add a sentinel at the end as well.
    481            There will be (2n+1) elements at the end. */
    482         InitHuffmanTree(&sentinel, BROTLI_UINT32_MAX, -1, -1);
    483         *node++ = sentinel;
    484         *node++ = sentinel;
    485 
    486         for (k = n - 1; k > 0; --k) {
    487           int left, right;
    488           if (tree[i].total_count_ <= tree[j].total_count_) {
    489             left = i;
    490             ++i;
    491           } else {
    492             left = j;
    493             ++j;
    494           }
    495           if (tree[i].total_count_ <= tree[j].total_count_) {
    496             right = i;
    497             ++i;
    498           } else {
    499             right = j;
    500             ++j;
    501           }
    502           /* The sentinel node becomes the parent node. */
    503           node[-1].total_count_ =
    504               tree[left].total_count_ + tree[right].total_count_;
    505           node[-1].index_left_ = (int16_t)left;
    506           node[-1].index_right_or_value_ = (int16_t)right;
    507           /* Add back the last sentinel node. */
    508           *node++ = sentinel;
    509         }
    510         if (BrotliSetDepth(2 * n - 1, tree, depth, 14)) {
    511           /* We need to pack the Huffman tree in 14 bits. If this was not
    512              successful, add fake entities to the lowest values and retry. */
    513           break;
    514         }
    515       }
    516     }
    517     BROTLI_FREE(m, tree);
    518   }
    519   BrotliConvertBitDepthsToSymbols(depth, length, bits);
    520   if (count <= 4) {
    521     size_t i;
    522     /* value of 1 indicates a simple Huffman code */
    523     BrotliWriteBits(2, 1, storage_ix, storage);
    524     BrotliWriteBits(2, count - 1, storage_ix, storage);  /* NSYM - 1 */
    525 
    526     /* Sort */
    527     for (i = 0; i < count; i++) {
    528       size_t j;
    529       for (j = i + 1; j < count; j++) {
    530         if (depth[symbols[j]] < depth[symbols[i]]) {
    531           BROTLI_SWAP(size_t, symbols, j, i);
    532         }
    533       }
    534     }
    535 
    536     if (count == 2) {
    537       BrotliWriteBits(max_bits, symbols[0], storage_ix, storage);
    538       BrotliWriteBits(max_bits, symbols[1], storage_ix, storage);
    539     } else if (count == 3) {
    540       BrotliWriteBits(max_bits, symbols[0], storage_ix, storage);
    541       BrotliWriteBits(max_bits, symbols[1], storage_ix, storage);
    542       BrotliWriteBits(max_bits, symbols[2], storage_ix, storage);
    543     } else {
    544       BrotliWriteBits(max_bits, symbols[0], storage_ix, storage);
    545       BrotliWriteBits(max_bits, symbols[1], storage_ix, storage);
    546       BrotliWriteBits(max_bits, symbols[2], storage_ix, storage);
    547       BrotliWriteBits(max_bits, symbols[3], storage_ix, storage);
    548       /* tree-select */
    549       BrotliWriteBits(1, depth[symbols[0]] == 1 ? 1 : 0, storage_ix, storage);
    550     }
    551   } else {
    552     uint8_t previous_value = 8;
    553     size_t i;
    554     /* Complex Huffman Tree */
    555     StoreStaticCodeLengthCode(storage_ix, storage);
    556 
    557     /* Actual RLE coding. */
    558     for (i = 0; i < length;) {
    559       const uint8_t value = depth[i];
    560       size_t reps = 1;
    561       size_t k;
    562       for (k = i + 1; k < length && depth[k] == value; ++k) {
    563         ++reps;
    564       }
    565       i += reps;
    566       if (value == 0) {
    567         BrotliWriteBits(kZeroRepsDepth[reps], kZeroRepsBits[reps],
    568                         storage_ix, storage);
    569       } else {
    570         if (previous_value != value) {
    571           BrotliWriteBits(kCodeLengthDepth[value], kCodeLengthBits[value],
    572                           storage_ix, storage);
    573           --reps;
    574         }
    575         if (reps < 3) {
    576           while (reps != 0) {
    577             reps--;
    578             BrotliWriteBits(kCodeLengthDepth[value], kCodeLengthBits[value],
    579                             storage_ix, storage);
    580           }
    581         } else {
    582           reps -= 3;
    583           BrotliWriteBits(kNonZeroRepsDepth[reps], kNonZeroRepsBits[reps],
    584                           storage_ix, storage);
    585         }
    586         previous_value = value;
    587       }
    588     }
    589   }
    590 }
    591 
    592 static size_t IndexOf(const uint8_t* v, size_t v_size, uint8_t value) {
    593   size_t i = 0;
    594   for (; i < v_size; ++i) {
    595     if (v[i] == value) return i;
    596   }
    597   return i;
    598 }
    599 
    600 static void MoveToFront(uint8_t* v, size_t index) {
    601   uint8_t value = v[index];
    602   size_t i;
    603   for (i = index; i != 0; --i) {
    604     v[i] = v[i - 1];
    605   }
    606   v[0] = value;
    607 }
    608 
    609 static void MoveToFrontTransform(const uint32_t* BROTLI_RESTRICT v_in,
    610                                  const size_t v_size,
    611                                  uint32_t* v_out) {
    612   size_t i;
    613   uint8_t mtf[256];
    614   uint32_t max_value;
    615   if (v_size == 0) {
    616     return;
    617   }
    618   max_value = v_in[0];
    619   for (i = 1; i < v_size; ++i) {
    620     if (v_in[i] > max_value) max_value = v_in[i];
    621   }
    622   assert(max_value < 256u);
    623   for (i = 0; i <= max_value; ++i) {
    624     mtf[i] = (uint8_t)i;
    625   }
    626   {
    627     size_t mtf_size = max_value + 1;
    628     for (i = 0; i < v_size; ++i) {
    629       size_t index = IndexOf(mtf, mtf_size, (uint8_t)v_in[i]);
    630       assert(index < mtf_size);
    631       v_out[i] = (uint32_t)index;
    632       MoveToFront(mtf, index);
    633     }
    634   }
    635 }
    636 
    637 /* Finds runs of zeros in v[0..in_size) and replaces them with a prefix code of
    638    the run length plus extra bits (lower 9 bits is the prefix code and the rest
    639    are the extra bits). Non-zero values in v[] are shifted by
    640    *max_length_prefix. Will not create prefix codes bigger than the initial
    641    value of *max_run_length_prefix. The prefix code of run length L is simply
    642    Log2Floor(L) and the number of extra bits is the same as the prefix code. */
    643 static void RunLengthCodeZeros(const size_t in_size,
    644     uint32_t* BROTLI_RESTRICT v, size_t* BROTLI_RESTRICT out_size,
    645     uint32_t* BROTLI_RESTRICT max_run_length_prefix) {
    646   uint32_t max_reps = 0;
    647   size_t i;
    648   uint32_t max_prefix;
    649   for (i = 0; i < in_size;) {
    650     uint32_t reps = 0;
    651     for (; i < in_size && v[i] != 0; ++i) ;
    652     for (; i < in_size && v[i] == 0; ++i) {
    653       ++reps;
    654     }
    655     max_reps = BROTLI_MAX(uint32_t, reps, max_reps);
    656   }
    657   max_prefix = max_reps > 0 ? Log2FloorNonZero(max_reps) : 0;
    658   max_prefix = BROTLI_MIN(uint32_t, max_prefix, *max_run_length_prefix);
    659   *max_run_length_prefix = max_prefix;
    660   *out_size = 0;
    661   for (i = 0; i < in_size;) {
    662     assert(*out_size <= i);
    663     if (v[i] != 0) {
    664       v[*out_size] = v[i] + *max_run_length_prefix;
    665       ++i;
    666       ++(*out_size);
    667     } else {
    668       uint32_t reps = 1;
    669       size_t k;
    670       for (k = i + 1; k < in_size && v[k] == 0; ++k) {
    671         ++reps;
    672       }
    673       i += reps;
    674       while (reps != 0) {
    675         if (reps < (2u << max_prefix)) {
    676           uint32_t run_length_prefix = Log2FloorNonZero(reps);
    677           const uint32_t extra_bits = reps - (1u << run_length_prefix);
    678           v[*out_size] = run_length_prefix + (extra_bits << 9);
    679           ++(*out_size);
    680           break;
    681         } else {
    682           const uint32_t extra_bits = (1u << max_prefix) - 1u;
    683           v[*out_size] = max_prefix + (extra_bits << 9);
    684           reps -= (2u << max_prefix) - 1u;
    685           ++(*out_size);
    686         }
    687       }
    688     }
    689   }
    690 }
    691 
    692 #define SYMBOL_BITS 9
    693 
    694 static void EncodeContextMap(MemoryManager* m,
    695                              const uint32_t* context_map,
    696                              size_t context_map_size,
    697                              size_t num_clusters,
    698                              HuffmanTree* tree,
    699                              size_t* storage_ix, uint8_t* storage) {
    700   size_t i;
    701   uint32_t* rle_symbols;
    702   uint32_t max_run_length_prefix = 6;
    703   size_t num_rle_symbols = 0;
    704   uint32_t histogram[BROTLI_MAX_CONTEXT_MAP_SYMBOLS];
    705   static const uint32_t kSymbolMask = (1u << SYMBOL_BITS) - 1u;
    706   uint8_t depths[BROTLI_MAX_CONTEXT_MAP_SYMBOLS];
    707   uint16_t bits[BROTLI_MAX_CONTEXT_MAP_SYMBOLS];
    708 
    709   StoreVarLenUint8(num_clusters - 1, storage_ix, storage);
    710 
    711   if (num_clusters == 1) {
    712     return;
    713   }
    714 
    715   rle_symbols = BROTLI_ALLOC(m, uint32_t, context_map_size);
    716   if (BROTLI_IS_OOM(m)) return;
    717   MoveToFrontTransform(context_map, context_map_size, rle_symbols);
    718   RunLengthCodeZeros(context_map_size, rle_symbols,
    719                      &num_rle_symbols, &max_run_length_prefix);
    720   memset(histogram, 0, sizeof(histogram));
    721   for (i = 0; i < num_rle_symbols; ++i) {
    722     ++histogram[rle_symbols[i] & kSymbolMask];
    723   }
    724   {
    725     BROTLI_BOOL use_rle = TO_BROTLI_BOOL(max_run_length_prefix > 0);
    726     BrotliWriteBits(1, (uint64_t)use_rle, storage_ix, storage);
    727     if (use_rle) {
    728       BrotliWriteBits(4, max_run_length_prefix - 1, storage_ix, storage);
    729     }
    730   }
    731   BuildAndStoreHuffmanTree(histogram, num_clusters + max_run_length_prefix,
    732                            tree, depths, bits, storage_ix, storage);
    733   for (i = 0; i < num_rle_symbols; ++i) {
    734     const uint32_t rle_symbol = rle_symbols[i] & kSymbolMask;
    735     const uint32_t extra_bits_val = rle_symbols[i] >> SYMBOL_BITS;
    736     BrotliWriteBits(depths[rle_symbol], bits[rle_symbol], storage_ix, storage);
    737     if (rle_symbol > 0 && rle_symbol <= max_run_length_prefix) {
    738       BrotliWriteBits(rle_symbol, extra_bits_val, storage_ix, storage);
    739     }
    740   }
    741   BrotliWriteBits(1, 1, storage_ix, storage);  /* use move-to-front */
    742   BROTLI_FREE(m, rle_symbols);
    743 }
    744 
    745 /* Stores the block switch command with index block_ix to the bit stream. */
    746 static BROTLI_INLINE void StoreBlockSwitch(BlockSplitCode* code,
    747                                            const uint32_t block_len,
    748                                            const uint8_t block_type,
    749                                            BROTLI_BOOL is_first_block,
    750                                            size_t* storage_ix,
    751                                            uint8_t* storage) {
    752   size_t typecode = NextBlockTypeCode(&code->type_code_calculator, block_type);
    753   size_t lencode;
    754   uint32_t len_nextra;
    755   uint32_t len_extra;
    756   if (!is_first_block) {
    757     BrotliWriteBits(code->type_depths[typecode], code->type_bits[typecode],
    758                     storage_ix, storage);
    759   }
    760   GetBlockLengthPrefixCode(block_len, &lencode, &len_nextra, &len_extra);
    761 
    762   BrotliWriteBits(code->length_depths[lencode], code->length_bits[lencode],
    763                   storage_ix, storage);
    764   BrotliWriteBits(len_nextra, len_extra, storage_ix, storage);
    765 }
    766 
    767 /* Builds a BlockSplitCode data structure from the block split given by the
    768    vector of block types and block lengths and stores it to the bit stream. */
    769 static void BuildAndStoreBlockSplitCode(const uint8_t* types,
    770                                         const uint32_t* lengths,
    771                                         const size_t num_blocks,
    772                                         const size_t num_types,
    773                                         HuffmanTree* tree,
    774                                         BlockSplitCode* code,
    775                                         size_t* storage_ix,
    776                                         uint8_t* storage) {
    777   uint32_t type_histo[BROTLI_MAX_BLOCK_TYPE_SYMBOLS];
    778   uint32_t length_histo[BROTLI_NUM_BLOCK_LEN_SYMBOLS];
    779   size_t i;
    780   BlockTypeCodeCalculator type_code_calculator;
    781   memset(type_histo, 0, (num_types + 2) * sizeof(type_histo[0]));
    782   memset(length_histo, 0, sizeof(length_histo));
    783   InitBlockTypeCodeCalculator(&type_code_calculator);
    784   for (i = 0; i < num_blocks; ++i) {
    785     size_t type_code = NextBlockTypeCode(&type_code_calculator, types[i]);
    786     if (i != 0) ++type_histo[type_code];
    787     ++length_histo[BlockLengthPrefixCode(lengths[i])];
    788   }
    789   StoreVarLenUint8(num_types - 1, storage_ix, storage);
    790   if (num_types > 1) {  /* TODO: else? could StoreBlockSwitch occur? */
    791     BuildAndStoreHuffmanTree(&type_histo[0], num_types + 2, tree,
    792                              &code->type_depths[0], &code->type_bits[0],
    793                              storage_ix, storage);
    794     BuildAndStoreHuffmanTree(&length_histo[0], BROTLI_NUM_BLOCK_LEN_SYMBOLS,
    795                              tree, &code->length_depths[0],
    796                              &code->length_bits[0], storage_ix, storage);
    797     StoreBlockSwitch(code, lengths[0], types[0], 1, storage_ix, storage);
    798   }
    799 }
    800 
    801 /* Stores a context map where the histogram type is always the block type. */
    802 static void StoreTrivialContextMap(size_t num_types,
    803                                    size_t context_bits,
    804                                    HuffmanTree* tree,
    805                                    size_t* storage_ix,
    806                                    uint8_t* storage) {
    807   StoreVarLenUint8(num_types - 1, storage_ix, storage);
    808   if (num_types > 1) {
    809     size_t repeat_code = context_bits - 1u;
    810     size_t repeat_bits = (1u << repeat_code) - 1u;
    811     size_t alphabet_size = num_types + repeat_code;
    812     uint32_t histogram[BROTLI_MAX_CONTEXT_MAP_SYMBOLS];
    813     uint8_t depths[BROTLI_MAX_CONTEXT_MAP_SYMBOLS];
    814     uint16_t bits[BROTLI_MAX_CONTEXT_MAP_SYMBOLS];
    815     size_t i;
    816     memset(histogram, 0, alphabet_size * sizeof(histogram[0]));
    817     /* Write RLEMAX. */
    818     BrotliWriteBits(1, 1, storage_ix, storage);
    819     BrotliWriteBits(4, repeat_code - 1, storage_ix, storage);
    820     histogram[repeat_code] = (uint32_t)num_types;
    821     histogram[0] = 1;
    822     for (i = context_bits; i < alphabet_size; ++i) {
    823       histogram[i] = 1;
    824     }
    825     BuildAndStoreHuffmanTree(histogram, alphabet_size, tree,
    826                              depths, bits, storage_ix, storage);
    827     for (i = 0; i < num_types; ++i) {
    828       size_t code = (i == 0 ? 0 : i + context_bits - 1);
    829       BrotliWriteBits(depths[code], bits[code], storage_ix, storage);
    830       BrotliWriteBits(
    831           depths[repeat_code], bits[repeat_code], storage_ix, storage);
    832       BrotliWriteBits(repeat_code, repeat_bits, storage_ix, storage);
    833     }
    834     /* Write IMTF (inverse-move-to-front) bit. */
    835     BrotliWriteBits(1, 1, storage_ix, storage);
    836   }
    837 }
    838 
    839 /* Manages the encoding of one block category (literal, command or distance). */
    840 typedef struct BlockEncoder {
    841   size_t alphabet_size_;
    842   size_t num_block_types_;
    843   const uint8_t* block_types_;  /* Not owned. */
    844   const uint32_t* block_lengths_;  /* Not owned. */
    845   size_t num_blocks_;
    846   BlockSplitCode block_split_code_;
    847   size_t block_ix_;
    848   size_t block_len_;
    849   size_t entropy_ix_;
    850   uint8_t* depths_;
    851   uint16_t* bits_;
    852 } BlockEncoder;
    853 
    854 static void InitBlockEncoder(BlockEncoder* self, size_t alphabet_size,
    855     size_t num_block_types, const uint8_t* block_types,
    856     const uint32_t* block_lengths, const size_t num_blocks) {
    857   self->alphabet_size_ = alphabet_size;
    858   self->num_block_types_ = num_block_types;
    859   self->block_types_ = block_types;
    860   self->block_lengths_ = block_lengths;
    861   self->num_blocks_ = num_blocks;
    862   InitBlockTypeCodeCalculator(&self->block_split_code_.type_code_calculator);
    863   self->block_ix_ = 0;
    864   self->block_len_ = num_blocks == 0 ? 0 : block_lengths[0];
    865   self->entropy_ix_ = 0;
    866   self->depths_ = 0;
    867   self->bits_ = 0;
    868 }
    869 
    870 static void CleanupBlockEncoder(MemoryManager* m, BlockEncoder* self) {
    871   BROTLI_FREE(m, self->depths_);
    872   BROTLI_FREE(m, self->bits_);
    873 }
    874 
    875 /* Creates entropy codes of block lengths and block types and stores them
    876    to the bit stream. */
    877 static void BuildAndStoreBlockSwitchEntropyCodes(BlockEncoder* self,
    878     HuffmanTree* tree, size_t* storage_ix, uint8_t* storage) {
    879   BuildAndStoreBlockSplitCode(self->block_types_, self->block_lengths_,
    880       self->num_blocks_, self->num_block_types_, tree, &self->block_split_code_,
    881       storage_ix, storage);
    882 }
    883 
    884 /* Stores the next symbol with the entropy code of the current block type.
    885    Updates the block type and block length at block boundaries. */
    886 static void StoreSymbol(BlockEncoder* self, size_t symbol, size_t* storage_ix,
    887     uint8_t* storage) {
    888   if (self->block_len_ == 0) {
    889     size_t block_ix = ++self->block_ix_;
    890     uint32_t block_len = self->block_lengths_[block_ix];
    891     uint8_t block_type = self->block_types_[block_ix];
    892     self->block_len_ = block_len;
    893     self->entropy_ix_ = block_type * self->alphabet_size_;
    894     StoreBlockSwitch(&self->block_split_code_, block_len, block_type, 0,
    895         storage_ix, storage);
    896   }
    897   --self->block_len_;
    898   {
    899     size_t ix = self->entropy_ix_ + symbol;
    900     BrotliWriteBits(self->depths_[ix], self->bits_[ix], storage_ix, storage);
    901   }
    902 }
    903 
    904 /* Stores the next symbol with the entropy code of the current block type and
    905    context value.
    906    Updates the block type and block length at block boundaries. */
    907 static void StoreSymbolWithContext(BlockEncoder* self, size_t symbol,
    908     size_t context, const uint32_t* context_map, size_t* storage_ix,
    909     uint8_t* storage, const size_t context_bits) {
    910   if (self->block_len_ == 0) {
    911     size_t block_ix = ++self->block_ix_;
    912     uint32_t block_len = self->block_lengths_[block_ix];
    913     uint8_t block_type = self->block_types_[block_ix];
    914     self->block_len_ = block_len;
    915     self->entropy_ix_ = (size_t)block_type << context_bits;
    916     StoreBlockSwitch(&self->block_split_code_, block_len, block_type, 0,
    917         storage_ix, storage);
    918   }
    919   --self->block_len_;
    920   {
    921     size_t histo_ix = context_map[self->entropy_ix_ + context];
    922     size_t ix = histo_ix * self->alphabet_size_ + symbol;
    923     BrotliWriteBits(self->depths_[ix], self->bits_[ix], storage_ix, storage);
    924   }
    925 }
    926 
    927 #define FN(X) X ## Literal
    928 /* NOLINTNEXTLINE(build/include) */
    929 #include "./block_encoder_inc.h"
    930 #undef FN
    931 
    932 #define FN(X) X ## Command
    933 /* NOLINTNEXTLINE(build/include) */
    934 #include "./block_encoder_inc.h"
    935 #undef FN
    936 
    937 #define FN(X) X ## Distance
    938 /* NOLINTNEXTLINE(build/include) */
    939 #include "./block_encoder_inc.h"
    940 #undef FN
    941 
    942 static void JumpToByteBoundary(size_t* storage_ix, uint8_t* storage) {
    943   *storage_ix = (*storage_ix + 7u) & ~7u;
    944   storage[*storage_ix >> 3] = 0;
    945 }
    946 
    947 void BrotliStoreMetaBlock(MemoryManager* m,
    948                           const uint8_t* input,
    949                           size_t start_pos,
    950                           size_t length,
    951                           size_t mask,
    952                           uint8_t prev_byte,
    953                           uint8_t prev_byte2,
    954                           BROTLI_BOOL is_last,
    955                           uint32_t num_direct_distance_codes,
    956                           uint32_t distance_postfix_bits,
    957                           ContextType literal_context_mode,
    958                           const Command *commands,
    959                           size_t n_commands,
    960                           const MetaBlockSplit* mb,
    961                           size_t *storage_ix,
    962                           uint8_t *storage) {
    963   size_t pos = start_pos;
    964   size_t i;
    965   size_t num_distance_codes =
    966       BROTLI_NUM_DISTANCE_SHORT_CODES + num_direct_distance_codes +
    967       (48u << distance_postfix_bits);
    968   HuffmanTree* tree;
    969   BlockEncoder literal_enc;
    970   BlockEncoder command_enc;
    971   BlockEncoder distance_enc;
    972 
    973   StoreCompressedMetaBlockHeader(is_last, length, storage_ix, storage);
    974 
    975   tree = BROTLI_ALLOC(m, HuffmanTree, MAX_HUFFMAN_TREE_SIZE);
    976   if (BROTLI_IS_OOM(m)) return;
    977   InitBlockEncoder(&literal_enc, 256, mb->literal_split.num_types,
    978       mb->literal_split.types, mb->literal_split.lengths,
    979       mb->literal_split.num_blocks);
    980   InitBlockEncoder(&command_enc, BROTLI_NUM_COMMAND_SYMBOLS,
    981       mb->command_split.num_types, mb->command_split.types,
    982       mb->command_split.lengths, mb->command_split.num_blocks);
    983   InitBlockEncoder(&distance_enc, num_distance_codes,
    984       mb->distance_split.num_types, mb->distance_split.types,
    985       mb->distance_split.lengths, mb->distance_split.num_blocks);
    986 
    987   BuildAndStoreBlockSwitchEntropyCodes(&literal_enc, tree, storage_ix, storage);
    988   BuildAndStoreBlockSwitchEntropyCodes(&command_enc, tree, storage_ix, storage);
    989   BuildAndStoreBlockSwitchEntropyCodes(
    990       &distance_enc, tree, storage_ix, storage);
    991 
    992   BrotliWriteBits(2, distance_postfix_bits, storage_ix, storage);
    993   BrotliWriteBits(4, num_direct_distance_codes >> distance_postfix_bits,
    994                   storage_ix, storage);
    995   for (i = 0; i < mb->literal_split.num_types; ++i) {
    996     BrotliWriteBits(2, literal_context_mode, storage_ix, storage);
    997   }
    998 
    999   if (mb->literal_context_map_size == 0) {
   1000     StoreTrivialContextMap(mb->literal_histograms_size,
   1001         BROTLI_LITERAL_CONTEXT_BITS, tree, storage_ix, storage);
   1002   } else {
   1003     EncodeContextMap(m,
   1004         mb->literal_context_map, mb->literal_context_map_size,
   1005         mb->literal_histograms_size, tree, storage_ix, storage);
   1006     if (BROTLI_IS_OOM(m)) return;
   1007   }
   1008 
   1009   if (mb->distance_context_map_size == 0) {
   1010     StoreTrivialContextMap(mb->distance_histograms_size,
   1011         BROTLI_DISTANCE_CONTEXT_BITS, tree, storage_ix, storage);
   1012   } else {
   1013     EncodeContextMap(m,
   1014         mb->distance_context_map, mb->distance_context_map_size,
   1015         mb->distance_histograms_size, tree, storage_ix, storage);
   1016     if (BROTLI_IS_OOM(m)) return;
   1017   }
   1018 
   1019   BuildAndStoreEntropyCodesLiteral(m, &literal_enc, mb->literal_histograms,
   1020       mb->literal_histograms_size, tree, storage_ix, storage);
   1021   if (BROTLI_IS_OOM(m)) return;
   1022   BuildAndStoreEntropyCodesCommand(m, &command_enc, mb->command_histograms,
   1023       mb->command_histograms_size, tree, storage_ix, storage);
   1024   if (BROTLI_IS_OOM(m)) return;
   1025   BuildAndStoreEntropyCodesDistance(m, &distance_enc, mb->distance_histograms,
   1026       mb->distance_histograms_size, tree, storage_ix, storage);
   1027   if (BROTLI_IS_OOM(m)) return;
   1028   BROTLI_FREE(m, tree);
   1029 
   1030   for (i = 0; i < n_commands; ++i) {
   1031     const Command cmd = commands[i];
   1032     size_t cmd_code = cmd.cmd_prefix_;
   1033     StoreSymbol(&command_enc, cmd_code, storage_ix, storage);
   1034     StoreCommandExtra(&cmd, storage_ix, storage);
   1035     if (mb->literal_context_map_size == 0) {
   1036       size_t j;
   1037       for (j = cmd.insert_len_; j != 0; --j) {
   1038         StoreSymbol(&literal_enc, input[pos & mask], storage_ix, storage);
   1039         ++pos;
   1040       }
   1041     } else {
   1042       size_t j;
   1043       for (j = cmd.insert_len_; j != 0; --j) {
   1044         size_t context = Context(prev_byte, prev_byte2, literal_context_mode);
   1045         uint8_t literal = input[pos & mask];
   1046         StoreSymbolWithContext(&literal_enc, literal, context,
   1047             mb->literal_context_map, storage_ix, storage,
   1048             BROTLI_LITERAL_CONTEXT_BITS);
   1049         prev_byte2 = prev_byte;
   1050         prev_byte = literal;
   1051         ++pos;
   1052       }
   1053     }
   1054     pos += CommandCopyLen(&cmd);
   1055     if (CommandCopyLen(&cmd)) {
   1056       prev_byte2 = input[(pos - 2) & mask];
   1057       prev_byte = input[(pos - 1) & mask];
   1058       if (cmd.cmd_prefix_ >= 128) {
   1059         size_t dist_code = cmd.dist_prefix_;
   1060         uint32_t distnumextra = cmd.dist_extra_ >> 24;
   1061         uint64_t distextra = cmd.dist_extra_ & 0xffffff;
   1062         if (mb->distance_context_map_size == 0) {
   1063           StoreSymbol(&distance_enc, dist_code, storage_ix, storage);
   1064         } else {
   1065           size_t context = CommandDistanceContext(&cmd);
   1066           StoreSymbolWithContext(&distance_enc, dist_code, context,
   1067               mb->distance_context_map, storage_ix, storage,
   1068               BROTLI_DISTANCE_CONTEXT_BITS);
   1069         }
   1070         BrotliWriteBits(distnumextra, distextra, storage_ix, storage);
   1071       }
   1072     }
   1073   }
   1074   CleanupBlockEncoder(m, &distance_enc);
   1075   CleanupBlockEncoder(m, &command_enc);
   1076   CleanupBlockEncoder(m, &literal_enc);
   1077   if (is_last) {
   1078     JumpToByteBoundary(storage_ix, storage);
   1079   }
   1080 }
   1081 
   1082 static void BuildHistograms(const uint8_t* input,
   1083                             size_t start_pos,
   1084                             size_t mask,
   1085                             const Command *commands,
   1086                             size_t n_commands,
   1087                             HistogramLiteral* lit_histo,
   1088                             HistogramCommand* cmd_histo,
   1089                             HistogramDistance* dist_histo) {
   1090   size_t pos = start_pos;
   1091   size_t i;
   1092   for (i = 0; i < n_commands; ++i) {
   1093     const Command cmd = commands[i];
   1094     size_t j;
   1095     HistogramAddCommand(cmd_histo, cmd.cmd_prefix_);
   1096     for (j = cmd.insert_len_; j != 0; --j) {
   1097       HistogramAddLiteral(lit_histo, input[pos & mask]);
   1098       ++pos;
   1099     }
   1100     pos += CommandCopyLen(&cmd);
   1101     if (CommandCopyLen(&cmd) && cmd.cmd_prefix_ >= 128) {
   1102       HistogramAddDistance(dist_histo, cmd.dist_prefix_);
   1103     }
   1104   }
   1105 }
   1106 
   1107 static void StoreDataWithHuffmanCodes(const uint8_t* input,
   1108                                       size_t start_pos,
   1109                                       size_t mask,
   1110                                       const Command *commands,
   1111                                       size_t n_commands,
   1112                                       const uint8_t* lit_depth,
   1113                                       const uint16_t* lit_bits,
   1114                                       const uint8_t* cmd_depth,
   1115                                       const uint16_t* cmd_bits,
   1116                                       const uint8_t* dist_depth,
   1117                                       const uint16_t* dist_bits,
   1118                                       size_t* storage_ix,
   1119                                       uint8_t* storage) {
   1120   size_t pos = start_pos;
   1121   size_t i;
   1122   for (i = 0; i < n_commands; ++i) {
   1123     const Command cmd = commands[i];
   1124     const size_t cmd_code = cmd.cmd_prefix_;
   1125     size_t j;
   1126     BrotliWriteBits(
   1127         cmd_depth[cmd_code], cmd_bits[cmd_code], storage_ix, storage);
   1128     StoreCommandExtra(&cmd, storage_ix, storage);
   1129     for (j = cmd.insert_len_; j != 0; --j) {
   1130       const uint8_t literal = input[pos & mask];
   1131       BrotliWriteBits(
   1132           lit_depth[literal], lit_bits[literal], storage_ix, storage);
   1133       ++pos;
   1134     }
   1135     pos += CommandCopyLen(&cmd);
   1136     if (CommandCopyLen(&cmd) && cmd.cmd_prefix_ >= 128) {
   1137       const size_t dist_code = cmd.dist_prefix_;
   1138       const uint32_t distnumextra = cmd.dist_extra_ >> 24;
   1139       const uint32_t distextra = cmd.dist_extra_ & 0xffffff;
   1140       BrotliWriteBits(dist_depth[dist_code], dist_bits[dist_code],
   1141                       storage_ix, storage);
   1142       BrotliWriteBits(distnumextra, distextra, storage_ix, storage);
   1143     }
   1144   }
   1145 }
   1146 
   1147 void BrotliStoreMetaBlockTrivial(MemoryManager* m,
   1148                                  const uint8_t* input,
   1149                                  size_t start_pos,
   1150                                  size_t length,
   1151                                  size_t mask,
   1152                                  BROTLI_BOOL is_last,
   1153                                  const Command *commands,
   1154                                  size_t n_commands,
   1155                                  size_t *storage_ix,
   1156                                  uint8_t *storage) {
   1157   HistogramLiteral lit_histo;
   1158   HistogramCommand cmd_histo;
   1159   HistogramDistance dist_histo;
   1160   uint8_t lit_depth[BROTLI_NUM_LITERAL_SYMBOLS];
   1161   uint16_t lit_bits[BROTLI_NUM_LITERAL_SYMBOLS];
   1162   uint8_t cmd_depth[BROTLI_NUM_COMMAND_SYMBOLS];
   1163   uint16_t cmd_bits[BROTLI_NUM_COMMAND_SYMBOLS];
   1164   uint8_t dist_depth[SIMPLE_DISTANCE_ALPHABET_SIZE];
   1165   uint16_t dist_bits[SIMPLE_DISTANCE_ALPHABET_SIZE];
   1166   HuffmanTree* tree;
   1167 
   1168   StoreCompressedMetaBlockHeader(is_last, length, storage_ix, storage);
   1169 
   1170   HistogramClearLiteral(&lit_histo);
   1171   HistogramClearCommand(&cmd_histo);
   1172   HistogramClearDistance(&dist_histo);
   1173 
   1174   BuildHistograms(input, start_pos, mask, commands, n_commands,
   1175                   &lit_histo, &cmd_histo, &dist_histo);
   1176 
   1177   BrotliWriteBits(13, 0, storage_ix, storage);
   1178 
   1179   tree = BROTLI_ALLOC(m, HuffmanTree, MAX_HUFFMAN_TREE_SIZE);
   1180   if (BROTLI_IS_OOM(m)) return;
   1181   BuildAndStoreHuffmanTree(lit_histo.data_, BROTLI_NUM_LITERAL_SYMBOLS, tree,
   1182                            lit_depth, lit_bits,
   1183                            storage_ix, storage);
   1184   BuildAndStoreHuffmanTree(cmd_histo.data_, BROTLI_NUM_COMMAND_SYMBOLS, tree,
   1185                            cmd_depth, cmd_bits,
   1186                            storage_ix, storage);
   1187   BuildAndStoreHuffmanTree(dist_histo.data_, SIMPLE_DISTANCE_ALPHABET_SIZE,
   1188                            tree,
   1189                            dist_depth, dist_bits,
   1190                            storage_ix, storage);
   1191   BROTLI_FREE(m, tree);
   1192   StoreDataWithHuffmanCodes(input, start_pos, mask, commands,
   1193                             n_commands, lit_depth, lit_bits,
   1194                             cmd_depth, cmd_bits,
   1195                             dist_depth, dist_bits,
   1196                             storage_ix, storage);
   1197   if (is_last) {
   1198     JumpToByteBoundary(storage_ix, storage);
   1199   }
   1200 }
   1201 
   1202 void BrotliStoreMetaBlockFast(MemoryManager* m,
   1203                               const uint8_t* input,
   1204                               size_t start_pos,
   1205                               size_t length,
   1206                               size_t mask,
   1207                               BROTLI_BOOL is_last,
   1208                               const Command *commands,
   1209                               size_t n_commands,
   1210                               size_t *storage_ix,
   1211                               uint8_t *storage) {
   1212   StoreCompressedMetaBlockHeader(is_last, length, storage_ix, storage);
   1213 
   1214   BrotliWriteBits(13, 0, storage_ix, storage);
   1215 
   1216   if (n_commands <= 128) {
   1217     uint32_t histogram[BROTLI_NUM_LITERAL_SYMBOLS] = { 0 };
   1218     size_t pos = start_pos;
   1219     size_t num_literals = 0;
   1220     size_t i;
   1221     uint8_t lit_depth[BROTLI_NUM_LITERAL_SYMBOLS];
   1222     uint16_t lit_bits[BROTLI_NUM_LITERAL_SYMBOLS];
   1223     for (i = 0; i < n_commands; ++i) {
   1224       const Command cmd = commands[i];
   1225       size_t j;
   1226       for (j = cmd.insert_len_; j != 0; --j) {
   1227         ++histogram[input[pos & mask]];
   1228         ++pos;
   1229       }
   1230       num_literals += cmd.insert_len_;
   1231       pos += CommandCopyLen(&cmd);
   1232     }
   1233     BrotliBuildAndStoreHuffmanTreeFast(m, histogram, num_literals,
   1234                                        /* max_bits = */ 8,
   1235                                        lit_depth, lit_bits,
   1236                                        storage_ix, storage);
   1237     if (BROTLI_IS_OOM(m)) return;
   1238     StoreStaticCommandHuffmanTree(storage_ix, storage);
   1239     StoreStaticDistanceHuffmanTree(storage_ix, storage);
   1240     StoreDataWithHuffmanCodes(input, start_pos, mask, commands,
   1241                               n_commands, lit_depth, lit_bits,
   1242                               kStaticCommandCodeDepth,
   1243                               kStaticCommandCodeBits,
   1244                               kStaticDistanceCodeDepth,
   1245                               kStaticDistanceCodeBits,
   1246                               storage_ix, storage);
   1247   } else {
   1248     HistogramLiteral lit_histo;
   1249     HistogramCommand cmd_histo;
   1250     HistogramDistance dist_histo;
   1251     uint8_t lit_depth[BROTLI_NUM_LITERAL_SYMBOLS];
   1252     uint16_t lit_bits[BROTLI_NUM_LITERAL_SYMBOLS];
   1253     uint8_t cmd_depth[BROTLI_NUM_COMMAND_SYMBOLS];
   1254     uint16_t cmd_bits[BROTLI_NUM_COMMAND_SYMBOLS];
   1255     uint8_t dist_depth[SIMPLE_DISTANCE_ALPHABET_SIZE];
   1256     uint16_t dist_bits[SIMPLE_DISTANCE_ALPHABET_SIZE];
   1257     HistogramClearLiteral(&lit_histo);
   1258     HistogramClearCommand(&cmd_histo);
   1259     HistogramClearDistance(&dist_histo);
   1260     BuildHistograms(input, start_pos, mask, commands, n_commands,
   1261                     &lit_histo, &cmd_histo, &dist_histo);
   1262     BrotliBuildAndStoreHuffmanTreeFast(m, lit_histo.data_,
   1263                                        lit_histo.total_count_,
   1264                                        /* max_bits = */ 8,
   1265                                        lit_depth, lit_bits,
   1266                                        storage_ix, storage);
   1267     if (BROTLI_IS_OOM(m)) return;
   1268     BrotliBuildAndStoreHuffmanTreeFast(m, cmd_histo.data_,
   1269                                        cmd_histo.total_count_,
   1270                                        /* max_bits = */ 10,
   1271                                        cmd_depth, cmd_bits,
   1272                                        storage_ix, storage);
   1273     if (BROTLI_IS_OOM(m)) return;
   1274     BrotliBuildAndStoreHuffmanTreeFast(m, dist_histo.data_,
   1275                                        dist_histo.total_count_,
   1276                                        /* max_bits = */
   1277                                        SIMPLE_DISTANCE_ALPHABET_BITS,
   1278                                        dist_depth, dist_bits,
   1279                                        storage_ix, storage);
   1280     if (BROTLI_IS_OOM(m)) return;
   1281     StoreDataWithHuffmanCodes(input, start_pos, mask, commands,
   1282                               n_commands, lit_depth, lit_bits,
   1283                               cmd_depth, cmd_bits,
   1284                               dist_depth, dist_bits,
   1285                               storage_ix, storage);
   1286   }
   1287 
   1288   if (is_last) {
   1289     JumpToByteBoundary(storage_ix, storage);
   1290   }
   1291 }
   1292 
   1293 /* This is for storing uncompressed blocks (simple raw storage of
   1294    bytes-as-bytes). */
   1295 void BrotliStoreUncompressedMetaBlock(BROTLI_BOOL is_final_block,
   1296                                       const uint8_t * BROTLI_RESTRICT input,
   1297                                       size_t position, size_t mask,
   1298                                       size_t len,
   1299                                       size_t * BROTLI_RESTRICT storage_ix,
   1300                                       uint8_t * BROTLI_RESTRICT storage) {
   1301   size_t masked_pos = position & mask;
   1302   BrotliStoreUncompressedMetaBlockHeader(len, storage_ix, storage);
   1303   JumpToByteBoundary(storage_ix, storage);
   1304 
   1305   if (masked_pos + len > mask + 1) {
   1306     size_t len1 = mask + 1 - masked_pos;
   1307     memcpy(&storage[*storage_ix >> 3], &input[masked_pos], len1);
   1308     *storage_ix += len1 << 3;
   1309     len -= len1;
   1310     masked_pos = 0;
   1311   }
   1312   memcpy(&storage[*storage_ix >> 3], &input[masked_pos], len);
   1313   *storage_ix += len << 3;
   1314 
   1315   /* We need to clear the next 4 bytes to continue to be
   1316      compatible with BrotliWriteBits. */
   1317   BrotliWriteBitsPrepareStorage(*storage_ix, storage);
   1318 
   1319   /* Since the uncompressed block itself may not be the final block, add an
   1320      empty one after this. */
   1321   if (is_final_block) {
   1322     BrotliWriteBits(1, 1, storage_ix, storage);  /* islast */
   1323     BrotliWriteBits(1, 1, storage_ix, storage);  /* isempty */
   1324     JumpToByteBoundary(storage_ix, storage);
   1325   }
   1326 }
   1327 
   1328 void BrotliStoreSyncMetaBlock(size_t* BROTLI_RESTRICT storage_ix,
   1329                               uint8_t* BROTLI_RESTRICT storage) {
   1330   /* Empty metadata meta-block bit pattern:
   1331        1 bit:  is_last (0)
   1332        2 bits: num nibbles (3)
   1333        1 bit:  reserved (0)
   1334        2 bits: metadata length bytes (0) */
   1335   BrotliWriteBits(6, 6, storage_ix, storage);
   1336   JumpToByteBoundary(storage_ix, storage);
   1337 }
   1338 
   1339 #if defined(__cplusplus) || defined(c_plusplus)
   1340 }  /* extern "C" */
   1341 #endif
   1342