tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

huffman.h (4080B)


      1 /* Copyright 2013 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 /* Utilities for building Huffman decoding tables. */
      8 
      9 #ifndef BROTLI_DEC_HUFFMAN_H_
     10 #define BROTLI_DEC_HUFFMAN_H_
     11 
     12 #include "../common/platform.h"
     13 
     14 #if defined(__cplusplus) || defined(c_plusplus)
     15 extern "C" {
     16 #endif
     17 
     18 #define BROTLI_HUFFMAN_MAX_CODE_LENGTH 15
     19 
     20 /* BROTLI_NUM_BLOCK_LEN_SYMBOLS == 26 */
     21 #define BROTLI_HUFFMAN_MAX_SIZE_26 396
     22 /* BROTLI_MAX_BLOCK_TYPE_SYMBOLS == 258 */
     23 #define BROTLI_HUFFMAN_MAX_SIZE_258 632
     24 /* BROTLI_MAX_CONTEXT_MAP_SYMBOLS == 272 */
     25 #define BROTLI_HUFFMAN_MAX_SIZE_272 646
     26 
     27 #define BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH 5
     28 
     29 #if ((defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_32)) && \
     30  BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0))
     31 #define BROTLI_HUFFMAN_CODE_FAST_LOAD
     32 #endif
     33 
     34 #if !defined(BROTLI_HUFFMAN_CODE_FAST_LOAD)
     35 /* Do not create this struct directly - use the ConstructHuffmanCode
     36 * constructor below! */
     37 typedef struct {
     38  uint8_t bits;    /* number of bits used for this symbol */
     39  uint16_t value;  /* symbol value or table offset */
     40 } HuffmanCode;
     41 
     42 static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits,
     43    const uint16_t value) {
     44  HuffmanCode h;
     45  h.bits = bits;
     46  h.value = value;
     47  return h;
     48 }
     49 
     50 /* Please use the following macros to optimize HuffmanCode accesses in hot
     51 * paths.
     52 *
     53 * For example, assuming |table| contains a HuffmanCode pointer:
     54 *
     55 *   BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table);
     56 *   BROTLI_HC_ADJUST_TABLE_INDEX(table, index_into_table);
     57 *   *bits = BROTLI_HC_GET_BITS(table);
     58 *   *value = BROTLI_HC_GET_VALUE(table);
     59 *   BROTLI_HC_ADJUST_TABLE_INDEX(table, offset);
     60 *   *bits2 = BROTLI_HC_GET_BITS(table);
     61 *   *value2 = BROTLI_HC_GET_VALUE(table);
     62 *
     63 */
     64 
     65 #define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H)
     66 #define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V)
     67 
     68 /* These must be given a HuffmanCode pointer! */
     69 #define BROTLI_HC_FAST_LOAD_BITS(H) (H->bits)
     70 #define BROTLI_HC_FAST_LOAD_VALUE(H) (H->value)
     71 
     72 #else /* BROTLI_HUFFMAN_CODE_FAST_LOAD */
     73 
     74 typedef BROTLI_ALIGNED(4) uint32_t HuffmanCode;
     75 
     76 static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits,
     77    const uint16_t value) {
     78  return (HuffmanCode) ((value & 0xFFFF) << 16) | (bits & 0xFF);
     79 }
     80 
     81 #define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H) uint32_t __fastload_##H = (*H)
     82 #define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V); __fastload_##H = (*H)
     83 
     84 /* These must be given a HuffmanCode pointer! */
     85 #define BROTLI_HC_FAST_LOAD_BITS(H) ((__fastload_##H) & 0xFF)
     86 #define BROTLI_HC_FAST_LOAD_VALUE(H) ((__fastload_##H) >> 16)
     87 #endif /* BROTLI_HUFFMAN_CODE_FAST_LOAD */
     88 
     89 /* Builds Huffman lookup table assuming code lengths are in symbol order. */
     90 BROTLI_INTERNAL void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table,
     91    const uint8_t* const code_lengths, uint16_t* count);
     92 
     93 /* Builds Huffman lookup table assuming code lengths are in symbol order.
     94   Returns size of resulting table. */
     95 BROTLI_INTERNAL uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
     96    int root_bits, const uint16_t* const symbol_lists, uint16_t* count);
     97 
     98 /* Builds a simple Huffman table. The |num_symbols| parameter is to be
     99   interpreted as follows: 0 means 1 symbol, 1 means 2 symbols,
    100   2 means 3 symbols, 3 means 4 symbols with lengths [2, 2, 2, 2],
    101   4 means 4 symbols with lengths [1, 2, 3, 3]. */
    102 BROTLI_INTERNAL uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
    103    int root_bits, uint16_t* symbols, uint32_t num_symbols);
    104 
    105 /* Contains a collection of Huffman trees with the same alphabet size. */
    106 /* alphabet_size_limit is needed due to simple codes, since
    107   log2(alphabet_size_max) could be greater than log2(alphabet_size_limit). */
    108 typedef struct {
    109  HuffmanCode** htrees;
    110  HuffmanCode* codes;
    111  uint16_t alphabet_size_max;
    112  uint16_t alphabet_size_limit;
    113  uint16_t num_htrees;
    114 } HuffmanTreeGroup;
    115 
    116 #if defined(__cplusplus) || defined(c_plusplus)
    117 }  /* extern "C" */
    118 #endif
    119 
    120 #endif  /* BROTLI_DEC_HUFFMAN_H_ */