diff options
author | eustas <eustas@chromium.org> | 2016-02-19 08:40:44 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-02-19 16:43:24 +0000 |
commit | 76806d6881faafa9731420012579f7b439e3dfc4 (patch) | |
tree | 2215c8c29ee5dd0ec8c5bd618ee144db9d6ba087 /third_party/brotli | |
parent | 8430468e13d9b71231c0febf277b56ebdfcb5301 (diff) | |
download | chromium_src-76806d6881faafa9731420012579f7b439e3dfc4.zip chromium_src-76806d6881faafa9731420012579f7b439e3dfc4.tar.gz chromium_src-76806d6881faafa9731420012579f7b439e3dfc4.tar.bz2 |
Update brotli from 24ffa7 (11 Dec 2015) to 722f89 (19 Feb 2016)
Summary - better formating, use less memory, removed obsolete API, bug fixing.
BUG=472009
Review URL: https://codereview.chromium.org/1708283002
Cr-Commit-Position: refs/heads/master@{#376455}
Diffstat (limited to 'third_party/brotli')
-rw-r--r-- | third_party/brotli/BUILD.gn | 2 | ||||
-rw-r--r-- | third_party/brotli/README.chromium | 3 | ||||
-rw-r--r-- | third_party/brotli/brotli.gyp | 2 | ||||
-rw-r--r-- | third_party/brotli/dec/Makefile | 2 | ||||
-rw-r--r-- | third_party/brotli/dec/bit_reader.c | 6 | ||||
-rw-r--r-- | third_party/brotli/dec/bit_reader.h | 46 | ||||
-rw-r--r-- | third_party/brotli/dec/context.h | 8 | ||||
-rw-r--r-- | third_party/brotli/dec/decode.c | 477 | ||||
-rw-r--r-- | third_party/brotli/dec/decode.h | 136 | ||||
-rw-r--r-- | third_party/brotli/dec/dictionary.c | 2 | ||||
-rw-r--r-- | third_party/brotli/dec/dictionary.h | 15 | ||||
-rw-r--r-- | third_party/brotli/dec/huffman.c | 207 | ||||
-rw-r--r-- | third_party/brotli/dec/huffman.h | 23 | ||||
-rw-r--r-- | third_party/brotli/dec/port.h | 50 | ||||
-rw-r--r-- | third_party/brotli/dec/prefix.h | 2 | ||||
-rw-r--r-- | third_party/brotli/dec/state.c | 26 | ||||
-rw-r--r-- | third_party/brotli/dec/state.h | 23 | ||||
-rw-r--r-- | third_party/brotli/dec/streams.c | 102 | ||||
-rw-r--r-- | third_party/brotli/dec/streams.h | 95 | ||||
-rw-r--r-- | third_party/brotli/dec/transform.h | 82 | ||||
-rw-r--r-- | third_party/brotli/dec/types.h | 4 |
21 files changed, 454 insertions, 859 deletions
diff --git a/third_party/brotli/BUILD.gn b/third_party/brotli/BUILD.gn index 00b1daf..e25cf2b 100644 --- a/third_party/brotli/BUILD.gn +++ b/third_party/brotli/BUILD.gn @@ -17,8 +17,6 @@ source_set("brotli") { "dec/prefix.h", "dec/state.c", "dec/state.h", - "dec/streams.c", - "dec/streams.h", "dec/transform.h", "dec/types.h", ] diff --git a/third_party/brotli/README.chromium b/third_party/brotli/README.chromium index 8908727..63f8a24 100644 --- a/third_party/brotli/README.chromium +++ b/third_party/brotli/README.chromium @@ -1,6 +1,6 @@ Name: Brotli URL: https://github.com/google/brotli -Version: 24ffa78414663b545b66be392caff7eb5574a62c +Version: 722f8996b05bdec7c375462f9139e72ed33e3c85 License: MIT License File: LICENSE Security Critical: yes @@ -16,4 +16,3 @@ Local Modifications: - .gitignore: Added. - BUILD.gn: Added. - brotli.gyp: Added. -- Cherry-picked 37a320dd81db8d546cd24a45b4c61d87b45dcade (04 Feb 2016)
\ No newline at end of file diff --git a/third_party/brotli/brotli.gyp b/third_party/brotli/brotli.gyp index f7b3aa1..08fa2d6 100644 --- a/third_party/brotli/brotli.gyp +++ b/third_party/brotli/brotli.gyp @@ -24,8 +24,6 @@ 'dec/prefix.h', 'dec/state.c', 'dec/state.h', - 'dec/streams.c', - 'dec/streams.h', 'dec/transform.h', 'dec/types.h', ], diff --git a/third_party/brotli/dec/Makefile b/third_party/brotli/dec/Makefile index fa2fc3d..4d11ed1 100644 --- a/third_party/brotli/dec/Makefile +++ b/third_party/brotli/dec/Makefile @@ -4,7 +4,7 @@ include ../shared.mk CFLAGS += -Wall -OBJS = bit_reader.o decode.o dictionary.o huffman.o state.o streams.o +OBJS = bit_reader.o decode.o dictionary.o huffman.o state.o all : $(OBJS) diff --git a/third_party/brotli/dec/bit_reader.c b/third_party/brotli/dec/bit_reader.c index fc814d0..cde90af 100644 --- a/third_party/brotli/dec/bit_reader.c +++ b/third_party/brotli/dec/bit_reader.c @@ -6,10 +6,10 @@ /* Bit reading helpers */ -#include <stdlib.h> - #include "./bit_reader.h" + #include "./port.h" +#include "./types.h" #if defined(__cplusplus) || defined(c_plusplus) extern "C" { @@ -44,5 +44,5 @@ int BrotliWarmupBitReader(BrotliBitReader* const br) { } #if defined(__cplusplus) || defined(c_plusplus) -} /* extern "C" */ +} /* extern "C" */ #endif diff --git a/third_party/brotli/dec/bit_reader.h b/third_party/brotli/dec/bit_reader.h index 0301a48..e77d114 100644 --- a/third_party/brotli/dec/bit_reader.h +++ b/third_party/brotli/dec/bit_reader.h @@ -9,11 +9,15 @@ #ifndef BROTLI_DEC_BIT_READER_H_ #define BROTLI_DEC_BIT_READER_H_ -#include <stdio.h> -#include <string.h> +#include <string.h> /* memcpy */ + #include "./port.h" #include "./types.h" +#ifdef BROTLI_DECODE_DEBUG +#include <stdio.h> +#endif + #if defined(__cplusplus) || defined(c_plusplus) extern "C" { #endif @@ -48,17 +52,17 @@ static BROTLI_INLINE uint32_t BitMask(uint32_t n) { } typedef struct { - reg_t val_; /* pre-fetched bits */ - uint32_t bit_pos_; /* current bit-reading position in val_ */ - const uint8_t* next_in; /* the byte we're reading from */ - size_t avail_in; + reg_t val_; /* pre-fetched bits */ + uint32_t bit_pos_; /* current bit-reading position in val_ */ + const uint8_t* next_in; /* the byte we're reading from */ + size_t avail_in; } BrotliBitReader; typedef struct { - reg_t val_; + reg_t val_; uint32_t bit_pos_; const uint8_t* next_in; - size_t avail_in; + size_t avail_in; } BrotliBitReaderState; /* Initializes the bitreader fields. */ @@ -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)); } @@ -228,9 +230,9 @@ static BROTLI_INLINE int BrotliPullByte(BrotliBitReader* const br) { } br->val_ >>= 8; #if (BROTLI_64_BITS) - br->val_ |= ((uint64_t)*br->next_in) << 56; + br->val_ |= ((uint64_t)*br->next_in) << 56; #else - br->val_ |= ((uint32_t)*br->next_in) << 24; + br->val_ |= ((uint32_t)*br->next_in) << 24; #endif br->bit_pos_ -= 8; --br->avail_in; @@ -262,7 +264,7 @@ static BROTLI_INLINE uint32_t BrotliGetBits( /* Tries to peek the specified amount of bits. Returns 0, if there is not enough input. */ static BROTLI_INLINE int BrotliSafeGetBits( - BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { + BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { while (BrotliGetAvailableBits(br) < n_bits) { if (!BrotliPullByte(br)) { return 0; @@ -283,7 +285,11 @@ static BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) { uint32_t unused_bits = unused_bytes << 3; br->avail_in += unused_bytes; br->next_in -= unused_bytes; - br->val_ <<= unused_bits; + if (unused_bits == sizeof(br->val_) << 3) { + br->val_ = 0; + } else { + br->val_ <<= unused_bits; + } br->bit_pos_ += unused_bits; } @@ -322,7 +328,7 @@ static BROTLI_INLINE uint32_t BrotliReadBits( /* Tries to read the specified amount of bits. Returns 0, if there is not enough input. n_bits MUST be positive. */ static BROTLI_INLINE int BrotliSafeReadBits( - BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { + BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { while (BrotliGetAvailableBits(br) < n_bits) { if (!BrotliPullByte(br)) { return 0; @@ -349,9 +355,7 @@ static BROTLI_INLINE int BrotliJumpToByteBoundary(BrotliBitReader* br) { static BROTLI_INLINE int BrotliPeekByte(BrotliBitReader* br, size_t offset) { uint32_t available_bits = BrotliGetAvailableBits(br); size_t bytes_left = available_bits >> 3; - if (available_bits & 7) { - return -1; - } + BROTLI_DCHECK((available_bits & 7) == 0); if (offset < bytes_left) { return (BrotliGetBitsUnmasked(br) >> (unsigned)(offset << 3)) & 0xFF; } @@ -379,7 +383,7 @@ static BROTLI_INLINE void BrotliCopyBytes(uint8_t* dest, } #if defined(__cplusplus) || defined(c_plusplus) -} /* extern "C" */ +} /* extern "C" */ #endif #endif /* BROTLI_DEC_BIT_READER_H_ */ diff --git a/third_party/brotli/dec/context.h b/third_party/brotli/dec/context.h index b71f9a3..37ebe6a 100644 --- a/third_party/brotli/dec/context.h +++ b/third_party/brotli/dec/context.h @@ -102,10 +102,10 @@ #include "./types.h" enum ContextType { - CONTEXT_LSB6 = 0, - CONTEXT_MSB6 = 1, - CONTEXT_UTF8 = 2, - CONTEXT_SIGNED = 3 + CONTEXT_LSB6 = 0, + CONTEXT_MSB6 = 1, + CONTEXT_UTF8 = 2, + CONTEXT_SIGNED = 3 }; /* Common context lookup table for all context modes. */ diff --git a/third_party/brotli/dec/decode.c b/third_party/brotli/dec/decode.c index 401dd15..a81706b 100644 --- a/third_party/brotli/dec/decode.c +++ b/third_party/brotli/dec/decode.c @@ -4,31 +4,47 @@ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT */ -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include "./bit_reader.h" -#include "./context.h" #include "./decode.h" -#include "./dictionary.h" -#include "./port.h" -#include "./transform.h" -#include "./huffman.h" -#include "./prefix.h" #ifdef __ARM_NEON__ #include <arm_neon.h> #endif +#include <stdio.h> /* printf (debug output) */ +#include <stdlib.h> /* free, malloc */ +#include <string.h> /* memcpy, memset */ + +#include "./bit_reader.h" +#include "./context.h" +#include "./dictionary.h" +#include "./huffman.h" +#include "./port.h" +#include "./prefix.h" +#include "./transform.h" + #if defined(__cplusplus) || defined(c_plusplus) extern "C" { #endif +/* 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) && !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) { + fprintf(stderr, "ERROR at %s:%d (%s)\n", f, l, fn); + fflush(stderr); + return BROTLI_RESULT_ERROR; +} +#endif + #ifdef BROTLI_DECODE_DEBUG -#define BROTLI_LOG_UINT(name) \ +#define BROTLI_LOG_UINT(name) \ printf("[%s] %s = %lu\n", __func__, #name, (unsigned long)(name)) -#define BROTLI_LOG_ARRAY_INDEX(array_name, idx) \ - printf("[%s] %s[%lu] = %lu\n", __func__, #array_name, \ +#define BROTLI_LOG_ARRAY_INDEX(array_name, idx) \ + printf("[%s] %s[%lu] = %lu\n", __func__, #array_name, \ (unsigned long)(idx), (unsigned long)array_name[idx]) #define BROTLI_LOG(x) printf x #else @@ -45,8 +61,8 @@ static const uint32_t kNumBlockLengthCodes = 26; static const int kLiteralContextBits = 6; static const int kDistanceContextBits = 2; -#define HUFFMAN_TABLE_BITS 8U -#define HUFFMAN_TABLE_MASK 0xff +#define HUFFMAN_TABLE_BITS 8U +#define HUFFMAN_TABLE_MASK 0xff #define CODE_LENGTH_CODES 18 static const uint8_t kCodeLengthCodeOrder[CODE_LENGTH_CODES] = { @@ -111,24 +127,13 @@ static uint32_t DecodeWindowBits(BrotliBitReader* br) { return 17; } -static BROTLI_INLINE BROTLI_NO_ASAN void memmove16( - uint8_t* dst, uint8_t* src) { -#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. - */ +static BROTLI_INLINE void memmove16(uint8_t* dst, uint8_t* src) { +#if defined(__ARM_NEON__) + vst1q_u8(dst, vld1q_u8(src)); +#else uint32_t buffer[4]; memcpy(buffer, src, 16); memcpy(dst, buffer, 16); -#elif defined(__ARM_NEON__) - 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 } @@ -239,7 +244,7 @@ static BrotliResult BROTLI_NOINLINE DecodeMetaBlockLength(BrotliState* s, /* No break, transit to the next state. */ case BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED: - if (!s->is_last_metablock && !s->is_metadata) { + if (!s->is_last_metablock) { if (!BrotliSafeReadBits(br, 1, &bits)) { return BROTLI_RESULT_NEEDS_MORE_INPUT; } @@ -283,9 +288,9 @@ static BrotliResult BROTLI_NOINLINE DecodeMetaBlockLength(BrotliState* s, } s->meta_block_remaining_len |= (int)(bits << (i * 8)); } - s->substate_metablock_header = - BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED; - break; + ++s->meta_block_remaining_len; + s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; + return BROTLI_RESULT_SUCCESS; default: return BROTLI_FAILURE(); @@ -473,6 +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)); } (*symbol)++; } @@ -513,6 +519,8 @@ static BROTLI_INLINE void ProcessRepeatedCodeLength(uint32_t code_len, *space = 0xFFFFF; return; } + BROTLI_LOG(("[ReadHuffmanCode] code_length[%d..%d] = %d\n", + *symbol, *symbol + repeat_delta - 1, *repeat_code_len)); if (*repeat_code_len != 0) { unsigned last = *symbol + repeat_delta; int next = next_symbol[*repeat_code_len]; @@ -522,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; } @@ -558,8 +566,8 @@ static BrotliResult ReadSymbolCodeLengths( BrotliFillBitWindow16(br); p += BrotliGetBitsUnmasked(br) & BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH); - BrotliDropBits(br, p->bits); /* Use 1..5 bits */ - code_len = p->value; /* code_len == 0..17 */ + BrotliDropBits(br, p->bits); /* Use 1..5 bits */ + code_len = p->value; /* code_len == 0..17 */ if (code_len < kCodeLengthRepeatCode) { ProcessSingleCodeLength(code_len, &symbol, &repeat, &space, &prev_code_len, symbol_lists, code_length_histo, next_symbol); @@ -775,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; } @@ -916,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) { @@ -1018,9 +1026,9 @@ rleCode: s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE; return BROTLI_RESULT_SUCCESS; } + default: + return BROTLI_FAILURE(); } - - return BROTLI_FAILURE(); } /* Decodes a command or literal and updates block type ringbuffer. @@ -1028,9 +1036,10 @@ rleCode: static BROTLI_INLINE int DecodeBlockTypeAndLength(int safe, BrotliState* s, int tree_type) { uint32_t max_block_type = s->num_block_types[tree_type]; - int tree_offset = tree_type * BROTLI_HUFFMAN_MAX_TABLE_SIZE; - const HuffmanCode* type_tree = &s->block_type_trees[tree_offset]; - const HuffmanCode* len_tree = &s->block_len_trees[tree_offset]; + const HuffmanCode* type_tree = &s->block_type_trees[ + tree_type * BROTLI_HUFFMAN_MAX_SIZE_258]; + const HuffmanCode* len_tree = &s->block_len_trees[ + tree_type * BROTLI_HUFFMAN_MAX_SIZE_26]; BrotliBitReader* br = &s->br; uint32_t* ringbuffer = &s->block_type_rb[tree_type * 2]; uint32_t block_type; @@ -1133,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) { @@ -1152,7 +1160,7 @@ static BrotliResult WriteRingBuffer(size_t* available_out, uint8_t** next_out, *available_out -= num_written; BROTLI_LOG_UINT(to_write); BROTLI_LOG_UINT(num_written); - s->partial_pos_out += (size_t)num_written; + s->partial_pos_out += num_written; *total_out = s->partial_pos_out; if (num_written < to_write) { return BROTLI_RESULT_NEEDS_MORE_OUTPUT; @@ -1160,9 +1168,48 @@ static BrotliResult WriteRingBuffer(size_t* available_out, uint8_t** next_out, return BROTLI_RESULT_SUCCESS; } +/* Allocates ringbuffer. + + s->ringbuffer_size MUST be updated by BrotliCalculateRingBufferSize before + this function is called. + + Last two bytes of ringbuffer are initialized to 0, so context calculation + could be done uniformly for the first two and all other positions. + + Custom dictionary, if any, is copied to the end of ringbuffer. +*/ +static int BROTLI_NOINLINE BrotliAllocateRingBuffer(BrotliState* s) { + /* We need the slack region for the following reasons: + - doing up to two 16-byte copies for fast backward copying + - inserting transformed dictionary word (5 prefix + 24 base + 8 suffix) */ + static const int kRingBufferWriteAheadSlack = 42; + s->ringbuffer = (uint8_t*)BROTLI_ALLOC(s, (size_t)(s->ringbuffer_size + + kRingBufferWriteAheadSlack)); + if (s->ringbuffer == 0) { + return 0; + } + + 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); + } + + return 1; +} + static BrotliResult BROTLI_NOINLINE CopyUncompressedBlockToOutput( size_t* available_out, uint8_t** next_out, size_t* total_out, BrotliState* s) { + /* TODO: avoid allocation for single uncompressed block. */ + if (!s->ringbuffer && !BrotliAllocateRingBuffer(s)) { + return BROTLI_FAILURE(); + } + /* State machine */ for (;;) { switch (s->substate_uncompressed) { @@ -1185,12 +1232,11 @@ static BrotliResult BROTLI_NOINLINE CopyUncompressedBlockToOutput( return BROTLI_RESULT_NEEDS_MORE_INPUT; } s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_WRITE; - /*s->partial_pos_rb += (size_t)s->ringbuffer_size;*/ /* 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; } @@ -1231,29 +1277,23 @@ int BrotliDecompressedSize(size_t encoded_size, return (next_block_header != -1) && ((next_block_header & 3) == 3); } -/* Allocates the smallest feasible ring buffer. +/* Calculates 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. + When this method is called, metablock size and flags MUST be decoded. */ -static int BROTLI_NOINLINE BrotliAllocateRingBuffer(BrotliState* s, +static void BROTLI_NOINLINE BrotliCalculateRingBufferSize(BrotliState* s, BrotliBitReader* br) { - /* We need the slack region for the following reasons: - - doing up to two 16-byte copies for fast backward copying - - inserting transformed dictionary word (5 prefix + 24 base + 8 suffix) */ - static const int kRingBufferWriteAheadSlack = 42; int is_last = s->is_last_metablock; s->ringbuffer_size = 1 << s->window_bits; if (s->is_uncompressed) { - 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 */ + 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; } } @@ -1262,8 +1302,8 @@ static int BROTLI_NOINLINE BrotliAllocateRingBuffer(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; } } @@ -1274,20 +1314,6 @@ static int BROTLI_NOINLINE BrotliAllocateRingBuffer(BrotliState* s, } s->ringbuffer_mask = s->ringbuffer_size - 1; - s->ringbuffer = (uint8_t*)BROTLI_ALLOC(s, (size_t)(s->ringbuffer_size + - kRingBufferWriteAheadSlack + kBrotliMaxDictionaryWordLength)); - if (s->ringbuffer == 0) { - return 0; - } - 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); - } - - return 1; } /* Reads 1..256 2-bit context modes. */ @@ -1378,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; @@ -1461,13 +1487,6 @@ static BROTLI_INLINE int SafeReadCommand(BrotliState* s, BrotliBitReader* br, return ReadCommandInternal(1, s, br, insert_length); } -static BROTLI_INLINE int WarmupBitReader(int safe, BrotliBitReader* const br) { - if (safe) { - return 1; - } - return BrotliWarmupBitReader(br); -} - static BROTLI_INLINE int CheckInputAmount(int safe, BrotliBitReader* const br, size_t num) { if (safe) { @@ -1476,16 +1495,17 @@ static BROTLI_INLINE int CheckInputAmount(int safe, return BrotliCheckInputAmount(br, num); } -#define BROTLI_SAFE(METHOD) { \ - if (safe) { \ - if (! Safe ## METHOD ) { \ - result = BROTLI_RESULT_NEEDS_MORE_INPUT; \ - goto saveStateAndReturn; \ - } \ - } else { \ - METHOD ; \ - } \ -} +#define BROTLI_SAFE(METHOD) \ + { \ + if (safe) { \ + if (!Safe##METHOD) { \ + result = BROTLI_RESULT_NEEDS_MORE_INPUT; \ + goto saveStateAndReturn; \ + } \ + } else { \ + METHOD; \ + } \ + } static BROTLI_INLINE BrotliResult ProcessCommandsInternal(int safe, BrotliState* s) { @@ -1494,10 +1514,13 @@ static BROTLI_INLINE BrotliResult ProcessCommandsInternal(int safe, BrotliResult result = BROTLI_RESULT_SUCCESS; BrotliBitReader* br = &s->br; - if (!CheckInputAmount(safe, br, 28) || !WarmupBitReader(safe, br)) { + if (!CheckInputAmount(safe, br, 28)) { result = BROTLI_RESULT_NEEDS_MORE_INPUT; goto saveStateAndReturn; } + if (!safe) { + BROTLI_UNUSED(BrotliWarmupBitReader(br)); + } /* Jump into state machine. */ if (s->state == BROTLI_STATE_COMMAND_BEGIN) { @@ -1527,9 +1550,8 @@ CommandBegin: } /* Read the insert/copy length in the command */ BROTLI_SAFE(ReadCommand(s, br, &i)); - BROTLI_LOG_UINT(i); - BROTLI_LOG_UINT(s->copy_length); - BROTLI_LOG_UINT(s->distance_code); + BROTLI_LOG(("[ProcessCommandsInternal] pos = %d insert = %d copy = %d\n", + pos, i, s->copy_length)); if (i == 0) { goto CommandPostDecodeLiterals; } @@ -1555,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)) { @@ -1615,6 +1637,7 @@ CommandInner: } } while (--i != 0); } + BROTLI_LOG_UINT(s->meta_block_remaining_len); if (s->meta_block_remaining_len <= 0) { s->state = BROTLI_STATE_METABLOCK_DONE; goto saveStateAndReturn; @@ -1635,7 +1658,8 @@ CommandPostDecodeLiterals: } BROTLI_SAFE(ReadDistance(s, br)); postReadDistance: - BROTLI_LOG_UINT(s->distance_code); + BROTLI_LOG(("[ProcessCommandsInternal] pos = %d distance = %d\n", + pos, 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; @@ -1649,7 +1673,7 @@ postReadDistance: if (s->distance_code > s->max_distance) { if (i >= kBrotliMinDictionaryWordLength && i <= kBrotliMaxDictionaryWordLength) { - int offset = kBrotliDictionaryOffsetsByLength[i]; + int offset = (int)kBrotliDictionaryOffsetsByLength[i]; int word_id = s->distance_code - s->max_distance - 1; uint32_t shift = kBrotliDictionarySizeBitsByLength[i]; int mask = (int)BitMask(shift); @@ -1674,58 +1698,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)); + "len: %d bytes left: %d\n", + 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) { - goto CommandPostWrapCopy; - } - if (copy_src + i > copy_dst) { - goto postSelfintersecting; - } - } else { - if (copy_src >= ringbuffer_end_minus_copy_length) { - goto CommandPostWrapCopy; - } - if (copy_dst + i > copy_src) { - goto postSelfintersecting; - } + if (src_end > pos && dst_end > src_start) { + /* Regions intersect. */ + goto CommandPostWrapCopy; + } + if (dst_end >= s->ringbuffer_size || src_end >= s->ringbuffer_size) { + /* At least one region wraps. */ + goto CommandPostWrapCopy; } pos += i; if (i > 16) { @@ -1738,6 +1748,7 @@ postReadDistance: } } } + BROTLI_LOG_UINT(s->meta_block_remaining_len); if (s->meta_block_remaining_len <= 0) { /* Next metablock, if any */ s->state = BROTLI_STATE_METABLOCK_DONE; @@ -1745,30 +1756,17 @@ 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; - 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;*/ - s->state = BROTLI_STATE_COMMAND_POST_WRITE_2; - goto saveStateAndReturn; + { + int wrap_guard = s->ringbuffer_size - pos; + while (--i >= 0) { + s->ringbuffer[pos] = + s->ringbuffer[(pos - s->distance_code) & s->ringbuffer_mask]; + ++pos; + if (PREDICT_FALSE(--wrap_guard == 0)) { + s->state = BROTLI_STATE_COMMAND_POST_WRITE_2; + goto saveStateAndReturn; + } } } if (s->meta_block_remaining_len <= 0) { @@ -1817,113 +1815,6 @@ BrotliResult BrotliDecompressBuffer(size_t encoded_size, return result; } -BrotliResult BrotliDecompress(BrotliInput input, BrotliOutput output) { - BrotliState s; - BrotliResult result; - BrotliStateInit(&s); - result = BrotliDecompressStreaming(input, output, 1, &s); - if (result == BROTLI_RESULT_NEEDS_MORE_INPUT) { - /* Not ok: it didn't finish even though this is a non-streaming function. */ - result = BROTLI_FAILURE(); - } - BrotliStateCleanup(&s); - return result; -} - -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) { - BrotliResult result = BrotliDecompressStream(available_in, next_in, - available_out, next_out, total_out, s); - if (finish && result == BROTLI_RESULT_NEEDS_MORE_INPUT) { - result = BROTLI_FAILURE(); - } - return result; -} - -BrotliResult BrotliDecompressStreaming(BrotliInput input, BrotliOutput output, - int finish, BrotliState* s) { - const size_t kBufferSize = 65536; - BrotliResult result; - uint8_t* input_buffer; - uint8_t* output_buffer; - size_t avail_in; - const uint8_t* next_in; - size_t total_out; - - if (s->legacy_input_buffer == 0) { - s->legacy_input_buffer = (uint8_t*)BROTLI_ALLOC(s, kBufferSize); - } - if (s->legacy_output_buffer == 0) { - s->legacy_output_buffer = (uint8_t*)BROTLI_ALLOC(s, kBufferSize); - } - if (s->legacy_input_buffer == 0 || s->legacy_output_buffer == 0) { - return BROTLI_FAILURE(); - } - input_buffer = s->legacy_input_buffer; - output_buffer = s->legacy_output_buffer; - - /* Push remaining output. */ - if (s->legacy_output_len > s->legacy_output_pos) { - size_t to_write = s->legacy_output_len - s->legacy_output_pos; - int num_written = BrotliWrite( - output, output_buffer + s->legacy_output_pos, to_write); - if (num_written < 0) { - return BROTLI_FAILURE(); - } - s->legacy_output_pos += (size_t)num_written; - if ((size_t)num_written < to_write) { - return BROTLI_RESULT_NEEDS_MORE_OUTPUT; - } - } - s->legacy_output_pos = 0; - - avail_in = s->legacy_input_len - s->legacy_input_pos; - next_in = input_buffer + s->legacy_input_pos; - - while (1) { - size_t to_write; - int num_written; - size_t avail_out = kBufferSize; - uint8_t* next_out = output_buffer; - result = BrotliDecompressStream(&avail_in, &next_in, - &avail_out, &next_out, &total_out, s); - s->legacy_input_pos = (size_t)(next_out - input_buffer); - to_write = (size_t)(next_out - output_buffer); - num_written = BrotliWrite(output, output_buffer, to_write); - if (num_written < 0) { - return BROTLI_FAILURE(); - } - if ((size_t)num_written < to_write) { - s->legacy_output_len = to_write; - s->legacy_output_pos = (size_t)num_written; - return BROTLI_RESULT_NEEDS_MORE_OUTPUT; - } - if (result == BROTLI_RESULT_NEEDS_MORE_INPUT) { - int num_read = BrotliRead(input, input_buffer, kBufferSize); - if (num_read < 0 || (num_read == 0 && finish)) { - return BROTLI_FAILURE(); - } - if (num_read == 0) { - s->legacy_input_len = 0; - s->legacy_input_pos = 0; - return BROTLI_RESULT_NEEDS_MORE_INPUT; - } - avail_in = (size_t)num_read; - next_in = input_buffer; - s->legacy_input_len = avail_in; - s->legacy_input_pos = 0; - } else if (result != BROTLI_RESULT_NEEDS_MORE_OUTPUT) { - /* Success or failure. */ - return result; - } - } -} - /* Invariant: input stream is never overconsumed: * invalid input implies that the whole stream is invalid -> any amount of input could be read and discarded @@ -2034,13 +1925,14 @@ BrotliResult BrotliDecompressStream(size_t* available_in, /* Allocate memory for both block_type_trees and block_len_trees. */ s->block_type_trees = (HuffmanCode*)BROTLI_ALLOC(s, - 6 * BROTLI_HUFFMAN_MAX_TABLE_SIZE * sizeof(HuffmanCode)); + sizeof(HuffmanCode) * 3 * + (BROTLI_HUFFMAN_MAX_SIZE_258 + BROTLI_HUFFMAN_MAX_SIZE_26)); if (s->block_type_trees == 0) { result = BROTLI_FAILURE(); break; } - s->block_len_trees = s->block_type_trees + - 3 * BROTLI_HUFFMAN_MAX_TABLE_SIZE; + 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 */ @@ -2073,10 +1965,7 @@ BrotliResult BrotliDecompressStream(size_t* available_in, break; } if (!s->ringbuffer) { - if (!BrotliAllocateRingBuffer(s, br)) { - result = BROTLI_FAILURE(); - break; - } + BrotliCalculateRingBufferSize(s, br); } if (s->is_uncompressed) { s->state = BROTLI_STATE_UNCOMPRESSED; @@ -2128,7 +2017,7 @@ BrotliResult BrotliDecompressStream(size_t* available_in, s->state = BROTLI_STATE_HUFFMAN_CODE_1; /* No break, continue to next state */ case BROTLI_STATE_HUFFMAN_CODE_1: { - int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_TABLE_SIZE; + int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_258; result = ReadHuffmanCode(s->num_block_types[s->loop_counter] + 2, &s->block_type_trees[tree_offset], NULL, s); if (result != BROTLI_RESULT_SUCCESS) break; @@ -2136,7 +2025,7 @@ BrotliResult BrotliDecompressStream(size_t* available_in, /* No break, continue to next state */ } case BROTLI_STATE_HUFFMAN_CODE_2: { - int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_TABLE_SIZE; + int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26; result = ReadHuffmanCode(kNumBlockLengthCodes, &s->block_len_trees[tree_offset], NULL, s); if (result != BROTLI_RESULT_SUCCESS) break; @@ -2144,7 +2033,7 @@ BrotliResult BrotliDecompressStream(size_t* available_in, /* No break, continue to next state */ } case BROTLI_STATE_HUFFMAN_CODE_3: { - int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_TABLE_SIZE; + int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26; if (!SafeReadBlockLength(s, &s->block_length[s->loop_counter], &s->block_len_trees[tree_offset], br)) { result = BROTLI_RESULT_NEEDS_MORE_INPUT; @@ -2163,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); @@ -2216,7 +2105,7 @@ BrotliResult BrotliDecompressStream(size_t* available_in, s->num_literal_htrees); BrotliHuffmanTreeGroupInit(s, &s->insert_copy_hgroup, kNumInsertAndCopyCodes, - s->num_block_types[1]); + s->num_block_types[1]); BrotliHuffmanTreeGroupInit(s, &s->distance_hgroup, num_distance_codes, s->num_dist_htrees); if (s->literal_hgroup.codes == 0 || @@ -2241,6 +2130,8 @@ BrotliResult BrotliDecompressStream(size_t* available_in, case 2: hgroup = &s->distance_hgroup; break; + default: + return BROTLI_FAILURE(); } result = HuffmanTreeGroupDecode(hgroup, s); } @@ -2256,6 +2147,10 @@ BrotliResult BrotliDecompressStream(size_t* available_in, &kContextLookup[kContextLookupOffsets[context_mode + 1]]; s->htree_command = s->insert_copy_hgroup.htrees[0]; s->literal_htree = s->literal_hgroup.htrees[s->literal_htree_index]; + if (!s->ringbuffer && !BrotliAllocateRingBuffer(s)) { + result = BROTLI_FAILURE(); + break; + } s->state = BROTLI_STATE_COMMAND_BEGIN; } break; @@ -2280,7 +2175,7 @@ BrotliResult BrotliDecompressStream(size_t* available_in, s->max_distance = s->max_backward_distance; if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_1) { memcpy(s->ringbuffer, s->ringbuffer_end, (size_t)s->pos); - if (s->meta_block_remaining_len <= 0) { + if (s->meta_block_remaining_len == 0) { /* Next metablock, if any */ s->state = BROTLI_STATE_METABLOCK_DONE; } else { @@ -2291,7 +2186,7 @@ BrotliResult BrotliDecompressStream(size_t* available_in, s->state = BROTLI_STATE_COMMAND_POST_WRAP_COPY; } else { /* BROTLI_STATE_COMMAND_INNER_WRITE */ if (s->loop_counter == 0) { - if (s->meta_block_remaining_len <= 0) { + if (s->meta_block_remaining_len == 0) { s->state = BROTLI_STATE_METABLOCK_DONE; } else { s->state = BROTLI_STATE_COMMAND_POST_DECODE_LITERALS; @@ -2333,10 +2228,10 @@ 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; } #if defined(__cplusplus) || defined(c_plusplus) -} /* extern "C" */ +} /* extern "C" */ #endif diff --git a/third_party/brotli/dec/decode.h b/third_party/brotli/dec/decode.h index af3c0b5..074b2f5 100644 --- a/third_party/brotli/dec/decode.h +++ b/third_party/brotli/dec/decode.h @@ -10,7 +10,6 @@ #define BROTLI_DEC_DECODE_H_ #include "./state.h" -#include "./streams.h" #include "./types.h" #if defined(__cplusplus) || defined(c_plusplus) @@ -18,126 +17,57 @@ extern "C" { #endif typedef enum { - /* Decoding error, e.g. corrupt input or no memory */ + /* Decoding error, e.g. corrupt input or memory allocation problem */ BROTLI_RESULT_ERROR = 0, - /* Successfully completely done */ + /* Decoding successfully completed */ BROTLI_RESULT_SUCCESS = 1, - /* Partially done, but must be called again with more input */ + /* Partially done; should be called again with more input */ BROTLI_RESULT_NEEDS_MORE_INPUT = 2, - /* Partially done, but must be called again with more output */ + /* Partially done; should be called again with more output */ BROTLI_RESULT_NEEDS_MORE_OUTPUT = 3 } BrotliResult; -/* 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) -#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) { - fprintf(stderr, "ERROR at %s:%d (%s)\n", f, l, fn); - fflush(stderr); - return BROTLI_RESULT_ERROR; -} -#endif - -/* Creates the instance of BrotliState and initializes it. alloc_func and - free_func MUST be both zero or both non-zero. In the case they are both zero, - default memory allocators are used. opaque parameter is passed to alloc_func - and free_func when they are called. */ +/* Creates the instance of BrotliState and initializes it. |alloc_func| and + |free_func| MUST be both zero or both non-zero. In the case they are both + zero, default memory allocators are used. |opaque| is passed to |alloc_func| + and |free_func| when they are called. */ BrotliState* BrotliCreateState( brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque); /* Deinitializes and frees BrotliState instance. */ void BrotliDestroyState(BrotliState* state); -/* Sets *decoded_size to the decompressed size of the given encoded stream. */ -/* This function only works if the encoded buffer has a single meta block, */ -/* or if it has two meta-blocks, where the first is uncompressed and the */ -/* second is empty. */ -/* Returns 1 on success, 0 on failure. */ +/* Sets |*decoded_size| to the decompressed size of the given encoded stream. + This function only works if the encoded buffer has a single meta block, + or if it has two meta-blocks, where the first is uncompressed and the + second is empty. + Returns 1 on success, 0 on failure. */ int BrotliDecompressedSize(size_t encoded_size, const uint8_t* encoded_buffer, size_t* decoded_size); -/* Decompresses the data in encoded_buffer into decoded_buffer, and sets */ -/* *decoded_size to the decompressed length. */ +/* Decompresses the data in |encoded_buffer| into |decoded_buffer|, and sets + |*decoded_size| to the decompressed length. */ BrotliResult BrotliDecompressBuffer(size_t encoded_size, const uint8_t* encoded_buffer, size_t* decoded_size, uint8_t* decoded_buffer); -/* Same as above, but uses the specified input and output callbacks instead */ -/* of reading from and writing to pre-allocated memory buffers. */ -/* DEPRECATED */ -BrotliResult BrotliDecompress(BrotliInput input, BrotliOutput output); - -/* Same as above, but supports the caller to call the decoder repeatedly with - partial data to support streaming. The state must be initialized with - BrotliStateInit and reused with every call for the same stream. - Return values: - 0: failure. - 1: success, and done. - 2: success so far, end not reached so should call again with more input. - The finish parameter is used as follows, for a series of calls with the - same state: - 0: Every call except the last one must be called with finish set to 0. The - last call may have finish set to either 0 or 1. Only if finish is 0, can - the function return 2. It may also return 0 or 1, in that case no more - calls (even with finish 1) may be made. - 1: Only the last call may have finish set to 1. It's ok to give empty input - if all input was already given to previous calls. It is also ok to have - only one single call in total, with finish 1, and with all input - available immediately. That matches the non-streaming case. If finish is - 1, the function can only return 0 or 1, never 2. After a finish, no more - calls may be done. - After everything is done, the state must be cleaned with BrotliStateCleanup - to free allocated resources. - The given BrotliOutput must always accept all output and make enough space, - it returning a smaller value than the amount of bytes to write always results - in an error. -*/ -/* DEPRECATED */ -BrotliResult BrotliDecompressStreaming(BrotliInput input, BrotliOutput output, - int finish, BrotliState* s); - -/* Same as above, but with memory buffers. - Must be called with an allocated input buffer in *next_in and an allocated - output buffer in *next_out. The values *available_in and *available_out - must specify the allocated size in *next_in and *next_out respectively. - The value *total_out must be 0 initially, and will be summed with the - amount of output bytes written after each call, so that at the end it - gives the complete decoded size. - After each call, *available_in will be decremented by the amount of input - bytes consumed, and the *next_in pointer will be incremented by that amount. - Similarly, *available_out will be decremented by the amount of output - bytes written, and the *next_out pointer will be incremented by that - amount. - - The input may be partial. With each next function call, *next_in and - *available_in must be updated to point to a next part of the compressed - input. The current implementation will always consume all input unless - an error occurs, so normally *available_in will always be 0 after - calling this function and the next adjacent part of input is desired. - - In the current implementation, the function requires that there is enough - output buffer size to write all currently processed input, so - *available_out must be large enough. Since the function updates *next_out - each time, as long as the output buffer is large enough you can keep - reusing this variable. It is also possible to update *next_out and - *available_out yourself before a next call, e.g. to point to a new larger - buffer. -*/ -/* DEPRECATED */ -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); +/* Decompresses the data. Supports partial input and output. + + Must be called with an allocated input buffer in |*next_in| and an allocated + output buffer in |*next_out|. The values |*available_in| and |*available_out| + must specify the allocated size in |*next_in| and |*next_out| respectively. + + After each call, |*available_in| will be decremented by the amount of input + bytes consumed, and the |*next_in| pointer will be incremented by that + amount. Similarly, |*available_out| will be decremented by the amount of + output bytes written, and the |*next_out| pointer will be incremented by that + amount. |total_out| will be set to the number of bytes decompressed since + last state initialization. + Input is never overconsumed, so |next_in| and |available_in| could be passed + to the next consumer after decoding is complete. */ BrotliResult BrotliDecompressStream(size_t* available_in, const uint8_t** next_in, size_t* available_out, @@ -150,10 +80,10 @@ BrotliResult BrotliDecompressStream(size_t* available_in, Not to be confused with the built-in transformable dictionary of Brotli. The dictionary must exist in memory until decoding is done and is owned by the caller. To use: - -initialize state with BrotliStateInit - -use BrotliSetCustomDictionary - -use BrotliDecompressBufferStreaming - -clean up with BrotliStateCleanup + 1) initialize state with BrotliStateInit + 2) use BrotliSetCustomDictionary + 3) use BrotliDecompressStream + 4) clean up with BrotliStateCleanup */ void BrotliSetCustomDictionary( size_t size, const uint8_t* dict, BrotliState* s); diff --git a/third_party/brotli/dec/dictionary.c b/third_party/brotli/dec/dictionary.c index d87f277..f8f5857 100644 --- a/third_party/brotli/dec/dictionary.c +++ b/third_party/brotli/dec/dictionary.c @@ -9462,5 +9462,5 @@ const uint8_t kBrotliDictionary[122784] = { }; #if defined(__cplusplus) || defined(c_plusplus) -} /* extern "C" */ +} /* extern "C" */ #endif diff --git a/third_party/brotli/dec/dictionary.h b/third_party/brotli/dec/dictionary.h index b327fd6..ae250c2 100644 --- a/third_party/brotli/dec/dictionary.h +++ b/third_party/brotli/dec/dictionary.h @@ -17,23 +17,22 @@ extern "C" { extern const uint8_t kBrotliDictionary[122784]; -static const int 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, +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 }; 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; static const int kBrotliMaxDictionaryWordLength = 24; #if defined(__cplusplus) || defined(c_plusplus) -} /* extern "C" */ +} /* extern "C" */ #endif #endif /* BROTLI_DEC_DICTIONARY_H_ */ diff --git a/third_party/brotli/dec/huffman.c b/third_party/brotli/dec/huffman.c index 291f0a7..159ac1c 100644 --- a/third_party/brotli/dec/huffman.c +++ b/third_party/brotli/dec/huffman.c @@ -6,11 +6,12 @@ /* Utilities for building Huffman decoding tables. */ -#include <stdlib.h> -#include <stdio.h> -#include <string.h> #include "./huffman.h" + +#include <string.h> /* memcpy, memset */ + #include "./port.h" +#include "./types.h" #if defined(__cplusplus) || defined(c_plusplus) extern "C" { @@ -23,43 +24,43 @@ extern "C" { #else #define BROTLI_REVERSE_BITS_BASE 0 static uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = { - 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, - 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, - 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, - 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, - 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, - 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, - 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, - 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC, - 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, - 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, - 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, - 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA, - 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, - 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6, - 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, - 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE, - 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, - 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1, - 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, - 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9, - 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, - 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5, - 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, - 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD, - 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, - 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3, - 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, - 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, - 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, - 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, - 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, - 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF + 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, + 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, + 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, + 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, + 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, + 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, + 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, + 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC, + 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, + 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, + 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, + 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA, + 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, + 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6, + 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, + 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE, + 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, + 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1, + 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, + 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9, + 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, + 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5, + 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, + 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD, + 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, + 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3, + 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, + 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, + 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, + 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, + 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, + 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF }; -#endif /* BROTLI_RBIT */ +#endif /* BROTLI_RBIT */ #define BROTLI_REVERSE_BITS_LOWEST \ - (1U << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE)) + (1U << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE)) /* Returns reverse(num >> BROTLI_REVERSE_BITS_BASE, BROTLI_REVERSE_BITS_MAX), where reverse(value, len) is the bit-wise reversal of the len least @@ -101,20 +102,20 @@ static BROTLI_INLINE int NextTableBitSize(const uint16_t* const count, void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table, const uint8_t* const code_lengths, - uint16_t *count) { - HuffmanCode code; /* current table entry */ - int symbol; /* symbol index in original or sorted table */ - uint32_t key; /* prefix code */ - uint32_t key_step; /* prefix code addend */ - int step; /* step size to replicate values in current table */ - int table_size; /* size of current table */ - int sorted[18]; /* symbols sorted by code length */ + uint16_t* count) { + HuffmanCode code; /* current table entry */ + int symbol; /* symbol index in original or sorted table */ + uint32_t key; /* prefix code */ + uint32_t key_step; /* prefix code addend */ + int step; /* step size to replicate values in current table */ + int table_size; /* size of current table */ + int sorted[18]; /* symbols sorted by code length */ /* offsets in sorted table for each length */ 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; @@ -169,26 +170,26 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table, uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table, int root_bits, const uint16_t* const symbol_lists, - uint16_t *count) { - HuffmanCode code; /* current table entry */ - HuffmanCode* table; /* next available space in table */ - int len; /* current code length */ - int symbol; /* symbol index in original or sorted 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 */ - 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 */ - int total_size; /* sum of root table size and 2nd level table sizes */ + uint16_t* count) { + HuffmanCode code; /* current table entry */ + HuffmanCode* table; /* next available space in table */ + int len; /* current code length */ + int symbol; /* symbol index in original or sorted 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 */ + 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 */ + int total_size; /* sum of root table size and 2nd level table sizes */ int max_length = -1; int bits; 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; @@ -237,7 +238,7 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table, for (len = root_bits + 1, step = 2; len <= max_length; ++len) { symbol = len - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1); for (; count[len] != 0; --count[len]) { - if (sub_key == (uint32_t)(BROTLI_REVERSE_BITS_LOWEST << 1)) { + if (sub_key == (BROTLI_REVERSE_BITS_LOWEST << 1U)) { table += table_size; table_bits = NextTableBitSize(count, len, root_bits); table_size = 1 << table_bits; @@ -245,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); @@ -264,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; @@ -301,49 +302,47 @@ uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table, table[3].bits = 2; table_size = 4; break; - case 3: - { - int i, k; - for (i = 0; i < 3; ++i) { - for (k = i + 1; k < 4; ++k) { - if (val[k] < val[i]) { - uint16_t t = val[k]; - val[k] = val[i]; - val[i] = t; - } + case 3: { + int i, k; + for (i = 0; i < 3; ++i) { + for (k = i + 1; k < 4; ++k) { + if (val[k] < val[i]) { + uint16_t t = val[k]; + val[k] = val[i]; + val[i] = t; } } - for (i = 0; i < 4; ++i) { - table[i].bits = 2; - } - table[0].value = val[0]; - table[2].value = val[1]; - table[1].value = val[2]; - table[3].value = val[3]; - table_size = 4; } + for (i = 0; i < 4; ++i) { + table[i].bits = 2; + } + table[0].value = val[0]; + table[2].value = val[1]; + table[1].value = val[2]; + table[3].value = val[3]; + table_size = 4; break; - case 4: - { - int i; - if (val[3] < val[2]) { - uint16_t t = val[3]; - val[3] = val[2]; - val[2] = t; - } - for (i = 0; i < 7; ++i) { - table[i].value = val[0]; - table[i].bits = (uint8_t)(1 + (i & 1)); - } - table[1].value = val[1]; - table[3].value = val[2]; - table[5].value = val[1]; - table[7].value = val[3]; - table[3].bits = 3; - table[7].bits = 3; - table_size = 8; + } + case 4: { + int i; + if (val[3] < val[2]) { + uint16_t t = val[3]; + val[3] = val[2]; + val[2] = t; + } + for (i = 0; i < 7; ++i) { + table[i].value = val[0]; + table[i].bits = (uint8_t)(1 + (i & 1)); } + table[1].value = val[1]; + table[3].value = val[2]; + table[5].value = val[1]; + table[7].value = val[3]; + table[3].bits = 3; + table[7].bits = 3; + table_size = 8; break; + } } while (table_size != goal_size) { memcpy(&table[table_size], &table[0], @@ -354,5 +353,5 @@ uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table, } #if defined(__cplusplus) || defined(c_plusplus) -} /* extern "C" */ +} /* extern "C" */ #endif diff --git a/third_party/brotli/dec/huffman.h b/third_party/brotli/dec/huffman.h index e8481f0..7cbec80 100644 --- a/third_party/brotli/dec/huffman.h +++ b/third_party/brotli/dec/huffman.h @@ -20,29 +20,34 @@ extern "C" { /* For current format this constant equals to kNumInsertAndCopyCodes */ #define BROTLI_HUFFMAN_MAX_CODE_LENGTHS_SIZE 704 -/* Maximum possible Huffman table size for an alphabet size of 704, max code - * length 15 and root table bits 8. */ -#define BROTLI_HUFFMAN_MAX_TABLE_SIZE 1080 +/* Maximum possible Huffman table size for an alphabet size of (index * 32), + * max code length 15 and root table bits 8. */ +static const uint16_t kMaxHuffmanTableSize[] = { + 256, 402, 436, 468, 500, 534, 566, 598, 630, 662, 694, 726, 758, 790, 822, + 854, 886, 920, 952, 984, 1016, 1048, 1080}; +#define BROTLI_HUFFMAN_MAX_SIZE_26 396 +#define BROTLI_HUFFMAN_MAX_SIZE_258 632 +#define BROTLI_HUFFMAN_MAX_SIZE_272 646 #define BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH 5 typedef struct { - uint8_t bits; /* number of bits used for this symbol */ - uint16_t value; /* symbol value or table offset */ + uint8_t bits; /* number of bits used for this symbol */ + uint16_t value; /* symbol value or table offset */ } HuffmanCode; /* 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 */ @@ -50,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. */ @@ -62,7 +67,7 @@ typedef struct { } HuffmanTreeGroup; #if defined(__cplusplus) || defined(c_plusplus) -} /* extern "C" */ +} /* extern "C" */ #endif #endif /* BROTLI_DEC_HUFFMAN_H_ */ diff --git a/third_party/brotli/dec/port.h b/third_party/brotli/dec/port.h index f80e957..6cbe498 100644 --- a/third_party/brotli/dec/port.h +++ b/third_party/brotli/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,25 +83,15 @@ #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 -#define BROTLI_SAFE_MEMMOVE 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_SAFE_MEMMOVE 0 +#define BROTLI_ALIGNED_READ 1 #endif -#define BROTLI_ASAN_BUILD __has_feature(address_sanitizer) - /* Define "PREDICT_TRUE" and "PREDICT_FALSE" macros for capable compilers. To apply compiler hint, enclose the branching condition into macros, like this: @@ -141,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 @@ -194,20 +184,14 @@ OR: #endif #if BROTLI_MODERN_COMPILER || __has_attribute(noinline) -#define BROTLI_NOINLINE __attribute__ ((noinline)) +#define BROTLI_NOINLINE __attribute__((noinline)) #else #define BROTLI_NOINLINE #endif -#if BROTLI_ASAN_BUILD && !defined(BROTLI_BUILD_PORTABLE) -#define BROTLI_NO_ASAN __attribute__((no_sanitize("address"))) BROTLI_NOINLINE -#else -#define BROTLI_NO_ASAN -#endif - -#define BROTLI_REPEAT(N, X) { \ - if ((N & 1) != 0) {X;} \ - if ((N & 2) != 0) {X; X;} \ +#define BROTLI_REPEAT(N, X) { \ + if ((N & 1) != 0) {X;} \ + if ((N & 2) != 0) {X; X;} \ if ((N & 4) != 0) {X; X; X; X;} \ } @@ -230,9 +214,9 @@ static BROTLI_INLINE unsigned BrotliRBit(unsigned input) { #define BROTLI_ALLOC(S, L) S->alloc_func(S->memory_manager_opaque, L) -#define BROTLI_FREE(S, X) { \ +#define BROTLI_FREE(S, X) { \ S->free_func(S->memory_manager_opaque, X); \ - X = NULL; \ + X = NULL; \ } #define BROTLI_UNUSED(X) (void)(X) diff --git a/third_party/brotli/dec/prefix.h b/third_party/brotli/dec/prefix.h index 41263fd..eaae37f 100644 --- a/third_party/brotli/dec/prefix.h +++ b/third_party/brotli/dec/prefix.h @@ -11,6 +11,8 @@ #ifndef BROTLI_DEC_PREFIX_H_ #define BROTLI_DEC_PREFIX_H_ +#include "./types.h" + /* Represents the range of values belonging to a prefix code: */ /* [offset, offset + 2^nbits) */ struct PrefixCodeRange { diff --git a/third_party/brotli/dec/state.c b/third_party/brotli/dec/state.c index d5815e0..52c0e6c 100644 --- a/third_party/brotli/dec/state.c +++ b/third_party/brotli/dec/state.c @@ -4,11 +4,12 @@ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT */ -#include "./huffman.h" #include "./state.h" -#include <stdlib.h> -#include <string.h> +#include <stdlib.h> /* free, malloc */ + +#include "./huffman.h" +#include "./types.h" #if defined(__cplusplus) || defined(c_plusplus) extern "C" { @@ -94,13 +95,6 @@ void BrotliStateInitWithCustomAllocators(BrotliState* s, s->symbol_lists = &s->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1]; s->mtf_upper_bound = 255; - - s->legacy_input_buffer = 0; - s->legacy_output_buffer = 0; - s->legacy_input_len = 0; - s->legacy_output_len = 0; - s->legacy_input_pos = 0; - s->legacy_output_pos = 0; } void BrotliStateMetablockBegin(BrotliState* s) { @@ -150,8 +144,6 @@ void BrotliStateCleanup(BrotliState* s) { BROTLI_FREE(s, s->ringbuffer); BROTLI_FREE(s, s->block_type_trees); - BROTLI_FREE(s, s->legacy_input_buffer); - BROTLI_FREE(s, s->legacy_output_buffer); } int BrotliStateIsStreamStart(const BrotliState* s) { @@ -166,10 +158,10 @@ int BrotliStateIsStreamEnd(const BrotliState* s) { void BrotliHuffmanTreeGroupInit(BrotliState* s, HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t ntrees) { /* Pack two allocations into one */ - const size_t code_size = - sizeof(HuffmanCode) * (size_t)(ntrees * BROTLI_HUFFMAN_MAX_TABLE_SIZE); - const size_t htree_size = sizeof(HuffmanCode*) * (size_t)ntrees; - char *p = (char*)BROTLI_ALLOC(s, code_size + htree_size); + 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); group->alphabet_size = (uint16_t)alphabet_size; group->num_htrees = (uint16_t)ntrees; group->codes = (HuffmanCode*)p; @@ -182,5 +174,5 @@ void BrotliHuffmanTreeGroupRelease(BrotliState* s, HuffmanTreeGroup* group) { } #if defined(__cplusplus) || defined(c_plusplus) -} /* extern "C" */ +} /* extern "C" */ #endif diff --git a/third_party/brotli/dec/state.h b/third_party/brotli/dec/state.h index f569f58b..925fa14 100644 --- a/third_party/brotli/dec/state.h +++ b/third_party/brotli/dec/state.h @@ -9,7 +9,6 @@ #ifndef BROTLI_DEC_STATE_H_ #define BROTLI_DEC_STATE_H_ -#include <stdio.h> #include "./bit_reader.h" #include "./huffman.h" #include "./types.h" @@ -95,6 +94,10 @@ typedef enum { struct BrotliStateStruct { BrotliRunningState state; + + /* This counter is reused for several disjoint loops. */ + int loop_counter; + BrotliBitReader br; brotli_alloc_func alloc_func; @@ -108,8 +111,6 @@ struct BrotliStateStruct { } buffer; uint32_t buffer_length; - /* This counter is reused for several disjoint loops. */ - int loop_counter; int pos; int max_backward_distance; int max_backward_distance_minus_custom_dict_size; @@ -149,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; @@ -173,7 +174,7 @@ struct BrotliStateStruct { uint16_t* symbol_lists; /* Storage from symbol_lists. */ uint16_t symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1 + - BROTLI_HUFFMAN_MAX_CODE_LENGTHS_SIZE]; + BROTLI_HUFFMAN_MAX_CODE_LENGTHS_SIZE]; /* Tails of symbol chains. */ int next_symbol[32]; uint8_t code_length_code_lengths[18]; @@ -188,7 +189,7 @@ struct BrotliStateStruct { uint32_t context_index; uint32_t max_run_length_prefix; uint32_t code; - HuffmanCode context_map_table[BROTLI_HUFFMAN_MAX_TABLE_SIZE]; + HuffmanCode context_map_table[BROTLI_HUFFMAN_MAX_SIZE_272]; /* For InverseMoveToFrontTransform */ uint32_t mtf_upper_bound; @@ -217,13 +218,6 @@ struct BrotliStateStruct { uint32_t num_literal_htrees; uint8_t* context_map; uint8_t* context_modes; - - uint8_t* legacy_input_buffer; - uint8_t* legacy_output_buffer; - size_t legacy_input_len; - size_t legacy_output_len; - size_t legacy_input_pos; - size_t legacy_output_pos; }; typedef struct BrotliStateStruct BrotliState; @@ -248,9 +242,8 @@ 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" */ +} /* extern "C" */ #endif #endif /* BROTLI_DEC_STATE_H_ */ diff --git a/third_party/brotli/dec/streams.c b/third_party/brotli/dec/streams.c deleted file mode 100644 index 0c781a7..0000000 --- a/third_party/brotli/dec/streams.c +++ /dev/null @@ -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 diff --git a/third_party/brotli/dec/streams.h b/third_party/brotli/dec/streams.h deleted file mode 100644 index a849432..0000000 --- a/third_party/brotli/dec/streams.h +++ /dev/null @@ -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_ */ diff --git a/third_party/brotli/dec/transform.h b/third_party/brotli/dec/transform.h index f47eeb4..8c08f3f 100644 --- a/third_party/brotli/dec/transform.h +++ b/third_party/brotli/dec/transform.h @@ -9,8 +9,6 @@ #ifndef BROTLI_DEC_TRANSFORM_H_ #define BROTLI_DEC_TRANSFORM_H_ -#include <stdio.h> -#include <ctype.h> #include "./port.h" #include "./types.h" @@ -19,27 +17,27 @@ extern "C" { #endif enum WordTransformType { - kIdentity = 0, - kOmitLast1 = 1, - kOmitLast2 = 2, - kOmitLast3 = 3, - kOmitLast4 = 4, - kOmitLast5 = 5, - kOmitLast6 = 6, - kOmitLast7 = 7, - kOmitLast8 = 8, - kOmitLast9 = 9, + kIdentity = 0, + kOmitLast1 = 1, + kOmitLast2 = 2, + kOmitLast3 = 3, + kOmitLast4 = 4, + kOmitLast5 = 5, + kOmitLast6 = 6, + kOmitLast7 = 7, + kOmitLast8 = 8, + kOmitLast9 = 9, kUppercaseFirst = 10, - kUppercaseAll = 11, - kOmitFirst1 = 12, - kOmitFirst2 = 13, - kOmitFirst3 = 14, - kOmitFirst4 = 15, - kOmitFirst5 = 16, - kOmitFirst6 = 17, - kOmitFirst7 = 18, - kOmitFirst8 = 19, - kOmitFirst9 = 20 + kUppercaseAll = 11, + kOmitFirst1 = 12, + kOmitFirst2 = 13, + kOmitFirst3 = 14, + kOmitFirst4 = 15, + kOmitFirst5 = 16, + kOmitFirst6 = 17, + kOmitFirst7 = 18, + kOmitFirst8 = 19, + kOmitFirst9 = 20 }; typedef struct { @@ -68,15 +66,15 @@ enum { kPFix_SP = 1, kPFix_COMMASP = 3, kPFix_SPofSPtheSP = 6, - kPFix_SPtheSP = 9, + kPFix_SPtheSP = 9, kPFix_eSP = 12, - kPFix_SPofSP = 15, + kPFix_SPofSP = 15, kPFix_sSP = 20, kPFix_DOT = 23, kPFix_SPandSP = 25, kPFix_SPinSP = 31, kPFix_DQUOT = 36, - kPFix_SPtoSP = 38, + kPFix_SPtoSP = 38, kPFix_DQUOTGT = 43, kPFix_NEWLINE = 46, kPFix_DOTSP = 48, @@ -86,20 +84,20 @@ enum { kPFix_SPthatSP = 63, kPFix_SQUOT = 70, kPFix_SPwithSP = 72, - kPFix_SPfromSP = 79, - kPFix_SPbySP = 86, + kPFix_SPfromSP = 79, + kPFix_SPbySP = 86, kPFix_OPEN = 91, kPFix_DOTSPTheSP = 93, - kPFix_SPonSP = 100, - kPFix_SPasSP = 105, - kPFix_SPisSP = 110, + kPFix_SPonSP = 100, + kPFix_SPasSP = 105, + kPFix_SPisSP = 110, kPFix_ingSP = 115, kPFix_NEWLINETAB = 120, kPFix_COLON = 123, kPFix_edSP = 125, kPFix_EQDQUOT = 129, kPFix_SPatSP = 132, - kPFix_lySP = 137, + kPFix_lySP = 137, kPFix_COMMA = 141, kPFix_EQSQUOT = 143, kPFix_DOTcomSLASH = 146, @@ -116,7 +114,6 @@ enum { kPFix_ousSP = 203 }; - static const Transform kTransforms[] = { { kPFix_EMPTY, kIdentity, kPFix_EMPTY }, { kPFix_EMPTY, kIdentity, kPFix_SP }, @@ -243,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; @@ -269,22 +266,19 @@ static BROTLI_NOINLINE int TransformDictionaryWord( } { const int t = kTransforms[transform].transform; - int skip = t < kOmitFirst1 ? 0 : t - (kOmitFirst1 - 1); int i = 0; - uint8_t* uppercase; - if (skip > len) { - skip = len; - } - word += skip; - len -= skip; - if (t <= kOmitLast9) { + int skip = t - (kOmitFirst1 - 1); + if (skip > 0) { + word += skip; + len -= skip; + } else if (t <= kOmitLast9) { len -= t; } while (i < len) { dst[idx++] = word[i++]; } - uppercase = &dst[idx - len]; if (t == kUppercaseFirst) { - ToUpperCase(uppercase); + ToUpperCase(&dst[idx - len]); } else if (t == kUppercaseAll) { + uint8_t* uppercase = &dst[idx - len]; while (len > 0) { int step = ToUpperCase(uppercase); uppercase += step; @@ -300,7 +294,7 @@ static BROTLI_NOINLINE int TransformDictionaryWord( } #if defined(__cplusplus) || defined(c_plusplus) -} /* extern "C" */ +} /* extern "C" */ #endif #endif /* BROTLI_DEC_TRANSFORM_H_ */ diff --git a/third_party/brotli/dec/types.h b/third_party/brotli/dec/types.h index 56fde60..0f7a021 100644 --- a/third_party/brotli/dec/types.h +++ b/third_party/brotli/dec/types.h @@ -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_ */ |