Path: blob/master/Utilities/cmliblzma/liblzma/lzma/lzma_decoder.c
3156 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file lzma_decoder.c5/// \brief LZMA decoder6///7// Authors: Igor Pavlov8// Lasse Collin9// Jia Tan10//11///////////////////////////////////////////////////////////////////////////////1213#include "lz_decoder.h"14#include "lzma_common.h"15#include "lzma_decoder.h"16#include "range_decoder.h"1718// The macros unroll loops with switch statements.19// Silence warnings about missing fall-through comments.20#if TUKLIB_GNUC_REQ(7, 0)21# pragma GCC diagnostic ignored "-Wimplicit-fallthrough"22#endif2324// Minimum number of input bytes to safely decode one LZMA symbol.25// The worst case is that we decode 22 bits using probabilities and 2626// direct bits. This may decode at maximum 20 bytes of input.27#define LZMA_IN_REQUIRED 20282930// Macros for (somewhat) size-optimized code.31// This is used to decode the match length (how many bytes must be repeated32// from the dictionary). This version is used in the Resumable mode and33// does not unroll any loops.34#define len_decode(target, ld, pos_state, seq) \35do { \36case seq ## _CHOICE: \37rc_if_0_safe(ld.choice, seq ## _CHOICE) { \38rc_update_0(ld.choice); \39probs = ld.low[pos_state];\40limit = LEN_LOW_SYMBOLS; \41target = MATCH_LEN_MIN; \42} else { \43rc_update_1(ld.choice); \44case seq ## _CHOICE2: \45rc_if_0_safe(ld.choice2, seq ## _CHOICE2) { \46rc_update_0(ld.choice2); \47probs = ld.mid[pos_state]; \48limit = LEN_MID_SYMBOLS; \49target = MATCH_LEN_MIN + LEN_LOW_SYMBOLS; \50} else { \51rc_update_1(ld.choice2); \52probs = ld.high; \53limit = LEN_HIGH_SYMBOLS; \54target = MATCH_LEN_MIN + LEN_LOW_SYMBOLS \55+ LEN_MID_SYMBOLS; \56} \57} \58symbol = 1; \59case seq ## _BITTREE: \60do { \61rc_bit_safe(probs[symbol], , , seq ## _BITTREE); \62} while (symbol < limit); \63target += symbol - limit; \64} while (0)656667// This is the faster version of the match length decoder that does not68// worry about being resumable. It unrolls the bittree decoding loop.69#define len_decode_fast(target, ld, pos_state) \70do { \71symbol = 1; \72rc_if_0(ld.choice) { \73rc_update_0(ld.choice); \74rc_bittree3(ld.low[pos_state], \75-LEN_LOW_SYMBOLS + MATCH_LEN_MIN); \76target = symbol; \77} else { \78rc_update_1(ld.choice); \79rc_if_0(ld.choice2) { \80rc_update_0(ld.choice2); \81rc_bittree3(ld.mid[pos_state], -LEN_MID_SYMBOLS \82+ MATCH_LEN_MIN + LEN_LOW_SYMBOLS); \83target = symbol; \84} else { \85rc_update_1(ld.choice2); \86rc_bittree8(ld.high, -LEN_HIGH_SYMBOLS \87+ MATCH_LEN_MIN \88+ LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS); \89target = symbol; \90} \91} \92} while (0)939495/// Length decoder probabilities; see comments in lzma_common.h.96typedef struct {97probability choice;98probability choice2;99probability low[POS_STATES_MAX][LEN_LOW_SYMBOLS];100probability mid[POS_STATES_MAX][LEN_MID_SYMBOLS];101probability high[LEN_HIGH_SYMBOLS];102} lzma_length_decoder;103104105typedef struct {106///////////////////107// Probabilities //108///////////////////109110/// Literals; see comments in lzma_common.h.111probability literal[LITERAL_CODERS_MAX * LITERAL_CODER_SIZE];112113/// If 1, it's a match. Otherwise it's a single 8-bit literal.114probability is_match[STATES][POS_STATES_MAX];115116/// If 1, it's a repeated match. The distance is one of rep0 .. rep3.117probability is_rep[STATES];118119/// If 0, distance of a repeated match is rep0.120/// Otherwise check is_rep1.121probability is_rep0[STATES];122123/// If 0, distance of a repeated match is rep1.124/// Otherwise check is_rep2.125probability is_rep1[STATES];126127/// If 0, distance of a repeated match is rep2. Otherwise it is rep3.128probability is_rep2[STATES];129130/// If 1, the repeated match has length of one byte. Otherwise131/// the length is decoded from rep_len_decoder.132probability is_rep0_long[STATES][POS_STATES_MAX];133134/// Probability tree for the highest two bits of the match distance.135/// There is a separate probability tree for match lengths of136/// 2 (i.e. MATCH_LEN_MIN), 3, 4, and [5, 273].137probability dist_slot[DIST_STATES][DIST_SLOTS];138139/// Probability trees for additional bits for match distance when the140/// distance is in the range [4, 127].141probability pos_special[FULL_DISTANCES - DIST_MODEL_END];142143/// Probability tree for the lowest four bits of a match distance144/// that is equal to or greater than 128.145probability pos_align[ALIGN_SIZE];146147/// Length of a normal match148lzma_length_decoder match_len_decoder;149150/// Length of a repeated match151lzma_length_decoder rep_len_decoder;152153///////////////////154// Decoder state //155///////////////////156157// Range coder158lzma_range_decoder rc;159160// Types of the most recently seen LZMA symbols161lzma_lzma_state state;162163uint32_t rep0; ///< Distance of the latest match164uint32_t rep1; ///< Distance of second latest match165uint32_t rep2; ///< Distance of third latest match166uint32_t rep3; ///< Distance of fourth latest match167168uint32_t pos_mask; // (1U << pb) - 1169uint32_t literal_context_bits;170uint32_t literal_mask;171172/// Uncompressed size as bytes, or LZMA_VLI_UNKNOWN if end of173/// payload marker is expected.174lzma_vli uncompressed_size;175176/// True if end of payload marker (EOPM) is allowed even when177/// uncompressed_size is known; false if EOPM must not be present.178/// This is ignored if uncompressed_size == LZMA_VLI_UNKNOWN.179bool allow_eopm;180181////////////////////////////////182// State of incomplete symbol //183////////////////////////////////184185/// Position where to continue the decoder loop186enum {187SEQ_NORMALIZE,188SEQ_IS_MATCH,189SEQ_LITERAL,190SEQ_LITERAL_MATCHED,191SEQ_LITERAL_WRITE,192SEQ_IS_REP,193SEQ_MATCH_LEN_CHOICE,194SEQ_MATCH_LEN_CHOICE2,195SEQ_MATCH_LEN_BITTREE,196SEQ_DIST_SLOT,197SEQ_DIST_MODEL,198SEQ_DIRECT,199SEQ_ALIGN,200SEQ_EOPM,201SEQ_IS_REP0,202SEQ_SHORTREP,203SEQ_IS_REP0_LONG,204SEQ_IS_REP1,205SEQ_IS_REP2,206SEQ_REP_LEN_CHOICE,207SEQ_REP_LEN_CHOICE2,208SEQ_REP_LEN_BITTREE,209SEQ_COPY,210} sequence;211212/// Base of the current probability tree213probability *probs;214215/// Symbol being decoded. This is also used as an index variable in216/// bittree decoders: probs[symbol]217uint32_t symbol;218219/// Used as a loop termination condition on bittree decoders and220/// direct bits decoder.221uint32_t limit;222223/// Matched literal decoder: 0x100 or 0 to help avoiding branches.224/// Bittree reverse decoders: Offset of the next bit: 1 << offset225uint32_t offset;226227/// If decoding a literal: match byte.228/// If decoding a match: length of the match.229uint32_t len;230} lzma_lzma1_decoder;231232233static lzma_ret234lzma_decode(void *coder_ptr, lzma_dict *restrict dictptr,235const uint8_t *restrict in,236size_t *restrict in_pos, size_t in_size)237{238lzma_lzma1_decoder *restrict coder = coder_ptr;239240////////////////////241// Initialization //242////////////////////243244{245const lzma_ret ret = rc_read_init(246&coder->rc, in, in_pos, in_size);247if (ret != LZMA_STREAM_END)248return ret;249}250251///////////////252// Variables //253///////////////254255// Making local copies of often-used variables improves both256// speed and readability.257258lzma_dict dict = *dictptr;259260const size_t dict_start = dict.pos;261262// Range decoder263rc_to_local(coder->rc, *in_pos, LZMA_IN_REQUIRED);264265// State266uint32_t state = coder->state;267uint32_t rep0 = coder->rep0;268uint32_t rep1 = coder->rep1;269uint32_t rep2 = coder->rep2;270uint32_t rep3 = coder->rep3;271272const uint32_t pos_mask = coder->pos_mask;273274// These variables are actually needed only if we last time ran275// out of input in the middle of the decoder loop.276probability *probs = coder->probs;277uint32_t symbol = coder->symbol;278uint32_t limit = coder->limit;279uint32_t offset = coder->offset;280uint32_t len = coder->len;281282const uint32_t literal_mask = coder->literal_mask;283const uint32_t literal_context_bits = coder->literal_context_bits;284285// Temporary variables286uint32_t pos_state = dict.pos & pos_mask;287288lzma_ret ret = LZMA_OK;289290// This is true when the next LZMA symbol is allowed to be EOPM.291// That is, if this is false, then EOPM is considered292// an invalid symbol and we will return LZMA_DATA_ERROR.293//294// EOPM is always required (not just allowed) when295// the uncompressed size isn't known. When uncompressed size296// is known, eopm_is_valid may be set to true later.297bool eopm_is_valid = coder->uncompressed_size == LZMA_VLI_UNKNOWN;298299// If uncompressed size is known and there is enough output space300// to decode all the data, limit the available buffer space so that301// the main loop won't try to decode past the end of the stream.302bool might_finish_without_eopm = false;303if (coder->uncompressed_size != LZMA_VLI_UNKNOWN304&& coder->uncompressed_size <= dict.limit - dict.pos) {305dict.limit = dict.pos + (size_t)(coder->uncompressed_size);306might_finish_without_eopm = true;307}308309// The main decoder loop. The "switch" is used to resume the decoder at310// correct location. Once resumed, the "switch" is no longer used.311// The decoder loops is split into two modes:312//313// 1 - Non-resumable mode (fast). This is used when it is guaranteed314// there is enough input to decode the next symbol. If the output315// limit is reached, then the decoder loop will save the place316// for the resumable mode to continue. This mode is not used if317// HAVE_SMALL is defined. This is faster than Resumable mode318// because it reduces the number of branches needed and allows319// for more compiler optimizations.320//321// 2 - Resumable mode (slow). This is used when a previous decoder322// loop did not have enough space in the input or output buffers323// to complete. It uses sequence enum values to set remind324// coder->sequence where to resume in the decoder loop. This325// is the only mode used when HAVE_SMALL is defined.326327switch (coder->sequence)328while (true) {329// Calculate new pos_state. This is skipped on the first loop330// since we already calculated it when setting up the local331// variables.332pos_state = dict.pos & pos_mask;333334#ifndef HAVE_SMALL335336///////////////////////////////337// Non-resumable Mode (fast) //338///////////////////////////////339340// Go to Resumable mode (1) if there is not enough input to341// safely decode any possible LZMA symbol or (2) if the342// dictionary is full, which may need special checks that343// are only done in the Resumable mode.344if (unlikely(!rc_is_fast_allowed()345|| dict.pos == dict.limit))346goto slow;347348// Decode the first bit from the next LZMA symbol.349// If the bit is a 0, then we handle it as a literal.350// If the bit is a 1, then it is a match of previously351// decoded data.352rc_if_0(coder->is_match[state][pos_state]) {353/////////////////////354// Decode literal. //355/////////////////////356357// Update the RC that we have decoded a 0.358rc_update_0(coder->is_match[state][pos_state]);359360// Get the correct probability array from lp and361// lc params.362probs = literal_subcoder(coder->literal,363literal_context_bits, literal_mask,364dict.pos, dict_get0(&dict));365366if (is_literal_state(state)) {367update_literal_normal(state);368369// Decode literal without match byte.370rc_bittree8(probs, 0);371} else {372update_literal_matched(state);373374// Decode literal with match byte.375rc_matched_literal(probs,376dict_get(&dict, rep0));377}378379// Write decoded literal to dictionary380dict_put(&dict, symbol);381continue;382}383384///////////////////385// Decode match. //386///////////////////387388// Instead of a new byte we are going to decode a389// distance-length pair. The distance represents how far390// back in the dictionary to begin copying. The length391// represents how many bytes to copy.392393rc_update_1(coder->is_match[state][pos_state]);394395rc_if_0(coder->is_rep[state]) {396///////////////////397// Simple match. //398///////////////////399400// Not a repeated match. In this case,401// the length (how many bytes to copy) must be402// decoded first. Then, the distance (where to403// start copying) is decoded.404//405// This is also how we know when we are done406// decoding. If the distance decodes to UINT32_MAX,407// then we know to stop decoding (end of payload408// marker).409410rc_update_0(coder->is_rep[state]);411update_match(state);412413// The latest three match distances are kept in414// memory in case there are repeated matches.415rep3 = rep2;416rep2 = rep1;417rep1 = rep0;418419// Decode the length of the match.420len_decode_fast(len, coder->match_len_decoder,421pos_state);422423// Next, decode the distance into rep0.424425// The next 6 bits determine how to decode the426// rest of the distance.427probs = coder->dist_slot[get_dist_state(len)];428429rc_bittree6(probs, -DIST_SLOTS);430assert(symbol <= 63);431432if (symbol < DIST_MODEL_START) {433// If the decoded symbol is < DIST_MODEL_START434// then we use its value directly as the435// match distance. No other bits are needed.436// The only possible distance values437// are [0, 3].438rep0 = symbol;439} else {440// Use the first two bits of symbol as the441// highest bits of the match distance.442443// "limit" represents the number of low bits444// to decode.445limit = (symbol >> 1) - 1;446assert(limit >= 1 && limit <= 30);447rep0 = 2 + (symbol & 1);448449if (symbol < DIST_MODEL_END) {450// When symbol is > DIST_MODEL_START,451// but symbol < DIST_MODEL_END, then452// it can decode distances between453// [4, 127].454assert(limit <= 5);455rep0 <<= limit;456assert(rep0 <= 96);457458// -1 is fine, because we start459// decoding at probs[1], not probs[0].460// NOTE: This violates the C standard,461// since we are doing pointer462// arithmetic past the beginning of463// the array.464assert((int32_t)(rep0 - symbol - 1)465>= -1);466assert((int32_t)(rep0 - symbol - 1)467<= 82);468probs = coder->pos_special + rep0469- symbol - 1;470symbol = 1;471offset = 1;472473// Variable number (1-5) of bits474// from a reverse bittree. This475// isn't worth manual unrolling.476//477// NOTE: Making one or many of the478// variables (probs, symbol, offset,479// or limit) local here (instead of480// using those declared outside the481// main loop) can affect code size482// and performance which isn't a483// surprise but it's not so clear484// what is the best.485do {486rc_bit_add_if_1(probs,487rep0, offset);488offset <<= 1;489} while (--limit > 0);490} else {491// The distance is >= 128. Decode the492// lower bits without probabilities493// except the lowest four bits.494assert(symbol >= 14);495assert(limit >= 6);496497limit -= ALIGN_BITS;498assert(limit >= 2);499500rc_direct(rep0, limit);501502// Decode the lowest four bits using503// probabilities.504rep0 <<= ALIGN_BITS;505rc_bittree_rev4(coder->pos_align);506rep0 += symbol;507508// If the end of payload marker (EOPM)509// is detected, jump to the safe code.510// The EOPM handling isn't speed511// critical at all.512//513// A final normalization is needed514// after the EOPM (there can be a515// dummy byte to read in some cases).516// If the normalization was done here517// in the fast code, it would need to518// be taken into account in the value519// of LZMA_IN_REQUIRED. Using the520// safe code allows keeping521// LZMA_IN_REQUIRED as 20 instead of522// 21.523if (rep0 == UINT32_MAX)524goto eopm;525}526}527528// Validate the distance we just decoded.529if (unlikely(!dict_is_distance_valid(&dict, rep0))) {530ret = LZMA_DATA_ERROR;531goto out;532}533534} else {535rc_update_1(coder->is_rep[state]);536537/////////////////////538// Repeated match. //539/////////////////////540541// The match distance is a value that we have decoded542// recently. The latest four match distances are543// available as rep0, rep1, rep2 and rep3. We will544// now decode which of them is the new distance.545//546// There cannot be a match if we haven't produced547// any output, so check that first.548if (unlikely(!dict_is_distance_valid(&dict, 0))) {549ret = LZMA_DATA_ERROR;550goto out;551}552553rc_if_0(coder->is_rep0[state]) {554rc_update_0(coder->is_rep0[state]);555// The distance is rep0.556557// Decode the next bit to determine if 1 byte558// should be copied from rep0 distance or559// if the number of bytes needs to be decoded.560561// If the next bit is 0, then it is a562// "Short Rep Match" and only 1 bit is copied.563// Otherwise, the length of the match is564// decoded after the "else" statement.565rc_if_0(coder->is_rep0_long[state][pos_state]) {566rc_update_0(coder->is_rep0_long[567state][pos_state]);568569update_short_rep(state);570dict_put(&dict, dict_get(&dict, rep0));571continue;572}573574// Repeating more than one byte at575// distance of rep0.576rc_update_1(coder->is_rep0_long[577state][pos_state]);578579} else {580rc_update_1(coder->is_rep0[state]);581582// The distance is rep1, rep2 or rep3. Once583// we find out which one of these three, it584// is stored to rep0 and rep1, rep2 and rep3585// are updated accordingly. There is no586// "Short Rep Match" option, so the length587// of the match must always be decoded next.588rc_if_0(coder->is_rep1[state]) {589// The distance is rep1.590rc_update_0(coder->is_rep1[state]);591592const uint32_t distance = rep1;593rep1 = rep0;594rep0 = distance;595596} else {597rc_update_1(coder->is_rep1[state]);598599rc_if_0(coder->is_rep2[state]) {600// The distance is rep2.601rc_update_0(coder->is_rep2[602state]);603604const uint32_t distance = rep2;605rep2 = rep1;606rep1 = rep0;607rep0 = distance;608609} else {610// The distance is rep3.611rc_update_1(coder->is_rep2[612state]);613614const uint32_t distance = rep3;615rep3 = rep2;616rep2 = rep1;617rep1 = rep0;618rep0 = distance;619}620}621}622623update_long_rep(state);624625// Decode the length of the repeated match.626len_decode_fast(len, coder->rep_len_decoder,627pos_state);628}629630/////////////////////////////////631// Repeat from history buffer. //632/////////////////////////////////633634// The length is always between these limits. There is no way635// to trigger the algorithm to set len outside this range.636assert(len >= MATCH_LEN_MIN);637assert(len <= MATCH_LEN_MAX);638639// Repeat len bytes from distance of rep0.640if (unlikely(dict_repeat(&dict, rep0, &len))) {641coder->sequence = SEQ_COPY;642goto out;643}644645continue;646647slow:648#endif649///////////////////////////650// Resumable Mode (slow) //651///////////////////////////652653// This is very similar to Non-resumable Mode, so most of the654// comments are not repeated. The main differences are:655// - case labels are used to resume at the correct location.656// - Loops are not unrolled.657// - Range coder macros take an extra sequence argument658// so they can save to coder->sequence the location to659// resume in case there is not enough input.660case SEQ_NORMALIZE:661case SEQ_IS_MATCH:662if (unlikely(might_finish_without_eopm663&& dict.pos == dict.limit)) {664// In rare cases there is a useless byte that needs665// to be read anyway.666rc_normalize_safe(SEQ_NORMALIZE);667668// If the range decoder state is such that we can669// be at the end of the LZMA stream, then the670// decoding is finished.671if (rc_is_finished(rc)) {672ret = LZMA_STREAM_END;673goto out;674}675676// If the caller hasn't allowed EOPM to be present677// together with known uncompressed size, then the678// LZMA stream is corrupt.679if (!coder->allow_eopm) {680ret = LZMA_DATA_ERROR;681goto out;682}683684// Otherwise continue decoding with the expectation685// that the next LZMA symbol is EOPM.686eopm_is_valid = true;687}688689rc_if_0_safe(coder->is_match[state][pos_state], SEQ_IS_MATCH) {690/////////////////////691// Decode literal. //692/////////////////////693694rc_update_0(coder->is_match[state][pos_state]);695696probs = literal_subcoder(coder->literal,697literal_context_bits, literal_mask,698dict.pos, dict_get0(&dict));699symbol = 1;700701if (is_literal_state(state)) {702update_literal_normal(state);703704// Decode literal without match byte.705// The "slow" version does not unroll706// the loop.707case SEQ_LITERAL:708do {709rc_bit_safe(probs[symbol], , ,710SEQ_LITERAL);711} while (symbol < (1 << 8));712} else {713update_literal_matched(state);714715// Decode literal with match byte.716len = (uint32_t)(dict_get(&dict, rep0)) << 1;717718offset = 0x100;719720case SEQ_LITERAL_MATCHED:721do {722const uint32_t match_bit723= len & offset;724const uint32_t subcoder_index725= offset + match_bit726+ symbol;727728rc_bit_safe(probs[subcoder_index],729offset &= ~match_bit,730offset &= match_bit,731SEQ_LITERAL_MATCHED);732733// It seems to be faster to do this734// here instead of putting it to the735// beginning of the loop and then736// putting the "case" in the middle737// of the loop.738len <<= 1;739740} while (symbol < (1 << 8));741}742743case SEQ_LITERAL_WRITE:744if (dict_put_safe(&dict, symbol)) {745coder->sequence = SEQ_LITERAL_WRITE;746goto out;747}748749continue;750}751752///////////////////753// Decode match. //754///////////////////755756rc_update_1(coder->is_match[state][pos_state]);757758case SEQ_IS_REP:759rc_if_0_safe(coder->is_rep[state], SEQ_IS_REP) {760///////////////////761// Simple match. //762///////////////////763764rc_update_0(coder->is_rep[state]);765update_match(state);766767rep3 = rep2;768rep2 = rep1;769rep1 = rep0;770771len_decode(len, coder->match_len_decoder,772pos_state, SEQ_MATCH_LEN);773774probs = coder->dist_slot[get_dist_state(len)];775symbol = 1;776777case SEQ_DIST_SLOT:778do {779rc_bit_safe(probs[symbol], , , SEQ_DIST_SLOT);780} while (symbol < DIST_SLOTS);781782symbol -= DIST_SLOTS;783assert(symbol <= 63);784785if (symbol < DIST_MODEL_START) {786rep0 = symbol;787} else {788limit = (symbol >> 1) - 1;789assert(limit >= 1 && limit <= 30);790rep0 = 2 + (symbol & 1);791792if (symbol < DIST_MODEL_END) {793assert(limit <= 5);794rep0 <<= limit;795assert(rep0 <= 96);796// -1 is fine, because we start797// decoding at probs[1], not probs[0].798// NOTE: This violates the C standard,799// since we are doing pointer800// arithmetic past the beginning of801// the array.802assert((int32_t)(rep0 - symbol - 1)803>= -1);804assert((int32_t)(rep0 - symbol - 1)805<= 82);806probs = coder->pos_special + rep0807- symbol - 1;808symbol = 1;809offset = 0;810case SEQ_DIST_MODEL:811do {812rc_bit_safe(probs[symbol], ,813rep0 += 1U << offset,814SEQ_DIST_MODEL);815} while (++offset < limit);816} else {817assert(symbol >= 14);818assert(limit >= 6);819limit -= ALIGN_BITS;820assert(limit >= 2);821case SEQ_DIRECT:822rc_direct_safe(rep0, limit,823SEQ_DIRECT);824825rep0 <<= ALIGN_BITS;826symbol = 0;827offset = 1;828case SEQ_ALIGN:829do {830rc_bit_last_safe(831coder->pos_align[832offset833+ symbol],834,835symbol += offset,836SEQ_ALIGN);837offset <<= 1;838} while (offset < ALIGN_SIZE);839840rep0 += symbol;841842if (rep0 == UINT32_MAX) {843// End of payload marker was844// found. It may only be845// present if846// - uncompressed size is847// unknown or848// - after known uncompressed849// size amount of bytes has850// been decompressed and851// caller has indicated852// that EOPM might be used853// (it's not allowed in854// LZMA2).855#ifndef HAVE_SMALL856eopm:857#endif858if (!eopm_is_valid) {859ret = LZMA_DATA_ERROR;860goto out;861}862863case SEQ_EOPM:864// LZMA1 stream with865// end-of-payload marker.866rc_normalize_safe(SEQ_EOPM);867ret = rc_is_finished(rc)868? LZMA_STREAM_END869: LZMA_DATA_ERROR;870goto out;871}872}873}874875if (unlikely(!dict_is_distance_valid(&dict, rep0))) {876ret = LZMA_DATA_ERROR;877goto out;878}879880} else {881/////////////////////882// Repeated match. //883/////////////////////884885rc_update_1(coder->is_rep[state]);886887if (unlikely(!dict_is_distance_valid(&dict, 0))) {888ret = LZMA_DATA_ERROR;889goto out;890}891892case SEQ_IS_REP0:893rc_if_0_safe(coder->is_rep0[state], SEQ_IS_REP0) {894rc_update_0(coder->is_rep0[state]);895896case SEQ_IS_REP0_LONG:897rc_if_0_safe(coder->is_rep0_long898[state][pos_state],899SEQ_IS_REP0_LONG) {900rc_update_0(coder->is_rep0_long[901state][pos_state]);902903update_short_rep(state);904905case SEQ_SHORTREP:906if (dict_put_safe(&dict,907dict_get(&dict,908rep0))) {909coder->sequence = SEQ_SHORTREP;910goto out;911}912913continue;914}915916rc_update_1(coder->is_rep0_long[917state][pos_state]);918919} else {920rc_update_1(coder->is_rep0[state]);921922case SEQ_IS_REP1:923rc_if_0_safe(coder->is_rep1[state], SEQ_IS_REP1) {924rc_update_0(coder->is_rep1[state]);925926const uint32_t distance = rep1;927rep1 = rep0;928rep0 = distance;929930} else {931rc_update_1(coder->is_rep1[state]);932case SEQ_IS_REP2:933rc_if_0_safe(coder->is_rep2[state],934SEQ_IS_REP2) {935rc_update_0(coder->is_rep2[936state]);937938const uint32_t distance = rep2;939rep2 = rep1;940rep1 = rep0;941rep0 = distance;942943} else {944rc_update_1(coder->is_rep2[945state]);946947const uint32_t distance = rep3;948rep3 = rep2;949rep2 = rep1;950rep1 = rep0;951rep0 = distance;952}953}954}955956update_long_rep(state);957958len_decode(len, coder->rep_len_decoder,959pos_state, SEQ_REP_LEN);960}961962/////////////////////////////////963// Repeat from history buffer. //964/////////////////////////////////965966assert(len >= MATCH_LEN_MIN);967assert(len <= MATCH_LEN_MAX);968969case SEQ_COPY:970if (unlikely(dict_repeat(&dict, rep0, &len))) {971coder->sequence = SEQ_COPY;972goto out;973}974}975976out:977// Save state978979// NOTE: Must not copy dict.limit.980dictptr->pos = dict.pos;981dictptr->full = dict.full;982983rc_from_local(coder->rc, *in_pos);984985coder->state = state;986coder->rep0 = rep0;987coder->rep1 = rep1;988coder->rep2 = rep2;989coder->rep3 = rep3;990991coder->probs = probs;992coder->symbol = symbol;993coder->limit = limit;994coder->offset = offset;995coder->len = len;996997// Update the remaining amount of uncompressed data if uncompressed998// size was known.999if (coder->uncompressed_size != LZMA_VLI_UNKNOWN) {1000coder->uncompressed_size -= dict.pos - dict_start;10011002// If we have gotten all the output but the decoder wants1003// to write more output, the file is corrupt. There are1004// three SEQ values where output is produced.1005if (coder->uncompressed_size == 0 && ret == LZMA_OK1006&& (coder->sequence == SEQ_LITERAL_WRITE1007|| coder->sequence == SEQ_SHORTREP1008|| coder->sequence == SEQ_COPY))1009ret = LZMA_DATA_ERROR;1010}10111012if (ret == LZMA_STREAM_END) {1013// Reset the range decoder so that it is ready to reinitialize1014// for a new LZMA2 chunk.1015rc_reset(coder->rc);1016coder->sequence = SEQ_IS_MATCH;1017}10181019return ret;1020}102110221023static void1024lzma_decoder_uncompressed(void *coder_ptr, lzma_vli uncompressed_size,1025bool allow_eopm)1026{1027lzma_lzma1_decoder *coder = coder_ptr;1028coder->uncompressed_size = uncompressed_size;1029coder->allow_eopm = allow_eopm;1030}103110321033static void1034lzma_decoder_reset(void *coder_ptr, const void *opt)1035{1036lzma_lzma1_decoder *coder = coder_ptr;1037const lzma_options_lzma *options = opt;10381039// NOTE: We assume that lc/lp/pb are valid since they were1040// successfully decoded with lzma_lzma_decode_properties().10411042// Calculate pos_mask. We don't need pos_bits as is for anything.1043coder->pos_mask = (1U << options->pb) - 1;10441045// Initialize the literal decoder.1046literal_init(coder->literal, options->lc, options->lp);10471048coder->literal_context_bits = options->lc;1049coder->literal_mask = literal_mask_calc(options->lc, options->lp);10501051// State1052coder->state = STATE_LIT_LIT;1053coder->rep0 = 0;1054coder->rep1 = 0;1055coder->rep2 = 0;1056coder->rep3 = 0;1057coder->pos_mask = (1U << options->pb) - 1;10581059// Range decoder1060rc_reset(coder->rc);10611062// Bit and bittree decoders1063for (uint32_t i = 0; i < STATES; ++i) {1064for (uint32_t j = 0; j <= coder->pos_mask; ++j) {1065bit_reset(coder->is_match[i][j]);1066bit_reset(coder->is_rep0_long[i][j]);1067}10681069bit_reset(coder->is_rep[i]);1070bit_reset(coder->is_rep0[i]);1071bit_reset(coder->is_rep1[i]);1072bit_reset(coder->is_rep2[i]);1073}10741075for (uint32_t i = 0; i < DIST_STATES; ++i)1076bittree_reset(coder->dist_slot[i], DIST_SLOT_BITS);10771078for (uint32_t i = 0; i < FULL_DISTANCES - DIST_MODEL_END; ++i)1079bit_reset(coder->pos_special[i]);10801081bittree_reset(coder->pos_align, ALIGN_BITS);10821083// Len decoders (also bit/bittree)1084const uint32_t num_pos_states = 1U << options->pb;1085bit_reset(coder->match_len_decoder.choice);1086bit_reset(coder->match_len_decoder.choice2);1087bit_reset(coder->rep_len_decoder.choice);1088bit_reset(coder->rep_len_decoder.choice2);10891090for (uint32_t pos_state = 0; pos_state < num_pos_states; ++pos_state) {1091bittree_reset(coder->match_len_decoder.low[pos_state],1092LEN_LOW_BITS);1093bittree_reset(coder->match_len_decoder.mid[pos_state],1094LEN_MID_BITS);10951096bittree_reset(coder->rep_len_decoder.low[pos_state],1097LEN_LOW_BITS);1098bittree_reset(coder->rep_len_decoder.mid[pos_state],1099LEN_MID_BITS);1100}11011102bittree_reset(coder->match_len_decoder.high, LEN_HIGH_BITS);1103bittree_reset(coder->rep_len_decoder.high, LEN_HIGH_BITS);11041105coder->sequence = SEQ_IS_MATCH;1106coder->probs = NULL;1107coder->symbol = 0;1108coder->limit = 0;1109coder->offset = 0;1110coder->len = 0;11111112return;1113}111411151116extern lzma_ret1117lzma_lzma_decoder_create(lzma_lz_decoder *lz, const lzma_allocator *allocator,1118const lzma_options_lzma *options, lzma_lz_options *lz_options)1119{1120if (lz->coder == NULL) {1121lz->coder = lzma_alloc(sizeof(lzma_lzma1_decoder), allocator);1122if (lz->coder == NULL)1123return LZMA_MEM_ERROR;11241125lz->code = &lzma_decode;1126lz->reset = &lzma_decoder_reset;1127lz->set_uncompressed = &lzma_decoder_uncompressed;1128}11291130// All dictionary sizes are OK here. LZ decoder will take care of1131// the special cases.1132lz_options->dict_size = options->dict_size;1133lz_options->preset_dict = options->preset_dict;1134lz_options->preset_dict_size = options->preset_dict_size;11351136return LZMA_OK;1137}113811391140/// Allocate and initialize LZMA decoder. This is used only via LZ1141/// initialization (lzma_lzma_decoder_init() passes function pointer to1142/// the LZ initialization).1143static lzma_ret1144lzma_decoder_init(lzma_lz_decoder *lz, const lzma_allocator *allocator,1145lzma_vli id, const void *options, lzma_lz_options *lz_options)1146{1147if (!is_lclppb_valid(options))1148return LZMA_PROG_ERROR;11491150lzma_vli uncomp_size = LZMA_VLI_UNKNOWN;1151bool allow_eopm = true;11521153if (id == LZMA_FILTER_LZMA1EXT) {1154const lzma_options_lzma *opt = options;11551156// Only one flag is supported.1157if (opt->ext_flags & ~LZMA_LZMA1EXT_ALLOW_EOPM)1158return LZMA_OPTIONS_ERROR;11591160// FIXME? Using lzma_vli instead of uint64_t is weird because1161// this has nothing to do with .xz headers and variable-length1162// integer encoding. On the other hand, using LZMA_VLI_UNKNOWN1163// instead of UINT64_MAX is clearer when unknown size is1164// meant. A problem with using lzma_vli is that now we1165// allow > LZMA_VLI_MAX which is fine in this file but1166// it's still confusing. Note that alone_decoder.c also1167// allows > LZMA_VLI_MAX when setting uncompressed size.1168uncomp_size = opt->ext_size_low1169+ ((uint64_t)(opt->ext_size_high) << 32);1170allow_eopm = (opt->ext_flags & LZMA_LZMA1EXT_ALLOW_EOPM) != 01171|| uncomp_size == LZMA_VLI_UNKNOWN;1172}11731174return_if_error(lzma_lzma_decoder_create(1175lz, allocator, options, lz_options));11761177lzma_decoder_reset(lz->coder, options);1178lzma_decoder_uncompressed(lz->coder, uncomp_size, allow_eopm);11791180return LZMA_OK;1181}118211831184extern lzma_ret1185lzma_lzma_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,1186const lzma_filter_info *filters)1187{1188// LZMA can only be the last filter in the chain. This is enforced1189// by the raw_decoder initialization.1190assert(filters[1].init == NULL);11911192return lzma_lz_decoder_init(next, allocator, filters,1193&lzma_decoder_init);1194}119511961197extern bool1198lzma_lzma_lclppb_decode(lzma_options_lzma *options, uint8_t byte)1199{1200if (byte > (4 * 5 + 4) * 9 + 8)1201return true;12021203// See the file format specification to understand this.1204options->pb = byte / (9 * 5);1205byte -= options->pb * 9 * 5;1206options->lp = byte / 9;1207options->lc = byte - options->lp * 9;12081209return options->lc + options->lp > LZMA_LCLP_MAX;1210}121112121213extern uint64_t1214lzma_lzma_decoder_memusage_nocheck(const void *options)1215{1216const lzma_options_lzma *const opt = options;1217return sizeof(lzma_lzma1_decoder)1218+ lzma_lz_decoder_memusage(opt->dict_size);1219}122012211222extern uint64_t1223lzma_lzma_decoder_memusage(const void *options)1224{1225if (!is_lclppb_valid(options))1226return UINT64_MAX;12271228return lzma_lzma_decoder_memusage_nocheck(options);1229}123012311232extern lzma_ret1233lzma_lzma_props_decode(void **options, const lzma_allocator *allocator,1234const uint8_t *props, size_t props_size)1235{1236if (props_size != 5)1237return LZMA_OPTIONS_ERROR;12381239lzma_options_lzma *opt1240= lzma_alloc(sizeof(lzma_options_lzma), allocator);1241if (opt == NULL)1242return LZMA_MEM_ERROR;12431244if (lzma_lzma_lclppb_decode(opt, props[0]))1245goto error;12461247// All dictionary sizes are accepted, including zero. LZ decoder1248// will automatically use a dictionary at least a few KiB even if1249// a smaller dictionary is requested.1250opt->dict_size = read32le(props + 1);12511252opt->preset_dict = NULL;1253opt->preset_dict_size = 0;12541255*options = opt;12561257return LZMA_OK;12581259error:1260lzma_free(opt, allocator);1261return LZMA_OPTIONS_ERROR;1262}126312641265