xref: /third_party/node/deps/brotli/c/dec/huffman.c (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci/* Copyright 2013 Google Inc. All Rights Reserved.
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci   Distributed under MIT license.
41cb0ef41Sopenharmony_ci   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
51cb0ef41Sopenharmony_ci*/
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci/* Utilities for building Huffman decoding tables. */
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#include "./huffman.h"
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci#include <string.h>  /* memcpy, memset */
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci#include "../common/constants.h"
141cb0ef41Sopenharmony_ci#include "../common/platform.h"
151cb0ef41Sopenharmony_ci#include <brotli/types.h>
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus)
181cb0ef41Sopenharmony_ciextern "C" {
191cb0ef41Sopenharmony_ci#endif
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci#define BROTLI_REVERSE_BITS_MAX 8
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci#if defined(BROTLI_RBIT)
241cb0ef41Sopenharmony_ci#define BROTLI_REVERSE_BITS_BASE \
251cb0ef41Sopenharmony_ci  ((sizeof(brotli_reg_t) << 3) - BROTLI_REVERSE_BITS_MAX)
261cb0ef41Sopenharmony_ci#else
271cb0ef41Sopenharmony_ci#define BROTLI_REVERSE_BITS_BASE 0
281cb0ef41Sopenharmony_cistatic uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = {
291cb0ef41Sopenharmony_ci  0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
301cb0ef41Sopenharmony_ci  0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
311cb0ef41Sopenharmony_ci  0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
321cb0ef41Sopenharmony_ci  0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
331cb0ef41Sopenharmony_ci  0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4,
341cb0ef41Sopenharmony_ci  0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
351cb0ef41Sopenharmony_ci  0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC,
361cb0ef41Sopenharmony_ci  0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
371cb0ef41Sopenharmony_ci  0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,
381cb0ef41Sopenharmony_ci  0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
391cb0ef41Sopenharmony_ci  0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA,
401cb0ef41Sopenharmony_ci  0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
411cb0ef41Sopenharmony_ci  0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6,
421cb0ef41Sopenharmony_ci  0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
431cb0ef41Sopenharmony_ci  0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE,
441cb0ef41Sopenharmony_ci  0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
451cb0ef41Sopenharmony_ci  0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1,
461cb0ef41Sopenharmony_ci  0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
471cb0ef41Sopenharmony_ci  0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9,
481cb0ef41Sopenharmony_ci  0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
491cb0ef41Sopenharmony_ci  0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5,
501cb0ef41Sopenharmony_ci  0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
511cb0ef41Sopenharmony_ci  0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED,
521cb0ef41Sopenharmony_ci  0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
531cb0ef41Sopenharmony_ci  0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3,
541cb0ef41Sopenharmony_ci  0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
551cb0ef41Sopenharmony_ci  0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB,
561cb0ef41Sopenharmony_ci  0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
571cb0ef41Sopenharmony_ci  0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7,
581cb0ef41Sopenharmony_ci  0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
591cb0ef41Sopenharmony_ci  0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF,
601cb0ef41Sopenharmony_ci  0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
611cb0ef41Sopenharmony_ci};
621cb0ef41Sopenharmony_ci#endif  /* BROTLI_RBIT */
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci#define BROTLI_REVERSE_BITS_LOWEST \
651cb0ef41Sopenharmony_ci  ((brotli_reg_t)1 << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE))
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci/* Returns reverse(num >> BROTLI_REVERSE_BITS_BASE, BROTLI_REVERSE_BITS_MAX),
681cb0ef41Sopenharmony_ci   where reverse(value, len) is the bit-wise reversal of the len least
691cb0ef41Sopenharmony_ci   significant bits of value. */
701cb0ef41Sopenharmony_cistatic BROTLI_INLINE brotli_reg_t BrotliReverseBits(brotli_reg_t num) {
711cb0ef41Sopenharmony_ci#if defined(BROTLI_RBIT)
721cb0ef41Sopenharmony_ci  return BROTLI_RBIT(num);
731cb0ef41Sopenharmony_ci#else
741cb0ef41Sopenharmony_ci  return kReverseBits[num];
751cb0ef41Sopenharmony_ci#endif
761cb0ef41Sopenharmony_ci}
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci/* Stores code in table[0], table[step], table[2*step], ..., table[end] */
791cb0ef41Sopenharmony_ci/* Assumes that end is an integer multiple of step */
801cb0ef41Sopenharmony_cistatic BROTLI_INLINE void ReplicateValue(HuffmanCode* table,
811cb0ef41Sopenharmony_ci                                         int step, int end,
821cb0ef41Sopenharmony_ci                                         HuffmanCode code) {
831cb0ef41Sopenharmony_ci  do {
841cb0ef41Sopenharmony_ci    end -= step;
851cb0ef41Sopenharmony_ci    table[end] = code;
861cb0ef41Sopenharmony_ci  } while (end > 0);
871cb0ef41Sopenharmony_ci}
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci/* Returns the table width of the next 2nd level table. |count| is the histogram
901cb0ef41Sopenharmony_ci   of bit lengths for the remaining symbols, |len| is the code length of the
911cb0ef41Sopenharmony_ci   next processed symbol. */
921cb0ef41Sopenharmony_cistatic BROTLI_INLINE int NextTableBitSize(const uint16_t* const count,
931cb0ef41Sopenharmony_ci                                          int len, int root_bits) {
941cb0ef41Sopenharmony_ci  int left = 1 << (len - root_bits);
951cb0ef41Sopenharmony_ci  while (len < BROTLI_HUFFMAN_MAX_CODE_LENGTH) {
961cb0ef41Sopenharmony_ci    left -= count[len];
971cb0ef41Sopenharmony_ci    if (left <= 0) break;
981cb0ef41Sopenharmony_ci    ++len;
991cb0ef41Sopenharmony_ci    left <<= 1;
1001cb0ef41Sopenharmony_ci  }
1011cb0ef41Sopenharmony_ci  return len - root_bits;
1021cb0ef41Sopenharmony_ci}
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_civoid BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
1051cb0ef41Sopenharmony_ci                                        const uint8_t* const code_lengths,
1061cb0ef41Sopenharmony_ci                                        uint16_t* count) {
1071cb0ef41Sopenharmony_ci  HuffmanCode code;       /* current table entry */
1081cb0ef41Sopenharmony_ci  int symbol;             /* symbol index in original or sorted table */
1091cb0ef41Sopenharmony_ci  brotli_reg_t key;       /* prefix code */
1101cb0ef41Sopenharmony_ci  brotli_reg_t key_step;  /* prefix code addend */
1111cb0ef41Sopenharmony_ci  int step;               /* step size to replicate values in current table */
1121cb0ef41Sopenharmony_ci  int table_size;         /* size of current table */
1131cb0ef41Sopenharmony_ci  int sorted[BROTLI_CODE_LENGTH_CODES];  /* symbols sorted by code length */
1141cb0ef41Sopenharmony_ci  /* offsets in sorted table for each length */
1151cb0ef41Sopenharmony_ci  int offset[BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1];
1161cb0ef41Sopenharmony_ci  int bits;
1171cb0ef41Sopenharmony_ci  int bits_count;
1181cb0ef41Sopenharmony_ci  BROTLI_DCHECK(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH <=
1191cb0ef41Sopenharmony_ci                BROTLI_REVERSE_BITS_MAX);
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci  /* Generate offsets into sorted symbol table by code length. */
1221cb0ef41Sopenharmony_ci  symbol = -1;
1231cb0ef41Sopenharmony_ci  bits = 1;
1241cb0ef41Sopenharmony_ci  BROTLI_REPEAT(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH, {
1251cb0ef41Sopenharmony_ci    symbol += count[bits];
1261cb0ef41Sopenharmony_ci    offset[bits] = symbol;
1271cb0ef41Sopenharmony_ci    bits++;
1281cb0ef41Sopenharmony_ci  });
1291cb0ef41Sopenharmony_ci  /* Symbols with code length 0 are placed after all other symbols. */
1301cb0ef41Sopenharmony_ci  offset[0] = BROTLI_CODE_LENGTH_CODES - 1;
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  /* Sort symbols by length, by symbol order within each length. */
1331cb0ef41Sopenharmony_ci  symbol = BROTLI_CODE_LENGTH_CODES;
1341cb0ef41Sopenharmony_ci  do {
1351cb0ef41Sopenharmony_ci    BROTLI_REPEAT(6, {
1361cb0ef41Sopenharmony_ci      symbol--;
1371cb0ef41Sopenharmony_ci      sorted[offset[code_lengths[symbol]]--] = symbol;
1381cb0ef41Sopenharmony_ci    });
1391cb0ef41Sopenharmony_ci  } while (symbol != 0);
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci  table_size = 1 << BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH;
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci  /* Special case: all symbols but one have 0 code length. */
1441cb0ef41Sopenharmony_ci  if (offset[0] == 0) {
1451cb0ef41Sopenharmony_ci    code = ConstructHuffmanCode(0, (uint16_t)sorted[0]);
1461cb0ef41Sopenharmony_ci    for (key = 0; key < (brotli_reg_t)table_size; ++key) {
1471cb0ef41Sopenharmony_ci      table[key] = code;
1481cb0ef41Sopenharmony_ci    }
1491cb0ef41Sopenharmony_ci    return;
1501cb0ef41Sopenharmony_ci  }
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci  /* Fill in table. */
1531cb0ef41Sopenharmony_ci  key = 0;
1541cb0ef41Sopenharmony_ci  key_step = BROTLI_REVERSE_BITS_LOWEST;
1551cb0ef41Sopenharmony_ci  symbol = 0;
1561cb0ef41Sopenharmony_ci  bits = 1;
1571cb0ef41Sopenharmony_ci  step = 2;
1581cb0ef41Sopenharmony_ci  do {
1591cb0ef41Sopenharmony_ci    for (bits_count = count[bits]; bits_count != 0; --bits_count) {
1601cb0ef41Sopenharmony_ci      code = ConstructHuffmanCode((uint8_t)bits, (uint16_t)sorted[symbol++]);
1611cb0ef41Sopenharmony_ci      ReplicateValue(&table[BrotliReverseBits(key)], step, table_size, code);
1621cb0ef41Sopenharmony_ci      key += key_step;
1631cb0ef41Sopenharmony_ci    }
1641cb0ef41Sopenharmony_ci    step <<= 1;
1651cb0ef41Sopenharmony_ci    key_step >>= 1;
1661cb0ef41Sopenharmony_ci  } while (++bits <= BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH);
1671cb0ef41Sopenharmony_ci}
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ciuint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
1701cb0ef41Sopenharmony_ci                                 int root_bits,
1711cb0ef41Sopenharmony_ci                                 const uint16_t* const symbol_lists,
1721cb0ef41Sopenharmony_ci                                 uint16_t* count) {
1731cb0ef41Sopenharmony_ci  HuffmanCode code;       /* current table entry */
1741cb0ef41Sopenharmony_ci  HuffmanCode* table;     /* next available space in table */
1751cb0ef41Sopenharmony_ci  int len;                /* current code length */
1761cb0ef41Sopenharmony_ci  int symbol;             /* symbol index in original or sorted table */
1771cb0ef41Sopenharmony_ci  brotli_reg_t key;       /* prefix code */
1781cb0ef41Sopenharmony_ci  brotli_reg_t key_step;  /* prefix code addend */
1791cb0ef41Sopenharmony_ci  brotli_reg_t sub_key;   /* 2nd level table prefix code */
1801cb0ef41Sopenharmony_ci  brotli_reg_t sub_key_step;  /* 2nd level table prefix code addend */
1811cb0ef41Sopenharmony_ci  int step;               /* step size to replicate values in current table */
1821cb0ef41Sopenharmony_ci  int table_bits;         /* key length of current table */
1831cb0ef41Sopenharmony_ci  int table_size;         /* size of current table */
1841cb0ef41Sopenharmony_ci  int total_size;         /* sum of root table size and 2nd level table sizes */
1851cb0ef41Sopenharmony_ci  int max_length = -1;
1861cb0ef41Sopenharmony_ci  int bits;
1871cb0ef41Sopenharmony_ci  int bits_count;
1881cb0ef41Sopenharmony_ci
1891cb0ef41Sopenharmony_ci  BROTLI_DCHECK(root_bits <= BROTLI_REVERSE_BITS_MAX);
1901cb0ef41Sopenharmony_ci  BROTLI_DCHECK(BROTLI_HUFFMAN_MAX_CODE_LENGTH - root_bits <=
1911cb0ef41Sopenharmony_ci                BROTLI_REVERSE_BITS_MAX);
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ci  while (symbol_lists[max_length] == 0xFFFF) max_length--;
1941cb0ef41Sopenharmony_ci  max_length += BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1;
1951cb0ef41Sopenharmony_ci
1961cb0ef41Sopenharmony_ci  table = root_table;
1971cb0ef41Sopenharmony_ci  table_bits = root_bits;
1981cb0ef41Sopenharmony_ci  table_size = 1 << table_bits;
1991cb0ef41Sopenharmony_ci  total_size = table_size;
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci  /* Fill in the root table. Reduce the table size to if possible,
2021cb0ef41Sopenharmony_ci     and create the repetitions by memcpy. */
2031cb0ef41Sopenharmony_ci  if (table_bits > max_length) {
2041cb0ef41Sopenharmony_ci    table_bits = max_length;
2051cb0ef41Sopenharmony_ci    table_size = 1 << table_bits;
2061cb0ef41Sopenharmony_ci  }
2071cb0ef41Sopenharmony_ci  key = 0;
2081cb0ef41Sopenharmony_ci  key_step = BROTLI_REVERSE_BITS_LOWEST;
2091cb0ef41Sopenharmony_ci  bits = 1;
2101cb0ef41Sopenharmony_ci  step = 2;
2111cb0ef41Sopenharmony_ci  do {
2121cb0ef41Sopenharmony_ci    symbol = bits - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
2131cb0ef41Sopenharmony_ci    for (bits_count = count[bits]; bits_count != 0; --bits_count) {
2141cb0ef41Sopenharmony_ci      symbol = symbol_lists[symbol];
2151cb0ef41Sopenharmony_ci      code = ConstructHuffmanCode((uint8_t)bits, (uint16_t)symbol);
2161cb0ef41Sopenharmony_ci      ReplicateValue(&table[BrotliReverseBits(key)], step, table_size, code);
2171cb0ef41Sopenharmony_ci      key += key_step;
2181cb0ef41Sopenharmony_ci    }
2191cb0ef41Sopenharmony_ci    step <<= 1;
2201cb0ef41Sopenharmony_ci    key_step >>= 1;
2211cb0ef41Sopenharmony_ci  } while (++bits <= table_bits);
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ci  /* If root_bits != table_bits then replicate to fill the remaining slots. */
2241cb0ef41Sopenharmony_ci  while (total_size != table_size) {
2251cb0ef41Sopenharmony_ci    memcpy(&table[table_size], &table[0],
2261cb0ef41Sopenharmony_ci           (size_t)table_size * sizeof(table[0]));
2271cb0ef41Sopenharmony_ci    table_size <<= 1;
2281cb0ef41Sopenharmony_ci  }
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_ci  /* Fill in 2nd level tables and add pointers to root table. */
2311cb0ef41Sopenharmony_ci  key_step = BROTLI_REVERSE_BITS_LOWEST >> (root_bits - 1);
2321cb0ef41Sopenharmony_ci  sub_key = (BROTLI_REVERSE_BITS_LOWEST << 1);
2331cb0ef41Sopenharmony_ci  sub_key_step = BROTLI_REVERSE_BITS_LOWEST;
2341cb0ef41Sopenharmony_ci  for (len = root_bits + 1, step = 2; len <= max_length; ++len) {
2351cb0ef41Sopenharmony_ci    symbol = len - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
2361cb0ef41Sopenharmony_ci    for (; count[len] != 0; --count[len]) {
2371cb0ef41Sopenharmony_ci      if (sub_key == (BROTLI_REVERSE_BITS_LOWEST << 1U)) {
2381cb0ef41Sopenharmony_ci        table += table_size;
2391cb0ef41Sopenharmony_ci        table_bits = NextTableBitSize(count, len, root_bits);
2401cb0ef41Sopenharmony_ci        table_size = 1 << table_bits;
2411cb0ef41Sopenharmony_ci        total_size += table_size;
2421cb0ef41Sopenharmony_ci        sub_key = BrotliReverseBits(key);
2431cb0ef41Sopenharmony_ci        key += key_step;
2441cb0ef41Sopenharmony_ci        root_table[sub_key] = ConstructHuffmanCode(
2451cb0ef41Sopenharmony_ci            (uint8_t)(table_bits + root_bits),
2461cb0ef41Sopenharmony_ci            (uint16_t)(((size_t)(table - root_table)) - sub_key));
2471cb0ef41Sopenharmony_ci        sub_key = 0;
2481cb0ef41Sopenharmony_ci      }
2491cb0ef41Sopenharmony_ci      symbol = symbol_lists[symbol];
2501cb0ef41Sopenharmony_ci      code = ConstructHuffmanCode((uint8_t)(len - root_bits), (uint16_t)symbol);
2511cb0ef41Sopenharmony_ci      ReplicateValue(
2521cb0ef41Sopenharmony_ci          &table[BrotliReverseBits(sub_key)], step, table_size, code);
2531cb0ef41Sopenharmony_ci      sub_key += sub_key_step;
2541cb0ef41Sopenharmony_ci    }
2551cb0ef41Sopenharmony_ci    step <<= 1;
2561cb0ef41Sopenharmony_ci    sub_key_step >>= 1;
2571cb0ef41Sopenharmony_ci  }
2581cb0ef41Sopenharmony_ci  return (uint32_t)total_size;
2591cb0ef41Sopenharmony_ci}
2601cb0ef41Sopenharmony_ci
2611cb0ef41Sopenharmony_ciuint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
2621cb0ef41Sopenharmony_ci                                       int root_bits,
2631cb0ef41Sopenharmony_ci                                       uint16_t* val,
2641cb0ef41Sopenharmony_ci                                       uint32_t num_symbols) {
2651cb0ef41Sopenharmony_ci  uint32_t table_size = 1;
2661cb0ef41Sopenharmony_ci  const uint32_t goal_size = 1U << root_bits;
2671cb0ef41Sopenharmony_ci  switch (num_symbols) {
2681cb0ef41Sopenharmony_ci    case 0:
2691cb0ef41Sopenharmony_ci      table[0] = ConstructHuffmanCode(0, val[0]);
2701cb0ef41Sopenharmony_ci      break;
2711cb0ef41Sopenharmony_ci    case 1:
2721cb0ef41Sopenharmony_ci      if (val[1] > val[0]) {
2731cb0ef41Sopenharmony_ci        table[0] = ConstructHuffmanCode(1, val[0]);
2741cb0ef41Sopenharmony_ci        table[1] = ConstructHuffmanCode(1, val[1]);
2751cb0ef41Sopenharmony_ci      } else {
2761cb0ef41Sopenharmony_ci        table[0] = ConstructHuffmanCode(1, val[1]);
2771cb0ef41Sopenharmony_ci        table[1] = ConstructHuffmanCode(1, val[0]);
2781cb0ef41Sopenharmony_ci      }
2791cb0ef41Sopenharmony_ci      table_size = 2;
2801cb0ef41Sopenharmony_ci      break;
2811cb0ef41Sopenharmony_ci    case 2:
2821cb0ef41Sopenharmony_ci      table[0] = ConstructHuffmanCode(1, val[0]);
2831cb0ef41Sopenharmony_ci      table[2] = ConstructHuffmanCode(1, val[0]);
2841cb0ef41Sopenharmony_ci      if (val[2] > val[1]) {
2851cb0ef41Sopenharmony_ci        table[1] = ConstructHuffmanCode(2, val[1]);
2861cb0ef41Sopenharmony_ci        table[3] = ConstructHuffmanCode(2, val[2]);
2871cb0ef41Sopenharmony_ci      } else {
2881cb0ef41Sopenharmony_ci        table[1] = ConstructHuffmanCode(2, val[2]);
2891cb0ef41Sopenharmony_ci        table[3] = ConstructHuffmanCode(2, val[1]);
2901cb0ef41Sopenharmony_ci      }
2911cb0ef41Sopenharmony_ci      table_size = 4;
2921cb0ef41Sopenharmony_ci      break;
2931cb0ef41Sopenharmony_ci    case 3: {
2941cb0ef41Sopenharmony_ci      int i, k;
2951cb0ef41Sopenharmony_ci      for (i = 0; i < 3; ++i) {
2961cb0ef41Sopenharmony_ci        for (k = i + 1; k < 4; ++k) {
2971cb0ef41Sopenharmony_ci          if (val[k] < val[i]) {
2981cb0ef41Sopenharmony_ci            uint16_t t = val[k];
2991cb0ef41Sopenharmony_ci            val[k] = val[i];
3001cb0ef41Sopenharmony_ci            val[i] = t;
3011cb0ef41Sopenharmony_ci          }
3021cb0ef41Sopenharmony_ci        }
3031cb0ef41Sopenharmony_ci      }
3041cb0ef41Sopenharmony_ci      table[0] = ConstructHuffmanCode(2, val[0]);
3051cb0ef41Sopenharmony_ci      table[2] = ConstructHuffmanCode(2, val[1]);
3061cb0ef41Sopenharmony_ci      table[1] = ConstructHuffmanCode(2, val[2]);
3071cb0ef41Sopenharmony_ci      table[3] = ConstructHuffmanCode(2, val[3]);
3081cb0ef41Sopenharmony_ci      table_size = 4;
3091cb0ef41Sopenharmony_ci      break;
3101cb0ef41Sopenharmony_ci    }
3111cb0ef41Sopenharmony_ci    case 4: {
3121cb0ef41Sopenharmony_ci      if (val[3] < val[2]) {
3131cb0ef41Sopenharmony_ci        uint16_t t = val[3];
3141cb0ef41Sopenharmony_ci        val[3] = val[2];
3151cb0ef41Sopenharmony_ci        val[2] = t;
3161cb0ef41Sopenharmony_ci      }
3171cb0ef41Sopenharmony_ci      table[0] = ConstructHuffmanCode(1, val[0]);
3181cb0ef41Sopenharmony_ci      table[1] = ConstructHuffmanCode(2, val[1]);
3191cb0ef41Sopenharmony_ci      table[2] = ConstructHuffmanCode(1, val[0]);
3201cb0ef41Sopenharmony_ci      table[3] = ConstructHuffmanCode(3, val[2]);
3211cb0ef41Sopenharmony_ci      table[4] = ConstructHuffmanCode(1, val[0]);
3221cb0ef41Sopenharmony_ci      table[5] = ConstructHuffmanCode(2, val[1]);
3231cb0ef41Sopenharmony_ci      table[6] = ConstructHuffmanCode(1, val[0]);
3241cb0ef41Sopenharmony_ci      table[7] = ConstructHuffmanCode(3, val[3]);
3251cb0ef41Sopenharmony_ci      table_size = 8;
3261cb0ef41Sopenharmony_ci      break;
3271cb0ef41Sopenharmony_ci    }
3281cb0ef41Sopenharmony_ci  }
3291cb0ef41Sopenharmony_ci  while (table_size != goal_size) {
3301cb0ef41Sopenharmony_ci    memcpy(&table[table_size], &table[0],
3311cb0ef41Sopenharmony_ci           (size_t)table_size * sizeof(table[0]));
3321cb0ef41Sopenharmony_ci    table_size <<= 1;
3331cb0ef41Sopenharmony_ci  }
3341cb0ef41Sopenharmony_ci  return goal_size;
3351cb0ef41Sopenharmony_ci}
3361cb0ef41Sopenharmony_ci
3371cb0ef41Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus)
3381cb0ef41Sopenharmony_ci}  /* extern "C" */
3391cb0ef41Sopenharmony_ci#endif
340