Path: blob/master/Utilities/cmliblzma/liblzma/common/block_decoder.c
3153 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file block_decoder.c5/// \brief Decodes .xz Blocks6//7// Author: Lasse Collin8//9///////////////////////////////////////////////////////////////////////////////1011#include "block_decoder.h"12#include "filter_decoder.h"13#include "check.h"141516typedef struct {17enum {18SEQ_CODE,19SEQ_PADDING,20SEQ_CHECK,21} sequence;2223/// The filters in the chain; initialized with lzma_raw_decoder_init().24lzma_next_coder next;2526/// Decoding options; we also write Compressed Size and Uncompressed27/// Size back to this structure when the decoding has been finished.28lzma_block *block;2930/// Compressed Size calculated while decoding31lzma_vli compressed_size;3233/// Uncompressed Size calculated while decoding34lzma_vli uncompressed_size;3536/// Maximum allowed Compressed Size; this takes into account the37/// size of the Block Header and Check fields when Compressed Size38/// is unknown.39lzma_vli compressed_limit;4041/// Maximum allowed Uncompressed Size.42lzma_vli uncompressed_limit;4344/// Position when reading the Check field45size_t check_pos;4647/// Check of the uncompressed data48lzma_check_state check;4950/// True if the integrity check won't be calculated and verified.51bool ignore_check;52} lzma_block_coder;535455static inline bool56is_size_valid(lzma_vli size, lzma_vli reference)57{58return reference == LZMA_VLI_UNKNOWN || reference == size;59}606162static lzma_ret63block_decode(void *coder_ptr, const lzma_allocator *allocator,64const uint8_t *restrict in, size_t *restrict in_pos,65size_t in_size, uint8_t *restrict out,66size_t *restrict out_pos, size_t out_size, lzma_action action)67{68lzma_block_coder *coder = coder_ptr;6970switch (coder->sequence) {71case SEQ_CODE: {72const size_t in_start = *in_pos;73const size_t out_start = *out_pos;7475// Limit the amount of input and output space that we give76// to the raw decoder based on the information we have77// (or don't have) from Block Header.78const size_t in_stop = *in_pos + (size_t)my_min(79in_size - *in_pos,80coder->compressed_limit - coder->compressed_size);81const size_t out_stop = *out_pos + (size_t)my_min(82out_size - *out_pos,83coder->uncompressed_limit - coder->uncompressed_size);8485const lzma_ret ret = coder->next.code(coder->next.coder,86allocator, in, in_pos, in_stop,87out, out_pos, out_stop, action);8889const size_t in_used = *in_pos - in_start;90const size_t out_used = *out_pos - out_start;9192// Because we have limited the input and output sizes,93// we know that these cannot grow too big or overflow.94coder->compressed_size += in_used;95coder->uncompressed_size += out_used;9697if (ret == LZMA_OK) {98const bool comp_done = coder->compressed_size99== coder->block->compressed_size;100const bool uncomp_done = coder->uncompressed_size101== coder->block->uncompressed_size;102103// If both input and output amounts match the sizes104// in Block Header but we still got LZMA_OK instead105// of LZMA_STREAM_END, the file is broken.106if (comp_done && uncomp_done)107return LZMA_DATA_ERROR;108109// If the decoder has consumed all the input that it110// needs but it still couldn't fill the output buffer111// or return LZMA_STREAM_END, the file is broken.112if (comp_done && *out_pos < out_size)113return LZMA_DATA_ERROR;114115// If the decoder has produced all the output but116// it still didn't return LZMA_STREAM_END or consume117// more input (for example, detecting an end of118// payload marker may need more input but produce119// no output) the file is broken.120if (uncomp_done && *in_pos < in_size)121return LZMA_DATA_ERROR;122}123124// Don't waste time updating the integrity check if it will be125// ignored. Also skip it if no new output was produced. This126// avoids null pointer + 0 (undefined behavior) when out == 0.127if (!coder->ignore_check && out_used > 0)128lzma_check_update(&coder->check, coder->block->check,129out + out_start, out_used);130131if (ret != LZMA_STREAM_END)132return ret;133134// Compressed and Uncompressed Sizes are now at their final135// values. Verify that they match the values given to us.136if (!is_size_valid(coder->compressed_size,137coder->block->compressed_size)138|| !is_size_valid(coder->uncompressed_size,139coder->block->uncompressed_size))140return LZMA_DATA_ERROR;141142// Copy the values into coder->block. The caller143// may use this information to construct Index.144coder->block->compressed_size = coder->compressed_size;145coder->block->uncompressed_size = coder->uncompressed_size;146147coder->sequence = SEQ_PADDING;148}149150// Fall through151152case SEQ_PADDING:153// Compressed Data is padded to a multiple of four bytes.154while (coder->compressed_size & 3) {155if (*in_pos >= in_size)156return LZMA_OK;157158// We use compressed_size here just get the Padding159// right. The actual Compressed Size was stored to160// coder->block already, and won't be modified by161// us anymore.162++coder->compressed_size;163164if (in[(*in_pos)++] != 0x00)165return LZMA_DATA_ERROR;166}167168if (coder->block->check == LZMA_CHECK_NONE)169return LZMA_STREAM_END;170171if (!coder->ignore_check)172lzma_check_finish(&coder->check, coder->block->check);173174coder->sequence = SEQ_CHECK;175176// Fall through177178case SEQ_CHECK: {179const size_t check_size = lzma_check_size(coder->block->check);180lzma_bufcpy(in, in_pos, in_size, coder->block->raw_check,181&coder->check_pos, check_size);182if (coder->check_pos < check_size)183return LZMA_OK;184185// Validate the Check only if we support it.186// coder->check.buffer may be uninitialized187// when the Check ID is not supported.188if (!coder->ignore_check189&& lzma_check_is_supported(coder->block->check)190&& memcmp(coder->block->raw_check,191coder->check.buffer.u8,192check_size) != 0)193return LZMA_DATA_ERROR;194195return LZMA_STREAM_END;196}197}198199return LZMA_PROG_ERROR;200}201202203static void204block_decoder_end(void *coder_ptr, const lzma_allocator *allocator)205{206lzma_block_coder *coder = coder_ptr;207lzma_next_end(&coder->next, allocator);208lzma_free(coder, allocator);209return;210}211212213extern lzma_ret214lzma_block_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,215lzma_block *block)216{217lzma_next_coder_init(&lzma_block_decoder_init, next, allocator);218219// Validate the options. lzma_block_unpadded_size() does that for us220// except for Uncompressed Size and filters. Filters are validated221// by the raw decoder.222if (lzma_block_unpadded_size(block) == 0223|| !lzma_vli_is_valid(block->uncompressed_size))224return LZMA_PROG_ERROR;225226// Allocate *next->coder if needed.227lzma_block_coder *coder = next->coder;228if (coder == NULL) {229coder = lzma_alloc(sizeof(lzma_block_coder), allocator);230if (coder == NULL)231return LZMA_MEM_ERROR;232233next->coder = coder;234next->code = &block_decode;235next->end = &block_decoder_end;236coder->next = LZMA_NEXT_CODER_INIT;237}238239// Basic initializations240coder->sequence = SEQ_CODE;241coder->block = block;242coder->compressed_size = 0;243coder->uncompressed_size = 0;244245// If Compressed Size is not known, we calculate the maximum allowed246// value so that encoded size of the Block (including Block Padding)247// is still a valid VLI and a multiple of four.248coder->compressed_limit249= block->compressed_size == LZMA_VLI_UNKNOWN250? (LZMA_VLI_MAX & ~LZMA_VLI_C(3))251- block->header_size252- lzma_check_size(block->check)253: block->compressed_size;254255// With Uncompressed Size this is simpler. If Block Header lacks256// the size info, then LZMA_VLI_MAX is the maximum possible257// Uncompressed Size.258coder->uncompressed_limit259= block->uncompressed_size == LZMA_VLI_UNKNOWN260? LZMA_VLI_MAX261: block->uncompressed_size;262263// Initialize the check. It's caller's problem if the Check ID is not264// supported, and the Block decoder cannot verify the Check field.265// Caller can test lzma_check_is_supported(block->check).266coder->check_pos = 0;267lzma_check_init(&coder->check, block->check);268269coder->ignore_check = block->version >= 1270? block->ignore_check : false;271272// Initialize the filter chain.273return lzma_raw_decoder_init(&coder->next, allocator,274block->filters);275}276277278extern LZMA_API(lzma_ret)279lzma_block_decoder(lzma_stream *strm, lzma_block *block)280{281lzma_next_strm_init(lzma_block_decoder_init, strm, block);282283strm->internal->supported_actions[LZMA_RUN] = true;284strm->internal->supported_actions[LZMA_FINISH] = true;285286return LZMA_OK;287}288289290