| OLD | NEW |
| 1 /* Copyright 2013 Google Inc. All Rights Reserved. | 1 /* Copyright 2013 Google Inc. All Rights Reserved. |
| 2 | 2 |
| 3 Distributed under MIT license. | 3 Distributed under MIT license. |
| 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT | 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #include "./decode.h" | 7 #include <brotli/decode.h> |
| 8 | 8 |
| 9 #ifdef __ARM_NEON__ | 9 #ifdef __ARM_NEON__ |
| 10 #include <arm_neon.h> | 10 #include <arm_neon.h> |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 #include <stdlib.h> /* free, malloc */ | 13 #include <stdlib.h> /* free, malloc */ |
| 14 #include <string.h> /* memcpy, memset */ | 14 #include <string.h> /* memcpy, memset */ |
| 15 | 15 |
| 16 #include "../common/constants.h" |
| 17 #include "../common/dictionary.h" |
| 18 #include "../common/version.h" |
| 16 #include "./bit_reader.h" | 19 #include "./bit_reader.h" |
| 17 #include "./context.h" | 20 #include "./context.h" |
| 18 #include "./dictionary.h" | |
| 19 #include "./huffman.h" | 21 #include "./huffman.h" |
| 20 #include "./port.h" | 22 #include "./port.h" |
| 21 #include "./prefix.h" | 23 #include "./prefix.h" |
| 22 #include "./state.h" | 24 #include "./state.h" |
| 23 #include "./transform.h" | 25 #include "./transform.h" |
| 24 | 26 |
| 25 #if defined(__cplusplus) || defined(c_plusplus) | 27 #if defined(__cplusplus) || defined(c_plusplus) |
| 26 extern "C" { | 28 extern "C" { |
| 27 #endif | 29 #endif |
| 28 | 30 |
| 29 #define BROTLI_FAILURE(CODE) (BROTLI_DUMP(), CODE) | 31 #define BROTLI_FAILURE(CODE) (BROTLI_DUMP(), CODE) |
| 30 | 32 |
| 31 #define BROTLI_LOG_UINT(name) \ | 33 #define BROTLI_LOG_UINT(name) \ |
| 32 BROTLI_LOG(("[%s] %s = %lu\n", __func__, #name, (unsigned long)(name))) | 34 BROTLI_LOG(("[%s] %s = %lu\n", __func__, #name, (unsigned long)(name))) |
| 33 #define BROTLI_LOG_ARRAY_INDEX(array_name, idx) \ | 35 #define BROTLI_LOG_ARRAY_INDEX(array_name, idx) \ |
| 34 BROTLI_LOG(("[%s] %s[%lu] = %lu\n", __func__, #array_name, \ | 36 BROTLI_LOG(("[%s] %s[%lu] = %lu\n", __func__, #array_name, \ |
| 35 (unsigned long)(idx), (unsigned long)array_name[idx])) | 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 #define HUFFMAN_TABLE_BITS 8U | 39 #define HUFFMAN_TABLE_BITS 8U |
| 46 #define HUFFMAN_TABLE_MASK 0xff | 40 #define HUFFMAN_TABLE_MASK 0xff |
| 47 | 41 |
| 48 #define CODE_LENGTH_CODES 18 | 42 static const uint8_t kCodeLengthCodeOrder[BROTLI_CODE_LENGTH_CODES] = { |
| 49 static const uint8_t kCodeLengthCodeOrder[CODE_LENGTH_CODES] = { | |
| 50 1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15, | 43 1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 51 }; | 44 }; |
| 52 | 45 |
| 53 /* Static prefix code for the complex code length code lengths. */ | 46 /* Static prefix code for the complex code length code lengths. */ |
| 54 static const uint8_t kCodeLengthPrefixLength[16] = { | 47 static const uint8_t kCodeLengthPrefixLength[16] = { |
| 55 2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 3, 2, 2, 2, 4, | 48 2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 3, 2, 2, 2, 4, |
| 56 }; | 49 }; |
| 57 | 50 |
| 58 static const uint8_t kCodeLengthPrefixValue[16] = { | 51 static const uint8_t kCodeLengthPrefixValue[16] = { |
| 59 0, 4, 3, 2, 0, 4, 3, 1, 0, 4, 3, 2, 0, 4, 3, 5, | 52 0, 4, 3, 2, 0, 4, 3, 1, 0, 4, 3, 2, 0, 4, 3, 5, |
| 60 }; | 53 }; |
| 61 | 54 |
| 62 #define NUM_DISTANCE_SHORT_CODES 16 | 55 BrotliDecoderState* BrotliDecoderCreateInstance( |
| 63 | |
| 64 BrotliState* BrotliCreateState( | |
| 65 brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) { | 56 brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) { |
| 66 BrotliState* state = 0; | 57 BrotliDecoderState* state = 0; |
| 67 if (!alloc_func && !free_func) { | 58 if (!alloc_func && !free_func) { |
| 68 state = (BrotliState*)malloc(sizeof(BrotliState)); | 59 state = (BrotliDecoderState*)malloc(sizeof(BrotliDecoderState)); |
| 69 } else if (alloc_func && free_func) { | 60 } else if (alloc_func && free_func) { |
| 70 state = (BrotliState*)alloc_func(opaque, sizeof(BrotliState)); | 61 state = (BrotliDecoderState*)alloc_func(opaque, sizeof(BrotliDecoderState)); |
| 71 } | 62 } |
| 72 if (state == 0) { | 63 if (state == 0) { |
| 73 BROTLI_DUMP(); | 64 BROTLI_DUMP(); |
| 74 return 0; | 65 return 0; |
| 75 } | 66 } |
| 76 BrotliStateInitWithCustomAllocators(state, alloc_func, free_func, opaque); | 67 BrotliDecoderStateInitWithCustomAllocators( |
| 77 state->error_code = BROTLI_NO_ERROR; | 68 state, alloc_func, free_func, opaque); |
| 69 state->error_code = BROTLI_DECODER_NO_ERROR; |
| 78 return state; | 70 return state; |
| 79 } | 71 } |
| 80 | 72 |
| 81 /* Deinitializes and frees BrotliState instance. */ | 73 /* Deinitializes and frees BrotliDecoderState instance. */ |
| 82 void BrotliDestroyState(BrotliState* state) { | 74 void BrotliDecoderDestroyInstance(BrotliDecoderState* state) { |
| 83 if (!state) { | 75 if (!state) { |
| 84 return; | 76 return; |
| 85 } else { | 77 } else { |
| 86 brotli_free_func free_func = state->free_func; | 78 brotli_free_func free_func = state->free_func; |
| 87 void* opaque = state->memory_manager_opaque; | 79 void* opaque = state->memory_manager_opaque; |
| 88 BrotliStateCleanup(state); | 80 BrotliDecoderStateCleanup(state); |
| 89 free_func(opaque, state); | 81 free_func(opaque, state); |
| 90 } | 82 } |
| 91 } | 83 } |
| 92 | 84 |
| 93 /* Saves error code and converts it to BrotliResult */ | 85 /* Saves error code and converts it to BrotliDecoderResult */ |
| 94 static BROTLI_NOINLINE BrotliResult SaveErrorCode( | 86 static BROTLI_NOINLINE BrotliDecoderResult SaveErrorCode( |
| 95 BrotliState* s, BrotliErrorCode e) { | 87 BrotliDecoderState* s, BrotliDecoderErrorCode e) { |
| 96 s->error_code = (int)e; | 88 s->error_code = (int)e; |
| 97 switch (e) { | 89 switch (e) { |
| 98 case BROTLI_SUCCESS: return BROTLI_RESULT_SUCCESS; | 90 case BROTLI_DECODER_SUCCESS: |
| 99 case BROTLI_NEEDS_MORE_INPUT: return BROTLI_RESULT_NEEDS_MORE_INPUT; | 91 return BROTLI_DECODER_RESULT_SUCCESS; |
| 100 case BROTLI_NEEDS_MORE_OUTPUT: return BROTLI_RESULT_NEEDS_MORE_OUTPUT; | 92 case BROTLI_DECODER_NEEDS_MORE_INPUT: |
| 101 default: return BROTLI_RESULT_ERROR; | 93 return BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT; |
| 94 case BROTLI_DECODER_NEEDS_MORE_OUTPUT: |
| 95 return BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT; |
| 96 default: |
| 97 return BROTLI_DECODER_RESULT_ERROR; |
| 102 } | 98 } |
| 103 } | 99 } |
| 104 | 100 |
| 105 /* Decodes a number in the range [9..24], by reading 1 - 7 bits. | 101 /* Decodes a number in the range [9..24], by reading 1 - 7 bits. |
| 106 Precondition: bit-reader accumulator has at least 7 bits. */ | 102 Precondition: bit-reader accumulator has at least 7 bits. */ |
| 107 static uint32_t DecodeWindowBits(BrotliBitReader* br) { | 103 static uint32_t DecodeWindowBits(BrotliBitReader* br) { |
| 108 uint32_t n; | 104 uint32_t n; |
| 109 BrotliTakeBits(br, 1, &n); | 105 BrotliTakeBits(br, 1, &n); |
| 110 if (n == 0) { | 106 if (n == 0) { |
| 111 return 16; | 107 return 16; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 125 #if defined(__ARM_NEON__) | 121 #if defined(__ARM_NEON__) |
| 126 vst1q_u8(dst, vld1q_u8(src)); | 122 vst1q_u8(dst, vld1q_u8(src)); |
| 127 #else | 123 #else |
| 128 uint32_t buffer[4]; | 124 uint32_t buffer[4]; |
| 129 memcpy(buffer, src, 16); | 125 memcpy(buffer, src, 16); |
| 130 memcpy(dst, buffer, 16); | 126 memcpy(dst, buffer, 16); |
| 131 #endif | 127 #endif |
| 132 } | 128 } |
| 133 | 129 |
| 134 /* Decodes a number in the range [0..255], by reading 1 - 11 bits. */ | 130 /* Decodes a number in the range [0..255], by reading 1 - 11 bits. */ |
| 135 static BROTLI_NOINLINE BrotliErrorCode DecodeVarLenUint8(BrotliState* s, | 131 static BROTLI_NOINLINE BrotliDecoderErrorCode DecodeVarLenUint8( |
| 136 BrotliBitReader* br, uint32_t* value) { | 132 BrotliDecoderState* s, BrotliBitReader* br, uint32_t* value) { |
| 137 uint32_t bits; | 133 uint32_t bits; |
| 138 switch (s->substate_decode_uint8) { | 134 switch (s->substate_decode_uint8) { |
| 139 case BROTLI_STATE_DECODE_UINT8_NONE: | 135 case BROTLI_STATE_DECODE_UINT8_NONE: |
| 140 if (PREDICT_FALSE(!BrotliSafeReadBits(br, 1, &bits))) { | 136 if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, 1, &bits))) { |
| 141 return BROTLI_NEEDS_MORE_INPUT; | 137 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 142 } | 138 } |
| 143 if (bits == 0) { | 139 if (bits == 0) { |
| 144 *value = 0; | 140 *value = 0; |
| 145 return BROTLI_SUCCESS; | 141 return BROTLI_DECODER_SUCCESS; |
| 146 } | 142 } |
| 147 /* No break, transit to the next state. */ | 143 /* No break, transit to the next state. */ |
| 148 | 144 |
| 149 case BROTLI_STATE_DECODE_UINT8_SHORT: | 145 case BROTLI_STATE_DECODE_UINT8_SHORT: |
| 150 if (PREDICT_FALSE(!BrotliSafeReadBits(br, 3, &bits))) { | 146 if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, 3, &bits))) { |
| 151 s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_SHORT; | 147 s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_SHORT; |
| 152 return BROTLI_NEEDS_MORE_INPUT; | 148 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 153 } | 149 } |
| 154 if (bits == 0) { | 150 if (bits == 0) { |
| 155 *value = 1; | 151 *value = 1; |
| 156 s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE; | 152 s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE; |
| 157 return BROTLI_SUCCESS; | 153 return BROTLI_DECODER_SUCCESS; |
| 158 } | 154 } |
| 159 /* Use output value as a temporary storage. It MUST be persisted. */ | 155 /* Use output value as a temporary storage. It MUST be persisted. */ |
| 160 *value = bits; | 156 *value = bits; |
| 161 /* No break, transit to the next state. */ | 157 /* No break, transit to the next state. */ |
| 162 | 158 |
| 163 case BROTLI_STATE_DECODE_UINT8_LONG: | 159 case BROTLI_STATE_DECODE_UINT8_LONG: |
| 164 if (PREDICT_FALSE(!BrotliSafeReadBits(br, *value, &bits))) { | 160 if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, *value, &bits))) { |
| 165 s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_LONG; | 161 s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_LONG; |
| 166 return BROTLI_NEEDS_MORE_INPUT; | 162 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 167 } | 163 } |
| 168 *value = (1U << *value) + bits; | 164 *value = (1U << *value) + bits; |
| 169 s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE; | 165 s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE; |
| 170 return BROTLI_SUCCESS; | 166 return BROTLI_DECODER_SUCCESS; |
| 171 | 167 |
| 172 default: | 168 default: |
| 173 return BROTLI_FAILURE(BROTLI_ERROR_UNREACHABLE_1); | 169 return |
| 170 BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); |
| 174 } | 171 } |
| 175 } | 172 } |
| 176 | 173 |
| 177 /* Decodes a metablock length and flags by reading 2 - 31 bits. */ | 174 /* Decodes a metablock length and flags by reading 2 - 31 bits. */ |
| 178 static BrotliErrorCode BROTLI_NOINLINE DecodeMetaBlockLength( | 175 static BrotliDecoderErrorCode BROTLI_NOINLINE DecodeMetaBlockLength( |
| 179 BrotliState* s, BrotliBitReader* br) { | 176 BrotliDecoderState* s, BrotliBitReader* br) { |
| 180 uint32_t bits; | 177 uint32_t bits; |
| 181 int i; | 178 int i; |
| 182 for (;;) { | 179 for (;;) { |
| 183 switch (s->substate_metablock_header) { | 180 switch (s->substate_metablock_header) { |
| 184 case BROTLI_STATE_METABLOCK_HEADER_NONE: | 181 case BROTLI_STATE_METABLOCK_HEADER_NONE: |
| 185 if (!BrotliSafeReadBits(br, 1, &bits)) { | 182 if (!BrotliSafeReadBits(br, 1, &bits)) { |
| 186 return BROTLI_NEEDS_MORE_INPUT; | 183 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 187 } | 184 } |
| 188 s->is_last_metablock = (uint8_t)bits; | 185 s->is_last_metablock = bits ? 1 : 0; |
| 189 s->meta_block_remaining_len = 0; | 186 s->meta_block_remaining_len = 0; |
| 190 s->is_uncompressed = 0; | 187 s->is_uncompressed = 0; |
| 191 s->is_metadata = 0; | 188 s->is_metadata = 0; |
| 192 if (!s->is_last_metablock) { | 189 if (!s->is_last_metablock) { |
| 193 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NIBBLES; | 190 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NIBBLES; |
| 194 break; | 191 break; |
| 195 } | 192 } |
| 196 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_EMPTY; | 193 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_EMPTY; |
| 197 /* No break, transit to the next state. */ | 194 /* No break, transit to the next state. */ |
| 198 | 195 |
| 199 case BROTLI_STATE_METABLOCK_HEADER_EMPTY: | 196 case BROTLI_STATE_METABLOCK_HEADER_EMPTY: |
| 200 if (!BrotliSafeReadBits(br, 1, &bits)) { | 197 if (!BrotliSafeReadBits(br, 1, &bits)) { |
| 201 return BROTLI_NEEDS_MORE_INPUT; | 198 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 202 } | 199 } |
| 203 if (bits) { | 200 if (bits) { |
| 204 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; | 201 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; |
| 205 return BROTLI_SUCCESS; | 202 return BROTLI_DECODER_SUCCESS; |
| 206 } | 203 } |
| 207 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NIBBLES; | 204 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NIBBLES; |
| 208 /* No break, transit to the next state. */ | 205 /* No break, transit to the next state. */ |
| 209 | 206 |
| 210 case BROTLI_STATE_METABLOCK_HEADER_NIBBLES: | 207 case BROTLI_STATE_METABLOCK_HEADER_NIBBLES: |
| 211 if (!BrotliSafeReadBits(br, 2, &bits)) { | 208 if (!BrotliSafeReadBits(br, 2, &bits)) { |
| 212 return BROTLI_NEEDS_MORE_INPUT; | 209 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 213 } | 210 } |
| 214 s->size_nibbles = (uint8_t)(bits + 4); | 211 s->size_nibbles = (uint8_t)(bits + 4); |
| 215 s->loop_counter = 0; | 212 s->loop_counter = 0; |
| 216 if (bits == 3) { | 213 if (bits == 3) { |
| 217 s->is_metadata = 1; | 214 s->is_metadata = 1; |
| 218 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_RESERVED; | 215 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_RESERVED; |
| 219 break; | 216 break; |
| 220 } | 217 } |
| 221 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_SIZE; | 218 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_SIZE; |
| 222 /* No break, transit to the next state. */ | 219 /* No break, transit to the next state. */ |
| 223 | 220 |
| 224 case BROTLI_STATE_METABLOCK_HEADER_SIZE: | 221 case BROTLI_STATE_METABLOCK_HEADER_SIZE: |
| 225 i = s->loop_counter; | 222 i = s->loop_counter; |
| 226 for (; i < s->size_nibbles; ++i) { | 223 for (; i < (int)s->size_nibbles; ++i) { |
| 227 if (!BrotliSafeReadBits(br, 4, &bits)) { | 224 if (!BrotliSafeReadBits(br, 4, &bits)) { |
| 228 s->loop_counter = i; | 225 s->loop_counter = i; |
| 229 return BROTLI_NEEDS_MORE_INPUT; | 226 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 230 } | 227 } |
| 231 if (i + 1 == s->size_nibbles && s->size_nibbles > 4 && bits == 0) { | 228 if (i + 1 == s->size_nibbles && s->size_nibbles > 4 && bits == 0) { |
| 232 return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_EXUBERANT_NIBBLE); | 229 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE); |
| 233 } | 230 } |
| 234 s->meta_block_remaining_len |= (int)(bits << (i * 4)); | 231 s->meta_block_remaining_len |= (int)(bits << (i * 4)); |
| 235 } | 232 } |
| 236 s->substate_metablock_header = | 233 s->substate_metablock_header = |
| 237 BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED; | 234 BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED; |
| 238 /* No break, transit to the next state. */ | 235 /* No break, transit to the next state. */ |
| 239 | 236 |
| 240 case BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED: | 237 case BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED: |
| 241 if (!s->is_last_metablock) { | 238 if (!s->is_last_metablock) { |
| 242 if (!BrotliSafeReadBits(br, 1, &bits)) { | 239 if (!BrotliSafeReadBits(br, 1, &bits)) { |
| 243 return BROTLI_NEEDS_MORE_INPUT; | 240 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 244 } | 241 } |
| 245 s->is_uncompressed = (uint8_t)bits; | 242 s->is_uncompressed = bits ? 1 : 0; |
| 246 } | 243 } |
| 247 ++s->meta_block_remaining_len; | 244 ++s->meta_block_remaining_len; |
| 248 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; | 245 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; |
| 249 return BROTLI_SUCCESS; | 246 return BROTLI_DECODER_SUCCESS; |
| 250 | 247 |
| 251 case BROTLI_STATE_METABLOCK_HEADER_RESERVED: | 248 case BROTLI_STATE_METABLOCK_HEADER_RESERVED: |
| 252 if (!BrotliSafeReadBits(br, 1, &bits)) { | 249 if (!BrotliSafeReadBits(br, 1, &bits)) { |
| 253 return BROTLI_NEEDS_MORE_INPUT; | 250 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 254 } | 251 } |
| 255 if (bits != 0) { | 252 if (bits != 0) { |
| 256 return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_RESERVED); | 253 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_RESERVED); |
| 257 } | 254 } |
| 258 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_BYTES; | 255 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_BYTES; |
| 259 /* No break, transit to the next state. */ | 256 /* No break, transit to the next state. */ |
| 260 | 257 |
| 261 case BROTLI_STATE_METABLOCK_HEADER_BYTES: | 258 case BROTLI_STATE_METABLOCK_HEADER_BYTES: |
| 262 if (!BrotliSafeReadBits(br, 2, &bits)) { | 259 if (!BrotliSafeReadBits(br, 2, &bits)) { |
| 263 return BROTLI_NEEDS_MORE_INPUT; | 260 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 264 } | 261 } |
| 265 if (bits == 0) { | 262 if (bits == 0) { |
| 266 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; | 263 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; |
| 267 return BROTLI_SUCCESS; | 264 return BROTLI_DECODER_SUCCESS; |
| 268 } | 265 } |
| 269 s->size_nibbles = (uint8_t)bits; | 266 s->size_nibbles = (uint8_t)bits; |
| 270 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_METADATA; | 267 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_METADATA; |
| 271 /* No break, transit to the next state. */ | 268 /* No break, transit to the next state. */ |
| 272 | 269 |
| 273 case BROTLI_STATE_METABLOCK_HEADER_METADATA: | 270 case BROTLI_STATE_METABLOCK_HEADER_METADATA: |
| 274 i = s->loop_counter; | 271 i = s->loop_counter; |
| 275 for (; i < s->size_nibbles; ++i) { | 272 for (; i < (int)s->size_nibbles; ++i) { |
| 276 if (!BrotliSafeReadBits(br, 8, &bits)) { | 273 if (!BrotliSafeReadBits(br, 8, &bits)) { |
| 277 s->loop_counter = i; | 274 s->loop_counter = i; |
| 278 return BROTLI_NEEDS_MORE_INPUT; | 275 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 279 } | 276 } |
| 280 if (i + 1 == s->size_nibbles && s->size_nibbles > 1 && bits == 0) { | 277 if (i + 1 == s->size_nibbles && s->size_nibbles > 1 && bits == 0) { |
| 281 return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_EXUBERANT_META_NIBBLE); | 278 return BROTLI_FAILURE( |
| 279 BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE); |
| 282 } | 280 } |
| 283 s->meta_block_remaining_len |= (int)(bits << (i * 8)); | 281 s->meta_block_remaining_len |= (int)(bits << (i * 8)); |
| 284 } | 282 } |
| 285 ++s->meta_block_remaining_len; | 283 ++s->meta_block_remaining_len; |
| 286 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; | 284 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE; |
| 287 return BROTLI_SUCCESS; | 285 return BROTLI_DECODER_SUCCESS; |
| 288 | 286 |
| 289 default: | 287 default: |
| 290 return BROTLI_FAILURE(BROTLI_ERROR_UNREACHABLE_2); | 288 return |
| 289 BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); |
| 291 } | 290 } |
| 292 } | 291 } |
| 293 } | 292 } |
| 294 | 293 |
| 295 /* Decodes the Huffman code. | 294 /* Decodes the Huffman code. |
| 296 This method doesn't read data from the bit reader, BUT drops the amount of | 295 This method doesn't read data from the bit reader, BUT drops the amount of |
| 297 bits that correspond to the decoded symbol. | 296 bits that correspond to the decoded symbol. |
| 298 bits MUST contain at least 15 (BROTLI_HUFFMAN_MAX_CODE_LENGTH) valid bits. */ | 297 bits MUST contain at least 15 (BROTLI_HUFFMAN_MAX_CODE_LENGTH) valid bits. */ |
| 299 static BROTLI_INLINE uint32_t DecodeSymbol(uint32_t bits, | 298 static BROTLI_INLINE uint32_t DecodeSymbol(uint32_t bits, |
| 300 const HuffmanCode* table, | 299 const HuffmanCode* table, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 312 | 311 |
| 313 /* Reads and decodes the next Huffman code from bit-stream. | 312 /* Reads and decodes the next Huffman code from bit-stream. |
| 314 This method peeks 16 bits of input and drops 0 - 15 of them. */ | 313 This method peeks 16 bits of input and drops 0 - 15 of them. */ |
| 315 static BROTLI_INLINE uint32_t ReadSymbol(const HuffmanCode* table, | 314 static BROTLI_INLINE uint32_t ReadSymbol(const HuffmanCode* table, |
| 316 BrotliBitReader* br) { | 315 BrotliBitReader* br) { |
| 317 return DecodeSymbol(BrotliGet16BitsUnmasked(br), table, br); | 316 return DecodeSymbol(BrotliGet16BitsUnmasked(br), table, br); |
| 318 } | 317 } |
| 319 | 318 |
| 320 /* Same as DecodeSymbol, but it is known that there is less than 15 bits of | 319 /* Same as DecodeSymbol, but it is known that there is less than 15 bits of |
| 321 input are currently available. */ | 320 input are currently available. */ |
| 322 static BROTLI_NOINLINE int SafeDecodeSymbol(const HuffmanCode* table, | 321 static BROTLI_NOINLINE BROTLI_BOOL SafeDecodeSymbol( |
| 323 BrotliBitReader* br, | 322 const HuffmanCode* table, BrotliBitReader* br, uint32_t* result) { |
| 324 uint32_t* result) { | |
| 325 uint32_t val; | 323 uint32_t val; |
| 326 uint32_t available_bits = BrotliGetAvailableBits(br); | 324 uint32_t available_bits = BrotliGetAvailableBits(br); |
| 327 if (available_bits == 0) { | 325 if (available_bits == 0) { |
| 328 if (table->bits == 0) { | 326 if (table->bits == 0) { |
| 329 *result = table->value; | 327 *result = table->value; |
| 330 return 1; | 328 return BROTLI_TRUE; |
| 331 } | 329 } |
| 332 return 0; /* No valid bits at all. */ | 330 return BROTLI_FALSE; /* No valid bits at all. */ |
| 333 } | 331 } |
| 334 val = (uint32_t)BrotliGetBitsUnmasked(br); | 332 val = (uint32_t)BrotliGetBitsUnmasked(br); |
| 335 table += val & HUFFMAN_TABLE_MASK; | 333 table += val & HUFFMAN_TABLE_MASK; |
| 336 if (table->bits <= HUFFMAN_TABLE_BITS) { | 334 if (table->bits <= HUFFMAN_TABLE_BITS) { |
| 337 if (table->bits <= available_bits) { | 335 if (table->bits <= available_bits) { |
| 338 BrotliDropBits(br, table->bits); | 336 BrotliDropBits(br, table->bits); |
| 339 *result = table->value; | 337 *result = table->value; |
| 340 return 1; | 338 return BROTLI_TRUE; |
| 341 } else { | 339 } else { |
| 342 return 0; /* Not enough bits for the first level. */ | 340 return BROTLI_FALSE; /* Not enough bits for the first level. */ |
| 343 } | 341 } |
| 344 } | 342 } |
| 345 if (available_bits <= HUFFMAN_TABLE_BITS) { | 343 if (available_bits <= HUFFMAN_TABLE_BITS) { |
| 346 return 0; /* Not enough bits to move to the second level. */ | 344 return BROTLI_FALSE; /* Not enough bits to move to the second level. */ |
| 347 } | 345 } |
| 348 | 346 |
| 349 /* Speculatively drop HUFFMAN_TABLE_BITS. */ | 347 /* Speculatively drop HUFFMAN_TABLE_BITS. */ |
| 350 val = (val & BitMask(table->bits)) >> HUFFMAN_TABLE_BITS; | 348 val = (val & BitMask(table->bits)) >> HUFFMAN_TABLE_BITS; |
| 351 available_bits -= HUFFMAN_TABLE_BITS; | 349 available_bits -= HUFFMAN_TABLE_BITS; |
| 352 table += table->value + val; | 350 table += table->value + val; |
| 353 if (available_bits < table->bits) { | 351 if (available_bits < table->bits) { |
| 354 return 0; /* Not enough bits for the second level. */ | 352 return BROTLI_FALSE; /* Not enough bits for the second level. */ |
| 355 } | 353 } |
| 356 | 354 |
| 357 BrotliDropBits(br, HUFFMAN_TABLE_BITS + table->bits); | 355 BrotliDropBits(br, HUFFMAN_TABLE_BITS + table->bits); |
| 358 *result = table->value; | 356 *result = table->value; |
| 359 return 1; | 357 return BROTLI_TRUE; |
| 360 } | 358 } |
| 361 | 359 |
| 362 static BROTLI_INLINE int SafeReadSymbol(const HuffmanCode* table, | 360 static BROTLI_INLINE BROTLI_BOOL SafeReadSymbol( |
| 363 BrotliBitReader* br, | 361 const HuffmanCode* table, BrotliBitReader* br, uint32_t* result) { |
| 364 uint32_t* result) { | |
| 365 uint32_t val; | 362 uint32_t val; |
| 366 if (PREDICT_TRUE(BrotliSafeGetBits(br, 15, &val))) { | 363 if (BROTLI_PREDICT_TRUE(BrotliSafeGetBits(br, 15, &val))) { |
| 367 *result = DecodeSymbol(val, table, br); | 364 *result = DecodeSymbol(val, table, br); |
| 368 return 1; | 365 return BROTLI_TRUE; |
| 369 } | 366 } |
| 370 return SafeDecodeSymbol(table, br, result); | 367 return SafeDecodeSymbol(table, br, result); |
| 371 } | 368 } |
| 372 | 369 |
| 373 /* Makes a look-up in first level Huffman table. Peeks 8 bits. */ | 370 /* Makes a look-up in first level Huffman table. Peeks 8 bits. */ |
| 374 static BROTLI_INLINE void PreloadSymbol(int safe, | 371 static BROTLI_INLINE void PreloadSymbol(int safe, |
| 375 const HuffmanCode* table, | 372 const HuffmanCode* table, |
| 376 BrotliBitReader* br, | 373 BrotliBitReader* br, |
| 377 uint32_t* bits, | 374 uint32_t* bits, |
| 378 uint32_t* value) { | 375 uint32_t* value) { |
| 379 if (safe) { | 376 if (safe) { |
| 380 return; | 377 return; |
| 381 } | 378 } |
| 382 table += BrotliGetBits(br, HUFFMAN_TABLE_BITS); | 379 table += BrotliGetBits(br, HUFFMAN_TABLE_BITS); |
| 383 *bits = table->bits; | 380 *bits = table->bits; |
| 384 *value = table->value; | 381 *value = table->value; |
| 385 } | 382 } |
| 386 | 383 |
| 387 /* Decodes the next Huffman code using data prepared by PreloadSymbol. | 384 /* Decodes the next Huffman code using data prepared by PreloadSymbol. |
| 388 Reads 0 - 15 bits. Also peeks 8 following bits. */ | 385 Reads 0 - 15 bits. Also peeks 8 following bits. */ |
| 389 static BROTLI_INLINE uint32_t ReadPreloadedSymbol(const HuffmanCode* table, | 386 static BROTLI_INLINE uint32_t ReadPreloadedSymbol(const HuffmanCode* table, |
| 390 BrotliBitReader* br, | 387 BrotliBitReader* br, |
| 391 uint32_t* bits, | 388 uint32_t* bits, |
| 392 uint32_t* value) { | 389 uint32_t* value) { |
| 393 uint32_t result = *value; | 390 uint32_t result = *value; |
| 394 if (PREDICT_FALSE(*bits > HUFFMAN_TABLE_BITS)) { | 391 if (BROTLI_PREDICT_FALSE(*bits > HUFFMAN_TABLE_BITS)) { |
| 395 uint32_t val = BrotliGet16BitsUnmasked(br); | 392 uint32_t val = BrotliGet16BitsUnmasked(br); |
| 396 const HuffmanCode* ext = table + (val & HUFFMAN_TABLE_MASK) + *value; | 393 const HuffmanCode* ext = table + (val & HUFFMAN_TABLE_MASK) + *value; |
| 397 uint32_t mask = BitMask((*bits - HUFFMAN_TABLE_BITS)); | 394 uint32_t mask = BitMask((*bits - HUFFMAN_TABLE_BITS)); |
| 398 BrotliDropBits(br, HUFFMAN_TABLE_BITS); | 395 BrotliDropBits(br, HUFFMAN_TABLE_BITS); |
| 399 ext += (val >> HUFFMAN_TABLE_BITS) & mask; | 396 ext += (val >> HUFFMAN_TABLE_BITS) & mask; |
| 400 BrotliDropBits(br, ext->bits); | 397 BrotliDropBits(br, ext->bits); |
| 401 result = ext->value; | 398 result = ext->value; |
| 402 } else { | 399 } else { |
| 403 BrotliDropBits(br, *bits); | 400 BrotliDropBits(br, *bits); |
| 404 } | 401 } |
| 405 PreloadSymbol(0, table, br, bits, value); | 402 PreloadSymbol(0, table, br, bits, value); |
| 406 return result; | 403 return result; |
| 407 } | 404 } |
| 408 | 405 |
| 409 static BROTLI_INLINE uint32_t Log2Floor(uint32_t x) { | 406 static BROTLI_INLINE uint32_t Log2Floor(uint32_t x) { |
| 410 uint32_t result = 0; | 407 uint32_t result = 0; |
| 411 while (x) { | 408 while (x) { |
| 412 x >>= 1; | 409 x >>= 1; |
| 413 ++result; | 410 ++result; |
| 414 } | 411 } |
| 415 return result; | 412 return result; |
| 416 } | 413 } |
| 417 | 414 |
| 418 /* Reads (s->symbol + 1) symbols. | 415 /* Reads (s->symbol + 1) symbols. |
| 419 Totally 1..4 symbols are read, 1..10 bits each. | 416 Totally 1..4 symbols are read, 1..10 bits each. |
| 420 The list of symbols MUST NOT contain duplicates. | 417 The list of symbols MUST NOT contain duplicates. |
| 421 */ | 418 */ |
| 422 static BrotliErrorCode ReadSimpleHuffmanSymbols(uint32_t alphabet_size, | 419 static BrotliDecoderErrorCode ReadSimpleHuffmanSymbols( |
| 423 BrotliState* s) { | 420 uint32_t alphabet_size, BrotliDecoderState* s) { |
| 424 /* max_bits == 1..10; symbol == 0..3; 1..40 bits will be read. */ | 421 /* max_bits == 1..10; symbol == 0..3; 1..40 bits will be read. */ |
| 425 BrotliBitReader* br = &s->br; | 422 BrotliBitReader* br = &s->br; |
| 426 uint32_t max_bits = Log2Floor(alphabet_size - 1); | 423 uint32_t max_bits = Log2Floor(alphabet_size - 1); |
| 427 uint32_t i = s->sub_loop_counter; | 424 uint32_t i = s->sub_loop_counter; |
| 428 uint32_t num_symbols = s->symbol; | 425 uint32_t num_symbols = s->symbol; |
| 429 while (i <= num_symbols) { | 426 while (i <= num_symbols) { |
| 430 uint32_t v; | 427 uint32_t v; |
| 431 if (PREDICT_FALSE(!BrotliSafeReadBits(br, max_bits, &v))) { | 428 if (BROTLI_PREDICT_FALSE(!BrotliSafeReadBits(br, max_bits, &v))) { |
| 432 s->sub_loop_counter = i; | 429 s->sub_loop_counter = i; |
| 433 s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_READ; | 430 s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_READ; |
| 434 return BROTLI_NEEDS_MORE_INPUT; | 431 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 435 } | 432 } |
| 436 if (v >= alphabet_size) { | 433 if (v >= alphabet_size) { |
| 437 return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET); | 434 return |
| 435 BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET); |
| 438 } | 436 } |
| 439 s->symbols_lists_array[i] = (uint16_t)v; | 437 s->symbols_lists_array[i] = (uint16_t)v; |
| 440 BROTLI_LOG_UINT(s->symbols_lists_array[i]); | 438 BROTLI_LOG_UINT(s->symbols_lists_array[i]); |
| 441 ++i; | 439 ++i; |
| 442 } | 440 } |
| 443 | 441 |
| 444 for (i = 0; i < num_symbols; ++i) { | 442 for (i = 0; i < num_symbols; ++i) { |
| 445 uint32_t k = i + 1; | 443 uint32_t k = i + 1; |
| 446 for (; k <= num_symbols; ++k) { | 444 for (; k <= num_symbols; ++k) { |
| 447 if (s->symbols_lists_array[i] == s->symbols_lists_array[k]) { | 445 if (s->symbols_lists_array[i] == s->symbols_lists_array[k]) { |
| 448 return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME); | 446 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME); |
| 449 } | 447 } |
| 450 } | 448 } |
| 451 } | 449 } |
| 452 | 450 |
| 453 return BROTLI_SUCCESS; | 451 return BROTLI_DECODER_SUCCESS; |
| 454 } | 452 } |
| 455 | 453 |
| 456 /* Process single decoded symbol code length: | 454 /* Process single decoded symbol code length: |
| 457 A) reset the repeat variable | 455 A) reset the repeat variable |
| 458 B) remember code length (if it is not 0) | 456 B) remember code length (if it is not 0) |
| 459 C) extend corredponding index-chain | 457 C) extend corresponding index-chain |
| 460 D) reduce the huffman space | 458 D) reduce the Huffman space |
| 461 E) update the histogram | 459 E) update the histogram |
| 462 */ | 460 */ |
| 463 static BROTLI_INLINE void ProcessSingleCodeLength(uint32_t code_len, | 461 static BROTLI_INLINE void ProcessSingleCodeLength(uint32_t code_len, |
| 464 uint32_t* symbol, uint32_t* repeat, uint32_t* space, | 462 uint32_t* symbol, uint32_t* repeat, uint32_t* space, |
| 465 uint32_t* prev_code_len, uint16_t* symbol_lists, | 463 uint32_t* prev_code_len, uint16_t* symbol_lists, |
| 466 uint16_t* code_length_histo, int* next_symbol) { | 464 uint16_t* code_length_histo, int* next_symbol) { |
| 467 *repeat = 0; | 465 *repeat = 0; |
| 468 if (code_len != 0) { /* code_len == 1..15 */ | 466 if (code_len != 0) { /* code_len == 1..15 */ |
| 469 symbol_lists[next_symbol[code_len]] = (uint16_t)(*symbol); | 467 symbol_lists[next_symbol[code_len]] = (uint16_t)(*symbol); |
| 470 next_symbol[code_len] = (int)(*symbol); | 468 next_symbol[code_len] = (int)(*symbol); |
| 471 *prev_code_len = code_len; | 469 *prev_code_len = code_len; |
| 472 *space -= 32768U >> code_len; | 470 *space -= 32768U >> code_len; |
| 473 code_length_histo[code_len]++; | 471 code_length_histo[code_len]++; |
| 474 BROTLI_LOG(("[ReadHuffmanCode] code_length[%d] = %d\n", *symbol, code_len)); | 472 BROTLI_LOG(("[ReadHuffmanCode] code_length[%d] = %d\n", *symbol, code_len)); |
| 475 } | 473 } |
| 476 (*symbol)++; | 474 (*symbol)++; |
| 477 } | 475 } |
| 478 | 476 |
| 479 /* Process repeated symbol code length. | 477 /* Process repeated symbol code length. |
| 480 A) Check if it is the extension of previous repeat sequence; if the decoded | 478 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 | 479 value is not BROTLI_REPEAT_PREVIOUS_CODE_LENGTH, then it is a new |
| 480 symbol-skip |
| 482 B) Update repeat variable | 481 B) Update repeat variable |
| 483 C) Check if operation is feasible (fits alphapet) | 482 C) Check if operation is feasible (fits alphabet) |
| 484 D) For each symbol do the same operations as in ProcessSingleCodeLength | 483 D) For each symbol do the same operations as in ProcessSingleCodeLength |
| 485 | 484 |
| 486 PRECONDITION: code_len == kCodeLengthRepeatCode or kCodeLengthRepeatCode + 1 | 485 PRECONDITION: code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH or |
| 486 code_len == BROTLI_REPEAT_ZERO_CODE_LENGTH |
| 487 */ | 487 */ |
| 488 static BROTLI_INLINE void ProcessRepeatedCodeLength(uint32_t code_len, | 488 static BROTLI_INLINE void ProcessRepeatedCodeLength(uint32_t code_len, |
| 489 uint32_t repeat_delta, uint32_t alphabet_size, uint32_t* symbol, | 489 uint32_t repeat_delta, uint32_t alphabet_size, uint32_t* symbol, |
| 490 uint32_t* repeat, uint32_t* space, uint32_t* prev_code_len, | 490 uint32_t* repeat, uint32_t* space, uint32_t* prev_code_len, |
| 491 uint32_t* repeat_code_len, uint16_t* symbol_lists, | 491 uint32_t* repeat_code_len, uint16_t* symbol_lists, |
| 492 uint16_t* code_length_histo, int* next_symbol) { | 492 uint16_t* code_length_histo, int* next_symbol) { |
| 493 uint32_t old_repeat; | 493 uint32_t old_repeat; |
| 494 uint32_t new_len = 0; | 494 uint32_t extra_bits = 3; /* for BROTLI_REPEAT_ZERO_CODE_LENGTH */ |
| 495 if (code_len == kCodeLengthRepeatCode) { | 495 uint32_t new_len = 0; /* for BROTLI_REPEAT_ZERO_CODE_LENGTH */ |
| 496 if (code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) { |
| 496 new_len = *prev_code_len; | 497 new_len = *prev_code_len; |
| 498 extra_bits = 2; |
| 497 } | 499 } |
| 498 if (*repeat_code_len != new_len) { | 500 if (*repeat_code_len != new_len) { |
| 499 *repeat = 0; | 501 *repeat = 0; |
| 500 *repeat_code_len = new_len; | 502 *repeat_code_len = new_len; |
| 501 } | 503 } |
| 502 old_repeat = *repeat; | 504 old_repeat = *repeat; |
| 503 if (*repeat > 0) { | 505 if (*repeat > 0) { |
| 504 *repeat -= 2; | 506 *repeat -= 2; |
| 505 *repeat <<= code_len - 14U; | 507 *repeat <<= extra_bits; |
| 506 } | 508 } |
| 507 *repeat += repeat_delta + 3U; | 509 *repeat += repeat_delta + 3U; |
| 508 repeat_delta = *repeat - old_repeat; | 510 repeat_delta = *repeat - old_repeat; |
| 509 if (*symbol + repeat_delta > alphabet_size) { | 511 if (*symbol + repeat_delta > alphabet_size) { |
| 510 BROTLI_DUMP(); | 512 BROTLI_DUMP(); |
| 511 *symbol = alphabet_size; | 513 *symbol = alphabet_size; |
| 512 *space = 0xFFFFF; | 514 *space = 0xFFFFF; |
| 513 return; | 515 return; |
| 514 } | 516 } |
| 515 BROTLI_LOG(("[ReadHuffmanCode] code_length[%d..%d] = %d\n", | 517 BROTLI_LOG(("[ReadHuffmanCode] code_length[%d..%d] = %d\n", |
| 516 *symbol, *symbol + repeat_delta - 1, *repeat_code_len)); | 518 *symbol, *symbol + repeat_delta - 1, *repeat_code_len)); |
| 517 if (*repeat_code_len != 0) { | 519 if (*repeat_code_len != 0) { |
| 518 unsigned last = *symbol + repeat_delta; | 520 unsigned last = *symbol + repeat_delta; |
| 519 int next = next_symbol[*repeat_code_len]; | 521 int next = next_symbol[*repeat_code_len]; |
| 520 do { | 522 do { |
| 521 symbol_lists[next] = (uint16_t)*symbol; | 523 symbol_lists[next] = (uint16_t)*symbol; |
| 522 next = (int)*symbol; | 524 next = (int)*symbol; |
| 523 } while (++(*symbol) != last); | 525 } while (++(*symbol) != last); |
| 524 next_symbol[*repeat_code_len] = next; | 526 next_symbol[*repeat_code_len] = next; |
| 525 *space -= repeat_delta << (15 - *repeat_code_len); | 527 *space -= repeat_delta << (15 - *repeat_code_len); |
| 526 code_length_histo[*repeat_code_len] = | 528 code_length_histo[*repeat_code_len] = |
| 527 (uint16_t)(code_length_histo[*repeat_code_len] + repeat_delta); | 529 (uint16_t)(code_length_histo[*repeat_code_len] + repeat_delta); |
| 528 } else { | 530 } else { |
| 529 *symbol += repeat_delta; | 531 *symbol += repeat_delta; |
| 530 } | 532 } |
| 531 } | 533 } |
| 532 | 534 |
| 533 /* Reads and decodes symbol codelengths. */ | 535 /* Reads and decodes symbol codelengths. */ |
| 534 static BrotliErrorCode ReadSymbolCodeLengths( | 536 static BrotliDecoderErrorCode ReadSymbolCodeLengths( |
| 535 uint32_t alphabet_size, BrotliState* s) { | 537 uint32_t alphabet_size, BrotliDecoderState* s) { |
| 536 BrotliBitReader* br = &s->br; | 538 BrotliBitReader* br = &s->br; |
| 537 uint32_t symbol = s->symbol; | 539 uint32_t symbol = s->symbol; |
| 538 uint32_t repeat = s->repeat; | 540 uint32_t repeat = s->repeat; |
| 539 uint32_t space = s->space; | 541 uint32_t space = s->space; |
| 540 uint32_t prev_code_len = s->prev_code_len; | 542 uint32_t prev_code_len = s->prev_code_len; |
| 541 uint32_t repeat_code_len = s->repeat_code_len; | 543 uint32_t repeat_code_len = s->repeat_code_len; |
| 542 uint16_t* symbol_lists = s->symbol_lists; | 544 uint16_t* symbol_lists = s->symbol_lists; |
| 543 uint16_t* code_length_histo = s->code_length_histo; | 545 uint16_t* code_length_histo = s->code_length_histo; |
| 544 int* next_symbol = s->next_symbol; | 546 int* next_symbol = s->next_symbol; |
| 545 if (!BrotliWarmupBitReader(br)) { | 547 if (!BrotliWarmupBitReader(br)) { |
| 546 return BROTLI_NEEDS_MORE_INPUT; | 548 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 547 } | 549 } |
| 548 while (symbol < alphabet_size && space > 0) { | 550 while (symbol < alphabet_size && space > 0) { |
| 549 const HuffmanCode* p = s->table; | 551 const HuffmanCode* p = s->table; |
| 550 uint32_t code_len; | 552 uint32_t code_len; |
| 551 if (!BrotliCheckInputAmount(br, BROTLI_SHORT_FILL_BIT_WINDOW_READ)) { | 553 if (!BrotliCheckInputAmount(br, BROTLI_SHORT_FILL_BIT_WINDOW_READ)) { |
| 552 s->symbol = symbol; | 554 s->symbol = symbol; |
| 553 s->repeat = repeat; | 555 s->repeat = repeat; |
| 554 s->prev_code_len = prev_code_len; | 556 s->prev_code_len = prev_code_len; |
| 555 s->repeat_code_len = repeat_code_len; | 557 s->repeat_code_len = repeat_code_len; |
| 556 s->space = space; | 558 s->space = space; |
| 557 return BROTLI_NEEDS_MORE_INPUT; | 559 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 558 } | 560 } |
| 559 BrotliFillBitWindow16(br); | 561 BrotliFillBitWindow16(br); |
| 560 p += BrotliGetBitsUnmasked(br) & | 562 p += BrotliGetBitsUnmasked(br) & |
| 561 BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH); | 563 BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH); |
| 562 BrotliDropBits(br, p->bits); /* Use 1..5 bits */ | 564 BrotliDropBits(br, p->bits); /* Use 1..5 bits */ |
| 563 code_len = p->value; /* code_len == 0..17 */ | 565 code_len = p->value; /* code_len == 0..17 */ |
| 564 if (code_len < kCodeLengthRepeatCode) { | 566 if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) { |
| 565 ProcessSingleCodeLength(code_len, &symbol, &repeat, &space, | 567 ProcessSingleCodeLength(code_len, &symbol, &repeat, &space, |
| 566 &prev_code_len, symbol_lists, code_length_histo, next_symbol); | 568 &prev_code_len, symbol_lists, code_length_histo, next_symbol); |
| 567 } else { /* code_len == 16..17, extra_bits == 2..3 */ | 569 } else { /* code_len == 16..17, extra_bits == 2..3 */ |
| 570 uint32_t extra_bits = |
| 571 (code_len == BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) ? 2 : 3; |
| 568 uint32_t repeat_delta = | 572 uint32_t repeat_delta = |
| 569 (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(code_len - 14U); | 573 (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(extra_bits); |
| 570 BrotliDropBits(br, code_len - 14U); | 574 BrotliDropBits(br, extra_bits); |
| 571 ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size, | 575 ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size, |
| 572 &symbol, &repeat, &space, &prev_code_len, &repeat_code_len, | 576 &symbol, &repeat, &space, &prev_code_len, &repeat_code_len, |
| 573 symbol_lists, code_length_histo, next_symbol); | 577 symbol_lists, code_length_histo, next_symbol); |
| 574 } | 578 } |
| 575 } | 579 } |
| 576 s->space = space; | 580 s->space = space; |
| 577 return BROTLI_SUCCESS; | 581 return BROTLI_DECODER_SUCCESS; |
| 578 } | 582 } |
| 579 | 583 |
| 580 static BrotliErrorCode SafeReadSymbolCodeLengths( | 584 static BrotliDecoderErrorCode SafeReadSymbolCodeLengths( |
| 581 uint32_t alphabet_size, BrotliState* s) { | 585 uint32_t alphabet_size, BrotliDecoderState* s) { |
| 582 BrotliBitReader* br = &s->br; | 586 BrotliBitReader* br = &s->br; |
| 583 while (s->symbol < alphabet_size && s->space > 0) { | 587 while (s->symbol < alphabet_size && s->space > 0) { |
| 584 const HuffmanCode* p = s->table; | 588 const HuffmanCode* p = s->table; |
| 585 uint32_t code_len; | 589 uint32_t code_len; |
| 586 uint32_t bits = 0; | 590 uint32_t bits = 0; |
| 587 uint32_t available_bits = BrotliGetAvailableBits(br); | 591 uint32_t available_bits = BrotliGetAvailableBits(br); |
| 588 if (available_bits != 0) { | 592 if (available_bits != 0) { |
| 589 bits = (uint32_t)BrotliGetBitsUnmasked(br); | 593 bits = (uint32_t)BrotliGetBitsUnmasked(br); |
| 590 } | 594 } |
| 591 p += bits & BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH); | 595 p += bits & BitMask(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH); |
| 592 if (p->bits > available_bits) goto pullMoreInput; | 596 if (p->bits > available_bits) goto pullMoreInput; |
| 593 code_len = p->value; /* code_len == 0..17 */ | 597 code_len = p->value; /* code_len == 0..17 */ |
| 594 if (code_len < kCodeLengthRepeatCode) { | 598 if (code_len < BROTLI_REPEAT_PREVIOUS_CODE_LENGTH) { |
| 595 BrotliDropBits(br, p->bits); | 599 BrotliDropBits(br, p->bits); |
| 596 ProcessSingleCodeLength(code_len, &s->symbol, &s->repeat, &s->space, | 600 ProcessSingleCodeLength(code_len, &s->symbol, &s->repeat, &s->space, |
| 597 &s->prev_code_len, s->symbol_lists, s->code_length_histo, | 601 &s->prev_code_len, s->symbol_lists, s->code_length_histo, |
| 598 s->next_symbol); | 602 s->next_symbol); |
| 599 } else { /* code_len == 16..17, extra_bits == 2..3 */ | 603 } else { /* code_len == 16..17, extra_bits == 2..3 */ |
| 600 uint32_t extra_bits = code_len - 14U; | 604 uint32_t extra_bits = code_len - 14U; |
| 601 uint32_t repeat_delta = (bits >> p->bits) & BitMask(extra_bits); | 605 uint32_t repeat_delta = (bits >> p->bits) & BitMask(extra_bits); |
| 602 if (available_bits < p->bits + extra_bits) goto pullMoreInput; | 606 if (available_bits < p->bits + extra_bits) goto pullMoreInput; |
| 603 BrotliDropBits(br, p->bits + extra_bits); | 607 BrotliDropBits(br, p->bits + extra_bits); |
| 604 ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size, | 608 ProcessRepeatedCodeLength(code_len, repeat_delta, alphabet_size, |
| 605 &s->symbol, &s->repeat, &s->space, &s->prev_code_len, | 609 &s->symbol, &s->repeat, &s->space, &s->prev_code_len, |
| 606 &s->repeat_code_len, s->symbol_lists, s->code_length_histo, | 610 &s->repeat_code_len, s->symbol_lists, s->code_length_histo, |
| 607 s->next_symbol); | 611 s->next_symbol); |
| 608 } | 612 } |
| 609 continue; | 613 continue; |
| 610 | 614 |
| 611 pullMoreInput: | 615 pullMoreInput: |
| 612 if (!BrotliPullByte(br)) { | 616 if (!BrotliPullByte(br)) { |
| 613 return BROTLI_NEEDS_MORE_INPUT; | 617 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 614 } | 618 } |
| 615 } | 619 } |
| 616 return BROTLI_SUCCESS; | 620 return BROTLI_DECODER_SUCCESS; |
| 617 } | 621 } |
| 618 | 622 |
| 619 /* Reads and decodes 15..18 codes using static prefix code. | 623 /* Reads and decodes 15..18 codes using static prefix code. |
| 620 Each code is 2..4 bits long. In total 30..72 bits are used. */ | 624 Each code is 2..4 bits long. In total 30..72 bits are used. */ |
| 621 static BrotliErrorCode ReadCodeLengthCodeLengths(BrotliState* s) { | 625 static BrotliDecoderErrorCode ReadCodeLengthCodeLengths(BrotliDecoderState* s) { |
| 622 BrotliBitReader* br = &s->br; | 626 BrotliBitReader* br = &s->br; |
| 623 uint32_t num_codes = s->repeat; | 627 uint32_t num_codes = s->repeat; |
| 624 unsigned space = s->space; | 628 unsigned space = s->space; |
| 625 uint32_t i = s->sub_loop_counter; | 629 uint32_t i = s->sub_loop_counter; |
| 626 for (; i < CODE_LENGTH_CODES; ++i) { | 630 for (; i < BROTLI_CODE_LENGTH_CODES; ++i) { |
| 627 const uint8_t code_len_idx = kCodeLengthCodeOrder[i]; | 631 const uint8_t code_len_idx = kCodeLengthCodeOrder[i]; |
| 628 uint32_t ix; | 632 uint32_t ix; |
| 629 uint32_t v; | 633 uint32_t v; |
| 630 if (PREDICT_FALSE(!BrotliSafeGetBits(br, 4, &ix))) { | 634 if (BROTLI_PREDICT_FALSE(!BrotliSafeGetBits(br, 4, &ix))) { |
| 631 uint32_t available_bits = BrotliGetAvailableBits(br); | 635 uint32_t available_bits = BrotliGetAvailableBits(br); |
| 632 if (available_bits != 0) { | 636 if (available_bits != 0) { |
| 633 ix = BrotliGetBitsUnmasked(br) & 0xF; | 637 ix = BrotliGetBitsUnmasked(br) & 0xF; |
| 634 } else { | 638 } else { |
| 635 ix = 0; | 639 ix = 0; |
| 636 } | 640 } |
| 637 if (kCodeLengthPrefixLength[ix] > available_bits) { | 641 if (kCodeLengthPrefixLength[ix] > available_bits) { |
| 638 s->sub_loop_counter = i; | 642 s->sub_loop_counter = i; |
| 639 s->repeat = num_codes; | 643 s->repeat = num_codes; |
| 640 s->space = space; | 644 s->space = space; |
| 641 s->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX; | 645 s->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX; |
| 642 return BROTLI_NEEDS_MORE_INPUT; | 646 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 643 } | 647 } |
| 644 } | 648 } |
| 645 v = kCodeLengthPrefixValue[ix]; | 649 v = kCodeLengthPrefixValue[ix]; |
| 646 BrotliDropBits(br, kCodeLengthPrefixLength[ix]); | 650 BrotliDropBits(br, kCodeLengthPrefixLength[ix]); |
| 647 s->code_length_code_lengths[code_len_idx] = (uint8_t)v; | 651 s->code_length_code_lengths[code_len_idx] = (uint8_t)v; |
| 648 BROTLI_LOG_ARRAY_INDEX(s->code_length_code_lengths, code_len_idx); | 652 BROTLI_LOG_ARRAY_INDEX(s->code_length_code_lengths, code_len_idx); |
| 649 if (v != 0) { | 653 if (v != 0) { |
| 650 space = space - (32U >> v); | 654 space = space - (32U >> v); |
| 651 ++num_codes; | 655 ++num_codes; |
| 652 ++s->code_length_histo[v]; | 656 ++s->code_length_histo[v]; |
| 653 if (space - 1U >= 32U) { | 657 if (space - 1U >= 32U) { |
| 654 /* space is 0 or wrapped around */ | 658 /* space is 0 or wrapped around */ |
| 655 break; | 659 break; |
| 656 } | 660 } |
| 657 } | 661 } |
| 658 } | 662 } |
| 659 if (!(num_codes == 1 || space == 0)) { | 663 if (!(num_codes == 1 || space == 0)) { |
| 660 return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_CL_SPACE); | 664 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_CL_SPACE); |
| 661 } | 665 } |
| 662 return BROTLI_SUCCESS; | 666 return BROTLI_DECODER_SUCCESS; |
| 663 } | 667 } |
| 664 | 668 |
| 665 /* Decodes the Huffman tables. | 669 /* Decodes the Huffman tables. |
| 666 There are 2 scenarios: | 670 There are 2 scenarios: |
| 667 A) Huffman code contains only few symbols (1..4). Those symbols are read | 671 A) Huffman code contains only few symbols (1..4). Those symbols are read |
| 668 directly; their code lengths are defined by the number of symbols. | 672 directly; their code lengths are defined by the number of symbols. |
| 669 For this scenario 4 - 45 bits will be read. | 673 For this scenario 4 - 45 bits will be read. |
| 670 | 674 |
| 671 B) 2-phase decoding: | 675 B) 2-phase decoding: |
| 672 B.1) Small Huffman table is decoded; it is specified with code lengths | 676 B.1) Small Huffman table is decoded; it is specified with code lengths |
| 673 encoded with predefined entropy code. 32 - 74 bits are used. | 677 encoded with predefined entropy code. 32 - 74 bits are used. |
| 674 B.2) Decoded table is used to decode code lengths of symbols in resulting | 678 B.2) Decoded table is used to decode code lengths of symbols in resulting |
| 675 Huffman table. In worst case 3520 bits are read. | 679 Huffman table. In worst case 3520 bits are read. |
| 676 */ | 680 */ |
| 677 static BrotliErrorCode ReadHuffmanCode(uint32_t alphabet_size, | 681 static BrotliDecoderErrorCode ReadHuffmanCode(uint32_t alphabet_size, |
| 678 HuffmanCode* table, | 682 HuffmanCode* table, |
| 679 uint32_t* opt_table_size, | 683 uint32_t* opt_table_size, |
| 680 BrotliState* s) { | 684 BrotliDecoderState* s) { |
| 681 BrotliBitReader* br = &s->br; | 685 BrotliBitReader* br = &s->br; |
| 682 /* Unnecessary masking, but might be good for safety. */ | 686 /* Unnecessary masking, but might be good for safety. */ |
| 683 alphabet_size &= 0x3ff; | 687 alphabet_size &= 0x3ff; |
| 684 /* State machine */ | 688 /* State machine */ |
| 685 switch (s->substate_huffman) { | 689 switch (s->substate_huffman) { |
| 686 case BROTLI_STATE_HUFFMAN_NONE: | 690 case BROTLI_STATE_HUFFMAN_NONE: |
| 687 if (!BrotliSafeReadBits(br, 2, &s->sub_loop_counter)) { | 691 if (!BrotliSafeReadBits(br, 2, &s->sub_loop_counter)) { |
| 688 return BROTLI_NEEDS_MORE_INPUT; | 692 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 689 } | 693 } |
| 690 BROTLI_LOG_UINT(s->sub_loop_counter); | 694 BROTLI_LOG_UINT(s->sub_loop_counter); |
| 691 /* The value is used as follows: | 695 /* The value is used as follows: |
| 692 1 for simple code; | 696 1 for simple code; |
| 693 0 for no skipping, 2 skips 2 code lengths, 3 skips 3 code lengths */ | 697 0 for no skipping, 2 skips 2 code lengths, 3 skips 3 code lengths */ |
| 694 if (s->sub_loop_counter != 1) { | 698 if (s->sub_loop_counter != 1) { |
| 695 s->space = 32; | 699 s->space = 32; |
| 696 s->repeat = 0; /* num_codes */ | 700 s->repeat = 0; /* num_codes */ |
| 697 memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo[0]) * | 701 memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo[0]) * |
| 698 (BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1)); | 702 (BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1)); |
| 699 memset(&s->code_length_code_lengths[0], 0, | 703 memset(&s->code_length_code_lengths[0], 0, |
| 700 sizeof(s->code_length_code_lengths)); | 704 sizeof(s->code_length_code_lengths)); |
| 701 s->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX; | 705 s->substate_huffman = BROTLI_STATE_HUFFMAN_COMPLEX; |
| 702 goto Complex; | 706 goto Complex; |
| 703 } | 707 } |
| 704 /* No break, transit to the next state. */ | 708 /* No break, transit to the next state. */ |
| 705 | 709 |
| 706 case BROTLI_STATE_HUFFMAN_SIMPLE_SIZE: | 710 case BROTLI_STATE_HUFFMAN_SIMPLE_SIZE: |
| 707 /* Read symbols, codes & code lengths directly. */ | 711 /* Read symbols, codes & code lengths directly. */ |
| 708 if (!BrotliSafeReadBits(br, 2, &s->symbol)) { /* num_symbols */ | 712 if (!BrotliSafeReadBits(br, 2, &s->symbol)) { /* num_symbols */ |
| 709 s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_SIZE; | 713 s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_SIZE; |
| 710 return BROTLI_NEEDS_MORE_INPUT; | 714 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 711 } | 715 } |
| 712 s->sub_loop_counter = 0; | 716 s->sub_loop_counter = 0; |
| 713 /* No break, transit to the next state. */ | 717 /* No break, transit to the next state. */ |
| 714 case BROTLI_STATE_HUFFMAN_SIMPLE_READ: { | 718 case BROTLI_STATE_HUFFMAN_SIMPLE_READ: { |
| 715 BrotliErrorCode result = ReadSimpleHuffmanSymbols(alphabet_size, s); | 719 BrotliDecoderErrorCode result = |
| 716 if (result != BROTLI_SUCCESS) { | 720 ReadSimpleHuffmanSymbols(alphabet_size, s); |
| 721 if (result != BROTLI_DECODER_SUCCESS) { |
| 717 return result; | 722 return result; |
| 718 } | 723 } |
| 719 /* No break, transit to the next state. */ | 724 /* No break, transit to the next state. */ |
| 720 } | 725 } |
| 721 case BROTLI_STATE_HUFFMAN_SIMPLE_BUILD: { | 726 case BROTLI_STATE_HUFFMAN_SIMPLE_BUILD: { |
| 722 uint32_t table_size; | 727 uint32_t table_size; |
| 723 if (s->symbol == 3) { | 728 if (s->symbol == 3) { |
| 724 uint32_t bits; | 729 uint32_t bits; |
| 725 if (!BrotliSafeReadBits(br, 1, &bits)) { | 730 if (!BrotliSafeReadBits(br, 1, &bits)) { |
| 726 s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_BUILD; | 731 s->substate_huffman = BROTLI_STATE_HUFFMAN_SIMPLE_BUILD; |
| 727 return BROTLI_NEEDS_MORE_INPUT; | 732 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 728 } | 733 } |
| 729 s->symbol += bits; | 734 s->symbol += bits; |
| 730 } | 735 } |
| 731 BROTLI_LOG_UINT(s->symbol); | 736 BROTLI_LOG_UINT(s->symbol); |
| 732 table_size = BrotliBuildSimpleHuffmanTable( | 737 table_size = BrotliBuildSimpleHuffmanTable( |
| 733 table, HUFFMAN_TABLE_BITS, s->symbols_lists_array, s->symbol); | 738 table, HUFFMAN_TABLE_BITS, s->symbols_lists_array, s->symbol); |
| 734 if (opt_table_size) { | 739 if (opt_table_size) { |
| 735 *opt_table_size = table_size; | 740 *opt_table_size = table_size; |
| 736 } | 741 } |
| 737 s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE; | 742 s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE; |
| 738 return BROTLI_SUCCESS; | 743 return BROTLI_DECODER_SUCCESS; |
| 739 } | 744 } |
| 740 | 745 |
| 741 Complex: /* Decode Huffman-coded code lengths. */ | 746 Complex: /* Decode Huffman-coded code lengths. */ |
| 742 case BROTLI_STATE_HUFFMAN_COMPLEX: { | 747 case BROTLI_STATE_HUFFMAN_COMPLEX: { |
| 743 uint32_t i; | 748 uint32_t i; |
| 744 BrotliErrorCode result = ReadCodeLengthCodeLengths(s); | 749 BrotliDecoderErrorCode result = ReadCodeLengthCodeLengths(s); |
| 745 if (result != BROTLI_SUCCESS) { | 750 if (result != BROTLI_DECODER_SUCCESS) { |
| 746 return result; | 751 return result; |
| 747 } | 752 } |
| 748 BrotliBuildCodeLengthsHuffmanTable(s->table, | 753 BrotliBuildCodeLengthsHuffmanTable(s->table, |
| 749 s->code_length_code_lengths, | 754 s->code_length_code_lengths, |
| 750 s->code_length_histo); | 755 s->code_length_histo); |
| 751 memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo)); | 756 memset(&s->code_length_histo[0], 0, sizeof(s->code_length_histo)); |
| 752 for (i = 0; i <= BROTLI_HUFFMAN_MAX_CODE_LENGTH; ++i) { | 757 for (i = 0; i <= BROTLI_HUFFMAN_MAX_CODE_LENGTH; ++i) { |
| 753 s->next_symbol[i] = (int)i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1); | 758 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; | 759 s->symbol_lists[(int)i - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1)] = 0xFFFF; |
| 755 } | 760 } |
| 756 | 761 |
| 757 s->symbol = 0; | 762 s->symbol = 0; |
| 758 s->prev_code_len = kDefaultCodeLength; | 763 s->prev_code_len = BROTLI_INITIAL_REPEATED_CODE_LENGTH; |
| 759 s->repeat = 0; | 764 s->repeat = 0; |
| 760 s->repeat_code_len = 0; | 765 s->repeat_code_len = 0; |
| 761 s->space = 32768; | 766 s->space = 32768; |
| 762 s->substate_huffman = BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS; | 767 s->substate_huffman = BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS; |
| 763 /* No break, transit to the next state. */ | 768 /* No break, transit to the next state. */ |
| 764 } | 769 } |
| 765 case BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS: { | 770 case BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS: { |
| 766 uint32_t table_size; | 771 uint32_t table_size; |
| 767 BrotliErrorCode result = ReadSymbolCodeLengths(alphabet_size, s); | 772 BrotliDecoderErrorCode result = ReadSymbolCodeLengths(alphabet_size, s); |
| 768 if (result == BROTLI_NEEDS_MORE_INPUT) { | 773 if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) { |
| 769 result = SafeReadSymbolCodeLengths(alphabet_size, s); | 774 result = SafeReadSymbolCodeLengths(alphabet_size, s); |
| 770 } | 775 } |
| 771 if (result != BROTLI_SUCCESS) { | 776 if (result != BROTLI_DECODER_SUCCESS) { |
| 772 return result; | 777 return result; |
| 773 } | 778 } |
| 774 | 779 |
| 775 if (s->space != 0) { | 780 if (s->space != 0) { |
| 776 BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", s->space)); | 781 BROTLI_LOG(("[ReadHuffmanCode] space = %d\n", s->space)); |
| 777 return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_HUFFMAN_SPACE); | 782 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE); |
| 778 } | 783 } |
| 779 table_size = BrotliBuildHuffmanTable( | 784 table_size = BrotliBuildHuffmanTable( |
| 780 table, HUFFMAN_TABLE_BITS, s->symbol_lists, s->code_length_histo); | 785 table, HUFFMAN_TABLE_BITS, s->symbol_lists, s->code_length_histo); |
| 781 if (opt_table_size) { | 786 if (opt_table_size) { |
| 782 *opt_table_size = table_size; | 787 *opt_table_size = table_size; |
| 783 } | 788 } |
| 784 s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE; | 789 s->substate_huffman = BROTLI_STATE_HUFFMAN_NONE; |
| 785 return BROTLI_SUCCESS; | 790 return BROTLI_DECODER_SUCCESS; |
| 786 } | 791 } |
| 787 | 792 |
| 788 default: | 793 default: |
| 789 return BROTLI_FAILURE(BROTLI_ERROR_UNREACHABLE_3); | 794 return |
| 795 BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); |
| 790 } | 796 } |
| 791 } | 797 } |
| 792 | 798 |
| 793 /* Decodes a block length by reading 3..39 bits. */ | 799 /* Decodes a block length by reading 3..39 bits. */ |
| 794 static BROTLI_INLINE uint32_t ReadBlockLength(const HuffmanCode* table, | 800 static BROTLI_INLINE uint32_t ReadBlockLength(const HuffmanCode* table, |
| 795 BrotliBitReader* br) { | 801 BrotliBitReader* br) { |
| 796 uint32_t code; | 802 uint32_t code; |
| 797 uint32_t nbits; | 803 uint32_t nbits; |
| 798 code = ReadSymbol(table, br); | 804 code = ReadSymbol(table, br); |
| 799 nbits = kBlockLengthPrefixCode[code].nbits; /* nbits == 2..24 */ | 805 nbits = kBlockLengthPrefixCode[code].nbits; /* nbits == 2..24 */ |
| 800 return kBlockLengthPrefixCode[code].offset + BrotliReadBits(br, nbits); | 806 return kBlockLengthPrefixCode[code].offset + BrotliReadBits(br, nbits); |
| 801 } | 807 } |
| 802 | 808 |
| 803 /* WARNING: if state is not BROTLI_STATE_READ_BLOCK_LENGTH_NONE, then | 809 /* WARNING: if state is not BROTLI_STATE_READ_BLOCK_LENGTH_NONE, then |
| 804 reading can't be continued with ReadBlockLength. */ | 810 reading can't be continued with ReadBlockLength. */ |
| 805 static BROTLI_INLINE int SafeReadBlockLength(BrotliState* s, | 811 static BROTLI_INLINE BROTLI_BOOL SafeReadBlockLength( |
| 806 uint32_t* result, | 812 BrotliDecoderState* s, uint32_t* result, const HuffmanCode* table, |
| 807 const HuffmanCode* table, | 813 BrotliBitReader* br) { |
| 808 BrotliBitReader* br) { | |
| 809 uint32_t index; | 814 uint32_t index; |
| 810 if (s->substate_read_block_length == BROTLI_STATE_READ_BLOCK_LENGTH_NONE) { | 815 if (s->substate_read_block_length == BROTLI_STATE_READ_BLOCK_LENGTH_NONE) { |
| 811 if (!SafeReadSymbol(table, br, &index)) { | 816 if (!SafeReadSymbol(table, br, &index)) { |
| 812 return 0; | 817 return BROTLI_FALSE; |
| 813 } | 818 } |
| 814 } else { | 819 } else { |
| 815 index = s->block_length_index; | 820 index = s->block_length_index; |
| 816 } | 821 } |
| 817 { | 822 { |
| 818 uint32_t bits; | 823 uint32_t bits; |
| 819 uint32_t nbits = kBlockLengthPrefixCode[index].nbits; /* nbits == 2..24 */ | 824 uint32_t nbits = kBlockLengthPrefixCode[index].nbits; /* nbits == 2..24 */ |
| 820 if (!BrotliSafeReadBits(br, nbits, &bits)) { | 825 if (!BrotliSafeReadBits(br, nbits, &bits)) { |
| 821 s->block_length_index = index; | 826 s->block_length_index = index; |
| 822 s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX; | 827 s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX; |
| 823 return 0; | 828 return BROTLI_FALSE; |
| 824 } | 829 } |
| 825 *result = kBlockLengthPrefixCode[index].offset + bits; | 830 *result = kBlockLengthPrefixCode[index].offset + bits; |
| 826 s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE; | 831 s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE; |
| 827 return 1; | 832 return BROTLI_TRUE; |
| 828 } | 833 } |
| 829 } | 834 } |
| 830 | 835 |
| 831 /* Transform: | 836 /* Transform: |
| 832 1) initialize list L with values 0, 1,... 255 | 837 1) initialize list L with values 0, 1,... 255 |
| 833 2) For each input element X: | 838 2) For each input element X: |
| 834 2.1) let Y = L[X] | 839 2.1) let Y = L[X] |
| 835 2.2) remove X-th element from L | 840 2.2) remove X-th element from L |
| 836 2.3) prepend Y to L | 841 2.3) prepend Y to L |
| 837 2.4) append Y to output | 842 2.4) append Y to output |
| 838 | 843 |
| 839 In most cases max(Y) <= 7, so most of L remains intact. | 844 In most cases max(Y) <= 7, so most of L remains intact. |
| 840 To reduce the cost of initialization, we reuse L, remember the upper bound | 845 To reduce the cost of initialization, we reuse L, remember the upper bound |
| 841 of Y values, and reinitialize only first elements in L. | 846 of Y values, and reinitialize only first elements in L. |
| 842 | 847 |
| 843 Most of input values are 0 and 1. To reduce number of branches, we replace | 848 Most of input values are 0 and 1. To reduce number of branches, we replace |
| 844 inner for loop with do-while. | 849 inner for loop with do-while. |
| 845 */ | 850 */ |
| 846 static BROTLI_NOINLINE void InverseMoveToFrontTransform(uint8_t* v, | 851 static BROTLI_NOINLINE void InverseMoveToFrontTransform( |
| 847 uint32_t v_len, BrotliState* state) { | 852 uint8_t* v, uint32_t v_len, BrotliDecoderState* state) { |
| 848 /* Reinitialize elements that could have been changed. */ | 853 /* Reinitialize elements that could have been changed. */ |
| 849 uint32_t i = 4; | 854 uint32_t i = 1; |
| 850 uint32_t upper_bound = state->mtf_upper_bound; | 855 uint32_t upper_bound = state->mtf_upper_bound; |
| 851 uint8_t* mtf = &state->mtf[4]; /* Make mtf[-1] addressable. */ | 856 uint32_t* mtf = &state->mtf[1]; /* Make mtf[-1] addressable. */ |
| 857 uint8_t* mtf_u8 = (uint8_t*)mtf; |
| 852 /* Load endian-aware constant. */ | 858 /* Load endian-aware constant. */ |
| 853 const uint8_t b0123[4] = {0, 1, 2, 3}; | 859 const uint8_t b0123[4] = {0, 1, 2, 3}; |
| 854 uint32_t pattern; | 860 uint32_t pattern; |
| 855 memcpy(&pattern, &b0123, 4); | 861 memcpy(&pattern, &b0123, 4); |
| 856 | 862 |
| 857 /* Initialize list using 4 consequent values pattern. */ | 863 /* Initialize list using 4 consequent values pattern. */ |
| 858 *(uint32_t*)mtf = pattern; | 864 mtf[0] = pattern; |
| 859 do { | 865 do { |
| 860 pattern += 0x04040404; /* Advance all 4 values by 4. */ | 866 pattern += 0x04040404; /* Advance all 4 values by 4. */ |
| 861 *(uint32_t*)(mtf + i) = pattern; | 867 mtf[i] = pattern; |
| 862 i += 4; | 868 i++; |
| 863 } while (i <= upper_bound); | 869 } while (i <= upper_bound); |
| 864 | 870 |
| 865 /* Transform the input. */ | 871 /* Transform the input. */ |
| 866 upper_bound = 0; | 872 upper_bound = 0; |
| 867 for (i = 0; i < v_len; ++i) { | 873 for (i = 0; i < v_len; ++i) { |
| 868 int index = v[i]; | 874 int index = v[i]; |
| 869 uint8_t value = mtf[index]; | 875 uint8_t value = mtf_u8[index]; |
| 870 upper_bound |= v[i]; | 876 upper_bound |= v[i]; |
| 871 v[i] = value; | 877 v[i] = value; |
| 872 mtf[-1] = value; | 878 mtf_u8[-1] = value; |
| 873 do { | 879 do { |
| 874 index--; | 880 index--; |
| 875 mtf[index + 1] = mtf[index]; | 881 mtf_u8[index + 1] = mtf_u8[index]; |
| 876 } while (index >= 0); | 882 } while (index >= 0); |
| 877 } | 883 } |
| 878 /* Remember amount of elements to be reinitialized. */ | 884 /* Remember amount of elements to be reinitialized. */ |
| 879 state->mtf_upper_bound = upper_bound; | 885 state->mtf_upper_bound = upper_bound >> 2; |
| 880 } | 886 } |
| 881 | 887 |
| 882 /* Decodes a series of Huffman table using ReadHuffmanCode function. */ | 888 /* Decodes a series of Huffman table using ReadHuffmanCode function. */ |
| 883 static BrotliErrorCode HuffmanTreeGroupDecode(HuffmanTreeGroup* group, | 889 static BrotliDecoderErrorCode HuffmanTreeGroupDecode( |
| 884 BrotliState* s) { | 890 HuffmanTreeGroup* group, BrotliDecoderState* s) { |
| 885 if (s->substate_tree_group != BROTLI_STATE_TREE_GROUP_LOOP) { | 891 if (s->substate_tree_group != BROTLI_STATE_TREE_GROUP_LOOP) { |
| 886 s->next = group->codes; | 892 s->next = group->codes; |
| 887 s->htree_index = 0; | 893 s->htree_index = 0; |
| 888 s->substate_tree_group = BROTLI_STATE_TREE_GROUP_LOOP; | 894 s->substate_tree_group = BROTLI_STATE_TREE_GROUP_LOOP; |
| 889 } | 895 } |
| 890 while (s->htree_index < group->num_htrees) { | 896 while (s->htree_index < group->num_htrees) { |
| 891 uint32_t table_size; | 897 uint32_t table_size; |
| 892 BrotliErrorCode result = | 898 BrotliDecoderErrorCode result = |
| 893 ReadHuffmanCode(group->alphabet_size, s->next, &table_size, s); | 899 ReadHuffmanCode(group->alphabet_size, s->next, &table_size, s); |
| 894 if (result != BROTLI_SUCCESS) return result; | 900 if (result != BROTLI_DECODER_SUCCESS) return result; |
| 895 group->htrees[s->htree_index] = s->next; | 901 group->htrees[s->htree_index] = s->next; |
| 896 s->next += table_size; | 902 s->next += table_size; |
| 897 ++s->htree_index; | 903 ++s->htree_index; |
| 898 } | 904 } |
| 899 s->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE; | 905 s->substate_tree_group = BROTLI_STATE_TREE_GROUP_NONE; |
| 900 return BROTLI_SUCCESS; | 906 return BROTLI_DECODER_SUCCESS; |
| 901 } | 907 } |
| 902 | 908 |
| 903 /* Decodes a context map. | 909 /* Decodes a context map. |
| 904 Decoding is done in 4 phases: | 910 Decoding is done in 4 phases: |
| 905 1) Read auxiliary information (6..16 bits) and allocate memory. | 911 1) Read auxiliary information (6..16 bits) and allocate memory. |
| 906 In case of trivial context map, decoding is finished at this phase. | 912 In case of trivial context map, decoding is finished at this phase. |
| 907 2) Decode Huffman table using ReadHuffmanCode function. | 913 2) Decode Huffman table using ReadHuffmanCode function. |
| 908 This table will be used for reading context map items. | 914 This table will be used for reading context map items. |
| 909 3) Read context map items; "0" values could be run-length encoded. | 915 3) Read context map items; "0" values could be run-length encoded. |
| 910 4) Optionally, apply InverseMoveToFront transform to the resulting map. | 916 4) Optionally, apply InverseMoveToFront transform to the resulting map. |
| 911 */ | 917 */ |
| 912 static BrotliErrorCode DecodeContextMap(uint32_t context_map_size, | 918 static BrotliDecoderErrorCode DecodeContextMap(uint32_t context_map_size, |
| 913 uint32_t* num_htrees, | 919 uint32_t* num_htrees, |
| 914 uint8_t** context_map_arg, | 920 uint8_t** context_map_arg, |
| 915 BrotliState* s) { | 921 BrotliDecoderState* s) { |
| 916 BrotliBitReader* br = &s->br; | 922 BrotliBitReader* br = &s->br; |
| 917 BrotliErrorCode result = BROTLI_SUCCESS; | 923 BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS; |
| 918 | 924 |
| 919 switch ((int)s->substate_context_map) { | 925 switch ((int)s->substate_context_map) { |
| 920 case BROTLI_STATE_CONTEXT_MAP_NONE: | 926 case BROTLI_STATE_CONTEXT_MAP_NONE: |
| 921 result = DecodeVarLenUint8(s, br, num_htrees); | 927 result = DecodeVarLenUint8(s, br, num_htrees); |
| 922 if (result != BROTLI_SUCCESS) { | 928 if (result != BROTLI_DECODER_SUCCESS) { |
| 923 return result; | 929 return result; |
| 924 } | 930 } |
| 925 (*num_htrees)++; | 931 (*num_htrees)++; |
| 926 s->context_index = 0; | 932 s->context_index = 0; |
| 927 BROTLI_LOG_UINT(context_map_size); | 933 BROTLI_LOG_UINT(context_map_size); |
| 928 BROTLI_LOG_UINT(*num_htrees); | 934 BROTLI_LOG_UINT(*num_htrees); |
| 929 *context_map_arg = (uint8_t*)BROTLI_ALLOC(s, (size_t)context_map_size); | 935 *context_map_arg = (uint8_t*)BROTLI_ALLOC(s, (size_t)context_map_size); |
| 930 if (*context_map_arg == 0) { | 936 if (*context_map_arg == 0) { |
| 931 return BROTLI_FAILURE(BROTLI_ERROR_ALLOC_CONTEXT_MAP); | 937 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP); |
| 932 } | 938 } |
| 933 if (*num_htrees <= 1) { | 939 if (*num_htrees <= 1) { |
| 934 memset(*context_map_arg, 0, (size_t)context_map_size); | 940 memset(*context_map_arg, 0, (size_t)context_map_size); |
| 935 return BROTLI_SUCCESS; | 941 return BROTLI_DECODER_SUCCESS; |
| 936 } | 942 } |
| 937 s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_READ_PREFIX; | 943 s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_READ_PREFIX; |
| 938 /* No break, continue to next state. */ | 944 /* No break, continue to next state. */ |
| 939 case BROTLI_STATE_CONTEXT_MAP_READ_PREFIX: { | 945 case BROTLI_STATE_CONTEXT_MAP_READ_PREFIX: { |
| 940 uint32_t bits; | 946 uint32_t bits; |
| 941 /* In next stage ReadHuffmanCode uses at least 4 bits, so it is safe | 947 /* In next stage ReadHuffmanCode uses at least 4 bits, so it is safe |
| 942 to peek 4 bits ahead. */ | 948 to peek 4 bits ahead. */ |
| 943 if (!BrotliSafeGetBits(br, 5, &bits)) { | 949 if (!BrotliSafeGetBits(br, 5, &bits)) { |
| 944 return BROTLI_NEEDS_MORE_INPUT; | 950 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 945 } | 951 } |
| 946 if ((bits & 1) != 0) { /* Use RLE for zeroes. */ | 952 if ((bits & 1) != 0) { /* Use RLE for zeros. */ |
| 947 s->max_run_length_prefix = (bits >> 1) + 1; | 953 s->max_run_length_prefix = (bits >> 1) + 1; |
| 948 BrotliDropBits(br, 5); | 954 BrotliDropBits(br, 5); |
| 949 } else { | 955 } else { |
| 950 s->max_run_length_prefix = 0; | 956 s->max_run_length_prefix = 0; |
| 951 BrotliDropBits(br, 1); | 957 BrotliDropBits(br, 1); |
| 952 } | 958 } |
| 953 BROTLI_LOG_UINT(s->max_run_length_prefix); | 959 BROTLI_LOG_UINT(s->max_run_length_prefix); |
| 954 s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_HUFFMAN; | 960 s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_HUFFMAN; |
| 955 /* No break, continue to next state. */ | 961 /* No break, continue to next state. */ |
| 956 } | 962 } |
| 957 case BROTLI_STATE_CONTEXT_MAP_HUFFMAN: | 963 case BROTLI_STATE_CONTEXT_MAP_HUFFMAN: |
| 958 result = ReadHuffmanCode(*num_htrees + s->max_run_length_prefix, | 964 result = ReadHuffmanCode(*num_htrees + s->max_run_length_prefix, |
| 959 s->context_map_table, NULL, s); | 965 s->context_map_table, NULL, s); |
| 960 if (result != BROTLI_SUCCESS) return result; | 966 if (result != BROTLI_DECODER_SUCCESS) return result; |
| 961 s->code = 0xFFFF; | 967 s->code = 0xFFFF; |
| 962 s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_DECODE; | 968 s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_DECODE; |
| 963 /* No break, continue to next state. */ | 969 /* No break, continue to next state. */ |
| 964 case BROTLI_STATE_CONTEXT_MAP_DECODE: { | 970 case BROTLI_STATE_CONTEXT_MAP_DECODE: { |
| 965 uint32_t context_index = s->context_index; | 971 uint32_t context_index = s->context_index; |
| 966 uint32_t max_run_length_prefix = s->max_run_length_prefix; | 972 uint32_t max_run_length_prefix = s->max_run_length_prefix; |
| 967 uint8_t* context_map = *context_map_arg; | 973 uint8_t* context_map = *context_map_arg; |
| 968 uint32_t code = s->code; | 974 uint32_t code = s->code; |
| 969 if (code != 0xFFFF) { | 975 if (code != 0xFFFF) { |
| 970 goto rleCode; | 976 goto rleCode; |
| 971 } | 977 } |
| 972 while (context_index < context_map_size) { | 978 while (context_index < context_map_size) { |
| 973 if (!SafeReadSymbol(s->context_map_table, br, &code)) { | 979 if (!SafeReadSymbol(s->context_map_table, br, &code)) { |
| 974 s->code = 0xFFFF; | 980 s->code = 0xFFFF; |
| 975 s->context_index = context_index; | 981 s->context_index = context_index; |
| 976 return BROTLI_NEEDS_MORE_INPUT; | 982 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 977 } | 983 } |
| 978 BROTLI_LOG_UINT(code); | 984 BROTLI_LOG_UINT(code); |
| 979 | 985 |
| 980 if (code == 0) { | 986 if (code == 0) { |
| 981 context_map[context_index++] = 0; | 987 context_map[context_index++] = 0; |
| 982 continue; | 988 continue; |
| 983 } | 989 } |
| 984 if (code > max_run_length_prefix) { | 990 if (code > max_run_length_prefix) { |
| 985 context_map[context_index++] = | 991 context_map[context_index++] = |
| 986 (uint8_t)(code - max_run_length_prefix); | 992 (uint8_t)(code - max_run_length_prefix); |
| 987 continue; | 993 continue; |
| 988 } | 994 } |
| 989 rleCode: | 995 rleCode: |
| 990 { | 996 { |
| 991 uint32_t reps; | 997 uint32_t reps; |
| 992 if (!BrotliSafeReadBits(br, code, &reps)) { | 998 if (!BrotliSafeReadBits(br, code, &reps)) { |
| 993 s->code = code; | 999 s->code = code; |
| 994 s->context_index = context_index; | 1000 s->context_index = context_index; |
| 995 return BROTLI_NEEDS_MORE_INPUT; | 1001 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 996 } | 1002 } |
| 997 reps += 1U << code; | 1003 reps += 1U << code; |
| 998 BROTLI_LOG_UINT(reps); | 1004 BROTLI_LOG_UINT(reps); |
| 999 if (context_index + reps > context_map_size) { | 1005 if (context_index + reps > context_map_size) { |
| 1000 return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_CONTEXT_MAP_REPEAT); | 1006 return |
| 1007 BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT); |
| 1001 } | 1008 } |
| 1002 do { | 1009 do { |
| 1003 context_map[context_index++] = 0; | 1010 context_map[context_index++] = 0; |
| 1004 } while (--reps); | 1011 } while (--reps); |
| 1005 } | 1012 } |
| 1006 } | 1013 } |
| 1007 /* No break, continue to next state. */ | 1014 /* No break, continue to next state. */ |
| 1008 } | 1015 } |
| 1009 case BROTLI_STATE_CONTEXT_MAP_TRANSFORM: { | 1016 case BROTLI_STATE_CONTEXT_MAP_TRANSFORM: { |
| 1010 uint32_t bits; | 1017 uint32_t bits; |
| 1011 if (!BrotliSafeReadBits(br, 1, &bits)) { | 1018 if (!BrotliSafeReadBits(br, 1, &bits)) { |
| 1012 s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_TRANSFORM; | 1019 s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_TRANSFORM; |
| 1013 return BROTLI_NEEDS_MORE_INPUT; | 1020 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 1014 } | 1021 } |
| 1015 if (bits != 0) { | 1022 if (bits != 0) { |
| 1016 InverseMoveToFrontTransform(*context_map_arg, context_map_size, s); | 1023 InverseMoveToFrontTransform(*context_map_arg, context_map_size, s); |
| 1017 } | 1024 } |
| 1018 s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE; | 1025 s->substate_context_map = BROTLI_STATE_CONTEXT_MAP_NONE; |
| 1019 return BROTLI_SUCCESS; | 1026 return BROTLI_DECODER_SUCCESS; |
| 1020 } | 1027 } |
| 1021 default: | 1028 default: |
| 1022 return BROTLI_FAILURE(BROTLI_ERROR_UNREACHABLE_4); | 1029 return |
| 1030 BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); |
| 1023 } | 1031 } |
| 1024 } | 1032 } |
| 1025 | 1033 |
| 1026 /* Decodes a command or literal and updates block type ringbuffer. | 1034 /* Decodes a command or literal and updates block type ring-buffer. |
| 1027 Reads 3..54 bits. */ | 1035 Reads 3..54 bits. */ |
| 1028 static BROTLI_INLINE int DecodeBlockTypeAndLength(int safe, | 1036 static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength( |
| 1029 BrotliState* s, int tree_type) { | 1037 int safe, BrotliDecoderState* s, int tree_type) { |
| 1030 uint32_t max_block_type = s->num_block_types[tree_type]; | 1038 uint32_t max_block_type = s->num_block_types[tree_type]; |
| 1031 const HuffmanCode* type_tree = &s->block_type_trees[ | 1039 const HuffmanCode* type_tree = &s->block_type_trees[ |
| 1032 tree_type * BROTLI_HUFFMAN_MAX_SIZE_258]; | 1040 tree_type * BROTLI_HUFFMAN_MAX_SIZE_258]; |
| 1033 const HuffmanCode* len_tree = &s->block_len_trees[ | 1041 const HuffmanCode* len_tree = &s->block_len_trees[ |
| 1034 tree_type * BROTLI_HUFFMAN_MAX_SIZE_26]; | 1042 tree_type * BROTLI_HUFFMAN_MAX_SIZE_26]; |
| 1035 BrotliBitReader* br = &s->br; | 1043 BrotliBitReader* br = &s->br; |
| 1036 uint32_t* ringbuffer = &s->block_type_rb[tree_type * 2]; | 1044 uint32_t* ringbuffer = &s->block_type_rb[tree_type * 2]; |
| 1037 uint32_t block_type; | 1045 uint32_t block_type; |
| 1038 | 1046 |
| 1039 /* Read 0..15 + 3..39 bits */ | 1047 /* Read 0..15 + 3..39 bits */ |
| 1040 if (!safe) { | 1048 if (!safe) { |
| 1041 block_type = ReadSymbol(type_tree, br); | 1049 block_type = ReadSymbol(type_tree, br); |
| 1042 s->block_length[tree_type] = ReadBlockLength(len_tree, br); | 1050 s->block_length[tree_type] = ReadBlockLength(len_tree, br); |
| 1043 } else { | 1051 } else { |
| 1044 BrotliBitReaderState memento; | 1052 BrotliBitReaderState memento; |
| 1045 BrotliBitReaderSaveState(br, &memento); | 1053 BrotliBitReaderSaveState(br, &memento); |
| 1046 if (!SafeReadSymbol(type_tree, br, &block_type)) return 0; | 1054 if (!SafeReadSymbol(type_tree, br, &block_type)) return BROTLI_FALSE; |
| 1047 if (!SafeReadBlockLength(s, &s->block_length[tree_type], len_tree, br)) { | 1055 if (!SafeReadBlockLength(s, &s->block_length[tree_type], len_tree, br)) { |
| 1048 s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE; | 1056 s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE; |
| 1049 BrotliBitReaderRestoreState(br, &memento); | 1057 BrotliBitReaderRestoreState(br, &memento); |
| 1050 return 0; | 1058 return BROTLI_FALSE; |
| 1051 } | 1059 } |
| 1052 } | 1060 } |
| 1053 | 1061 |
| 1054 if (block_type == 1) { | 1062 if (block_type == 1) { |
| 1055 block_type = ringbuffer[1] + 1; | 1063 block_type = ringbuffer[1] + 1; |
| 1056 } else if (block_type == 0) { | 1064 } else if (block_type == 0) { |
| 1057 block_type = ringbuffer[0]; | 1065 block_type = ringbuffer[0]; |
| 1058 } else { | 1066 } else { |
| 1059 block_type -= 2; | 1067 block_type -= 2; |
| 1060 } | 1068 } |
| 1061 if (block_type >= max_block_type) { | 1069 if (block_type >= max_block_type) { |
| 1062 block_type -= max_block_type; | 1070 block_type -= max_block_type; |
| 1063 } | 1071 } |
| 1064 ringbuffer[0] = ringbuffer[1]; | 1072 ringbuffer[0] = ringbuffer[1]; |
| 1065 ringbuffer[1] = block_type; | 1073 ringbuffer[1] = block_type; |
| 1066 return 1; | 1074 return BROTLI_TRUE; |
| 1075 } |
| 1076 |
| 1077 static BROTLI_INLINE void DetectTrivialLiteralBlockTypes( |
| 1078 BrotliDecoderState* s) { |
| 1079 size_t i; |
| 1080 for (i = 0; i < 8; ++i) s->trivial_literal_contexts[i] = 0; |
| 1081 for (i = 0; i < s->num_block_types[0]; i++) { |
| 1082 size_t offset = i << BROTLI_LITERAL_CONTEXT_BITS; |
| 1083 size_t error = 0; |
| 1084 size_t sample = s->context_map[offset]; |
| 1085 size_t j; |
| 1086 for (j = 0; j < (1u << BROTLI_LITERAL_CONTEXT_BITS);) { |
| 1087 BROTLI_REPEAT(4, error |= s->context_map[offset + j++] ^ sample;) |
| 1088 } |
| 1089 if (error == 0) { |
| 1090 s->trivial_literal_contexts[i >> 5] |= 1u << (i & 31); |
| 1091 } |
| 1092 } |
| 1093 } |
| 1094 |
| 1095 static BROTLI_INLINE void PrepareLiteralDecoding(BrotliDecoderState* s) { |
| 1096 uint8_t context_mode; |
| 1097 size_t trivial; |
| 1098 uint32_t block_type = s->block_type_rb[1]; |
| 1099 uint32_t context_offset = block_type << BROTLI_LITERAL_CONTEXT_BITS; |
| 1100 s->context_map_slice = s->context_map + context_offset; |
| 1101 trivial = s->trivial_literal_contexts[block_type >> 5]; |
| 1102 s->trivial_literal_context = (trivial >> (block_type & 31)) & 1; |
| 1103 s->literal_htree = s->literal_hgroup.htrees[s->context_map_slice[0]]; |
| 1104 context_mode = s->context_modes[block_type]; |
| 1105 s->context_lookup1 = &kContextLookup[kContextLookupOffsets[context_mode]]; |
| 1106 s->context_lookup2 = &kContextLookup[kContextLookupOffsets[context_mode + 1]]; |
| 1067 } | 1107 } |
| 1068 | 1108 |
| 1069 /* Decodes the block type and updates the state for literal context. | 1109 /* Decodes the block type and updates the state for literal context. |
| 1070 Reads 3..54 bits. */ | 1110 Reads 3..54 bits. */ |
| 1071 static BROTLI_INLINE int DecodeLiteralBlockSwitchInternal(int safe, | 1111 static BROTLI_INLINE BROTLI_BOOL DecodeLiteralBlockSwitchInternal( |
| 1072 BrotliState* s) { | 1112 int safe, BrotliDecoderState* s) { |
| 1073 uint8_t context_mode; | |
| 1074 uint32_t context_offset; | |
| 1075 if (!DecodeBlockTypeAndLength(safe, s, 0)) { | 1113 if (!DecodeBlockTypeAndLength(safe, s, 0)) { |
| 1076 return 0; | 1114 return BROTLI_FALSE; |
| 1077 } | 1115 } |
| 1078 context_offset = s->block_type_rb[1] << kLiteralContextBits; | 1116 PrepareLiteralDecoding(s); |
| 1079 s->context_map_slice = s->context_map + context_offset; | 1117 return BROTLI_TRUE; |
| 1080 s->literal_htree_index = s->context_map_slice[0]; | 1118 } |
| 1081 s->literal_htree = s->literal_hgroup.htrees[s->literal_htree_index]; | 1119 |
| 1082 context_mode = s->context_modes[s->block_type_rb[1]]; | 1120 static void BROTLI_NOINLINE DecodeLiteralBlockSwitch(BrotliDecoderState* s) { |
| 1083 s->context_lookup1 = &kContextLookup[kContextLookupOffsets[context_mode]]; | |
| 1084 s->context_lookup2 = &kContextLookup[kContextLookupOffsets[context_mode + 1]]; | |
| 1085 return 1; | |
| 1086 } | |
| 1087 | |
| 1088 static void BROTLI_NOINLINE DecodeLiteralBlockSwitch(BrotliState* s) { | |
| 1089 DecodeLiteralBlockSwitchInternal(0, s); | 1121 DecodeLiteralBlockSwitchInternal(0, s); |
| 1090 } | 1122 } |
| 1091 | 1123 |
| 1092 static int BROTLI_NOINLINE SafeDecodeLiteralBlockSwitch(BrotliState* s) { | 1124 static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeLiteralBlockSwitch( |
| 1125 BrotliDecoderState* s) { |
| 1093 return DecodeLiteralBlockSwitchInternal(1, s); | 1126 return DecodeLiteralBlockSwitchInternal(1, s); |
| 1094 } | 1127 } |
| 1095 | 1128 |
| 1096 /* Block switch for insert/copy length. | 1129 /* Block switch for insert/copy length. |
| 1097 Reads 3..54 bits. */ | 1130 Reads 3..54 bits. */ |
| 1098 static BROTLI_INLINE int DecodeCommandBlockSwitchInternal(int safe, | 1131 static BROTLI_INLINE BROTLI_BOOL DecodeCommandBlockSwitchInternal( |
| 1099 BrotliState* s) { | 1132 int safe, BrotliDecoderState* s) { |
| 1100 if (!DecodeBlockTypeAndLength(safe, s, 1)) { | 1133 if (!DecodeBlockTypeAndLength(safe, s, 1)) { |
| 1101 return 0; | 1134 return BROTLI_FALSE; |
| 1102 } | 1135 } |
| 1103 s->htree_command = s->insert_copy_hgroup.htrees[s->block_type_rb[3]]; | 1136 s->htree_command = s->insert_copy_hgroup.htrees[s->block_type_rb[3]]; |
| 1104 return 1; | 1137 return BROTLI_TRUE; |
| 1105 } | 1138 } |
| 1106 | 1139 |
| 1107 static void BROTLI_NOINLINE DecodeCommandBlockSwitch(BrotliState* s) { | 1140 static void BROTLI_NOINLINE DecodeCommandBlockSwitch(BrotliDecoderState* s) { |
| 1108 DecodeCommandBlockSwitchInternal(0, s); | 1141 DecodeCommandBlockSwitchInternal(0, s); |
| 1109 } | 1142 } |
| 1110 static int BROTLI_NOINLINE SafeDecodeCommandBlockSwitch(BrotliState* s) { | 1143 static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeCommandBlockSwitch( |
| 1144 BrotliDecoderState* s) { |
| 1111 return DecodeCommandBlockSwitchInternal(1, s); | 1145 return DecodeCommandBlockSwitchInternal(1, s); |
| 1112 } | 1146 } |
| 1113 | 1147 |
| 1114 /* Block switch for distance codes. | 1148 /* Block switch for distance codes. |
| 1115 Reads 3..54 bits. */ | 1149 Reads 3..54 bits. */ |
| 1116 static BROTLI_INLINE int DecodeDistanceBlockSwitchInternal(int safe, | 1150 static BROTLI_INLINE BROTLI_BOOL DecodeDistanceBlockSwitchInternal( |
| 1117 BrotliState* s) { | 1151 int safe, BrotliDecoderState* s) { |
| 1118 if (!DecodeBlockTypeAndLength(safe, s, 2)) { | 1152 if (!DecodeBlockTypeAndLength(safe, s, 2)) { |
| 1119 return 0; | 1153 return BROTLI_FALSE; |
| 1120 } | 1154 } |
| 1121 s->dist_context_map_slice = | 1155 s->dist_context_map_slice = s->dist_context_map + |
| 1122 s->dist_context_map + (s->block_type_rb[5] << kDistanceContextBits); | 1156 (s->block_type_rb[5] << BROTLI_DISTANCE_CONTEXT_BITS); |
| 1123 s->dist_htree_index = s->dist_context_map_slice[s->distance_context]; | 1157 s->dist_htree_index = s->dist_context_map_slice[s->distance_context]; |
| 1124 return 1; | 1158 return BROTLI_TRUE; |
| 1125 } | 1159 } |
| 1126 | 1160 |
| 1127 static void BROTLI_NOINLINE DecodeDistanceBlockSwitch(BrotliState* s) { | 1161 static void BROTLI_NOINLINE DecodeDistanceBlockSwitch(BrotliDecoderState* s) { |
| 1128 DecodeDistanceBlockSwitchInternal(0, s); | 1162 DecodeDistanceBlockSwitchInternal(0, s); |
| 1129 } | 1163 } |
| 1130 | 1164 |
| 1131 static int BROTLI_NOINLINE SafeDecodeDistanceBlockSwitch(BrotliState* s) { | 1165 static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeDistanceBlockSwitch( |
| 1166 BrotliDecoderState* s) { |
| 1132 return DecodeDistanceBlockSwitchInternal(1, s); | 1167 return DecodeDistanceBlockSwitchInternal(1, s); |
| 1133 } | 1168 } |
| 1134 | 1169 |
| 1135 static BrotliErrorCode BROTLI_NOINLINE WriteRingBuffer(size_t* available_out, | 1170 static size_t UnwrittenBytes(const BrotliDecoderState* s, BROTLI_BOOL wrap) { |
| 1136 uint8_t** next_out, size_t* total_out, BrotliState* s) { | 1171 size_t pos = wrap && s->pos > s->ringbuffer_size ? |
| 1137 size_t pos = (s->pos > s->ringbuffer_size) ? (size_t)s->ringbuffer_size | 1172 (size_t)s->ringbuffer_size : (size_t)(s->pos); |
| 1138 : (size_t)(s->pos); | 1173 size_t partial_pos_rb = (s->rb_roundtrips * (size_t)s->ringbuffer_size) + pos; |
| 1174 return partial_pos_rb - s->partial_pos_out; |
| 1175 } |
| 1176 |
| 1177 /* Dumps output. |
| 1178 Returns BROTLI_DECODER_NEEDS_MORE_OUTPUT only if there is more output to push |
| 1179 and either ring-buffer is as big as window size, or |force| is true. |
| 1180 */ |
| 1181 static BrotliDecoderErrorCode BROTLI_NOINLINE WriteRingBuffer( |
| 1182 BrotliDecoderState* s, size_t* available_out, uint8_t** next_out, |
| 1183 size_t* total_out, BROTLI_BOOL force) { |
| 1139 uint8_t* start = | 1184 uint8_t* start = |
| 1140 s->ringbuffer + (s->partial_pos_out & (size_t)s->ringbuffer_mask); | 1185 s->ringbuffer + (s->partial_pos_out & (size_t)s->ringbuffer_mask); |
| 1141 size_t partial_pos_rb = (s->rb_roundtrips * (size_t)s->ringbuffer_size) + pos; | 1186 size_t to_write = UnwrittenBytes(s, BROTLI_TRUE); |
| 1142 size_t to_write = (partial_pos_rb - s->partial_pos_out); | |
| 1143 size_t num_written = *available_out; | 1187 size_t num_written = *available_out; |
| 1144 if (num_written > to_write) { | 1188 if (num_written > to_write) { |
| 1145 num_written = to_write; | 1189 num_written = to_write; |
| 1146 } | 1190 } |
| 1147 if (s->meta_block_remaining_len < 0) { | 1191 if (s->meta_block_remaining_len < 0) { |
| 1148 return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_BLOCK_LENGTH_1); | 1192 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1); |
| 1149 } | 1193 } |
| 1150 memcpy(*next_out, start, num_written); | 1194 if (next_out && !*next_out) { |
| 1151 *next_out += num_written; | 1195 *next_out = start; |
| 1196 } else { |
| 1197 if (next_out) { |
| 1198 memcpy(*next_out, start, num_written); |
| 1199 *next_out += num_written; |
| 1200 } |
| 1201 } |
| 1152 *available_out -= num_written; | 1202 *available_out -= num_written; |
| 1153 BROTLI_LOG_UINT(to_write); | 1203 BROTLI_LOG_UINT(to_write); |
| 1154 BROTLI_LOG_UINT(num_written); | 1204 BROTLI_LOG_UINT(num_written); |
| 1155 s->partial_pos_out += num_written; | 1205 s->partial_pos_out += num_written; |
| 1156 *total_out = s->partial_pos_out; | 1206 if (total_out) *total_out = s->partial_pos_out - (size_t)s->custom_dict_size; |
| 1157 if (num_written < to_write) { | 1207 if (num_written < to_write) { |
| 1158 return BROTLI_NEEDS_MORE_OUTPUT; | 1208 if (s->ringbuffer_size == (1 << s->window_bits) || force) { |
| 1159 } | 1209 return BROTLI_DECODER_NEEDS_MORE_OUTPUT; |
| 1160 | 1210 } else { |
| 1161 if (s->pos >= s->ringbuffer_size) { | 1211 return BROTLI_DECODER_SUCCESS; |
| 1212 } |
| 1213 } |
| 1214 /* Wrap ring buffer only if it has reached its maximal size. */ |
| 1215 if (s->ringbuffer_size == (1 << s->window_bits) && |
| 1216 s->pos >= s->ringbuffer_size) { |
| 1162 s->pos -= s->ringbuffer_size; | 1217 s->pos -= s->ringbuffer_size; |
| 1163 s->rb_roundtrips++; | 1218 s->rb_roundtrips++; |
| 1164 } | 1219 s->should_wrap_ringbuffer = (size_t)s->pos != 0 ? 1 : 0; |
| 1165 return BROTLI_SUCCESS; | 1220 } |
| 1166 } | 1221 return BROTLI_DECODER_SUCCESS; |
| 1167 | 1222 } |
| 1168 /* Allocates ringbuffer. | 1223 |
| 1169 | 1224 static void BROTLI_NOINLINE WrapRingBuffer(BrotliDecoderState* s) { |
| 1170 s->ringbuffer_size MUST be updated by BrotliCalculateRingBufferSize before | 1225 if (s->should_wrap_ringbuffer) { |
| 1171 this function is called. | 1226 memcpy(s->ringbuffer, s->ringbuffer_end, (size_t)s->pos); |
| 1172 | 1227 s->should_wrap_ringbuffer = 0; |
| 1173 Last two bytes of ringbuffer are initialized to 0, so context calculation | 1228 } |
| 1229 } |
| 1230 |
| 1231 /* Allocates ring-buffer. |
| 1232 |
| 1233 s->ringbuffer_size MUST be updated by BrotliCalculateRingBufferSize before |
| 1234 this function is called. |
| 1235 |
| 1236 Last two bytes of ring-buffer are initialized to 0, so context calculation |
| 1174 could be done uniformly for the first two and all other positions. | 1237 could be done uniformly for the first two and all other positions. |
| 1175 | 1238 |
| 1176 Custom dictionary, if any, is copied to the end of ringbuffer. | 1239 Custom dictionary, if any, is copied to the end of ring-buffer. |
| 1177 */ | 1240 */ |
| 1178 static int BROTLI_NOINLINE BrotliAllocateRingBuffer(BrotliState* s) { | 1241 static BROTLI_BOOL BROTLI_NOINLINE BrotliEnsureRingBuffer( |
| 1242 BrotliDecoderState* s) { |
| 1179 /* We need the slack region for the following reasons: | 1243 /* We need the slack region for the following reasons: |
| 1180 - doing up to two 16-byte copies for fast backward copying | 1244 - doing up to two 16-byte copies for fast backward copying |
| 1181 - inserting transformed dictionary word (5 prefix + 24 base + 8 suffix) */ | 1245 - inserting transformed dictionary word (5 prefix + 24 base + 8 suffix) */ |
| 1182 static const int kRingBufferWriteAheadSlack = 42; | 1246 static const int kRingBufferWriteAheadSlack = 42; |
| 1183 s->ringbuffer = (uint8_t*)BROTLI_ALLOC(s, (size_t)(s->ringbuffer_size + | 1247 uint8_t* old_ringbuffer = s->ringbuffer; |
| 1248 if (s->ringbuffer_size == s->new_ringbuffer_size) { |
| 1249 return BROTLI_TRUE; |
| 1250 } |
| 1251 |
| 1252 s->ringbuffer = (uint8_t*)BROTLI_ALLOC(s, (size_t)(s->new_ringbuffer_size + |
| 1184 kRingBufferWriteAheadSlack)); | 1253 kRingBufferWriteAheadSlack)); |
| 1185 if (s->ringbuffer == 0) { | 1254 if (s->ringbuffer == 0) { |
| 1186 return 0; | 1255 /* Restore previous value. */ |
| 1187 } | 1256 s->ringbuffer = old_ringbuffer; |
| 1188 | 1257 return BROTLI_FALSE; |
| 1258 } |
| 1259 s->ringbuffer[s->new_ringbuffer_size - 2] = 0; |
| 1260 s->ringbuffer[s->new_ringbuffer_size - 1] = 0; |
| 1261 |
| 1262 if (!old_ringbuffer) { |
| 1263 if (s->custom_dict) { |
| 1264 memcpy(s->ringbuffer, s->custom_dict, (size_t)s->custom_dict_size); |
| 1265 s->partial_pos_out = (size_t)s->custom_dict_size; |
| 1266 s->pos = s->custom_dict_size; |
| 1267 } |
| 1268 } else { |
| 1269 memcpy(s->ringbuffer, old_ringbuffer, (size_t)s->pos); |
| 1270 BROTLI_FREE(s, old_ringbuffer); |
| 1271 } |
| 1272 |
| 1273 s->ringbuffer_size = s->new_ringbuffer_size; |
| 1274 s->ringbuffer_mask = s->new_ringbuffer_size - 1; |
| 1189 s->ringbuffer_end = s->ringbuffer + s->ringbuffer_size; | 1275 s->ringbuffer_end = s->ringbuffer + s->ringbuffer_size; |
| 1190 | 1276 |
| 1191 s->ringbuffer[s->ringbuffer_size - 2] = 0; | 1277 return BROTLI_TRUE; |
| 1192 s->ringbuffer[s->ringbuffer_size - 1] = 0; | 1278 } |
| 1193 | 1279 |
| 1194 if (s->custom_dict) { | 1280 static BrotliDecoderErrorCode BROTLI_NOINLINE CopyUncompressedBlockToOutput( |
| 1195 memcpy(&s->ringbuffer[(-s->custom_dict_size) & s->ringbuffer_mask], | |
| 1196 s->custom_dict, (size_t)s->custom_dict_size); | |
| 1197 } | |
| 1198 | |
| 1199 return 1; | |
| 1200 } | |
| 1201 | |
| 1202 static BrotliErrorCode BROTLI_NOINLINE CopyUncompressedBlockToOutput( | |
| 1203 size_t* available_out, uint8_t** next_out, size_t* total_out, | 1281 size_t* available_out, uint8_t** next_out, size_t* total_out, |
| 1204 BrotliState* s) { | 1282 BrotliDecoderState* s) { |
| 1205 /* TODO: avoid allocation for single uncompressed block. */ | 1283 /* TODO: avoid allocation for single uncompressed block. */ |
| 1206 if (!s->ringbuffer && !BrotliAllocateRingBuffer(s)) { | 1284 if (!BrotliEnsureRingBuffer(s)) { |
| 1207 return BROTLI_FAILURE(BROTLI_ERROR_ALLOC_RING_BUFFER_1); | 1285 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1); |
| 1208 } | 1286 } |
| 1209 | 1287 |
| 1210 /* State machine */ | 1288 /* State machine */ |
| 1211 for (;;) { | 1289 for (;;) { |
| 1212 switch (s->substate_uncompressed) { | 1290 switch (s->substate_uncompressed) { |
| 1213 case BROTLI_STATE_UNCOMPRESSED_NONE: { | 1291 case BROTLI_STATE_UNCOMPRESSED_NONE: { |
| 1214 int nbytes = (int)BrotliGetRemainingBytes(&s->br); | 1292 int nbytes = (int)BrotliGetRemainingBytes(&s->br); |
| 1215 if (nbytes > s->meta_block_remaining_len) { | 1293 if (nbytes > s->meta_block_remaining_len) { |
| 1216 nbytes = s->meta_block_remaining_len; | 1294 nbytes = s->meta_block_remaining_len; |
| 1217 } | 1295 } |
| 1218 if (s->pos + nbytes > s->ringbuffer_size) { | 1296 if (s->pos + nbytes > s->ringbuffer_size) { |
| 1219 nbytes = s->ringbuffer_size - s->pos; | 1297 nbytes = s->ringbuffer_size - s->pos; |
| 1220 } | 1298 } |
| 1221 /* Copy remaining bytes from s->br.buf_ to ringbuffer. */ | 1299 /* Copy remaining bytes from s->br.buf_ to ring-buffer. */ |
| 1222 BrotliCopyBytes(&s->ringbuffer[s->pos], &s->br, (size_t)nbytes); | 1300 BrotliCopyBytes(&s->ringbuffer[s->pos], &s->br, (size_t)nbytes); |
| 1223 s->pos += nbytes; | 1301 s->pos += nbytes; |
| 1224 s->meta_block_remaining_len -= nbytes; | 1302 s->meta_block_remaining_len -= nbytes; |
| 1225 if (s->pos < s->ringbuffer_size) { | 1303 if (s->pos < 1 << s->window_bits) { |
| 1226 if (s->meta_block_remaining_len == 0) { | 1304 if (s->meta_block_remaining_len == 0) { |
| 1227 return BROTLI_SUCCESS; | 1305 return BROTLI_DECODER_SUCCESS; |
| 1228 } | 1306 } |
| 1229 return BROTLI_NEEDS_MORE_INPUT; | 1307 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 1230 } | 1308 } |
| 1231 s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_WRITE; | 1309 s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_WRITE; |
| 1232 /* No break, continue to next state */ | 1310 /* No break, continue to next state */ |
| 1233 } | 1311 } |
| 1234 case BROTLI_STATE_UNCOMPRESSED_WRITE: { | 1312 case BROTLI_STATE_UNCOMPRESSED_WRITE: { |
| 1235 BrotliErrorCode result = | 1313 BrotliDecoderErrorCode result; |
| 1236 WriteRingBuffer(available_out, next_out, total_out, s); | 1314 result = WriteRingBuffer( |
| 1237 if (result != BROTLI_SUCCESS) { | 1315 s, available_out, next_out, total_out, BROTLI_FALSE); |
| 1316 if (result != BROTLI_DECODER_SUCCESS) { |
| 1238 return result; | 1317 return result; |
| 1239 } | 1318 } |
| 1240 s->max_distance = s->max_backward_distance; | 1319 if (s->ringbuffer_size == 1 << s->window_bits) { |
| 1320 s->max_distance = s->max_backward_distance; |
| 1321 } |
| 1241 s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE; | 1322 s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE; |
| 1242 break; | 1323 break; |
| 1243 } | 1324 } |
| 1244 } | 1325 } |
| 1245 } | 1326 } |
| 1246 BROTLI_DCHECK(0); /* Unreachable */ | 1327 BROTLI_DCHECK(0); /* Unreachable */ |
| 1247 } | 1328 } |
| 1248 | 1329 |
| 1249 int BrotliDecompressedSize(size_t encoded_size, | |
| 1250 const uint8_t* encoded_buffer, | |
| 1251 size_t* decoded_size) { | |
| 1252 BrotliState s; | |
| 1253 int next_block_header; | |
| 1254 BrotliStateInit(&s); | |
| 1255 s.br.next_in = encoded_buffer; | |
| 1256 s.br.avail_in = encoded_size; | |
| 1257 if (!BrotliWarmupBitReader(&s.br)) { | |
| 1258 return 0; | |
| 1259 } | |
| 1260 DecodeWindowBits(&s.br); | |
| 1261 if (DecodeMetaBlockLength(&s, &s.br) != BROTLI_SUCCESS) { | |
| 1262 return 0; | |
| 1263 } | |
| 1264 *decoded_size = (size_t)s.meta_block_remaining_len; | |
| 1265 if (s.is_last_metablock) { | |
| 1266 return 1; | |
| 1267 } | |
| 1268 if (!s.is_uncompressed || !BrotliJumpToByteBoundary(&s.br)) { | |
| 1269 return 0; | |
| 1270 } | |
| 1271 next_block_header = BrotliPeekByte(&s.br, (size_t)s.meta_block_remaining_len); | |
| 1272 return (next_block_header != -1) && ((next_block_header & 3) == 3); | |
| 1273 } | |
| 1274 | |
| 1275 /* Calculates the smallest feasible ring buffer. | 1330 /* Calculates the smallest feasible ring buffer. |
| 1276 | 1331 |
| 1277 If we know the data size is small, do not allocate more ringbuffer | 1332 If we know the data size is small, do not allocate more ring buffer |
| 1278 size than needed to reduce memory usage. | 1333 size than needed to reduce memory usage. |
| 1279 | 1334 |
| 1280 When this method is called, metablock size and flags MUST be decoded. | 1335 When this method is called, metablock size and flags MUST be decoded. |
| 1281 */ | 1336 */ |
| 1282 static void BROTLI_NOINLINE BrotliCalculateRingBufferSize(BrotliState* s, | 1337 static void BROTLI_NOINLINE BrotliCalculateRingBufferSize( |
| 1283 BrotliBitReader* br) { | 1338 BrotliDecoderState* s) { |
| 1284 int is_last = s->is_last_metablock; | |
| 1285 int window_size = 1 << s->window_bits; | 1339 int window_size = 1 << s->window_bits; |
| 1286 s->ringbuffer_size = window_size; | 1340 int new_ringbuffer_size = window_size; |
| 1341 /* We need at least 2 bytes of ring buffer size to get the last two |
| 1342 bytes for context from there */ |
| 1343 int min_size = s->ringbuffer_size ? s->ringbuffer_size : 1024; |
| 1344 int output_size; |
| 1287 | 1345 |
| 1288 if (s->is_uncompressed) { | 1346 /* If maximum is already reached, no further extension is retired. */ |
| 1289 int next_block_header = | 1347 if (s->ringbuffer_size == window_size) { |
| 1290 BrotliPeekByte(br, (size_t)s->meta_block_remaining_len); | 1348 return; |
| 1291 if (next_block_header != -1) { /* Peek succeeded */ | |
| 1292 if ((next_block_header & 3) == 3) { /* ISLAST and ISEMPTY */ | |
| 1293 is_last = 1; | |
| 1294 } | |
| 1295 } | |
| 1296 } | 1349 } |
| 1297 | 1350 |
| 1298 /* We need at least 2 bytes of ring buffer size to get the last two | 1351 /* Metadata blocks does not touch ring buffer. */ |
| 1299 bytes for context from there */ | 1352 if (s->is_metadata) { |
| 1300 if (is_last) { | 1353 return; |
| 1301 int min_size_x2 = (s->meta_block_remaining_len + s->custom_dict_size) * 2; | |
| 1302 while (s->ringbuffer_size >= min_size_x2 && s->ringbuffer_size > 32) { | |
| 1303 s->ringbuffer_size >>= 1; | |
| 1304 } | |
| 1305 } | 1354 } |
| 1306 | 1355 |
| 1307 s->ringbuffer_mask = s->ringbuffer_size - 1; | 1356 if (!s->ringbuffer) { |
| 1357 /* Custom dictionary counts as a "virtual" output. */ |
| 1358 output_size = s->custom_dict_size; |
| 1359 } else { |
| 1360 output_size = s->pos; |
| 1361 } |
| 1362 output_size += s->meta_block_remaining_len; |
| 1363 min_size = min_size < output_size ? output_size : min_size; |
| 1364 |
| 1365 while ((new_ringbuffer_size >> 1) >= min_size) { |
| 1366 new_ringbuffer_size >>= 1; |
| 1367 } |
| 1368 |
| 1369 s->new_ringbuffer_size = new_ringbuffer_size; |
| 1308 } | 1370 } |
| 1309 | 1371 |
| 1310 /* Reads 1..256 2-bit context modes. */ | 1372 /* Reads 1..256 2-bit context modes. */ |
| 1311 static BrotliErrorCode ReadContextModes(BrotliState* s) { | 1373 static BrotliDecoderErrorCode ReadContextModes(BrotliDecoderState* s) { |
| 1312 BrotliBitReader* br = &s->br; | 1374 BrotliBitReader* br = &s->br; |
| 1313 int i = s->loop_counter; | 1375 int i = s->loop_counter; |
| 1314 | 1376 |
| 1315 while (i < (int)s->num_block_types[0]) { | 1377 while (i < (int)s->num_block_types[0]) { |
| 1316 uint32_t bits; | 1378 uint32_t bits; |
| 1317 if (!BrotliSafeReadBits(br, 2, &bits)) { | 1379 if (!BrotliSafeReadBits(br, 2, &bits)) { |
| 1318 s->loop_counter = i; | 1380 s->loop_counter = i; |
| 1319 return BROTLI_NEEDS_MORE_INPUT; | 1381 return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 1320 } | 1382 } |
| 1321 s->context_modes[i] = (uint8_t)(bits << 1); | 1383 s->context_modes[i] = (uint8_t)(bits << 1); |
| 1322 BROTLI_LOG_ARRAY_INDEX(s->context_modes, i); | 1384 BROTLI_LOG_ARRAY_INDEX(s->context_modes, i); |
| 1323 i++; | 1385 i++; |
| 1324 } | 1386 } |
| 1325 return BROTLI_SUCCESS; | 1387 return BROTLI_DECODER_SUCCESS; |
| 1326 } | 1388 } |
| 1327 | 1389 |
| 1328 static BROTLI_INLINE void TakeDistanceFromRingBuffer(BrotliState* s) { | 1390 static BROTLI_INLINE void TakeDistanceFromRingBuffer(BrotliDecoderState* s) { |
| 1329 if (s->distance_code == 0) { | 1391 if (s->distance_code == 0) { |
| 1330 --s->dist_rb_idx; | 1392 --s->dist_rb_idx; |
| 1331 s->distance_code = s->dist_rb[s->dist_rb_idx & 3]; | 1393 s->distance_code = s->dist_rb[s->dist_rb_idx & 3]; |
| 1332 } else { | 1394 } else { |
| 1333 int distance_code = s->distance_code << 1; | 1395 int distance_code = s->distance_code << 1; |
| 1334 /* kDistanceShortCodeIndexOffset has 2-bit values from LSB: */ | 1396 /* kDistanceShortCodeIndexOffset has 2-bit values from LSB: */ |
| 1335 /* 3, 2, 1, 0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2 */ | 1397 /* 3, 2, 1, 0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2 */ |
| 1336 const uint32_t kDistanceShortCodeIndexOffset = 0xaaafff1b; | 1398 const uint32_t kDistanceShortCodeIndexOffset = 0xaaafff1b; |
| 1337 /* kDistanceShortCodeValueOffset has 2-bit values from LSB: */ | 1399 /* kDistanceShortCodeValueOffset has 2-bit values from LSB: */ |
| 1338 /*-0, 0,-0, 0,-1, 1,-2, 2,-3, 3,-1, 1,-2, 2,-3, 3 */ | 1400 /*-0, 0,-0, 0,-1, 1,-2, 2,-3, 3,-1, 1,-2, 2,-3, 3 */ |
| 1339 const uint32_t kDistanceShortCodeValueOffset = 0xfa5fa500; | 1401 const uint32_t kDistanceShortCodeValueOffset = 0xfa5fa500; |
| 1340 int v = (s->dist_rb_idx + | 1402 int v = (s->dist_rb_idx + |
| 1341 (int)(kDistanceShortCodeIndexOffset >> distance_code)) & 0x3; | 1403 (int)(kDistanceShortCodeIndexOffset >> distance_code)) & 0x3; |
| 1342 s->distance_code = s->dist_rb[v]; | 1404 s->distance_code = s->dist_rb[v]; |
| 1343 v = (int)(kDistanceShortCodeValueOffset >> distance_code) & 0x3; | 1405 v = (int)(kDistanceShortCodeValueOffset >> distance_code) & 0x3; |
| 1344 if ((distance_code & 0x3) != 0) { | 1406 if ((distance_code & 0x3) != 0) { |
| 1345 s->distance_code += v; | 1407 s->distance_code += v; |
| 1346 } else { | 1408 } else { |
| 1347 s->distance_code -= v; | 1409 s->distance_code -= v; |
| 1348 if (s->distance_code <= 0) { | 1410 if (s->distance_code <= 0) { |
| 1349 /* A huge distance will cause a BROTLI_FAILURE() soon. */ | 1411 /* A huge distance will cause a BROTLI_FAILURE() soon. */ |
| 1350 /* This is a little faster than failing here. */ | 1412 /* This is a little faster than failing here. */ |
| 1351 s->distance_code = 0x0fffffff; | 1413 s->distance_code = 0x0fffffff; |
| 1352 } | 1414 } |
| 1353 } | 1415 } |
| 1354 } | 1416 } |
| 1355 } | 1417 } |
| 1356 | 1418 |
| 1357 static BROTLI_INLINE int SafeReadBits( | 1419 static BROTLI_INLINE BROTLI_BOOL SafeReadBits( |
| 1358 BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { | 1420 BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { |
| 1359 if (n_bits != 0) { | 1421 if (n_bits != 0) { |
| 1360 return BrotliSafeReadBits(br, n_bits, val); | 1422 return BrotliSafeReadBits(br, n_bits, val); |
| 1361 } else { | 1423 } else { |
| 1362 *val = 0; | 1424 *val = 0; |
| 1363 return 1; | 1425 return BROTLI_TRUE; |
| 1364 } | 1426 } |
| 1365 } | 1427 } |
| 1366 | 1428 |
| 1367 /* Precondition: s->distance_code < 0 */ | 1429 /* Precondition: s->distance_code < 0 */ |
| 1368 static BROTLI_INLINE int ReadDistanceInternal(int safe, | 1430 static BROTLI_INLINE BROTLI_BOOL ReadDistanceInternal( |
| 1369 BrotliState* s, BrotliBitReader* br) { | 1431 int safe, BrotliDecoderState* s, BrotliBitReader* br) { |
| 1370 int distval; | 1432 int distval; |
| 1371 BrotliBitReaderState memento; | 1433 BrotliBitReaderState memento; |
| 1372 HuffmanCode* distance_tree = s->distance_hgroup.htrees[s->dist_htree_index]; | 1434 HuffmanCode* distance_tree = s->distance_hgroup.htrees[s->dist_htree_index]; |
| 1373 if (!safe) { | 1435 if (!safe) { |
| 1374 s->distance_code = (int)ReadSymbol(distance_tree, br); | 1436 s->distance_code = (int)ReadSymbol(distance_tree, br); |
| 1375 } else { | 1437 } else { |
| 1376 uint32_t code; | 1438 uint32_t code; |
| 1377 BrotliBitReaderSaveState(br, &memento); | 1439 BrotliBitReaderSaveState(br, &memento); |
| 1378 if (!SafeReadSymbol(distance_tree, br, &code)) { | 1440 if (!SafeReadSymbol(distance_tree, br, &code)) { |
| 1379 return 0; | 1441 return BROTLI_FALSE; |
| 1380 } | 1442 } |
| 1381 s->distance_code = (int)code; | 1443 s->distance_code = (int)code; |
| 1382 } | 1444 } |
| 1383 /* Convert the distance code to the actual distance by possibly */ | 1445 /* Convert the distance code to the actual distance by possibly */ |
| 1384 /* looking up past distances from the s->ringbuffer. */ | 1446 /* looking up past distances from the s->ringbuffer. */ |
| 1385 if ((s->distance_code & ~0xf) == 0) { | 1447 if ((s->distance_code & ~0xf) == 0) { |
| 1386 TakeDistanceFromRingBuffer(s); | 1448 TakeDistanceFromRingBuffer(s); |
| 1387 --s->block_length[2]; | 1449 --s->block_length[2]; |
| 1388 return 1; | 1450 return BROTLI_TRUE; |
| 1389 } | 1451 } |
| 1390 distval = s->distance_code - (int)s->num_direct_distance_codes; | 1452 distval = s->distance_code - (int)s->num_direct_distance_codes; |
| 1391 if (distval >= 0) { | 1453 if (distval >= 0) { |
| 1392 uint32_t nbits; | 1454 uint32_t nbits; |
| 1393 int postfix; | 1455 int postfix; |
| 1394 int offset; | 1456 int offset; |
| 1395 if (!safe && (s->distance_postfix_bits == 0)) { | 1457 if (!safe && (s->distance_postfix_bits == 0)) { |
| 1396 nbits = ((uint32_t)distval >> 1) + 1; | 1458 nbits = ((uint32_t)distval >> 1) + 1; |
| 1397 offset = ((2 + (distval & 1)) << nbits) - 4; | 1459 offset = ((2 + (distval & 1)) << nbits) - 4; |
| 1398 s->distance_code = (int)s->num_direct_distance_codes + offset + | 1460 s->distance_code = (int)s->num_direct_distance_codes + offset + |
| 1399 (int)BrotliReadBits(br, nbits); | 1461 (int)BrotliReadBits(br, nbits); |
| 1400 } else { | 1462 } else { |
| 1401 /* This branch also works well when s->distance_postfix_bits == 0 */ | 1463 /* This branch also works well when s->distance_postfix_bits == 0 */ |
| 1402 uint32_t bits; | 1464 uint32_t bits; |
| 1403 postfix = distval & s->distance_postfix_mask; | 1465 postfix = distval & s->distance_postfix_mask; |
| 1404 distval >>= s->distance_postfix_bits; | 1466 distval >>= s->distance_postfix_bits; |
| 1405 nbits = ((uint32_t)distval >> 1) + 1; | 1467 nbits = ((uint32_t)distval >> 1) + 1; |
| 1406 if (safe) { | 1468 if (safe) { |
| 1407 if (!SafeReadBits(br, nbits, &bits)) { | 1469 if (!SafeReadBits(br, nbits, &bits)) { |
| 1408 s->distance_code = -1; /* Restore precondition. */ | 1470 s->distance_code = -1; /* Restore precondition. */ |
| 1409 BrotliBitReaderRestoreState(br, &memento); | 1471 BrotliBitReaderRestoreState(br, &memento); |
| 1410 return 0; | 1472 return BROTLI_FALSE; |
| 1411 } | 1473 } |
| 1412 } else { | 1474 } else { |
| 1413 bits = BrotliReadBits(br, nbits); | 1475 bits = BrotliReadBits(br, nbits); |
| 1414 } | 1476 } |
| 1415 offset = ((2 + (distval & 1)) << nbits) - 4; | 1477 offset = ((2 + (distval & 1)) << nbits) - 4; |
| 1416 s->distance_code = (int)s->num_direct_distance_codes + | 1478 s->distance_code = (int)s->num_direct_distance_codes + |
| 1417 ((offset + (int)bits) << s->distance_postfix_bits) + postfix; | 1479 ((offset + (int)bits) << s->distance_postfix_bits) + postfix; |
| 1418 } | 1480 } |
| 1419 } | 1481 } |
| 1420 s->distance_code = s->distance_code - NUM_DISTANCE_SHORT_CODES + 1; | 1482 s->distance_code = s->distance_code - BROTLI_NUM_DISTANCE_SHORT_CODES + 1; |
| 1421 --s->block_length[2]; | 1483 --s->block_length[2]; |
| 1422 return 1; | 1484 return BROTLI_TRUE; |
| 1423 } | 1485 } |
| 1424 | 1486 |
| 1425 static BROTLI_INLINE void ReadDistance(BrotliState* s, BrotliBitReader* br) { | 1487 static BROTLI_INLINE void ReadDistance( |
| 1488 BrotliDecoderState* s, BrotliBitReader* br) { |
| 1426 ReadDistanceInternal(0, s, br); | 1489 ReadDistanceInternal(0, s, br); |
| 1427 } | 1490 } |
| 1428 | 1491 |
| 1429 static BROTLI_INLINE int SafeReadDistance(BrotliState* s, BrotliBitReader* br) { | 1492 static BROTLI_INLINE BROTLI_BOOL SafeReadDistance( |
| 1493 BrotliDecoderState* s, BrotliBitReader* br) { |
| 1430 return ReadDistanceInternal(1, s, br); | 1494 return ReadDistanceInternal(1, s, br); |
| 1431 } | 1495 } |
| 1432 | 1496 |
| 1433 static BROTLI_INLINE int ReadCommandInternal(int safe, | 1497 static BROTLI_INLINE BROTLI_BOOL ReadCommandInternal( |
| 1434 BrotliState* s, BrotliBitReader* br, int* insert_length) { | 1498 int safe, BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) { |
| 1435 uint32_t cmd_code; | 1499 uint32_t cmd_code; |
| 1436 uint32_t insert_len_extra = 0; | 1500 uint32_t insert_len_extra = 0; |
| 1437 uint32_t copy_length; | 1501 uint32_t copy_length; |
| 1438 CmdLutElement v; | 1502 CmdLutElement v; |
| 1439 BrotliBitReaderState memento; | 1503 BrotliBitReaderState memento; |
| 1440 if (!safe) { | 1504 if (!safe) { |
| 1441 cmd_code = ReadSymbol(s->htree_command, br); | 1505 cmd_code = ReadSymbol(s->htree_command, br); |
| 1442 } else { | 1506 } else { |
| 1443 BrotliBitReaderSaveState(br, &memento); | 1507 BrotliBitReaderSaveState(br, &memento); |
| 1444 if (!SafeReadSymbol(s->htree_command, br, &cmd_code)) { | 1508 if (!SafeReadSymbol(s->htree_command, br, &cmd_code)) { |
| 1445 return 0; | 1509 return BROTLI_FALSE; |
| 1446 } | 1510 } |
| 1447 } | 1511 } |
| 1448 v = kCmdLut[cmd_code]; | 1512 v = kCmdLut[cmd_code]; |
| 1449 s->distance_code = v.distance_code; | 1513 s->distance_code = v.distance_code; |
| 1450 s->distance_context = v.context; | 1514 s->distance_context = v.context; |
| 1451 s->dist_htree_index = s->dist_context_map_slice[s->distance_context]; | 1515 s->dist_htree_index = s->dist_context_map_slice[s->distance_context]; |
| 1452 *insert_length = v.insert_len_offset; | 1516 *insert_length = v.insert_len_offset; |
| 1453 if (!safe) { | 1517 if (!safe) { |
| 1454 if (PREDICT_FALSE(v.insert_len_extra_bits != 0)) { | 1518 if (BROTLI_PREDICT_FALSE(v.insert_len_extra_bits != 0)) { |
| 1455 insert_len_extra = BrotliReadBits(br, v.insert_len_extra_bits); | 1519 insert_len_extra = BrotliReadBits(br, v.insert_len_extra_bits); |
| 1456 } | 1520 } |
| 1457 copy_length = BrotliReadBits(br, v.copy_len_extra_bits); | 1521 copy_length = BrotliReadBits(br, v.copy_len_extra_bits); |
| 1458 } else { | 1522 } else { |
| 1459 if (!SafeReadBits(br, v.insert_len_extra_bits, &insert_len_extra) || | 1523 if (!SafeReadBits(br, v.insert_len_extra_bits, &insert_len_extra) || |
| 1460 !SafeReadBits(br, v.copy_len_extra_bits, ©_length)) { | 1524 !SafeReadBits(br, v.copy_len_extra_bits, ©_length)) { |
| 1461 BrotliBitReaderRestoreState(br, &memento); | 1525 BrotliBitReaderRestoreState(br, &memento); |
| 1462 return 0; | 1526 return BROTLI_FALSE; |
| 1463 } | 1527 } |
| 1464 } | 1528 } |
| 1465 s->copy_length = (int)copy_length + v.copy_len_offset; | 1529 s->copy_length = (int)copy_length + v.copy_len_offset; |
| 1466 --s->block_length[1]; | 1530 --s->block_length[1]; |
| 1467 *insert_length += (int)insert_len_extra; | 1531 *insert_length += (int)insert_len_extra; |
| 1468 return 1; | 1532 return BROTLI_TRUE; |
| 1469 } | 1533 } |
| 1470 | 1534 |
| 1471 static BROTLI_INLINE void ReadCommand(BrotliState* s, BrotliBitReader* br, | 1535 static BROTLI_INLINE void ReadCommand( |
| 1472 int* insert_length) { | 1536 BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) { |
| 1473 ReadCommandInternal(0, s, br, insert_length); | 1537 ReadCommandInternal(0, s, br, insert_length); |
| 1474 } | 1538 } |
| 1475 | 1539 |
| 1476 static BROTLI_INLINE int SafeReadCommand(BrotliState* s, BrotliBitReader* br, | 1540 static BROTLI_INLINE BROTLI_BOOL SafeReadCommand( |
| 1477 int* insert_length) { | 1541 BrotliDecoderState* s, BrotliBitReader* br, int* insert_length) { |
| 1478 return ReadCommandInternal(1, s, br, insert_length); | 1542 return ReadCommandInternal(1, s, br, insert_length); |
| 1479 } | 1543 } |
| 1480 | 1544 |
| 1481 static BROTLI_INLINE int CheckInputAmount(int safe, | 1545 static BROTLI_INLINE BROTLI_BOOL CheckInputAmount( |
| 1482 BrotliBitReader* const br, size_t num) { | 1546 int safe, BrotliBitReader* const br, size_t num) { |
| 1483 if (safe) { | 1547 if (safe) { |
| 1484 return 1; | 1548 return BROTLI_TRUE; |
| 1485 } | 1549 } |
| 1486 return BrotliCheckInputAmount(br, num); | 1550 return BrotliCheckInputAmount(br, num); |
| 1487 } | 1551 } |
| 1488 | 1552 |
| 1489 #define BROTLI_SAFE(METHOD) \ | 1553 #define BROTLI_SAFE(METHOD) \ |
| 1490 { \ | 1554 { \ |
| 1491 if (safe) { \ | 1555 if (safe) { \ |
| 1492 if (!Safe##METHOD) { \ | 1556 if (!Safe##METHOD) { \ |
| 1493 result = BROTLI_NEEDS_MORE_INPUT; \ | 1557 result = BROTLI_DECODER_NEEDS_MORE_INPUT; \ |
| 1494 goto saveStateAndReturn; \ | 1558 goto saveStateAndReturn; \ |
| 1495 } \ | 1559 } \ |
| 1496 } else { \ | 1560 } else { \ |
| 1497 METHOD; \ | 1561 METHOD; \ |
| 1498 } \ | 1562 } \ |
| 1499 } | 1563 } |
| 1500 | 1564 |
| 1501 static BROTLI_INLINE BrotliErrorCode ProcessCommandsInternal(int safe, | 1565 static BROTLI_INLINE BrotliDecoderErrorCode ProcessCommandsInternal( |
| 1502 BrotliState* s) { | 1566 int safe, BrotliDecoderState* s) { |
| 1503 int pos = s->pos; | 1567 int pos = s->pos; |
| 1504 int i = s->loop_counter; | 1568 int i = s->loop_counter; |
| 1505 BrotliErrorCode result = BROTLI_SUCCESS; | 1569 BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS; |
| 1506 BrotliBitReader* br = &s->br; | 1570 BrotliBitReader* br = &s->br; |
| 1507 | 1571 |
| 1508 if (!CheckInputAmount(safe, br, 28)) { | 1572 if (!CheckInputAmount(safe, br, 28)) { |
| 1509 result = BROTLI_NEEDS_MORE_INPUT; | 1573 result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 1510 goto saveStateAndReturn; | 1574 goto saveStateAndReturn; |
| 1511 } | 1575 } |
| 1512 if (!safe) { | 1576 if (!safe) { |
| 1513 BROTLI_UNUSED(BrotliWarmupBitReader(br)); | 1577 BROTLI_UNUSED(BrotliWarmupBitReader(br)); |
| 1514 } | 1578 } |
| 1515 | 1579 |
| 1516 /* Jump into state machine. */ | 1580 /* Jump into state machine. */ |
| 1517 if (s->state == BROTLI_STATE_COMMAND_BEGIN) { | 1581 if (s->state == BROTLI_STATE_COMMAND_BEGIN) { |
| 1518 goto CommandBegin; | 1582 goto CommandBegin; |
| 1519 } else if (s->state == BROTLI_STATE_COMMAND_INNER) { | 1583 } else if (s->state == BROTLI_STATE_COMMAND_INNER) { |
| 1520 goto CommandInner; | 1584 goto CommandInner; |
| 1521 } else if (s->state == BROTLI_STATE_COMMAND_POST_DECODE_LITERALS) { | 1585 } else if (s->state == BROTLI_STATE_COMMAND_POST_DECODE_LITERALS) { |
| 1522 goto CommandPostDecodeLiterals; | 1586 goto CommandPostDecodeLiterals; |
| 1523 } else if (s->state == BROTLI_STATE_COMMAND_POST_WRAP_COPY) { | 1587 } else if (s->state == BROTLI_STATE_COMMAND_POST_WRAP_COPY) { |
| 1524 goto CommandPostWrapCopy; | 1588 goto CommandPostWrapCopy; |
| 1525 } else { | 1589 } else { |
| 1526 return BROTLI_FAILURE(BROTLI_ERROR_UNREACHABLE_5); | 1590 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_UNREACHABLE); |
| 1527 } | 1591 } |
| 1528 | 1592 |
| 1529 CommandBegin: | 1593 CommandBegin: |
| 1530 if (safe) { | 1594 if (safe) { |
| 1531 s->state = BROTLI_STATE_COMMAND_BEGIN; | 1595 s->state = BROTLI_STATE_COMMAND_BEGIN; |
| 1532 } | 1596 } |
| 1533 if (!CheckInputAmount(safe, br, 28)) { /* 156 bits + 7 bytes */ | 1597 if (!CheckInputAmount(safe, br, 28)) { /* 156 bits + 7 bytes */ |
| 1534 s->state = BROTLI_STATE_COMMAND_BEGIN; | 1598 s->state = BROTLI_STATE_COMMAND_BEGIN; |
| 1535 result = BROTLI_NEEDS_MORE_INPUT; | 1599 result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 1536 goto saveStateAndReturn; | 1600 goto saveStateAndReturn; |
| 1537 } | 1601 } |
| 1538 if (PREDICT_FALSE(s->block_length[1] == 0)) { | 1602 if (BROTLI_PREDICT_FALSE(s->block_length[1] == 0)) { |
| 1539 BROTLI_SAFE(DecodeCommandBlockSwitch(s)); | 1603 BROTLI_SAFE(DecodeCommandBlockSwitch(s)); |
| 1540 goto CommandBegin; | 1604 goto CommandBegin; |
| 1541 } | 1605 } |
| 1542 /* Read the insert/copy length in the command */ | 1606 /* Read the insert/copy length in the command */ |
| 1543 BROTLI_SAFE(ReadCommand(s, br, &i)); | 1607 BROTLI_SAFE(ReadCommand(s, br, &i)); |
| 1544 BROTLI_LOG(("[ProcessCommandsInternal] pos = %d insert = %d copy = %d\n", | 1608 BROTLI_LOG(("[ProcessCommandsInternal] pos = %d insert = %d copy = %d\n", |
| 1545 pos, i, s->copy_length)); | 1609 pos, i, s->copy_length)); |
| 1546 if (i == 0) { | 1610 if (i == 0) { |
| 1547 goto CommandPostDecodeLiterals; | 1611 goto CommandPostDecodeLiterals; |
| 1548 } | 1612 } |
| 1549 s->meta_block_remaining_len -= i; | 1613 s->meta_block_remaining_len -= i; |
| 1550 | 1614 |
| 1551 CommandInner: | 1615 CommandInner: |
| 1552 if (safe) { | 1616 if (safe) { |
| 1553 s->state = BROTLI_STATE_COMMAND_INNER; | 1617 s->state = BROTLI_STATE_COMMAND_INNER; |
| 1554 } | 1618 } |
| 1555 /* Read the literals in the command */ | 1619 /* Read the literals in the command */ |
| 1556 if (s->trivial_literal_context) { | 1620 if (s->trivial_literal_context) { |
| 1557 uint32_t bits; | 1621 uint32_t bits; |
| 1558 uint32_t value; | 1622 uint32_t value; |
| 1559 PreloadSymbol(safe, s->literal_htree, br, &bits, &value); | 1623 PreloadSymbol(safe, s->literal_htree, br, &bits, &value); |
| 1560 do { | 1624 do { |
| 1561 if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */ | 1625 if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */ |
| 1562 s->state = BROTLI_STATE_COMMAND_INNER; | 1626 s->state = BROTLI_STATE_COMMAND_INNER; |
| 1563 result = BROTLI_NEEDS_MORE_INPUT; | 1627 result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 1564 goto saveStateAndReturn; | 1628 goto saveStateAndReturn; |
| 1565 } | 1629 } |
| 1566 if (PREDICT_FALSE(s->block_length[0] == 0)) { | 1630 if (BROTLI_PREDICT_FALSE(s->block_length[0] == 0)) { |
| 1567 BROTLI_SAFE(DecodeLiteralBlockSwitch(s)); | 1631 BROTLI_SAFE(DecodeLiteralBlockSwitch(s)); |
| 1568 PreloadSymbol(safe, s->literal_htree, br, &bits, &value); | 1632 PreloadSymbol(safe, s->literal_htree, br, &bits, &value); |
| 1633 if (!s->trivial_literal_context) goto CommandInner; |
| 1569 } | 1634 } |
| 1570 if (!safe) { | 1635 if (!safe) { |
| 1571 s->ringbuffer[pos] = | 1636 s->ringbuffer[pos] = |
| 1572 (uint8_t)ReadPreloadedSymbol(s->literal_htree, br, &bits, &value); | 1637 (uint8_t)ReadPreloadedSymbol(s->literal_htree, br, &bits, &value); |
| 1573 } else { | 1638 } else { |
| 1574 uint32_t literal; | 1639 uint32_t literal; |
| 1575 if (!SafeReadSymbol(s->literal_htree, br, &literal)) { | 1640 if (!SafeReadSymbol(s->literal_htree, br, &literal)) { |
| 1576 result = BROTLI_NEEDS_MORE_INPUT; | 1641 result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 1577 goto saveStateAndReturn; | 1642 goto saveStateAndReturn; |
| 1578 } | 1643 } |
| 1579 s->ringbuffer[pos] = (uint8_t)literal; | 1644 s->ringbuffer[pos] = (uint8_t)literal; |
| 1580 } | 1645 } |
| 1581 --s->block_length[0]; | 1646 --s->block_length[0]; |
| 1582 BROTLI_LOG_UINT(s->literal_htree_index); | |
| 1583 BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos); | 1647 BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos); |
| 1584 ++pos; | 1648 ++pos; |
| 1585 if (PREDICT_FALSE(pos == s->ringbuffer_size)) { | 1649 if (BROTLI_PREDICT_FALSE(pos == s->ringbuffer_size)) { |
| 1586 s->state = BROTLI_STATE_COMMAND_INNER_WRITE; | 1650 s->state = BROTLI_STATE_COMMAND_INNER_WRITE; |
| 1587 --i; | 1651 --i; |
| 1588 goto saveStateAndReturn; | 1652 goto saveStateAndReturn; |
| 1589 } | 1653 } |
| 1590 } while (--i != 0); | 1654 } while (--i != 0); |
| 1591 } else { | 1655 } else { |
| 1592 uint8_t p1 = s->ringbuffer[(pos - 1) & s->ringbuffer_mask]; | 1656 uint8_t p1 = s->ringbuffer[(pos - 1) & s->ringbuffer_mask]; |
| 1593 uint8_t p2 = s->ringbuffer[(pos - 2) & s->ringbuffer_mask]; | 1657 uint8_t p2 = s->ringbuffer[(pos - 2) & s->ringbuffer_mask]; |
| 1594 do { | 1658 do { |
| 1595 const HuffmanCode* hc; | 1659 const HuffmanCode* hc; |
| 1596 uint8_t context; | 1660 uint8_t context; |
| 1597 if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */ | 1661 if (!CheckInputAmount(safe, br, 28)) { /* 162 bits + 7 bytes */ |
| 1598 s->state = BROTLI_STATE_COMMAND_INNER; | 1662 s->state = BROTLI_STATE_COMMAND_INNER; |
| 1599 result = BROTLI_NEEDS_MORE_INPUT; | 1663 result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 1600 goto saveStateAndReturn; | 1664 goto saveStateAndReturn; |
| 1601 } | 1665 } |
| 1602 if (PREDICT_FALSE(s->block_length[0] == 0)) { | 1666 if (BROTLI_PREDICT_FALSE(s->block_length[0] == 0)) { |
| 1603 BROTLI_SAFE(DecodeLiteralBlockSwitch(s)); | 1667 BROTLI_SAFE(DecodeLiteralBlockSwitch(s)); |
| 1668 if (s->trivial_literal_context) goto CommandInner; |
| 1604 } | 1669 } |
| 1605 context = s->context_lookup1[p1] | s->context_lookup2[p2]; | 1670 context = s->context_lookup1[p1] | s->context_lookup2[p2]; |
| 1606 BROTLI_LOG_UINT(context); | 1671 BROTLI_LOG_UINT(context); |
| 1607 hc = s->literal_hgroup.htrees[s->context_map_slice[context]]; | 1672 hc = s->literal_hgroup.htrees[s->context_map_slice[context]]; |
| 1608 p2 = p1; | 1673 p2 = p1; |
| 1609 if (!safe) { | 1674 if (!safe) { |
| 1610 p1 = (uint8_t)ReadSymbol(hc, br); | 1675 p1 = (uint8_t)ReadSymbol(hc, br); |
| 1611 } else { | 1676 } else { |
| 1612 uint32_t literal; | 1677 uint32_t literal; |
| 1613 if (!SafeReadSymbol(hc, br, &literal)) { | 1678 if (!SafeReadSymbol(hc, br, &literal)) { |
| 1614 result = BROTLI_NEEDS_MORE_INPUT; | 1679 result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 1615 goto saveStateAndReturn; | 1680 goto saveStateAndReturn; |
| 1616 } | 1681 } |
| 1617 p1 = (uint8_t)literal; | 1682 p1 = (uint8_t)literal; |
| 1618 } | 1683 } |
| 1619 s->ringbuffer[pos] = p1; | 1684 s->ringbuffer[pos] = p1; |
| 1620 --s->block_length[0]; | 1685 --s->block_length[0]; |
| 1621 BROTLI_LOG_UINT(s->context_map_slice[context]); | 1686 BROTLI_LOG_UINT(s->context_map_slice[context]); |
| 1622 BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos & s->ringbuffer_mask); | 1687 BROTLI_LOG_ARRAY_INDEX(s->ringbuffer, pos & s->ringbuffer_mask); |
| 1623 ++pos; | 1688 ++pos; |
| 1624 if (PREDICT_FALSE(pos == s->ringbuffer_size)) { | 1689 if (BROTLI_PREDICT_FALSE(pos == s->ringbuffer_size)) { |
| 1625 s->state = BROTLI_STATE_COMMAND_INNER_WRITE; | 1690 s->state = BROTLI_STATE_COMMAND_INNER_WRITE; |
| 1626 --i; | 1691 --i; |
| 1627 goto saveStateAndReturn; | 1692 goto saveStateAndReturn; |
| 1628 } | 1693 } |
| 1629 } while (--i != 0); | 1694 } while (--i != 0); |
| 1630 } | 1695 } |
| 1631 BROTLI_LOG_UINT(s->meta_block_remaining_len); | 1696 BROTLI_LOG_UINT(s->meta_block_remaining_len); |
| 1632 if (s->meta_block_remaining_len <= 0) { | 1697 if (BROTLI_PREDICT_FALSE(s->meta_block_remaining_len <= 0)) { |
| 1633 s->state = BROTLI_STATE_METABLOCK_DONE; | 1698 s->state = BROTLI_STATE_METABLOCK_DONE; |
| 1634 goto saveStateAndReturn; | 1699 goto saveStateAndReturn; |
| 1635 } | 1700 } |
| 1636 | 1701 |
| 1637 CommandPostDecodeLiterals: | 1702 CommandPostDecodeLiterals: |
| 1638 if (safe) { | 1703 if (safe) { |
| 1639 s->state = BROTLI_STATE_COMMAND_POST_DECODE_LITERALS; | 1704 s->state = BROTLI_STATE_COMMAND_POST_DECODE_LITERALS; |
| 1640 } | 1705 } |
| 1641 if (s->distance_code >= 0) { | 1706 if (s->distance_code >= 0) { |
| 1642 --s->dist_rb_idx; | 1707 --s->dist_rb_idx; |
| 1643 s->distance_code = s->dist_rb[s->dist_rb_idx & 3]; | 1708 s->distance_code = s->dist_rb[s->dist_rb_idx & 3]; |
| 1644 goto postReadDistance; /* We already have the implicit distance */ | 1709 goto postReadDistance; /* We already have the implicit distance */ |
| 1645 } | 1710 } |
| 1646 /* Read distance code in the command, unless it was implicitly zero. */ | 1711 /* Read distance code in the command, unless it was implicitly zero. */ |
| 1647 if (PREDICT_FALSE(s->block_length[2] == 0)) { | 1712 if (BROTLI_PREDICT_FALSE(s->block_length[2] == 0)) { |
| 1648 BROTLI_SAFE(DecodeDistanceBlockSwitch(s)); | 1713 BROTLI_SAFE(DecodeDistanceBlockSwitch(s)); |
| 1649 } | 1714 } |
| 1650 BROTLI_SAFE(ReadDistance(s, br)); | 1715 BROTLI_SAFE(ReadDistance(s, br)); |
| 1651 postReadDistance: | 1716 postReadDistance: |
| 1652 BROTLI_LOG(("[ProcessCommandsInternal] pos = %d distance = %d\n", | 1717 BROTLI_LOG(("[ProcessCommandsInternal] pos = %d distance = %d\n", |
| 1653 pos, s->distance_code)); | 1718 pos, s->distance_code)); |
| 1654 if (s->max_distance != s->max_backward_distance) { | 1719 if (s->max_distance != s->max_backward_distance) { |
| 1655 if (pos < s->max_backward_distance_minus_custom_dict_size) { | 1720 s->max_distance = |
| 1656 s->max_distance = pos + s->custom_dict_size; | 1721 (pos < s->max_backward_distance) ? pos : s->max_backward_distance; |
| 1657 } else { | |
| 1658 s->max_distance = s->max_backward_distance; | |
| 1659 } | |
| 1660 } | 1722 } |
| 1661 i = s->copy_length; | 1723 i = s->copy_length; |
| 1662 /* Apply copy of LZ77 back-reference, or static dictionary reference if | 1724 /* Apply copy of LZ77 back-reference, or static dictionary reference if |
| 1663 the distance is larger than the max LZ77 distance */ | 1725 the distance is larger than the max LZ77 distance */ |
| 1664 if (s->distance_code > s->max_distance) { | 1726 if (s->distance_code > s->max_distance) { |
| 1665 if (i >= kBrotliMinDictionaryWordLength && | 1727 if (i >= BROTLI_MIN_DICTIONARY_WORD_LENGTH && |
| 1666 i <= kBrotliMaxDictionaryWordLength) { | 1728 i <= BROTLI_MAX_DICTIONARY_WORD_LENGTH) { |
| 1667 int offset = (int)kBrotliDictionaryOffsetsByLength[i]; | 1729 int offset = (int)kBrotliDictionaryOffsetsByLength[i]; |
| 1668 int word_id = s->distance_code - s->max_distance - 1; | 1730 int word_id = s->distance_code - s->max_distance - 1; |
| 1669 uint32_t shift = kBrotliDictionarySizeBitsByLength[i]; | 1731 uint32_t shift = kBrotliDictionarySizeBitsByLength[i]; |
| 1670 int mask = (int)BitMask(shift); | 1732 int mask = (int)BitMask(shift); |
| 1671 int word_idx = word_id & mask; | 1733 int word_idx = word_id & mask; |
| 1672 int transform_idx = word_id >> shift; | 1734 int transform_idx = word_id >> shift; |
| 1673 offset += word_idx * i; | 1735 offset += word_idx * i; |
| 1674 if (transform_idx < kNumTransforms) { | 1736 if (transform_idx < kNumTransforms) { |
| 1675 const uint8_t* word = &kBrotliDictionary[offset]; | 1737 const uint8_t* word = &kBrotliDictionary[offset]; |
| 1676 int len = i; | 1738 int len = i; |
| 1677 if (transform_idx == 0) { | 1739 if (transform_idx == 0) { |
| 1678 memcpy(&s->ringbuffer[pos], word, (size_t)len); | 1740 memcpy(&s->ringbuffer[pos], word, (size_t)len); |
| 1679 } else { | 1741 } else { |
| 1680 len = TransformDictionaryWord( | 1742 len = TransformDictionaryWord( |
| 1681 &s->ringbuffer[pos], word, len, transform_idx); | 1743 &s->ringbuffer[pos], word, len, transform_idx); |
| 1682 } | 1744 } |
| 1683 pos += len; | 1745 pos += len; |
| 1684 s->meta_block_remaining_len -= len; | 1746 s->meta_block_remaining_len -= len; |
| 1685 if (pos >= s->ringbuffer_size) { | 1747 if (pos >= s->ringbuffer_size) { |
| 1686 /*s->partial_pos_rb += (size_t)s->ringbuffer_size;*/ | 1748 /*s->partial_pos_rb += (size_t)s->ringbuffer_size;*/ |
| 1687 s->state = BROTLI_STATE_COMMAND_POST_WRITE_1; | 1749 s->state = BROTLI_STATE_COMMAND_POST_WRITE_1; |
| 1688 goto saveStateAndReturn; | 1750 goto saveStateAndReturn; |
| 1689 } | 1751 } |
| 1690 } else { | 1752 } else { |
| 1691 BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d " | 1753 BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d " |
| 1692 "len: %d bytes left: %d\n", | 1754 "len: %d bytes left: %d\n", |
| 1693 pos, s->distance_code, i, s->meta_block_remaining_len)); | 1755 pos, s->distance_code, i, s->meta_block_remaining_len)); |
| 1694 return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_TRANSFORM); | 1756 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_TRANSFORM); |
| 1695 } | 1757 } |
| 1696 } else { | 1758 } else { |
| 1697 BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d " | 1759 BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d " |
| 1698 "len: %d bytes left: %d\n", | 1760 "len: %d bytes left: %d\n", |
| 1699 pos, s->distance_code, i, s->meta_block_remaining_len)); | 1761 pos, s->distance_code, i, s->meta_block_remaining_len)); |
| 1700 return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_DICTIONARY); | 1762 return BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_DICTIONARY); |
| 1701 } | 1763 } |
| 1702 } else { | 1764 } else { |
| 1703 int src_start = (pos - s->distance_code) & s->ringbuffer_mask; | 1765 int src_start = (pos - s->distance_code) & s->ringbuffer_mask; |
| 1704 uint8_t* copy_dst = &s->ringbuffer[pos]; | 1766 uint8_t* copy_dst = &s->ringbuffer[pos]; |
| 1705 uint8_t* copy_src = &s->ringbuffer[src_start]; | 1767 uint8_t* copy_src = &s->ringbuffer[src_start]; |
| 1706 int dst_end = pos + i; | 1768 int dst_end = pos + i; |
| 1707 int src_end = src_start + i; | 1769 int src_end = src_start + i; |
| 1708 /* update the recent distances cache */ | 1770 /* update the recent distances cache */ |
| 1709 s->dist_rb[s->dist_rb_idx & 3] = s->distance_code; | 1771 s->dist_rb[s->dist_rb_idx & 3] = s->distance_code; |
| 1710 ++s->dist_rb_idx; | 1772 ++s->dist_rb_idx; |
| 1711 s->meta_block_remaining_len -= i; | 1773 s->meta_block_remaining_len -= i; |
| 1712 if (PREDICT_FALSE(s->meta_block_remaining_len < 0)) { | 1774 /* There are 32+ bytes of slack in the ring-buffer allocation. |
| 1713 BROTLI_LOG(("Invalid backward reference. pos: %d distance: %d " | |
| 1714 "len: %d bytes left: %d\n", | |
| 1715 pos, s->distance_code, i, s->meta_block_remaining_len)); | |
| 1716 return BROTLI_FAILURE(BROTLI_ERROR_FORMAT_BLOCK_LENGTH_2); | |
| 1717 } | |
| 1718 /* There are 32+ bytes of slack in the ringbuffer allocation. | |
| 1719 Also, we have 16 short codes, that make these 16 bytes irrelevant | 1775 Also, we have 16 short codes, that make these 16 bytes irrelevant |
| 1720 in the ringbuffer. Let's copy over them as a first guess. | 1776 in the ring-buffer. Let's copy over them as a first guess. |
| 1721 */ | 1777 */ |
| 1722 memmove16(copy_dst, copy_src); | 1778 memmove16(copy_dst, copy_src); |
| 1723 if (src_end > pos && dst_end > src_start) { | 1779 if (src_end > pos && dst_end > src_start) { |
| 1724 /* Regions intersect. */ | 1780 /* Regions intersect. */ |
| 1725 goto CommandPostWrapCopy; | 1781 goto CommandPostWrapCopy; |
| 1726 } | 1782 } |
| 1727 if (dst_end >= s->ringbuffer_size || src_end >= s->ringbuffer_size) { | 1783 if (dst_end >= s->ringbuffer_size || src_end >= s->ringbuffer_size) { |
| 1728 /* At least one region wraps. */ | 1784 /* At least one region wraps. */ |
| 1729 goto CommandPostWrapCopy; | 1785 goto CommandPostWrapCopy; |
| 1730 } | 1786 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1747 } else { | 1803 } else { |
| 1748 goto CommandBegin; | 1804 goto CommandBegin; |
| 1749 } | 1805 } |
| 1750 CommandPostWrapCopy: | 1806 CommandPostWrapCopy: |
| 1751 { | 1807 { |
| 1752 int wrap_guard = s->ringbuffer_size - pos; | 1808 int wrap_guard = s->ringbuffer_size - pos; |
| 1753 while (--i >= 0) { | 1809 while (--i >= 0) { |
| 1754 s->ringbuffer[pos] = | 1810 s->ringbuffer[pos] = |
| 1755 s->ringbuffer[(pos - s->distance_code) & s->ringbuffer_mask]; | 1811 s->ringbuffer[(pos - s->distance_code) & s->ringbuffer_mask]; |
| 1756 ++pos; | 1812 ++pos; |
| 1757 if (PREDICT_FALSE(--wrap_guard == 0)) { | 1813 if (BROTLI_PREDICT_FALSE(--wrap_guard == 0)) { |
| 1758 s->state = BROTLI_STATE_COMMAND_POST_WRITE_2; | 1814 s->state = BROTLI_STATE_COMMAND_POST_WRITE_2; |
| 1759 goto saveStateAndReturn; | 1815 goto saveStateAndReturn; |
| 1760 } | 1816 } |
| 1761 } | 1817 } |
| 1762 } | 1818 } |
| 1763 if (s->meta_block_remaining_len <= 0) { | 1819 if (s->meta_block_remaining_len <= 0) { |
| 1764 /* Next metablock, if any */ | 1820 /* Next metablock, if any */ |
| 1765 s->state = BROTLI_STATE_METABLOCK_DONE; | 1821 s->state = BROTLI_STATE_METABLOCK_DONE; |
| 1766 goto saveStateAndReturn; | 1822 goto saveStateAndReturn; |
| 1767 } else { | 1823 } else { |
| 1768 goto CommandBegin; | 1824 goto CommandBegin; |
| 1769 } | 1825 } |
| 1770 | 1826 |
| 1771 saveStateAndReturn: | 1827 saveStateAndReturn: |
| 1772 s->pos = pos; | 1828 s->pos = pos; |
| 1773 s->loop_counter = i; | 1829 s->loop_counter = i; |
| 1774 return result; | 1830 return result; |
| 1775 } | 1831 } |
| 1776 | 1832 |
| 1777 #undef BROTLI_SAFE | 1833 #undef BROTLI_SAFE |
| 1778 | 1834 |
| 1779 static BROTLI_NOINLINE BrotliErrorCode ProcessCommands(BrotliState* s) { | 1835 static BROTLI_NOINLINE BrotliDecoderErrorCode ProcessCommands( |
| 1836 BrotliDecoderState* s) { |
| 1780 return ProcessCommandsInternal(0, s); | 1837 return ProcessCommandsInternal(0, s); |
| 1781 } | 1838 } |
| 1782 | 1839 |
| 1783 static BROTLI_NOINLINE BrotliErrorCode SafeProcessCommands(BrotliState* s) { | 1840 static BROTLI_NOINLINE BrotliDecoderErrorCode SafeProcessCommands( |
| 1841 BrotliDecoderState* s) { |
| 1784 return ProcessCommandsInternal(1, s); | 1842 return ProcessCommandsInternal(1, s); |
| 1785 } | 1843 } |
| 1786 | 1844 |
| 1787 BrotliResult BrotliDecompressBuffer(size_t encoded_size, | 1845 BrotliDecoderResult BrotliDecoderDecompress( |
| 1788 const uint8_t* encoded_buffer, | 1846 size_t encoded_size, const uint8_t* encoded_buffer, size_t* decoded_size, |
| 1789 size_t* decoded_size, | 1847 uint8_t* decoded_buffer) { |
| 1790 uint8_t* decoded_buffer) { | 1848 BrotliDecoderState s; |
| 1791 BrotliState s; | 1849 BrotliDecoderResult result; |
| 1792 BrotliResult result; | |
| 1793 size_t total_out = 0; | 1850 size_t total_out = 0; |
| 1794 size_t available_in = encoded_size; | 1851 size_t available_in = encoded_size; |
| 1795 const uint8_t* next_in = encoded_buffer; | 1852 const uint8_t* next_in = encoded_buffer; |
| 1796 size_t available_out = *decoded_size; | 1853 size_t available_out = *decoded_size; |
| 1797 uint8_t* next_out = decoded_buffer; | 1854 uint8_t* next_out = decoded_buffer; |
| 1798 BrotliStateInit(&s); | 1855 BrotliDecoderStateInit(&s); |
| 1799 result = BrotliDecompressStream(&available_in, &next_in, &available_out, | 1856 result = BrotliDecoderDecompressStream( |
| 1800 &next_out, &total_out, &s); | 1857 &s, &available_in, &next_in, &available_out, &next_out, &total_out); |
| 1801 *decoded_size = total_out; | 1858 *decoded_size = total_out; |
| 1802 BrotliStateCleanup(&s); | 1859 BrotliDecoderStateCleanup(&s); |
| 1803 if (result != BROTLI_RESULT_SUCCESS) { | 1860 if (result != BROTLI_DECODER_RESULT_SUCCESS) { |
| 1804 result = BROTLI_RESULT_ERROR; | 1861 result = BROTLI_DECODER_RESULT_ERROR; |
| 1805 } | 1862 } |
| 1806 return result; | 1863 return result; |
| 1807 } | 1864 } |
| 1808 | 1865 |
| 1809 /* Invariant: input stream is never overconsumed: | 1866 /* Invariant: input stream is never overconsumed: |
| 1810 * invalid input implies that the whole stream is invalid -> any amount of | 1867 * invalid input implies that the whole stream is invalid -> any amount of |
| 1811 input could be read and discarded | 1868 input could be read and discarded |
| 1812 * when result is "needs more input", then at leat one more byte is REQUIRED | 1869 * when result is "needs more input", then at least one more byte is REQUIRED |
| 1813 to complete decoding; all input data MUST be consumed by decoder, so | 1870 to complete decoding; all input data MUST be consumed by decoder, so |
| 1814 client could swap the input buffer | 1871 client could swap the input buffer |
| 1815 * when result is "needs more output" decoder MUST ensure that it doesn't | 1872 * when result is "needs more output" decoder MUST ensure that it doesn't |
| 1816 hold more than 7 bits in bit reader; this saves client from swapping input | 1873 hold more than 7 bits in bit reader; this saves client from swapping input |
| 1817 buffer ahead of time | 1874 buffer ahead of time |
| 1818 * when result is "success" decoder MUST return all unused data back to input | 1875 * when result is "success" decoder MUST return all unused data back to input |
| 1819 buffer; this is possible because the invariant is hold on enter | 1876 buffer; this is possible because the invariant is hold on enter |
| 1820 */ | 1877 */ |
| 1821 BrotliResult BrotliDecompressStream(size_t* available_in, | 1878 BrotliDecoderResult BrotliDecoderDecompressStream( |
| 1822 const uint8_t** next_in, size_t* available_out, uint8_t** next_out, | 1879 BrotliDecoderState* s, size_t* available_in, const uint8_t** next_in, |
| 1823 size_t* total_out, BrotliState* s) { | 1880 size_t* available_out, uint8_t** next_out, size_t* total_out) { |
| 1824 BrotliErrorCode result = BROTLI_SUCCESS; | 1881 BrotliDecoderErrorCode result = BROTLI_DECODER_SUCCESS; |
| 1825 BrotliBitReader* br = &s->br; | 1882 BrotliBitReader* br = &s->br; |
| 1883 if (*available_out && (!next_out || !*next_out)) { |
| 1884 return SaveErrorCode( |
| 1885 s, BROTLI_FAILURE(BROTLI_DECODER_ERROR_INVALID_ARGUMENTS)); |
| 1886 } |
| 1887 if (!*available_out) next_out = 0; |
| 1826 if (s->buffer_length == 0) { /* Just connect bit reader to input stream. */ | 1888 if (s->buffer_length == 0) { /* Just connect bit reader to input stream. */ |
| 1827 br->avail_in = *available_in; | 1889 br->avail_in = *available_in; |
| 1828 br->next_in = *next_in; | 1890 br->next_in = *next_in; |
| 1829 } else { | 1891 } else { |
| 1830 /* At least one byte of input is required. More than one byte of input may | 1892 /* At least one byte of input is required. More than one byte of input may |
| 1831 be required to complete the transaction -> reading more data must be | 1893 be required to complete the transaction -> reading more data must be |
| 1832 done in a loop -> do it in a main loop. */ | 1894 done in a loop -> do it in a main loop. */ |
| 1833 result = BROTLI_NEEDS_MORE_INPUT; | 1895 result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 1834 br->next_in = &s->buffer.u8[0]; | 1896 br->next_in = &s->buffer.u8[0]; |
| 1835 } | 1897 } |
| 1836 /* State machine */ | 1898 /* State machine */ |
| 1837 for (;;) { | 1899 for (;;) { |
| 1838 if (result != BROTLI_SUCCESS) { /* Error | needs more input/output */ | 1900 if (result != BROTLI_DECODER_SUCCESS) { /* Error, needs more input/output */ |
| 1839 if (result == BROTLI_NEEDS_MORE_INPUT) { | 1901 if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) { |
| 1840 if (s->ringbuffer != 0) { /* Proactively push output. */ | 1902 if (s->ringbuffer != 0) { /* Pro-actively push output. */ |
| 1841 WriteRingBuffer(available_out, next_out, total_out, s); | 1903 WriteRingBuffer(s, available_out, next_out, total_out, BROTLI_TRUE); |
| 1842 } | 1904 } |
| 1843 if (s->buffer_length != 0) { /* Used with internal buffer. */ | 1905 if (s->buffer_length != 0) { /* Used with internal buffer. */ |
| 1844 if (br->avail_in == 0) { /* Successfully finished read transaction. */ | 1906 if (br->avail_in == 0) { /* Successfully finished read transaction. */ |
| 1845 /* Accamulator contains less than 8 bits, because internal buffer | 1907 /* Accumulator contains less than 8 bits, because internal buffer |
| 1846 is expanded byte-by-byte until it is enough to complete read. */ | 1908 is expanded byte-by-byte until it is enough to complete read. */ |
| 1847 s->buffer_length = 0; | 1909 s->buffer_length = 0; |
| 1848 /* Switch to input stream and restart. */ | 1910 /* Switch to input stream and restart. */ |
| 1849 result = BROTLI_SUCCESS; | 1911 result = BROTLI_DECODER_SUCCESS; |
| 1850 br->avail_in = *available_in; | 1912 br->avail_in = *available_in; |
| 1851 br->next_in = *next_in; | 1913 br->next_in = *next_in; |
| 1852 continue; | 1914 continue; |
| 1853 } else if (*available_in != 0) { | 1915 } else if (*available_in != 0) { |
| 1854 /* Not enough data in buffer, but can take one more byte from | 1916 /* Not enough data in buffer, but can take one more byte from |
| 1855 input stream. */ | 1917 input stream. */ |
| 1856 result = BROTLI_SUCCESS; | 1918 result = BROTLI_DECODER_SUCCESS; |
| 1857 s->buffer.u8[s->buffer_length] = **next_in; | 1919 s->buffer.u8[s->buffer_length] = **next_in; |
| 1858 s->buffer_length++; | 1920 s->buffer_length++; |
| 1859 br->avail_in = s->buffer_length; | 1921 br->avail_in = s->buffer_length; |
| 1860 (*next_in)++; | 1922 (*next_in)++; |
| 1861 (*available_in)--; | 1923 (*available_in)--; |
| 1862 /* Retry with more data in buffer. */ | 1924 /* Retry with more data in buffer. */ |
| 1863 continue; | 1925 continue; |
| 1864 } | 1926 } |
| 1865 /* Can't finish reading and no more input.*/ | 1927 /* Can't finish reading and no more input.*/ |
| 1866 break; | 1928 break; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1880 } | 1942 } |
| 1881 | 1943 |
| 1882 /* Fail or needs more output. */ | 1944 /* Fail or needs more output. */ |
| 1883 | 1945 |
| 1884 if (s->buffer_length != 0) { | 1946 if (s->buffer_length != 0) { |
| 1885 /* Just consumed the buffered input and produced some output. Otherwise | 1947 /* Just consumed the buffered input and produced some output. Otherwise |
| 1886 it would result in "needs more input". Reset internal buffer.*/ | 1948 it would result in "needs more input". Reset internal buffer.*/ |
| 1887 s->buffer_length = 0; | 1949 s->buffer_length = 0; |
| 1888 } else { | 1950 } else { |
| 1889 /* Using input stream in last iteration. When decoder switches to input | 1951 /* Using input stream in last iteration. When decoder switches to input |
| 1890 stream it has less than 8 bits in accamulator, so it is safe to | 1952 stream it has less than 8 bits in accumulator, so it is safe to |
| 1891 return unused accamulator bits there. */ | 1953 return unused accumulator bits there. */ |
| 1892 BrotliBitReaderUnload(br); | 1954 BrotliBitReaderUnload(br); |
| 1893 *available_in = br->avail_in; | 1955 *available_in = br->avail_in; |
| 1894 *next_in = br->next_in; | 1956 *next_in = br->next_in; |
| 1895 } | 1957 } |
| 1896 break; | 1958 break; |
| 1897 } | 1959 } |
| 1898 switch (s->state) { | 1960 switch (s->state) { |
| 1899 case BROTLI_STATE_UNINITED: | 1961 case BROTLI_STATE_UNINITED: |
| 1900 /* Prepare to the first read. */ | 1962 /* Prepare to the first read. */ |
| 1901 if (!BrotliWarmupBitReader(br)) { | 1963 if (!BrotliWarmupBitReader(br)) { |
| 1902 result = BROTLI_NEEDS_MORE_INPUT; | 1964 result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 1903 break; | 1965 break; |
| 1904 } | 1966 } |
| 1905 /* Decode window size. */ | 1967 /* Decode window size. */ |
| 1906 s->window_bits = DecodeWindowBits(br); /* Reads 1..7 bits. */ | 1968 s->window_bits = DecodeWindowBits(br); /* Reads 1..7 bits. */ |
| 1907 BROTLI_LOG_UINT(s->window_bits); | 1969 BROTLI_LOG_UINT(s->window_bits); |
| 1908 if (s->window_bits == 9) { | 1970 if (s->window_bits == 9) { |
| 1909 /* Value 9 is reserved for future use. */ | 1971 /* Value 9 is reserved for future use. */ |
| 1910 result = BROTLI_FAILURE(BROTLI_ERROR_FORMAT_WINDOW_BITS); | 1972 result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS); |
| 1911 break; | 1973 break; |
| 1912 } | 1974 } |
| 1913 /* Maximum distance, see section 9.1. of the spec. */ | 1975 /* Maximum distance, see section 9.1. of the spec. */ |
| 1914 s->max_backward_distance = (1 << s->window_bits) - 16; | 1976 s->max_backward_distance = (1 << s->window_bits) - BROTLI_WINDOW_GAP; |
| 1915 /* Limit custom dictionary size. */ | 1977 /* Limit custom dictionary size. */ |
| 1916 if (s->custom_dict_size >= s->max_backward_distance) { | 1978 if (s->custom_dict_size >= s->max_backward_distance) { |
| 1917 s->custom_dict += s->custom_dict_size - s->max_backward_distance; | 1979 s->custom_dict += s->custom_dict_size - s->max_backward_distance; |
| 1918 s->custom_dict_size = s->max_backward_distance; | 1980 s->custom_dict_size = s->max_backward_distance; |
| 1919 } | 1981 } |
| 1920 s->max_backward_distance_minus_custom_dict_size = | |
| 1921 s->max_backward_distance - s->custom_dict_size; | |
| 1922 | 1982 |
| 1923 /* Allocate memory for both block_type_trees and block_len_trees. */ | 1983 /* Allocate memory for both block_type_trees and block_len_trees. */ |
| 1924 s->block_type_trees = (HuffmanCode*)BROTLI_ALLOC(s, | 1984 s->block_type_trees = (HuffmanCode*)BROTLI_ALLOC(s, |
| 1925 sizeof(HuffmanCode) * 3 * | 1985 sizeof(HuffmanCode) * 3 * |
| 1926 (BROTLI_HUFFMAN_MAX_SIZE_258 + BROTLI_HUFFMAN_MAX_SIZE_26)); | 1986 (BROTLI_HUFFMAN_MAX_SIZE_258 + BROTLI_HUFFMAN_MAX_SIZE_26)); |
| 1927 if (s->block_type_trees == 0) { | 1987 if (s->block_type_trees == 0) { |
| 1928 result = BROTLI_FAILURE(BROTLI_ERROR_ALLOC_BLOCK_TYPE_TREES); | 1988 result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES); |
| 1929 break; | 1989 break; |
| 1930 } | 1990 } |
| 1931 s->block_len_trees = | 1991 s->block_len_trees = |
| 1932 s->block_type_trees + 3 * BROTLI_HUFFMAN_MAX_SIZE_258; | 1992 s->block_type_trees + 3 * BROTLI_HUFFMAN_MAX_SIZE_258; |
| 1933 | 1993 |
| 1934 s->state = BROTLI_STATE_METABLOCK_BEGIN; | 1994 s->state = BROTLI_STATE_METABLOCK_BEGIN; |
| 1935 /* No break, continue to next state */ | 1995 /* No break, continue to next state */ |
| 1936 case BROTLI_STATE_METABLOCK_BEGIN: | 1996 case BROTLI_STATE_METABLOCK_BEGIN: |
| 1937 BrotliStateMetablockBegin(s); | 1997 BrotliDecoderStateMetablockBegin(s); |
| 1938 BROTLI_LOG_UINT(s->pos); | 1998 BROTLI_LOG_UINT(s->pos); |
| 1939 s->state = BROTLI_STATE_METABLOCK_HEADER; | 1999 s->state = BROTLI_STATE_METABLOCK_HEADER; |
| 1940 /* No break, continue to next state */ | 2000 /* No break, continue to next state */ |
| 1941 case BROTLI_STATE_METABLOCK_HEADER: | 2001 case BROTLI_STATE_METABLOCK_HEADER: |
| 1942 result = DecodeMetaBlockLength(s, br); /* Reads 2 - 31 bits. */ | 2002 result = DecodeMetaBlockLength(s, br); /* Reads 2 - 31 bits. */ |
| 1943 if (result != BROTLI_SUCCESS) { | 2003 if (result != BROTLI_DECODER_SUCCESS) { |
| 1944 break; | 2004 break; |
| 1945 } | 2005 } |
| 1946 BROTLI_LOG_UINT(s->is_last_metablock); | 2006 BROTLI_LOG_UINT(s->is_last_metablock); |
| 1947 BROTLI_LOG_UINT(s->meta_block_remaining_len); | 2007 BROTLI_LOG_UINT(s->meta_block_remaining_len); |
| 1948 BROTLI_LOG_UINT(s->is_metadata); | 2008 BROTLI_LOG_UINT(s->is_metadata); |
| 1949 BROTLI_LOG_UINT(s->is_uncompressed); | 2009 BROTLI_LOG_UINT(s->is_uncompressed); |
| 1950 if (s->is_metadata || s->is_uncompressed) { | 2010 if (s->is_metadata || s->is_uncompressed) { |
| 1951 if (!BrotliJumpToByteBoundary(br)) { | 2011 if (!BrotliJumpToByteBoundary(br)) { |
| 1952 result = BROTLI_FAILURE(BROTLI_ERROR_FORMAT_PADDING_1); | 2012 result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_PADDING_1); |
| 1953 break; | 2013 break; |
| 1954 } | 2014 } |
| 1955 } | 2015 } |
| 1956 if (s->is_metadata) { | 2016 if (s->is_metadata) { |
| 1957 s->state = BROTLI_STATE_METADATA; | 2017 s->state = BROTLI_STATE_METADATA; |
| 1958 break; | 2018 break; |
| 1959 } | 2019 } |
| 1960 if (s->meta_block_remaining_len == 0) { | 2020 if (s->meta_block_remaining_len == 0) { |
| 1961 s->state = BROTLI_STATE_METABLOCK_DONE; | 2021 s->state = BROTLI_STATE_METABLOCK_DONE; |
| 1962 break; | 2022 break; |
| 1963 } | 2023 } |
| 1964 if (!s->ringbuffer) { | 2024 BrotliCalculateRingBufferSize(s); |
| 1965 BrotliCalculateRingBufferSize(s, br); | |
| 1966 } | |
| 1967 if (s->is_uncompressed) { | 2025 if (s->is_uncompressed) { |
| 1968 s->state = BROTLI_STATE_UNCOMPRESSED; | 2026 s->state = BROTLI_STATE_UNCOMPRESSED; |
| 1969 break; | 2027 break; |
| 1970 } | 2028 } |
| 1971 s->loop_counter = 0; | 2029 s->loop_counter = 0; |
| 1972 s->state = BROTLI_STATE_HUFFMAN_CODE_0; | 2030 s->state = BROTLI_STATE_HUFFMAN_CODE_0; |
| 1973 break; | 2031 break; |
| 1974 case BROTLI_STATE_UNCOMPRESSED: { | 2032 case BROTLI_STATE_UNCOMPRESSED: { |
| 1975 int bytes_copied = s->meta_block_remaining_len; | 2033 int bytes_copied = s->meta_block_remaining_len; |
| 1976 result = CopyUncompressedBlockToOutput( | 2034 result = CopyUncompressedBlockToOutput( |
| 1977 available_out, next_out, total_out, s); | 2035 available_out, next_out, total_out, s); |
| 1978 bytes_copied -= s->meta_block_remaining_len; | 2036 bytes_copied -= s->meta_block_remaining_len; |
| 1979 if (result != BROTLI_SUCCESS) { | 2037 if (result != BROTLI_DECODER_SUCCESS) { |
| 1980 break; | 2038 break; |
| 1981 } | 2039 } |
| 1982 s->state = BROTLI_STATE_METABLOCK_DONE; | 2040 s->state = BROTLI_STATE_METABLOCK_DONE; |
| 1983 break; | 2041 break; |
| 1984 } | 2042 } |
| 1985 case BROTLI_STATE_METADATA: | 2043 case BROTLI_STATE_METADATA: |
| 1986 for (; s->meta_block_remaining_len > 0; --s->meta_block_remaining_len) { | 2044 for (; s->meta_block_remaining_len > 0; --s->meta_block_remaining_len) { |
| 1987 uint32_t bits; | 2045 uint32_t bits; |
| 1988 /* Read one byte and ignore it. */ | 2046 /* Read one byte and ignore it. */ |
| 1989 if (!BrotliSafeReadBits(br, 8, &bits)) { | 2047 if (!BrotliSafeReadBits(br, 8, &bits)) { |
| 1990 result = BROTLI_NEEDS_MORE_INPUT; | 2048 result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 1991 break; | 2049 break; |
| 1992 } | 2050 } |
| 1993 } | 2051 } |
| 1994 if (result == BROTLI_SUCCESS) { | 2052 if (result == BROTLI_DECODER_SUCCESS) { |
| 1995 s->state = BROTLI_STATE_METABLOCK_DONE; | 2053 s->state = BROTLI_STATE_METABLOCK_DONE; |
| 1996 } | 2054 } |
| 1997 break; | 2055 break; |
| 1998 case BROTLI_STATE_HUFFMAN_CODE_0: | 2056 case BROTLI_STATE_HUFFMAN_CODE_0: |
| 1999 if (s->loop_counter >= 3) { | 2057 if (s->loop_counter >= 3) { |
| 2000 s->state = BROTLI_STATE_METABLOCK_HEADER_2; | 2058 s->state = BROTLI_STATE_METABLOCK_HEADER_2; |
| 2001 break; | 2059 break; |
| 2002 } | 2060 } |
| 2003 /* Reads 1..11 bits. */ | 2061 /* Reads 1..11 bits. */ |
| 2004 result = DecodeVarLenUint8(s, br, &s->num_block_types[s->loop_counter]); | 2062 result = DecodeVarLenUint8(s, br, &s->num_block_types[s->loop_counter]); |
| 2005 if (result != BROTLI_SUCCESS) { | 2063 if (result != BROTLI_DECODER_SUCCESS) { |
| 2006 break; | 2064 break; |
| 2007 } | 2065 } |
| 2008 s->num_block_types[s->loop_counter]++; | 2066 s->num_block_types[s->loop_counter]++; |
| 2009 BROTLI_LOG_UINT(s->num_block_types[s->loop_counter]); | 2067 BROTLI_LOG_UINT(s->num_block_types[s->loop_counter]); |
| 2010 if (s->num_block_types[s->loop_counter] < 2) { | 2068 if (s->num_block_types[s->loop_counter] < 2) { |
| 2011 s->loop_counter++; | 2069 s->loop_counter++; |
| 2012 break; | 2070 break; |
| 2013 } | 2071 } |
| 2014 s->state = BROTLI_STATE_HUFFMAN_CODE_1; | 2072 s->state = BROTLI_STATE_HUFFMAN_CODE_1; |
| 2015 /* No break, continue to next state */ | 2073 /* No break, continue to next state */ |
| 2016 case BROTLI_STATE_HUFFMAN_CODE_1: { | 2074 case BROTLI_STATE_HUFFMAN_CODE_1: { |
| 2017 int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_258; | 2075 int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_258; |
| 2018 result = ReadHuffmanCode(s->num_block_types[s->loop_counter] + 2, | 2076 result = ReadHuffmanCode(s->num_block_types[s->loop_counter] + 2, |
| 2019 &s->block_type_trees[tree_offset], NULL, s); | 2077 &s->block_type_trees[tree_offset], NULL, s); |
| 2020 if (result != BROTLI_SUCCESS) break; | 2078 if (result != BROTLI_DECODER_SUCCESS) break; |
| 2021 s->state = BROTLI_STATE_HUFFMAN_CODE_2; | 2079 s->state = BROTLI_STATE_HUFFMAN_CODE_2; |
| 2022 /* No break, continue to next state */ | 2080 /* No break, continue to next state */ |
| 2023 } | 2081 } |
| 2024 case BROTLI_STATE_HUFFMAN_CODE_2: { | 2082 case BROTLI_STATE_HUFFMAN_CODE_2: { |
| 2025 int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26; | 2083 int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26; |
| 2026 result = ReadHuffmanCode(kNumBlockLengthCodes, | 2084 result = ReadHuffmanCode(BROTLI_NUM_BLOCK_LEN_SYMBOLS, |
| 2027 &s->block_len_trees[tree_offset], NULL, s); | 2085 &s->block_len_trees[tree_offset], NULL, s); |
| 2028 if (result != BROTLI_SUCCESS) break; | 2086 if (result != BROTLI_DECODER_SUCCESS) break; |
| 2029 s->state = BROTLI_STATE_HUFFMAN_CODE_3; | 2087 s->state = BROTLI_STATE_HUFFMAN_CODE_3; |
| 2030 /* No break, continue to next state */ | 2088 /* No break, continue to next state */ |
| 2031 } | 2089 } |
| 2032 case BROTLI_STATE_HUFFMAN_CODE_3: { | 2090 case BROTLI_STATE_HUFFMAN_CODE_3: { |
| 2033 int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26; | 2091 int tree_offset = s->loop_counter * BROTLI_HUFFMAN_MAX_SIZE_26; |
| 2034 if (!SafeReadBlockLength(s, &s->block_length[s->loop_counter], | 2092 if (!SafeReadBlockLength(s, &s->block_length[s->loop_counter], |
| 2035 &s->block_len_trees[tree_offset], br)) { | 2093 &s->block_len_trees[tree_offset], br)) { |
| 2036 result = BROTLI_NEEDS_MORE_INPUT; | 2094 result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 2037 break; | 2095 break; |
| 2038 } | 2096 } |
| 2039 BROTLI_LOG_UINT(s->block_length[s->loop_counter]); | 2097 BROTLI_LOG_UINT(s->block_length[s->loop_counter]); |
| 2040 s->loop_counter++; | 2098 s->loop_counter++; |
| 2041 s->state = BROTLI_STATE_HUFFMAN_CODE_0; | 2099 s->state = BROTLI_STATE_HUFFMAN_CODE_0; |
| 2042 break; | 2100 break; |
| 2043 } | 2101 } |
| 2044 case BROTLI_STATE_METABLOCK_HEADER_2: { | 2102 case BROTLI_STATE_METABLOCK_HEADER_2: { |
| 2045 uint32_t bits; | 2103 uint32_t bits; |
| 2046 if (!BrotliSafeReadBits(br, 6, &bits)) { | 2104 if (!BrotliSafeReadBits(br, 6, &bits)) { |
| 2047 result = BROTLI_NEEDS_MORE_INPUT; | 2105 result = BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 2048 break; | 2106 break; |
| 2049 } | 2107 } |
| 2050 s->distance_postfix_bits = bits & BitMask(2); | 2108 s->distance_postfix_bits = bits & BitMask(2); |
| 2051 bits >>= 2; | 2109 bits >>= 2; |
| 2052 s->num_direct_distance_codes = | 2110 s->num_direct_distance_codes = BROTLI_NUM_DISTANCE_SHORT_CODES + |
| 2053 NUM_DISTANCE_SHORT_CODES + (bits << s->distance_postfix_bits); | 2111 (bits << s->distance_postfix_bits); |
| 2054 BROTLI_LOG_UINT(s->num_direct_distance_codes); | 2112 BROTLI_LOG_UINT(s->num_direct_distance_codes); |
| 2055 BROTLI_LOG_UINT(s->distance_postfix_bits); | 2113 BROTLI_LOG_UINT(s->distance_postfix_bits); |
| 2056 s->distance_postfix_mask = (int)BitMask(s->distance_postfix_bits); | 2114 s->distance_postfix_mask = (int)BitMask(s->distance_postfix_bits); |
| 2057 s->context_modes = | 2115 s->context_modes = |
| 2058 (uint8_t*)BROTLI_ALLOC(s, (size_t)s->num_block_types[0]); | 2116 (uint8_t*)BROTLI_ALLOC(s, (size_t)s->num_block_types[0]); |
| 2059 if (s->context_modes == 0) { | 2117 if (s->context_modes == 0) { |
| 2060 result = BROTLI_FAILURE(BROTLI_ERROR_ALLOC_CONTEXT_MODES); | 2118 result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES); |
| 2061 break; | 2119 break; |
| 2062 } | 2120 } |
| 2063 s->loop_counter = 0; | 2121 s->loop_counter = 0; |
| 2064 s->state = BROTLI_STATE_CONTEXT_MODES; | 2122 s->state = BROTLI_STATE_CONTEXT_MODES; |
| 2065 /* No break, continue to next state */ | 2123 /* No break, continue to next state */ |
| 2066 } | 2124 } |
| 2067 case BROTLI_STATE_CONTEXT_MODES: | 2125 case BROTLI_STATE_CONTEXT_MODES: |
| 2068 result = ReadContextModes(s); | 2126 result = ReadContextModes(s); |
| 2069 if (result != BROTLI_SUCCESS) { | 2127 if (result != BROTLI_DECODER_SUCCESS) { |
| 2070 break; | 2128 break; |
| 2071 } | 2129 } |
| 2072 s->state = BROTLI_STATE_CONTEXT_MAP_1; | 2130 s->state = BROTLI_STATE_CONTEXT_MAP_1; |
| 2073 /* No break, continue to next state */ | 2131 /* No break, continue to next state */ |
| 2074 case BROTLI_STATE_CONTEXT_MAP_1: { | 2132 case BROTLI_STATE_CONTEXT_MAP_1: |
| 2075 uint32_t j; | 2133 result = DecodeContextMap( |
| 2076 result = DecodeContextMap(s->num_block_types[0] << kLiteralContextBits, | 2134 s->num_block_types[0] << BROTLI_LITERAL_CONTEXT_BITS, |
| 2077 &s->num_literal_htrees, &s->context_map, s); | 2135 &s->num_literal_htrees, &s->context_map, s); |
| 2078 if (result != BROTLI_SUCCESS) { | 2136 if (result != BROTLI_DECODER_SUCCESS) { |
| 2079 break; | 2137 break; |
| 2080 } | 2138 } |
| 2081 s->trivial_literal_context = 1; | 2139 DetectTrivialLiteralBlockTypes(s); |
| 2082 for (j = 0; j < s->num_block_types[0] << kLiteralContextBits; j++) { | 2140 s->state = BROTLI_STATE_CONTEXT_MAP_2; |
| 2083 if (s->context_map[j] != j >> kLiteralContextBits) { | 2141 /* No break, continue to next state */ |
| 2084 s->trivial_literal_context = 0; | 2142 case BROTLI_STATE_CONTEXT_MAP_2: |
| 2143 { |
| 2144 uint32_t num_distance_codes = s->num_direct_distance_codes + |
| 2145 ((2 * BROTLI_MAX_DISTANCE_BITS) << s->distance_postfix_bits); |
| 2146 BROTLI_BOOL allocation_success = BROTLI_TRUE; |
| 2147 result = DecodeContextMap( |
| 2148 s->num_block_types[2] << BROTLI_DISTANCE_CONTEXT_BITS, |
| 2149 &s->num_dist_htrees, &s->dist_context_map, s); |
| 2150 if (result != BROTLI_DECODER_SUCCESS) { |
| 2085 break; | 2151 break; |
| 2086 } | 2152 } |
| 2087 } | 2153 allocation_success &= BrotliDecoderHuffmanTreeGroupInit( |
| 2088 s->state = BROTLI_STATE_CONTEXT_MAP_2; | 2154 s, &s->literal_hgroup, BROTLI_NUM_LITERAL_SYMBOLS, |
| 2089 /* No break, continue to next state */ | 2155 s->num_literal_htrees); |
| 2090 } | 2156 allocation_success &= BrotliDecoderHuffmanTreeGroupInit( |
| 2091 case BROTLI_STATE_CONTEXT_MAP_2: | 2157 s, &s->insert_copy_hgroup, BROTLI_NUM_COMMAND_SYMBOLS, |
| 2092 { | 2158 s->num_block_types[1]); |
| 2093 uint32_t num_distance_codes = | 2159 allocation_success &= BrotliDecoderHuffmanTreeGroupInit( |
| 2094 s->num_direct_distance_codes + (48U << s->distance_postfix_bits); | 2160 s, &s->distance_hgroup, num_distance_codes, |
| 2095 result = DecodeContextMap( | 2161 s->num_dist_htrees); |
| 2096 s->num_block_types[2] << kDistanceContextBits, | 2162 if (!allocation_success) { |
| 2097 &s->num_dist_htrees, &s->dist_context_map, s); | |
| 2098 if (result != BROTLI_SUCCESS) { | |
| 2099 break; | |
| 2100 } | |
| 2101 BrotliHuffmanTreeGroupInit(s, &s->literal_hgroup, kNumLiteralCodes, | |
| 2102 s->num_literal_htrees); | |
| 2103 BrotliHuffmanTreeGroupInit(s, &s->insert_copy_hgroup, | |
| 2104 kNumInsertAndCopyCodes, | |
| 2105 s->num_block_types[1]); | |
| 2106 BrotliHuffmanTreeGroupInit(s, &s->distance_hgroup, num_distance_codes, | |
| 2107 s->num_dist_htrees); | |
| 2108 if (s->literal_hgroup.codes == 0 || | |
| 2109 s->insert_copy_hgroup.codes == 0 || | |
| 2110 s->distance_hgroup.codes == 0) { | |
| 2111 return SaveErrorCode(s, | 2163 return SaveErrorCode(s, |
| 2112 BROTLI_FAILURE(BROTLI_ERROR_ALLOC_TREE_GROUPS)); | 2164 BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS)); |
| 2113 } | 2165 } |
| 2114 } | 2166 } |
| 2115 s->loop_counter = 0; | 2167 s->loop_counter = 0; |
| 2116 s->state = BROTLI_STATE_TREE_GROUP; | 2168 s->state = BROTLI_STATE_TREE_GROUP; |
| 2117 /* No break, continue to next state */ | 2169 /* No break, continue to next state */ |
| 2118 case BROTLI_STATE_TREE_GROUP: | 2170 case BROTLI_STATE_TREE_GROUP: |
| 2119 { | 2171 { |
| 2120 HuffmanTreeGroup* hgroup = NULL; | 2172 HuffmanTreeGroup* hgroup = NULL; |
| 2121 switch (s->loop_counter) { | 2173 switch (s->loop_counter) { |
| 2122 case 0: | 2174 case 0: |
| 2123 hgroup = &s->literal_hgroup; | 2175 hgroup = &s->literal_hgroup; |
| 2124 break; | 2176 break; |
| 2125 case 1: | 2177 case 1: |
| 2126 hgroup = &s->insert_copy_hgroup; | 2178 hgroup = &s->insert_copy_hgroup; |
| 2127 break; | 2179 break; |
| 2128 case 2: | 2180 case 2: |
| 2129 hgroup = &s->distance_hgroup; | 2181 hgroup = &s->distance_hgroup; |
| 2130 break; | 2182 break; |
| 2131 default: | 2183 default: |
| 2132 return SaveErrorCode(s, | 2184 return SaveErrorCode(s, BROTLI_FAILURE( |
| 2133 BROTLI_FAILURE(BROTLI_ERROR_UNREACHABLE_6)); | 2185 BROTLI_DECODER_ERROR_UNREACHABLE)); |
| 2134 } | 2186 } |
| 2135 result = HuffmanTreeGroupDecode(hgroup, s); | 2187 result = HuffmanTreeGroupDecode(hgroup, s); |
| 2136 } | 2188 } |
| 2137 if (result != BROTLI_SUCCESS) break; | 2189 if (result != BROTLI_DECODER_SUCCESS) break; |
| 2138 s->loop_counter++; | 2190 s->loop_counter++; |
| 2139 if (s->loop_counter >= 3) { | 2191 if (s->loop_counter >= 3) { |
| 2140 uint8_t context_mode = s->context_modes[s->block_type_rb[1]]; | 2192 PrepareLiteralDecoding(s); |
| 2141 s->context_map_slice = s->context_map; | |
| 2142 s->dist_context_map_slice = s->dist_context_map; | 2193 s->dist_context_map_slice = s->dist_context_map; |
| 2143 s->context_lookup1 = | |
| 2144 &kContextLookup[kContextLookupOffsets[context_mode]]; | |
| 2145 s->context_lookup2 = | |
| 2146 &kContextLookup[kContextLookupOffsets[context_mode + 1]]; | |
| 2147 s->htree_command = s->insert_copy_hgroup.htrees[0]; | 2194 s->htree_command = s->insert_copy_hgroup.htrees[0]; |
| 2148 s->literal_htree = s->literal_hgroup.htrees[s->literal_htree_index]; | 2195 if (!BrotliEnsureRingBuffer(s)) { |
| 2149 if (!s->ringbuffer && !BrotliAllocateRingBuffer(s)) { | 2196 result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2); |
| 2150 result = BROTLI_FAILURE(BROTLI_ERROR_ALLOC_RING_BUFFER_2); | |
| 2151 break; | 2197 break; |
| 2152 } | 2198 } |
| 2153 s->state = BROTLI_STATE_COMMAND_BEGIN; | 2199 s->state = BROTLI_STATE_COMMAND_BEGIN; |
| 2154 } | 2200 } |
| 2155 break; | 2201 break; |
| 2156 case BROTLI_STATE_COMMAND_BEGIN: | 2202 case BROTLI_STATE_COMMAND_BEGIN: |
| 2157 case BROTLI_STATE_COMMAND_INNER: | 2203 case BROTLI_STATE_COMMAND_INNER: |
| 2158 case BROTLI_STATE_COMMAND_POST_DECODE_LITERALS: | 2204 case BROTLI_STATE_COMMAND_POST_DECODE_LITERALS: |
| 2159 case BROTLI_STATE_COMMAND_POST_WRAP_COPY: | 2205 case BROTLI_STATE_COMMAND_POST_WRAP_COPY: |
| 2160 result = ProcessCommands(s); | 2206 result = ProcessCommands(s); |
| 2161 if (result == BROTLI_NEEDS_MORE_INPUT) { | 2207 if (result == BROTLI_DECODER_NEEDS_MORE_INPUT) { |
| 2162 result = SafeProcessCommands(s); | 2208 result = SafeProcessCommands(s); |
| 2163 } | 2209 } |
| 2164 break; | 2210 break; |
| 2165 case BROTLI_STATE_COMMAND_INNER_WRITE: | 2211 case BROTLI_STATE_COMMAND_INNER_WRITE: |
| 2166 case BROTLI_STATE_COMMAND_POST_WRITE_1: | 2212 case BROTLI_STATE_COMMAND_POST_WRITE_1: |
| 2167 case BROTLI_STATE_COMMAND_POST_WRITE_2: | 2213 case BROTLI_STATE_COMMAND_POST_WRITE_2: |
| 2168 result = WriteRingBuffer(available_out, next_out, total_out, s); | 2214 result = WriteRingBuffer( |
| 2169 if (result != BROTLI_SUCCESS) { | 2215 s, available_out, next_out, total_out, BROTLI_FALSE); |
| 2216 if (result != BROTLI_DECODER_SUCCESS) { |
| 2170 break; | 2217 break; |
| 2171 } | 2218 } |
| 2172 s->max_distance = s->max_backward_distance; | 2219 WrapRingBuffer(s); |
| 2220 if (s->ringbuffer_size == 1 << s->window_bits) { |
| 2221 s->max_distance = s->max_backward_distance; |
| 2222 } |
| 2173 if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_1) { | 2223 if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_1) { |
| 2174 memcpy(s->ringbuffer, s->ringbuffer_end, (size_t)s->pos); | |
| 2175 if (s->meta_block_remaining_len == 0) { | 2224 if (s->meta_block_remaining_len == 0) { |
| 2176 /* Next metablock, if any */ | 2225 /* Next metablock, if any */ |
| 2177 s->state = BROTLI_STATE_METABLOCK_DONE; | 2226 s->state = BROTLI_STATE_METABLOCK_DONE; |
| 2178 } else { | 2227 } else { |
| 2179 s->state = BROTLI_STATE_COMMAND_BEGIN; | 2228 s->state = BROTLI_STATE_COMMAND_BEGIN; |
| 2180 } | 2229 } |
| 2181 break; | 2230 break; |
| 2182 } else if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_2) { | 2231 } else if (s->state == BROTLI_STATE_COMMAND_POST_WRITE_2) { |
| 2183 s->state = BROTLI_STATE_COMMAND_POST_WRAP_COPY; | 2232 s->state = BROTLI_STATE_COMMAND_POST_WRAP_COPY; |
| 2184 } else { /* BROTLI_STATE_COMMAND_INNER_WRITE */ | 2233 } else { /* BROTLI_STATE_COMMAND_INNER_WRITE */ |
| 2185 if (s->loop_counter == 0) { | 2234 if (s->loop_counter == 0) { |
| 2186 if (s->meta_block_remaining_len == 0) { | 2235 if (s->meta_block_remaining_len == 0) { |
| 2187 s->state = BROTLI_STATE_METABLOCK_DONE; | 2236 s->state = BROTLI_STATE_METABLOCK_DONE; |
| 2188 } else { | 2237 } else { |
| 2189 s->state = BROTLI_STATE_COMMAND_POST_DECODE_LITERALS; | 2238 s->state = BROTLI_STATE_COMMAND_POST_DECODE_LITERALS; |
| 2190 } | 2239 } |
| 2191 break; | 2240 break; |
| 2192 } | 2241 } |
| 2193 s->state = BROTLI_STATE_COMMAND_INNER; | 2242 s->state = BROTLI_STATE_COMMAND_INNER; |
| 2194 } | 2243 } |
| 2195 break; | 2244 break; |
| 2196 case BROTLI_STATE_METABLOCK_DONE: | 2245 case BROTLI_STATE_METABLOCK_DONE: |
| 2197 BrotliStateCleanupAfterMetablock(s); | 2246 if (s->meta_block_remaining_len < 0) { |
| 2247 result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2); |
| 2248 break; |
| 2249 } |
| 2250 BrotliDecoderStateCleanupAfterMetablock(s); |
| 2198 if (!s->is_last_metablock) { | 2251 if (!s->is_last_metablock) { |
| 2199 s->state = BROTLI_STATE_METABLOCK_BEGIN; | 2252 s->state = BROTLI_STATE_METABLOCK_BEGIN; |
| 2200 break; | 2253 break; |
| 2201 } | 2254 } |
| 2202 if (!BrotliJumpToByteBoundary(br)) { | 2255 if (!BrotliJumpToByteBoundary(br)) { |
| 2203 result = BROTLI_FAILURE(BROTLI_ERROR_FORMAT_PADDING_2); | 2256 result = BROTLI_FAILURE(BROTLI_DECODER_ERROR_FORMAT_PADDING_2); |
| 2204 break; | 2257 break; |
| 2205 } | 2258 } |
| 2206 if (s->buffer_length == 0) { | 2259 if (s->buffer_length == 0) { |
| 2207 BrotliBitReaderUnload(br); | 2260 BrotliBitReaderUnload(br); |
| 2208 *available_in = br->avail_in; | 2261 *available_in = br->avail_in; |
| 2209 *next_in = br->next_in; | 2262 *next_in = br->next_in; |
| 2210 } | 2263 } |
| 2211 s->state = BROTLI_STATE_DONE; | 2264 s->state = BROTLI_STATE_DONE; |
| 2212 /* No break, continue to next state */ | 2265 /* No break, continue to next state */ |
| 2213 case BROTLI_STATE_DONE: | 2266 case BROTLI_STATE_DONE: |
| 2214 if (s->ringbuffer != 0) { | 2267 if (s->ringbuffer != 0) { |
| 2215 result = WriteRingBuffer(available_out, next_out, total_out, s); | 2268 result = WriteRingBuffer( |
| 2216 if (result != BROTLI_SUCCESS) { | 2269 s, available_out, next_out, total_out, BROTLI_TRUE); |
| 2270 if (result != BROTLI_DECODER_SUCCESS) { |
| 2217 break; | 2271 break; |
| 2218 } | 2272 } |
| 2219 } | 2273 } |
| 2220 return SaveErrorCode(s, result); | 2274 return SaveErrorCode(s, result); |
| 2221 } | 2275 } |
| 2222 } | 2276 } |
| 2223 return SaveErrorCode(s, result); | 2277 return SaveErrorCode(s, result); |
| 2224 } | 2278 } |
| 2225 | 2279 |
| 2226 void BrotliSetCustomDictionary( | 2280 void BrotliDecoderSetCustomDictionary( |
| 2227 size_t size, const uint8_t* dict, BrotliState* s) { | 2281 BrotliDecoderState* s, size_t size, const uint8_t* dict) { |
| 2228 if (size > (1u << 24)) { | 2282 if (size > (1u << 24)) { |
| 2229 return; | 2283 return; |
| 2230 } | 2284 } |
| 2231 s->custom_dict = dict; | 2285 s->custom_dict = dict; |
| 2232 s->custom_dict_size = (int)size; | 2286 s->custom_dict_size = (int)size; |
| 2233 } | 2287 } |
| 2234 | 2288 |
| 2235 BrotliErrorCode BrotliGetErrorCode(const BrotliState* s) { | 2289 BROTLI_BOOL BrotliDecoderHasMoreOutput(const BrotliDecoderState* s) { |
| 2236 return (BrotliErrorCode)s->error_code; | 2290 return TO_BROTLI_BOOL( |
| 2291 s->ringbuffer != 0 && UnwrittenBytes(s, BROTLI_FALSE) != 0); |
| 2292 } |
| 2293 |
| 2294 const uint8_t* BrotliDecoderTakeOutput(BrotliDecoderState* s, size_t* size) { |
| 2295 uint8_t* result = 0; |
| 2296 size_t available_out = *size ? *size : 1u << 24; |
| 2297 size_t requested_out = available_out; |
| 2298 BrotliDecoderErrorCode status; |
| 2299 if (s->ringbuffer == 0) { |
| 2300 *size = 0; |
| 2301 return 0; |
| 2302 } |
| 2303 WrapRingBuffer(s); |
| 2304 status = WriteRingBuffer(s, &available_out, &result, 0, BROTLI_TRUE); |
| 2305 if (status == BROTLI_DECODER_SUCCESS || |
| 2306 status == BROTLI_DECODER_NEEDS_MORE_OUTPUT) { |
| 2307 *size = requested_out - available_out; |
| 2308 } else { |
| 2309 /* This might happen if previous decoder error code was ignored. */ |
| 2310 *size = 0; |
| 2311 result = 0; |
| 2312 } |
| 2313 return result; |
| 2314 } |
| 2315 |
| 2316 BROTLI_BOOL BrotliDecoderIsUsed(const BrotliDecoderState* s) { |
| 2317 return TO_BROTLI_BOOL(s->state != BROTLI_STATE_UNINITED || |
| 2318 BrotliGetAvailableBits(&s->br) != 0); |
| 2319 } |
| 2320 |
| 2321 BROTLI_BOOL BrotliDecoderIsFinished(const BrotliDecoderState* s) { |
| 2322 return TO_BROTLI_BOOL(s->state == BROTLI_STATE_DONE) && |
| 2323 !BrotliDecoderHasMoreOutput(s); |
| 2324 } |
| 2325 |
| 2326 BrotliDecoderErrorCode BrotliDecoderGetErrorCode(const BrotliDecoderState* s) { |
| 2327 return (BrotliDecoderErrorCode)s->error_code; |
| 2328 } |
| 2329 |
| 2330 const char* BrotliDecoderErrorString(BrotliDecoderErrorCode c) { |
| 2331 switch (c) { |
| 2332 #define BROTLI_ERROR_CODE_CASE_(PREFIX, NAME, CODE) \ |
| 2333 case BROTLI_DECODER ## PREFIX ## NAME: return #NAME; |
| 2334 #define BROTLI_NOTHING_ |
| 2335 BROTLI_DECODER_ERROR_CODES_LIST(BROTLI_ERROR_CODE_CASE_, BROTLI_NOTHING_) |
| 2336 #undef BROTLI_ERROR_CODE_CASE_ |
| 2337 #undef BROTLI_NOTHING_ |
| 2338 default: return "INVALID"; |
| 2339 } |
| 2340 } |
| 2341 |
| 2342 uint32_t BrotliDecoderVersion() { |
| 2343 return BROTLI_VERSION; |
| 2237 } | 2344 } |
| 2238 | 2345 |
| 2239 #if defined(__cplusplus) || defined(c_plusplus) | 2346 #if defined(__cplusplus) || defined(c_plusplus) |
| 2240 } /* extern "C" */ | 2347 } /* extern "C" */ |
| 2241 #endif | 2348 #endif |
| OLD | NEW |