brotli 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.travis.yml +11 -3
  4. data/Gemfile +2 -0
  5. data/ext/brotli/brotli.c +279 -0
  6. data/ext/brotli/brotli.h +2 -0
  7. data/ext/brotli/buffer.c +95 -0
  8. data/ext/brotli/buffer.h +19 -0
  9. data/ext/brotli/extconf.rb +21 -81
  10. data/lib/brotli/version.rb +1 -1
  11. data/vendor/brotli/dec/bit_reader.c +5 -5
  12. data/vendor/brotli/dec/bit_reader.h +15 -15
  13. data/vendor/brotli/dec/context.h +1 -1
  14. data/vendor/brotli/dec/decode.c +433 -348
  15. data/vendor/brotli/dec/decode.h +74 -48
  16. data/vendor/brotli/dec/huffman.c +5 -4
  17. data/vendor/brotli/dec/huffman.h +4 -4
  18. data/vendor/brotli/dec/port.h +2 -95
  19. data/vendor/brotli/dec/prefix.h +5 -3
  20. data/vendor/brotli/dec/state.c +15 -27
  21. data/vendor/brotli/dec/state.h +21 -17
  22. data/vendor/brotli/dec/transform.h +1 -1
  23. data/vendor/brotli/enc/backward_references.c +892 -0
  24. data/vendor/brotli/enc/backward_references.h +85 -102
  25. data/vendor/brotli/enc/backward_references_inc.h +147 -0
  26. data/vendor/brotli/enc/bit_cost.c +35 -0
  27. data/vendor/brotli/enc/bit_cost.h +23 -121
  28. data/vendor/brotli/enc/bit_cost_inc.h +127 -0
  29. data/vendor/brotli/enc/block_encoder_inc.h +33 -0
  30. data/vendor/brotli/enc/block_splitter.c +197 -0
  31. data/vendor/brotli/enc/block_splitter.h +40 -50
  32. data/vendor/brotli/enc/block_splitter_inc.h +432 -0
  33. data/vendor/brotli/enc/brotli_bit_stream.c +1334 -0
  34. data/vendor/brotli/enc/brotli_bit_stream.h +95 -167
  35. data/vendor/brotli/enc/cluster.c +56 -0
  36. data/vendor/brotli/enc/cluster.h +23 -305
  37. data/vendor/brotli/enc/cluster_inc.h +315 -0
  38. data/vendor/brotli/enc/command.h +83 -76
  39. data/vendor/brotli/enc/compress_fragment.c +747 -0
  40. data/vendor/brotli/enc/compress_fragment.h +48 -37
  41. data/vendor/brotli/enc/compress_fragment_two_pass.c +557 -0
  42. data/vendor/brotli/enc/compress_fragment_two_pass.h +37 -26
  43. data/vendor/brotli/enc/compressor.cc +139 -0
  44. data/vendor/brotli/enc/compressor.h +146 -0
  45. data/vendor/brotli/enc/context.h +102 -96
  46. data/vendor/brotli/enc/dictionary_hash.h +9 -5
  47. data/vendor/brotli/enc/encode.c +1562 -0
  48. data/vendor/brotli/enc/encode.h +211 -199
  49. data/vendor/brotli/enc/encode_parallel.cc +161 -151
  50. data/vendor/brotli/enc/encode_parallel.h +7 -8
  51. data/vendor/brotli/enc/entropy_encode.c +501 -0
  52. data/vendor/brotli/enc/entropy_encode.h +107 -89
  53. data/vendor/brotli/enc/entropy_encode_static.h +29 -62
  54. data/vendor/brotli/enc/fast_log.h +26 -20
  55. data/vendor/brotli/enc/find_match_length.h +23 -20
  56. data/vendor/brotli/enc/hash.h +614 -871
  57. data/vendor/brotli/enc/hash_forgetful_chain_inc.h +249 -0
  58. data/vendor/brotli/enc/hash_longest_match_inc.h +241 -0
  59. data/vendor/brotli/enc/hash_longest_match_quickly_inc.h +230 -0
  60. data/vendor/brotli/enc/histogram.c +95 -0
  61. data/vendor/brotli/enc/histogram.h +49 -83
  62. data/vendor/brotli/enc/histogram_inc.h +51 -0
  63. data/vendor/brotli/enc/literal_cost.c +178 -0
  64. data/vendor/brotli/enc/literal_cost.h +16 -10
  65. data/vendor/brotli/enc/memory.c +181 -0
  66. data/vendor/brotli/enc/memory.h +62 -0
  67. data/vendor/brotli/enc/metablock.c +515 -0
  68. data/vendor/brotli/enc/metablock.h +87 -57
  69. data/vendor/brotli/enc/metablock_inc.h +183 -0
  70. data/vendor/brotli/enc/port.h +73 -47
  71. data/vendor/brotli/enc/prefix.h +34 -61
  72. data/vendor/brotli/enc/quality.h +130 -0
  73. data/vendor/brotli/enc/ringbuffer.h +137 -122
  74. data/vendor/brotli/enc/{static_dict.cc → static_dict.c} +162 -139
  75. data/vendor/brotli/enc/static_dict.h +23 -18
  76. data/vendor/brotli/enc/static_dict_lut.h +11223 -12037
  77. data/vendor/brotli/enc/streams.cc +7 -7
  78. data/vendor/brotli/enc/streams.h +32 -32
  79. data/vendor/brotli/enc/{utf8_util.cc → utf8_util.c} +22 -20
  80. data/vendor/brotli/enc/utf8_util.h +16 -9
  81. data/vendor/brotli/enc/write_bits.h +49 -43
  82. metadata +34 -25
  83. data/ext/brotli/brotli.cc +0 -181
  84. data/vendor/brotli/dec/Makefile +0 -12
  85. data/vendor/brotli/dec/dictionary.c +0 -9466
  86. data/vendor/brotli/dec/dictionary.h +0 -38
  87. data/vendor/brotli/dec/types.h +0 -38
  88. data/vendor/brotli/enc/Makefile +0 -14
  89. data/vendor/brotli/enc/backward_references.cc +0 -858
  90. data/vendor/brotli/enc/block_splitter.cc +0 -505
  91. data/vendor/brotli/enc/brotli_bit_stream.cc +0 -1181
  92. data/vendor/brotli/enc/compress_fragment.cc +0 -701
  93. data/vendor/brotli/enc/compress_fragment_two_pass.cc +0 -524
  94. data/vendor/brotli/enc/dictionary.cc +0 -9466
  95. data/vendor/brotli/enc/dictionary.h +0 -41
  96. data/vendor/brotli/enc/encode.cc +0 -1180
  97. data/vendor/brotli/enc/entropy_encode.cc +0 -480
  98. data/vendor/brotli/enc/histogram.cc +0 -67
  99. data/vendor/brotli/enc/literal_cost.cc +0 -165
  100. data/vendor/brotli/enc/metablock.cc +0 -539
  101. data/vendor/brotli/enc/transform.h +0 -248
  102. data/vendor/brotli/enc/types.h +0 -29
@@ -13,9 +13,10 @@
13
13
  #include <stdlib.h> /* free, malloc */
14
14
  #include <string.h> /* memcpy, memset */
15
15
 
16
+ #include "../common/constants.h"
17
+ #include "../common/dictionary.h"
16
18
  #include "./bit_reader.h"
17
19
  #include "./context.h"
18
- #include "./dictionary.h"
19
20
  #include "./huffman.h"
20
21
  #include "./port.h"
21
22
  #include "./prefix.h"
@@ -34,19 +35,10 @@ extern "C" {
34
35
  BROTLI_LOG(("[%s] %s[%lu] = %lu\n", __func__, #array_name, \
35
36
  (unsigned long)(idx), (unsigned long)array_name[idx]))
36
37
 
37
- static const uint32_t kDefaultCodeLength = 8;
38
- static const uint32_t kCodeLengthRepeatCode = 16;
39
- static const uint32_t kNumLiteralCodes = 256;
40
- static const uint32_t kNumInsertAndCopyCodes = 704;
41
- static const uint32_t kNumBlockLengthCodes = 26;
42
- static const int kLiteralContextBits = 6;
43
- static const int kDistanceContextBits = 2;
44
-
45
38
  #define HUFFMAN_TABLE_BITS 8U
46
39
  #define HUFFMAN_TABLE_MASK 0xff
47
40
 
48
- #define CODE_LENGTH_CODES 18
49
- static const uint8_t kCodeLengthCodeOrder[CODE_LENGTH_CODES] = {
41
+ static const uint8_t kCodeLengthCodeOrder[BROTLI_CODE_LENGTH_CODES] = {
50
42
  1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15,
51
43
  };
52
44
 
@@ -59,46 +51,49 @@ static const uint8_t kCodeLengthPrefixValue[16] = {
59
51
  0, 4, 3, 2, 0, 4, 3, 1, 0, 4, 3, 2, 0, 4, 3, 5,
60
52
  };
61
53
 
62
- #define NUM_DISTANCE_SHORT_CODES 16
63
-
64
- BrotliState* BrotliCreateState(
54
+ BrotliDecoderState* BrotliDecoderCreateInstance(
65
55
  brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
66
- BrotliState* state = 0;
56
+ BrotliDecoderState* state = 0;
67
57
  if (!alloc_func && !free_func) {
68
- state = (BrotliState*)malloc(sizeof(BrotliState));
58
+ state = (BrotliDecoderState*)malloc(sizeof(BrotliDecoderState));
69
59
  } else if (alloc_func && free_func) {
70
- state = (BrotliState*)alloc_func(opaque, sizeof(BrotliState));
60
+ state = (BrotliDecoderState*)alloc_func(opaque, sizeof(BrotliDecoderState));
71
61
  }
72
62
  if (state == 0) {
73
63
  BROTLI_DUMP();
74
64
  return 0;
75
65
  }
76
- BrotliStateInitWithCustomAllocators(state, alloc_func, free_func, opaque);
77
- state->error_code = BROTLI_NO_ERROR;
66
+ BrotliDecoderStateInitWithCustomAllocators(
67
+ state, alloc_func, free_func, opaque);
68
+ state->error_code = BROTLI_DECODER_NO_ERROR;
78
69
  return state;
79
70
  }
80
71
 
81
- /* Deinitializes and frees BrotliState instance. */
82
- void BrotliDestroyState(BrotliState* state) {
72
+ /* Deinitializes and frees BrotliDecoderState instance. */
73
+ void BrotliDecoderDestroyInstance(BrotliDecoderState* state) {
83
74
  if (!state) {
84
75
  return;
85
76
  } else {
86
77
  brotli_free_func free_func = state->free_func;
87
78
  void* opaque = state->memory_manager_opaque;
88
- BrotliStateCleanup(state);
79
+ BrotliDecoderStateCleanup(state);
89
80
  free_func(opaque, state);
90
81
  }
91
82
  }
92
83
 
93
- /* Saves error code and converts it to BrotliResult */
94
- static BROTLI_NOINLINE BrotliResult SaveErrorCode(
95
- BrotliState* s, BrotliErrorCode e) {
84
+ /* Saves error code and converts it to BrotliDecoderResult */
85
+ static BROTLI_NOINLINE BrotliDecoderResult SaveErrorCode(
86
+ BrotliDecoderState* s, BrotliDecoderErrorCode e) {
96
87
  s->error_code = (int)e;
97
88
  switch (e) {
98
- case BROTLI_SUCCESS: return BROTLI_RESULT_SUCCESS;
99
- case BROTLI_NEEDS_MORE_INPUT: return BROTLI_RESULT_NEEDS_MORE_INPUT;
100
- case BROTLI_NEEDS_MORE_OUTPUT: return BROTLI_RESULT_NEEDS_MORE_OUTPUT;
101
- default: return BROTLI_RESULT_ERROR;
89
+ case BROTLI_DECODER_SUCCESS:
90
+ return BROTLI_DECODER_RESULT_SUCCESS;
91
+ case BROTLI_DECODER_NEEDS_MORE_INPUT:
92
+ return BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT;
93
+ case BROTLI_DECODER_NEEDS_MORE_OUTPUT:
94
+ return BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
95
+ default:
96
+ return BROTLI_DECODER_RESULT_ERROR;
102
97
  }
103
98
  }
104
99
 
@@ -132,29 +127,29 @@ static BROTLI_INLINE void memmove16(uint8_t* dst, uint8_t* src) {
132
127
  }
133
128
 
134
129
  /* Decodes a number in the range [0..255], by reading 1 - 11 bits. */
135
- static BROTLI_NOINLINE BrotliErrorCode DecodeVarLenUint8(BrotliState* s,
136
- BrotliBitReader* br, uint32_t* value) {
130
+ static BROTLI_NOINLINE BrotliDecoderErrorCode DecodeVarLenUint8(
131
+ BrotliDecoderState* s, BrotliBitReader* br, uint32_t* value) {
137
132
  uint32_t bits;
138
133
  switch (s->substate_decode_uint8) {
139
134
  case BROTLI_STATE_DECODE_UINT8_NONE:
140
135
  if (PREDICT_FALSE(!BrotliSafeReadBits(br, 1, &bits))) {
141
- return BROTLI_NEEDS_MORE_INPUT;
136
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
142
137
  }
143
138
  if (bits == 0) {
144
139
  *value = 0;
145
- return BROTLI_SUCCESS;
140
+ return BROTLI_DECODER_SUCCESS;
146
141
  }
147
142
  /* No break, transit to the next state. */
148
143
 
149
144
  case BROTLI_STATE_DECODE_UINT8_SHORT:
150
145
  if (PREDICT_FALSE(!BrotliSafeReadBits(br, 3, &bits))) {
151
146
  s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_SHORT;
152
- return BROTLI_NEEDS_MORE_INPUT;
147
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
153
148
  }
154
149
  if (bits == 0) {
155
150
  *value = 1;
156
151
  s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
157
- return BROTLI_SUCCESS;
152
+ return BROTLI_DECODER_SUCCESS;
158
153
  }
159
154
  /* Use output value as a temporary storage. It MUST be persisted. */
160
155
  *value = bits;
@@ -163,27 +158,28 @@ static BROTLI_NOINLINE BrotliErrorCode DecodeVarLenUint8(BrotliState* s,
163
158
  case BROTLI_STATE_DECODE_UINT8_LONG:
164
159
  if (PREDICT_FALSE(!BrotliSafeReadBits(br, *value, &bits))) {
165
160
  s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_LONG;
166
- return BROTLI_NEEDS_MORE_INPUT;
161
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
167
162
  }
168
163
  *value = (1U << *value) + bits;
169
164
  s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
170
- return BROTLI_SUCCESS;
165
+ return BROTLI_DECODER_SUCCESS;
171
166
 
172
167
  default:
173
- return BROTLI_FAILURE(BROTLI_ERROR_UNREACHABLE);
168
+ return
169
+ BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE);
174
170
  }
175
171
  }
176
172
 
177
173
  /* Decodes a metablock length and flags by reading 2 - 31 bits. */
178
- static BrotliErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
179
- BrotliState* s, BrotliBitReader* br) {
174
+ static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
175
+ BrotliDecoderState* s, BrotliBitReader* br) {
180
176
  uint32_t bits;
181
177
  int i;
182
178
  for (;;) {
183
179
  switch (s->substate_metablock_header) {
184
180
  case BROTLI_STATE_METABLOCK_HEADER_NONE:
185
181
  if (!BrotliSafeReadBits(br, 1, &bits)) {
186
- return BROTLI_NEEDS_MORE_INPUT;
182
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
187
183
  }
188
184
  s->is_last_metablock = (uint8_t)bits;
189
185
  s->meta_block_remaining_len = 0;
@@ -198,18 +194,18 @@ static BrotliErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
198
194
 
199
195
  case BROTLI_STATE_METABLOCK_HEADER_EMPTY:
200
196
  if (!BrotliSafeReadBits(br, 1, &bits)) {
201
- return BROTLI_NEEDS_MORE_INPUT;
197
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
202
198
  }
203
199
  if (bits) {
204
200
  s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
205
- return BROTLI_SUCCESS;
201
+ return BROTLI_DECODER_SUCCESS;
206
202
  }
207
203
  s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NIBBLES;
208
204
  /* No break, transit to the next state. */
209
205
 
210
206
  case BROTLI_STATE_METABLOCK_HEADER_NIBBLES:
211
207
  if (!BrotliSafeReadBits(br, 2, &bits)) {
212
- return BROTLI_NEEDS_MORE_INPUT;
208
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
213
209
  }
214
210
  s->size_nibbles = (uint8_t)(bits + 4);
215
211
  s->loop_counter = 0;
@@ -226,10 +222,10 @@ static BrotliErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
226
222
  for (; i < s->size_nibbles; ++i) {
227
223
  if (!BrotliSafeReadBits(br, 4, &bits)) {
228
224
  s->loop_counter = i;
229
- return BROTLI_NEEDS_MORE_INPUT;
225
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
230
226
  }
231
227
  if (i + 1 == s->size_nibbles && s->size_nibbles > 4 && bits == 0) {
232
- return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_EXUBERANT_NIBBLE);
228
+ return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE);
233
229
  }
234
230
  s->meta_block_remaining_len |= (int)(bits << (i * 4));
235
231
  }
@@ -240,31 +236,31 @@ static BrotliErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
240
236
  case BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED:
241
237
  if (!s->is_last_metablock) {
242
238
  if (!BrotliSafeReadBits(br, 1, &bits)) {
243
- return BROTLI_NEEDS_MORE_INPUT;
239
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
244
240
  }
245
241
  s->is_uncompressed = (uint8_t)bits;
246
242
  }
247
243
  ++s->meta_block_remaining_len;
248
244
  s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
249
- return BROTLI_SUCCESS;
245
+ return BROTLI_DECODER_SUCCESS;
250
246
 
251
247
  case BROTLI_STATE_METABLOCK_HEADER_RESERVED:
252
248
  if (!BrotliSafeReadBits(br, 1, &bits)) {
253
- return BROTLI_NEEDS_MORE_INPUT;
249
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
254
250
  }
255
251
  if (bits != 0) {
256
- return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_RESERVED);
252
+ return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_RESERVED);
257
253
  }
258
254
  s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_BYTES;
259
255
  /* No break, transit to the next state. */
260
256
 
261
257
  case BROTLI_STATE_METABLOCK_HEADER_BYTES:
262
258
  if (!BrotliSafeReadBits(br, 2, &bits)) {
263
- return BROTLI_NEEDS_MORE_INPUT;
259
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
264
260
  }
265
261
  if (bits == 0) {
266
262
  s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
267
- return BROTLI_SUCCESS;
263
+ return BROTLI_DECODER_SUCCESS;
268
264
  }
269
265
  s->size_nibbles = (uint8_t)bits;
270
266
  s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_METADATA;
@@ -275,19 +271,21 @@ static BrotliErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
275
271
  for (; i < s->size_nibbles; ++i) {
276
272
  if (!BrotliSafeReadBits(br, 8, &bits)) {
277
273
  s->loop_counter = i;
278
- return BROTLI_NEEDS_MORE_INPUT;
274
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
279
275
  }
280
276
  if (i + 1 == s->size_nibbles && s->size_nibbles > 1 && bits == 0) {
281
- return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_EXUBERANT_META_NIBBLE);
277
+ return BROTLI_FAILURE(
278
+ BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE);
282
279
  }
283
280
  s->meta_block_remaining_len |= (int)(bits << (i * 8));
284
281
  }
285
282
  ++s->meta_block_remaining_len;
286
283
  s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
287
- return BROTLI_SUCCESS;
284
+ return BROTLI_DECODER_SUCCESS;
288
285
 
289
286
  default:
290
- return BROTLI_FAILURE(BROTLI_ERROR_UNREACHABLE);
287
+ return
288
+ BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE);
291
289
  }
292
290
  }
293
291
  }
@@ -319,17 +317,16 @@ static BROTLI_INLINE uint32_t ReadSymbol(const HuffmanCode* table,
319
317
 
320
318
  /* Same as DecodeSymbol, but it is known that there is less than 15 bits of
321
319
  input are currently available. */
322
- static BROTLI_NOINLINE int SafeDecodeSymbol(const HuffmanCode* table,
323
- BrotliBitReader* br,
324
- uint32_t* result) {
320
+ static BROTLI_NOINLINE BROTLI_BOOL SafeDecodeSymbol(
321
+ const HuffmanCode* table, BrotliBitReader* br, uint32_t* result) {
325
322
  uint32_t val;
326
323
  uint32_t available_bits = BrotliGetAvailableBits(br);
327
324
  if (available_bits == 0) {
328
325
  if (table->bits == 0) {
329
326
  *result = table->value;
330
- return 1;
327
+ return BROTLI_TRUE;
331
328
  }
332
- return 0; /* No valid bits at all. */
329
+ return BROTLI_FALSE; /* No valid bits at all. */
333
330
  }
334
331
  val = (uint32_t)BrotliGetBitsUnmasked(br);
335
332
  table += val & HUFFMAN_TABLE_MASK;
@@ -337,13 +334,13 @@ static BROTLI_NOINLINE int SafeDecodeSymbol(const HuffmanCode* table,
337
334
  if (table->bits <= available_bits) {
338
335
  BrotliDropBits(br, table->bits);
339
336
  *result = table->value;
340
- return 1;
337
+ return BROTLI_TRUE;
341
338
  } else {
342
- return 0; /* Not enough bits for the first level. */
339
+ return BROTLI_FALSE; /* Not enough bits for the first level. */
343
340
  }
344
341
  }
345
342
  if (available_bits <= HUFFMAN_TABLE_BITS) {
346
- return 0; /* Not enough bits to move to the second level. */
343
+ return BROTLI_FALSE; /* Not enough bits to move to the second level. */
347
344
  }
348
345
 
349
346
  /* Speculatively drop HUFFMAN_TABLE_BITS. */
@@ -351,21 +348,20 @@ static BROTLI_NOINLINE int SafeDecodeSymbol(const HuffmanCode* table,
351
348
  available_bits -= HUFFMAN_TABLE_BITS;
352
349
  table += table->value + val;
353
350
  if (available_bits < table->bits) {
354
- return 0; /* Not enough bits for the second level. */
351
+ return BROTLI_FALSE; /* Not enough bits for the second level. */
355
352
  }
356
353
 
357
354
  BrotliDropBits(br, HUFFMAN_TABLE_BITS + table->bits);
358
355
  *result = table->value;
359
- return 1;
356
+ return BROTLI_TRUE;
360
357
  }
361
358
 
362
- static BROTLI_INLINE int SafeReadSymbol(const HuffmanCode* table,
363
- BrotliBitReader* br,
364
- uint32_t* result) {
359
+ static BROTLI_INLINE BROTLI_BOOL SafeReadSymbol(
360
+ const HuffmanCode* table, BrotliBitReader* br, uint32_t* result) {
365
361
  uint32_t val;
366
362
  if (PREDICT_TRUE(BrotliSafeGetBits(br, 15, &val))) {
367
363
  *result = DecodeSymbol(val, table, br);
368
- return 1;
364
+ return BROTLI_TRUE;
369
365
  }
370
366
  return SafeDecodeSymbol(table, br, result);
371
367
  }
@@ -419,8 +415,8 @@ static BROTLI_INLINE uint32_t Log2Floor(uint32_t x) {
419
415
  Totally 1..4 symbols are read, 1..10 bits each.
420
416
  The list of symbols MUST NOT contain duplicates.
421
417
  */
422
- static BrotliErrorCode ReadSimpleHuffmanSymbols(uint32_t alphabet_size,
423
- BrotliState* s) {
418
+ static BrotliDecoderErrorCode ReadSimpleHuffmanSymbols(
419
+ uint32_t alphabet_size, BrotliDecoderState* s) {
424
420
  /* max_bits == 1..10; symbol == 0..3; 1..40 bits will be read. */
425
421
  BrotliBitReader* br = &s->br;
426
422
  uint32_t max_bits = Log2Floor(alphabet_size - 1);
@@ -431,10 +427,11 @@ static BrotliErrorCode ReadSimpleHuffmanSymbols(uint32_t alphabet_size,
431
427
  if (PREDICT_FALSE(!BrotliSafeReadBits(br, max_bits, &v))) {
432
428
  s->sub_loop_counter = i;
433
429
  s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_READ;
434
- return BROTLI_NEEDS_MORE_INPUT;
430
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
435
431
  }
436
432
  if (v >= alphabet_size) {
437
- return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET);
433
+ return
434
+ BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET);
438
435
  }
439
436
  s->symbols_lists_array[i] = (uint16_t)v;
440
437
  BROTLI_LOG_UINT(s->symbols_lists_array[i]);
@@ -445,12 +442,12 @@ static BrotliErrorCode ReadSimpleHuffmanSymbols(uint32_t alphabet_size,
445
442
  uint32_t k = i + 1;
446
443
  for (; k <= num_symbols; ++k) {
447
444
  if (s->symbols_lists_array[i] == s->symbols_lists_array[k]) {
448
- return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME);
445
+ return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME);
449
446
  }
450
447
  }
451
448
  }
452
449
 
453
- return BROTLI_SUCCESS;
450
+ return BROTLI_DECODER_SUCCESS;
454
451
  }
455
452
 
456
453
  /* Process single decoded symbol code length:
@@ -478,12 +475,14 @@ static BROTLI_INLINE void ProcessSingleCodeLength(uint32_t code_len,
478
475
 
479
476
  /* Process repeated symbol code length.
480
477
  A) Check if it is the extension of previous repeat sequence; if the decoded
481
- value is not kCodeLengthRepeatCode, then it is a new symbol-skip
478
+ value is not BROTLI_REPEAT_PREVIOUS_CODE_LENGTH, then it is a new
479
+ symbol-skip
482
480
  B) Update repeat variable
483
481
  C) Check if operation is feasible (fits alphapet)
484
482
  D) For each symbol do the same operations as in ProcessSingleCodeLength
485
483
 
486
- PRECONDITION: code_len == kCodeLengthRepeatCode or kCodeLengthRepeatCode + 1
484
+ PRECONDITION: code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH or
485
+ code_len == BROTLI_REPEAT_ZERO_CODE_LENGTH
487
486
  */
488
487
  static BROTLI_INLINE void ProcessRepeatedCodeLength(uint32_t code_len,
489
488
  uint32_t repeat_delta, uint32_t alphabet_size, uint32_t* symbol,
@@ -491,9 +490,11 @@ static BROTLI_INLINE void ProcessRepeatedCodeLength(uint32_t code_len,
491
490
  uint32_t* repeat_code_len, uint16_t* symbol_lists,
492
491
  uint16_t* code_length_histo, int* next_symbol) {
493
492
  uint32_t old_repeat;
494
- uint32_t new_len = 0;
495
- if (code_len == kCodeLengthRepeatCode) {
493
+ uint32_t extra_bits = 3; /* for BROTLI_REPEAT_ZERO_CODE_LENGTH */
494
+ uint32_t new_len = 0; /* for BROTLI_REPEAT_ZERO_CODE_LENGTH */
495
+ if (code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) {
496
496
  new_len = *prev_code_len;
497
+ extra_bits = 2;
497
498
  }
498
499
  if (*repeat_code_len != new_len) {
499
500
  *repeat = 0;
@@ -502,7 +503,7 @@ static BROTLI_INLINE void ProcessRepeatedCodeLength(uint32_t code_len,
502
503
  old_repeat = *repeat;
503
504
  if (*repeat > 0) {
504
505
  *repeat -= 2;
505
- *repeat <<= code_len - 14U;
506
+ *repeat <<= extra_bits;
506
507
  }
507
508
  *repeat += repeat_delta + 3U;
508
509
  repeat_delta = *repeat - old_repeat;
@@ -531,8 +532,8 @@ static BROTLI_INLINE void ProcessRepeatedCodeLength(uint32_t code_len,
531
532
  }
532
533
 
533
534
  /* Reads and decodes symbol codelengths. */
534
- static BrotliErrorCode ReadSymbolCodeLengths(
535
- uint32_t alphabet_size, BrotliState* s) {
535
+ static BrotliDecoderErrorCode ReadSymbolCodeLengths(
536
+ uint32_t alphabet_size, BrotliDecoderState* s) {
536
537
  BrotliBitReader* br = &s->br;
537
538
  uint32_t symbol = s->symbol;
538
539
  uint32_t repeat = s->repeat;
@@ -543,7 +544,7 @@ static BrotliErrorCode ReadSymbolCodeLengths(
543
544
  uint16_t* code_length_histo = s->code_length_histo;
544
545
  int* next_symbol = s->next_symbol;
545
546
  if (!BrotliWarmupBitReader(br)) {
546
- return BROTLI_NEEDS_MORE_INPUT;
547
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
547
548
  }
548
549
  while (symbol < alphabet_size && space > 0) {
549
550
  const HuffmanCode* p = s->table;
@@ -554,31 +555,33 @@ static BrotliErrorCode ReadSymbolCodeLengths(
554
555
  s->prev_code_len = prev_code_len;
555
556
  s->repeat_code_len = repeat_code_len;
556
557
  s->space = space;
557
- return BROTLI_NEEDS_MORE_INPUT;
558
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
558
559
  }
559
560
  BrotliFillBitWindow16(br);
560
561
  p += BrotliGetBitsUnmasked(br) &
561
562
  BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH);
562
563
  BrotliDropBits(br, p->bits); /* Use 1..5 bits */
563
564
  code_len = p->value; /* code_len == 0..17 */
564
- if (code_len < kCodeLengthRepeatCode) {
565
+ if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) {
565
566
  ProcessSingleCodeLength(code_len, &symbol, &repeat, &space,
566
567
  &prev_code_len, symbol_lists, code_length_histo, next_symbol);
567
568
  } else { /* code_len == 16..17, extra_bits == 2..3 */
569
+ uint32_t extra_bits =
570
+ (code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) ? 2 : 3;
568
571
  uint32_t repeat_delta =
569
- (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(code_len - 14U);
570
- BrotliDropBits(br, code_len - 14U);
572
+ (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(extra_bits);
573
+ BrotliDropBits(br, extra_bits);
571
574
  ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size,
572
575
  &symbol, &repeat, &space, &prev_code_len, &repeat_code_len,
573
576
  symbol_lists, code_length_histo, next_symbol);
574
577
  }
575
578
  }
576
579
  s->space = space;
577
- return BROTLI_SUCCESS;
580
+ return BROTLI_DECODER_SUCCESS;
578
581
  }
579
582
 
580
- static BrotliErrorCode SafeReadSymbolCodeLengths(
581
- uint32_t alphabet_size, BrotliState* s) {
583
+ static BrotliDecoderErrorCode SafeReadSymbolCodeLengths(
584
+ uint32_t alphabet_size, BrotliDecoderState* s) {
582
585
  BrotliBitReader* br = &s->br;
583
586
  while (s->symbol < alphabet_size && s->space > 0) {
584
587
  const HuffmanCode* p = s->table;
@@ -591,7 +594,7 @@ static BrotliErrorCode SafeReadSymbolCodeLengths(
591
594
  p += bits & BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH);
592
595
  if (p->bits > available_bits) goto pullMoreInput;
593
596
  code_len = p->value; /* code_len == 0..17 */
594
- if (code_len < kCodeLengthRepeatCode) {
597
+ if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) {
595
598
  BrotliDropBits(br, p->bits);
596
599
  ProcessSingleCodeLength(code_len, &s->symbol, &s->repeat, &s->space,
597
600
  &s->prev_code_len, s->symbol_lists, s->code_length_histo,
@@ -610,20 +613,20 @@ static BrotliErrorCode SafeReadSymbolCodeLengths(
610
613
 
611
614
  pullMoreInput:
612
615
  if (!BrotliPullByte(br)) {
613
- return BROTLI_NEEDS_MORE_INPUT;
616
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
614
617
  }
615
618
  }
616
- return BROTLI_SUCCESS;
619
+ return BROTLI_DECODER_SUCCESS;
617
620
  }
618
621
 
619
622
  /* Reads and decodes 15..18 codes using static prefix code.
620
623
  Each code is 2..4 bits long. In total 30..72 bits are used. */
621
- static BrotliErrorCode ReadCodeLengthCodeLengths(BrotliState* s) {
624
+ static BrotliDecoderErrorCode ReadCodeLengthCodeLengths(BrotliDecoderState* s) {
622
625
  BrotliBitReader* br = &s->br;
623
626
  uint32_t num_codes = s->repeat;
624
627
  unsigned space = s->space;
625
628
  uint32_t i = s->sub_loop_counter;
626
- for (; i < CODE_LENGTH_CODES; ++i) {
629
+ for (; i < BROTLI_CODE_LENGTH_CODES; ++i) {
627
630
  const uint8_t code_len_idx = kCodeLengthCodeOrder[i];
628
631
  uint32_t ix;
629
632
  uint32_t v;
@@ -639,7 +642,7 @@ static BrotliErrorCode ReadCodeLengthCodeLengths(BrotliState* s) {
639
642
  s->repeat = num_codes;
640
643
  s->space = space;
641
644
  s->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX;
642
- return BROTLI_NEEDS_MORE_INPUT;
645
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
643
646
  }
644
647
  }
645
648
  v = kCodeLengthPrefixValue[ix];
@@ -657,9 +660,9 @@ static BrotliErrorCode ReadCodeLengthCodeLengths(BrotliState* s) {
657
660
  }
658
661
  }
659
662
  if (!(num_codes == 1 || space == 0)) {
660
- return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_CL_SPACE);
663
+ return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_CL_SPACE);
661
664
  }
662
- return BROTLI_SUCCESS;
665
+ return BROTLI_DECODER_SUCCESS;
663
666
  }
664
667
 
665
668
  /* Decodes the Huffman tables.
@@ -674,10 +677,10 @@ static BrotliErrorCode ReadCodeLengthCodeLengths(BrotliState* s) {
674
677
  B.2) Decoded table is used to decode code lengths of symbols in resulting
675
678
  Huffman table. In worst case 3520 bits are read.
676
679
  */
677
- static BrotliErrorCode ReadHuffmanCode(uint32_t alphabet_size,
678
- HuffmanCode* table,
679
- uint32_t* opt_table_size,
680
- BrotliState* s) {
680
+ static BrotliDecoderErrorCode ReadHuffmanCode(uint32_t alphabet_size,
681
+ HuffmanCode* table,
682
+ uint32_t* opt_table_size,
683
+ BrotliDecoderState* s) {
681
684
  BrotliBitReader* br = &s->br;
682
685
  /* Unnecessary masking, but might be good for safety. */
683
686
  alphabet_size &= 0x3ff;
@@ -685,7 +688,7 @@ static BrotliErrorCode ReadHuffmanCode(uint32_t alphabet_size,
685
688
  switch (s->substate_huffman) {
686
689
  case BROTLI_STATE_HUFFMAN_NONE:
687
690
  if (!BrotliSafeReadBits(br, 2, &s->sub_loop_counter)) {
688
- return BROTLI_NEEDS_MORE_INPUT;
691
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
689
692
  }
690
693
  BROTLI_LOG_UINT(s->sub_loop_counter);
691
694
  /* The value is used as follows:
@@ -707,13 +710,14 @@ static BrotliErrorCode ReadHuffmanCode(uint32_t alphabet_size,
707
710
  /* Read symbols, codes & code lengths directly. */
708
711
  if (!BrotliSafeReadBits(br, 2, &s->symbol)) { /* num_symbols */
709
712
  s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_SIZE;
710
- return BROTLI_NEEDS_MORE_INPUT;
713
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
711
714
  }
712
715
  s->sub_loop_counter = 0;
713
716
  /* No break, transit to the next state. */
714
717
  case BROTLI_STATE_HUFFMAN_SIMPLE_READ: {
715
- BrotliErrorCode result = ReadSimpleHuffmanSymbols(alphabet_size, s);
716
- if (result != BROTLI_SUCCESS) {
718
+ BrotliDecoderErrorCode result =
719
+ ReadSimpleHuffmanSymbols(alphabet_size, s);
720
+ if (result != BROTLI_DECODER_SUCCESS) {
717
721
  return result;
718
722
  }
719
723
  /* No break, transit to the next state. */
@@ -724,7 +728,7 @@ static BrotliErrorCode ReadHuffmanCode(uint32_t alphabet_size,
724
728
  uint32_t bits;
725
729
  if (!BrotliSafeReadBits(br, 1, &bits)) {
726
730
  s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_BUILD;
727
- return BROTLI_NEEDS_MORE_INPUT;
731
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
728
732
  }
729
733
  s->symbol += bits;
730
734
  }
@@ -735,14 +739,14 @@ static BrotliErrorCode ReadHuffmanCode(uint32_t alphabet_size,
735
739
  *opt_table_size = table_size;
736
740
  }
737
741
  s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
738
- return BROTLI_SUCCESS;
742
+ return BROTLI_DECODER_SUCCESS;
739
743
  }
740
744
 
741
745
  Complex: /* Decode Huffman-coded code lengths. */
742
746
  case BROTLI_STATE_HUFFMAN_COMPLEX: {
743
747
  uint32_t i;
744
- BrotliErrorCode result = ReadCodeLengthCodeLengths(s);
745
- if (result != BROTLI_SUCCESS) {
748
+ BrotliDecoderErrorCode result = ReadCodeLengthCodeLengths(s);
749
+ if (result != BROTLI_DECODER_SUCCESS) {
746
750
  return result;
747
751
  }
748
752
  BrotliBuildCodeLengthsHuffmanTable(s->table,
@@ -755,7 +759,7 @@ Complex: /* Decode Huffman-coded code lengths. */
755
759
  }
756
760
 
757
761
  s->symbol = 0;
758
- s->prev_code_len = kDefaultCodeLength;
762
+ s->prev_code_len = BROTLI_INITIAL_REPEATED_CODE_LENGTH;
759
763
  s->repeat = 0;
760
764
  s->repeat_code_len = 0;
761
765
  s->space = 32768;
@@ -764,17 +768,17 @@ Complex: /* Decode Huffman-coded code lengths. */
764
768
  }
765
769
  case BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS: {
766
770
  uint32_t table_size;
767
- BrotliErrorCode result = ReadSymbolCodeLengths(alphabet_size, s);
768
- if (result == BROTLI_NEEDS_MORE_INPUT) {
771
+ BrotliDecoderErrorCode result = ReadSymbolCodeLengths(alphabet_size, s);
772
+ if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) {
769
773
  result = SafeReadSymbolCodeLengths(alphabet_size, s);
770
774
  }
771
- if (result != BROTLI_SUCCESS) {
775
+ if (result != BROTLI_DECODER_SUCCESS) {
772
776
  return result;
773
777
  }
774
778
 
775
779
  if (s->space != 0) {
776
780
  BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", s->space));
777
- return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_HUFFMAN_SPACE);
781
+ return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE);
778
782
  }
779
783
  table_size = BrotliBuildHuffmanTable(
780
784
  table, HUFFMAN_TABLE_BITS, s->symbol_lists, s->code_length_histo);
@@ -782,11 +786,12 @@ Complex: /* Decode Huffman-coded code lengths. */
782
786
  *opt_table_size = table_size;
783
787
  }
784
788
  s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
785
- return BROTLI_SUCCESS;
789
+ return BROTLI_DECODER_SUCCESS;
786
790
  }
787
791
 
788
792
  default:
789
- return BROTLI_FAILURE(BROTLI_ERROR_UNREACHABLE);
793
+ return
794
+ BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE);
790
795
  }
791
796
  }
792
797
 
@@ -802,14 +807,13 @@ static BROTLI_INLINE uint32_t ReadBlockLength(const HuffmanCode* table,
802
807
 
803
808
  /* WARNING: if state is not BROTLI_STATE_READ_BLOCK_LENGTH_NONE, then
804
809
  reading can't be continued with ReadBlockLength. */
805
- static BROTLI_INLINE int SafeReadBlockLength(BrotliState* s,
806
- uint32_t* result,
807
- const HuffmanCode* table,
808
- BrotliBitReader* br) {
810
+ static BROTLI_INLINE BROTLI_BOOL SafeReadBlockLength(
811
+ BrotliDecoderState* s, uint32_t* result, const HuffmanCode* table,
812
+ BrotliBitReader* br) {
809
813
  uint32_t index;
810
814
  if (s->substate_read_block_length == BROTLI_STATE_READ_BLOCK_LENGTH_NONE) {
811
815
  if (!SafeReadSymbol(table, br, &index)) {
812
- return 0;
816
+ return BROTLI_FALSE;
813
817
  }
814
818
  } else {
815
819
  index = s->block_length_index;
@@ -820,11 +824,11 @@ static BROTLI_INLINE int SafeReadBlockLength(BrotliState* s,
820
824
  if (!BrotliSafeReadBits(br, nbits, &bits)) {
821
825
  s->block_length_index = index;
822
826
  s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX;
823
- return 0;
827
+ return BROTLI_FALSE;
824
828
  }
825
829
  *result = kBlockLengthPrefixCode[index].offset + bits;
826
830
  s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
827
- return 1;
831
+ return BROTLI_TRUE;
828
832
  }
829
833
  }
830
834
 
@@ -843,8 +847,8 @@ static BROTLI_INLINE int SafeReadBlockLength(BrotliState* s,
843
847
  Most of input values are 0 and 1. To reduce number of branches, we replace
844
848
  inner for loop with do-while.
845
849
  */
846
- static BROTLI_NOINLINE void InverseMoveToFrontTransform(uint8_t* v,
847
- uint32_t v_len, BrotliState* state) {
850
+ static BROTLI_NOINLINE void InverseMoveToFrontTransform(
851
+ uint8_t* v, uint32_t v_len, BrotliDecoderState* state) {
848
852
  /* Reinitialize elements that could have been changed. */
849
853
  uint32_t i = 4;
850
854
  uint32_t upper_bound = state->mtf_upper_bound;
@@ -880,8 +884,8 @@ static BROTLI_NOINLINE void InverseMoveToFrontTransform(uint8_t* v,
880
884
  }
881
885
 
882
886
  /* Decodes a series of Huffman table using ReadHuffmanCode function. */
883
- static BrotliErrorCode HuffmanTreeGroupDecode(HuffmanTreeGroup* group,
884
- BrotliState* s) {
887
+ static BrotliDecoderErrorCode HuffmanTreeGroupDecode(
888
+ HuffmanTreeGroup* group, BrotliDecoderState* s) {
885
889
  if (s->substate_tree_group != BROTLI_STATE_TREE_GROUP_LOOP) {
886
890
  s->next = group->codes;
887
891
  s->htree_index = 0;
@@ -889,15 +893,15 @@ static BrotliErrorCode HuffmanTreeGroupDecode(HuffmanTreeGroup* group,
889
893
  }
890
894
  while (s->htree_index < group->num_htrees) {
891
895
  uint32_t table_size;
892
- BrotliErrorCode result =
896
+ BrotliDecoderErrorCode result =
893
897
  ReadHuffmanCode(group->alphabet_size, s->next, &table_size, s);
894
- if (result != BROTLI_SUCCESS) return result;
898
+ if (result != BROTLI_DECODER_SUCCESS) return result;
895
899
  group->htrees[s->htree_index] = s->next;
896
900
  s->next += table_size;
897
901
  ++s->htree_index;
898
902
  }
899
903
  s->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;
900
- return BROTLI_SUCCESS;
904
+ return BROTLI_DECODER_SUCCESS;
901
905
  }
902
906
 
903
907
  /* Decodes a context map.
@@ -909,17 +913,17 @@ static BrotliErrorCode HuffmanTreeGroupDecode(HuffmanTreeGroup* group,
909
913
  3) Read context map items; "0" values could be run-length encoded.
910
914
  4) Optionally, apply InverseMoveToFront transform to the resulting map.
911
915
  */
912
- static BrotliErrorCode DecodeContextMap(uint32_t context_map_size,
913
- uint32_t* num_htrees,
914
- uint8_t** context_map_arg,
915
- BrotliState* s) {
916
+ static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size,
917
+ uint32_t* num_htrees,
918
+ uint8_t** context_map_arg,
919
+ BrotliDecoderState* s) {
916
920
  BrotliBitReader* br = &s->br;
917
- BrotliErrorCode result = BROTLI_SUCCESS;
921
+ BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS;
918
922
 
919
923
  switch ((int)s->substate_context_map) {
920
924
  case BROTLI_STATE_CONTEXT_MAP_NONE:
921
925
  result = DecodeVarLenUint8(s, br, num_htrees);
922
- if (result != BROTLI_SUCCESS) {
926
+ if (result != BROTLI_DECODER_SUCCESS) {
923
927
  return result;
924
928
  }
925
929
  (*num_htrees)++;
@@ -928,11 +932,11 @@ static BrotliErrorCode DecodeContextMap(uint32_t context_map_size,
928
932
  BROTLI_LOG_UINT(*num_htrees);
929
933
  *context_map_arg = (uint8_t*)BROTLI_ALLOC(s, (size_t)context_map_size);
930
934
  if (*context_map_arg == 0) {
931
- return BROTLI_FAILURE(BROTLI_ERROR_ALLOC_CONTEXT_MAP);
935
+ return BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP);
932
936
  }
933
937
  if (*num_htrees <= 1) {
934
938
  memset(*context_map_arg, 0, (size_t)context_map_size);
935
- return BROTLI_SUCCESS;
939
+ return BROTLI_DECODER_SUCCESS;
936
940
  }
937
941
  s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_READ_PREFIX;
938
942
  /* No break, continue to next state. */
@@ -941,7 +945,7 @@ static BrotliErrorCode DecodeContextMap(uint32_t context_map_size,
941
945
  /* In next stage ReadHuffmanCode uses at least 4 bits, so it is safe
942
946
  to peek 4 bits ahead. */
943
947
  if (!BrotliSafeGetBits(br, 5, &bits)) {
944
- return BROTLI_NEEDS_MORE_INPUT;
948
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
945
949
  }
946
950
  if ((bits & 1) != 0) { /* Use RLE for zeroes. */
947
951
  s->max_run_length_prefix = (bits >> 1) + 1;
@@ -957,7 +961,7 @@ static BrotliErrorCode DecodeContextMap(uint32_t context_map_size,
957
961
  case BROTLI_STATE_CONTEXT_MAP_HUFFMAN:
958
962
  result = ReadHuffmanCode(*num_htrees + s->max_run_length_prefix,
959
963
  s->context_map_table, NULL, s);
960
- if (result != BROTLI_SUCCESS) return result;
964
+ if (result != BROTLI_DECODER_SUCCESS) return result;
961
965
  s->code = 0xFFFF;
962
966
  s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_DECODE;
963
967
  /* No break, continue to next state. */
@@ -973,7 +977,7 @@ static BrotliErrorCode DecodeContextMap(uint32_t context_map_size,
973
977
  if (!SafeReadSymbol(s->context_map_table, br, &code)) {
974
978
  s->code = 0xFFFF;
975
979
  s->context_index = context_index;
976
- return BROTLI_NEEDS_MORE_INPUT;
980
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
977
981
  }
978
982
  BROTLI_LOG_UINT(code);
979
983
 
@@ -992,12 +996,13 @@ rleCode:
992
996
  if (!BrotliSafeReadBits(br, code, &reps)) {
993
997
  s->code = code;
994
998
  s->context_index = context_index;
995
- return BROTLI_NEEDS_MORE_INPUT;
999
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
996
1000
  }
997
1001
  reps += 1U << code;
998
1002
  BROTLI_LOG_UINT(reps);
999
1003
  if (context_index + reps > context_map_size) {
1000
- return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_CONTEXT_MAP_REPEAT);
1004
+ return
1005
+ BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT);
1001
1006
  }
1002
1007
  do {
1003
1008
  context_map[context_index++] = 0;
@@ -1010,23 +1015,24 @@ rleCode:
1010
1015
  uint32_t bits;
1011
1016
  if (!BrotliSafeReadBits(br, 1, &bits)) {
1012
1017
  s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_TRANSFORM;
1013
- return BROTLI_NEEDS_MORE_INPUT;
1018
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
1014
1019
  }
1015
1020
  if (bits != 0) {
1016
1021
  InverseMoveToFrontTransform(*context_map_arg, context_map_size, s);
1017
1022
  }
1018
1023
  s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;
1019
- return BROTLI_SUCCESS;
1024
+ return BROTLI_DECODER_SUCCESS;
1020
1025
  }
1021
1026
  default:
1022
- return BROTLI_FAILURE(BROTLI_ERROR_UNREACHABLE);
1027
+ return
1028
+ BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE);
1023
1029
  }
1024
1030
  }
1025
1031
 
1026
1032
  /* Decodes a command or literal and updates block type ringbuffer.
1027
1033
  Reads 3..54 bits. */
1028
- static BROTLI_INLINE int DecodeBlockTypeAndLength(int safe,
1029
- BrotliState* s, int tree_type) {
1034
+ static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength(
1035
+ int safe, BrotliDecoderState* s, int tree_type) {
1030
1036
  uint32_t max_block_type = s->num_block_types[tree_type];
1031
1037
  const HuffmanCode* type_tree = &s->block_type_trees[
1032
1038
  tree_type * BROTLI_HUFFMAN_MAX_SIZE_258];
@@ -1043,11 +1049,11 @@ static BROTLI_INLINE int DecodeBlockTypeAndLength(int safe,
1043
1049
  } else {
1044
1050
  BrotliBitReaderState memento;
1045
1051
  BrotliBitReaderSaveState(br, &memento);
1046
- if (!SafeReadSymbol(type_tree, br, &block_type)) return 0;
1052
+ if (!SafeReadSymbol(type_tree, br, &block_type)) return BROTLI_FALSE;
1047
1053
  if (!SafeReadBlockLength(s, &s->block_length[tree_type], len_tree, br)) {
1048
1054
  s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
1049
1055
  BrotliBitReaderRestoreState(br, &memento);
1050
- return 0;
1056
+ return BROTLI_FALSE;
1051
1057
  }
1052
1058
  }
1053
1059
 
@@ -1063,18 +1069,19 @@ static BROTLI_INLINE int DecodeBlockTypeAndLength(int safe,
1063
1069
  }
1064
1070
  ringbuffer[0] = ringbuffer[1];
1065
1071
  ringbuffer[1] = block_type;
1066
- return 1;
1072
+ return BROTLI_TRUE;
1067
1073
  }
1068
1074
 
1069
- static BROTLI_INLINE void DetectTrivialLiteralBlockTypes(BrotliState* s) {
1075
+ static BROTLI_INLINE void DetectTrivialLiteralBlockTypes(
1076
+ BrotliDecoderState* s) {
1070
1077
  size_t i;
1071
1078
  for (i = 0; i < 8; ++i) s->trivial_literal_contexts[i] = 0;
1072
1079
  for (i = 0; i < s->num_block_types[0]; i++) {
1073
- size_t offset = i << kLiteralContextBits;
1080
+ size_t offset = i << BROTLI_LITERAL_CONTEXT_BITS;
1074
1081
  size_t error = 0;
1075
1082
  size_t sample = s->context_map[offset];
1076
1083
  size_t j;
1077
- for (j = 0; j < (1u << kLiteralContextBits);) {
1084
+ for (j = 0; j < (1u << BROTLI_LITERAL_CONTEXT_BITS);) {
1078
1085
  BROTLI_REPEAT(4, error |= s->context_map[offset + j++] ^ sample;)
1079
1086
  }
1080
1087
  if (error == 0) {
@@ -1083,11 +1090,11 @@ static BROTLI_INLINE void DetectTrivialLiteralBlockTypes(BrotliState* s) {
1083
1090
  }
1084
1091
  }
1085
1092
 
1086
- static BROTLI_INLINE void PrepareLiteralDecoding(BrotliState* s) {
1093
+ static BROTLI_INLINE void PrepareLiteralDecoding(BrotliDecoderState* s) {
1087
1094
  uint8_t context_mode;
1088
1095
  size_t trivial;
1089
1096
  uint32_t block_type = s->block_type_rb[1];
1090
- uint32_t context_offset = block_type << kLiteralContextBits;
1097
+ uint32_t context_offset = block_type << BROTLI_LITERAL_CONTEXT_BITS;
1091
1098
  s->context_map_slice = s->context_map + context_offset;
1092
1099
  trivial = s->trivial_literal_contexts[block_type >> 5];
1093
1100
  s->trivial_literal_context = (trivial >> (block_type & 31)) & 1;
@@ -1099,76 +1106,84 @@ static BROTLI_INLINE void PrepareLiteralDecoding(BrotliState* s) {
1099
1106
 
1100
1107
  /* Decodes the block type and updates the state for literal context.
1101
1108
  Reads 3..54 bits. */
1102
- static BROTLI_INLINE int DecodeLiteralBlockSwitchInternal(int safe,
1103
- BrotliState* s) {
1109
+ static BROTLI_INLINE BROTLI_BOOL DecodeLiteralBlockSwitchInternal(
1110
+ int safe, BrotliDecoderState* s) {
1104
1111
  if (!DecodeBlockTypeAndLength(safe, s, 0)) {
1105
- return 0;
1112
+ return BROTLI_FALSE;
1106
1113
  }
1107
1114
  PrepareLiteralDecoding(s);
1108
- return 1;
1115
+ return BROTLI_TRUE;
1109
1116
  }
1110
1117
 
1111
- static void BROTLI_NOINLINE DecodeLiteralBlockSwitch(BrotliState* s) {
1118
+ static void BROTLI_NOINLINE DecodeLiteralBlockSwitch(BrotliDecoderState* s) {
1112
1119
  DecodeLiteralBlockSwitchInternal(0, s);
1113
1120
  }
1114
1121
 
1115
- static int BROTLI_NOINLINE SafeDecodeLiteralBlockSwitch(BrotliState* s) {
1122
+ static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeLiteralBlockSwitch(
1123
+ BrotliDecoderState* s) {
1116
1124
  return DecodeLiteralBlockSwitchInternal(1, s);
1117
1125
  }
1118
1126
 
1119
1127
  /* Block switch for insert/copy length.
1120
1128
  Reads 3..54 bits. */
1121
- static BROTLI_INLINE int DecodeCommandBlockSwitchInternal(int safe,
1122
- BrotliState* s) {
1129
+ static BROTLI_INLINE BROTLI_BOOL DecodeCommandBlockSwitchInternal(
1130
+ int safe, BrotliDecoderState* s) {
1123
1131
  if (!DecodeBlockTypeAndLength(safe, s, 1)) {
1124
- return 0;
1132
+ return BROTLI_FALSE;
1125
1133
  }
1126
1134
  s->htree_command = s->insert_copy_hgroup.htrees[s->block_type_rb[3]];
1127
- return 1;
1135
+ return BROTLI_TRUE;
1128
1136
  }
1129
1137
 
1130
- static void BROTLI_NOINLINE DecodeCommandBlockSwitch(BrotliState* s) {
1138
+ static void BROTLI_NOINLINE DecodeCommandBlockSwitch(BrotliDecoderState* s) {
1131
1139
  DecodeCommandBlockSwitchInternal(0, s);
1132
1140
  }
1133
- static int BROTLI_NOINLINE SafeDecodeCommandBlockSwitch(BrotliState* s) {
1141
+ static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeCommandBlockSwitch(
1142
+ BrotliDecoderState* s) {
1134
1143
  return DecodeCommandBlockSwitchInternal(1, s);
1135
1144
  }
1136
1145
 
1137
1146
  /* Block switch for distance codes.
1138
1147
  Reads 3..54 bits. */
1139
- static BROTLI_INLINE int DecodeDistanceBlockSwitchInternal(int safe,
1140
- BrotliState* s) {
1148
+ static BROTLI_INLINE BROTLI_BOOL DecodeDistanceBlockSwitchInternal(
1149
+ int safe, BrotliDecoderState* s) {
1141
1150
  if (!DecodeBlockTypeAndLength(safe, s, 2)) {
1142
- return 0;
1151
+ return BROTLI_FALSE;
1143
1152
  }
1144
- s->dist_context_map_slice =
1145
- s->dist_context_map + (s->block_type_rb[5] << kDistanceContextBits);
1153
+ s->dist_context_map_slice = s->dist_context_map +
1154
+ (s->block_type_rb[5] << BROTLI_DISTANCE_CONTEXT_BITS);
1146
1155
  s->dist_htree_index = s->dist_context_map_slice[s->distance_context];
1147
- return 1;
1156
+ return BROTLI_TRUE;
1148
1157
  }
1149
1158
 
1150
- static void BROTLI_NOINLINE DecodeDistanceBlockSwitch(BrotliState* s) {
1159
+ static void BROTLI_NOINLINE DecodeDistanceBlockSwitch(BrotliDecoderState* s) {
1151
1160
  DecodeDistanceBlockSwitchInternal(0, s);
1152
1161
  }
1153
1162
 
1154
- static int BROTLI_NOINLINE SafeDecodeDistanceBlockSwitch(BrotliState* s) {
1163
+ static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeDistanceBlockSwitch(
1164
+ BrotliDecoderState* s) {
1155
1165
  return DecodeDistanceBlockSwitchInternal(1, s);
1156
1166
  }
1157
1167
 
1158
- static BrotliErrorCode BROTLI_NOINLINE WriteRingBuffer(size_t* available_out,
1159
- uint8_t** next_out, size_t* total_out, BrotliState* s) {
1160
- size_t pos = (s->pos > s->ringbuffer_size) ? (size_t)s->ringbuffer_size
1161
- : (size_t)(s->pos);
1168
+ static size_t UnwrittenBytes(const BrotliDecoderState* s, BROTLI_BOOL wrap) {
1169
+ size_t pos = wrap && s->pos > s->ringbuffer_size ?
1170
+ (size_t)s->ringbuffer_size : (size_t)(s->pos);
1171
+ size_t partial_pos_rb = (s->rb_roundtrips * (size_t)s->ringbuffer_size) + pos;
1172
+ return partial_pos_rb - s->partial_pos_out;
1173
+ }
1174
+
1175
+ static BrotliDecoderErrorCode BROTLI_NOINLINE WriteRingBuffer(
1176
+ BrotliDecoderState* s, size_t* available_out, uint8_t** next_out,
1177
+ size_t* total_out) {
1162
1178
  uint8_t* start =
1163
1179
  s->ringbuffer + (s->partial_pos_out & (size_t)s->ringbuffer_mask);
1164
- size_t partial_pos_rb = (s->rb_roundtrips * (size_t)s->ringbuffer_size) + pos;
1165
- size_t to_write = (partial_pos_rb - s->partial_pos_out);
1180
+ size_t to_write = UnwrittenBytes(s, BROTLI_TRUE);
1166
1181
  size_t num_written = *available_out;
1167
1182
  if (num_written > to_write) {
1168
1183
  num_written = to_write;
1169
1184
  }
1170
1185
  if (s->meta_block_remaining_len < 0) {
1171
- return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_BLOCK_LENGTH_1);
1186
+ return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1);
1172
1187
  }
1173
1188
  memcpy(*next_out, start, num_written);
1174
1189
  *next_out += num_written;
@@ -1178,14 +1193,14 @@ static BrotliErrorCode BROTLI_NOINLINE WriteRingBuffer(size_t* available_out,
1178
1193
  s->partial_pos_out += num_written;
1179
1194
  if (total_out) *total_out = s->partial_pos_out;
1180
1195
  if (num_written < to_write) {
1181
- return BROTLI_NEEDS_MORE_OUTPUT;
1196
+ return BROTLI_DECODER_NEEDS_MORE_OUTPUT;
1182
1197
  }
1183
1198
 
1184
1199
  if (s->pos >= s->ringbuffer_size) {
1185
1200
  s->pos -= s->ringbuffer_size;
1186
1201
  s->rb_roundtrips++;
1187
1202
  }
1188
- return BROTLI_SUCCESS;
1203
+ return BROTLI_DECODER_SUCCESS;
1189
1204
  }
1190
1205
 
1191
1206
  /* Allocates ringbuffer.
@@ -1198,7 +1213,8 @@ static BrotliErrorCode BROTLI_NOINLINE WriteRingBuffer(size_t* available_out,
1198
1213
 
1199
1214
  Custom dictionary, if any, is copied to the end of ringbuffer.
1200
1215
  */
1201
- static int BROTLI_NOINLINE BrotliAllocateRingBuffer(BrotliState* s) {
1216
+ static BROTLI_BOOL BROTLI_NOINLINE BrotliAllocateRingBuffer(
1217
+ BrotliDecoderState* s) {
1202
1218
  /* We need the slack region for the following reasons:
1203
1219
  - doing up to two 16-byte copies for fast backward copying
1204
1220
  - inserting transformed dictionary word (5 prefix + 24 base + 8 suffix) */
@@ -1206,7 +1222,7 @@ static int BROTLI_NOINLINE BrotliAllocateRingBuffer(BrotliState* s) {
1206
1222
  s->ringbuffer = (uint8_t*)BROTLI_ALLOC(s, (size_t)(s->ringbuffer_size +
1207
1223
  kRingBufferWriteAheadSlack));
1208
1224
  if (s->ringbuffer == 0) {
1209
- return 0;
1225
+ return BROTLI_FALSE;
1210
1226
  }
1211
1227
 
1212
1228
  s->ringbuffer_end = s->ringbuffer + s->ringbuffer_size;
@@ -1219,15 +1235,15 @@ static int BROTLI_NOINLINE BrotliAllocateRingBuffer(BrotliState* s) {
1219
1235
  s->custom_dict, (size_t)s->custom_dict_size);
1220
1236
  }
1221
1237
 
1222
- return 1;
1238
+ return BROTLI_TRUE;
1223
1239
  }
1224
1240
 
1225
- static BrotliErrorCode BROTLI_NOINLINE CopyUncompressedBlockToOutput(
1241
+ static BrotliDecoderErrorCode BROTLI_NOINLINE CopyUncompressedBlockToOutput(
1226
1242
  size_t* available_out, uint8_t** next_out, size_t* total_out,
1227
- BrotliState* s) {
1243
+ BrotliDecoderState* s) {
1228
1244
  /* TODO: avoid allocation for single uncompressed block. */
1229
1245
  if (!s->ringbuffer && !BrotliAllocateRingBuffer(s)) {
1230
- return BROTLI_FAILURE(BROTLI_ERROR_ALLOC_RING_BUFFER_1);
1246
+ return BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1);
1231
1247
  }
1232
1248
 
1233
1249
  /* State machine */
@@ -1247,17 +1263,17 @@ static BrotliErrorCode BROTLI_NOINLINE CopyUncompressedBlockToOutput(
1247
1263
  s->meta_block_remaining_len -= nbytes;
1248
1264
  if (s->pos < s->ringbuffer_size) {
1249
1265
  if (s->meta_block_remaining_len == 0) {
1250
- return BROTLI_SUCCESS;
1266
+ return BROTLI_DECODER_SUCCESS;
1251
1267
  }
1252
- return BROTLI_NEEDS_MORE_INPUT;
1268
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
1253
1269
  }
1254
1270
  s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_WRITE;
1255
1271
  /* No break, continue to next state */
1256
1272
  }
1257
1273
  case BROTLI_STATE_UNCOMPRESSED_WRITE: {
1258
- BrotliErrorCode result =
1259
- WriteRingBuffer(available_out, next_out, total_out, s);
1260
- if (result != BROTLI_SUCCESS) {
1274
+ BrotliDecoderErrorCode result =
1275
+ WriteRingBuffer(s, available_out, next_out, total_out);
1276
+ if (result != BROTLI_DECODER_SUCCESS) {
1261
1277
  return result;
1262
1278
  }
1263
1279
  s->max_distance = s->max_backward_distance;
@@ -1269,30 +1285,41 @@ static BrotliErrorCode BROTLI_NOINLINE CopyUncompressedBlockToOutput(
1269
1285
  BROTLI_DCHECK(0); /* Unreachable */
1270
1286
  }
1271
1287
 
1272
- int BrotliDecompressedSize(size_t encoded_size,
1273
- const uint8_t* encoded_buffer,
1274
- size_t* decoded_size) {
1275
- BrotliState s;
1276
- int next_block_header;
1277
- BrotliStateInit(&s);
1278
- s.br.next_in = encoded_buffer;
1279
- s.br.avail_in = encoded_size;
1280
- if (!BrotliWarmupBitReader(&s.br)) {
1281
- return 0;
1282
- }
1283
- DecodeWindowBits(&s.br);
1284
- if (DecodeMetaBlockLength(&s, &s.br) != BROTLI_SUCCESS) {
1285
- return 0;
1286
- }
1287
- *decoded_size = (size_t)s.meta_block_remaining_len;
1288
- if (s.is_last_metablock) {
1289
- return 1;
1290
- }
1291
- if (!s.is_uncompressed || !BrotliJumpToByteBoundary(&s.br)) {
1292
- return 0;
1288
+ BROTLI_BOOL BrotliDecompressedSize(size_t encoded_size,
1289
+ const uint8_t* encoded_buffer,
1290
+ size_t* decoded_size) {
1291
+ size_t total_size = 0;
1292
+ BrotliDecoderState s;
1293
+ BrotliBitReader* br;
1294
+ BrotliDecoderStateInit(&s);
1295
+ br = &s.br;
1296
+ *decoded_size = 0;
1297
+ br->next_in = encoded_buffer;
1298
+ br->avail_in = encoded_size;
1299
+ if (!BrotliWarmupBitReader(br)) return BROTLI_FALSE;
1300
+ DecodeWindowBits(br);
1301
+ while (1) {
1302
+ size_t block_size;
1303
+ if (DecodeMetaBlockLength(&s, br) != BROTLI_DECODER_SUCCESS) {
1304
+ return BROTLI_FALSE;
1305
+ }
1306
+ block_size = (size_t)s.meta_block_remaining_len;
1307
+ if (!s.is_metadata) {
1308
+ if ((block_size + total_size) < total_size) return BROTLI_FALSE;
1309
+ total_size += block_size;
1310
+ }
1311
+ if (s.is_last_metablock) {
1312
+ *decoded_size = total_size;
1313
+ return BROTLI_TRUE;
1314
+ }
1315
+ if (!s.is_uncompressed && !s.is_metadata) return BROTLI_FALSE;
1316
+ if (!BrotliJumpToByteBoundary(br)) return BROTLI_FALSE;
1317
+ BrotliBitReaderUnload(br);
1318
+ if (br->avail_in < block_size) return BROTLI_FALSE;
1319
+ br->avail_in -= block_size;
1320
+ br->next_in += block_size;
1321
+ if (!BrotliWarmupBitReader(br)) return BROTLI_FALSE;
1293
1322
  }
1294
- next_block_header = BrotliPeekByte(&s.br, (size_t)s.meta_block_remaining_len);
1295
- return (next_block_header != -1) && ((next_block_header & 3) == 3);
1296
1323
  }
1297
1324
 
1298
1325
  /* Calculates the smallest feasible ring buffer.
@@ -1302,9 +1329,9 @@ int BrotliDecompressedSize(size_t encoded_size,
1302
1329
 
1303
1330
  When this method is called, metablock size and flags MUST be decoded.
1304
1331
  */
1305
- static void BROTLI_NOINLINE BrotliCalculateRingBufferSize(BrotliState* s,
1306
- BrotliBitReader* br) {
1307
- int is_last = s->is_last_metablock;
1332
+ static void BROTLI_NOINLINE BrotliCalculateRingBufferSize(
1333
+ BrotliDecoderState* s, BrotliBitReader* br) {
1334
+ BROTLI_BOOL is_last = TO_BROTLI_BOOL(s->is_last_metablock);
1308
1335
  int window_size = 1 << s->window_bits;
1309
1336
  s->ringbuffer_size = window_size;
1310
1337
 
@@ -1313,7 +1340,7 @@ static void BROTLI_NOINLINE BrotliCalculateRingBufferSize(BrotliState* s,
1313
1340
  BrotliPeekByte(br, (size_t)s->meta_block_remaining_len);
1314
1341
  if (next_block_header != -1) { /* Peek succeeded */
1315
1342
  if ((next_block_header & 3) == 3) { /* ISLAST and ISEMPTY */
1316
- is_last = 1;
1343
+ is_last = BROTLI_TRUE;
1317
1344
  }
1318
1345
  }
1319
1346
  }
@@ -1331,7 +1358,7 @@ static void BROTLI_NOINLINE BrotliCalculateRingBufferSize(BrotliState* s,
1331
1358
  }
1332
1359
 
1333
1360
  /* Reads 1..256 2-bit context modes. */
1334
- static BrotliErrorCode ReadContextModes(BrotliState* s) {
1361
+ static BrotliDecoderErrorCode ReadContextModes(BrotliDecoderState* s) {
1335
1362
  BrotliBitReader* br = &s->br;
1336
1363
  int i = s->loop_counter;
1337
1364
 
@@ -1339,16 +1366,16 @@ static BrotliErrorCode ReadContextModes(BrotliState* s) {
1339
1366
  uint32_t bits;
1340
1367
  if (!BrotliSafeReadBits(br, 2, &bits)) {
1341
1368
  s->loop_counter = i;
1342
- return BROTLI_NEEDS_MORE_INPUT;
1369
+ return BROTLI_DECODER_NEEDS_MORE_INPUT;
1343
1370
  }
1344
1371
  s->context_modes[i] = (uint8_t)(bits << 1);
1345
1372
  BROTLI_LOG_ARRAY_INDEX(s->context_modes, i);
1346
1373
  i++;
1347
1374
  }
1348
- return BROTLI_SUCCESS;
1375
+ return BROTLI_DECODER_SUCCESS;
1349
1376
  }
1350
1377
 
1351
- static BROTLI_INLINE void TakeDistanceFromRingBuffer(BrotliState* s) {
1378
+ static BROTLI_INLINE void TakeDistanceFromRingBuffer(BrotliDecoderState* s) {
1352
1379
  if (s->distance_code == 0) {
1353
1380
  --s->dist_rb_idx;
1354
1381
  s->distance_code = s->dist_rb[s->dist_rb_idx & 3];
@@ -1377,19 +1404,19 @@ static BROTLI_INLINE void TakeDistanceFromRingBuffer(BrotliState* s) {
1377
1404
  }
1378
1405
  }
1379
1406
 
1380
- static BROTLI_INLINE int SafeReadBits(
1407
+ static BROTLI_INLINE BROTLI_BOOL SafeReadBits(
1381
1408
  BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
1382
1409
  if (n_bits != 0) {
1383
1410
  return BrotliSafeReadBits(br, n_bits, val);
1384
1411
  } else {
1385
1412
  *val = 0;
1386
- return 1;
1413
+ return BROTLI_TRUE;
1387
1414
  }
1388
1415
  }
1389
1416
 
1390
1417
  /* Precondition: s->distance_code < 0 */
1391
- static BROTLI_INLINE int ReadDistanceInternal(int safe,
1392
- BrotliState* s, BrotliBitReader* br) {
1418
+ static BROTLI_INLINE BROTLI_BOOL ReadDistanceInternal(
1419
+ int safe, BrotliDecoderState* s, BrotliBitReader* br) {
1393
1420
  int distval;
1394
1421
  BrotliBitReaderState memento;
1395
1422
  HuffmanCode* distance_tree = s->distance_hgroup.htrees[s->dist_htree_index];
@@ -1399,7 +1426,7 @@ static BROTLI_INLINE int ReadDistanceInternal(int safe,
1399
1426
  uint32_t code;
1400
1427
  BrotliBitReaderSaveState(br, &memento);
1401
1428
  if (!SafeReadSymbol(distance_tree, br, &code)) {
1402
- return 0;
1429
+ return BROTLI_FALSE;
1403
1430
  }
1404
1431
  s->distance_code = (int)code;
1405
1432
  }
@@ -1408,7 +1435,7 @@ static BROTLI_INLINE int ReadDistanceInternal(int safe,
1408
1435
  if ((s->distance_code & ~0xf) == 0) {
1409
1436
  TakeDistanceFromRingBuffer(s);
1410
1437
  --s->block_length[2];
1411
- return 1;
1438
+ return BROTLI_TRUE;
1412
1439
  }
1413
1440
  distval = s->distance_code - (int)s->num_direct_distance_codes;
1414
1441
  if (distval >= 0) {
@@ -1430,7 +1457,7 @@ static BROTLI_INLINE int ReadDistanceInternal(int safe,
1430
1457
  if (!SafeReadBits(br, nbits, &bits)) {
1431
1458
  s->distance_code = -1; /* Restore precondition. */
1432
1459
  BrotliBitReaderRestoreState(br, &memento);
1433
- return 0;
1460
+ return BROTLI_FALSE;
1434
1461
  }
1435
1462
  } else {
1436
1463
  bits = BrotliReadBits(br, nbits);
@@ -1440,21 +1467,23 @@ static BROTLI_INLINE int ReadDistanceInternal(int safe,
1440
1467
  ((offset + (int)bits) << s->distance_postfix_bits) + postfix;
1441
1468
  }
1442
1469
  }
1443
- s->distance_code = s->distance_code - NUM_DISTANCE_SHORT_CODES + 1;
1470
+ s->distance_code = s->distance_code - BROTLI_NUM_DISTANCE_SHORT_CODES + 1;
1444
1471
  --s->block_length[2];
1445
- return 1;
1472
+ return BROTLI_TRUE;
1446
1473
  }
1447
1474
 
1448
- static BROTLI_INLINE void ReadDistance(BrotliState* s, BrotliBitReader* br) {
1475
+ static BROTLI_INLINE void ReadDistance(
1476
+ BrotliDecoderState* s, BrotliBitReader* br) {
1449
1477
  ReadDistanceInternal(0, s, br);
1450
1478
  }
1451
1479
 
1452
- static BROTLI_INLINE int SafeReadDistance(BrotliState* s, BrotliBitReader* br) {
1480
+ static BROTLI_INLINE BROTLI_BOOL SafeReadDistance(
1481
+ BrotliDecoderState* s, BrotliBitReader* br) {
1453
1482
  return ReadDistanceInternal(1, s, br);
1454
1483
  }
1455
1484
 
1456
- static BROTLI_INLINE int ReadCommandInternal(int safe,
1457
- BrotliState* s, BrotliBitReader* br, int* insert_length) {
1485
+ static BROTLI_INLINE BROTLI_BOOL ReadCommandInternal(
1486
+ int safe, BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) {
1458
1487
  uint32_t cmd_code;
1459
1488
  uint32_t insert_len_extra = 0;
1460
1489
  uint32_t copy_length;
@@ -1465,7 +1494,7 @@ static BROTLI_INLINE int ReadCommandInternal(int safe,
1465
1494
  } else {
1466
1495
  BrotliBitReaderSaveState(br, &memento);
1467
1496
  if (!SafeReadSymbol(s->htree_command, br, &cmd_code)) {
1468
- return 0;
1497
+ return BROTLI_FALSE;
1469
1498
  }
1470
1499
  }
1471
1500
  v = kCmdLut[cmd_code];
@@ -1482,54 +1511,54 @@ static BROTLI_INLINE int ReadCommandInternal(int safe,
1482
1511
  if (!SafeReadBits(br, v.insert_len_extra_bits, &insert_len_extra) ||
1483
1512
  !SafeReadBits(br, v.copy_len_extra_bits, &copy_length)) {
1484
1513
  BrotliBitReaderRestoreState(br, &memento);
1485
- return 0;
1514
+ return BROTLI_FALSE;
1486
1515
  }
1487
1516
  }
1488
1517
  s->copy_length = (int)copy_length + v.copy_len_offset;
1489
1518
  --s->block_length[1];
1490
1519
  *insert_length += (int)insert_len_extra;
1491
- return 1;
1520
+ return BROTLI_TRUE;
1492
1521
  }
1493
1522
 
1494
- static BROTLI_INLINE void ReadCommand(BrotliState* s, BrotliBitReader* br,
1495
- int* insert_length) {
1523
+ static BROTLI_INLINE void ReadCommand(
1524
+ BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) {
1496
1525
  ReadCommandInternal(0, s, br, insert_length);
1497
1526
  }
1498
1527
 
1499
- static BROTLI_INLINE int SafeReadCommand(BrotliState* s, BrotliBitReader* br,
1500
- int* insert_length) {
1528
+ static BROTLI_INLINE BROTLI_BOOL SafeReadCommand(
1529
+ BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) {
1501
1530
  return ReadCommandInternal(1, s, br, insert_length);
1502
1531
  }
1503
1532
 
1504
- static BROTLI_INLINE int CheckInputAmount(int safe,
1505
- BrotliBitReader* const br, size_t num) {
1533
+ static BROTLI_INLINE BROTLI_BOOL CheckInputAmount(
1534
+ int safe, BrotliBitReader* const br, size_t num) {
1506
1535
  if (safe) {
1507
- return 1;
1536
+ return BROTLI_TRUE;
1508
1537
  }
1509
1538
  return BrotliCheckInputAmount(br, num);
1510
1539
  }
1511
1540
 
1512
- #define BROTLI_SAFE(METHOD) \
1513
- { \
1514
- if (safe) { \
1515
- if (!Safe##METHOD) { \
1516
- result = BROTLI_NEEDS_MORE_INPUT; \
1517
- goto saveStateAndReturn; \
1518
- } \
1519
- } else { \
1520
- METHOD; \
1521
- } \
1541
+ #define BROTLI_SAFE(METHOD) \
1542
+ { \
1543
+ if (safe) { \
1544
+ if (!Safe##METHOD) { \
1545
+ result = BROTLI_DECODER_NEEDS_MORE_INPUT; \
1546
+ goto saveStateAndReturn; \
1547
+ } \
1548
+ } else { \
1549
+ METHOD; \
1550
+ } \
1522
1551
  }
1523
1552
 
1524
- static BROTLI_INLINE BrotliErrorCode ProcessCommandsInternal(int safe,
1525
- BrotliState* s) {
1553
+ static BROTLI_INLINE BrotliDecoderErrorCode ProcessCommandsInternal(
1554
+ int safe, BrotliDecoderState* s) {
1526
1555
  int pos = s->pos;
1527
1556
  int i = s->loop_counter;
1528
- BrotliErrorCode result = BROTLI_SUCCESS;
1557
+ BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS;
1529
1558
  BrotliBitReader* br = &s->br;
1530
1559
 
1531
1560
  if (!CheckInputAmount(safe, br, 28)) {
1532
- result = BROTLI_NEEDS_MORE_INPUT;
1561
+ result = BROTLI_DECODER_NEEDS_MORE_INPUT;
1533
1562
  goto saveStateAndReturn;
1534
1563
  }
1535
1564
  if (!safe) {
@@ -1546,7 +1575,7 @@ static BROTLI_INLINE BrotliErrorCode ProcessCommandsInternal(int safe,
1546
1575
  } else if (s->state == BROTLI_STATE_COMMAND_POST_WRAP_COPY) {
1547
1576
  goto CommandPostWrapCopy;
1548
1577
  } else {
1549
- return BROTLI_FAILURE(BROTLI_ERROR_UNREACHABLE);
1578
+ return BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE);
1550
1579
  }
1551
1580
 
1552
1581
  CommandBegin:
@@ -1555,7 +1584,7 @@ CommandBegin:
1555
1584
  }
1556
1585
  if (!CheckInputAmount(safe, br, 28)) { /* 156 bits + 7 bytes */
1557
1586
  s->state = BROTLI_STATE_COMMAND_BEGIN;
1558
- result = BROTLI_NEEDS_MORE_INPUT;
1587
+ result = BROTLI_DECODER_NEEDS_MORE_INPUT;
1559
1588
  goto saveStateAndReturn;
1560
1589
  }
1561
1590
  if (PREDICT_FALSE(s->block_length[1] == 0)) {
@@ -1583,7 +1612,7 @@ CommandInner:
1583
1612
  do {
1584
1613
  if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */
1585
1614
  s->state = BROTLI_STATE_COMMAND_INNER;
1586
- result = BROTLI_NEEDS_MORE_INPUT;
1615
+ result = BROTLI_DECODER_NEEDS_MORE_INPUT;
1587
1616
  goto saveStateAndReturn;
1588
1617
  }
1589
1618
  if (PREDICT_FALSE(s->block_length[0] == 0)) {
@@ -1597,7 +1626,7 @@ CommandInner:
1597
1626
  } else {
1598
1627
  uint32_t literal;
1599
1628
  if (!SafeReadSymbol(s->literal_htree, br, &literal)) {
1600
- result = BROTLI_NEEDS_MORE_INPUT;
1629
+ result = BROTLI_DECODER_NEEDS_MORE_INPUT;
1601
1630
  goto saveStateAndReturn;
1602
1631
  }
1603
1632
  s->ringbuffer[pos] = (uint8_t)literal;
@@ -1619,7 +1648,7 @@ CommandInner:
1619
1648
  uint8_t context;
1620
1649
  if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */
1621
1650
  s->state = BROTLI_STATE_COMMAND_INNER;
1622
- result = BROTLI_NEEDS_MORE_INPUT;
1651
+ result = BROTLI_DECODER_NEEDS_MORE_INPUT;
1623
1652
  goto saveStateAndReturn;
1624
1653
  }
1625
1654
  if (PREDICT_FALSE(s->block_length[0] == 0)) {
@@ -1635,7 +1664,7 @@ CommandInner:
1635
1664
  } else {
1636
1665
  uint32_t literal;
1637
1666
  if (!SafeReadSymbol(hc, br, &literal)) {
1638
- result = BROTLI_NEEDS_MORE_INPUT;
1667
+ result = BROTLI_DECODER_NEEDS_MORE_INPUT;
1639
1668
  goto saveStateAndReturn;
1640
1669
  }
1641
1670
  p1 = (uint8_t)literal;
@@ -1715,13 +1744,13 @@ postReadDistance:
1715
1744
  BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
1716
1745
  "len: %d bytes left: %d\n",
1717
1746
  pos, s->distance_code, i, s->meta_block_remaining_len));
1718
- return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_TRANSFORM);
1747
+ return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_TRANSFORM);
1719
1748
  }
1720
1749
  } else {
1721
1750
  BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
1722
1751
  "len: %d bytes left: %d\n",
1723
1752
  pos, s->distance_code, i, s->meta_block_remaining_len));
1724
- return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_DICTIONARY);
1753
+ return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_DICTIONARY);
1725
1754
  }
1726
1755
  } else {
1727
1756
  int src_start = (pos - s->distance_code) & s->ringbuffer_mask;
@@ -1794,32 +1823,33 @@ saveStateAndReturn:
1794
1823
 
1795
1824
  #undef BROTLI_SAFE
1796
1825
 
1797
- static BROTLI_NOINLINE BrotliErrorCode ProcessCommands(BrotliState* s) {
1826
+ static BROTLI_NOINLINE BrotliDecoderErrorCode ProcessCommands(
1827
+ BrotliDecoderState* s) {
1798
1828
  return ProcessCommandsInternal(0, s);
1799
1829
  }
1800
1830
 
1801
- static BROTLI_NOINLINE BrotliErrorCode SafeProcessCommands(BrotliState* s) {
1831
+ static BROTLI_NOINLINE BrotliDecoderErrorCode SafeProcessCommands(
1832
+ BrotliDecoderState* s) {
1802
1833
  return ProcessCommandsInternal(1, s);
1803
1834
  }
1804
1835
 
1805
- BrotliResult BrotliDecompressBuffer(size_t encoded_size,
1806
- const uint8_t* encoded_buffer,
1807
- size_t* decoded_size,
1808
- uint8_t* decoded_buffer) {
1809
- BrotliState s;
1810
- BrotliResult result;
1836
+ BrotliDecoderResult BrotliDecoderDecompress(
1837
+ size_t encoded_size, const uint8_t* encoded_buffer, size_t* decoded_size,
1838
+ uint8_t* decoded_buffer) {
1839
+ BrotliDecoderState s;
1840
+ BrotliDecoderResult result;
1811
1841
  size_t total_out = 0;
1812
1842
  size_t available_in = encoded_size;
1813
1843
  const uint8_t* next_in = encoded_buffer;
1814
1844
  size_t available_out = *decoded_size;
1815
1845
  uint8_t* next_out = decoded_buffer;
1816
- BrotliStateInit(&s);
1817
- result = BrotliDecompressStream(&available_in, &next_in, &available_out,
1818
- &next_out, &total_out, &s);
1846
+ BrotliDecoderStateInit(&s);
1847
+ result = BrotliDecoderDecompressStream(
1848
+ &s, &available_in, &next_in, &available_out, &next_out, &total_out);
1819
1849
  *decoded_size = total_out;
1820
- BrotliStateCleanup(&s);
1821
- if (result != BROTLI_RESULT_SUCCESS) {
1822
- result = BROTLI_RESULT_ERROR;
1850
+ BrotliDecoderStateCleanup(&s);
1851
+ if (result != BROTLI_DECODER_RESULT_SUCCESS) {
1852
+ result = BROTLI_DECODER_RESULT_ERROR;
1823
1853
  }
1824
1854
  return result;
1825
1855
  }
@@ -1836,10 +1866,10 @@ BrotliResult BrotliDecompressBuffer(size_t encoded_size,
1836
1866
  * when result is "success" decoder MUST return all unused data back to input
1837
1867
  buffer; this is possible because the invariant is hold on enter
1838
1868
  */
1839
- BrotliResult BrotliDecompressStream(size_t* available_in,
1840
- const uint8_t** next_in, size_t* available_out, uint8_t** next_out,
1841
- size_t* total_out, BrotliState* s) {
1842
- BrotliErrorCode result = BROTLI_SUCCESS;
1869
+ BrotliDecoderResult BrotliDecoderDecompressStream(
1870
+ BrotliDecoderState* s, size_t* available_in, const uint8_t** next_in,
1871
+ size_t* available_out, uint8_t** next_out, size_t* total_out) {
1872
+ BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS;
1843
1873
  BrotliBitReader* br = &s->br;
1844
1874
  if (s->buffer_length == 0) { /* Just connect bit reader to input stream. */
1845
1875
  br->avail_in = *available_in;
@@ -1848,15 +1878,15 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
1848
1878
  /* At least one byte of input is required. More than one byte of input may
1849
1879
  be required to complete the transaction -> reading more data must be
1850
1880
  done in a loop -> do it in a main loop. */
1851
- result = BROTLI_NEEDS_MORE_INPUT;
1881
+ result = BROTLI_DECODER_NEEDS_MORE_INPUT;
1852
1882
  br->next_in = &s->buffer.u8[0];
1853
1883
  }
1854
1884
  /* State machine */
1855
1885
  for (;;) {
1856
- if (result != BROTLI_SUCCESS) { /* Error | needs more input/output */
1857
- if (result == BROTLI_NEEDS_MORE_INPUT) {
1886
+ if (result != BROTLI_DECODER_SUCCESS) { /* Error, needs more input/output */
1887
+ if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) {
1858
1888
  if (s->ringbuffer != 0) { /* Proactively push output. */
1859
- WriteRingBuffer(available_out, next_out, total_out, s);
1889
+ WriteRingBuffer(s, available_out, next_out, total_out);
1860
1890
  }
1861
1891
  if (s->buffer_length != 0) { /* Used with internal buffer. */
1862
1892
  if (br->avail_in == 0) { /* Successfully finished read transaction. */
@@ -1864,14 +1894,14 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
1864
1894
  is expanded byte-by-byte until it is enough to complete read. */
1865
1895
  s->buffer_length = 0;
1866
1896
  /* Switch to input stream and restart. */
1867
- result = BROTLI_SUCCESS;
1897
+ result = BROTLI_DECODER_SUCCESS;
1868
1898
  br->avail_in = *available_in;
1869
1899
  br->next_in = *next_in;
1870
1900
  continue;
1871
1901
  } else if (*available_in != 0) {
1872
1902
  /* Not enough data in buffer, but can take one more byte from
1873
1903
  input stream. */
1874
- result = BROTLI_SUCCESS;
1904
+ result = BROTLI_DECODER_SUCCESS;
1875
1905
  s->buffer.u8[s->buffer_length] = **next_in;
1876
1906
  s->buffer_length++;
1877
1907
  br->avail_in = s->buffer_length;
@@ -1917,7 +1947,7 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
1917
1947
  case BROTLI_STATE_UNINITED:
1918
1948
  /* Prepare to the first read. */
1919
1949
  if (!BrotliWarmupBitReader(br)) {
1920
- result = BROTLI_NEEDS_MORE_INPUT;
1950
+ result = BROTLI_DECODER_NEEDS_MORE_INPUT;
1921
1951
  break;
1922
1952
  }
1923
1953
  /* Decode window size. */
@@ -1925,7 +1955,7 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
1925
1955
  BROTLI_LOG_UINT(s->window_bits);
1926
1956
  if (s->window_bits == 9) {
1927
1957
  /* Value 9 is reserved for future use. */
1928
- result = BROTLI_FAILURE(BROTLI_ERROR_FORMAT_WINDOW_BITS);
1958
+ result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS);
1929
1959
  break;
1930
1960
  }
1931
1961
  /* Maximum distance, see section 9.1. of the spec. */
@@ -1943,7 +1973,7 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
1943
1973
  sizeof(HuffmanCode) * 3 *
1944
1974
  (BROTLI_HUFFMAN_MAX_SIZE_258 + BROTLI_HUFFMAN_MAX_SIZE_26));
1945
1975
  if (s->block_type_trees == 0) {
1946
- result = BROTLI_FAILURE(BROTLI_ERROR_ALLOC_BLOCK_TYPE_TREES);
1976
+ result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES);
1947
1977
  break;
1948
1978
  }
1949
1979
  s->block_len_trees =
@@ -1952,13 +1982,13 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
1952
1982
  s->state = BROTLI_STATE_METABLOCK_BEGIN;
1953
1983
  /* No break, continue to next state */
1954
1984
  case BROTLI_STATE_METABLOCK_BEGIN:
1955
- BrotliStateMetablockBegin(s);
1985
+ BrotliDecoderStateMetablockBegin(s);
1956
1986
  BROTLI_LOG_UINT(s->pos);
1957
1987
  s->state = BROTLI_STATE_METABLOCK_HEADER;
1958
1988
  /* No break, continue to next state */
1959
1989
  case BROTLI_STATE_METABLOCK_HEADER:
1960
1990
  result = DecodeMetaBlockLength(s, br); /* Reads 2 - 31 bits. */
1961
- if (result != BROTLI_SUCCESS) {
1991
+ if (result != BROTLI_DECODER_SUCCESS) {
1962
1992
  break;
1963
1993
  }
1964
1994
  BROTLI_LOG_UINT(s->is_last_metablock);
@@ -1967,7 +1997,7 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
1967
1997
  BROTLI_LOG_UINT(s->is_uncompressed);
1968
1998
  if (s->is_metadata || s->is_uncompressed) {
1969
1999
  if (!BrotliJumpToByteBoundary(br)) {
1970
- result = BROTLI_FAILURE(BROTLI_ERROR_FORMAT_PADDING_1);
2000
+ result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_PADDING_1);
1971
2001
  break;
1972
2002
  }
1973
2003
  }
@@ -1994,7 +2024,7 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
1994
2024
  result = CopyUncompressedBlockToOutput(
1995
2025
  available_out, next_out, total_out, s);
1996
2026
  bytes_copied -= s->meta_block_remaining_len;
1997
- if (result != BROTLI_SUCCESS) {
2027
+ if (result != BROTLI_DECODER_SUCCESS) {
1998
2028
  break;
1999
2029
  }
2000
2030
  s->state = BROTLI_STATE_METABLOCK_DONE;
@@ -2005,11 +2035,11 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
2005
2035
  uint32_t bits;
2006
2036
  /* Read one byte and ignore it. */
2007
2037
  if (!BrotliSafeReadBits(br, 8, &bits)) {
2008
- result = BROTLI_NEEDS_MORE_INPUT;
2038
+ result = BROTLI_DECODER_NEEDS_MORE_INPUT;
2009
2039
  break;
2010
2040
  }
2011
2041
  }
2012
- if (result == BROTLI_SUCCESS) {
2042
+ if (result == BROTLI_DECODER_SUCCESS) {
2013
2043
  s->state = BROTLI_STATE_METABLOCK_DONE;
2014
2044
  }
2015
2045
  break;
@@ -2020,7 +2050,7 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
2020
2050
  }
2021
2051
  /* Reads 1..11 bits. */
2022
2052
  result = DecodeVarLenUint8(s, br, &s->num_block_types[s->loop_counter]);
2023
- if (result != BROTLI_SUCCESS) {
2053
+ if (result != BROTLI_DECODER_SUCCESS) {
2024
2054
  break;
2025
2055
  }
2026
2056
  s->num_block_types[s->loop_counter]++;
@@ -2035,15 +2065,15 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
2035
2065
  int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_258;
2036
2066
  result = ReadHuffmanCode(s->num_block_types[s->loop_counter] + 2,
2037
2067
  &s->block_type_trees[tree_offset], NULL, s);
2038
- if (result != BROTLI_SUCCESS) break;
2068
+ if (result != BROTLI_DECODER_SUCCESS) break;
2039
2069
  s->state = BROTLI_STATE_HUFFMAN_CODE_2;
2040
2070
  /* No break, continue to next state */
2041
2071
  }
2042
2072
  case BROTLI_STATE_HUFFMAN_CODE_2: {
2043
2073
  int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26;
2044
- result = ReadHuffmanCode(kNumBlockLengthCodes,
2074
+ result = ReadHuffmanCode(BROTLI_NUM_BLOCK_LEN_SYMBOLS,
2045
2075
  &s->block_len_trees[tree_offset], NULL, s);
2046
- if (result != BROTLI_SUCCESS) break;
2076
+ if (result != BROTLI_DECODER_SUCCESS) break;
2047
2077
  s->state = BROTLI_STATE_HUFFMAN_CODE_3;
2048
2078
  /* No break, continue to next state */
2049
2079
  }
@@ -2051,7 +2081,7 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
2051
2081
  int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26;
2052
2082
  if (!SafeReadBlockLength(s, &s->block_length[s->loop_counter],
2053
2083
  &s->block_len_trees[tree_offset], br)) {
2054
- result = BROTLI_NEEDS_MORE_INPUT;
2084
+ result = BROTLI_DECODER_NEEDS_MORE_INPUT;
2055
2085
  break;
2056
2086
  }
2057
2087
  BROTLI_LOG_UINT(s->block_length[s->loop_counter]);
@@ -2062,20 +2092,20 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
2062
2092
  case BROTLI_STATE_METABLOCK_HEADER_2: {
2063
2093
  uint32_t bits;
2064
2094
  if (!BrotliSafeReadBits(br, 6, &bits)) {
2065
- result = BROTLI_NEEDS_MORE_INPUT;
2095
+ result = BROTLI_DECODER_NEEDS_MORE_INPUT;
2066
2096
  break;
2067
2097
  }
2068
2098
  s->distance_postfix_bits = bits & BitMask(2);
2069
2099
  bits >>= 2;
2070
- s->num_direct_distance_codes =
2071
- NUM_DISTANCE_SHORT_CODES + (bits << s->distance_postfix_bits);
2100
+ s->num_direct_distance_codes = BROTLI_NUM_DISTANCE_SHORT_CODES +
2101
+ (bits << s->distance_postfix_bits);
2072
2102
  BROTLI_LOG_UINT(s->num_direct_distance_codes);
2073
2103
  BROTLI_LOG_UINT(s->distance_postfix_bits);
2074
2104
  s->distance_postfix_mask = (int)BitMask(s->distance_postfix_bits);
2075
2105
  s->context_modes =
2076
2106
  (uint8_t*)BROTLI_ALLOC(s, (size_t)s->num_block_types[0]);
2077
2107
  if (s->context_modes == 0) {
2078
- result = BROTLI_FAILURE(BROTLI_ERROR_ALLOC_CONTEXT_MODES);
2108
+ result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES);
2079
2109
  break;
2080
2110
  }
2081
2111
  s->loop_counter = 0;
@@ -2084,16 +2114,16 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
2084
2114
  }
2085
2115
  case BROTLI_STATE_CONTEXT_MODES:
2086
2116
  result = ReadContextModes(s);
2087
- if (result != BROTLI_SUCCESS) {
2117
+ if (result != BROTLI_DECODER_SUCCESS) {
2088
2118
  break;
2089
2119
  }
2090
2120
  s->state = BROTLI_STATE_CONTEXT_MAP_1;
2091
2121
  /* No break, continue to next state */
2092
2122
  case BROTLI_STATE_CONTEXT_MAP_1:
2093
2123
  result = DecodeContextMap(
2094
- s->num_block_types[0] << kLiteralContextBits,
2124
+ s->num_block_types[0] << BROTLI_LITERAL_CONTEXT_BITS,
2095
2125
  &s->num_literal_htrees, &s->context_map, s);
2096
- if (result != BROTLI_SUCCESS) {
2126
+ if (result != BROTLI_DECODER_SUCCESS) {
2097
2127
  break;
2098
2128
  }
2099
2129
  DetectTrivialLiteralBlockTypes(s);
@@ -2104,23 +2134,25 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
2104
2134
  uint32_t num_distance_codes =
2105
2135
  s->num_direct_distance_codes + (48U << s->distance_postfix_bits);
2106
2136
  result = DecodeContextMap(
2107
- s->num_block_types[2] << kDistanceContextBits,
2137
+ s->num_block_types[2] << BROTLI_DISTANCE_CONTEXT_BITS,
2108
2138
  &s->num_dist_htrees, &s->dist_context_map, s);
2109
- if (result != BROTLI_SUCCESS) {
2139
+ if (result != BROTLI_DECODER_SUCCESS) {
2110
2140
  break;
2111
2141
  }
2112
- BrotliHuffmanTreeGroupInit(s, &s->literal_hgroup, kNumLiteralCodes,
2113
- s->num_literal_htrees);
2114
- BrotliHuffmanTreeGroupInit(s, &s->insert_copy_hgroup,
2115
- kNumInsertAndCopyCodes,
2116
- s->num_block_types[1]);
2117
- BrotliHuffmanTreeGroupInit(s, &s->distance_hgroup, num_distance_codes,
2118
- s->num_dist_htrees);
2142
+ BrotliDecoderHuffmanTreeGroupInit(
2143
+ s, &s->literal_hgroup, BROTLI_NUM_LITERAL_SYMBOLS,
2144
+ s->num_literal_htrees);
2145
+ BrotliDecoderHuffmanTreeGroupInit(
2146
+ s, &s->insert_copy_hgroup, BROTLI_NUM_COMMAND_SYMBOLS,
2147
+ s->num_block_types[1]);
2148
+ BrotliDecoderHuffmanTreeGroupInit(
2149
+ s, &s->distance_hgroup, num_distance_codes,
2150
+ s->num_dist_htrees);
2119
2151
  if (s->literal_hgroup.codes == 0 ||
2120
2152
  s->insert_copy_hgroup.codes == 0 ||
2121
2153
  s->distance_hgroup.codes == 0) {
2122
2154
  return SaveErrorCode(s,
2123
- BROTLI_FAILURE(BROTLI_ERROR_ALLOC_TREE_GROUPS));
2155
+ BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS));
2124
2156
  }
2125
2157
  }
2126
2158
  s->loop_counter = 0;
@@ -2140,19 +2172,19 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
2140
2172
  hgroup = &s->distance_hgroup;
2141
2173
  break;
2142
2174
  default:
2143
- return SaveErrorCode(s,
2144
- BROTLI_FAILURE(BROTLI_ERROR_UNREACHABLE));
2175
+ return SaveErrorCode(s, BROTLI_FAILURE(
2176
+ BROTLI_DECODER_ERROR_UNREACHABLE));
2145
2177
  }
2146
2178
  result = HuffmanTreeGroupDecode(hgroup, s);
2147
2179
  }
2148
- if (result != BROTLI_SUCCESS) break;
2180
+ if (result != BROTLI_DECODER_SUCCESS) break;
2149
2181
  s->loop_counter++;
2150
2182
  if (s->loop_counter >= 3) {
2151
2183
  PrepareLiteralDecoding(s);
2152
2184
  s->dist_context_map_slice = s->dist_context_map;
2153
2185
  s->htree_command = s->insert_copy_hgroup.htrees[0];
2154
2186
  if (!s->ringbuffer && !BrotliAllocateRingBuffer(s)) {
2155
- result = BROTLI_FAILURE(BROTLI_ERROR_ALLOC_RING_BUFFER_2);
2187
+ result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2);
2156
2188
  break;
2157
2189
  }
2158
2190
  s->state = BROTLI_STATE_COMMAND_BEGIN;
@@ -2163,15 +2195,15 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
2163
2195
  case BROTLI_STATE_COMMAND_POST_DECODE_LITERALS:
2164
2196
  case BROTLI_STATE_COMMAND_POST_WRAP_COPY:
2165
2197
  result = ProcessCommands(s);
2166
- if (result == BROTLI_NEEDS_MORE_INPUT) {
2198
+ if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) {
2167
2199
  result = SafeProcessCommands(s);
2168
2200
  }
2169
2201
  break;
2170
2202
  case BROTLI_STATE_COMMAND_INNER_WRITE:
2171
2203
  case BROTLI_STATE_COMMAND_POST_WRITE_1:
2172
2204
  case BROTLI_STATE_COMMAND_POST_WRITE_2:
2173
- result = WriteRingBuffer(available_out, next_out, total_out, s);
2174
- if (result != BROTLI_SUCCESS) {
2205
+ result = WriteRingBuffer(s, available_out, next_out, total_out);
2206
+ if (result != BROTLI_DECODER_SUCCESS) {
2175
2207
  break;
2176
2208
  }
2177
2209
  s->max_distance = s->max_backward_distance;
@@ -2200,16 +2232,16 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
2200
2232
  break;
2201
2233
  case BROTLI_STATE_METABLOCK_DONE:
2202
2234
  if (s->meta_block_remaining_len < 0) {
2203
- result = BROTLI_FAILURE(BROTLI_ERROR_FORMAT_BLOCK_LENGTH_2);
2235
+ result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2);
2204
2236
  break;
2205
2237
  }
2206
- BrotliStateCleanupAfterMetablock(s);
2238
+ BrotliDecoderStateCleanupAfterMetablock(s);
2207
2239
  if (!s->is_last_metablock) {
2208
2240
  s->state = BROTLI_STATE_METABLOCK_BEGIN;
2209
2241
  break;
2210
2242
  }
2211
2243
  if (!BrotliJumpToByteBoundary(br)) {
2212
- result = BROTLI_FAILURE(BROTLI_ERROR_FORMAT_PADDING_2);
2244
+ result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_PADDING_2);
2213
2245
  break;
2214
2246
  }
2215
2247
  if (s->buffer_length == 0) {
@@ -2221,8 +2253,8 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
2221
2253
  /* No break, continue to next state */
2222
2254
  case BROTLI_STATE_DONE:
2223
2255
  if (s->ringbuffer != 0) {
2224
- result = WriteRingBuffer(available_out, next_out, total_out, s);
2225
- if (result != BROTLI_SUCCESS) {
2256
+ result = WriteRingBuffer(s, available_out, next_out, total_out);
2257
+ if (result != BROTLI_DECODER_SUCCESS) {
2226
2258
  break;
2227
2259
  }
2228
2260
  }
@@ -2232,8 +2264,8 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
2232
2264
  return SaveErrorCode(s, result);
2233
2265
  }
2234
2266
 
2235
- void BrotliSetCustomDictionary(
2236
- size_t size, const uint8_t* dict, BrotliState* s) {
2267
+ void BrotliDecoderSetCustomDictionary(
2268
+ BrotliDecoderState* s, size_t size, const uint8_t* dict) {
2237
2269
  if (size > (1u << 24)) {
2238
2270
  return;
2239
2271
  }
@@ -2241,22 +2273,75 @@ void BrotliSetCustomDictionary(
2241
2273
  s->custom_dict_size = (int)size;
2242
2274
  }
2243
2275
 
2244
- BrotliErrorCode BrotliGetErrorCode(const BrotliState* s) {
2245
- return (BrotliErrorCode)s->error_code;
2276
+ BROTLI_BOOL BrotliDecoderHasMoreOutput(const BrotliDecoderState* s) {
2277
+ return TO_BROTLI_BOOL(
2278
+ s->ringbuffer != 0 && UnwrittenBytes(s, BROTLI_FALSE) != 0);
2246
2279
  }
2247
2280
 
2248
- const char* BrotliErrorString(BrotliErrorCode c) {
2281
+ BROTLI_BOOL BrotliDecoderIsUsed(const BrotliDecoderState* s) {
2282
+ return TO_BROTLI_BOOL(s->state != BROTLI_STATE_UNINITED ||
2283
+ BrotliGetAvailableBits(&s->br) != 0);
2284
+ }
2285
+
2286
+ BROTLI_BOOL BrotliDecoderIsFinished(const BrotliDecoderState* s) {
2287
+ return TO_BROTLI_BOOL(s->state == BROTLI_STATE_DONE);
2288
+ }
2289
+
2290
+ BrotliDecoderErrorCode BrotliDecoderGetErrorCode(const BrotliDecoderState* s) {
2291
+ return (BrotliDecoderErrorCode)s->error_code;
2292
+ }
2293
+
2294
+ const char* BrotliDecoderErrorString(BrotliDecoderErrorCode c) {
2249
2295
  switch (c) {
2250
2296
  #define _BROTLI_ERROR_CODE_CASE(PREFIX, NAME, CODE) \
2251
- case BROTLI ## PREFIX ## NAME: return #NAME;
2297
+ case BROTLI_DECODER ## PREFIX ## NAME: return #NAME;
2252
2298
  #define _BROTLI_NOTHING
2253
- BROTLI_ERROR_CODES_LIST(_BROTLI_ERROR_CODE_CASE, _BROTLI_NOTHING)
2299
+ BROTLI_DECODER_ERROR_CODES_LIST(_BROTLI_ERROR_CODE_CASE, _BROTLI_NOTHING)
2254
2300
  #undef _BROTLI_ERROR_CODE_CASE
2255
2301
  #undef _BROTLI_NOTHING
2256
2302
  default: return "INVALID";
2257
2303
  }
2258
2304
  }
2259
2305
 
2306
+ /* DEPRECATED >>> */
2307
+ BrotliState* BrotliCreateState(
2308
+ brotli_alloc_func alloc, brotli_free_func free, void* opaque) {
2309
+ return (BrotliState*)BrotliDecoderCreateInstance(alloc, free, opaque);
2310
+ }
2311
+ void BrotliDestroyState(BrotliState* state) {
2312
+ BrotliDecoderDestroyInstance((BrotliDecoderState*)state);
2313
+ }
2314
+ BrotliResult BrotliDecompressBuffer(
2315
+ size_t encoded_size, const uint8_t* encoded_buffer, size_t* decoded_size,
2316
+ uint8_t* decoded_buffer) {
2317
+ return (BrotliResult)BrotliDecoderDecompress(
2318
+ encoded_size, encoded_buffer, decoded_size, decoded_buffer);
2319
+ }
2320
+ BrotliResult BrotliDecompressStream(
2321
+ size_t* available_in, const uint8_t** next_in, size_t* available_out,
2322
+ uint8_t** next_out, size_t* total_out, BrotliState* s) {
2323
+ return (BrotliResult)BrotliDecoderDecompressStream((BrotliDecoderState*)s,
2324
+ available_in, next_in, available_out, next_out, total_out);
2325
+ }
2326
+ void BrotliSetCustomDictionary(
2327
+ size_t size, const uint8_t* dict, BrotliState* s) {
2328
+ BrotliDecoderSetCustomDictionary((BrotliDecoderState*)s, size, dict);
2329
+ }
2330
+ BROTLI_BOOL BrotliStateIsStreamStart(const BrotliState* s) {
2331
+ return !BrotliDecoderIsUsed((const BrotliDecoderState*)s);
2332
+ }
2333
+ BROTLI_BOOL BrotliStateIsStreamEnd(const BrotliState* s) {
2334
+ return BrotliDecoderIsFinished((const BrotliDecoderState*)s);
2335
+ }
2336
+ BrotliErrorCode BrotliGetErrorCode(const BrotliState* s) {
2337
+ return (BrotliErrorCode)BrotliDecoderGetErrorCode(
2338
+ (const BrotliDecoderState*)s);
2339
+ }
2340
+ const char* BrotliErrorString(BrotliErrorCode c) {
2341
+ return BrotliDecoderErrorString((BrotliDecoderErrorCode)c);
2342
+ }
2343
+ /* <<< DEPRECATED */
2344
+
2260
2345
  #if defined(__cplusplus) || defined(c_plusplus)
2261
2346
  } /* extern "C" */
2262
2347
  #endif