1cb93a386Sopenharmony_ci/* Copyright 2013 Google Inc. All Rights Reserved.
2cb93a386Sopenharmony_ci
3cb93a386Sopenharmony_ci   Distributed under MIT license.
4cb93a386Sopenharmony_ci   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5cb93a386Sopenharmony_ci*/
6cb93a386Sopenharmony_ci
7cb93a386Sopenharmony_ci/* Utilities for building Huffman decoding tables. */
8cb93a386Sopenharmony_ci
9cb93a386Sopenharmony_ci#include "./huffman.h"
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include <string.h>  /* memcpy, memset */
12cb93a386Sopenharmony_ci
13cb93a386Sopenharmony_ci#include "../common/constants.h"
14cb93a386Sopenharmony_ci#include "../common/platform.h"
15cb93a386Sopenharmony_ci#include <brotli/types.h>
16cb93a386Sopenharmony_ci
17cb93a386Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus)
18cb93a386Sopenharmony_ciextern "C" {
19cb93a386Sopenharmony_ci#endif
20cb93a386Sopenharmony_ci
21cb93a386Sopenharmony_ci#define BROTLI_REVERSE_BITS_MAX 8
22cb93a386Sopenharmony_ci
23cb93a386Sopenharmony_ci#if defined(BROTLI_RBIT)
24cb93a386Sopenharmony_ci#define BROTLI_REVERSE_BITS_BASE \
25cb93a386Sopenharmony_ci  ((sizeof(brotli_reg_t) << 3) - BROTLI_REVERSE_BITS_MAX)
26cb93a386Sopenharmony_ci#else
27cb93a386Sopenharmony_ci#define BROTLI_REVERSE_BITS_BASE 0
28cb93a386Sopenharmony_cistatic uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = {
29cb93a386Sopenharmony_ci  0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
30cb93a386Sopenharmony_ci  0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
31cb93a386Sopenharmony_ci  0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
32cb93a386Sopenharmony_ci  0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
33cb93a386Sopenharmony_ci  0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4,
34cb93a386Sopenharmony_ci  0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
35cb93a386Sopenharmony_ci  0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC,
36cb93a386Sopenharmony_ci  0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
37cb93a386Sopenharmony_ci  0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,
38cb93a386Sopenharmony_ci  0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
39cb93a386Sopenharmony_ci  0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA,
40cb93a386Sopenharmony_ci  0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
41cb93a386Sopenharmony_ci  0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6,
42cb93a386Sopenharmony_ci  0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
43cb93a386Sopenharmony_ci  0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE,
44cb93a386Sopenharmony_ci  0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
45cb93a386Sopenharmony_ci  0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1,
46cb93a386Sopenharmony_ci  0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
47cb93a386Sopenharmony_ci  0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9,
48cb93a386Sopenharmony_ci  0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
49cb93a386Sopenharmony_ci  0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5,
50cb93a386Sopenharmony_ci  0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
51cb93a386Sopenharmony_ci  0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED,
52cb93a386Sopenharmony_ci  0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
53cb93a386Sopenharmony_ci  0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3,
54cb93a386Sopenharmony_ci  0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
55cb93a386Sopenharmony_ci  0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB,
56cb93a386Sopenharmony_ci  0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
57cb93a386Sopenharmony_ci  0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7,
58cb93a386Sopenharmony_ci  0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
59cb93a386Sopenharmony_ci  0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF,
60cb93a386Sopenharmony_ci  0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
61cb93a386Sopenharmony_ci};
62cb93a386Sopenharmony_ci#endif  /* BROTLI_RBIT */
63cb93a386Sopenharmony_ci
64cb93a386Sopenharmony_ci#define BROTLI_REVERSE_BITS_LOWEST \
65cb93a386Sopenharmony_ci  ((brotli_reg_t)1 << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE))
66cb93a386Sopenharmony_ci
67cb93a386Sopenharmony_ci/* Returns reverse(num >> BROTLI_REVERSE_BITS_BASE, BROTLI_REVERSE_BITS_MAX),
68cb93a386Sopenharmony_ci   where reverse(value, len) is the bit-wise reversal of the len least
69cb93a386Sopenharmony_ci   significant bits of value. */
70cb93a386Sopenharmony_cistatic BROTLI_INLINE brotli_reg_t BrotliReverseBits(brotli_reg_t num) {
71cb93a386Sopenharmony_ci#if defined(BROTLI_RBIT)
72cb93a386Sopenharmony_ci  return BROTLI_RBIT(num);
73cb93a386Sopenharmony_ci#else
74cb93a386Sopenharmony_ci  return kReverseBits[num];
75cb93a386Sopenharmony_ci#endif
76cb93a386Sopenharmony_ci}
77cb93a386Sopenharmony_ci
78cb93a386Sopenharmony_ci/* Stores code in table[0], table[step], table[2*step], ..., table[end] */
79cb93a386Sopenharmony_ci/* Assumes that end is an integer multiple of step */
80cb93a386Sopenharmony_cistatic BROTLI_INLINE void ReplicateValue(HuffmanCode* table,
81cb93a386Sopenharmony_ci                                         int step, int end,
82cb93a386Sopenharmony_ci                                         HuffmanCode code) {
83cb93a386Sopenharmony_ci  do {
84cb93a386Sopenharmony_ci    end -= step;
85cb93a386Sopenharmony_ci    table[end] = code;
86cb93a386Sopenharmony_ci  } while (end > 0);
87cb93a386Sopenharmony_ci}
88cb93a386Sopenharmony_ci
89cb93a386Sopenharmony_ci/* Returns the table width of the next 2nd level table. |count| is the histogram
90cb93a386Sopenharmony_ci   of bit lengths for the remaining symbols, |len| is the code length of the
91cb93a386Sopenharmony_ci   next processed symbol. */
92cb93a386Sopenharmony_cistatic BROTLI_INLINE int NextTableBitSize(const uint16_t* const count,
93cb93a386Sopenharmony_ci                                          int len, int root_bits) {
94cb93a386Sopenharmony_ci  int left = 1 << (len - root_bits);
95cb93a386Sopenharmony_ci  while (len < BROTLI_HUFFMAN_MAX_CODE_LENGTH) {
96cb93a386Sopenharmony_ci    left -= count[len];
97cb93a386Sopenharmony_ci    if (left <= 0) break;
98cb93a386Sopenharmony_ci    ++len;
99cb93a386Sopenharmony_ci    left <<= 1;
100cb93a386Sopenharmony_ci  }
101cb93a386Sopenharmony_ci  return len - root_bits;
102cb93a386Sopenharmony_ci}
103cb93a386Sopenharmony_ci
104cb93a386Sopenharmony_civoid BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
105cb93a386Sopenharmony_ci                                        const uint8_t* const code_lengths,
106cb93a386Sopenharmony_ci                                        uint16_t* count) {
107cb93a386Sopenharmony_ci  HuffmanCode code;       /* current table entry */
108cb93a386Sopenharmony_ci  int symbol;             /* symbol index in original or sorted table */
109cb93a386Sopenharmony_ci  brotli_reg_t key;       /* prefix code */
110cb93a386Sopenharmony_ci  brotli_reg_t key_step;  /* prefix code addend */
111cb93a386Sopenharmony_ci  int step;               /* step size to replicate values in current table */
112cb93a386Sopenharmony_ci  int table_size;         /* size of current table */
113cb93a386Sopenharmony_ci  int sorted[BROTLI_CODE_LENGTH_CODES];  /* symbols sorted by code length */
114cb93a386Sopenharmony_ci  /* offsets in sorted table for each length */
115cb93a386Sopenharmony_ci  int offset[BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1];
116cb93a386Sopenharmony_ci  int bits;
117cb93a386Sopenharmony_ci  int bits_count;
118cb93a386Sopenharmony_ci  BROTLI_DCHECK(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH <=
119cb93a386Sopenharmony_ci                BROTLI_REVERSE_BITS_MAX);
120cb93a386Sopenharmony_ci
121cb93a386Sopenharmony_ci  /* Generate offsets into sorted symbol table by code length. */
122cb93a386Sopenharmony_ci  symbol = -1;
123cb93a386Sopenharmony_ci  bits = 1;
124cb93a386Sopenharmony_ci  BROTLI_REPEAT(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH, {
125cb93a386Sopenharmony_ci    symbol += count[bits];
126cb93a386Sopenharmony_ci    offset[bits] = symbol;
127cb93a386Sopenharmony_ci    bits++;
128cb93a386Sopenharmony_ci  });
129cb93a386Sopenharmony_ci  /* Symbols with code length 0 are placed after all other symbols. */
130cb93a386Sopenharmony_ci  offset[0] = BROTLI_CODE_LENGTH_CODES - 1;
131cb93a386Sopenharmony_ci
132cb93a386Sopenharmony_ci  /* Sort symbols by length, by symbol order within each length. */
133cb93a386Sopenharmony_ci  symbol = BROTLI_CODE_LENGTH_CODES;
134cb93a386Sopenharmony_ci  do {
135cb93a386Sopenharmony_ci    BROTLI_REPEAT(6, {
136cb93a386Sopenharmony_ci      symbol--;
137cb93a386Sopenharmony_ci      sorted[offset[code_lengths[symbol]]--] = symbol;
138cb93a386Sopenharmony_ci    });
139cb93a386Sopenharmony_ci  } while (symbol != 0);
140cb93a386Sopenharmony_ci
141cb93a386Sopenharmony_ci  table_size = 1 << BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH;
142cb93a386Sopenharmony_ci
143cb93a386Sopenharmony_ci  /* Special case: all symbols but one have 0 code length. */
144cb93a386Sopenharmony_ci  if (offset[0] == 0) {
145cb93a386Sopenharmony_ci    code = ConstructHuffmanCode(0, (uint16_t)sorted[0]);
146cb93a386Sopenharmony_ci    for (key = 0; key < (brotli_reg_t)table_size; ++key) {
147cb93a386Sopenharmony_ci      table[key] = code;
148cb93a386Sopenharmony_ci    }
149cb93a386Sopenharmony_ci    return;
150cb93a386Sopenharmony_ci  }
151cb93a386Sopenharmony_ci
152cb93a386Sopenharmony_ci  /* Fill in table. */
153cb93a386Sopenharmony_ci  key = 0;
154cb93a386Sopenharmony_ci  key_step = BROTLI_REVERSE_BITS_LOWEST;
155cb93a386Sopenharmony_ci  symbol = 0;
156cb93a386Sopenharmony_ci  bits = 1;
157cb93a386Sopenharmony_ci  step = 2;
158cb93a386Sopenharmony_ci  do {
159cb93a386Sopenharmony_ci    for (bits_count = count[bits]; bits_count != 0; --bits_count) {
160cb93a386Sopenharmony_ci      code = ConstructHuffmanCode((uint8_t)bits, (uint16_t)sorted[symbol++]);
161cb93a386Sopenharmony_ci      ReplicateValue(&table[BrotliReverseBits(key)], step, table_size, code);
162cb93a386Sopenharmony_ci      key += key_step;
163cb93a386Sopenharmony_ci    }
164cb93a386Sopenharmony_ci    step <<= 1;
165cb93a386Sopenharmony_ci    key_step >>= 1;
166cb93a386Sopenharmony_ci  } while (++bits <= BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH);
167cb93a386Sopenharmony_ci}
168cb93a386Sopenharmony_ci
169cb93a386Sopenharmony_ciuint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
170cb93a386Sopenharmony_ci                                 int root_bits,
171cb93a386Sopenharmony_ci                                 const uint16_t* const symbol_lists,
172cb93a386Sopenharmony_ci                                 uint16_t* count) {
173cb93a386Sopenharmony_ci  HuffmanCode code;       /* current table entry */
174cb93a386Sopenharmony_ci  HuffmanCode* table;     /* next available space in table */
175cb93a386Sopenharmony_ci  int len;                /* current code length */
176cb93a386Sopenharmony_ci  int symbol;             /* symbol index in original or sorted table */
177cb93a386Sopenharmony_ci  brotli_reg_t key;       /* prefix code */
178cb93a386Sopenharmony_ci  brotli_reg_t key_step;  /* prefix code addend */
179cb93a386Sopenharmony_ci  brotli_reg_t sub_key;   /* 2nd level table prefix code */
180cb93a386Sopenharmony_ci  brotli_reg_t sub_key_step;  /* 2nd level table prefix code addend */
181cb93a386Sopenharmony_ci  int step;               /* step size to replicate values in current table */
182cb93a386Sopenharmony_ci  int table_bits;         /* key length of current table */
183cb93a386Sopenharmony_ci  int table_size;         /* size of current table */
184cb93a386Sopenharmony_ci  int total_size;         /* sum of root table size and 2nd level table sizes */
185cb93a386Sopenharmony_ci  int max_length = -1;
186cb93a386Sopenharmony_ci  int bits;
187cb93a386Sopenharmony_ci  int bits_count;
188cb93a386Sopenharmony_ci
189cb93a386Sopenharmony_ci  BROTLI_DCHECK(root_bits <= BROTLI_REVERSE_BITS_MAX);
190cb93a386Sopenharmony_ci  BROTLI_DCHECK(BROTLI_HUFFMAN_MAX_CODE_LENGTH - root_bits <=
191cb93a386Sopenharmony_ci                BROTLI_REVERSE_BITS_MAX);
192cb93a386Sopenharmony_ci
193cb93a386Sopenharmony_ci  while (symbol_lists[max_length] == 0xFFFF) max_length--;
194cb93a386Sopenharmony_ci  max_length += BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1;
195cb93a386Sopenharmony_ci
196cb93a386Sopenharmony_ci  table = root_table;
197cb93a386Sopenharmony_ci  table_bits = root_bits;
198cb93a386Sopenharmony_ci  table_size = 1 << table_bits;
199cb93a386Sopenharmony_ci  total_size = table_size;
200cb93a386Sopenharmony_ci
201cb93a386Sopenharmony_ci  /* Fill in the root table. Reduce the table size to if possible,
202cb93a386Sopenharmony_ci     and create the repetitions by memcpy. */
203cb93a386Sopenharmony_ci  if (table_bits > max_length) {
204cb93a386Sopenharmony_ci    table_bits = max_length;
205cb93a386Sopenharmony_ci    table_size = 1 << table_bits;
206cb93a386Sopenharmony_ci  }
207cb93a386Sopenharmony_ci  key = 0;
208cb93a386Sopenharmony_ci  key_step = BROTLI_REVERSE_BITS_LOWEST;
209cb93a386Sopenharmony_ci  bits = 1;
210cb93a386Sopenharmony_ci  step = 2;
211cb93a386Sopenharmony_ci  do {
212cb93a386Sopenharmony_ci    symbol = bits - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
213cb93a386Sopenharmony_ci    for (bits_count = count[bits]; bits_count != 0; --bits_count) {
214cb93a386Sopenharmony_ci      symbol = symbol_lists[symbol];
215cb93a386Sopenharmony_ci      code = ConstructHuffmanCode((uint8_t)bits, (uint16_t)symbol);
216cb93a386Sopenharmony_ci      ReplicateValue(&table[BrotliReverseBits(key)], step, table_size, code);
217cb93a386Sopenharmony_ci      key += key_step;
218cb93a386Sopenharmony_ci    }
219cb93a386Sopenharmony_ci    step <<= 1;
220cb93a386Sopenharmony_ci    key_step >>= 1;
221cb93a386Sopenharmony_ci  } while (++bits <= table_bits);
222cb93a386Sopenharmony_ci
223cb93a386Sopenharmony_ci  /* If root_bits != table_bits then replicate to fill the remaining slots. */
224cb93a386Sopenharmony_ci  while (total_size != table_size) {
225cb93a386Sopenharmony_ci    memcpy(&table[table_size], &table[0],
226cb93a386Sopenharmony_ci           (size_t)table_size * sizeof(table[0]));
227cb93a386Sopenharmony_ci    table_size <<= 1;
228cb93a386Sopenharmony_ci  }
229cb93a386Sopenharmony_ci
230cb93a386Sopenharmony_ci  /* Fill in 2nd level tables and add pointers to root table. */
231cb93a386Sopenharmony_ci  key_step = BROTLI_REVERSE_BITS_LOWEST >> (root_bits - 1);
232cb93a386Sopenharmony_ci  sub_key = (BROTLI_REVERSE_BITS_LOWEST << 1);
233cb93a386Sopenharmony_ci  sub_key_step = BROTLI_REVERSE_BITS_LOWEST;
234cb93a386Sopenharmony_ci  for (len = root_bits + 1, step = 2; len <= max_length; ++len) {
235cb93a386Sopenharmony_ci    symbol = len - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
236cb93a386Sopenharmony_ci    for (; count[len] != 0; --count[len]) {
237cb93a386Sopenharmony_ci      if (sub_key == (BROTLI_REVERSE_BITS_LOWEST << 1U)) {
238cb93a386Sopenharmony_ci        table += table_size;
239cb93a386Sopenharmony_ci        table_bits = NextTableBitSize(count, len, root_bits);
240cb93a386Sopenharmony_ci        table_size = 1 << table_bits;
241cb93a386Sopenharmony_ci        total_size += table_size;
242cb93a386Sopenharmony_ci        sub_key = BrotliReverseBits(key);
243cb93a386Sopenharmony_ci        key += key_step;
244cb93a386Sopenharmony_ci        root_table[sub_key] = ConstructHuffmanCode(
245cb93a386Sopenharmony_ci            (uint8_t)(table_bits + root_bits),
246cb93a386Sopenharmony_ci            (uint16_t)(((size_t)(table - root_table)) - sub_key));
247cb93a386Sopenharmony_ci        sub_key = 0;
248cb93a386Sopenharmony_ci      }
249cb93a386Sopenharmony_ci      symbol = symbol_lists[symbol];
250cb93a386Sopenharmony_ci      code = ConstructHuffmanCode((uint8_t)(len - root_bits), (uint16_t)symbol);
251cb93a386Sopenharmony_ci      ReplicateValue(
252cb93a386Sopenharmony_ci          &table[BrotliReverseBits(sub_key)], step, table_size, code);
253cb93a386Sopenharmony_ci      sub_key += sub_key_step;
254cb93a386Sopenharmony_ci    }
255cb93a386Sopenharmony_ci    step <<= 1;
256cb93a386Sopenharmony_ci    sub_key_step >>= 1;
257cb93a386Sopenharmony_ci  }
258cb93a386Sopenharmony_ci  return (uint32_t)total_size;
259cb93a386Sopenharmony_ci}
260cb93a386Sopenharmony_ci
261cb93a386Sopenharmony_ciuint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
262cb93a386Sopenharmony_ci                                       int root_bits,
263cb93a386Sopenharmony_ci                                       uint16_t* val,
264cb93a386Sopenharmony_ci                                       uint32_t num_symbols) {
265cb93a386Sopenharmony_ci  uint32_t table_size = 1;
266cb93a386Sopenharmony_ci  const uint32_t goal_size = 1U << root_bits;
267cb93a386Sopenharmony_ci  switch (num_symbols) {
268cb93a386Sopenharmony_ci    case 0:
269cb93a386Sopenharmony_ci      table[0] = ConstructHuffmanCode(0, val[0]);
270cb93a386Sopenharmony_ci      break;
271cb93a386Sopenharmony_ci    case 1:
272cb93a386Sopenharmony_ci      if (val[1] > val[0]) {
273cb93a386Sopenharmony_ci        table[0] = ConstructHuffmanCode(1, val[0]);
274cb93a386Sopenharmony_ci        table[1] = ConstructHuffmanCode(1, val[1]);
275cb93a386Sopenharmony_ci      } else {
276cb93a386Sopenharmony_ci        table[0] = ConstructHuffmanCode(1, val[1]);
277cb93a386Sopenharmony_ci        table[1] = ConstructHuffmanCode(1, val[0]);
278cb93a386Sopenharmony_ci      }
279cb93a386Sopenharmony_ci      table_size = 2;
280cb93a386Sopenharmony_ci      break;
281cb93a386Sopenharmony_ci    case 2:
282cb93a386Sopenharmony_ci      table[0] = ConstructHuffmanCode(1, val[0]);
283cb93a386Sopenharmony_ci      table[2] = ConstructHuffmanCode(1, val[0]);
284cb93a386Sopenharmony_ci      if (val[2] > val[1]) {
285cb93a386Sopenharmony_ci        table[1] = ConstructHuffmanCode(2, val[1]);
286cb93a386Sopenharmony_ci        table[3] = ConstructHuffmanCode(2, val[2]);
287cb93a386Sopenharmony_ci      } else {
288cb93a386Sopenharmony_ci        table[1] = ConstructHuffmanCode(2, val[2]);
289cb93a386Sopenharmony_ci        table[3] = ConstructHuffmanCode(2, val[1]);
290cb93a386Sopenharmony_ci      }
291cb93a386Sopenharmony_ci      table_size = 4;
292cb93a386Sopenharmony_ci      break;
293cb93a386Sopenharmony_ci    case 3: {
294cb93a386Sopenharmony_ci      int i, k;
295cb93a386Sopenharmony_ci      for (i = 0; i < 3; ++i) {
296cb93a386Sopenharmony_ci        for (k = i + 1; k < 4; ++k) {
297cb93a386Sopenharmony_ci          if (val[k] < val[i]) {
298cb93a386Sopenharmony_ci            uint16_t t = val[k];
299cb93a386Sopenharmony_ci            val[k] = val[i];
300cb93a386Sopenharmony_ci            val[i] = t;
301cb93a386Sopenharmony_ci          }
302cb93a386Sopenharmony_ci        }
303cb93a386Sopenharmony_ci      }
304cb93a386Sopenharmony_ci      table[0] = ConstructHuffmanCode(2, val[0]);
305cb93a386Sopenharmony_ci      table[2] = ConstructHuffmanCode(2, val[1]);
306cb93a386Sopenharmony_ci      table[1] = ConstructHuffmanCode(2, val[2]);
307cb93a386Sopenharmony_ci      table[3] = ConstructHuffmanCode(2, val[3]);
308cb93a386Sopenharmony_ci      table_size = 4;
309cb93a386Sopenharmony_ci      break;
310cb93a386Sopenharmony_ci    }
311cb93a386Sopenharmony_ci    case 4: {
312cb93a386Sopenharmony_ci      if (val[3] < val[2]) {
313cb93a386Sopenharmony_ci        uint16_t t = val[3];
314cb93a386Sopenharmony_ci        val[3] = val[2];
315cb93a386Sopenharmony_ci        val[2] = t;
316cb93a386Sopenharmony_ci      }
317cb93a386Sopenharmony_ci      table[0] = ConstructHuffmanCode(1, val[0]);
318cb93a386Sopenharmony_ci      table[1] = ConstructHuffmanCode(2, val[1]);
319cb93a386Sopenharmony_ci      table[2] = ConstructHuffmanCode(1, val[0]);
320cb93a386Sopenharmony_ci      table[3] = ConstructHuffmanCode(3, val[2]);
321cb93a386Sopenharmony_ci      table[4] = ConstructHuffmanCode(1, val[0]);
322cb93a386Sopenharmony_ci      table[5] = ConstructHuffmanCode(2, val[1]);
323cb93a386Sopenharmony_ci      table[6] = ConstructHuffmanCode(1, val[0]);
324cb93a386Sopenharmony_ci      table[7] = ConstructHuffmanCode(3, val[3]);
325cb93a386Sopenharmony_ci      table_size = 8;
326cb93a386Sopenharmony_ci      break;
327cb93a386Sopenharmony_ci    }
328cb93a386Sopenharmony_ci  }
329cb93a386Sopenharmony_ci  while (table_size != goal_size) {
330cb93a386Sopenharmony_ci    memcpy(&table[table_size], &table[0],
331cb93a386Sopenharmony_ci           (size_t)table_size * sizeof(table[0]));
332cb93a386Sopenharmony_ci    table_size <<= 1;
333cb93a386Sopenharmony_ci  }
334cb93a386Sopenharmony_ci  return goal_size;
335cb93a386Sopenharmony_ci}
336cb93a386Sopenharmony_ci
337cb93a386Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus)
338cb93a386Sopenharmony_ci}  /* extern "C" */
339cb93a386Sopenharmony_ci#endif
340