xz_dec_lzma2.c 33 KB

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