2013-12-16 13:45:57 +00:00
|
|
|
/* Copyright 2013 Google Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2013-10-11 08:26:07 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2013-11-19 22:32:56 +00:00
|
|
|
#include <string.h>
|
2013-10-11 08:26:07 +00:00
|
|
|
#include "./bit_reader.h"
|
|
|
|
#include "./context.h"
|
|
|
|
#include "./decode.h"
|
2014-02-17 13:25:36 +00:00
|
|
|
#include "./dictionary.h"
|
2015-05-07 14:53:43 +00:00
|
|
|
#include "./port.h"
|
2014-02-17 13:25:36 +00:00
|
|
|
#include "./transform.h"
|
2013-10-11 08:26:07 +00:00
|
|
|
#include "./huffman.h"
|
|
|
|
#include "./prefix.h"
|
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
#ifdef __ARM_NEON__
|
|
|
|
#include <arm_neon.h>
|
|
|
|
#endif
|
|
|
|
|
2013-10-11 08:26:07 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BROTLI_DECODE_DEBUG
|
|
|
|
#define BROTLI_LOG_UINT(name) \
|
2013-11-15 18:02:17 +00:00
|
|
|
printf("[%s] %s = %lu\n", __func__, #name, (unsigned long)(name))
|
2013-10-11 08:26:07 +00:00
|
|
|
#define BROTLI_LOG_ARRAY_INDEX(array_name, idx) \
|
2013-10-22 13:02:54 +00:00
|
|
|
printf("[%s] %s[%lu] = %lu\n", __func__, #array_name, \
|
2013-11-15 18:02:17 +00:00
|
|
|
(unsigned long)(idx), (unsigned long)array_name[idx])
|
2015-06-26 15:37:00 +00:00
|
|
|
#define BROTLI_LOG(x) printf x
|
2013-10-11 08:26:07 +00:00
|
|
|
#else
|
|
|
|
#define BROTLI_LOG_UINT(name)
|
|
|
|
#define BROTLI_LOG_ARRAY_INDEX(array_name, idx)
|
2015-06-26 15:37:00 +00:00
|
|
|
#define BROTLI_LOG(x)
|
2013-10-11 08:26:07 +00:00
|
|
|
#endif
|
|
|
|
|
2014-01-08 11:34:35 +00:00
|
|
|
static const uint8_t kDefaultCodeLength = 8;
|
|
|
|
static const uint8_t kCodeLengthRepeatCode = 16;
|
2013-10-11 08:26:07 +00:00
|
|
|
static const int kNumLiteralCodes = 256;
|
|
|
|
static const int kNumInsertAndCopyCodes = 704;
|
|
|
|
static const int kNumBlockLengthCodes = 26;
|
2013-11-15 18:02:17 +00:00
|
|
|
static const int kLiteralContextBits = 6;
|
|
|
|
static const int kDistanceContextBits = 2;
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2014-02-14 14:04:23 +00:00
|
|
|
#define HUFFMAN_TABLE_BITS 8
|
|
|
|
#define HUFFMAN_TABLE_MASK 0xff
|
|
|
|
|
2013-12-12 12:18:04 +00:00
|
|
|
#define CODE_LENGTH_CODES 18
|
2013-10-11 08:26:07 +00:00
|
|
|
static const uint8_t kCodeLengthCodeOrder[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,
|
|
|
|
};
|
|
|
|
|
2013-10-11 08:26:07 +00:00
|
|
|
#define NUM_DISTANCE_SHORT_CODES 16
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
static BROTLI_INLINE BROTLI_NO_ASAN void memmove16(
|
|
|
|
uint8_t* dst, uint8_t* src) {
|
2015-09-28 10:38:29 +00:00
|
|
|
#if BROTLI_SAFE_MEMMOVE
|
|
|
|
/* For x86 this compiles to the same binary as signle memcpy.
|
|
|
|
On ARM memcpy is not inlined, so it works slower.
|
|
|
|
This implementation makes decompression 1% slower than regular one,
|
|
|
|
and 2% slower than NEON implementation.
|
|
|
|
*/
|
|
|
|
uint32_t buffer[4];
|
|
|
|
memcpy(buffer, src, 16);
|
|
|
|
memcpy(dst, buffer, 16);
|
|
|
|
#elif defined(__ARM_NEON__)
|
2015-08-10 11:35:23 +00:00
|
|
|
vst1q_u8(dst, vld1q_u8(src));
|
|
|
|
#else
|
|
|
|
/* memcpy is unsafe for overlapping regions and ASAN detects this.
|
|
|
|
But, because of optimizations, it works exactly as memmove:
|
|
|
|
copies data to registers first, and then stores them to dst. */
|
|
|
|
memcpy(dst, src, 16);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-12-16 13:45:57 +00:00
|
|
|
/* Decodes a number in the range [0..255], by reading 1 - 11 bits. */
|
2015-09-24 16:35:25 +00:00
|
|
|
static BROTLI_NOINLINE BrotliResult DecodeVarLenUint8(BrotliState* s,
|
|
|
|
BrotliBitReader* br, int* value) {
|
|
|
|
uint32_t bits;
|
|
|
|
switch (s->substate_decode_uint8) {
|
|
|
|
case BROTLI_STATE_DECODE_UINT8_NONE:
|
|
|
|
if (PREDICT_FALSE(!BrotliSafeReadBits(br, 1, &bits))) {
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
}
|
|
|
|
if (bits == 0) {
|
|
|
|
*value = 0;
|
|
|
|
return BROTLI_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
/* 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;
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
}
|
|
|
|
if (bits == 0) {
|
|
|
|
*value = 1;
|
|
|
|
s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
|
|
|
|
return BROTLI_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
/* Use output value as a temporary storage. It MUST be persisted. */
|
|
|
|
*value = (int)bits;
|
|
|
|
/* 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;
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
}
|
|
|
|
*value = (1 << *value) + (int)bits;
|
|
|
|
s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
|
|
|
|
return BROTLI_RESULT_SUCCESS;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return BROTLI_FAILURE();
|
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. */
|
|
|
|
static BrotliResult BROTLI_NOINLINE DecodeMetaBlockLength(BrotliState* s,
|
|
|
|
BrotliBitReader* br) {
|
|
|
|
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)) {
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
}
|
|
|
|
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)) {
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
}
|
|
|
|
if (bits) {
|
|
|
|
s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
|
|
|
|
return BROTLI_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
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)) {
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
}
|
|
|
|
if (i + 1 == s->size_nibbles && s->size_nibbles > 4 && bits == 0) {
|
|
|
|
return BROTLI_FAILURE();
|
|
|
|
}
|
|
|
|
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:
|
|
|
|
if (!s->is_last_metablock && !s->is_metadata) {
|
|
|
|
if (!BrotliSafeReadBits(br, 1, &bits)) {
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
}
|
|
|
|
s->is_uncompressed = (uint8_t)bits;
|
|
|
|
}
|
|
|
|
++s->meta_block_remaining_len;
|
|
|
|
s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
|
|
|
|
return BROTLI_RESULT_SUCCESS;
|
|
|
|
|
|
|
|
case BROTLI_STATE_METABLOCK_HEADER_RESERVED:
|
|
|
|
if (!BrotliSafeReadBits(br, 1, &bits)) {
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
}
|
|
|
|
if (bits != 0) {
|
|
|
|
return BROTLI_FAILURE();
|
|
|
|
}
|
|
|
|
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)) {
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
}
|
|
|
|
if (bits == 0) {
|
|
|
|
s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
|
|
|
|
return BROTLI_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
}
|
|
|
|
if (i + 1 == s->size_nibbles && s->size_nibbles > 1 && bits == 0) {
|
|
|
|
return BROTLI_FAILURE();
|
|
|
|
}
|
|
|
|
s->meta_block_remaining_len |= (int)(bits << (i * 8));
|
|
|
|
}
|
|
|
|
s->substate_metablock_header =
|
|
|
|
BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-08-28 13:20:24 +00:00
|
|
|
return BROTLI_FAILURE();
|
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-09-23 10:24:06 +00:00
|
|
|
/* Decodes the next Huffman code from bit-stream. Reads 0 - 15 bits. */
|
2014-02-14 14:04:23 +00:00
|
|
|
static BROTLI_INLINE int ReadSymbol(const HuffmanCode* table,
|
2013-10-11 08:26:07 +00:00
|
|
|
BrotliBitReader* br) {
|
2015-08-10 11:35:23 +00:00
|
|
|
/* Read the bits for two reads at once. */
|
2015-08-28 13:20:24 +00:00
|
|
|
uint32_t val = BrotliGetBitsUnmasked(br, 15);
|
2015-05-07 15:10:27 +00:00
|
|
|
table += val & HUFFMAN_TABLE_MASK;
|
2015-08-28 13:20:24 +00:00
|
|
|
if (table->bits > HUFFMAN_TABLE_BITS) {
|
2015-08-10 11:35:23 +00:00
|
|
|
int nbits = table->bits - HUFFMAN_TABLE_BITS;
|
|
|
|
BrotliDropBits(br, HUFFMAN_TABLE_BITS);
|
2014-02-14 14:04:23 +00:00
|
|
|
table += table->value;
|
2015-08-28 13:20:24 +00:00
|
|
|
table += (int)(val >> HUFFMAN_TABLE_BITS) & (int)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-09-23 10:24:06 +00:00
|
|
|
/* Makes a look-up in first level Huffman table. Peeks 8 bits. */
|
2015-08-10 11:35:23 +00:00
|
|
|
static BROTLI_INLINE void PreloadSymbol(const HuffmanCode* table,
|
|
|
|
BrotliBitReader* br,
|
|
|
|
unsigned* bits,
|
|
|
|
unsigned* value) {
|
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-08-10 11:35:23 +00:00
|
|
|
static BROTLI_INLINE unsigned ReadPreloadedSymbol(const HuffmanCode* table,
|
|
|
|
BrotliBitReader* br,
|
|
|
|
unsigned* bits,
|
|
|
|
unsigned* value) {
|
|
|
|
unsigned result = *value;
|
|
|
|
if (PREDICT_FALSE(*bits > HUFFMAN_TABLE_BITS)) {
|
2015-08-28 13:20:24 +00:00
|
|
|
uint32_t val = BrotliGetBitsUnmasked(br, 15);
|
2015-08-10 11:35:23 +00:00
|
|
|
const HuffmanCode* ext = table + (val & HUFFMAN_TABLE_MASK) + *value;
|
2015-08-28 13:20:24 +00:00
|
|
|
int mask = (int)BitMask((int)(*bits - HUFFMAN_TABLE_BITS));
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliDropBits(br, HUFFMAN_TABLE_BITS);
|
|
|
|
ext += (int)(val >> HUFFMAN_TABLE_BITS) & mask;
|
|
|
|
BrotliDropBits(br, ext->bits);
|
|
|
|
result = ext->value;
|
|
|
|
} else {
|
2015-08-28 13:20:24 +00:00
|
|
|
BrotliDropBits(br, (int)*bits);
|
2013-12-12 12:18:04 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
PreloadSymbol(table, br, bits, value);
|
|
|
|
return result;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 16:35:25 +00:00
|
|
|
static BROTLI_INLINE int Log2Floor(int x) {
|
|
|
|
int result = 0;
|
|
|
|
while (x) {
|
|
|
|
x >>= 1;
|
|
|
|
++result;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2015-03-27 12:54:43 +00:00
|
|
|
static BrotliResult ReadHuffmanCode(int alphabet_size,
|
|
|
|
HuffmanCode* table,
|
|
|
|
int* opt_table_size,
|
|
|
|
BrotliState* s) {
|
2015-03-20 15:13:15 +00:00
|
|
|
BrotliBitReader* br = &s->br;
|
2015-09-24 16:35:25 +00:00
|
|
|
int i;
|
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:
|
|
|
|
if (!BrotliCheckInputAmount(br, 32)) {
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
2015-08-28 13:20:24 +00:00
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
i = (int)BrotliReadBits(br, 2);
|
|
|
|
/* The value is used as follows:
|
|
|
|
1 for simple code;
|
|
|
|
0 for no skipping, 2 skips 2 code lengths, 3 skips 3 code lengths */
|
|
|
|
BROTLI_LOG_UINT((unsigned)i);
|
|
|
|
if (i == 1) {
|
|
|
|
/* Read symbols, codes & code lengths directly. */
|
|
|
|
int max_bits = Log2Floor(alphabet_size - 1);
|
|
|
|
uint32_t num_symbols = BrotliReadBits(br, 2);
|
|
|
|
for (i = 0; i < 4; ++i) {
|
|
|
|
s->symbols_lists_array[i] = 0;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
i = 0;
|
|
|
|
/* max_bits == 0..10; symbol == 0..3; 0..40 bits will be read. */
|
|
|
|
do {
|
|
|
|
uint32_t v = BrotliReadBits(br, max_bits);
|
|
|
|
if (v >= alphabet_size) {
|
2015-08-10 11:35:23 +00:00
|
|
|
return BROTLI_FAILURE();
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
s->symbols_lists_array[i] = (uint16_t)v;
|
|
|
|
BROTLI_LOG_UINT(s->symbols_lists_array[i]);
|
|
|
|
} while (++i <= num_symbols);
|
|
|
|
for (i = 0; i < num_symbols; ++i) {
|
|
|
|
int k = i + 1;
|
|
|
|
for (; k <= num_symbols; ++k) {
|
|
|
|
if (s->symbols_lists_array[i] == s->symbols_lists_array[k]) {
|
|
|
|
return BROTLI_FAILURE();
|
|
|
|
}
|
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
if (num_symbols == 3) {
|
|
|
|
num_symbols += BrotliReadBits(br, 1);
|
|
|
|
}
|
|
|
|
BROTLI_LOG_UINT(num_symbols);
|
|
|
|
i = BrotliBuildSimpleHuffmanTable(
|
|
|
|
table, HUFFMAN_TABLE_BITS, s->symbols_lists_array, num_symbols);
|
|
|
|
if (opt_table_size) {
|
|
|
|
*opt_table_size = i;
|
|
|
|
}
|
|
|
|
s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
|
|
|
|
return BROTLI_RESULT_SUCCESS;
|
|
|
|
} else { /* Decode Huffman-coded code lengths. */
|
|
|
|
int8_t num_codes = 0;
|
|
|
|
unsigned space = 32;
|
|
|
|
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,
|
|
|
|
sizeof(s->code_length_code_lengths));
|
|
|
|
/* 15..18 codes will be read, 2..4 bits each; 30..72 bits totally. */
|
|
|
|
for (; i < CODE_LENGTH_CODES; ++i) {
|
|
|
|
const uint8_t code_len_idx = kCodeLengthCodeOrder[i];
|
|
|
|
uint8_t ix = (uint8_t)BrotliGetBits(br, 4);
|
|
|
|
uint8_t v = kCodeLengthPrefixValue[ix];
|
|
|
|
BrotliDropBits(br, kCodeLengthPrefixLength[ix]);
|
|
|
|
s->code_length_code_lengths[code_len_idx] = 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;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
if (!(num_codes == 1 || space == 0)) {
|
|
|
|
return BROTLI_FAILURE();
|
|
|
|
}
|
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) {
|
|
|
|
s->next_symbol[i] = i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
|
|
|
|
s->symbol_lists[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;
|
|
|
|
s->prev_code_len = kDefaultCodeLength;
|
|
|
|
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. */
|
|
|
|
case BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS: {
|
|
|
|
uint32_t symbol = s->symbol;
|
|
|
|
uint32_t repeat = s->repeat;
|
|
|
|
uint32_t space = s->space;
|
|
|
|
uint8_t prev_code_len = s->prev_code_len;
|
|
|
|
uint8_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;
|
|
|
|
while (symbol < alphabet_size && space > 0) {
|
|
|
|
const HuffmanCode* p = s->table;
|
|
|
|
uint8_t code_len;
|
|
|
|
if (!BrotliCheckInputAmount(br, 8)) {
|
|
|
|
s->symbol = symbol;
|
|
|
|
s->repeat = repeat;
|
|
|
|
s->prev_code_len = prev_code_len;
|
|
|
|
s->repeat_code_len = repeat_code_len;
|
|
|
|
s->space = space;
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
}
|
|
|
|
p += BrotliGetBits(br, BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH);
|
|
|
|
BrotliDropBits(br, p->bits); /* Use 1..5 bits */
|
|
|
|
code_len = (uint8_t)p->value; /* code_len == 0..17 */
|
|
|
|
if (code_len < kCodeLengthRepeatCode) {
|
2015-08-28 13:20:24 +00:00
|
|
|
repeat = 0;
|
2015-09-24 16:35:25 +00:00
|
|
|
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]++;
|
|
|
|
}
|
|
|
|
symbol++;
|
|
|
|
} else { /* code_len == 16..17, extra_bits == 2..3 */
|
|
|
|
uint32_t repeat_delta = BrotliReadBits(br, code_len - 14);
|
|
|
|
uint32_t old_repeat;
|
|
|
|
uint8_t new_len = 0;
|
|
|
|
if (code_len == kCodeLengthRepeatCode) {
|
|
|
|
new_len = prev_code_len;
|
|
|
|
}
|
|
|
|
if (repeat_code_len != new_len) {
|
|
|
|
repeat = 0;
|
|
|
|
repeat_code_len = new_len;
|
|
|
|
}
|
|
|
|
old_repeat = repeat;
|
|
|
|
if (repeat > 0) {
|
|
|
|
repeat -= 2;
|
|
|
|
repeat <<= code_len - 14;
|
|
|
|
}
|
|
|
|
repeat += repeat_delta + 3;
|
|
|
|
repeat_delta = repeat - old_repeat; /* repeat_delta >= 3 */
|
|
|
|
/* So, for extra 2..3 bits we produce more than 2 symbols.
|
|
|
|
Consequently, at most 5 bits per symbol are used. */
|
|
|
|
if (symbol + repeat_delta > alphabet_size) {
|
|
|
|
return BROTLI_FAILURE();
|
|
|
|
}
|
|
|
|
if (repeat_code_len != 0) {
|
|
|
|
unsigned last = symbol + repeat_delta;
|
|
|
|
i = next_symbol[repeat_code_len];
|
|
|
|
do {
|
|
|
|
symbol_lists[i] = (uint16_t)symbol;
|
|
|
|
i = (int)symbol;
|
|
|
|
} while (++symbol != last);
|
|
|
|
next_symbol[repeat_code_len] = i;
|
|
|
|
space -= repeat_delta << (15 - repeat_code_len);
|
|
|
|
code_length_histo[repeat_code_len] = (uint16_t)
|
|
|
|
(code_length_histo[repeat_code_len] + repeat_delta);
|
|
|
|
} else {
|
|
|
|
symbol += repeat_delta;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
}
|
|
|
|
if (space != 0) {
|
|
|
|
BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", space));
|
|
|
|
return BROTLI_FAILURE();
|
|
|
|
}
|
|
|
|
{
|
|
|
|
int table_size = BrotliBuildHuffmanTable(
|
|
|
|
table, HUFFMAN_TABLE_BITS, symbol_lists,
|
|
|
|
s->code_length_histo);
|
|
|
|
if (opt_table_size) {
|
|
|
|
*opt_table_size = table_size;
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE;
|
|
|
|
return BROTLI_RESULT_SUCCESS;
|
2015-08-28 13:20:24 +00:00
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return BROTLI_FAILURE();
|
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. */
|
2014-02-14 14:04:23 +00:00
|
|
|
static BROTLI_INLINE int ReadBlockLength(const HuffmanCode* table,
|
|
|
|
BrotliBitReader* br) {
|
2013-10-22 13:02:54 +00:00
|
|
|
int code;
|
|
|
|
int 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 */
|
2014-01-08 11:34:35 +00:00
|
|
|
return kBlockLengthPrefixCode[code].offset + (int)BrotliReadBits(br, nbits);
|
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.
|
|
|
|
*/
|
|
|
|
static BROTLI_NOINLINE void InverseMoveToFrontTransform(uint8_t* v, int v_len,
|
|
|
|
BrotliState* state) {
|
|
|
|
/* Reinitialize elements that could have been changed. */
|
|
|
|
int i = 4;
|
|
|
|
int upper_bound = state->mtf_upper_bound;
|
|
|
|
uint8_t* mtf = state->mtf;
|
|
|
|
/* 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];
|
|
|
|
v[i] = value;
|
2015-08-10 11:35:23 +00:00
|
|
|
upper_bound |= index;
|
|
|
|
do {
|
|
|
|
index--;
|
|
|
|
mtf[index + 1] = mtf[index];
|
|
|
|
} while (index > 0);
|
2015-04-22 12:25:08 +00:00
|
|
|
mtf[0] = value;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Expose function for testing. Will be removed by linker as unused. */
|
|
|
|
void InverseMoveToFrontTransformForTesting(uint8_t* v, int l, BrotliState* s) {
|
|
|
|
InverseMoveToFrontTransform(v, l, s);
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 13:20:24 +00:00
|
|
|
|
2015-09-23 10:24:06 +00:00
|
|
|
/* Decodes a series of Huffman table using ReadHuffmanCode function. */
|
2015-03-27 12:54:43 +00:00
|
|
|
static BrotliResult HuffmanTreeGroupDecode(HuffmanTreeGroup* group,
|
|
|
|
BrotliState* 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) {
|
|
|
|
int table_size;
|
|
|
|
BrotliResult result =
|
|
|
|
ReadHuffmanCode(group->alphabet_size, s->next, &table_size, s);
|
|
|
|
if (result != BROTLI_RESULT_SUCCESS) return result;
|
|
|
|
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;
|
2015-08-28 13:20:24 +00:00
|
|
|
return BROTLI_RESULT_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.
|
|
|
|
*/
|
2015-03-27 12:54:43 +00:00
|
|
|
static BrotliResult DecodeContextMap(int context_map_size,
|
|
|
|
int* num_htrees,
|
2015-08-28 13:20:24 +00:00
|
|
|
uint8_t** context_map_arg,
|
2015-03-27 12:54:43 +00:00
|
|
|
BrotliState* s) {
|
2015-03-20 15:13:15 +00:00
|
|
|
BrotliBitReader* br = &s->br;
|
2015-03-27 12:54:43 +00:00
|
|
|
BrotliResult result = BROTLI_RESULT_SUCCESS;
|
2014-02-14 14:04:23 +00:00
|
|
|
int use_rle_for_zeros;
|
2013-10-11 08:26:07 +00:00
|
|
|
|
2015-09-23 10:24:06 +00:00
|
|
|
switch((int)s->substate_context_map) {
|
|
|
|
case BROTLI_STATE_CONTEXT_MAP_NONE:
|
2015-09-24 16:35:25 +00:00
|
|
|
result = DecodeVarLenUint8(s, br, num_htrees);
|
|
|
|
if (result != BROTLI_RESULT_SUCCESS) {
|
|
|
|
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-08-28 13:20:24 +00:00
|
|
|
*context_map_arg = (uint8_t*)malloc((size_t)context_map_size);
|
|
|
|
if (*context_map_arg == 0) {
|
2015-06-26 15:37:00 +00:00
|
|
|
return BROTLI_FAILURE();
|
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);
|
2015-03-27 12:54:43 +00:00
|
|
|
return BROTLI_RESULT_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. */
|
|
|
|
case BROTLI_STATE_CONTEXT_MAP_READ_PREFIX:
|
|
|
|
if (!BrotliWarmupBitReader(br) || !BrotliCheckInputAmount(br, 8)) {
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
use_rle_for_zeros = (int)BrotliReadBits(br, 1);
|
|
|
|
if (use_rle_for_zeros) {
|
2015-03-30 16:00:40 +00:00
|
|
|
s->max_run_length_prefix = (int)BrotliReadBits(br, 4) + 1;
|
|
|
|
} else {
|
|
|
|
s->max_run_length_prefix = 0;
|
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-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);
|
2015-03-27 12:54:43 +00:00
|
|
|
if (result != BROTLI_RESULT_SUCCESS) return result;
|
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-08-10 11:35:23 +00:00
|
|
|
int context_index = s->context_index;
|
2015-08-28 13:20:24 +00:00
|
|
|
int max_run_length_prefix = s->max_run_length_prefix;
|
|
|
|
uint8_t* context_map = *context_map_arg;
|
|
|
|
int code;
|
2015-08-10 11:35:23 +00:00
|
|
|
while (context_index < context_map_size) {
|
|
|
|
if (!BrotliCheckInputAmount(br, 32)) {
|
|
|
|
s->context_index = context_index;
|
2015-04-22 15:33:21 +00:00
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
code = ReadSymbol(s->context_map_table, br);
|
2015-09-24 16:35:25 +00:00
|
|
|
BROTLI_LOG_UINT(code);
|
2015-03-20 15:13:15 +00:00
|
|
|
if (code == 0) {
|
2015-08-28 13:20:24 +00:00
|
|
|
context_map[context_index++] = 0;
|
|
|
|
} else if (code - max_run_length_prefix <= 0) {
|
|
|
|
int reps = (1 << code) + (int)BrotliReadBits(br, 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) {
|
|
|
|
return BROTLI_FAILURE();
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
do {
|
|
|
|
context_map[context_index++] = 0;
|
|
|
|
} while (--reps);
|
2015-03-20 15:13:15 +00:00
|
|
|
} else {
|
2015-08-28 13:20:24 +00:00
|
|
|
context_map[context_index++] =
|
|
|
|
(uint8_t)(code - max_run_length_prefix);
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
if (BrotliReadBits(br, 1)) {
|
2015-08-28 13:20:24 +00:00
|
|
|
InverseMoveToFrontTransform(context_map, 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;
|
2015-03-27 12:54:43 +00:00
|
|
|
return BROTLI_RESULT_SUCCESS;
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
|
2015-06-26 15:37:00 +00:00
|
|
|
return BROTLI_FAILURE();
|
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.
|
|
|
|
Reads 0..15 bits. */
|
2015-08-10 11:35:23 +00:00
|
|
|
static void DecodeBlockType(const int max_block_type,
|
|
|
|
const HuffmanCode* trees,
|
|
|
|
int tree_type,
|
|
|
|
int* ringbuffers,
|
|
|
|
BrotliBitReader* br) {
|
2013-10-11 08:26:07 +00:00
|
|
|
int* ringbuffer = ringbuffers + tree_type * 2;
|
2015-08-10 11:35:23 +00:00
|
|
|
int block_type =
|
|
|
|
ReadSymbol(&trees[tree_type * BROTLI_HUFFMAN_MAX_TABLE_SIZE], br) - 2;
|
|
|
|
if (block_type == -1) {
|
|
|
|
block_type = ringbuffer[1] + 1;
|
|
|
|
} else if (block_type == -2) {
|
|
|
|
block_type = ringbuffer[0];
|
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;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 10:24:06 +00:00
|
|
|
/* Decodes the block type and updates the state for literal context.
|
|
|
|
Reads 18..54 bits. */
|
2015-08-10 11:35:23 +00:00
|
|
|
static void DecodeBlockTypeWithContext(BrotliState* s,
|
|
|
|
BrotliBitReader* br) {
|
|
|
|
uint8_t context_mode;
|
|
|
|
int context_offset;
|
2015-08-28 13:20:24 +00:00
|
|
|
DecodeBlockType(s->num_block_types[0], s->block_type_trees, 0,
|
2015-09-23 10:24:06 +00:00
|
|
|
s->block_type_rb, br); /* Reads 0..15 bits. */
|
|
|
|
s->block_length[0] = ReadBlockLength(s->block_len_trees, br); /* 3..39 bits */
|
2015-08-10 11:35:23 +00:00
|
|
|
context_offset = s->block_type_rb[1] << kLiteralContextBits;
|
|
|
|
s->context_map_slice = s->context_map + context_offset;
|
2015-04-01 14:15:39 +00:00
|
|
|
s->literal_htree_index = s->context_map_slice[0];
|
2015-08-28 13:20:24 +00:00
|
|
|
s->literal_htree = s->literal_hgroup.htrees[s->literal_htree_index];
|
2015-08-10 11:35:23 +00:00
|
|
|
context_mode = s->context_modes[s->block_type_rb[1]];
|
2015-08-28 13:20:24 +00:00
|
|
|
s->context_lookup1 = &kContextLookup[kContextLookupOffsets[context_mode]];
|
|
|
|
s->context_lookup2 = &kContextLookup[kContextLookupOffsets[context_mode + 1]];
|
2015-04-01 14:15:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliResult WriteRingBuffer(BrotliOutput output,
|
|
|
|
BrotliState* s) {
|
2015-09-24 16:35:25 +00:00
|
|
|
int num_written;
|
|
|
|
if (s->meta_block_remaining_len < 0) {
|
|
|
|
return BROTLI_FAILURE();
|
|
|
|
}
|
|
|
|
num_written = BrotliWrite(
|
2015-08-10 11:35:23 +00:00
|
|
|
output, s->ringbuffer + s->partially_written,
|
|
|
|
(size_t)(s->to_write - s->partially_written));
|
2015-09-24 16:35:25 +00:00
|
|
|
BROTLI_LOG_UINT(s->partially_written);
|
|
|
|
BROTLI_LOG_UINT(s->to_write);
|
|
|
|
BROTLI_LOG_UINT(num_written);
|
2015-08-10 11:35:23 +00:00
|
|
|
if (num_written < 0) {
|
|
|
|
return BROTLI_FAILURE();
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
s->partially_written += num_written;
|
|
|
|
if (s->partially_written < s->to_write) {
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_OUTPUT;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
return BROTLI_RESULT_SUCCESS;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliResult BROTLI_NOINLINE CopyUncompressedBlockToOutput(BrotliOutput output,
|
|
|
|
int pos,
|
|
|
|
BrotliState* s) {
|
|
|
|
BrotliResult result;
|
2015-03-20 15:13:15 +00:00
|
|
|
int num_read;
|
2015-09-28 10:40:39 +00:00
|
|
|
int nbytes;
|
2015-03-20 15:13:15 +00:00
|
|
|
/* State machine */
|
|
|
|
for (;;) {
|
2015-09-23 10:24:06 +00:00
|
|
|
switch ((int)s->substate_uncompressed) {
|
|
|
|
case BROTLI_STATE_UNCOMPRESSED_NONE:
|
2015-03-20 15:13:15 +00:00
|
|
|
/* For short lengths copy byte-by-byte */
|
2015-08-10 11:35:23 +00:00
|
|
|
if (s->meta_block_remaining_len < 8 ||
|
|
|
|
s->meta_block_remaining_len < BrotliGetRemainingBytes(&s->br)) {
|
2015-09-23 10:24:06 +00:00
|
|
|
s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_SHORT;
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Copy remaining bytes from s->br.buf_ to ringbuffer. */
|
2015-09-28 10:40:39 +00:00
|
|
|
nbytes = (int)BrotliGetRemainingBytes(&s->br);
|
|
|
|
BrotliCopyBytes(&s->ringbuffer[pos], &s->br, (size_t)nbytes);
|
|
|
|
pos += nbytes;
|
|
|
|
s->meta_block_remaining_len -= nbytes;
|
2015-08-10 11:35:23 +00:00
|
|
|
if (pos >= s->ringbuffer_size) {
|
|
|
|
s->to_write = s->ringbuffer_size;
|
|
|
|
s->partially_written = 0;
|
2015-09-28 10:40:39 +00:00
|
|
|
s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_WRITE;
|
2015-08-10 11:35:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-09-28 10:40:39 +00:00
|
|
|
s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_COPY;
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
2015-09-23 10:24:06 +00:00
|
|
|
case BROTLI_STATE_UNCOMPRESSED_SHORT:
|
|
|
|
if (!BrotliWarmupBitReader(&s->br)) {
|
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
while (s->meta_block_remaining_len > 0) {
|
2015-09-23 10:24:06 +00:00
|
|
|
if (!BrotliCheckInputAmount(&s->br, 8)) {
|
2015-04-22 15:33:21 +00:00
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
s->ringbuffer[pos++] = (uint8_t)BrotliReadBits(&s->br, 8);
|
2015-03-20 15:13:15 +00:00
|
|
|
s->meta_block_remaining_len--;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
if (pos >= s->ringbuffer_size) {
|
|
|
|
s->to_write = s->ringbuffer_size;
|
|
|
|
s->partially_written = 0;
|
2015-09-28 10:40:39 +00:00
|
|
|
s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_WRITE;
|
2015-08-10 11:35:23 +00:00
|
|
|
} else {
|
2015-09-23 10:24:06 +00:00
|
|
|
s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE;
|
2015-04-22 15:33:21 +00:00
|
|
|
return BROTLI_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
/* No break, if state is updated, continue to next state */
|
2015-09-28 10:40:39 +00:00
|
|
|
case BROTLI_STATE_UNCOMPRESSED_WRITE:
|
2015-08-10 11:35:23 +00:00
|
|
|
result = WriteRingBuffer(output, s);
|
|
|
|
if (result != BROTLI_RESULT_SUCCESS) {
|
|
|
|
return result;
|
|
|
|
}
|
2015-09-01 10:18:22 +00:00
|
|
|
pos &= s->ringbuffer_mask;
|
2015-08-10 11:35:23 +00:00
|
|
|
s->max_distance = s->max_backward_distance;
|
2015-09-25 15:43:54 +00:00
|
|
|
/* If we wrote past the logical end of the ringbuffer, copy the tail
|
|
|
|
of the ringbuffer to its beginning and flush the ringbuffer to the
|
|
|
|
output. */
|
|
|
|
memcpy(s->ringbuffer, s->ringbuffer_end, (size_t)pos);
|
2015-09-28 10:40:39 +00:00
|
|
|
s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_COPY;
|
2015-03-20 16:03:11 +00:00
|
|
|
/* No break, continue to next state */
|
2015-09-23 10:24:06 +00:00
|
|
|
case BROTLI_STATE_UNCOMPRESSED_COPY:
|
2015-03-20 15:13:15 +00:00
|
|
|
/* Copy straight from the input onto the ringbuffer. The ringbuffer will
|
|
|
|
be flushed to the output at a later time. */
|
2015-09-28 10:40:39 +00:00
|
|
|
nbytes = s->meta_block_remaining_len;
|
|
|
|
if (pos + nbytes > s->ringbuffer_size) {
|
|
|
|
nbytes = s->ringbuffer_size - pos;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
num_read = BrotliRead(s->br.input_, &s->ringbuffer[pos],
|
2015-09-28 10:40:39 +00:00
|
|
|
(size_t)nbytes);
|
2015-09-24 16:35:25 +00:00
|
|
|
pos += num_read;
|
2015-03-20 15:13:15 +00:00
|
|
|
s->meta_block_remaining_len -= num_read;
|
2015-09-28 10:40:39 +00:00
|
|
|
if (num_read < nbytes) {
|
2015-08-10 11:35:23 +00:00
|
|
|
if (num_read < 0) return BROTLI_FAILURE();
|
2015-04-22 15:33:21 +00:00
|
|
|
return BROTLI_RESULT_NEEDS_MORE_INPUT;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-09-28 10:40:39 +00:00
|
|
|
if (pos == s->ringbuffer_size) {
|
|
|
|
s->to_write = s->ringbuffer_size;
|
|
|
|
s->partially_written = 0;
|
|
|
|
s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_WRITE;
|
|
|
|
break;
|
|
|
|
}
|
2015-09-23 10:24:06 +00:00
|
|
|
s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE;
|
2015-03-27 12:54:43 +00:00
|
|
|
return BROTLI_RESULT_SUCCESS;
|
2014-02-14 14:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-26 15:37:00 +00:00
|
|
|
return BROTLI_FAILURE();
|
2014-02-14 14:04:23 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 11:35:23 +00:00
|
|
|
int BrotliDecompressedSize(size_t encoded_size,
|
|
|
|
const uint8_t* encoded_buffer,
|
|
|
|
size_t* decoded_size) {
|
2015-09-23 10:24:06 +00:00
|
|
|
BrotliMemInput memin;
|
|
|
|
BrotliInput in = BrotliInitMemInput(encoded_buffer, encoded_size, &memin);
|
|
|
|
BrotliBitReader br;
|
|
|
|
BrotliState s;
|
|
|
|
int next_block_header;
|
|
|
|
int offset;
|
|
|
|
BrotliStateInit(&s);
|
|
|
|
BrotliInitBitReader(&br, in);
|
|
|
|
if (!BrotliReadInput(&br, 1) || !BrotliWarmupBitReader(&br)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
DecodeWindowBits(&br);
|
|
|
|
if (DecodeMetaBlockLength(&s, &br) != BROTLI_RESULT_SUCCESS) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*decoded_size = (size_t)s.meta_block_remaining_len;
|
|
|
|
if (s.is_last_metablock) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!s.is_uncompressed || !BrotliJumpToByteBoundary(&br)) {
|
2015-08-10 11:35:23 +00:00
|
|
|
return 0;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-09-23 10:24:06 +00:00
|
|
|
next_block_header = BrotliPeekByte(&br, s.meta_block_remaining_len);
|
|
|
|
if (next_block_header != -1) {
|
|
|
|
return (next_block_header & 3) == 3;
|
2014-10-15 11:41:00 +00:00
|
|
|
}
|
2015-09-23 10:24:06 +00:00
|
|
|
/* Currently bit reader can't peek outside of its buffer... */
|
|
|
|
offset = BROTLI_READ_SIZE - (int)BrotliGetRemainingBytes(&br);
|
|
|
|
offset += s.meta_block_remaining_len;
|
|
|
|
return (offset < encoded_size) && ((encoded_buffer[offset] & 3) == 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocates the smallest feasible ring buffer.
|
|
|
|
|
|
|
|
If we know the data size is small, do not allocate more ringbuffer
|
|
|
|
size than needed to reduce memory usage.
|
|
|
|
|
|
|
|
This method is called before the first non-empty non-metadata block is
|
|
|
|
processed. When this method is called, metablock size and flags MUST be
|
|
|
|
decoded.
|
|
|
|
*/
|
|
|
|
int BROTLI_NOINLINE BrotliAllocateRingBuffer(BrotliState* s,
|
|
|
|
BrotliBitReader* br) {
|
|
|
|
static const int kRingBufferWriteAheadSlack = BROTLI_READ_SIZE;
|
|
|
|
int is_last = s->is_last_metablock;
|
|
|
|
s->ringbuffer_size = 1 << s->window_bits;
|
|
|
|
|
|
|
|
if (s->is_uncompressed) {
|
|
|
|
int next_block_header = BrotliPeekByte(br, s->meta_block_remaining_len);
|
|
|
|
if (next_block_header != -1) { /* Peek succeeded */
|
|
|
|
if ((next_block_header & 3) == 3) { /* ISLAST and ISEMPTY */
|
|
|
|
is_last = 1;
|
|
|
|
}
|
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) {
|
2015-09-23 10:24:06 +00:00
|
|
|
while (s->ringbuffer_size >= s->meta_block_remaining_len * 2
|
|
|
|
&& s->ringbuffer_size > 32) {
|
|
|
|
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
|
|
|
|
|
|
|
/* But make it fit the custom dictionary if there is one. */
|
|
|
|
while (s->ringbuffer_size < s->custom_dict_size) {
|
|
|
|
s->ringbuffer_size <<= 1;
|
2015-05-07 15:44:33 +00:00
|
|
|
}
|
2015-09-23 10:24:06 +00:00
|
|
|
|
|
|
|
s->ringbuffer_mask = s->ringbuffer_size - 1;
|
|
|
|
s->ringbuffer = (uint8_t*)malloc((size_t)(s->ringbuffer_size +
|
|
|
|
kRingBufferWriteAheadSlack +
|
2015-09-24 16:35:25 +00:00
|
|
|
kBrotliMaxDictionaryWordLength));
|
2015-10-02 14:19:30 +00:00
|
|
|
if (s->ringbuffer == 0) {
|
2015-09-23 10:24:06 +00:00
|
|
|
return 0;
|
2014-10-15 11:41:00 +00:00
|
|
|
}
|
2015-09-23 10:24:06 +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],
|
|
|
|
s->custom_dict, (size_t)s->custom_dict_size);
|
2014-10-15 11:41:00 +00:00
|
|
|
}
|
2015-09-23 10:24:06 +00:00
|
|
|
|
|
|
|
return 1;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:54:43 +00:00
|
|
|
BrotliResult BrotliDecompressBuffer(size_t encoded_size,
|
|
|
|
const uint8_t* encoded_buffer,
|
|
|
|
size_t* decoded_size,
|
|
|
|
uint8_t* decoded_buffer) {
|
2013-11-15 18:02:17 +00:00
|
|
|
BrotliMemInput memin;
|
|
|
|
BrotliInput in = BrotliInitMemInput(encoded_buffer, encoded_size, &memin);
|
|
|
|
BrotliMemOutput mout;
|
|
|
|
BrotliOutput out = BrotliInitMemOutput(decoded_buffer, *decoded_size, &mout);
|
2015-03-27 12:54:43 +00:00
|
|
|
BrotliResult success = BrotliDecompress(in, out);
|
2013-11-15 18:02:17 +00:00
|
|
|
*decoded_size = mout.pos;
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2015-03-27 12:54:43 +00:00
|
|
|
BrotliResult BrotliDecompress(BrotliInput input, BrotliOutput output) {
|
2015-03-20 15:13:15 +00:00
|
|
|
BrotliState s;
|
2015-03-27 12:54:43 +00:00
|
|
|
BrotliResult result;
|
2015-03-20 15:13:15 +00:00
|
|
|
BrotliStateInit(&s);
|
2015-03-27 12:54:43 +00:00
|
|
|
result = BrotliDecompressStreaming(input, output, 1, &s);
|
2015-04-22 15:33:21 +00:00
|
|
|
if (result == BROTLI_RESULT_NEEDS_MORE_INPUT) {
|
2015-03-20 15:13:15 +00:00
|
|
|
/* Not ok: it didn't finish even though this is a non-streaming function. */
|
2015-06-26 15:37:00 +00:00
|
|
|
result = BROTLI_FAILURE();
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
BrotliStateCleanup(&s);
|
2015-03-27 12:54:43 +00:00
|
|
|
return result;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 12:54:43 +00:00
|
|
|
BrotliResult BrotliDecompressBufferStreaming(size_t* available_in,
|
|
|
|
const uint8_t** next_in,
|
|
|
|
int finish,
|
|
|
|
size_t* available_out,
|
|
|
|
uint8_t** next_out,
|
|
|
|
size_t* total_out,
|
|
|
|
BrotliState* s) {
|
2015-03-20 15:13:15 +00:00
|
|
|
BrotliMemInput memin;
|
|
|
|
BrotliInput in = BrotliInitMemInput(*next_in, *available_in, &memin);
|
|
|
|
BrotliMemOutput memout;
|
|
|
|
BrotliOutput out = BrotliInitMemOutput(*next_out, *available_out, &memout);
|
2015-08-28 13:20:24 +00:00
|
|
|
BrotliResult result = BrotliDecompressStreaming(in, out, finish, s);
|
2015-03-20 15:13:15 +00:00
|
|
|
/* The current implementation reads everything, so 0 bytes are available. */
|
|
|
|
*next_in += memin.pos;
|
|
|
|
*available_in -= memin.pos;
|
|
|
|
/* Update the output position to where we write next. */
|
|
|
|
*next_out += memout.pos;
|
|
|
|
*available_out -= memout.pos;
|
|
|
|
*total_out += memout.pos;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-03-27 12:54:43 +00:00
|
|
|
BrotliResult BrotliDecompressStreaming(BrotliInput input, BrotliOutput output,
|
|
|
|
int finish, BrotliState* s) {
|
2015-03-20 15:13:15 +00:00
|
|
|
uint8_t context;
|
|
|
|
int pos = s->pos;
|
|
|
|
int i = s->loop_counter;
|
2015-03-27 12:54:43 +00:00
|
|
|
BrotliResult result = BROTLI_RESULT_SUCCESS;
|
2015-03-20 15:13:15 +00:00
|
|
|
BrotliBitReader* br = &s->br;
|
2015-03-20 16:03:11 +00:00
|
|
|
int initial_remaining_len;
|
2015-04-01 14:23:18 +00:00
|
|
|
int bytes_copied;
|
2015-08-10 11:35:23 +00:00
|
|
|
uint8_t *copy_src;
|
|
|
|
uint8_t *copy_dst;
|
2014-02-14 14:04:23 +00:00
|
|
|
/* We need the slack region for the following reasons:
|
2015-08-28 13:20:24 +00:00
|
|
|
- doing up to two 16-byte copies for fast backward copying
|
2014-02-14 14:04:23 +00:00
|
|
|
- transforms
|
2015-03-20 15:13:15 +00:00
|
|
|
- flushing the input s->ringbuffer when decoding uncompressed blocks */
|
2015-05-07 14:53:43 +00:00
|
|
|
s->br.input_ = input;
|
2015-03-20 15:13:15 +00:00
|
|
|
/* State machine */
|
|
|
|
for (;;) {
|
2015-03-27 12:54:43 +00:00
|
|
|
if (result != BROTLI_RESULT_SUCCESS) {
|
2015-08-10 11:35:23 +00:00
|
|
|
if (result == BROTLI_RESULT_NEEDS_MORE_INPUT) {
|
|
|
|
if (BrotliReadInput(br, finish)) {
|
|
|
|
result = BROTLI_RESULT_SUCCESS;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (finish) {
|
|
|
|
BROTLI_LOG(("Unexpected end of input. State: %d\n", s->state));
|
|
|
|
result = BROTLI_FAILURE();
|
|
|
|
}
|
2015-03-20 16:03:11 +00:00
|
|
|
}
|
|
|
|
break; /* Fail, or partial data. */
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
switch (s->state) {
|
|
|
|
case BROTLI_STATE_UNINITED:
|
|
|
|
pos = 0;
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliInitBitReader(br, input);
|
2015-03-20 15:13:15 +00:00
|
|
|
|
|
|
|
s->state = BROTLI_STATE_BITREADER_WARMUP;
|
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_BITREADER_WARMUP:
|
2015-09-23 10:24:06 +00:00
|
|
|
/* Prepare to the first read. */
|
|
|
|
if (!BrotliWarmupBitReader(br)) {
|
2015-04-22 15:33:21 +00:00
|
|
|
result = BROTLI_RESULT_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. */
|
2015-06-26 15:37:00 +00:00
|
|
|
result = BROTLI_FAILURE();
|
2015-05-07 15:44:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
s->max_backward_distance = (1 << s->window_bits) - 16;
|
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-03-20 16:03:11 +00:00
|
|
|
s->block_type_trees = (HuffmanCode*)malloc(
|
2015-08-10 14:39:50 +00:00
|
|
|
6 * BROTLI_HUFFMAN_MAX_TABLE_SIZE * sizeof(HuffmanCode));
|
2015-10-02 14:19:30 +00:00
|
|
|
if (s->block_type_trees == 0) {
|
2015-06-26 15:37:00 +00:00
|
|
|
result = BROTLI_FAILURE();
|
2015-03-20 16:03:11 +00:00
|
|
|
break;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-10 14:39:50 +00:00
|
|
|
s->block_len_trees = s->block_type_trees +
|
|
|
|
3 * BROTLI_HUFFMAN_MAX_TABLE_SIZE;
|
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:
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliStateMetablockBegin(s);
|
2015-03-20 15:13:15 +00:00
|
|
|
BROTLI_LOG_UINT(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. */
|
|
|
|
if (result != BROTLI_RESULT_SUCCESS) {
|
|
|
|
i = s->loop_counter; /* Has been updated in DecodeMetaBlockLength. */
|
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)) {
|
2015-06-26 15:37:00 +00:00
|
|
|
result = BROTLI_FAILURE();
|
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) {
|
|
|
|
if (!BrotliAllocateRingBuffer(s, br)) {
|
2015-06-26 15:37:00 +00:00
|
|
|
result = BROTLI_FAILURE();
|
2015-04-22 12:35:21 +00:00
|
|
|
break;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
s->state = BROTLI_STATE_HUFFMAN_CODE_0;
|
|
|
|
break;
|
|
|
|
case BROTLI_STATE_UNCOMPRESSED:
|
2015-03-20 16:03:11 +00:00
|
|
|
initial_remaining_len = s->meta_block_remaining_len;
|
|
|
|
/* pos is given as argument since s->pos is only updated at the end. */
|
2015-03-27 12:54:43 +00:00
|
|
|
result = CopyUncompressedBlockToOutput(output, pos, s);
|
2015-04-01 14:23:18 +00:00
|
|
|
bytes_copied = initial_remaining_len - s->meta_block_remaining_len;
|
2015-08-10 11:35:23 +00:00
|
|
|
pos = (pos + bytes_copied) & s->ringbuffer_mask;
|
|
|
|
if (result != BROTLI_RESULT_SUCCESS) {
|
|
|
|
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-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)) {
|
2015-04-22 15:33:21 +00:00
|
|
|
result = BROTLI_RESULT_NEEDS_MORE_INPUT;
|
2015-04-22 12:25:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
if (result == BROTLI_RESULT_SUCCESS) {
|
|
|
|
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:
|
|
|
|
if (i >= 3) {
|
2015-09-23 10:24:06 +00:00
|
|
|
s->state = BROTLI_STATE_CONTEXT_MODES;
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
/* Reads 1..11 bits. */
|
|
|
|
result = DecodeVarLenUint8(s, br, &s->num_block_types[i]);
|
|
|
|
if (result != BROTLI_RESULT_SUCCESS) {
|
2015-09-23 10:24:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-09-24 16:35:25 +00:00
|
|
|
s->num_block_types[i]++;
|
|
|
|
BROTLI_LOG_UINT(s->num_block_types[i]);
|
2015-03-20 15:13:15 +00:00
|
|
|
s->state = BROTLI_STATE_HUFFMAN_CODE_1;
|
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_HUFFMAN_CODE_1:
|
2015-09-24 16:35:25 +00:00
|
|
|
if (!BrotliWarmupBitReader(br)) {
|
|
|
|
result = BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
break;
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
if (s->num_block_types[i] >= 2) {
|
2015-03-27 12:54:43 +00:00
|
|
|
result = ReadHuffmanCode(s->num_block_types[i] + 2,
|
2015-03-20 15:13:15 +00:00
|
|
|
&s->block_type_trees[i * BROTLI_HUFFMAN_MAX_TABLE_SIZE],
|
|
|
|
NULL, s);
|
2015-03-27 12:54:43 +00:00
|
|
|
if (result != BROTLI_RESULT_SUCCESS) break;
|
2015-03-20 15:13:15 +00:00
|
|
|
s->state = BROTLI_STATE_HUFFMAN_CODE_2;
|
|
|
|
} else {
|
|
|
|
i++;
|
|
|
|
s->state = BROTLI_STATE_HUFFMAN_CODE_0;
|
2015-03-20 16:03:11 +00:00
|
|
|
break;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
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_HUFFMAN_CODE_2:
|
2015-03-27 12:54:43 +00:00
|
|
|
result = ReadHuffmanCode(kNumBlockLengthCodes,
|
|
|
|
&s->block_len_trees[i * BROTLI_HUFFMAN_MAX_TABLE_SIZE],
|
|
|
|
NULL, s);
|
|
|
|
if (result != BROTLI_RESULT_SUCCESS) break;
|
2015-09-23 10:24:06 +00:00
|
|
|
s->state = BROTLI_STATE_HUFFMAN_CODE_3;
|
|
|
|
/* No break, continue to next state */
|
|
|
|
case BROTLI_STATE_HUFFMAN_CODE_3:
|
|
|
|
if (!BrotliCheckInputAmount(br, 8)) {
|
|
|
|
result = BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s->block_length[i] = ReadBlockLength( /* Reads 3..39 bits. */
|
2015-03-20 15:13:15 +00:00
|
|
|
&s->block_len_trees[i * BROTLI_HUFFMAN_MAX_TABLE_SIZE], br);
|
2015-09-24 16:35:25 +00:00
|
|
|
BROTLI_LOG_UINT(s->block_length[i]);
|
2015-03-20 15:13:15 +00:00
|
|
|
i++;
|
|
|
|
s->state = BROTLI_STATE_HUFFMAN_CODE_0;
|
|
|
|
break;
|
2015-09-23 10:24:06 +00:00
|
|
|
case BROTLI_STATE_CONTEXT_MODES:
|
2015-06-26 15:37:00 +00:00
|
|
|
/* We need up to 256 * 2 + 6 bits, this fits in 128 bytes. */
|
2015-08-10 11:35:23 +00:00
|
|
|
if (!BrotliCheckInputAmount(br, 128)) {
|
2015-04-22 15:33:21 +00:00
|
|
|
result = BROTLI_RESULT_NEEDS_MORE_INPUT;
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
s->distance_postfix_bits = (int)BrotliReadBits(br, 2);
|
|
|
|
s->num_direct_distance_codes = NUM_DISTANCE_SHORT_CODES +
|
|
|
|
((int)BrotliReadBits(br, 4) << 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-03-20 15:13:15 +00:00
|
|
|
s->context_modes = (uint8_t*)malloc((size_t)s->num_block_types[0]);
|
|
|
|
if (s->context_modes == 0) {
|
2015-06-26 15:37:00 +00:00
|
|
|
result = BROTLI_FAILURE();
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (i = 0; i < s->num_block_types[0]; ++i) {
|
|
|
|
s->context_modes[i] = (uint8_t)(BrotliReadBits(br, 2) << 1);
|
|
|
|
BROTLI_LOG_ARRAY_INDEX(s->context_modes, i);
|
|
|
|
}
|
|
|
|
s->state = BROTLI_STATE_CONTEXT_MAP_1;
|
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_1:
|
2015-03-27 12:54:43 +00:00
|
|
|
result = DecodeContextMap(s->num_block_types[0] << kLiteralContextBits,
|
|
|
|
&s->num_literal_htrees, &s->context_map, s);
|
2015-08-28 13:20:24 +00:00
|
|
|
if (result != BROTLI_RESULT_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
2015-04-01 14:15:39 +00:00
|
|
|
s->trivial_literal_context = 1;
|
|
|
|
for (i = 0; i < s->num_block_types[0] << kLiteralContextBits; i++) {
|
|
|
|
if (s->context_map[i] != i >> kLiteralContextBits) {
|
|
|
|
s->trivial_literal_context = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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-08-28 13:20:24 +00:00
|
|
|
int num_distance_codes =
|
|
|
|
s->num_direct_distance_codes + (48 << s->distance_postfix_bits);
|
|
|
|
result = DecodeContextMap(
|
|
|
|
s->num_block_types[2] << kDistanceContextBits,
|
2015-09-23 10:24:06 +00:00
|
|
|
&s->num_dist_htrees, &s->dist_context_map, s);
|
2015-08-28 13:20:24 +00:00
|
|
|
if (result != BROTLI_RESULT_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
BrotliHuffmanTreeGroupInit(
|
|
|
|
&s->literal_hgroup, kNumLiteralCodes, s->num_literal_htrees);
|
|
|
|
BrotliHuffmanTreeGroupInit(
|
|
|
|
&s->insert_copy_hgroup, kNumInsertAndCopyCodes,
|
|
|
|
s->num_block_types[1]);
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliHuffmanTreeGroupInit(
|
2015-09-23 10:24:06 +00:00
|
|
|
&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) {
|
|
|
|
return BROTLI_FAILURE();
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
i = 0;
|
|
|
|
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;
|
|
|
|
switch (i) {
|
|
|
|
case 0:
|
|
|
|
hgroup = &s->literal_hgroup;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
hgroup = &s->insert_copy_hgroup;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
hgroup = &s->distance_hgroup;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
result = HuffmanTreeGroupDecode(hgroup, s);
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
2015-03-27 12:54:43 +00:00
|
|
|
if (result != BROTLI_RESULT_SUCCESS) break;
|
2015-03-20 15:13:15 +00:00
|
|
|
i++;
|
|
|
|
if (i >= 3) {
|
2015-08-10 11:35:23 +00:00
|
|
|
uint8_t context_mode = s->context_modes[s->block_type_rb[1]];
|
2015-03-20 15:13:15 +00:00
|
|
|
s->context_map_slice = s->context_map;
|
|
|
|
s->dist_context_map_slice = s->dist_context_map;
|
2015-08-10 11:35:23 +00:00
|
|
|
s->context_lookup1 =
|
|
|
|
&kContextLookup[kContextLookupOffsets[context_mode]];
|
|
|
|
s->context_lookup2 =
|
|
|
|
&kContextLookup[kContextLookupOffsets[context_mode + 1]];
|
|
|
|
s->htree_command = s->insert_copy_hgroup.htrees[0];
|
2015-08-28 13:20:24 +00:00
|
|
|
s->literal_htree = s->literal_hgroup.htrees[s->literal_htree_index];
|
|
|
|
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:
|
2015-03-20 15:13:15 +00:00
|
|
|
if (s->meta_block_remaining_len <= 0) {
|
|
|
|
/* Next metablock, if any */
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
break;
|
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
/* Decoding of Brotli commands is the inner loop, jumping with goto makes it
|
|
|
|
3% faster */
|
|
|
|
CommandBegin:
|
|
|
|
if (!BrotliCheckInputAmount(br, 32)) {
|
|
|
|
s->state = BROTLI_STATE_COMMAND_BEGIN;
|
|
|
|
result = BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Read the insert/copy length in the command */
|
2015-03-20 15:13:15 +00:00
|
|
|
if (s->block_length[1] == 0) {
|
2015-09-23 10:24:06 +00:00
|
|
|
/* Block switch for insert/copy length. Reads 0..15 bits. */
|
2015-03-20 15:13:15 +00:00
|
|
|
DecodeBlockType(s->num_block_types[1],
|
|
|
|
s->block_type_trees, 1,
|
2015-08-10 11:35:23 +00:00
|
|
|
s->block_type_rb, br);
|
2015-08-28 13:20:24 +00:00
|
|
|
s->htree_command = s->insert_copy_hgroup.htrees[s->block_type_rb[3]];
|
2015-09-23 10:24:06 +00:00
|
|
|
s->block_length[1] = ReadBlockLength( /* Reads 3..39 bits. */
|
2015-03-20 15:13:15 +00:00
|
|
|
&s->block_len_trees[BROTLI_HUFFMAN_MAX_TABLE_SIZE], br);
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
{
|
|
|
|
int cmd_code = ReadSymbol(s->htree_command, br);
|
2015-09-28 10:34:40 +00:00
|
|
|
int insert_len_extra = 0;
|
2015-08-28 13:20:24 +00:00
|
|
|
CmdLutElement v;
|
|
|
|
--s->block_length[1];
|
|
|
|
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];
|
2015-09-28 10:34:40 +00:00
|
|
|
i = v.insert_len_offset;
|
|
|
|
if (PREDICT_FALSE(v.insert_len_extra_bits != 0)) {
|
|
|
|
insert_len_extra = (int)BrotliReadBits(br, v.insert_len_extra_bits);
|
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
s->copy_length = (int)BrotliReadBits(br, v.copy_len_extra_bits) +
|
2015-09-28 10:34:40 +00:00
|
|
|
v.copy_len_offset;
|
|
|
|
i += insert_len_extra;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
BROTLI_LOG_UINT(i);
|
2015-03-20 15:13:15 +00:00
|
|
|
BROTLI_LOG_UINT(s->copy_length);
|
|
|
|
BROTLI_LOG_UINT(s->distance_code);
|
2015-08-28 13:20:24 +00:00
|
|
|
if (i == 0) {
|
|
|
|
goto postDecodeLiterals;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
s->meta_block_remaining_len -= i;
|
2015-03-20 15:13:15 +00:00
|
|
|
/* No break, go to next state */
|
2015-08-28 13:20:24 +00:00
|
|
|
case BROTLI_STATE_COMMAND_INNER:
|
|
|
|
/* Read the literals in the command */
|
|
|
|
if (s->trivial_literal_context) {
|
|
|
|
unsigned bits;
|
|
|
|
unsigned value;
|
|
|
|
PreloadSymbol(s->literal_htree, br, &bits, &value);
|
|
|
|
do {
|
|
|
|
if (!BrotliCheckInputAmount(br, 64)) {
|
|
|
|
s->state = BROTLI_STATE_COMMAND_INNER;
|
2015-04-22 15:33:21 +00:00
|
|
|
result = BROTLI_RESULT_NEEDS_MORE_INPUT;
|
2015-04-01 14:15:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
if (PREDICT_FALSE(s->block_length[0] == 0)) {
|
|
|
|
/* Block switch for literals */
|
|
|
|
DecodeBlockTypeWithContext(s, br);
|
|
|
|
PreloadSymbol(s->literal_htree, br, &bits, &value);
|
|
|
|
}
|
|
|
|
s->ringbuffer[pos] =
|
|
|
|
(uint8_t)ReadPreloadedSymbol(s->literal_htree,
|
|
|
|
br, &bits, &value);
|
|
|
|
--s->block_length[0];
|
|
|
|
BROTLI_LOG_UINT(s->literal_htree_index);
|
|
|
|
BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos);
|
|
|
|
++pos;
|
|
|
|
if (PREDICT_FALSE(pos == s->ringbuffer_size)) {
|
|
|
|
s->to_write = s->ringbuffer_size;
|
|
|
|
s->partially_written = 0;
|
|
|
|
s->state = BROTLI_STATE_COMMAND_INNER_WRITE;
|
|
|
|
--i;
|
|
|
|
goto innerWrite;
|
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
if (!BrotliCheckInputAmount(br, 64)) {
|
|
|
|
s->state = BROTLI_STATE_COMMAND_INNER;
|
|
|
|
result = BROTLI_RESULT_NEEDS_MORE_INPUT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (PREDICT_FALSE(s->block_length[0] == 0)) {
|
|
|
|
/* Block switch for literals */
|
|
|
|
DecodeBlockTypeWithContext(s, br);
|
|
|
|
}
|
|
|
|
context = s->context_lookup1[p1] | s->context_lookup2[p2];
|
|
|
|
BROTLI_LOG_UINT(context);
|
|
|
|
hc = s->literal_hgroup.htrees[s->context_map_slice[context]];
|
|
|
|
--s->block_length[0];
|
|
|
|
p2 = p1;
|
|
|
|
p1 = (uint8_t)ReadSymbol(hc, br);
|
|
|
|
s->ringbuffer[pos] = p1;
|
|
|
|
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->to_write = s->ringbuffer_size;
|
|
|
|
s->partially_written = 0;
|
|
|
|
s->state = BROTLI_STATE_COMMAND_INNER_WRITE;
|
|
|
|
--i;
|
|
|
|
goto innerWrite;
|
|
|
|
}
|
|
|
|
} while (--i != 0);
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
if (result != BROTLI_RESULT_SUCCESS) break;
|
2015-03-20 16:03:11 +00:00
|
|
|
if (s->meta_block_remaining_len <= 0) {
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
break;
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
postDecodeLiterals:
|
|
|
|
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 */
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
2015-09-21 19:04:07 +00:00
|
|
|
/* Read distance code in the command, unless it was implicitly zero. */
|
2015-05-07 14:53:43 +00:00
|
|
|
BROTLI_DCHECK(s->distance_code < 0);
|
2015-03-20 15:13:15 +00:00
|
|
|
if (s->block_length[2] == 0) {
|
2015-08-28 13:20:24 +00:00
|
|
|
/* Block switch for distance codes */
|
2015-08-10 11:35:23 +00:00
|
|
|
int dist_context_offset;
|
2015-03-20 15:13:15 +00:00
|
|
|
DecodeBlockType(s->num_block_types[2],
|
|
|
|
s->block_type_trees, 2,
|
2015-09-23 10:24:06 +00:00
|
|
|
s->block_type_rb, br); /* Reads 0..15 bits. */
|
|
|
|
s->block_length[2] = ReadBlockLength( /* Reads 3..39 bits. */
|
2015-03-20 15:13:15 +00:00
|
|
|
&s->block_len_trees[2 * BROTLI_HUFFMAN_MAX_TABLE_SIZE], br);
|
2015-08-10 11:35:23 +00:00
|
|
|
dist_context_offset = s->block_type_rb[5] << kDistanceContextBits;
|
2015-03-20 15:13:15 +00:00
|
|
|
s->dist_context_map_slice =
|
2015-08-10 11:35:23 +00:00
|
|
|
s->dist_context_map + dist_context_offset;
|
2015-08-28 13:20:24 +00:00
|
|
|
s->dist_htree_index = s->dist_context_map_slice[s->distance_context];
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
|
|
|
--s->block_length[2];
|
|
|
|
s->distance_code =
|
2015-08-10 11:35:23 +00:00
|
|
|
ReadSymbol(s->distance_hgroup.htrees[s->dist_htree_index], br);
|
2015-03-20 15:13:15 +00:00
|
|
|
/* Convert the distance code to the actual distance by possibly */
|
2015-08-10 11:35:23 +00:00
|
|
|
/* looking up past distances from the s->ringbuffer. */
|
|
|
|
if ((s->distance_code & ~0xf) == 0) {
|
2015-08-28 13:20:24 +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;
|
2015-08-10 11:35:23 +00:00
|
|
|
int v = (s->dist_rb_idx +
|
2015-08-28 13:20:24 +00:00
|
|
|
(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;
|
2015-08-10 11:35:23 +00:00
|
|
|
} else {
|
2015-08-28 13:20:24 +00:00
|
|
|
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;
|
2015-08-10 11:35:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-08-28 13:20:24 +00:00
|
|
|
int distval = s->distance_code - s->num_direct_distance_codes;
|
|
|
|
if (distval >= 0) {
|
|
|
|
int nbits;
|
|
|
|
int postfix;
|
|
|
|
int offset;
|
|
|
|
if (s->distance_postfix_bits == 0) {
|
|
|
|
nbits = (distval >> 1) + 1;
|
|
|
|
offset = ((2 + (distval & 1)) << nbits) - 4;
|
|
|
|
s->distance_code = s->num_direct_distance_codes +
|
|
|
|
offset + (int)BrotliReadBits(br, nbits);
|
|
|
|
} else {
|
|
|
|
postfix = distval & s->distance_postfix_mask;
|
|
|
|
distval >>= s->distance_postfix_bits;
|
|
|
|
nbits = (distval >> 1) + 1;
|
|
|
|
offset = ((2 + (distval & 1)) << nbits) - 4;
|
|
|
|
s->distance_code = s->num_direct_distance_codes +
|
|
|
|
((offset + (int)BrotliReadBits(br, nbits)) <<
|
|
|
|
s->distance_postfix_bits) + postfix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->distance_code = s->distance_code - NUM_DISTANCE_SHORT_CODES + 1;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
postReadDistance:
|
|
|
|
BROTLI_LOG_UINT(s->distance_code);
|
|
|
|
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;
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
i = s->copy_length;
|
2015-08-28 13:20:24 +00:00
|
|
|
/* 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) {
|
2015-09-24 16:35:25 +00:00
|
|
|
if (i >= kBrotliMinDictionaryWordLength &&
|
|
|
|
i <= kBrotliMaxDictionaryWordLength) {
|
2015-08-10 11:35:23 +00:00
|
|
|
int offset = kBrotliDictionaryOffsetsByLength[i];
|
2015-08-28 13:20:24 +00:00
|
|
|
int word_id = s->distance_code - s->max_distance - 1;
|
2015-08-10 11:35:23 +00:00
|
|
|
int shift = kBrotliDictionarySizeBitsByLength[i];
|
2015-08-28 13:20:24 +00:00
|
|
|
int mask = (int)BitMask(shift);
|
2015-03-20 15:13:15 +00:00
|
|
|
int word_idx = word_id & mask;
|
|
|
|
int transform_idx = word_id >> shift;
|
2015-08-10 11:35:23 +00:00
|
|
|
offset += word_idx * i;
|
2015-03-20 15:13:15 +00:00
|
|
|
if (transform_idx < kNumTransforms) {
|
|
|
|
const uint8_t* word = &kBrotliDictionary[offset];
|
2015-08-10 11:35:23 +00:00
|
|
|
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);
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
pos += len;
|
|
|
|
s->meta_block_remaining_len -= len;
|
2015-08-10 11:35:23 +00:00
|
|
|
if (pos >= s->ringbuffer_size) {
|
|
|
|
s->to_write = s->ringbuffer_size;
|
2015-04-22 15:33:21 +00:00
|
|
|
s->partially_written = 0;
|
2015-08-28 13:20:24 +00:00
|
|
|
s->state = BROTLI_STATE_COMMAND_POST_WRITE_1;
|
2015-08-10 11:35:23 +00:00
|
|
|
break;
|
2014-02-17 13:25:36 +00:00
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
} else {
|
2015-06-26 15:37:00 +00:00
|
|
|
BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
|
2015-03-20 15:13:15 +00:00
|
|
|
"len: %d bytes left: %d\n",
|
2015-08-28 13:20:24 +00:00
|
|
|
pos, s->distance_code, i,
|
2015-06-26 15:37:00 +00:00
|
|
|
s->meta_block_remaining_len));
|
|
|
|
result = BROTLI_FAILURE();
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
2014-02-17 13:25:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-06-26 15:37:00 +00:00
|
|
|
BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
|
2015-08-28 13:20:24 +00:00
|
|
|
"len: %d bytes left: %d\n", pos, s->distance_code, i,
|
2015-06-26 15:37:00 +00:00
|
|
|
s->meta_block_remaining_len));
|
|
|
|
result = BROTLI_FAILURE();
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
2014-02-17 13:25:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-08-10 11:35:23 +00:00
|
|
|
const uint8_t *ringbuffer_end_minus_copy_length =
|
|
|
|
s->ringbuffer_end - i;
|
2015-08-28 13:20:24 +00:00
|
|
|
copy_src = &s->ringbuffer[(pos - s->distance_code) &
|
|
|
|
s->ringbuffer_mask];
|
2015-08-10 11:35:23 +00:00
|
|
|
copy_dst = &s->ringbuffer[pos];
|
2015-08-28 13:20:24 +00:00
|
|
|
/* update the recent distances cache */
|
|
|
|
s->dist_rb[s->dist_rb_idx & 3] = s->distance_code;
|
2015-08-10 11:35:23 +00:00
|
|
|
++s->dist_rb_idx;
|
|
|
|
s->meta_block_remaining_len -= i;
|
|
|
|
if (PREDICT_FALSE(s->meta_block_remaining_len < 0)) {
|
2015-06-26 15:37:00 +00:00
|
|
|
BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
|
2015-08-28 13:20:24 +00:00
|
|
|
"len: %d bytes left: %d\n", pos, s->distance_code, i,
|
2015-06-26 15:37:00 +00:00
|
|
|
s->meta_block_remaining_len));
|
|
|
|
result = BROTLI_FAILURE();
|
2015-03-20 15:13:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
/* There is 128+ bytes of slack in the ringbuffer allocation.
|
|
|
|
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);
|
|
|
|
/* Now check if the copy extends over the ringbuffer end,
|
|
|
|
or if the copy overlaps with itself, if yes, do wrap-copy. */
|
|
|
|
if (copy_src < copy_dst) {
|
|
|
|
if (copy_dst >= ringbuffer_end_minus_copy_length) {
|
|
|
|
goto postWrapCopy;
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
if (copy_src + i > copy_dst) {
|
|
|
|
goto postSelfintersecting;
|
2015-05-07 15:10:27 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
} else {
|
|
|
|
if (copy_src >= ringbuffer_end_minus_copy_length) {
|
|
|
|
goto postWrapCopy;
|
|
|
|
}
|
|
|
|
if (copy_dst + i > copy_src) {
|
|
|
|
goto postSelfintersecting;
|
2013-11-19 22:32:56 +00:00
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-08-10 11:35:23 +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);
|
|
|
|
}
|
2015-04-22 15:33:21 +00:00
|
|
|
}
|
2013-11-15 18:02:17 +00:00
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
if (s->meta_block_remaining_len <= 0) {
|
|
|
|
/* Next metablock, if any */
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
goto CommandBegin;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
postSelfintersecting:
|
|
|
|
while (--i >= 0) {
|
|
|
|
s->ringbuffer[pos] =
|
2015-08-28 13:20:24 +00:00
|
|
|
s->ringbuffer[(pos - s->distance_code) & s->ringbuffer_mask];
|
2015-08-10 11:35:23 +00:00
|
|
|
++pos;
|
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
if (s->meta_block_remaining_len <= 0) {
|
|
|
|
/* Next metablock, if any */
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
goto CommandBegin;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
postWrapCopy:
|
2015-08-28 13:20:24 +00:00
|
|
|
s->state = BROTLI_STATE_COMMAND_POST_WRAP_COPY;
|
|
|
|
/* No break, go to next state */
|
|
|
|
case BROTLI_STATE_COMMAND_POST_WRAP_COPY:
|
2015-08-10 11:35:23 +00:00
|
|
|
while (--i >= 0) {
|
|
|
|
s->ringbuffer[pos] =
|
2015-08-28 13:20:24 +00:00
|
|
|
s->ringbuffer[(pos - s->distance_code) & s->ringbuffer_mask];
|
2015-08-10 11:35:23 +00:00
|
|
|
++pos;
|
|
|
|
if (pos == s->ringbuffer_size) {
|
|
|
|
s->to_write = s->ringbuffer_size;
|
|
|
|
s->partially_written = 0;
|
2015-08-28 13:20:24 +00:00
|
|
|
s->state = BROTLI_STATE_COMMAND_POST_WRITE_2;
|
2015-08-10 11:35:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-08-28 13:20:24 +00:00
|
|
|
if (s->state == BROTLI_STATE_COMMAND_POST_WRAP_COPY) {
|
|
|
|
if (s->meta_block_remaining_len <= 0) {
|
|
|
|
/* Next metablock, if any */
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
goto CommandBegin;
|
|
|
|
}
|
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:
|
|
|
|
innerWrite:
|
2015-08-10 11:35:23 +00:00
|
|
|
result = WriteRingBuffer(output, s);
|
|
|
|
if (result != BROTLI_RESULT_SUCCESS) {
|
2015-04-22 15:33:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-08-10 11:35:23 +00:00
|
|
|
pos -= s->ringbuffer_size;
|
|
|
|
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-08-10 11:35:23 +00:00
|
|
|
memcpy(s->ringbuffer, s->ringbuffer_end, (size_t)pos);
|
2015-08-28 13:20:24 +00:00
|
|
|
if (s->meta_block_remaining_len <= 0) {
|
|
|
|
/* Next metablock, if any */
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
goto CommandBegin;
|
|
|
|
}
|
|
|
|
} else if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_2) {
|
|
|
|
s->state = BROTLI_STATE_COMMAND_POST_WRAP_COPY;
|
|
|
|
} else { /* BROTLI_STATE_COMMAND_INNER_WRITE */
|
|
|
|
if (i == 0) {
|
|
|
|
if (s->meta_block_remaining_len <= 0) {
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_DONE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto postDecodeLiterals;
|
|
|
|
}
|
|
|
|
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:
|
2015-08-10 11:35:23 +00:00
|
|
|
BrotliStateCleanupAfterMetablock(s);
|
2015-09-23 10:24:06 +00:00
|
|
|
if (!s->is_last_metablock) {
|
|
|
|
s->state = BROTLI_STATE_METABLOCK_BEGIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s->to_write = pos;
|
|
|
|
s->partially_written = 0;
|
|
|
|
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) {
|
2015-08-10 11:35:23 +00:00
|
|
|
result = WriteRingBuffer(output, s);
|
|
|
|
if (result != BROTLI_RESULT_SUCCESS) {
|
2015-04-22 15:33:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
}
|
2015-09-01 10:18:22 +00:00
|
|
|
if (!BrotliJumpToByteBoundary(br)) {
|
2015-06-26 15:37:00 +00:00
|
|
|
result = BROTLI_FAILURE();
|
2015-04-22 12:35:21 +00:00
|
|
|
}
|
2015-09-23 10:24:06 +00:00
|
|
|
if (!BrotliIsBitReaderOK(br)) {
|
|
|
|
/* The brotli input stream was too small, does not follow the spec.
|
|
|
|
NOTE: larger input is allowed, smaller not. */
|
2015-08-28 13:20:24 +00:00
|
|
|
result = BROTLI_FAILURE();
|
|
|
|
}
|
2015-03-27 12:54:43 +00:00
|
|
|
return result;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-20 15:13:15 +00:00
|
|
|
s->pos = pos;
|
|
|
|
s->loop_counter = i;
|
2015-03-27 12:54:43 +00:00
|
|
|
return result;
|
2013-10-11 08:26:07 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 13:43:54 +00:00
|
|
|
void BrotliSetCustomDictionary(
|
|
|
|
size_t size, const uint8_t* dict, BrotliState* s) {
|
|
|
|
s->custom_dict = dict;
|
|
|
|
s->custom_dict_size = (int) size;
|
|
|
|
}
|
|
|
|
|
2015-09-23 10:24:06 +00:00
|
|
|
|
2013-10-11 08:26:07 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
2013-12-16 13:45:57 +00:00
|
|
|
} /* extern "C" */
|
2013-10-11 08:26:07 +00:00
|
|
|
#endif
|