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.
Files changed (111) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/main.yml +34 -0
  3. data/.github/workflows/publish.yml +34 -0
  4. data/Gemfile +6 -2
  5. data/Rakefile +18 -6
  6. data/bin/before_install.sh +9 -0
  7. data/brotli.gemspec +7 -13
  8. data/ext/brotli/brotli.c +209 -11
  9. data/ext/brotli/buffer.c +1 -7
  10. data/ext/brotli/buffer.h +1 -1
  11. data/ext/brotli/extconf.rb +45 -26
  12. data/lib/brotli/version.rb +1 -1
  13. data/smoke.sh +1 -1
  14. data/test/brotli_test.rb +104 -0
  15. data/test/brotli_writer_test.rb +36 -0
  16. data/test/test_helper.rb +8 -0
  17. data/vendor/brotli/c/common/constants.c +15 -0
  18. data/vendor/brotli/c/common/constants.h +149 -6
  19. data/vendor/brotli/c/{dec/context.h → common/context.c} +91 -186
  20. data/vendor/brotli/c/common/context.h +113 -0
  21. data/vendor/brotli/c/common/dictionary.bin +0 -0
  22. data/vendor/brotli/c/common/dictionary.bin.br +0 -0
  23. data/vendor/brotli/c/common/dictionary.c +11 -2
  24. data/vendor/brotli/c/common/dictionary.h +4 -4
  25. data/vendor/brotli/c/common/platform.c +22 -0
  26. data/vendor/brotli/c/common/platform.h +594 -0
  27. data/vendor/brotli/c/common/transform.c +291 -0
  28. data/vendor/brotli/c/common/transform.h +85 -0
  29. data/vendor/brotli/c/common/version.h +8 -1
  30. data/vendor/brotli/c/dec/bit_reader.c +29 -1
  31. data/vendor/brotli/c/dec/bit_reader.h +91 -100
  32. data/vendor/brotli/c/dec/decode.c +665 -437
  33. data/vendor/brotli/c/dec/huffman.c +65 -84
  34. data/vendor/brotli/c/dec/huffman.h +67 -14
  35. data/vendor/brotli/c/dec/prefix.h +1 -20
  36. data/vendor/brotli/c/dec/state.c +32 -45
  37. data/vendor/brotli/c/dec/state.h +173 -55
  38. data/vendor/brotli/c/enc/backward_references.c +27 -16
  39. data/vendor/brotli/c/enc/backward_references.h +7 -7
  40. data/vendor/brotli/c/enc/backward_references_hq.c +155 -116
  41. data/vendor/brotli/c/enc/backward_references_hq.h +22 -23
  42. data/vendor/brotli/c/enc/backward_references_inc.h +32 -22
  43. data/vendor/brotli/c/enc/bit_cost.c +1 -1
  44. data/vendor/brotli/c/enc/bit_cost.h +5 -5
  45. data/vendor/brotli/c/enc/block_encoder_inc.h +7 -6
  46. data/vendor/brotli/c/enc/block_splitter.c +5 -6
  47. data/vendor/brotli/c/enc/block_splitter.h +1 -1
  48. data/vendor/brotli/c/enc/block_splitter_inc.h +26 -17
  49. data/vendor/brotli/c/enc/brotli_bit_stream.c +107 -123
  50. data/vendor/brotli/c/enc/brotli_bit_stream.h +19 -38
  51. data/vendor/brotli/c/enc/cluster.c +1 -1
  52. data/vendor/brotli/c/enc/cluster.h +1 -1
  53. data/vendor/brotli/c/enc/cluster_inc.h +6 -3
  54. data/vendor/brotli/c/enc/command.c +28 -0
  55. data/vendor/brotli/c/enc/command.h +52 -42
  56. data/vendor/brotli/c/enc/compress_fragment.c +21 -22
  57. data/vendor/brotli/c/enc/compress_fragment.h +1 -1
  58. data/vendor/brotli/c/enc/compress_fragment_two_pass.c +102 -69
  59. data/vendor/brotli/c/enc/compress_fragment_two_pass.h +1 -1
  60. data/vendor/brotli/c/enc/dictionary_hash.c +1827 -1101
  61. data/vendor/brotli/c/enc/dictionary_hash.h +2 -1
  62. data/vendor/brotli/c/enc/encode.c +358 -195
  63. data/vendor/brotli/c/enc/encoder_dict.c +33 -0
  64. data/vendor/brotli/c/enc/encoder_dict.h +43 -0
  65. data/vendor/brotli/c/enc/entropy_encode.c +16 -14
  66. data/vendor/brotli/c/enc/entropy_encode.h +7 -7
  67. data/vendor/brotli/c/enc/entropy_encode_static.h +3 -3
  68. data/vendor/brotli/c/enc/fast_log.c +105 -0
  69. data/vendor/brotli/c/enc/fast_log.h +20 -99
  70. data/vendor/brotli/c/enc/find_match_length.h +5 -6
  71. data/vendor/brotli/c/enc/hash.h +145 -103
  72. data/vendor/brotli/c/enc/hash_composite_inc.h +125 -0
  73. data/vendor/brotli/c/enc/hash_forgetful_chain_inc.h +93 -53
  74. data/vendor/brotli/c/enc/hash_longest_match64_inc.h +54 -53
  75. data/vendor/brotli/c/enc/hash_longest_match_inc.h +58 -54
  76. data/vendor/brotli/c/enc/hash_longest_match_quickly_inc.h +95 -63
  77. data/vendor/brotli/c/enc/hash_rolling_inc.h +212 -0
  78. data/vendor/brotli/c/enc/hash_to_binary_tree_inc.h +46 -43
  79. data/vendor/brotli/c/enc/histogram.c +9 -6
  80. data/vendor/brotli/c/enc/histogram.h +6 -3
  81. data/vendor/brotli/c/enc/histogram_inc.h +1 -1
  82. data/vendor/brotli/c/enc/literal_cost.c +5 -5
  83. data/vendor/brotli/c/enc/literal_cost.h +2 -2
  84. data/vendor/brotli/c/enc/memory.c +5 -16
  85. data/vendor/brotli/c/enc/memory.h +52 -1
  86. data/vendor/brotli/c/enc/metablock.c +171 -36
  87. data/vendor/brotli/c/enc/metablock.h +13 -8
  88. data/vendor/brotli/c/enc/metablock_inc.h +2 -2
  89. data/vendor/brotli/c/enc/params.h +46 -0
  90. data/vendor/brotli/c/enc/prefix.h +3 -4
  91. data/vendor/brotli/c/enc/quality.h +29 -24
  92. data/vendor/brotli/c/enc/ringbuffer.h +19 -12
  93. data/vendor/brotli/c/enc/static_dict.c +49 -45
  94. data/vendor/brotli/c/enc/static_dict.h +4 -3
  95. data/vendor/brotli/c/enc/static_dict_lut.h +1 -1
  96. data/vendor/brotli/c/enc/utf8_util.c +21 -21
  97. data/vendor/brotli/c/enc/utf8_util.h +1 -1
  98. data/vendor/brotli/c/enc/write_bits.h +35 -38
  99. data/vendor/brotli/c/include/brotli/decode.h +13 -8
  100. data/vendor/brotli/c/include/brotli/encode.h +54 -8
  101. data/vendor/brotli/c/include/brotli/port.h +225 -83
  102. data/vendor/brotli/c/include/brotli/types.h +0 -7
  103. metadata +28 -87
  104. data/.travis.yml +0 -30
  105. data/spec/brotli_spec.rb +0 -88
  106. data/spec/inflate_spec.rb +0 -75
  107. data/spec/spec_helper.rb +0 -4
  108. data/vendor/brotli/c/dec/port.h +0 -168
  109. data/vendor/brotli/c/dec/transform.h +0 -300
  110. data/vendor/brotli/c/enc/context.h +0 -184
  111. data/vendor/brotli/c/enc/port.h +0 -184
@@ -6,23 +6,23 @@
6
6
 
7
7
  #include <brotli/decode.h>
8
8
 
9
- #ifdef __ARM_NEON__
10
- #include <arm_neon.h>
11
- #endif
12
-
13
9
  #include <stdlib.h> /* free, malloc */
14
10
  #include <string.h> /* memcpy, memset */
15
11
 
16
12
  #include "../common/constants.h"
13
+ #include "../common/context.h"
17
14
  #include "../common/dictionary.h"
15
+ #include "../common/platform.h"
16
+ #include "../common/transform.h"
18
17
  #include "../common/version.h"
19
18
  #include "./bit_reader.h"
20
- #include "./context.h"
21
19
  #include "./huffman.h"
22
- #include "./port.h"
23
20
  #include "./prefix.h"
24
21
  #include "./state.h"
25
- #include "./transform.h"
22
+
23
+ #if defined(BROTLI_TARGET_NEON)
24
+ #include <arm_neon.h>
25
+ #endif
26
26
 
27
27
  #if defined(__cplusplus) || defined(c_plusplus)
28
28
  extern "C" {
@@ -37,11 +37,12 @@ extern "C" {
37
37
  (unsigned long)(idx), (unsigned long)array_name[idx]))
38
38
 
39
39
  #define HUFFMAN_TABLE_BITS 8U
40
- #define HUFFMAN_TABLE_MASK 0xff
40
+ #define HUFFMAN_TABLE_MASK 0xFF
41
41
 
42
42
  /* We need the slack region for the following reasons:
43
43
  - doing up to two 16-byte copies for fast backward copying
44
- - inserting transformed dictionary word (5 prefix + 24 base + 8 suffix) */
44
+ - inserting transformed dictionary word:
45
+ 5 prefix + 24 base + 8 suffix */
45
46
  static const uint32_t kRingBufferWriteAheadSlack = 42;
46
47
 
47
48
  static const uint8_t kCodeLengthCodeOrder[BROTLI_CODE_LENGTH_CODES] = {
@@ -59,11 +60,16 @@ static const uint8_t kCodeLengthPrefixValue[16] = {
59
60
 
60
61
  BROTLI_BOOL BrotliDecoderSetParameter(
61
62
  BrotliDecoderState* state, BrotliDecoderParameter p, uint32_t value) {
63
+ if (state->state != BROTLI_STATE_UNINITED) return BROTLI_FALSE;
62
64
  switch (p) {
63
65
  case BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION:
64
66
  state->canny_ringbuffer_allocation = !!value ? 0 : 1;
65
67
  return BROTLI_TRUE;
66
68
 
69
+ case BROTLI_DECODER_PARAM_LARGE_WINDOW:
70
+ state->large_window = TO_BROTLI_BOOL(!!value);
71
+ return BROTLI_TRUE;
72
+
67
73
  default: return BROTLI_FALSE;
68
74
  }
69
75
  }
@@ -80,8 +86,15 @@ BrotliDecoderState* BrotliDecoderCreateInstance(
80
86
  BROTLI_DUMP();
81
87
  return 0;
82
88
  }
83
- BrotliDecoderStateInitWithCustomAllocators(
84
- state, alloc_func, free_func, opaque);
89
+ if (!BrotliDecoderStateInit(state, alloc_func, free_func, opaque)) {
90
+ BROTLI_DUMP();
91
+ if (!alloc_func && !free_func) {
92
+ free(state);
93
+ } else if (alloc_func && free_func) {
94
+ free_func(opaque, state);
95
+ }
96
+ return 0;
97
+ }
85
98
  return state;
86
99
  }
87
100
 
@@ -97,43 +110,65 @@ void BrotliDecoderDestroyInstance(BrotliDecoderState* state) {
97
110
  }
98
111
  }
99
112
 
100
- /* Saves error code and converts it to BrotliDecoderResult */
113
+ /* Saves error code and converts it to BrotliDecoderResult. */
101
114
  static BROTLI_NOINLINE BrotliDecoderResult SaveErrorCode(
102
115
  BrotliDecoderState* s, BrotliDecoderErrorCode e) {
103
116
  s->error_code = (int)e;
104
117
  switch (e) {
105
118
  case BROTLI_DECODER_SUCCESS:
106
119
  return BROTLI_DECODER_RESULT_SUCCESS;
120
+
107
121
  case BROTLI_DECODER_NEEDS_MORE_INPUT:
108
122
  return BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT;
123
+
109
124
  case BROTLI_DECODER_NEEDS_MORE_OUTPUT:
110
125
  return BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
126
+
111
127
  default:
112
128
  return BROTLI_DECODER_RESULT_ERROR;
113
129
  }
114
130
  }
115
131
 
116
- /* Decodes a number in the range [9..24], by reading 1 - 7 bits.
117
- Precondition: bit-reader accumulator has at least 7 bits. */
118
- static uint32_t DecodeWindowBits(BrotliBitReader* br) {
132
+ /* Decodes WBITS by reading 1 - 7 bits, or 0x11 for "Large Window Brotli".
133
+ Precondition: bit-reader accumulator has at least 8 bits. */
134
+ static BrotliDecoderErrorCode DecodeWindowBits(BrotliDecoderState* s,
135
+ BrotliBitReader* br) {
119
136
  uint32_t n;
137
+ BROTLI_BOOL large_window = s->large_window;
138
+ s->large_window = BROTLI_FALSE;
120
139
  BrotliTakeBits(br, 1, &n);
121
140
  if (n == 0) {
122
- return 16;
141
+ s->window_bits = 16;
142
+ return BROTLI_DECODER_SUCCESS;
123
143
  }
124
144
  BrotliTakeBits(br, 3, &n);
125
145
  if (n != 0) {
126
- return 17 + n;
146
+ s->window_bits = 17 + n;
147
+ return BROTLI_DECODER_SUCCESS;
127
148
  }
128
149
  BrotliTakeBits(br, 3, &n);
150
+ if (n == 1) {
151
+ if (large_window) {
152
+ BrotliTakeBits(br, 1, &n);
153
+ if (n == 1) {
154
+ return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS);
155
+ }
156
+ s->large_window = BROTLI_TRUE;
157
+ return BROTLI_DECODER_SUCCESS;
158
+ } else {
159
+ return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS);
160
+ }
161
+ }
129
162
  if (n != 0) {
130
- return 8 + n;
163
+ s->window_bits = 8 + n;
164
+ return BROTLI_DECODER_SUCCESS;
131
165
  }
132
- return 17;
166
+ s->window_bits = 17;
167
+ return BROTLI_DECODER_SUCCESS;
133
168
  }
134
169
 
135
170
  static BROTLI_INLINE void memmove16(uint8_t* dst, uint8_t* src) {
136
- #if defined(__ARM_NEON__)
171
+ #if defined(BROTLI_TARGET_NEON)
137
172
  vst1q_u8(dst, vld1q_u8(src));
138
173
  #else
139
174
  uint32_t buffer[4];
@@ -155,7 +190,7 @@ static BROTLI_NOINLINE BrotliDecoderErrorCode DecodeVarLenUint8(
155
190
  *value = 0;
156
191
  return BROTLI_DECODER_SUCCESS;
157
192
  }
158
- /* No break, transit to the next state. */
193
+ /* Fall through. */
159
194
 
160
195
  case BROTLI_STATE_DECODE_UINT8_SHORT:
161
196
  if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, 3, &bits))) {
@@ -169,7 +204,7 @@ static BROTLI_NOINLINE BrotliDecoderErrorCode DecodeVarLenUint8(
169
204
  }
170
205
  /* Use output value as a temporary storage. It MUST be persisted. */
171
206
  *value = bits;
172
- /* No break, transit to the next state. */
207
+ /* Fall through. */
173
208
 
174
209
  case BROTLI_STATE_DECODE_UINT8_LONG:
175
210
  if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, *value, &bits))) {
@@ -206,7 +241,7 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
206
241
  break;
207
242
  }
208
243
  s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_EMPTY;
209
- /* No break, transit to the next state. */
244
+ /* Fall through. */
210
245
 
211
246
  case BROTLI_STATE_METABLOCK_HEADER_EMPTY:
212
247
  if (!BrotliSafeReadBits(br, 1, &bits)) {
@@ -217,7 +252,7 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
217
252
  return BROTLI_DECODER_SUCCESS;
218
253
  }
219
254
  s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NIBBLES;
220
- /* No break, transit to the next state. */
255
+ /* Fall through. */
221
256
 
222
257
  case BROTLI_STATE_METABLOCK_HEADER_NIBBLES:
223
258
  if (!BrotliSafeReadBits(br, 2, &bits)) {
@@ -231,7 +266,7 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
231
266
  break;
232
267
  }
233
268
  s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_SIZE;
234
- /* No break, transit to the next state. */
269
+ /* Fall through. */
235
270
 
236
271
  case BROTLI_STATE_METABLOCK_HEADER_SIZE:
237
272
  i = s->loop_counter;
@@ -240,14 +275,15 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
240
275
  s->loop_counter = i;
241
276
  return BROTLI_DECODER_NEEDS_MORE_INPUT;
242
277
  }
243
- if (i + 1 == s->size_nibbles && s->size_nibbles > 4 && bits == 0) {
278
+ if (i + 1 == (int)s->size_nibbles && s->size_nibbles > 4 &&
279
+ bits == 0) {
244
280
  return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE);
245
281
  }
246
282
  s->meta_block_remaining_len |= (int)(bits << (i * 4));
247
283
  }
248
284
  s->substate_metablock_header =
249
285
  BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED;
250
- /* No break, transit to the next state. */
286
+ /* Fall through. */
251
287
 
252
288
  case BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED:
253
289
  if (!s->is_last_metablock) {
@@ -268,7 +304,7 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
268
304
  return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_RESERVED);
269
305
  }
270
306
  s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_BYTES;
271
- /* No break, transit to the next state. */
307
+ /* Fall through. */
272
308
 
273
309
  case BROTLI_STATE_METABLOCK_HEADER_BYTES:
274
310
  if (!BrotliSafeReadBits(br, 2, &bits)) {
@@ -280,7 +316,7 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
280
316
  }
281
317
  s->size_nibbles = (uint8_t)bits;
282
318
  s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_METADATA;
283
- /* No break, transit to the next state. */
319
+ /* Fall through. */
284
320
 
285
321
  case BROTLI_STATE_METABLOCK_HEADER_METADATA:
286
322
  i = s->loop_counter;
@@ -289,7 +325,8 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
289
325
  s->loop_counter = i;
290
326
  return BROTLI_DECODER_NEEDS_MORE_INPUT;
291
327
  }
292
- if (i + 1 == s->size_nibbles && s->size_nibbles > 1 && bits == 0) {
328
+ if (i + 1 == (int)s->size_nibbles && s->size_nibbles > 1 &&
329
+ bits == 0) {
293
330
  return BROTLI_FAILURE(
294
331
  BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE);
295
332
  }
@@ -313,15 +350,17 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
313
350
  static BROTLI_INLINE uint32_t DecodeSymbol(uint32_t bits,
314
351
  const HuffmanCode* table,
315
352
  BrotliBitReader* br) {
316
- table += bits & HUFFMAN_TABLE_MASK;
317
- if (table->bits > HUFFMAN_TABLE_BITS) {
318
- uint32_t nbits = table->bits - HUFFMAN_TABLE_BITS;
353
+ BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table);
354
+ BROTLI_HC_ADJUST_TABLE_INDEX(table, bits & HUFFMAN_TABLE_MASK);
355
+ if (BROTLI_HC_FAST_LOAD_BITS(table) > HUFFMAN_TABLE_BITS) {
356
+ uint32_t nbits = BROTLI_HC_FAST_LOAD_BITS(table) - HUFFMAN_TABLE_BITS;
319
357
  BrotliDropBits(br, HUFFMAN_TABLE_BITS);
320
- table += table->value;
321
- table += (bits >> HUFFMAN_TABLE_BITS) & BitMask(nbits);
358
+ BROTLI_HC_ADJUST_TABLE_INDEX(table,
359
+ BROTLI_HC_FAST_LOAD_VALUE(table) +
360
+ ((bits >> HUFFMAN_TABLE_BITS) & BitMask(nbits)));
322
361
  }
323
- BrotliDropBits(br, table->bits);
324
- return table->value;
362
+ BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(table));
363
+ return BROTLI_HC_FAST_LOAD_VALUE(table);
325
364
  }
326
365
 
327
366
  /* Reads and decodes the next Huffman code from bit-stream.
@@ -337,38 +376,39 @@ static BROTLI_NOINLINE BROTLI_BOOL SafeDecodeSymbol(
337
376
  const HuffmanCode* table, BrotliBitReader* br, uint32_t* result) {
338
377
  uint32_t val;
339
378
  uint32_t available_bits = BrotliGetAvailableBits(br);
379
+ BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table);
340
380
  if (available_bits == 0) {
341
- if (table->bits == 0) {
342
- *result = table->value;
381
+ if (BROTLI_HC_FAST_LOAD_BITS(table) == 0) {
382
+ *result = BROTLI_HC_FAST_LOAD_VALUE(table);
343
383
  return BROTLI_TRUE;
344
384
  }
345
- return BROTLI_FALSE; /* No valid bits at all. */
385
+ return BROTLI_FALSE; /* No valid bits at all. */
346
386
  }
347
387
  val = (uint32_t)BrotliGetBitsUnmasked(br);
348
- table += val & HUFFMAN_TABLE_MASK;
349
- if (table->bits <= HUFFMAN_TABLE_BITS) {
350
- if (table->bits <= available_bits) {
351
- BrotliDropBits(br, table->bits);
352
- *result = table->value;
388
+ BROTLI_HC_ADJUST_TABLE_INDEX(table, val & HUFFMAN_TABLE_MASK);
389
+ if (BROTLI_HC_FAST_LOAD_BITS(table) <= HUFFMAN_TABLE_BITS) {
390
+ if (BROTLI_HC_FAST_LOAD_BITS(table) <= available_bits) {
391
+ BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(table));
392
+ *result = BROTLI_HC_FAST_LOAD_VALUE(table);
353
393
  return BROTLI_TRUE;
354
394
  } else {
355
- return BROTLI_FALSE; /* Not enough bits for the first level. */
395
+ return BROTLI_FALSE; /* Not enough bits for the first level. */
356
396
  }
357
397
  }
358
398
  if (available_bits <= HUFFMAN_TABLE_BITS) {
359
- return BROTLI_FALSE; /* Not enough bits to move to the second level. */
399
+ return BROTLI_FALSE; /* Not enough bits to move to the second level. */
360
400
  }
361
401
 
362
402
  /* Speculatively drop HUFFMAN_TABLE_BITS. */
363
- val = (val & BitMask(table->bits)) >> HUFFMAN_TABLE_BITS;
403
+ val = (val & BitMask(BROTLI_HC_FAST_LOAD_BITS(table))) >> HUFFMAN_TABLE_BITS;
364
404
  available_bits -= HUFFMAN_TABLE_BITS;
365
- table += table->value + val;
366
- if (available_bits < table->bits) {
367
- return BROTLI_FALSE; /* Not enough bits for the second level. */
405
+ BROTLI_HC_ADJUST_TABLE_INDEX(table, BROTLI_HC_FAST_LOAD_VALUE(table) + val);
406
+ if (available_bits < BROTLI_HC_FAST_LOAD_BITS(table)) {
407
+ return BROTLI_FALSE; /* Not enough bits for the second level. */
368
408
  }
369
409
 
370
- BrotliDropBits(br, HUFFMAN_TABLE_BITS + table->bits);
371
- *result = table->value;
410
+ BrotliDropBits(br, HUFFMAN_TABLE_BITS + BROTLI_HC_FAST_LOAD_BITS(table));
411
+ *result = BROTLI_HC_FAST_LOAD_VALUE(table);
372
412
  return BROTLI_TRUE;
373
413
  }
374
414
 
@@ -391,9 +431,10 @@ static BROTLI_INLINE void PreloadSymbol(int safe,
391
431
  if (safe) {
392
432
  return;
393
433
  }
394
- table += BrotliGetBits(br, HUFFMAN_TABLE_BITS);
395
- *bits = table->bits;
396
- *value = table->value;
434
+ BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table);
435
+ BROTLI_HC_ADJUST_TABLE_INDEX(table, BrotliGetBits(br, HUFFMAN_TABLE_BITS));
436
+ *bits = BROTLI_HC_FAST_LOAD_BITS(table);
437
+ *value = BROTLI_HC_FAST_LOAD_VALUE(table);
397
438
  }
398
439
 
399
440
  /* Decodes the next Huffman code using data prepared by PreloadSymbol.
@@ -407,10 +448,11 @@ static BROTLI_INLINE uint32_t ReadPreloadedSymbol(const HuffmanCode* table,
407
448
  uint32_t val = BrotliGet16BitsUnmasked(br);
408
449
  const HuffmanCode* ext = table + (val & HUFFMAN_TABLE_MASK) + *value;
409
450
  uint32_t mask = BitMask((*bits - HUFFMAN_TABLE_BITS));
451
+ BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(ext);
410
452
  BrotliDropBits(br, HUFFMAN_TABLE_BITS);
411
- ext += (val >> HUFFMAN_TABLE_BITS) & mask;
412
- BrotliDropBits(br, ext->bits);
413
- result = ext->value;
453
+ BROTLI_HC_ADJUST_TABLE_INDEX(ext, (val >> HUFFMAN_TABLE_BITS) & mask);
454
+ BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(ext));
455
+ result = BROTLI_HC_FAST_LOAD_VALUE(ext);
414
456
  } else {
415
457
  BrotliDropBits(br, *bits);
416
458
  }
@@ -428,36 +470,37 @@ static BROTLI_INLINE uint32_t Log2Floor(uint32_t x) {
428
470
  }
429
471
 
430
472
  /* Reads (s->symbol + 1) symbols.
431
- Totally 1..4 symbols are read, 1..10 bits each.
432
- The list of symbols MUST NOT contain duplicates.
433
- */
473
+ Totally 1..4 symbols are read, 1..11 bits each.
474
+ The list of symbols MUST NOT contain duplicates. */
434
475
  static BrotliDecoderErrorCode ReadSimpleHuffmanSymbols(
435
- uint32_t alphabet_size, BrotliDecoderState* s) {
436
- /* max_bits == 1..10; symbol == 0..3; 1..40 bits will be read. */
476
+ uint32_t alphabet_size_max, uint32_t alphabet_size_limit,
477
+ BrotliDecoderState* s) {
478
+ /* max_bits == 1..11; symbol == 0..3; 1..44 bits will be read. */
437
479
  BrotliBitReader* br = &s->br;
438
- uint32_t max_bits = Log2Floor(alphabet_size - 1);
439
- uint32_t i = s->sub_loop_counter;
440
- uint32_t num_symbols = s->symbol;
480
+ BrotliMetablockHeaderArena* h = &s->arena.header;
481
+ uint32_t max_bits = Log2Floor(alphabet_size_max - 1);
482
+ uint32_t i = h->sub_loop_counter;
483
+ uint32_t num_symbols = h->symbol;
441
484
  while (i <= num_symbols) {
442
485
  uint32_t v;
443
486
  if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, max_bits, &v))) {
444
- s->sub_loop_counter = i;
445
- s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_READ;
487
+ h->sub_loop_counter = i;
488
+ h->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_READ;
446
489
  return BROTLI_DECODER_NEEDS_MORE_INPUT;
447
490
  }
448
- if (v >= alphabet_size) {
491
+ if (v >= alphabet_size_limit) {
449
492
  return
450
493
  BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET);
451
494
  }
452
- s->symbols_lists_array[i] = (uint16_t)v;
453
- BROTLI_LOG_UINT(s->symbols_lists_array[i]);
495
+ h->symbols_lists_array[i] = (uint16_t)v;
496
+ BROTLI_LOG_UINT(h->symbols_lists_array[i]);
454
497
  ++i;
455
498
  }
456
499
 
457
500
  for (i = 0; i < num_symbols; ++i) {
458
501
  uint32_t k = i + 1;
459
502
  for (; k <= num_symbols; ++k) {
460
- if (s->symbols_lists_array[i] == s->symbols_lists_array[k]) {
503
+ if (h->symbols_lists_array[i] == h->symbols_lists_array[k]) {
461
504
  return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME);
462
505
  }
463
506
  }
@@ -471,20 +514,20 @@ static BrotliDecoderErrorCode ReadSimpleHuffmanSymbols(
471
514
  B) remember code length (if it is not 0)
472
515
  C) extend corresponding index-chain
473
516
  D) reduce the Huffman space
474
- E) update the histogram
475
- */
517
+ E) update the histogram */
476
518
  static BROTLI_INLINE void ProcessSingleCodeLength(uint32_t code_len,
477
519
  uint32_t* symbol, uint32_t* repeat, uint32_t* space,
478
520
  uint32_t* prev_code_len, uint16_t* symbol_lists,
479
521
  uint16_t* code_length_histo, int* next_symbol) {
480
522
  *repeat = 0;
481
- if (code_len != 0) { /* code_len == 1..15 */
523
+ if (code_len != 0) { /* code_len == 1..15 */
482
524
  symbol_lists[next_symbol[code_len]] = (uint16_t)(*symbol);
483
525
  next_symbol[code_len] = (int)(*symbol);
484
526
  *prev_code_len = code_len;
485
527
  *space -= 32768U >> code_len;
486
528
  code_length_histo[code_len]++;
487
- BROTLI_LOG(("[ReadHuffmanCode] code_length[%d] = %d\n", *symbol, code_len));
529
+ BROTLI_LOG(("[ReadHuffmanCode] code_length[%d] = %d\n",
530
+ (int)*symbol, (int)code_len));
488
531
  }
489
532
  (*symbol)++;
490
533
  }
@@ -498,8 +541,7 @@ static BROTLI_INLINE void ProcessSingleCodeLength(uint32_t code_len,
498
541
  D) For each symbol do the same operations as in ProcessSingleCodeLength
499
542
 
500
543
  PRECONDITION: code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH or
501
- code_len == BROTLI_REPEAT_ZERO_CODE_LENGTH
502
- */
544
+ code_len == BROTLI_REPEAT_ZERO_CODE_LENGTH */
503
545
  static BROTLI_INLINE void ProcessRepeatedCodeLength(uint32_t code_len,
504
546
  uint32_t repeat_delta, uint32_t alphabet_size, uint32_t* symbol,
505
547
  uint32_t* repeat, uint32_t* space, uint32_t* prev_code_len,
@@ -530,7 +572,7 @@ static BROTLI_INLINE void ProcessRepeatedCodeLength(uint32_t code_len,
530
572
  return;
531
573
  }
532
574
  BROTLI_LOG(("[ReadHuffmanCode] code_length[%d..%d] = %d\n",
533
- *symbol, *symbol + repeat_delta - 1, *repeat_code_len));
575
+ (int)*symbol, (int)(*symbol + repeat_delta - 1), (int)*repeat_code_len));
534
576
  if (*repeat_code_len != 0) {
535
577
  unsigned last = *symbol + repeat_delta;
536
578
  int next = next_symbol[*repeat_code_len];
@@ -551,37 +593,39 @@ static BROTLI_INLINE void ProcessRepeatedCodeLength(uint32_t code_len,
551
593
  static BrotliDecoderErrorCode ReadSymbolCodeLengths(
552
594
  uint32_t alphabet_size, BrotliDecoderState* s) {
553
595
  BrotliBitReader* br = &s->br;
554
- uint32_t symbol = s->symbol;
555
- uint32_t repeat = s->repeat;
556
- uint32_t space = s->space;
557
- uint32_t prev_code_len = s->prev_code_len;
558
- uint32_t repeat_code_len = s->repeat_code_len;
559
- uint16_t* symbol_lists = s->symbol_lists;
560
- uint16_t* code_length_histo = s->code_length_histo;
561
- int* next_symbol = s->next_symbol;
596
+ BrotliMetablockHeaderArena* h = &s->arena.header;
597
+ uint32_t symbol = h->symbol;
598
+ uint32_t repeat = h->repeat;
599
+ uint32_t space = h->space;
600
+ uint32_t prev_code_len = h->prev_code_len;
601
+ uint32_t repeat_code_len = h->repeat_code_len;
602
+ uint16_t* symbol_lists = h->symbol_lists;
603
+ uint16_t* code_length_histo = h->code_length_histo;
604
+ int* next_symbol = h->next_symbol;
562
605
  if (!BrotliWarmupBitReader(br)) {
563
606
  return BROTLI_DECODER_NEEDS_MORE_INPUT;
564
607
  }
565
608
  while (symbol < alphabet_size && space > 0) {
566
- const HuffmanCode* p = s->table;
609
+ const HuffmanCode* p = h->table;
567
610
  uint32_t code_len;
611
+ BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(p);
568
612
  if (!BrotliCheckInputAmount(br, BROTLI_SHORT_FILL_BIT_WINDOW_READ)) {
569
- s->symbol = symbol;
570
- s->repeat = repeat;
571
- s->prev_code_len = prev_code_len;
572
- s->repeat_code_len = repeat_code_len;
573
- s->space = space;
613
+ h->symbol = symbol;
614
+ h->repeat = repeat;
615
+ h->prev_code_len = prev_code_len;
616
+ h->repeat_code_len = repeat_code_len;
617
+ h->space = space;
574
618
  return BROTLI_DECODER_NEEDS_MORE_INPUT;
575
619
  }
576
620
  BrotliFillBitWindow16(br);
577
- p += BrotliGetBitsUnmasked(br) &
578
- BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH);
579
- BrotliDropBits(br, p->bits); /* Use 1..5 bits */
580
- code_len = p->value; /* code_len == 0..17 */
621
+ BROTLI_HC_ADJUST_TABLE_INDEX(p, BrotliGetBitsUnmasked(br) &
622
+ BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH));
623
+ BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(p)); /* Use 1..5 bits. */
624
+ code_len = BROTLI_HC_FAST_LOAD_VALUE(p); /* code_len == 0..17 */
581
625
  if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) {
582
626
  ProcessSingleCodeLength(code_len, &symbol, &repeat, &space,
583
627
  &prev_code_len, symbol_lists, code_length_histo, next_symbol);
584
- } else { /* code_len == 16..17, extra_bits == 2..3 */
628
+ } else { /* code_len == 16..17, extra_bits == 2..3 */
585
629
  uint32_t extra_bits =
586
630
  (code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) ? 2 : 3;
587
631
  uint32_t repeat_delta =
@@ -592,48 +636,52 @@ static BrotliDecoderErrorCode ReadSymbolCodeLengths(
592
636
  symbol_lists, code_length_histo, next_symbol);
593
637
  }
594
638
  }
595
- s->space = space;
639
+ h->space = space;
596
640
  return BROTLI_DECODER_SUCCESS;
597
641
  }
598
642
 
599
643
  static BrotliDecoderErrorCode SafeReadSymbolCodeLengths(
600
644
  uint32_t alphabet_size, BrotliDecoderState* s) {
601
645
  BrotliBitReader* br = &s->br;
646
+ BrotliMetablockHeaderArena* h = &s->arena.header;
602
647
  BROTLI_BOOL get_byte = BROTLI_FALSE;
603
- while (s->symbol < alphabet_size && s->space > 0) {
604
- const HuffmanCode* p = s->table;
648
+ while (h->symbol < alphabet_size && h->space > 0) {
649
+ const HuffmanCode* p = h->table;
605
650
  uint32_t code_len;
606
651
  uint32_t available_bits;
607
652
  uint32_t bits = 0;
653
+ BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(p);
608
654
  if (get_byte && !BrotliPullByte(br)) return BROTLI_DECODER_NEEDS_MORE_INPUT;
609
655
  get_byte = BROTLI_FALSE;
610
656
  available_bits = BrotliGetAvailableBits(br);
611
657
  if (available_bits != 0) {
612
658
  bits = (uint32_t)BrotliGetBitsUnmasked(br);
613
659
  }
614
- p += bits & BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH);
615
- if (p->bits > available_bits) {
660
+ BROTLI_HC_ADJUST_TABLE_INDEX(p,
661
+ bits & BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH));
662
+ if (BROTLI_HC_FAST_LOAD_BITS(p) > available_bits) {
616
663
  get_byte = BROTLI_TRUE;
617
664
  continue;
618
665
  }
619
- code_len = p->value; /* code_len == 0..17 */
666
+ code_len = BROTLI_HC_FAST_LOAD_VALUE(p); /* code_len == 0..17 */
620
667
  if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) {
621
- BrotliDropBits(br, p->bits);
622
- ProcessSingleCodeLength(code_len, &s->symbol, &s->repeat, &s->space,
623
- &s->prev_code_len, s->symbol_lists, s->code_length_histo,
624
- s->next_symbol);
625
- } else { /* code_len == 16..17, extra_bits == 2..3 */
668
+ BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(p));
669
+ ProcessSingleCodeLength(code_len, &h->symbol, &h->repeat, &h->space,
670
+ &h->prev_code_len, h->symbol_lists, h->code_length_histo,
671
+ h->next_symbol);
672
+ } else { /* code_len == 16..17, extra_bits == 2..3 */
626
673
  uint32_t extra_bits = code_len - 14U;
627
- uint32_t repeat_delta = (bits >> p->bits) & BitMask(extra_bits);
628
- if (available_bits < p->bits + extra_bits) {
674
+ uint32_t repeat_delta = (bits >> BROTLI_HC_FAST_LOAD_BITS(p)) &
675
+ BitMask(extra_bits);
676
+ if (available_bits < BROTLI_HC_FAST_LOAD_BITS(p) + extra_bits) {
629
677
  get_byte = BROTLI_TRUE;
630
678
  continue;
631
679
  }
632
- BrotliDropBits(br, p->bits + extra_bits);
680
+ BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(p) + extra_bits);
633
681
  ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size,
634
- &s->symbol, &s->repeat, &s->space, &s->prev_code_len,
635
- &s->repeat_code_len, s->symbol_lists, s->code_length_histo,
636
- s->next_symbol);
682
+ &h->symbol, &h->repeat, &h->space, &h->prev_code_len,
683
+ &h->repeat_code_len, h->symbol_lists, h->code_length_histo,
684
+ h->next_symbol);
637
685
  }
638
686
  }
639
687
  return BROTLI_DECODER_SUCCESS;
@@ -643,9 +691,10 @@ static BrotliDecoderErrorCode SafeReadSymbolCodeLengths(
643
691
  Each code is 2..4 bits long. In total 30..72 bits are used. */
644
692
  static BrotliDecoderErrorCode ReadCodeLengthCodeLengths(BrotliDecoderState* s) {
645
693
  BrotliBitReader* br = &s->br;
646
- uint32_t num_codes = s->repeat;
647
- unsigned space = s->space;
648
- uint32_t i = s->sub_loop_counter;
694
+ BrotliMetablockHeaderArena* h = &s->arena.header;
695
+ uint32_t num_codes = h->repeat;
696
+ unsigned space = h->space;
697
+ uint32_t i = h->sub_loop_counter;
649
698
  for (; i < BROTLI_CODE_LENGTH_CODES; ++i) {
650
699
  const uint8_t code_len_idx = kCodeLengthCodeOrder[i];
651
700
  uint32_t ix;
@@ -658,23 +707,23 @@ static BrotliDecoderErrorCode ReadCodeLengthCodeLengths(BrotliDecoderState* s) {
658
707
  ix = 0;
659
708
  }
660
709
  if (kCodeLengthPrefixLength[ix] > available_bits) {
661
- s->sub_loop_counter = i;
662
- s->repeat = num_codes;
663
- s->space = space;
664
- s->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX;
710
+ h->sub_loop_counter = i;
711
+ h->repeat = num_codes;
712
+ h->space = space;
713
+ h->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX;
665
714
  return BROTLI_DECODER_NEEDS_MORE_INPUT;
666
715
  }
667
716
  }
668
717
  v = kCodeLengthPrefixValue[ix];
669
718
  BrotliDropBits(br, kCodeLengthPrefixLength[ix]);
670
- s->code_length_code_lengths[code_len_idx] = (uint8_t)v;
671
- BROTLI_LOG_ARRAY_INDEX(s->code_length_code_lengths, code_len_idx);
719
+ h->code_length_code_lengths[code_len_idx] = (uint8_t)v;
720
+ BROTLI_LOG_ARRAY_INDEX(h->code_length_code_lengths, code_len_idx);
672
721
  if (v != 0) {
673
722
  space = space - (32U >> v);
674
723
  ++num_codes;
675
- ++s->code_length_histo[v];
724
+ ++h->code_length_histo[v];
676
725
  if (space - 1U >= 32U) {
677
- /* space is 0 or wrapped around */
726
+ /* space is 0 or wrapped around. */
678
727
  break;
679
728
  }
680
729
  }
@@ -689,77 +738,78 @@ static BrotliDecoderErrorCode ReadCodeLengthCodeLengths(BrotliDecoderState* s) {
689
738
  There are 2 scenarios:
690
739
  A) Huffman code contains only few symbols (1..4). Those symbols are read
691
740
  directly; their code lengths are defined by the number of symbols.
692
- For this scenario 4 - 45 bits will be read.
741
+ For this scenario 4 - 49 bits will be read.
693
742
 
694
743
  B) 2-phase decoding:
695
744
  B.1) Small Huffman table is decoded; it is specified with code lengths
696
745
  encoded with predefined entropy code. 32 - 74 bits are used.
697
746
  B.2) Decoded table is used to decode code lengths of symbols in resulting
698
- Huffman table. In worst case 3520 bits are read.
699
- */
700
- static BrotliDecoderErrorCode ReadHuffmanCode(uint32_t alphabet_size,
747
+ Huffman table. In worst case 3520 bits are read. */
748
+ static BrotliDecoderErrorCode ReadHuffmanCode(uint32_t alphabet_size_max,
749
+ uint32_t alphabet_size_limit,
701
750
  HuffmanCode* table,
702
751
  uint32_t* opt_table_size,
703
752
  BrotliDecoderState* s) {
704
753
  BrotliBitReader* br = &s->br;
705
- /* Unnecessary masking, but might be good for safety. */
706
- alphabet_size &= 0x3ff;
707
- /* State machine */
754
+ BrotliMetablockHeaderArena* h = &s->arena.header;
755
+ /* State machine. */
708
756
  for (;;) {
709
- switch (s->substate_huffman) {
757
+ switch (h->substate_huffman) {
710
758
  case BROTLI_STATE_HUFFMAN_NONE:
711
- if (!BrotliSafeReadBits(br, 2, &s->sub_loop_counter)) {
759
+ if (!BrotliSafeReadBits(br, 2, &h->sub_loop_counter)) {
712
760
  return BROTLI_DECODER_NEEDS_MORE_INPUT;
713
761
  }
714
- BROTLI_LOG_UINT(s->sub_loop_counter);
762
+ BROTLI_LOG_UINT(h->sub_loop_counter);
715
763
  /* The value is used as follows:
716
764
  1 for simple code;
717
765
  0 for no skipping, 2 skips 2 code lengths, 3 skips 3 code lengths */
718
- if (s->sub_loop_counter != 1) {
719
- s->space = 32;
720
- s->repeat = 0; /* num_codes */
721
- memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo[0]) *
766
+ if (h->sub_loop_counter != 1) {
767
+ h->space = 32;
768
+ h->repeat = 0; /* num_codes */
769
+ memset(&h->code_length_histo[0], 0, sizeof(h->code_length_histo[0]) *
722
770
  (BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1));
723
- memset(&s->code_length_code_lengths[0], 0,
724
- sizeof(s->code_length_code_lengths));
725
- s->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX;
771
+ memset(&h->code_length_code_lengths[0], 0,
772
+ sizeof(h->code_length_code_lengths));
773
+ h->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX;
726
774
  continue;
727
775
  }
728
- /* No break, transit to the next state. */
776
+ /* Fall through. */
729
777
 
730
778
  case BROTLI_STATE_HUFFMAN_SIMPLE_SIZE:
731
779
  /* Read symbols, codes & code lengths directly. */
732
- if (!BrotliSafeReadBits(br, 2, &s->symbol)) { /* num_symbols */
733
- s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_SIZE;
780
+ if (!BrotliSafeReadBits(br, 2, &h->symbol)) { /* num_symbols */
781
+ h->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_SIZE;
734
782
  return BROTLI_DECODER_NEEDS_MORE_INPUT;
735
783
  }
736
- s->sub_loop_counter = 0;
737
- /* No break, transit to the next state. */
784
+ h->sub_loop_counter = 0;
785
+ /* Fall through. */
786
+
738
787
  case BROTLI_STATE_HUFFMAN_SIMPLE_READ: {
739
788
  BrotliDecoderErrorCode result =
740
- ReadSimpleHuffmanSymbols(alphabet_size, s);
789
+ ReadSimpleHuffmanSymbols(alphabet_size_max, alphabet_size_limit, s);
741
790
  if (result != BROTLI_DECODER_SUCCESS) {
742
791
  return result;
743
792
  }
744
- /* No break, transit to the next state. */
745
793
  }
794
+ /* Fall through. */
795
+
746
796
  case BROTLI_STATE_HUFFMAN_SIMPLE_BUILD: {
747
797
  uint32_t table_size;
748
- if (s->symbol == 3) {
798
+ if (h->symbol == 3) {
749
799
  uint32_t bits;
750
800
  if (!BrotliSafeReadBits(br, 1, &bits)) {
751
- s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_BUILD;
801
+ h->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_BUILD;
752
802
  return BROTLI_DECODER_NEEDS_MORE_INPUT;
753
803
  }
754
- s->symbol += bits;
804
+ h->symbol += bits;
755
805
  }
756
- BROTLI_LOG_UINT(s->symbol);
806
+ BROTLI_LOG_UINT(h->symbol);
757
807
  table_size = BrotliBuildSimpleHuffmanTable(
758
- table, HUFFMAN_TABLE_BITS, s->symbols_lists_array, s->symbol);
808
+ table, HUFFMAN_TABLE_BITS, h->symbols_lists_array, h->symbol);
759
809
  if (opt_table_size) {
760
810
  *opt_table_size = table_size;
761
811
  }
762
- s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
812
+ h->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
763
813
  return BROTLI_DECODER_SUCCESS;
764
814
  }
765
815
 
@@ -770,43 +820,45 @@ static BrotliDecoderErrorCode ReadHuffmanCode(uint32_t alphabet_size,
770
820
  if (result != BROTLI_DECODER_SUCCESS) {
771
821
  return result;
772
822
  }
773
- BrotliBuildCodeLengthsHuffmanTable(s->table,
774
- s->code_length_code_lengths,
775
- s->code_length_histo);
776
- memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo));
823
+ BrotliBuildCodeLengthsHuffmanTable(h->table,
824
+ h->code_length_code_lengths,
825
+ h->code_length_histo);
826
+ memset(&h->code_length_histo[0], 0, sizeof(h->code_length_histo));
777
827
  for (i = 0; i <= BROTLI_HUFFMAN_MAX_CODE_LENGTH; ++i) {
778
- s->next_symbol[i] = (int)i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
779
- s->symbol_lists[s->next_symbol[i]] = 0xFFFF;
828
+ h->next_symbol[i] = (int)i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
829
+ h->symbol_lists[h->next_symbol[i]] = 0xFFFF;
780
830
  }
781
831
 
782
- s->symbol = 0;
783
- s->prev_code_len = BROTLI_INITIAL_REPEATED_CODE_LENGTH;
784
- s->repeat = 0;
785
- s->repeat_code_len = 0;
786
- s->space = 32768;
787
- s->substate_huffman = BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS;
788
- /* No break, transit to the next state. */
832
+ h->symbol = 0;
833
+ h->prev_code_len = BROTLI_INITIAL_REPEATED_CODE_LENGTH;
834
+ h->repeat = 0;
835
+ h->repeat_code_len = 0;
836
+ h->space = 32768;
837
+ h->substate_huffman = BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS;
789
838
  }
839
+ /* Fall through. */
840
+
790
841
  case BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS: {
791
842
  uint32_t table_size;
792
- BrotliDecoderErrorCode result = ReadSymbolCodeLengths(alphabet_size, s);
843
+ BrotliDecoderErrorCode result = ReadSymbolCodeLengths(
844
+ alphabet_size_limit, s);
793
845
  if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) {
794
- result = SafeReadSymbolCodeLengths(alphabet_size, s);
846
+ result = SafeReadSymbolCodeLengths(alphabet_size_limit, s);
795
847
  }
796
848
  if (result != BROTLI_DECODER_SUCCESS) {
797
849
  return result;
798
850
  }
799
851
 
800
- if (s->space != 0) {
801
- BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", s->space));
852
+ if (h->space != 0) {
853
+ BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", (int)h->space));
802
854
  return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE);
803
855
  }
804
856
  table_size = BrotliBuildHuffmanTable(
805
- table, HUFFMAN_TABLE_BITS, s->symbol_lists, s->code_length_histo);
857
+ table, HUFFMAN_TABLE_BITS, h->symbol_lists, h->code_length_histo);
806
858
  if (opt_table_size) {
807
859
  *opt_table_size = table_size;
808
860
  }
809
- s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
861
+ h->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
810
862
  return BROTLI_DECODER_SUCCESS;
811
863
  }
812
864
 
@@ -823,8 +875,8 @@ static BROTLI_INLINE uint32_t ReadBlockLength(const HuffmanCode* table,
823
875
  uint32_t code;
824
876
  uint32_t nbits;
825
877
  code = ReadSymbol(table, br);
826
- nbits = kBlockLengthPrefixCode[code].nbits; /* nbits == 2..24 */
827
- return kBlockLengthPrefixCode[code].offset + BrotliReadBits(br, nbits);
878
+ nbits = _kBrotliPrefixCodeRanges[code].nbits; /* nbits == 2..24 */
879
+ return _kBrotliPrefixCodeRanges[code].offset + BrotliReadBits24(br, nbits);
828
880
  }
829
881
 
830
882
  /* WARNING: if state is not BROTLI_STATE_READ_BLOCK_LENGTH_NONE, then
@@ -842,13 +894,14 @@ static BROTLI_INLINE BROTLI_BOOL SafeReadBlockLength(
842
894
  }
843
895
  {
844
896
  uint32_t bits;
845
- uint32_t nbits = kBlockLengthPrefixCode[index].nbits; /* nbits == 2..24 */
897
+ uint32_t nbits = _kBrotliPrefixCodeRanges[index].nbits;
898
+ uint32_t offset = _kBrotliPrefixCodeRanges[index].offset;
846
899
  if (!BrotliSafeReadBits(br, nbits, &bits)) {
847
900
  s->block_length_index = index;
848
901
  s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX;
849
902
  return BROTLI_FALSE;
850
903
  }
851
- *result = kBlockLengthPrefixCode[index].offset + bits;
904
+ *result = offset + bits;
852
905
  s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
853
906
  return BROTLI_TRUE;
854
907
  }
@@ -867,8 +920,7 @@ static BROTLI_INLINE BROTLI_BOOL SafeReadBlockLength(
867
920
  of Y values, and reinitialize only first elements in L.
868
921
 
869
922
  Most of input values are 0 and 1. To reduce number of branches, we replace
870
- inner for loop with do-while.
871
- */
923
+ inner for loop with do-while. */
872
924
  static BROTLI_NOINLINE void InverseMoveToFrontTransform(
873
925
  uint8_t* v, uint32_t v_len, BrotliDecoderState* state) {
874
926
  /* Reinitialize elements that could have been changed. */
@@ -884,7 +936,7 @@ static BROTLI_NOINLINE void InverseMoveToFrontTransform(
884
936
  /* Initialize list using 4 consequent values pattern. */
885
937
  mtf[0] = pattern;
886
938
  do {
887
- pattern += 0x04040404; /* Advance all 4 values by 4. */
939
+ pattern += 0x04040404; /* Advance all 4 values by 4. */
888
940
  mtf[i] = pattern;
889
941
  i++;
890
942
  } while (i <= upper_bound);
@@ -909,21 +961,22 @@ static BROTLI_NOINLINE void InverseMoveToFrontTransform(
909
961
  /* Decodes a series of Huffman table using ReadHuffmanCode function. */
910
962
  static BrotliDecoderErrorCode HuffmanTreeGroupDecode(
911
963
  HuffmanTreeGroup* group, BrotliDecoderState* s) {
912
- if (s->substate_tree_group != BROTLI_STATE_TREE_GROUP_LOOP) {
913
- s->next = group->codes;
914
- s->htree_index = 0;
915
- s->substate_tree_group = BROTLI_STATE_TREE_GROUP_LOOP;
964
+ BrotliMetablockHeaderArena* h = &s->arena.header;
965
+ if (h->substate_tree_group != BROTLI_STATE_TREE_GROUP_LOOP) {
966
+ h->next = group->codes;
967
+ h->htree_index = 0;
968
+ h->substate_tree_group = BROTLI_STATE_TREE_GROUP_LOOP;
916
969
  }
917
- while (s->htree_index < group->num_htrees) {
970
+ while (h->htree_index < group->num_htrees) {
918
971
  uint32_t table_size;
919
- BrotliDecoderErrorCode result =
920
- ReadHuffmanCode(group->alphabet_size, s->next, &table_size, s);
972
+ BrotliDecoderErrorCode result = ReadHuffmanCode(group->alphabet_size_max,
973
+ group->alphabet_size_limit, h->next, &table_size, s);
921
974
  if (result != BROTLI_DECODER_SUCCESS) return result;
922
- group->htrees[s->htree_index] = s->next;
923
- s->next += table_size;
924
- ++s->htree_index;
975
+ group->htrees[h->htree_index] = h->next;
976
+ h->next += table_size;
977
+ ++h->htree_index;
925
978
  }
926
- s->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;
979
+ h->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;
927
980
  return BROTLI_DECODER_SUCCESS;
928
981
  }
929
982
 
@@ -934,26 +987,27 @@ static BrotliDecoderErrorCode HuffmanTreeGroupDecode(
934
987
  2) Decode Huffman table using ReadHuffmanCode function.
935
988
  This table will be used for reading context map items.
936
989
  3) Read context map items; "0" values could be run-length encoded.
937
- 4) Optionally, apply InverseMoveToFront transform to the resulting map.
938
- */
990
+ 4) Optionally, apply InverseMoveToFront transform to the resulting map. */
939
991
  static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size,
940
992
  uint32_t* num_htrees,
941
993
  uint8_t** context_map_arg,
942
994
  BrotliDecoderState* s) {
943
995
  BrotliBitReader* br = &s->br;
944
996
  BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS;
997
+ BrotliMetablockHeaderArena* h = &s->arena.header;
945
998
 
946
- switch ((int)s->substate_context_map) {
999
+ switch ((int)h->substate_context_map) {
947
1000
  case BROTLI_STATE_CONTEXT_MAP_NONE:
948
1001
  result = DecodeVarLenUint8(s, br, num_htrees);
949
1002
  if (result != BROTLI_DECODER_SUCCESS) {
950
1003
  return result;
951
1004
  }
952
1005
  (*num_htrees)++;
953
- s->context_index = 0;
1006
+ h->context_index = 0;
954
1007
  BROTLI_LOG_UINT(context_map_size);
955
1008
  BROTLI_LOG_UINT(*num_htrees);
956
- *context_map_arg = (uint8_t*)BROTLI_ALLOC(s, (size_t)context_map_size);
1009
+ *context_map_arg =
1010
+ (uint8_t*)BROTLI_DECODER_ALLOC(s, (size_t)context_map_size);
957
1011
  if (*context_map_arg == 0) {
958
1012
  return BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP);
959
1013
  }
@@ -961,8 +1015,9 @@ static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size,
961
1015
  memset(*context_map_arg, 0, (size_t)context_map_size);
962
1016
  return BROTLI_DECODER_SUCCESS;
963
1017
  }
964
- s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_READ_PREFIX;
965
- /* No break, continue to next state. */
1018
+ h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_READ_PREFIX;
1019
+ /* Fall through. */
1020
+
966
1021
  case BROTLI_STATE_CONTEXT_MAP_READ_PREFIX: {
967
1022
  uint32_t bits;
968
1023
  /* In next stage ReadHuffmanCode uses at least 4 bits, so it is safe
@@ -971,34 +1026,38 @@ static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size,
971
1026
  return BROTLI_DECODER_NEEDS_MORE_INPUT;
972
1027
  }
973
1028
  if ((bits & 1) != 0) { /* Use RLE for zeros. */
974
- s->max_run_length_prefix = (bits >> 1) + 1;
1029
+ h->max_run_length_prefix = (bits >> 1) + 1;
975
1030
  BrotliDropBits(br, 5);
976
1031
  } else {
977
- s->max_run_length_prefix = 0;
1032
+ h->max_run_length_prefix = 0;
978
1033
  BrotliDropBits(br, 1);
979
1034
  }
980
- BROTLI_LOG_UINT(s->max_run_length_prefix);
981
- s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_HUFFMAN;
982
- /* No break, continue to next state. */
1035
+ BROTLI_LOG_UINT(h->max_run_length_prefix);
1036
+ h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_HUFFMAN;
983
1037
  }
984
- case BROTLI_STATE_CONTEXT_MAP_HUFFMAN:
985
- result = ReadHuffmanCode(*num_htrees + s->max_run_length_prefix,
986
- s->context_map_table, NULL, s);
1038
+ /* Fall through. */
1039
+
1040
+ case BROTLI_STATE_CONTEXT_MAP_HUFFMAN: {
1041
+ uint32_t alphabet_size = *num_htrees + h->max_run_length_prefix;
1042
+ result = ReadHuffmanCode(alphabet_size, alphabet_size,
1043
+ h->context_map_table, NULL, s);
987
1044
  if (result != BROTLI_DECODER_SUCCESS) return result;
988
- s->code = 0xFFFF;
989
- s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_DECODE;
990
- /* No break, continue to next state. */
1045
+ h->code = 0xFFFF;
1046
+ h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_DECODE;
1047
+ }
1048
+ /* Fall through. */
1049
+
991
1050
  case BROTLI_STATE_CONTEXT_MAP_DECODE: {
992
- uint32_t context_index = s->context_index;
993
- uint32_t max_run_length_prefix = s->max_run_length_prefix;
1051
+ uint32_t context_index = h->context_index;
1052
+ uint32_t max_run_length_prefix = h->max_run_length_prefix;
994
1053
  uint8_t* context_map = *context_map_arg;
995
- uint32_t code = s->code;
1054
+ uint32_t code = h->code;
996
1055
  BROTLI_BOOL skip_preamble = (code != 0xFFFF);
997
1056
  while (context_index < context_map_size || skip_preamble) {
998
1057
  if (!skip_preamble) {
999
- if (!SafeReadSymbol(s->context_map_table, br, &code)) {
1000
- s->code = 0xFFFF;
1001
- s->context_index = context_index;
1058
+ if (!SafeReadSymbol(h->context_map_table, br, &code)) {
1059
+ h->code = 0xFFFF;
1060
+ h->context_index = context_index;
1002
1061
  return BROTLI_DECODER_NEEDS_MORE_INPUT;
1003
1062
  }
1004
1063
  BROTLI_LOG_UINT(code);
@@ -1019,8 +1078,8 @@ static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size,
1019
1078
  {
1020
1079
  uint32_t reps;
1021
1080
  if (!BrotliSafeReadBits(br, code, &reps)) {
1022
- s->code = code;
1023
- s->context_index = context_index;
1081
+ h->code = code;
1082
+ h->context_index = context_index;
1024
1083
  return BROTLI_DECODER_NEEDS_MORE_INPUT;
1025
1084
  }
1026
1085
  reps += 1U << code;
@@ -1034,20 +1093,22 @@ static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size,
1034
1093
  } while (--reps);
1035
1094
  }
1036
1095
  }
1037
- /* No break, continue to next state. */
1038
1096
  }
1097
+ /* Fall through. */
1098
+
1039
1099
  case BROTLI_STATE_CONTEXT_MAP_TRANSFORM: {
1040
1100
  uint32_t bits;
1041
1101
  if (!BrotliSafeReadBits(br, 1, &bits)) {
1042
- s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_TRANSFORM;
1102
+ h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_TRANSFORM;
1043
1103
  return BROTLI_DECODER_NEEDS_MORE_INPUT;
1044
1104
  }
1045
1105
  if (bits != 0) {
1046
1106
  InverseMoveToFrontTransform(*context_map_arg, context_map_size, s);
1047
1107
  }
1048
- s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;
1108
+ h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;
1049
1109
  return BROTLI_DECODER_SUCCESS;
1050
1110
  }
1111
+
1051
1112
  default:
1052
1113
  return
1053
1114
  BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE);
@@ -1066,8 +1127,11 @@ static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength(
1066
1127
  BrotliBitReader* br = &s->br;
1067
1128
  uint32_t* ringbuffer = &s->block_type_rb[tree_type * 2];
1068
1129
  uint32_t block_type;
1130
+ if (max_block_type <= 1) {
1131
+ return BROTLI_FALSE;
1132
+ }
1069
1133
 
1070
- /* Read 0..15 + 3..39 bits */
1134
+ /* Read 0..15 + 3..39 bits. */
1071
1135
  if (!safe) {
1072
1136
  block_type = ReadSymbol(type_tree, br);
1073
1137
  s->block_length[tree_type] = ReadBlockLength(len_tree, br);
@@ -1124,9 +1188,8 @@ static BROTLI_INLINE void PrepareLiteralDecoding(BrotliDecoderState* s) {
1124
1188
  trivial = s->trivial_literal_contexts[block_type >> 5];
1125
1189
  s->trivial_literal_context = (trivial >> (block_type & 31)) & 1;
1126
1190
  s->literal_htree = s->literal_hgroup.htrees[s->context_map_slice[0]];
1127
- context_mode = s->context_modes[block_type];
1128
- s->context_lookup1 = &kContextLookup[kContextLookupOffsets[context_mode]];
1129
- s->context_lookup2 = &kContextLookup[kContextLookupOffsets[context_mode + 1]];
1191
+ context_mode = s->context_modes[block_type] & 3;
1192
+ s->context_lookup = BROTLI_CONTEXT_LUT(context_mode);
1130
1193
  }
1131
1194
 
1132
1195
  /* Decodes the block type and updates the state for literal context.
@@ -1163,6 +1226,7 @@ static BROTLI_INLINE BROTLI_BOOL DecodeCommandBlockSwitchInternal(
1163
1226
  static void BROTLI_NOINLINE DecodeCommandBlockSwitch(BrotliDecoderState* s) {
1164
1227
  DecodeCommandBlockSwitchInternal(0, s);
1165
1228
  }
1229
+
1166
1230
  static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeCommandBlockSwitch(
1167
1231
  BrotliDecoderState* s) {
1168
1232
  return DecodeCommandBlockSwitchInternal(1, s);
@@ -1199,8 +1263,7 @@ static size_t UnwrittenBytes(const BrotliDecoderState* s, BROTLI_BOOL wrap) {
1199
1263
 
1200
1264
  /* Dumps output.
1201
1265
  Returns BROTLI_DECODER_NEEDS_MORE_OUTPUT only if there is more output to push
1202
- and either ring-buffer is as big as window size, or |force| is true.
1203
- */
1266
+ and either ring-buffer is as big as window size, or |force| is true. */
1204
1267
  static BrotliDecoderErrorCode BROTLI_NOINLINE WriteRingBuffer(
1205
1268
  BrotliDecoderState* s, size_t* available_out, uint8_t** next_out,
1206
1269
  size_t* total_out, BROTLI_BOOL force) {
@@ -1259,8 +1322,7 @@ static void BROTLI_NOINLINE WrapRingBuffer(BrotliDecoderState* s) {
1259
1322
  this function is called.
1260
1323
 
1261
1324
  Last two bytes of ring-buffer are initialized to 0, so context calculation
1262
- could be done uniformly for the first two and all other positions.
1263
- */
1325
+ could be done uniformly for the first two and all other positions. */
1264
1326
  static BROTLI_BOOL BROTLI_NOINLINE BrotliEnsureRingBuffer(
1265
1327
  BrotliDecoderState* s) {
1266
1328
  uint8_t* old_ringbuffer = s->ringbuffer;
@@ -1268,8 +1330,8 @@ static BROTLI_BOOL BROTLI_NOINLINE BrotliEnsureRingBuffer(
1268
1330
  return BROTLI_TRUE;
1269
1331
  }
1270
1332
 
1271
- s->ringbuffer = (uint8_t*)BROTLI_ALLOC(s, (size_t)(s->new_ringbuffer_size) +
1272
- kRingBufferWriteAheadSlack);
1333
+ s->ringbuffer = (uint8_t*)BROTLI_DECODER_ALLOC(s,
1334
+ (size_t)(s->new_ringbuffer_size) + kRingBufferWriteAheadSlack);
1273
1335
  if (s->ringbuffer == 0) {
1274
1336
  /* Restore previous value. */
1275
1337
  s->ringbuffer = old_ringbuffer;
@@ -1280,7 +1342,7 @@ static BROTLI_BOOL BROTLI_NOINLINE BrotliEnsureRingBuffer(
1280
1342
 
1281
1343
  if (!!old_ringbuffer) {
1282
1344
  memcpy(s->ringbuffer, old_ringbuffer, (size_t)s->pos);
1283
- BROTLI_FREE(s, old_ringbuffer);
1345
+ BROTLI_DECODER_FREE(s, old_ringbuffer);
1284
1346
  }
1285
1347
 
1286
1348
  s->ringbuffer_size = s->new_ringbuffer_size;
@@ -1320,8 +1382,9 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE CopyUncompressedBlockToOutput(
1320
1382
  return BROTLI_DECODER_NEEDS_MORE_INPUT;
1321
1383
  }
1322
1384
  s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_WRITE;
1323
- /* No break, continue to next state */
1324
1385
  }
1386
+ /* Fall through. */
1387
+
1325
1388
  case BROTLI_STATE_UNCOMPRESSED_WRITE: {
1326
1389
  BrotliDecoderErrorCode result;
1327
1390
  result = WriteRingBuffer(
@@ -1345,8 +1408,7 @@ static BrotliDecoderErrorCode BROTLI_NOINLINE CopyUncompressedBlockToOutput(
1345
1408
  If we know the data size is small, do not allocate more ring buffer
1346
1409
  size than needed to reduce memory usage.
1347
1410
 
1348
- When this method is called, metablock size and flags MUST be decoded.
1349
- */
1411
+ When this method is called, metablock size and flags MUST be decoded. */
1350
1412
  static void BROTLI_NOINLINE BrotliCalculateRingBufferSize(
1351
1413
  BrotliDecoderState* s) {
1352
1414
  int window_size = 1 << s->window_bits;
@@ -1377,7 +1439,7 @@ static void BROTLI_NOINLINE BrotliCalculateRingBufferSize(
1377
1439
  if (!!s->canny_ringbuffer_allocation) {
1378
1440
  /* Reduce ring buffer size to save memory when server is unscrupulous.
1379
1441
  In worst case memory usage might be 1.5x bigger for a short period of
1380
- ring buffer reallocation.*/
1442
+ ring buffer reallocation. */
1381
1443
  while ((new_ringbuffer_size >> 1) >= min_size) {
1382
1444
  new_ringbuffer_size >>= 1;
1383
1445
  }
@@ -1397,7 +1459,7 @@ static BrotliDecoderErrorCode ReadContextModes(BrotliDecoderState* s) {
1397
1459
  s->loop_counter = i;
1398
1460
  return BROTLI_DECODER_NEEDS_MORE_INPUT;
1399
1461
  }
1400
- s->context_modes[i] = (uint8_t)(bits << 1);
1462
+ s->context_modes[i] = (uint8_t)bits;
1401
1463
  BROTLI_LOG_ARRAY_INDEX(s->context_modes, i);
1402
1464
  i++;
1403
1465
  }
@@ -1405,32 +1467,28 @@ static BrotliDecoderErrorCode ReadContextModes(BrotliDecoderState* s) {
1405
1467
  }
1406
1468
 
1407
1469
  static BROTLI_INLINE void TakeDistanceFromRingBuffer(BrotliDecoderState* s) {
1408
- if (s->distance_code == 0) {
1409
- --s->dist_rb_idx;
1410
- s->distance_code = s->dist_rb[s->dist_rb_idx & 3];
1470
+ int offset = s->distance_code - 3;
1471
+ if (s->distance_code <= 3) {
1411
1472
  /* Compensate double distance-ring-buffer roll for dictionary items. */
1412
- s->distance_context = 1;
1473
+ s->distance_context = 1 >> s->distance_code;
1474
+ s->distance_code = s->dist_rb[(s->dist_rb_idx - offset) & 3];
1475
+ s->dist_rb_idx -= s->distance_context;
1413
1476
  } else {
1414
- int distance_code = s->distance_code << 1;
1415
- /* kDistanceShortCodeIndexOffset has 2-bit values from LSB: */
1416
- /* 3, 2, 1, 0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2 */
1417
- const uint32_t kDistanceShortCodeIndexOffset = 0xaaafff1b;
1418
- /* kDistanceShortCodeValueOffset has 2-bit values from LSB: */
1419
- /*-0, 0,-0, 0,-1, 1,-2, 2,-3, 3,-1, 1,-2, 2,-3, 3 */
1420
- const uint32_t kDistanceShortCodeValueOffset = 0xfa5fa500;
1421
- int v = (s->dist_rb_idx +
1422
- (int)(kDistanceShortCodeIndexOffset >> distance_code)) & 0x3;
1423
- s->distance_code = s->dist_rb[v];
1424
- v = (int)(kDistanceShortCodeValueOffset >> distance_code) & 0x3;
1425
- if ((distance_code & 0x3) != 0) {
1426
- s->distance_code += v;
1477
+ int index_delta = 3;
1478
+ int delta;
1479
+ int base = s->distance_code - 10;
1480
+ if (s->distance_code < 10) {
1481
+ base = s->distance_code - 4;
1427
1482
  } else {
1428
- s->distance_code -= v;
1429
- if (s->distance_code <= 0) {
1430
- /* A huge distance will cause a BROTLI_FAILURE() soon. */
1431
- /* This is a little faster than failing here. */
1432
- s->distance_code = 0x0fffffff;
1433
- }
1483
+ index_delta = 2;
1484
+ }
1485
+ /* Unpack one of six 4-bit values. */
1486
+ delta = ((0x605142 >> (4 * base)) & 0xF) - 3;
1487
+ s->distance_code = s->dist_rb[(s->dist_rb_idx + index_delta) & 0x3] + delta;
1488
+ if (s->distance_code <= 0) {
1489
+ /* A huge distance will cause a BROTLI_FAILURE() soon.
1490
+ This is a little faster than failing here. */
1491
+ s->distance_code = 0x7FFFFFFF;
1434
1492
  }
1435
1493
  }
1436
1494
  }
@@ -1445,62 +1503,153 @@ static BROTLI_INLINE BROTLI_BOOL SafeReadBits(
1445
1503
  }
1446
1504
  }
1447
1505
 
1448
- /* Precondition: s->distance_code < 0 */
1506
+ static BROTLI_INLINE BROTLI_BOOL SafeReadBits32(
1507
+ BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
1508
+ if (n_bits != 0) {
1509
+ return BrotliSafeReadBits32(br, n_bits, val);
1510
+ } else {
1511
+ *val = 0;
1512
+ return BROTLI_TRUE;
1513
+ }
1514
+ }
1515
+
1516
+ /*
1517
+ RFC 7932 Section 4 with "..." shortenings and "[]" emendations.
1518
+
1519
+ Each distance ... is represented with a pair <distance code, extra bits>...
1520
+ The distance code is encoded using a prefix code... The number of extra bits
1521
+ can be 0..24... Two additional parameters: NPOSTFIX (0..3), and ...
1522
+ NDIRECT (0..120) ... are encoded in the meta-block header...
1523
+
1524
+ The first 16 distance symbols ... reference past distances... ring buffer ...
1525
+ Next NDIRECT distance symbols ... represent distances from 1 to NDIRECT...
1526
+ [For] distance symbols 16 + NDIRECT and greater ... the number of extra bits
1527
+ ... is given by the following formula:
1528
+
1529
+ [ xcode = dcode - NDIRECT - 16 ]
1530
+ ndistbits = 1 + [ xcode ] >> (NPOSTFIX + 1)
1531
+
1532
+ ...
1533
+ */
1534
+
1535
+ /*
1536
+ RFC 7932 Section 9.2 with "..." shortenings and "[]" emendations.
1537
+
1538
+ ... to get the actual value of the parameter NDIRECT, left-shift this
1539
+ four-bit number by NPOSTFIX bits ...
1540
+ */
1541
+
1542
+ /* Remaining formulas from RFC 7932 Section 4 could be rewritten as following:
1543
+
1544
+ alphabet_size = 16 + NDIRECT + (max_distbits << (NPOSTFIX + 1))
1545
+
1546
+ half = ((xcode >> NPOSTFIX) & 1) << ndistbits
1547
+ postfix = xcode & ((1 << NPOSTFIX) - 1)
1548
+ range_start = 2 * (1 << ndistbits - 1 - 1)
1549
+
1550
+ distance = (range_start + half + extra) << NPOSTFIX + postfix + NDIRECT + 1
1551
+
1552
+ NB: ndistbits >= 1 -> range_start >= 0
1553
+ NB: range_start has factor 2, as the range is covered by 2 "halves"
1554
+ NB: extra -1 offset in range_start formula covers the absence of
1555
+ ndistbits = 0 case
1556
+ NB: when NPOSTFIX = 0, NDIRECT is not greater than 15
1557
+
1558
+ In other words, xcode has the following binary structure - XXXHPPP:
1559
+ - XXX represent the number of extra distance bits
1560
+ - H selects upper / lower range of distances
1561
+ - PPP represent "postfix"
1562
+
1563
+ "Regular" distance encoding has NPOSTFIX = 0; omitting the postfix part
1564
+ simplifies distance calculation.
1565
+
1566
+ Using NPOSTFIX > 0 allows cheaper encoding of regular structures, e.g. where
1567
+ most of distances have the same reminder of division by 2/4/8. For example,
1568
+ the table of int32_t values that come from different sources; if it is likely
1569
+ that 3 highest bytes of values from the same source are the same, then
1570
+ copy distance often looks like 4x + y.
1571
+
1572
+ Distance calculation could be rewritten to:
1573
+
1574
+ ndistbits = NDISTBITS(NDIRECT, NPOSTFIX)[dcode]
1575
+ distance = OFFSET(NDIRECT, NPOSTFIX)[dcode] + extra << NPOSTFIX
1576
+
1577
+ NDISTBITS and OFFSET could be pre-calculated, as NDIRECT and NPOSTFIX could
1578
+ change only once per meta-block.
1579
+ */
1580
+
1581
+ /* Calculates distance lookup table.
1582
+ NB: it is possible to have all 64 tables precalculated. */
1583
+ static void CalculateDistanceLut(BrotliDecoderState* s) {
1584
+ BrotliMetablockBodyArena* b = &s->arena.body;
1585
+ uint32_t npostfix = s->distance_postfix_bits;
1586
+ uint32_t ndirect = s->num_direct_distance_codes;
1587
+ uint32_t alphabet_size_limit = s->distance_hgroup.alphabet_size_limit;
1588
+ uint32_t postfix = 1u << npostfix;
1589
+ uint32_t j;
1590
+ uint32_t bits = 1;
1591
+ uint32_t half = 0;
1592
+
1593
+ /* Skip short codes. */
1594
+ uint32_t i = BROTLI_NUM_DISTANCE_SHORT_CODES;
1595
+
1596
+ /* Fill direct codes. */
1597
+ for (j = 0; j < ndirect; ++j) {
1598
+ b->dist_extra_bits[i] = 0;
1599
+ b->dist_offset[i] = j + 1;
1600
+ ++i;
1601
+ }
1602
+
1603
+ /* Fill regular distance codes. */
1604
+ while (i < alphabet_size_limit) {
1605
+ uint32_t base = ndirect + ((((2 + half) << bits) - 4) << npostfix) + 1;
1606
+ /* Always fill the complete group. */
1607
+ for (j = 0; j < postfix; ++j) {
1608
+ b->dist_extra_bits[i] = (uint8_t)bits;
1609
+ b->dist_offset[i] = base + j;
1610
+ ++i;
1611
+ }
1612
+ bits = bits + half;
1613
+ half = half ^ 1;
1614
+ }
1615
+ }
1616
+
1617
+ /* Precondition: s->distance_code < 0. */
1449
1618
  static BROTLI_INLINE BROTLI_BOOL ReadDistanceInternal(
1450
1619
  int safe, BrotliDecoderState* s, BrotliBitReader* br) {
1451
- int distval;
1620
+ BrotliMetablockBodyArena* b = &s->arena.body;
1621
+ uint32_t code;
1622
+ uint32_t bits;
1452
1623
  BrotliBitReaderState memento;
1453
1624
  HuffmanCode* distance_tree = s->distance_hgroup.htrees[s->dist_htree_index];
1454
1625
  if (!safe) {
1455
- s->distance_code = (int)ReadSymbol(distance_tree, br);
1626
+ code = ReadSymbol(distance_tree, br);
1456
1627
  } else {
1457
- uint32_t code;
1458
1628
  BrotliBitReaderSaveState(br, &memento);
1459
1629
  if (!SafeReadSymbol(distance_tree, br, &code)) {
1460
1630
  return BROTLI_FALSE;
1461
1631
  }
1462
- s->distance_code = (int)code;
1463
1632
  }
1464
- /* Convert the distance code to the actual distance by possibly */
1465
- /* looking up past distances from the s->ringbuffer. */
1633
+ --s->block_length[2];
1634
+ /* Convert the distance code to the actual distance by possibly
1635
+ looking up past distances from the s->dist_rb. */
1466
1636
  s->distance_context = 0;
1467
- if ((s->distance_code & ~0xf) == 0) {
1637
+ if ((code & ~0xFu) == 0) {
1638
+ s->distance_code = (int)code;
1468
1639
  TakeDistanceFromRingBuffer(s);
1469
- --s->block_length[2];
1470
1640
  return BROTLI_TRUE;
1471
1641
  }
1472
- distval = s->distance_code - (int)s->num_direct_distance_codes;
1473
- if (distval >= 0) {
1474
- uint32_t nbits;
1475
- int postfix;
1476
- int offset;
1477
- if (!safe && (s->distance_postfix_bits == 0)) {
1478
- nbits = ((uint32_t)distval >> 1) + 1;
1479
- offset = ((2 + (distval & 1)) << nbits) - 4;
1480
- s->distance_code = (int)s->num_direct_distance_codes + offset +
1481
- (int)BrotliReadBits(br, nbits);
1482
- } else {
1483
- /* This branch also works well when s->distance_postfix_bits == 0 */
1484
- uint32_t bits;
1485
- postfix = distval & s->distance_postfix_mask;
1486
- distval >>= s->distance_postfix_bits;
1487
- nbits = ((uint32_t)distval >> 1) + 1;
1488
- if (safe) {
1489
- if (!SafeReadBits(br, nbits, &bits)) {
1490
- s->distance_code = -1; /* Restore precondition. */
1491
- BrotliBitReaderRestoreState(br, &memento);
1492
- return BROTLI_FALSE;
1493
- }
1494
- } else {
1495
- bits = BrotliReadBits(br, nbits);
1496
- }
1497
- offset = ((2 + (distval & 1)) << nbits) - 4;
1498
- s->distance_code = (int)s->num_direct_distance_codes +
1499
- ((offset + (int)bits) << s->distance_postfix_bits) + postfix;
1642
+ if (!safe) {
1643
+ bits = BrotliReadBits32(br, b->dist_extra_bits[code]);
1644
+ } else {
1645
+ if (!SafeReadBits32(br, b->dist_extra_bits[code], &bits)) {
1646
+ ++s->block_length[2];
1647
+ BrotliBitReaderRestoreState(br, &memento);
1648
+ return BROTLI_FALSE;
1500
1649
  }
1501
1650
  }
1502
- s->distance_code = s->distance_code - BROTLI_NUM_DISTANCE_SHORT_CODES + 1;
1503
- --s->block_length[2];
1651
+ s->distance_code =
1652
+ (int)(b->dist_offset[code] + (bits << s->distance_postfix_bits));
1504
1653
  return BROTLI_TRUE;
1505
1654
  }
1506
1655
 
@@ -1536,9 +1685,9 @@ static BROTLI_INLINE BROTLI_BOOL ReadCommandInternal(
1536
1685
  *insert_length = v.insert_len_offset;
1537
1686
  if (!safe) {
1538
1687
  if (BROTLI_PREDICT_FALSE(v.insert_len_extra_bits != 0)) {
1539
- insert_len_extra = BrotliReadBits(br, v.insert_len_extra_bits);
1688
+ insert_len_extra = BrotliReadBits24(br, v.insert_len_extra_bits);
1540
1689
  }
1541
- copy_length = BrotliReadBits(br, v.copy_len_extra_bits);
1690
+ copy_length = BrotliReadBits24(br, v.copy_len_extra_bits);
1542
1691
  } else {
1543
1692
  if (!SafeReadBits(br, v.insert_len_extra_bits, &insert_len_extra) ||
1544
1693
  !SafeReadBits(br, v.copy_len_extra_bits, &copy_length)) {
@@ -1614,7 +1763,7 @@ CommandBegin:
1614
1763
  if (safe) {
1615
1764
  s->state = BROTLI_STATE_COMMAND_BEGIN;
1616
1765
  }
1617
- if (!CheckInputAmount(safe, br, 28)) { /* 156 bits + 7 bytes */
1766
+ if (!CheckInputAmount(safe, br, 28)) { /* 156 bits + 7 bytes */
1618
1767
  s->state = BROTLI_STATE_COMMAND_BEGIN;
1619
1768
  result = BROTLI_DECODER_NEEDS_MORE_INPUT;
1620
1769
  goto saveStateAndReturn;
@@ -1623,7 +1772,7 @@ CommandBegin:
1623
1772
  BROTLI_SAFE(DecodeCommandBlockSwitch(s));
1624
1773
  goto CommandBegin;
1625
1774
  }
1626
- /* Read the insert/copy length in the command */
1775
+ /* Read the insert/copy length in the command. */
1627
1776
  BROTLI_SAFE(ReadCommand(s, br, &i));
1628
1777
  BROTLI_LOG(("[ProcessCommandsInternal] pos = %d insert = %d copy = %d\n",
1629
1778
  pos, i, s->copy_length));
@@ -1636,13 +1785,13 @@ CommandInner:
1636
1785
  if (safe) {
1637
1786
  s->state = BROTLI_STATE_COMMAND_INNER;
1638
1787
  }
1639
- /* Read the literals in the command */
1788
+ /* Read the literals in the command. */
1640
1789
  if (s->trivial_literal_context) {
1641
1790
  uint32_t bits;
1642
1791
  uint32_t value;
1643
1792
  PreloadSymbol(safe, s->literal_htree, br, &bits, &value);
1644
1793
  do {
1645
- if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */
1794
+ if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */
1646
1795
  s->state = BROTLI_STATE_COMMAND_INNER;
1647
1796
  result = BROTLI_DECODER_NEEDS_MORE_INPUT;
1648
1797
  goto saveStateAndReturn;
@@ -1678,7 +1827,7 @@ CommandInner:
1678
1827
  do {
1679
1828
  const HuffmanCode* hc;
1680
1829
  uint8_t context;
1681
- if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */
1830
+ if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */
1682
1831
  s->state = BROTLI_STATE_COMMAND_INNER;
1683
1832
  result = BROTLI_DECODER_NEEDS_MORE_INPUT;
1684
1833
  goto saveStateAndReturn;
@@ -1687,7 +1836,7 @@ CommandInner:
1687
1836
  BROTLI_SAFE(DecodeLiteralBlockSwitch(s));
1688
1837
  if (s->trivial_literal_context) goto CommandInner;
1689
1838
  }
1690
- context = s->context_lookup1[p1] | s->context_lookup2[p2];
1839
+ context = BROTLI_CONTEXT(p1, p2, s->context_lookup);
1691
1840
  BROTLI_LOG_UINT(context);
1692
1841
  hc = s->literal_hgroup.htrees[s->context_map_slice[context]];
1693
1842
  p2 = p1;
@@ -1743,32 +1892,44 @@ CommandPostDecodeLiterals:
1743
1892
  }
1744
1893
  i = s->copy_length;
1745
1894
  /* Apply copy of LZ77 back-reference, or static dictionary reference if
1746
- the distance is larger than the max LZ77 distance */
1895
+ the distance is larger than the max LZ77 distance */
1747
1896
  if (s->distance_code > s->max_distance) {
1748
- int address = s->distance_code - s->max_distance - 1;
1897
+ /* The maximum allowed distance is BROTLI_MAX_ALLOWED_DISTANCE = 0x7FFFFFFC.
1898
+ With this choice, no signed overflow can occur after decoding
1899
+ a special distance code (e.g., after adding 3 to the last distance). */
1900
+ if (s->distance_code > BROTLI_MAX_ALLOWED_DISTANCE) {
1901
+ BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
1902
+ "len: %d bytes left: %d\n",
1903
+ pos, s->distance_code, i, s->meta_block_remaining_len));
1904
+ return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_DISTANCE);
1905
+ }
1749
1906
  if (i >= BROTLI_MIN_DICTIONARY_WORD_LENGTH &&
1750
1907
  i <= BROTLI_MAX_DICTIONARY_WORD_LENGTH) {
1908
+ int address = s->distance_code - s->max_distance - 1;
1909
+ const BrotliDictionary* words = s->dictionary;
1910
+ const BrotliTransforms* transforms = s->transforms;
1751
1911
  int offset = (int)s->dictionary->offsets_by_length[i];
1752
1912
  uint32_t shift = s->dictionary->size_bits_by_length[i];
1913
+
1753
1914
  int mask = (int)BitMask(shift);
1754
1915
  int word_idx = address & mask;
1755
1916
  int transform_idx = address >> shift;
1756
1917
  /* Compensate double distance-ring-buffer roll. */
1757
1918
  s->dist_rb_idx += s->distance_context;
1758
1919
  offset += word_idx * i;
1759
- if (BROTLI_PREDICT_FALSE(!s->dictionary->data)) {
1920
+ if (BROTLI_PREDICT_FALSE(!words->data)) {
1760
1921
  return BROTLI_FAILURE(BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET);
1761
1922
  }
1762
- if (transform_idx < kNumTransforms) {
1763
- const uint8_t* word = &s->dictionary->data[offset];
1923
+ if (transform_idx < (int)transforms->num_transforms) {
1924
+ const uint8_t* word = &words->data[offset];
1764
1925
  int len = i;
1765
- if (transform_idx == 0) {
1926
+ if (transform_idx == transforms->cutOffTransforms[0]) {
1766
1927
  memcpy(&s->ringbuffer[pos], word, (size_t)len);
1767
1928
  BROTLI_LOG(("[ProcessCommandsInternal] dictionary word: [%.*s]\n",
1768
1929
  len, word));
1769
1930
  } else {
1770
- len = TransformDictionaryWord(
1771
- &s->ringbuffer[pos], word, len, transform_idx);
1931
+ len = BrotliTransformDictionaryWord(&s->ringbuffer[pos], word, len,
1932
+ transforms, transform_idx);
1772
1933
  BROTLI_LOG(("[ProcessCommandsInternal] dictionary word: [%.*s],"
1773
1934
  " transform_idx = %d, transformed: [%.*s]\n",
1774
1935
  i, word, transform_idx, len, &s->ringbuffer[pos]));
@@ -1776,7 +1937,6 @@ CommandPostDecodeLiterals:
1776
1937
  pos += len;
1777
1938
  s->meta_block_remaining_len -= len;
1778
1939
  if (pos >= s->ringbuffer_size) {
1779
- /*s->partial_pos_rb += (size_t)s->ringbuffer_size;*/
1780
1940
  s->state = BROTLI_STATE_COMMAND_POST_WRITE_1;
1781
1941
  goto saveStateAndReturn;
1782
1942
  }
@@ -1798,14 +1958,13 @@ CommandPostDecodeLiterals:
1798
1958
  uint8_t* copy_src = &s->ringbuffer[src_start];
1799
1959
  int dst_end = pos + i;
1800
1960
  int src_end = src_start + i;
1801
- /* update the recent distances cache */
1961
+ /* Update the recent distances cache. */
1802
1962
  s->dist_rb[s->dist_rb_idx & 3] = s->distance_code;
1803
1963
  ++s->dist_rb_idx;
1804
1964
  s->meta_block_remaining_len -= i;
1805
1965
  /* There are 32+ bytes of slack in the ring-buffer allocation.
1806
1966
  Also, we have 16 short codes, that make these 16 bytes irrelevant
1807
- in the ring-buffer. Let's copy over them as a first guess.
1808
- */
1967
+ in the ring-buffer. Let's copy over them as a first guess. */
1809
1968
  memmove16(copy_dst, copy_src);
1810
1969
  if (src_end > pos && dst_end > src_start) {
1811
1970
  /* Regions intersect. */
@@ -1828,7 +1987,7 @@ CommandPostDecodeLiterals:
1828
1987
  }
1829
1988
  BROTLI_LOG_UINT(s->meta_block_remaining_len);
1830
1989
  if (s->meta_block_remaining_len <= 0) {
1831
- /* Next metablock, if any */
1990
+ /* Next metablock, if any. */
1832
1991
  s->state = BROTLI_STATE_METABLOCK_DONE;
1833
1992
  goto saveStateAndReturn;
1834
1993
  } else {
@@ -1848,7 +2007,7 @@ CommandPostWrapCopy:
1848
2007
  }
1849
2008
  }
1850
2009
  if (s->meta_block_remaining_len <= 0) {
1851
- /* Next metablock, if any */
2010
+ /* Next metablock, if any. */
1852
2011
  s->state = BROTLI_STATE_METABLOCK_DONE;
1853
2012
  goto saveStateAndReturn;
1854
2013
  } else {
@@ -1883,7 +2042,9 @@ BrotliDecoderResult BrotliDecoderDecompress(
1883
2042
  const uint8_t* next_in = encoded_buffer;
1884
2043
  size_t available_out = *decoded_size;
1885
2044
  uint8_t* next_out = decoded_buffer;
1886
- BrotliDecoderStateInit(&s);
2045
+ if (!BrotliDecoderStateInit(&s, 0, 0, 0)) {
2046
+ return BROTLI_DECODER_RESULT_ERROR;
2047
+ }
1887
2048
  result = BrotliDecoderDecompressStream(
1888
2049
  &s, &available_in, &next_in, &available_out, &next_out, &total_out);
1889
2050
  *decoded_size = total_out;
@@ -1895,22 +2056,25 @@ BrotliDecoderResult BrotliDecoderDecompress(
1895
2056
  }
1896
2057
 
1897
2058
  /* Invariant: input stream is never overconsumed:
1898
- * invalid input implies that the whole stream is invalid -> any amount of
2059
+ - invalid input implies that the whole stream is invalid -> any amount of
1899
2060
  input could be read and discarded
1900
- * when result is "needs more input", then at least one more byte is REQUIRED
2061
+ - when result is "needs more input", then at least one more byte is REQUIRED
1901
2062
  to complete decoding; all input data MUST be consumed by decoder, so
1902
2063
  client could swap the input buffer
1903
- * when result is "needs more output" decoder MUST ensure that it doesn't
2064
+ - when result is "needs more output" decoder MUST ensure that it doesn't
1904
2065
  hold more than 7 bits in bit reader; this saves client from swapping input
1905
2066
  buffer ahead of time
1906
- * when result is "success" decoder MUST return all unused data back to input
1907
- buffer; this is possible because the invariant is hold on enter
1908
- */
2067
+ - when result is "success" decoder MUST return all unused data back to input
2068
+ buffer; this is possible because the invariant is held on enter */
1909
2069
  BrotliDecoderResult BrotliDecoderDecompressStream(
1910
2070
  BrotliDecoderState* s, size_t* available_in, const uint8_t** next_in,
1911
2071
  size_t* available_out, uint8_t** next_out, size_t* total_out) {
1912
2072
  BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS;
1913
2073
  BrotliBitReader* br = &s->br;
2074
+ /* Ensure that |total_out| is set, even if no data will ever be pushed out. */
2075
+ if (total_out) {
2076
+ *total_out = s->partial_pos_out;
2077
+ }
1914
2078
  /* Do not try to process further in a case of unrecoverable error. */
1915
2079
  if ((int)s->error_code < 0) {
1916
2080
  return BROTLI_DECODER_RESULT_ERROR;
@@ -1920,7 +2084,7 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
1920
2084
  s, BROTLI_FAILURE(BROTLI_DECODER_ERROR_INVALID_ARGUMENTS));
1921
2085
  }
1922
2086
  if (!*available_out) next_out = 0;
1923
- if (s->buffer_length == 0) { /* Just connect bit reader to input stream. */
2087
+ if (s->buffer_length == 0) { /* Just connect bit reader to input stream. */
1924
2088
  br->avail_in = *available_in;
1925
2089
  br->next_in = *next_in;
1926
2090
  } else {
@@ -1932,9 +2096,10 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
1932
2096
  }
1933
2097
  /* State machine */
1934
2098
  for (;;) {
1935
- if (result != BROTLI_DECODER_SUCCESS) { /* Error, needs more input/output */
2099
+ if (result != BROTLI_DECODER_SUCCESS) {
2100
+ /* Error, needs more input/output. */
1936
2101
  if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) {
1937
- if (s->ringbuffer != 0) { /* Pro-actively push output. */
2102
+ if (s->ringbuffer != 0) { /* Pro-actively push output. */
1938
2103
  BrotliDecoderErrorCode intermediate_result = WriteRingBuffer(s,
1939
2104
  available_out, next_out, total_out, BROTLI_TRUE);
1940
2105
  /* WriteRingBuffer checks s->meta_block_remaining_len validity. */
@@ -1943,9 +2108,10 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
1943
2108
  break;
1944
2109
  }
1945
2110
  }
1946
- if (s->buffer_length != 0) { /* Used with internal buffer. */
1947
- if (br->avail_in == 0) { /* Successfully finished read transaction. */
1948
- /* Accumulator contains less than 8 bits, because internal buffer
2111
+ if (s->buffer_length != 0) { /* Used with internal buffer. */
2112
+ if (br->avail_in == 0) {
2113
+ /* Successfully finished read transaction.
2114
+ Accumulator contains less than 8 bits, because internal buffer
1949
2115
  is expanded byte-by-byte until it is enough to complete read. */
1950
2116
  s->buffer_length = 0;
1951
2117
  /* Switch to input stream and restart. */
@@ -1965,9 +2131,9 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
1965
2131
  /* Retry with more data in buffer. */
1966
2132
  continue;
1967
2133
  }
1968
- /* Can't finish reading and no more input.*/
2134
+ /* Can't finish reading and no more input. */
1969
2135
  break;
1970
- } else { /* Input stream doesn't contain enough input. */
2136
+ } else { /* Input stream doesn't contain enough input. */
1971
2137
  /* Copy tail to internal buffer and return. */
1972
2138
  *next_in = br->next_in;
1973
2139
  *available_in = br->avail_in;
@@ -1986,7 +2152,7 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
1986
2152
 
1987
2153
  if (s->buffer_length != 0) {
1988
2154
  /* Just consumed the buffered input and produced some output. Otherwise
1989
- it would result in "needs more input". Reset internal buffer.*/
2155
+ it would result in "needs more input". Reset internal buffer. */
1990
2156
  s->buffer_length = 0;
1991
2157
  } else {
1992
2158
  /* Using input stream in last iteration. When decoder switches to input
@@ -2006,18 +2172,37 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
2006
2172
  break;
2007
2173
  }
2008
2174
  /* Decode window size. */
2009
- s->window_bits = DecodeWindowBits(br); /* Reads 1..7 bits. */
2010
- BROTLI_LOG_UINT(s->window_bits);
2011
- if (s->window_bits == 9) {
2012
- /* Value 9 is reserved for future use. */
2175
+ result = DecodeWindowBits(s, br); /* Reads 1..8 bits. */
2176
+ if (result != BROTLI_DECODER_SUCCESS) {
2177
+ break;
2178
+ }
2179
+ if (s->large_window) {
2180
+ s->state = BROTLI_STATE_LARGE_WINDOW_BITS;
2181
+ break;
2182
+ }
2183
+ s->state = BROTLI_STATE_INITIALIZE;
2184
+ break;
2185
+
2186
+ case BROTLI_STATE_LARGE_WINDOW_BITS:
2187
+ if (!BrotliSafeReadBits(br, 6, &s->window_bits)) {
2188
+ result = BROTLI_DECODER_NEEDS_MORE_INPUT;
2189
+ break;
2190
+ }
2191
+ if (s->window_bits < BROTLI_LARGE_MIN_WBITS ||
2192
+ s->window_bits > BROTLI_LARGE_MAX_WBITS) {
2013
2193
  result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS);
2014
2194
  break;
2015
2195
  }
2196
+ s->state = BROTLI_STATE_INITIALIZE;
2197
+ /* Fall through. */
2198
+
2199
+ case BROTLI_STATE_INITIALIZE:
2200
+ BROTLI_LOG_UINT(s->window_bits);
2016
2201
  /* Maximum distance, see section 9.1. of the spec. */
2017
2202
  s->max_backward_distance = (1 << s->window_bits) - BROTLI_WINDOW_GAP;
2018
2203
 
2019
2204
  /* Allocate memory for both block_type_trees and block_len_trees. */
2020
- s->block_type_trees = (HuffmanCode*)BROTLI_ALLOC(s,
2205
+ s->block_type_trees = (HuffmanCode*)BROTLI_DECODER_ALLOC(s,
2021
2206
  sizeof(HuffmanCode) * 3 *
2022
2207
  (BROTLI_HUFFMAN_MAX_SIZE_258 + BROTLI_HUFFMAN_MAX_SIZE_26));
2023
2208
  if (s->block_type_trees == 0) {
@@ -2028,14 +2213,16 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
2028
2213
  s->block_type_trees + 3 * BROTLI_HUFFMAN_MAX_SIZE_258;
2029
2214
 
2030
2215
  s->state = BROTLI_STATE_METABLOCK_BEGIN;
2031
- /* No break, continue to next state */
2216
+ /* Fall through. */
2217
+
2032
2218
  case BROTLI_STATE_METABLOCK_BEGIN:
2033
2219
  BrotliDecoderStateMetablockBegin(s);
2034
2220
  BROTLI_LOG_UINT(s->pos);
2035
2221
  s->state = BROTLI_STATE_METABLOCK_HEADER;
2036
- /* No break, continue to next state */
2222
+ /* Fall through. */
2223
+
2037
2224
  case BROTLI_STATE_METABLOCK_HEADER:
2038
- result = DecodeMetaBlockLength(s, br); /* Reads 2 - 31 bits. */
2225
+ result = DecodeMetaBlockLength(s, br); /* Reads 2 - 31 bits. */
2039
2226
  if (result != BROTLI_DECODER_SUCCESS) {
2040
2227
  break;
2041
2228
  }
@@ -2062,31 +2249,24 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
2062
2249
  s->state = BROTLI_STATE_UNCOMPRESSED;
2063
2250
  break;
2064
2251
  }
2252
+ s->state = BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_HEADER;
2253
+ /* Fall through. */
2254
+
2255
+ case BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_HEADER: {
2256
+ BrotliMetablockHeaderArena* h = &s->arena.header;
2065
2257
  s->loop_counter = 0;
2258
+ /* Initialize compressed metablock header arena. */
2259
+ h->sub_loop_counter = 0;
2260
+ /* Make small negative indexes addressable. */
2261
+ h->symbol_lists =
2262
+ &h->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1];
2263
+ h->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
2264
+ h->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;
2265
+ h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;
2066
2266
  s->state = BROTLI_STATE_HUFFMAN_CODE_0;
2067
- break;
2068
- case BROTLI_STATE_UNCOMPRESSED: {
2069
- result = CopyUncompressedBlockToOutput(
2070
- available_out, next_out, total_out, s);
2071
- if (result != BROTLI_DECODER_SUCCESS) {
2072
- break;
2073
- }
2074
- s->state = BROTLI_STATE_METABLOCK_DONE;
2075
- break;
2076
2267
  }
2077
- case BROTLI_STATE_METADATA:
2078
- for (; s->meta_block_remaining_len > 0; --s->meta_block_remaining_len) {
2079
- uint32_t bits;
2080
- /* Read one byte and ignore it. */
2081
- if (!BrotliSafeReadBits(br, 8, &bits)) {
2082
- result = BROTLI_DECODER_NEEDS_MORE_INPUT;
2083
- break;
2084
- }
2085
- }
2086
- if (result == BROTLI_DECODER_SUCCESS) {
2087
- s->state = BROTLI_STATE_METABLOCK_DONE;
2088
- }
2089
- break;
2268
+ /* Fall through. */
2269
+
2090
2270
  case BROTLI_STATE_HUFFMAN_CODE_0:
2091
2271
  if (s->loop_counter >= 3) {
2092
2272
  s->state = BROTLI_STATE_METABLOCK_HEADER_2;
@@ -2104,23 +2284,28 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
2104
2284
  break;
2105
2285
  }
2106
2286
  s->state = BROTLI_STATE_HUFFMAN_CODE_1;
2107
- /* No break, continue to next state */
2287
+ /* Fall through. */
2288
+
2108
2289
  case BROTLI_STATE_HUFFMAN_CODE_1: {
2290
+ uint32_t alphabet_size = s->num_block_types[s->loop_counter] + 2;
2109
2291
  int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_258;
2110
- result = ReadHuffmanCode(s->num_block_types[s->loop_counter] + 2,
2292
+ result = ReadHuffmanCode(alphabet_size, alphabet_size,
2111
2293
  &s->block_type_trees[tree_offset], NULL, s);
2112
2294
  if (result != BROTLI_DECODER_SUCCESS) break;
2113
2295
  s->state = BROTLI_STATE_HUFFMAN_CODE_2;
2114
- /* No break, continue to next state */
2115
2296
  }
2297
+ /* Fall through. */
2298
+
2116
2299
  case BROTLI_STATE_HUFFMAN_CODE_2: {
2300
+ uint32_t alphabet_size = BROTLI_NUM_BLOCK_LEN_SYMBOLS;
2117
2301
  int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26;
2118
- result = ReadHuffmanCode(BROTLI_NUM_BLOCK_LEN_SYMBOLS,
2302
+ result = ReadHuffmanCode(alphabet_size, alphabet_size,
2119
2303
  &s->block_len_trees[tree_offset], NULL, s);
2120
2304
  if (result != BROTLI_DECODER_SUCCESS) break;
2121
2305
  s->state = BROTLI_STATE_HUFFMAN_CODE_3;
2122
- /* No break, continue to next state */
2123
2306
  }
2307
+ /* Fall through. */
2308
+
2124
2309
  case BROTLI_STATE_HUFFMAN_CODE_3: {
2125
2310
  int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26;
2126
2311
  if (!SafeReadBlockLength(s, &s->block_length[s->loop_counter],
@@ -2133,6 +2318,31 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
2133
2318
  s->state = BROTLI_STATE_HUFFMAN_CODE_0;
2134
2319
  break;
2135
2320
  }
2321
+
2322
+ case BROTLI_STATE_UNCOMPRESSED: {
2323
+ result = CopyUncompressedBlockToOutput(
2324
+ available_out, next_out, total_out, s);
2325
+ if (result != BROTLI_DECODER_SUCCESS) {
2326
+ break;
2327
+ }
2328
+ s->state = BROTLI_STATE_METABLOCK_DONE;
2329
+ break;
2330
+ }
2331
+
2332
+ case BROTLI_STATE_METADATA:
2333
+ for (; s->meta_block_remaining_len > 0; --s->meta_block_remaining_len) {
2334
+ uint32_t bits;
2335
+ /* Read one byte and ignore it. */
2336
+ if (!BrotliSafeReadBits(br, 8, &bits)) {
2337
+ result = BROTLI_DECODER_NEEDS_MORE_INPUT;
2338
+ break;
2339
+ }
2340
+ }
2341
+ if (result == BROTLI_DECODER_SUCCESS) {
2342
+ s->state = BROTLI_STATE_METABLOCK_DONE;
2343
+ }
2344
+ break;
2345
+
2136
2346
  case BROTLI_STATE_METABLOCK_HEADER_2: {
2137
2347
  uint32_t bits;
2138
2348
  if (!BrotliSafeReadBits(br, 6, &bits)) {
@@ -2141,28 +2351,28 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
2141
2351
  }
2142
2352
  s->distance_postfix_bits = bits & BitMask(2);
2143
2353
  bits >>= 2;
2144
- s->num_direct_distance_codes = BROTLI_NUM_DISTANCE_SHORT_CODES +
2145
- (bits << s->distance_postfix_bits);
2354
+ s->num_direct_distance_codes = bits << s->distance_postfix_bits;
2146
2355
  BROTLI_LOG_UINT(s->num_direct_distance_codes);
2147
2356
  BROTLI_LOG_UINT(s->distance_postfix_bits);
2148
- s->distance_postfix_mask = (int)BitMask(s->distance_postfix_bits);
2149
2357
  s->context_modes =
2150
- (uint8_t*)BROTLI_ALLOC(s, (size_t)s->num_block_types[0]);
2358
+ (uint8_t*)BROTLI_DECODER_ALLOC(s, (size_t)s->num_block_types[0]);
2151
2359
  if (s->context_modes == 0) {
2152
2360
  result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES);
2153
2361
  break;
2154
2362
  }
2155
2363
  s->loop_counter = 0;
2156
2364
  s->state = BROTLI_STATE_CONTEXT_MODES;
2157
- /* No break, continue to next state */
2158
2365
  }
2366
+ /* Fall through. */
2367
+
2159
2368
  case BROTLI_STATE_CONTEXT_MODES:
2160
2369
  result = ReadContextModes(s);
2161
2370
  if (result != BROTLI_DECODER_SUCCESS) {
2162
2371
  break;
2163
2372
  }
2164
2373
  s->state = BROTLI_STATE_CONTEXT_MAP_1;
2165
- /* No break, continue to next state */
2374
+ /* Fall through. */
2375
+
2166
2376
  case BROTLI_STATE_CONTEXT_MAP_1:
2167
2377
  result = DecodeContextMap(
2168
2378
  s->num_block_types[0] << BROTLI_LITERAL_CONTEXT_BITS,
@@ -2172,78 +2382,94 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
2172
2382
  }
2173
2383
  DetectTrivialLiteralBlockTypes(s);
2174
2384
  s->state = BROTLI_STATE_CONTEXT_MAP_2;
2175
- /* No break, continue to next state */
2176
- case BROTLI_STATE_CONTEXT_MAP_2:
2177
- {
2178
- uint32_t num_distance_codes = s->num_direct_distance_codes +
2179
- ((2 * BROTLI_MAX_DISTANCE_BITS) << s->distance_postfix_bits);
2180
- BROTLI_BOOL allocation_success = BROTLI_TRUE;
2181
- result = DecodeContextMap(
2182
- s->num_block_types[2] << BROTLI_DISTANCE_CONTEXT_BITS,
2183
- &s->num_dist_htrees, &s->dist_context_map, s);
2184
- if (result != BROTLI_DECODER_SUCCESS) {
2185
- break;
2186
- }
2187
- allocation_success &= BrotliDecoderHuffmanTreeGroupInit(
2188
- s, &s->literal_hgroup, BROTLI_NUM_LITERAL_SYMBOLS,
2189
- s->num_literal_htrees);
2190
- allocation_success &= BrotliDecoderHuffmanTreeGroupInit(
2191
- s, &s->insert_copy_hgroup, BROTLI_NUM_COMMAND_SYMBOLS,
2192
- s->num_block_types[1]);
2193
- allocation_success &= BrotliDecoderHuffmanTreeGroupInit(
2194
- s, &s->distance_hgroup, num_distance_codes,
2195
- s->num_dist_htrees);
2196
- if (!allocation_success) {
2197
- return SaveErrorCode(s,
2198
- BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS));
2199
- }
2385
+ /* Fall through. */
2386
+
2387
+ case BROTLI_STATE_CONTEXT_MAP_2: {
2388
+ uint32_t npostfix = s->distance_postfix_bits;
2389
+ uint32_t ndirect = s->num_direct_distance_codes;
2390
+ uint32_t distance_alphabet_size_max = BROTLI_DISTANCE_ALPHABET_SIZE(
2391
+ npostfix, ndirect, BROTLI_MAX_DISTANCE_BITS);
2392
+ uint32_t distance_alphabet_size_limit = distance_alphabet_size_max;
2393
+ BROTLI_BOOL allocation_success = BROTLI_TRUE;
2394
+ if (s->large_window) {
2395
+ BrotliDistanceCodeLimit limit = BrotliCalculateDistanceCodeLimit(
2396
+ BROTLI_MAX_ALLOWED_DISTANCE, npostfix, ndirect);
2397
+ distance_alphabet_size_max = BROTLI_DISTANCE_ALPHABET_SIZE(
2398
+ npostfix, ndirect, BROTLI_LARGE_MAX_DISTANCE_BITS);
2399
+ distance_alphabet_size_limit = limit.max_alphabet_size;
2400
+ }
2401
+ result = DecodeContextMap(
2402
+ s->num_block_types[2] << BROTLI_DISTANCE_CONTEXT_BITS,
2403
+ &s->num_dist_htrees, &s->dist_context_map, s);
2404
+ if (result != BROTLI_DECODER_SUCCESS) {
2405
+ break;
2406
+ }
2407
+ allocation_success &= BrotliDecoderHuffmanTreeGroupInit(
2408
+ s, &s->literal_hgroup, BROTLI_NUM_LITERAL_SYMBOLS,
2409
+ BROTLI_NUM_LITERAL_SYMBOLS, s->num_literal_htrees);
2410
+ allocation_success &= BrotliDecoderHuffmanTreeGroupInit(
2411
+ s, &s->insert_copy_hgroup, BROTLI_NUM_COMMAND_SYMBOLS,
2412
+ BROTLI_NUM_COMMAND_SYMBOLS, s->num_block_types[1]);
2413
+ allocation_success &= BrotliDecoderHuffmanTreeGroupInit(
2414
+ s, &s->distance_hgroup, distance_alphabet_size_max,
2415
+ distance_alphabet_size_limit, s->num_dist_htrees);
2416
+ if (!allocation_success) {
2417
+ return SaveErrorCode(s,
2418
+ BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS));
2200
2419
  }
2201
2420
  s->loop_counter = 0;
2202
2421
  s->state = BROTLI_STATE_TREE_GROUP;
2203
- /* No break, continue to next state */
2204
- case BROTLI_STATE_TREE_GROUP:
2205
- {
2206
- HuffmanTreeGroup* hgroup = NULL;
2207
- switch (s->loop_counter) {
2208
- case 0:
2209
- hgroup = &s->literal_hgroup;
2210
- break;
2211
- case 1:
2212
- hgroup = &s->insert_copy_hgroup;
2213
- break;
2214
- case 2:
2215
- hgroup = &s->distance_hgroup;
2216
- break;
2217
- default:
2218
- return SaveErrorCode(s, BROTLI_FAILURE(
2219
- BROTLI_DECODER_ERROR_UNREACHABLE));
2220
- }
2221
- result = HuffmanTreeGroupDecode(hgroup, s);
2222
- }
2422
+ }
2423
+ /* Fall through. */
2424
+
2425
+ case BROTLI_STATE_TREE_GROUP: {
2426
+ HuffmanTreeGroup* hgroup = NULL;
2427
+ switch (s->loop_counter) {
2428
+ case 0: hgroup = &s->literal_hgroup; break;
2429
+ case 1: hgroup = &s->insert_copy_hgroup; break;
2430
+ case 2: hgroup = &s->distance_hgroup; break;
2431
+ default: return SaveErrorCode(s, BROTLI_FAILURE(
2432
+ BROTLI_DECODER_ERROR_UNREACHABLE));
2433
+ }
2434
+ result = HuffmanTreeGroupDecode(hgroup, s);
2223
2435
  if (result != BROTLI_DECODER_SUCCESS) break;
2224
2436
  s->loop_counter++;
2225
- if (s->loop_counter >= 3) {
2226
- PrepareLiteralDecoding(s);
2227
- s->dist_context_map_slice = s->dist_context_map;
2228
- s->htree_command = s->insert_copy_hgroup.htrees[0];
2229
- if (!BrotliEnsureRingBuffer(s)) {
2230
- result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2);
2231
- break;
2232
- }
2233
- s->state = BROTLI_STATE_COMMAND_BEGIN;
2437
+ if (s->loop_counter < 3) {
2438
+ break;
2234
2439
  }
2235
- break;
2440
+ s->state = BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_BODY;
2441
+ }
2442
+ /* Fall through. */
2443
+
2444
+ case BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_BODY:
2445
+ PrepareLiteralDecoding(s);
2446
+ s->dist_context_map_slice = s->dist_context_map;
2447
+ s->htree_command = s->insert_copy_hgroup.htrees[0];
2448
+ if (!BrotliEnsureRingBuffer(s)) {
2449
+ result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2);
2450
+ break;
2451
+ }
2452
+ CalculateDistanceLut(s);
2453
+ s->state = BROTLI_STATE_COMMAND_BEGIN;
2454
+ /* Fall through. */
2455
+
2236
2456
  case BROTLI_STATE_COMMAND_BEGIN:
2457
+ /* Fall through. */
2237
2458
  case BROTLI_STATE_COMMAND_INNER:
2459
+ /* Fall through. */
2238
2460
  case BROTLI_STATE_COMMAND_POST_DECODE_LITERALS:
2461
+ /* Fall through. */
2239
2462
  case BROTLI_STATE_COMMAND_POST_WRAP_COPY:
2240
2463
  result = ProcessCommands(s);
2241
2464
  if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) {
2242
2465
  result = SafeProcessCommands(s);
2243
2466
  }
2244
2467
  break;
2468
+
2245
2469
  case BROTLI_STATE_COMMAND_INNER_WRITE:
2470
+ /* Fall through. */
2246
2471
  case BROTLI_STATE_COMMAND_POST_WRITE_1:
2472
+ /* Fall through. */
2247
2473
  case BROTLI_STATE_COMMAND_POST_WRITE_2:
2248
2474
  result = WriteRingBuffer(
2249
2475
  s, available_out, next_out, total_out, BROTLI_FALSE);
@@ -2256,7 +2482,7 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
2256
2482
  }
2257
2483
  if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_1) {
2258
2484
  if (s->meta_block_remaining_len == 0) {
2259
- /* Next metablock, if any */
2485
+ /* Next metablock, if any. */
2260
2486
  s->state = BROTLI_STATE_METABLOCK_DONE;
2261
2487
  } else {
2262
2488
  s->state = BROTLI_STATE_COMMAND_BEGIN;
@@ -2276,6 +2502,7 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
2276
2502
  s->state = BROTLI_STATE_COMMAND_INNER;
2277
2503
  }
2278
2504
  break;
2505
+
2279
2506
  case BROTLI_STATE_METABLOCK_DONE:
2280
2507
  if (s->meta_block_remaining_len < 0) {
2281
2508
  result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2);
@@ -2296,7 +2523,8 @@ BrotliDecoderResult BrotliDecoderDecompressStream(
2296
2523
  *next_in = br->next_in;
2297
2524
  }
2298
2525
  s->state = BROTLI_STATE_DONE;
2299
- /* No break, continue to next state */
2526
+ /* Fall through. */
2527
+
2300
2528
  case BROTLI_STATE_DONE:
2301
2529
  if (s->ringbuffer != 0) {
2302
2530
  result = WriteRingBuffer(