Coverage Report

Created: 2025-08-29 06:20

/src/ots/subprojects/brotli-1.1.0/c/dec/decode.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright 2013 Google Inc. All Rights Reserved.
2
3
   Distributed under MIT license.
4
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
*/
6
7
#include <brotli/decode.h>
8
9
#include <stdlib.h>  /* free, malloc */
10
#include <string.h>  /* memcpy, memset */
11
12
#include "../common/constants.h"
13
#include "../common/context.h"
14
#include "../common/dictionary.h"
15
#include "../common/platform.h"
16
#include "../common/shared_dictionary_internal.h"
17
#include "../common/transform.h"
18
#include "../common/version.h"
19
#include "bit_reader.h"
20
#include "huffman.h"
21
#include "prefix.h"
22
#include "state.h"
23
24
#if defined(BROTLI_TARGET_NEON)
25
#include <arm_neon.h>
26
#endif
27
28
#if defined(__cplusplus) || defined(c_plusplus)
29
extern "C" {
30
#endif
31
32
539
#define BROTLI_FAILURE(CODE) (BROTLI_DUMP(), CODE)
33
34
#define BROTLI_LOG_UINT(name)                                       \
35
  BROTLI_LOG(("[%s] %s = %lu\n", __func__, #name, (unsigned long)(name)))
36
#define BROTLI_LOG_ARRAY_INDEX(array_name, idx)                     \
37
  BROTLI_LOG(("[%s] %s[%lu] = %lu\n", __func__, #array_name,        \
38
         (unsigned long)(idx), (unsigned long)array_name[idx]))
39
40
180M
#define HUFFMAN_TABLE_BITS 8U
41
15.4k
#define HUFFMAN_TABLE_MASK 0xFF
42
43
/* We need the slack region for the following reasons:
44
    - doing up to two 16-byte copies for fast backward copying
45
    - inserting transformed dictionary word:
46
        255 prefix + 32 base + 255 suffix */
47
static const brotli_reg_t kRingBufferWriteAheadSlack = 542;
48
49
static const uint8_t kCodeLengthCodeOrder[BROTLI_CODE_LENGTH_CODES] = {
50
  1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15,
51
};
52
53
/* Static prefix code for the complex code length code lengths. */
54
static const uint8_t kCodeLengthPrefixLength[16] = {
55
  2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 3, 2, 2, 2, 4,
56
};
57
58
static const uint8_t kCodeLengthPrefixValue[16] = {
59
  0, 4, 3, 2, 0, 4, 3, 1, 0, 4, 3, 2, 0, 4, 3, 5,
60
};
61
62
BROTLI_BOOL BrotliDecoderSetParameter(
63
0
    BrotliDecoderState* state, BrotliDecoderParameter p, uint32_t value) {
64
0
  if (state->state != BROTLI_STATE_UNINITED) return BROTLI_FALSE;
65
0
  switch (p) {
66
0
    case BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION:
67
0
      state->canny_ringbuffer_allocation = !!value ? 0 : 1;
68
0
      return BROTLI_TRUE;
69
70
0
    case BROTLI_DECODER_PARAM_LARGE_WINDOW:
71
0
      state->large_window = TO_BROTLI_BOOL(!!value);
72
0
      return BROTLI_TRUE;
73
74
0
    default: return BROTLI_FALSE;
75
0
  }
76
0
}
77
78
BrotliDecoderState* BrotliDecoderCreateInstance(
79
0
    brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
80
0
  BrotliDecoderState* state = 0;
81
0
  if (!alloc_func && !free_func) {
82
0
    state = (BrotliDecoderState*)malloc(sizeof(BrotliDecoderState));
83
0
  } else if (alloc_func && free_func) {
84
0
    state = (BrotliDecoderState*)alloc_func(opaque, sizeof(BrotliDecoderState));
85
0
  }
86
0
  if (state == 0) {
87
0
    BROTLI_DUMP();
88
0
    return 0;
89
0
  }
90
0
  if (!BrotliDecoderStateInit(state, alloc_func, free_func, opaque)) {
91
0
    BROTLI_DUMP();
92
0
    if (!alloc_func && !free_func) {
93
0
      free(state);
94
0
    } else if (alloc_func && free_func) {
95
0
      free_func(opaque, state);
96
0
    }
97
0
    return 0;
98
0
  }
99
0
  return state;
100
0
}
101
102
/* Deinitializes and frees BrotliDecoderState instance. */
103
0
void BrotliDecoderDestroyInstance(BrotliDecoderState* state) {
104
0
  if (!state) {
105
0
    return;
106
0
  } else {
107
0
    brotli_free_func free_func = state->free_func;
108
0
    void* opaque = state->memory_manager_opaque;
109
0
    BrotliDecoderStateCleanup(state);
110
0
    free_func(opaque, state);
111
0
  }
112
0
}
113
114
/* Saves error code and converts it to BrotliDecoderResult. */
115
static BROTLI_NOINLINE BrotliDecoderResult SaveErrorCode(
116
1.67k
    BrotliDecoderState* s, BrotliDecoderErrorCode e, size_t consumed_input) {
117
1.67k
  s->error_code = (int)e;
118
1.67k
  s->used_input += consumed_input;
119
1.67k
  if ((s->buffer_length != 0) && (s->br.next_in == s->br.last_in)) {
120
    /* If internal buffer is depleted at last, reset it. */
121
0
    s->buffer_length = 0;
122
0
  }
123
1.67k
  switch (e) {
124
555
    case BROTLI_DECODER_SUCCESS:
125
555
      return BROTLI_DECODER_RESULT_SUCCESS;
126
127
554
    case BROTLI_DECODER_NEEDS_MORE_INPUT:
128
554
      return BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT;
129
130
31
    case BROTLI_DECODER_NEEDS_MORE_OUTPUT:
131
31
      return BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
132
133
539
    default:
134
539
      return BROTLI_DECODER_RESULT_ERROR;
135
1.67k
  }
136
1.67k
}
137
138
/* Decodes WBITS by reading 1 - 7 bits, or 0x11 for "Large Window Brotli".
139
   Precondition: bit-reader accumulator has at least 8 bits. */
140
static BrotliDecoderErrorCode DecodeWindowBits(BrotliDecoderState* s,
141
1.62k
                                               BrotliBitReader* br) {
142
1.62k
  brotli_reg_t n;
143
1.62k
  BROTLI_BOOL large_window = s->large_window;
144
1.62k
  s->large_window = BROTLI_FALSE;
145
1.62k
  BrotliTakeBits(br, 1, &n);
146
1.62k
  if (n == 0) {
147
615
    s->window_bits = 16;
148
615
    return BROTLI_DECODER_SUCCESS;
149
615
  }
150
1.01k
  BrotliTakeBits(br, 3, &n);
151
1.01k
  if (n != 0) {
152
919
    s->window_bits = (17u + n) & 63u;
153
919
    return BROTLI_DECODER_SUCCESS;
154
919
  }
155
94
  BrotliTakeBits(br, 3, &n);
156
94
  if (n == 1) {
157
1
    if (large_window) {
158
0
      BrotliTakeBits(br, 1, &n);
159
0
      if (n == 1) {
160
0
        return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS);
161
0
      }
162
0
      s->large_window = BROTLI_TRUE;
163
0
      return BROTLI_DECODER_SUCCESS;
164
1
    } else {
165
1
      return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS);
166
1
    }
167
1
  }
168
93
  if (n != 0) {
169
70
    s->window_bits = (8u + n) & 63u;
170
70
    return BROTLI_DECODER_SUCCESS;
171
70
  }
172
23
  s->window_bits = 17;
173
23
  return BROTLI_DECODER_SUCCESS;
174
93
}
175
176
12.8M
static BROTLI_INLINE void memmove16(uint8_t* dst, uint8_t* src) {
177
#if defined(BROTLI_TARGET_NEON)
178
  vst1q_u8(dst, vld1q_u8(src));
179
#else
180
12.8M
  uint32_t buffer[4];
181
12.8M
  memcpy(buffer, src, 16);
182
12.8M
  memcpy(dst, buffer, 16);
183
12.8M
#endif
184
12.8M
}
185
186
/* Decodes a number in the range [0..255], by reading 1 - 11 bits. */
187
static BROTLI_NOINLINE BrotliDecoderErrorCode DecodeVarLenUint8(
188
22.6k
    BrotliDecoderState* s, BrotliBitReader* br, brotli_reg_t* value) {
189
22.6k
  brotli_reg_t bits;
190
22.6k
  switch (s->substate_decode_uint8) {
191
22.6k
    case BROTLI_STATE_DECODE_UINT8_NONE:
192
22.6k
      if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, 1, &bits))) {
193
1
        return BROTLI_DECODER_NEEDS_MORE_INPUT;
194
1
      }
195
22.6k
      if (bits == 0) {
196
18.7k
        *value = 0;
197
18.7k
        return BROTLI_DECODER_SUCCESS;
198
18.7k
      }
199
    /* Fall through. */
200
201
3.81k
    case BROTLI_STATE_DECODE_UINT8_SHORT:
202
3.81k
      if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, 3, &bits))) {
203
4
        s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_SHORT;
204
4
        return BROTLI_DECODER_NEEDS_MORE_INPUT;
205
4
      }
206
3.81k
      if (bits == 0) {
207
559
        *value = 1;
208
559
        s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
209
559
        return BROTLI_DECODER_SUCCESS;
210
559
      }
211
      /* Use output value as a temporary storage. It MUST be persisted. */
212
3.25k
      *value = bits;
213
    /* Fall through. */
214
215
3.25k
    case BROTLI_STATE_DECODE_UINT8_LONG:
216
3.25k
      if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, *value, &bits))) {
217
2
        s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_LONG;
218
2
        return BROTLI_DECODER_NEEDS_MORE_INPUT;
219
2
      }
220
3.25k
      *value = (1U << *value) + bits;
221
3.25k
      s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
222
3.25k
      return BROTLI_DECODER_SUCCESS;
223
224
0
    default:
225
0
      return
226
0
          BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE);  /* COV_NF_LINE */
227
22.6k
  }
228
22.6k
}
229
230
/* Decodes a metablock length and flags by reading 2 - 31 bits. */
231
static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
232
45.1k
    BrotliDecoderState* s, BrotliBitReader* br) {
233
45.1k
  brotli_reg_t bits;
234
45.1k
  int i;
235
126k
  for (;;) {
236
126k
    switch (s->substate_metablock_header) {
237
45.1k
      case BROTLI_STATE_METABLOCK_HEADER_NONE:
238
45.1k
        if (!BrotliSafeReadBits(br, 1, &bits)) {
239
2
          return BROTLI_DECODER_NEEDS_MORE_INPUT;
240
2
        }
241
45.1k
        s->is_last_metablock = bits ? 1 : 0;
242
45.1k
        s->meta_block_remaining_len = 0;
243
45.1k
        s->is_uncompressed = 0;
244
45.1k
        s->is_metadata = 0;
245
45.1k
        if (!s->is_last_metablock) {
246
44.2k
          s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NIBBLES;
247
44.2k
          break;
248
44.2k
        }
249
869
        s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_EMPTY;
250
      /* Fall through. */
251
252
869
      case BROTLI_STATE_METABLOCK_HEADER_EMPTY:
253
869
        if (!BrotliSafeReadBits(br, 1, &bits)) {
254
1
          return BROTLI_DECODER_NEEDS_MORE_INPUT;
255
1
        }
256
868
        if (bits) {
257
35
          s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
258
35
          return BROTLI_DECODER_SUCCESS;
259
35
        }
260
833
        s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NIBBLES;
261
      /* Fall through. */
262
263
45.0k
      case BROTLI_STATE_METABLOCK_HEADER_NIBBLES:
264
45.0k
        if (!BrotliSafeReadBits(br, 2, &bits)) {
265
4
          return BROTLI_DECODER_NEEDS_MORE_INPUT;
266
4
        }
267
45.0k
        s->size_nibbles = (uint8_t)(bits + 4);
268
45.0k
        s->loop_counter = 0;
269
45.0k
        if (bits == 3) {
270
37.5k
          s->is_metadata = 1;
271
37.5k
          s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_RESERVED;
272
37.5k
          break;
273
37.5k
        }
274
7.47k
        s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_SIZE;
275
      /* Fall through. */
276
277
7.47k
      case BROTLI_STATE_METABLOCK_HEADER_SIZE:
278
7.47k
        i = s->loop_counter;
279
37.7k
        for (; i < (int)s->size_nibbles; ++i) {
280
30.3k
          if (!BrotliSafeReadBits(br, 4, &bits)) {
281
37
            s->loop_counter = i;
282
37
            return BROTLI_DECODER_NEEDS_MORE_INPUT;
283
37
          }
284
30.3k
          if (i + 1 == (int)s->size_nibbles && s->size_nibbles > 4 &&
285
30.3k
              bits == 0) {
286
5
            return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE);
287
5
          }
288
30.3k
          s->meta_block_remaining_len |= (int)(bits << (i * 4));
289
30.3k
        }
290
7.43k
        s->substate_metablock_header =
291
7.43k
            BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED;
292
      /* Fall through. */
293
294
7.43k
      case BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED:
295
7.43k
        if (!s->is_last_metablock) {
296
6.63k
          if (!BrotliSafeReadBits(br, 1, &bits)) {
297
1
            return BROTLI_DECODER_NEEDS_MORE_INPUT;
298
1
          }
299
6.62k
          s->is_uncompressed = bits ? 1 : 0;
300
6.62k
        }
301
7.43k
        ++s->meta_block_remaining_len;
302
7.43k
        s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
303
7.43k
        return BROTLI_DECODER_SUCCESS;
304
305
37.5k
      case BROTLI_STATE_METABLOCK_HEADER_RESERVED:
306
37.5k
        if (!BrotliSafeReadBits(br, 1, &bits)) {
307
2
          return BROTLI_DECODER_NEEDS_MORE_INPUT;
308
2
        }
309
37.5k
        if (bits != 0) {
310
4
          return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_RESERVED);
311
4
        }
312
37.5k
        s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_BYTES;
313
      /* Fall through. */
314
315
37.5k
      case BROTLI_STATE_METABLOCK_HEADER_BYTES:
316
37.5k
        if (!BrotliSafeReadBits(br, 2, &bits)) {
317
1
          return BROTLI_DECODER_NEEDS_MORE_INPUT;
318
1
        }
319
37.5k
        if (bits == 0) {
320
29.0k
          s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
321
29.0k
          return BROTLI_DECODER_SUCCESS;
322
29.0k
        }
323
8.56k
        s->size_nibbles = (uint8_t)bits;
324
8.56k
        s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_METADATA;
325
      /* Fall through. */
326
327
8.56k
      case BROTLI_STATE_METABLOCK_HEADER_METADATA:
328
8.56k
        i = s->loop_counter;
329
17.1k
        for (; i < (int)s->size_nibbles; ++i) {
330
8.61k
          if (!BrotliSafeReadBits(br, 8, &bits)) {
331
2
            s->loop_counter = i;
332
2
            return BROTLI_DECODER_NEEDS_MORE_INPUT;
333
2
          }
334
8.60k
          if (i + 1 == (int)s->size_nibbles && s->size_nibbles > 1 &&
335
8.60k
              bits == 0) {
336
1
            return BROTLI_FAILURE(
337
1
                BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE);
338
1
          }
339
8.60k
          s->meta_block_remaining_len |= (int)(bits << (i * 8));
340
8.60k
        }
341
8.56k
        ++s->meta_block_remaining_len;
342
8.56k
        s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
343
8.56k
        return BROTLI_DECODER_SUCCESS;
344
345
0
      default:
346
0
        return
347
0
            BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE);  /* COV_NF_LINE */
348
126k
    }
349
126k
  }
350
45.1k
}
351
352
/* Decodes the Huffman code.
353
   This method doesn't read data from the bit reader, BUT drops the amount of
354
   bits that correspond to the decoded symbol.
355
   bits MUST contain at least 15 (BROTLI_HUFFMAN_MAX_CODE_LENGTH) valid bits. */
356
static BROTLI_INLINE brotli_reg_t DecodeSymbol(brotli_reg_t bits,
357
                                               const HuffmanCode* table,
358
177M
                                               BrotliBitReader* br) {
359
177M
  BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table);
360
177M
  BROTLI_HC_ADJUST_TABLE_INDEX(table, bits & HUFFMAN_TABLE_MASK);
361
177M
  if (BROTLI_HC_FAST_LOAD_BITS(table) > HUFFMAN_TABLE_BITS) {
362
292k
    brotli_reg_t nbits = BROTLI_HC_FAST_LOAD_BITS(table) - HUFFMAN_TABLE_BITS;
363
292k
    BrotliDropBits(br, HUFFMAN_TABLE_BITS);
364
292k
    BROTLI_HC_ADJUST_TABLE_INDEX(table,
365
292k
        BROTLI_HC_FAST_LOAD_VALUE(table) +
366
292k
        ((bits >> HUFFMAN_TABLE_BITS) & BitMask(nbits)));
367
292k
  }
368
177M
  BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(table));
369
177M
  return BROTLI_HC_FAST_LOAD_VALUE(table);
370
177M
}
371
372
/* Reads and decodes the next Huffman code from bit-stream.
373
   This method peeks 16 bits of input and drops 0 - 15 of them. */
374
static BROTLI_INLINE brotli_reg_t ReadSymbol(const HuffmanCode* table,
375
12.6M
                                             BrotliBitReader* br) {
376
12.6M
  return DecodeSymbol(BrotliGet16BitsUnmasked(br), table, br);
377
12.6M
}
378
379
/* Same as DecodeSymbol, but it is known that there is less than 15 bits of
380
   input are currently available. */
381
static BROTLI_NOINLINE BROTLI_BOOL SafeDecodeSymbol(
382
3.05M
    const HuffmanCode* table, BrotliBitReader* br, brotli_reg_t* result) {
383
3.05M
  brotli_reg_t val;
384
3.05M
  brotli_reg_t available_bits = BrotliGetAvailableBits(br);
385
3.05M
  BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table);
386
3.05M
  if (available_bits == 0) {
387
50.3k
    if (BROTLI_HC_FAST_LOAD_BITS(table) == 0) {
388
50.2k
      *result = BROTLI_HC_FAST_LOAD_VALUE(table);
389
50.2k
      return BROTLI_TRUE;
390
50.2k
    }
391
86
    return BROTLI_FALSE;  /* No valid bits at all. */
392
50.3k
  }
393
3.00M
  val = BrotliGetBitsUnmasked(br);
394
3.00M
  BROTLI_HC_ADJUST_TABLE_INDEX(table, val & HUFFMAN_TABLE_MASK);
395
3.00M
  if (BROTLI_HC_FAST_LOAD_BITS(table) <= HUFFMAN_TABLE_BITS) {
396
3.00M
    if (BROTLI_HC_FAST_LOAD_BITS(table) <= available_bits) {
397
3.00M
      BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(table));
398
3.00M
      *result = BROTLI_HC_FAST_LOAD_VALUE(table);
399
3.00M
      return BROTLI_TRUE;
400
3.00M
    } else {
401
55
      return BROTLI_FALSE;  /* Not enough bits for the first level. */
402
55
    }
403
3.00M
  }
404
182
  if (available_bits <= HUFFMAN_TABLE_BITS) {
405
5
    return BROTLI_FALSE;  /* Not enough bits to move to the second level. */
406
5
  }
407
408
  /* Speculatively drop HUFFMAN_TABLE_BITS. */
409
177
  val = (val & BitMask(BROTLI_HC_FAST_LOAD_BITS(table))) >> HUFFMAN_TABLE_BITS;
410
177
  available_bits -= HUFFMAN_TABLE_BITS;
411
177
  BROTLI_HC_ADJUST_TABLE_INDEX(table, BROTLI_HC_FAST_LOAD_VALUE(table) + val);
412
177
  if (available_bits < BROTLI_HC_FAST_LOAD_BITS(table)) {
413
1
    return BROTLI_FALSE;  /* Not enough bits for the second level. */
414
1
  }
415
416
176
  BrotliDropBits(br, HUFFMAN_TABLE_BITS + BROTLI_HC_FAST_LOAD_BITS(table));
417
176
  *result = BROTLI_HC_FAST_LOAD_VALUE(table);
418
176
  return BROTLI_TRUE;
419
177
}
420
421
static BROTLI_INLINE BROTLI_BOOL SafeReadSymbol(
422
167M
    const HuffmanCode* table, BrotliBitReader* br, brotli_reg_t* result) {
423
167M
  brotli_reg_t val;
424
167M
  if (BROTLI_PREDICT_TRUE(BrotliSafeGetBits(br, 15, &val))) {
425
164M
    *result = DecodeSymbol(val, table, br);
426
164M
    return BROTLI_TRUE;
427
164M
  }
428
3.05M
  return SafeDecodeSymbol(table, br, result);
429
167M
}
430
431
/* Makes a look-up in first level Huffman table. Peeks 8 bits. */
432
static BROTLI_INLINE void PreloadSymbol(int safe,
433
                                        const HuffmanCode* table,
434
                                        BrotliBitReader* br,
435
                                        brotli_reg_t* bits,
436
39.7M
                                        brotli_reg_t* value) {
437
39.7M
  if (safe) {
438
5.72M
    return;
439
5.72M
  }
440
34.0M
  BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(table);
441
34.0M
  BROTLI_HC_ADJUST_TABLE_INDEX(table, BrotliGetBits(br, HUFFMAN_TABLE_BITS));
442
34.0M
  *bits = BROTLI_HC_FAST_LOAD_BITS(table);
443
34.0M
  *value = BROTLI_HC_FAST_LOAD_VALUE(table);
444
34.0M
}
445
446
/* Decodes the next Huffman code using data prepared by PreloadSymbol.
447
   Reads 0 - 15 bits. Also peeks 8 following bits. */
448
static BROTLI_INLINE brotli_reg_t ReadPreloadedSymbol(const HuffmanCode* table,
449
                                                  BrotliBitReader* br,
450
                                                  brotli_reg_t* bits,
451
33.7M
                                                  brotli_reg_t* value) {
452
33.7M
  brotli_reg_t result = *value;
453
33.7M
  if (BROTLI_PREDICT_FALSE(*bits > HUFFMAN_TABLE_BITS)) {
454
15.4k
    brotli_reg_t val = BrotliGet16BitsUnmasked(br);
455
15.4k
    const HuffmanCode* ext = table + (val & HUFFMAN_TABLE_MASK) + *value;
456
15.4k
    brotli_reg_t mask = BitMask((*bits - HUFFMAN_TABLE_BITS));
457
15.4k
    BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(ext);
458
15.4k
    BrotliDropBits(br, HUFFMAN_TABLE_BITS);
459
15.4k
    BROTLI_HC_ADJUST_TABLE_INDEX(ext, (val >> HUFFMAN_TABLE_BITS) & mask);
460
15.4k
    BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(ext));
461
15.4k
    result = BROTLI_HC_FAST_LOAD_VALUE(ext);
462
33.6M
  } else {
463
33.6M
    BrotliDropBits(br, *bits);
464
33.6M
  }
465
33.7M
  PreloadSymbol(0, table, br, bits, value);
466
33.7M
  return result;
467
33.7M
}
468
469
17.2k
static BROTLI_INLINE brotli_reg_t Log2Floor(brotli_reg_t x) {
470
17.2k
  brotli_reg_t result = 0;
471
132k
  while (x) {
472
115k
    x >>= 1;
473
115k
    ++result;
474
115k
  }
475
17.2k
  return result;
476
17.2k
}
477
478
/* Reads (s->symbol + 1) symbols.
479
   Totally 1..4 symbols are read, 1..11 bits each.
480
   The list of symbols MUST NOT contain duplicates. */
481
static BrotliDecoderErrorCode ReadSimpleHuffmanSymbols(
482
    brotli_reg_t alphabet_size_max, brotli_reg_t alphabet_size_limit,
483
17.2k
    BrotliDecoderState* s) {
484
  /* max_bits == 1..11; symbol == 0..3; 1..44 bits will be read. */
485
17.2k
  BrotliBitReader* br = &s->br;
486
17.2k
  BrotliMetablockHeaderArena* h = &s->arena.header;
487
17.2k
  brotli_reg_t max_bits = Log2Floor(alphabet_size_max - 1);
488
17.2k
  brotli_reg_t i = h->sub_loop_counter;
489
17.2k
  brotli_reg_t num_symbols = h->symbol;
490
49.4k
  while (i <= num_symbols) {
491
32.2k
    brotli_reg_t v;
492
32.2k
    if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, max_bits, &v))) {
493
8
      h->sub_loop_counter = i;
494
8
      h->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_READ;
495
8
      return BROTLI_DECODER_NEEDS_MORE_INPUT;
496
8
    }
497
32.2k
    if (v >= alphabet_size_limit) {
498
5
      return
499
5
          BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET);
500
5
    }
501
32.2k
    h->symbols_lists_array[i] = (uint16_t)v;
502
32.2k
    BROTLI_LOG_UINT(h->symbols_lists_array[i]);
503
32.2k
    ++i;
504
32.2k
  }
505
506
32.2k
  for (i = 0; i < num_symbols; ++i) {
507
15.0k
    brotli_reg_t k = i + 1;
508
36.5k
    for (; k <= num_symbols; ++k) {
509
21.5k
      if (h->symbols_lists_array[i] == h->symbols_lists_array[k]) {
510
7
        return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME);
511
7
      }
512
21.5k
    }
513
15.0k
  }
514
515
17.1k
  return BROTLI_DECODER_SUCCESS;
516
17.1k
}
517
518
/* Process single decoded symbol code length:
519
    A) reset the repeat variable
520
    B) remember code length (if it is not 0)
521
    C) extend corresponding index-chain
522
    D) reduce the Huffman space
523
    E) update the histogram */
524
static BROTLI_INLINE void ProcessSingleCodeLength(brotli_reg_t code_len,
525
    brotli_reg_t* symbol, brotli_reg_t* repeat, brotli_reg_t* space,
526
    brotli_reg_t* prev_code_len, uint16_t* symbol_lists,
527
435k
    uint16_t* code_length_histo, int* next_symbol) {
528
435k
  *repeat = 0;
529
435k
  if (code_len != 0) {  /* code_len == 1..15 */
530
274k
    symbol_lists[next_symbol[code_len]] = (uint16_t)(*symbol);
531
274k
    next_symbol[code_len] = (int)(*symbol);
532
274k
    *prev_code_len = code_len;
533
274k
    *space -= 32768U >> code_len;
534
274k
    code_length_histo[code_len]++;
535
274k
    BROTLI_LOG(("[ReadHuffmanCode] code_length[%d] = %d\n",
536
274k
        (int)*symbol, (int)code_len));
537
274k
  }
538
435k
  (*symbol)++;
539
435k
}
540
541
/* Process repeated symbol code length.
542
    A) Check if it is the extension of previous repeat sequence; if the decoded
543
       value is not BROTLI_REPEAT_PREVIOUS_CODE_LENGTH, then it is a new
544
       symbol-skip
545
    B) Update repeat variable
546
    C) Check if operation is feasible (fits alphabet)
547
    D) For each symbol do the same operations as in ProcessSingleCodeLength
548
549
   PRECONDITION: code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH or
550
                 code_len == BROTLI_REPEAT_ZERO_CODE_LENGTH */
551
static BROTLI_INLINE void ProcessRepeatedCodeLength(brotli_reg_t code_len,
552
    brotli_reg_t repeat_delta, brotli_reg_t alphabet_size, brotli_reg_t* symbol,
553
    brotli_reg_t* repeat, brotli_reg_t* space, brotli_reg_t* prev_code_len,
554
    brotli_reg_t* repeat_code_len, uint16_t* symbol_lists,
555
115k
    uint16_t* code_length_histo, int* next_symbol) {
556
115k
  brotli_reg_t old_repeat;
557
115k
  brotli_reg_t extra_bits = 3;  /* for BROTLI_REPEAT_ZERO_CODE_LENGTH */
558
115k
  brotli_reg_t new_len = 0;  /* for BROTLI_REPEAT_ZERO_CODE_LENGTH */
559
115k
  if (code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) {
560
58.3k
    new_len = *prev_code_len;
561
58.3k
    extra_bits = 2;
562
58.3k
  }
563
115k
  if (*repeat_code_len != new_len) {
564
44.9k
    *repeat = 0;
565
44.9k
    *repeat_code_len = new_len;
566
44.9k
  }
567
115k
  old_repeat = *repeat;
568
115k
  if (*repeat > 0) {
569
37.7k
    *repeat -= 2;
570
37.7k
    *repeat <<= extra_bits;
571
37.7k
  }
572
115k
  *repeat += repeat_delta + 3U;
573
115k
  repeat_delta = *repeat - old_repeat;
574
115k
  if (*symbol + repeat_delta > alphabet_size) {
575
37
    BROTLI_DUMP();
576
37
    *symbol = alphabet_size;
577
37
    *space = 0xFFFFF;
578
37
    return;
579
37
  }
580
115k
  BROTLI_LOG(("[ReadHuffmanCode] code_length[%d..%d] = %d\n",
581
115k
      (int)*symbol, (int)(*symbol + repeat_delta - 1), (int)*repeat_code_len));
582
115k
  if (*repeat_code_len != 0) {
583
58.3k
    brotli_reg_t last = *symbol + repeat_delta;
584
58.3k
    int next = next_symbol[*repeat_code_len];
585
436k
    do {
586
436k
      symbol_lists[next] = (uint16_t)*symbol;
587
436k
      next = (int)*symbol;
588
436k
    } while (++(*symbol) != last);
589
58.3k
    next_symbol[*repeat_code_len] = next;
590
58.3k
    *space -= repeat_delta << (15 - *repeat_code_len);
591
58.3k
    code_length_histo[*repeat_code_len] =
592
58.3k
        (uint16_t)(code_length_histo[*repeat_code_len] + repeat_delta);
593
58.3k
  } else {
594
57.2k
    *symbol += repeat_delta;
595
57.2k
  }
596
115k
}
597
598
/* Reads and decodes symbol codelengths. */
599
static BrotliDecoderErrorCode ReadSymbolCodeLengths(
600
9.11k
    brotli_reg_t alphabet_size, BrotliDecoderState* s) {
601
9.11k
  BrotliBitReader* br = &s->br;
602
9.11k
  BrotliMetablockHeaderArena* h = &s->arena.header;
603
9.11k
  brotli_reg_t symbol = h->symbol;
604
9.11k
  brotli_reg_t repeat = h->repeat;
605
9.11k
  brotli_reg_t space = h->space;
606
9.11k
  brotli_reg_t prev_code_len = h->prev_code_len;
607
9.11k
  brotli_reg_t repeat_code_len = h->repeat_code_len;
608
9.11k
  uint16_t* symbol_lists = h->symbol_lists;
609
9.11k
  uint16_t* code_length_histo = h->code_length_histo;
610
9.11k
  int* next_symbol = h->next_symbol;
611
9.11k
  if (!BrotliWarmupBitReader(br)) {
612
7
    return BROTLI_DECODER_NEEDS_MORE_INPUT;
613
7
  }
614
543k
  while (symbol < alphabet_size && space > 0) {
615
534k
    const HuffmanCode* p = h->table;
616
534k
    brotli_reg_t code_len;
617
534k
    BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(p);
618
534k
    if (!BrotliCheckInputAmount(br)) {
619
268
      h->symbol = symbol;
620
268
      h->repeat = repeat;
621
268
      h->prev_code_len = prev_code_len;
622
268
      h->repeat_code_len = repeat_code_len;
623
268
      h->space = space;
624
268
      return BROTLI_DECODER_NEEDS_MORE_INPUT;
625
268
    }
626
534k
    BrotliFillBitWindow16(br);
627
534k
    BROTLI_HC_ADJUST_TABLE_INDEX(p, BrotliGetBitsUnmasked(br) &
628
534k
        BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH));
629
534k
    BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(p));  /* Use 1..5 bits. */
630
534k
    code_len = BROTLI_HC_FAST_LOAD_VALUE(p);  /* code_len == 0..17 */
631
534k
    if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) {
632
419k
      ProcessSingleCodeLength(code_len, &symbol, &repeat, &space,
633
419k
          &prev_code_len, symbol_lists, code_length_histo, next_symbol);
634
419k
    } else {  /* code_len == 16..17, extra_bits == 2..3 */
635
115k
      brotli_reg_t extra_bits =
636
115k
          (code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) ? 2 : 3;
637
115k
      brotli_reg_t repeat_delta =
638
115k
          BrotliGetBitsUnmasked(br) & BitMask(extra_bits);
639
115k
      BrotliDropBits(br, extra_bits);
640
115k
      ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size,
641
115k
          &symbol, &repeat, &space, &prev_code_len, &repeat_code_len,
642
115k
          symbol_lists, code_length_histo, next_symbol);
643
115k
    }
644
534k
  }
645
8.84k
  h->space = space;
646
8.84k
  return BROTLI_DECODER_SUCCESS;
647
9.11k
}
648
649
static BrotliDecoderErrorCode SafeReadSymbolCodeLengths(
650
275
    brotli_reg_t alphabet_size, BrotliDecoderState* s) {
651
275
  BrotliBitReader* br = &s->br;
652
275
  BrotliMetablockHeaderArena* h = &s->arena.header;
653
275
  BROTLI_BOOL get_byte = BROTLI_FALSE;
654
18.8k
  while (h->symbol < alphabet_size && h->space > 0) {
655
18.6k
    const HuffmanCode* p = h->table;
656
18.6k
    brotli_reg_t code_len;
657
18.6k
    brotli_reg_t available_bits;
658
18.6k
    brotli_reg_t bits = 0;
659
18.6k
    BROTLI_HC_MARK_TABLE_FOR_FAST_LOAD(p);
660
18.6k
    if (get_byte && !BrotliPullByte(br)) return BROTLI_DECODER_NEEDS_MORE_INPUT;
661
18.6k
    get_byte = BROTLI_FALSE;
662
18.6k
    available_bits = BrotliGetAvailableBits(br);
663
18.6k
    if (available_bits != 0) {
664
17.4k
      bits = (uint32_t)BrotliGetBitsUnmasked(br);
665
17.4k
    }
666
18.6k
    BROTLI_HC_ADJUST_TABLE_INDEX(p,
667
18.6k
        bits & BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH));
668
18.6k
    if (BROTLI_HC_FAST_LOAD_BITS(p) > available_bits) {
669
882
      get_byte = BROTLI_TRUE;
670
882
      continue;
671
882
    }
672
17.7k
    code_len = BROTLI_HC_FAST_LOAD_VALUE(p);  /* code_len == 0..17 */
673
17.7k
    if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) {
674
16.8k
      BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(p));
675
16.8k
      ProcessSingleCodeLength(code_len, &h->symbol, &h->repeat, &h->space,
676
16.8k
          &h->prev_code_len, h->symbol_lists, h->code_length_histo,
677
16.8k
          h->next_symbol);
678
16.8k
    } else {  /* code_len == 16..17, extra_bits == 2..3 */
679
827
      brotli_reg_t extra_bits = code_len - 14U;
680
827
      brotli_reg_t repeat_delta = (bits >> BROTLI_HC_FAST_LOAD_BITS(p)) &
681
827
          BitMask(extra_bits);
682
827
      if (available_bits < BROTLI_HC_FAST_LOAD_BITS(p) + extra_bits) {
683
200
        get_byte = BROTLI_TRUE;
684
200
        continue;
685
200
      }
686
627
      BrotliDropBits(br, BROTLI_HC_FAST_LOAD_BITS(p) + extra_bits);
687
627
      ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size,
688
627
          &h->symbol, &h->repeat, &h->space, &h->prev_code_len,
689
627
          &h->repeat_code_len, h->symbol_lists, h->code_length_histo,
690
627
          h->next_symbol);
691
627
    }
692
17.7k
  }
693
245
  return BROTLI_DECODER_SUCCESS;
694
275
}
695
696
/* Reads and decodes 15..18 codes using static prefix code.
697
   Each code is 2..4 bits long. In total 30..72 bits are used. */
698
9.22k
static BrotliDecoderErrorCode ReadCodeLengthCodeLengths(BrotliDecoderState* s) {
699
9.22k
  BrotliBitReader* br = &s->br;
700
9.22k
  BrotliMetablockHeaderArena* h = &s->arena.header;
701
9.22k
  brotli_reg_t num_codes = h->repeat;
702
9.22k
  brotli_reg_t space = h->space;
703
9.22k
  brotli_reg_t i = h->sub_loop_counter;
704
79.4k
  for (; i < BROTLI_CODE_LENGTH_CODES; ++i) {
705
79.1k
    const uint8_t code_len_idx = kCodeLengthCodeOrder[i];
706
79.1k
    brotli_reg_t ix;
707
79.1k
    brotli_reg_t v;
708
79.1k
    if (BROTLI_PREDICT_FALSE(!BrotliSafeGetBits(br, 4, &ix))) {
709
54
      brotli_reg_t available_bits = BrotliGetAvailableBits(br);
710
54
      if (available_bits != 0) {
711
43
        ix = BrotliGetBitsUnmasked(br) & 0xF;
712
43
      } else {
713
11
        ix = 0;
714
11
      }
715
54
      if (kCodeLengthPrefixLength[ix] > available_bits) {
716
28
        h->sub_loop_counter = i;
717
28
        h->repeat = num_codes;
718
28
        h->space = space;
719
28
        h->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX;
720
28
        return BROTLI_DECODER_NEEDS_MORE_INPUT;
721
28
      }
722
54
    }
723
79.1k
    v = kCodeLengthPrefixValue[ix];
724
79.1k
    BrotliDropBits(br, kCodeLengthPrefixLength[ix]);
725
79.1k
    h->code_length_code_lengths[code_len_idx] = (uint8_t)v;
726
79.1k
    BROTLI_LOG_ARRAY_INDEX(h->code_length_code_lengths, code_len_idx);
727
79.1k
    if (v != 0) {
728
61.6k
      space = space - (32U >> v);
729
61.6k
      ++num_codes;
730
61.6k
      ++h->code_length_histo[v];
731
61.6k
      if (space - 1U >= 32U) {
732
        /* space is 0 or wrapped around. */
733
8.94k
        break;
734
8.94k
      }
735
61.6k
    }
736
79.1k
  }
737
9.19k
  if (!(num_codes == 1 || space == 0)) {
738
74
    return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_CL_SPACE);
739
74
  }
740
9.11k
  return BROTLI_DECODER_SUCCESS;
741
9.19k
}
742
743
/* Decodes the Huffman tables.
744
   There are 2 scenarios:
745
    A) Huffman code contains only few symbols (1..4). Those symbols are read
746
       directly; their code lengths are defined by the number of symbols.
747
       For this scenario 4 - 49 bits will be read.
748
749
    B) 2-phase decoding:
750
    B.1) Small Huffman table is decoded; it is specified with code lengths
751
         encoded with predefined entropy code. 32 - 74 bits are used.
752
    B.2) Decoded table is used to decode code lengths of symbols in resulting
753
         Huffman table. In worst case 3520 bits are read. */
754
static BrotliDecoderErrorCode ReadHuffmanCode(brotli_reg_t alphabet_size_max,
755
                                              brotli_reg_t alphabet_size_limit,
756
                                              HuffmanCode* table,
757
                                              brotli_reg_t* opt_table_size,
758
26.4k
                                              BrotliDecoderState* s) {
759
26.4k
  BrotliBitReader* br = &s->br;
760
26.4k
  BrotliMetablockHeaderArena* h = &s->arena.header;
761
  /* State machine. */
762
35.6k
  for (;;) {
763
35.6k
    switch (h->substate_huffman) {
764
26.4k
      case BROTLI_STATE_HUFFMAN_NONE:
765
26.4k
        if (!BrotliSafeReadBits(br, 2, &h->sub_loop_counter)) {
766
14
          return BROTLI_DECODER_NEEDS_MORE_INPUT;
767
14
        }
768
26.4k
        BROTLI_LOG_UINT(h->sub_loop_counter);
769
        /* The value is used as follows:
770
           1 for simple code;
771
           0 for no skipping, 2 skips 2 code lengths, 3 skips 3 code lengths */
772
26.4k
        if (h->sub_loop_counter != 1) {
773
9.22k
          h->space = 32;
774
9.22k
          h->repeat = 0;  /* num_codes */
775
9.22k
          memset(&h->code_length_histo[0], 0, sizeof(h->code_length_histo[0]) *
776
9.22k
              (BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1));
777
9.22k
          memset(&h->code_length_code_lengths[0], 0,
778
9.22k
              sizeof(h->code_length_code_lengths));
779
9.22k
          h->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX;
780
9.22k
          continue;
781
9.22k
        }
782
      /* Fall through. */
783
784
17.2k
      case BROTLI_STATE_HUFFMAN_SIMPLE_SIZE:
785
        /* Read symbols, codes & code lengths directly. */
786
17.2k
        if (!BrotliSafeReadBits(br, 2, &h->symbol)) {  /* num_symbols */
787
1
          h->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_SIZE;
788
1
          return BROTLI_DECODER_NEEDS_MORE_INPUT;
789
1
        }
790
17.2k
        h->sub_loop_counter = 0;
791
      /* Fall through. */
792
793
17.2k
      case BROTLI_STATE_HUFFMAN_SIMPLE_READ: {
794
17.2k
        BrotliDecoderErrorCode result =
795
17.2k
            ReadSimpleHuffmanSymbols(alphabet_size_max, alphabet_size_limit, s);
796
17.2k
        if (result != BROTLI_DECODER_SUCCESS) {
797
20
          return result;
798
20
        }
799
17.2k
      }
800
      /* Fall through. */
801
802
17.1k
      case BROTLI_STATE_HUFFMAN_SIMPLE_BUILD: {
803
17.1k
        brotli_reg_t table_size;
804
17.1k
        if (h->symbol == 3) {
805
851
          brotli_reg_t bits;
806
851
          if (!BrotliSafeReadBits(br, 1, &bits)) {
807
1
            h->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_BUILD;
808
1
            return BROTLI_DECODER_NEEDS_MORE_INPUT;
809
1
          }
810
850
          h->symbol += bits;
811
850
        }
812
17.1k
        BROTLI_LOG_UINT(h->symbol);
813
17.1k
        table_size = BrotliBuildSimpleHuffmanTable(table, HUFFMAN_TABLE_BITS,
814
17.1k
                                                   h->symbols_lists_array,
815
17.1k
                                                   (uint32_t)h->symbol);
816
17.1k
        if (opt_table_size) {
817
11.7k
          *opt_table_size = table_size;
818
11.7k
        }
819
17.1k
        h->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
820
17.1k
        return BROTLI_DECODER_SUCCESS;
821
17.1k
      }
822
823
      /* Decode Huffman-coded code lengths. */
824
9.22k
      case BROTLI_STATE_HUFFMAN_COMPLEX: {
825
9.22k
        brotli_reg_t i;
826
9.22k
        BrotliDecoderErrorCode result = ReadCodeLengthCodeLengths(s);
827
9.22k
        if (result != BROTLI_DECODER_SUCCESS) {
828
102
          return result;
829
102
        }
830
9.11k
        BrotliBuildCodeLengthsHuffmanTable(h->table,
831
9.11k
                                           h->code_length_code_lengths,
832
9.11k
                                           h->code_length_histo);
833
9.11k
        memset(&h->code_length_histo[0], 0, sizeof(h->code_length_histo));
834
155k
        for (i = 0; i <= BROTLI_HUFFMAN_MAX_CODE_LENGTH; ++i) {
835
145k
          h->next_symbol[i] = (int)i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
836
145k
          h->symbol_lists[h->next_symbol[i]] = 0xFFFF;
837
145k
        }
838
839
9.11k
        h->symbol = 0;
840
9.11k
        h->prev_code_len = BROTLI_INITIAL_REPEATED_CODE_LENGTH;
841
9.11k
        h->repeat = 0;
842
9.11k
        h->repeat_code_len = 0;
843
9.11k
        h->space = 32768;
844
9.11k
        h->substate_huffman = BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS;
845
9.11k
      }
846
      /* Fall through. */
847
848
9.11k
      case BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS: {
849
9.11k
        brotli_reg_t table_size;
850
9.11k
        BrotliDecoderErrorCode result = ReadSymbolCodeLengths(
851
9.11k
            alphabet_size_limit, s);
852
9.11k
        if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) {
853
275
          result = SafeReadSymbolCodeLengths(alphabet_size_limit, s);
854
275
        }
855
9.11k
        if (result != BROTLI_DECODER_SUCCESS) {
856
30
          return result;
857
30
        }
858
859
9.08k
        if (h->space != 0) {
860
94
          BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", (int)h->space));
861
94
          return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE);
862
94
        }
863
8.99k
        table_size = BrotliBuildHuffmanTable(
864
8.99k
            table, HUFFMAN_TABLE_BITS, h->symbol_lists, h->code_length_histo);
865
8.99k
        if (opt_table_size) {
866
7.94k
          *opt_table_size = table_size;
867
7.94k
        }
868
8.99k
        h->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
869
8.99k
        return BROTLI_DECODER_SUCCESS;
870
9.08k
      }
871
872
0
      default:
873
0
        return
874
0
            BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE);  /* COV_NF_LINE */
875
35.6k
    }
876
35.6k
  }
877
26.4k
}
878
879
/* Decodes a block length by reading 3..39 bits. */
880
static BROTLI_INLINE brotli_reg_t ReadBlockLength(const HuffmanCode* table,
881
257k
                                                  BrotliBitReader* br) {
882
257k
  brotli_reg_t code;
883
257k
  brotli_reg_t nbits;
884
257k
  code = ReadSymbol(table, br);
885
257k
  nbits = _kBrotliPrefixCodeRanges[code].nbits;  /* nbits == 2..24 */
886
257k
  return _kBrotliPrefixCodeRanges[code].offset + BrotliReadBits24(br, nbits);
887
257k
}
888
889
/* WARNING: if state is not BROTLI_STATE_READ_BLOCK_LENGTH_NONE, then
890
   reading can't be continued with ReadBlockLength. */
891
static BROTLI_INLINE BROTLI_BOOL SafeReadBlockLength(
892
    BrotliDecoderState* s, brotli_reg_t* result, const HuffmanCode* table,
893
4.98k
    BrotliBitReader* br) {
894
4.98k
  brotli_reg_t index;
895
4.98k
  if (s->substate_read_block_length == BROTLI_STATE_READ_BLOCK_LENGTH_NONE) {
896
4.98k
    if (!SafeReadSymbol(table, br, &index)) {
897
12
      return BROTLI_FALSE;
898
12
    }
899
4.98k
  } else {
900
0
    index = s->block_length_index;
901
0
  }
902
4.97k
  {
903
4.97k
    brotli_reg_t bits;
904
4.97k
    brotli_reg_t nbits = _kBrotliPrefixCodeRanges[index].nbits;
905
4.97k
    brotli_reg_t offset = _kBrotliPrefixCodeRanges[index].offset;
906
4.97k
    if (!BrotliSafeReadBits(br, nbits, &bits)) {
907
23
      s->block_length_index = index;
908
23
      s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX;
909
23
      return BROTLI_FALSE;
910
23
    }
911
4.95k
    *result = offset + bits;
912
4.95k
    s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
913
4.95k
    return BROTLI_TRUE;
914
4.97k
  }
915
4.97k
}
916
917
/* Transform:
918
    1) initialize list L with values 0, 1,... 255
919
    2) For each input element X:
920
    2.1) let Y = L[X]
921
    2.2) remove X-th element from L
922
    2.3) prepend Y to L
923
    2.4) append Y to output
924
925
   In most cases max(Y) <= 7, so most of L remains intact.
926
   To reduce the cost of initialization, we reuse L, remember the upper bound
927
   of Y values, and reinitialize only first elements in L.
928
929
   Most of input values are 0 and 1. To reduce number of branches, we replace
930
   inner for loop with do-while. */
931
static BROTLI_NOINLINE void InverseMoveToFrontTransform(
932
856
    uint8_t* v, brotli_reg_t v_len, BrotliDecoderState* state) {
933
  /* Reinitialize elements that could have been changed. */
934
856
  brotli_reg_t i = 1;
935
856
  brotli_reg_t upper_bound = state->mtf_upper_bound;
936
856
  uint32_t* mtf = &state->mtf[1];  /* Make mtf[-1] addressable. */
937
856
  uint8_t* mtf_u8 = (uint8_t*)mtf;
938
  /* Load endian-aware constant. */
939
856
  const uint8_t b0123[4] = {0, 1, 2, 3};
940
856
  uint32_t pattern;
941
856
  memcpy(&pattern, &b0123, 4);
942
943
  /* Initialize list using 4 consequent values pattern. */
944
856
  mtf[0] = pattern;
945
48.2k
  do {
946
48.2k
    pattern += 0x04040404;  /* Advance all 4 values by 4. */
947
48.2k
    mtf[i] = pattern;
948
48.2k
    i++;
949
48.2k
  } while (i <= upper_bound);
950
951
  /* Transform the input. */
952
856
  upper_bound = 0;
953
177k
  for (i = 0; i < v_len; ++i) {
954
176k
    int index = v[i];
955
176k
    uint8_t value = mtf_u8[index];
956
176k
    upper_bound |= v[i];
957
176k
    v[i] = value;
958
176k
    mtf_u8[-1] = value;
959
1.44M
    do {
960
1.44M
      index--;
961
1.44M
      mtf_u8[index + 1] = mtf_u8[index];
962
1.44M
    } while (index >= 0);
963
176k
  }
964
  /* Remember amount of elements to be reinitialized. */
965
856
  state->mtf_upper_bound = upper_bound >> 2;
966
856
}
967
968
/* Decodes a series of Huffman table using ReadHuffmanCode function. */
969
static BrotliDecoderErrorCode HuffmanTreeGroupDecode(
970
13.0k
    HuffmanTreeGroup* group, BrotliDecoderState* s) {
971
13.0k
  BrotliMetablockHeaderArena* h = &s->arena.header;
972
13.0k
  if (h->substate_tree_group != BROTLI_STATE_TREE_GROUP_LOOP) {
973
13.0k
    h->next = group->codes;
974
13.0k
    h->htree_index = 0;
975
13.0k
    h->substate_tree_group = BROTLI_STATE_TREE_GROUP_LOOP;
976
13.0k
  }
977
32.7k
  while (h->htree_index < group->num_htrees) {
978
19.8k
    brotli_reg_t table_size;
979
19.8k
    BrotliDecoderErrorCode result = ReadHuffmanCode(group->alphabet_size_max,
980
19.8k
        group->alphabet_size_limit, h->next, &table_size, s);
981
19.8k
    if (result != BROTLI_DECODER_SUCCESS) return result;
982
19.6k
    group->htrees[h->htree_index] = h->next;
983
19.6k
    h->next += table_size;
984
19.6k
    ++h->htree_index;
985
19.6k
  }
986
12.8k
  h->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;
987
12.8k
  return BROTLI_DECODER_SUCCESS;
988
13.0k
}
989
990
/* Decodes a context map.
991
   Decoding is done in 4 phases:
992
    1) Read auxiliary information (6..16 bits) and allocate memory.
993
       In case of trivial context map, decoding is finished at this phase.
994
    2) Decode Huffman table using ReadHuffmanCode function.
995
       This table will be used for reading context map items.
996
    3) Read context map items; "0" values could be run-length encoded.
997
    4) Optionally, apply InverseMoveToFront transform to the resulting map. */
998
static BrotliDecoderErrorCode DecodeContextMap(brotli_reg_t context_map_size,
999
                                               brotli_reg_t* num_htrees,
1000
                                               uint8_t** context_map_arg,
1001
8.95k
                                               BrotliDecoderState* s) {
1002
8.95k
  BrotliBitReader* br = &s->br;
1003
8.95k
  BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS;
1004
8.95k
  BrotliMetablockHeaderArena* h = &s->arena.header;
1005
1006
8.95k
  switch ((int)h->substate_context_map) {
1007
8.95k
    case BROTLI_STATE_CONTEXT_MAP_NONE:
1008
8.95k
      result = DecodeVarLenUint8(s, br, num_htrees);
1009
8.95k
      if (result != BROTLI_DECODER_SUCCESS) {
1010
4
        return result;
1011
4
      }
1012
8.94k
      (*num_htrees)++;
1013
8.94k
      h->context_index = 0;
1014
8.94k
      BROTLI_LOG_UINT(context_map_size);
1015
8.94k
      BROTLI_LOG_UINT(*num_htrees);
1016
8.94k
      *context_map_arg =
1017
8.94k
          (uint8_t*)BROTLI_DECODER_ALLOC(s, (size_t)context_map_size);
1018
8.94k
      if (*context_map_arg == 0) {
1019
0
        return BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP);
1020
0
      }
1021
8.94k
      if (*num_htrees <= 1) {
1022
7.97k
        memset(*context_map_arg, 0, (size_t)context_map_size);
1023
7.97k
        return BROTLI_DECODER_SUCCESS;
1024
7.97k
      }
1025
972
      h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_READ_PREFIX;
1026
    /* Fall through. */
1027
1028
972
    case BROTLI_STATE_CONTEXT_MAP_READ_PREFIX: {
1029
972
      brotli_reg_t bits;
1030
      /* In next stage ReadHuffmanCode uses at least 4 bits, so it is safe
1031
         to peek 4 bits ahead. */
1032
972
      if (!BrotliSafeGetBits(br, 5, &bits)) {
1033
1
        return BROTLI_DECODER_NEEDS_MORE_INPUT;
1034
1
      }
1035
971
      if ((bits & 1) != 0) { /* Use RLE for zeros. */
1036
873
        h->max_run_length_prefix = (bits >> 1) + 1;
1037
873
        BrotliDropBits(br, 5);
1038
873
      } else {
1039
98
        h->max_run_length_prefix = 0;
1040
98
        BrotliDropBits(br, 1);
1041
98
      }
1042
971
      BROTLI_LOG_UINT(h->max_run_length_prefix);
1043
971
      h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_HUFFMAN;
1044
971
    }
1045
    /* Fall through. */
1046
1047
971
    case BROTLI_STATE_CONTEXT_MAP_HUFFMAN: {
1048
971
      brotli_reg_t alphabet_size = *num_htrees + h->max_run_length_prefix;
1049
971
      result = ReadHuffmanCode(alphabet_size, alphabet_size,
1050
971
                               h->context_map_table, NULL, s);
1051
971
      if (result != BROTLI_DECODER_SUCCESS) return result;
1052
937
      h->code = 0xFFFF;
1053
937
      h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_DECODE;
1054
937
    }
1055
    /* Fall through. */
1056
1057
937
    case BROTLI_STATE_CONTEXT_MAP_DECODE: {
1058
937
      brotli_reg_t context_index = h->context_index;
1059
937
      brotli_reg_t max_run_length_prefix = h->max_run_length_prefix;
1060
937
      uint8_t* context_map = *context_map_arg;
1061
937
      brotli_reg_t code = h->code;
1062
937
      BROTLI_BOOL skip_preamble = (code != 0xFFFF);
1063
91.0k
      while (context_index < context_map_size || skip_preamble) {
1064
90.1k
        if (!skip_preamble) {
1065
90.1k
          if (!SafeReadSymbol(h->context_map_table, br, &code)) {
1066
12
            h->code = 0xFFFF;
1067
12
            h->context_index = context_index;
1068
12
            return BROTLI_DECODER_NEEDS_MORE_INPUT;
1069
12
          }
1070
90.1k
          BROTLI_LOG_UINT(code);
1071
1072
90.1k
          if (code == 0) {
1073
14.9k
            context_map[context_index++] = 0;
1074
14.9k
            continue;
1075
14.9k
          }
1076
75.1k
          if (code > max_run_length_prefix) {
1077
61.8k
            context_map[context_index++] =
1078
61.8k
                (uint8_t)(code - max_run_length_prefix);
1079
61.8k
            continue;
1080
61.8k
          }
1081
75.1k
        } else {
1082
0
          skip_preamble = BROTLI_FALSE;
1083
0
        }
1084
        /* RLE sub-stage. */
1085
13.2k
        {
1086
13.2k
          brotli_reg_t reps;
1087
13.2k
          if (!BrotliSafeReadBits(br, code, &reps)) {
1088
7
            h->code = code;
1089
7
            h->context_index = context_index;
1090
7
            return BROTLI_DECODER_NEEDS_MORE_INPUT;
1091
7
          }
1092
13.2k
          reps += 1U << code;
1093
13.2k
          BROTLI_LOG_UINT(reps);
1094
13.2k
          if (context_index + reps > context_map_size) {
1095
19
            return
1096
19
                BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT);
1097
19
          }
1098
144k
          do {
1099
144k
            context_map[context_index++] = 0;
1100
144k
          } while (--reps);
1101
13.2k
        }
1102
13.2k
      }
1103
937
    }
1104
    /* Fall through. */
1105
1106
899
    case BROTLI_STATE_CONTEXT_MAP_TRANSFORM: {
1107
899
      brotli_reg_t bits;
1108
899
      if (!BrotliSafeReadBits(br, 1, &bits)) {
1109
1
        h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_TRANSFORM;
1110
1
        return BROTLI_DECODER_NEEDS_MORE_INPUT;
1111
1
      }
1112
898
      if (bits != 0) {
1113
856
        InverseMoveToFrontTransform(*context_map_arg, context_map_size, s);
1114
856
      }
1115
898
      h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;
1116
898
      return BROTLI_DECODER_SUCCESS;
1117
899
    }
1118
1119
0
    default:
1120
0
      return
1121
0
          BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE);  /* COV_NF_LINE */
1122
8.95k
  }
1123
8.95k
}
1124
1125
/* Decodes a command or literal and updates block type ring-buffer.
1126
   Reads 3..54 bits. */
1127
static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength(
1128
259k
    int safe, BrotliDecoderState* s, int tree_type) {
1129
259k
  brotli_reg_t max_block_type = s->num_block_types[tree_type];
1130
259k
  const HuffmanCode* type_tree = &s->block_type_trees[
1131
259k
      tree_type * BROTLI_HUFFMAN_MAX_SIZE_258];
1132
259k
  const HuffmanCode* len_tree = &s->block_len_trees[
1133
259k
      tree_type * BROTLI_HUFFMAN_MAX_SIZE_26];
1134
259k
  BrotliBitReader* br = &s->br;
1135
259k
  brotli_reg_t* ringbuffer = &s->block_type_rb[tree_type * 2];
1136
259k
  brotli_reg_t block_type;
1137
259k
  if (max_block_type <= 1) {
1138
0
    return BROTLI_FALSE;
1139
0
  }
1140
1141
  /* Read 0..15 + 3..39 bits. */
1142
259k
  if (!safe) {
1143
257k
    block_type = ReadSymbol(type_tree, br);
1144
257k
    s->block_length[tree_type] = ReadBlockLength(len_tree, br);
1145
257k
  } else {
1146
2.22k
    BrotliBitReaderState memento;
1147
2.22k
    BrotliBitReaderSaveState(br, &memento);
1148
2.22k
    if (!SafeReadSymbol(type_tree, br, &block_type)) return BROTLI_FALSE;
1149
2.21k
    if (!SafeReadBlockLength(s, &s->block_length[tree_type], len_tree, br)) {
1150
30
      s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
1151
30
      BrotliBitReaderRestoreState(br, &memento);
1152
30
      return BROTLI_FALSE;
1153
30
    }
1154
2.21k
  }
1155
1156
259k
  if (block_type == 1) {
1157
13.4k
    block_type = ringbuffer[1] + 1;
1158
246k
  } else if (block_type == 0) {
1159
218k
    block_type = ringbuffer[0];
1160
218k
  } else {
1161
27.3k
    block_type -= 2;
1162
27.3k
  }
1163
259k
  if (block_type >= max_block_type) {
1164
3.92k
    block_type -= max_block_type;
1165
3.92k
  }
1166
259k
  ringbuffer[0] = ringbuffer[1];
1167
259k
  ringbuffer[1] = block_type;
1168
259k
  return BROTLI_TRUE;
1169
259k
}
1170
1171
static BROTLI_INLINE void DetectTrivialLiteralBlockTypes(
1172
4.44k
    BrotliDecoderState* s) {
1173
4.44k
  size_t i;
1174
40.0k
  for (i = 0; i < 8; ++i) s->trivial_literal_contexts[i] = 0;
1175
19.8k
  for (i = 0; i < s->num_block_types[0]; i++) {
1176
15.4k
    size_t offset = i << BROTLI_LITERAL_CONTEXT_BITS;
1177
15.4k
    size_t error = 0;
1178
15.4k
    size_t sample = s->context_map[offset];
1179
15.4k
    size_t j;
1180
261k
    for (j = 0; j < (1u << BROTLI_LITERAL_CONTEXT_BITS);) {
1181
      /* NOLINTNEXTLINE(bugprone-macro-repeated-side-effects) */
1182
246k
      BROTLI_REPEAT_4({ error |= s->context_map[offset + j++] ^ sample; })
1183
246k
    }
1184
15.4k
    if (error == 0) {
1185
12.6k
      s->trivial_literal_contexts[i >> 5] |= 1u << (i & 31);
1186
12.6k
    }
1187
15.4k
  }
1188
4.44k
}
1189
1190
22.3k
static BROTLI_INLINE void PrepareLiteralDecoding(BrotliDecoderState* s) {
1191
22.3k
  uint8_t context_mode;
1192
22.3k
  size_t trivial;
1193
22.3k
  brotli_reg_t block_type = s->block_type_rb[1];
1194
22.3k
  brotli_reg_t context_offset = block_type << BROTLI_LITERAL_CONTEXT_BITS;
1195
22.3k
  s->context_map_slice = s->context_map + context_offset;
1196
22.3k
  trivial = s->trivial_literal_contexts[block_type >> 5];
1197
22.3k
  s->trivial_literal_context = (trivial >> (block_type & 31)) & 1;
1198
22.3k
  s->literal_htree = s->literal_hgroup.htrees[s->context_map_slice[0]];
1199
22.3k
  context_mode = s->context_modes[block_type] & 3;
1200
22.3k
  s->context_lookup = BROTLI_CONTEXT_LUT(context_mode);
1201
22.3k
}
1202
1203
/* Decodes the block type and updates the state for literal context.
1204
   Reads 3..54 bits. */
1205
static BROTLI_INLINE BROTLI_BOOL DecodeLiteralBlockSwitchInternal(
1206
18.0k
    int safe, BrotliDecoderState* s) {
1207
18.0k
  if (!DecodeBlockTypeAndLength(safe, s, 0)) {
1208
25
    return BROTLI_FALSE;
1209
25
  }
1210
18.0k
  PrepareLiteralDecoding(s);
1211
18.0k
  return BROTLI_TRUE;
1212
18.0k
}
1213
1214
16.9k
static void BROTLI_NOINLINE DecodeLiteralBlockSwitch(BrotliDecoderState* s) {
1215
16.9k
  DecodeLiteralBlockSwitchInternal(0, s);
1216
16.9k
}
1217
1218
static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeLiteralBlockSwitch(
1219
1.10k
    BrotliDecoderState* s) {
1220
1.10k
  return DecodeLiteralBlockSwitchInternal(1, s);
1221
1.10k
}
1222
1223
/* Block switch for insert/copy length.
1224
   Reads 3..54 bits. */
1225
static BROTLI_INLINE BROTLI_BOOL DecodeCommandBlockSwitchInternal(
1226
209k
    int safe, BrotliDecoderState* s) {
1227
209k
  if (!DecodeBlockTypeAndLength(safe, s, 1)) {
1228
9
    return BROTLI_FALSE;
1229
9
  }
1230
209k
  s->htree_command = s->insert_copy_hgroup.htrees[s->block_type_rb[3]];
1231
209k
  return BROTLI_TRUE;
1232
209k
}
1233
1234
208k
static void BROTLI_NOINLINE DecodeCommandBlockSwitch(BrotliDecoderState* s) {
1235
208k
  DecodeCommandBlockSwitchInternal(0, s);
1236
208k
}
1237
1238
static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeCommandBlockSwitch(
1239
669
    BrotliDecoderState* s) {
1240
669
  return DecodeCommandBlockSwitchInternal(1, s);
1241
669
}
1242
1243
/* Block switch for distance codes.
1244
   Reads 3..54 bits. */
1245
static BROTLI_INLINE BROTLI_BOOL DecodeDistanceBlockSwitchInternal(
1246
32.5k
    int safe, BrotliDecoderState* s) {
1247
32.5k
  if (!DecodeBlockTypeAndLength(safe, s, 2)) {
1248
11
    return BROTLI_FALSE;
1249
11
  }
1250
32.5k
  s->dist_context_map_slice = s->dist_context_map +
1251
32.5k
      (s->block_type_rb[5] << BROTLI_DISTANCE_CONTEXT_BITS);
1252
32.5k
  s->dist_htree_index = s->dist_context_map_slice[s->distance_context];
1253
32.5k
  return BROTLI_TRUE;
1254
32.5k
}
1255
1256
32.0k
static void BROTLI_NOINLINE DecodeDistanceBlockSwitch(BrotliDecoderState* s) {
1257
32.0k
  DecodeDistanceBlockSwitchInternal(0, s);
1258
32.0k
}
1259
1260
static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeDistanceBlockSwitch(
1261
452
    BrotliDecoderState* s) {
1262
452
  return DecodeDistanceBlockSwitchInternal(1, s);
1263
452
}
1264
1265
3.87k
static size_t UnwrittenBytes(const BrotliDecoderState* s, BROTLI_BOOL wrap) {
1266
3.87k
  size_t pos = wrap && s->pos > s->ringbuffer_size ?
1267
3.67k
      (size_t)s->ringbuffer_size : (size_t)(s->pos);
1268
3.87k
  size_t partial_pos_rb = (s->rb_roundtrips * (size_t)s->ringbuffer_size) + pos;
1269
3.87k
  return partial_pos_rb - s->partial_pos_out;
1270
3.87k
}
1271
1272
/* Dumps output.
1273
   Returns BROTLI_DECODER_NEEDS_MORE_OUTPUT only if there is more output to push
1274
   and either ring-buffer is as big as window size, or |force| is true. */
1275
static BrotliDecoderErrorCode BROTLI_NOINLINE WriteRingBuffer(
1276
    BrotliDecoderState* s, size_t* available_out, uint8_t** next_out,
1277
3.87k
    size_t* total_out, BROTLI_BOOL force) {
1278
3.87k
  uint8_t* start =
1279
3.87k
      s->ringbuffer + (s->partial_pos_out & (size_t)s->ringbuffer_mask);
1280
3.87k
  size_t to_write = UnwrittenBytes(s, BROTLI_TRUE);
1281
3.87k
  size_t num_written = *available_out;
1282
3.87k
  if (num_written > to_write) {
1283
3.04k
    num_written = to_write;
1284
3.04k
  }
1285
3.87k
  if (s->meta_block_remaining_len < 0) {
1286
54
    return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1);
1287
54
  }
1288
3.81k
  if (next_out && !*next_out) {
1289
0
    *next_out = start;
1290
3.81k
  } else {
1291
3.81k
    if (next_out) {
1292
3.81k
      memcpy(*next_out, start, num_written);
1293
3.81k
      *next_out += num_written;
1294
3.81k
    }
1295
3.81k
  }
1296
3.81k
  *available_out -= num_written;
1297
3.81k
  BROTLI_LOG_UINT(to_write);
1298
3.81k
  BROTLI_LOG_UINT(num_written);
1299
3.81k
  s->partial_pos_out += num_written;
1300
3.81k
  if (total_out) {
1301
3.81k
    *total_out = s->partial_pos_out;
1302
3.81k
  }
1303
3.81k
  if (num_written < to_write) {
1304
236
    if (s->ringbuffer_size == (1 << s->window_bits) || force) {
1305
232
      return BROTLI_DECODER_NEEDS_MORE_OUTPUT;
1306
232
    } else {
1307
4
      return BROTLI_DECODER_SUCCESS;
1308
4
    }
1309
236
  }
1310
  /* Wrap ring buffer only if it has reached its maximal size. */
1311
3.58k
  if (s->ringbuffer_size == (1 << s->window_bits) &&
1312
3.58k
      s->pos >= s->ringbuffer_size) {
1313
2.90k
    s->pos -= s->ringbuffer_size;
1314
2.90k
    s->rb_roundtrips++;
1315
2.90k
    s->should_wrap_ringbuffer = (size_t)s->pos != 0 ? 1 : 0;
1316
2.90k
  }
1317
3.58k
  return BROTLI_DECODER_SUCCESS;
1318
3.81k
}
1319
1320
2.87k
static void BROTLI_NOINLINE WrapRingBuffer(BrotliDecoderState* s) {
1321
2.87k
  if (s->should_wrap_ringbuffer) {
1322
191
    memcpy(s->ringbuffer, s->ringbuffer_end, (size_t)s->pos);
1323
191
    s->should_wrap_ringbuffer = 0;
1324
191
  }
1325
2.87k
}
1326
1327
/* Allocates ring-buffer.
1328
1329
   s->ringbuffer_size MUST be updated by BrotliCalculateRingBufferSize before
1330
   this function is called.
1331
1332
   Last two bytes of ring-buffer are initialized to 0, so context calculation
1333
   could be done uniformly for the first two and all other positions. */
1334
static BROTLI_BOOL BROTLI_NOINLINE BrotliEnsureRingBuffer(
1335
7.09k
    BrotliDecoderState* s) {
1336
7.09k
  uint8_t* old_ringbuffer = s->ringbuffer;
1337
7.09k
  if (s->ringbuffer_size == s->new_ringbuffer_size) {
1338
5.72k
    return BROTLI_TRUE;
1339
5.72k
  }
1340
1341
1.37k
  s->ringbuffer = (uint8_t*)BROTLI_DECODER_ALLOC(s,
1342
1.37k
      (size_t)(s->new_ringbuffer_size) + kRingBufferWriteAheadSlack);
1343
1.37k
  if (s->ringbuffer == 0) {
1344
    /* Restore previous value. */
1345
0
    s->ringbuffer = old_ringbuffer;
1346
0
    return BROTLI_FALSE;
1347
0
  }
1348
1.37k
  s->ringbuffer[s->new_ringbuffer_size - 2] = 0;
1349
1.37k
  s->ringbuffer[s->new_ringbuffer_size - 1] = 0;
1350
1351
1.37k
  if (!!old_ringbuffer) {
1352
122
    memcpy(s->ringbuffer, old_ringbuffer, (size_t)s->pos);
1353
122
    BROTLI_DECODER_FREE(s, old_ringbuffer);
1354
122
  }
1355
1356
1.37k
  s->ringbuffer_size = s->new_ringbuffer_size;
1357
1.37k
  s->ringbuffer_mask = s->new_ringbuffer_size - 1;
1358
1.37k
  s->ringbuffer_end = s->ringbuffer + s->ringbuffer_size;
1359
1360
1.37k
  return BROTLI_TRUE;
1361
1.37k
}
1362
1363
static BrotliDecoderErrorCode BROTLI_NOINLINE
1364
37.5k
SkipMetadataBlock(BrotliDecoderState* s) {
1365
37.5k
  BrotliBitReader* br = &s->br;
1366
1367
37.5k
  if (s->meta_block_remaining_len == 0) {
1368
29.0k
    return BROTLI_DECODER_SUCCESS;
1369
29.0k
  }
1370
1371
8.56k
  BROTLI_DCHECK((BrotliGetAvailableBits(br) & 7) == 0);
1372
1373
  /* Drain accumulator. */
1374
8.56k
  if (BrotliGetAvailableBits(br) >= 8) {
1375
1.41k
    uint8_t buffer[8];
1376
1.41k
    int nbytes = (int)(BrotliGetAvailableBits(br)) >> 3;
1377
1.41k
    BROTLI_DCHECK(nbytes <= 8);
1378
1.41k
    if (nbytes > s->meta_block_remaining_len) {
1379
402
      nbytes = s->meta_block_remaining_len;
1380
402
    }
1381
1.41k
    BrotliCopyBytes(buffer, br, (size_t)nbytes);
1382
1.41k
    if (s->metadata_chunk_func) {
1383
0
      s->metadata_chunk_func(s->metadata_callback_opaque, buffer,
1384
0
                             (size_t)nbytes);
1385
0
    }
1386
1.41k
    s->meta_block_remaining_len -= nbytes;
1387
1.41k
    if (s->meta_block_remaining_len == 0) {
1388
610
      return BROTLI_DECODER_SUCCESS;
1389
610
    }
1390
1.41k
  }
1391
1392
  /* Direct access to metadata is possible. */
1393
7.95k
  int nbytes = (int)BrotliGetRemainingBytes(br);
1394
7.95k
  if (nbytes > s->meta_block_remaining_len) {
1395
7.92k
    nbytes = s->meta_block_remaining_len;
1396
7.92k
  }
1397
7.95k
  if (nbytes > 0) {
1398
7.95k
    if (s->metadata_chunk_func) {
1399
0
      s->metadata_chunk_func(s->metadata_callback_opaque, br->next_in,
1400
0
                             (size_t)nbytes);
1401
0
    }
1402
7.95k
    BrotliDropBytes(br, (size_t)nbytes);
1403
7.95k
    s->meta_block_remaining_len -= nbytes;
1404
7.95k
    if (s->meta_block_remaining_len == 0) {
1405
7.92k
      return BROTLI_DECODER_SUCCESS;
1406
7.92k
    }
1407
7.95k
  }
1408
1409
30
  BROTLI_DCHECK(BrotliGetRemainingBytes(br) == 0);
1410
1411
30
  return BROTLI_DECODER_NEEDS_MORE_INPUT;
1412
7.95k
}
1413
1414
static BrotliDecoderErrorCode BROTLI_NOINLINE CopyUncompressedBlockToOutput(
1415
    size_t* available_out, uint8_t** next_out, size_t* total_out,
1416
2.83k
    BrotliDecoderState* s) {
1417
  /* TODO(eustas): avoid allocation for single uncompressed block. */
1418
2.83k
  if (!BrotliEnsureRingBuffer(s)) {
1419
0
    return BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1);
1420
0
  }
1421
1422
  /* State machine */
1423
2.86k
  for (;;) {
1424
2.86k
    switch (s->substate_uncompressed) {
1425
2.86k
      case BROTLI_STATE_UNCOMPRESSED_NONE: {
1426
2.86k
        int nbytes = (int)BrotliGetRemainingBytes(&s->br);
1427
2.86k
        if (nbytes > s->meta_block_remaining_len) {
1428
2.78k
          nbytes = s->meta_block_remaining_len;
1429
2.78k
        }
1430
2.86k
        if (s->pos + nbytes > s->ringbuffer_size) {
1431
34
          nbytes = s->ringbuffer_size - s->pos;
1432
34
        }
1433
        /* Copy remaining bytes from s->br.buf_ to ring-buffer. */
1434
2.86k
        BrotliCopyBytes(&s->ringbuffer[s->pos], &s->br, (size_t)nbytes);
1435
2.86k
        s->pos += nbytes;
1436
2.86k
        s->meta_block_remaining_len -= nbytes;
1437
2.86k
        if (s->pos < 1 << s->window_bits) {
1438
2.83k
          if (s->meta_block_remaining_len == 0) {
1439
2.78k
            return BROTLI_DECODER_SUCCESS;
1440
2.78k
          }
1441
52
          return BROTLI_DECODER_NEEDS_MORE_INPUT;
1442
2.83k
        }
1443
34
        s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_WRITE;
1444
34
      }
1445
      /* Fall through. */
1446
1447
34
      case BROTLI_STATE_UNCOMPRESSED_WRITE: {
1448
34
        BrotliDecoderErrorCode result;
1449
34
        result = WriteRingBuffer(
1450
34
            s, available_out, next_out, total_out, BROTLI_FALSE);
1451
34
        if (result != BROTLI_DECODER_SUCCESS) {
1452
3
          return result;
1453
3
        }
1454
31
        if (s->ringbuffer_size == 1 << s->window_bits) {
1455
31
          s->max_distance = s->max_backward_distance;
1456
31
        }
1457
31
        s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE;
1458
31
        break;
1459
34
      }
1460
2.86k
    }
1461
2.86k
  }
1462
0
  BROTLI_DCHECK(0);  /* Unreachable */
1463
0
}
1464
1465
static BROTLI_BOOL AttachCompoundDictionary(
1466
0
    BrotliDecoderState* state, const uint8_t* data, size_t size) {
1467
0
  BrotliDecoderCompoundDictionary* addon = state->compound_dictionary;
1468
0
  if (state->state != BROTLI_STATE_UNINITED) return BROTLI_FALSE;
1469
0
  if (!addon) {
1470
0
    addon = (BrotliDecoderCompoundDictionary*)BROTLI_DECODER_ALLOC(
1471
0
        state, sizeof(BrotliDecoderCompoundDictionary));
1472
0
    if (!addon) return BROTLI_FALSE;
1473
0
    addon->num_chunks = 0;
1474
0
    addon->total_size = 0;
1475
0
    addon->br_length = 0;
1476
0
    addon->br_copied = 0;
1477
0
    addon->block_bits = -1;
1478
0
    addon->chunk_offsets[0] = 0;
1479
0
    state->compound_dictionary = addon;
1480
0
  }
1481
0
  if (addon->num_chunks == 15) return BROTLI_FALSE;
1482
0
  addon->chunks[addon->num_chunks] = data;
1483
0
  addon->num_chunks++;
1484
0
  addon->total_size += (int)size;
1485
0
  addon->chunk_offsets[addon->num_chunks] = addon->total_size;
1486
0
  return BROTLI_TRUE;
1487
0
}
1488
1489
0
static void EnsureCoumpoundDictionaryInitialized(BrotliDecoderState* state) {
1490
0
  BrotliDecoderCompoundDictionary* addon = state->compound_dictionary;
1491
  /* 256 = (1 << 8) slots in block map. */
1492
0
  int block_bits = 8;
1493
0
  int cursor = 0;
1494
0
  int index = 0;
1495
0
  if (addon->block_bits != -1) return;
1496
0
  while (((addon->total_size - 1) >> block_bits) != 0) block_bits++;
1497
0
  block_bits -= 8;
1498
0
  addon->block_bits = block_bits;
1499
0
  while (cursor < addon->total_size) {
1500
0
    while (addon->chunk_offsets[index + 1] < cursor) index++;
1501
0
    addon->block_map[cursor >> block_bits] = (uint8_t)index;
1502
0
    cursor += 1 << block_bits;
1503
0
  }
1504
0
}
1505
1506
static BROTLI_BOOL InitializeCompoundDictionaryCopy(BrotliDecoderState* s,
1507
0
    int address, int length) {
1508
0
  BrotliDecoderCompoundDictionary* addon = s->compound_dictionary;
1509
0
  int index;
1510
0
  EnsureCoumpoundDictionaryInitialized(s);
1511
0
  index = addon->block_map[address >> addon->block_bits];
1512
0
  while (address >= addon->chunk_offsets[index + 1]) index++;
1513
0
  if (addon->total_size < address + length) return BROTLI_FALSE;
1514
  /* Update the recent distances cache. */
1515
0
  s->dist_rb[s->dist_rb_idx & 3] = s->distance_code;
1516
0
  ++s->dist_rb_idx;
1517
0
  s->meta_block_remaining_len -= length;
1518
0
  addon->br_index = index;
1519
0
  addon->br_offset = address - addon->chunk_offsets[index];
1520
0
  addon->br_length = length;
1521
0
  addon->br_copied = 0;
1522
0
  return BROTLI_TRUE;
1523
0
}
1524
1525
8.14k
static int GetCompoundDictionarySize(BrotliDecoderState* s) {
1526
8.14k
  return s->compound_dictionary ? s->compound_dictionary->total_size : 0;
1527
8.14k
}
1528
1529
0
static int CopyFromCompoundDictionary(BrotliDecoderState* s, int pos) {
1530
0
  BrotliDecoderCompoundDictionary* addon = s->compound_dictionary;
1531
0
  int orig_pos = pos;
1532
0
  while (addon->br_length != addon->br_copied) {
1533
0
    uint8_t* copy_dst = &s->ringbuffer[pos];
1534
0
    const uint8_t* copy_src =
1535
0
        addon->chunks[addon->br_index] + addon->br_offset;
1536
0
    int space = s->ringbuffer_size - pos;
1537
0
    int rem_chunk_length = (addon->chunk_offsets[addon->br_index + 1] -
1538
0
        addon->chunk_offsets[addon->br_index]) - addon->br_offset;
1539
0
    int length = addon->br_length - addon->br_copied;
1540
0
    if (length > rem_chunk_length) length = rem_chunk_length;
1541
0
    if (length > space) length = space;
1542
0
    memcpy(copy_dst, copy_src, (size_t)length);
1543
0
    pos += length;
1544
0
    addon->br_offset += length;
1545
0
    addon->br_copied += length;
1546
0
    if (length == rem_chunk_length) {
1547
0
      addon->br_index++;
1548
0
      addon->br_offset = 0;
1549
0
    }
1550
0
    if (pos == s->ringbuffer_size) break;
1551
0
  }
1552
0
  return pos - orig_pos;
1553
0
}
1554
1555
BROTLI_BOOL BrotliDecoderAttachDictionary(
1556
    BrotliDecoderState* state, BrotliSharedDictionaryType type,
1557
0
    size_t data_size, const uint8_t data[BROTLI_ARRAY_PARAM(data_size)]) {
1558
0
  brotli_reg_t i;
1559
0
  brotli_reg_t num_prefix_before = state->dictionary->num_prefix;
1560
0
  if (state->state != BROTLI_STATE_UNINITED) return BROTLI_FALSE;
1561
0
  if (!BrotliSharedDictionaryAttach(state->dictionary, type, data_size, data)) {
1562
0
    return BROTLI_FALSE;
1563
0
  }
1564
0
  for (i = num_prefix_before; i < state->dictionary->num_prefix; i++) {
1565
0
    if (!AttachCompoundDictionary(
1566
0
        state, state->dictionary->prefix[i],
1567
0
        state->dictionary->prefix_size[i])) {
1568
0
      return BROTLI_FALSE;
1569
0
    }
1570
0
  }
1571
0
  return BROTLI_TRUE;
1572
0
}
1573
1574
/* Calculates the smallest feasible ring buffer.
1575
1576
   If we know the data size is small, do not allocate more ring buffer
1577
   size than needed to reduce memory usage.
1578
1579
   When this method is called, metablock size and flags MUST be decoded. */
1580
static void BROTLI_NOINLINE BrotliCalculateRingBufferSize(
1581
7.42k
    BrotliDecoderState* s) {
1582
7.42k
  int window_size = 1 << s->window_bits;
1583
7.42k
  int new_ringbuffer_size = window_size;
1584
  /* We need at least 2 bytes of ring buffer size to get the last two
1585
     bytes for context from there */
1586
7.42k
  int min_size = s->ringbuffer_size ? s->ringbuffer_size : 1024;
1587
7.42k
  int output_size;
1588
1589
  /* If maximum is already reached, no further extension is retired. */
1590
7.42k
  if (s->ringbuffer_size == window_size) {
1591
57
    return;
1592
57
  }
1593
1594
  /* Metadata blocks does not touch ring buffer. */
1595
7.36k
  if (s->is_metadata) {
1596
0
    return;
1597
0
  }
1598
1599
7.36k
  if (!s->ringbuffer) {
1600
1.53k
    output_size = 0;
1601
5.83k
  } else {
1602
5.83k
    output_size = s->pos;
1603
5.83k
  }
1604
7.36k
  output_size += s->meta_block_remaining_len;
1605
7.36k
  min_size = min_size < output_size ? output_size : min_size;
1606
1607
7.36k
  if (!!s->canny_ringbuffer_allocation) {
1608
    /* Reduce ring buffer size to save memory when server is unscrupulous.
1609
       In worst case memory usage might be 1.5x bigger for a short period of
1610
       ring buffer reallocation. */
1611
42.9k
    while ((new_ringbuffer_size >> 1) >= min_size) {
1612
35.5k
      new_ringbuffer_size >>= 1;
1613
35.5k
    }
1614
7.36k
  }
1615
1616
7.36k
  s->new_ringbuffer_size = new_ringbuffer_size;
1617
7.36k
}
1618
1619
/* Reads 1..256 2-bit context modes. */
1620
4.50k
static BrotliDecoderErrorCode ReadContextModes(BrotliDecoderState* s) {
1621
4.50k
  BrotliBitReader* br = &s->br;
1622
4.50k
  int i = s->loop_counter;
1623
1624
20.7k
  while (i < (int)s->num_block_types[0]) {
1625
16.2k
    brotli_reg_t bits;
1626
16.2k
    if (!BrotliSafeReadBits(br, 2, &bits)) {
1627
5
      s->loop_counter = i;
1628
5
      return BROTLI_DECODER_NEEDS_MORE_INPUT;
1629
5
    }
1630
16.2k
    s->context_modes[i] = (uint8_t)bits;
1631
16.2k
    BROTLI_LOG_ARRAY_INDEX(s->context_modes, i);
1632
16.2k
    i++;
1633
16.2k
  }
1634
4.50k
  return BROTLI_DECODER_SUCCESS;
1635
4.50k
}
1636
1637
8.46M
static BROTLI_INLINE void TakeDistanceFromRingBuffer(BrotliDecoderState* s) {
1638
8.46M
  int offset = s->distance_code - 3;
1639
8.46M
  if (s->distance_code <= 3) {
1640
    /* Compensate double distance-ring-buffer roll for dictionary items. */
1641
8.40M
    s->distance_context = 1 >> s->distance_code;
1642
8.40M
    s->distance_code = s->dist_rb[(s->dist_rb_idx - offset) & 3];
1643
8.40M
    s->dist_rb_idx -= s->distance_context;
1644
8.40M
  } else {
1645
67.3k
    int index_delta = 3;
1646
67.3k
    int delta;
1647
67.3k
    int base = s->distance_code - 10;
1648
67.3k
    if (s->distance_code < 10) {
1649
27.8k
      base = s->distance_code - 4;
1650
39.5k
    } else {
1651
39.5k
      index_delta = 2;
1652
39.5k
    }
1653
    /* Unpack one of six 4-bit values. */
1654
67.3k
    delta = ((0x605142 >> (4 * base)) & 0xF) - 3;
1655
67.3k
    s->distance_code = s->dist_rb[(s->dist_rb_idx + index_delta) & 0x3] + delta;
1656
67.3k
    if (s->distance_code <= 0) {
1657
      /* A huge distance will cause a BROTLI_FAILURE() soon.
1658
         This is a little faster than failing here. */
1659
23
      s->distance_code = 0x7FFFFFFF;
1660
23
    }
1661
67.3k
  }
1662
8.46M
}
1663
1664
static BROTLI_INLINE BROTLI_BOOL SafeReadBits(
1665
16.3M
    BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) {
1666
16.3M
  if (n_bits != 0) {
1667
5.26k
    return BrotliSafeReadBits(br, n_bits, val);
1668
16.3M
  } else {
1669
16.3M
    *val = 0;
1670
16.3M
    return BROTLI_TRUE;
1671
16.3M
  }
1672
16.3M
}
1673
1674
static BROTLI_INLINE BROTLI_BOOL SafeReadBits32(
1675
16.4k
    BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) {
1676
16.4k
  if (n_bits != 0) {
1677
2.04k
    return BrotliSafeReadBits32(br, n_bits, val);
1678
14.4k
  } else {
1679
14.4k
    *val = 0;
1680
14.4k
    return BROTLI_TRUE;
1681
14.4k
  }
1682
16.4k
}
1683
1684
/*
1685
   RFC 7932 Section 4 with "..." shortenings and "[]" emendations.
1686
1687
   Each distance ... is represented with a pair <distance code, extra bits>...
1688
   The distance code is encoded using a prefix code... The number of extra bits
1689
   can be 0..24... Two additional parameters: NPOSTFIX (0..3), and ...
1690
   NDIRECT (0..120) ... are encoded in the meta-block header...
1691
1692
   The first 16 distance symbols ... reference past distances... ring buffer ...
1693
   Next NDIRECT distance symbols ... represent distances from 1 to NDIRECT...
1694
   [For] distance symbols 16 + NDIRECT and greater ... the number of extra bits
1695
   ... is given by the following formula:
1696
1697
   [ xcode = dcode - NDIRECT - 16 ]
1698
   ndistbits = 1 + [ xcode ] >> (NPOSTFIX + 1)
1699
1700
   ...
1701
*/
1702
1703
/*
1704
   RFC 7932 Section 9.2 with "..." shortenings and "[]" emendations.
1705
1706
   ... to get the actual value of the parameter NDIRECT, left-shift this
1707
   four-bit number by NPOSTFIX bits ...
1708
*/
1709
1710
/* Remaining formulas from RFC 7932 Section 4 could be rewritten as following:
1711
1712
     alphabet_size = 16 + NDIRECT + (max_distbits << (NPOSTFIX + 1))
1713
1714
     half = ((xcode >> NPOSTFIX) & 1) << ndistbits
1715
     postfix = xcode & ((1 << NPOSTFIX) - 1)
1716
     range_start = 2 * (1 << ndistbits - 1 - 1)
1717
1718
     distance = (range_start + half + extra) << NPOSTFIX + postfix + NDIRECT + 1
1719
1720
   NB: ndistbits >= 1 -> range_start >= 0
1721
   NB: range_start has factor 2, as the range is covered by 2 "halves"
1722
   NB: extra -1 offset in range_start formula covers the absence of
1723
       ndistbits = 0 case
1724
   NB: when NPOSTFIX = 0, NDIRECT is not greater than 15
1725
1726
   In other words, xcode has the following binary structure - XXXHPPP:
1727
    - XXX represent the number of extra distance bits
1728
    - H selects upper / lower range of distances
1729
    - PPP represent "postfix"
1730
1731
  "Regular" distance encoding has NPOSTFIX = 0; omitting the postfix part
1732
  simplifies distance calculation.
1733
1734
  Using NPOSTFIX > 0 allows cheaper encoding of regular structures, e.g. where
1735
  most of distances have the same reminder of division by 2/4/8. For example,
1736
  the table of int32_t values that come from different sources; if it is likely
1737
  that 3 highest bytes of values from the same source are the same, then
1738
  copy distance often looks like 4x + y.
1739
1740
  Distance calculation could be rewritten to:
1741
1742
    ndistbits = NDISTBITS(NDIRECT, NPOSTFIX)[dcode]
1743
    distance = OFFSET(NDIRECT, NPOSTFIX)[dcode] + extra << NPOSTFIX
1744
1745
  NDISTBITS and OFFSET could be pre-calculated, as NDIRECT and NPOSTFIX could
1746
  change only once per meta-block.
1747
*/
1748
1749
/* Calculates distance lookup table.
1750
   NB: it is possible to have all 64 tables precalculated. */
1751
4.26k
static void CalculateDistanceLut(BrotliDecoderState* s) {
1752
4.26k
  BrotliMetablockBodyArena* b = &s->arena.body;
1753
4.26k
  brotli_reg_t npostfix = s->distance_postfix_bits;
1754
4.26k
  brotli_reg_t ndirect = s->num_direct_distance_codes;
1755
4.26k
  brotli_reg_t alphabet_size_limit = s->distance_hgroup.alphabet_size_limit;
1756
4.26k
  brotli_reg_t postfix = 1u << npostfix;
1757
4.26k
  brotli_reg_t j;
1758
4.26k
  brotli_reg_t bits = 1;
1759
4.26k
  brotli_reg_t half = 0;
1760
1761
  /* Skip short codes. */
1762
4.26k
  brotli_reg_t i = BROTLI_NUM_DISTANCE_SHORT_CODES;
1763
1764
  /* Fill direct codes. */
1765
26.3k
  for (j = 0; j < ndirect; ++j) {
1766
22.1k
    b->dist_extra_bits[i] = 0;
1767
22.1k
    b->dist_offset[i] = j + 1;
1768
22.1k
    ++i;
1769
22.1k
  }
1770
1771
  /* Fill regular distance codes. */
1772
208k
  while (i < alphabet_size_limit) {
1773
204k
    brotli_reg_t base = ndirect + ((((2 + half) << bits) - 4) << npostfix) + 1;
1774
    /* Always fill the complete group. */
1775
497k
    for (j = 0; j < postfix; ++j) {
1776
293k
      b->dist_extra_bits[i] = (uint8_t)bits;
1777
293k
      b->dist_offset[i] = base + j;
1778
293k
      ++i;
1779
293k
    }
1780
204k
    bits = bits + half;
1781
204k
    half = half ^ 1;
1782
204k
  }
1783
4.26k
}
1784
1785
/* Precondition: s->distance_code < 0. */
1786
static BROTLI_INLINE BROTLI_BOOL ReadDistanceInternal(
1787
9.16M
    int safe, BrotliDecoderState* s, BrotliBitReader* br) {
1788
9.16M
  BrotliMetablockBodyArena* b = &s->arena.body;
1789
9.16M
  brotli_reg_t code;
1790
9.16M
  brotli_reg_t bits;
1791
9.16M
  BrotliBitReaderState memento;
1792
9.16M
  HuffmanCode* distance_tree = s->distance_hgroup.htrees[s->dist_htree_index];
1793
9.16M
  if (!safe) {
1794
1.19M
    code = ReadSymbol(distance_tree, br);
1795
7.96M
  } else {
1796
7.96M
    BrotliBitReaderSaveState(br, &memento);
1797
7.96M
    if (!SafeReadSymbol(distance_tree, br, &code)) {
1798
21
      return BROTLI_FALSE;
1799
21
    }
1800
7.96M
  }
1801
9.16M
  --s->block_length[2];
1802
  /* Convert the distance code to the actual distance by possibly
1803
     looking up past distances from the s->dist_rb. */
1804
9.16M
  s->distance_context = 0;
1805
9.16M
  if ((code & ~0xFu) == 0) {
1806
8.46M
    s->distance_code = (int)code;
1807
8.46M
    TakeDistanceFromRingBuffer(s);
1808
8.46M
    return BROTLI_TRUE;
1809
8.46M
  }
1810
693k
  if (!safe) {
1811
677k
    bits = BrotliReadBits32(br, b->dist_extra_bits[code]);
1812
677k
  } else {
1813
16.4k
    if (!SafeReadBits32(br, b->dist_extra_bits[code], &bits)) {
1814
50
      ++s->block_length[2];
1815
50
      BrotliBitReaderRestoreState(br, &memento);
1816
50
      return BROTLI_FALSE;
1817
50
    }
1818
16.4k
  }
1819
693k
  s->distance_code =
1820
693k
      (int)(b->dist_offset[code] + (bits << s->distance_postfix_bits));
1821
693k
  return BROTLI_TRUE;
1822
693k
}
1823
1824
static BROTLI_INLINE void ReadDistance(
1825
1.19M
    BrotliDecoderState* s, BrotliBitReader* br) {
1826
1.19M
  ReadDistanceInternal(0, s, br);
1827
1.19M
}
1828
1829
static BROTLI_INLINE BROTLI_BOOL SafeReadDistance(
1830
7.96M
    BrotliDecoderState* s, BrotliBitReader* br) {
1831
7.96M
  return ReadDistanceInternal(1, s, br);
1832
7.96M
}
1833
1834
static BROTLI_INLINE BROTLI_BOOL ReadCommandInternal(
1835
12.8M
    int safe, BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) {
1836
12.8M
  brotli_reg_t cmd_code;
1837
12.8M
  brotli_reg_t insert_len_extra = 0;
1838
12.8M
  brotli_reg_t copy_length;
1839
12.8M
  CmdLutElement v;
1840
12.8M
  BrotliBitReaderState memento;
1841
12.8M
  if (!safe) {
1842
4.69M
    cmd_code = ReadSymbol(s->htree_command, br);
1843
8.16M
  } else {
1844
8.16M
    BrotliBitReaderSaveState(br, &memento);
1845
8.16M
    if (!SafeReadSymbol(s->htree_command, br, &cmd_code)) {
1846
27
      return BROTLI_FALSE;
1847
27
    }
1848
8.16M
  }
1849
12.8M
  v = kCmdLut[cmd_code];
1850
12.8M
  s->distance_code = v.distance_code;
1851
12.8M
  s->distance_context = v.context;
1852
12.8M
  s->dist_htree_index = s->dist_context_map_slice[s->distance_context];
1853
12.8M
  *insert_length = v.insert_len_offset;
1854
12.8M
  if (!safe) {
1855
4.69M
    if (BROTLI_PREDICT_FALSE(v.insert_len_extra_bits != 0)) {
1856
58.2k
      insert_len_extra = BrotliReadBits24(br, v.insert_len_extra_bits);
1857
58.2k
    }
1858
4.69M
    copy_length = BrotliReadBits24(br, v.copy_len_extra_bits);
1859
8.16M
  } else {
1860
8.16M
    if (!SafeReadBits(br, v.insert_len_extra_bits, &insert_len_extra) ||
1861
8.16M
        !SafeReadBits(br, v.copy_len_extra_bits, &copy_length)) {
1862
67
      BrotliBitReaderRestoreState(br, &memento);
1863
67
      return BROTLI_FALSE;
1864
67
    }
1865
8.16M
  }
1866
12.8M
  s->copy_length = (int)copy_length + v.copy_len_offset;
1867
12.8M
  --s->block_length[1];
1868
12.8M
  *insert_length += (int)insert_len_extra;
1869
12.8M
  return BROTLI_TRUE;
1870
12.8M
}
1871
1872
static BROTLI_INLINE void ReadCommand(
1873
4.69M
    BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) {
1874
4.69M
  ReadCommandInternal(0, s, br, insert_length);
1875
4.69M
}
1876
1877
static BROTLI_INLINE BROTLI_BOOL SafeReadCommand(
1878
8.16M
    BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) {
1879
8.16M
  return ReadCommandInternal(1, s, br, insert_length);
1880
8.16M
}
1881
1882
static BROTLI_INLINE BROTLI_BOOL CheckInputAmount(
1883
204M
    int safe, BrotliBitReader* const br) {
1884
204M
  if (safe) {
1885
159M
    return BROTLI_TRUE;
1886
159M
  }
1887
44.9M
  return BrotliCheckInputAmount(br);
1888
204M
}
1889
1890
#define BROTLI_SAFE(METHOD)                       \
1891
22.2M
  {                                               \
1892
22.2M
    if (safe) {                                   \
1893
16.1M
      if (!Safe##METHOD) {                        \
1894
210
        result = BROTLI_DECODER_NEEDS_MORE_INPUT; \
1895
210
        goto saveStateAndReturn;                  \
1896
210
      }                                           \
1897
16.1M
    } else {                                      \
1898
6.14M
      METHOD;                                     \
1899
6.14M
    }                                             \
1900
22.2M
  }
1901
1902
static BROTLI_INLINE BrotliDecoderErrorCode ProcessCommandsInternal(
1903
8.14k
    int safe, BrotliDecoderState* s) {
1904
8.14k
  int pos = s->pos;
1905
8.14k
  int i = s->loop_counter;
1906
8.14k
  BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS;
1907
8.14k
  BrotliBitReader* br = &s->br;
1908
8.14k
  int compound_dictionary_size = GetCompoundDictionarySize(s);
1909
1910
8.14k
  if (!CheckInputAmount(safe, br)) {
1911
361
    result = BROTLI_DECODER_NEEDS_MORE_INPUT;
1912
361
    goto saveStateAndReturn;
1913
361
  }
1914
7.77k
  if (!safe) {
1915
6.77k
    BROTLI_UNUSED(BrotliWarmupBitReader(br));
1916
6.77k
  }
1917
1918
  /* Jump into state machine. */
1919
7.77k
  if (s->state == BROTLI_STATE_COMMAND_BEGIN) {
1920
4.68k
    goto CommandBegin;
1921
4.68k
  } else if (s->state == BROTLI_STATE_COMMAND_INNER) {
1922
918
    goto CommandInner;
1923
2.17k
  } else if (s->state == BROTLI_STATE_COMMAND_POST_DECODE_LITERALS) {
1924
276
    goto CommandPostDecodeLiterals;
1925
1.89k
  } else if (s->state == BROTLI_STATE_COMMAND_POST_WRAP_COPY) {
1926
1.89k
    goto CommandPostWrapCopy;
1927
1.89k
  } else {
1928
0
    return BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE);  /* COV_NF_LINE */
1929
0
  }
1930
1931
13.0M
CommandBegin:
1932
13.0M
  if (safe) {
1933
8.16M
    s->state = BROTLI_STATE_COMMAND_BEGIN;
1934
8.16M
  }
1935
13.0M
  if (!CheckInputAmount(safe, br)) {
1936
184
    s->state = BROTLI_STATE_COMMAND_BEGIN;
1937
184
    result = BROTLI_DECODER_NEEDS_MORE_INPUT;
1938
184
    goto saveStateAndReturn;
1939
184
  }
1940
13.0M
  if (BROTLI_PREDICT_FALSE(s->block_length[1] == 0)) {
1941
209k
    BROTLI_SAFE(DecodeCommandBlockSwitch(s));
1942
209k
    goto CommandBegin;
1943
209k
  }
1944
  /* Read the insert/copy length in the command. */
1945
12.8M
  BROTLI_SAFE(ReadCommand(s, br, &i));
1946
12.8M
  BROTLI_LOG(("[ProcessCommandsInternal] pos = %d insert = %d copy = %d\n",
1947
12.8M
              pos, i, s->copy_length));
1948
12.8M
  if (i == 0) {
1949
5.23M
    goto CommandPostDecodeLiterals;
1950
5.23M
  }
1951
7.62M
  s->meta_block_remaining_len -= i;
1952
1953
7.64M
CommandInner:
1954
7.64M
  if (safe) {
1955
5.73M
    s->state = BROTLI_STATE_COMMAND_INNER;
1956
5.73M
  }
1957
  /* Read the literals in the command. */
1958
7.64M
  if (s->trivial_literal_context) {
1959
6.07M
    brotli_reg_t bits;
1960
6.07M
    brotli_reg_t value;
1961
6.07M
    PreloadSymbol(safe, s->literal_htree, br, &bits, &value);
1962
184M
    do {
1963
184M
      if (!CheckInputAmount(safe, br)) {
1964
122
        s->state = BROTLI_STATE_COMMAND_INNER;
1965
122
        result = BROTLI_DECODER_NEEDS_MORE_INPUT;
1966
122
        goto saveStateAndReturn;
1967
122
      }
1968
184M
      if (BROTLI_PREDICT_FALSE(s->block_length[0] == 0)) {
1969
4.38k
        goto NextLiteralBlock;
1970
4.38k
      }
1971
184M
      if (!safe) {
1972
33.7M
        s->ringbuffer[pos] =
1973
33.7M
            (uint8_t)ReadPreloadedSymbol(s->literal_htree, br, &bits, &value);
1974
151M
      } else {
1975
151M
        brotli_reg_t literal;
1976
151M
        if (!SafeReadSymbol(s->literal_htree, br, &literal)) {
1977
30
          result = BROTLI_DECODER_NEEDS_MORE_INPUT;
1978
30
          goto saveStateAndReturn;
1979
30
        }
1980
151M
        s->ringbuffer[pos] = (uint8_t)literal;
1981
151M
      }
1982
184M
      --s->block_length[0];
1983
184M
      BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos);
1984
184M
      ++pos;
1985
184M
      if (BROTLI_PREDICT_FALSE(pos == s->ringbuffer_size)) {
1986
663
        s->state = BROTLI_STATE_COMMAND_INNER_WRITE;
1987
663
        --i;
1988
663
        goto saveStateAndReturn;
1989
663
      }
1990
184M
    } while (--i != 0);
1991
6.07M
  } else {
1992
1.57M
    uint8_t p1 = s->ringbuffer[(pos - 1) & s->ringbuffer_mask];
1993
1.57M
    uint8_t p2 = s->ringbuffer[(pos - 2) & s->ringbuffer_mask];
1994
6.36M
    do {
1995
6.36M
      const HuffmanCode* hc;
1996
6.36M
      uint8_t context;
1997
6.36M
      if (!CheckInputAmount(safe, br)) {
1998
339
        s->state = BROTLI_STATE_COMMAND_INNER;
1999
339
        result = BROTLI_DECODER_NEEDS_MORE_INPUT;
2000
339
        goto saveStateAndReturn;
2001
339
      }
2002
6.36M
      if (BROTLI_PREDICT_FALSE(s->block_length[0] == 0)) {
2003
13.7k
        goto NextLiteralBlock;
2004
13.7k
      }
2005
6.35M
      context = BROTLI_CONTEXT(p1, p2, s->context_lookup);
2006
6.35M
      BROTLI_LOG_UINT(context);
2007
6.35M
      hc = s->literal_hgroup.htrees[s->context_map_slice[context]];
2008
6.35M
      p2 = p1;
2009
6.35M
      if (!safe) {
2010
6.26M
        p1 = (uint8_t)ReadSymbol(hc, br);
2011
6.26M
      } else {
2012
86.2k
        brotli_reg_t literal;
2013
86.2k
        if (!SafeReadSymbol(hc, br, &literal)) {
2014
30
          result = BROTLI_DECODER_NEEDS_MORE_INPUT;
2015
30
          goto saveStateAndReturn;
2016
30
        }
2017
86.2k
        p1 = (uint8_t)literal;
2018
86.2k
      }
2019
6.35M
      s->ringbuffer[pos] = p1;
2020
6.35M
      --s->block_length[0];
2021
6.35M
      BROTLI_LOG_UINT(s->context_map_slice[context]);
2022
6.35M
      BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos & s->ringbuffer_mask);
2023
6.35M
      ++pos;
2024
6.35M
      if (BROTLI_PREDICT_FALSE(pos == s->ringbuffer_size)) {
2025
91
        s->state = BROTLI_STATE_COMMAND_INNER_WRITE;
2026
91
        --i;
2027
91
        goto saveStateAndReturn;
2028
91
      }
2029
6.35M
    } while (--i != 0);
2030
1.57M
  }
2031
7.62M
  BROTLI_LOG_UINT(s->meta_block_remaining_len);
2032
7.62M
  if (BROTLI_PREDICT_FALSE(s->meta_block_remaining_len <= 0)) {
2033
1.68k
    s->state = BROTLI_STATE_METABLOCK_DONE;
2034
1.68k
    goto saveStateAndReturn;
2035
1.68k
  }
2036
2037
12.8M
CommandPostDecodeLiterals:
2038
12.8M
  if (safe) {
2039
8.16M
    s->state = BROTLI_STATE_COMMAND_POST_DECODE_LITERALS;
2040
8.16M
  }
2041
12.8M
  if (s->distance_code >= 0) {
2042
    /* Implicit distance case. */
2043
3.69M
    s->distance_context = s->distance_code ? 0 : 1;
2044
3.69M
    --s->dist_rb_idx;
2045
3.69M
    s->distance_code = s->dist_rb[s->dist_rb_idx & 3];
2046
9.16M
  } else {
2047
    /* Read distance code in the command, unless it was implicitly zero. */
2048
9.16M
    if (BROTLI_PREDICT_FALSE(s->block_length[2] == 0)) {
2049
32.5k
      BROTLI_SAFE(DecodeDistanceBlockSwitch(s));
2050
32.5k
    }
2051
9.16M
    BROTLI_SAFE(ReadDistance(s, br));
2052
9.16M
  }
2053
12.8M
  BROTLI_LOG(("[ProcessCommandsInternal] pos = %d distance = %d\n",
2054
12.8M
              pos, s->distance_code));
2055
12.8M
  if (s->max_distance != s->max_backward_distance) {
2056
12.5M
    s->max_distance =
2057
12.5M
        (pos < s->max_backward_distance) ? pos : s->max_backward_distance;
2058
12.5M
  }
2059
12.8M
  i = s->copy_length;
2060
  /* Apply copy of LZ77 back-reference, or static dictionary reference if
2061
     the distance is larger than the max LZ77 distance */
2062
12.8M
  if (s->distance_code > s->max_distance) {
2063
    /* The maximum allowed distance is BROTLI_MAX_ALLOWED_DISTANCE = 0x7FFFFFFC.
2064
       With this choice, no signed overflow can occur after decoding
2065
       a special distance code (e.g., after adding 3 to the last distance). */
2066
37.6k
    if (s->distance_code > BROTLI_MAX_ALLOWED_DISTANCE) {
2067
23
      BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
2068
23
          "len: %d bytes left: %d\n",
2069
23
          pos, s->distance_code, i, s->meta_block_remaining_len));
2070
23
      return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_DISTANCE);
2071
23
    }
2072
37.6k
    if (s->distance_code - s->max_distance - 1 < compound_dictionary_size) {
2073
0
      int address = compound_dictionary_size -
2074
0
          (s->distance_code - s->max_distance);
2075
0
      if (!InitializeCompoundDictionaryCopy(s, address, i)) {
2076
0
        return BROTLI_FAILURE(BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY);
2077
0
      }
2078
0
      pos += CopyFromCompoundDictionary(s, pos);
2079
0
      if (pos >= s->ringbuffer_size) {
2080
0
        s->state = BROTLI_STATE_COMMAND_POST_WRITE_1;
2081
0
        goto saveStateAndReturn;
2082
0
      }
2083
37.6k
    } else if (i >= SHARED_BROTLI_MIN_DICTIONARY_WORD_LENGTH &&
2084
37.6k
               i <= SHARED_BROTLI_MAX_DICTIONARY_WORD_LENGTH) {
2085
37.5k
      uint8_t p1 = s->ringbuffer[(pos - 1) & s->ringbuffer_mask];
2086
37.5k
      uint8_t p2 = s->ringbuffer[(pos - 2) & s->ringbuffer_mask];
2087
37.5k
      uint8_t dict_id = s->dictionary->context_based ?
2088
0
          s->dictionary->context_map[BROTLI_CONTEXT(p1, p2, s->context_lookup)]
2089
37.5k
          : 0;
2090
37.5k
      const BrotliDictionary* words = s->dictionary->words[dict_id];
2091
37.5k
      const BrotliTransforms* transforms = s->dictionary->transforms[dict_id];
2092
37.5k
      int offset = (int)words->offsets_by_length[i];
2093
37.5k
      brotli_reg_t shift = words->size_bits_by_length[i];
2094
37.5k
      int address =
2095
37.5k
          s->distance_code - s->max_distance - 1 - compound_dictionary_size;
2096
37.5k
      int mask = (int)BitMask(shift);
2097
37.5k
      int word_idx = address & mask;
2098
37.5k
      int transform_idx = address >> shift;
2099
      /* Compensate double distance-ring-buffer roll. */
2100
37.5k
      s->dist_rb_idx += s->distance_context;
2101
37.5k
      offset += word_idx * i;
2102
      /* If the distance is out of bound, select a next static dictionary if
2103
         there exist multiple. */
2104
37.5k
      if ((transform_idx >= (int)transforms->num_transforms ||
2105
37.5k
          words->size_bits_by_length[i] == 0) &&
2106
37.5k
          s->dictionary->num_dictionaries > 1) {
2107
0
        uint8_t dict_id2;
2108
0
        int dist_remaining = address -
2109
0
            (int)(((1u << shift) & ~1u)) * (int)transforms->num_transforms;
2110
0
        for (dict_id2 = 0; dict_id2 < s->dictionary->num_dictionaries;
2111
0
            dict_id2++) {
2112
0
          const BrotliDictionary* words2 = s->dictionary->words[dict_id2];
2113
0
          if (dict_id2 != dict_id && words2->size_bits_by_length[i] != 0) {
2114
0
            const BrotliTransforms* transforms2 =
2115
0
                s->dictionary->transforms[dict_id2];
2116
0
            brotli_reg_t shift2 = words2->size_bits_by_length[i];
2117
0
            int num = (int)((1u << shift2) & ~1u) *
2118
0
                (int)transforms2->num_transforms;
2119
0
            if (dist_remaining < num) {
2120
0
              dict_id = dict_id2;
2121
0
              words = words2;
2122
0
              transforms = transforms2;
2123
0
              address = dist_remaining;
2124
0
              shift = shift2;
2125
0
              mask = (int)BitMask(shift);
2126
0
              word_idx = address & mask;
2127
0
              transform_idx = address >> shift;
2128
0
              offset = (int)words->offsets_by_length[i] + word_idx * i;
2129
0
              break;
2130
0
            }
2131
0
            dist_remaining -= num;
2132
0
          }
2133
0
        }
2134
0
      }
2135
37.5k
      if (BROTLI_PREDICT_FALSE(words->size_bits_by_length[i] == 0)) {
2136
11
        BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
2137
11
            "len: %d bytes left: %d\n",
2138
11
            pos, s->distance_code, i, s->meta_block_remaining_len));
2139
11
        return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_DICTIONARY);
2140
11
      }
2141
37.5k
      if (BROTLI_PREDICT_FALSE(!words->data)) {
2142
0
        return BROTLI_FAILURE(BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET);
2143
0
      }
2144
37.5k
      if (transform_idx < (int)transforms->num_transforms) {
2145
37.4k
        const uint8_t* word = &words->data[offset];
2146
37.4k
        int len = i;
2147
37.4k
        if (transform_idx == transforms->cutOffTransforms[0]) {
2148
12.0k
          memcpy(&s->ringbuffer[pos], word, (size_t)len);
2149
12.0k
          BROTLI_LOG(("[ProcessCommandsInternal] dictionary word: [%.*s]\n",
2150
12.0k
                      len, word));
2151
25.4k
        } else {
2152
25.4k
          len = BrotliTransformDictionaryWord(&s->ringbuffer[pos], word, len,
2153
25.4k
              transforms, transform_idx);
2154
25.4k
          BROTLI_LOG(("[ProcessCommandsInternal] dictionary word: [%.*s],"
2155
25.4k
                      " transform_idx = %d, transformed: [%.*s]\n",
2156
25.4k
                      i, word, transform_idx, len, &s->ringbuffer[pos]));
2157
25.4k
          if (len == 0 && s->distance_code <= 120) {
2158
0
            BROTLI_LOG(("Invalid length-0 dictionary word after transform\n"));
2159
0
            return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_TRANSFORM);
2160
0
          }
2161
25.4k
        }
2162
37.4k
        pos += len;
2163
37.4k
        s->meta_block_remaining_len -= len;
2164
37.4k
        if (pos >= s->ringbuffer_size) {
2165
247
          s->state = BROTLI_STATE_COMMAND_POST_WRITE_1;
2166
247
          goto saveStateAndReturn;
2167
247
        }
2168
37.4k
      } else {
2169
47
        BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
2170
47
            "len: %d bytes left: %d\n",
2171
47
            pos, s->distance_code, i, s->meta_block_remaining_len));
2172
47
        return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_TRANSFORM);
2173
47
      }
2174
37.5k
    } else {
2175
45
      BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
2176
45
          "len: %d bytes left: %d\n",
2177
45
          pos, s->distance_code, i, s->meta_block_remaining_len));
2178
45
      return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_DICTIONARY);
2179
45
    }
2180
12.8M
  } else {
2181
12.8M
    int src_start = (pos - s->distance_code) & s->ringbuffer_mask;
2182
12.8M
    uint8_t* copy_dst = &s->ringbuffer[pos];
2183
12.8M
    uint8_t* copy_src = &s->ringbuffer[src_start];
2184
12.8M
    int dst_end = pos + i;
2185
12.8M
    int src_end = src_start + i;
2186
    /* Update the recent distances cache. */
2187
12.8M
    s->dist_rb[s->dist_rb_idx & 3] = s->distance_code;
2188
12.8M
    ++s->dist_rb_idx;
2189
12.8M
    s->meta_block_remaining_len -= i;
2190
    /* There are 32+ bytes of slack in the ring-buffer allocation.
2191
       Also, we have 16 short codes, that make these 16 bytes irrelevant
2192
       in the ring-buffer. Let's copy over them as a first guess. */
2193
12.8M
    memmove16(copy_dst, copy_src);
2194
12.8M
    if (src_end > pos && dst_end > src_start) {
2195
      /* Regions intersect. */
2196
3.82M
      goto CommandPostWrapCopy;
2197
3.82M
    }
2198
8.99M
    if (dst_end >= s->ringbuffer_size || src_end >= s->ringbuffer_size) {
2199
      /* At least one region wraps. */
2200
1.10k
      goto CommandPostWrapCopy;
2201
1.10k
    }
2202
8.99M
    pos += i;
2203
8.99M
    if (i > 16) {
2204
47.6k
      if (i > 32) {
2205
31.5k
        memcpy(copy_dst + 16, copy_src + 16, (size_t)(i - 16));
2206
31.5k
      } else {
2207
        /* This branch covers about 45% cases.
2208
           Fixed size short copy allows more compiler optimizations. */
2209
16.0k
        memmove16(copy_dst + 16, copy_src + 16);
2210
16.0k
      }
2211
47.6k
    }
2212
8.99M
  }
2213
9.03M
  BROTLI_LOG_UINT(s->meta_block_remaining_len);
2214
9.03M
  if (s->meta_block_remaining_len <= 0) {
2215
    /* Next metablock, if any. */
2216
1.72k
    s->state = BROTLI_STATE_METABLOCK_DONE;
2217
1.72k
    goto saveStateAndReturn;
2218
9.03M
  } else {
2219
9.03M
    goto CommandBegin;
2220
9.03M
  }
2221
3.82M
CommandPostWrapCopy:
2222
3.82M
  {
2223
3.82M
    int wrap_guard = s->ringbuffer_size - pos;
2224
319M
    while (--i >= 0) {
2225
315M
      s->ringbuffer[pos] =
2226
315M
          s->ringbuffer[(pos - s->distance_code) & s->ringbuffer_mask];
2227
315M
      ++pos;
2228
315M
      if (BROTLI_PREDICT_FALSE(--wrap_guard == 0)) {
2229
1.93k
        s->state = BROTLI_STATE_COMMAND_POST_WRITE_2;
2230
1.93k
        goto saveStateAndReturn;
2231
1.93k
      }
2232
315M
    }
2233
3.82M
  }
2234
3.82M
  if (s->meta_block_remaining_len <= 0) {
2235
    /* Next metablock, if any. */
2236
389
    s->state = BROTLI_STATE_METABLOCK_DONE;
2237
389
    goto saveStateAndReturn;
2238
3.82M
  } else {
2239
3.82M
    goto CommandBegin;
2240
3.82M
  }
2241
2242
18.0k
NextLiteralBlock:
2243
18.0k
  BROTLI_SAFE(DecodeLiteralBlockSwitch(s));
2244
18.0k
  goto CommandInner;
2245
2246
8.01k
saveStateAndReturn:
2247
8.01k
  s->pos = pos;
2248
8.01k
  s->loop_counter = i;
2249
8.01k
  return result;
2250
18.0k
}
2251
2252
#undef BROTLI_SAFE
2253
2254
static BROTLI_NOINLINE BrotliDecoderErrorCode ProcessCommands(
2255
7.13k
    BrotliDecoderState* s) {
2256
7.13k
  return ProcessCommandsInternal(0, s);
2257
7.13k
}
2258
2259
static BROTLI_NOINLINE BrotliDecoderErrorCode SafeProcessCommands(
2260
1.00k
    BrotliDecoderState* s) {
2261
1.00k
  return ProcessCommandsInternal(1, s);
2262
1.00k
}
2263
2264
BrotliDecoderResult BrotliDecoderDecompress(
2265
    size_t encoded_size,
2266
    const uint8_t encoded_buffer[BROTLI_ARRAY_PARAM(encoded_size)],
2267
    size_t* decoded_size,
2268
1.67k
    uint8_t decoded_buffer[BROTLI_ARRAY_PARAM(*decoded_size)]) {
2269
1.67k
  BrotliDecoderState s;
2270
1.67k
  BrotliDecoderResult result;
2271
1.67k
  size_t total_out = 0;
2272
1.67k
  size_t available_in = encoded_size;
2273
1.67k
  const uint8_t* next_in = encoded_buffer;
2274
1.67k
  size_t available_out = *decoded_size;
2275
1.67k
  uint8_t* next_out = decoded_buffer;
2276
1.67k
  if (!BrotliDecoderStateInit(&s, 0, 0, 0)) {
2277
0
    return BROTLI_DECODER_RESULT_ERROR;
2278
0
  }
2279
1.67k
  result = BrotliDecoderDecompressStream(
2280
1.67k
      &s, &available_in, &next_in, &available_out, &next_out, &total_out);
2281
1.67k
  *decoded_size = total_out;
2282
1.67k
  BrotliDecoderStateCleanup(&s);
2283
1.67k
  if (result != BROTLI_DECODER_RESULT_SUCCESS) {
2284
1.12k
    result = BROTLI_DECODER_RESULT_ERROR;
2285
1.12k
  }
2286
1.67k
  return result;
2287
1.67k
}
2288
2289
/* Invariant: input stream is never overconsumed:
2290
    - invalid input implies that the whole stream is invalid -> any amount of
2291
      input could be read and discarded
2292
    - when result is "needs more input", then at least one more byte is REQUIRED
2293
      to complete decoding; all input data MUST be consumed by decoder, so
2294
      client could swap the input buffer
2295
    - when result is "needs more output" decoder MUST ensure that it doesn't
2296
      hold more than 7 bits in bit reader; this saves client from swapping input
2297
      buffer ahead of time
2298
    - when result is "success" decoder MUST return all unused data back to input
2299
      buffer; this is possible because the invariant is held on enter */
2300
BrotliDecoderResult BrotliDecoderDecompressStream(
2301
    BrotliDecoderState* s, size_t* available_in, const uint8_t** next_in,
2302
1.67k
    size_t* available_out, uint8_t** next_out, size_t* total_out) {
2303
1.67k
  BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS;
2304
1.67k
  BrotliBitReader* br = &s->br;
2305
1.67k
  size_t input_size = *available_in;
2306
1.67k
#define BROTLI_SAVE_ERROR_CODE(code) \
2307
1.67k
    SaveErrorCode(s, (code), input_size - *available_in)
2308
  /* Ensure that |total_out| is set, even if no data will ever be pushed out. */
2309
1.67k
  if (total_out) {
2310
1.67k
    *total_out = s->partial_pos_out;
2311
1.67k
  }
2312
  /* Do not try to process further in a case of unrecoverable error. */
2313
1.67k
  if ((int)s->error_code < 0) {
2314
0
    return BROTLI_DECODER_RESULT_ERROR;
2315
0
  }
2316
1.67k
  if (*available_out && (!next_out || !*next_out)) {
2317
0
    return BROTLI_SAVE_ERROR_CODE(
2318
0
        BROTLI_FAILURE(BROTLI_DECODER_ERROR_INVALID_ARGUMENTS));
2319
0
  }
2320
1.67k
  if (!*available_out) next_out = 0;
2321
1.67k
  if (s->buffer_length == 0) {  /* Just connect bit reader to input stream. */
2322
1.67k
    BrotliBitReaderSetInput(br, *next_in, *available_in);
2323
1.67k
  } else {
2324
    /* At least one byte of input is required. More than one byte of input may
2325
       be required to complete the transaction -> reading more data must be
2326
       done in a loop -> do it in a main loop. */
2327
0
    result = BROTLI_DECODER_NEEDS_MORE_INPUT;
2328
0
    BrotliBitReaderSetInput(br, &s->buffer.u8[0], s->buffer_length);
2329
0
  }
2330
  /* State machine */
2331
165k
  for (;;) {
2332
165k
    if (result != BROTLI_DECODER_SUCCESS) {
2333
      /* Error, needs more input/output. */
2334
1.12k
      if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) {
2335
575
        if (s->ringbuffer != 0) {  /* Pro-actively push output. */
2336
348
          BrotliDecoderErrorCode intermediate_result = WriteRingBuffer(s,
2337
348
              available_out, next_out, total_out, BROTLI_TRUE);
2338
          /* WriteRingBuffer checks s->meta_block_remaining_len validity. */
2339
348
          if ((int)intermediate_result < 0) {
2340
21
            result = intermediate_result;
2341
21
            break;
2342
21
          }
2343
348
        }
2344
554
        if (s->buffer_length != 0) {  /* Used with internal buffer. */
2345
0
          if (br->next_in == br->last_in) {
2346
            /* Successfully finished read transaction.
2347
               Accumulator contains less than 8 bits, because internal buffer
2348
               is expanded byte-by-byte until it is enough to complete read. */
2349
0
            s->buffer_length = 0;
2350
            /* Switch to input stream and restart. */
2351
0
            result = BROTLI_DECODER_SUCCESS;
2352
0
            BrotliBitReaderSetInput(br, *next_in, *available_in);
2353
0
            continue;
2354
0
          } else if (*available_in != 0) {
2355
            /* Not enough data in buffer, but can take one more byte from
2356
               input stream. */
2357
0
            result = BROTLI_DECODER_SUCCESS;
2358
0
            BROTLI_DCHECK(s->buffer_length < 8);
2359
0
            s->buffer.u8[s->buffer_length] = **next_in;
2360
0
            s->buffer_length++;
2361
0
            BrotliBitReaderSetInput(br, &s->buffer.u8[0], s->buffer_length);
2362
0
            (*next_in)++;
2363
0
            (*available_in)--;
2364
            /* Retry with more data in buffer. */
2365
0
            continue;
2366
0
          }
2367
          /* Can't finish reading and no more input. */
2368
0
          break;
2369
554
        } else {  /* Input stream doesn't contain enough input. */
2370
          /* Copy tail to internal buffer and return. */
2371
554
          *next_in = br->next_in;
2372
554
          *available_in = BrotliBitReaderGetAvailIn(br);
2373
571
          while (*available_in) {
2374
17
            s->buffer.u8[s->buffer_length] = **next_in;
2375
17
            s->buffer_length++;
2376
17
            (*next_in)++;
2377
17
            (*available_in)--;
2378
17
          }
2379
554
          break;
2380
554
        }
2381
        /* Unreachable. */
2382
554
      }
2383
2384
      /* Fail or needs more output. */
2385
2386
549
      if (s->buffer_length != 0) {
2387
        /* Just consumed the buffered input and produced some output. Otherwise
2388
           it would result in "needs more input". Reset internal buffer. */
2389
0
        s->buffer_length = 0;
2390
549
      } else {
2391
        /* Using input stream in last iteration. When decoder switches to input
2392
           stream it has less than 8 bits in accumulator, so it is safe to
2393
           return unused accumulator bits there. */
2394
549
        BrotliBitReaderUnload(br);
2395
549
        *available_in = BrotliBitReaderGetAvailIn(br);
2396
549
        *next_in = br->next_in;
2397
549
      }
2398
549
      break;
2399
1.12k
    }
2400
163k
    switch (s->state) {
2401
1.67k
      case BROTLI_STATE_UNINITED:
2402
        /* Prepare to the first read. */
2403
1.67k
        if (!BrotliWarmupBitReader(br)) {
2404
51
          result = BROTLI_DECODER_NEEDS_MORE_INPUT;
2405
51
          break;
2406
51
        }
2407
        /* Decode window size. */
2408
1.62k
        result = DecodeWindowBits(s, br);  /* Reads 1..8 bits. */
2409
1.62k
        if (result != BROTLI_DECODER_SUCCESS) {
2410
1
          break;
2411
1
        }
2412
1.62k
        if (s->large_window) {
2413
0
          s->state = BROTLI_STATE_LARGE_WINDOW_BITS;
2414
0
          break;
2415
0
        }
2416
1.62k
        s->state = BROTLI_STATE_INITIALIZE;
2417
1.62k
        break;
2418
2419
0
      case BROTLI_STATE_LARGE_WINDOW_BITS: {
2420
0
        brotli_reg_t bits;
2421
0
        if (!BrotliSafeReadBits(br, 6, &bits)) {
2422
0
          result = BROTLI_DECODER_NEEDS_MORE_INPUT;
2423
0
          break;
2424
0
        }
2425
0
        s->window_bits = bits & 63u;
2426
0
        if (s->window_bits < BROTLI_LARGE_MIN_WBITS ||
2427
0
            s->window_bits > BROTLI_LARGE_MAX_WBITS) {
2428
0
          result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS);
2429
0
          break;
2430
0
        }
2431
0
        s->state = BROTLI_STATE_INITIALIZE;
2432
0
      }
2433
      /* Fall through. */
2434
2435
1.62k
      case BROTLI_STATE_INITIALIZE:
2436
1.62k
        BROTLI_LOG_UINT(s->window_bits);
2437
        /* Maximum distance, see section 9.1. of the spec. */
2438
1.62k
        s->max_backward_distance = (1 << s->window_bits) - BROTLI_WINDOW_GAP;
2439
2440
        /* Allocate memory for both block_type_trees and block_len_trees. */
2441
1.62k
        s->block_type_trees = (HuffmanCode*)BROTLI_DECODER_ALLOC(s,
2442
1.62k
            sizeof(HuffmanCode) * 3 *
2443
1.62k
                (BROTLI_HUFFMAN_MAX_SIZE_258 + BROTLI_HUFFMAN_MAX_SIZE_26));
2444
1.62k
        if (s->block_type_trees == 0) {
2445
0
          result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES);
2446
0
          break;
2447
0
        }
2448
1.62k
        s->block_len_trees =
2449
1.62k
            s->block_type_trees + 3 * BROTLI_HUFFMAN_MAX_SIZE_258;
2450
2451
1.62k
        s->state = BROTLI_STATE_METABLOCK_BEGIN;
2452
      /* Fall through. */
2453
2454
45.1k
      case BROTLI_STATE_METABLOCK_BEGIN:
2455
45.1k
        BrotliDecoderStateMetablockBegin(s);
2456
45.1k
        BROTLI_LOG_UINT(s->pos);
2457
45.1k
        s->state = BROTLI_STATE_METABLOCK_HEADER;
2458
      /* Fall through. */
2459
2460
45.1k
      case BROTLI_STATE_METABLOCK_HEADER:
2461
45.1k
        result = DecodeMetaBlockLength(s, br);  /* Reads 2 - 31 bits. */
2462
45.1k
        if (result != BROTLI_DECODER_SUCCESS) {
2463
60
          break;
2464
60
        }
2465
45.0k
        BROTLI_LOG_UINT(s->is_last_metablock);
2466
45.0k
        BROTLI_LOG_UINT(s->meta_block_remaining_len);
2467
45.0k
        BROTLI_LOG_UINT(s->is_metadata);
2468
45.0k
        BROTLI_LOG_UINT(s->is_uncompressed);
2469
45.0k
        if (s->is_metadata || s->is_uncompressed) {
2470
40.4k
          if (!BrotliJumpToByteBoundary(br)) {
2471
19
            result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_PADDING_1);
2472
19
            break;
2473
19
          }
2474
40.4k
        }
2475
45.0k
        if (s->is_metadata) {
2476
37.5k
          s->state = BROTLI_STATE_METADATA;
2477
37.5k
          if (s->metadata_start_func) {
2478
0
            s->metadata_start_func(s->metadata_callback_opaque,
2479
0
                                   (size_t)s->meta_block_remaining_len);
2480
0
          }
2481
37.5k
          break;
2482
37.5k
        }
2483
7.45k
        if (s->meta_block_remaining_len == 0) {
2484
35
          s->state = BROTLI_STATE_METABLOCK_DONE;
2485
35
          break;
2486
35
        }
2487
7.42k
        BrotliCalculateRingBufferSize(s);
2488
7.42k
        if (s->is_uncompressed) {
2489
2.83k
          s->state = BROTLI_STATE_UNCOMPRESSED;
2490
2.83k
          break;
2491
2.83k
        }
2492
4.58k
        s->state = BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_HEADER;
2493
      /* Fall through. */
2494
2495
4.58k
      case BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_HEADER: {
2496
4.58k
        BrotliMetablockHeaderArena* h = &s->arena.header;
2497
4.58k
        s->loop_counter = 0;
2498
        /* Initialize compressed metablock header arena. */
2499
4.58k
        h->sub_loop_counter = 0;
2500
        /* Make small negative indexes addressable. */
2501
4.58k
        h->symbol_lists =
2502
4.58k
            &h->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1];
2503
4.58k
        h->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
2504
4.58k
        h->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;
2505
4.58k
        h->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;
2506
4.58k
        s->state = BROTLI_STATE_HUFFMAN_CODE_0;
2507
4.58k
      }
2508
      /* Fall through. */
2509
2510
18.1k
      case BROTLI_STATE_HUFFMAN_CODE_0:
2511
18.1k
        if (s->loop_counter >= 3) {
2512
4.51k
          s->state = BROTLI_STATE_METABLOCK_HEADER_2;
2513
4.51k
          break;
2514
4.51k
        }
2515
        /* Reads 1..11 bits. */
2516
13.6k
        result = DecodeVarLenUint8(s, br, &s->num_block_types[s->loop_counter]);
2517
13.6k
        if (result != BROTLI_DECODER_SUCCESS) {
2518
3
          break;
2519
3
        }
2520
13.6k
        s->num_block_types[s->loop_counter]++;
2521
13.6k
        BROTLI_LOG_UINT(s->num_block_types[s->loop_counter]);
2522
13.6k
        if (s->num_block_types[s->loop_counter] < 2) {
2523
10.8k
          s->loop_counter++;
2524
10.8k
          break;
2525
10.8k
        }
2526
2.83k
        s->state = BROTLI_STATE_HUFFMAN_CODE_1;
2527
      /* Fall through. */
2528
2529
2.83k
      case BROTLI_STATE_HUFFMAN_CODE_1: {
2530
2.83k
        brotli_reg_t alphabet_size = s->num_block_types[s->loop_counter] + 2;
2531
2.83k
        int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_258;
2532
2.83k
        result = ReadHuffmanCode(alphabet_size, alphabet_size,
2533
2.83k
            &s->block_type_trees[tree_offset], NULL, s);
2534
2.83k
        if (result != BROTLI_DECODER_SUCCESS) break;
2535
2.79k
        s->state = BROTLI_STATE_HUFFMAN_CODE_2;
2536
2.79k
      }
2537
      /* Fall through. */
2538
2539
2.79k
      case BROTLI_STATE_HUFFMAN_CODE_2: {
2540
2.79k
        brotli_reg_t alphabet_size = BROTLI_NUM_BLOCK_LEN_SYMBOLS;
2541
2.79k
        int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26;
2542
2.79k
        result = ReadHuffmanCode(alphabet_size, alphabet_size,
2543
2.79k
            &s->block_len_trees[tree_offset], NULL, s);
2544
2.79k
        if (result != BROTLI_DECODER_SUCCESS) break;
2545
2.77k
        s->state = BROTLI_STATE_HUFFMAN_CODE_3;
2546
2.77k
      }
2547
      /* Fall through. */
2548
2549
2.77k
      case BROTLI_STATE_HUFFMAN_CODE_3: {
2550
2.77k
        int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26;
2551
2.77k
        if (!SafeReadBlockLength(s, &s->block_length[s->loop_counter],
2552
2.77k
            &s->block_len_trees[tree_offset], br)) {
2553
5
          result = BROTLI_DECODER_NEEDS_MORE_INPUT;
2554
5
          break;
2555
5
        }
2556
2.76k
        BROTLI_LOG_UINT(s->block_length[s->loop_counter]);
2557
2.76k
        s->loop_counter++;
2558
2.76k
        s->state = BROTLI_STATE_HUFFMAN_CODE_0;
2559
2.76k
        break;
2560
2.77k
      }
2561
2562
2.83k
      case BROTLI_STATE_UNCOMPRESSED: {
2563
2.83k
        result = CopyUncompressedBlockToOutput(
2564
2.83k
            available_out, next_out, total_out, s);
2565
2.83k
        if (result != BROTLI_DECODER_SUCCESS) {
2566
55
          break;
2567
55
        }
2568
2.78k
        s->state = BROTLI_STATE_METABLOCK_DONE;
2569
2.78k
        break;
2570
2.83k
      }
2571
2572
37.5k
      case BROTLI_STATE_METADATA:
2573
37.5k
        result = SkipMetadataBlock(s);
2574
37.5k
        if (result != BROTLI_DECODER_SUCCESS) {
2575
30
          break;
2576
30
        }
2577
37.5k
        s->state = BROTLI_STATE_METABLOCK_DONE;
2578
37.5k
        break;
2579
2580
4.51k
      case BROTLI_STATE_METABLOCK_HEADER_2: {
2581
4.51k
        brotli_reg_t bits;
2582
4.51k
        if (!BrotliSafeReadBits(br, 6, &bits)) {
2583
2
          result = BROTLI_DECODER_NEEDS_MORE_INPUT;
2584
2
          break;
2585
2
        }
2586
4.50k
        s->distance_postfix_bits = bits & BitMask(2);
2587
4.50k
        bits >>= 2;
2588
4.50k
        s->num_direct_distance_codes = bits << s->distance_postfix_bits;
2589
4.50k
        BROTLI_LOG_UINT(s->num_direct_distance_codes);
2590
4.50k
        BROTLI_LOG_UINT(s->distance_postfix_bits);
2591
4.50k
        s->context_modes =
2592
4.50k
            (uint8_t*)BROTLI_DECODER_ALLOC(s, (size_t)s->num_block_types[0]);
2593
4.50k
        if (s->context_modes == 0) {
2594
0
          result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES);
2595
0
          break;
2596
0
        }
2597
4.50k
        s->loop_counter = 0;
2598
4.50k
        s->state = BROTLI_STATE_CONTEXT_MODES;
2599
4.50k
      }
2600
      /* Fall through. */
2601
2602
4.50k
      case BROTLI_STATE_CONTEXT_MODES:
2603
4.50k
        result = ReadContextModes(s);
2604
4.50k
        if (result != BROTLI_DECODER_SUCCESS) {
2605
5
          break;
2606
5
        }
2607
4.50k
        s->state = BROTLI_STATE_CONTEXT_MAP_1;
2608
      /* Fall through. */
2609
2610
4.50k
      case BROTLI_STATE_CONTEXT_MAP_1:
2611
4.50k
        result = DecodeContextMap(
2612
4.50k
            s->num_block_types[0] << BROTLI_LITERAL_CONTEXT_BITS,
2613
4.50k
            &s->num_literal_htrees, &s->context_map, s);
2614
4.50k
        if (result != BROTLI_DECODER_SUCCESS) {
2615
56
          break;
2616
56
        }
2617
4.44k
        DetectTrivialLiteralBlockTypes(s);
2618
4.44k
        s->state = BROTLI_STATE_CONTEXT_MAP_2;
2619
      /* Fall through. */
2620
2621
4.44k
      case BROTLI_STATE_CONTEXT_MAP_2: {
2622
4.44k
        brotli_reg_t npostfix = s->distance_postfix_bits;
2623
4.44k
        brotli_reg_t ndirect = s->num_direct_distance_codes;
2624
4.44k
        brotli_reg_t distance_alphabet_size_max = BROTLI_DISTANCE_ALPHABET_SIZE(
2625
4.44k
            npostfix, ndirect, BROTLI_MAX_DISTANCE_BITS);
2626
4.44k
        brotli_reg_t distance_alphabet_size_limit = distance_alphabet_size_max;
2627
4.44k
        BROTLI_BOOL allocation_success = BROTLI_TRUE;
2628
4.44k
        if (s->large_window) {
2629
0
          BrotliDistanceCodeLimit limit = BrotliCalculateDistanceCodeLimit(
2630
0
              BROTLI_MAX_ALLOWED_DISTANCE, (uint32_t)npostfix,
2631
0
              (uint32_t)ndirect);
2632
0
          distance_alphabet_size_max = BROTLI_DISTANCE_ALPHABET_SIZE(
2633
0
              npostfix, ndirect, BROTLI_LARGE_MAX_DISTANCE_BITS);
2634
0
          distance_alphabet_size_limit = limit.max_alphabet_size;
2635
0
        }
2636
4.44k
        result = DecodeContextMap(
2637
4.44k
            s->num_block_types[2] << BROTLI_DISTANCE_CONTEXT_BITS,
2638
4.44k
            &s->num_dist_htrees, &s->dist_context_map, s);
2639
4.44k
        if (result != BROTLI_DECODER_SUCCESS) {
2640
22
          break;
2641
22
        }
2642
4.42k
        allocation_success &= BrotliDecoderHuffmanTreeGroupInit(
2643
4.42k
            s, &s->literal_hgroup, BROTLI_NUM_LITERAL_SYMBOLS,
2644
4.42k
            BROTLI_NUM_LITERAL_SYMBOLS, s->num_literal_htrees);
2645
4.42k
        allocation_success &= BrotliDecoderHuffmanTreeGroupInit(
2646
4.42k
            s, &s->insert_copy_hgroup, BROTLI_NUM_COMMAND_SYMBOLS,
2647
4.42k
            BROTLI_NUM_COMMAND_SYMBOLS, s->num_block_types[1]);
2648
4.42k
        allocation_success &= BrotliDecoderHuffmanTreeGroupInit(
2649
4.42k
            s, &s->distance_hgroup, distance_alphabet_size_max,
2650
4.42k
            distance_alphabet_size_limit, s->num_dist_htrees);
2651
4.42k
        if (!allocation_success) {
2652
0
          return BROTLI_SAVE_ERROR_CODE(
2653
0
              BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS));
2654
0
        }
2655
4.42k
        s->loop_counter = 0;
2656
4.42k
        s->state = BROTLI_STATE_TREE_GROUP;
2657
4.42k
      }
2658
      /* Fall through. */
2659
2660
13.0k
      case BROTLI_STATE_TREE_GROUP: {
2661
13.0k
        HuffmanTreeGroup* hgroup = NULL;
2662
13.0k
        switch (s->loop_counter) {
2663
4.42k
          case 0: hgroup = &s->literal_hgroup; break;
2664
4.33k
          case 1: hgroup = &s->insert_copy_hgroup; break;
2665
4.28k
          case 2: hgroup = &s->distance_hgroup; break;
2666
0
          default: return BROTLI_SAVE_ERROR_CODE(BROTLI_FAILURE(
2667
13.0k
              BROTLI_DECODER_ERROR_UNREACHABLE));  /* COV_NF_LINE */
2668
13.0k
        }
2669
13.0k
        result = HuffmanTreeGroupDecode(hgroup, s);
2670
13.0k
        if (result != BROTLI_DECODER_SUCCESS) break;
2671
12.8k
        s->loop_counter++;
2672
12.8k
        if (s->loop_counter < 3) {
2673
8.61k
          break;
2674
8.61k
        }
2675
4.26k
        s->state = BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_BODY;
2676
4.26k
      }
2677
      /* Fall through. */
2678
2679
4.26k
      case BROTLI_STATE_BEFORE_COMPRESSED_METABLOCK_BODY:
2680
4.26k
        PrepareLiteralDecoding(s);
2681
4.26k
        s->dist_context_map_slice = s->dist_context_map;
2682
4.26k
        s->htree_command = s->insert_copy_hgroup.htrees[0];
2683
4.26k
        if (!BrotliEnsureRingBuffer(s)) {
2684
0
          result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2);
2685
0
          break;
2686
0
        }
2687
4.26k
        CalculateDistanceLut(s);
2688
4.26k
        s->state = BROTLI_STATE_COMMAND_BEGIN;
2689
      /* Fall through. */
2690
2691
4.50k
      case BROTLI_STATE_COMMAND_BEGIN:
2692
      /* Fall through. */
2693
4.96k
      case BROTLI_STATE_COMMAND_INNER:
2694
      /* Fall through. */
2695
5.23k
      case BROTLI_STATE_COMMAND_POST_DECODE_LITERALS:
2696
      /* Fall through. */
2697
7.13k
      case BROTLI_STATE_COMMAND_POST_WRAP_COPY:
2698
7.13k
        result = ProcessCommands(s);
2699
7.13k
        if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) {
2700
1.00k
          result = SafeProcessCommands(s);
2701
1.00k
        }
2702
7.13k
        break;
2703
2704
754
      case BROTLI_STATE_COMMAND_INNER_WRITE:
2705
      /* Fall through. */
2706
1.00k
      case BROTLI_STATE_COMMAND_POST_WRITE_1:
2707
      /* Fall through. */
2708
2.93k
      case BROTLI_STATE_COMMAND_POST_WRITE_2:
2709
2.93k
        result = WriteRingBuffer(
2710
2.93k
            s, available_out, next_out, total_out, BROTLI_FALSE);
2711
2.93k
        if (result != BROTLI_DECODER_SUCCESS) {
2712
58
          break;
2713
58
        }
2714
2.87k
        WrapRingBuffer(s);
2715
2.87k
        if (s->ringbuffer_size == 1 << s->window_bits) {
2716
2.87k
          s->max_distance = s->max_backward_distance;
2717
2.87k
        }
2718
2.87k
        if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_1) {
2719
242
          BrotliDecoderCompoundDictionary* addon = s->compound_dictionary;
2720
242
          if (addon && (addon->br_length != addon->br_copied)) {
2721
0
            s->pos += CopyFromCompoundDictionary(s, s->pos);
2722
0
            if (s->pos >= s->ringbuffer_size) continue;
2723
0
          }
2724
242
          if (s->meta_block_remaining_len == 0) {
2725
            /* Next metablock, if any. */
2726
1
            s->state = BROTLI_STATE_METABLOCK_DONE;
2727
241
          } else {
2728
241
            s->state = BROTLI_STATE_COMMAND_BEGIN;
2729
241
          }
2730
242
          break;
2731
2.63k
        } else if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_2) {
2732
1.89k
          s->state = BROTLI_STATE_COMMAND_POST_WRAP_COPY;
2733
1.89k
        } else {  /* BROTLI_STATE_COMMAND_INNER_WRITE */
2734
735
          if (s->loop_counter == 0) {
2735
278
            if (s->meta_block_remaining_len == 0) {
2736
2
              s->state = BROTLI_STATE_METABLOCK_DONE;
2737
276
            } else {
2738
276
              s->state = BROTLI_STATE_COMMAND_POST_DECODE_LITERALS;
2739
276
            }
2740
278
            break;
2741
278
          }
2742
457
          s->state = BROTLI_STATE_COMMAND_INNER;
2743
457
        }
2744
2.35k
        break;
2745
2746
44.1k
      case BROTLI_STATE_METABLOCK_DONE:
2747
44.1k
        if (s->meta_block_remaining_len < 0) {
2748
118
          result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2);
2749
118
          break;
2750
118
        }
2751
44.0k
        BrotliDecoderStateCleanupAfterMetablock(s);
2752
44.0k
        if (!s->is_last_metablock) {
2753
43.4k
          s->state = BROTLI_STATE_METABLOCK_BEGIN;
2754
43.4k
          break;
2755
43.4k
        }
2756
570
        if (!BrotliJumpToByteBoundary(br)) {
2757
12
          result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_PADDING_2);
2758
12
          break;
2759
12
        }
2760
558
        if (s->buffer_length == 0) {
2761
558
          BrotliBitReaderUnload(br);
2762
558
          *available_in = BrotliBitReaderGetAvailIn(br);
2763
558
          *next_in = br->next_in;
2764
558
        }
2765
558
        s->state = BROTLI_STATE_DONE;
2766
      /* Fall through. */
2767
2768
558
      case BROTLI_STATE_DONE:
2769
558
        if (s->ringbuffer != 0) {
2770
555
          result = WriteRingBuffer(
2771
555
              s, available_out, next_out, total_out, BROTLI_TRUE);
2772
555
          if (result != BROTLI_DECODER_SUCCESS) {
2773
3
            break;
2774
3
          }
2775
555
        }
2776
555
        return BROTLI_SAVE_ERROR_CODE(result);
2777
163k
    }
2778
163k
  }
2779
1.12k
  return BROTLI_SAVE_ERROR_CODE(result);
2780
1.67k
#undef BROTLI_SAVE_ERROR_CODE
2781
1.67k
}
2782
2783
0
BROTLI_BOOL BrotliDecoderHasMoreOutput(const BrotliDecoderState* s) {
2784
  /* After unrecoverable error remaining output is considered nonsensical. */
2785
0
  if ((int)s->error_code < 0) {
2786
0
    return BROTLI_FALSE;
2787
0
  }
2788
0
  return TO_BROTLI_BOOL(
2789
0
      s->ringbuffer != 0 && UnwrittenBytes(s, BROTLI_FALSE) != 0);
2790
0
}
2791
2792
0
const uint8_t* BrotliDecoderTakeOutput(BrotliDecoderState* s, size_t* size) {
2793
0
  uint8_t* result = 0;
2794
0
  size_t available_out = *size ? *size : 1u << 24;
2795
0
  size_t requested_out = available_out;
2796
0
  BrotliDecoderErrorCode status;
2797
0
  if ((s->ringbuffer == 0) || ((int)s->error_code < 0)) {
2798
0
    *size = 0;
2799
0
    return 0;
2800
0
  }
2801
0
  WrapRingBuffer(s);
2802
0
  status = WriteRingBuffer(s, &available_out, &result, 0, BROTLI_TRUE);
2803
  /* Either WriteRingBuffer returns those "success" codes... */
2804
0
  if (status == BROTLI_DECODER_SUCCESS ||
2805
0
      status == BROTLI_DECODER_NEEDS_MORE_OUTPUT) {
2806
0
    *size = requested_out - available_out;
2807
0
  } else {
2808
    /* ... or stream is broken. Normally this should be caught by
2809
       BrotliDecoderDecompressStream, this is just a safeguard. */
2810
0
    if ((int)status < 0) SaveErrorCode(s, status, 0);
2811
0
    *size = 0;
2812
0
    result = 0;
2813
0
  }
2814
0
  return result;
2815
0
}
2816
2817
0
BROTLI_BOOL BrotliDecoderIsUsed(const BrotliDecoderState* s) {
2818
0
  return TO_BROTLI_BOOL(s->state != BROTLI_STATE_UNINITED ||
2819
0
      BrotliGetAvailableBits(&s->br) != 0);
2820
0
}
2821
2822
0
BROTLI_BOOL BrotliDecoderIsFinished(const BrotliDecoderState* s) {
2823
0
  return TO_BROTLI_BOOL(s->state == BROTLI_STATE_DONE) &&
2824
0
      !BrotliDecoderHasMoreOutput(s);
2825
0
}
2826
2827
0
BrotliDecoderErrorCode BrotliDecoderGetErrorCode(const BrotliDecoderState* s) {
2828
0
  return (BrotliDecoderErrorCode)s->error_code;
2829
0
}
2830
2831
0
const char* BrotliDecoderErrorString(BrotliDecoderErrorCode c) {
2832
0
  switch (c) {
2833
0
#define BROTLI_ERROR_CODE_CASE_(PREFIX, NAME, CODE) \
2834
0
    case BROTLI_DECODER ## PREFIX ## NAME: return #PREFIX #NAME;
2835
0
#define BROTLI_NOTHING_
2836
0
    BROTLI_DECODER_ERROR_CODES_LIST(BROTLI_ERROR_CODE_CASE_, BROTLI_NOTHING_)
2837
0
#undef BROTLI_ERROR_CODE_CASE_
2838
0
#undef BROTLI_NOTHING_
2839
0
    default: return "INVALID";
2840
0
  }
2841
0
}
2842
2843
0
uint32_t BrotliDecoderVersion(void) {
2844
0
  return BROTLI_VERSION;
2845
0
}
2846
2847
void BrotliDecoderSetMetadataCallbacks(
2848
    BrotliDecoderState* state,
2849
    brotli_decoder_metadata_start_func start_func,
2850
0
    brotli_decoder_metadata_chunk_func chunk_func, void* opaque) {
2851
0
  state->metadata_start_func = start_func;
2852
0
  state->metadata_chunk_func = chunk_func;
2853
0
  state->metadata_callback_opaque = opaque;
2854
0
}
2855
2856
/* Escalate internal functions visibility; for testing purposes only. */
2857
#if defined(BROTLI_TEST)
2858
BROTLI_BOOL SafeReadSymbolForTest(
2859
    const HuffmanCode*, BrotliBitReader*, brotli_reg_t*);
2860
BROTLI_BOOL SafeReadSymbolForTest(
2861
    const HuffmanCode* table, BrotliBitReader* br, brotli_reg_t* result) {
2862
  return SafeReadSymbol(table, br, result);
2863
}
2864
2865
void InverseMoveToFrontTransformForTest(
2866
    uint8_t*, brotli_reg_t, BrotliDecoderState*);
2867
void InverseMoveToFrontTransformForTest(
2868
    uint8_t* v, brotli_reg_t l, BrotliDecoderState* s) {
2869
  InverseMoveToFrontTransform(v, l, s);
2870
}
2871
#endif
2872
2873
#if defined(__cplusplus) || defined(c_plusplus)
2874
}  /* extern "C" */
2875
#endif