brotli 0.2.0 → 0.4.0
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.
- checksums.yaml +5 -5
- data/.github/workflows/main.yml +34 -0
- data/.github/workflows/publish.yml +34 -0
- data/Gemfile +6 -2
- data/Rakefile +18 -6
- data/bin/before_install.sh +9 -0
- data/brotli.gemspec +7 -13
- data/ext/brotli/brotli.c +209 -11
- data/ext/brotli/buffer.c +1 -7
- data/ext/brotli/buffer.h +1 -1
- data/ext/brotli/extconf.rb +45 -26
- data/lib/brotli/version.rb +1 -1
- data/smoke.sh +1 -1
- data/test/brotli_test.rb +104 -0
- data/test/brotli_writer_test.rb +36 -0
- data/test/test_helper.rb +8 -0
- data/vendor/brotli/c/common/constants.c +15 -0
- data/vendor/brotli/c/common/constants.h +149 -6
- data/vendor/brotli/c/{dec/context.h → common/context.c} +91 -186
- data/vendor/brotli/c/common/context.h +113 -0
- data/vendor/brotli/c/common/dictionary.bin +0 -0
- data/vendor/brotli/c/common/dictionary.bin.br +0 -0
- data/vendor/brotli/c/common/dictionary.c +11 -2
- data/vendor/brotli/c/common/dictionary.h +4 -4
- data/vendor/brotli/c/common/platform.c +22 -0
- data/vendor/brotli/c/common/platform.h +594 -0
- data/vendor/brotli/c/common/transform.c +291 -0
- data/vendor/brotli/c/common/transform.h +85 -0
- data/vendor/brotli/c/common/version.h +8 -1
- data/vendor/brotli/c/dec/bit_reader.c +29 -1
- data/vendor/brotli/c/dec/bit_reader.h +91 -100
- data/vendor/brotli/c/dec/decode.c +665 -437
- data/vendor/brotli/c/dec/huffman.c +65 -84
- data/vendor/brotli/c/dec/huffman.h +67 -14
- data/vendor/brotli/c/dec/prefix.h +1 -20
- data/vendor/brotli/c/dec/state.c +32 -45
- data/vendor/brotli/c/dec/state.h +173 -55
- data/vendor/brotli/c/enc/backward_references.c +27 -16
- data/vendor/brotli/c/enc/backward_references.h +7 -7
- data/vendor/brotli/c/enc/backward_references_hq.c +155 -116
- data/vendor/brotli/c/enc/backward_references_hq.h +22 -23
- data/vendor/brotli/c/enc/backward_references_inc.h +32 -22
- data/vendor/brotli/c/enc/bit_cost.c +1 -1
- data/vendor/brotli/c/enc/bit_cost.h +5 -5
- data/vendor/brotli/c/enc/block_encoder_inc.h +7 -6
- data/vendor/brotli/c/enc/block_splitter.c +5 -6
- data/vendor/brotli/c/enc/block_splitter.h +1 -1
- data/vendor/brotli/c/enc/block_splitter_inc.h +26 -17
- data/vendor/brotli/c/enc/brotli_bit_stream.c +107 -123
- data/vendor/brotli/c/enc/brotli_bit_stream.h +19 -38
- data/vendor/brotli/c/enc/cluster.c +1 -1
- data/vendor/brotli/c/enc/cluster.h +1 -1
- data/vendor/brotli/c/enc/cluster_inc.h +6 -3
- data/vendor/brotli/c/enc/command.c +28 -0
- data/vendor/brotli/c/enc/command.h +52 -42
- data/vendor/brotli/c/enc/compress_fragment.c +21 -22
- data/vendor/brotli/c/enc/compress_fragment.h +1 -1
- data/vendor/brotli/c/enc/compress_fragment_two_pass.c +102 -69
- data/vendor/brotli/c/enc/compress_fragment_two_pass.h +1 -1
- data/vendor/brotli/c/enc/dictionary_hash.c +1827 -1101
- data/vendor/brotli/c/enc/dictionary_hash.h +2 -1
- data/vendor/brotli/c/enc/encode.c +358 -195
- data/vendor/brotli/c/enc/encoder_dict.c +33 -0
- data/vendor/brotli/c/enc/encoder_dict.h +43 -0
- data/vendor/brotli/c/enc/entropy_encode.c +16 -14
- data/vendor/brotli/c/enc/entropy_encode.h +7 -7
- data/vendor/brotli/c/enc/entropy_encode_static.h +3 -3
- data/vendor/brotli/c/enc/fast_log.c +105 -0
- data/vendor/brotli/c/enc/fast_log.h +20 -99
- data/vendor/brotli/c/enc/find_match_length.h +5 -6
- data/vendor/brotli/c/enc/hash.h +145 -103
- data/vendor/brotli/c/enc/hash_composite_inc.h +125 -0
- data/vendor/brotli/c/enc/hash_forgetful_chain_inc.h +93 -53
- data/vendor/brotli/c/enc/hash_longest_match64_inc.h +54 -53
- data/vendor/brotli/c/enc/hash_longest_match_inc.h +58 -54
- data/vendor/brotli/c/enc/hash_longest_match_quickly_inc.h +95 -63
- data/vendor/brotli/c/enc/hash_rolling_inc.h +212 -0
- data/vendor/brotli/c/enc/hash_to_binary_tree_inc.h +46 -43
- data/vendor/brotli/c/enc/histogram.c +9 -6
- data/vendor/brotli/c/enc/histogram.h +6 -3
- data/vendor/brotli/c/enc/histogram_inc.h +1 -1
- data/vendor/brotli/c/enc/literal_cost.c +5 -5
- data/vendor/brotli/c/enc/literal_cost.h +2 -2
- data/vendor/brotli/c/enc/memory.c +5 -16
- data/vendor/brotli/c/enc/memory.h +52 -1
- data/vendor/brotli/c/enc/metablock.c +171 -36
- data/vendor/brotli/c/enc/metablock.h +13 -8
- data/vendor/brotli/c/enc/metablock_inc.h +2 -2
- data/vendor/brotli/c/enc/params.h +46 -0
- data/vendor/brotli/c/enc/prefix.h +3 -4
- data/vendor/brotli/c/enc/quality.h +29 -24
- data/vendor/brotli/c/enc/ringbuffer.h +19 -12
- data/vendor/brotli/c/enc/static_dict.c +49 -45
- data/vendor/brotli/c/enc/static_dict.h +4 -3
- data/vendor/brotli/c/enc/static_dict_lut.h +1 -1
- data/vendor/brotli/c/enc/utf8_util.c +21 -21
- data/vendor/brotli/c/enc/utf8_util.h +1 -1
- data/vendor/brotli/c/enc/write_bits.h +35 -38
- data/vendor/brotli/c/include/brotli/decode.h +13 -8
- data/vendor/brotli/c/include/brotli/encode.h +54 -8
- data/vendor/brotli/c/include/brotli/port.h +225 -83
- data/vendor/brotli/c/include/brotli/types.h +0 -7
- metadata +28 -87
- data/.travis.yml +0 -30
- data/spec/brotli_spec.rb +0 -88
- data/spec/inflate_spec.rb +0 -75
- data/spec/spec_helper.rb +0 -4
- data/vendor/brotli/c/dec/port.h +0 -168
- data/vendor/brotli/c/dec/transform.h +0 -300
- data/vendor/brotli/c/enc/context.h +0 -184
- data/vendor/brotli/c/enc/port.h +0 -184
@@ -11,8 +11,8 @@
|
|
11
11
|
#include <string.h> /* memcpy, memset */
|
12
12
|
|
13
13
|
#include "../common/constants.h"
|
14
|
+
#include "../common/platform.h"
|
14
15
|
#include <brotli/types.h>
|
15
|
-
#include "./port.h"
|
16
16
|
|
17
17
|
#if defined(__cplusplus) || defined(c_plusplus)
|
18
18
|
extern "C" {
|
@@ -20,9 +20,9 @@ extern "C" {
|
|
20
20
|
|
21
21
|
#define BROTLI_REVERSE_BITS_MAX 8
|
22
22
|
|
23
|
-
#
|
23
|
+
#if defined(BROTLI_RBIT)
|
24
24
|
#define BROTLI_REVERSE_BITS_BASE \
|
25
|
-
((sizeof(
|
25
|
+
((sizeof(brotli_reg_t) << 3) - BROTLI_REVERSE_BITS_MAX)
|
26
26
|
#else
|
27
27
|
#define BROTLI_REVERSE_BITS_BASE 0
|
28
28
|
static uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = {
|
@@ -62,13 +62,13 @@ static uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = {
|
|
62
62
|
#endif /* BROTLI_RBIT */
|
63
63
|
|
64
64
|
#define BROTLI_REVERSE_BITS_LOWEST \
|
65
|
-
((
|
65
|
+
((brotli_reg_t)1 << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE))
|
66
66
|
|
67
67
|
/* Returns reverse(num >> BROTLI_REVERSE_BITS_BASE, BROTLI_REVERSE_BITS_MAX),
|
68
68
|
where reverse(value, len) is the bit-wise reversal of the len least
|
69
69
|
significant bits of value. */
|
70
|
-
static BROTLI_INLINE
|
71
|
-
#
|
70
|
+
static BROTLI_INLINE brotli_reg_t BrotliReverseBits(brotli_reg_t num) {
|
71
|
+
#if defined(BROTLI_RBIT)
|
72
72
|
return BROTLI_RBIT(num);
|
73
73
|
#else
|
74
74
|
return kReverseBits[num];
|
@@ -86,9 +86,9 @@ static BROTLI_INLINE void ReplicateValue(HuffmanCode* table,
|
|
86
86
|
} while (end > 0);
|
87
87
|
}
|
88
88
|
|
89
|
-
/* Returns the table width of the next 2nd level table. count is the histogram
|
90
|
-
of bit lengths for the remaining symbols, len is the code length of the
|
91
|
-
processed symbol */
|
89
|
+
/* Returns the table width of the next 2nd level table. |count| is the histogram
|
90
|
+
of bit lengths for the remaining symbols, |len| is the code length of the
|
91
|
+
next processed symbol. */
|
92
92
|
static BROTLI_INLINE int NextTableBitSize(const uint16_t* const count,
|
93
93
|
int len, int root_bits) {
|
94
94
|
int left = 1 << (len - root_bits);
|
@@ -104,12 +104,12 @@ static BROTLI_INLINE int NextTableBitSize(const uint16_t* const count,
|
|
104
104
|
void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
|
105
105
|
const uint8_t* const code_lengths,
|
106
106
|
uint16_t* count) {
|
107
|
-
HuffmanCode code;
|
108
|
-
int symbol;
|
109
|
-
|
110
|
-
|
111
|
-
int step;
|
112
|
-
int table_size;
|
107
|
+
HuffmanCode code; /* current table entry */
|
108
|
+
int symbol; /* symbol index in original or sorted table */
|
109
|
+
brotli_reg_t key; /* prefix code */
|
110
|
+
brotli_reg_t key_step; /* prefix code addend */
|
111
|
+
int step; /* step size to replicate values in current table */
|
112
|
+
int table_size; /* size of current table */
|
113
113
|
int sorted[BROTLI_CODE_LENGTH_CODES]; /* symbols sorted by code length */
|
114
114
|
/* offsets in sorted table for each length */
|
115
115
|
int offset[BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1];
|
@@ -118,7 +118,7 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
|
|
118
118
|
BROTLI_DCHECK(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH <=
|
119
119
|
BROTLI_REVERSE_BITS_MAX);
|
120
120
|
|
121
|
-
/*
|
121
|
+
/* Generate offsets into sorted symbol table by code length. */
|
122
122
|
symbol = -1;
|
123
123
|
bits = 1;
|
124
124
|
BROTLI_REPEAT(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH, {
|
@@ -129,7 +129,7 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
|
|
129
129
|
/* Symbols with code length 0 are placed after all other symbols. */
|
130
130
|
offset[0] = BROTLI_CODE_LENGTH_CODES - 1;
|
131
131
|
|
132
|
-
/*
|
132
|
+
/* Sort symbols by length, by symbol order within each length. */
|
133
133
|
symbol = BROTLI_CODE_LENGTH_CODES;
|
134
134
|
do {
|
135
135
|
BROTLI_REPEAT(6, {
|
@@ -142,24 +142,22 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
|
|
142
142
|
|
143
143
|
/* Special case: all symbols but one have 0 code length. */
|
144
144
|
if (offset[0] == 0) {
|
145
|
-
code
|
146
|
-
|
147
|
-
for (key = 0; key < (reg_t)table_size; ++key) {
|
145
|
+
code = ConstructHuffmanCode(0, (uint16_t)sorted[0]);
|
146
|
+
for (key = 0; key < (brotli_reg_t)table_size; ++key) {
|
148
147
|
table[key] = code;
|
149
148
|
}
|
150
149
|
return;
|
151
150
|
}
|
152
151
|
|
153
|
-
/*
|
152
|
+
/* Fill in table. */
|
154
153
|
key = 0;
|
155
154
|
key_step = BROTLI_REVERSE_BITS_LOWEST;
|
156
155
|
symbol = 0;
|
157
156
|
bits = 1;
|
158
157
|
step = 2;
|
159
158
|
do {
|
160
|
-
code.bits = (uint8_t)bits;
|
161
159
|
for (bits_count = count[bits]; bits_count != 0; --bits_count) {
|
162
|
-
code
|
160
|
+
code = ConstructHuffmanCode((uint8_t)bits, (uint16_t)sorted[symbol++]);
|
163
161
|
ReplicateValue(&table[BrotliReverseBits(key)], step, table_size, code);
|
164
162
|
key += key_step;
|
165
163
|
}
|
@@ -172,18 +170,18 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
|
172
170
|
int root_bits,
|
173
171
|
const uint16_t* const symbol_lists,
|
174
172
|
uint16_t* count) {
|
175
|
-
HuffmanCode code;
|
176
|
-
HuffmanCode* table;
|
177
|
-
int len;
|
178
|
-
int symbol;
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
int step;
|
184
|
-
int table_bits;
|
185
|
-
int table_size;
|
186
|
-
int total_size;
|
173
|
+
HuffmanCode code; /* current table entry */
|
174
|
+
HuffmanCode* table; /* next available space in table */
|
175
|
+
int len; /* current code length */
|
176
|
+
int symbol; /* symbol index in original or sorted table */
|
177
|
+
brotli_reg_t key; /* prefix code */
|
178
|
+
brotli_reg_t key_step; /* prefix code addend */
|
179
|
+
brotli_reg_t sub_key; /* 2nd level table prefix code */
|
180
|
+
brotli_reg_t sub_key_step; /* 2nd level table prefix code addend */
|
181
|
+
int step; /* step size to replicate values in current table */
|
182
|
+
int table_bits; /* key length of current table */
|
183
|
+
int table_size; /* size of current table */
|
184
|
+
int total_size; /* sum of root table size and 2nd level table sizes */
|
187
185
|
int max_length = -1;
|
188
186
|
int bits;
|
189
187
|
int bits_count;
|
@@ -200,9 +198,8 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
|
200
198
|
table_size = 1 << table_bits;
|
201
199
|
total_size = table_size;
|
202
200
|
|
203
|
-
/*
|
204
|
-
|
205
|
-
/* create the repetitions by memcpy if possible in the coming loop */
|
201
|
+
/* Fill in the root table. Reduce the table size to if possible,
|
202
|
+
and create the repetitions by memcpy. */
|
206
203
|
if (table_bits > max_length) {
|
207
204
|
table_bits = max_length;
|
208
205
|
table_size = 1 << table_bits;
|
@@ -212,11 +209,10 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
|
212
209
|
bits = 1;
|
213
210
|
step = 2;
|
214
211
|
do {
|
215
|
-
code.bits = (uint8_t)bits;
|
216
212
|
symbol = bits - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
|
217
213
|
for (bits_count = count[bits]; bits_count != 0; --bits_count) {
|
218
214
|
symbol = symbol_lists[symbol];
|
219
|
-
code
|
215
|
+
code = ConstructHuffmanCode((uint8_t)bits, (uint16_t)symbol);
|
220
216
|
ReplicateValue(&table[BrotliReverseBits(key)], step, table_size, code);
|
221
217
|
key += key_step;
|
222
218
|
}
|
@@ -224,15 +220,14 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
|
224
220
|
key_step >>= 1;
|
225
221
|
} while (++bits <= table_bits);
|
226
222
|
|
227
|
-
/*
|
228
|
-
/* table, and we need to replicate it now. */
|
223
|
+
/* If root_bits != table_bits then replicate to fill the remaining slots. */
|
229
224
|
while (total_size != table_size) {
|
230
225
|
memcpy(&table[table_size], &table[0],
|
231
226
|
(size_t)table_size * sizeof(table[0]));
|
232
227
|
table_size <<= 1;
|
233
228
|
}
|
234
229
|
|
235
|
-
/*
|
230
|
+
/* Fill in 2nd level tables and add pointers to root table. */
|
236
231
|
key_step = BROTLI_REVERSE_BITS_LOWEST >> (root_bits - 1);
|
237
232
|
sub_key = (BROTLI_REVERSE_BITS_LOWEST << 1);
|
238
233
|
sub_key_step = BROTLI_REVERSE_BITS_LOWEST;
|
@@ -246,14 +241,13 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
|
246
241
|
total_size += table_size;
|
247
242
|
sub_key = BrotliReverseBits(key);
|
248
243
|
key += key_step;
|
249
|
-
root_table[sub_key]
|
250
|
-
|
251
|
-
(uint16_t)(((size_t)(table - root_table)) - sub_key);
|
244
|
+
root_table[sub_key] = ConstructHuffmanCode(
|
245
|
+
(uint8_t)(table_bits + root_bits),
|
246
|
+
(uint16_t)(((size_t)(table - root_table)) - sub_key));
|
252
247
|
sub_key = 0;
|
253
248
|
}
|
254
|
-
code.bits = (uint8_t)(len - root_bits);
|
255
249
|
symbol = symbol_lists[symbol];
|
256
|
-
code
|
250
|
+
code = ConstructHuffmanCode((uint8_t)(len - root_bits), (uint16_t)symbol);
|
257
251
|
ReplicateValue(
|
258
252
|
&table[BrotliReverseBits(sub_key)], step, table_size, code);
|
259
253
|
sub_key += sub_key_step;
|
@@ -272,35 +266,28 @@ uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
|
|
272
266
|
const uint32_t goal_size = 1U << root_bits;
|
273
267
|
switch (num_symbols) {
|
274
268
|
case 0:
|
275
|
-
table[0]
|
276
|
-
table[0].value = val[0];
|
269
|
+
table[0] = ConstructHuffmanCode(0, val[0]);
|
277
270
|
break;
|
278
271
|
case 1:
|
279
|
-
table[0].bits = 1;
|
280
|
-
table[1].bits = 1;
|
281
272
|
if (val[1] > val[0]) {
|
282
|
-
table[0]
|
283
|
-
table[1]
|
273
|
+
table[0] = ConstructHuffmanCode(1, val[0]);
|
274
|
+
table[1] = ConstructHuffmanCode(1, val[1]);
|
284
275
|
} else {
|
285
|
-
table[0]
|
286
|
-
table[1]
|
276
|
+
table[0] = ConstructHuffmanCode(1, val[1]);
|
277
|
+
table[1] = ConstructHuffmanCode(1, val[0]);
|
287
278
|
}
|
288
279
|
table_size = 2;
|
289
280
|
break;
|
290
281
|
case 2:
|
291
|
-
table[0]
|
292
|
-
table[
|
293
|
-
table[2].bits = 1;
|
294
|
-
table[2].value = val[0];
|
282
|
+
table[0] = ConstructHuffmanCode(1, val[0]);
|
283
|
+
table[2] = ConstructHuffmanCode(1, val[0]);
|
295
284
|
if (val[2] > val[1]) {
|
296
|
-
table[1]
|
297
|
-
table[3]
|
285
|
+
table[1] = ConstructHuffmanCode(2, val[1]);
|
286
|
+
table[3] = ConstructHuffmanCode(2, val[2]);
|
298
287
|
} else {
|
299
|
-
table[1]
|
300
|
-
table[3]
|
288
|
+
table[1] = ConstructHuffmanCode(2, val[2]);
|
289
|
+
table[3] = ConstructHuffmanCode(2, val[1]);
|
301
290
|
}
|
302
|
-
table[1].bits = 2;
|
303
|
-
table[3].bits = 2;
|
304
291
|
table_size = 4;
|
305
292
|
break;
|
306
293
|
case 3: {
|
@@ -314,33 +301,27 @@ uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
|
|
314
301
|
}
|
315
302
|
}
|
316
303
|
}
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
table[
|
321
|
-
table[2].value = val[1];
|
322
|
-
table[1].value = val[2];
|
323
|
-
table[3].value = val[3];
|
304
|
+
table[0] = ConstructHuffmanCode(2, val[0]);
|
305
|
+
table[2] = ConstructHuffmanCode(2, val[1]);
|
306
|
+
table[1] = ConstructHuffmanCode(2, val[2]);
|
307
|
+
table[3] = ConstructHuffmanCode(2, val[3]);
|
324
308
|
table_size = 4;
|
325
309
|
break;
|
326
310
|
}
|
327
311
|
case 4: {
|
328
|
-
int i;
|
329
312
|
if (val[3] < val[2]) {
|
330
313
|
uint16_t t = val[3];
|
331
314
|
val[3] = val[2];
|
332
315
|
val[2] = t;
|
333
316
|
}
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
table[
|
339
|
-
table[
|
340
|
-
table[
|
341
|
-
table[7]
|
342
|
-
table[3].bits = 3;
|
343
|
-
table[7].bits = 3;
|
317
|
+
table[0] = ConstructHuffmanCode(1, val[0]);
|
318
|
+
table[1] = ConstructHuffmanCode(2, val[1]);
|
319
|
+
table[2] = ConstructHuffmanCode(1, val[0]);
|
320
|
+
table[3] = ConstructHuffmanCode(3, val[2]);
|
321
|
+
table[4] = ConstructHuffmanCode(1, val[0]);
|
322
|
+
table[5] = ConstructHuffmanCode(2, val[1]);
|
323
|
+
table[6] = ConstructHuffmanCode(1, val[0]);
|
324
|
+
table[7] = ConstructHuffmanCode(3, val[3]);
|
344
325
|
table_size = 8;
|
345
326
|
break;
|
346
327
|
}
|
@@ -9,8 +9,8 @@
|
|
9
9
|
#ifndef BROTLI_DEC_HUFFMAN_H_
|
10
10
|
#define BROTLI_DEC_HUFFMAN_H_
|
11
11
|
|
12
|
+
#include "../common/platform.h"
|
12
13
|
#include <brotli/types.h>
|
13
|
-
#include "./port.h"
|
14
14
|
|
15
15
|
#if defined(__cplusplus) || defined(c_plusplus)
|
16
16
|
extern "C" {
|
@@ -18,11 +18,6 @@ extern "C" {
|
|
18
18
|
|
19
19
|
#define BROTLI_HUFFMAN_MAX_CODE_LENGTH 15
|
20
20
|
|
21
|
-
/* Maximum possible Huffman table size for an alphabet size of (index * 32),
|
22
|
-
* max code length 15 and root table bits 8. */
|
23
|
-
static const uint16_t kMaxHuffmanTableSize[] = {
|
24
|
-
256, 402, 436, 468, 500, 534, 566, 598, 630, 662, 694, 726, 758, 790, 822,
|
25
|
-
854, 886, 920, 952, 984, 1016, 1048, 1080};
|
26
21
|
/* BROTLI_NUM_BLOCK_LEN_SYMBOLS == 26 */
|
27
22
|
#define BROTLI_HUFFMAN_MAX_SIZE_26 396
|
28
23
|
/* BROTLI_MAX_BLOCK_TYPE_SYMBOLS == 258 */
|
@@ -32,32 +27,90 @@ static const uint16_t kMaxHuffmanTableSize[] = {
|
|
32
27
|
|
33
28
|
#define BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH 5
|
34
29
|
|
30
|
+
#if ((defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8_32)) && \
|
31
|
+
BROTLI_GNUC_HAS_ATTRIBUTE(aligned, 2, 7, 0))
|
32
|
+
#define BROTLI_HUFFMAN_CODE_FAST_LOAD
|
33
|
+
#endif
|
34
|
+
|
35
|
+
#if !defined(BROTLI_HUFFMAN_CODE_FAST_LOAD)
|
36
|
+
/* Do not create this struct directly - use the ConstructHuffmanCode
|
37
|
+
* constructor below! */
|
35
38
|
typedef struct {
|
36
39
|
uint8_t bits; /* number of bits used for this symbol */
|
37
40
|
uint16_t value; /* symbol value or table offset */
|
38
41
|
} HuffmanCode;
|
39
42
|
|
43
|
+
static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits,
|
44
|
+
const uint16_t value) {
|
45
|
+
HuffmanCode h;
|
46
|
+
h.bits = bits;
|
47
|
+
h.value = value;
|
48
|
+
return h;
|
49
|
+
}
|
50
|
+
|
51
|
+
/* Please use the following macros to optimize HuffmanCode accesses in hot
|
52
|
+
* paths.
|
53
|
+
*
|
54
|
+
* For example, assuming |table| contains a HuffmanCode pointer:
|
55
|
+
*
|
56
|
+
* BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table);
|
57
|
+
* BROTLI_HC_ADJUST_TABLE_INDEX(table, index_into_table);
|
58
|
+
* *bits = BROTLI_HC_GET_BITS(table);
|
59
|
+
* *value = BROTLI_HC_GET_VALUE(table);
|
60
|
+
* BROTLI_HC_ADJUST_TABLE_INDEX(table, offset);
|
61
|
+
* *bits2 = BROTLI_HC_GET_BITS(table);
|
62
|
+
* *value2 = BROTLI_HC_GET_VALUE(table);
|
63
|
+
*
|
64
|
+
*/
|
65
|
+
|
66
|
+
#define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H)
|
67
|
+
#define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V)
|
68
|
+
|
69
|
+
/* These must be given a HuffmanCode pointer! */
|
70
|
+
#define BROTLI_HC_FAST_LOAD_BITS(H) (H->bits)
|
71
|
+
#define BROTLI_HC_FAST_LOAD_VALUE(H) (H->value)
|
72
|
+
|
73
|
+
#else /* BROTLI_HUFFMAN_CODE_FAST_LOAD */
|
74
|
+
|
75
|
+
typedef BROTLI_ALIGNED(4) uint32_t HuffmanCode;
|
76
|
+
|
77
|
+
static BROTLI_INLINE HuffmanCode ConstructHuffmanCode(const uint8_t bits,
|
78
|
+
const uint16_t value) {
|
79
|
+
return (HuffmanCode) ((value & 0xFFFF) << 16) | (bits & 0xFF);
|
80
|
+
}
|
81
|
+
|
82
|
+
#define BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(H) uint32_t __fastload_##H = (*H)
|
83
|
+
#define BROTLI_HC_ADJUST_TABLE_INDEX(H, V) H += (V); __fastload_##H = (*H)
|
84
|
+
|
85
|
+
/* These must be given a HuffmanCode pointer! */
|
86
|
+
#define BROTLI_HC_FAST_LOAD_BITS(H) ((__fastload_##H) & 0xFF)
|
87
|
+
#define BROTLI_HC_FAST_LOAD_VALUE(H) ((__fastload_##H) >> 16)
|
88
|
+
#endif /* BROTLI_HUFFMAN_CODE_FAST_LOAD */
|
89
|
+
|
40
90
|
/* Builds Huffman lookup table assuming code lengths are in symbol order. */
|
41
91
|
BROTLI_INTERNAL void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table,
|
42
92
|
const uint8_t* const code_lengths, uint16_t* count);
|
43
93
|
|
44
|
-
/* Builds Huffman lookup table assuming code lengths are in symbol order.
|
45
|
-
|
94
|
+
/* Builds Huffman lookup table assuming code lengths are in symbol order.
|
95
|
+
Returns size of resulting table. */
|
46
96
|
BROTLI_INTERNAL uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
47
|
-
int root_bits, const uint16_t* const symbol_lists, uint16_t*
|
97
|
+
int root_bits, const uint16_t* const symbol_lists, uint16_t* count);
|
48
98
|
|
49
|
-
/* Builds a simple Huffman table. The num_symbols parameter is to be
|
50
|
-
|
51
|
-
|
52
|
-
|
99
|
+
/* Builds a simple Huffman table. The |num_symbols| parameter is to be
|
100
|
+
interpreted as follows: 0 means 1 symbol, 1 means 2 symbols,
|
101
|
+
2 means 3 symbols, 3 means 4 symbols with lengths [2, 2, 2, 2],
|
102
|
+
4 means 4 symbols with lengths [1, 2, 3, 3]. */
|
53
103
|
BROTLI_INTERNAL uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
|
54
104
|
int root_bits, uint16_t* symbols, uint32_t num_symbols);
|
55
105
|
|
56
106
|
/* Contains a collection of Huffman trees with the same alphabet size. */
|
107
|
+
/* alphabet_size_limit is needed due to simple codes, since
|
108
|
+
log2(alphabet_size_max) could be greater than log2(alphabet_size_limit). */
|
57
109
|
typedef struct {
|
58
110
|
HuffmanCode** htrees;
|
59
111
|
HuffmanCode* codes;
|
60
|
-
uint16_t
|
112
|
+
uint16_t alphabet_size_max;
|
113
|
+
uint16_t alphabet_size_limit;
|
61
114
|
uint16_t num_htrees;
|
62
115
|
} HuffmanTreeGroup;
|
63
116
|
|
@@ -5,8 +5,7 @@
|
|
5
5
|
*/
|
6
6
|
|
7
7
|
/* Lookup tables to map prefix codes to value ranges. This is used during
|
8
|
-
decoding of the block lengths, literal insertion lengths and copy lengths.
|
9
|
-
*/
|
8
|
+
decoding of the block lengths, literal insertion lengths and copy lengths. */
|
10
9
|
|
11
10
|
#ifndef BROTLI_DEC_PREFIX_H_
|
12
11
|
#define BROTLI_DEC_PREFIX_H_
|
@@ -14,24 +13,6 @@
|
|
14
13
|
#include "../common/constants.h"
|
15
14
|
#include <brotli/types.h>
|
16
15
|
|
17
|
-
/* Represents the range of values belonging to a prefix code: */
|
18
|
-
/* [offset, offset + 2^nbits) */
|
19
|
-
struct PrefixCodeRange {
|
20
|
-
uint16_t offset;
|
21
|
-
uint8_t nbits;
|
22
|
-
};
|
23
|
-
|
24
|
-
static const struct PrefixCodeRange
|
25
|
-
kBlockLengthPrefixCode[BROTLI_NUM_BLOCK_LEN_SYMBOLS] = {
|
26
|
-
{ 1, 2}, { 5, 2}, { 9, 2}, { 13, 2},
|
27
|
-
{ 17, 3}, { 25, 3}, { 33, 3}, { 41, 3},
|
28
|
-
{ 49, 4}, { 65, 4}, { 81, 4}, { 97, 4},
|
29
|
-
{ 113, 5}, { 145, 5}, { 177, 5}, { 209, 5},
|
30
|
-
{ 241, 6}, { 305, 6}, { 369, 7}, { 497, 8},
|
31
|
-
{ 753, 9}, { 1265, 10}, {2289, 11}, {4337, 12},
|
32
|
-
{8433, 13}, {16625, 24}
|
33
|
-
};
|
34
|
-
|
35
16
|
typedef struct CmdLutElement {
|
36
17
|
uint8_t insert_len_extra_bits;
|
37
18
|
uint8_t copy_len_extra_bits;
|