mirror of
https://github.com/google/brotli.git
synced 2024-11-22 11:40:06 +00:00
commit
722f8996b0
@ -1,8 +1,10 @@
|
||||
Want to contribute? Great! First, read this page (including the small print at the end).
|
||||
Want to contribute? Great! First, read this page (including the small print at
|
||||
the end).
|
||||
|
||||
### Before you contribute
|
||||
Before we can use your code, you must sign the
|
||||
[Google Individual Contributor License Agreement](https://cla.developers.google.com/about/google-individual)
|
||||
[Google Individual Contributor License Agreement]
|
||||
(https://cla.developers.google.com/about/google-individual)
|
||||
(CLA), which you can do online. The CLA is necessary mainly because you own the
|
||||
copyright to your changes, even after your contribution becomes part of our
|
||||
codebase, so we need your permission to use and distribute your code. We also
|
||||
@ -21,5 +23,5 @@ use Github pull requests for this purpose.
|
||||
|
||||
### The small print
|
||||
Contributions made by corporations are covered by a different agreement than
|
||||
the one above, the
|
||||
[Software Grant and Corporate Contributor License Agreement](https://cla.developers.google.com/about/google-corporate).
|
||||
the one above, the [Software Grant and Corporate Contributor License Agreement]
|
||||
(https://cla.developers.google.com/about/google-corporate).
|
||||
|
@ -14,6 +14,10 @@
|
||||
#include "./port.h"
|
||||
#include "./types.h"
|
||||
|
||||
#ifdef BROTLI_DECODE_DEBUG
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
@ -66,7 +70,7 @@ void BrotliInitBitReader(BrotliBitReader* const br);
|
||||
|
||||
/* Ensures that accumulator is not empty. May consume one byte of input.
|
||||
Returns 0 if data is required but there is no input available.
|
||||
For BROTLI_BUILD_PORTABLE this function also prepares bit reader for aligned
|
||||
For BROTLI_ALIGNED_READ this function also prepares bit reader for aligned
|
||||
reading. */
|
||||
int BrotliWarmupBitReader(BrotliBitReader* const br);
|
||||
|
||||
@ -109,9 +113,7 @@ static BROTLI_INLINE uint16_t BrotliLoad16LE(const uint8_t* in) {
|
||||
return *((const uint16_t*)in);
|
||||
} else if (BROTLI_BIG_ENDIAN) {
|
||||
uint16_t value = *((const uint16_t*)in);
|
||||
return (uint16_t)(
|
||||
((value & 0xFFU) << 8) |
|
||||
((value & 0xFF00U) >> 8));
|
||||
return (uint16_t)(((value & 0xFFU) << 8) | ((value & 0xFF00U) >> 8));
|
||||
} else {
|
||||
return (uint16_t)(in[0] | (in[1] << 8));
|
||||
}
|
||||
|
139
dec/decode.c
139
dec/decode.c
@ -28,12 +28,12 @@ extern "C" {
|
||||
|
||||
/* BROTLI_FAILURE macro unwraps to BROTLI_RESULT_ERROR in non-debug build. */
|
||||
/* In debug build it dumps file name, line and pretty function name. */
|
||||
#if defined(_MSC_VER) || !defined(BROTLI_DEBUG)
|
||||
#if defined(_MSC_VER) || \
|
||||
(!defined(BROTLI_DEBUG) && !defined(BROTLI_DECODE_DEBUG))
|
||||
#define BROTLI_FAILURE() BROTLI_RESULT_ERROR
|
||||
#else
|
||||
#define BROTLI_FAILURE() \
|
||||
BrotliFailure(__FILE__, __LINE__, __PRETTY_FUNCTION__)
|
||||
static inline BrotliResult BrotliFailure(const char *f, int l, const char *fn) {
|
||||
#define BROTLI_FAILURE() BrotliFailure(__FILE__, __LINE__, __PRETTY_FUNCTION__)
|
||||
static inline BrotliResult BrotliFailure(const char* f, int l, const char* fn) {
|
||||
fprintf(stderr, "ERROR at %s:%d (%s)\n", f, l, fn);
|
||||
fflush(stderr);
|
||||
return BROTLI_RESULT_ERROR;
|
||||
@ -127,8 +127,7 @@ static uint32_t DecodeWindowBits(BrotliBitReader* br) {
|
||||
return 17;
|
||||
}
|
||||
|
||||
static BROTLI_INLINE void memmove16(
|
||||
uint8_t* dst, uint8_t* src) {
|
||||
static BROTLI_INLINE void memmove16(uint8_t* dst, uint8_t* src) {
|
||||
#if defined(__ARM_NEON__)
|
||||
vst1q_u8(dst, vld1q_u8(src));
|
||||
#else
|
||||
@ -479,8 +478,7 @@ static BROTLI_INLINE void ProcessSingleCodeLength(uint32_t code_len,
|
||||
*prev_code_len = code_len;
|
||||
*space -= 32768U >> code_len;
|
||||
code_length_histo[code_len]++;
|
||||
BROTLI_LOG(("[ReadHuffmanCode] code_length[%d] = %d\n",
|
||||
*symbol, code_len));
|
||||
BROTLI_LOG(("[ReadHuffmanCode] code_length[%d] = %d\n", *symbol, code_len));
|
||||
}
|
||||
(*symbol)++;
|
||||
}
|
||||
@ -532,8 +530,8 @@ static BROTLI_INLINE void ProcessRepeatedCodeLength(uint32_t code_len,
|
||||
} while (++(*symbol) != last);
|
||||
next_symbol[*repeat_code_len] = next;
|
||||
*space -= repeat_delta << (15 - *repeat_code_len);
|
||||
code_length_histo[*repeat_code_len] = (uint16_t)
|
||||
(code_length_histo[*repeat_code_len] + repeat_delta);
|
||||
code_length_histo[*repeat_code_len] =
|
||||
(uint16_t)(code_length_histo[*repeat_code_len] + repeat_delta);
|
||||
} else {
|
||||
*symbol += repeat_delta;
|
||||
}
|
||||
@ -785,8 +783,8 @@ Complex: /* Decode Huffman-coded code lengths. */
|
||||
BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", s->space));
|
||||
return BROTLI_FAILURE();
|
||||
}
|
||||
table_size = BrotliBuildHuffmanTable(table, HUFFMAN_TABLE_BITS,
|
||||
s->symbol_lists, s->code_length_histo);
|
||||
table_size = BrotliBuildHuffmanTable(
|
||||
table, HUFFMAN_TABLE_BITS, s->symbol_lists, s->code_length_histo);
|
||||
if (opt_table_size) {
|
||||
*opt_table_size = table_size;
|
||||
}
|
||||
@ -926,7 +924,7 @@ static BrotliResult DecodeContextMap(uint32_t context_map_size,
|
||||
BrotliBitReader* br = &s->br;
|
||||
BrotliResult result = BROTLI_RESULT_SUCCESS;
|
||||
|
||||
switch((int)s->substate_context_map) {
|
||||
switch ((int)s->substate_context_map) {
|
||||
case BROTLI_STATE_CONTEXT_MAP_NONE:
|
||||
result = DecodeVarLenUint8(s, br, num_htrees);
|
||||
if (result != BROTLI_RESULT_SUCCESS) {
|
||||
@ -1144,12 +1142,11 @@ static int BROTLI_NOINLINE SafeDecodeDistanceBlockSwitch(BrotliState* s) {
|
||||
|
||||
static BrotliResult WriteRingBuffer(size_t* available_out, uint8_t** next_out,
|
||||
size_t* total_out, BrotliState* s) {
|
||||
size_t pos = (s->pos > s->ringbuffer_size) ?
|
||||
(size_t)s->ringbuffer_size : (size_t)(s->pos);
|
||||
uint8_t* start = s->ringbuffer
|
||||
+ (s->partial_pos_out & (size_t)s->ringbuffer_mask);
|
||||
size_t partial_pos_rb =
|
||||
(s->rb_roundtrips * (size_t)s->ringbuffer_size) + pos;
|
||||
size_t pos = (s->pos > s->ringbuffer_size) ? (size_t)s->ringbuffer_size
|
||||
: (size_t)(s->pos);
|
||||
uint8_t* start =
|
||||
s->ringbuffer + (s->partial_pos_out & (size_t)s->ringbuffer_mask);
|
||||
size_t partial_pos_rb = (s->rb_roundtrips * (size_t)s->ringbuffer_size) + pos;
|
||||
size_t to_write = (partial_pos_rb - s->partial_pos_out);
|
||||
size_t num_written = *available_out;
|
||||
if (num_written > to_write) {
|
||||
@ -1238,8 +1235,8 @@ static BrotliResult BROTLI_NOINLINE CopyUncompressedBlockToOutput(
|
||||
/* No break, continue to next state */
|
||||
}
|
||||
case BROTLI_STATE_UNCOMPRESSED_WRITE: {
|
||||
BrotliResult result = WriteRingBuffer(
|
||||
available_out, next_out, total_out, s);
|
||||
BrotliResult result =
|
||||
WriteRingBuffer(available_out, next_out, total_out, s);
|
||||
if (result != BROTLI_RESULT_SUCCESS) {
|
||||
return result;
|
||||
}
|
||||
@ -1293,8 +1290,8 @@ static void BROTLI_NOINLINE BrotliCalculateRingBufferSize(BrotliState* s,
|
||||
s->ringbuffer_size = 1 << s->window_bits;
|
||||
|
||||
if (s->is_uncompressed) {
|
||||
int next_block_header = BrotliPeekByte(br,
|
||||
(size_t)s->meta_block_remaining_len);
|
||||
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 */
|
||||
is_last = 1;
|
||||
@ -1305,8 +1302,8 @@ static void BROTLI_NOINLINE BrotliCalculateRingBufferSize(BrotliState* s,
|
||||
/* We need at least 2 bytes of ring buffer size to get the last two
|
||||
bytes for context from there */
|
||||
if (is_last) {
|
||||
while (s->ringbuffer_size >= s->meta_block_remaining_len * 2
|
||||
&& s->ringbuffer_size > 32) {
|
||||
while (s->ringbuffer_size >= s->meta_block_remaining_len * 2 &&
|
||||
s->ringbuffer_size > 32) {
|
||||
s->ringbuffer_size >>= 1;
|
||||
}
|
||||
}
|
||||
@ -1407,8 +1404,8 @@ static BROTLI_INLINE int ReadDistanceInternal(int safe,
|
||||
if (!safe && (s->distance_postfix_bits == 0)) {
|
||||
nbits = ((uint32_t)distval >> 1) + 1;
|
||||
offset = ((2 + (distval & 1)) << nbits) - 4;
|
||||
s->distance_code = (int)s->num_direct_distance_codes +
|
||||
offset + (int)BrotliReadBits(br, nbits);
|
||||
s->distance_code = (int)s->num_direct_distance_codes + offset +
|
||||
(int)BrotliReadBits(br, nbits);
|
||||
} else {
|
||||
/* This branch also works well when s->distance_postfix_bits == 0 */
|
||||
uint32_t bits;
|
||||
@ -1498,16 +1495,17 @@ static BROTLI_INLINE int CheckInputAmount(int safe,
|
||||
return BrotliCheckInputAmount(br, num);
|
||||
}
|
||||
|
||||
#define BROTLI_SAFE(METHOD) { \
|
||||
#define BROTLI_SAFE(METHOD) \
|
||||
{ \
|
||||
if (safe) { \
|
||||
if (! Safe ## METHOD ) { \
|
||||
if (!Safe##METHOD) { \
|
||||
result = BROTLI_RESULT_NEEDS_MORE_INPUT; \
|
||||
goto saveStateAndReturn; \
|
||||
} \
|
||||
} else { \
|
||||
METHOD ; \
|
||||
METHOD; \
|
||||
} \
|
||||
}
|
||||
}
|
||||
|
||||
static BROTLI_INLINE BrotliResult ProcessCommandsInternal(int safe,
|
||||
BrotliState* s) {
|
||||
@ -1579,8 +1577,8 @@ CommandInner:
|
||||
PreloadSymbol(safe, s->literal_htree, br, &bits, &value);
|
||||
}
|
||||
if (!safe) {
|
||||
s->ringbuffer[pos] = (uint8_t)ReadPreloadedSymbol(
|
||||
s->literal_htree, br, &bits, &value);
|
||||
s->ringbuffer[pos] =
|
||||
(uint8_t)ReadPreloadedSymbol(s->literal_htree, br, &bits, &value);
|
||||
} else {
|
||||
uint32_t literal;
|
||||
if (!SafeReadSymbol(s->literal_htree, br, &literal)) {
|
||||
@ -1701,58 +1699,44 @@ postReadDistance:
|
||||
} else {
|
||||
BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
|
||||
"len: %d bytes left: %d\n",
|
||||
pos, s->distance_code, i,
|
||||
s->meta_block_remaining_len));
|
||||
pos, s->distance_code, i, s->meta_block_remaining_len));
|
||||
return BROTLI_FAILURE();
|
||||
}
|
||||
} else {
|
||||
BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
|
||||
"len: %d bytes left: %d\n", pos, s->distance_code, i,
|
||||
s->meta_block_remaining_len));
|
||||
"len: %d bytes left: %d\n",
|
||||
pos, s->distance_code, i, s->meta_block_remaining_len));
|
||||
return BROTLI_FAILURE();
|
||||
}
|
||||
} else {
|
||||
const uint8_t *ringbuffer_end_minus_copy_length =
|
||||
s->ringbuffer_end - i;
|
||||
uint8_t* copy_src = &s->ringbuffer[
|
||||
(pos - s->distance_code) & s->ringbuffer_mask];
|
||||
int src_start = (pos - s->distance_code) & s->ringbuffer_mask;
|
||||
uint8_t* copy_dst = &s->ringbuffer[pos];
|
||||
/* Check for possible underflow and clamp the pointer to 0. */
|
||||
if (PREDICT_FALSE(s->ringbuffer_end < (const uint8_t*)0 + i)) {
|
||||
ringbuffer_end_minus_copy_length = 0;
|
||||
}
|
||||
uint8_t* copy_src = &s->ringbuffer[src_start];
|
||||
int dst_end = pos + i;
|
||||
int src_end = src_start + i;
|
||||
/* 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;
|
||||
if (PREDICT_FALSE(s->meta_block_remaining_len < 0)) {
|
||||
BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d "
|
||||
"len: %d bytes left: %d\n", pos, s->distance_code, i,
|
||||
s->meta_block_remaining_len));
|
||||
"len: %d bytes left: %d\n",
|
||||
pos, s->distance_code, i, s->meta_block_remaining_len));
|
||||
return BROTLI_FAILURE();
|
||||
}
|
||||
/* There is 128+ bytes of slack in the ringbuffer allocation.
|
||||
/* There are 32+ 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) {
|
||||
if (src_end > pos && dst_end > src_start) {
|
||||
/* Regions intersect. */
|
||||
goto CommandPostWrapCopy;
|
||||
}
|
||||
if (copy_src + i > copy_dst) {
|
||||
goto postSelfintersecting;
|
||||
}
|
||||
} else {
|
||||
if (copy_src >= ringbuffer_end_minus_copy_length) {
|
||||
if (dst_end >= s->ringbuffer_size || src_end >= s->ringbuffer_size) {
|
||||
/* At least one region wraps. */
|
||||
goto CommandPostWrapCopy;
|
||||
}
|
||||
if (copy_dst + i > copy_src) {
|
||||
goto postSelfintersecting;
|
||||
}
|
||||
}
|
||||
pos += i;
|
||||
if (i > 16) {
|
||||
if (i > 32) {
|
||||
@ -1772,32 +1756,19 @@ postReadDistance:
|
||||
} else {
|
||||
goto CommandBegin;
|
||||
}
|
||||
postSelfintersecting:
|
||||
while (--i >= 0) {
|
||||
s->ringbuffer[pos] =
|
||||
s->ringbuffer[(pos - s->distance_code) & s->ringbuffer_mask];
|
||||
++pos;
|
||||
}
|
||||
if (s->meta_block_remaining_len <= 0) {
|
||||
/* Next metablock, if any */
|
||||
s->state = BROTLI_STATE_METABLOCK_DONE;
|
||||
goto saveStateAndReturn;
|
||||
} else {
|
||||
goto CommandBegin;
|
||||
}
|
||||
|
||||
CommandPostWrapCopy:
|
||||
s->state = BROTLI_STATE_COMMAND_POST_WRAP_COPY;
|
||||
{
|
||||
int wrap_guard = s->ringbuffer_size - pos;
|
||||
while (--i >= 0) {
|
||||
s->ringbuffer[pos] =
|
||||
s->ringbuffer[(pos - s->distance_code) & s->ringbuffer_mask];
|
||||
++pos;
|
||||
if (pos == s->ringbuffer_size) {
|
||||
/*s->partial_pos_rb += (size_t)s->ringbuffer_size;*/
|
||||
if (PREDICT_FALSE(--wrap_guard == 0)) {
|
||||
s->state = BROTLI_STATE_COMMAND_POST_WRITE_2;
|
||||
goto saveStateAndReturn;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (s->meta_block_remaining_len <= 0) {
|
||||
/* Next metablock, if any */
|
||||
s->state = BROTLI_STATE_METABLOCK_DONE;
|
||||
@ -1960,8 +1931,8 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
|
||||
result = BROTLI_FAILURE();
|
||||
break;
|
||||
}
|
||||
s->block_len_trees = s->block_type_trees +
|
||||
3 * BROTLI_HUFFMAN_MAX_SIZE_258;
|
||||
s->block_len_trees =
|
||||
s->block_type_trees + 3 * BROTLI_HUFFMAN_MAX_SIZE_258;
|
||||
|
||||
s->state = BROTLI_STATE_METABLOCK_BEGIN;
|
||||
/* No break, continue to next state */
|
||||
@ -2081,8 +2052,8 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
|
||||
}
|
||||
s->distance_postfix_bits = bits & BitMask(2);
|
||||
bits >>= 2;
|
||||
s->num_direct_distance_codes = NUM_DISTANCE_SHORT_CODES +
|
||||
(bits << s->distance_postfix_bits);
|
||||
s->num_direct_distance_codes =
|
||||
NUM_DISTANCE_SHORT_CODES + (bits << s->distance_postfix_bits);
|
||||
BROTLI_LOG_UINT(s->num_direct_distance_codes);
|
||||
BROTLI_LOG_UINT(s->distance_postfix_bits);
|
||||
s->distance_postfix_mask = (int)BitMask(s->distance_postfix_bits);
|
||||
@ -2159,6 +2130,8 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
|
||||
case 2:
|
||||
hgroup = &s->distance_hgroup;
|
||||
break;
|
||||
default:
|
||||
return BROTLI_FAILURE();
|
||||
}
|
||||
result = HuffmanTreeGroupDecode(hgroup, s);
|
||||
}
|
||||
@ -2255,7 +2228,7 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
|
||||
void BrotliSetCustomDictionary(
|
||||
size_t size, const uint8_t* dict, BrotliState* s) {
|
||||
s->custom_dict = dict;
|
||||
s->custom_dict_size = (int) size;
|
||||
s->custom_dict_size = (int)size;
|
||||
}
|
||||
|
||||
|
||||
|
@ -18,15 +18,14 @@ extern "C" {
|
||||
extern const uint8_t kBrotliDictionary[122784];
|
||||
|
||||
static const uint32_t kBrotliDictionaryOffsetsByLength[] = {
|
||||
0, 0, 0, 0, 0, 4096, 9216, 21504, 35840, 44032,
|
||||
53248, 63488, 74752, 87040, 93696, 100864, 104704, 106752, 108928, 113536,
|
||||
115968, 118528, 119872, 121280, 122016,
|
||||
0, 0, 0, 0, 0, 4096, 9216, 21504, 35840, 44032, 53248, 63488, 74752, 87040,
|
||||
93696, 100864, 104704, 106752, 108928, 113536, 115968, 118528, 119872, 121280,
|
||||
122016
|
||||
};
|
||||
|
||||
static const uint8_t kBrotliDictionarySizeBitsByLength[] = {
|
||||
0, 0, 0, 0, 10, 10, 11, 11, 10, 10,
|
||||
10, 10, 10, 9, 9, 8, 7, 7, 8, 7,
|
||||
7, 6, 6, 5, 5,
|
||||
0, 0, 0, 0, 10, 10, 11, 11, 10, 10, 10, 10, 10,
|
||||
9, 9, 8, 7, 7, 8, 7, 7, 6, 6, 5, 5,
|
||||
};
|
||||
|
||||
static const int kBrotliMinDictionaryWordLength = 4;
|
||||
|
@ -102,7 +102,7 @@ static BROTLI_INLINE int NextTableBitSize(const uint16_t* const count,
|
||||
|
||||
void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
|
||||
const uint8_t* const code_lengths,
|
||||
uint16_t *count) {
|
||||
uint16_t* count) {
|
||||
HuffmanCode code; /* current table entry */
|
||||
int symbol; /* symbol index in original or sorted table */
|
||||
uint32_t key; /* prefix code */
|
||||
@ -114,8 +114,8 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
|
||||
int offset[BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1];
|
||||
int bits;
|
||||
int bits_count;
|
||||
BROTLI_DCHECK(
|
||||
BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH <= BROTLI_REVERSE_BITS_MAX);
|
||||
BROTLI_DCHECK(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH <=
|
||||
BROTLI_REVERSE_BITS_MAX);
|
||||
|
||||
/* generate offsets into sorted symbol table by code length */
|
||||
symbol = -1;
|
||||
@ -170,7 +170,7 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
|
||||
uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
||||
int root_bits,
|
||||
const uint16_t* const symbol_lists,
|
||||
uint16_t *count) {
|
||||
uint16_t* count) {
|
||||
HuffmanCode code; /* current table entry */
|
||||
HuffmanCode* table; /* next available space in table */
|
||||
int len; /* current code length */
|
||||
@ -178,7 +178,7 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
||||
uint32_t key; /* prefix code */
|
||||
uint32_t key_step; /* prefix code addend */
|
||||
uint32_t sub_key; /* 2nd level table prefix code */
|
||||
uint32_t sub_key_step;/* 2nd level table prefix code addend */
|
||||
uint32_t sub_key_step; /* 2nd level table prefix code addend */
|
||||
int step; /* step size to replicate values in current table */
|
||||
int table_bits; /* key length of current table */
|
||||
int table_size; /* size of current table */
|
||||
@ -188,8 +188,8 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
||||
int bits_count;
|
||||
|
||||
BROTLI_DCHECK(root_bits <= BROTLI_REVERSE_BITS_MAX);
|
||||
BROTLI_DCHECK(
|
||||
BROTLI_HUFFMAN_MAX_CODE_LENGTH - root_bits <= BROTLI_REVERSE_BITS_MAX);
|
||||
BROTLI_DCHECK(BROTLI_HUFFMAN_MAX_CODE_LENGTH - root_bits <=
|
||||
BROTLI_REVERSE_BITS_MAX);
|
||||
|
||||
while (symbol_lists[max_length] == 0xFFFF) max_length--;
|
||||
max_length += BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1;
|
||||
@ -246,8 +246,8 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
||||
sub_key = BrotliReverseBits(key);
|
||||
key += key_step;
|
||||
root_table[sub_key].bits = (uint8_t)(table_bits + root_bits);
|
||||
root_table[sub_key].value = (uint16_t)(
|
||||
((size_t)(table - root_table)) - sub_key);
|
||||
root_table[sub_key].value =
|
||||
(uint16_t)(((size_t)(table - root_table)) - sub_key);
|
||||
sub_key = 0;
|
||||
}
|
||||
code.bits = (uint8_t)(len - root_bits);
|
||||
@ -265,7 +265,7 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
||||
|
||||
uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
|
||||
int root_bits,
|
||||
uint16_t *val,
|
||||
uint16_t* val,
|
||||
uint32_t num_symbols) {
|
||||
uint32_t table_size = 1;
|
||||
const uint32_t goal_size = 1U << root_bits;
|
||||
@ -302,8 +302,7 @@ uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
|
||||
table[3].bits = 2;
|
||||
table_size = 4;
|
||||
break;
|
||||
case 3:
|
||||
{
|
||||
case 3: {
|
||||
int i, k;
|
||||
for (i = 0; i < 3; ++i) {
|
||||
for (k = i + 1; k < 4; ++k) {
|
||||
@ -322,10 +321,9 @@ uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
|
||||
table[1].value = val[2];
|
||||
table[3].value = val[3];
|
||||
table_size = 4;
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
{
|
||||
}
|
||||
case 4: {
|
||||
int i;
|
||||
if (val[3] < val[2]) {
|
||||
uint16_t t = val[3];
|
||||
@ -343,9 +341,9 @@ uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
|
||||
table[3].bits = 3;
|
||||
table[7].bits = 3;
|
||||
table_size = 8;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
while (table_size != goal_size) {
|
||||
memcpy(&table[table_size], &table[0],
|
||||
(size_t)table_size * sizeof(table[0]));
|
||||
|
@ -40,14 +40,14 @@ typedef struct {
|
||||
/* Builds Huffman lookup table assuming code lengths are in symbol order. */
|
||||
void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table,
|
||||
const uint8_t* const code_lengths,
|
||||
uint16_t *count);
|
||||
uint16_t* count);
|
||||
|
||||
/* Builds Huffman lookup table assuming code lengths are in symbol order. */
|
||||
/* Returns size of resulting table. */
|
||||
uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
||||
int root_bits,
|
||||
const uint16_t* const symbol_lists,
|
||||
uint16_t *count_arg);
|
||||
uint16_t* count_arg);
|
||||
|
||||
/* Builds a simple Huffman table. The num_symbols parameter is to be */
|
||||
/* interpreted as follows: 0 means 1 symbol, 1 means 2 symbols, 2 means 3 */
|
||||
@ -55,7 +55,7 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
|
||||
/* lengths 1,2,3,3. */
|
||||
uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
|
||||
int root_bits,
|
||||
uint16_t *symbols,
|
||||
uint16_t* symbols,
|
||||
uint32_t num_symbols);
|
||||
|
||||
/* Contains a collection of Huffman trees with the same alphabet size. */
|
||||
|
30
dec/port.h
30
dec/port.h
@ -24,7 +24,7 @@
|
||||
#ifndef BROTLI_DEC_PORT_H_
|
||||
#define BROTLI_DEC_PORT_H_
|
||||
|
||||
#include<assert.h>
|
||||
#include <assert.h>
|
||||
|
||||
/* Compatibility with non-clang compilers. */
|
||||
#ifndef __has_builtin
|
||||
@ -39,10 +39,6 @@
|
||||
#define __has_feature(x) 0
|
||||
#endif
|
||||
|
||||
#if defined(__sparc)
|
||||
#define BROTLI_TARGET_SPARC
|
||||
#endif
|
||||
|
||||
#if defined(__arm__) || defined(__thumb__) || \
|
||||
defined(_M_ARM) || defined(_M_ARMT)
|
||||
#define BROTLI_TARGET_ARM
|
||||
@ -55,6 +51,10 @@
|
||||
#endif /* ARMv8 */
|
||||
#endif /* ARM */
|
||||
|
||||
#if defined(__i386) || defined(_M_IX86)
|
||||
#define BROTLI_TARGET_X86
|
||||
#endif
|
||||
|
||||
#if defined(__x86_64__) || defined(_M_X64)
|
||||
#define BROTLI_TARGET_X64
|
||||
#endif
|
||||
@ -83,19 +83,13 @@
|
||||
#define BROTLI_MODERN_COMPILER 0
|
||||
#endif
|
||||
|
||||
/* SPARC and ARMv6 don't support unaligned read.
|
||||
Choose portable build for them. */
|
||||
#if !defined(BROTLI_BUILD_PORTABLE)
|
||||
#if defined(BROTLI_TARGET_SPARC) || \
|
||||
(defined(BROTLI_TARGET_ARM) && !defined(BROTLI_TARGET_ARMV7))
|
||||
#define BROTLI_BUILD_PORTABLE
|
||||
#endif /* SPARK or ARMv6 */
|
||||
#endif /* portable build */
|
||||
|
||||
#ifdef BROTLI_BUILD_PORTABLE
|
||||
#define BROTLI_ALIGNED_READ 1
|
||||
#elif defined(BROTLI_TARGET_X86) || defined(BROTLI_TARGET_X64) || \
|
||||
defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8)
|
||||
#define BROTLI_ALIGNED_READ 0 /* Allow unaligned access on whitelisted CPUs. */
|
||||
#else
|
||||
#define BROTLI_ALIGNED_READ 0
|
||||
#define BROTLI_ALIGNED_READ 1
|
||||
#endif
|
||||
|
||||
/* Define "PREDICT_TRUE" and "PREDICT_FALSE" macros for capable compilers.
|
||||
@ -137,8 +131,8 @@ OR:
|
||||
#endif
|
||||
|
||||
#ifndef _MSC_VER
|
||||
#if defined(__cplusplus) || !defined(__STRICT_ANSI__) \
|
||||
|| __STDC_VERSION__ >= 199901L
|
||||
#if defined(__cplusplus) || !defined(__STRICT_ANSI__) || \
|
||||
__STDC_VERSION__ >= 199901L
|
||||
#define BROTLI_INLINE inline ATTRIBUTE_ALWAYS_INLINE
|
||||
#else
|
||||
#define BROTLI_INLINE
|
||||
@ -190,7 +184,7 @@ OR:
|
||||
#endif
|
||||
|
||||
#if BROTLI_MODERN_COMPILER || __has_attribute(noinline)
|
||||
#define BROTLI_NOINLINE __attribute__ ((noinline))
|
||||
#define BROTLI_NOINLINE __attribute__((noinline))
|
||||
#else
|
||||
#define BROTLI_NOINLINE
|
||||
#endif
|
||||
|
@ -161,7 +161,7 @@ void BrotliHuffmanTreeGroupInit(BrotliState* s, HuffmanTreeGroup* group,
|
||||
const size_t max_table_size = kMaxHuffmanTableSize[(alphabet_size + 31) >> 5];
|
||||
const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;
|
||||
const size_t htree_size = sizeof(HuffmanCode*) * ntrees;
|
||||
char *p = (char*)BROTLI_ALLOC(s, code_size + htree_size);
|
||||
char* p = (char*)BROTLI_ALLOC(s, code_size + htree_size);
|
||||
group->alphabet_size = (uint16_t)alphabet_size;
|
||||
group->num_htrees = (uint16_t)ntrees;
|
||||
group->codes = (HuffmanCode*)p;
|
||||
|
@ -150,7 +150,7 @@ struct BrotliStateStruct {
|
||||
int distance_postfix_mask;
|
||||
uint32_t num_dist_htrees;
|
||||
uint8_t* dist_context_map;
|
||||
HuffmanCode *literal_htree;
|
||||
HuffmanCode* literal_htree;
|
||||
uint8_t literal_htree_index;
|
||||
uint8_t dist_htree_index;
|
||||
uint32_t repeat_code_len;
|
||||
@ -242,7 +242,6 @@ int BrotliStateIsStreamStart(const BrotliState* s);
|
||||
produced all of the output, and 0 otherwise. */
|
||||
int BrotliStateIsStreamEnd(const BrotliState* s);
|
||||
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
102
dec/streams.c
102
dec/streams.c
@ -1,102 +0,0 @@
|
||||
/* Copyright 2013 Google Inc. All Rights Reserved.
|
||||
|
||||
Distributed under MIT license.
|
||||
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
/* Functions for streaming input and output. */
|
||||
|
||||
#include <string.h>
|
||||
#ifndef _WIN32
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include "./streams.h"
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
int BrotliMemInputFunction(void* data, uint8_t* buf, size_t count) {
|
||||
BrotliMemInput* input = (BrotliMemInput*)data;
|
||||
if (input->pos > input->length) {
|
||||
return -1;
|
||||
}
|
||||
if (input->pos + count > input->length) {
|
||||
count = input->length - input->pos;
|
||||
}
|
||||
memcpy(buf, input->buffer + input->pos, count);
|
||||
input->pos += count;
|
||||
return (int)count;
|
||||
}
|
||||
|
||||
BrotliInput BrotliInitMemInput(const uint8_t* buffer, size_t length,
|
||||
BrotliMemInput* mem_input) {
|
||||
BrotliInput input;
|
||||
mem_input->buffer = buffer;
|
||||
mem_input->length = length;
|
||||
mem_input->pos = 0;
|
||||
input.cb_ = &BrotliMemInputFunction;
|
||||
input.data_ = mem_input;
|
||||
return input;
|
||||
}
|
||||
|
||||
int BrotliMemOutputFunction(void* data, const uint8_t* buf, size_t count) {
|
||||
BrotliMemOutput* output = (BrotliMemOutput*)data;
|
||||
size_t limit = output->length - output->pos;
|
||||
if (count > limit) {
|
||||
count = limit;
|
||||
}
|
||||
memcpy(output->buffer + output->pos, buf, count);
|
||||
output->pos += count;
|
||||
return (int)count;
|
||||
}
|
||||
|
||||
BrotliOutput BrotliInitMemOutput(uint8_t* buffer, size_t length,
|
||||
BrotliMemOutput* mem_output) {
|
||||
BrotliOutput output;
|
||||
mem_output->buffer = buffer;
|
||||
mem_output->length = length;
|
||||
mem_output->pos = 0;
|
||||
output.cb_ = &BrotliMemOutputFunction;
|
||||
output.data_ = mem_output;
|
||||
return output;
|
||||
}
|
||||
|
||||
int BrotliFileInputFunction(void* data, uint8_t* buf, size_t count) {
|
||||
return (int)fread(buf, 1, count, (FILE*)data);
|
||||
}
|
||||
|
||||
BrotliInput BrotliFileInput(FILE* f) {
|
||||
BrotliInput in;
|
||||
in.cb_ = BrotliFileInputFunction;
|
||||
in.data_ = f;
|
||||
return in;
|
||||
}
|
||||
|
||||
int BrotliFileOutputFunction(void* data, const uint8_t* buf, size_t count) {
|
||||
return (int)fwrite(buf, 1, count, (FILE*)data);
|
||||
}
|
||||
|
||||
BrotliOutput BrotliFileOutput(FILE* f) {
|
||||
BrotliOutput out;
|
||||
out.cb_ = BrotliFileOutputFunction;
|
||||
out.data_ = f;
|
||||
return out;
|
||||
}
|
||||
|
||||
int BrotliNullOutputFunction(void* data , const uint8_t* buf, size_t count) {
|
||||
BROTLI_UNUSED(data);
|
||||
BROTLI_UNUSED(buf);
|
||||
return (int)count;
|
||||
}
|
||||
|
||||
BrotliOutput BrotliNullOutput(void) {
|
||||
BrotliOutput out;
|
||||
out.cb_ = BrotliNullOutputFunction;
|
||||
out.data_ = NULL;
|
||||
return out;
|
||||
}
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
} /* extern "C" */
|
||||
#endif
|
@ -1,95 +0,0 @@
|
||||
/* Copyright 2013 Google Inc. All Rights Reserved.
|
||||
|
||||
Distributed under MIT license.
|
||||
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
/* Functions for streaming input and output. */
|
||||
|
||||
#ifndef BROTLI_DEC_STREAMS_H_
|
||||
#define BROTLI_DEC_STREAMS_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include "./port.h"
|
||||
#include "./types.h"
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Function pointer type used to read len bytes into buf. Returns the */
|
||||
/* number of bytes read or -1 on error. */
|
||||
typedef int (*BrotliInputFunction)(void* data, uint8_t* buf, size_t len);
|
||||
|
||||
/* Input callback function with associated data. */
|
||||
typedef struct {
|
||||
BrotliInputFunction cb_;
|
||||
void* data_;
|
||||
} BrotliInput;
|
||||
|
||||
/* Reads len bytes into buf, using the in callback. */
|
||||
static BROTLI_INLINE int BrotliRead(BrotliInput in, uint8_t* buf, size_t len) {
|
||||
return in.cb_(in.data_, buf, len);
|
||||
}
|
||||
|
||||
/* Function pointer type used to write len bytes into buf. Returns the */
|
||||
/* number of bytes written or -1 on error. */
|
||||
typedef int (*BrotliOutputFunction)(void* data, const uint8_t* buf, size_t len);
|
||||
|
||||
/* Output callback function with associated data. */
|
||||
typedef struct {
|
||||
BrotliOutputFunction cb_;
|
||||
void* data_;
|
||||
} BrotliOutput;
|
||||
|
||||
/* Writes len bytes into buf, using the out callback. */
|
||||
static BROTLI_INLINE int BrotliWrite(BrotliOutput out,
|
||||
const uint8_t* buf, size_t len) {
|
||||
return out.cb_(out.data_, buf, len);
|
||||
}
|
||||
|
||||
/* Memory region with position. */
|
||||
typedef struct {
|
||||
const uint8_t* buffer;
|
||||
size_t length;
|
||||
size_t pos;
|
||||
} BrotliMemInput;
|
||||
|
||||
/* Input callback where *data is a BrotliMemInput struct. */
|
||||
int BrotliMemInputFunction(void* data, uint8_t* buf, size_t count);
|
||||
|
||||
/* Returns an input callback that wraps the given memory region. */
|
||||
BrotliInput BrotliInitMemInput(const uint8_t* buffer, size_t length,
|
||||
BrotliMemInput* mem_input);
|
||||
|
||||
/* Output buffer with position. */
|
||||
typedef struct {
|
||||
uint8_t* buffer;
|
||||
size_t length;
|
||||
size_t pos;
|
||||
} BrotliMemOutput;
|
||||
|
||||
/* Output callback where *data is a BrotliMemOutput struct. */
|
||||
int BrotliMemOutputFunction(void* data, const uint8_t* buf, size_t count);
|
||||
|
||||
/* Returns an output callback that wraps the given memory region. */
|
||||
BrotliOutput BrotliInitMemOutput(uint8_t* buffer, size_t length,
|
||||
BrotliMemOutput* mem_output);
|
||||
|
||||
/* Input callback that reads from a file. */
|
||||
int BrotliFileInputFunction(void* data, uint8_t* buf, size_t count);
|
||||
BrotliInput BrotliFileInput(FILE* f);
|
||||
|
||||
/* Output callback that writes to a file. */
|
||||
int BrotliFileOutputFunction(void* data, const uint8_t* buf, size_t count);
|
||||
BrotliOutput BrotliFileOutput(FILE* f);
|
||||
|
||||
/* Output callback that does nothing, always consumes the whole input. */
|
||||
int BrotliNullOutputFunction(void* data, const uint8_t* buf, size_t count);
|
||||
BrotliOutput BrotliNullOutput(void);
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* BROTLI_DEC_STREAMS_H_ */
|
@ -114,7 +114,6 @@ enum {
|
||||
kPFix_ousSP = 203
|
||||
};
|
||||
|
||||
|
||||
static const Transform kTransforms[] = {
|
||||
{ kPFix_EMPTY, kIdentity, kPFix_EMPTY },
|
||||
{ kPFix_EMPTY, kIdentity, kPFix_SP },
|
||||
@ -241,7 +240,7 @@ static const Transform kTransforms[] = {
|
||||
|
||||
static const int kNumTransforms = sizeof(kTransforms) / sizeof(kTransforms[0]);
|
||||
|
||||
static int ToUpperCase(uint8_t *p) {
|
||||
static int ToUpperCase(uint8_t* p) {
|
||||
if (p[0] < 0xc0) {
|
||||
if (p[0] >= 'a' && p[0] <= 'z') {
|
||||
p[0] ^= 32;
|
||||
|
@ -29,10 +29,10 @@ typedef __int64 int64_t;
|
||||
size length. Neither items nor size are allowed to be 0.
|
||||
opaque argument is a pointer provided by client and could be used to bind
|
||||
function to specific object (memory pool). */
|
||||
typedef void* (*brotli_alloc_func) (void* opaque, size_t size);
|
||||
typedef void* (*brotli_alloc_func)(void* opaque, size_t size);
|
||||
|
||||
/* Deallocating function pointer. Function SHOULD be no-op in the case the
|
||||
address is 0. */
|
||||
typedef void (*brotli_free_func) (void* opaque, void* address);
|
||||
typedef void (*brotli_free_func)(void* opaque, void* address);
|
||||
|
||||
#endif /* BROTLI_DEC_TYPES_H_ */
|
||||
|
Loading…
Reference in New Issue
Block a user