lzma_decoder.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file lzma_decoder.c
  4. /// \brief LZMA decoder
  5. ///
  6. // Authors: Igor Pavlov
  7. // Lasse Collin
  8. //
  9. // This file has been put into the public domain.
  10. // You can do whatever you want with this file.
  11. //
  12. ///////////////////////////////////////////////////////////////////////////////
  13. #include "lz_decoder.h"
  14. #include "lzma_common.h"
  15. #include "lzma_decoder.h"
  16. #include "range_decoder.h"
  17. #ifdef HAVE_SMALL
  18. // Macros for (somewhat) size-optimized code.
  19. #define seq_4(seq) seq
  20. #define seq_6(seq) seq
  21. #define seq_8(seq) seq
  22. #define seq_len(seq) \
  23. seq ## _CHOICE, \
  24. seq ## _CHOICE2, \
  25. seq ## _BITTREE
  26. #define len_decode(target, ld, pos_state, seq) \
  27. do { \
  28. case seq ## _CHOICE: \
  29. rc_if_0(ld.choice, seq ## _CHOICE) { \
  30. rc_update_0(ld.choice); \
  31. probs = ld.low[pos_state];\
  32. limit = LEN_LOW_SYMBOLS; \
  33. target = MATCH_LEN_MIN; \
  34. } else { \
  35. rc_update_1(ld.choice); \
  36. case seq ## _CHOICE2: \
  37. rc_if_0(ld.choice2, seq ## _CHOICE2) { \
  38. rc_update_0(ld.choice2); \
  39. probs = ld.mid[pos_state]; \
  40. limit = LEN_MID_SYMBOLS; \
  41. target = MATCH_LEN_MIN + LEN_LOW_SYMBOLS; \
  42. } else { \
  43. rc_update_1(ld.choice2); \
  44. probs = ld.high; \
  45. limit = LEN_HIGH_SYMBOLS; \
  46. target = MATCH_LEN_MIN + LEN_LOW_SYMBOLS \
  47. + LEN_MID_SYMBOLS; \
  48. } \
  49. } \
  50. symbol = 1; \
  51. case seq ## _BITTREE: \
  52. do { \
  53. rc_bit(probs[symbol], , , seq ## _BITTREE); \
  54. } while (symbol < limit); \
  55. target += symbol - limit; \
  56. } while (0)
  57. #else // HAVE_SMALL
  58. // Unrolled versions
  59. #define seq_4(seq) \
  60. seq ## 0, \
  61. seq ## 1, \
  62. seq ## 2, \
  63. seq ## 3
  64. #define seq_6(seq) \
  65. seq ## 0, \
  66. seq ## 1, \
  67. seq ## 2, \
  68. seq ## 3, \
  69. seq ## 4, \
  70. seq ## 5
  71. #define seq_8(seq) \
  72. seq ## 0, \
  73. seq ## 1, \
  74. seq ## 2, \
  75. seq ## 3, \
  76. seq ## 4, \
  77. seq ## 5, \
  78. seq ## 6, \
  79. seq ## 7
  80. #define seq_len(seq) \
  81. seq ## _CHOICE, \
  82. seq ## _LOW0, \
  83. seq ## _LOW1, \
  84. seq ## _LOW2, \
  85. seq ## _CHOICE2, \
  86. seq ## _MID0, \
  87. seq ## _MID1, \
  88. seq ## _MID2, \
  89. seq ## _HIGH0, \
  90. seq ## _HIGH1, \
  91. seq ## _HIGH2, \
  92. seq ## _HIGH3, \
  93. seq ## _HIGH4, \
  94. seq ## _HIGH5, \
  95. seq ## _HIGH6, \
  96. seq ## _HIGH7
  97. #define len_decode(target, ld, pos_state, seq) \
  98. do { \
  99. symbol = 1; \
  100. case seq ## _CHOICE: \
  101. rc_if_0(ld.choice, seq ## _CHOICE) { \
  102. rc_update_0(ld.choice); \
  103. rc_bit_case(ld.low[pos_state][symbol], 0, 0, seq ## _LOW0); \
  104. rc_bit_case(ld.low[pos_state][symbol], 0, 0, seq ## _LOW1); \
  105. rc_bit_case(ld.low[pos_state][symbol], 0, 0, seq ## _LOW2); \
  106. target = symbol - LEN_LOW_SYMBOLS + MATCH_LEN_MIN; \
  107. } else { \
  108. rc_update_1(ld.choice); \
  109. case seq ## _CHOICE2: \
  110. rc_if_0(ld.choice2, seq ## _CHOICE2) { \
  111. rc_update_0(ld.choice2); \
  112. rc_bit_case(ld.mid[pos_state][symbol], 0, 0, \
  113. seq ## _MID0); \
  114. rc_bit_case(ld.mid[pos_state][symbol], 0, 0, \
  115. seq ## _MID1); \
  116. rc_bit_case(ld.mid[pos_state][symbol], 0, 0, \
  117. seq ## _MID2); \
  118. target = symbol - LEN_MID_SYMBOLS \
  119. + MATCH_LEN_MIN + LEN_LOW_SYMBOLS; \
  120. } else { \
  121. rc_update_1(ld.choice2); \
  122. rc_bit_case(ld.high[symbol], 0, 0, seq ## _HIGH0); \
  123. rc_bit_case(ld.high[symbol], 0, 0, seq ## _HIGH1); \
  124. rc_bit_case(ld.high[symbol], 0, 0, seq ## _HIGH2); \
  125. rc_bit_case(ld.high[symbol], 0, 0, seq ## _HIGH3); \
  126. rc_bit_case(ld.high[symbol], 0, 0, seq ## _HIGH4); \
  127. rc_bit_case(ld.high[symbol], 0, 0, seq ## _HIGH5); \
  128. rc_bit_case(ld.high[symbol], 0, 0, seq ## _HIGH6); \
  129. rc_bit_case(ld.high[symbol], 0, 0, seq ## _HIGH7); \
  130. target = symbol - LEN_HIGH_SYMBOLS \
  131. + MATCH_LEN_MIN \
  132. + LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS; \
  133. } \
  134. } \
  135. } while (0)
  136. #endif // HAVE_SMALL
  137. /// Length decoder probabilities; see comments in lzma_common.h.
  138. typedef struct {
  139. probability choice;
  140. probability choice2;
  141. probability low[POS_STATES_MAX][LEN_LOW_SYMBOLS];
  142. probability mid[POS_STATES_MAX][LEN_MID_SYMBOLS];
  143. probability high[LEN_HIGH_SYMBOLS];
  144. } lzma_length_decoder;
  145. struct lzma_coder_s {
  146. ///////////////////
  147. // Probabilities //
  148. ///////////////////
  149. /// Literals; see comments in lzma_common.h.
  150. probability literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE];
  151. /// If 1, it's a match. Otherwise it's a single 8-bit literal.
  152. probability is_match[STATES][POS_STATES_MAX];
  153. /// If 1, it's a repeated match. The distance is one of rep0 .. rep3.
  154. probability is_rep[STATES];
  155. /// If 0, distance of a repeated match is rep0.
  156. /// Otherwise check is_rep1.
  157. probability is_rep0[STATES];
  158. /// If 0, distance of a repeated match is rep1.
  159. /// Otherwise check is_rep2.
  160. probability is_rep1[STATES];
  161. /// If 0, distance of a repeated match is rep2. Otherwise it is rep3.
  162. probability is_rep2[STATES];
  163. /// If 1, the repeated match has length of one byte. Otherwise
  164. /// the length is decoded from rep_len_decoder.
  165. probability is_rep0_long[STATES][POS_STATES_MAX];
  166. /// Probability tree for the highest two bits of the match distance.
  167. /// There is a separate probability tree for match lengths of
  168. /// 2 (i.e. MATCH_LEN_MIN), 3, 4, and [5, 273].
  169. probability pos_slot[LEN_TO_POS_STATES][POS_SLOTS];
  170. /// Probability trees for additional bits for match distance when the
  171. /// distance is in the range [4, 127].
  172. probability pos_special[FULL_DISTANCES - END_POS_MODEL_INDEX];
  173. /// Probability tree for the lowest four bits of a match distance
  174. /// that is equal to or greater than 128.
  175. probability pos_align[ALIGN_TABLE_SIZE];
  176. /// Length of a normal match
  177. lzma_length_decoder match_len_decoder;
  178. /// Length of a repeated match
  179. lzma_length_decoder rep_len_decoder;
  180. ///////////////////
  181. // Decoder state //
  182. ///////////////////
  183. // Range coder
  184. lzma_range_decoder rc;
  185. // Types of the most recently seen LZMA symbols
  186. lzma_lzma_state state;
  187. uint32_t rep0; ///< Distance of the latest match
  188. uint32_t rep1; ///< Distance of second latest match
  189. uint32_t rep2; ///< Distance of third latest match
  190. uint32_t rep3; ///< Distance of fourth latest match
  191. uint32_t pos_mask; // (1U << pb) - 1
  192. uint32_t literal_context_bits;
  193. uint32_t literal_pos_mask;
  194. /// Uncompressed size as bytes, or LZMA_VLI_UNKNOWN if end of
  195. /// payload marker is expected.
  196. lzma_vli uncompressed_size;
  197. ////////////////////////////////
  198. // State of incomplete symbol //
  199. ////////////////////////////////
  200. /// Position where to continue the decoder loop
  201. enum {
  202. SEQ_NORMALIZE,
  203. SEQ_IS_MATCH,
  204. seq_8(SEQ_LITERAL),
  205. seq_8(SEQ_LITERAL_MATCHED),
  206. SEQ_LITERAL_WRITE,
  207. SEQ_IS_REP,
  208. seq_len(SEQ_MATCH_LEN),
  209. seq_6(SEQ_POS_SLOT),
  210. SEQ_POS_MODEL,
  211. SEQ_DIRECT,
  212. seq_4(SEQ_ALIGN),
  213. SEQ_EOPM,
  214. SEQ_IS_REP0,
  215. SEQ_SHORTREP,
  216. SEQ_IS_REP0_LONG,
  217. SEQ_IS_REP1,
  218. SEQ_IS_REP2,
  219. seq_len(SEQ_REP_LEN),
  220. SEQ_COPY,
  221. } sequence;
  222. /// Base of the current probability tree
  223. probability *probs;
  224. /// Symbol being decoded. This is also used as an index variable in
  225. /// bittree decoders: probs[symbol]
  226. uint32_t symbol;
  227. /// Used as a loop termination condition on bittree decoders and
  228. /// direct bits decoder.
  229. uint32_t limit;
  230. /// Matched literal decoder: 0x100 or 0 to help avoiding branches.
  231. /// Bittree reverse decoders: Offset of the next bit: 1 << offset
  232. uint32_t offset;
  233. /// If decoding a literal: match byte.
  234. /// If decoding a match: length of the match.
  235. uint32_t len;
  236. };
  237. static lzma_ret
  238. lzma_decode(lzma_coder *LZMA_RESTRICT coder, lzma_dict *LZMA_RESTRICT dictptr,
  239. const uint8_t *LZMA_RESTRICT in,
  240. size_t *LZMA_RESTRICT in_pos, size_t in_size)
  241. {
  242. ///////////////
  243. // Variables //
  244. ///////////////
  245. // Making local copies of often-used variables improves both
  246. // speed and readability.
  247. lzma_dict dict = *dictptr;
  248. const size_t dict_start = dict.pos;
  249. // Range decoder
  250. rc_to_local(coder->rc, *in_pos);
  251. // State
  252. uint32_t state = coder->state;
  253. uint32_t rep0 = coder->rep0;
  254. uint32_t rep1 = coder->rep1;
  255. uint32_t rep2 = coder->rep2;
  256. uint32_t rep3 = coder->rep3;
  257. const uint32_t pos_mask = coder->pos_mask;
  258. // These variables are actually needed only if we last time ran
  259. // out of input in the middle of the decoder loop.
  260. probability *probs = coder->probs;
  261. uint32_t symbol = coder->symbol;
  262. uint32_t limit = coder->limit;
  263. uint32_t offset = coder->offset;
  264. uint32_t len = coder->len;
  265. const uint32_t literal_pos_mask = coder->literal_pos_mask;
  266. const uint32_t literal_context_bits = coder->literal_context_bits;
  267. // Temporary variables
  268. uint32_t pos_state = dict.pos & pos_mask;
  269. lzma_ret ret = LZMA_OK;
  270. // If uncompressed size is known, there must be no end of payload
  271. // marker.
  272. const bool no_eopm = coder->uncompressed_size
  273. != LZMA_VLI_UNKNOWN;
  274. if (no_eopm && coder->uncompressed_size < dict.limit - dict.pos)
  275. dict.limit = dict.pos + (size_t)(coder->uncompressed_size);
  276. ////////////////////
  277. // Initialization //
  278. ////////////////////
  279. if (!rc_read_init(&coder->rc, in, in_pos, in_size))
  280. return LZMA_OK;
  281. rc = coder->rc;
  282. rc_in_pos = *in_pos;
  283. // The main decoder loop. The "switch" is used to restart the decoder at
  284. // correct location. Once restarted, the "switch" is no longer used.
  285. switch (coder->sequence)
  286. while (true) {
  287. // Calculate new pos_state. This is skipped on the first loop
  288. // since we already calculated it when setting up the local
  289. // variables.
  290. pos_state = dict.pos & pos_mask;
  291. case SEQ_NORMALIZE:
  292. case SEQ_IS_MATCH:
  293. if (unlikely(no_eopm && dict.pos == dict.limit))
  294. break;
  295. rc_if_0(coder->is_match[state][pos_state], SEQ_IS_MATCH) {
  296. static const lzma_lzma_state next_state[] = {
  297. STATE_LIT_LIT,
  298. STATE_LIT_LIT,
  299. STATE_LIT_LIT,
  300. STATE_LIT_LIT,
  301. STATE_MATCH_LIT_LIT,
  302. STATE_REP_LIT_LIT,
  303. STATE_SHORTREP_LIT_LIT,
  304. STATE_MATCH_LIT,
  305. STATE_REP_LIT,
  306. STATE_SHORTREP_LIT,
  307. STATE_MATCH_LIT,
  308. STATE_REP_LIT
  309. };
  310. rc_update_0(coder->is_match[state][pos_state]);
  311. // It's a literal i.e. a single 8-bit byte.
  312. probs = literal_subcoder(coder->literal,
  313. literal_context_bits, literal_pos_mask,
  314. dict.pos, dict_get(&dict, 0));
  315. symbol = 1;
  316. if (is_literal_state(state)) {
  317. // Decode literal without match byte.
  318. #ifdef HAVE_SMALL
  319. case SEQ_LITERAL:
  320. do {
  321. rc_bit(probs[symbol], , , SEQ_LITERAL);
  322. } while (symbol < (1 << 8));
  323. #else
  324. rc_bit_case(probs[symbol], 0, 0, SEQ_LITERAL0);
  325. rc_bit_case(probs[symbol], 0, 0, SEQ_LITERAL1);
  326. rc_bit_case(probs[symbol], 0, 0, SEQ_LITERAL2);
  327. rc_bit_case(probs[symbol], 0, 0, SEQ_LITERAL3);
  328. rc_bit_case(probs[symbol], 0, 0, SEQ_LITERAL4);
  329. rc_bit_case(probs[symbol], 0, 0, SEQ_LITERAL5);
  330. rc_bit_case(probs[symbol], 0, 0, SEQ_LITERAL6);
  331. rc_bit_case(probs[symbol], 0, 0, SEQ_LITERAL7);
  332. #endif
  333. } else {
  334. #ifndef HAVE_SMALL
  335. uint32_t match_bit;
  336. uint32_t subcoder_index;
  337. #endif
  338. // Decode literal with match byte.
  339. //
  340. // We store the byte we compare against
  341. // ("match byte") to "len" to minimize the
  342. // number of variables we need to store
  343. // between decoder calls.
  344. len = dict_get(&dict, rep0) << 1;
  345. // The usage of "offset" allows omitting some
  346. // branches, which should give tiny speed
  347. // improvement on some CPUs. "offset" gets
  348. // set to zero if match_bit didn't match.
  349. offset = 0x100;
  350. #ifdef HAVE_SMALL
  351. case SEQ_LITERAL_MATCHED:
  352. do {
  353. const uint32_t match_bit
  354. = len & offset;
  355. const uint32_t subcoder_index
  356. = offset + match_bit
  357. + symbol;
  358. rc_bit(probs[subcoder_index],
  359. offset &= ~match_bit,
  360. offset &= match_bit,
  361. SEQ_LITERAL_MATCHED);
  362. // It seems to be faster to do this
  363. // here instead of putting it to the
  364. // beginning of the loop and then
  365. // putting the "case" in the middle
  366. // of the loop.
  367. len <<= 1;
  368. } while (symbol < (1 << 8));
  369. #else
  370. // Unroll the loop.
  371. # define d(seq) \
  372. case seq: \
  373. match_bit = len & offset; \
  374. subcoder_index = offset + match_bit + symbol; \
  375. rc_bit(probs[subcoder_index], \
  376. offset &= ~match_bit, \
  377. offset &= match_bit, \
  378. seq)
  379. d(SEQ_LITERAL_MATCHED0);
  380. len <<= 1;
  381. d(SEQ_LITERAL_MATCHED1);
  382. len <<= 1;
  383. d(SEQ_LITERAL_MATCHED2);
  384. len <<= 1;
  385. d(SEQ_LITERAL_MATCHED3);
  386. len <<= 1;
  387. d(SEQ_LITERAL_MATCHED4);
  388. len <<= 1;
  389. d(SEQ_LITERAL_MATCHED5);
  390. len <<= 1;
  391. d(SEQ_LITERAL_MATCHED6);
  392. len <<= 1;
  393. d(SEQ_LITERAL_MATCHED7);
  394. # undef d
  395. #endif
  396. }
  397. //update_literal(state);
  398. // Use a lookup table to update to literal state,
  399. // since compared to other state updates, this would
  400. // need two branches.
  401. state = next_state[state];
  402. case SEQ_LITERAL_WRITE:
  403. if (unlikely(dict_put(&dict, symbol))) {
  404. coder->sequence = SEQ_LITERAL_WRITE;
  405. goto out;
  406. }
  407. continue;
  408. }
  409. // Instead of a new byte we are going to get a byte range
  410. // (distance and length) which will be repeated from our
  411. // output history.
  412. rc_update_1(coder->is_match[state][pos_state]);
  413. case SEQ_IS_REP:
  414. rc_if_0(coder->is_rep[state], SEQ_IS_REP) {
  415. // Not a repeated match
  416. rc_update_0(coder->is_rep[state]);
  417. update_match(state);
  418. // The latest three match distances are kept in
  419. // memory in case there are repeated matches.
  420. rep3 = rep2;
  421. rep2 = rep1;
  422. rep1 = rep0;
  423. // Decode the length of the match.
  424. len_decode(len, coder->match_len_decoder,
  425. pos_state, SEQ_MATCH_LEN);
  426. // Prepare to decode the highest two bits of the
  427. // match distance.
  428. probs = coder->pos_slot[get_len_to_pos_state(len)];
  429. symbol = 1;
  430. #ifdef HAVE_SMALL
  431. case SEQ_POS_SLOT:
  432. do {
  433. rc_bit(probs[symbol], , , SEQ_POS_SLOT);
  434. } while (symbol < POS_SLOTS);
  435. #else
  436. rc_bit_case(probs[symbol], 0, 0, SEQ_POS_SLOT0);
  437. rc_bit_case(probs[symbol], 0, 0, SEQ_POS_SLOT1);
  438. rc_bit_case(probs[symbol], 0, 0, SEQ_POS_SLOT2);
  439. rc_bit_case(probs[symbol], 0, 0, SEQ_POS_SLOT3);
  440. rc_bit_case(probs[symbol], 0, 0, SEQ_POS_SLOT4);
  441. rc_bit_case(probs[symbol], 0, 0, SEQ_POS_SLOT5);
  442. #endif
  443. // Get rid of the highest bit that was needed for
  444. // indexing of the probability array.
  445. symbol -= POS_SLOTS;
  446. assert(symbol <= 63);
  447. if (symbol < START_POS_MODEL_INDEX) {
  448. // Match distances [0, 3] have only two bits.
  449. rep0 = symbol;
  450. } else {
  451. // Decode the lowest [1, 29] bits of
  452. // the match distance.
  453. limit = (symbol >> 1) - 1;
  454. assert(limit >= 1 && limit <= 30);
  455. rep0 = 2 + (symbol & 1);
  456. if (symbol < END_POS_MODEL_INDEX) {
  457. // Prepare to decode the low bits for
  458. // a distance of [4, 127].
  459. assert(limit <= 5);
  460. rep0 <<= limit;
  461. assert(rep0 <= 96);
  462. // -1 is fine, because we start
  463. // decoding at probs[1], not probs[0].
  464. // NOTE: This violates the C standard,
  465. // since we are doing pointer
  466. // arithmetic past the beginning of
  467. // the array.
  468. assert((int32_t)(rep0 - symbol - 1)
  469. >= -1);
  470. assert((int32_t)(rep0 - symbol - 1)
  471. <= 82);
  472. probs = coder->pos_special + rep0
  473. - symbol - 1;
  474. symbol = 1;
  475. offset = 0;
  476. case SEQ_POS_MODEL:
  477. #ifdef HAVE_SMALL
  478. do {
  479. rc_bit(probs[symbol], ,
  480. rep0 += 1 << offset,
  481. SEQ_POS_MODEL);
  482. } while (++offset < limit);
  483. #else
  484. switch (limit) {
  485. case 5:
  486. assert(offset == 0);
  487. rc_bit(probs[symbol], 0,
  488. rep0 += 1,
  489. SEQ_POS_MODEL);
  490. ++offset;
  491. --limit;
  492. case 4:
  493. rc_bit(probs[symbol], 0,
  494. rep0 += 1 << offset,
  495. SEQ_POS_MODEL);
  496. ++offset;
  497. --limit;
  498. case 3:
  499. rc_bit(probs[symbol], 0,
  500. rep0 += 1 << offset,
  501. SEQ_POS_MODEL);
  502. ++offset;
  503. --limit;
  504. case 2:
  505. rc_bit(probs[symbol], 0,
  506. rep0 += 1 << offset,
  507. SEQ_POS_MODEL);
  508. ++offset;
  509. --limit;
  510. case 1:
  511. // We need "symbol" only for
  512. // indexing the probability
  513. // array, thus we can use
  514. // rc_bit_last() here to omit
  515. // the unneeded updating of
  516. // "symbol".
  517. rc_bit_last(probs[symbol], 0,
  518. rep0 += 1 << offset,
  519. SEQ_POS_MODEL);
  520. }
  521. #endif
  522. } else {
  523. // The distance is >= 128. Decode the
  524. // lower bits without probabilities
  525. // except the lowest four bits.
  526. assert(symbol >= 14);
  527. assert(limit >= 6);
  528. limit -= ALIGN_BITS;
  529. assert(limit >= 2);
  530. case SEQ_DIRECT:
  531. // Not worth manual unrolling
  532. do {
  533. rc_direct(rep0, SEQ_DIRECT);
  534. } while (--limit > 0);
  535. // Decode the lowest four bits using
  536. // probabilities.
  537. rep0 <<= ALIGN_BITS;
  538. symbol = 1;
  539. #ifdef HAVE_SMALL
  540. offset = 0;
  541. case SEQ_ALIGN:
  542. do {
  543. rc_bit(coder->pos_align[
  544. symbol], ,
  545. rep0 += 1 << offset,
  546. SEQ_ALIGN);
  547. } while (++offset < ALIGN_BITS);
  548. #else
  549. case SEQ_ALIGN0:
  550. rc_bit(coder->pos_align[symbol], 0,
  551. rep0 += 1, SEQ_ALIGN0);
  552. case SEQ_ALIGN1:
  553. rc_bit(coder->pos_align[symbol], 0,
  554. rep0 += 2, SEQ_ALIGN1);
  555. case SEQ_ALIGN2:
  556. rc_bit(coder->pos_align[symbol], 0,
  557. rep0 += 4, SEQ_ALIGN2);
  558. case SEQ_ALIGN3:
  559. // Like in SEQ_POS_MODEL, we don't
  560. // need "symbol" for anything else
  561. // than indexing the probability array.
  562. rc_bit_last(coder->pos_align[symbol], 0,
  563. rep0 += 8, SEQ_ALIGN3);
  564. #endif
  565. if (rep0 == UINT32_MAX) {
  566. // End of payload marker was
  567. // found. It must not be
  568. // present if uncompressed
  569. // size is known.
  570. if (coder->uncompressed_size
  571. != LZMA_VLI_UNKNOWN) {
  572. ret = LZMA_DATA_ERROR;
  573. goto out;
  574. }
  575. case SEQ_EOPM:
  576. // LZMA1 stream with
  577. // end-of-payload marker.
  578. rc_normalize(SEQ_EOPM);
  579. ret = LZMA_STREAM_END;
  580. goto out;
  581. }
  582. }
  583. }
  584. // Validate the distance we just decoded.
  585. if (unlikely(!dict_is_distance_valid(&dict, rep0))) {
  586. ret = LZMA_DATA_ERROR;
  587. goto out;
  588. }
  589. } else {
  590. rc_update_1(coder->is_rep[state]);
  591. // Repeated match
  592. //
  593. // The match distance is a value that we have had
  594. // earlier. The latest four match distances are
  595. // available as rep0, rep1, rep2 and rep3. We will
  596. // now decode which of them is the new distance.
  597. //
  598. // There cannot be a match if we haven't produced
  599. // any output, so check that first.
  600. if (unlikely(!dict_is_distance_valid(&dict, 0))) {
  601. ret = LZMA_DATA_ERROR;
  602. goto out;
  603. }
  604. case SEQ_IS_REP0:
  605. rc_if_0(coder->is_rep0[state], SEQ_IS_REP0) {
  606. rc_update_0(coder->is_rep0[state]);
  607. // The distance is rep0.
  608. case SEQ_IS_REP0_LONG:
  609. rc_if_0(coder->is_rep0_long[state][pos_state],
  610. SEQ_IS_REP0_LONG) {
  611. rc_update_0(coder->is_rep0_long[
  612. state][pos_state]);
  613. update_short_rep(state);
  614. case SEQ_SHORTREP:
  615. if (unlikely(dict_put(&dict, dict_get(
  616. &dict, rep0)))) {
  617. coder->sequence = SEQ_SHORTREP;
  618. goto out;
  619. }
  620. continue;
  621. }
  622. // Repeating more than one byte at
  623. // distance of rep0.
  624. rc_update_1(coder->is_rep0_long[
  625. state][pos_state]);
  626. } else {
  627. rc_update_1(coder->is_rep0[state]);
  628. case SEQ_IS_REP1:
  629. // The distance is rep1, rep2 or rep3. Once
  630. // we find out which one of these three, it
  631. // is stored to rep0 and rep1, rep2 and rep3
  632. // are updated accordingly.
  633. rc_if_0(coder->is_rep1[state], SEQ_IS_REP1) {
  634. uint32_t distance;
  635. rc_update_0(coder->is_rep1[state]);
  636. distance = rep1;
  637. rep1 = rep0;
  638. rep0 = distance;
  639. } else {
  640. rc_update_1(coder->is_rep1[state]);
  641. case SEQ_IS_REP2:
  642. rc_if_0(coder->is_rep2[state],
  643. SEQ_IS_REP2) {
  644. uint32_t distance;
  645. rc_update_0(coder->is_rep2[
  646. state]);
  647. distance = rep2;
  648. rep2 = rep1;
  649. rep1 = rep0;
  650. rep0 = distance;
  651. } else {
  652. uint32_t distance;
  653. rc_update_1(coder->is_rep2[
  654. state]);
  655. distance = rep3;
  656. rep3 = rep2;
  657. rep2 = rep1;
  658. rep1 = rep0;
  659. rep0 = distance;
  660. }
  661. }
  662. }
  663. update_long_rep(state);
  664. // Decode the length of the repeated match.
  665. len_decode(len, coder->rep_len_decoder,
  666. pos_state, SEQ_REP_LEN);
  667. }
  668. /////////////////////////////////
  669. // Repeat from history buffer. //
  670. /////////////////////////////////
  671. // The length is always between these limits. There is no way
  672. // to trigger the algorithm to set len outside this range.
  673. assert(len >= MATCH_LEN_MIN);
  674. assert(len <= MATCH_LEN_MAX);
  675. case SEQ_COPY:
  676. // Repeat len bytes from distance of rep0.
  677. if (unlikely(dict_repeat(&dict, rep0, &len))) {
  678. coder->sequence = SEQ_COPY;
  679. goto out;
  680. }
  681. }
  682. rc_normalize(SEQ_NORMALIZE);
  683. coder->sequence = SEQ_IS_MATCH;
  684. out:
  685. // Save state
  686. // NOTE: Must not copy dict.limit.
  687. dictptr->pos = dict.pos;
  688. dictptr->full = dict.full;
  689. rc_from_local(coder->rc, *in_pos);
  690. coder->state = state;
  691. coder->rep0 = rep0;
  692. coder->rep1 = rep1;
  693. coder->rep2 = rep2;
  694. coder->rep3 = rep3;
  695. coder->probs = probs;
  696. coder->symbol = symbol;
  697. coder->limit = limit;
  698. coder->offset = offset;
  699. coder->len = len;
  700. // Update the remaining amount of uncompressed data if uncompressed
  701. // size was known.
  702. if (coder->uncompressed_size != LZMA_VLI_UNKNOWN) {
  703. coder->uncompressed_size -= dict.pos - dict_start;
  704. // Since there cannot be end of payload marker if the
  705. // uncompressed size was known, we check here if we
  706. // finished decoding.
  707. if (coder->uncompressed_size == 0 && ret == LZMA_OK
  708. && coder->sequence != SEQ_NORMALIZE)
  709. ret = coder->sequence == SEQ_IS_MATCH
  710. ? LZMA_STREAM_END : LZMA_DATA_ERROR;
  711. }
  712. // We can do an additional check in the range decoder to catch some
  713. // corrupted files.
  714. if (ret == LZMA_STREAM_END) {
  715. if (!rc_is_finished(coder->rc))
  716. ret = LZMA_DATA_ERROR;
  717. // Reset the range decoder so that it is ready to reinitialize
  718. // for a new LZMA2 chunk.
  719. rc_reset(coder->rc);
  720. }
  721. return ret;
  722. }
  723. static void
  724. lzma_decoder_uncompressed(lzma_coder *coder, lzma_vli uncompressed_size)
  725. {
  726. coder->uncompressed_size = uncompressed_size;
  727. }
  728. /*
  729. extern void
  730. lzma_lzma_decoder_uncompressed(void *coder_ptr, lzma_vli uncompressed_size)
  731. {
  732. // This is hack.
  733. (*(lzma_coder **)(coder))->uncompressed_size = uncompressed_size;
  734. }
  735. */
  736. static void
  737. lzma_decoder_reset(lzma_coder *coder, const void *opt)
  738. {
  739. uint32_t i, j, pos_state;
  740. uint32_t num_pos_states;
  741. const lzma_options_lzma *options = opt;
  742. // NOTE: We assume that lc/lp/pb are valid since they were
  743. // successfully decoded with lzma_lzma_decode_properties().
  744. // Calculate pos_mask. We don't need pos_bits as is for anything.
  745. coder->pos_mask = (1U << options->pb) - 1;
  746. // Initialize the literal decoder.
  747. literal_init(coder->literal, options->lc, options->lp);
  748. coder->literal_context_bits = options->lc;
  749. coder->literal_pos_mask = (1U << options->lp) - 1;
  750. // State
  751. coder->state = STATE_LIT_LIT;
  752. coder->rep0 = 0;
  753. coder->rep1 = 0;
  754. coder->rep2 = 0;
  755. coder->rep3 = 0;
  756. coder->pos_mask = (1U << options->pb) - 1;
  757. // Range decoder
  758. rc_reset(coder->rc);
  759. // Bit and bittree decoders
  760. for (i = 0; i < STATES; ++i) {
  761. for (j = 0; j <= coder->pos_mask; ++j) {
  762. bit_reset(coder->is_match[i][j]);
  763. bit_reset(coder->is_rep0_long[i][j]);
  764. }
  765. bit_reset(coder->is_rep[i]);
  766. bit_reset(coder->is_rep0[i]);
  767. bit_reset(coder->is_rep1[i]);
  768. bit_reset(coder->is_rep2[i]);
  769. }
  770. for (i = 0; i < LEN_TO_POS_STATES; ++i)
  771. bittree_reset(coder->pos_slot[i], POS_SLOT_BITS);
  772. for (i = 0; i < FULL_DISTANCES - END_POS_MODEL_INDEX; ++i)
  773. bit_reset(coder->pos_special[i]);
  774. bittree_reset(coder->pos_align, ALIGN_BITS);
  775. // Len decoders (also bit/bittree)
  776. num_pos_states = 1U << options->pb;
  777. bit_reset(coder->match_len_decoder.choice);
  778. bit_reset(coder->match_len_decoder.choice2);
  779. bit_reset(coder->rep_len_decoder.choice);
  780. bit_reset(coder->rep_len_decoder.choice2);
  781. for (pos_state = 0; pos_state < num_pos_states; ++pos_state) {
  782. bittree_reset(coder->match_len_decoder.low[pos_state],
  783. LEN_LOW_BITS);
  784. bittree_reset(coder->match_len_decoder.mid[pos_state],
  785. LEN_MID_BITS);
  786. bittree_reset(coder->rep_len_decoder.low[pos_state],
  787. LEN_LOW_BITS);
  788. bittree_reset(coder->rep_len_decoder.mid[pos_state],
  789. LEN_MID_BITS);
  790. }
  791. bittree_reset(coder->match_len_decoder.high, LEN_HIGH_BITS);
  792. bittree_reset(coder->rep_len_decoder.high, LEN_HIGH_BITS);
  793. coder->sequence = SEQ_IS_MATCH;
  794. coder->probs = NULL;
  795. coder->symbol = 0;
  796. coder->limit = 0;
  797. coder->offset = 0;
  798. coder->len = 0;
  799. return;
  800. }
  801. extern lzma_ret
  802. lzma_lzma_decoder_create(lzma_lz_decoder *lz, lzma_allocator *allocator,
  803. const void *opt, lzma_lz_options *lz_options)
  804. {
  805. const lzma_options_lzma *options = opt;
  806. if (lz->coder == NULL) {
  807. lz->coder = lzma_alloc(sizeof(lzma_coder), allocator);
  808. if (lz->coder == NULL)
  809. return LZMA_MEM_ERROR;
  810. lz->code = &lzma_decode;
  811. lz->reset = &lzma_decoder_reset;
  812. lz->set_uncompressed = &lzma_decoder_uncompressed;
  813. }
  814. // All dictionary sizes are OK here. LZ decoder will take care of
  815. // the special cases.
  816. lz_options->dict_size = options->dict_size;
  817. lz_options->preset_dict = options->preset_dict;
  818. lz_options->preset_dict_size = options->preset_dict_size;
  819. return LZMA_OK;
  820. }
  821. /// Allocate and initialize LZMA decoder. This is used only via LZ
  822. /// initialization (lzma_lzma_decoder_init() passes function pointer to
  823. /// the LZ initialization).
  824. static lzma_ret
  825. lzma_decoder_init(lzma_lz_decoder *lz, lzma_allocator *allocator,
  826. const void *options, lzma_lz_options *lz_options)
  827. {
  828. if (!is_lclppb_valid(options))
  829. return LZMA_PROG_ERROR;
  830. return_if_error(lzma_lzma_decoder_create(
  831. lz, allocator, options, lz_options));
  832. lzma_decoder_reset(lz->coder, options);
  833. lzma_decoder_uncompressed(lz->coder, LZMA_VLI_UNKNOWN);
  834. return LZMA_OK;
  835. }
  836. extern lzma_ret
  837. lzma_lzma_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
  838. const lzma_filter_info *filters)
  839. {
  840. // LZMA can only be the last filter in the chain. This is enforced
  841. // by the raw_decoder initialization.
  842. assert(filters[1].init == NULL);
  843. return lzma_lz_decoder_init(next, allocator, filters,
  844. &lzma_decoder_init);
  845. }
  846. extern bool
  847. lzma_lzma_lclppb_decode(lzma_options_lzma *options, uint8_t byte)
  848. {
  849. if (byte > (4 * 5 + 4) * 9 + 8)
  850. return true;
  851. // See the file format specification to understand this.
  852. options->pb = byte / (9 * 5);
  853. byte -= options->pb * 9 * 5;
  854. options->lp = byte / 9;
  855. options->lc = byte - options->lp * 9;
  856. return options->lc + options->lp > LZMA_LCLP_MAX;
  857. }
  858. extern uint64_t
  859. lzma_lzma_decoder_memusage_nocheck(const void *options)
  860. {
  861. const lzma_options_lzma *const opt = options;
  862. return sizeof(lzma_coder) + lzma_lz_decoder_memusage(opt->dict_size);
  863. }
  864. extern uint64_t
  865. lzma_lzma_decoder_memusage(const void *options)
  866. {
  867. if (!is_lclppb_valid(options))
  868. return UINT64_MAX;
  869. return lzma_lzma_decoder_memusage_nocheck(options);
  870. }
  871. extern lzma_ret
  872. lzma_lzma_props_decode(void **options, lzma_allocator *allocator,
  873. const uint8_t *props, size_t props_size)
  874. {
  875. lzma_options_lzma *opt;
  876. if (props_size != 5)
  877. return LZMA_OPTIONS_ERROR;
  878. opt = lzma_alloc(sizeof(lzma_options_lzma), allocator);
  879. if (opt == NULL)
  880. return LZMA_MEM_ERROR;
  881. if (lzma_lzma_lclppb_decode(opt, props[0]))
  882. goto error;
  883. // All dictionary sizes are accepted, including zero. LZ decoder
  884. // will automatically use a dictionary at least a few KiB even if
  885. // a smaller dictionary is requested.
  886. opt->dict_size = unaligned_read32le(props + 1);
  887. opt->preset_dict = NULL;
  888. opt->preset_dict_size = 0;
  889. *options = opt;
  890. return LZMA_OK;
  891. error:
  892. lzma_free(opt, allocator);
  893. return LZMA_OPTIONS_ERROR;
  894. }