xz_dec_lzma2.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. // SPDX-License-Identifier: 0BSD
  2. /*
  3. * LZMA2 decoder
  4. *
  5. * Authors: Lasse Collin <lasse.collin@tukaani.org>
  6. * Igor Pavlov <https://7-zip.org/>
  7. */
  8. #include "xz_private.h"
  9. #include "xz_lzma2.h"
  10. /*
  11. * Range decoder initialization eats the first five bytes of each LZMA chunk.
  12. */
  13. #define RC_INIT_BYTES 5
  14. /*
  15. * Minimum number of usable input buffer to safely decode one LZMA symbol.
  16. * The worst case is that we decode 22 bits using probabilities and 26
  17. * direct bits. This may decode at maximum of 20 bytes of input. However,
  18. * lzma_main() does an extra normalization before returning, thus we
  19. * need to put 21 here.
  20. */
  21. #define LZMA_IN_REQUIRED 21
  22. /*
  23. * Dictionary (history buffer)
  24. *
  25. * These are always true:
  26. * start <= pos <= full <= end
  27. * pos <= limit <= end
  28. *
  29. * In multi-call mode, also these are true:
  30. * end == size
  31. * size <= size_max
  32. * allocated <= size
  33. *
  34. * Most of these variables are size_t to support single-call mode,
  35. * in which the dictionary variables address the actual output
  36. * buffer directly.
  37. */
  38. struct dictionary {
  39. /* Beginning of the history buffer */
  40. uint8_t *buf;
  41. /* Old position in buf (before decoding more data) */
  42. size_t start;
  43. /* Position in buf */
  44. size_t pos;
  45. /*
  46. * How full dictionary is. This is used to detect corrupt input that
  47. * would read beyond the beginning of the uncompressed stream.
  48. */
  49. size_t full;
  50. /* Write limit; we don't write to buf[limit] or later bytes. */
  51. size_t limit;
  52. /*
  53. * End of the dictionary buffer. In multi-call mode, this is
  54. * the same as the dictionary size. In single-call mode, this
  55. * indicates the size of the output buffer.
  56. */
  57. size_t end;
  58. /*
  59. * Size of the dictionary as specified in Block Header. This is used
  60. * together with "full" to detect corrupt input that would make us
  61. * read beyond the beginning of the uncompressed stream.
  62. */
  63. uint32_t size;
  64. /*
  65. * Maximum allowed dictionary size in multi-call mode.
  66. * This is ignored in single-call mode.
  67. */
  68. uint32_t size_max;
  69. /*
  70. * Amount of memory currently allocated for the dictionary.
  71. * This is used only with XZ_DYNALLOC. (With XZ_PREALLOC,
  72. * size_max is always the same as the allocated size.)
  73. */
  74. uint32_t allocated;
  75. /* Operation mode */
  76. enum xz_mode mode;
  77. };
  78. /* Range decoder */
  79. struct rc_dec {
  80. uint32_t range;
  81. uint32_t code;
  82. /*
  83. * Number of initializing bytes remaining to be read
  84. * by rc_read_init().
  85. */
  86. uint32_t init_bytes_left;
  87. /*
  88. * Buffer from which we read our input. It can be either
  89. * temp.buf or the caller-provided input buffer.
  90. */
  91. const uint8_t *in;
  92. size_t in_pos;
  93. size_t in_limit;
  94. };
  95. /* Probabilities for a length decoder. */
  96. struct lzma_len_dec {
  97. /* Probability of match length being at least 10 */
  98. uint16_t choice;
  99. /* Probability of match length being at least 18 */
  100. uint16_t choice2;
  101. /* Probabilities for match lengths 2-9 */
  102. uint16_t low[POS_STATES_MAX][LEN_LOW_SYMBOLS];
  103. /* Probabilities for match lengths 10-17 */
  104. uint16_t mid[POS_STATES_MAX][LEN_MID_SYMBOLS];
  105. /* Probabilities for match lengths 18-273 */
  106. uint16_t high[LEN_HIGH_SYMBOLS];
  107. };
  108. struct lzma_dec {
  109. /* Distances of latest four matches */
  110. uint32_t rep0;
  111. uint32_t rep1;
  112. uint32_t rep2;
  113. uint32_t rep3;
  114. /* Types of the most recently seen LZMA symbols */
  115. enum lzma_state state;
  116. /*
  117. * Length of a match. This is updated so that dict_repeat can
  118. * be called again to finish repeating the whole match.
  119. */
  120. uint32_t len;
  121. /*
  122. * LZMA properties or related bit masks (number of literal
  123. * context bits, a mask derived from the number of literal
  124. * position bits, and a mask derived from the number
  125. * position bits)
  126. */
  127. uint32_t lc;
  128. uint32_t literal_pos_mask; /* (1 << lp) - 1 */
  129. uint32_t pos_mask; /* (1 << pb) - 1 */
  130. /* If 1, it's a match. Otherwise it's a single 8-bit literal. */
  131. uint16_t is_match[STATES][POS_STATES_MAX];
  132. /* If 1, it's a repeated match. The distance is one of rep0 .. rep3. */
  133. uint16_t is_rep[STATES];
  134. /*
  135. * If 0, distance of a repeated match is rep0.
  136. * Otherwise check is_rep1.
  137. */
  138. uint16_t is_rep0[STATES];
  139. /*
  140. * If 0, distance of a repeated match is rep1.
  141. * Otherwise check is_rep2.
  142. */
  143. uint16_t is_rep1[STATES];
  144. /* If 0, distance of a repeated match is rep2. Otherwise it is rep3. */
  145. uint16_t is_rep2[STATES];
  146. /*
  147. * If 1, the repeated match has length of one byte. Otherwise
  148. * the length is decoded from rep_len_decoder.
  149. */
  150. uint16_t is_rep0_long[STATES][POS_STATES_MAX];
  151. /*
  152. * Probability tree for the highest two bits of the match
  153. * distance. There is a separate probability tree for match
  154. * lengths of 2 (i.e. MATCH_LEN_MIN), 3, 4, and [5, 273].
  155. */
  156. uint16_t dist_slot[DIST_STATES][DIST_SLOTS];
  157. /*
  158. * Probility trees for additional bits for match distance
  159. * when the distance is in the range [4, 127].
  160. */
  161. uint16_t dist_special[FULL_DISTANCES - DIST_MODEL_END];
  162. /*
  163. * Probability tree for the lowest four bits of a match
  164. * distance that is equal to or greater than 128.
  165. */
  166. uint16_t dist_align[ALIGN_SIZE];
  167. /* Length of a normal match */
  168. struct lzma_len_dec match_len_dec;
  169. /* Length of a repeated match */
  170. struct lzma_len_dec rep_len_dec;
  171. /* Probabilities of literals */
  172. uint16_t literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE];
  173. };
  174. struct lzma2_dec {
  175. /* Position in xz_dec_lzma2_run(). */
  176. enum lzma2_seq {
  177. SEQ_CONTROL,
  178. SEQ_UNCOMPRESSED_1,
  179. SEQ_UNCOMPRESSED_2,
  180. SEQ_COMPRESSED_0,
  181. SEQ_COMPRESSED_1,
  182. SEQ_PROPERTIES,
  183. SEQ_LZMA_PREPARE,
  184. SEQ_LZMA_RUN,
  185. SEQ_COPY
  186. } sequence;
  187. /* Next position after decoding the compressed size of the chunk. */
  188. enum lzma2_seq next_sequence;
  189. /* Uncompressed size of LZMA chunk (2 MiB at maximum) */
  190. uint32_t uncompressed;
  191. /*
  192. * Compressed size of LZMA chunk or compressed/uncompressed
  193. * size of uncompressed chunk (64 KiB at maximum)
  194. */
  195. uint32_t compressed;
  196. /*
  197. * True if dictionary reset is needed. This is false before
  198. * the first chunk (LZMA or uncompressed).
  199. */
  200. bool need_dict_reset;
  201. /*
  202. * True if new LZMA properties are needed. This is false
  203. * before the first LZMA chunk.
  204. */
  205. bool need_props;
  206. #ifdef XZ_DEC_MICROLZMA
  207. bool pedantic_microlzma;
  208. #endif
  209. };
  210. struct xz_dec_lzma2 {
  211. /*
  212. * The order below is important on x86 to reduce code size and
  213. * it shouldn't hurt on other platforms. Everything up to and
  214. * including lzma.pos_mask are in the first 128 bytes on x86-32,
  215. * which allows using smaller instructions to access those
  216. * variables. On x86-64, fewer variables fit into the first 128
  217. * bytes, but this is still the best order without sacrificing
  218. * the readability by splitting the structures.
  219. */
  220. struct rc_dec rc;
  221. struct dictionary dict;
  222. struct lzma2_dec lzma2;
  223. struct lzma_dec lzma;
  224. /*
  225. * Temporary buffer which holds small number of input bytes between
  226. * decoder calls. See lzma2_lzma() for details.
  227. */
  228. struct {
  229. uint32_t size;
  230. uint8_t buf[3 * LZMA_IN_REQUIRED];
  231. } temp;
  232. };
  233. /**************
  234. * Dictionary *
  235. **************/
  236. /*
  237. * Reset the dictionary state. When in single-call mode, set up the beginning
  238. * of the dictionary to point to the actual output buffer.
  239. */
  240. static void dict_reset(struct dictionary *dict, struct xz_buf *b)
  241. {
  242. if (DEC_IS_SINGLE(dict->mode)) {
  243. dict->buf = b->out + b->out_pos;
  244. dict->end = b->out_size - b->out_pos;
  245. }
  246. dict->start = 0;
  247. dict->pos = 0;
  248. dict->limit = 0;
  249. dict->full = 0;
  250. }
  251. /* Set dictionary write limit */
  252. static void dict_limit(struct dictionary *dict, size_t out_max)
  253. {
  254. if (dict->end - dict->pos <= out_max)
  255. dict->limit = dict->end;
  256. else
  257. dict->limit = dict->pos + out_max;
  258. }
  259. /* Return true if at least one byte can be written into the dictionary. */
  260. static inline bool dict_has_space(const struct dictionary *dict)
  261. {
  262. return dict->pos < dict->limit;
  263. }
  264. /*
  265. * Get a byte from the dictionary at the given distance. The distance is
  266. * assumed to valid, or as a special case, zero when the dictionary is
  267. * still empty. This special case is needed for single-call decoding to
  268. * avoid writing a '\0' to the end of the destination buffer.
  269. */
  270. static inline uint32_t dict_get(const struct dictionary *dict, uint32_t dist)
  271. {
  272. size_t offset = dict->pos - dist - 1;
  273. if (dist >= dict->pos)
  274. offset += dict->end;
  275. return dict->full > 0 ? dict->buf[offset] : 0;
  276. }
  277. /*
  278. * Put one byte into the dictionary. It is assumed that there is space for it.
  279. */
  280. static inline void dict_put(struct dictionary *dict, uint8_t byte)
  281. {
  282. dict->buf[dict->pos++] = byte;
  283. if (dict->full < dict->pos)
  284. dict->full = dict->pos;
  285. }
  286. /*
  287. * Repeat given number of bytes from the given distance. If the distance is
  288. * invalid, false is returned. On success, true is returned and *len is
  289. * updated to indicate how many bytes were left to be repeated.
  290. */
  291. static bool dict_repeat(struct dictionary *dict, uint32_t *len, uint32_t dist)
  292. {
  293. size_t back;
  294. uint32_t left;
  295. if (dist >= dict->full || dist >= dict->size)
  296. return false;
  297. left = min_t(size_t, dict->limit - dict->pos, *len);
  298. *len -= left;
  299. back = dict->pos - dist - 1;
  300. if (dist >= dict->pos)
  301. back += dict->end;
  302. do {
  303. dict->buf[dict->pos++] = dict->buf[back++];
  304. if (back == dict->end)
  305. back = 0;
  306. } while (--left > 0);
  307. if (dict->full < dict->pos)
  308. dict->full = dict->pos;
  309. return true;
  310. }
  311. /* Copy uncompressed data as is from input to dictionary and output buffers. */
  312. static void dict_uncompressed(struct dictionary *dict, struct xz_buf *b,
  313. uint32_t *left)
  314. {
  315. size_t copy_size;
  316. while (*left > 0 && b->in_pos < b->in_size
  317. && b->out_pos < b->out_size) {
  318. copy_size = min(b->in_size - b->in_pos,
  319. b->out_size - b->out_pos);
  320. if (copy_size > dict->end - dict->pos)
  321. copy_size = dict->end - dict->pos;
  322. if (copy_size > *left)
  323. copy_size = *left;
  324. *left -= copy_size;
  325. /*
  326. * If doing in-place decompression in single-call mode and the
  327. * uncompressed size of the file is larger than the caller
  328. * thought (i.e. it is invalid input!), the buffers below may
  329. * overlap and cause undefined behavior with memcpy().
  330. * With valid inputs memcpy() would be fine here.
  331. */
  332. memmove(dict->buf + dict->pos, b->in + b->in_pos, copy_size);
  333. dict->pos += copy_size;
  334. if (dict->full < dict->pos)
  335. dict->full = dict->pos;
  336. if (DEC_IS_MULTI(dict->mode)) {
  337. if (dict->pos == dict->end)
  338. dict->pos = 0;
  339. /*
  340. * Like above but for multi-call mode: use memmove()
  341. * to avoid undefined behavior with invalid input.
  342. */
  343. memmove(b->out + b->out_pos, b->in + b->in_pos,
  344. copy_size);
  345. }
  346. dict->start = dict->pos;
  347. b->out_pos += copy_size;
  348. b->in_pos += copy_size;
  349. }
  350. }
  351. #ifdef XZ_DEC_MICROLZMA
  352. # define DICT_FLUSH_SUPPORTS_SKIPPING true
  353. #else
  354. # define DICT_FLUSH_SUPPORTS_SKIPPING false
  355. #endif
  356. /*
  357. * Flush pending data from dictionary to b->out. It is assumed that there is
  358. * enough space in b->out. This is guaranteed because caller uses dict_limit()
  359. * before decoding data into the dictionary.
  360. */
  361. static uint32_t dict_flush(struct dictionary *dict, struct xz_buf *b)
  362. {
  363. size_t copy_size = dict->pos - dict->start;
  364. if (DEC_IS_MULTI(dict->mode)) {
  365. if (dict->pos == dict->end)
  366. dict->pos = 0;
  367. /*
  368. * These buffers cannot overlap even if doing in-place
  369. * decompression because in multi-call mode dict->buf
  370. * has been allocated by us in this file; it's not
  371. * provided by the caller like in single-call mode.
  372. *
  373. * With MicroLZMA, b->out can be NULL to skip bytes that
  374. * the caller doesn't need. This cannot be done with XZ
  375. * because it would break BCJ filters.
  376. */
  377. if (!DICT_FLUSH_SUPPORTS_SKIPPING || b->out != NULL)
  378. memcpy(b->out + b->out_pos, dict->buf + dict->start,
  379. copy_size);
  380. }
  381. dict->start = dict->pos;
  382. b->out_pos += copy_size;
  383. return copy_size;
  384. }
  385. /*****************
  386. * Range decoder *
  387. *****************/
  388. /* Reset the range decoder. */
  389. static void rc_reset(struct rc_dec *rc)
  390. {
  391. rc->range = (uint32_t)-1;
  392. rc->code = 0;
  393. rc->init_bytes_left = RC_INIT_BYTES;
  394. }
  395. /*
  396. * Read the first five initial bytes into rc->code if they haven't been
  397. * read already. (Yes, the first byte gets completely ignored.)
  398. */
  399. static bool rc_read_init(struct rc_dec *rc, struct xz_buf *b)
  400. {
  401. while (rc->init_bytes_left > 0) {
  402. if (b->in_pos == b->in_size)
  403. return false;
  404. rc->code = (rc->code << 8) + b->in[b->in_pos++];
  405. --rc->init_bytes_left;
  406. }
  407. return true;
  408. }
  409. /* Return true if there may not be enough input for the next decoding loop. */
  410. static inline bool rc_limit_exceeded(const struct rc_dec *rc)
  411. {
  412. return rc->in_pos > rc->in_limit;
  413. }
  414. /*
  415. * Return true if it is possible (from point of view of range decoder) that
  416. * we have reached the end of the LZMA chunk.
  417. */
  418. static inline bool rc_is_finished(const struct rc_dec *rc)
  419. {
  420. return rc->code == 0;
  421. }
  422. /* Read the next input byte if needed. */
  423. static __always_inline void rc_normalize(struct rc_dec *rc)
  424. {
  425. if (rc->range < RC_TOP_VALUE) {
  426. rc->range <<= RC_SHIFT_BITS;
  427. rc->code = (rc->code << RC_SHIFT_BITS) + rc->in[rc->in_pos++];
  428. }
  429. }
  430. /*
  431. * Decode one bit. In some versions, this function has been split in three
  432. * functions so that the compiler is supposed to be able to more easily avoid
  433. * an extra branch. In this particular version of the LZMA decoder, this
  434. * doesn't seem to be a good idea (tested with GCC 3.3.6, 3.4.6, and 4.3.3
  435. * on x86). Using a non-split version results in nicer looking code too.
  436. *
  437. * NOTE: This must return an int. Do not make it return a bool or the speed
  438. * of the code generated by GCC 3.x decreases 10-15 %. (GCC 4.3 doesn't care,
  439. * and it generates 10-20 % faster code than GCC 3.x from this file anyway.)
  440. */
  441. static __always_inline int rc_bit(struct rc_dec *rc, uint16_t *prob)
  442. {
  443. uint32_t bound;
  444. int bit;
  445. rc_normalize(rc);
  446. bound = (rc->range >> RC_BIT_MODEL_TOTAL_BITS) * *prob;
  447. if (rc->code < bound) {
  448. rc->range = bound;
  449. *prob += (RC_BIT_MODEL_TOTAL - *prob) >> RC_MOVE_BITS;
  450. bit = 0;
  451. } else {
  452. rc->range -= bound;
  453. rc->code -= bound;
  454. *prob -= *prob >> RC_MOVE_BITS;
  455. bit = 1;
  456. }
  457. return bit;
  458. }
  459. /* Decode a bittree starting from the most significant bit. */
  460. static __always_inline uint32_t rc_bittree(struct rc_dec *rc,
  461. uint16_t *probs, uint32_t limit)
  462. {
  463. uint32_t symbol = 1;
  464. do {
  465. if (rc_bit(rc, &probs[symbol]))
  466. symbol = (symbol << 1) + 1;
  467. else
  468. symbol <<= 1;
  469. } while (symbol < limit);
  470. return symbol;
  471. }
  472. /* Decode a bittree starting from the least significant bit. */
  473. static __always_inline void rc_bittree_reverse(struct rc_dec *rc,
  474. uint16_t *probs,
  475. uint32_t *dest, uint32_t limit)
  476. {
  477. uint32_t symbol = 1;
  478. uint32_t i = 0;
  479. do {
  480. if (rc_bit(rc, &probs[symbol])) {
  481. symbol = (symbol << 1) + 1;
  482. *dest += 1 << i;
  483. } else {
  484. symbol <<= 1;
  485. }
  486. } while (++i < limit);
  487. }
  488. /* Decode direct bits (fixed fifty-fifty probability) */
  489. static inline void rc_direct(struct rc_dec *rc, uint32_t *dest, uint32_t limit)
  490. {
  491. uint32_t mask;
  492. do {
  493. rc_normalize(rc);
  494. rc->range >>= 1;
  495. rc->code -= rc->range;
  496. mask = (uint32_t)0 - (rc->code >> 31);
  497. rc->code += rc->range & mask;
  498. *dest = (*dest << 1) + (mask + 1);
  499. } while (--limit > 0);
  500. }
  501. /********
  502. * LZMA *
  503. ********/
  504. /* Get pointer to literal coder probability array. */
  505. static uint16_t *lzma_literal_probs(struct xz_dec_lzma2 *s)
  506. {
  507. uint32_t prev_byte = dict_get(&s->dict, 0);
  508. uint32_t low = prev_byte >> (8 - s->lzma.lc);
  509. uint32_t high = (s->dict.pos & s->lzma.literal_pos_mask) << s->lzma.lc;
  510. return s->lzma.literal[low + high];
  511. }
  512. /* Decode a literal (one 8-bit byte) */
  513. static void lzma_literal(struct xz_dec_lzma2 *s)
  514. {
  515. uint16_t *probs;
  516. uint32_t symbol;
  517. uint32_t match_byte;
  518. uint32_t match_bit;
  519. uint32_t offset;
  520. uint32_t i;
  521. probs = lzma_literal_probs(s);
  522. if (lzma_state_is_literal(s->lzma.state)) {
  523. symbol = rc_bittree(&s->rc, probs, 0x100);
  524. } else {
  525. symbol = 1;
  526. match_byte = dict_get(&s->dict, s->lzma.rep0) << 1;
  527. offset = 0x100;
  528. do {
  529. match_bit = match_byte & offset;
  530. match_byte <<= 1;
  531. i = offset + match_bit + symbol;
  532. if (rc_bit(&s->rc, &probs[i])) {
  533. symbol = (symbol << 1) + 1;
  534. offset &= match_bit;
  535. } else {
  536. symbol <<= 1;
  537. offset &= ~match_bit;
  538. }
  539. } while (symbol < 0x100);
  540. }
  541. dict_put(&s->dict, (uint8_t)symbol);
  542. lzma_state_literal(&s->lzma.state);
  543. }
  544. /* Decode the length of the match into s->lzma.len. */
  545. static void lzma_len(struct xz_dec_lzma2 *s, struct lzma_len_dec *l,
  546. uint32_t pos_state)
  547. {
  548. uint16_t *probs;
  549. uint32_t limit;
  550. if (!rc_bit(&s->rc, &l->choice)) {
  551. probs = l->low[pos_state];
  552. limit = LEN_LOW_SYMBOLS;
  553. s->lzma.len = MATCH_LEN_MIN;
  554. } else {
  555. if (!rc_bit(&s->rc, &l->choice2)) {
  556. probs = l->mid[pos_state];
  557. limit = LEN_MID_SYMBOLS;
  558. s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS;
  559. } else {
  560. probs = l->high;
  561. limit = LEN_HIGH_SYMBOLS;
  562. s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS
  563. + LEN_MID_SYMBOLS;
  564. }
  565. }
  566. s->lzma.len += rc_bittree(&s->rc, probs, limit) - limit;
  567. }
  568. /* Decode a match. The distance will be stored in s->lzma.rep0. */
  569. static void lzma_match(struct xz_dec_lzma2 *s, uint32_t pos_state)
  570. {
  571. uint16_t *probs;
  572. uint32_t dist_slot;
  573. uint32_t limit;
  574. lzma_state_match(&s->lzma.state);
  575. s->lzma.rep3 = s->lzma.rep2;
  576. s->lzma.rep2 = s->lzma.rep1;
  577. s->lzma.rep1 = s->lzma.rep0;
  578. lzma_len(s, &s->lzma.match_len_dec, pos_state);
  579. probs = s->lzma.dist_slot[lzma_get_dist_state(s->lzma.len)];
  580. dist_slot = rc_bittree(&s->rc, probs, DIST_SLOTS) - DIST_SLOTS;
  581. if (dist_slot < DIST_MODEL_START) {
  582. s->lzma.rep0 = dist_slot;
  583. } else {
  584. limit = (dist_slot >> 1) - 1;
  585. s->lzma.rep0 = 2 + (dist_slot & 1);
  586. if (dist_slot < DIST_MODEL_END) {
  587. s->lzma.rep0 <<= limit;
  588. probs = s->lzma.dist_special + s->lzma.rep0
  589. - dist_slot - 1;
  590. rc_bittree_reverse(&s->rc, probs,
  591. &s->lzma.rep0, limit);
  592. } else {
  593. rc_direct(&s->rc, &s->lzma.rep0, limit - ALIGN_BITS);
  594. s->lzma.rep0 <<= ALIGN_BITS;
  595. rc_bittree_reverse(&s->rc, s->lzma.dist_align,
  596. &s->lzma.rep0, ALIGN_BITS);
  597. }
  598. }
  599. }
  600. /*
  601. * Decode a repeated match. The distance is one of the four most recently
  602. * seen matches. The distance will be stored in s->lzma.rep0.
  603. */
  604. static void lzma_rep_match(struct xz_dec_lzma2 *s, uint32_t pos_state)
  605. {
  606. uint32_t tmp;
  607. if (!rc_bit(&s->rc, &s->lzma.is_rep0[s->lzma.state])) {
  608. if (!rc_bit(&s->rc, &s->lzma.is_rep0_long[
  609. s->lzma.state][pos_state])) {
  610. lzma_state_short_rep(&s->lzma.state);
  611. s->lzma.len = 1;
  612. return;
  613. }
  614. } else {
  615. if (!rc_bit(&s->rc, &s->lzma.is_rep1[s->lzma.state])) {
  616. tmp = s->lzma.rep1;
  617. } else {
  618. if (!rc_bit(&s->rc, &s->lzma.is_rep2[s->lzma.state])) {
  619. tmp = s->lzma.rep2;
  620. } else {
  621. tmp = s->lzma.rep3;
  622. s->lzma.rep3 = s->lzma.rep2;
  623. }
  624. s->lzma.rep2 = s->lzma.rep1;
  625. }
  626. s->lzma.rep1 = s->lzma.rep0;
  627. s->lzma.rep0 = tmp;
  628. }
  629. lzma_state_long_rep(&s->lzma.state);
  630. lzma_len(s, &s->lzma.rep_len_dec, pos_state);
  631. }
  632. /* LZMA decoder core */
  633. static bool lzma_main(struct xz_dec_lzma2 *s)
  634. {
  635. uint32_t pos_state;
  636. /*
  637. * If the dictionary was reached during the previous call, try to
  638. * finish the possibly pending repeat in the dictionary.
  639. */
  640. if (dict_has_space(&s->dict) && s->lzma.len > 0)
  641. dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0);
  642. /*
  643. * Decode more LZMA symbols. One iteration may consume up to
  644. * LZMA_IN_REQUIRED - 1 bytes.
  645. */
  646. while (dict_has_space(&s->dict) && !rc_limit_exceeded(&s->rc)) {
  647. pos_state = s->dict.pos & s->lzma.pos_mask;
  648. if (!rc_bit(&s->rc, &s->lzma.is_match[
  649. s->lzma.state][pos_state])) {
  650. lzma_literal(s);
  651. } else {
  652. if (rc_bit(&s->rc, &s->lzma.is_rep[s->lzma.state]))
  653. lzma_rep_match(s, pos_state);
  654. else
  655. lzma_match(s, pos_state);
  656. if (!dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0))
  657. return false;
  658. }
  659. }
  660. /*
  661. * Having the range decoder always normalized when we are outside
  662. * this function makes it easier to correctly handle end of the chunk.
  663. */
  664. rc_normalize(&s->rc);
  665. return true;
  666. }
  667. /*
  668. * Reset the LZMA decoder and range decoder state. Dictionary is not reset
  669. * here, because LZMA state may be reset without resetting the dictionary.
  670. */
  671. static void lzma_reset(struct xz_dec_lzma2 *s)
  672. {
  673. uint16_t *probs;
  674. size_t i;
  675. s->lzma.state = STATE_LIT_LIT;
  676. s->lzma.rep0 = 0;
  677. s->lzma.rep1 = 0;
  678. s->lzma.rep2 = 0;
  679. s->lzma.rep3 = 0;
  680. s->lzma.len = 0;
  681. /*
  682. * All probabilities are initialized to the same value. This hack
  683. * makes the code smaller by avoiding a separate loop for each
  684. * probability array.
  685. *
  686. * This could be optimized so that only that part of literal
  687. * probabilities that are actually required. In the common case
  688. * we would write 12 KiB less.
  689. */
  690. probs = s->lzma.is_match[0];
  691. for (i = 0; i < PROBS_TOTAL; ++i)
  692. probs[i] = RC_BIT_MODEL_TOTAL / 2;
  693. rc_reset(&s->rc);
  694. }
  695. /*
  696. * Decode and validate LZMA properties (lc/lp/pb) and calculate the bit masks
  697. * from the decoded lp and pb values. On success, the LZMA decoder state is
  698. * reset and true is returned.
  699. */
  700. static bool lzma_props(struct xz_dec_lzma2 *s, uint8_t props)
  701. {
  702. if (props > (4 * 5 + 4) * 9 + 8)
  703. return false;
  704. s->lzma.pos_mask = 0;
  705. while (props >= 9 * 5) {
  706. props -= 9 * 5;
  707. ++s->lzma.pos_mask;
  708. }
  709. s->lzma.pos_mask = (1 << s->lzma.pos_mask) - 1;
  710. s->lzma.literal_pos_mask = 0;
  711. while (props >= 9) {
  712. props -= 9;
  713. ++s->lzma.literal_pos_mask;
  714. }
  715. s->lzma.lc = props;
  716. if (s->lzma.lc + s->lzma.literal_pos_mask > 4)
  717. return false;
  718. s->lzma.literal_pos_mask = (1 << s->lzma.literal_pos_mask) - 1;
  719. lzma_reset(s);
  720. return true;
  721. }
  722. /*********
  723. * LZMA2 *
  724. *********/
  725. /*
  726. * The LZMA decoder assumes that if the input limit (s->rc.in_limit) hasn't
  727. * been exceeded, it is safe to read up to LZMA_IN_REQUIRED bytes. This
  728. * wrapper function takes care of making the LZMA decoder's assumption safe.
  729. *
  730. * As long as there is plenty of input left to be decoded in the current LZMA
  731. * chunk, we decode directly from the caller-supplied input buffer until
  732. * there's LZMA_IN_REQUIRED bytes left. Those remaining bytes are copied into
  733. * s->temp.buf, which (hopefully) gets filled on the next call to this
  734. * function. We decode a few bytes from the temporary buffer so that we can
  735. * continue decoding from the caller-supplied input buffer again.
  736. */
  737. static bool lzma2_lzma(struct xz_dec_lzma2 *s, struct xz_buf *b)
  738. {
  739. size_t in_avail;
  740. uint32_t tmp;
  741. in_avail = b->in_size - b->in_pos;
  742. if (s->temp.size > 0 || s->lzma2.compressed == 0) {
  743. tmp = 2 * LZMA_IN_REQUIRED - s->temp.size;
  744. if (tmp > s->lzma2.compressed - s->temp.size)
  745. tmp = s->lzma2.compressed - s->temp.size;
  746. if (tmp > in_avail)
  747. tmp = in_avail;
  748. memcpy(s->temp.buf + s->temp.size, b->in + b->in_pos, tmp);
  749. if (s->temp.size + tmp == s->lzma2.compressed) {
  750. memzero(s->temp.buf + s->temp.size + tmp,
  751. sizeof(s->temp.buf)
  752. - s->temp.size - tmp);
  753. s->rc.in_limit = s->temp.size + tmp;
  754. } else if (s->temp.size + tmp < LZMA_IN_REQUIRED) {
  755. s->temp.size += tmp;
  756. b->in_pos += tmp;
  757. return true;
  758. } else {
  759. s->rc.in_limit = s->temp.size + tmp - LZMA_IN_REQUIRED;
  760. }
  761. s->rc.in = s->temp.buf;
  762. s->rc.in_pos = 0;
  763. if (!lzma_main(s) || s->rc.in_pos > s->temp.size + tmp)
  764. return false;
  765. s->lzma2.compressed -= s->rc.in_pos;
  766. if (s->rc.in_pos < s->temp.size) {
  767. s->temp.size -= s->rc.in_pos;
  768. memmove(s->temp.buf, s->temp.buf + s->rc.in_pos,
  769. s->temp.size);
  770. return true;
  771. }
  772. b->in_pos += s->rc.in_pos - s->temp.size;
  773. s->temp.size = 0;
  774. }
  775. in_avail = b->in_size - b->in_pos;
  776. if (in_avail >= LZMA_IN_REQUIRED) {
  777. s->rc.in = b->in;
  778. s->rc.in_pos = b->in_pos;
  779. if (in_avail >= s->lzma2.compressed + LZMA_IN_REQUIRED)
  780. s->rc.in_limit = b->in_pos + s->lzma2.compressed;
  781. else
  782. s->rc.in_limit = b->in_size - LZMA_IN_REQUIRED;
  783. if (!lzma_main(s))
  784. return false;
  785. in_avail = s->rc.in_pos - b->in_pos;
  786. if (in_avail > s->lzma2.compressed)
  787. return false;
  788. s->lzma2.compressed -= in_avail;
  789. b->in_pos = s->rc.in_pos;
  790. }
  791. in_avail = b->in_size - b->in_pos;
  792. if (in_avail < LZMA_IN_REQUIRED) {
  793. if (in_avail > s->lzma2.compressed)
  794. in_avail = s->lzma2.compressed;
  795. memcpy(s->temp.buf, b->in + b->in_pos, in_avail);
  796. s->temp.size = in_avail;
  797. b->in_pos += in_avail;
  798. }
  799. return true;
  800. }
  801. /*
  802. * Take care of the LZMA2 control layer, and forward the job of actual LZMA
  803. * decoding or copying of uncompressed chunks to other functions.
  804. */
  805. enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s, struct xz_buf *b)
  806. {
  807. uint32_t tmp;
  808. while (b->in_pos < b->in_size || s->lzma2.sequence == SEQ_LZMA_RUN) {
  809. switch (s->lzma2.sequence) {
  810. case SEQ_CONTROL:
  811. /*
  812. * LZMA2 control byte
  813. *
  814. * Exact values:
  815. * 0x00 End marker
  816. * 0x01 Dictionary reset followed by
  817. * an uncompressed chunk
  818. * 0x02 Uncompressed chunk (no dictionary reset)
  819. *
  820. * Highest three bits (s->control & 0xE0):
  821. * 0xE0 Dictionary reset, new properties and state
  822. * reset, followed by LZMA compressed chunk
  823. * 0xC0 New properties and state reset, followed
  824. * by LZMA compressed chunk (no dictionary
  825. * reset)
  826. * 0xA0 State reset using old properties,
  827. * followed by LZMA compressed chunk (no
  828. * dictionary reset)
  829. * 0x80 LZMA chunk (no dictionary or state reset)
  830. *
  831. * For LZMA compressed chunks, the lowest five bits
  832. * (s->control & 1F) are the highest bits of the
  833. * uncompressed size (bits 16-20).
  834. *
  835. * A new LZMA2 stream must begin with a dictionary
  836. * reset. The first LZMA chunk must set new
  837. * properties and reset the LZMA state.
  838. *
  839. * Values that don't match anything described above
  840. * are invalid and we return XZ_DATA_ERROR.
  841. */
  842. tmp = b->in[b->in_pos++];
  843. if (tmp == 0x00)
  844. return XZ_STREAM_END;
  845. if (tmp >= 0xE0 || tmp == 0x01) {
  846. s->lzma2.need_props = true;
  847. s->lzma2.need_dict_reset = false;
  848. dict_reset(&s->dict, b);
  849. } else if (s->lzma2.need_dict_reset) {
  850. return XZ_DATA_ERROR;
  851. }
  852. if (tmp >= 0x80) {
  853. s->lzma2.uncompressed = (tmp & 0x1F) << 16;
  854. s->lzma2.sequence = SEQ_UNCOMPRESSED_1;
  855. if (tmp >= 0xC0) {
  856. /*
  857. * When there are new properties,
  858. * state reset is done at
  859. * SEQ_PROPERTIES.
  860. */
  861. s->lzma2.need_props = false;
  862. s->lzma2.next_sequence
  863. = SEQ_PROPERTIES;
  864. } else if (s->lzma2.need_props) {
  865. return XZ_DATA_ERROR;
  866. } else {
  867. s->lzma2.next_sequence
  868. = SEQ_LZMA_PREPARE;
  869. if (tmp >= 0xA0)
  870. lzma_reset(s);
  871. }
  872. } else {
  873. if (tmp > 0x02)
  874. return XZ_DATA_ERROR;
  875. s->lzma2.sequence = SEQ_COMPRESSED_0;
  876. s->lzma2.next_sequence = SEQ_COPY;
  877. }
  878. break;
  879. case SEQ_UNCOMPRESSED_1:
  880. s->lzma2.uncompressed
  881. += (uint32_t)b->in[b->in_pos++] << 8;
  882. s->lzma2.sequence = SEQ_UNCOMPRESSED_2;
  883. break;
  884. case SEQ_UNCOMPRESSED_2:
  885. s->lzma2.uncompressed
  886. += (uint32_t)b->in[b->in_pos++] + 1;
  887. s->lzma2.sequence = SEQ_COMPRESSED_0;
  888. break;
  889. case SEQ_COMPRESSED_0:
  890. s->lzma2.compressed
  891. = (uint32_t)b->in[b->in_pos++] << 8;
  892. s->lzma2.sequence = SEQ_COMPRESSED_1;
  893. break;
  894. case SEQ_COMPRESSED_1:
  895. s->lzma2.compressed
  896. += (uint32_t)b->in[b->in_pos++] + 1;
  897. s->lzma2.sequence = s->lzma2.next_sequence;
  898. break;
  899. case SEQ_PROPERTIES:
  900. if (!lzma_props(s, b->in[b->in_pos++]))
  901. return XZ_DATA_ERROR;
  902. s->lzma2.sequence = SEQ_LZMA_PREPARE;
  903. fallthrough;
  904. case SEQ_LZMA_PREPARE:
  905. if (s->lzma2.compressed < RC_INIT_BYTES)
  906. return XZ_DATA_ERROR;
  907. if (!rc_read_init(&s->rc, b))
  908. return XZ_OK;
  909. s->lzma2.compressed -= RC_INIT_BYTES;
  910. s->lzma2.sequence = SEQ_LZMA_RUN;
  911. fallthrough;
  912. case SEQ_LZMA_RUN:
  913. /*
  914. * Set dictionary limit to indicate how much we want
  915. * to be encoded at maximum. Decode new data into the
  916. * dictionary. Flush the new data from dictionary to
  917. * b->out. Check if we finished decoding this chunk.
  918. * In case the dictionary got full but we didn't fill
  919. * the output buffer yet, we may run this loop
  920. * multiple times without changing s->lzma2.sequence.
  921. */
  922. dict_limit(&s->dict, min_t(size_t,
  923. b->out_size - b->out_pos,
  924. s->lzma2.uncompressed));
  925. if (!lzma2_lzma(s, b))
  926. return XZ_DATA_ERROR;
  927. s->lzma2.uncompressed -= dict_flush(&s->dict, b);
  928. if (s->lzma2.uncompressed == 0) {
  929. if (s->lzma2.compressed > 0 || s->lzma.len > 0
  930. || !rc_is_finished(&s->rc))
  931. return XZ_DATA_ERROR;
  932. rc_reset(&s->rc);
  933. s->lzma2.sequence = SEQ_CONTROL;
  934. } else if (b->out_pos == b->out_size
  935. || (b->in_pos == b->in_size
  936. && s->temp.size
  937. < s->lzma2.compressed)) {
  938. return XZ_OK;
  939. }
  940. break;
  941. case SEQ_COPY:
  942. dict_uncompressed(&s->dict, b, &s->lzma2.compressed);
  943. if (s->lzma2.compressed > 0)
  944. return XZ_OK;
  945. s->lzma2.sequence = SEQ_CONTROL;
  946. break;
  947. }
  948. }
  949. return XZ_OK;
  950. }
  951. struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode, uint32_t dict_max)
  952. {
  953. struct xz_dec_lzma2 *s = kmalloc_obj(*s);
  954. if (s == NULL)
  955. return NULL;
  956. s->dict.mode = mode;
  957. s->dict.size_max = dict_max;
  958. if (DEC_IS_PREALLOC(mode)) {
  959. s->dict.buf = vmalloc(dict_max);
  960. if (s->dict.buf == NULL) {
  961. kfree(s);
  962. return NULL;
  963. }
  964. } else if (DEC_IS_DYNALLOC(mode)) {
  965. s->dict.buf = NULL;
  966. s->dict.allocated = 0;
  967. }
  968. return s;
  969. }
  970. enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s, uint8_t props)
  971. {
  972. /* This limits dictionary size to 3 GiB to keep parsing simpler. */
  973. if (props > 39)
  974. return XZ_OPTIONS_ERROR;
  975. s->dict.size = 2 + (props & 1);
  976. s->dict.size <<= (props >> 1) + 11;
  977. if (DEC_IS_MULTI(s->dict.mode)) {
  978. if (s->dict.size > s->dict.size_max)
  979. return XZ_MEMLIMIT_ERROR;
  980. s->dict.end = s->dict.size;
  981. if (DEC_IS_DYNALLOC(s->dict.mode)) {
  982. if (s->dict.allocated < s->dict.size) {
  983. s->dict.allocated = s->dict.size;
  984. vfree(s->dict.buf);
  985. s->dict.buf = vmalloc(s->dict.size);
  986. if (s->dict.buf == NULL) {
  987. s->dict.allocated = 0;
  988. return XZ_MEM_ERROR;
  989. }
  990. }
  991. }
  992. }
  993. s->lzma2.sequence = SEQ_CONTROL;
  994. s->lzma2.need_dict_reset = true;
  995. s->temp.size = 0;
  996. return XZ_OK;
  997. }
  998. void xz_dec_lzma2_end(struct xz_dec_lzma2 *s)
  999. {
  1000. if (DEC_IS_MULTI(s->dict.mode))
  1001. vfree(s->dict.buf);
  1002. kfree(s);
  1003. }
  1004. #ifdef XZ_DEC_MICROLZMA
  1005. /* This is a wrapper struct to have a nice struct name in the public API. */
  1006. struct xz_dec_microlzma {
  1007. struct xz_dec_lzma2 s;
  1008. };
  1009. enum xz_ret xz_dec_microlzma_run(struct xz_dec_microlzma *s_ptr,
  1010. struct xz_buf *b)
  1011. {
  1012. struct xz_dec_lzma2 *s = &s_ptr->s;
  1013. /*
  1014. * sequence is SEQ_PROPERTIES before the first input byte,
  1015. * SEQ_LZMA_PREPARE until a total of five bytes have been read,
  1016. * and SEQ_LZMA_RUN for the rest of the input stream.
  1017. */
  1018. if (s->lzma2.sequence != SEQ_LZMA_RUN) {
  1019. if (s->lzma2.sequence == SEQ_PROPERTIES) {
  1020. /* One byte is needed for the props. */
  1021. if (b->in_pos >= b->in_size)
  1022. return XZ_OK;
  1023. /*
  1024. * Don't increment b->in_pos here. The same byte is
  1025. * also passed to rc_read_init() which will ignore it.
  1026. */
  1027. if (!lzma_props(s, ~b->in[b->in_pos]))
  1028. return XZ_DATA_ERROR;
  1029. s->lzma2.sequence = SEQ_LZMA_PREPARE;
  1030. }
  1031. /*
  1032. * xz_dec_microlzma_reset() doesn't validate the compressed
  1033. * size so we do it here. We have to limit the maximum size
  1034. * to avoid integer overflows in lzma2_lzma(). 3 GiB is a nice
  1035. * round number and much more than users of this code should
  1036. * ever need.
  1037. */
  1038. if (s->lzma2.compressed < RC_INIT_BYTES
  1039. || s->lzma2.compressed > (3U << 30))
  1040. return XZ_DATA_ERROR;
  1041. if (!rc_read_init(&s->rc, b))
  1042. return XZ_OK;
  1043. s->lzma2.compressed -= RC_INIT_BYTES;
  1044. s->lzma2.sequence = SEQ_LZMA_RUN;
  1045. dict_reset(&s->dict, b);
  1046. }
  1047. /* This is to allow increasing b->out_size between calls. */
  1048. if (DEC_IS_SINGLE(s->dict.mode))
  1049. s->dict.end = b->out_size - b->out_pos;
  1050. while (true) {
  1051. dict_limit(&s->dict, min_t(size_t, b->out_size - b->out_pos,
  1052. s->lzma2.uncompressed));
  1053. if (!lzma2_lzma(s, b))
  1054. return XZ_DATA_ERROR;
  1055. s->lzma2.uncompressed -= dict_flush(&s->dict, b);
  1056. if (s->lzma2.uncompressed == 0) {
  1057. if (s->lzma2.pedantic_microlzma) {
  1058. if (s->lzma2.compressed > 0 || s->lzma.len > 0
  1059. || !rc_is_finished(&s->rc))
  1060. return XZ_DATA_ERROR;
  1061. }
  1062. return XZ_STREAM_END;
  1063. }
  1064. if (b->out_pos == b->out_size)
  1065. return XZ_OK;
  1066. if (b->in_pos == b->in_size
  1067. && s->temp.size < s->lzma2.compressed)
  1068. return XZ_OK;
  1069. }
  1070. }
  1071. struct xz_dec_microlzma *xz_dec_microlzma_alloc(enum xz_mode mode,
  1072. uint32_t dict_size)
  1073. {
  1074. struct xz_dec_microlzma *s;
  1075. /* Restrict dict_size to the same range as in the LZMA2 code. */
  1076. if (dict_size < 4096 || dict_size > (3U << 30))
  1077. return NULL;
  1078. s = kmalloc_obj(*s);
  1079. if (s == NULL)
  1080. return NULL;
  1081. s->s.dict.mode = mode;
  1082. s->s.dict.size = dict_size;
  1083. if (DEC_IS_MULTI(mode)) {
  1084. s->s.dict.end = dict_size;
  1085. s->s.dict.buf = vmalloc(dict_size);
  1086. if (s->s.dict.buf == NULL) {
  1087. kfree(s);
  1088. return NULL;
  1089. }
  1090. }
  1091. return s;
  1092. }
  1093. void xz_dec_microlzma_reset(struct xz_dec_microlzma *s, uint32_t comp_size,
  1094. uint32_t uncomp_size, int uncomp_size_is_exact)
  1095. {
  1096. /*
  1097. * comp_size is validated in xz_dec_microlzma_run().
  1098. * uncomp_size can safely be anything.
  1099. */
  1100. s->s.lzma2.compressed = comp_size;
  1101. s->s.lzma2.uncompressed = uncomp_size;
  1102. s->s.lzma2.pedantic_microlzma = uncomp_size_is_exact;
  1103. s->s.lzma2.sequence = SEQ_PROPERTIES;
  1104. s->s.temp.size = 0;
  1105. }
  1106. void xz_dec_microlzma_end(struct xz_dec_microlzma *s)
  1107. {
  1108. if (DEC_IS_MULTI(s->s.dict.mode))
  1109. vfree(s->s.dict.buf);
  1110. kfree(s);
  1111. }
  1112. #endif