2013-12-16 13:45:57 +00:00
|
|
|
/* Copyright 2013 Google Inc. All Rights Reserved.
|
|
|
|
|
2015-12-11 10:11:51 +00:00
|
|
|
Distributed under MIT license.
|
2015-11-27 10:27:11 +00:00
|
|
|
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
2013-12-16 13:45:57 +00:00
|
|
|
*/
|
2013-10-11 08:26:07 +00:00
|
|
|
|
|
|
|
#include "./decode.h"
|
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
#ifdef __ARM_NEON__
|
|
|
|
#include <arm_neon.h>
|
|
|
|
#endif
|
|
|
|
|
2016-01-22 09:19:41 +00:00
|
|
|
#include <stdlib.h> /* free, malloc */
|
|
|
|
#include <string.h> /* memcpy, memset */
|
|
|
|
|
2016-06-03 08:51:04 +00:00
|
|
|
#include "../common/constants.h"
|
|
|
|
#include "../common/dictionary.h"
|
2016-01-22 09:19:41 +00:00
|
|
|
#include "./bit_reader.h"
|
|
|
|
#include "./context.h"
|
|
|
|
#include "./huffman.h"
|
|
|
|
#include "./port.h"
|
|
|
|
#include "./prefix.h"
|
2016-04-19 14:29:10 +00:00
|
|
|
#include "./state.h"
|
2016-01-22 09:19:41 +00:00
|
|
|
#include "./transform.h"
|
|
|
|
|
2013-10-11 08:26:07 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2016-05-11 13:23:07 +00:00
|
|
|
#define BROTLI_FAILURE(CODE) (BROTLI_DUMP(), CODE)
|
2016-01-22 09:19:41 +00:00
|
|
|
|
2016-02-18 14:03:44 +00:00
|
|
|
#define BROTLI_LOG_UINT(name) \
|
2016-04-19 14:29:10 +00:00
|
|
|
BROTLI_LOG(("[%s] %s = %lu\n", __func__, #name, (unsigned long)(name)))
|
2016-02-18 14:03:44 +00:00
|
|
|
#define BROTLI_LOG_ARRAY_INDEX(array_name, idx) \
|
2016-04-19 14:29:10 +00:00
|
|
|
BROTLI_LOG(("[%s] %s[%lu] = %lu\n", __func__, #array_name, \
|
|
|
|
(unsigned long)(idx), (unsigned long)array_name[idx]))
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2016-02-18 14:03:44 +00:00
|
|
|
#define HUFFMAN_TABLE_BITS 8U
|
|
|
|
#define HUFFMAN_TABLE_MASK 0xff
|
2014-02-14 14:04:23 +00:00
|
|
|
|
2016-06-03 08:51:04 +00:00
|
|
|
static const uint8_t kCodeLengthCodeOrder[BROTLI_CODE_LENGTH_CODES] = {
|
2014-02-14 14:04:23 +00:00
|
|
|
1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
2013-10-11 08:26:07 +00:00
|
|
|
};
|
|
|
|
|
2015-09-24 16:35:25 +00:00
|
|
|
/* Static prefix code for the complex code length code lengths. */
|
|
|
|
static const uint8_t kCodeLengthPrefixLength[16] = {
|
|
|
|
2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 3, 2, 2, 2, 4,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint8_t kCodeLengthPrefixValue[16] = {
|
|
|
|
0, 4, 3, 2, 0, 4, 3, 1, 0, 4, 3, 2, 0, 4, 3, 5,
|
|
|
|
};
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderState* BrotliDecoderCreateInstance(
|
2015-11-23 09:21:09 +00:00
|
|
|
brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderState* state = 0;
|
2015-11-23 09:21:09 +00:00
|
|
|
if (!alloc_func && !free_func) {
|
2016-07-25 08:17:04 +00:00
|
|
|
state = (BrotliDecoderState*)malloc(sizeof(BrotliDecoderState));
|
2015-11-23 09:21:09 +00:00
|
|
|
} else if (alloc_func && free_func) {
|
2016-07-25 08:17:04 +00:00
|
|
|
state = (BrotliDecoderState*)alloc_func(opaque, sizeof(BrotliDecoderState));
|
2015-11-23 09:21:09 +00:00
|
|
|
}
|
|
|
|
if (state == 0) {
|
2016-04-19 14:29:10 +00:00
|
|
|
BROTLI_DUMP();
|
2015-11-23 09:21:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderStateInitWithCustomAllocators(
|
|
|
|
state, alloc_func, free_func, opaque);
|
|
|
|
state->error_code = BROTLI_DECODER_NO_ERROR;
|
2015-11-23 09:21:09 +00:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
/* Deinitializes and frees BrotliDecoderState instance. */
|
|
|
|
void BrotliDecoderDestroyInstance(BrotliDecoderState* state) {
|
2015-11-23 09:21:09 +00:00
|
|
|
if (!state) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
brotli_free_func free_func = state->free_func;
|
|
|
|
void* opaque = state->memory_manager_opaque;
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderStateCleanup(state);
|
2015-11-23 09:21:09 +00:00
|
|
|
free_func(opaque, state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
/* Saves error code and converts it to BrotliDecoderResult */
|
|
|
|
static BROTLI_NOINLINE BrotliDecoderResult SaveErrorCode(
|
|
|
|
BrotliDecoderState* s, BrotliDecoderErrorCode e) {
|
2016-05-11 13:23:07 +00:00
|
|
|
s->error_code = (int)e;
|
|
|
|
switch (e) {
|
2016-07-25 08:17:04 +00:00
|
|
|
case BROTLI_DECODER_SUCCESS:
|
|
|
|
return BROTLI_DECODER_RESULT_SUCCESS;
|
|
|
|
case BROTLI_DECODER_NEEDS_MORE_INPUT:
|
|
|
|
return BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
case BROTLI_DECODER_NEEDS_MORE_OUTPUT:
|
|
|
|
return BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
|
|
|
|
default:
|
|
|
|
return BROTLI_DECODER_RESULT_ERROR;
|
2016-05-11 13:23:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 10:24:06 +00:00
|
|
|
/* Decodes a number in the range [9..24], by reading 1 - 7 bits.
|
|
|
|
Precondition: bit-reader accumulator has at least 7 bits. */
|
2015-08-10 11:35:23 +00:00
|
|
|
static uint32_t DecodeWindowBits(BrotliBitReader* br) {
|
|
|
|
uint32_t n;
|
2015-09-23 10:24:06 +00:00
|
|
|
BrotliTakeBits(br, 1, &n);
|
|
|
|
if (n == 0) {
|
2013-11-28 16:37:13 +00:00
|
|
|
return 16;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2015-09-23 10:24:06 +00:00
|
|
|
BrotliTakeBits(br, 3, &n);
|
2015-08-10 11:35:23 +00:00
|
|
|
if (n != 0) {
|
2015-05-07 15:44:33 +00:00
|
|
|
return 17 + n;
|
|
|
|
}
|
2015-09-23 10:24:06 +00:00
|
|
|
BrotliTakeBits(br, 3, &n);
|
2015-08-10 11:35:23 +00:00
|
|
|
if (n != 0) {
|
2015-05-07 15:44:33 +00:00
|
|
|
return 8 + n;
|
|
|
|
}
|
|
|
|
return 17;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2016-02-18 14:03:44 +00:00
|
|
|
static BROTLI_INLINE void memmove16(uint8_t* dst, uint8_t* src) {
|
2016-01-28 15:28:36 +00:00
|
|
|
#if defined(__ARM_NEON__)
|
|
|
|
vst1q_u8(dst, vld1q_u8(src));
|
|
|
|
#else
|
2015-09-28 10:38:29 +00:00
|
|
|
uint32_t buffer[4];
|
|
|
|
memcpy(buffer, src, 16);
|
|
|
|
memcpy(dst, buffer, 16);
|
2015-08-10 11:35:23 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-12-16 13:45:57 +00:00
|
|
|
/* Decodes a number in the range [0..255], by reading 1 - 11 bits. */
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_NOINLINE BrotliDecoderErrorCode DecodeVarLenUint8(
|
|
|
|
BrotliDecoderState* s, BrotliBitReader* br, uint32_t* value) {
|
2015-09-24 16:35:25 +00:00
|
|
|
uint32_t bits;
|
|
|
|
switch (s->substate_decode_uint8) {
|
|
|
|
case BROTLI_STATE_DECODE_UINT8_NONE:
|
|
|
|
if (PREDICT_FALSE(!BrotliSafeReadBits(br, 1, &bits))) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-09-24 16:35:25 +00:00
|
|
|
}
|
|
|
|
if (bits == 0) {
|
|
|
|
*value = 0;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2015-09-24 16:35:25 +00:00
|
|
|
}
|
|
|
|
/* No break, transit to the next state. */
|
|
|
|
|
|
|
|
case BROTLI_STATE_DECODE_UINT8_SHORT:
|
|
|
|
if (PREDICT_FALSE(!BrotliSafeReadBits(br, 3, &bits))) {
|
|
|
|
s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_SHORT;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-09-24 16:35:25 +00:00
|
|
|
}
|
|
|
|
if (bits == 0) {
|
|
|
|
*value = 1;
|
|
|
|
s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2015-09-24 16:35:25 +00:00
|
|
|
}
|
|
|
|
/* Use output value as a temporary storage. It MUST be persisted. */
|
2015-10-05 08:23:32 +00:00
|
|
|
*value = bits;
|
2015-09-24 16:35:25 +00:00
|
|
|
/* No break, transit to the next state. */
|
|
|
|
|
|
|
|
case BROTLI_STATE_DECODE_UINT8_LONG:
|
|
|
|
if (PREDICT_FALSE(!BrotliSafeReadBits(br, *value, &bits))) {
|
|
|
|
s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_LONG;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-09-24 16:35:25 +00:00
|
|
|
}
|
2015-10-05 08:23:32 +00:00
|
|
|
*value = (1U << *value) + bits;
|
2015-09-24 16:35:25 +00:00
|
|
|
s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2015-09-24 16:35:25 +00:00
|
|
|
|
|
|
|
default:
|
2016-07-25 08:17:04 +00:00
|
|
|
return
|
|
|
|
BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE);
|
2013-12-12 12:18:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 10:24:06 +00:00
|
|
|
/* Decodes a metablock length and flags by reading 2 - 31 bits. */
|
2016-07-25 08:17:04 +00:00
|
|
|
static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(
|
|
|
|
BrotliDecoderState* s, BrotliBitReader* br) {
|
2015-09-23 10:24:06 +00:00
|
|
|
uint32_t bits;
|
2013-12-13 09:39:46 +00:00
|
|
|
int i;
|
2015-09-23 10:24:06 +00:00
|
|
|
for (;;) {
|
|
|
|
switch (s->substate_metablock_header) {
|
|
|
|
case BROTLI_STATE_METABLOCK_HEADER_NONE:
|
|
|
|
if (!BrotliSafeReadBits(br, 1, &bits)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-09-23 10:24:06 +00:00
|
|
|
}
|
|
|
|
s->is_last_metablock = (uint8_t)bits;
|
|
|
|
s->meta_block_remaining_len = 0;
|
|
|
|
s->is_uncompressed = 0;
|
|
|
|
s->is_metadata = 0;
|
|
|
|
if (!s->is_last_metablock) {
|
|
|
|
s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NIBBLES;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_EMPTY;
|
|
|
|
/* No break, transit to the next state. */
|
|
|
|
|
|
|
|
case BROTLI_STATE_METABLOCK_HEADER_EMPTY:
|
|
|
|
if (!BrotliSafeReadBits(br, 1, &bits)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-09-23 10:24:06 +00:00
|
|
|
}
|
|
|
|
if (bits) {
|
|
|
|
s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2015-09-23 10:24:06 +00:00
|
|
|
}
|
|
|
|
s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NIBBLES;
|
|
|
|
/* No break, transit to the next state. */
|
|
|
|
|
|
|
|
case BROTLI_STATE_METABLOCK_HEADER_NIBBLES:
|
|
|
|
if (!BrotliSafeReadBits(br, 2, &bits)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-09-23 10:24:06 +00:00
|
|
|
}
|
|
|
|
s->size_nibbles = (uint8_t)(bits + 4);
|
|
|
|
s->loop_counter = 0;
|
|
|
|
if (bits == 3) {
|
|
|
|
s->is_metadata = 1;
|
|
|
|
s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_RESERVED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_SIZE;
|
|
|
|
/* No break, transit to the next state. */
|
|
|
|
|
|
|
|
case BROTLI_STATE_METABLOCK_HEADER_SIZE:
|
|
|
|
i = s->loop_counter;
|
|
|
|
for (; i < s->size_nibbles; ++i) {
|
|
|
|
if (!BrotliSafeReadBits(br, 4, &bits)) {
|
|
|
|
s->loop_counter = i;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-09-23 10:24:06 +00:00
|
|
|
}
|
|
|
|
if (i + 1 == s->size_nibbles && s->size_nibbles > 4 && bits == 0) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE);
|
2015-09-23 10:24:06 +00:00
|
|
|
}
|
|
|
|
s->meta_block_remaining_len |= (int)(bits << (i * 4));
|
|
|
|
}
|
|
|
|
s->substate_metablock_header =
|
|
|
|
BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED;
|
|
|
|
/* No break, transit to the next state. */
|
|
|
|
|
|
|
|
case BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED:
|
2016-01-22 09:19:41 +00:00
|
|
|
if (!s->is_last_metablock) {
|
2015-09-23 10:24:06 +00:00
|
|
|
if (!BrotliSafeReadBits(br, 1, &bits)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-09-23 10:24:06 +00:00
|
|
|
}
|
|
|
|
s->is_uncompressed = (uint8_t)bits;
|
|
|
|
}
|
|
|
|
++s->meta_block_remaining_len;
|
|
|
|
s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2015-09-23 10:24:06 +00:00
|
|
|
|
|
|
|
case BROTLI_STATE_METABLOCK_HEADER_RESERVED:
|
|
|
|
if (!BrotliSafeReadBits(br, 1, &bits)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-09-23 10:24:06 +00:00
|
|
|
}
|
|
|
|
if (bits != 0) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_RESERVED);
|
2015-09-23 10:24:06 +00:00
|
|
|
}
|
|
|
|
s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_BYTES;
|
|
|
|
/* No break, transit to the next state. */
|
|
|
|
|
|
|
|
case BROTLI_STATE_METABLOCK_HEADER_BYTES:
|
|
|
|
if (!BrotliSafeReadBits(br, 2, &bits)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-09-23 10:24:06 +00:00
|
|
|
}
|
|
|
|
if (bits == 0) {
|
|
|
|
s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2015-09-23 10:24:06 +00:00
|
|
|
}
|
|
|
|
s->size_nibbles = (uint8_t)bits;
|
|
|
|
s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_METADATA;
|
|
|
|
/* No break, transit to the next state. */
|
|
|
|
|
|
|
|
case BROTLI_STATE_METABLOCK_HEADER_METADATA:
|
|
|
|
i = s->loop_counter;
|
|
|
|
for (; i < s->size_nibbles; ++i) {
|
|
|
|
if (!BrotliSafeReadBits(br, 8, &bits)) {
|
|
|
|
s->loop_counter = i;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-09-23 10:24:06 +00:00
|
|
|
}
|
|
|
|
if (i + 1 == s->size_nibbles && s->size_nibbles > 1 && bits == 0) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FAILURE(
|
|
|
|
BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE);
|
2015-09-23 10:24:06 +00:00
|
|
|
}
|
|
|
|
s->meta_block_remaining_len |= (int)(bits << (i * 8));
|
|
|
|
}
|
2016-01-22 09:19:41 +00:00
|
|
|
++s->meta_block_remaining_len;
|
|
|
|
s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2015-09-23 10:24:06 +00:00
|
|
|
|
|
|
|
default:
|
2016-07-25 08:17:04 +00:00
|
|
|
return
|
|
|
|
BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE);
|
2015-04-22 12:35:21 +00:00
|
|
|
}
|
2013-11-28 16:37:13 +00:00
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 10:46:04 +00:00
|
|
|
/* Decodes the Huffman code.
|
|
|
|
This method doesn't read data from the bit reader, BUT drops the amount of
|
|
|
|
bits that correspond to the decoded symbol.
|
|
|
|
bits MUST contain at least 15 (BROTLI_HUFFMAN_MAX_CODE_LENGTH) valid bits. */
|
|
|
|
static BROTLI_INLINE uint32_t DecodeSymbol(uint32_t bits,
|
|
|
|
const HuffmanCode* table,
|
|
|
|
BrotliBitReader* br) {
|
|
|
|
table += bits & HUFFMAN_TABLE_MASK;
|
2015-08-28 13:20:24 +00:00
|
|
|
if (table->bits > HUFFMAN_TABLE_BITS) {
|
2015-10-05 08:23:32 +00:00
|
|
|
uint32_t nbits = table->bits - HUFFMAN_TABLE_BITS;
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliDropBits(br, HUFFMAN_TABLE_BITS);
|
2014-02-14 14:04:23 +00:00
|
|
|
table += table->value;
|
2015-11-06 10:46:04 +00:00
|
|
|
table += (bits >> HUFFMAN_TABLE_BITS) & BitMask(nbits);
|
2014-02-14 14:04:23 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliDropBits(br, table->bits);
|
2014-02-14 14:04:23 +00:00
|
|
|
return table->value;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 10:46:04 +00:00
|
|
|
/* Reads and decodes the next Huffman code from bit-stream.
|
|
|
|
This method peeks 16 bits of input and drops 0 - 15 of them. */
|
|
|
|
static BROTLI_INLINE uint32_t ReadSymbol(const HuffmanCode* table,
|
|
|
|
BrotliBitReader* br) {
|
|
|
|
return DecodeSymbol(BrotliGet16BitsUnmasked(br), table, br);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Same as DecodeSymbol, but it is known that there is less than 15 bits of
|
|
|
|
input are currently available. */
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_NOINLINE BROTLI_BOOL SafeDecodeSymbol(
|
|
|
|
const HuffmanCode* table, BrotliBitReader* br, uint32_t* result) {
|
2015-11-06 10:46:04 +00:00
|
|
|
uint32_t val;
|
|
|
|
uint32_t available_bits = BrotliGetAvailableBits(br);
|
|
|
|
if (available_bits == 0) {
|
|
|
|
if (table->bits == 0) {
|
|
|
|
*result = table->value;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_TRUE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FALSE; /* No valid bits at all. */
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
val = (uint32_t)BrotliGetBitsUnmasked(br);
|
|
|
|
table += val & HUFFMAN_TABLE_MASK;
|
|
|
|
if (table->bits <= HUFFMAN_TABLE_BITS) {
|
|
|
|
if (table->bits <= available_bits) {
|
|
|
|
BrotliDropBits(br, table->bits);
|
|
|
|
*result = table->value;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_TRUE;
|
2015-11-06 10:46:04 +00:00
|
|
|
} else {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FALSE; /* Not enough bits for the first level. */
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (available_bits <= HUFFMAN_TABLE_BITS) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FALSE; /* Not enough bits to move to the second level. */
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Speculatively drop HUFFMAN_TABLE_BITS. */
|
|
|
|
val = (val & BitMask(table->bits)) >> HUFFMAN_TABLE_BITS;
|
|
|
|
available_bits -= HUFFMAN_TABLE_BITS;
|
|
|
|
table += table->value + val;
|
|
|
|
if (available_bits < table->bits) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FALSE; /* Not enough bits for the second level. */
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BrotliDropBits(br, HUFFMAN_TABLE_BITS + table->bits);
|
|
|
|
*result = table->value;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_TRUE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_INLINE BROTLI_BOOL SafeReadSymbol(
|
|
|
|
const HuffmanCode* table, BrotliBitReader* br, uint32_t* result) {
|
2015-11-06 10:46:04 +00:00
|
|
|
uint32_t val;
|
|
|
|
if (PREDICT_TRUE(BrotliSafeGetBits(br, 15, &val))) {
|
|
|
|
*result = DecodeSymbol(val, table, br);
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_TRUE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
return SafeDecodeSymbol(table, br, result);
|
|
|
|
}
|
|
|
|
|
2015-09-23 10:24:06 +00:00
|
|
|
/* Makes a look-up in first level Huffman table. Peeks 8 bits. */
|
2015-11-06 10:46:04 +00:00
|
|
|
static BROTLI_INLINE void PreloadSymbol(int safe,
|
|
|
|
const HuffmanCode* table,
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliBitReader* br,
|
2015-10-05 08:23:32 +00:00
|
|
|
uint32_t* bits,
|
|
|
|
uint32_t* value) {
|
2015-11-06 10:46:04 +00:00
|
|
|
if (safe) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
table += BrotliGetBits(br, HUFFMAN_TABLE_BITS);
|
2015-08-10 11:35:23 +00:00
|
|
|
*bits = table->bits;
|
|
|
|
*value = table->value;
|
|
|
|
}
|
|
|
|
|
2015-09-23 10:24:06 +00:00
|
|
|
/* Decodes the next Huffman code using data prepared by PreloadSymbol.
|
|
|
|
Reads 0 - 15 bits. Also peeks 8 following bits. */
|
2015-10-05 08:23:32 +00:00
|
|
|
static BROTLI_INLINE uint32_t ReadPreloadedSymbol(const HuffmanCode* table,
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliBitReader* br,
|
2015-10-05 08:23:32 +00:00
|
|
|
uint32_t* bits,
|
|
|
|
uint32_t* value) {
|
|
|
|
uint32_t result = *value;
|
2015-08-10 11:35:23 +00:00
|
|
|
if (PREDICT_FALSE(*bits > HUFFMAN_TABLE_BITS)) {
|
2015-11-06 10:46:04 +00:00
|
|
|
uint32_t val = BrotliGet16BitsUnmasked(br);
|
2015-08-10 11:35:23 +00:00
|
|
|
const HuffmanCode* ext = table + (val & HUFFMAN_TABLE_MASK) + *value;
|
2015-10-05 08:23:32 +00:00
|
|
|
uint32_t mask = BitMask((*bits - HUFFMAN_TABLE_BITS));
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliDropBits(br, HUFFMAN_TABLE_BITS);
|
2015-10-05 08:23:32 +00:00
|
|
|
ext += (val >> HUFFMAN_TABLE_BITS) & mask;
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliDropBits(br, ext->bits);
|
|
|
|
result = ext->value;
|
|
|
|
} else {
|
2015-11-06 10:46:04 +00:00
|
|
|
BrotliDropBits(br, *bits);
|
2013-12-12 12:18:04 +00:00
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
PreloadSymbol(0, table, br, bits, value);
|
2015-08-10 11:35:23 +00:00
|
|
|
return result;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 08:23:32 +00:00
|
|
|
static BROTLI_INLINE uint32_t Log2Floor(uint32_t x) {
|
|
|
|
uint32_t result = 0;
|
2015-09-24 16:35:25 +00:00
|
|
|
while (x) {
|
|
|
|
x >>= 1;
|
|
|
|
++result;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-11-06 10:46:04 +00:00
|
|
|
/* Reads (s->symbol + 1) symbols.
|
|
|
|
Totally 1..4 symbols are read, 1..10 bits each.
|
|
|
|
The list of symbols MUST NOT contain duplicates.
|
|
|
|
*/
|
2016-07-25 08:17:04 +00:00
|
|
|
static BrotliDecoderErrorCode ReadSimpleHuffmanSymbols(
|
|
|
|
uint32_t alphabet_size, BrotliDecoderState* s) {
|
2015-11-06 10:46:04 +00:00
|
|
|
/* max_bits == 1..10; symbol == 0..3; 1..40 bits will be read. */
|
|
|
|
BrotliBitReader* br = &s->br;
|
|
|
|
uint32_t max_bits = Log2Floor(alphabet_size - 1);
|
|
|
|
uint32_t i = s->sub_loop_counter;
|
|
|
|
uint32_t num_symbols = s->symbol;
|
|
|
|
while (i <= num_symbols) {
|
|
|
|
uint32_t v;
|
|
|
|
if (PREDICT_FALSE(!BrotliSafeReadBits(br, max_bits, &v))) {
|
|
|
|
s->sub_loop_counter = i;
|
|
|
|
s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_READ;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
if (v >= alphabet_size) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return
|
|
|
|
BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET);
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
s->symbols_lists_array[i] = (uint16_t)v;
|
|
|
|
BROTLI_LOG_UINT(s->symbols_lists_array[i]);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_symbols; ++i) {
|
|
|
|
uint32_t k = i + 1;
|
|
|
|
for (; k <= num_symbols; ++k) {
|
|
|
|
if (s->symbols_lists_array[i] == s->symbols_lists_array[k]) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME);
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process single decoded symbol code length:
|
|
|
|
A) reset the repeat variable
|
|
|
|
B) remember code length (if it is not 0)
|
|
|
|
C) extend corredponding index-chain
|
|
|
|
D) reduce the huffman space
|
|
|
|
E) update the histogram
|
|
|
|
*/
|
|
|
|
static BROTLI_INLINE void ProcessSingleCodeLength(uint32_t code_len,
|
|
|
|
uint32_t* symbol, uint32_t* repeat, uint32_t* space,
|
|
|
|
uint32_t* prev_code_len, uint16_t* symbol_lists,
|
|
|
|
uint16_t* code_length_histo, int* next_symbol) {
|
|
|
|
*repeat = 0;
|
|
|
|
if (code_len != 0) { /* code_len == 1..15 */
|
|
|
|
symbol_lists[next_symbol[code_len]] = (uint16_t)(*symbol);
|
|
|
|
next_symbol[code_len] = (int)(*symbol);
|
|
|
|
*prev_code_len = code_len;
|
|
|
|
*space -= 32768U >> code_len;
|
|
|
|
code_length_histo[code_len]++;
|
2016-02-18 14:03:44 +00:00
|
|
|
BROTLI_LOG(("[ReadHuffmanCode] code_length[%d] = %d\n", *symbol, code_len));
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
(*symbol)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process repeated symbol code length.
|
|
|
|
A) Check if it is the extension of previous repeat sequence; if the decoded
|
2016-06-03 08:51:04 +00:00
|
|
|
value is not BROTLI_REPEAT_PREVIOUS_CODE_LENGTH, then it is a new
|
|
|
|
symbol-skip
|
2015-11-06 10:46:04 +00:00
|
|
|
B) Update repeat variable
|
|
|
|
C) Check if operation is feasible (fits alphapet)
|
|
|
|
D) For each symbol do the same operations as in ProcessSingleCodeLength
|
|
|
|
|
2016-06-03 08:51:04 +00:00
|
|
|
PRECONDITION: code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH or
|
|
|
|
code_len == BROTLI_REPEAT_ZERO_CODE_LENGTH
|
2015-11-06 10:46:04 +00:00
|
|
|
*/
|
|
|
|
static BROTLI_INLINE void ProcessRepeatedCodeLength(uint32_t code_len,
|
|
|
|
uint32_t repeat_delta, uint32_t alphabet_size, uint32_t* symbol,
|
|
|
|
uint32_t* repeat, uint32_t* space, uint32_t* prev_code_len,
|
|
|
|
uint32_t* repeat_code_len, uint16_t* symbol_lists,
|
|
|
|
uint16_t* code_length_histo, int* next_symbol) {
|
|
|
|
uint32_t old_repeat;
|
2016-06-03 08:51:04 +00:00
|
|
|
uint32_t extra_bits = 3; /* for BROTLI_REPEAT_ZERO_CODE_LENGTH */
|
|
|
|
uint32_t new_len = 0; /* for BROTLI_REPEAT_ZERO_CODE_LENGTH */
|
|
|
|
if (code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) {
|
2015-11-06 10:46:04 +00:00
|
|
|
new_len = *prev_code_len;
|
2016-06-03 08:51:04 +00:00
|
|
|
extra_bits = 2;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
if (*repeat_code_len != new_len) {
|
|
|
|
*repeat = 0;
|
|
|
|
*repeat_code_len = new_len;
|
|
|
|
}
|
|
|
|
old_repeat = *repeat;
|
|
|
|
if (*repeat > 0) {
|
|
|
|
*repeat -= 2;
|
2016-06-03 08:51:04 +00:00
|
|
|
*repeat <<= extra_bits;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
*repeat += repeat_delta + 3U;
|
|
|
|
repeat_delta = *repeat - old_repeat;
|
|
|
|
if (*symbol + repeat_delta > alphabet_size) {
|
2016-04-19 14:29:10 +00:00
|
|
|
BROTLI_DUMP();
|
2015-11-06 10:46:04 +00:00
|
|
|
*symbol = alphabet_size;
|
|
|
|
*space = 0xFFFFF;
|
|
|
|
return;
|
|
|
|
}
|
2016-01-22 09:19:41 +00:00
|
|
|
BROTLI_LOG(("[ReadHuffmanCode] code_length[%d..%d] = %d\n",
|
|
|
|
*symbol, *symbol + repeat_delta - 1, *repeat_code_len));
|
2015-11-06 10:46:04 +00:00
|
|
|
if (*repeat_code_len != 0) {
|
|
|
|
unsigned last = *symbol + repeat_delta;
|
|
|
|
int next = next_symbol[*repeat_code_len];
|
|
|
|
do {
|
|
|
|
symbol_lists[next] = (uint16_t)*symbol;
|
|
|
|
next = (int)*symbol;
|
|
|
|
} while (++(*symbol) != last);
|
|
|
|
next_symbol[*repeat_code_len] = next;
|
|
|
|
*space -= repeat_delta << (15 - *repeat_code_len);
|
2016-02-18 14:03:44 +00:00
|
|
|
code_length_histo[*repeat_code_len] =
|
|
|
|
(uint16_t)(code_length_histo[*repeat_code_len] + repeat_delta);
|
2015-11-06 10:46:04 +00:00
|
|
|
} else {
|
|
|
|
*symbol += repeat_delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reads and decodes symbol codelengths. */
|
2016-07-25 08:17:04 +00:00
|
|
|
static BrotliDecoderErrorCode ReadSymbolCodeLengths(
|
|
|
|
uint32_t alphabet_size, BrotliDecoderState* s) {
|
2015-11-06 10:46:04 +00:00
|
|
|
BrotliBitReader* br = &s->br;
|
|
|
|
uint32_t symbol = s->symbol;
|
|
|
|
uint32_t repeat = s->repeat;
|
|
|
|
uint32_t space = s->space;
|
|
|
|
uint32_t prev_code_len = s->prev_code_len;
|
|
|
|
uint32_t repeat_code_len = s->repeat_code_len;
|
|
|
|
uint16_t* symbol_lists = s->symbol_lists;
|
|
|
|
uint16_t* code_length_histo = s->code_length_histo;
|
|
|
|
int* next_symbol = s->next_symbol;
|
|
|
|
if (!BrotliWarmupBitReader(br)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
while (symbol < alphabet_size && space > 0) {
|
|
|
|
const HuffmanCode* p = s->table;
|
|
|
|
uint32_t code_len;
|
|
|
|
if (!BrotliCheckInputAmount(br, BROTLI_SHORT_FILL_BIT_WINDOW_READ)) {
|
|
|
|
s->symbol = symbol;
|
|
|
|
s->repeat = repeat;
|
|
|
|
s->prev_code_len = prev_code_len;
|
|
|
|
s->repeat_code_len = repeat_code_len;
|
|
|
|
s->space = space;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
BrotliFillBitWindow16(br);
|
|
|
|
p += BrotliGetBitsUnmasked(br) &
|
|
|
|
BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH);
|
2016-02-18 14:03:44 +00:00
|
|
|
BrotliDropBits(br, p->bits); /* Use 1..5 bits */
|
|
|
|
code_len = p->value; /* code_len == 0..17 */
|
2016-06-03 08:51:04 +00:00
|
|
|
if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) {
|
2015-11-06 10:46:04 +00:00
|
|
|
ProcessSingleCodeLength(code_len, &symbol, &repeat, &space,
|
|
|
|
&prev_code_len, symbol_lists, code_length_histo, next_symbol);
|
|
|
|
} else { /* code_len == 16..17, extra_bits == 2..3 */
|
2016-06-03 08:51:04 +00:00
|
|
|
uint32_t extra_bits =
|
|
|
|
(code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) ? 2 : 3;
|
2015-11-06 10:46:04 +00:00
|
|
|
uint32_t repeat_delta =
|
2016-06-03 08:51:04 +00:00
|
|
|
(uint32_t)BrotliGetBitsUnmasked(br) & BitMask(extra_bits);
|
|
|
|
BrotliDropBits(br, extra_bits);
|
2015-11-06 10:46:04 +00:00
|
|
|
ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size,
|
|
|
|
&symbol, &repeat, &space, &prev_code_len, &repeat_code_len,
|
|
|
|
symbol_lists, code_length_histo, next_symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->space = space;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static BrotliDecoderErrorCode SafeReadSymbolCodeLengths(
|
|
|
|
uint32_t alphabet_size, BrotliDecoderState* s) {
|
2015-11-06 10:46:04 +00:00
|
|
|
BrotliBitReader* br = &s->br;
|
|
|
|
while (s->symbol < alphabet_size && s->space > 0) {
|
|
|
|
const HuffmanCode* p = s->table;
|
|
|
|
uint32_t code_len;
|
|
|
|
uint32_t bits = 0;
|
|
|
|
uint32_t available_bits = BrotliGetAvailableBits(br);
|
|
|
|
if (available_bits != 0) {
|
|
|
|
bits = (uint32_t)BrotliGetBitsUnmasked(br);
|
|
|
|
}
|
|
|
|
p += bits & BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH);
|
|
|
|
if (p->bits > available_bits) goto pullMoreInput;
|
|
|
|
code_len = p->value; /* code_len == 0..17 */
|
2016-06-03 08:51:04 +00:00
|
|
|
if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) {
|
2015-11-06 10:46:04 +00:00
|
|
|
BrotliDropBits(br, p->bits);
|
|
|
|
ProcessSingleCodeLength(code_len, &s->symbol, &s->repeat, &s->space,
|
|
|
|
&s->prev_code_len, s->symbol_lists, s->code_length_histo,
|
|
|
|
s->next_symbol);
|
|
|
|
} else { /* code_len == 16..17, extra_bits == 2..3 */
|
|
|
|
uint32_t extra_bits = code_len - 14U;
|
|
|
|
uint32_t repeat_delta = (bits >> p->bits) & BitMask(extra_bits);
|
|
|
|
if (available_bits < p->bits + extra_bits) goto pullMoreInput;
|
|
|
|
BrotliDropBits(br, p->bits + extra_bits);
|
|
|
|
ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size,
|
|
|
|
&s->symbol, &s->repeat, &s->space, &s->prev_code_len,
|
|
|
|
&s->repeat_code_len, s->symbol_lists, s->code_length_histo,
|
|
|
|
s->next_symbol);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
pullMoreInput:
|
|
|
|
if (!BrotliPullByte(br)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Reads and decodes 15..18 codes using static prefix code.
|
|
|
|
Each code is 2..4 bits long. In total 30..72 bits are used. */
|
2016-07-25 08:17:04 +00:00
|
|
|
static BrotliDecoderErrorCode ReadCodeLengthCodeLengths(BrotliDecoderState* s) {
|
2015-11-06 10:46:04 +00:00
|
|
|
BrotliBitReader* br = &s->br;
|
|
|
|
uint32_t num_codes = s->repeat;
|
|
|
|
unsigned space = s->space;
|
|
|
|
uint32_t i = s->sub_loop_counter;
|
2016-06-03 08:51:04 +00:00
|
|
|
for (; i < BROTLI_CODE_LENGTH_CODES; ++i) {
|
2015-11-06 10:46:04 +00:00
|
|
|
const uint8_t code_len_idx = kCodeLengthCodeOrder[i];
|
|
|
|
uint32_t ix;
|
|
|
|
uint32_t v;
|
|
|
|
if (PREDICT_FALSE(!BrotliSafeGetBits(br, 4, &ix))) {
|
|
|
|
uint32_t available_bits = BrotliGetAvailableBits(br);
|
|
|
|
if (available_bits != 0) {
|
|
|
|
ix = BrotliGetBitsUnmasked(br) & 0xF;
|
|
|
|
} else {
|
|
|
|
ix = 0;
|
|
|
|
}
|
|
|
|
if (kCodeLengthPrefixLength[ix] > available_bits) {
|
|
|
|
s->sub_loop_counter = i;
|
|
|
|
s->repeat = num_codes;
|
|
|
|
s->space = space;
|
|
|
|
s->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
v = kCodeLengthPrefixValue[ix];
|
|
|
|
BrotliDropBits(br, kCodeLengthPrefixLength[ix]);
|
|
|
|
s->code_length_code_lengths[code_len_idx] = (uint8_t)v;
|
|
|
|
BROTLI_LOG_ARRAY_INDEX(s->code_length_code_lengths, code_len_idx);
|
|
|
|
if (v != 0) {
|
|
|
|
space = space - (32U >> v);
|
|
|
|
++num_codes;
|
|
|
|
++s->code_length_histo[v];
|
|
|
|
if (space - 1U >= 32U) {
|
|
|
|
/* space is 0 or wrapped around */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(num_codes == 1 || space == 0)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_CL_SPACE);
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 10:24:06 +00:00
|
|
|
/* Decodes the Huffman tables.
|
|
|
|
There are 2 scenarios:
|
2015-09-24 16:35:25 +00:00
|
|
|
A) Huffman code contains only few symbols (1..4). Those symbols are read
|
2015-09-23 10:24:06 +00:00
|
|
|
directly; their code lengths are defined by the number of symbols.
|
2015-09-24 16:35:25 +00:00
|
|
|
For this scenario 4 - 45 bits will be read.
|
2015-09-23 10:24:06 +00:00
|
|
|
|
|
|
|
B) 2-phase decoding:
|
|
|
|
B.1) Small Huffman table is decoded; it is specified with code lengths
|
2015-09-24 16:35:25 +00:00
|
|
|
encoded with predefined entropy code. 32 - 74 bits are used.
|
2015-09-23 10:24:06 +00:00
|
|
|
B.2) Decoded table is used to decode code lengths of symbols in resulting
|
|
|
|
Huffman table. In worst case 3520 bits are read.
|
|
|
|
*/
|
2016-07-25 08:17:04 +00:00
|
|
|
static BrotliDecoderErrorCode ReadHuffmanCode(uint32_t alphabet_size,
|
|
|
|
HuffmanCode* table,
|
|
|
|
uint32_t* opt_table_size,
|
|
|
|
BrotliDecoderState* s) {
|
2015-03-20 15:13:15 +00:00
|
|
|
BrotliBitReader* br = &s->br;
|
2015-08-10 14:39:50 +00:00
|
|
|
/* Unnecessary masking, but might be good for safety. */
|
|
|
|
alphabet_size &= 0x3ff;
|
2015-03-20 15:13:15 +00:00
|
|
|
/* State machine */
|
2015-09-24 16:35:25 +00:00
|
|
|
switch (s->substate_huffman) {
|
|
|
|
case BROTLI_STATE_HUFFMAN_NONE:
|
2015-11-06 10:46:04 +00:00
|
|
|
if (!BrotliSafeReadBits(br, 2, &s->sub_loop_counter)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-08-28 13:20:24 +00:00
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
BROTLI_LOG_UINT(s->sub_loop_counter);
|
2015-09-24 16:35:25 +00:00
|
|
|
/* The value is used as follows:
|
|
|
|
1 for simple code;
|
|
|
|
0 for no skipping, 2 skips 2 code lengths, 3 skips 3 code lengths */
|
2015-11-06 10:46:04 +00:00
|
|
|
if (s->sub_loop_counter != 1) {
|
|
|
|
s->space = 32;
|
|
|
|
s->repeat = 0; /* num_codes */
|
2015-09-24 16:35:25 +00:00
|
|
|
memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo[0]) *
|
|
|
|
(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1));
|
|
|
|
memset(&s->code_length_code_lengths[0], 0,
|
2015-11-06 10:46:04 +00:00
|
|
|
sizeof(s->code_length_code_lengths));
|
|
|
|
s->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX;
|
|
|
|
goto Complex;
|
|
|
|
}
|
|
|
|
/* No break, transit to the next state. */
|
|
|
|
|
|
|
|
case BROTLI_STATE_HUFFMAN_SIMPLE_SIZE:
|
|
|
|
/* Read symbols, codes & code lengths directly. */
|
|
|
|
if (!BrotliSafeReadBits(br, 2, &s->symbol)) { /* num_symbols */
|
|
|
|
s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_SIZE;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
s->sub_loop_counter = 0;
|
|
|
|
/* No break, transit to the next state. */
|
|
|
|
case BROTLI_STATE_HUFFMAN_SIMPLE_READ: {
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderErrorCode result =
|
|
|
|
ReadSimpleHuffmanSymbols(alphabet_size, s);
|
|
|
|
if (result != BROTLI_DECODER_SUCCESS) {
|
2015-11-06 10:46:04 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
/* No break, transit to the next state. */
|
|
|
|
}
|
|
|
|
case BROTLI_STATE_HUFFMAN_SIMPLE_BUILD: {
|
|
|
|
uint32_t table_size;
|
|
|
|
if (s->symbol == 3) {
|
|
|
|
uint32_t bits;
|
|
|
|
if (!BrotliSafeReadBits(br, 1, &bits)) {
|
|
|
|
s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_BUILD;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-09-24 16:35:25 +00:00
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
s->symbol += bits;
|
|
|
|
}
|
|
|
|
BROTLI_LOG_UINT(s->symbol);
|
|
|
|
table_size = BrotliBuildSimpleHuffmanTable(
|
|
|
|
table, HUFFMAN_TABLE_BITS, s->symbols_lists_array, s->symbol);
|
|
|
|
if (opt_table_size) {
|
|
|
|
*opt_table_size = table_size;
|
|
|
|
}
|
|
|
|
s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Complex: /* Decode Huffman-coded code lengths. */
|
|
|
|
case BROTLI_STATE_HUFFMAN_COMPLEX: {
|
|
|
|
uint32_t i;
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderErrorCode result = ReadCodeLengthCodeLengths(s);
|
|
|
|
if (result != BROTLI_DECODER_SUCCESS) {
|
2015-11-06 10:46:04 +00:00
|
|
|
return result;
|
2015-08-28 13:20:24 +00:00
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
BrotliBuildCodeLengthsHuffmanTable(s->table,
|
|
|
|
s->code_length_code_lengths,
|
|
|
|
s->code_length_histo);
|
|
|
|
memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo));
|
|
|
|
for (i = 0; i <= BROTLI_HUFFMAN_MAX_CODE_LENGTH; ++i) {
|
2015-10-05 08:23:32 +00:00
|
|
|
s->next_symbol[i] = (int)i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
|
|
|
|
s->symbol_lists[(int)i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1)] = 0xFFFF;
|
2015-08-28 13:20:24 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 16:35:25 +00:00
|
|
|
s->symbol = 0;
|
2016-06-03 08:51:04 +00:00
|
|
|
s->prev_code_len = BROTLI_INITIAL_REPEATED_CODE_LENGTH;
|
2015-09-24 16:35:25 +00:00
|
|
|
s->repeat = 0;
|
|
|
|
s->repeat_code_len = 0;
|
|
|
|
s->space = 32768;
|
|
|
|
s->substate_huffman = BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS;
|
|
|
|
/* No break, transit to the next state. */
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
case BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS: {
|
2015-11-06 10:46:04 +00:00
|
|
|
uint32_t table_size;
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderErrorCode result = ReadSymbolCodeLengths(alphabet_size, s);
|
|
|
|
if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) {
|
2015-11-06 10:46:04 +00:00
|
|
|
result = SafeReadSymbolCodeLengths(alphabet_size, s);
|
|
|
|
}
|
2016-07-25 08:17:04 +00:00
|
|
|
if (result != BROTLI_DECODER_SUCCESS) {
|
2015-11-06 10:46:04 +00:00
|
|
|
return result;
|
2015-09-24 16:35:25 +00:00
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
|
|
|
|
if (s->space != 0) {
|
|
|
|
BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", s->space));
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE);
|
2015-09-24 16:35:25 +00:00
|
|
|
}
|
2016-02-18 14:03:44 +00:00
|
|
|
table_size = BrotliBuildHuffmanTable(
|
|
|
|
table, HUFFMAN_TABLE_BITS, s->symbol_lists, s->code_length_histo);
|
2015-11-06 10:46:04 +00:00
|
|
|
if (opt_table_size) {
|
|
|
|
*opt_table_size = table_size;
|
2015-08-28 13:20:24 +00:00
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2015-08-28 13:20:24 +00:00
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
|
|
|
|
default:
|
2016-07-25 08:17:04 +00:00
|
|
|
return
|
|
|
|
BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE);
|
2015-08-28 13:20:24 +00:00
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 10:24:06 +00:00
|
|
|
/* Decodes a block length by reading 3..39 bits. */
|
2015-11-06 10:46:04 +00:00
|
|
|
static BROTLI_INLINE uint32_t ReadBlockLength(const HuffmanCode* table,
|
|
|
|
BrotliBitReader* br) {
|
2015-10-05 08:23:32 +00:00
|
|
|
uint32_t code;
|
|
|
|
uint32_t nbits;
|
2014-02-14 14:04:23 +00:00
|
|
|
code = ReadSymbol(table, br);
|
2015-09-23 10:24:06 +00:00
|
|
|
nbits = kBlockLengthPrefixCode[code].nbits; /* nbits == 2..24 */
|
2015-11-06 10:46:04 +00:00
|
|
|
return kBlockLengthPrefixCode[code].offset + BrotliReadBits(br, nbits);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* WARNING: if state is not BROTLI_STATE_READ_BLOCK_LENGTH_NONE, then
|
|
|
|
reading can't be continued with ReadBlockLength. */
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_INLINE BROTLI_BOOL SafeReadBlockLength(
|
|
|
|
BrotliDecoderState* s, uint32_t* result, const HuffmanCode* table,
|
|
|
|
BrotliBitReader* br) {
|
2015-11-06 10:46:04 +00:00
|
|
|
uint32_t index;
|
|
|
|
if (s->substate_read_block_length == BROTLI_STATE_READ_BLOCK_LENGTH_NONE) {
|
|
|
|
if (!SafeReadSymbol(table, br, &index)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FALSE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
index = s->block_length_index;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
uint32_t bits;
|
|
|
|
uint32_t nbits = kBlockLengthPrefixCode[index].nbits; /* nbits == 2..24 */
|
|
|
|
if (!BrotliSafeReadBits(br, nbits, &bits)) {
|
|
|
|
s->block_length_index = index;
|
|
|
|
s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FALSE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
*result = kBlockLengthPrefixCode[index].offset + bits;
|
|
|
|
s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_TRUE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
/* Transform:
|
|
|
|
1) initialize list L with values 0, 1,... 255
|
|
|
|
2) For each input element X:
|
|
|
|
2.1) let Y = L[X]
|
|
|
|
2.2) remove X-th element from L
|
|
|
|
2.3) prepend Y to L
|
|
|
|
2.4) append Y to output
|
|
|
|
|
|
|
|
In most cases max(Y) <= 7, so most of L remains intact.
|
|
|
|
To reduce the cost of initialization, we reuse L, remember the upper bound
|
|
|
|
of Y values, and reinitialize only first elements in L.
|
|
|
|
|
2015-09-21 19:04:07 +00:00
|
|
|
Most of input values are 0 and 1. To reduce number of branches, we replace
|
2015-08-10 11:35:23 +00:00
|
|
|
inner for loop with do-while.
|
|
|
|
*/
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_NOINLINE void InverseMoveToFrontTransform(
|
|
|
|
uint8_t* v, uint32_t v_len, BrotliDecoderState* state) {
|
2015-08-10 11:35:23 +00:00
|
|
|
/* Reinitialize elements that could have been changed. */
|
2015-10-05 08:23:32 +00:00
|
|
|
uint32_t i = 4;
|
|
|
|
uint32_t upper_bound = state->mtf_upper_bound;
|
2016-04-29 11:44:16 +00:00
|
|
|
uint8_t* mtf = &state->mtf[4]; /* Make mtf[-1] addressable. */
|
2015-08-10 11:35:23 +00:00
|
|
|
/* Load endian-aware constant. */
|
|
|
|
const uint8_t b0123[4] = {0, 1, 2, 3};
|
|
|
|
uint32_t pattern;
|
|
|
|
memcpy(&pattern, &b0123, 4);
|
|
|
|
|
|
|
|
/* Initialize list using 4 consequent values pattern. */
|
|
|
|
*(uint32_t*)mtf = pattern;
|
|
|
|
do {
|
|
|
|
pattern += 0x04040404; /* Advance all 4 values by 4. */
|
|
|
|
*(uint32_t*)(mtf + i) = pattern;
|
|
|
|
i += 4;
|
|
|
|
} while (i <= upper_bound);
|
|
|
|
|
|
|
|
/* Transform the input. */
|
|
|
|
upper_bound = 0;
|
2013-10-11 08:26:07 +00:00
|
|
|
for (i = 0; i < v_len; ++i) {
|
2015-08-10 11:35:23 +00:00
|
|
|
int index = v[i];
|
2015-04-22 12:25:08 +00:00
|
|
|
uint8_t value = mtf[index];
|
2015-10-05 08:23:32 +00:00
|
|
|
upper_bound |= v[i];
|
2015-04-22 12:25:08 +00:00
|
|
|
v[i] = value;
|
2016-04-29 11:44:16 +00:00
|
|
|
mtf[-1] = value;
|
2015-08-10 11:35:23 +00:00
|
|
|
do {
|
|
|
|
index--;
|
|
|
|
mtf[index + 1] = mtf[index];
|
2016-04-29 11:44:16 +00:00
|
|
|
} while (index >= 0);
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
/* Remember amount of elements to be reinitialized. */
|
|
|
|
state->mtf_upper_bound = upper_bound;
|
|
|
|
}
|
|
|
|
|
2015-09-23 10:24:06 +00:00
|
|
|
/* Decodes a series of Huffman table using ReadHuffmanCode function. */
|
2016-07-25 08:17:04 +00:00
|
|
|
static BrotliDecoderErrorCode HuffmanTreeGroupDecode(
|
|
|
|
HuffmanTreeGroup* group, BrotliDecoderState* s) {
|
2015-09-23 10:24:06 +00:00
|
|
|
if (s->substate_tree_group != BROTLI_STATE_TREE_GROUP_LOOP) {
|
2015-08-28 13:20:24 +00:00
|
|
|
s->next = group->codes;
|
|
|
|
s->htree_index = 0;
|
2015-09-23 10:24:06 +00:00
|
|
|
s->substate_tree_group = BROTLI_STATE_TREE_GROUP_LOOP;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
while (s->htree_index < group->num_htrees) {
|
2015-10-05 08:23:32 +00:00
|
|
|
uint32_t table_size;
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderErrorCode result =
|
2015-08-28 13:20:24 +00:00
|
|
|
ReadHuffmanCode(group->alphabet_size, s->next, &table_size, s);
|
2016-07-25 08:17:04 +00:00
|
|
|
if (result != BROTLI_DECODER_SUCCESS) return result;
|
2015-08-28 13:20:24 +00:00
|
|
|
group->htrees[s->htree_index] = s->next;
|
|
|
|
s->next += table_size;
|
|
|
|
++s->htree_index;
|
|
|
|
}
|
2015-09-23 10:24:06 +00:00
|
|
|
s->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 10:24:06 +00:00
|
|
|
/* Decodes a context map.
|
|
|
|
Decoding is done in 4 phases:
|
|
|
|
1) Read auxiliary information (6..16 bits) and allocate memory.
|
|
|
|
In case of trivial context map, decoding is finished at this phase.
|
|
|
|
2) Decode Huffman table using ReadHuffmanCode function.
|
|
|
|
This table will be used for reading context map items.
|
|
|
|
3) Read context map items; "0" values could be run-length encoded.
|
|
|
|
4) Optionally, apply InverseMoveToFront transform to the resulting map.
|
|
|
|
*/
|
2016-07-25 08:17:04 +00:00
|
|
|
static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size,
|
|
|
|
uint32_t* num_htrees,
|
|
|
|
uint8_t** context_map_arg,
|
|
|
|
BrotliState* s) {
|
2015-03-20 15:13:15 +00:00
|
|
|
BrotliBitReader* br = &s->br;
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS;
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2016-02-18 14:03:44 +00:00
|
|
|
switch ((int)s->substate_context_map) {
|
2015-09-23 10:24:06 +00:00
|
|
|
case BROTLI_STATE_CONTEXT_MAP_NONE:
|
2015-09-24 16:35:25 +00:00
|
|
|
result = DecodeVarLenUint8(s, br, num_htrees);
|
2016-07-25 08:17:04 +00:00
|
|
|
if (result != BROTLI_DECODER_SUCCESS) {
|
2015-09-24 16:35:25 +00:00
|
|
|
return result;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
(*num_htrees)++;
|
2015-03-20 15:13:15 +00:00
|
|
|
s->context_index = 0;
|
|
|
|
BROTLI_LOG_UINT(context_map_size);
|
|
|
|
BROTLI_LOG_UINT(*num_htrees);
|
2015-11-23 09:21:09 +00:00
|
|
|
*context_map_arg = (uint8_t*)BROTLI_ALLOC(s, (size_t)context_map_size);
|
2015-08-28 13:20:24 +00:00
|
|
|
if (*context_map_arg == 0) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP);
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
if (*num_htrees <= 1) {
|
2015-08-28 13:20:24 +00:00
|
|
|
memset(*context_map_arg, 0, (size_t)context_map_size);
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_READ_PREFIX;
|
|
|
|
/* No break, continue to next state. */
|
2015-11-06 10:46:04 +00:00
|
|
|
case BROTLI_STATE_CONTEXT_MAP_READ_PREFIX: {
|
|
|
|
uint32_t bits;
|
|
|
|
/* In next stage ReadHuffmanCode uses at least 4 bits, so it is safe
|
|
|
|
to peek 4 bits ahead. */
|
|
|
|
if (!BrotliSafeGetBits(br, 5, &bits)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-09-24 16:35:25 +00:00
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
if ((bits & 1) != 0) { /* Use RLE for zeroes. */
|
|
|
|
s->max_run_length_prefix = (bits >> 1) + 1;
|
|
|
|
BrotliDropBits(br, 5);
|
2015-03-30 16:00:40 +00:00
|
|
|
} else {
|
|
|
|
s->max_run_length_prefix = 0;
|
2015-11-06 10:46:04 +00:00
|
|
|
BrotliDropBits(br, 1);
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
BROTLI_LOG_UINT(s->max_run_length_prefix);
|
2015-09-23 10:24:06 +00:00
|
|
|
s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_HUFFMAN;
|
2015-03-20 15:13:15 +00:00
|
|
|
/* No break, continue to next state. */
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
2015-09-23 10:24:06 +00:00
|
|
|
case BROTLI_STATE_CONTEXT_MAP_HUFFMAN:
|
2015-03-30 16:00:40 +00:00
|
|
|
result = ReadHuffmanCode(*num_htrees + s->max_run_length_prefix,
|
|
|
|
s->context_map_table, NULL, s);
|
2016-07-25 08:17:04 +00:00
|
|
|
if (result != BROTLI_DECODER_SUCCESS) return result;
|
2015-11-06 10:46:04 +00:00
|
|
|
s->code = 0xFFFF;
|
2015-09-23 10:24:06 +00:00
|
|
|
s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_DECODE;
|
2015-03-20 15:13:15 +00:00
|
|
|
/* No break, continue to next state. */
|
2015-09-23 10:24:06 +00:00
|
|
|
case BROTLI_STATE_CONTEXT_MAP_DECODE: {
|
2015-10-05 08:23:32 +00:00
|
|
|
uint32_t context_index = s->context_index;
|
|
|
|
uint32_t max_run_length_prefix = s->max_run_length_prefix;
|
2015-08-28 13:20:24 +00:00
|
|
|
uint8_t* context_map = *context_map_arg;
|
2015-11-06 10:46:04 +00:00
|
|
|
uint32_t code = s->code;
|
|
|
|
if (code != 0xFFFF) {
|
|
|
|
goto rleCode;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
while (context_index < context_map_size) {
|
2015-11-06 10:46:04 +00:00
|
|
|
if (!SafeReadSymbol(s->context_map_table, br, &code)) {
|
|
|
|
s->code = 0xFFFF;
|
2015-08-10 11:35:23 +00:00
|
|
|
s->context_index = context_index;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
BROTLI_LOG_UINT(code);
|
2015-11-06 10:46:04 +00:00
|
|
|
|
2015-03-20 15:13:15 +00:00
|
|
|
if (code == 0) {
|
2015-08-28 13:20:24 +00:00
|
|
|
context_map[context_index++] = 0;
|
2015-11-06 10:46:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (code > max_run_length_prefix) {
|
|
|
|
context_map[context_index++] =
|
|
|
|
(uint8_t)(code - max_run_length_prefix);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rleCode:
|
|
|
|
{
|
|
|
|
uint32_t reps;
|
|
|
|
if (!BrotliSafeReadBits(br, code, &reps)) {
|
|
|
|
s->code = code;
|
|
|
|
s->context_index = context_index;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
reps += 1U << code;
|
2015-09-24 16:35:25 +00:00
|
|
|
BROTLI_LOG_UINT(reps);
|
2015-08-28 13:20:24 +00:00
|
|
|
if (context_index + reps > context_map_size) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return
|
|
|
|
BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT);
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
do {
|
|
|
|
context_map[context_index++] = 0;
|
|
|
|
} while (--reps);
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
/* No break, continue to next state. */
|
|
|
|
}
|
|
|
|
case BROTLI_STATE_CONTEXT_MAP_TRANSFORM: {
|
|
|
|
uint32_t bits;
|
|
|
|
if (!BrotliSafeReadBits(br, 1, &bits)) {
|
|
|
|
s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_TRANSFORM;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
if (bits != 0) {
|
|
|
|
InverseMoveToFrontTransform(*context_map_arg, context_map_size, s);
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-09-23 10:24:06 +00:00
|
|
|
s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
2016-01-22 09:19:41 +00:00
|
|
|
default:
|
2016-07-25 08:17:04 +00:00
|
|
|
return
|
|
|
|
BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE);
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 10:24:06 +00:00
|
|
|
/* Decodes a command or literal and updates block type ringbuffer.
|
2015-11-06 10:46:04 +00:00
|
|
|
Reads 3..54 bits. */
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength(
|
|
|
|
int safe, BrotliDecoderState* s, int tree_type) {
|
2015-11-06 10:46:04 +00:00
|
|
|
uint32_t max_block_type = s->num_block_types[tree_type];
|
2016-01-22 09:19:41 +00:00
|
|
|
const HuffmanCode* type_tree = &s->block_type_trees[
|
|
|
|
tree_type * BROTLI_HUFFMAN_MAX_SIZE_258];
|
|
|
|
const HuffmanCode* len_tree = &s->block_len_trees[
|
|
|
|
tree_type * BROTLI_HUFFMAN_MAX_SIZE_26];
|
2015-11-06 10:46:04 +00:00
|
|
|
BrotliBitReader* br = &s->br;
|
|
|
|
uint32_t* ringbuffer = &s->block_type_rb[tree_type * 2];
|
|
|
|
uint32_t block_type;
|
|
|
|
|
|
|
|
/* Read 0..15 + 3..39 bits */
|
|
|
|
if (!safe) {
|
|
|
|
block_type = ReadSymbol(type_tree, br);
|
|
|
|
s->block_length[tree_type] = ReadBlockLength(len_tree, br);
|
|
|
|
} else {
|
|
|
|
BrotliBitReaderState memento;
|
|
|
|
BrotliBitReaderSaveState(br, &memento);
|
2016-07-25 08:17:04 +00:00
|
|
|
if (!SafeReadSymbol(type_tree, br, &block_type)) return BROTLI_FALSE;
|
2015-11-06 10:46:04 +00:00
|
|
|
if (!SafeReadBlockLength(s, &s->block_length[tree_type], len_tree, br)) {
|
|
|
|
s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
|
|
|
|
BrotliBitReaderRestoreState(br, &memento);
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FALSE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 08:23:32 +00:00
|
|
|
if (block_type == 1) {
|
2015-08-10 11:35:23 +00:00
|
|
|
block_type = ringbuffer[1] + 1;
|
2015-10-05 08:23:32 +00:00
|
|
|
} else if (block_type == 0) {
|
2015-08-10 11:35:23 +00:00
|
|
|
block_type = ringbuffer[0];
|
2015-10-05 08:23:32 +00:00
|
|
|
} else {
|
|
|
|
block_type -= 2;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2014-01-06 15:01:57 +00:00
|
|
|
if (block_type >= max_block_type) {
|
|
|
|
block_type -= max_block_type;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
ringbuffer[0] = ringbuffer[1];
|
|
|
|
ringbuffer[1] = block_type;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_TRUE;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_INLINE void DetectTrivialLiteralBlockTypes(
|
|
|
|
BrotliDecoderState* s) {
|
2016-05-31 13:40:59 +00:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < 8; ++i) s->trivial_literal_contexts[i] = 0;
|
|
|
|
for (i = 0; i < s->num_block_types[0]; i++) {
|
2016-06-03 08:51:04 +00:00
|
|
|
size_t offset = i << BROTLI_LITERAL_CONTEXT_BITS;
|
2016-05-31 13:40:59 +00:00
|
|
|
size_t error = 0;
|
|
|
|
size_t sample = s->context_map[offset];
|
|
|
|
size_t j;
|
2016-06-03 08:51:04 +00:00
|
|
|
for (j = 0; j < (1u << BROTLI_LITERAL_CONTEXT_BITS);) {
|
2016-05-31 13:40:59 +00:00
|
|
|
BROTLI_REPEAT(4, error |= s->context_map[offset + j++] ^ sample;)
|
|
|
|
}
|
|
|
|
if (error == 0) {
|
|
|
|
s->trivial_literal_contexts[i >> 5] |= 1u << (i & 31);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_INLINE void PrepareLiteralDecoding(BrotliDecoderState* s) {
|
2016-05-31 13:40:59 +00:00
|
|
|
uint8_t context_mode;
|
|
|
|
size_t trivial;
|
|
|
|
uint32_t block_type = s->block_type_rb[1];
|
2016-06-03 08:51:04 +00:00
|
|
|
uint32_t context_offset = block_type << BROTLI_LITERAL_CONTEXT_BITS;
|
2016-05-31 13:40:59 +00:00
|
|
|
s->context_map_slice = s->context_map + context_offset;
|
|
|
|
trivial = s->trivial_literal_contexts[block_type >> 5];
|
|
|
|
s->trivial_literal_context = (trivial >> (block_type & 31)) & 1;
|
|
|
|
s->literal_htree = s->literal_hgroup.htrees[s->context_map_slice[0]];
|
|
|
|
context_mode = s->context_modes[block_type];
|
|
|
|
s->context_lookup1 = &kContextLookup[kContextLookupOffsets[context_mode]];
|
|
|
|
s->context_lookup2 = &kContextLookup[kContextLookupOffsets[context_mode + 1]];
|
|
|
|
}
|
|
|
|
|
2015-09-23 10:24:06 +00:00
|
|
|
/* Decodes the block type and updates the state for literal context.
|
2015-11-06 10:46:04 +00:00
|
|
|
Reads 3..54 bits. */
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_INLINE BROTLI_BOOL DecodeLiteralBlockSwitchInternal(
|
|
|
|
int safe, BrotliDecoderState* s) {
|
2015-11-06 10:46:04 +00:00
|
|
|
if (!DecodeBlockTypeAndLength(safe, s, 0)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FALSE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
2016-05-31 13:40:59 +00:00
|
|
|
PrepareLiteralDecoding(s);
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_TRUE;
|
2015-04-01 14:15:39 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static void BROTLI_NOINLINE DecodeLiteralBlockSwitch(BrotliDecoderState* s) {
|
2015-11-06 10:46:04 +00:00
|
|
|
DecodeLiteralBlockSwitchInternal(0, s);
|
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeLiteralBlockSwitch(
|
|
|
|
BrotliDecoderState* s) {
|
2015-11-06 10:46:04 +00:00
|
|
|
return DecodeLiteralBlockSwitchInternal(1, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Block switch for insert/copy length.
|
|
|
|
Reads 3..54 bits. */
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_INLINE BROTLI_BOOL DecodeCommandBlockSwitchInternal(
|
|
|
|
int safe, BrotliDecoderState* s) {
|
2015-11-06 10:46:04 +00:00
|
|
|
if (!DecodeBlockTypeAndLength(safe, s, 1)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FALSE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
s->htree_command = s->insert_copy_hgroup.htrees[s->block_type_rb[3]];
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_TRUE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static void BROTLI_NOINLINE DecodeCommandBlockSwitch(BrotliDecoderState* s) {
|
2015-11-06 10:46:04 +00:00
|
|
|
DecodeCommandBlockSwitchInternal(0, s);
|
|
|
|
}
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeCommandBlockSwitch(
|
|
|
|
BrotliDecoderState* s) {
|
2015-11-06 10:46:04 +00:00
|
|
|
return DecodeCommandBlockSwitchInternal(1, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Block switch for distance codes.
|
|
|
|
Reads 3..54 bits. */
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_INLINE BROTLI_BOOL DecodeDistanceBlockSwitchInternal(
|
|
|
|
int safe, BrotliDecoderState* s) {
|
2015-11-06 10:46:04 +00:00
|
|
|
if (!DecodeBlockTypeAndLength(safe, s, 2)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FALSE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
2016-06-03 08:51:04 +00:00
|
|
|
s->dist_context_map_slice = s->dist_context_map +
|
|
|
|
(s->block_type_rb[5] << BROTLI_DISTANCE_CONTEXT_BITS);
|
2015-11-06 10:46:04 +00:00
|
|
|
s->dist_htree_index = s->dist_context_map_slice[s->distance_context];
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_TRUE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static void BROTLI_NOINLINE DecodeDistanceBlockSwitch(BrotliDecoderState* s) {
|
2015-11-06 10:46:04 +00:00
|
|
|
DecodeDistanceBlockSwitchInternal(0, s);
|
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeDistanceBlockSwitch(
|
|
|
|
BrotliDecoderState* s) {
|
2015-11-06 10:46:04 +00:00
|
|
|
return DecodeDistanceBlockSwitchInternal(1, s);
|
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static size_t UnwrittenBytes(const BrotliDecoderState* s, BROTLI_BOOL wrap) {
|
|
|
|
size_t pos = wrap && s->pos > s->ringbuffer_size ?
|
|
|
|
(size_t)s->ringbuffer_size : (size_t)(s->pos);
|
|
|
|
size_t partial_pos_rb = (s->rb_roundtrips * (size_t)s->ringbuffer_size) + pos;
|
|
|
|
return partial_pos_rb - s->partial_pos_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BrotliDecoderErrorCode BROTLI_NOINLINE WriteRingBuffer(
|
|
|
|
BrotliDecoderState* s, size_t* available_out, uint8_t** next_out,
|
|
|
|
size_t* total_out) {
|
2016-02-18 14:03:44 +00:00
|
|
|
uint8_t* start =
|
|
|
|
s->ringbuffer + (s->partial_pos_out & (size_t)s->ringbuffer_mask);
|
2016-07-25 08:17:04 +00:00
|
|
|
size_t to_write = UnwrittenBytes(s, BROTLI_TRUE);
|
2015-11-06 10:46:04 +00:00
|
|
|
size_t num_written = *available_out;
|
|
|
|
if (num_written > to_write) {
|
|
|
|
num_written = to_write;
|
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
if (s->meta_block_remaining_len < 0) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1);
|
2015-09-24 16:35:25 +00:00
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
memcpy(*next_out, start, num_written);
|
|
|
|
*next_out += num_written;
|
|
|
|
*available_out -= num_written;
|
|
|
|
BROTLI_LOG_UINT(to_write);
|
2015-09-24 16:35:25 +00:00
|
|
|
BROTLI_LOG_UINT(num_written);
|
2016-01-22 09:19:41 +00:00
|
|
|
s->partial_pos_out += num_written;
|
2016-05-31 13:40:59 +00:00
|
|
|
if (total_out) *total_out = s->partial_pos_out;
|
2015-11-06 10:46:04 +00:00
|
|
|
if (num_written < to_write) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_OUTPUT;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2016-05-11 13:23:07 +00:00
|
|
|
|
|
|
|
if (s->pos >= s->ringbuffer_size) {
|
|
|
|
s->pos -= s->ringbuffer_size;
|
|
|
|
s->rb_roundtrips++;
|
|
|
|
}
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 09:19:41 +00:00
|
|
|
/* Allocates ringbuffer.
|
|
|
|
|
|
|
|
s->ringbuffer_size MUST be updated by BrotliCalculateRingBufferSize before
|
|
|
|
this function is called.
|
|
|
|
|
|
|
|
Last two bytes of ringbuffer are initialized to 0, so context calculation
|
|
|
|
could be done uniformly for the first two and all other positions.
|
|
|
|
|
|
|
|
Custom dictionary, if any, is copied to the end of ringbuffer.
|
|
|
|
*/
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_BOOL BROTLI_NOINLINE BrotliAllocateRingBuffer(
|
|
|
|
BrotliDecoderState* s) {
|
2016-01-22 09:19:41 +00:00
|
|
|
/* We need the slack region for the following reasons:
|
|
|
|
- doing up to two 16-byte copies for fast backward copying
|
|
|
|
- inserting transformed dictionary word (5 prefix + 24 base + 8 suffix) */
|
|
|
|
static const int kRingBufferWriteAheadSlack = 42;
|
|
|
|
s->ringbuffer = (uint8_t*)BROTLI_ALLOC(s, (size_t)(s->ringbuffer_size +
|
|
|
|
kRingBufferWriteAheadSlack));
|
|
|
|
if (s->ringbuffer == 0) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FALSE;
|
2016-01-22 09:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s->ringbuffer_end = s->ringbuffer + s->ringbuffer_size;
|
|
|
|
|
|
|
|
s->ringbuffer[s->ringbuffer_size - 2] = 0;
|
|
|
|
s->ringbuffer[s->ringbuffer_size - 1] = 0;
|
|
|
|
|
|
|
|
if (s->custom_dict) {
|
|
|
|
memcpy(&s->ringbuffer[(-s->custom_dict_size) & s->ringbuffer_mask],
|
2016-02-18 14:03:44 +00:00
|
|
|
s->custom_dict, (size_t)s->custom_dict_size);
|
2016-01-22 09:19:41 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_TRUE;
|
2016-01-22 09:19:41 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static BrotliDecoderErrorCode BROTLI_NOINLINE CopyUncompressedBlockToOutput(
|
2015-11-06 10:46:04 +00:00
|
|
|
size_t* available_out, uint8_t** next_out, size_t* total_out,
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderState* s) {
|
2016-01-22 09:19:41 +00:00
|
|
|
/* TODO: avoid allocation for single uncompressed block. */
|
|
|
|
if (!s->ringbuffer && !BrotliAllocateRingBuffer(s)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1);
|
2016-01-22 09:19:41 +00:00
|
|
|
}
|
|
|
|
|
2015-03-20 15:13:15 +00:00
|
|
|
/* State machine */
|
|
|
|
for (;;) {
|
2015-11-06 10:46:04 +00:00
|
|
|
switch (s->substate_uncompressed) {
|
|
|
|
case BROTLI_STATE_UNCOMPRESSED_NONE: {
|
|
|
|
int nbytes = (int)BrotliGetRemainingBytes(&s->br);
|
|
|
|
if (nbytes > s->meta_block_remaining_len) {
|
|
|
|
nbytes = s->meta_block_remaining_len;
|
|
|
|
}
|
|
|
|
if (s->pos + nbytes > s->ringbuffer_size) {
|
|
|
|
nbytes = s->ringbuffer_size - s->pos;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
/* Copy remaining bytes from s->br.buf_ to ringbuffer. */
|
2015-11-06 10:46:04 +00:00
|
|
|
BrotliCopyBytes(&s->ringbuffer[s->pos], &s->br, (size_t)nbytes);
|
|
|
|
s->pos += nbytes;
|
2015-09-28 10:40:39 +00:00
|
|
|
s->meta_block_remaining_len -= nbytes;
|
2015-11-06 10:46:04 +00:00
|
|
|
if (s->pos < s->ringbuffer_size) {
|
|
|
|
if (s->meta_block_remaining_len == 0) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-04-22 15:33:21 +00:00
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_WRITE;
|
|
|
|
/* No break, continue to next state */
|
|
|
|
}
|
|
|
|
case BROTLI_STATE_UNCOMPRESSED_WRITE: {
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderErrorCode result =
|
|
|
|
WriteRingBuffer(s, available_out, next_out, total_out);
|
|
|
|
if (result != BROTLI_DECODER_SUCCESS) {
|
2015-08-10 11:35:23 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
s->max_distance = s->max_backward_distance;
|
2015-09-23 10:24:06 +00:00
|
|
|
s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE;
|
2015-11-06 10:46:04 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-02-14 14:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-19 14:29:10 +00:00
|
|
|
BROTLI_DCHECK(0); /* Unreachable */
|
2014-02-14 14:04:23 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
BROTLI_BOOL BrotliDecompressedSize(size_t encoded_size,
|
|
|
|
const uint8_t* encoded_buffer,
|
|
|
|
size_t* decoded_size) {
|
2016-06-24 13:32:51 +00:00
|
|
|
size_t total_size = 0;
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderState s;
|
2016-06-24 13:32:51 +00:00
|
|
|
BrotliBitReader* br;
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderStateInit(&s);
|
2016-06-24 13:32:51 +00:00
|
|
|
br = &s.br;
|
|
|
|
*decoded_size = 0;
|
|
|
|
br->next_in = encoded_buffer;
|
|
|
|
br->avail_in = encoded_size;
|
2016-07-25 08:17:04 +00:00
|
|
|
if (!BrotliWarmupBitReader(br)) return BROTLI_FALSE;
|
2016-06-24 13:32:51 +00:00
|
|
|
DecodeWindowBits(br);
|
|
|
|
while (1) {
|
|
|
|
size_t block_size;
|
2016-07-25 08:17:04 +00:00
|
|
|
if (DecodeMetaBlockLength(&s, br) != BROTLI_DECODER_SUCCESS) {
|
|
|
|
return BROTLI_FALSE;
|
|
|
|
}
|
2016-06-24 13:32:51 +00:00
|
|
|
block_size = (size_t)s.meta_block_remaining_len;
|
|
|
|
if (!s.is_metadata) {
|
2016-07-25 08:17:04 +00:00
|
|
|
if ((block_size + total_size) < total_size) return BROTLI_FALSE;
|
2016-06-24 13:32:51 +00:00
|
|
|
total_size += block_size;
|
|
|
|
}
|
|
|
|
if (s.is_last_metablock) {
|
|
|
|
*decoded_size = total_size;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_TRUE;
|
2016-06-24 13:32:51 +00:00
|
|
|
}
|
2016-07-25 08:17:04 +00:00
|
|
|
if (!s.is_uncompressed && !s.is_metadata) return BROTLI_FALSE;
|
|
|
|
if (!BrotliJumpToByteBoundary(br)) return BROTLI_FALSE;
|
2016-06-24 13:32:51 +00:00
|
|
|
BrotliBitReaderUnload(br);
|
2016-07-25 08:17:04 +00:00
|
|
|
if (br->avail_in < block_size) return BROTLI_FALSE;
|
2016-06-24 13:32:51 +00:00
|
|
|
br->avail_in -= block_size;
|
|
|
|
br->next_in += block_size;
|
2016-07-25 08:17:04 +00:00
|
|
|
if (!BrotliWarmupBitReader(br)) return BROTLI_FALSE;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-09-23 10:24:06 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 09:19:41 +00:00
|
|
|
/* Calculates the smallest feasible ring buffer.
|
2015-09-23 10:24:06 +00:00
|
|
|
|
|
|
|
If we know the data size is small, do not allocate more ringbuffer
|
|
|
|
size than needed to reduce memory usage.
|
|
|
|
|
2016-01-22 09:19:41 +00:00
|
|
|
When this method is called, metablock size and flags MUST be decoded.
|
2015-09-23 10:24:06 +00:00
|
|
|
*/
|
2016-07-25 08:17:04 +00:00
|
|
|
static void BROTLI_NOINLINE BrotliCalculateRingBufferSize(
|
|
|
|
BrotliDecoderState* s, BrotliBitReader* br) {
|
|
|
|
BROTLI_BOOL is_last = TO_BROTLI_BOOL(s->is_last_metablock);
|
2016-04-19 14:29:10 +00:00
|
|
|
int window_size = 1 << s->window_bits;
|
|
|
|
s->ringbuffer_size = window_size;
|
2015-09-23 10:24:06 +00:00
|
|
|
|
|
|
|
if (s->is_uncompressed) {
|
2016-02-18 14:03:44 +00:00
|
|
|
int next_block_header =
|
|
|
|
BrotliPeekByte(br, (size_t)s->meta_block_remaining_len);
|
|
|
|
if (next_block_header != -1) { /* Peek succeeded */
|
|
|
|
if ((next_block_header & 3) == 3) { /* ISLAST and ISEMPTY */
|
2016-07-25 08:17:04 +00:00
|
|
|
is_last = BROTLI_TRUE;
|
2015-09-23 10:24:06 +00:00
|
|
|
}
|
2015-05-07 15:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-23 10:24:06 +00:00
|
|
|
|
|
|
|
/* We need at least 2 bytes of ring buffer size to get the last two
|
|
|
|
bytes for context from there */
|
2014-10-15 11:41:00 +00:00
|
|
|
if (is_last) {
|
2016-04-19 14:29:10 +00:00
|
|
|
int min_size_x2 = (s->meta_block_remaining_len + s->custom_dict_size) * 2;
|
|
|
|
while (s->ringbuffer_size >= min_size_x2 && s->ringbuffer_size > 32) {
|
2015-09-23 10:24:06 +00:00
|
|
|
s->ringbuffer_size >>= 1;
|
2014-10-15 11:41:00 +00:00
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-09-23 10:24:06 +00:00
|
|
|
|
|
|
|
s->ringbuffer_mask = s->ringbuffer_size - 1;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 10:46:04 +00:00
|
|
|
/* Reads 1..256 2-bit context modes. */
|
2016-07-25 08:17:04 +00:00
|
|
|
static BrotliDecoderErrorCode ReadContextModes(BrotliDecoderState* s) {
|
2015-11-06 10:46:04 +00:00
|
|
|
BrotliBitReader* br = &s->br;
|
|
|
|
int i = s->loop_counter;
|
2013-11-15 18:02:17 +00:00
|
|
|
|
2015-11-07 10:52:13 +00:00
|
|
|
while (i < (int)s->num_block_types[0]) {
|
2015-11-06 10:46:04 +00:00
|
|
|
uint32_t bits;
|
|
|
|
if (!BrotliSafeReadBits(br, 2, &bits)) {
|
|
|
|
s->loop_counter = i;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
s->context_modes[i] = (uint8_t)(bits << 1);
|
|
|
|
BROTLI_LOG_ARRAY_INDEX(s->context_modes, i);
|
|
|
|
i++;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_DECODER_SUCCESS;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_INLINE void TakeDistanceFromRingBuffer(BrotliDecoderState* s) {
|
2015-11-06 10:46:04 +00:00
|
|
|
if (s->distance_code == 0) {
|
|
|
|
--s->dist_rb_idx;
|
|
|
|
s->distance_code = s->dist_rb[s->dist_rb_idx & 3];
|
|
|
|
} else {
|
|
|
|
int distance_code = s->distance_code << 1;
|
|
|
|
/* kDistanceShortCodeIndexOffset has 2-bit values from LSB: */
|
|
|
|
/* 3, 2, 1, 0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2 */
|
|
|
|
const uint32_t kDistanceShortCodeIndexOffset = 0xaaafff1b;
|
|
|
|
/* kDistanceShortCodeValueOffset has 2-bit values from LSB: */
|
|
|
|
/*-0, 0,-0, 0,-1, 1,-2, 2,-3, 3,-1, 1,-2, 2,-3, 3 */
|
|
|
|
const uint32_t kDistanceShortCodeValueOffset = 0xfa5fa500;
|
|
|
|
int v = (s->dist_rb_idx +
|
|
|
|
(int)(kDistanceShortCodeIndexOffset >> distance_code)) & 0x3;
|
|
|
|
s->distance_code = s->dist_rb[v];
|
|
|
|
v = (int)(kDistanceShortCodeValueOffset >> distance_code) & 0x3;
|
|
|
|
if ((distance_code & 0x3) != 0) {
|
|
|
|
s->distance_code += v;
|
|
|
|
} else {
|
|
|
|
s->distance_code -= v;
|
|
|
|
if (s->distance_code <= 0) {
|
|
|
|
/* A huge distance will cause a BROTLI_FAILURE() soon. */
|
|
|
|
/* This is a little faster than failing here. */
|
|
|
|
s->distance_code = 0x0fffffff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_INLINE BROTLI_BOOL SafeReadBits(
|
2015-11-06 10:46:04 +00:00
|
|
|
BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
|
|
|
|
if (n_bits != 0) {
|
|
|
|
return BrotliSafeReadBits(br, n_bits, val);
|
|
|
|
} else {
|
|
|
|
*val = 0;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_TRUE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Precondition: s->distance_code < 0 */
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_INLINE BROTLI_BOOL ReadDistanceInternal(
|
|
|
|
int safe, BrotliDecoderState* s, BrotliBitReader* br) {
|
2015-11-06 10:46:04 +00:00
|
|
|
int distval;
|
|
|
|
BrotliBitReaderState memento;
|
|
|
|
HuffmanCode* distance_tree = s->distance_hgroup.htrees[s->dist_htree_index];
|
|
|
|
if (!safe) {
|
|
|
|
s->distance_code = (int)ReadSymbol(distance_tree, br);
|
|
|
|
} else {
|
|
|
|
uint32_t code;
|
|
|
|
BrotliBitReaderSaveState(br, &memento);
|
|
|
|
if (!SafeReadSymbol(distance_tree, br, &code)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FALSE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
s->distance_code = (int)code;
|
|
|
|
}
|
|
|
|
/* Convert the distance code to the actual distance by possibly */
|
|
|
|
/* looking up past distances from the s->ringbuffer. */
|
|
|
|
if ((s->distance_code & ~0xf) == 0) {
|
|
|
|
TakeDistanceFromRingBuffer(s);
|
|
|
|
--s->block_length[2];
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_TRUE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
distval = s->distance_code - (int)s->num_direct_distance_codes;
|
|
|
|
if (distval >= 0) {
|
|
|
|
uint32_t nbits;
|
|
|
|
int postfix;
|
|
|
|
int offset;
|
|
|
|
if (!safe && (s->distance_postfix_bits == 0)) {
|
|
|
|
nbits = ((uint32_t)distval >> 1) + 1;
|
|
|
|
offset = ((2 + (distval & 1)) << nbits) - 4;
|
2016-02-18 14:03:44 +00:00
|
|
|
s->distance_code = (int)s->num_direct_distance_codes + offset +
|
|
|
|
(int)BrotliReadBits(br, nbits);
|
2015-11-06 10:46:04 +00:00
|
|
|
} else {
|
|
|
|
/* This branch also works well when s->distance_postfix_bits == 0 */
|
|
|
|
uint32_t bits;
|
|
|
|
postfix = distval & s->distance_postfix_mask;
|
|
|
|
distval >>= s->distance_postfix_bits;
|
|
|
|
nbits = ((uint32_t)distval >> 1) + 1;
|
|
|
|
if (safe) {
|
|
|
|
if (!SafeReadBits(br, nbits, &bits)) {
|
|
|
|
s->distance_code = -1; /* Restore precondition. */
|
|
|
|
BrotliBitReaderRestoreState(br, &memento);
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FALSE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bits = BrotliReadBits(br, nbits);
|
|
|
|
}
|
|
|
|
offset = ((2 + (distval & 1)) << nbits) - 4;
|
|
|
|
s->distance_code = (int)s->num_direct_distance_codes +
|
|
|
|
((offset + (int)bits) << s->distance_postfix_bits) + postfix;
|
|
|
|
}
|
|
|
|
}
|
2016-06-03 08:51:04 +00:00
|
|
|
s->distance_code = s->distance_code - BROTLI_NUM_DISTANCE_SHORT_CODES + 1;
|
2015-11-06 10:46:04 +00:00
|
|
|
--s->block_length[2];
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_TRUE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_INLINE void ReadDistance(
|
|
|
|
BrotliDecoderState* s, BrotliBitReader* br) {
|
2015-11-06 10:46:04 +00:00
|
|
|
ReadDistanceInternal(0, s, br);
|
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_INLINE BROTLI_BOOL SafeReadDistance(
|
|
|
|
BrotliDecoderState* s, BrotliBitReader* br) {
|
2015-11-06 10:46:04 +00:00
|
|
|
return ReadDistanceInternal(1, s, br);
|
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_INLINE BROTLI_BOOL ReadCommandInternal(
|
|
|
|
int safe, BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) {
|
2015-11-06 10:46:04 +00:00
|
|
|
uint32_t cmd_code;
|
|
|
|
uint32_t insert_len_extra = 0;
|
|
|
|
uint32_t copy_length;
|
|
|
|
CmdLutElement v;
|
|
|
|
BrotliBitReaderState memento;
|
|
|
|
if (!safe) {
|
|
|
|
cmd_code = ReadSymbol(s->htree_command, br);
|
|
|
|
} else {
|
|
|
|
BrotliBitReaderSaveState(br, &memento);
|
|
|
|
if (!SafeReadSymbol(s->htree_command, br, &cmd_code)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FALSE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
v = kCmdLut[cmd_code];
|
|
|
|
s->distance_code = v.distance_code;
|
|
|
|
s->distance_context = v.context;
|
|
|
|
s->dist_htree_index = s->dist_context_map_slice[s->distance_context];
|
|
|
|
*insert_length = v.insert_len_offset;
|
|
|
|
if (!safe) {
|
|
|
|
if (PREDICT_FALSE(v.insert_len_extra_bits != 0)) {
|
|
|
|
insert_len_extra = BrotliReadBits(br, v.insert_len_extra_bits);
|
|
|
|
}
|
|
|
|
copy_length = BrotliReadBits(br, v.copy_len_extra_bits);
|
|
|
|
} else {
|
|
|
|
if (!SafeReadBits(br, v.insert_len_extra_bits, &insert_len_extra) ||
|
|
|
|
!SafeReadBits(br, v.copy_len_extra_bits, ©_length)) {
|
|
|
|
BrotliBitReaderRestoreState(br, &memento);
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FALSE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
s->copy_length = (int)copy_length + v.copy_len_offset;
|
|
|
|
--s->block_length[1];
|
|
|
|
*insert_length += (int)insert_len_extra;
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_TRUE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_INLINE void ReadCommand(
|
|
|
|
BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) {
|
2015-11-06 10:46:04 +00:00
|
|
|
ReadCommandInternal(0, s, br, insert_length);
|
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_INLINE BROTLI_BOOL SafeReadCommand(
|
|
|
|
BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) {
|
2015-11-06 10:46:04 +00:00
|
|
|
return ReadCommandInternal(1, s, br, insert_length);
|
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_INLINE BROTLI_BOOL CheckInputAmount(
|
|
|
|
int safe, BrotliBitReader* const br, size_t num) {
|
2015-11-06 10:46:04 +00:00
|
|
|
if (safe) {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_TRUE;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
return BrotliCheckInputAmount(br, num);
|
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
#define BROTLI_SAFE(METHOD) \
|
|
|
|
{ \
|
|
|
|
if (safe) { \
|
|
|
|
if (!Safe##METHOD) { \
|
|
|
|
result = BROTLI_DECODER_NEEDS_MORE_INPUT; \
|
|
|
|
goto saveStateAndReturn; \
|
|
|
|
} \
|
|
|
|
} else { \
|
|
|
|
METHOD; \
|
|
|
|
} \
|
2016-02-18 14:03:44 +00:00
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_INLINE BrotliDecoderErrorCode ProcessCommandsInternal(
|
|
|
|
int safe, BrotliDecoderState* s) {
|
2015-11-06 10:46:04 +00:00
|
|
|
int pos = s->pos;
|
|
|
|
int i = s->loop_counter;
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS;
|
2015-11-06 10:46:04 +00:00
|
|
|
BrotliBitReader* br = &s->br;
|
|
|
|
|
2016-01-22 09:19:41 +00:00
|
|
|
if (!CheckInputAmount(safe, br, 28)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-11-06 10:46:04 +00:00
|
|
|
goto saveStateAndReturn;
|
|
|
|
}
|
2016-01-22 09:19:41 +00:00
|
|
|
if (!safe) {
|
|
|
|
BROTLI_UNUSED(BrotliWarmupBitReader(br));
|
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
|
|
|
|
/* Jump into state machine. */
|
|
|
|
if (s->state == BROTLI_STATE_COMMAND_BEGIN) {
|
|
|
|
goto CommandBegin;
|
|
|
|
} else if (s->state == BROTLI_STATE_COMMAND_INNER) {
|
|
|
|
goto CommandInner;
|
|
|
|
} else if (s->state == BROTLI_STATE_COMMAND_POST_DECODE_LITERALS) {
|
|
|
|
goto CommandPostDecodeLiterals;
|
|
|
|
} else if (s->state == BROTLI_STATE_COMMAND_POST_WRAP_COPY) {
|
|
|
|
goto CommandPostWrapCopy;
|
|
|
|
} else {
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE);
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CommandBegin:
|
|
|
|
if (safe) {
|
|
|
|
s->state = BROTLI_STATE_COMMAND_BEGIN;
|
|
|
|
}
|
|
|
|
if (!CheckInputAmount(safe, br, 28)) { /* 156 bits + 7 bytes */
|
|
|
|
s->state = BROTLI_STATE_COMMAND_BEGIN;
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-11-06 10:46:04 +00:00
|
|
|
goto saveStateAndReturn;
|
|
|
|
}
|
|
|
|
if (PREDICT_FALSE(s->block_length[1] == 0)) {
|
|
|
|
BROTLI_SAFE(DecodeCommandBlockSwitch(s));
|
|
|
|
goto CommandBegin;
|
|
|
|
}
|
|
|
|
/* Read the insert/copy length in the command */
|
|
|
|
BROTLI_SAFE(ReadCommand(s, br, &i));
|
2016-01-22 09:19:41 +00:00
|
|
|
BROTLI_LOG(("[ProcessCommandsInternal] pos = %d insert = %d copy = %d\n",
|
|
|
|
pos, i, s->copy_length));
|
2015-11-06 10:46:04 +00:00
|
|
|
if (i == 0) {
|
|
|
|
goto CommandPostDecodeLiterals;
|
|
|
|
}
|
|
|
|
s->meta_block_remaining_len -= i;
|
|
|
|
|
|
|
|
CommandInner:
|
|
|
|
if (safe) {
|
|
|
|
s->state = BROTLI_STATE_COMMAND_INNER;
|
|
|
|
}
|
|
|
|
/* Read the literals in the command */
|
|
|
|
if (s->trivial_literal_context) {
|
|
|
|
uint32_t bits;
|
|
|
|
uint32_t value;
|
|
|
|
PreloadSymbol(safe, s->literal_htree, br, &bits, &value);
|
|
|
|
do {
|
|
|
|
if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */
|
|
|
|
s->state = BROTLI_STATE_COMMAND_INNER;
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-11-06 10:46:04 +00:00
|
|
|
goto saveStateAndReturn;
|
|
|
|
}
|
|
|
|
if (PREDICT_FALSE(s->block_length[0] == 0)) {
|
|
|
|
BROTLI_SAFE(DecodeLiteralBlockSwitch(s));
|
|
|
|
PreloadSymbol(safe, s->literal_htree, br, &bits, &value);
|
2016-05-31 13:40:59 +00:00
|
|
|
if (!s->trivial_literal_context) goto CommandInner;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
if (!safe) {
|
2016-02-18 14:03:44 +00:00
|
|
|
s->ringbuffer[pos] =
|
|
|
|
(uint8_t)ReadPreloadedSymbol(s->literal_htree, br, &bits, &value);
|
2015-11-06 10:46:04 +00:00
|
|
|
} else {
|
|
|
|
uint32_t literal;
|
|
|
|
if (!SafeReadSymbol(s->literal_htree, br, &literal)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-11-06 10:46:04 +00:00
|
|
|
goto saveStateAndReturn;
|
|
|
|
}
|
|
|
|
s->ringbuffer[pos] = (uint8_t)literal;
|
|
|
|
}
|
|
|
|
--s->block_length[0];
|
|
|
|
BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos);
|
|
|
|
++pos;
|
|
|
|
if (PREDICT_FALSE(pos == s->ringbuffer_size)) {
|
|
|
|
s->state = BROTLI_STATE_COMMAND_INNER_WRITE;
|
|
|
|
--i;
|
|
|
|
goto saveStateAndReturn;
|
|
|
|
}
|
|
|
|
} while (--i != 0);
|
|
|
|
} else {
|
|
|
|
uint8_t p1 = s->ringbuffer[(pos - 1) & s->ringbuffer_mask];
|
|
|
|
uint8_t p2 = s->ringbuffer[(pos - 2) & s->ringbuffer_mask];
|
|
|
|
do {
|
|
|
|
const HuffmanCode* hc;
|
|
|
|
uint8_t context;
|
|
|
|
if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */
|
|
|
|
s->state = BROTLI_STATE_COMMAND_INNER;
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-11-06 10:46:04 +00:00
|
|
|
goto saveStateAndReturn;
|
|
|
|
}
|
|
|
|
if (PREDICT_FALSE(s->block_length[0] == 0)) {
|
|
|
|
BROTLI_SAFE(DecodeLiteralBlockSwitch(s));
|
2016-05-31 13:40:59 +00:00
|
|
|
if (s->trivial_literal_context) goto CommandInner;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
context = s->context_lookup1[p1] | s->context_lookup2[p2];
|
|
|
|
BROTLI_LOG_UINT(context);
|
|
|
|
hc = s->literal_hgroup.htrees[s->context_map_slice[context]];
|
|
|
|
p2 = p1;
|
|
|
|
if (!safe) {
|
|
|
|
p1 = (uint8_t)ReadSymbol(hc, br);
|
|
|
|
} else {
|
|
|
|
uint32_t literal;
|
|
|
|
if (!SafeReadSymbol(hc, br, &literal)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-11-06 10:46:04 +00:00
|
|
|
goto saveStateAndReturn;
|
|
|
|
}
|
|
|
|
p1 = (uint8_t)literal;
|
|
|
|
}
|
|
|
|
s->ringbuffer[pos] = p1;
|
|
|
|
--s->block_length[0];
|
|
|
|
BROTLI_LOG_UINT(s->context_map_slice[context]);
|
|
|
|
BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos & s->ringbuffer_mask);
|
|
|
|
++pos;
|
|
|
|
if (PREDICT_FALSE(pos == s->ringbuffer_size)) {
|
|
|
|
s->state = BROTLI_STATE_COMMAND_INNER_WRITE;
|
|
|
|
--i;
|
|
|
|
goto saveStateAndReturn;
|
|
|
|
}
|
|
|
|
} while (--i != 0);
|
|
|
|
}
|
2016-01-22 09:19:41 +00:00
|
|
|
BROTLI_LOG_UINT(s->meta_block_remaining_len);
|
2016-05-31 13:40:59 +00:00
|
|
|
if (PREDICT_FALSE(s->meta_block_remaining_len <= 0)) {
|
2015-11-06 10:46:04 +00:00
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
goto saveStateAndReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandPostDecodeLiterals:
|
|
|
|
if (safe) {
|
|
|
|
s->state = BROTLI_STATE_COMMAND_POST_DECODE_LITERALS;
|
|
|
|
}
|
|
|
|
if (s->distance_code >= 0) {
|
|
|
|
--s->dist_rb_idx;
|
|
|
|
s->distance_code = s->dist_rb[s->dist_rb_idx & 3];
|
|
|
|
goto postReadDistance; /* We already have the implicit distance */
|
|
|
|
}
|
|
|
|
/* Read distance code in the command, unless it was implicitly zero. */
|
|
|
|
if (PREDICT_FALSE(s->block_length[2] == 0)) {
|
|
|
|
BROTLI_SAFE(DecodeDistanceBlockSwitch(s));
|
|
|
|
}
|
|
|
|
BROTLI_SAFE(ReadDistance(s, br));
|
|
|
|
postReadDistance:
|
2016-01-22 09:19:41 +00:00
|
|
|
BROTLI_LOG(("[ProcessCommandsInternal] pos = %d distance = %d\n",
|
|
|
|
pos, s->distance_code));
|
2015-11-06 10:46:04 +00:00
|
|
|
if (s->max_distance != s->max_backward_distance) {
|
|
|
|
if (pos < s->max_backward_distance_minus_custom_dict_size) {
|
|
|
|
s->max_distance = pos + s->custom_dict_size;
|
|
|
|
} else {
|
|
|
|
s->max_distance = s->max_backward_distance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i = s->copy_length;
|
|
|
|
/* Apply copy of LZ77 back-reference, or static dictionary reference if
|
|
|
|
the distance is larger than the max LZ77 distance */
|
|
|
|
if (s->distance_code > s->max_distance) {
|
|
|
|
if (i >= kBrotliMinDictionaryWordLength &&
|
|
|
|
i <= kBrotliMaxDictionaryWordLength) {
|
2016-01-22 09:19:41 +00:00
|
|
|
int offset = (int)kBrotliDictionaryOffsetsByLength[i];
|
2015-11-06 10:46:04 +00:00
|
|
|
int word_id = s->distance_code - s->max_distance - 1;
|
|
|
|
uint32_t shift = kBrotliDictionarySizeBitsByLength[i];
|
|
|
|
int mask = (int)BitMask(shift);
|
|
|
|
int word_idx = word_id & mask;
|
|
|
|
int transform_idx = word_id >> shift;
|
|
|
|
offset += word_idx * i;
|
|
|
|
if (transform_idx < kNumTransforms) {
|
|
|
|
const uint8_t* word = &kBrotliDictionary[offset];
|
|
|
|
int len = i;
|
|
|
|
if (transform_idx == 0) {
|
|
|
|
memcpy(&s->ringbuffer[pos], word, (size_t)len);
|
|
|
|
} else {
|
|
|
|
len = TransformDictionaryWord(
|
|
|
|
&s->ringbuffer[pos], word, len, transform_idx);
|
|
|
|
}
|
|
|
|
pos += len;
|
|
|
|
s->meta_block_remaining_len -= len;
|
|
|
|
if (pos >= s->ringbuffer_size) {
|
|
|
|
/*s->partial_pos_rb += (size_t)s->ringbuffer_size;*/
|
|
|
|
s->state = BROTLI_STATE_COMMAND_POST_WRITE_1;
|
|
|
|
goto saveStateAndReturn;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
|
2016-02-18 14:03:44 +00:00
|
|
|
"len: %d bytes left: %d\n",
|
|
|
|
pos, s->distance_code, i, s->meta_block_remaining_len));
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_TRANSFORM);
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
|
2016-02-18 14:03:44 +00:00
|
|
|
"len: %d bytes left: %d\n",
|
|
|
|
pos, s->distance_code, i, s->meta_block_remaining_len));
|
2016-07-25 08:17:04 +00:00
|
|
|
return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_DICTIONARY);
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-02-18 14:03:44 +00:00
|
|
|
int src_start = (pos - s->distance_code) & s->ringbuffer_mask;
|
2016-02-04 14:42:53 +00:00
|
|
|
uint8_t* copy_dst = &s->ringbuffer[pos];
|
2016-02-18 14:03:44 +00:00
|
|
|
uint8_t* copy_src = &s->ringbuffer[src_start];
|
|
|
|
int dst_end = pos + i;
|
|
|
|
int src_end = src_start + i;
|
2015-11-06 10:46:04 +00:00
|
|
|
/* update the recent distances cache */
|
|
|
|
s->dist_rb[s->dist_rb_idx & 3] = s->distance_code;
|
|
|
|
++s->dist_rb_idx;
|
|
|
|
s->meta_block_remaining_len -= i;
|
2016-02-18 14:03:44 +00:00
|
|
|
/* There are 32+ bytes of slack in the ringbuffer allocation.
|
2015-11-06 10:46:04 +00:00
|
|
|
Also, we have 16 short codes, that make these 16 bytes irrelevant
|
|
|
|
in the ringbuffer. Let's copy over them as a first guess.
|
|
|
|
*/
|
|
|
|
memmove16(copy_dst, copy_src);
|
2016-02-18 14:03:44 +00:00
|
|
|
if (src_end > pos && dst_end > src_start) {
|
|
|
|
/* Regions intersect. */
|
|
|
|
goto CommandPostWrapCopy;
|
|
|
|
}
|
|
|
|
if (dst_end >= s->ringbuffer_size || src_end >= s->ringbuffer_size) {
|
|
|
|
/* At least one region wraps. */
|
|
|
|
goto CommandPostWrapCopy;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
pos += i;
|
|
|
|
if (i > 16) {
|
|
|
|
if (i > 32) {
|
|
|
|
memcpy(copy_dst + 16, copy_src + 16, (size_t)(i - 16));
|
|
|
|
} else {
|
|
|
|
/* This branch covers about 45% cases.
|
|
|
|
Fixed size short copy allows more compiler optimizations. */
|
|
|
|
memmove16(copy_dst + 16, copy_src + 16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-22 09:19:41 +00:00
|
|
|
BROTLI_LOG_UINT(s->meta_block_remaining_len);
|
2015-11-06 10:46:04 +00:00
|
|
|
if (s->meta_block_remaining_len <= 0) {
|
|
|
|
/* Next metablock, if any */
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
goto saveStateAndReturn;
|
|
|
|
} else {
|
|
|
|
goto CommandBegin;
|
|
|
|
}
|
|
|
|
CommandPostWrapCopy:
|
2016-02-18 14:03:44 +00:00
|
|
|
{
|
|
|
|
int wrap_guard = s->ringbuffer_size - pos;
|
|
|
|
while (--i >= 0) {
|
|
|
|
s->ringbuffer[pos] =
|
|
|
|
s->ringbuffer[(pos - s->distance_code) & s->ringbuffer_mask];
|
|
|
|
++pos;
|
|
|
|
if (PREDICT_FALSE(--wrap_guard == 0)) {
|
|
|
|
s->state = BROTLI_STATE_COMMAND_POST_WRITE_2;
|
|
|
|
goto saveStateAndReturn;
|
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (s->meta_block_remaining_len <= 0) {
|
|
|
|
/* Next metablock, if any */
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
goto saveStateAndReturn;
|
|
|
|
} else {
|
|
|
|
goto CommandBegin;
|
|
|
|
}
|
|
|
|
|
|
|
|
saveStateAndReturn:
|
|
|
|
s->pos = pos;
|
|
|
|
s->loop_counter = i;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef BROTLI_SAFE
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_NOINLINE BrotliDecoderErrorCode ProcessCommands(
|
|
|
|
BrotliDecoderState* s) {
|
2015-11-06 10:46:04 +00:00
|
|
|
return ProcessCommandsInternal(0, s);
|
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
static BROTLI_NOINLINE BrotliDecoderErrorCode SafeProcessCommands(
|
|
|
|
BrotliDecoderState* s) {
|
2015-11-06 10:46:04 +00:00
|
|
|
return ProcessCommandsInternal(1, s);
|
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderResult BrotliDecoderDecompress(
|
|
|
|
size_t encoded_size, const uint8_t* encoded_buffer, size_t* decoded_size,
|
|
|
|
uint8_t* decoded_buffer) {
|
|
|
|
BrotliDecoderState s;
|
|
|
|
BrotliDecoderResult result;
|
2015-11-06 10:46:04 +00:00
|
|
|
size_t total_out = 0;
|
|
|
|
size_t available_in = encoded_size;
|
|
|
|
const uint8_t* next_in = encoded_buffer;
|
|
|
|
size_t available_out = *decoded_size;
|
|
|
|
uint8_t* next_out = decoded_buffer;
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderStateInit(&s);
|
|
|
|
result = BrotliDecoderDecompressStream(
|
|
|
|
&s, &available_in, &next_in, &available_out, &next_out, &total_out);
|
2015-11-06 10:46:04 +00:00
|
|
|
*decoded_size = total_out;
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderStateCleanup(&s);
|
|
|
|
if (result != BROTLI_DECODER_RESULT_SUCCESS) {
|
|
|
|
result = BROTLI_DECODER_RESULT_ERROR;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Invariant: input stream is never overconsumed:
|
|
|
|
* invalid input implies that the whole stream is invalid -> any amount of
|
|
|
|
input could be read and discarded
|
|
|
|
* when result is "needs more input", then at leat one more byte is REQUIRED
|
|
|
|
to complete decoding; all input data MUST be consumed by decoder, so
|
|
|
|
client could swap the input buffer
|
|
|
|
* when result is "needs more output" decoder MUST ensure that it doesn't
|
|
|
|
hold more than 7 bits in bit reader; this saves client from swapping input
|
|
|
|
buffer ahead of time
|
|
|
|
* when result is "success" decoder MUST return all unused data back to input
|
|
|
|
buffer; this is possible because the invariant is hold on enter
|
|
|
|
*/
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderResult BrotliDecoderDecompressStream(
|
|
|
|
BrotliDecoderState* s, size_t* available_in, const uint8_t** next_in,
|
|
|
|
size_t* available_out, uint8_t** next_out, size_t* total_out) {
|
|
|
|
BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS;
|
2015-03-20 15:13:15 +00:00
|
|
|
BrotliBitReader* br = &s->br;
|
2015-11-06 10:46:04 +00:00
|
|
|
if (s->buffer_length == 0) { /* Just connect bit reader to input stream. */
|
|
|
|
br->avail_in = *available_in;
|
|
|
|
br->next_in = *next_in;
|
|
|
|
} else {
|
|
|
|
/* At least one byte of input is required. More than one byte of input may
|
|
|
|
be required to complete the transaction -> reading more data must be
|
|
|
|
done in a loop -> do it in a main loop. */
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-11-06 10:46:04 +00:00
|
|
|
br->next_in = &s->buffer.u8[0];
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
/* State machine */
|
|
|
|
for (;;) {
|
2016-07-25 08:17:04 +00:00
|
|
|
if (result != BROTLI_DECODER_SUCCESS) { /* Error, needs more input/output */
|
|
|
|
if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) {
|
2015-11-06 10:46:04 +00:00
|
|
|
if (s->ringbuffer != 0) { /* Proactively push output. */
|
2016-07-25 08:17:04 +00:00
|
|
|
WriteRingBuffer(s, available_out, next_out, total_out);
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
if (s->buffer_length != 0) { /* Used with internal buffer. */
|
|
|
|
if (br->avail_in == 0) { /* Successfully finished read transaction. */
|
|
|
|
/* Accamulator contains less than 8 bits, because internal buffer
|
|
|
|
is expanded byte-by-byte until it is enough to complete read. */
|
|
|
|
s->buffer_length = 0;
|
|
|
|
/* Switch to input stream and restart. */
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_DECODER_SUCCESS;
|
2015-11-06 10:46:04 +00:00
|
|
|
br->avail_in = *available_in;
|
|
|
|
br->next_in = *next_in;
|
|
|
|
continue;
|
|
|
|
} else if (*available_in != 0) {
|
|
|
|
/* Not enough data in buffer, but can take one more byte from
|
|
|
|
input stream. */
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_DECODER_SUCCESS;
|
2015-11-06 10:46:04 +00:00
|
|
|
s->buffer.u8[s->buffer_length] = **next_in;
|
|
|
|
s->buffer_length++;
|
|
|
|
br->avail_in = s->buffer_length;
|
|
|
|
(*next_in)++;
|
|
|
|
(*available_in)--;
|
|
|
|
/* Retry with more data in buffer. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Can't finish reading and no more input.*/
|
|
|
|
break;
|
|
|
|
} else { /* Input stream doesn't contain enough input. */
|
|
|
|
/* Copy tail to internal buffer and return. */
|
|
|
|
*next_in = br->next_in;
|
|
|
|
*available_in = br->avail_in;
|
|
|
|
while (*available_in) {
|
|
|
|
s->buffer.u8[s->buffer_length] = **next_in;
|
|
|
|
s->buffer_length++;
|
|
|
|
(*next_in)++;
|
|
|
|
(*available_in)--;
|
|
|
|
}
|
|
|
|
break;
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
/* Unreachable. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fail or needs more output. */
|
|
|
|
|
|
|
|
if (s->buffer_length != 0) {
|
|
|
|
/* Just consumed the buffered input and produced some output. Otherwise
|
|
|
|
it would result in "needs more input". Reset internal buffer.*/
|
|
|
|
s->buffer_length = 0;
|
|
|
|
} else {
|
|
|
|
/* Using input stream in last iteration. When decoder switches to input
|
|
|
|
stream it has less than 8 bits in accamulator, so it is safe to
|
|
|
|
return unused accamulator bits there. */
|
|
|
|
BrotliBitReaderUnload(br);
|
|
|
|
*available_in = br->avail_in;
|
|
|
|
*next_in = br->next_in;
|
2015-03-20 16:03:11 +00:00
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
break;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
switch (s->state) {
|
|
|
|
case BROTLI_STATE_UNINITED:
|
2015-09-23 10:24:06 +00:00
|
|
|
/* Prepare to the first read. */
|
|
|
|
if (!BrotliWarmupBitReader(br)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Decode window size. */
|
2015-09-23 10:24:06 +00:00
|
|
|
s->window_bits = DecodeWindowBits(br); /* Reads 1..7 bits. */
|
2015-09-24 16:35:25 +00:00
|
|
|
BROTLI_LOG_UINT(s->window_bits);
|
2015-05-07 15:44:33 +00:00
|
|
|
if (s->window_bits == 9) {
|
|
|
|
/* Value 9 is reserved for future use. */
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS);
|
2015-05-07 15:44:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-04-29 11:44:16 +00:00
|
|
|
/* Maximum distance, see section 9.1. of the spec. */
|
2015-03-20 15:13:15 +00:00
|
|
|
s->max_backward_distance = (1 << s->window_bits) - 16;
|
2016-04-29 11:44:16 +00:00
|
|
|
/* Limit custom dictionary size. */
|
|
|
|
if (s->custom_dict_size >= s->max_backward_distance) {
|
|
|
|
s->custom_dict += s->custom_dict_size - s->max_backward_distance;
|
|
|
|
s->custom_dict_size = s->max_backward_distance;
|
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
s->max_backward_distance_minus_custom_dict_size =
|
|
|
|
s->max_backward_distance - s->custom_dict_size;
|
2015-03-20 15:13:15 +00:00
|
|
|
|
2015-08-10 14:39:50 +00:00
|
|
|
/* Allocate memory for both block_type_trees and block_len_trees. */
|
2015-11-23 09:21:09 +00:00
|
|
|
s->block_type_trees = (HuffmanCode*)BROTLI_ALLOC(s,
|
2016-01-22 09:19:41 +00:00
|
|
|
sizeof(HuffmanCode) * 3 *
|
|
|
|
(BROTLI_HUFFMAN_MAX_SIZE_258 + BROTLI_HUFFMAN_MAX_SIZE_26));
|
2015-10-02 14:19:30 +00:00
|
|
|
if (s->block_type_trees == 0) {
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES);
|
2015-03-20 16:03:11 +00:00
|
|
|
break;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2016-02-18 14:03:44 +00:00
|
|
|
s->block_len_trees =
|
|
|
|
s->block_type_trees + 3 * BROTLI_HUFFMAN_MAX_SIZE_258;
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2015-03-20 15:13:15 +00:00
|
|
|
s->state = BROTLI_STATE_METABLOCK_BEGIN;
|
2015-03-20 16:03:11 +00:00
|
|
|
/* No break, continue to next state */
|
2015-03-20 15:13:15 +00:00
|
|
|
case BROTLI_STATE_METABLOCK_BEGIN:
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderStateMetablockBegin(s);
|
2015-11-06 10:46:04 +00:00
|
|
|
BROTLI_LOG_UINT(s->pos);
|
2015-09-23 10:24:06 +00:00
|
|
|
s->state = BROTLI_STATE_METABLOCK_HEADER;
|
|
|
|
/* No break, continue to next state */
|
|
|
|
case BROTLI_STATE_METABLOCK_HEADER:
|
|
|
|
result = DecodeMetaBlockLength(s, br); /* Reads 2 - 31 bits. */
|
2016-07-25 08:17:04 +00:00
|
|
|
if (result != BROTLI_DECODER_SUCCESS) {
|
2015-04-22 12:25:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
BROTLI_LOG_UINT(s->is_last_metablock);
|
2015-03-20 15:13:15 +00:00
|
|
|
BROTLI_LOG_UINT(s->meta_block_remaining_len);
|
2015-09-24 16:35:25 +00:00
|
|
|
BROTLI_LOG_UINT(s->is_metadata);
|
|
|
|
BROTLI_LOG_UINT(s->is_uncompressed);
|
2015-09-23 10:24:06 +00:00
|
|
|
if (s->is_metadata || s->is_uncompressed) {
|
2015-09-01 10:18:22 +00:00
|
|
|
if (!BrotliJumpToByteBoundary(br)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_PADDING_1);
|
2015-04-22 12:35:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-09-23 10:24:06 +00:00
|
|
|
}
|
|
|
|
if (s->is_metadata) {
|
2015-04-22 12:25:08 +00:00
|
|
|
s->state = BROTLI_STATE_METADATA;
|
|
|
|
break;
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
if (s->meta_block_remaining_len == 0) {
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
break;
|
|
|
|
}
|
2015-09-23 10:24:06 +00:00
|
|
|
if (!s->ringbuffer) {
|
2016-01-22 09:19:41 +00:00
|
|
|
BrotliCalculateRingBufferSize(s, br);
|
2015-09-23 10:24:06 +00:00
|
|
|
}
|
|
|
|
if (s->is_uncompressed) {
|
2015-03-20 15:13:15 +00:00
|
|
|
s->state = BROTLI_STATE_UNCOMPRESSED;
|
|
|
|
break;
|
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
s->loop_counter = 0;
|
2015-03-20 15:13:15 +00:00
|
|
|
s->state = BROTLI_STATE_HUFFMAN_CODE_0;
|
|
|
|
break;
|
2015-11-06 10:46:04 +00:00
|
|
|
case BROTLI_STATE_UNCOMPRESSED: {
|
|
|
|
int bytes_copied = s->meta_block_remaining_len;
|
|
|
|
result = CopyUncompressedBlockToOutput(
|
|
|
|
available_out, next_out, total_out, s);
|
|
|
|
bytes_copied -= s->meta_block_remaining_len;
|
2016-07-25 08:17:04 +00:00
|
|
|
if (result != BROTLI_DECODER_SUCCESS) {
|
2015-08-10 11:35:23 +00:00
|
|
|
break;
|
2015-04-01 14:23:18 +00:00
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
break;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
2015-04-22 12:25:08 +00:00
|
|
|
case BROTLI_STATE_METADATA:
|
|
|
|
for (; s->meta_block_remaining_len > 0; --s->meta_block_remaining_len) {
|
2015-09-23 10:24:06 +00:00
|
|
|
uint32_t bits;
|
|
|
|
/* Read one byte and ignore it. */
|
|
|
|
if (!BrotliSafeReadBits(br, 8, &bits)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-04-22 12:25:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-07-25 08:17:04 +00:00
|
|
|
if (result == BROTLI_DECODER_SUCCESS) {
|
2015-08-10 11:35:23 +00:00
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
}
|
2015-04-22 12:25:08 +00:00
|
|
|
break;
|
2015-03-20 15:13:15 +00:00
|
|
|
case BROTLI_STATE_HUFFMAN_CODE_0:
|
2015-11-06 10:46:04 +00:00
|
|
|
if (s->loop_counter >= 3) {
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_HEADER_2;
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
/* Reads 1..11 bits. */
|
2015-11-06 10:46:04 +00:00
|
|
|
result = DecodeVarLenUint8(s, br, &s->num_block_types[s->loop_counter]);
|
2016-07-25 08:17:04 +00:00
|
|
|
if (result != BROTLI_DECODER_SUCCESS) {
|
2015-09-23 10:24:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
s->num_block_types[s->loop_counter]++;
|
|
|
|
BROTLI_LOG_UINT(s->num_block_types[s->loop_counter]);
|
|
|
|
if (s->num_block_types[s->loop_counter] < 2) {
|
|
|
|
s->loop_counter++;
|
2015-03-20 16:03:11 +00:00
|
|
|
break;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
s->state = BROTLI_STATE_HUFFMAN_CODE_1;
|
2015-03-20 16:03:11 +00:00
|
|
|
/* No break, continue to next state */
|
2015-11-06 10:46:04 +00:00
|
|
|
case BROTLI_STATE_HUFFMAN_CODE_1: {
|
2016-01-22 09:19:41 +00:00
|
|
|
int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_258;
|
2015-11-06 10:46:04 +00:00
|
|
|
result = ReadHuffmanCode(s->num_block_types[s->loop_counter] + 2,
|
|
|
|
&s->block_type_trees[tree_offset], NULL, s);
|
2016-07-25 08:17:04 +00:00
|
|
|
if (result != BROTLI_DECODER_SUCCESS) break;
|
2015-11-06 10:46:04 +00:00
|
|
|
s->state = BROTLI_STATE_HUFFMAN_CODE_2;
|
|
|
|
/* No break, continue to next state */
|
|
|
|
}
|
|
|
|
case BROTLI_STATE_HUFFMAN_CODE_2: {
|
2016-01-22 09:19:41 +00:00
|
|
|
int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26;
|
2016-06-03 08:51:04 +00:00
|
|
|
result = ReadHuffmanCode(BROTLI_NUM_BLOCK_LEN_SYMBOLS,
|
2015-11-06 10:46:04 +00:00
|
|
|
&s->block_len_trees[tree_offset], NULL, s);
|
2016-07-25 08:17:04 +00:00
|
|
|
if (result != BROTLI_DECODER_SUCCESS) break;
|
2015-09-23 10:24:06 +00:00
|
|
|
s->state = BROTLI_STATE_HUFFMAN_CODE_3;
|
|
|
|
/* No break, continue to next state */
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
case BROTLI_STATE_HUFFMAN_CODE_3: {
|
2016-01-22 09:19:41 +00:00
|
|
|
int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26;
|
2015-11-06 10:46:04 +00:00
|
|
|
if (!SafeReadBlockLength(s, &s->block_length[s->loop_counter],
|
|
|
|
&s->block_len_trees[tree_offset], br)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-09-23 10:24:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
BROTLI_LOG_UINT(s->block_length[s->loop_counter]);
|
|
|
|
s->loop_counter++;
|
2015-03-20 15:13:15 +00:00
|
|
|
s->state = BROTLI_STATE_HUFFMAN_CODE_0;
|
|
|
|
break;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
case BROTLI_STATE_METABLOCK_HEADER_2: {
|
|
|
|
uint32_t bits;
|
|
|
|
if (!BrotliSafeReadBits(br, 6, &bits)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_DECODER_NEEDS_MORE_INPUT;
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
s->distance_postfix_bits = bits & BitMask(2);
|
|
|
|
bits >>= 2;
|
2016-06-03 08:51:04 +00:00
|
|
|
s->num_direct_distance_codes = BROTLI_NUM_DISTANCE_SHORT_CODES +
|
|
|
|
(bits << s->distance_postfix_bits);
|
2015-09-24 16:35:25 +00:00
|
|
|
BROTLI_LOG_UINT(s->num_direct_distance_codes);
|
|
|
|
BROTLI_LOG_UINT(s->distance_postfix_bits);
|
2015-08-28 13:20:24 +00:00
|
|
|
s->distance_postfix_mask = (int)BitMask(s->distance_postfix_bits);
|
2015-11-23 09:21:09 +00:00
|
|
|
s->context_modes =
|
|
|
|
(uint8_t*)BROTLI_ALLOC(s, (size_t)s->num_block_types[0]);
|
2015-03-20 15:13:15 +00:00
|
|
|
if (s->context_modes == 0) {
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES);
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
s->loop_counter = 0;
|
|
|
|
s->state = BROTLI_STATE_CONTEXT_MODES;
|
|
|
|
/* No break, continue to next state */
|
|
|
|
}
|
|
|
|
case BROTLI_STATE_CONTEXT_MODES:
|
|
|
|
result = ReadContextModes(s);
|
2016-07-25 08:17:04 +00:00
|
|
|
if (result != BROTLI_DECODER_SUCCESS) {
|
2015-11-06 10:46:04 +00:00
|
|
|
break;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
s->state = BROTLI_STATE_CONTEXT_MAP_1;
|
2015-03-20 16:03:11 +00:00
|
|
|
/* No break, continue to next state */
|
2016-05-31 13:40:59 +00:00
|
|
|
case BROTLI_STATE_CONTEXT_MAP_1:
|
|
|
|
result = DecodeContextMap(
|
2016-06-03 08:51:04 +00:00
|
|
|
s->num_block_types[0] << BROTLI_LITERAL_CONTEXT_BITS,
|
2016-05-31 13:40:59 +00:00
|
|
|
&s->num_literal_htrees, &s->context_map, s);
|
2016-07-25 08:17:04 +00:00
|
|
|
if (result != BROTLI_DECODER_SUCCESS) {
|
2015-08-28 13:20:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-05-31 13:40:59 +00:00
|
|
|
DetectTrivialLiteralBlockTypes(s);
|
2015-03-20 15:13:15 +00:00
|
|
|
s->state = BROTLI_STATE_CONTEXT_MAP_2;
|
2015-03-20 16:03:11 +00:00
|
|
|
/* No break, continue to next state */
|
2015-03-20 15:13:15 +00:00
|
|
|
case BROTLI_STATE_CONTEXT_MAP_2:
|
2015-08-10 11:35:23 +00:00
|
|
|
{
|
2015-10-05 08:23:32 +00:00
|
|
|
uint32_t num_distance_codes =
|
|
|
|
s->num_direct_distance_codes + (48U << s->distance_postfix_bits);
|
2015-08-28 13:20:24 +00:00
|
|
|
result = DecodeContextMap(
|
2016-06-03 08:51:04 +00:00
|
|
|
s->num_block_types[2] << BROTLI_DISTANCE_CONTEXT_BITS,
|
2015-09-23 10:24:06 +00:00
|
|
|
&s->num_dist_htrees, &s->dist_context_map, s);
|
2016-07-25 08:17:04 +00:00
|
|
|
if (result != BROTLI_DECODER_SUCCESS) {
|
2015-08-28 13:20:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderHuffmanTreeGroupInit(
|
|
|
|
s, &s->literal_hgroup, BROTLI_NUM_LITERAL_SYMBOLS,
|
|
|
|
s->num_literal_htrees);
|
|
|
|
BrotliDecoderHuffmanTreeGroupInit(
|
|
|
|
s, &s->insert_copy_hgroup, BROTLI_NUM_COMMAND_SYMBOLS,
|
|
|
|
s->num_block_types[1]);
|
|
|
|
BrotliDecoderHuffmanTreeGroupInit(
|
|
|
|
s, &s->distance_hgroup, num_distance_codes,
|
|
|
|
s->num_dist_htrees);
|
2015-10-02 14:19:30 +00:00
|
|
|
if (s->literal_hgroup.codes == 0 ||
|
|
|
|
s->insert_copy_hgroup.codes == 0 ||
|
|
|
|
s->distance_hgroup.codes == 0) {
|
2016-05-11 13:23:07 +00:00
|
|
|
return SaveErrorCode(s,
|
2016-07-25 08:17:04 +00:00
|
|
|
BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS));
|
2015-10-02 14:19:30 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
s->loop_counter = 0;
|
2015-03-20 15:13:15 +00:00
|
|
|
s->state = BROTLI_STATE_TREE_GROUP;
|
2015-03-20 16:03:11 +00:00
|
|
|
/* No break, continue to next state */
|
2015-03-20 15:13:15 +00:00
|
|
|
case BROTLI_STATE_TREE_GROUP:
|
2015-09-23 10:24:06 +00:00
|
|
|
{
|
|
|
|
HuffmanTreeGroup* hgroup = NULL;
|
2015-11-06 10:46:04 +00:00
|
|
|
switch (s->loop_counter) {
|
2015-09-23 10:24:06 +00:00
|
|
|
case 0:
|
|
|
|
hgroup = &s->literal_hgroup;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
hgroup = &s->insert_copy_hgroup;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
hgroup = &s->distance_hgroup;
|
|
|
|
break;
|
2016-02-18 14:03:44 +00:00
|
|
|
default:
|
2016-07-25 08:17:04 +00:00
|
|
|
return SaveErrorCode(s, BROTLI_FAILURE(
|
|
|
|
BROTLI_DECODER_ERROR_UNREACHABLE));
|
2015-09-23 10:24:06 +00:00
|
|
|
}
|
|
|
|
result = HuffmanTreeGroupDecode(hgroup, s);
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
2016-07-25 08:17:04 +00:00
|
|
|
if (result != BROTLI_DECODER_SUCCESS) break;
|
2015-11-06 10:46:04 +00:00
|
|
|
s->loop_counter++;
|
|
|
|
if (s->loop_counter >= 3) {
|
2016-05-31 13:40:59 +00:00
|
|
|
PrepareLiteralDecoding(s);
|
2015-03-20 15:13:15 +00:00
|
|
|
s->dist_context_map_slice = s->dist_context_map;
|
2015-08-10 11:35:23 +00:00
|
|
|
s->htree_command = s->insert_copy_hgroup.htrees[0];
|
2016-01-22 09:19:41 +00:00
|
|
|
if (!s->ringbuffer && !BrotliAllocateRingBuffer(s)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2);
|
2016-01-22 09:19:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
s->state = BROTLI_STATE_COMMAND_BEGIN;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-08-28 13:20:24 +00:00
|
|
|
case BROTLI_STATE_COMMAND_BEGIN:
|
|
|
|
case BROTLI_STATE_COMMAND_INNER:
|
2015-11-06 10:46:04 +00:00
|
|
|
case BROTLI_STATE_COMMAND_POST_DECODE_LITERALS:
|
2015-08-28 13:20:24 +00:00
|
|
|
case BROTLI_STATE_COMMAND_POST_WRAP_COPY:
|
2015-11-06 10:46:04 +00:00
|
|
|
result = ProcessCommands(s);
|
2016-07-25 08:17:04 +00:00
|
|
|
if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) {
|
2015-11-06 10:46:04 +00:00
|
|
|
result = SafeProcessCommands(s);
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-08-28 13:20:24 +00:00
|
|
|
case BROTLI_STATE_COMMAND_INNER_WRITE:
|
|
|
|
case BROTLI_STATE_COMMAND_POST_WRITE_1:
|
|
|
|
case BROTLI_STATE_COMMAND_POST_WRITE_2:
|
2016-07-25 08:17:04 +00:00
|
|
|
result = WriteRingBuffer(s, available_out, next_out, total_out);
|
|
|
|
if (result != BROTLI_DECODER_SUCCESS) {
|
2015-04-22 15:33:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
s->max_distance = s->max_backward_distance;
|
2015-08-28 13:20:24 +00:00
|
|
|
if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_1) {
|
2015-11-06 10:46:04 +00:00
|
|
|
memcpy(s->ringbuffer, s->ringbuffer_end, (size_t)s->pos);
|
2016-01-22 09:19:41 +00:00
|
|
|
if (s->meta_block_remaining_len == 0) {
|
2015-08-28 13:20:24 +00:00
|
|
|
/* Next metablock, if any */
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
} else {
|
2015-11-06 10:46:04 +00:00
|
|
|
s->state = BROTLI_STATE_COMMAND_BEGIN;
|
2015-08-28 13:20:24 +00:00
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
break;
|
2015-08-28 13:20:24 +00:00
|
|
|
} else if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_2) {
|
|
|
|
s->state = BROTLI_STATE_COMMAND_POST_WRAP_COPY;
|
|
|
|
} else { /* BROTLI_STATE_COMMAND_INNER_WRITE */
|
2015-11-06 10:46:04 +00:00
|
|
|
if (s->loop_counter == 0) {
|
2016-01-22 09:19:41 +00:00
|
|
|
if (s->meta_block_remaining_len == 0) {
|
2015-08-28 13:20:24 +00:00
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
2015-11-06 10:46:04 +00:00
|
|
|
} else {
|
|
|
|
s->state = BROTLI_STATE_COMMAND_POST_DECODE_LITERALS;
|
2015-08-28 13:20:24 +00:00
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
break;
|
2015-08-28 13:20:24 +00:00
|
|
|
}
|
|
|
|
s->state = BROTLI_STATE_COMMAND_INNER;
|
2015-04-22 15:33:21 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-03-20 15:13:15 +00:00
|
|
|
case BROTLI_STATE_METABLOCK_DONE:
|
2016-05-31 13:40:59 +00:00
|
|
|
if (s->meta_block_remaining_len < 0) {
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2);
|
2016-05-31 13:40:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-07-25 08:17:04 +00:00
|
|
|
BrotliDecoderStateCleanupAfterMetablock(s);
|
2015-09-23 10:24:06 +00:00
|
|
|
if (!s->is_last_metablock) {
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_BEGIN;
|
|
|
|
break;
|
|
|
|
}
|
2015-11-06 10:46:04 +00:00
|
|
|
if (!BrotliJumpToByteBoundary(br)) {
|
2016-07-25 08:17:04 +00:00
|
|
|
result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_PADDING_2);
|
2016-05-11 13:23:07 +00:00
|
|
|
break;
|
2015-11-06 10:46:04 +00:00
|
|
|
}
|
|
|
|
if (s->buffer_length == 0) {
|
|
|
|
BrotliBitReaderUnload(br);
|
|
|
|
*available_in = br->avail_in;
|
|
|
|
*next_in = br->next_in;
|
|
|
|
}
|
2015-09-23 10:24:06 +00:00
|
|
|
s->state = BROTLI_STATE_DONE;
|
|
|
|
/* No break, continue to next state */
|
2015-03-20 15:13:15 +00:00
|
|
|
case BROTLI_STATE_DONE:
|
|
|
|
if (s->ringbuffer != 0) {
|
2016-07-25 08:17:04 +00:00
|
|
|
result = WriteRingBuffer(s, available_out, next_out, total_out);
|
|
|
|
if (result != BROTLI_DECODER_SUCCESS) {
|
2015-04-22 15:33:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2016-05-11 13:23:07 +00:00
|
|
|
return SaveErrorCode(s, result);
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-11 13:23:07 +00:00
|
|
|
return SaveErrorCode(s, result);
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
void BrotliDecoderSetCustomDictionary(
|
|
|
|
BrotliDecoderState* s, size_t size, const uint8_t* dict) {
|
2016-04-27 14:52:24 +00:00
|
|
|
if (size > (1u << 24)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-12 13:43:54 +00:00
|
|
|
s->custom_dict = dict;
|
2016-02-18 14:03:44 +00:00
|
|
|
s->custom_dict_size = (int)size;
|
2015-06-12 13:43:54 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
BROTLI_BOOL BrotliDecoderHasMoreOutput(const BrotliDecoderState* s) {
|
|
|
|
return TO_BROTLI_BOOL(
|
|
|
|
s->ringbuffer != 0 && UnwrittenBytes(s, BROTLI_FALSE) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
BROTLI_BOOL BrotliDecoderIsUsed(const BrotliDecoderState* s) {
|
|
|
|
return TO_BROTLI_BOOL(s->state != BROTLI_STATE_UNINITED ||
|
|
|
|
BrotliGetAvailableBits(&s->br) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
BROTLI_BOOL BrotliDecoderIsFinished(const BrotliDecoderState* s) {
|
|
|
|
return TO_BROTLI_BOOL(s->state == BROTLI_STATE_DONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
BrotliDecoderErrorCode BrotliDecoderGetErrorCode(const BrotliDecoderState* s) {
|
|
|
|
return (BrotliDecoderErrorCode)s->error_code;
|
2016-05-11 13:23:07 +00:00
|
|
|
}
|
2015-09-23 10:24:06 +00:00
|
|
|
|
2016-07-25 08:17:04 +00:00
|
|
|
const char* BrotliDecoderErrorString(BrotliDecoderErrorCode c) {
|
2016-06-06 09:19:09 +00:00
|
|
|
switch (c) {
|
|
|
|
#define _BROTLI_ERROR_CODE_CASE(PREFIX, NAME, CODE) \
|
2016-07-25 08:17:04 +00:00
|
|
|
case BROTLI_DECODER ## PREFIX ## NAME: return #NAME;
|
2016-06-06 09:19:09 +00:00
|
|
|
#define _BROTLI_NOTHING
|
2016-07-25 08:17:04 +00:00
|
|
|
BROTLI_DECODER_ERROR_CODES_LIST(_BROTLI_ERROR_CODE_CASE, _BROTLI_NOTHING)
|
2016-06-06 09:19:09 +00:00
|
|
|
#undef _BROTLI_ERROR_CODE_CASE
|
|
|
|
#undef _BROTLI_NOTHING
|
|
|
|
default: return "INVALID";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-11 08:26:07 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
2016-02-18 14:03:44 +00:00
|
|
|
} /* extern "C" */
|
2013-10-11 08:26:07 +00:00
|
|
|
#endif
|