Changeset 222903 in webkit
- Timestamp:
- Oct 5, 2017 12:54:02 AM (7 years ago)
- Location:
- trunk/Source/ThirdParty
- Files:
-
- 15 added
- 5 deleted
- 28 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/ThirdParty/ChangeLog
r220403 r222903 1 2017-10-05 Frederic Wang <fwang@igalia.com> 2 3 Upgrade Brotli to version 1.0.1 and WOFF2 to latest upstream 4 https://bugs.webkit.org/show_bug.cgi?id=177768 5 6 Reviewed by Michael Catanzaro. 7 8 * brotli/CMakeLists.txt: 9 * brotli/README.webkit: 10 * brotli/common/constants.h: Added. 11 * brotli/common/dictionary.bin: Added. 12 * brotli/common/dictionary.c: Added. 13 * brotli/common/dictionary.h: Added. 14 * brotli/common/version.h: Added. 15 * brotli/dec/Makefile: Removed. 16 * brotli/dec/bit_reader.c: 17 * brotli/dec/bit_reader.h: 18 * brotli/dec/context.h: 19 * brotli/dec/decode.c: 20 * brotli/dec/decode.h: Removed. 21 * brotli/dec/dictionary.c: Removed. 22 * brotli/dec/dictionary.h: Removed. 23 * brotli/dec/huffman.c: 24 * brotli/dec/huffman.h: 25 * brotli/dec/port.h: 26 (BrotliDump): 27 (BrotliRBit): 28 * brotli/dec/prefix.h: 29 * brotli/dec/state.c: 30 * brotli/dec/state.h: 31 * brotli/dec/transform.h: 32 * brotli/dec/types.h: Removed. 33 * brotli/include/brotli/decode.h: Added. 34 * brotli/include/brotli/encode.h: Added. 35 * brotli/include/brotli/port.h: Added. 36 * brotli/include/brotli/types.h: Added. 37 * brotli/update.sh: 38 * woff2/CMakeLists.txt: 39 * woff2/README.webkit: 40 * woff2/src/buffer.h: 41 (woff2::Buffer::Buffer): 42 (woff2::Buffer::Read): 43 * woff2/src/convert_woff2ttf_*.cc: Added. 44 * woff2/src/font.cc: 45 * woff2/src/glyph.cc: 46 * woff2/src/normalize.cc: 47 * woff2/src/port.h: 48 * woff2/src/store_bytes.h: 49 (woff2::Store16): 50 * woff2/src/variable_length.cc: 51 * woff2/src/woff2_common.cc: 52 * woff2/src/woff2_dec.cc: 53 * woff2/src/woff2_decompress.cc: 54 * woff2/src/woff2_enc.cc: 55 * woff2/src/woff2_info.cc: Added. 56 1 57 2017-08-08 Michael Catanzaro <mcatanzaro@igalia.com> 2 58 -
trunk/Source/ThirdParty/brotli/CMakeLists.txt
r220403 r222903 2 2 3 3 set(BROTLI_INCLUDE_DIRECTORIES 4 "${BROTLI_DIR}/common" 4 5 "${BROTLI_DIR}/dec" 6 "${BROTLI_DIR}/include" 5 7 ) 6 8 7 9 set(BROTLI_SOURCES 10 ${BROTLI_DIR}/common/dictionary.c 8 11 ${BROTLI_DIR}/dec/bit_reader.c 9 12 ${BROTLI_DIR}/dec/decode.c 10 ${BROTLI_DIR}/dec/dictionary.c11 13 ${BROTLI_DIR}/dec/huffman.c 12 14 ${BROTLI_DIR}/dec/state.c -
trunk/Source/ThirdParty/brotli/README.webkit
r202225 r222903 15 15 from within the modules/brotli directory. 16 16 17 Current version: [commit 29d31d5921b0a2b323ac24e7f7d0cdc9a3c0dd08].17 Current version: [commit 4f8cd4c0f4443d51a9603790a445b06c073b7a4d]. -
trunk/Source/ThirdParty/brotli/dec/bit_reader.c
r197933 r222903 9 9 #include "./bit_reader.h" 10 10 11 #include <brotli/types.h> 11 12 #include "./port.h" 12 #include "./types.h"13 13 14 14 #if defined(__cplusplus) || defined(c_plusplus) … … 21 21 } 22 22 23 intBrotliWarmupBitReader(BrotliBitReader* const br) {23 BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br) { 24 24 size_t aligned_read_mask = (sizeof(br->val_) >> 1) - 1; 25 25 /* Fixing alignment after unaligned BrotliFillWindow would result accumulator 26 26 overflow. If unalignment is caused by BrotliSafeReadBits, then there is 27 enough space in accumulator to fix alig ment. */27 enough space in accumulator to fix alignment. */ 28 28 if (!BROTLI_ALIGNED_READ) { 29 29 aligned_read_mask = 0; … … 31 31 if (BrotliGetAvailableBits(br) == 0) { 32 32 if (!BrotliPullByte(br)) { 33 return 0;33 return BROTLI_FALSE; 34 34 } 35 35 } … … 38 38 if (!BrotliPullByte(br)) { 39 39 /* If we consumed all the input, we don't care about the alignment. */ 40 return 1;40 return BROTLI_TRUE; 41 41 } 42 42 } 43 return 1;43 return BROTLI_TRUE; 44 44 } 45 45 -
trunk/Source/ThirdParty/brotli/dec/bit_reader.h
r202225 r222903 12 12 #include <string.h> /* memcpy */ 13 13 14 #include <brotli/types.h> 14 15 #include "./port.h" 15 #include "./types.h"16 16 17 17 #if defined(__cplusplus) || defined(c_plusplus) … … 19 19 #endif 20 20 21 #if (BROTLI_64_BITS) 22 #define BROTLI_SHORT_FILL_BIT_WINDOW_READ 4 23 typedef uint64_t reg_t; 24 #else 25 #define BROTLI_SHORT_FILL_BIT_WINDOW_READ 2 26 typedef uint32_t reg_t; 27 #endif 21 #define BROTLI_SHORT_FILL_BIT_WINDOW_READ (sizeof(reg_t) >> 1) 28 22 29 23 static const uint32_t kBitMask[33] = { 0x0000, … … 62 56 } BrotliBitReaderState; 63 57 64 /* Initializes the bitreader fields. */58 /* Initializes the BrotliBitReader fields. */ 65 59 BROTLI_INTERNAL void BrotliInitBitReader(BrotliBitReader* const br); 66 60 … … 69 63 For BROTLI_ALIGNED_READ this function also prepares bit reader for aligned 70 64 reading. */ 71 BROTLI_INTERNAL intBrotliWarmupBitReader(BrotliBitReader* const br);65 BROTLI_INTERNAL BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br); 72 66 73 67 static BROTLI_INLINE void BrotliBitReaderSaveState( … … 98 92 } 99 93 100 /* Checks if there is at least num bytes left in the input ringbuffer (excluding101 the bits remaining in br->val_). */102 static BROTLI_INLINE intBrotliCheckInputAmount(94 /* Checks if there is at least |num| bytes left in the input ring-buffer 95 (excluding the bits remaining in br->val_). */ 96 static BROTLI_INLINE BROTLI_BOOL BrotliCheckInputAmount( 103 97 BrotliBitReader* const br, size_t num) { 104 return br->avail_in >= num;98 return TO_BROTLI_BOOL(br->avail_in >= num); 105 99 } 106 100 … … 164 158 Precondition: accumulator contains at least 1 bit. 165 159 n_bits should be in the range [1..24] for regular build. For portable 166 non-64-bit little 160 non-64-bit little-endian build only 16 bits are safe to request. */ 167 161 static BROTLI_INLINE void BrotliFillBitWindow( 168 162 BrotliBitReader* const br, uint32_t n_bits) { … … 214 208 } 215 209 216 /* Mos ltly like BrotliFillBitWindow, but guarantees only 16 bits and reads no210 /* Mostly like BrotliFillBitWindow, but guarantees only 16 bits and reads no 217 211 more than BROTLI_SHORT_FILL_BIT_WINDOW_READ bytes of input. */ 218 212 static BROTLI_INLINE void BrotliFillBitWindow16(BrotliBitReader* const br) { … … 221 215 222 216 /* Pulls one byte of input to accumulator. */ 223 static BROTLI_INLINE intBrotliPullByte(BrotliBitReader* const br) {217 static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) { 224 218 if (br->avail_in == 0) { 225 return 0;219 return BROTLI_FALSE; 226 220 } 227 221 br->val_ >>= 8; … … 234 228 --br->avail_in; 235 229 ++br->next_in; 236 return 1;230 return BROTLI_TRUE; 237 231 } 238 232 239 233 /* Returns currently available bits. 240 The number of valid bits could be calc lulated by BrotliGetAvailableBits. */234 The number of valid bits could be calculated by BrotliGetAvailableBits. */ 241 235 static BROTLI_INLINE reg_t BrotliGetBitsUnmasked(BrotliBitReader* const br) { 242 236 return br->val_ >> br->bit_pos_; … … 251 245 } 252 246 253 /* Returns the specified number of bits from brwithout advancing bit pos. */247 /* Returns the specified number of bits from |br| without advancing bit pos. */ 254 248 static BROTLI_INLINE uint32_t BrotliGetBits( 255 249 BrotliBitReader* const br, uint32_t n_bits) { … … 260 254 /* Tries to peek the specified amount of bits. Returns 0, if there is not 261 255 enough input. */ 262 static BROTLI_INLINE intBrotliSafeGetBits(256 static BROTLI_INLINE BROTLI_BOOL BrotliSafeGetBits( 263 257 BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { 264 258 while (BrotliGetAvailableBits(br) < n_bits) { 265 259 if (!BrotliPullByte(br)) { 266 return 0;260 return BROTLI_FALSE; 267 261 } 268 262 } 269 263 *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits); 270 return 1;264 return BROTLI_TRUE; 271 265 } 272 266 … … 290 284 } 291 285 292 /* Reads the specified number of bits from brand advances the bit pos.286 /* Reads the specified number of bits from |br| and advances the bit pos. 293 287 Precondition: accumulator MUST contain at least n_bits. */ 294 288 static BROTLI_INLINE void BrotliTakeBits( … … 300 294 } 301 295 302 /* Reads the specified number of bits from brand advances the bit pos.296 /* Reads the specified number of bits from |br| and advances the bit pos. 303 297 Assumes that there is enough input to perform BrotliFillBitWindow. */ 304 298 static BROTLI_INLINE uint32_t BrotliReadBits( … … 322 316 /* Tries to read the specified amount of bits. Returns 0, if there is not 323 317 enough input. n_bits MUST be positive. */ 324 static BROTLI_INLINE intBrotliSafeReadBits(318 static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits( 325 319 BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { 326 320 while (BrotliGetAvailableBits(br) < n_bits) { 327 321 if (!BrotliPullByte(br)) { 328 return 0;322 return BROTLI_FALSE; 329 323 } 330 324 } 331 325 BrotliTakeBits(br, n_bits, val); 332 return 1;326 return BROTLI_TRUE; 333 327 } 334 328 335 329 /* Advances the bit reader position to the next byte boundary and verifies 336 330 that any skipped bits are set to zero. */ 337 static BROTLI_INLINE intBrotliJumpToByteBoundary(BrotliBitReader* br) {331 static BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) { 338 332 uint32_t pad_bits_count = BrotliGetAvailableBits(br) & 0x7; 339 333 uint32_t pad_bits = 0; … … 341 335 BrotliTakeBits(br, pad_bits_count, &pad_bits); 342 336 } 343 return pad_bits == 0; 344 } 345 346 /* Peeks a byte at specified offset. 347 Precondition: bit reader is parked to a byte boundary. 348 Returns -1 if operation is not feasible. */ 349 static BROTLI_INLINE int BrotliPeekByte(BrotliBitReader* br, size_t offset) { 350 uint32_t available_bits = BrotliGetAvailableBits(br); 351 size_t bytes_left = available_bits >> 3; 352 BROTLI_DCHECK((available_bits & 7) == 0); 353 if (offset < bytes_left) { 354 return (BrotliGetBitsUnmasked(br) >> (unsigned)(offset << 3)) & 0xFF; 355 } 356 offset -= bytes_left; 357 if (offset < br->avail_in) { 358 return br->next_in[offset]; 359 } 360 return -1; 337 return TO_BROTLI_BOOL(pad_bits == 0); 361 338 } 362 339 -
trunk/Source/ThirdParty/brotli/dec/context.h
r197933 r222903 100 100 #define BROTLI_DEC_CONTEXT_H_ 101 101 102 #include "./types.h"102 #include <brotli/types.h> 103 103 104 104 enum ContextType { -
trunk/Source/ThirdParty/brotli/dec/decode.c
r202225 r222903 5 5 */ 6 6 7 #include "./decode.h"7 #include <brotli/decode.h> 8 8 9 9 #ifdef __ARM_NEON__ … … 14 14 #include <string.h> /* memcpy, memset */ 15 15 16 #include "../common/constants.h" 17 #include "../common/dictionary.h" 18 #include "../common/version.h" 16 19 #include "./bit_reader.h" 17 20 #include "./context.h" 18 #include "./dictionary.h"19 21 #include "./huffman.h" 20 22 #include "./port.h" … … 35 37 (unsigned long)(idx), (unsigned long)array_name[idx])) 36 38 37 static const uint32_t kDefaultCodeLength = 8;38 static const uint32_t kCodeLengthRepeatCode = 16;39 static const uint32_t kNumLiteralCodes = 256;40 static const uint32_t kNumInsertAndCopyCodes = 704;41 static const uint32_t kNumBlockLengthCodes = 26;42 static const int kLiteralContextBits = 6;43 static const int kDistanceContextBits = 2;44 45 39 #define HUFFMAN_TABLE_BITS 8U 46 40 #define HUFFMAN_TABLE_MASK 0xff 47 41 48 #define CODE_LENGTH_CODES 18 49 static const uint8_t kCodeLengthCodeOrder[CODE_LENGTH_CODES] = { 42 /* We need the slack region for the following reasons: 43 - doing up to two 16-byte copies for fast backward copying 44 - inserting transformed dictionary word (5 prefix + 24 base + 8 suffix) */ 45 static const uint32_t kRingBufferWriteAheadSlack = 42; 46 47 static const uint8_t kCodeLengthCodeOrder[BROTLI_CODE_LENGTH_CODES] = { 50 48 1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15, 51 49 }; … … 60 58 }; 61 59 62 #define NUM_DISTANCE_SHORT_CODES 16 63 64 BrotliState* BrotliCreateState( 60 BROTLI_BOOL BrotliDecoderSetParameter( 61 BrotliDecoderState* state, BrotliDecoderParameter p, uint32_t value) { 62 switch (p) { 63 case BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION: 64 state->canny_ringbuffer_allocation = !!value ? 0 : 1; 65 return BROTLI_TRUE; 66 67 default: return BROTLI_FALSE; 68 } 69 } 70 71 BrotliDecoderState* BrotliDecoderCreateInstance( 65 72 brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) { 66 Brotli State* state = 0;73 BrotliDecoderState* state = 0; 67 74 if (!alloc_func && !free_func) { 68 state = (Brotli State*)malloc(sizeof(BrotliState));75 state = (BrotliDecoderState*)malloc(sizeof(BrotliDecoderState)); 69 76 } else if (alloc_func && free_func) { 70 state = (Brotli State*)alloc_func(opaque, sizeof(BrotliState));77 state = (BrotliDecoderState*)alloc_func(opaque, sizeof(BrotliDecoderState)); 71 78 } 72 79 if (state == 0) { … … 74 81 return 0; 75 82 } 76 Brotli StateInitWithCustomAllocators(state, alloc_func, free_func, opaque);77 state->error_code = BROTLI_NO_ERROR;83 BrotliDecoderStateInitWithCustomAllocators( 84 state, alloc_func, free_func, opaque); 78 85 return state; 79 86 } 80 87 81 /* Deinitializes and frees Brotli State instance. */82 void BrotliDe stroyState(BrotliState* state) {88 /* Deinitializes and frees BrotliDecoderState instance. */ 89 void BrotliDecoderDestroyInstance(BrotliDecoderState* state) { 83 90 if (!state) { 84 91 return; … … 86 93 brotli_free_func free_func = state->free_func; 87 94 void* opaque = state->memory_manager_opaque; 88 Brotli StateCleanup(state);95 BrotliDecoderStateCleanup(state); 89 96 free_func(opaque, state); 90 97 } 91 98 } 92 99 93 /* Saves error code and converts it to Brotli Result */94 static BROTLI_NOINLINE Brotli Result SaveErrorCode(95 Brotli State* s, BrotliErrorCode e) {100 /* Saves error code and converts it to BrotliDecoderResult */ 101 static BROTLI_NOINLINE BrotliDecoderResult SaveErrorCode( 102 BrotliDecoderState* s, BrotliDecoderErrorCode e) { 96 103 s->error_code = (int)e; 97 104 switch (e) { 98 case BROTLI_SUCCESS: return BROTLI_RESULT_SUCCESS; 99 case BROTLI_NEEDS_MORE_INPUT: return BROTLI_RESULT_NEEDS_MORE_INPUT; 100 case BROTLI_NEEDS_MORE_OUTPUT: return BROTLI_RESULT_NEEDS_MORE_OUTPUT; 101 default: return BROTLI_RESULT_ERROR; 105 case BROTLI_DECODER_SUCCESS: 106 return BROTLI_DECODER_RESULT_SUCCESS; 107 case BROTLI_DECODER_NEEDS_MORE_INPUT: 108 return BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT; 109 case BROTLI_DECODER_NEEDS_MORE_OUTPUT: 110 return BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT; 111 default: 112 return BROTLI_DECODER_RESULT_ERROR; 102 113 } 103 114 } … … 133 144 134 145 /* Decodes a number in the range [0..255], by reading 1 - 11 bits. */ 135 static BROTLI_NOINLINE Brotli ErrorCode DecodeVarLenUint8(BrotliState* s,136 Brotli BitReader* br, uint32_t* value) {146 static BROTLI_NOINLINE BrotliDecoderErrorCode DecodeVarLenUint8( 147 BrotliDecoderState* s, BrotliBitReader* br, uint32_t* value) { 137 148 uint32_t bits; 138 149 switch (s->substate_decode_uint8) { 139 150 case BROTLI_STATE_DECODE_UINT8_NONE: 140 if ( PREDICT_FALSE(!BrotliSafeReadBits(br, 1, &bits))) {141 return BROTLI_ NEEDS_MORE_INPUT;151 if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, 1, &bits))) { 152 return BROTLI_DECODER_NEEDS_MORE_INPUT; 142 153 } 143 154 if (bits == 0) { 144 155 *value = 0; 145 return BROTLI_ SUCCESS;156 return BROTLI_DECODER_SUCCESS; 146 157 } 147 158 /* No break, transit to the next state. */ 148 159 149 160 case BROTLI_STATE_DECODE_UINT8_SHORT: 150 if ( PREDICT_FALSE(!BrotliSafeReadBits(br, 3, &bits))) {161 if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, 3, &bits))) { 151 162 s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_SHORT; 152 return BROTLI_ NEEDS_MORE_INPUT;163 return BROTLI_DECODER_NEEDS_MORE_INPUT; 153 164 } 154 165 if (bits == 0) { 155 166 *value = 1; 156 167 s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE; 157 return BROTLI_ SUCCESS;168 return BROTLI_DECODER_SUCCESS; 158 169 } 159 170 /* Use output value as a temporary storage. It MUST be persisted. */ … … 162 173 163 174 case BROTLI_STATE_DECODE_UINT8_LONG: 164 if ( PREDICT_FALSE(!BrotliSafeReadBits(br, *value, &bits))) {175 if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, *value, &bits))) { 165 176 s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_LONG; 166 return BROTLI_ NEEDS_MORE_INPUT;177 return BROTLI_DECODER_NEEDS_MORE_INPUT; 167 178 } 168 179 *value = (1U << *value) + bits; 169 180 s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE; 170 return BROTLI_ SUCCESS;181 return BROTLI_DECODER_SUCCESS; 171 182 172 183 default: 173 return BROTLI_FAILURE(BROTLI_ERROR_UNREACHABLE); 184 return 185 BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); 174 186 } 175 187 } 176 188 177 189 /* Decodes a metablock length and flags by reading 2 - 31 bits. */ 178 static Brotli ErrorCode BROTLI_NOINLINE DecodeMetaBlockLength(179 Brotli State* s, BrotliBitReader* br) {190 static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength( 191 BrotliDecoderState* s, BrotliBitReader* br) { 180 192 uint32_t bits; 181 193 int i; … … 184 196 case BROTLI_STATE_METABLOCK_HEADER_NONE: 185 197 if (!BrotliSafeReadBits(br, 1, &bits)) { 186 return BROTLI_ NEEDS_MORE_INPUT;187 } 188 s->is_last_metablock = (uint8_t)bits;198 return BROTLI_DECODER_NEEDS_MORE_INPUT; 199 } 200 s->is_last_metablock = bits ? 1 : 0; 189 201 s->meta_block_remaining_len = 0; 190 202 s->is_uncompressed = 0; … … 199 211 case BROTLI_STATE_METABLOCK_HEADER_EMPTY: 200 212 if (!BrotliSafeReadBits(br, 1, &bits)) { 201 return BROTLI_ NEEDS_MORE_INPUT;213 return BROTLI_DECODER_NEEDS_MORE_INPUT; 202 214 } 203 215 if (bits) { 204 216 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; 205 return BROTLI_ SUCCESS;217 return BROTLI_DECODER_SUCCESS; 206 218 } 207 219 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NIBBLES; … … 210 222 case BROTLI_STATE_METABLOCK_HEADER_NIBBLES: 211 223 if (!BrotliSafeReadBits(br, 2, &bits)) { 212 return BROTLI_ NEEDS_MORE_INPUT;224 return BROTLI_DECODER_NEEDS_MORE_INPUT; 213 225 } 214 226 s->size_nibbles = (uint8_t)(bits + 4); … … 224 236 case BROTLI_STATE_METABLOCK_HEADER_SIZE: 225 237 i = s->loop_counter; 226 for (; i < s->size_nibbles; ++i) {238 for (; i < (int)s->size_nibbles; ++i) { 227 239 if (!BrotliSafeReadBits(br, 4, &bits)) { 228 240 s->loop_counter = i; 229 return BROTLI_ NEEDS_MORE_INPUT;241 return BROTLI_DECODER_NEEDS_MORE_INPUT; 230 242 } 231 243 if (i + 1 == s->size_nibbles && s->size_nibbles > 4 && bits == 0) { 232 return BROTLI_FAILURE(BROTLI_ ERROR_FORMAT_EXUBERANT_NIBBLE);244 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE); 233 245 } 234 246 s->meta_block_remaining_len |= (int)(bits << (i * 4)); … … 241 253 if (!s->is_last_metablock) { 242 254 if (!BrotliSafeReadBits(br, 1, &bits)) { 243 return BROTLI_ NEEDS_MORE_INPUT;255 return BROTLI_DECODER_NEEDS_MORE_INPUT; 244 256 } 245 s->is_uncompressed = (uint8_t)bits;257 s->is_uncompressed = bits ? 1 : 0; 246 258 } 247 259 ++s->meta_block_remaining_len; 248 260 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; 249 return BROTLI_ SUCCESS;261 return BROTLI_DECODER_SUCCESS; 250 262 251 263 case BROTLI_STATE_METABLOCK_HEADER_RESERVED: 252 264 if (!BrotliSafeReadBits(br, 1, &bits)) { 253 return BROTLI_ NEEDS_MORE_INPUT;265 return BROTLI_DECODER_NEEDS_MORE_INPUT; 254 266 } 255 267 if (bits != 0) { 256 return BROTLI_FAILURE(BROTLI_ ERROR_FORMAT_RESERVED);268 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_RESERVED); 257 269 } 258 270 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_BYTES; … … 261 273 case BROTLI_STATE_METABLOCK_HEADER_BYTES: 262 274 if (!BrotliSafeReadBits(br, 2, &bits)) { 263 return BROTLI_ NEEDS_MORE_INPUT;275 return BROTLI_DECODER_NEEDS_MORE_INPUT; 264 276 } 265 277 if (bits == 0) { 266 278 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; 267 return BROTLI_ SUCCESS;279 return BROTLI_DECODER_SUCCESS; 268 280 } 269 281 s->size_nibbles = (uint8_t)bits; … … 273 285 case BROTLI_STATE_METABLOCK_HEADER_METADATA: 274 286 i = s->loop_counter; 275 for (; i < s->size_nibbles; ++i) {287 for (; i < (int)s->size_nibbles; ++i) { 276 288 if (!BrotliSafeReadBits(br, 8, &bits)) { 277 289 s->loop_counter = i; 278 return BROTLI_ NEEDS_MORE_INPUT;290 return BROTLI_DECODER_NEEDS_MORE_INPUT; 279 291 } 280 292 if (i + 1 == s->size_nibbles && s->size_nibbles > 1 && bits == 0) { 281 return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_EXUBERANT_META_NIBBLE); 293 return BROTLI_FAILURE( 294 BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE); 282 295 } 283 296 s->meta_block_remaining_len |= (int)(bits << (i * 8)); … … 285 298 ++s->meta_block_remaining_len; 286 299 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; 287 return BROTLI_ SUCCESS;300 return BROTLI_DECODER_SUCCESS; 288 301 289 302 default: 290 return BROTLI_FAILURE(BROTLI_ERROR_UNREACHABLE); 303 return 304 BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); 291 305 } 292 306 } … … 320 334 /* Same as DecodeSymbol, but it is known that there is less than 15 bits of 321 335 input are currently available. */ 322 static BROTLI_NOINLINE int SafeDecodeSymbol(const HuffmanCode* table, 323 BrotliBitReader* br, 324 uint32_t* result) { 336 static BROTLI_NOINLINE BROTLI_BOOL SafeDecodeSymbol( 337 const HuffmanCode* table, BrotliBitReader* br, uint32_t* result) { 325 338 uint32_t val; 326 339 uint32_t available_bits = BrotliGetAvailableBits(br); … … 328 341 if (table->bits == 0) { 329 342 *result = table->value; 330 return 1;331 } 332 return 0; /* No valid bits at all. */343 return BROTLI_TRUE; 344 } 345 return BROTLI_FALSE; /* No valid bits at all. */ 333 346 } 334 347 val = (uint32_t)BrotliGetBitsUnmasked(br); … … 338 351 BrotliDropBits(br, table->bits); 339 352 *result = table->value; 340 return 1;353 return BROTLI_TRUE; 341 354 } else { 342 return 0; /* Not enough bits for the first level. */355 return BROTLI_FALSE; /* Not enough bits for the first level. */ 343 356 } 344 357 } 345 358 if (available_bits <= HUFFMAN_TABLE_BITS) { 346 return 0; /* Not enough bits to move to the second level. */359 return BROTLI_FALSE; /* Not enough bits to move to the second level. */ 347 360 } 348 361 … … 352 365 table += table->value + val; 353 366 if (available_bits < table->bits) { 354 return 0; /* Not enough bits for the second level. */367 return BROTLI_FALSE; /* Not enough bits for the second level. */ 355 368 } 356 369 357 370 BrotliDropBits(br, HUFFMAN_TABLE_BITS + table->bits); 358 371 *result = table->value; 359 return 1; 360 } 361 362 static BROTLI_INLINE int SafeReadSymbol(const HuffmanCode* table, 363 BrotliBitReader* br, 364 uint32_t* result) { 372 return BROTLI_TRUE; 373 } 374 375 static BROTLI_INLINE BROTLI_BOOL SafeReadSymbol( 376 const HuffmanCode* table, BrotliBitReader* br, uint32_t* result) { 365 377 uint32_t val; 366 if ( PREDICT_TRUE(BrotliSafeGetBits(br, 15, &val))) {378 if (BROTLI_PREDICT_TRUE(BrotliSafeGetBits(br, 15, &val))) { 367 379 *result = DecodeSymbol(val, table, br); 368 return 1;380 return BROTLI_TRUE; 369 381 } 370 382 return SafeDecodeSymbol(table, br, result); … … 392 404 uint32_t* value) { 393 405 uint32_t result = *value; 394 if ( PREDICT_FALSE(*bits > HUFFMAN_TABLE_BITS)) {406 if (BROTLI_PREDICT_FALSE(*bits > HUFFMAN_TABLE_BITS)) { 395 407 uint32_t val = BrotliGet16BitsUnmasked(br); 396 408 const HuffmanCode* ext = table + (val & HUFFMAN_TABLE_MASK) + *value; … … 420 432 The list of symbols MUST NOT contain duplicates. 421 433 */ 422 static Brotli ErrorCode ReadSimpleHuffmanSymbols(uint32_t alphabet_size,423 BrotliState* s) {434 static BrotliDecoderErrorCode ReadSimpleHuffmanSymbols( 435 uint32_t alphabet_size, BrotliDecoderState* s) { 424 436 /* max_bits == 1..10; symbol == 0..3; 1..40 bits will be read. */ 425 437 BrotliBitReader* br = &s->br; … … 429 441 while (i <= num_symbols) { 430 442 uint32_t v; 431 if ( PREDICT_FALSE(!BrotliSafeReadBits(br, max_bits, &v))) {443 if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, max_bits, &v))) { 432 444 s->sub_loop_counter = i; 433 445 s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_READ; 434 return BROTLI_ NEEDS_MORE_INPUT;446 return BROTLI_DECODER_NEEDS_MORE_INPUT; 435 447 } 436 448 if (v >= alphabet_size) { 437 return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET); 449 return 450 BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET); 438 451 } 439 452 s->symbols_lists_array[i] = (uint16_t)v; … … 446 459 for (; k <= num_symbols; ++k) { 447 460 if (s->symbols_lists_array[i] == s->symbols_lists_array[k]) { 448 return BROTLI_FAILURE(BROTLI_ ERROR_FORMAT_SIMPLE_HUFFMAN_SAME);449 } 450 } 451 } 452 453 return BROTLI_ SUCCESS;461 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME); 462 } 463 } 464 } 465 466 return BROTLI_DECODER_SUCCESS; 454 467 } 455 468 … … 457 470 A) reset the repeat variable 458 471 B) remember code length (if it is not 0) 459 C) extend corre dponding index-chain460 D) reduce the huffman space472 C) extend corresponding index-chain 473 D) reduce the Huffman space 461 474 E) update the histogram 462 475 */ … … 479 492 /* Process repeated symbol code length. 480 493 A) Check if it is the extension of previous repeat sequence; if the decoded 481 value is not kCodeLengthRepeatCode, then it is a new symbol-skip 494 value is not BROTLI_REPEAT_PREVIOUS_CODE_LENGTH, then it is a new 495 symbol-skip 482 496 B) Update repeat variable 483 C) Check if operation is feasible (fits alpha pet)497 C) Check if operation is feasible (fits alphabet) 484 498 D) For each symbol do the same operations as in ProcessSingleCodeLength 485 499 486 PRECONDITION: code_len == kCodeLengthRepeatCode or kCodeLengthRepeatCode + 1 500 PRECONDITION: code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH or 501 code_len == BROTLI_REPEAT_ZERO_CODE_LENGTH 487 502 */ 488 503 static BROTLI_INLINE void ProcessRepeatedCodeLength(uint32_t code_len, … … 492 507 uint16_t* code_length_histo, int* next_symbol) { 493 508 uint32_t old_repeat; 494 uint32_t new_len = 0; 495 if (code_len == kCodeLengthRepeatCode) { 509 uint32_t extra_bits = 3; /* for BROTLI_REPEAT_ZERO_CODE_LENGTH */ 510 uint32_t new_len = 0; /* for BROTLI_REPEAT_ZERO_CODE_LENGTH */ 511 if (code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) { 496 512 new_len = *prev_code_len; 513 extra_bits = 2; 497 514 } 498 515 if (*repeat_code_len != new_len) { … … 503 520 if (*repeat > 0) { 504 521 *repeat -= 2; 505 *repeat <<= code_len - 14U;522 *repeat <<= extra_bits; 506 523 } 507 524 *repeat += repeat_delta + 3U; … … 532 549 533 550 /* Reads and decodes symbol codelengths. */ 534 static Brotli ErrorCode ReadSymbolCodeLengths(535 uint32_t alphabet_size, Brotli State* s) {551 static BrotliDecoderErrorCode ReadSymbolCodeLengths( 552 uint32_t alphabet_size, BrotliDecoderState* s) { 536 553 BrotliBitReader* br = &s->br; 537 554 uint32_t symbol = s->symbol; … … 544 561 int* next_symbol = s->next_symbol; 545 562 if (!BrotliWarmupBitReader(br)) { 546 return BROTLI_ NEEDS_MORE_INPUT;563 return BROTLI_DECODER_NEEDS_MORE_INPUT; 547 564 } 548 565 while (symbol < alphabet_size && space > 0) { … … 555 572 s->repeat_code_len = repeat_code_len; 556 573 s->space = space; 557 return BROTLI_ NEEDS_MORE_INPUT;574 return BROTLI_DECODER_NEEDS_MORE_INPUT; 558 575 } 559 576 BrotliFillBitWindow16(br); … … 562 579 BrotliDropBits(br, p->bits); /* Use 1..5 bits */ 563 580 code_len = p->value; /* code_len == 0..17 */ 564 if (code_len < kCodeLengthRepeatCode) {581 if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) { 565 582 ProcessSingleCodeLength(code_len, &symbol, &repeat, &space, 566 583 &prev_code_len, symbol_lists, code_length_histo, next_symbol); 567 584 } else { /* code_len == 16..17, extra_bits == 2..3 */ 585 uint32_t extra_bits = 586 (code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) ? 2 : 3; 568 587 uint32_t repeat_delta = 569 (uint32_t)BrotliGetBitsUnmasked(br) & BitMask( code_len - 14U);570 BrotliDropBits(br, code_len - 14U);588 (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(extra_bits); 589 BrotliDropBits(br, extra_bits); 571 590 ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size, 572 591 &symbol, &repeat, &space, &prev_code_len, &repeat_code_len, … … 575 594 } 576 595 s->space = space; 577 return BROTLI_ SUCCESS;578 } 579 580 static Brotli ErrorCode SafeReadSymbolCodeLengths(581 uint32_t alphabet_size, Brotli State* s) {596 return BROTLI_DECODER_SUCCESS; 597 } 598 599 static BrotliDecoderErrorCode SafeReadSymbolCodeLengths( 600 uint32_t alphabet_size, BrotliDecoderState* s) { 582 601 BrotliBitReader* br = &s->br; 602 BROTLI_BOOL get_byte = BROTLI_FALSE; 583 603 while (s->symbol < alphabet_size && s->space > 0) { 584 604 const HuffmanCode* p = s->table; 585 605 uint32_t code_len; 606 uint32_t available_bits; 586 607 uint32_t bits = 0; 587 uint32_t available_bits = BrotliGetAvailableBits(br); 608 if (get_byte && !BrotliPullByte(br)) return BROTLI_DECODER_NEEDS_MORE_INPUT; 609 get_byte = BROTLI_FALSE; 610 available_bits = BrotliGetAvailableBits(br); 588 611 if (available_bits != 0) { 589 612 bits = (uint32_t)BrotliGetBitsUnmasked(br); 590 613 } 591 614 p += bits & BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH); 592 if (p->bits > available_bits) goto pullMoreInput; 615 if (p->bits > available_bits) { 616 get_byte = BROTLI_TRUE; 617 continue; 618 } 593 619 code_len = p->value; /* code_len == 0..17 */ 594 if (code_len < kCodeLengthRepeatCode) {620 if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) { 595 621 BrotliDropBits(br, p->bits); 596 622 ProcessSingleCodeLength(code_len, &s->symbol, &s->repeat, &s->space, … … 600 626 uint32_t extra_bits = code_len - 14U; 601 627 uint32_t repeat_delta = (bits >> p->bits) & BitMask(extra_bits); 602 if (available_bits < p->bits + extra_bits) goto pullMoreInput; 628 if (available_bits < p->bits + extra_bits) { 629 get_byte = BROTLI_TRUE; 630 continue; 631 } 603 632 BrotliDropBits(br, p->bits + extra_bits); 604 633 ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size, … … 607 636 s->next_symbol); 608 637 } 609 continue; 610 611 pullMoreInput: 612 if (!BrotliPullByte(br)) { 613 return BROTLI_NEEDS_MORE_INPUT; 614 } 615 } 616 return BROTLI_SUCCESS; 638 } 639 return BROTLI_DECODER_SUCCESS; 617 640 } 618 641 619 642 /* Reads and decodes 15..18 codes using static prefix code. 620 643 Each code is 2..4 bits long. In total 30..72 bits are used. */ 621 static Brotli ErrorCode ReadCodeLengthCodeLengths(BrotliState* s) {644 static BrotliDecoderErrorCode ReadCodeLengthCodeLengths(BrotliDecoderState* s) { 622 645 BrotliBitReader* br = &s->br; 623 646 uint32_t num_codes = s->repeat; 624 647 unsigned space = s->space; 625 648 uint32_t i = s->sub_loop_counter; 626 for (; i < CODE_LENGTH_CODES; ++i) {649 for (; i < BROTLI_CODE_LENGTH_CODES; ++i) { 627 650 const uint8_t code_len_idx = kCodeLengthCodeOrder[i]; 628 651 uint32_t ix; 629 652 uint32_t v; 630 if ( PREDICT_FALSE(!BrotliSafeGetBits(br, 4, &ix))) {653 if (BROTLI_PREDICT_FALSE(!BrotliSafeGetBits(br, 4, &ix))) { 631 654 uint32_t available_bits = BrotliGetAvailableBits(br); 632 655 if (available_bits != 0) { … … 640 663 s->space = space; 641 664 s->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX; 642 return BROTLI_ NEEDS_MORE_INPUT;665 return BROTLI_DECODER_NEEDS_MORE_INPUT; 643 666 } 644 667 } … … 658 681 } 659 682 if (!(num_codes == 1 || space == 0)) { 660 return BROTLI_FAILURE(BROTLI_ ERROR_FORMAT_CL_SPACE);661 } 662 return BROTLI_ SUCCESS;683 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_CL_SPACE); 684 } 685 return BROTLI_DECODER_SUCCESS; 663 686 } 664 687 … … 675 698 Huffman table. In worst case 3520 bits are read. 676 699 */ 677 static Brotli ErrorCode ReadHuffmanCode(uint32_t alphabet_size,678 HuffmanCode* table,679 uint32_t* opt_table_size,680 BrotliState* s) {700 static BrotliDecoderErrorCode ReadHuffmanCode(uint32_t alphabet_size, 701 HuffmanCode* table, 702 uint32_t* opt_table_size, 703 BrotliDecoderState* s) { 681 704 BrotliBitReader* br = &s->br; 682 705 /* Unnecessary masking, but might be good for safety. */ 683 706 alphabet_size &= 0x3ff; 684 707 /* State machine */ 685 switch (s->substate_huffman) { 686 case BROTLI_STATE_HUFFMAN_NONE: 687 if (!BrotliSafeReadBits(br, 2, &s->sub_loop_counter)) { 688 return BROTLI_NEEDS_MORE_INPUT; 689 } 690 BROTLI_LOG_UINT(s->sub_loop_counter); 691 /* The value is used as follows: 692 1 for simple code; 693 0 for no skipping, 2 skips 2 code lengths, 3 skips 3 code lengths */ 694 if (s->sub_loop_counter != 1) { 695 s->space = 32; 696 s->repeat = 0; /* num_codes */ 697 memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo[0]) * 698 (BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1)); 699 memset(&s->code_length_code_lengths[0], 0, 700 sizeof(s->code_length_code_lengths)); 701 s->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX; 702 goto Complex; 703 } 704 /* No break, transit to the next state. */ 705 706 case BROTLI_STATE_HUFFMAN_SIMPLE_SIZE: 707 /* Read symbols, codes & code lengths directly. */ 708 if (!BrotliSafeReadBits(br, 2, &s->symbol)) { /* num_symbols */ 709 s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_SIZE; 710 return BROTLI_NEEDS_MORE_INPUT; 711 } 712 s->sub_loop_counter = 0; 713 /* No break, transit to the next state. */ 714 case BROTLI_STATE_HUFFMAN_SIMPLE_READ: { 715 BrotliErrorCode result = ReadSimpleHuffmanSymbols(alphabet_size, s); 716 if (result != BROTLI_SUCCESS) { 717 return result; 718 } 719 /* No break, transit to the next state. */ 720 } 721 case BROTLI_STATE_HUFFMAN_SIMPLE_BUILD: { 722 uint32_t table_size; 723 if (s->symbol == 3) { 724 uint32_t bits; 725 if (!BrotliSafeReadBits(br, 1, &bits)) { 726 s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_BUILD; 727 return BROTLI_NEEDS_MORE_INPUT; 728 } 729 s->symbol += bits; 730 } 731 BROTLI_LOG_UINT(s->symbol); 732 table_size = BrotliBuildSimpleHuffmanTable( 733 table, HUFFMAN_TABLE_BITS, s->symbols_lists_array, s->symbol); 734 if (opt_table_size) { 735 *opt_table_size = table_size; 736 } 737 s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE; 738 return BROTLI_SUCCESS; 739 } 740 741 Complex: /* Decode Huffman-coded code lengths. */ 742 case BROTLI_STATE_HUFFMAN_COMPLEX: { 743 uint32_t i; 744 BrotliErrorCode result = ReadCodeLengthCodeLengths(s); 745 if (result != BROTLI_SUCCESS) { 746 return result; 747 } 748 BrotliBuildCodeLengthsHuffmanTable(s->table, 749 s->code_length_code_lengths, 750 s->code_length_histo); 751 memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo)); 752 for (i = 0; i <= BROTLI_HUFFMAN_MAX_CODE_LENGTH; ++i) { 753 s->next_symbol[i] = (int)i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1); 754 s->symbol_lists[(int)i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1)] = 0xFFFF; 755 } 756 757 s->symbol = 0; 758 s->prev_code_len = kDefaultCodeLength; 759 s->repeat = 0; 760 s->repeat_code_len = 0; 761 s->space = 32768; 762 s->substate_huffman = BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS; 763 /* No break, transit to the next state. */ 764 } 765 case BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS: { 766 uint32_t table_size; 767 BrotliErrorCode result = ReadSymbolCodeLengths(alphabet_size, s); 768 if (result == BROTLI_NEEDS_MORE_INPUT) { 769 result = SafeReadSymbolCodeLengths(alphabet_size, s); 770 } 771 if (result != BROTLI_SUCCESS) { 772 return result; 773 } 774 775 if (s->space != 0) { 776 BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", s->space)); 777 return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_HUFFMAN_SPACE); 778 } 779 table_size = BrotliBuildHuffmanTable( 780 table, HUFFMAN_TABLE_BITS, s->symbol_lists, s->code_length_histo); 781 if (opt_table_size) { 782 *opt_table_size = table_size; 783 } 784 s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE; 785 return BROTLI_SUCCESS; 786 } 787 788 default: 789 return BROTLI_FAILURE(BROTLI_ERROR_UNREACHABLE); 708 for (;;) { 709 switch (s->substate_huffman) { 710 case BROTLI_STATE_HUFFMAN_NONE: 711 if (!BrotliSafeReadBits(br, 2, &s->sub_loop_counter)) { 712 return BROTLI_DECODER_NEEDS_MORE_INPUT; 713 } 714 BROTLI_LOG_UINT(s->sub_loop_counter); 715 /* The value is used as follows: 716 1 for simple code; 717 0 for no skipping, 2 skips 2 code lengths, 3 skips 3 code lengths */ 718 if (s->sub_loop_counter != 1) { 719 s->space = 32; 720 s->repeat = 0; /* num_codes */ 721 memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo[0]) * 722 (BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1)); 723 memset(&s->code_length_code_lengths[0], 0, 724 sizeof(s->code_length_code_lengths)); 725 s->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX; 726 continue; 727 } 728 /* No break, transit to the next state. */ 729 730 case BROTLI_STATE_HUFFMAN_SIMPLE_SIZE: 731 /* Read symbols, codes & code lengths directly. */ 732 if (!BrotliSafeReadBits(br, 2, &s->symbol)) { /* num_symbols */ 733 s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_SIZE; 734 return BROTLI_DECODER_NEEDS_MORE_INPUT; 735 } 736 s->sub_loop_counter = 0; 737 /* No break, transit to the next state. */ 738 case BROTLI_STATE_HUFFMAN_SIMPLE_READ: { 739 BrotliDecoderErrorCode result = 740 ReadSimpleHuffmanSymbols(alphabet_size, s); 741 if (result != BROTLI_DECODER_SUCCESS) { 742 return result; 743 } 744 /* No break, transit to the next state. */ 745 } 746 case BROTLI_STATE_HUFFMAN_SIMPLE_BUILD: { 747 uint32_t table_size; 748 if (s->symbol == 3) { 749 uint32_t bits; 750 if (!BrotliSafeReadBits(br, 1, &bits)) { 751 s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_BUILD; 752 return BROTLI_DECODER_NEEDS_MORE_INPUT; 753 } 754 s->symbol += bits; 755 } 756 BROTLI_LOG_UINT(s->symbol); 757 table_size = BrotliBuildSimpleHuffmanTable( 758 table, HUFFMAN_TABLE_BITS, s->symbols_lists_array, s->symbol); 759 if (opt_table_size) { 760 *opt_table_size = table_size; 761 } 762 s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE; 763 return BROTLI_DECODER_SUCCESS; 764 } 765 766 /* Decode Huffman-coded code lengths. */ 767 case BROTLI_STATE_HUFFMAN_COMPLEX: { 768 uint32_t i; 769 BrotliDecoderErrorCode result = ReadCodeLengthCodeLengths(s); 770 if (result != BROTLI_DECODER_SUCCESS) { 771 return result; 772 } 773 BrotliBuildCodeLengthsHuffmanTable(s->table, 774 s->code_length_code_lengths, 775 s->code_length_histo); 776 memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo)); 777 for (i = 0; i <= BROTLI_HUFFMAN_MAX_CODE_LENGTH; ++i) { 778 s->next_symbol[i] = (int)i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1); 779 s->symbol_lists[s->next_symbol[i]] = 0xFFFF; 780 } 781 782 s->symbol = 0; 783 s->prev_code_len = BROTLI_INITIAL_REPEATED_CODE_LENGTH; 784 s->repeat = 0; 785 s->repeat_code_len = 0; 786 s->space = 32768; 787 s->substate_huffman = BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS; 788 /* No break, transit to the next state. */ 789 } 790 case BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS: { 791 uint32_t table_size; 792 BrotliDecoderErrorCode result = ReadSymbolCodeLengths(alphabet_size, s); 793 if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) { 794 result = SafeReadSymbolCodeLengths(alphabet_size, s); 795 } 796 if (result != BROTLI_DECODER_SUCCESS) { 797 return result; 798 } 799 800 if (s->space != 0) { 801 BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", s->space)); 802 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE); 803 } 804 table_size = BrotliBuildHuffmanTable( 805 table, HUFFMAN_TABLE_BITS, s->symbol_lists, s->code_length_histo); 806 if (opt_table_size) { 807 *opt_table_size = table_size; 808 } 809 s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE; 810 return BROTLI_DECODER_SUCCESS; 811 } 812 813 default: 814 return 815 BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); 816 } 790 817 } 791 818 } … … 803 830 /* WARNING: if state is not BROTLI_STATE_READ_BLOCK_LENGTH_NONE, then 804 831 reading can't be continued with ReadBlockLength. */ 805 static BROTLI_INLINE int SafeReadBlockLength(BrotliState* s, 806 uint32_t* result, 807 const HuffmanCode* table, 808 BrotliBitReader* br) { 832 static BROTLI_INLINE BROTLI_BOOL SafeReadBlockLength( 833 BrotliDecoderState* s, uint32_t* result, const HuffmanCode* table, 834 BrotliBitReader* br) { 809 835 uint32_t index; 810 836 if (s->substate_read_block_length == BROTLI_STATE_READ_BLOCK_LENGTH_NONE) { 811 837 if (!SafeReadSymbol(table, br, &index)) { 812 return 0;838 return BROTLI_FALSE; 813 839 } 814 840 } else { … … 821 847 s->block_length_index = index; 822 848 s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX; 823 return 0;849 return BROTLI_FALSE; 824 850 } 825 851 *result = kBlockLengthPrefixCode[index].offset + bits; 826 852 s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE; 827 return 1;853 return BROTLI_TRUE; 828 854 } 829 855 } … … 844 870 inner for loop with do-while. 845 871 */ 846 static BROTLI_NOINLINE void InverseMoveToFrontTransform( uint8_t* v,847 uint 32_t v_len, BrotliState* state) {872 static BROTLI_NOINLINE void InverseMoveToFrontTransform( 873 uint8_t* v, uint32_t v_len, BrotliDecoderState* state) { 848 874 /* Reinitialize elements that could have been changed. */ 849 uint32_t i = 4;875 uint32_t i = 1; 850 876 uint32_t upper_bound = state->mtf_upper_bound; 851 uint8_t* mtf = &state->mtf[4]; /* Make mtf[-1] addressable. */ 877 uint32_t* mtf = &state->mtf[1]; /* Make mtf[-1] addressable. */ 878 uint8_t* mtf_u8 = (uint8_t*)mtf; 852 879 /* Load endian-aware constant. */ 853 880 const uint8_t b0123[4] = {0, 1, 2, 3}; … … 856 883 857 884 /* Initialize list using 4 consequent values pattern. */ 858 *(uint32_t*)mtf= pattern;885 mtf[0] = pattern; 859 886 do { 860 887 pattern += 0x04040404; /* Advance all 4 values by 4. */ 861 *(uint32_t*)(mtf + i)= pattern;862 i += 4;888 mtf[i] = pattern; 889 i++; 863 890 } while (i <= upper_bound); 864 891 … … 867 894 for (i = 0; i < v_len; ++i) { 868 895 int index = v[i]; 869 uint8_t value = mtf [index];896 uint8_t value = mtf_u8[index]; 870 897 upper_bound |= v[i]; 871 898 v[i] = value; 872 mtf [-1] = value;899 mtf_u8[-1] = value; 873 900 do { 874 901 index--; 875 mtf [index + 1] = mtf[index];902 mtf_u8[index + 1] = mtf_u8[index]; 876 903 } while (index >= 0); 877 904 } 878 905 /* Remember amount of elements to be reinitialized. */ 879 state->mtf_upper_bound = upper_bound ;906 state->mtf_upper_bound = upper_bound >> 2; 880 907 } 881 908 882 909 /* Decodes a series of Huffman table using ReadHuffmanCode function. */ 883 static Brotli ErrorCode HuffmanTreeGroupDecode(HuffmanTreeGroup* group,884 BrotliState* s) {910 static BrotliDecoderErrorCode HuffmanTreeGroupDecode( 911 HuffmanTreeGroup* group, BrotliDecoderState* s) { 885 912 if (s->substate_tree_group != BROTLI_STATE_TREE_GROUP_LOOP) { 886 913 s->next = group->codes; … … 890 917 while (s->htree_index < group->num_htrees) { 891 918 uint32_t table_size; 892 Brotli ErrorCode result =919 BrotliDecoderErrorCode result = 893 920 ReadHuffmanCode(group->alphabet_size, s->next, &table_size, s); 894 if (result != BROTLI_ SUCCESS) return result;921 if (result != BROTLI_DECODER_SUCCESS) return result; 895 922 group->htrees[s->htree_index] = s->next; 896 923 s->next += table_size; … … 898 925 } 899 926 s->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE; 900 return BROTLI_ SUCCESS;927 return BROTLI_DECODER_SUCCESS; 901 928 } 902 929 … … 910 937 4) Optionally, apply InverseMoveToFront transform to the resulting map. 911 938 */ 912 static Brotli ErrorCode DecodeContextMap(uint32_t context_map_size,913 uint32_t* num_htrees,914 uint8_t** context_map_arg,915 BrotliState* s) {939 static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size, 940 uint32_t* num_htrees, 941 uint8_t** context_map_arg, 942 BrotliDecoderState* s) { 916 943 BrotliBitReader* br = &s->br; 917 Brotli ErrorCode result = BROTLI_SUCCESS;944 BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS; 918 945 919 946 switch ((int)s->substate_context_map) { 920 947 case BROTLI_STATE_CONTEXT_MAP_NONE: 921 948 result = DecodeVarLenUint8(s, br, num_htrees); 922 if (result != BROTLI_ SUCCESS) {949 if (result != BROTLI_DECODER_SUCCESS) { 923 950 return result; 924 951 } … … 929 956 *context_map_arg = (uint8_t*)BROTLI_ALLOC(s, (size_t)context_map_size); 930 957 if (*context_map_arg == 0) { 931 return BROTLI_FAILURE(BROTLI_ ERROR_ALLOC_CONTEXT_MAP);958 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP); 932 959 } 933 960 if (*num_htrees <= 1) { 934 961 memset(*context_map_arg, 0, (size_t)context_map_size); 935 return BROTLI_ SUCCESS;962 return BROTLI_DECODER_SUCCESS; 936 963 } 937 964 s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_READ_PREFIX; … … 942 969 to peek 4 bits ahead. */ 943 970 if (!BrotliSafeGetBits(br, 5, &bits)) { 944 return BROTLI_ NEEDS_MORE_INPUT;945 } 946 if ((bits & 1) != 0) { /* Use RLE for zero es. */971 return BROTLI_DECODER_NEEDS_MORE_INPUT; 972 } 973 if ((bits & 1) != 0) { /* Use RLE for zeros. */ 947 974 s->max_run_length_prefix = (bits >> 1) + 1; 948 975 BrotliDropBits(br, 5); … … 958 985 result = ReadHuffmanCode(*num_htrees + s->max_run_length_prefix, 959 986 s->context_map_table, NULL, s); 960 if (result != BROTLI_ SUCCESS) return result;987 if (result != BROTLI_DECODER_SUCCESS) return result; 961 988 s->code = 0xFFFF; 962 989 s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_DECODE; … … 967 994 uint8_t* context_map = *context_map_arg; 968 995 uint32_t code = s->code; 969 if (code != 0xFFFF) { 970 goto rleCode; 971 } 972 while (context_index < context_map_size) { 973 if (!SafeReadSymbol(s->context_map_table, br, &code)) { 974 s->code = 0xFFFF; 975 s->context_index = context_index; 976 return BROTLI_NEEDS_MORE_INPUT; 977 } 978 BROTLI_LOG_UINT(code); 979 980 if (code == 0) { 981 context_map[context_index++] = 0; 982 continue; 983 } 984 if (code > max_run_length_prefix) { 985 context_map[context_index++] = 986 (uint8_t)(code - max_run_length_prefix); 987 continue; 988 } 989 rleCode: 996 BROTLI_BOOL skip_preamble = (code != 0xFFFF); 997 while (context_index < context_map_size || skip_preamble) { 998 if (!skip_preamble) { 999 if (!SafeReadSymbol(s->context_map_table, br, &code)) { 1000 s->code = 0xFFFF; 1001 s->context_index = context_index; 1002 return BROTLI_DECODER_NEEDS_MORE_INPUT; 1003 } 1004 BROTLI_LOG_UINT(code); 1005 1006 if (code == 0) { 1007 context_map[context_index++] = 0; 1008 continue; 1009 } 1010 if (code > max_run_length_prefix) { 1011 context_map[context_index++] = 1012 (uint8_t)(code - max_run_length_prefix); 1013 continue; 1014 } 1015 } else { 1016 skip_preamble = BROTLI_FALSE; 1017 } 1018 /* RLE sub-stage. */ 990 1019 { 991 1020 uint32_t reps; … … 993 1022 s->code = code; 994 1023 s->context_index = context_index; 995 return BROTLI_ NEEDS_MORE_INPUT;1024 return BROTLI_DECODER_NEEDS_MORE_INPUT; 996 1025 } 997 1026 reps += 1U << code; 998 1027 BROTLI_LOG_UINT(reps); 999 1028 if (context_index + reps > context_map_size) { 1000 return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_CONTEXT_MAP_REPEAT); 1029 return 1030 BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT); 1001 1031 } 1002 1032 do { … … 1011 1041 if (!BrotliSafeReadBits(br, 1, &bits)) { 1012 1042 s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_TRANSFORM; 1013 return BROTLI_ NEEDS_MORE_INPUT;1043 return BROTLI_DECODER_NEEDS_MORE_INPUT; 1014 1044 } 1015 1045 if (bits != 0) { … … 1017 1047 } 1018 1048 s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE; 1019 return BROTLI_ SUCCESS;1049 return BROTLI_DECODER_SUCCESS; 1020 1050 } 1021 1051 default: 1022 return BROTLI_FAILURE(BROTLI_ERROR_UNREACHABLE); 1023 } 1024 } 1025 1026 /* Decodes a command or literal and updates block type ringbuffer. 1052 return 1053 BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); 1054 } 1055 } 1056 1057 /* Decodes a command or literal and updates block type ring-buffer. 1027 1058 Reads 3..54 bits. */ 1028 static BROTLI_INLINE int DecodeBlockTypeAndLength(int safe,1029 BrotliState* s, int tree_type) {1059 static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength( 1060 int safe, BrotliDecoderState* s, int tree_type) { 1030 1061 uint32_t max_block_type = s->num_block_types[tree_type]; 1031 1062 const HuffmanCode* type_tree = &s->block_type_trees[ … … 1044 1075 BrotliBitReaderState memento; 1045 1076 BrotliBitReaderSaveState(br, &memento); 1046 if (!SafeReadSymbol(type_tree, br, &block_type)) return 0;1077 if (!SafeReadSymbol(type_tree, br, &block_type)) return BROTLI_FALSE; 1047 1078 if (!SafeReadBlockLength(s, &s->block_length[tree_type], len_tree, br)) { 1048 1079 s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE; 1049 1080 BrotliBitReaderRestoreState(br, &memento); 1050 return 0;1081 return BROTLI_FALSE; 1051 1082 } 1052 1083 } … … 1064 1095 ringbuffer[0] = ringbuffer[1]; 1065 1096 ringbuffer[1] = block_type; 1066 return 1; 1067 } 1068 1069 static BROTLI_INLINE void DetectTrivialLiteralBlockTypes(BrotliState* s) { 1097 return BROTLI_TRUE; 1098 } 1099 1100 static BROTLI_INLINE void DetectTrivialLiteralBlockTypes( 1101 BrotliDecoderState* s) { 1070 1102 size_t i; 1071 1103 for (i = 0; i < 8; ++i) s->trivial_literal_contexts[i] = 0; 1072 1104 for (i = 0; i < s->num_block_types[0]; i++) { 1073 size_t offset = i << kLiteralContextBits;1105 size_t offset = i << BROTLI_LITERAL_CONTEXT_BITS; 1074 1106 size_t error = 0; 1075 1107 size_t sample = s->context_map[offset]; 1076 1108 size_t j; 1077 for (j = 0; j < (1u << kLiteralContextBits);) {1109 for (j = 0; j < (1u << BROTLI_LITERAL_CONTEXT_BITS);) { 1078 1110 BROTLI_REPEAT(4, error |= s->context_map[offset + j++] ^ sample;) 1079 1111 } … … 1084 1116 } 1085 1117 1086 static BROTLI_INLINE void PrepareLiteralDecoding(Brotli State* s) {1118 static BROTLI_INLINE void PrepareLiteralDecoding(BrotliDecoderState* s) { 1087 1119 uint8_t context_mode; 1088 1120 size_t trivial; 1089 1121 uint32_t block_type = s->block_type_rb[1]; 1090 uint32_t context_offset = block_type << kLiteralContextBits;1122 uint32_t context_offset = block_type << BROTLI_LITERAL_CONTEXT_BITS; 1091 1123 s->context_map_slice = s->context_map + context_offset; 1092 1124 trivial = s->trivial_literal_contexts[block_type >> 5]; … … 1100 1132 /* Decodes the block type and updates the state for literal context. 1101 1133 Reads 3..54 bits. */ 1102 static BROTLI_INLINE int DecodeLiteralBlockSwitchInternal(int safe,1103 BrotliState* s) {1134 static BROTLI_INLINE BROTLI_BOOL DecodeLiteralBlockSwitchInternal( 1135 int safe, BrotliDecoderState* s) { 1104 1136 if (!DecodeBlockTypeAndLength(safe, s, 0)) { 1105 return 0;1137 return BROTLI_FALSE; 1106 1138 } 1107 1139 PrepareLiteralDecoding(s); 1108 return 1;1109 } 1110 1111 static void BROTLI_NOINLINE DecodeLiteralBlockSwitch(Brotli State* s) {1140 return BROTLI_TRUE; 1141 } 1142 1143 static void BROTLI_NOINLINE DecodeLiteralBlockSwitch(BrotliDecoderState* s) { 1112 1144 DecodeLiteralBlockSwitchInternal(0, s); 1113 1145 } 1114 1146 1115 static int BROTLI_NOINLINE SafeDecodeLiteralBlockSwitch(BrotliState* s) { 1147 static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeLiteralBlockSwitch( 1148 BrotliDecoderState* s) { 1116 1149 return DecodeLiteralBlockSwitchInternal(1, s); 1117 1150 } … … 1119 1152 /* Block switch for insert/copy length. 1120 1153 Reads 3..54 bits. */ 1121 static BROTLI_INLINE int DecodeCommandBlockSwitchInternal(int safe,1122 BrotliState* s) {1154 static BROTLI_INLINE BROTLI_BOOL DecodeCommandBlockSwitchInternal( 1155 int safe, BrotliDecoderState* s) { 1123 1156 if (!DecodeBlockTypeAndLength(safe, s, 1)) { 1124 return 0;1157 return BROTLI_FALSE; 1125 1158 } 1126 1159 s->htree_command = s->insert_copy_hgroup.htrees[s->block_type_rb[3]]; 1127 return 1;1128 } 1129 1130 static void BROTLI_NOINLINE DecodeCommandBlockSwitch(Brotli State* s) {1160 return BROTLI_TRUE; 1161 } 1162 1163 static void BROTLI_NOINLINE DecodeCommandBlockSwitch(BrotliDecoderState* s) { 1131 1164 DecodeCommandBlockSwitchInternal(0, s); 1132 1165 } 1133 static int BROTLI_NOINLINE SafeDecodeCommandBlockSwitch(BrotliState* s) { 1166 static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeCommandBlockSwitch( 1167 BrotliDecoderState* s) { 1134 1168 return DecodeCommandBlockSwitchInternal(1, s); 1135 1169 } … … 1137 1171 /* Block switch for distance codes. 1138 1172 Reads 3..54 bits. */ 1139 static BROTLI_INLINE int DecodeDistanceBlockSwitchInternal(int safe,1140 BrotliState* s) {1173 static BROTLI_INLINE BROTLI_BOOL DecodeDistanceBlockSwitchInternal( 1174 int safe, BrotliDecoderState* s) { 1141 1175 if (!DecodeBlockTypeAndLength(safe, s, 2)) { 1142 return 0;1143 } 1144 s->dist_context_map_slice = 1145 s->dist_context_map + (s->block_type_rb[5] << kDistanceContextBits);1176 return BROTLI_FALSE; 1177 } 1178 s->dist_context_map_slice = s->dist_context_map + 1179 (s->block_type_rb[5] << BROTLI_DISTANCE_CONTEXT_BITS); 1146 1180 s->dist_htree_index = s->dist_context_map_slice[s->distance_context]; 1147 return 1;1148 } 1149 1150 static void BROTLI_NOINLINE DecodeDistanceBlockSwitch(Brotli State* s) {1181 return BROTLI_TRUE; 1182 } 1183 1184 static void BROTLI_NOINLINE DecodeDistanceBlockSwitch(BrotliDecoderState* s) { 1151 1185 DecodeDistanceBlockSwitchInternal(0, s); 1152 1186 } 1153 1187 1154 static int BROTLI_NOINLINE SafeDecodeDistanceBlockSwitch(BrotliState* s) { 1188 static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeDistanceBlockSwitch( 1189 BrotliDecoderState* s) { 1155 1190 return DecodeDistanceBlockSwitchInternal(1, s); 1156 1191 } 1157 1192 1158 static BrotliErrorCode BROTLI_NOINLINE WriteRingBuffer(size_t* available_out, 1159 uint8_t** next_out, size_t* total_out, BrotliState* s) { 1160 size_t pos = (s->pos > s->ringbuffer_size) ? (size_t)s->ringbuffer_size 1161 : (size_t)(s->pos); 1193 static size_t UnwrittenBytes(const BrotliDecoderState* s, BROTLI_BOOL wrap) { 1194 size_t pos = wrap && s->pos > s->ringbuffer_size ? 1195 (size_t)s->ringbuffer_size : (size_t)(s->pos); 1196 size_t partial_pos_rb = (s->rb_roundtrips * (size_t)s->ringbuffer_size) + pos; 1197 return partial_pos_rb - s->partial_pos_out; 1198 } 1199 1200 /* Dumps output. 1201 Returns BROTLI_DECODER_NEEDS_MORE_OUTPUT only if there is more output to push 1202 and either ring-buffer is as big as window size, or |force| is true. 1203 */ 1204 static BrotliDecoderErrorCode BROTLI_NOINLINE WriteRingBuffer( 1205 BrotliDecoderState* s, size_t* available_out, uint8_t** next_out, 1206 size_t* total_out, BROTLI_BOOL force) { 1162 1207 uint8_t* start = 1163 1208 s->ringbuffer + (s->partial_pos_out & (size_t)s->ringbuffer_mask); 1164 size_t partial_pos_rb = (s->rb_roundtrips * (size_t)s->ringbuffer_size) + pos; 1165 size_t to_write = (partial_pos_rb - s->partial_pos_out); 1209 size_t to_write = UnwrittenBytes(s, BROTLI_TRUE); 1166 1210 size_t num_written = *available_out; 1167 1211 if (num_written > to_write) { … … 1169 1213 } 1170 1214 if (s->meta_block_remaining_len < 0) { 1171 return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_BLOCK_LENGTH_1); 1172 } 1173 memcpy(*next_out, start, num_written); 1174 *next_out += num_written; 1215 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1); 1216 } 1217 if (next_out && !*next_out) { 1218 *next_out = start; 1219 } else { 1220 if (next_out) { 1221 memcpy(*next_out, start, num_written); 1222 *next_out += num_written; 1223 } 1224 } 1175 1225 *available_out -= num_written; 1176 1226 BROTLI_LOG_UINT(to_write); 1177 1227 BROTLI_LOG_UINT(num_written); 1178 1228 s->partial_pos_out += num_written; 1179 if (total_out) *total_out = s->partial_pos_out; 1229 if (total_out) { 1230 *total_out = s->partial_pos_out; 1231 } 1180 1232 if (num_written < to_write) { 1181 return BROTLI_NEEDS_MORE_OUTPUT; 1182 } 1183 1184 if (s->pos >= s->ringbuffer_size) { 1233 if (s->ringbuffer_size == (1 << s->window_bits) || force) { 1234 return BROTLI_DECODER_NEEDS_MORE_OUTPUT; 1235 } else { 1236 return BROTLI_DECODER_SUCCESS; 1237 } 1238 } 1239 /* Wrap ring buffer only if it has reached its maximal size. */ 1240 if (s->ringbuffer_size == (1 << s->window_bits) && 1241 s->pos >= s->ringbuffer_size) { 1185 1242 s->pos -= s->ringbuffer_size; 1186 1243 s->rb_roundtrips++; 1187 } 1188 return BROTLI_SUCCESS; 1189 } 1190 1191 /* Allocates ringbuffer. 1192 1193 s->ringbuffer_size MUST be updated by BrotliCalculateRingBufferSize before 1194 this function is called. 1195 1196 Last two bytes of ringbuffer are initialized to 0, so context calculation 1244 s->should_wrap_ringbuffer = (size_t)s->pos != 0 ? 1 : 0; 1245 } 1246 return BROTLI_DECODER_SUCCESS; 1247 } 1248 1249 static void BROTLI_NOINLINE WrapRingBuffer(BrotliDecoderState* s) { 1250 if (s->should_wrap_ringbuffer) { 1251 memcpy(s->ringbuffer, s->ringbuffer_end, (size_t)s->pos); 1252 s->should_wrap_ringbuffer = 0; 1253 } 1254 } 1255 1256 /* Allocates ring-buffer. 1257 1258 s->ringbuffer_size MUST be updated by BrotliCalculateRingBufferSize before 1259 this function is called. 1260 1261 Last two bytes of ring-buffer are initialized to 0, so context calculation 1197 1262 could be done uniformly for the first two and all other positions. 1198 1199 Custom dictionary, if any, is copied to the end of ringbuffer.1200 1263 */ 1201 static int BROTLI_NOINLINE BrotliAllocateRingBuffer(BrotliState* s) { 1202 /* We need the slack region for the following reasons: 1203 - doing up to two 16-byte copies for fast backward copying 1204 - inserting transformed dictionary word (5 prefix + 24 base + 8 suffix) */ 1205 static const int kRingBufferWriteAheadSlack = 42; 1206 s->ringbuffer = (uint8_t*)BROTLI_ALLOC(s, (size_t)(s->ringbuffer_size + 1207 kRingBufferWriteAheadSlack)); 1264 static BROTLI_BOOL BROTLI_NOINLINE BrotliEnsureRingBuffer( 1265 BrotliDecoderState* s) { 1266 uint8_t* old_ringbuffer = s->ringbuffer; 1267 if (s->ringbuffer_size == s->new_ringbuffer_size) { 1268 return BROTLI_TRUE; 1269 } 1270 1271 s->ringbuffer = (uint8_t*)BROTLI_ALLOC(s, (size_t)(s->new_ringbuffer_size) + 1272 kRingBufferWriteAheadSlack); 1208 1273 if (s->ringbuffer == 0) { 1209 return 0; 1210 } 1211 1274 /* Restore previous value. */ 1275 s->ringbuffer = old_ringbuffer; 1276 return BROTLI_FALSE; 1277 } 1278 s->ringbuffer[s->new_ringbuffer_size - 2] = 0; 1279 s->ringbuffer[s->new_ringbuffer_size - 1] = 0; 1280 1281 if (!!old_ringbuffer) { 1282 memcpy(s->ringbuffer, old_ringbuffer, (size_t)s->pos); 1283 BROTLI_FREE(s, old_ringbuffer); 1284 } 1285 1286 s->ringbuffer_size = s->new_ringbuffer_size; 1287 s->ringbuffer_mask = s->new_ringbuffer_size - 1; 1212 1288 s->ringbuffer_end = s->ringbuffer + s->ringbuffer_size; 1213 1289 1214 s->ringbuffer[s->ringbuffer_size - 2] = 0; 1215 s->ringbuffer[s->ringbuffer_size - 1] = 0; 1216 1217 if (s->custom_dict) { 1218 memcpy(&s->ringbuffer[(-s->custom_dict_size) & s->ringbuffer_mask], 1219 s->custom_dict, (size_t)s->custom_dict_size); 1220 } 1221 1222 return 1; 1223 } 1224 1225 static BrotliErrorCode BROTLI_NOINLINE CopyUncompressedBlockToOutput( 1290 return BROTLI_TRUE; 1291 } 1292 1293 static BrotliDecoderErrorCode BROTLI_NOINLINE CopyUncompressedBlockToOutput( 1226 1294 size_t* available_out, uint8_t** next_out, size_t* total_out, 1227 Brotli State* s) {1295 BrotliDecoderState* s) { 1228 1296 /* TODO: avoid allocation for single uncompressed block. */ 1229 if (! s->ringbuffer && !BrotliAllocateRingBuffer(s)) {1230 return BROTLI_FAILURE(BROTLI_ ERROR_ALLOC_RING_BUFFER_1);1297 if (!BrotliEnsureRingBuffer(s)) { 1298 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1); 1231 1299 } 1232 1300 … … 1242 1310 nbytes = s->ringbuffer_size - s->pos; 1243 1311 } 1244 /* Copy remaining bytes from s->br.buf_ to ring buffer. */1312 /* Copy remaining bytes from s->br.buf_ to ring-buffer. */ 1245 1313 BrotliCopyBytes(&s->ringbuffer[s->pos], &s->br, (size_t)nbytes); 1246 1314 s->pos += nbytes; 1247 1315 s->meta_block_remaining_len -= nbytes; 1248 if (s->pos < s->ringbuffer_size) {1316 if (s->pos < 1 << s->window_bits) { 1249 1317 if (s->meta_block_remaining_len == 0) { 1250 return BROTLI_ SUCCESS;1318 return BROTLI_DECODER_SUCCESS; 1251 1319 } 1252 return BROTLI_ NEEDS_MORE_INPUT;1320 return BROTLI_DECODER_NEEDS_MORE_INPUT; 1253 1321 } 1254 1322 s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_WRITE; … … 1256 1324 } 1257 1325 case BROTLI_STATE_UNCOMPRESSED_WRITE: { 1258 BrotliErrorCode result = 1259 WriteRingBuffer(available_out, next_out, total_out, s); 1260 if (result != BROTLI_SUCCESS) { 1326 BrotliDecoderErrorCode result; 1327 result = WriteRingBuffer( 1328 s, available_out, next_out, total_out, BROTLI_FALSE); 1329 if (result != BROTLI_DECODER_SUCCESS) { 1261 1330 return result; 1262 1331 } 1263 s->max_distance = s->max_backward_distance; 1332 if (s->ringbuffer_size == 1 << s->window_bits) { 1333 s->max_distance = s->max_backward_distance; 1334 } 1264 1335 s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE; 1265 1336 break; … … 1270 1341 } 1271 1342 1272 int BrotliDecompressedSize(size_t encoded_size,1273 const uint8_t* encoded_buffer,1274 size_t* decoded_size) {1275 BrotliState s;1276 int next_block_header;1277 BrotliStateInit(&s);1278 s.br.next_in = encoded_buffer;1279 s.br.avail_in = encoded_size;1280 if (!BrotliWarmupBitReader(&s.br)) {1281 return 0;1282 }1283 DecodeWindowBits(&s.br);1284 if (DecodeMetaBlockLength(&s, &s.br) != BROTLI_SUCCESS) {1285 return 0;1286 }1287 *decoded_size = (size_t)s.meta_block_remaining_len;1288 if (s.is_last_metablock) {1289 return 1;1290 }1291 if (!s.is_uncompressed || !BrotliJumpToByteBoundary(&s.br)) {1292 return 0;1293 }1294 next_block_header = BrotliPeekByte(&s.br, (size_t)s.meta_block_remaining_len);1295 return (next_block_header != -1) && ((next_block_header & 3) == 3);1296 }1297 1298 1343 /* Calculates the smallest feasible ring buffer. 1299 1344 1300 If we know the data size is small, do not allocate more ring buffer1345 If we know the data size is small, do not allocate more ring buffer 1301 1346 size than needed to reduce memory usage. 1302 1347 1303 1348 When this method is called, metablock size and flags MUST be decoded. 1304 1349 */ 1305 static void BROTLI_NOINLINE BrotliCalculateRingBufferSize(BrotliState* s, 1306 BrotliBitReader* br) { 1307 int is_last = s->is_last_metablock; 1350 static void BROTLI_NOINLINE BrotliCalculateRingBufferSize( 1351 BrotliDecoderState* s) { 1308 1352 int window_size = 1 << s->window_bits; 1309 s->ringbuffer_size = window_size; 1310 1311 if (s->is_uncompressed) { 1312 int next_block_header = 1313 BrotliPeekByte(br, (size_t)s->meta_block_remaining_len); 1314 if (next_block_header != -1) { /* Peek succeeded */ 1315 if ((next_block_header & 3) == 3) { /* ISLAST and ISEMPTY */ 1316 is_last = 1; 1317 } 1318 } 1319 } 1320 1353 int new_ringbuffer_size = window_size; 1321 1354 /* We need at least 2 bytes of ring buffer size to get the last two 1322 1355 bytes for context from there */ 1323 if (is_last) { 1324 int min_size_x2 = (s->meta_block_remaining_len + s->custom_dict_size) * 2; 1325 while (s->ringbuffer_size >= min_size_x2 && s->ringbuffer_size > 32) { 1326 s->ringbuffer_size >>= 1; 1327 } 1328 } 1329 1330 s->ringbuffer_mask = s->ringbuffer_size - 1; 1356 int min_size = s->ringbuffer_size ? s->ringbuffer_size : 1024; 1357 int output_size; 1358 1359 /* If maximum is already reached, no further extension is retired. */ 1360 if (s->ringbuffer_size == window_size) { 1361 return; 1362 } 1363 1364 /* Metadata blocks does not touch ring buffer. */ 1365 if (s->is_metadata) { 1366 return; 1367 } 1368 1369 if (!s->ringbuffer) { 1370 output_size = 0; 1371 } else { 1372 output_size = s->pos; 1373 } 1374 output_size += s->meta_block_remaining_len; 1375 min_size = min_size < output_size ? output_size : min_size; 1376 1377 if (!!s->canny_ringbuffer_allocation) { 1378 /* Reduce ring buffer size to save memory when server is unscrupulous. 1379 In worst case memory usage might be 1.5x bigger for a short period of 1380 ring buffer reallocation.*/ 1381 while ((new_ringbuffer_size >> 1) >= min_size) { 1382 new_ringbuffer_size >>= 1; 1383 } 1384 } 1385 1386 s->new_ringbuffer_size = new_ringbuffer_size; 1331 1387 } 1332 1388 1333 1389 /* Reads 1..256 2-bit context modes. */ 1334 static Brotli ErrorCode ReadContextModes(BrotliState* s) {1390 static BrotliDecoderErrorCode ReadContextModes(BrotliDecoderState* s) { 1335 1391 BrotliBitReader* br = &s->br; 1336 1392 int i = s->loop_counter; … … 1340 1396 if (!BrotliSafeReadBits(br, 2, &bits)) { 1341 1397 s->loop_counter = i; 1342 return BROTLI_ NEEDS_MORE_INPUT;1398 return BROTLI_DECODER_NEEDS_MORE_INPUT; 1343 1399 } 1344 1400 s->context_modes[i] = (uint8_t)(bits << 1); … … 1346 1402 i++; 1347 1403 } 1348 return BROTLI_ SUCCESS;1349 } 1350 1351 static BROTLI_INLINE void TakeDistanceFromRingBuffer(Brotli State* s) {1404 return BROTLI_DECODER_SUCCESS; 1405 } 1406 1407 static BROTLI_INLINE void TakeDistanceFromRingBuffer(BrotliDecoderState* s) { 1352 1408 if (s->distance_code == 0) { 1353 1409 --s->dist_rb_idx; 1354 1410 s->distance_code = s->dist_rb[s->dist_rb_idx & 3]; 1411 /* Compensate double distance-ring-buffer roll for dictionary items. */ 1412 s->distance_context = 1; 1355 1413 } else { 1356 1414 int distance_code = s->distance_code << 1; … … 1378 1436 } 1379 1437 1380 static BROTLI_INLINE intSafeReadBits(1438 static BROTLI_INLINE BROTLI_BOOL SafeReadBits( 1381 1439 BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { 1382 1440 if (n_bits != 0) { … … 1384 1442 } else { 1385 1443 *val = 0; 1386 return 1;1444 return BROTLI_TRUE; 1387 1445 } 1388 1446 } 1389 1447 1390 1448 /* Precondition: s->distance_code < 0 */ 1391 static BROTLI_INLINE int ReadDistanceInternal(int safe,1392 BrotliState* s, BrotliBitReader* br) {1449 static BROTLI_INLINE BROTLI_BOOL ReadDistanceInternal( 1450 int safe, BrotliDecoderState* s, BrotliBitReader* br) { 1393 1451 int distval; 1394 1452 BrotliBitReaderState memento; … … 1400 1458 BrotliBitReaderSaveState(br, &memento); 1401 1459 if (!SafeReadSymbol(distance_tree, br, &code)) { 1402 return 0;1460 return BROTLI_FALSE; 1403 1461 } 1404 1462 s->distance_code = (int)code; … … 1406 1464 /* Convert the distance code to the actual distance by possibly */ 1407 1465 /* looking up past distances from the s->ringbuffer. */ 1466 s->distance_context = 0; 1408 1467 if ((s->distance_code & ~0xf) == 0) { 1409 1468 TakeDistanceFromRingBuffer(s); 1410 1469 --s->block_length[2]; 1411 return 1;1470 return BROTLI_TRUE; 1412 1471 } 1413 1472 distval = s->distance_code - (int)s->num_direct_distance_codes; … … 1431 1490 s->distance_code = -1; /* Restore precondition. */ 1432 1491 BrotliBitReaderRestoreState(br, &memento); 1433 return 0;1492 return BROTLI_FALSE; 1434 1493 } 1435 1494 } else { … … 1441 1500 } 1442 1501 } 1443 s->distance_code = s->distance_code - NUM_DISTANCE_SHORT_CODES + 1;1502 s->distance_code = s->distance_code - BROTLI_NUM_DISTANCE_SHORT_CODES + 1; 1444 1503 --s->block_length[2]; 1445 return 1; 1446 } 1447 1448 static BROTLI_INLINE void ReadDistance(BrotliState* s, BrotliBitReader* br) { 1504 return BROTLI_TRUE; 1505 } 1506 1507 static BROTLI_INLINE void ReadDistance( 1508 BrotliDecoderState* s, BrotliBitReader* br) { 1449 1509 ReadDistanceInternal(0, s, br); 1450 1510 } 1451 1511 1452 static BROTLI_INLINE int SafeReadDistance(BrotliState* s, BrotliBitReader* br) { 1512 static BROTLI_INLINE BROTLI_BOOL SafeReadDistance( 1513 BrotliDecoderState* s, BrotliBitReader* br) { 1453 1514 return ReadDistanceInternal(1, s, br); 1454 1515 } 1455 1516 1456 static BROTLI_INLINE int ReadCommandInternal(int safe,1457 BrotliState* s, BrotliBitReader* br, int* insert_length) {1517 static BROTLI_INLINE BROTLI_BOOL ReadCommandInternal( 1518 int safe, BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) { 1458 1519 uint32_t cmd_code; 1459 1520 uint32_t insert_len_extra = 0; … … 1466 1527 BrotliBitReaderSaveState(br, &memento); 1467 1528 if (!SafeReadSymbol(s->htree_command, br, &cmd_code)) { 1468 return 0;1529 return BROTLI_FALSE; 1469 1530 } 1470 1531 } … … 1475 1536 *insert_length = v.insert_len_offset; 1476 1537 if (!safe) { 1477 if ( PREDICT_FALSE(v.insert_len_extra_bits != 0)) {1538 if (BROTLI_PREDICT_FALSE(v.insert_len_extra_bits != 0)) { 1478 1539 insert_len_extra = BrotliReadBits(br, v.insert_len_extra_bits); 1479 1540 } … … 1483 1544 !SafeReadBits(br, v.copy_len_extra_bits, ©_length)) { 1484 1545 BrotliBitReaderRestoreState(br, &memento); 1485 return 0;1546 return BROTLI_FALSE; 1486 1547 } 1487 1548 } … … 1489 1550 --s->block_length[1]; 1490 1551 *insert_length += (int)insert_len_extra; 1491 return 1;1492 } 1493 1494 static BROTLI_INLINE void ReadCommand( BrotliState* s, BrotliBitReader* br,1495 int* insert_length) {1552 return BROTLI_TRUE; 1553 } 1554 1555 static BROTLI_INLINE void ReadCommand( 1556 BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) { 1496 1557 ReadCommandInternal(0, s, br, insert_length); 1497 1558 } 1498 1559 1499 static BROTLI_INLINE int SafeReadCommand(BrotliState* s, BrotliBitReader* br,1500 int* insert_length) {1560 static BROTLI_INLINE BROTLI_BOOL SafeReadCommand( 1561 BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) { 1501 1562 return ReadCommandInternal(1, s, br, insert_length); 1502 1563 } 1503 1564 1504 static BROTLI_INLINE int CheckInputAmount(int safe,1505 BrotliBitReader* const br, size_t num) {1565 static BROTLI_INLINE BROTLI_BOOL CheckInputAmount( 1566 int safe, BrotliBitReader* const br, size_t num) { 1506 1567 if (safe) { 1507 return 1;1568 return BROTLI_TRUE; 1508 1569 } 1509 1570 return BrotliCheckInputAmount(br, num); 1510 1571 } 1511 1572 1512 #define BROTLI_SAFE(METHOD) \1513 { \1514 if (safe) { \1515 if (!Safe##METHOD) { \1516 result = BROTLI_ NEEDS_MORE_INPUT; \1517 goto saveStateAndReturn; \1518 } \1519 } else { \1520 METHOD; \1521 } \1522 } 1523 1524 static BROTLI_INLINE Brotli ErrorCode ProcessCommandsInternal(int safe,1525 BrotliState* s) {1573 #define BROTLI_SAFE(METHOD) \ 1574 { \ 1575 if (safe) { \ 1576 if (!Safe##METHOD) { \ 1577 result = BROTLI_DECODER_NEEDS_MORE_INPUT; \ 1578 goto saveStateAndReturn; \ 1579 } \ 1580 } else { \ 1581 METHOD; \ 1582 } \ 1583 } 1584 1585 static BROTLI_INLINE BrotliDecoderErrorCode ProcessCommandsInternal( 1586 int safe, BrotliDecoderState* s) { 1526 1587 int pos = s->pos; 1527 1588 int i = s->loop_counter; 1528 Brotli ErrorCode result = BROTLI_SUCCESS;1589 BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS; 1529 1590 BrotliBitReader* br = &s->br; 1530 1591 1531 1592 if (!CheckInputAmount(safe, br, 28)) { 1532 result = BROTLI_ NEEDS_MORE_INPUT;1593 result = BROTLI_DECODER_NEEDS_MORE_INPUT; 1533 1594 goto saveStateAndReturn; 1534 1595 } … … 1547 1608 goto CommandPostWrapCopy; 1548 1609 } else { 1549 return BROTLI_FAILURE(BROTLI_ ERROR_UNREACHABLE);1610 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); 1550 1611 } 1551 1612 … … 1556 1617 if (!CheckInputAmount(safe, br, 28)) { /* 156 bits + 7 bytes */ 1557 1618 s->state = BROTLI_STATE_COMMAND_BEGIN; 1558 result = BROTLI_ NEEDS_MORE_INPUT;1619 result = BROTLI_DECODER_NEEDS_MORE_INPUT; 1559 1620 goto saveStateAndReturn; 1560 1621 } 1561 if ( PREDICT_FALSE(s->block_length[1] == 0)) {1622 if (BROTLI_PREDICT_FALSE(s->block_length[1] == 0)) { 1562 1623 BROTLI_SAFE(DecodeCommandBlockSwitch(s)); 1563 1624 goto CommandBegin; … … 1584 1645 if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */ 1585 1646 s->state = BROTLI_STATE_COMMAND_INNER; 1586 result = BROTLI_ NEEDS_MORE_INPUT;1647 result = BROTLI_DECODER_NEEDS_MORE_INPUT; 1587 1648 goto saveStateAndReturn; 1588 1649 } 1589 if ( PREDICT_FALSE(s->block_length[0] == 0)) {1650 if (BROTLI_PREDICT_FALSE(s->block_length[0] == 0)) { 1590 1651 BROTLI_SAFE(DecodeLiteralBlockSwitch(s)); 1591 1652 PreloadSymbol(safe, s->literal_htree, br, &bits, &value); … … 1598 1659 uint32_t literal; 1599 1660 if (!SafeReadSymbol(s->literal_htree, br, &literal)) { 1600 result = BROTLI_ NEEDS_MORE_INPUT;1661 result = BROTLI_DECODER_NEEDS_MORE_INPUT; 1601 1662 goto saveStateAndReturn; 1602 1663 } … … 1606 1667 BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos); 1607 1668 ++pos; 1608 if ( PREDICT_FALSE(pos == s->ringbuffer_size)) {1669 if (BROTLI_PREDICT_FALSE(pos == s->ringbuffer_size)) { 1609 1670 s->state = BROTLI_STATE_COMMAND_INNER_WRITE; 1610 1671 --i; … … 1620 1681 if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */ 1621 1682 s->state = BROTLI_STATE_COMMAND_INNER; 1622 result = BROTLI_ NEEDS_MORE_INPUT;1683 result = BROTLI_DECODER_NEEDS_MORE_INPUT; 1623 1684 goto saveStateAndReturn; 1624 1685 } 1625 if ( PREDICT_FALSE(s->block_length[0] == 0)) {1686 if (BROTLI_PREDICT_FALSE(s->block_length[0] == 0)) { 1626 1687 BROTLI_SAFE(DecodeLiteralBlockSwitch(s)); 1627 1688 if (s->trivial_literal_context) goto CommandInner; … … 1636 1697 uint32_t literal; 1637 1698 if (!SafeReadSymbol(hc, br, &literal)) { 1638 result = BROTLI_ NEEDS_MORE_INPUT;1699 result = BROTLI_DECODER_NEEDS_MORE_INPUT; 1639 1700 goto saveStateAndReturn; 1640 1701 } … … 1646 1707 BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos & s->ringbuffer_mask); 1647 1708 ++pos; 1648 if ( PREDICT_FALSE(pos == s->ringbuffer_size)) {1709 if (BROTLI_PREDICT_FALSE(pos == s->ringbuffer_size)) { 1649 1710 s->state = BROTLI_STATE_COMMAND_INNER_WRITE; 1650 1711 --i; … … 1654 1715 } 1655 1716 BROTLI_LOG_UINT(s->meta_block_remaining_len); 1656 if ( PREDICT_FALSE(s->meta_block_remaining_len <= 0)) {1717 if (BROTLI_PREDICT_FALSE(s->meta_block_remaining_len <= 0)) { 1657 1718 s->state = BROTLI_STATE_METABLOCK_DONE; 1658 1719 goto saveStateAndReturn; … … 1664 1725 } 1665 1726 if (s->distance_code >= 0) { 1727 /* Implicit distance case. */ 1728 s->distance_context = s->distance_code ? 0 : 1; 1666 1729 --s->dist_rb_idx; 1667 1730 s->distance_code = s->dist_rb[s->dist_rb_idx & 3]; 1668 goto postReadDistance; /* We already have the implicit distance */ 1669 } 1670 /* Read distance code in the command, unless it was implicitly zero. */ 1671 if (PREDICT_FALSE(s->block_length[2] == 0)) { 1672 BROTLI_SAFE(DecodeDistanceBlockSwitch(s)); 1673 } 1674 BROTLI_SAFE(ReadDistance(s, br)); 1675 postReadDistance: 1731 } else { 1732 /* Read distance code in the command, unless it was implicitly zero. */ 1733 if (BROTLI_PREDICT_FALSE(s->block_length[2] == 0)) { 1734 BROTLI_SAFE(DecodeDistanceBlockSwitch(s)); 1735 } 1736 BROTLI_SAFE(ReadDistance(s, br)); 1737 } 1676 1738 BROTLI_LOG(("[ProcessCommandsInternal] pos = %d distance = %d\n", 1677 1739 pos, s->distance_code)); 1678 1740 if (s->max_distance != s->max_backward_distance) { 1679 if (pos < s->max_backward_distance_minus_custom_dict_size) { 1680 s->max_distance = pos + s->custom_dict_size; 1681 } else { 1682 s->max_distance = s->max_backward_distance; 1683 } 1741 s->max_distance = 1742 (pos < s->max_backward_distance) ? pos : s->max_backward_distance; 1684 1743 } 1685 1744 i = s->copy_length; … … 1687 1746 the distance is larger than the max LZ77 distance */ 1688 1747 if (s->distance_code > s->max_distance) { 1689 i f (i >= kBrotliMinDictionaryWordLength &&1690 i <= kBrotliMaxDictionaryWordLength) {1691 int offset = (int)kBrotliDictionaryOffsetsByLength[i];1692 int word_id = s->distance_code - s->max_distance - 1;1693 uint32_t shift = kBrotliDictionarySizeBitsByLength[i];1748 int address = s->distance_code - s->max_distance - 1; 1749 if (i >= BROTLI_MIN_DICTIONARY_WORD_LENGTH && 1750 i <= BROTLI_MAX_DICTIONARY_WORD_LENGTH) { 1751 int offset = (int)s->dictionary->offsets_by_length[i]; 1752 uint32_t shift = s->dictionary->size_bits_by_length[i]; 1694 1753 int mask = (int)BitMask(shift); 1695 int word_idx = word_id & mask; 1696 int transform_idx = word_id >> shift; 1754 int word_idx = address & mask; 1755 int transform_idx = address >> shift; 1756 /* Compensate double distance-ring-buffer roll. */ 1757 s->dist_rb_idx += s->distance_context; 1697 1758 offset += word_idx * i; 1759 if (BROTLI_PREDICT_FALSE(!s->dictionary->data)) { 1760 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET); 1761 } 1698 1762 if (transform_idx < kNumTransforms) { 1699 const uint8_t* word = & kBrotliDictionary[offset];1763 const uint8_t* word = &s->dictionary->data[offset]; 1700 1764 int len = i; 1701 1765 if (transform_idx == 0) { 1702 1766 memcpy(&s->ringbuffer[pos], word, (size_t)len); 1767 BROTLI_LOG(("[ProcessCommandsInternal] dictionary word: [%.*s]\n", 1768 len, word)); 1703 1769 } else { 1704 1770 len = TransformDictionaryWord( 1705 1771 &s->ringbuffer[pos], word, len, transform_idx); 1772 BROTLI_LOG(("[ProcessCommandsInternal] dictionary word: [%.*s]," 1773 " transform_idx = %d, transformed: [%.*s]\n", 1774 i, word, transform_idx, len, &s->ringbuffer[pos])); 1706 1775 } 1707 1776 pos += len; … … 1716 1785 "len: %d bytes left: %d\n", 1717 1786 pos, s->distance_code, i, s->meta_block_remaining_len)); 1718 return BROTLI_FAILURE(BROTLI_ ERROR_FORMAT_TRANSFORM);1787 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_TRANSFORM); 1719 1788 } 1720 1789 } else { … … 1722 1791 "len: %d bytes left: %d\n", 1723 1792 pos, s->distance_code, i, s->meta_block_remaining_len)); 1724 return BROTLI_FAILURE(BROTLI_ ERROR_FORMAT_DICTIONARY);1793 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_DICTIONARY); 1725 1794 } 1726 1795 } else { … … 1734 1803 ++s->dist_rb_idx; 1735 1804 s->meta_block_remaining_len -= i; 1736 /* There are 32+ bytes of slack in the ring buffer allocation.1805 /* There are 32+ bytes of slack in the ring-buffer allocation. 1737 1806 Also, we have 16 short codes, that make these 16 bytes irrelevant 1738 in the ring buffer. Let's copy over them as a first guess.1807 in the ring-buffer. Let's copy over them as a first guess. 1739 1808 */ 1740 1809 memmove16(copy_dst, copy_src); … … 1773 1842 s->ringbuffer[(pos - s->distance_code) & s->ringbuffer_mask]; 1774 1843 ++pos; 1775 if ( PREDICT_FALSE(--wrap_guard == 0)) {1844 if (BROTLI_PREDICT_FALSE(--wrap_guard == 0)) { 1776 1845 s->state = BROTLI_STATE_COMMAND_POST_WRITE_2; 1777 1846 goto saveStateAndReturn; … … 1795 1864 #undef BROTLI_SAFE 1796 1865 1797 static BROTLI_NOINLINE BrotliErrorCode ProcessCommands(BrotliState* s) { 1866 static BROTLI_NOINLINE BrotliDecoderErrorCode ProcessCommands( 1867 BrotliDecoderState* s) { 1798 1868 return ProcessCommandsInternal(0, s); 1799 1869 } 1800 1870 1801 static BROTLI_NOINLINE BrotliErrorCode SafeProcessCommands(BrotliState* s) { 1871 static BROTLI_NOINLINE BrotliDecoderErrorCode SafeProcessCommands( 1872 BrotliDecoderState* s) { 1802 1873 return ProcessCommandsInternal(1, s); 1803 1874 } 1804 1875 1805 BrotliResult BrotliDecompressBuffer(size_t encoded_size, 1806 const uint8_t* encoded_buffer, 1807 size_t* decoded_size, 1808 uint8_t* decoded_buffer) { 1809 BrotliState s; 1810 BrotliResult result; 1876 BrotliDecoderResult BrotliDecoderDecompress( 1877 size_t encoded_size, const uint8_t* encoded_buffer, size_t* decoded_size, 1878 uint8_t* decoded_buffer) { 1879 BrotliDecoderState s; 1880 BrotliDecoderResult result; 1811 1881 size_t total_out = 0; 1812 1882 size_t available_in = encoded_size; … … 1814 1884 size_t available_out = *decoded_size; 1815 1885 uint8_t* next_out = decoded_buffer; 1816 Brotli StateInit(&s);1817 result = BrotliDeco mpressStream(&available_in, &next_in, &available_out,1818 & next_out, &total_out, &s);1886 BrotliDecoderStateInit(&s); 1887 result = BrotliDecoderDecompressStream( 1888 &s, &available_in, &next_in, &available_out, &next_out, &total_out); 1819 1889 *decoded_size = total_out; 1820 Brotli StateCleanup(&s);1821 if (result != BROTLI_ RESULT_SUCCESS) {1822 result = BROTLI_ RESULT_ERROR;1890 BrotliDecoderStateCleanup(&s); 1891 if (result != BROTLI_DECODER_RESULT_SUCCESS) { 1892 result = BROTLI_DECODER_RESULT_ERROR; 1823 1893 } 1824 1894 return result; … … 1828 1898 * invalid input implies that the whole stream is invalid -> any amount of 1829 1899 input could be read and discarded 1830 * when result is "needs more input", then at lea t one more byte is REQUIRED1900 * when result is "needs more input", then at least one more byte is REQUIRED 1831 1901 to complete decoding; all input data MUST be consumed by decoder, so 1832 1902 client could swap the input buffer … … 1837 1907 buffer; this is possible because the invariant is hold on enter 1838 1908 */ 1839 Brotli Result BrotliDecompressStream(size_t* available_in,1840 const uint8_t** next_in, size_t* available_out, uint8_t** next_out,1841 size_t* total_out, BrotliState* s) {1842 Brotli ErrorCode result = BROTLI_SUCCESS;1909 BrotliDecoderResult BrotliDecoderDecompressStream( 1910 BrotliDecoderState* s, size_t* available_in, const uint8_t** next_in, 1911 size_t* available_out, uint8_t** next_out, size_t* total_out) { 1912 BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS; 1843 1913 BrotliBitReader* br = &s->br; 1914 /* Do not try to process further in a case of unrecoverable error. */ 1915 if ((int)s->error_code < 0) { 1916 return BROTLI_DECODER_RESULT_ERROR; 1917 } 1918 if (*available_out && (!next_out || !*next_out)) { 1919 return SaveErrorCode( 1920 s, BROTLI_FAILURE(BROTLI_DECODER_ERROR_INVALID_ARGUMENTS)); 1921 } 1922 if (!*available_out) next_out = 0; 1844 1923 if (s->buffer_length == 0) { /* Just connect bit reader to input stream. */ 1845 1924 br->avail_in = *available_in; … … 1849 1928 be required to complete the transaction -> reading more data must be 1850 1929 done in a loop -> do it in a main loop. */ 1851 result = BROTLI_ NEEDS_MORE_INPUT;1930 result = BROTLI_DECODER_NEEDS_MORE_INPUT; 1852 1931 br->next_in = &s->buffer.u8[0]; 1853 1932 } 1854 1933 /* State machine */ 1855 1934 for (;;) { 1856 if (result != BROTLI_SUCCESS) { /* Error | needs more input/output */ 1857 if (result == BROTLI_NEEDS_MORE_INPUT) { 1858 if (s->ringbuffer != 0) { /* Proactively push output. */ 1859 WriteRingBuffer(available_out, next_out, total_out, s); 1935 if (result != BROTLI_DECODER_SUCCESS) { /* Error, needs more input/output */ 1936 if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) { 1937 if (s->ringbuffer != 0) { /* Pro-actively push output. */ 1938 BrotliDecoderErrorCode intermediate_result = WriteRingBuffer(s, 1939 available_out, next_out, total_out, BROTLI_TRUE); 1940 /* WriteRingBuffer checks s->meta_block_remaining_len validity. */ 1941 if ((int)intermediate_result < 0) { 1942 result = intermediate_result; 1943 break; 1944 } 1860 1945 } 1861 1946 if (s->buffer_length != 0) { /* Used with internal buffer. */ 1862 1947 if (br->avail_in == 0) { /* Successfully finished read transaction. */ 1863 /* Acc amulator contains less than 8 bits, because internal buffer1948 /* Accumulator contains less than 8 bits, because internal buffer 1864 1949 is expanded byte-by-byte until it is enough to complete read. */ 1865 1950 s->buffer_length = 0; 1866 1951 /* Switch to input stream and restart. */ 1867 result = BROTLI_ SUCCESS;1952 result = BROTLI_DECODER_SUCCESS; 1868 1953 br->avail_in = *available_in; 1869 1954 br->next_in = *next_in; … … 1872 1957 /* Not enough data in buffer, but can take one more byte from 1873 1958 input stream. */ 1874 result = BROTLI_ SUCCESS;1959 result = BROTLI_DECODER_SUCCESS; 1875 1960 s->buffer.u8[s->buffer_length] = **next_in; 1876 1961 s->buffer_length++; … … 1906 1991 } else { 1907 1992 /* Using input stream in last iteration. When decoder switches to input 1908 stream it has less than 8 bits in acc amulator, so it is safe to1909 return unused acc amulator bits there. */1993 stream it has less than 8 bits in accumulator, so it is safe to 1994 return unused accumulator bits there. */ 1910 1995 BrotliBitReaderUnload(br); 1911 1996 *available_in = br->avail_in; … … 1918 2003 /* Prepare to the first read. */ 1919 2004 if (!BrotliWarmupBitReader(br)) { 1920 result = BROTLI_ NEEDS_MORE_INPUT;2005 result = BROTLI_DECODER_NEEDS_MORE_INPUT; 1921 2006 break; 1922 2007 } … … 1926 2011 if (s->window_bits == 9) { 1927 2012 /* Value 9 is reserved for future use. */ 1928 result = BROTLI_FAILURE(BROTLI_ ERROR_FORMAT_WINDOW_BITS);2013 result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS); 1929 2014 break; 1930 2015 } 1931 2016 /* Maximum distance, see section 9.1. of the spec. */ 1932 s->max_backward_distance = (1 << s->window_bits) - 16; 1933 /* Limit custom dictionary size. */ 1934 if (s->custom_dict_size >= s->max_backward_distance) { 1935 s->custom_dict += s->custom_dict_size - s->max_backward_distance; 1936 s->custom_dict_size = s->max_backward_distance; 1937 } 1938 s->max_backward_distance_minus_custom_dict_size = 1939 s->max_backward_distance - s->custom_dict_size; 2017 s->max_backward_distance = (1 << s->window_bits) - BROTLI_WINDOW_GAP; 1940 2018 1941 2019 /* Allocate memory for both block_type_trees and block_len_trees. */ … … 1944 2022 (BROTLI_HUFFMAN_MAX_SIZE_258 + BROTLI_HUFFMAN_MAX_SIZE_26)); 1945 2023 if (s->block_type_trees == 0) { 1946 result = BROTLI_FAILURE(BROTLI_ ERROR_ALLOC_BLOCK_TYPE_TREES);2024 result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES); 1947 2025 break; 1948 2026 } … … 1953 2031 /* No break, continue to next state */ 1954 2032 case BROTLI_STATE_METABLOCK_BEGIN: 1955 Brotli StateMetablockBegin(s);2033 BrotliDecoderStateMetablockBegin(s); 1956 2034 BROTLI_LOG_UINT(s->pos); 1957 2035 s->state = BROTLI_STATE_METABLOCK_HEADER; … … 1959 2037 case BROTLI_STATE_METABLOCK_HEADER: 1960 2038 result = DecodeMetaBlockLength(s, br); /* Reads 2 - 31 bits. */ 1961 if (result != BROTLI_ SUCCESS) {2039 if (result != BROTLI_DECODER_SUCCESS) { 1962 2040 break; 1963 2041 } … … 1968 2046 if (s->is_metadata || s->is_uncompressed) { 1969 2047 if (!BrotliJumpToByteBoundary(br)) { 1970 result = BROTLI_FAILURE(BROTLI_ ERROR_FORMAT_PADDING_1);2048 result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_PADDING_1); 1971 2049 break; 1972 2050 } … … 1980 2058 break; 1981 2059 } 1982 if (!s->ringbuffer) { 1983 BrotliCalculateRingBufferSize(s, br); 1984 } 2060 BrotliCalculateRingBufferSize(s); 1985 2061 if (s->is_uncompressed) { 1986 2062 s->state = BROTLI_STATE_UNCOMPRESSED; … … 1991 2067 break; 1992 2068 case BROTLI_STATE_UNCOMPRESSED: { 1993 int bytes_copied = s->meta_block_remaining_len;1994 2069 result = CopyUncompressedBlockToOutput( 1995 2070 available_out, next_out, total_out, s); 1996 bytes_copied -= s->meta_block_remaining_len; 1997 if (result != BROTLI_SUCCESS) { 2071 if (result != BROTLI_DECODER_SUCCESS) { 1998 2072 break; 1999 2073 } … … 2006 2080 /* Read one byte and ignore it. */ 2007 2081 if (!BrotliSafeReadBits(br, 8, &bits)) { 2008 result = BROTLI_ NEEDS_MORE_INPUT;2082 result = BROTLI_DECODER_NEEDS_MORE_INPUT; 2009 2083 break; 2010 2084 } 2011 2085 } 2012 if (result == BROTLI_ SUCCESS) {2086 if (result == BROTLI_DECODER_SUCCESS) { 2013 2087 s->state = BROTLI_STATE_METABLOCK_DONE; 2014 2088 } … … 2021 2095 /* Reads 1..11 bits. */ 2022 2096 result = DecodeVarLenUint8(s, br, &s->num_block_types[s->loop_counter]); 2023 if (result != BROTLI_ SUCCESS) {2097 if (result != BROTLI_DECODER_SUCCESS) { 2024 2098 break; 2025 2099 } … … 2036 2110 result = ReadHuffmanCode(s->num_block_types[s->loop_counter] + 2, 2037 2111 &s->block_type_trees[tree_offset], NULL, s); 2038 if (result != BROTLI_ SUCCESS) break;2112 if (result != BROTLI_DECODER_SUCCESS) break; 2039 2113 s->state = BROTLI_STATE_HUFFMAN_CODE_2; 2040 2114 /* No break, continue to next state */ … … 2042 2116 case BROTLI_STATE_HUFFMAN_CODE_2: { 2043 2117 int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26; 2044 result = ReadHuffmanCode( kNumBlockLengthCodes,2118 result = ReadHuffmanCode(BROTLI_NUM_BLOCK_LEN_SYMBOLS, 2045 2119 &s->block_len_trees[tree_offset], NULL, s); 2046 if (result != BROTLI_ SUCCESS) break;2120 if (result != BROTLI_DECODER_SUCCESS) break; 2047 2121 s->state = BROTLI_STATE_HUFFMAN_CODE_3; 2048 2122 /* No break, continue to next state */ … … 2052 2126 if (!SafeReadBlockLength(s, &s->block_length[s->loop_counter], 2053 2127 &s->block_len_trees[tree_offset], br)) { 2054 result = BROTLI_ NEEDS_MORE_INPUT;2128 result = BROTLI_DECODER_NEEDS_MORE_INPUT; 2055 2129 break; 2056 2130 } … … 2063 2137 uint32_t bits; 2064 2138 if (!BrotliSafeReadBits(br, 6, &bits)) { 2065 result = BROTLI_ NEEDS_MORE_INPUT;2139 result = BROTLI_DECODER_NEEDS_MORE_INPUT; 2066 2140 break; 2067 2141 } 2068 2142 s->distance_postfix_bits = bits & BitMask(2); 2069 2143 bits >>= 2; 2070 s->num_direct_distance_codes = 2071 NUM_DISTANCE_SHORT_CODES +(bits << s->distance_postfix_bits);2144 s->num_direct_distance_codes = BROTLI_NUM_DISTANCE_SHORT_CODES + 2145 (bits << s->distance_postfix_bits); 2072 2146 BROTLI_LOG_UINT(s->num_direct_distance_codes); 2073 2147 BROTLI_LOG_UINT(s->distance_postfix_bits); … … 2076 2150 (uint8_t*)BROTLI_ALLOC(s, (size_t)s->num_block_types[0]); 2077 2151 if (s->context_modes == 0) { 2078 result = BROTLI_FAILURE(BROTLI_ ERROR_ALLOC_CONTEXT_MODES);2152 result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES); 2079 2153 break; 2080 2154 } … … 2085 2159 case BROTLI_STATE_CONTEXT_MODES: 2086 2160 result = ReadContextModes(s); 2087 if (result != BROTLI_ SUCCESS) {2161 if (result != BROTLI_DECODER_SUCCESS) { 2088 2162 break; 2089 2163 } … … 2092 2166 case BROTLI_STATE_CONTEXT_MAP_1: 2093 2167 result = DecodeContextMap( 2094 s->num_block_types[0] << kLiteralContextBits,2168 s->num_block_types[0] << BROTLI_LITERAL_CONTEXT_BITS, 2095 2169 &s->num_literal_htrees, &s->context_map, s); 2096 if (result != BROTLI_ SUCCESS) {2170 if (result != BROTLI_DECODER_SUCCESS) { 2097 2171 break; 2098 2172 } … … 2102 2176 case BROTLI_STATE_CONTEXT_MAP_2: 2103 2177 { 2104 uint32_t num_distance_codes = 2105 s->num_direct_distance_codes + (48U << s->distance_postfix_bits); 2178 uint32_t num_distance_codes = s->num_direct_distance_codes + 2179 ((2 * BROTLI_MAX_DISTANCE_BITS) << s->distance_postfix_bits); 2180 BROTLI_BOOL allocation_success = BROTLI_TRUE; 2106 2181 result = DecodeContextMap( 2107 s->num_block_types[2] << kDistanceContextBits,2182 s->num_block_types[2] << BROTLI_DISTANCE_CONTEXT_BITS, 2108 2183 &s->num_dist_htrees, &s->dist_context_map, s); 2109 if (result != BROTLI_ SUCCESS) {2184 if (result != BROTLI_DECODER_SUCCESS) { 2110 2185 break; 2111 2186 } 2112 BrotliHuffmanTreeGroupInit(s, &s->literal_hgroup, kNumLiteralCodes,2113 s->num_literal_htrees);2114 BrotliHuffmanTreeGroupInit(s, &s->insert_copy_hgroup,2115 kNumInsertAndCopyCodes,2116 s->num_block_types[1]);2117 BrotliHuffmanTreeGroupInit(s, &s->distance_hgroup, num_distance_codes,2118 s->num_dist_htrees);2119 if (s->literal_hgroup.codes == 0 ||2120 s-> insert_copy_hgroup.codes == 0 ||2121 s->distance_hgroup.codes == 0) {2187 allocation_success &= BrotliDecoderHuffmanTreeGroupInit( 2188 s, &s->literal_hgroup, BROTLI_NUM_LITERAL_SYMBOLS, 2189 s->num_literal_htrees); 2190 allocation_success &= BrotliDecoderHuffmanTreeGroupInit( 2191 s, &s->insert_copy_hgroup, BROTLI_NUM_COMMAND_SYMBOLS, 2192 s->num_block_types[1]); 2193 allocation_success &= BrotliDecoderHuffmanTreeGroupInit( 2194 s, &s->distance_hgroup, num_distance_codes, 2195 s->num_dist_htrees); 2196 if (!allocation_success) { 2122 2197 return SaveErrorCode(s, 2123 BROTLI_FAILURE(BROTLI_ ERROR_ALLOC_TREE_GROUPS));2198 BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS)); 2124 2199 } 2125 2200 } … … 2141 2216 break; 2142 2217 default: 2143 return SaveErrorCode(s, 2144 BROTLI_ FAILURE(BROTLI_ERROR_UNREACHABLE));2218 return SaveErrorCode(s, BROTLI_FAILURE( 2219 BROTLI_DECODER_ERROR_UNREACHABLE)); 2145 2220 } 2146 2221 result = HuffmanTreeGroupDecode(hgroup, s); 2147 2222 } 2148 if (result != BROTLI_ SUCCESS) break;2223 if (result != BROTLI_DECODER_SUCCESS) break; 2149 2224 s->loop_counter++; 2150 2225 if (s->loop_counter >= 3) { … … 2152 2227 s->dist_context_map_slice = s->dist_context_map; 2153 2228 s->htree_command = s->insert_copy_hgroup.htrees[0]; 2154 if (! s->ringbuffer && !BrotliAllocateRingBuffer(s)) {2155 result = BROTLI_FAILURE(BROTLI_ ERROR_ALLOC_RING_BUFFER_2);2229 if (!BrotliEnsureRingBuffer(s)) { 2230 result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2); 2156 2231 break; 2157 2232 } … … 2164 2239 case BROTLI_STATE_COMMAND_POST_WRAP_COPY: 2165 2240 result = ProcessCommands(s); 2166 if (result == BROTLI_ NEEDS_MORE_INPUT) {2241 if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) { 2167 2242 result = SafeProcessCommands(s); 2168 2243 } … … 2171 2246 case BROTLI_STATE_COMMAND_POST_WRITE_1: 2172 2247 case BROTLI_STATE_COMMAND_POST_WRITE_2: 2173 result = WriteRingBuffer(available_out, next_out, total_out, s); 2174 if (result != BROTLI_SUCCESS) { 2175 break; 2176 } 2177 s->max_distance = s->max_backward_distance; 2248 result = WriteRingBuffer( 2249 s, available_out, next_out, total_out, BROTLI_FALSE); 2250 if (result != BROTLI_DECODER_SUCCESS) { 2251 break; 2252 } 2253 WrapRingBuffer(s); 2254 if (s->ringbuffer_size == 1 << s->window_bits) { 2255 s->max_distance = s->max_backward_distance; 2256 } 2178 2257 if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_1) { 2179 memcpy(s->ringbuffer, s->ringbuffer_end, (size_t)s->pos);2180 2258 if (s->meta_block_remaining_len == 0) { 2181 2259 /* Next metablock, if any */ … … 2201 2279 case BROTLI_STATE_METABLOCK_DONE: 2202 2280 if (s->meta_block_remaining_len < 0) { 2203 result = BROTLI_FAILURE(BROTLI_ ERROR_FORMAT_BLOCK_LENGTH_2);2204 break; 2205 } 2206 Brotli StateCleanupAfterMetablock(s);2281 result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2); 2282 break; 2283 } 2284 BrotliDecoderStateCleanupAfterMetablock(s); 2207 2285 if (!s->is_last_metablock) { 2208 2286 s->state = BROTLI_STATE_METABLOCK_BEGIN; … … 2210 2288 } 2211 2289 if (!BrotliJumpToByteBoundary(br)) { 2212 result = BROTLI_FAILURE(BROTLI_ ERROR_FORMAT_PADDING_2);2290 result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_PADDING_2); 2213 2291 break; 2214 2292 } … … 2222 2300 case BROTLI_STATE_DONE: 2223 2301 if (s->ringbuffer != 0) { 2224 result = WriteRingBuffer(available_out, next_out, total_out, s); 2225 if (result != BROTLI_SUCCESS) { 2302 result = WriteRingBuffer( 2303 s, available_out, next_out, total_out, BROTLI_TRUE); 2304 if (result != BROTLI_DECODER_SUCCESS) { 2226 2305 break; 2227 2306 } … … 2233 2312 } 2234 2313 2235 void BrotliSetCustomDictionary( 2236 size_t size, const uint8_t* dict, BrotliState* s) { 2237 if (size > (1u << 24)) { 2238 return; 2239 } 2240 s->custom_dict = dict; 2241 s->custom_dict_size = (int)size; 2242 } 2243 2244 BrotliErrorCode BrotliGetErrorCode(const BrotliState* s) { 2245 return (BrotliErrorCode)s->error_code; 2246 } 2247 2248 const char* BrotliErrorString(BrotliErrorCode c) { 2314 BROTLI_BOOL BrotliDecoderHasMoreOutput(const BrotliDecoderState* s) { 2315 /* After unrecoverable error remaining output is considered nonsensical. */ 2316 if ((int)s->error_code < 0) { 2317 return BROTLI_FALSE; 2318 } 2319 return TO_BROTLI_BOOL( 2320 s->ringbuffer != 0 && UnwrittenBytes(s, BROTLI_FALSE) != 0); 2321 } 2322 2323 const uint8_t* BrotliDecoderTakeOutput(BrotliDecoderState* s, size_t* size) { 2324 uint8_t* result = 0; 2325 size_t available_out = *size ? *size : 1u << 24; 2326 size_t requested_out = available_out; 2327 BrotliDecoderErrorCode status; 2328 if ((s->ringbuffer == 0) || ((int)s->error_code < 0)) { 2329 *size = 0; 2330 return 0; 2331 } 2332 WrapRingBuffer(s); 2333 status = WriteRingBuffer(s, &available_out, &result, 0, BROTLI_TRUE); 2334 /* Either WriteRingBuffer returns those "success" codes... */ 2335 if (status == BROTLI_DECODER_SUCCESS || 2336 status == BROTLI_DECODER_NEEDS_MORE_OUTPUT) { 2337 *size = requested_out - available_out; 2338 } else { 2339 /* ... or stream is broken. Normally this should be caught by 2340 BrotliDecoderDecompressStream, this is just a safeguard. */ 2341 if ((int)status < 0) SaveErrorCode(s, status); 2342 *size = 0; 2343 result = 0; 2344 } 2345 return result; 2346 } 2347 2348 BROTLI_BOOL BrotliDecoderIsUsed(const BrotliDecoderState* s) { 2349 return TO_BROTLI_BOOL(s->state != BROTLI_STATE_UNINITED || 2350 BrotliGetAvailableBits(&s->br) != 0); 2351 } 2352 2353 BROTLI_BOOL BrotliDecoderIsFinished(const BrotliDecoderState* s) { 2354 return TO_BROTLI_BOOL(s->state == BROTLI_STATE_DONE) && 2355 !BrotliDecoderHasMoreOutput(s); 2356 } 2357 2358 BrotliDecoderErrorCode BrotliDecoderGetErrorCode(const BrotliDecoderState* s) { 2359 return (BrotliDecoderErrorCode)s->error_code; 2360 } 2361 2362 const char* BrotliDecoderErrorString(BrotliDecoderErrorCode c) { 2249 2363 switch (c) { 2250 #define _BROTLI_ERROR_CODE_CASE(PREFIX, NAME, CODE) \2251 case BROTLI ## PREFIX ## NAME: return #NAME;2252 #define _BROTLI_NOTHING2253 BROTLI_ ERROR_CODES_LIST(_BROTLI_ERROR_CODE_CASE, _BROTLI_NOTHING)2254 #undef _BROTLI_ERROR_CODE_CASE2255 #undef _BROTLI_NOTHING2364 #define BROTLI_ERROR_CODE_CASE_(PREFIX, NAME, CODE) \ 2365 case BROTLI_DECODER ## PREFIX ## NAME: return #NAME; 2366 #define BROTLI_NOTHING_ 2367 BROTLI_DECODER_ERROR_CODES_LIST(BROTLI_ERROR_CODE_CASE_, BROTLI_NOTHING_) 2368 #undef BROTLI_ERROR_CODE_CASE_ 2369 #undef BROTLI_NOTHING_ 2256 2370 default: return "INVALID"; 2257 2371 } 2372 } 2373 2374 uint32_t BrotliDecoderVersion() { 2375 return BROTLI_VERSION; 2258 2376 } 2259 2377 -
trunk/Source/ThirdParty/brotli/dec/huffman.c
r202225 r222903 11 11 #include <string.h> /* memcpy, memset */ 12 12 13 #include "../common/constants.h" 14 #include <brotli/types.h> 13 15 #include "./port.h" 14 #include "./types.h"15 16 16 17 #if defined(__cplusplus) || defined(c_plusplus) … … 21 22 22 23 #ifdef BROTLI_RBIT 23 #define BROTLI_REVERSE_BITS_BASE (32 - BROTLI_REVERSE_BITS_MAX) 24 #define BROTLI_REVERSE_BITS_BASE \ 25 ((sizeof(reg_t) << 3) - BROTLI_REVERSE_BITS_MAX) 24 26 #else 25 27 #define BROTLI_REVERSE_BITS_BASE 0 … … 61 63 62 64 #define BROTLI_REVERSE_BITS_LOWEST \ 63 ( 1U<< (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE))65 ((reg_t)1 << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE)) 64 66 65 67 /* Returns reverse(num >> BROTLI_REVERSE_BITS_BASE, BROTLI_REVERSE_BITS_MAX), 66 68 where reverse(value, len) is the bit-wise reversal of the len least 67 69 significant bits of value. */ 68 static BROTLI_INLINE uint32_t BrotliReverseBits(uint32_t num) {70 static BROTLI_INLINE reg_t BrotliReverseBits(reg_t num) { 69 71 #ifdef BROTLI_RBIT 70 72 return BROTLI_RBIT(num); … … 103 105 const uint8_t* const code_lengths, 104 106 uint16_t* count) { 105 HuffmanCode code; 106 int symbol; 107 uint32_t key;/* prefix code */108 uint32_t key_step;/* prefix code addend */109 int step; 110 int table_size; 111 int sorted[ 18];/* symbols sorted by code length */107 HuffmanCode code; /* current table entry */ 108 int symbol; /* symbol index in original or sorted table */ 109 reg_t key; /* prefix code */ 110 reg_t key_step; /* prefix code addend */ 111 int step; /* step size to replicate values in current table */ 112 int table_size; /* size of current table */ 113 int sorted[BROTLI_CODE_LENGTH_CODES]; /* symbols sorted by code length */ 112 114 /* offsets in sorted table for each length */ 113 115 int offset[BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1]; … … 126 128 }); 127 129 /* Symbols with code length 0 are placed after all other symbols. */ 128 offset[0] = 17;130 offset[0] = BROTLI_CODE_LENGTH_CODES - 1; 129 131 130 132 /* sort symbols by length, by symbol order within each length */ 131 symbol = 18;133 symbol = BROTLI_CODE_LENGTH_CODES; 132 134 do { 133 135 BROTLI_REPEAT(6, { … … 143 145 code.bits = 0; 144 146 code.value = (uint16_t)sorted[0]; 145 for (key = 0; key < ( uint32_t)table_size; ++key) {147 for (key = 0; key < (reg_t)table_size; ++key) { 146 148 table[key] = code; 147 149 } … … 171 173 const uint16_t* const symbol_lists, 172 174 uint16_t* count) { 173 HuffmanCode code; 174 HuffmanCode* table; 175 int len; 176 int symbol; 177 uint32_t key; /* prefix code */178 uint32_t key_step; /* prefix code addend */179 uint32_t sub_key; /* 2nd level table prefix code */180 uint32_t sub_key_step; /* 2nd level table prefix code addend */181 int step; 182 int table_bits; 183 int table_size; 184 int total_size; 175 HuffmanCode code; /* current table entry */ 176 HuffmanCode* table; /* next available space in table */ 177 int len; /* current code length */ 178 int symbol; /* symbol index in original or sorted table */ 179 reg_t key; /* prefix code */ 180 reg_t key_step; /* prefix code addend */ 181 reg_t sub_key; /* 2nd level table prefix code */ 182 reg_t sub_key_step; /* 2nd level table prefix code addend */ 183 int step; /* step size to replicate values in current table */ 184 int table_bits; /* key length of current table */ 185 int table_size; /* size of current table */ 186 int total_size; /* sum of root table size and 2nd level table sizes */ 185 187 int max_length = -1; 186 188 int bits; -
trunk/Source/ThirdParty/brotli/dec/huffman.h
r202225 r222903 10 10 #define BROTLI_DEC_HUFFMAN_H_ 11 11 12 #include "./types.h"12 #include <brotli/types.h> 13 13 #include "./port.h" 14 14 … … 19 19 #define BROTLI_HUFFMAN_MAX_CODE_LENGTH 15 20 20 21 /* For current format this constant equals to kNumInsertAndCopyCodes */22 #define BROTLI_HUFFMAN_MAX_CODE_LENGTHS_SIZE 70423 24 21 /* Maximum possible Huffman table size for an alphabet size of (index * 32), 25 22 * max code length 15 and root table bits 8. */ … … 27 24 256, 402, 436, 468, 500, 534, 566, 598, 630, 662, 694, 726, 758, 790, 822, 28 25 854, 886, 920, 952, 984, 1016, 1048, 1080}; 26 /* BROTLI_NUM_BLOCK_LEN_SYMBOLS == 26 */ 29 27 #define BROTLI_HUFFMAN_MAX_SIZE_26 396 28 /* BROTLI_MAX_BLOCK_TYPE_SYMBOLS == 258 */ 30 29 #define BROTLI_HUFFMAN_MAX_SIZE_258 632 30 /* BROTLI_MAX_CONTEXT_MAP_SYMBOLS == 272 */ 31 31 #define BROTLI_HUFFMAN_MAX_SIZE_272 646 32 32 -
trunk/Source/ThirdParty/brotli/dec/port.h
r202225 r222903 17 17 * BROTLI_BUILD_PORTABLE disables dangerous optimizations, like unaligned 18 18 read and overlapping memcpy; this reduces decompression speed by 5% 19 * BROTLI_BUILD_NO_RBIT disables "rbit" optimization for ARM CPUs 19 20 * BROTLI_DEBUG dumps file name and line number when decoder detects stream 20 21 or memory error … … 30 31 #endif 31 32 32 /* Compatibility with non-clang compilers. */ 33 #ifndef __has_builtin 34 #define __has_builtin(x) 0 35 #endif 36 37 #ifndef __has_attribute 38 #define __has_attribute(x) 0 39 #endif 40 41 #ifndef __has_feature 42 #define __has_feature(x) 0 43 #endif 33 #include <brotli/port.h> 44 34 45 35 #if defined(__arm__) || defined(__thumb__) || \ 46 defined(_M_ARM) || defined(_M_ARMT) 36 defined(_M_ARM) || defined(_M_ARMT) || defined(__ARM64_ARCH_8__) 47 37 #define BROTLI_TARGET_ARM 48 #if (defined(__ARM_ARCH) && (__ARM_ARCH >= 7)) || \49 (defined(M_ARM) && (M_ARM >= 7))38 #if (defined(__ARM_ARCH) && (__ARM_ARCH == 7)) || \ 39 (defined(M_ARM) && (M_ARM == 7)) 50 40 #define BROTLI_TARGET_ARMV7 51 41 #endif /* ARMv7 */ 52 #if defined(__aarch64__) 42 #if defined(__aarch64__) || defined(__ARM64_ARCH_8__) 53 43 #define BROTLI_TARGET_ARMV8 54 44 #endif /* ARMv8 */ … … 67 57 #endif 68 58 69 #if defined(__GNUC__) && defined(__GNUC_MINOR__)70 #define BROTLI_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)71 #else72 #define BROTLI_GCC_VERSION 073 #endif74 75 #if defined(__ICC)76 #define BROTLI_ICC_VERSION __ICC77 #else78 #define BROTLI_ICC_VERSION 079 #endif80 81 #if defined(BROTLI_BUILD_MODERN_COMPILER)82 #define BROTLI_MODERN_COMPILER 183 #elif (BROTLI_GCC_VERSION > 300) || (BROTLI_ICC_VERSION >= 1600)84 #define BROTLI_MODERN_COMPILER 185 #else86 #define BROTLI_MODERN_COMPILER 087 #endif88 89 59 #ifdef BROTLI_BUILD_PORTABLE 90 60 #define BROTLI_ALIGNED_READ (!!1) 91 61 #elif defined(BROTLI_TARGET_X86) || defined(BROTLI_TARGET_X64) || \ 92 62 defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8) 93 /* Allow unaligned read only for white listed CPUs. */63 /* Allow unaligned read only for white-listed CPUs. */ 94 64 #define BROTLI_ALIGNED_READ (!!0) 95 65 #else 96 66 #define BROTLI_ALIGNED_READ (!!1) 97 #endif98 99 /* Define "PREDICT_TRUE" and "PREDICT_FALSE" macros for capable compilers.100 101 To apply compiler hint, enclose the branching condition into macros, like this:102 103 if (PREDICT_TRUE(zero == 0)) {104 // main execution path105 } else {106 // compiler should place this code outside of main execution path107 }108 109 OR:110 111 if (PREDICT_FALSE(something_rare_or_unexpected_happens)) {112 // compiler should place this code outside of main execution path113 }114 115 */116 #if BROTLI_MODERN_COMPILER || __has_builtin(__builtin_expect)117 #define PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))118 #define PREDICT_FALSE(x) (__builtin_expect(x, 0))119 #else120 #define PREDICT_FALSE(x) (x)121 #define PREDICT_TRUE(x) (x)122 67 #endif 123 68 … … 128 73 #define IS_CONSTANT(x) (!!0) 129 74 #endif 130 131 #if BROTLI_MODERN_COMPILER || __has_attribute(always_inline)132 #define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((always_inline))133 #else134 #define ATTRIBUTE_ALWAYS_INLINE135 #endif136 137 #if defined(_WIN32) || defined(__CYGWIN__)138 #define ATTRIBUTE_VISIBILITY_HIDDEN139 #elif BROTLI_MODERN_COMPILER || __has_attribute(visibility)140 #define ATTRIBUTE_VISIBILITY_HIDDEN __attribute__ ((visibility ("hidden")))141 #else142 #define ATTRIBUTE_VISIBILITY_HIDDEN143 #endif144 145 #ifndef BROTLI_INTERNAL146 #define BROTLI_INTERNAL ATTRIBUTE_VISIBILITY_HIDDEN147 #endif148 149 #ifndef _MSC_VER150 #if defined(__cplusplus) || !defined(__STRICT_ANSI__) || \151 __STDC_VERSION__ >= 199901L152 #define BROTLI_INLINE inline ATTRIBUTE_ALWAYS_INLINE153 #else154 #define BROTLI_INLINE155 #endif156 #else /* _MSC_VER */157 #define BROTLI_INLINE __forceinline158 #endif /* _MSC_VER */159 75 160 76 #ifdef BROTLI_ENABLE_LOG … … 167 83 168 84 #if defined(BROTLI_DEBUG) || defined(BROTLI_ENABLE_LOG) 169 static inlinevoid BrotliDump(const char* f, int l, const char* fn) {85 static BROTLI_INLINE void BrotliDump(const char* f, int l, const char* fn) { 170 86 fprintf(stderr, "%s:%d (%s)\n", f, l, fn); 171 87 fflush(stderr); … … 185 101 #else 186 102 #define BROTLI_64_BITS 0 103 #endif 104 105 #if (BROTLI_64_BITS) 106 #define reg_t uint64_t 107 #else 108 #define reg_t uint32_t 187 109 #endif 188 110 … … 212 134 #endif 213 135 214 #if BROTLI_MODERN_COMPILER || __has_attribute(noinline)215 #define BROTLI_NOINLINE __attribute__((noinline))216 #else217 #define BROTLI_NOINLINE218 #endif219 220 136 #define BROTLI_REPEAT(N, X) { \ 221 137 if ((N & 1) != 0) {X;} \ … … 224 140 } 225 141 226 #if BROTLI_MODERN_COMPILER || defined(__llvm__) 227 #if defined(BROTLI_TARGET_ARMV7) 228 static BROTLI_INLINE unsigned BrotliRBit(unsigned input) { 229 unsigned output; 142 #if (BROTLI_MODERN_COMPILER || defined(__llvm__)) && \ 143 !defined(BROTLI_BUILD_NO_RBIT) 144 #if defined(BROTLI_TARGET_ARMV7) || defined(BROTLI_TARGET_ARMV8) 145 /* TODO: detect ARMv6T2 and enable this code for it. */ 146 static BROTLI_INLINE reg_t BrotliRBit(reg_t input) { 147 reg_t output; 230 148 __asm__("rbit %0, %1\n" : "=r"(output) : "r"(input)); 231 149 return output; … … 248 166 } 249 167 250 #define BROTLI_UNUSED(X) (void)(X)251 252 168 #endif /* BROTLI_DEC_PORT_H_ */ -
trunk/Source/ThirdParty/brotli/dec/prefix.h
r197933 r222903 12 12 #define BROTLI_DEC_PREFIX_H_ 13 13 14 #include "./types.h" 14 #include "../common/constants.h" 15 #include <brotli/types.h> 15 16 16 17 /* Represents the range of values belonging to a prefix code: */ … … 21 22 }; 22 23 23 static const struct PrefixCodeRange kBlockLengthPrefixCode[] = { 24 static const struct PrefixCodeRange 25 kBlockLengthPrefixCode[BROTLI_NUM_BLOCK_LEN_SYMBOLS] = { 24 26 { 1, 2}, { 5, 2}, { 9, 2}, { 13, 2}, 25 27 { 17, 3}, { 25, 3}, { 33, 3}, { 41, 3}, … … 40 42 } CmdLutElement; 41 43 42 static const CmdLutElement kCmdLut[ 704] = {44 static const CmdLutElement kCmdLut[BROTLI_NUM_COMMAND_SYMBOLS] = { 43 45 { 0x00, 0x00, 0, 0x00, 0x0000, 0x0002 }, 44 46 { 0x00, 0x00, 0, 0x01, 0x0000, 0x0003 }, -
trunk/Source/ThirdParty/brotli/dec/state.c
r202225 r222903 9 9 #include <stdlib.h> /* free, malloc */ 10 10 11 #include <brotli/types.h> 11 12 #include "./huffman.h" 12 #include "./types.h"13 13 14 14 #if defined(__cplusplus) || defined(c_plusplus) 15 15 extern "C" { 16 16 #endif 17 18 /* Declared in decode.h */19 int BrotliStateIsStreamStart(const BrotliState* s);20 int BrotliStateIsStreamEnd(const BrotliState* s);21 17 22 18 static void* DefaultAllocFunc(void* opaque, size_t size) { … … 30 26 } 31 27 32 void Brotli StateInit(BrotliState* s) {33 Brotli StateInitWithCustomAllocators(s, 0, 0, 0);28 void BrotliDecoderStateInit(BrotliDecoderState* s) { 29 BrotliDecoderStateInitWithCustomAllocators(s, 0, 0, 0); 34 30 } 35 31 36 void Brotli StateInitWithCustomAllocators(BrotliState* s,32 void BrotliDecoderStateInitWithCustomAllocators(BrotliDecoderState* s, 37 33 brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) { 38 34 if (!alloc_func) { … … 46 42 } 47 43 44 s->error_code = 0; /* BROTLI_DECODER_NO_ERROR */ 45 48 46 BrotliInitBitReader(&s->br); 49 47 s->state = BROTLI_STATE_UNINITED; … … 56 54 s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE; 57 55 56 s->dictionary = BrotliGetDictionary(); 57 58 58 s->buffer_length = 0; 59 59 s->loop_counter = 0; … … 65 65 s->block_len_trees = NULL; 66 66 s->ringbuffer = NULL; 67 s->ringbuffer_size = 0; 68 s->new_ringbuffer_size = 0; 69 s->ringbuffer_mask = 0; 67 70 68 71 s->context_map = NULL; … … 81 84 s->distance_hgroup.htrees = NULL; 82 85 83 s->custom_dict = NULL; 84 s->custom_dict_size = 0; 86 s->is_last_metablock = 0; 87 s->is_uncompressed = 0; 88 s->is_metadata = 0; 89 s->should_wrap_ringbuffer = 0; 90 s->canny_ringbuffer_allocation = 1; 85 91 86 s->is_last_metablock = 0;87 92 s->window_bits = 0; 88 93 s->max_distance = 0; … … 98 103 s->symbol_lists = &s->symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1]; 99 104 100 s->mtf_upper_bound = 255;105 s->mtf_upper_bound = 63; 101 106 } 102 107 103 void Brotli StateMetablockBegin(BrotliState* s) {108 void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) { 104 109 s->meta_block_remaining_len = 0; 105 110 s->block_length[0] = 1U << 28; … … 132 137 } 133 138 134 void Brotli StateCleanupAfterMetablock(BrotliState* s) {139 void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) { 135 140 BROTLI_FREE(s, s->context_modes); 136 141 BROTLI_FREE(s, s->context_map); 137 142 BROTLI_FREE(s, s->dist_context_map); 138 139 BrotliHuffmanTreeGroupRelease(s, &s->literal_hgroup); 140 BrotliHuffmanTreeGroupRelease(s, &s->insert_copy_hgroup); 141 BrotliHuffmanTreeGroupRelease(s, &s->distance_hgroup); 143 BROTLI_FREE(s, s->literal_hgroup.htrees); 144 BROTLI_FREE(s, s->insert_copy_hgroup.htrees); 145 BROTLI_FREE(s, s->distance_hgroup.htrees); 142 146 } 143 147 144 void Brotli StateCleanup(BrotliState* s) {145 Brotli StateCleanupAfterMetablock(s);148 void BrotliDecoderStateCleanup(BrotliDecoderState* s) { 149 BrotliDecoderStateCleanupAfterMetablock(s); 146 150 147 151 BROTLI_FREE(s, s->ringbuffer); … … 149 153 } 150 154 151 int BrotliStateIsStreamStart(const BrotliState* s) { 152 return (s->state == BROTLI_STATE_UNINITED && 153 BrotliGetAvailableBits(&s->br) == 0); 154 } 155 156 int BrotliStateIsStreamEnd(const BrotliState* s) { 157 return s->state == BROTLI_STATE_DONE; 158 } 159 160 void BrotliHuffmanTreeGroupInit(BrotliState* s, HuffmanTreeGroup* group, 161 uint32_t alphabet_size, uint32_t ntrees) { 155 BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s, 156 HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t ntrees) { 162 157 /* Pack two allocations into one */ 163 158 const size_t max_table_size = kMaxHuffmanTableSize[(alphabet_size + 31) >> 5]; 164 159 const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size; 165 160 const size_t htree_size = sizeof(HuffmanCode*) * ntrees; 166 char* p = (char*)BROTLI_ALLOC(s, code_size + htree_size); 161 /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */ 162 HuffmanCode** p = (HuffmanCode**)BROTLI_ALLOC(s, code_size + htree_size); 167 163 group->alphabet_size = (uint16_t)alphabet_size; 168 164 group->num_htrees = (uint16_t)ntrees; 169 group->codes = (HuffmanCode*)p; 170 group->htrees = (HuffmanCode**)(p + code_size); 171 } 172 173 void BrotliHuffmanTreeGroupRelease(BrotliState* s, HuffmanTreeGroup* group) { 174 BROTLI_FREE(s, group->codes); 175 group->htrees = NULL; 165 group->htrees = p; 166 group->codes = (HuffmanCode*)(&p[ntrees]); 167 return !!p; 176 168 } 177 169 -
trunk/Source/ThirdParty/brotli/dec/state.h
r202225 r222903 10 10 #define BROTLI_DEC_STATE_H_ 11 11 12 #include "../common/constants.h" 13 #include "../common/dictionary.h" 14 #include <brotli/types.h> 12 15 #include "./bit_reader.h" 13 16 #include "./huffman.h" 14 #include "./types.h"15 17 #include "./port.h" 16 18 … … 94 96 } BrotliRunningReadBlockLengthState; 95 97 96 struct Brotli StateStruct {98 struct BrotliDecoderStateStruct { 97 99 BrotliRunningState state; 98 100 … … 115 117 int pos; 116 118 int max_backward_distance; 117 int max_backward_distance_minus_custom_dict_size;118 119 int max_distance; 119 120 int ringbuffer_size; … … 141 142 block type. It is then not needed to keep the context (faster decoding). */ 142 143 int trivial_literal_context; 144 /* Distance context is actual after command is decoded and before distance 145 is computed. After distance computation it is used as a temporary variable. */ 143 146 int distance_context; 144 147 int meta_block_remaining_len; … … 161 164 162 165 /* For partial write operations */ 163 size_t rb_roundtrips; /* How many times we went around the ring buffer */164 size_t partial_pos_out; /* How much output to the user in total (<= rb)*/166 size_t rb_roundtrips; /* How many times we went around the ring-buffer */ 167 size_t partial_pos_out; /* How much output to the user in total */ 165 168 166 169 /* For ReadHuffmanCode */ … … 170 173 171 174 HuffmanCode table[32]; 172 /* List of of symbol chains. */175 /* List of heads of symbol chains. */ 173 176 uint16_t* symbol_lists; 174 177 /* Storage from symbol_lists. */ 175 178 uint16_t symbols_lists_array[BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1 + 176 BROTLI_ HUFFMAN_MAX_CODE_LENGTHS_SIZE];179 BROTLI_NUM_COMMAND_SYMBOLS]; 177 180 /* Tails of symbol chains. */ 178 181 int next_symbol[32]; 179 uint8_t code_length_code_lengths[ 18];182 uint8_t code_length_code_lengths[BROTLI_CODE_LENGTH_CODES]; 180 183 /* Population counts for the code lengths */ 181 184 uint16_t code_length_histo[16]; … … 193 196 /* For InverseMoveToFrontTransform */ 194 197 uint32_t mtf_upper_bound; 195 uint8_t mtf[256 + 4]; 196 197 /* For custom dictionaries */ 198 const uint8_t* custom_dict; 199 int custom_dict_size; 198 uint32_t mtf[64 + 1]; 200 199 201 200 /* less used attributes are in the end of this struct */ … … 209 208 BrotliRunningReadBlockLengthState substate_read_block_length; 210 209 211 uint8_t is_last_metablock; 212 uint8_t is_uncompressed; 213 uint8_t is_metadata; 214 uint8_t size_nibbles; 210 unsigned int is_last_metablock : 1; 211 unsigned int is_uncompressed : 1; 212 unsigned int is_metadata : 1; 213 unsigned int should_wrap_ringbuffer : 1; 214 unsigned int canny_ringbuffer_allocation : 1; 215 unsigned int size_nibbles : 8; 215 216 uint32_t window_bits; 217 218 int new_ringbuffer_size; 216 219 217 220 uint32_t num_literal_htrees; 218 221 uint8_t* context_map; 219 222 uint8_t* context_modes; 223 const BrotliDictionary* dictionary; 220 224 221 225 uint32_t trivial_literal_contexts[8]; /* 256 bits */ 222 226 }; 223 227 224 typedef struct BrotliStateStruct BrotliStateInternal; 225 #define BrotliState BrotliStateInternal 226 227 BROTLI_INTERNAL void BrotliStateInit(BrotliState* s); 228 BROTLI_INTERNAL void BrotliStateInitWithCustomAllocators(BrotliState* s, 229 brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque); 230 BROTLI_INTERNAL void BrotliStateCleanup(BrotliState* s); 231 BROTLI_INTERNAL void BrotliStateMetablockBegin(BrotliState* s); 232 BROTLI_INTERNAL void BrotliStateCleanupAfterMetablock(BrotliState* s); 233 BROTLI_INTERNAL void BrotliHuffmanTreeGroupInit(BrotliState* s, 234 HuffmanTreeGroup* group, uint32_t alphabet_size, uint32_t ntrees); 235 BROTLI_INTERNAL void BrotliHuffmanTreeGroupRelease(BrotliState* s, 236 HuffmanTreeGroup* group); 228 typedef struct BrotliDecoderStateStruct BrotliDecoderStateInternal; 229 #define BrotliDecoderState BrotliDecoderStateInternal 230 231 BROTLI_INTERNAL void BrotliDecoderStateInit(BrotliDecoderState* s); 232 BROTLI_INTERNAL void BrotliDecoderStateInitWithCustomAllocators( 233 BrotliDecoderState* s, brotli_alloc_func alloc_func, 234 brotli_free_func free_func, void* opaque); 235 BROTLI_INTERNAL void BrotliDecoderStateCleanup(BrotliDecoderState* s); 236 BROTLI_INTERNAL void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s); 237 BROTLI_INTERNAL void BrotliDecoderStateCleanupAfterMetablock( 238 BrotliDecoderState* s); 239 BROTLI_INTERNAL BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit( 240 BrotliDecoderState* s, HuffmanTreeGroup* group, uint32_t alphabet_size, 241 uint32_t ntrees); 237 242 238 243 #if defined(__cplusplus) || defined(c_plusplus) -
trunk/Source/ThirdParty/brotli/dec/transform.h
r197933 r222903 10 10 #define BROTLI_DEC_TRANSFORM_H_ 11 11 12 #include <brotli/types.h> 12 13 #include "./port.h" 13 #include "./types.h"14 14 15 15 #if defined(__cplusplus) || defined(c_plusplus) … … 248 248 return 1; 249 249 } 250 /* An overly simplified uppercasing model for utf-8. */250 /* An overly simplified uppercasing model for UTF-8. */ 251 251 if (p[0] < 0xe0) { 252 252 p[1] ^= 32; -
trunk/Source/ThirdParty/brotli/update.sh
r197933 r222903 11 11 perl -p -i -e "s/\[commit [0-9a-f]{40}\]/[${COMMIT}]/" README.webkit; 12 12 13 rm -rf dec 14 mv ${MY_TEMP_DIR}/brotli/dec dec 13 rm -rf dec common include/brotli 14 mv ${MY_TEMP_DIR}/brotli/c/dec dec 15 mv ${MY_TEMP_DIR}/brotli/c/common common 16 mv ${MY_TEMP_DIR}/brotli/c/include/brotli include/brotli 15 17 rm -rf ${MY_TEMP_DIR} 16 18 -
trunk/Source/ThirdParty/woff2/CMakeLists.txt
r220403 r222903 13 13 ) 14 14 15 include_directories("${THIRDPARTY_DIR}/brotli/ dec")15 include_directories("${THIRDPARTY_DIR}/brotli/include") 16 16 include_directories("${WOFF2_INCLUDE_DIRECTORIES}") 17 17 add_library(woff2 STATIC ${WOFF2_SOURCES}) -
trunk/Source/ThirdParty/woff2/README.webkit
r206511 r222903 12 12 from within the modules/woff2 directory. 13 13 14 Current version: [commit 63b8fb6d0d797f04e77ee825fd8fcf7ea6205aac].14 Current version: [commit aa283a500aeb655834d77f3cf9cf1b093b0b4389]. -
trunk/Source/ThirdParty/woff2/src/buffer.h
r197933 r222903 66 66 class Buffer { 67 67 public: 68 Buffer(const uint8_t * buffer, size_t len)69 : buffer_( buffer),68 Buffer(const uint8_t *data, size_t len) 69 : buffer_(data), 70 70 length_(len), 71 71 offset_(0) { } … … 75 75 } 76 76 77 bool Read(uint8_t * buffer, size_t n_bytes) {77 bool Read(uint8_t *data, size_t n_bytes) { 78 78 if (n_bytes > 1024 * 1024 * 1024) { 79 79 return FONT_COMPRESSION_FAILURE(); … … 83 83 return FONT_COMPRESSION_FAILURE(); 84 84 } 85 if ( buffer) {86 std::memcpy( buffer, buffer_ + offset_, n_bytes);85 if (data) { 86 std::memcpy(data, buffer_ + offset_, n_bytes); 87 87 } 88 88 offset_ += n_bytes; -
trunk/Source/ThirdParty/woff2/src/font.cc
r206511 r222903 106 106 } 107 107 108 // Sanity check key tables 109 const Font::Table* head_table = font->FindTable(kHeadTableTag); 110 if (head_table != NULL && head_table->length < 52) { 111 return FONT_COMPRESSION_FAILURE(); 112 } 113 108 114 return true; 109 115 } … … 326 332 } 327 333 int index_fmt = IndexFormat(font); 328 int num_glyphs = (loca_table->length / (index_fmt == 0 ? 2 : 4)) - 1; 329 return num_glyphs; 334 int loca_record_size = (index_fmt == 0 ? 2 : 4); 335 if (loca_table->length < loca_record_size) { 336 return 0; 337 } 338 return (loca_table->length / loca_record_size) - 1; 330 339 } 331 340 -
trunk/Source/ThirdParty/woff2/src/glyph.cc
r206511 r222903 119 119 // Read the run-length coded flags. 120 120 std::vector<std::vector<uint8_t> > flags(num_contours); 121 uint8_t flag = 0; 122 uint8_t flag_repeat = 0; 123 for (int i = 0; i < num_contours; ++i) { 124 flags[i].resize(glyph->contours[i].size()); 125 for (size_t j = 0; j < glyph->contours[i].size(); ++j) { 126 if (flag_repeat == 0) { 127 if (!buffer.ReadU8(&flag)) { 128 return FONT_COMPRESSION_FAILURE(); 129 } 130 if (flag & kFLAG_REPEAT) { 131 if (!buffer.ReadU8(&flag_repeat)) { 121 { 122 uint8_t flag = 0; 123 uint8_t flag_repeat = 0; 124 for (int i = 0; i < num_contours; ++i) { 125 flags[i].resize(glyph->contours[i].size()); 126 for (size_t j = 0; j < glyph->contours[i].size(); ++j) { 127 if (flag_repeat == 0) { 128 if (!buffer.ReadU8(&flag)) { 132 129 return FONT_COMPRESSION_FAILURE(); 133 130 } 134 } 135 } else { 136 flag_repeat--; 137 } 138 flags[i][j] = flag; 139 glyph->contours[i][j].on_curve = flag & kFLAG_ONCURVE; 131 if (flag & kFLAG_REPEAT) { 132 if (!buffer.ReadU8(&flag_repeat)) { 133 return FONT_COMPRESSION_FAILURE(); 134 } 135 } 136 } else { 137 flag_repeat--; 138 } 139 flags[i][j] = flag; 140 glyph->contours[i][j].on_curve = flag & kFLAG_ONCURVE; 141 } 140 142 } 141 143 } -
trunk/Source/ThirdParty/woff2/src/normalize.cc
r197933 r222903 53 53 loca_table->length = (num_glyphs + 1) * glyph_sz; 54 54 55 uint8_t* glyf_dst = &glyf_table->buffer[0];55 uint8_t* glyf_dst = num_glyphs ? &glyf_table->buffer[0] : NULL; 56 56 uint8_t* loca_dst = &loca_table->buffer[0]; 57 57 uint32_t glyf_offset = 0; … … 79 79 glyf_offset += glyf_dst_size; 80 80 } 81 if (glyf_offset == 0) {82 return false;83 }84 81 85 82 StoreLoca(index_fmt, glyf_offset, &loca_offset, loca_dst); 86 83 87 84 glyf_table->buffer.resize(glyf_offset); 88 glyf_table->data = &glyf_table->buffer[0];85 glyf_table->data = glyf_offset ? &glyf_table->buffer[0] : NULL; 89 86 glyf_table->length = glyf_offset; 90 loca_table->data = &loca_table->buffer[0];87 loca_table->data = loca_offset ? &loca_table->buffer[0] : NULL; 91 88 92 89 return true; -
trunk/Source/ThirdParty/woff2/src/port.h
r206418 r222903 61 61 #endif 62 62 63 #if (defined(__ARM_ARCH) && (__ARM_ARCH == 7)) || \ 64 (defined(M_ARM) && (M_ARM == 7)) || \ 65 defined(__aarch64__) || defined(__ARM64_ARCH_8__) || defined(__i386) || \ 66 defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64) 67 #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) 68 #define WOFF_LITTLE_ENDIAN 69 #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) 70 #define WOFF_BIG_ENDIAN 71 #endif /* endianness */ 72 #endif /* CPU whitelist */ 73 63 74 #endif // WOFF2_PORT_H_ -
trunk/Source/ThirdParty/woff2/src/store_bytes.h
r206511 r222903 23 23 #include <string.h> 24 24 25 #include "./port.h" 26 25 27 namespace woff2 { 26 28 … … 34 36 35 37 inline size_t Store16(uint8_t* dst, size_t offset, int x) { 36 #if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))38 #if defined(WOFF_LITTLE_ENDIAN) 37 39 *reinterpret_cast<uint16_t*>(dst + offset) = 38 40 ((x & 0xFF) << 8) | ((x & 0xFF00) >> 8); 39 #elif (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))41 #elif defined(WOFF_BIG_ENDIAN) 40 42 *reinterpret_cast<uint16_t*>(dst + offset) = static_cast<uint16_t>(x); 41 43 #else … … 54 56 55 57 inline void Store16(int val, size_t* offset, uint8_t* dst) { 56 #if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))58 #if defined(WOFF_LITTLE_ENDIAN) 57 59 *reinterpret_cast<uint16_t*>(dst + *offset) = 58 60 ((val & 0xFF) << 8) | ((val & 0xFF00) >> 8); 59 61 *offset += 2; 60 #elif (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))62 #elif defined(WOFF_BIG_ENDIAN) 61 63 *reinterpret_cast<uint16_t*>(dst + *offset) = static_cast<uint16_t>(val); 62 64 *offset += 2; -
trunk/Source/ThirdParty/woff2/src/variable_length.cc
r197933 r222903 50 50 std::vector<uint8_t> packed; 51 51 Write255UShort(&packed, val); 52 for (uint8_t val: packed) {53 dst[(*offset)++] = val;52 for (uint8_t packed_byte : packed) { 53 dst[(*offset)++] = packed_byte; 54 54 } 55 55 } -
trunk/Source/ThirdParty/woff2/src/woff2_common.cc
r206511 r222903 19 19 #include "./woff2_common.h" 20 20 21 #include "./port.h" 22 21 23 namespace woff2 { 22 24 … … 26 28 size_t aligned_size = size & ~3; 27 29 for (size_t i = 0; i < aligned_size; i += 4) { 28 #if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))30 #if defined(WOFF_LITTLE_ENDIAN) 29 31 uint32_t v = *reinterpret_cast<const uint32_t*>(buf + i); 30 32 checksum += (((v & 0xFF) << 24) | ((v & 0xFF00) << 8) | 31 33 ((v & 0xFF0000) >> 8) | ((v & 0xFF000000) >> 24)); 32 #elif (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))34 #elif defined(WOFF_BIG_ENDIAN) 33 35 checksum += *reinterpret_cast<const uint32_t*>(buf + i); 34 36 #else -
trunk/Source/ThirdParty/woff2/src/woff2_dec.cc
r206511 r222903 28 28 #include <utility> 29 29 30 #include "./ decode.h"30 #include "./brotli/decode.h" 31 31 #include "./buffer.h" 32 32 #include "./port.h" … … 412 412 } 413 413 414 // https://dev.w3.org/webfonts/WOFF2/spec/#conform-mustRejectLoca 415 // dst_length here is origLength in the spec 416 uint32_t expected_loca_dst_length = (info->index_format ? 4 : 2) 417 * (static_cast<uint32_t>(info->num_glyphs) + 1); 418 if (PREDICT_FALSE(loca_table->dst_length != expected_loca_dst_length)) { 419 return FONT_COMPRESSION_FAILURE(); 420 } 421 414 422 unsigned int offset = (2 + kNumSubStreams) * 4; 415 423 if (PREDICT_FALSE(offset > glyf_table->transform_length)) { … … 593 601 return FONT_COMPRESSION_FAILURE(); 594 602 } 603 } else { 604 // n_contours == 0; empty glyph. Must NOT have a bbox. 605 if (PREDICT_FALSE(have_bbox)) { 606 #ifdef FONT_COMPRESSION_BIN 607 fprintf(stderr, "Empty glyph has a bbox\n"); 608 #endif 609 return FONT_COMPRESSION_FAILURE(); 610 } 595 611 } 596 612 … … 670 686 bool has_monospace_lsbs = (hmtx_flags & 2) == 0; 671 687 688 // Bits 2-7 are reserved and MUST be zero. 689 if ((hmtx_flags & 0xFC) != 0) { 690 #ifdef FONT_COMPRESSION_BIN 691 fprintf(stderr, "Illegal hmtx flags; bits 2-7 must be 0\n"); 692 #endif 693 return FONT_COMPRESSION_FAILURE(); 694 } 695 672 696 // you say you transformed but there is little evidence of it 673 697 if (has_proportional_lsbs && has_monospace_lsbs) { … … 743 767 const uint8_t* src_buf, size_t src_size) { 744 768 size_t uncompressed_size = dst_size; 745 int ok = BrotliDecompressBuffer(src_size, src_buf, 746 &uncompressed_size, dst_buf); 747 if (PREDICT_FALSE(!ok || uncompressed_size != dst_size)) { 769 BrotliDecoderResult result = BrotliDecoderDecompress( 770 src_size, src_buf, &uncompressed_size, dst_buf); 771 if (PREDICT_FALSE(result != BROTLI_DECODER_RESULT_SUCCESS || 772 uncompressed_size != dst_size)) { 748 773 return FONT_COMPRESSION_FAILURE(); 749 774 } … … 876 901 877 902 // 'glyf' without 'loca' doesn't make sense 878 if (PREDICT_FALSE(static_cast<bool>(FindTable(&tables, kGlyfTableTag)) != 879 static_cast<bool>(FindTable(&tables, kLocaTableTag)))) { 880 return FONT_COMPRESSION_FAILURE(); 903 const Table* glyf_table = FindTable(&tables, kGlyfTableTag); 904 const Table* loca_table = FindTable(&tables, kLocaTableTag); 905 if (PREDICT_FALSE(static_cast<bool>(glyf_table) != 906 static_cast<bool>(loca_table))) { 907 #ifdef FONT_COMPRESSION_BIN 908 fprintf(stderr, "Cannot have just one of glyf/loca\n"); 909 #endif 910 return FONT_COMPRESSION_FAILURE(); 911 } 912 913 if (glyf_table != NULL) { 914 if (PREDICT_FALSE((glyf_table->flags & kWoff2FlagsTransform) 915 != (loca_table->flags & kWoff2FlagsTransform))) { 916 #ifdef FONT_COMPRESSION_BIN 917 fprintf(stderr, "Cannot transform just one of glyf/loca\n"); 918 #endif 919 return FONT_COMPRESSION_FAILURE(); 920 } 881 921 } 882 922 … … 899 939 // TODO(user) a collection with optimized hmtx that reused glyf/loca 900 940 // would fail. We don't optimize hmtx for collections yet. 901 if (PREDICT_FALSE(static_cast<uint64_t>(table.src_offset + table.src_length)941 if (PREDICT_FALSE(static_cast<uint64_t>(table.src_offset) + table.src_length 902 942 > transformed_buf_size)) { 903 943 return FONT_COMPRESSION_FAILURE(); … … 1100 1140 ttc_font.table_indices.resize(num_tables); 1101 1141 1102 const Table* glyf_table = NULL; 1103 const Table* loca_table = NULL; 1142 1143 unsigned int glyf_idx = 0; 1144 unsigned int loca_idx = 0; 1104 1145 1105 1146 for (uint32_t j = 0; j < num_tables; j++) { … … 1113 1154 const Table& table = hdr->tables[table_idx]; 1114 1155 if (table.tag == kLocaTableTag) { 1115 loca_ table = &table;1156 loca_idx = table_idx; 1116 1157 } 1117 1158 if (table.tag == kGlyfTableTag) { 1118 glyf_ table = &table;1159 glyf_idx = table_idx; 1119 1160 } 1120 1161 1121 1162 } 1122 1163 1123 if (PREDICT_FALSE((glyf_table == NULL) != (loca_table == NULL))) { 1164 // if we have both glyf and loca make sure they are consecutive 1165 // if we have just one we'll reject the font elsewhere 1166 if (glyf_idx > 0 || loca_idx > 0) { 1167 if (PREDICT_FALSE(glyf_idx > loca_idx || loca_idx - glyf_idx != 1)) { 1124 1168 #ifdef FONT_COMPRESSION_BIN 1125 fprintf(stderr, " Cannot have just one of glyf/loca\n");1169 fprintf(stderr, "TTC font %d has non-consecutive glyf/loca\n", i); 1126 1170 #endif 1127 return FONT_COMPRESSION_FAILURE(); 1171 return FONT_COMPRESSION_FAILURE(); 1172 } 1128 1173 } 1129 1174 } … … 1299 1344 const uint8_t* src_buf = data + hdr.compressed_offset; 1300 1345 std::vector<uint8_t> uncompressed_buf(hdr.uncompressed_size); 1346 if (PREDICT_FALSE(hdr.uncompressed_size < 1)) { 1347 return FONT_COMPRESSION_FAILURE(); 1348 } 1301 1349 if (PREDICT_FALSE(!Woff2Uncompress(&uncompressed_buf[0], 1302 1350 hdr.uncompressed_size, src_buf, -
trunk/Source/ThirdParty/woff2/src/woff2_decompress.cc
r206511 r222903 33 33 string outfilename = filename.substr(0, filename.find_last_of(".")) + ".ttf"; 34 34 35 // Note: update woff2_dec_fuzzer_new_entry.cc if this pattern changes. 35 36 string input = woff2::GetFileContent(filename); 36 37 const uint8_t* raw_input = reinterpret_cast<const uint8_t*>(input.data()); -
trunk/Source/ThirdParty/woff2/src/woff2_enc.cc
r206511 r222903 24 24 #include <vector> 25 25 26 #include "./ compressor.h"26 #include "./brotli/encode.h" 27 27 #include "./buffer.h" 28 28 #include "./font.h" … … 35 35 #include "./woff2_common.h" 36 36 37 38 37 namespace woff2 { 39 38 … … 48 47 const size_t kWoff2EntrySize = 20; 49 48 50 51 bool Compress(const uint8_t* data, const size_t len, 52 uint8_t* result, uint32_t* result_len, 53 brotli::BrotliParams::Mode mode, int quality) { 49 bool Compress(const uint8_t* data, const size_t len, uint8_t* result, 50 uint32_t* result_len, BrotliEncoderMode mode, int quality) { 54 51 size_t compressed_len = *result_len; 55 brotli::BrotliParams params; 56 params.mode = mode; 57 params.quality = quality; 58 if (brotli::BrotliCompressBuffer(params, len, data, &compressed_len, result) 59 == 0) { 52 if (BrotliEncoderCompress(quality, BROTLI_DEFAULT_WINDOW, mode, len, data, 53 &compressed_len, result) == 0) { 60 54 return false; 61 55 } … … 68 62 int quality) { 69 63 return Compress(data, len, result, result_len, 70 brotli::BrotliParams::MODE_FONT, quality);64 BROTLI_MODE_FONT, quality); 71 65 } 72 66 … … 75 69 int quality) { 76 70 return Compress(data, len, result, result_len, 77 brotli::BrotliParams::MODE_TEXT, quality);71 BROTLI_MODE_TEXT, quality); 78 72 } 79 73 … … 112 106 size_t ComputeWoff2Length(const FontCollection& font_collection, 113 107 const std::vector<Table>& tables, 114 std::map<uint32_t, uint16_t> index_by_offset, 108 std::map<std::pair<uint32_t, uint32_t>, uint16_t> 109 index_by_tag_offset, 115 110 size_t compressed_data_length, 116 111 size_t extended_metadata_length) { … … 135 130 if (table.tag & 0x80808080) continue; 136 131 137 uint16_t table_index = index_by_offset[table.offset]; 132 std::pair<uint32_t, uint32_t> tag_offset(table.tag, table.offset); 133 uint16_t table_index = index_by_tag_offset[tag_offset]; 138 134 size += Size255UShort(table_index); // 255UInt16 index entry 139 135 } … … 327 323 328 324 std::vector<Table> tables; 329 std::map< uint32_t, uint16_t> index_by_offset;325 std::map<std::pair<uint32_t, uint32_t>, uint16_t> index_by_tag_offset; 330 326 331 327 for (const auto& font : font_collection.fonts) { … … 337 333 } 338 334 339 if (index_by_offset.find(src_table.offset) == index_by_offset.end()) { 340 index_by_offset[src_table.offset] = tables.size(); 335 std::pair<uint32_t, uint32_t> tag_offset(src_table.tag, src_table.offset); 336 if (index_by_tag_offset.find(tag_offset) == index_by_tag_offset.end()) { 337 index_by_tag_offset[tag_offset] = tables.size(); 341 338 } else { 342 339 return false; … … 363 360 364 361 size_t woff2_length = ComputeWoff2Length(font_collection, tables, 365 index_by_offset, total_compressed_length, compressed_metadata_buf_length); 362 index_by_tag_offset, total_compressed_length, 363 compressed_metadata_buf_length); 366 364 if (woff2_length > *result_length) { 367 365 #ifdef FONT_COMPRESSION_BIN … … 436 434 uint32_t table_length = 437 435 table.IsReused() ? table.reuse_of->length : table.length; 438 if (index_by_offset.find(table_offset) == index_by_offset.end()) { 436 std::pair<uint32_t, uint32_t> tag_offset(table.tag, table_offset); 437 if (index_by_tag_offset.find(tag_offset) == index_by_tag_offset.end()) { 439 438 #ifdef FONT_COMPRESSION_BIN 440 439 fprintf(stderr, "Missing table index for offset 0x%08x\n", … … 443 442 return FONT_COMPRESSION_FAILURE(); 444 443 } 445 uint16_t index = index_by_ offset[table_offset];444 uint16_t index = index_by_tag_offset[tag_offset]; 446 445 Store255UShort(index, &offset, result); 447 446
Note: See TracChangeset
for help on using the changeset viewer.