@@ -1176,7 +1176,7 @@ static BrotliDecoderErrorCode DecodeContextMap(brotli_reg_t context_map_size, |
1176 | 1176 |
|
1177 | 1177 | /* Decodes a command or literal and updates block type ring-buffer. |
1178 | 1178 | Reads 3..54 bits. */ |
1179 | | -static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength( |
| 1179 | +static BROTLI_INLINE BrotliDecoderErrorCode DecodeBlockTypeAndLength( |
1180 | 1180 | int safe, BrotliDecoderState* s, int tree_type) { |
1181 | 1181 | brotli_reg_t max_block_type = s->num_block_types[tree_type]; |
1182 | 1182 | const HuffmanCode* type_tree = &s->block_type_trees[ |
@@ -1187,7 +1187,7 @@ static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength( |
1187 | 1187 | brotli_reg_t* ringbuffer = &s->block_type_rb[tree_type * 2]; |
1188 | 1188 | brotli_reg_t block_type; |
1189 | 1189 | if (max_block_type <= 1) { |
1190 | | - return BROTLI_FALSE; |
| 1190 | + return BROTLI_DECODER_ERROR_FORMAT_BLOCK_SWITCH; |
1191 | 1191 | } |
1192 | 1192 |
|
1193 | 1193 | /* Read 0..15 + 3..39 bits. */ |
@@ -1197,11 +1197,13 @@ static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength( |
1197 | 1197 | } else { |
1198 | 1198 | BrotliBitReaderState memento; |
1199 | 1199 | BrotliBitReaderSaveState(br, &memento); |
1200 | | - if (!SafeReadSymbol(type_tree, br, &block_type)) return BROTLI_FALSE; |
| 1200 | + if (!SafeReadSymbol(type_tree, br, &block_type)) { |
| 1201 | + return BROTLI_DECODER_NEEDS_MORE_INPUT; |
| 1202 | + } |
1201 | 1203 | if (!SafeReadBlockLength(s, &s->block_length[tree_type], len_tree, br)) { |
1202 | 1204 | s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE; |
1203 | 1205 | BrotliBitReaderRestoreState(br, &memento); |
1204 | | - return BROTLI_FALSE; |
| 1206 | + return BROTLI_DECODER_NEEDS_MORE_INPUT; |
1205 | 1207 | } |
1206 | 1208 | } |
1207 | 1209 |
|
@@ -1217,7 +1219,7 @@ static BROTLI_INLINE BROTLI_BOOL DecodeBlockTypeAndLength( |
1217 | 1219 | } |
1218 | 1220 | ringbuffer[0] = ringbuffer[1]; |
1219 | 1221 | ringbuffer[1] = block_type; |
1220 | | - return BROTLI_TRUE; |
| 1222 | + return BROTLI_DECODER_SUCCESS; |
1221 | 1223 | } |
1222 | 1224 |
|
1223 | 1225 | static BROTLI_INLINE void DetectTrivialLiteralBlockTypes( |
@@ -1254,59 +1256,65 @@ static BROTLI_INLINE void PrepareLiteralDecoding(BrotliDecoderState* s) { |
1254 | 1256 |
|
1255 | 1257 | /* Decodes the block type and updates the state for literal context. |
1256 | 1258 | Reads 3..54 bits. */ |
1257 | | -static BROTLI_INLINE BROTLI_BOOL DecodeLiteralBlockSwitchInternal( |
| 1259 | +static BROTLI_INLINE BrotliDecoderErrorCode DecodeLiteralBlockSwitchInternal( |
1258 | 1260 | int safe, BrotliDecoderState* s) { |
1259 | | - if (!DecodeBlockTypeAndLength(safe, s, 0)) { |
1260 | | - return BROTLI_FALSE; |
| 1261 | + BrotliDecoderErrorCode result = DecodeBlockTypeAndLength(safe, s, 0); |
| 1262 | + if (result != BROTLI_DECODER_SUCCESS) { |
| 1263 | + return result; |
1261 | 1264 | } |
1262 | 1265 | PrepareLiteralDecoding(s); |
1263 | | - return BROTLI_TRUE; |
| 1266 | + return BROTLI_DECODER_SUCCESS; |
1264 | 1267 | } |
1265 | 1268 |
|
1266 | | -static void BROTLI_NOINLINE DecodeLiteralBlockSwitch(BrotliDecoderState* s) { |
1267 | | - DecodeLiteralBlockSwitchInternal(0, s); |
| 1269 | +static BROTLI_NOINLINE BrotliDecoderErrorCode |
| 1270 | +DecodeLiteralBlockSwitch(BrotliDecoderState* s) { |
| 1271 | + return DecodeLiteralBlockSwitchInternal(0, s); |
1268 | 1272 | } |
1269 | 1273 |
|
1270 | | -static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeLiteralBlockSwitch( |
| 1274 | +static BROTLI_NOINLINE BrotliDecoderErrorCode SafeDecodeLiteralBlockSwitch( |
1271 | 1275 | BrotliDecoderState* s) { |
1272 | 1276 | return DecodeLiteralBlockSwitchInternal(1, s); |
1273 | 1277 | } |
1274 | 1278 |
|
1275 | 1279 | /* Block switch for insert/copy length. |
1276 | 1280 | Reads 3..54 bits. */ |
1277 | | -static BROTLI_INLINE BROTLI_BOOL DecodeCommandBlockSwitchInternal( |
| 1281 | +static BROTLI_INLINE BrotliDecoderErrorCode DecodeCommandBlockSwitchInternal( |
1278 | 1282 | int safe, BrotliDecoderState* s) { |
1279 | | - if (!DecodeBlockTypeAndLength(safe, s, 1)) { |
1280 | | - return BROTLI_FALSE; |
| 1283 | + BrotliDecoderErrorCode result = DecodeBlockTypeAndLength(safe, s, 1); |
| 1284 | + if (result != BROTLI_DECODER_SUCCESS) { |
| 1285 | + return result; |
1281 | 1286 | } |
1282 | 1287 | s->htree_command = s->insert_copy_hgroup.htrees[s->block_type_rb[3]]; |
1283 | | - return BROTLI_TRUE; |
| 1288 | + return BROTLI_DECODER_SUCCESS; |
1284 | 1289 | } |
1285 | 1290 |
|
1286 | | -static void BROTLI_NOINLINE DecodeCommandBlockSwitch(BrotliDecoderState* s) { |
1287 | | - DecodeCommandBlockSwitchInternal(0, s); |
| 1291 | +static BROTLI_NOINLINE BrotliDecoderErrorCode |
| 1292 | +DecodeCommandBlockSwitch(BrotliDecoderState* s) { |
| 1293 | + return DecodeCommandBlockSwitchInternal(0, s); |
1288 | 1294 | } |
1289 | 1295 |
|
1290 | | -static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeCommandBlockSwitch( |
1291 | | - BrotliDecoderState* s) { |
| 1296 | +static BROTLI_NOINLINE BrotliDecoderErrorCode |
| 1297 | +SafeDecodeCommandBlockSwitch(BrotliDecoderState* s) { |
1292 | 1298 | return DecodeCommandBlockSwitchInternal(1, s); |
1293 | 1299 | } |
1294 | 1300 |
|
1295 | 1301 | /* Block switch for distance codes. |
1296 | 1302 | Reads 3..54 bits. */ |
1297 | | -static BROTLI_INLINE BROTLI_BOOL DecodeDistanceBlockSwitchInternal( |
| 1303 | +static BROTLI_INLINE BrotliDecoderErrorCode DecodeDistanceBlockSwitchInternal( |
1298 | 1304 | int safe, BrotliDecoderState* s) { |
1299 | | - if (!DecodeBlockTypeAndLength(safe, s, 2)) { |
1300 | | - return BROTLI_FALSE; |
| 1305 | + BrotliDecoderErrorCode result = DecodeBlockTypeAndLength(safe, s, 2); |
| 1306 | + if (result != BROTLI_DECODER_SUCCESS) { |
| 1307 | + return result; |
1301 | 1308 | } |
1302 | 1309 | s->dist_context_map_slice = s->dist_context_map + |
1303 | 1310 | (s->block_type_rb[5] << BROTLI_DISTANCE_CONTEXT_BITS); |
1304 | 1311 | s->dist_htree_index = s->dist_context_map_slice[s->distance_context]; |
1305 | | - return BROTLI_TRUE; |
| 1312 | + return BROTLI_DECODER_SUCCESS; |
1306 | 1313 | } |
1307 | 1314 |
|
1308 | | -static void BROTLI_NOINLINE DecodeDistanceBlockSwitch(BrotliDecoderState* s) { |
1309 | | - DecodeDistanceBlockSwitchInternal(0, s); |
| 1315 | +static BROTLI_NOINLINE BrotliDecoderErrorCode |
| 1316 | +DecodeDistanceBlockSwitch(BrotliDecoderState* s) { |
| 1317 | + return DecodeDistanceBlockSwitchInternal(0, s); |
1310 | 1318 | } |
1311 | 1319 |
|
1312 | 1320 | static BROTLI_BOOL BROTLI_NOINLINE SafeDecodeDistanceBlockSwitch( |
@@ -1940,6 +1948,9 @@ static BROTLI_INLINE BROTLI_BOOL CheckInputAmount( |
1940 | 1948 | return BrotliCheckInputAmount(br); |
1941 | 1949 | } |
1942 | 1950 |
|
| 1951 | +/* NB: METHOD should return BROTLI_FALSE only in case there is not enough input; |
| 1952 | + in case of "unsafe" execution, when input is guaranteed to be sufficient, |
| 1953 | + result is ignored. */ |
1943 | 1954 | #define BROTLI_SAFE(METHOD) \ |
1944 | 1955 | { \ |
1945 | 1956 | if (safe) { \ |
@@ -1952,6 +1963,22 @@ static BROTLI_INLINE BROTLI_BOOL CheckInputAmount( |
1952 | 1963 | } \ |
1953 | 1964 | } |
1954 | 1965 |
|
| 1966 | +/* NB: METHOD should return BROTLI_DECODER_SUCCESS, BROTLI_DECODER_ERROR_*, or |
| 1967 | + BROTLI_DECODER_NEEDS_MORE_INPUT; the later two break the processing. */ |
| 1968 | +#define BROTLI_SAFE_WITH_STATUS(METHOD) \ |
| 1969 | + { \ |
| 1970 | + BrotliDecoderErrorCode status; \ |
| 1971 | + if (safe) { \ |
| 1972 | + status = Safe##METHOD; \ |
| 1973 | + } else { \ |
| 1974 | + status = METHOD; \ |
| 1975 | + } \ |
| 1976 | + if (status != BROTLI_DECODER_SUCCESS) { \ |
| 1977 | + result = status; \ |
| 1978 | + goto saveStateAndReturn; \ |
| 1979 | + } \ |
| 1980 | + } |
| 1981 | + |
1955 | 1982 | static BROTLI_INLINE BrotliDecoderErrorCode ProcessCommandsInternal( |
1956 | 1983 | int safe, BrotliDecoderState* s) { |
1957 | 1984 | int pos = s->pos; |
@@ -1991,7 +2018,7 @@ static BROTLI_INLINE BrotliDecoderErrorCode ProcessCommandsInternal( |
1991 | 2018 | goto saveStateAndReturn; |
1992 | 2019 | } |
1993 | 2020 | if (BROTLI_PREDICT_FALSE(s->block_length[1] == 0)) { |
1994 | | - BROTLI_SAFE(DecodeCommandBlockSwitch(s)); |
| 2021 | + BROTLI_SAFE_WITH_STATUS(DecodeCommandBlockSwitch(s)); |
1995 | 2022 | goto CommandBegin; |
1996 | 2023 | } |
1997 | 2024 | /* Read the insert/copy length in the command. */ |
@@ -2136,7 +2163,7 @@ static BROTLI_INLINE BrotliDecoderErrorCode ProcessCommandsInternal( |
2136 | 2163 | } else { |
2137 | 2164 | /* Read distance code in the command, unless it was implicitly zero. */ |
2138 | 2165 | if (BROTLI_PREDICT_FALSE(s->block_length[2] == 0)) { |
2139 | | - BROTLI_SAFE(DecodeDistanceBlockSwitch(s)); |
| 2166 | + BROTLI_SAFE_WITH_STATUS(DecodeDistanceBlockSwitch(s)); |
2140 | 2167 | } |
2141 | 2168 | BROTLI_SAFE(ReadDistance(s, br)); |
2142 | 2169 | } |
@@ -2330,7 +2357,7 @@ static BROTLI_INLINE BrotliDecoderErrorCode ProcessCommandsInternal( |
2330 | 2357 | } |
2331 | 2358 |
|
2332 | 2359 | NextLiteralBlock: |
2333 | | - BROTLI_SAFE(DecodeLiteralBlockSwitch(s)); |
| 2360 | + BROTLI_SAFE_WITH_STATUS(DecodeLiteralBlockSwitch(s)); |
2334 | 2361 | goto CommandInner; |
2335 | 2362 |
|
2336 | 2363 | saveStateAndReturn: |
@@ -2552,6 +2579,8 @@ BrotliDecoderResult BrotliDecoderDecompressStream( |
2552 | 2579 | if (result != BROTLI_DECODER_SUCCESS) { |
2553 | 2580 | break; |
2554 | 2581 | } |
| 2582 | + BROTLI_DCHECK(s->meta_block_remaining_len <= |
| 2583 | + (int)BROTLI_BLOCK_SIZE_CAP); |
2555 | 2584 | BROTLI_LOG_UINT(s->is_last_metablock); |
2556 | 2585 | BROTLI_LOG_UINT(s->meta_block_remaining_len); |
2557 | 2586 | BROTLI_LOG_UINT(s->is_metadata); |
|