]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/BrotliCustomDecompressLib/dec/huffman.c
MdeModulePkg: Update Brotli DecompressLib to the latest v1.0.6
[mirror_edk2.git] / MdeModulePkg / Library / BrotliCustomDecompressLib / dec / huffman.c
index 6b99cfea4b88b6723379cb2261ffba5b1c939c2b..bf20109e07d54bf47471e1e3329abc5600416419 100644 (file)
@@ -11,8 +11,8 @@
 //#include <string.h>  /* memcpy, memset */\r
 \r
 #include "../common/constants.h"\r
-#include "../common/types.h"\r
-#include "./port.h"\r
+#include "../common/platform.h"\r
+#include <brotli/types.h>\r
 \r
 #if defined(__cplusplus) || defined(c_plusplus)\r
 extern "C" {\r
@@ -20,8 +20,9 @@ extern "C" {
 \r
 #define BROTLI_REVERSE_BITS_MAX 8\r
 \r
-#ifdef BROTLI_RBIT\r
-#define BROTLI_REVERSE_BITS_BASE (32 - BROTLI_REVERSE_BITS_MAX)\r
+#if defined(BROTLI_RBIT)\r
+#define BROTLI_REVERSE_BITS_BASE \\r
+  ((sizeof(brotli_reg_t) << 3) - BROTLI_REVERSE_BITS_MAX)\r
 #else\r
 #define BROTLI_REVERSE_BITS_BASE 0\r
 static uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = {\r
@@ -61,13 +62,13 @@ static uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = {
 #endif  /* BROTLI_RBIT */\r
 \r
 #define BROTLI_REVERSE_BITS_LOWEST \\r
-  (1U << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE))\r
+  ((brotli_reg_t)1 << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE))\r
 \r
 /* Returns reverse(num >> BROTLI_REVERSE_BITS_BASE, BROTLI_REVERSE_BITS_MAX),\r
    where reverse(value, len) is the bit-wise reversal of the len least\r
    significant bits of value. */\r
-static BROTLI_INLINE uint32_t BrotliReverseBits(uint32_t num) {\r
-#ifdef BROTLI_RBIT\r
+static BROTLI_INLINE brotli_reg_t BrotliReverseBits(brotli_reg_t num) {\r
+#if defined(BROTLI_RBIT)\r
   return BROTLI_RBIT(num);\r
 #else\r
   return kReverseBits[num];\r
@@ -85,9 +86,9 @@ static BROTLI_INLINE void ReplicateValue(HuffmanCode* table,
   } while (end > 0);\r
 }\r
 \r
-/* Returns the table width of the next 2nd level table. count is the histogram\r
-   of bit lengths for the remaining symbols, len is the code length of the next\r
-   processed symbol */\r
+/* Returns the table width of the next 2nd level table. |count| is the histogram\r
+   of bit lengths for the remaining symbols, |len| is the code length of the\r
+   next processed symbol. */\r
 static BROTLI_INLINE int NextTableBitSize(const uint16_t* const count,\r
                                           int len, int root_bits) {\r
   int left = 1 << (len - root_bits);\r
@@ -103,12 +104,12 @@ static BROTLI_INLINE int NextTableBitSize(const uint16_t* const count,
 void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,\r
                                         const uint8_t* const code_lengths,\r
                                         uint16_t* count) {\r
-  HuffmanCode code;   /* current table entry */\r
-  int symbol;         /* symbol index in original or sorted table */\r
-  uint32_t key;       /* prefix code */\r
-  uint32_t key_step;  /* prefix code addend */\r
-  int step;           /* step size to replicate values in current table */\r
-  int table_size;     /* size of current table */\r
+  HuffmanCode code;       /* current table entry */\r
+  int symbol;             /* symbol index in original or sorted table */\r
+  brotli_reg_t key;       /* prefix code */\r
+  brotli_reg_t key_step;  /* prefix code addend */\r
+  int step;               /* step size to replicate values in current table */\r
+  int table_size;         /* size of current table */\r
   int sorted[BROTLI_CODE_LENGTH_CODES];  /* symbols sorted by code length */\r
   /* offsets in sorted table for each length */\r
   int offset[BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1];\r
@@ -117,7 +118,7 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
   BROTLI_DCHECK(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH <=\r
                 BROTLI_REVERSE_BITS_MAX);\r
 \r
-  /* generate offsets into sorted symbol table by code length */\r
+  /* Generate offsets into sorted symbol table by code length. */\r
   symbol = -1;\r
   bits = 1;\r
   BROTLI_REPEAT(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH, {\r
@@ -128,7 +129,7 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
   /* Symbols with code length 0 are placed after all other symbols. */\r
   offset[0] = BROTLI_CODE_LENGTH_CODES - 1;\r
 \r
-  /* sort symbols by length, by symbol order within each length */\r
+  /* Sort symbols by length, by symbol order within each length. */\r
   symbol = BROTLI_CODE_LENGTH_CODES;\r
   do {\r
     BROTLI_REPEAT(6, {\r
@@ -143,13 +144,13 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
   if (offset[0] == 0) {\r
     code.bits = 0;\r
     code.value = (uint16_t)sorted[0];\r
-    for (key = 0; key < (uint32_t)table_size; ++key) {\r
+    for (key = 0; key < (brotli_reg_t)table_size; ++key) {\r
       table[key] = code;\r
     }\r
     return;\r
   }\r
 \r
-  /* fill in table */\r
+  /* Fill in table. */\r
   key = 0;\r
   key_step = BROTLI_REVERSE_BITS_LOWEST;\r
   symbol = 0;\r
@@ -175,10 +176,10 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
   HuffmanCode* table;     /* next available space in table */\r
   int len;                /* current code length */\r
   int symbol;             /* symbol index in original or sorted table */\r
-  uint32_t key;           /* prefix code */\r
-  uint32_t key_step;      /* prefix code addend */\r
-  uint32_t sub_key;       /* 2nd level table prefix code */\r
-  uint32_t sub_key_step;  /* 2nd level table prefix code addend */\r
+  brotli_reg_t key;       /* prefix code */\r
+  brotli_reg_t key_step;  /* prefix code addend */\r
+  brotli_reg_t sub_key;   /* 2nd level table prefix code */\r
+  brotli_reg_t sub_key_step;  /* 2nd level table prefix code addend */\r
   int step;               /* step size to replicate values in current table */\r
   int table_bits;         /* key length of current table */\r
   int table_size;         /* size of current table */\r
@@ -199,9 +200,8 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
   table_size = 1 << table_bits;\r
   total_size = table_size;\r
 \r
-  /* fill in root table */\r
-  /* let's reduce the table size to a smaller size if possible, and */\r
-  /* create the repetitions by memcpy if possible in the coming loop */\r
+  /* Fill in the root table. Reduce the table size to if possible,\r
+     and create the repetitions by memcpy. */\r
   if (table_bits > max_length) {\r
     table_bits = max_length;\r
     table_size = 1 << table_bits;\r
@@ -223,15 +223,14 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
     key_step >>= 1;\r
   } while (++bits <= table_bits);\r
 \r
-  /* if root_bits != table_bits we only created one fraction of the */\r
-  /* table, and we need to replicate it now. */\r
+  /* If root_bits != table_bits then replicate to fill the remaining slots. */\r
   while (total_size != table_size) {\r
     memcpy(&table[table_size], &table[0],\r
            (size_t)table_size * sizeof(table[0]));\r
     table_size <<= 1;\r
   }\r
 \r
-  /* fill in 2nd level tables and add pointers to root table */\r
+  /* Fill in 2nd level tables and add pointers to root table. */\r
   key_step = BROTLI_REVERSE_BITS_LOWEST >> (root_bits - 1);\r
   sub_key = (BROTLI_REVERSE_BITS_LOWEST << 1);\r
   sub_key_step = BROTLI_REVERSE_BITS_LOWEST;\r