Path: blob/master/Utilities/cmliblzma/liblzma/lzma/lzma2_decoder.c
3156 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file lzma2_decoder.c5/// \brief LZMA2 decoder6///7// Authors: Igor Pavlov8// Lasse Collin9//10///////////////////////////////////////////////////////////////////////////////1112#include "lzma2_decoder.h"13#include "lz_decoder.h"14#include "lzma_decoder.h"151617typedef struct {18enum sequence {19SEQ_CONTROL,20SEQ_UNCOMPRESSED_1,21SEQ_UNCOMPRESSED_2,22SEQ_COMPRESSED_0,23SEQ_COMPRESSED_1,24SEQ_PROPERTIES,25SEQ_LZMA,26SEQ_COPY,27} sequence;2829/// Sequence after the size fields have been decoded.30enum sequence next_sequence;3132/// LZMA decoder33lzma_lz_decoder lzma;3435/// Uncompressed size of LZMA chunk36size_t uncompressed_size;3738/// Compressed size of the chunk (naturally equals to uncompressed39/// size of uncompressed chunk)40size_t compressed_size;4142/// True if properties are needed. This is false before the43/// first LZMA chunk.44bool need_properties;4546/// True if dictionary reset is needed. This is false before the47/// first chunk (LZMA or uncompressed).48bool need_dictionary_reset;4950lzma_options_lzma options;51} lzma_lzma2_coder;525354static lzma_ret55lzma2_decode(void *coder_ptr, lzma_dict *restrict dict,56const uint8_t *restrict in, size_t *restrict in_pos,57size_t in_size)58{59lzma_lzma2_coder *restrict coder = coder_ptr;6061// With SEQ_LZMA it is possible that no new input is needed to do62// some progress. The rest of the sequences assume that there is63// at least one byte of input.64while (*in_pos < in_size || coder->sequence == SEQ_LZMA)65switch (coder->sequence) {66case SEQ_CONTROL: {67const uint32_t control = in[*in_pos];68++*in_pos;6970// End marker71if (control == 0x00)72return LZMA_STREAM_END;7374if (control >= 0xE0 || control == 1) {75// Dictionary reset implies that next LZMA chunk has76// to set new properties.77coder->need_properties = true;78coder->need_dictionary_reset = true;79} else if (coder->need_dictionary_reset) {80return LZMA_DATA_ERROR;81}8283if (control >= 0x80) {84// LZMA chunk. The highest five bits of the85// uncompressed size are taken from the control byte.86coder->uncompressed_size = (control & 0x1F) << 16;87coder->sequence = SEQ_UNCOMPRESSED_1;8889// See if there are new properties or if we need to90// reset the state.91if (control >= 0xC0) {92// When there are new properties, state reset93// is done at SEQ_PROPERTIES.94coder->need_properties = false;95coder->next_sequence = SEQ_PROPERTIES;9697} else if (coder->need_properties) {98return LZMA_DATA_ERROR;99100} else {101coder->next_sequence = SEQ_LZMA;102103// If only state reset is wanted with old104// properties, do the resetting here for105// simplicity.106if (control >= 0xA0)107coder->lzma.reset(coder->lzma.coder,108&coder->options);109}110} else {111// Invalid control values112if (control > 2)113return LZMA_DATA_ERROR;114115// It's uncompressed chunk116coder->sequence = SEQ_COMPRESSED_0;117coder->next_sequence = SEQ_COPY;118}119120if (coder->need_dictionary_reset) {121// Finish the dictionary reset and let the caller122// flush the dictionary to the actual output buffer.123coder->need_dictionary_reset = false;124dict_reset(dict);125return LZMA_OK;126}127128break;129}130131case SEQ_UNCOMPRESSED_1:132coder->uncompressed_size += (uint32_t)(in[(*in_pos)++]) << 8;133coder->sequence = SEQ_UNCOMPRESSED_2;134break;135136case SEQ_UNCOMPRESSED_2:137coder->uncompressed_size += in[(*in_pos)++] + 1U;138coder->sequence = SEQ_COMPRESSED_0;139coder->lzma.set_uncompressed(coder->lzma.coder,140coder->uncompressed_size, false);141break;142143case SEQ_COMPRESSED_0:144coder->compressed_size = (uint32_t)(in[(*in_pos)++]) << 8;145coder->sequence = SEQ_COMPRESSED_1;146break;147148case SEQ_COMPRESSED_1:149coder->compressed_size += in[(*in_pos)++] + 1U;150coder->sequence = coder->next_sequence;151break;152153case SEQ_PROPERTIES:154if (lzma_lzma_lclppb_decode(&coder->options, in[(*in_pos)++]))155return LZMA_DATA_ERROR;156157coder->lzma.reset(coder->lzma.coder, &coder->options);158159coder->sequence = SEQ_LZMA;160break;161162case SEQ_LZMA: {163// Store the start offset so that we can update164// coder->compressed_size later.165const size_t in_start = *in_pos;166167// Decode from in[] to *dict.168const lzma_ret ret = coder->lzma.code(coder->lzma.coder,169dict, in, in_pos, in_size);170171// Validate and update coder->compressed_size.172const size_t in_used = *in_pos - in_start;173if (in_used > coder->compressed_size)174return LZMA_DATA_ERROR;175176coder->compressed_size -= in_used;177178// Return if we didn't finish the chunk, or an error occurred.179if (ret != LZMA_STREAM_END)180return ret;181182// The LZMA decoder must have consumed the whole chunk now.183// We don't need to worry about uncompressed size since it184// is checked by the LZMA decoder.185if (coder->compressed_size != 0)186return LZMA_DATA_ERROR;187188coder->sequence = SEQ_CONTROL;189break;190}191192case SEQ_COPY: {193// Copy from input to the dictionary as is.194dict_write(dict, in, in_pos, in_size, &coder->compressed_size);195if (coder->compressed_size != 0)196return LZMA_OK;197198coder->sequence = SEQ_CONTROL;199break;200}201202default:203assert(0);204return LZMA_PROG_ERROR;205}206207return LZMA_OK;208}209210211static void212lzma2_decoder_end(void *coder_ptr, const lzma_allocator *allocator)213{214lzma_lzma2_coder *coder = coder_ptr;215216assert(coder->lzma.end == NULL);217lzma_free(coder->lzma.coder, allocator);218219lzma_free(coder, allocator);220221return;222}223224225static lzma_ret226lzma2_decoder_init(lzma_lz_decoder *lz, const lzma_allocator *allocator,227lzma_vli id lzma_attribute((__unused__)), const void *opt,228lzma_lz_options *lz_options)229{230lzma_lzma2_coder *coder = lz->coder;231if (coder == NULL) {232coder = lzma_alloc(sizeof(lzma_lzma2_coder), allocator);233if (coder == NULL)234return LZMA_MEM_ERROR;235236lz->coder = coder;237lz->code = &lzma2_decode;238lz->end = &lzma2_decoder_end;239240coder->lzma = LZMA_LZ_DECODER_INIT;241}242243const lzma_options_lzma *options = opt;244245coder->sequence = SEQ_CONTROL;246coder->need_properties = true;247coder->need_dictionary_reset = options->preset_dict == NULL248|| options->preset_dict_size == 0;249250return lzma_lzma_decoder_create(&coder->lzma,251allocator, options, lz_options);252}253254255extern lzma_ret256lzma_lzma2_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,257const lzma_filter_info *filters)258{259// LZMA2 can only be the last filter in the chain. This is enforced260// by the raw_decoder initialization.261assert(filters[1].init == NULL);262263return lzma_lz_decoder_init(next, allocator, filters,264&lzma2_decoder_init);265}266267268extern uint64_t269lzma_lzma2_decoder_memusage(const void *options)270{271return sizeof(lzma_lzma2_coder)272+ lzma_lzma_decoder_memusage_nocheck(options);273}274275276extern lzma_ret277lzma_lzma2_props_decode(void **options, const lzma_allocator *allocator,278const uint8_t *props, size_t props_size)279{280if (props_size != 1)281return LZMA_OPTIONS_ERROR;282283// Check that reserved bits are unset.284if (props[0] & 0xC0)285return LZMA_OPTIONS_ERROR;286287// Decode the dictionary size.288if (props[0] > 40)289return LZMA_OPTIONS_ERROR;290291lzma_options_lzma *opt = lzma_alloc(292sizeof(lzma_options_lzma), allocator);293if (opt == NULL)294return LZMA_MEM_ERROR;295296if (props[0] == 40) {297opt->dict_size = UINT32_MAX;298} else {299opt->dict_size = 2 | (props[0] & 1U);300opt->dict_size <<= props[0] / 2U + 11;301}302303opt->preset_dict = NULL;304opt->preset_dict_size = 0;305306*options = opt;307308return LZMA_OK;309}310311312