lzma2_decoder.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file lzma2_decoder.c
  4. /// \brief LZMA2 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 "lzma2_decoder.h"
  14. #include "lz_decoder.h"
  15. #include "lzma_decoder.h"
  16. typedef struct {
  17. enum sequence {
  18. SEQ_CONTROL,
  19. SEQ_UNCOMPRESSED_1,
  20. SEQ_UNCOMPRESSED_2,
  21. SEQ_COMPRESSED_0,
  22. SEQ_COMPRESSED_1,
  23. SEQ_PROPERTIES,
  24. SEQ_LZMA,
  25. SEQ_COPY,
  26. } sequence;
  27. /// Sequence after the size fields have been decoded.
  28. enum sequence next_sequence;
  29. /// LZMA decoder
  30. lzma_lz_decoder lzma;
  31. /// Uncompressed size of LZMA chunk
  32. size_t uncompressed_size;
  33. /// Compressed size of the chunk (naturally equals to uncompressed
  34. /// size of uncompressed chunk)
  35. size_t compressed_size;
  36. /// True if properties are needed. This is false before the
  37. /// first LZMA chunk.
  38. bool need_properties;
  39. /// True if dictionary reset is needed. This is false before the
  40. /// first chunk (LZMA or uncompressed).
  41. bool need_dictionary_reset;
  42. lzma_options_lzma options;
  43. } lzma_lzma2_coder;
  44. static lzma_ret
  45. lzma2_decode(void *coder_ptr, lzma_dict *restrict dict,
  46. const uint8_t *restrict in, size_t *restrict in_pos,
  47. size_t in_size)
  48. {
  49. lzma_lzma2_coder *restrict coder = coder_ptr;
  50. // With SEQ_LZMA it is possible that no new input is needed to do
  51. // some progress. The rest of the sequences assume that there is
  52. // at least one byte of input.
  53. while (*in_pos < in_size || coder->sequence == SEQ_LZMA)
  54. switch (coder->sequence) {
  55. case SEQ_CONTROL: {
  56. const uint32_t control = in[*in_pos];
  57. ++*in_pos;
  58. // End marker
  59. if (control == 0x00)
  60. return LZMA_STREAM_END;
  61. if (control >= 0xE0 || control == 1) {
  62. // Dictionary reset implies that next LZMA chunk has
  63. // to set new properties.
  64. coder->need_properties = true;
  65. coder->need_dictionary_reset = true;
  66. } else if (coder->need_dictionary_reset) {
  67. return LZMA_DATA_ERROR;
  68. }
  69. if (control >= 0x80) {
  70. // LZMA chunk. The highest five bits of the
  71. // uncompressed size are taken from the control byte.
  72. coder->uncompressed_size = (control & 0x1F) << 16;
  73. coder->sequence = SEQ_UNCOMPRESSED_1;
  74. // See if there are new properties or if we need to
  75. // reset the state.
  76. if (control >= 0xC0) {
  77. // When there are new properties, state reset
  78. // is done at SEQ_PROPERTIES.
  79. coder->need_properties = false;
  80. coder->next_sequence = SEQ_PROPERTIES;
  81. } else if (coder->need_properties) {
  82. return LZMA_DATA_ERROR;
  83. } else {
  84. coder->next_sequence = SEQ_LZMA;
  85. // If only state reset is wanted with old
  86. // properties, do the resetting here for
  87. // simplicity.
  88. if (control >= 0xA0)
  89. coder->lzma.reset(coder->lzma.coder,
  90. &coder->options);
  91. }
  92. } else {
  93. // Invalid control values
  94. if (control > 2)
  95. return LZMA_DATA_ERROR;
  96. // It's uncompressed chunk
  97. coder->sequence = SEQ_COMPRESSED_0;
  98. coder->next_sequence = SEQ_COPY;
  99. }
  100. if (coder->need_dictionary_reset) {
  101. // Finish the dictionary reset and let the caller
  102. // flush the dictionary to the actual output buffer.
  103. coder->need_dictionary_reset = false;
  104. dict_reset(dict);
  105. return LZMA_OK;
  106. }
  107. break;
  108. }
  109. case SEQ_UNCOMPRESSED_1:
  110. coder->uncompressed_size += (uint32_t)(in[(*in_pos)++]) << 8;
  111. coder->sequence = SEQ_UNCOMPRESSED_2;
  112. break;
  113. case SEQ_UNCOMPRESSED_2:
  114. coder->uncompressed_size += in[(*in_pos)++] + 1;
  115. coder->sequence = SEQ_COMPRESSED_0;
  116. coder->lzma.set_uncompressed(coder->lzma.coder,
  117. coder->uncompressed_size);
  118. break;
  119. case SEQ_COMPRESSED_0:
  120. coder->compressed_size = (uint32_t)(in[(*in_pos)++]) << 8;
  121. coder->sequence = SEQ_COMPRESSED_1;
  122. break;
  123. case SEQ_COMPRESSED_1:
  124. coder->compressed_size += in[(*in_pos)++] + 1;
  125. coder->sequence = coder->next_sequence;
  126. break;
  127. case SEQ_PROPERTIES:
  128. if (lzma_lzma_lclppb_decode(&coder->options, in[(*in_pos)++]))
  129. return LZMA_DATA_ERROR;
  130. coder->lzma.reset(coder->lzma.coder, &coder->options);
  131. coder->sequence = SEQ_LZMA;
  132. break;
  133. case SEQ_LZMA: {
  134. // Store the start offset so that we can update
  135. // coder->compressed_size later.
  136. const size_t in_start = *in_pos;
  137. // Decode from in[] to *dict.
  138. const lzma_ret ret = coder->lzma.code(coder->lzma.coder,
  139. dict, in, in_pos, in_size);
  140. // Validate and update coder->compressed_size.
  141. const size_t in_used = *in_pos - in_start;
  142. if (in_used > coder->compressed_size)
  143. return LZMA_DATA_ERROR;
  144. coder->compressed_size -= in_used;
  145. // Return if we didn't finish the chunk, or an error occurred.
  146. if (ret != LZMA_STREAM_END)
  147. return ret;
  148. // The LZMA decoder must have consumed the whole chunk now.
  149. // We don't need to worry about uncompressed size since it
  150. // is checked by the LZMA decoder.
  151. if (coder->compressed_size != 0)
  152. return LZMA_DATA_ERROR;
  153. coder->sequence = SEQ_CONTROL;
  154. break;
  155. }
  156. case SEQ_COPY: {
  157. // Copy from input to the dictionary as is.
  158. dict_write(dict, in, in_pos, in_size, &coder->compressed_size);
  159. if (coder->compressed_size != 0)
  160. return LZMA_OK;
  161. coder->sequence = SEQ_CONTROL;
  162. break;
  163. }
  164. default:
  165. assert(0);
  166. return LZMA_PROG_ERROR;
  167. }
  168. return LZMA_OK;
  169. }
  170. static void
  171. lzma2_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
  172. {
  173. lzma_lzma2_coder *coder = coder_ptr;
  174. assert(coder->lzma.end == NULL);
  175. lzma_free(coder->lzma.coder, allocator);
  176. lzma_free(coder, allocator);
  177. return;
  178. }
  179. static lzma_ret
  180. lzma2_decoder_init(lzma_lz_decoder *lz, const lzma_allocator *allocator,
  181. const void *opt, lzma_lz_options *lz_options)
  182. {
  183. lzma_lzma2_coder *coder = lz->coder;
  184. if (coder == NULL) {
  185. coder = lzma_alloc(sizeof(lzma_lzma2_coder), allocator);
  186. if (coder == NULL)
  187. return LZMA_MEM_ERROR;
  188. lz->coder = coder;
  189. lz->code = &lzma2_decode;
  190. lz->end = &lzma2_decoder_end;
  191. coder->lzma = LZMA_LZ_DECODER_INIT;
  192. }
  193. const lzma_options_lzma *options = opt;
  194. coder->sequence = SEQ_CONTROL;
  195. coder->need_properties = true;
  196. coder->need_dictionary_reset = options->preset_dict == NULL
  197. || options->preset_dict_size == 0;
  198. return lzma_lzma_decoder_create(&coder->lzma,
  199. allocator, options, lz_options);
  200. }
  201. extern lzma_ret
  202. lzma_lzma2_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  203. const lzma_filter_info *filters)
  204. {
  205. // LZMA2 can only be the last filter in the chain. This is enforced
  206. // by the raw_decoder initialization.
  207. assert(filters[1].init == NULL);
  208. return lzma_lz_decoder_init(next, allocator, filters,
  209. &lzma2_decoder_init);
  210. }
  211. extern uint64_t
  212. lzma_lzma2_decoder_memusage(const void *options)
  213. {
  214. return sizeof(lzma_lzma2_coder)
  215. + lzma_lzma_decoder_memusage_nocheck(options);
  216. }
  217. extern lzma_ret
  218. lzma_lzma2_props_decode(void **options, const lzma_allocator *allocator,
  219. const uint8_t *props, size_t props_size)
  220. {
  221. if (props_size != 1)
  222. return LZMA_OPTIONS_ERROR;
  223. // Check that reserved bits are unset.
  224. if (props[0] & 0xC0)
  225. return LZMA_OPTIONS_ERROR;
  226. // Decode the dictionary size.
  227. if (props[0] > 40)
  228. return LZMA_OPTIONS_ERROR;
  229. lzma_options_lzma *opt = lzma_alloc(
  230. sizeof(lzma_options_lzma), allocator);
  231. if (opt == NULL)
  232. return LZMA_MEM_ERROR;
  233. if (props[0] == 40) {
  234. opt->dict_size = UINT32_MAX;
  235. } else {
  236. opt->dict_size = 2 | (props[0] & 1);
  237. opt->dict_size <<= props[0] / 2 + 11;
  238. }
  239. opt->preset_dict = NULL;
  240. opt->preset_dict_size = 0;
  241. *options = opt;
  242. return LZMA_OK;
  243. }