brotli 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +1 -1
  3. data/.travis.yml +2 -1
  4. data/README.md +1 -1
  5. data/Rakefile +1 -1
  6. data/ext/brotli/brotli.cc +1 -1
  7. data/ext/brotli/extconf.rb +72 -14
  8. data/lib/brotli/version.rb +1 -1
  9. data/vendor/brotli/LICENSE +19 -202
  10. data/vendor/brotli/dec/Makefile +1 -1
  11. data/vendor/brotli/dec/bit_reader.c +23 -30
  12. data/vendor/brotli/dec/bit_reader.h +270 -141
  13. data/vendor/brotli/dec/context.h +3 -12
  14. data/vendor/brotli/dec/decode.c +1813 -1048
  15. data/vendor/brotli/dec/decode.h +22 -16
  16. data/vendor/brotli/dec/dictionary.c +9466 -0
  17. data/vendor/brotli/dec/dictionary.h +6 -9461
  18. data/vendor/brotli/dec/huffman.c +104 -71
  19. data/vendor/brotli/dec/huffman.h +19 -28
  20. data/vendor/brotli/dec/port.h +124 -32
  21. data/vendor/brotli/dec/prefix.h +4 -13
  22. data/vendor/brotli/dec/state.c +93 -56
  23. data/vendor/brotli/dec/state.h +124 -53
  24. data/vendor/brotli/dec/streams.c +14 -11
  25. data/vendor/brotli/dec/streams.h +6 -11
  26. data/vendor/brotli/dec/transform.h +2 -11
  27. data/vendor/brotli/dec/types.h +21 -19
  28. data/vendor/brotli/enc/Makefile +4 -1
  29. data/vendor/brotli/enc/backward_references.cc +87 -94
  30. data/vendor/brotli/enc/backward_references.h +8 -18
  31. data/vendor/brotli/enc/bit_cost.h +11 -19
  32. data/vendor/brotli/enc/block_splitter.cc +43 -48
  33. data/vendor/brotli/enc/block_splitter.h +7 -16
  34. data/vendor/brotli/enc/brotli_bit_stream.cc +48 -50
  35. data/vendor/brotli/enc/brotli_bit_stream.h +7 -16
  36. data/vendor/brotli/enc/cluster.h +24 -25
  37. data/vendor/brotli/enc/command.h +34 -41
  38. data/vendor/brotli/enc/context.h +11 -18
  39. data/vendor/brotli/enc/dictionary.cc +9466 -0
  40. data/vendor/brotli/enc/dictionary.h +20 -9464
  41. data/vendor/brotli/enc/dictionary_hash.h +7 -15
  42. data/vendor/brotli/enc/encode.cc +80 -148
  43. data/vendor/brotli/enc/encode.h +19 -29
  44. data/vendor/brotli/enc/encode_parallel.cc +35 -108
  45. data/vendor/brotli/enc/encode_parallel.h +7 -16
  46. data/vendor/brotli/enc/entropy_encode.cc +33 -42
  47. data/vendor/brotli/enc/entropy_encode.h +8 -16
  48. data/vendor/brotli/enc/fast_log.h +8 -15
  49. data/vendor/brotli/enc/find_match_length.h +7 -17
  50. data/vendor/brotli/enc/hash.h +130 -150
  51. data/vendor/brotli/enc/histogram.cc +7 -16
  52. data/vendor/brotli/enc/histogram.h +11 -17
  53. data/vendor/brotli/enc/literal_cost.cc +28 -35
  54. data/vendor/brotli/enc/literal_cost.h +9 -23
  55. data/vendor/brotli/enc/metablock.cc +18 -26
  56. data/vendor/brotli/enc/metablock.h +6 -14
  57. data/vendor/brotli/enc/port.h +14 -14
  58. data/vendor/brotli/enc/prefix.h +11 -18
  59. data/vendor/brotli/enc/ringbuffer.h +18 -27
  60. data/vendor/brotli/enc/static_dict.cc +7 -1
  61. data/vendor/brotli/enc/static_dict.h +7 -15
  62. data/vendor/brotli/enc/static_dict_lut.h +7 -15
  63. data/vendor/brotli/enc/streams.cc +15 -28
  64. data/vendor/brotli/enc/streams.h +27 -35
  65. data/vendor/brotli/enc/transform.h +9 -16
  66. data/vendor/brotli/enc/types.h +27 -0
  67. data/vendor/brotli/enc/utf8_util.cc +82 -0
  68. data/vendor/brotli/enc/utf8_util.h +25 -0
  69. data/vendor/brotli/enc/write_bits.h +11 -18
  70. metadata +7 -2
@@ -1,16 +1,7 @@
1
1
  /* Copyright 2013 Google Inc. All Rights Reserved.
2
2
 
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
6
-
7
- http://www.apache.org/licenses/LICENSE-2.0
8
-
9
- Unless required by applicable law or agreed to in writing, software
10
- distributed under the License is distributed on an "AS IS" BASIS,
11
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- See the License for the specific language governing permissions and
13
- limitations under the License.
3
+ Distributed under MIT license.
4
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
14
5
  */
15
6
 
16
7
  /* Utilities for building Huffman decoding tables. */
@@ -25,17 +16,59 @@
25
16
  extern "C" {
26
17
  #endif
27
18
 
28
- /* Returns reverse(reverse(key, len) + 1, len), where reverse(key, len) is the
29
- bit-wise reversal of the len least significant bits of key. */
30
- static BROTLI_INLINE uint32_t GetNextKey(uint32_t key, int len) {
19
+ #define BROTLI_REVERSE_BITS_MAX 8
20
+
31
21
  #ifdef BROTLI_RBIT
32
- return BROTLI_RBIT(BROTLI_RBIT(key) + (1 << (8 * sizeof(unsigned) - len)));
22
+ #define BROTLI_REVERSE_BITS_BASE (32 - BROTLI_REVERSE_BITS_MAX)
33
23
  #else
34
- unsigned step = (unsigned)(1 << (len - 1));
35
- while (key & step) {
36
- step >>= 1;
37
- }
38
- return (key & (step - 1)) + step;
24
+ #define BROTLI_REVERSE_BITS_BASE 0
25
+ static uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = {
26
+ 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
27
+ 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
28
+ 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
29
+ 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
30
+ 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4,
31
+ 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
32
+ 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC,
33
+ 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
34
+ 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,
35
+ 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
36
+ 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA,
37
+ 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
38
+ 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6,
39
+ 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
40
+ 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE,
41
+ 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
42
+ 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1,
43
+ 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
44
+ 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9,
45
+ 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
46
+ 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5,
47
+ 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
48
+ 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED,
49
+ 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
50
+ 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3,
51
+ 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
52
+ 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB,
53
+ 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
54
+ 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7,
55
+ 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
56
+ 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF,
57
+ 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
58
+ };
59
+ #endif /* BROTLI_RBIT */
60
+
61
+ #define BROTLI_REVERSE_BITS_LOWEST \
62
+ (1U << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE))
63
+
64
+ /* Returns reverse(num >> BROTLI_REVERSE_BITS_BASE, BROTLI_REVERSE_BITS_MAX),
65
+ where reverse(value, len) is the bit-wise reversal of the len least
66
+ significant bits of value. */
67
+ static BROTLI_INLINE uint32_t BrotliReverseBits(uint32_t num) {
68
+ #ifdef BROTLI_RBIT
69
+ return BROTLI_RBIT(num);
70
+ #else
71
+ return kReverseBits[num];
39
72
  #endif
40
73
  }
41
74
 
@@ -71,7 +104,8 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
71
104
  uint16_t *count) {
72
105
  HuffmanCode code; /* current table entry */
73
106
  int symbol; /* symbol index in original or sorted table */
74
- unsigned key; /* reversed prefix code */
107
+ uint32_t key; /* prefix code */
108
+ uint32_t key_step; /* prefix code addend */
75
109
  int step; /* step size to replicate values in current table */
76
110
  int table_size; /* size of current table */
77
111
  int sorted[18]; /* symbols sorted by code length */
@@ -79,6 +113,8 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
79
113
  int offset[BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1];
80
114
  int bits;
81
115
  int bits_count;
116
+ BROTLI_DCHECK(
117
+ BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH <= BROTLI_REVERSE_BITS_MAX);
82
118
 
83
119
  /* generate offsets into sorted symbol table by code length */
84
120
  symbol = -1;
@@ -88,6 +124,7 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
88
124
  offset[bits] = symbol;
89
125
  bits++;
90
126
  });
127
+ /* Symbols with code length 0 are placed after all other symbols. */
91
128
  offset[0] = 17;
92
129
 
93
130
  /* sort symbols by length, by symbol order within each length */
@@ -101,11 +138,11 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
101
138
 
102
139
  table_size = 1 << BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH;
103
140
 
104
- /* special case code with only one value */
105
- if (offset[BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH] == 0) {
141
+ /* Special case: all symbols but one have 0 code length. */
142
+ if (offset[0] == 0) {
106
143
  code.bits = 0;
107
144
  code.value = (uint16_t)sorted[0];
108
- for (key = 0; key < table_size; ++key) {
145
+ for (key = 0; key < (uint32_t)table_size; ++key) {
109
146
  table[key] = code;
110
147
  }
111
148
  return;
@@ -113,6 +150,7 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
113
150
 
114
151
  /* fill in table */
115
152
  key = 0;
153
+ key_step = BROTLI_REVERSE_BITS_LOWEST;
116
154
  symbol = 0;
117
155
  bits = 1;
118
156
  step = 2;
@@ -120,25 +158,27 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
120
158
  code.bits = (uint8_t)bits;
121
159
  for (bits_count = count[bits]; bits_count != 0; --bits_count) {
122
160
  code.value = (uint16_t)sorted[symbol++];
123
- ReplicateValue(&table[key], step, table_size, code);
124
- key = GetNextKey(key, bits);
161
+ ReplicateValue(&table[BrotliReverseBits(key)], step, table_size, code);
162
+ key += key_step;
125
163
  }
126
164
  step <<= 1;
165
+ key_step >>= 1;
127
166
  } while (++bits <= BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH);
128
167
  }
129
168
 
130
- int BrotliBuildHuffmanTable(HuffmanCode* root_table,
131
- int root_bits,
132
- const uint16_t* const symbol_lists,
133
- uint16_t *count) {
169
+ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
170
+ int root_bits,
171
+ const uint16_t* const symbol_lists,
172
+ uint16_t *count) {
134
173
  HuffmanCode code; /* current table entry */
135
174
  HuffmanCode* table; /* next available space in table */
136
175
  int len; /* current code length */
137
176
  int symbol; /* symbol index in original or sorted table */
138
- unsigned key; /* reversed prefix code */
177
+ uint32_t key; /* prefix code */
178
+ uint32_t key_step; /* prefix code addend */
179
+ uint32_t sub_key; /* 2nd level table prefix code */
180
+ uint32_t sub_key_step;/* 2nd level table prefix code addend */
139
181
  int step; /* step size to replicate values in current table */
140
- unsigned low; /* low bits for current root entry */
141
- unsigned mask; /* mask for low bits */
142
182
  int table_bits; /* key length of current table */
143
183
  int table_size; /* size of current table */
144
184
  int total_size; /* sum of root table size and 2nd level table sizes */
@@ -146,6 +186,10 @@ int BrotliBuildHuffmanTable(HuffmanCode* root_table,
146
186
  int bits;
147
187
  int bits_count;
148
188
 
189
+ BROTLI_DCHECK(root_bits <= BROTLI_REVERSE_BITS_MAX);
190
+ BROTLI_DCHECK(
191
+ BROTLI_HUFFMAN_MAX_CODE_LENGTH - root_bits <= BROTLI_REVERSE_BITS_MAX);
192
+
149
193
  while (symbol_lists[max_length] == 0xFFFF) max_length--;
150
194
  max_length += BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1;
151
195
 
@@ -162,6 +206,7 @@ int BrotliBuildHuffmanTable(HuffmanCode* root_table,
162
206
  table_size = 1 << table_bits;
163
207
  }
164
208
  key = 0;
209
+ key_step = BROTLI_REVERSE_BITS_LOWEST;
165
210
  bits = 1;
166
211
  step = 2;
167
212
  do {
@@ -170,10 +215,11 @@ int BrotliBuildHuffmanTable(HuffmanCode* root_table,
170
215
  for (bits_count = count[bits]; bits_count != 0; --bits_count) {
171
216
  symbol = symbol_lists[symbol];
172
217
  code.value = (uint16_t)symbol;
173
- ReplicateValue(&table[key], step, table_size, code);
174
- key = GetNextKey(key, bits);
218
+ ReplicateValue(&table[BrotliReverseBits(key)], step, table_size, code);
219
+ key += key_step;
175
220
  }
176
221
  step <<= 1;
222
+ key_step >>= 1;
177
223
  } while (++bits <= table_bits);
178
224
 
179
225
  /* if root_bits != table_bits we only created one fraction of the */
@@ -185,37 +231,43 @@ int BrotliBuildHuffmanTable(HuffmanCode* root_table,
185
231
  }
186
232
 
187
233
  /* fill in 2nd level tables and add pointers to root table */
188
- mask = (unsigned)(total_size - 1);
189
- low = (unsigned)-1;
190
- for (len = root_bits + 1, step = 2; len <= max_length; ++len, step <<= 1) {
234
+ key_step = BROTLI_REVERSE_BITS_LOWEST >> (root_bits - 1);
235
+ sub_key = (BROTLI_REVERSE_BITS_LOWEST << 1);
236
+ sub_key_step = BROTLI_REVERSE_BITS_LOWEST;
237
+ for (len = root_bits + 1, step = 2; len <= max_length; ++len) {
191
238
  symbol = len - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
192
239
  for (; count[len] != 0; --count[len]) {
193
- if ((key & mask) != low) {
240
+ if (sub_key == (uint32_t)(BROTLI_REVERSE_BITS_LOWEST << 1)) {
194
241
  table += table_size;
195
242
  table_bits = NextTableBitSize(count, len, root_bits);
196
243
  table_size = 1 << table_bits;
197
244
  total_size += table_size;
198
- low = key & mask;
199
- root_table[low].bits = (uint8_t)(table_bits + root_bits);
200
- root_table[low].value = (uint16_t)(
201
- ((size_t)(table - root_table)) - low);
245
+ sub_key = BrotliReverseBits(key);
246
+ key += key_step;
247
+ root_table[sub_key].bits = (uint8_t)(table_bits + root_bits);
248
+ root_table[sub_key].value = (uint16_t)(
249
+ ((size_t)(table - root_table)) - sub_key);
250
+ sub_key = 0;
202
251
  }
203
252
  code.bits = (uint8_t)(len - root_bits);
204
253
  symbol = symbol_lists[symbol];
205
254
  code.value = (uint16_t)symbol;
206
- ReplicateValue(&table[key >> root_bits], step, table_size, code);
207
- key = GetNextKey(key, len);
255
+ ReplicateValue(
256
+ &table[BrotliReverseBits(sub_key)], step, table_size, code);
257
+ sub_key += sub_key_step;
208
258
  }
259
+ step <<= 1;
260
+ sub_key_step >>= 1;
209
261
  }
210
- return total_size;
262
+ return (uint32_t)total_size;
211
263
  }
212
264
 
213
- int BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
214
- int root_bits,
215
- uint16_t *val,
216
- uint32_t num_symbols) {
217
- int table_size = 1;
218
- const int goal_size = 1 << root_bits;
265
+ uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
266
+ int root_bits,
267
+ uint16_t *val,
268
+ uint32_t num_symbols) {
269
+ uint32_t table_size = 1;
270
+ const uint32_t goal_size = 1U << root_bits;
219
271
  switch (num_symbols) {
220
272
  case 0:
221
273
  table[0].bits = 0;
@@ -301,25 +353,6 @@ int BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
301
353
  return goal_size;
302
354
  }
303
355
 
304
- void BrotliHuffmanTreeGroupInit(HuffmanTreeGroup* group, int alphabet_size,
305
- int ntrees) {
306
- /* Pack two mallocs into one */
307
- const size_t code_size =
308
- sizeof(HuffmanCode) * (size_t)(ntrees * BROTLI_HUFFMAN_MAX_TABLE_SIZE);
309
- const size_t htree_size = sizeof(HuffmanCode*) * (size_t)ntrees;
310
- char *p = (char*)malloc(code_size + htree_size);
311
- group->alphabet_size = (int16_t)alphabet_size;
312
- group->num_htrees = (int16_t)ntrees;
313
- group->codes = (HuffmanCode*)p;
314
- group->htrees = (HuffmanCode**)(p + code_size);
315
- }
316
-
317
- void BrotliHuffmanTreeGroupRelease(HuffmanTreeGroup* group) {
318
- if (group->codes) {
319
- free(group->codes);
320
- }
321
- }
322
-
323
356
  #if defined(__cplusplus) || defined(c_plusplus)
324
357
  } /* extern "C" */
325
358
  #endif
@@ -1,16 +1,7 @@
1
1
  /* Copyright 2013 Google Inc. All Rights Reserved.
2
2
 
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
6
-
7
- http://www.apache.org/licenses/LICENSE-2.0
8
-
9
- Unless required by applicable law or agreed to in writing, software
10
- distributed under the License is distributed on an "AS IS" BASIS,
11
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- See the License for the specific language governing permissions and
13
- limitations under the License.
3
+ Distributed under MIT license.
4
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
14
5
  */
15
6
 
16
7
  /* Utilities for building Huffman decoding tables. */
@@ -48,28 +39,28 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table,
48
39
 
49
40
  /* Builds Huffman lookup table assuming code lengths are in symbol order. */
50
41
  /* Returns size of resulting table. */
51
- int BrotliBuildHuffmanTable(HuffmanCode* root_table,
52
- int root_bits,
53
- const uint16_t* const symbol_lists,
54
- uint16_t *count_arg);
55
-
56
- int BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
57
- int root_bits,
58
- uint16_t *symbols,
59
- uint32_t num_symbols);
60
-
61
- /* Contains a collection of huffman trees with the same alphabet size. */
42
+ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
43
+ int root_bits,
44
+ const uint16_t* const symbol_lists,
45
+ uint16_t *count_arg);
46
+
47
+ /* Builds a simple Huffman table. The num_symbols parameter is to be */
48
+ /* interpreted as follows: 0 means 1 symbol, 1 means 2 symbols, 2 means 3 */
49
+ /* symbols, 3 means 4 symbols with lengths 2,2,2,2, 4 means 4 symbols with */
50
+ /* lengths 1,2,3,3. */
51
+ uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
52
+ int root_bits,
53
+ uint16_t *symbols,
54
+ uint32_t num_symbols);
55
+
56
+ /* Contains a collection of Huffman trees with the same alphabet size. */
62
57
  typedef struct {
63
58
  HuffmanCode** htrees;
64
59
  HuffmanCode* codes;
65
- int16_t alphabet_size;
66
- int16_t num_htrees;
60
+ uint16_t alphabet_size;
61
+ uint16_t num_htrees;
67
62
  } HuffmanTreeGroup;
68
63
 
69
- void BrotliHuffmanTreeGroupInit(HuffmanTreeGroup* group,
70
- int alphabet_size, int ntrees);
71
- void BrotliHuffmanTreeGroupRelease(HuffmanTreeGroup* group);
72
-
73
64
  #if defined(__cplusplus) || defined(c_plusplus)
74
65
  } /* extern "C" */
75
66
  #endif
@@ -1,19 +1,25 @@
1
1
  /* Copyright 2015 Google Inc. All Rights Reserved.
2
2
 
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
6
-
7
- http://www.apache.org/licenses/LICENSE-2.0
8
-
9
- Unless required by applicable law or agreed to in writing, software
10
- distributed under the License is distributed on an "AS IS" BASIS,
11
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- See the License for the specific language governing permissions and
13
- limitations under the License.
3
+ Distributed under MIT license.
4
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
14
5
  */
15
6
 
16
- /* Macros for branch prediction. */
7
+ /* Macros for compiler / platform specific features and build options.
8
+
9
+ Build options are:
10
+ * BROTLI_BUILD_32_BIT disables 64-bit optimizations
11
+ * BROTLI_BUILD_64_BIT forces to use 64-bit optimizations
12
+ * BROTLI_BUILD_BIG_ENDIAN forces to use big-endian optimizations
13
+ * BROTLI_BUILD_ENDIAN_NEUTRAL disables endian-aware optimizations
14
+ * BROTLI_BUILD_LITTLE_ENDIAN forces to use little-endian optimizations
15
+ * BROTLI_BUILD_MODERN_COMPILER forces to use modern compilers built-ins,
16
+ features and attributes
17
+ * BROTLI_BUILD_PORTABLE disables dangerous optimizations, like unaligned
18
+ read and overlapping memcpy; this reduces decompression speed by 5%
19
+ * BROTLI_DEBUG dumps file name and line number when decoder detects stream
20
+ or memory error
21
+ * BROTLI_DECODE_DEBUG enables asserts and dumps various state information
22
+ */
17
23
 
18
24
  #ifndef BROTLI_DEC_PORT_H_
19
25
  #define BROTLI_DEC_PORT_H_
@@ -33,6 +39,67 @@
33
39
  #define __has_feature(x) 0
34
40
  #endif
35
41
 
42
+ #if defined(__sparc)
43
+ #define BROTLI_TARGET_SPARC
44
+ #endif
45
+
46
+ #if defined(__arm__) || defined(__thumb__) || \
47
+ defined(_M_ARM) || defined(_M_ARMT)
48
+ #define BROTLI_TARGET_ARM
49
+ #if (defined(__ARM_ARCH) && (__ARM_ARCH >= 7)) || \
50
+ (defined(M_ARM) && (M_ARM >= 7))
51
+ #define BROTLI_TARGET_ARMV7
52
+ #endif /* ARMv7 */
53
+ #if defined(__aarch64__)
54
+ #define BROTLI_TARGET_ARMV8
55
+ #endif /* ARMv8 */
56
+ #endif /* ARM */
57
+
58
+ #if defined(__x86_64__) || defined(_M_X64)
59
+ #define BROTLI_TARGET_X64
60
+ #endif
61
+
62
+ #if defined(__PPC64__)
63
+ #define BROTLI_TARGET_POWERPC64
64
+ #endif
65
+
66
+ #if defined(__GNUC__) && defined(__GNUC_MINOR__)
67
+ #define BROTLI_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
68
+ #else
69
+ #define BROTLI_GCC_VERSION 0
70
+ #endif
71
+
72
+ #if defined(__ICC)
73
+ #define BROTLI_ICC_VERSION __ICC
74
+ #else
75
+ #define BROTLI_ICC_VERSION 0
76
+ #endif
77
+
78
+ #if defined(BROTLI_BUILD_MODERN_COMPILER)
79
+ #define BROTLI_MODERN_COMPILER 1
80
+ #elif (BROTLI_GCC_VERSION > 300) || (BROTLI_ICC_VERSION >= 1600)
81
+ #define BROTLI_MODERN_COMPILER 1
82
+ #else
83
+ #define BROTLI_MODERN_COMPILER 0
84
+ #endif
85
+
86
+ /* SPARC and ARMv6 don't support unaligned read.
87
+ Choose portable build for them. */
88
+ #if !defined(BROTLI_BUILD_PORTABLE)
89
+ #if defined(BROTLI_TARGET_SPARC) || \
90
+ (defined(BROTLI_TARGET_ARM) && !defined(BROTLI_TARGET_ARMV7))
91
+ #define BROTLI_BUILD_PORTABLE
92
+ #endif /* SPARK or ARMv6 */
93
+ #endif /* portable build */
94
+
95
+ #ifdef BROTLI_BUILD_PORTABLE
96
+ #define BROTLI_ALIGNED_READ 1
97
+ #define BROTLI_SAFE_MEMMOVE 1
98
+ #else
99
+ #define BROTLI_ALIGNED_READ 0
100
+ #define BROTLI_SAFE_MEMMOVE 0
101
+ #endif
102
+
36
103
  #define BROTLI_ASAN_BUILD __has_feature(address_sanitizer)
37
104
 
38
105
  /* Define "PREDICT_TRUE" and "PREDICT_FALSE" macros for capable compilers.
@@ -52,8 +119,7 @@ OR:
52
119
  }
53
120
 
54
121
  */
55
- #if (__GNUC__ > 2) || (__GNUC__ == 2 && __GNUC_MINOR__ > 95) || \
56
- (defined(__llvm__) && __has_builtin(__builtin_expect))
122
+ #if BROTLI_MODERN_COMPILER || __has_builtin(__builtin_expect)
57
123
  #define PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
58
124
  #define PREDICT_FALSE(x) (__builtin_expect(x, 0))
59
125
  #else
@@ -62,15 +128,13 @@ OR:
62
128
  #endif
63
129
 
64
130
  /* IS_CONSTANT macros returns true for compile-time constant expressions. */
65
- #if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ > 0) || \
66
- (defined(__llvm__) && __has_builtin(__builtin_constant_p))
131
+ #if BROTLI_MODERN_COMPILER || __has_builtin(__builtin_constant_p)
67
132
  #define IS_CONSTANT(x) __builtin_constant_p(x)
68
133
  #else
69
134
  #define IS_CONSTANT(x) 0
70
135
  #endif
71
136
 
72
- #if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ > 0) || \
73
- (defined(__llvm__) && __has_attribute(always_inline))
137
+ #if BROTLI_MODERN_COMPILER || __has_attribute(always_inline)
74
138
  #define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((always_inline))
75
139
  #else
76
140
  #define ATTRIBUTE_ALWAYS_INLINE
@@ -93,36 +157,49 @@ OR:
93
157
  #define BROTLI_DCHECK(x)
94
158
  #endif
95
159
 
96
- #if (defined(__x86_64__) || defined(_M_X64) || defined(__aarch64__) || \
97
- defined(__PPC64__))
160
+ #if defined(BROTLI_BUILD_64_BIT)
161
+ #define BROTLI_64_BITS 1
162
+ #elif defined(BROTLI_BUILD_32_BIT)
163
+ #define BROTLI_64_BITS 0
164
+ #elif defined(BROTLI_TARGET_X64) || defined(BROTLI_TARGET_ARMV8) || \
165
+ defined(BROTLI_TARGET_POWERPC64)
98
166
  #define BROTLI_64_BITS 1
99
167
  #else
100
168
  #define BROTLI_64_BITS 0
101
169
  #endif
102
170
 
103
- #if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
171
+ #if defined(BROTLI_BUILD_BIG_ENDIAN)
172
+ #define BROTLI_LITTLE_ENDIAN 0
173
+ #define BROTLI_BIG_ENDIAN 1
174
+ #elif defined(BROTLI_BUILD_LITTLE_ENDIAN)
104
175
  #define BROTLI_LITTLE_ENDIAN 1
176
+ #define BROTLI_BIG_ENDIAN 0
177
+ #elif defined(BROTLI_BUILD_ENDIAN_NEUTRAL)
178
+ #define BROTLI_LITTLE_ENDIAN 0
179
+ #define BROTLI_BIG_ENDIAN 0
180
+ #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
181
+ #define BROTLI_LITTLE_ENDIAN 1
182
+ #define BROTLI_BIG_ENDIAN 0
105
183
  #elif defined(_WIN32)
106
184
  /* Win32 can currently always be assumed to be little endian */
107
185
  #define BROTLI_LITTLE_ENDIAN 1
186
+ #define BROTLI_BIG_ENDIAN 0
108
187
  #else
109
- #define BROTLI_LITTLE_ENDIAN 0
110
- #endif
111
-
112
- #if (BROTLI_64_BITS && BROTLI_LITTLE_ENDIAN)
113
- #define BROTLI_64_BITS_LITTLE_ENDIAN 1
188
+ #if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
189
+ #define BROTLI_BIG_ENDIAN 1
114
190
  #else
115
- #define BROTLI_64_BITS_LITTLE_ENDIAN 0
191
+ #define BROTLI_BIG_ENDIAN 0
192
+ #endif
193
+ #define BROTLI_LITTLE_ENDIAN 0
116
194
  #endif
117
195
 
118
- #if (__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || \
119
- (defined(__llvm__) && __has_attribute(noinline))
196
+ #if BROTLI_MODERN_COMPILER || __has_attribute(noinline)
120
197
  #define BROTLI_NOINLINE __attribute__ ((noinline))
121
198
  #else
122
199
  #define BROTLI_NOINLINE
123
200
  #endif
124
201
 
125
- #if BROTLI_ASAN_BUILD
202
+ #if BROTLI_ASAN_BUILD && !defined(BROTLI_BUILD_PORTABLE)
126
203
  #define BROTLI_NO_ASAN __attribute__((no_sanitize("address"))) BROTLI_NOINLINE
127
204
  #else
128
205
  #define BROTLI_NO_ASAN
@@ -134,8 +211,8 @@ OR:
134
211
  if ((N & 4) != 0) {X; X; X; X;} \
135
212
  }
136
213
 
137
- #if (__GNUC__ > 2) || defined(__llvm__)
138
- #if (defined(__ARM_ARCH) && (__ARM_ARCH >= 7))
214
+ #if BROTLI_MODERN_COMPILER || defined(__llvm__)
215
+ #if defined(BROTLI_TARGET_ARMV7)
139
216
  static BROTLI_INLINE unsigned BrotliRBit(unsigned input) {
140
217
  unsigned output;
141
218
  __asm__("rbit %0, %1\n" : "=r"(output) : "r"(input));
@@ -145,4 +222,19 @@ static BROTLI_INLINE unsigned BrotliRBit(unsigned input) {
145
222
  #endif /* armv7 */
146
223
  #endif /* gcc || clang */
147
224
 
225
+ #if defined(BROTLI_TARGET_ARM)
226
+ #define BROTLI_HAS_UBFX 1
227
+ #else
228
+ #define BROTLI_HAS_UBFX 0
229
+ #endif
230
+
231
+ #define BROTLI_ALLOC(S, L) S->alloc_func(S->memory_manager_opaque, L)
232
+
233
+ #define BROTLI_FREE(S, X) { \
234
+ S->free_func(S->memory_manager_opaque, X); \
235
+ X = NULL; \
236
+ }
237
+
238
+ #define BROTLI_UNUSED(X) (void)(X)
239
+
148
240
  #endif /* BROTLI_DEC_PORT_H_ */
@@ -1,16 +1,7 @@
1
1
  /* Copyright 2013 Google Inc. All Rights Reserved.
2
2
 
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
6
-
7
- http://www.apache.org/licenses/LICENSE-2.0
8
-
9
- Unless required by applicable law or agreed to in writing, software
10
- distributed under the License is distributed on an "AS IS" BASIS,
11
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- See the License for the specific language governing permissions and
13
- limitations under the License.
3
+ Distributed under MIT license.
4
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
14
5
  */
15
6
 
16
7
  /* Lookup tables to map prefix codes to value ranges. This is used during
@@ -23,8 +14,8 @@
23
14
  /* Represents the range of values belonging to a prefix code: */
24
15
  /* [offset, offset + 2^nbits) */
25
16
  struct PrefixCodeRange {
26
- int16_t offset;
27
- int8_t nbits;
17
+ uint16_t offset;
18
+ uint8_t nbits;
28
19
  };
29
20
 
30
21
  static const struct PrefixCodeRange kBlockLengthPrefixCode[] = {