Path: blob/master/Utilities/cmliblzma/liblzma/common/alone_decoder.c
3153 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file alone_decoder.c5/// \brief Decoder for LZMA_Alone files6//7// Author: Lasse Collin8//9///////////////////////////////////////////////////////////////////////////////1011#include "alone_decoder.h"12#include "lzma_decoder.h"13#include "lz_decoder.h"141516typedef struct {17lzma_next_coder next;1819enum {20SEQ_PROPERTIES,21SEQ_DICTIONARY_SIZE,22SEQ_UNCOMPRESSED_SIZE,23SEQ_CODER_INIT,24SEQ_CODE,25} sequence;2627/// If true, reject files that are unlikely to be .lzma files.28/// If false, more non-.lzma files get accepted and will give29/// LZMA_DATA_ERROR either immediately or after a few output bytes.30bool picky;3132/// Position in the header fields33size_t pos;3435/// Uncompressed size decoded from the header36lzma_vli uncompressed_size;3738/// Memory usage limit39uint64_t memlimit;4041/// Amount of memory actually needed (only an estimate)42uint64_t memusage;4344/// Options decoded from the header needed to initialize45/// the LZMA decoder46lzma_options_lzma options;47} lzma_alone_coder;484950static lzma_ret51alone_decode(void *coder_ptr, const lzma_allocator *allocator,52const uint8_t *restrict in, size_t *restrict in_pos,53size_t in_size, uint8_t *restrict out,54size_t *restrict out_pos, size_t out_size,55lzma_action action)56{57lzma_alone_coder *coder = coder_ptr;5859while (*out_pos < out_size60&& (coder->sequence == SEQ_CODE || *in_pos < in_size))61switch (coder->sequence) {62case SEQ_PROPERTIES:63if (lzma_lzma_lclppb_decode(&coder->options, in[*in_pos]))64return LZMA_FORMAT_ERROR;6566coder->sequence = SEQ_DICTIONARY_SIZE;67++*in_pos;68break;6970case SEQ_DICTIONARY_SIZE:71coder->options.dict_size72|= (size_t)(in[*in_pos]) << (coder->pos * 8);7374if (++coder->pos == 4) {75if (coder->picky && coder->options.dict_size76!= UINT32_MAX) {77// A hack to ditch tons of false positives:78// We allow only dictionary sizes that are79// 2^n or 2^n + 2^(n-1). LZMA_Alone created80// only files with 2^n, but accepts any81// dictionary size.82uint32_t d = coder->options.dict_size - 1;83d |= d >> 2;84d |= d >> 3;85d |= d >> 4;86d |= d >> 8;87d |= d >> 16;88++d;8990if (d != coder->options.dict_size)91return LZMA_FORMAT_ERROR;92}9394coder->pos = 0;95coder->sequence = SEQ_UNCOMPRESSED_SIZE;96}9798++*in_pos;99break;100101case SEQ_UNCOMPRESSED_SIZE:102coder->uncompressed_size103|= (lzma_vli)(in[*in_pos]) << (coder->pos * 8);104++*in_pos;105if (++coder->pos < 8)106break;107108// Another hack to ditch false positives: Assume that109// if the uncompressed size is known, it must be less110// than 256 GiB.111//112// FIXME? Without picky we allow > LZMA_VLI_MAX which doesn't113// really matter in this specific situation (> LZMA_VLI_MAX is114// safe in the LZMA decoder) but it's somewhat weird still.115if (coder->picky116&& coder->uncompressed_size != LZMA_VLI_UNKNOWN117&& coder->uncompressed_size118>= (LZMA_VLI_C(1) << 38))119return LZMA_FORMAT_ERROR;120121// Use LZMA_FILTER_LZMA1EXT features to specify the122// uncompressed size and that the end marker is allowed123// even when the uncompressed size is known. Both .lzma124// header and LZMA1EXT use UINT64_MAX indicate that size125// is unknown.126coder->options.ext_flags = LZMA_LZMA1EXT_ALLOW_EOPM;127lzma_set_ext_size(coder->options, coder->uncompressed_size);128129// Calculate the memory usage so that it is ready130// for SEQ_CODER_INIT.131coder->memusage = lzma_lzma_decoder_memusage(&coder->options)132+ LZMA_MEMUSAGE_BASE;133134coder->pos = 0;135coder->sequence = SEQ_CODER_INIT;136137// Fall through138139case SEQ_CODER_INIT: {140if (coder->memusage > coder->memlimit)141return LZMA_MEMLIMIT_ERROR;142143lzma_filter_info filters[2] = {144{145.id = LZMA_FILTER_LZMA1EXT,146.init = &lzma_lzma_decoder_init,147.options = &coder->options,148}, {149.init = NULL,150}151};152153return_if_error(lzma_next_filter_init(&coder->next,154allocator, filters));155156coder->sequence = SEQ_CODE;157break;158}159160case SEQ_CODE: {161return coder->next.code(coder->next.coder,162allocator, in, in_pos, in_size,163out, out_pos, out_size, action);164}165166default:167return LZMA_PROG_ERROR;168}169170return LZMA_OK;171}172173174static void175alone_decoder_end(void *coder_ptr, const lzma_allocator *allocator)176{177lzma_alone_coder *coder = coder_ptr;178lzma_next_end(&coder->next, allocator);179lzma_free(coder, allocator);180return;181}182183184static lzma_ret185alone_decoder_memconfig(void *coder_ptr, uint64_t *memusage,186uint64_t *old_memlimit, uint64_t new_memlimit)187{188lzma_alone_coder *coder = coder_ptr;189190*memusage = coder->memusage;191*old_memlimit = coder->memlimit;192193if (new_memlimit != 0) {194if (new_memlimit < coder->memusage)195return LZMA_MEMLIMIT_ERROR;196197coder->memlimit = new_memlimit;198}199200return LZMA_OK;201}202203204extern lzma_ret205lzma_alone_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,206uint64_t memlimit, bool picky)207{208lzma_next_coder_init(&lzma_alone_decoder_init, next, allocator);209210lzma_alone_coder *coder = next->coder;211212if (coder == NULL) {213coder = lzma_alloc(sizeof(lzma_alone_coder), allocator);214if (coder == NULL)215return LZMA_MEM_ERROR;216217next->coder = coder;218next->code = &alone_decode;219next->end = &alone_decoder_end;220next->memconfig = &alone_decoder_memconfig;221coder->next = LZMA_NEXT_CODER_INIT;222}223224coder->sequence = SEQ_PROPERTIES;225coder->picky = picky;226coder->pos = 0;227coder->options.dict_size = 0;228coder->options.preset_dict = NULL;229coder->options.preset_dict_size = 0;230coder->uncompressed_size = 0;231coder->memlimit = my_max(1, memlimit);232coder->memusage = LZMA_MEMUSAGE_BASE;233234return LZMA_OK;235}236237238extern LZMA_API(lzma_ret)239lzma_alone_decoder(lzma_stream *strm, uint64_t memlimit)240{241lzma_next_strm_init(lzma_alone_decoder_init, strm, memlimit, false);242243strm->internal->supported_actions[LZMA_RUN] = true;244strm->internal->supported_actions[LZMA_FINISH] = true;245246return LZMA_OK;247}248249250