lz_decoder.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file lz_decoder.c
  4. /// \brief LZ out window
  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. // liblzma supports multiple LZ77-based filters. The LZ part is shared
  14. // between these filters. The LZ code takes care of dictionary handling
  15. // and passing the data between filters in the chain. The filter-specific
  16. // part decodes from the input buffer to the dictionary.
  17. #include "lz_decoder.h"
  18. typedef struct {
  19. /// Dictionary (history buffer)
  20. lzma_dict dict;
  21. /// The actual LZ-based decoder e.g. LZMA
  22. lzma_lz_decoder lz;
  23. /// Next filter in the chain, if any. Note that LZMA and LZMA2 are
  24. /// only allowed as the last filter, but the long-range filter in
  25. /// future can be in the middle of the chain.
  26. lzma_next_coder next;
  27. /// True if the next filter in the chain has returned LZMA_STREAM_END.
  28. bool next_finished;
  29. /// True if the LZ decoder (e.g. LZMA) has detected end of payload
  30. /// marker. This may become true before next_finished becomes true.
  31. bool this_finished;
  32. /// Temporary buffer needed when the LZ-based filter is not the last
  33. /// filter in the chain. The output of the next filter is first
  34. /// decoded into buffer[], which is then used as input for the actual
  35. /// LZ-based decoder.
  36. struct {
  37. size_t pos;
  38. size_t size;
  39. uint8_t buffer[LZMA_BUFFER_SIZE];
  40. } temp;
  41. } lzma_coder;
  42. static void
  43. lz_decoder_reset(lzma_coder *coder)
  44. {
  45. coder->dict.pos = 0;
  46. coder->dict.full = 0;
  47. coder->dict.buf[coder->dict.size - 1] = '\0';
  48. coder->dict.need_reset = false;
  49. return;
  50. }
  51. static lzma_ret
  52. decode_buffer(lzma_coder *coder,
  53. const uint8_t *restrict in, size_t *restrict in_pos,
  54. size_t in_size, uint8_t *restrict out,
  55. size_t *restrict out_pos, size_t out_size)
  56. {
  57. while (true) {
  58. // Wrap the dictionary if needed.
  59. if (coder->dict.pos == coder->dict.size)
  60. coder->dict.pos = 0;
  61. // Store the current dictionary position. It is needed to know
  62. // where to start copying to the out[] buffer.
  63. const size_t dict_start = coder->dict.pos;
  64. // Calculate how much we allow coder->lz.code() to decode.
  65. // It must not decode past the end of the dictionary
  66. // buffer, and we don't want it to decode more than is
  67. // actually needed to fill the out[] buffer.
  68. coder->dict.limit = coder->dict.pos
  69. + my_min(out_size - *out_pos,
  70. coder->dict.size - coder->dict.pos);
  71. // Call the coder->lz.code() to do the actual decoding.
  72. const lzma_ret ret = coder->lz.code(
  73. coder->lz.coder, &coder->dict,
  74. in, in_pos, in_size);
  75. // Copy the decoded data from the dictionary to the out[]
  76. // buffer.
  77. const size_t copy_size = coder->dict.pos - dict_start;
  78. assert(copy_size <= out_size - *out_pos);
  79. memcpy(out + *out_pos, coder->dict.buf + dict_start,
  80. copy_size);
  81. *out_pos += copy_size;
  82. // Reset the dictionary if so requested by coder->lz.code().
  83. if (coder->dict.need_reset) {
  84. lz_decoder_reset(coder);
  85. // Since we reset dictionary, we don't check if
  86. // dictionary became full.
  87. if (ret != LZMA_OK || *out_pos == out_size)
  88. return ret;
  89. } else {
  90. // Return if everything got decoded or an error
  91. // occurred, or if there's no more data to decode.
  92. //
  93. // Note that detecting if there's something to decode
  94. // is done by looking if dictionary become full
  95. // instead of looking if *in_pos == in_size. This
  96. // is because it is possible that all the input was
  97. // consumed already but some data is pending to be
  98. // written to the dictionary.
  99. if (ret != LZMA_OK || *out_pos == out_size
  100. || coder->dict.pos < coder->dict.size)
  101. return ret;
  102. }
  103. }
  104. }
  105. static lzma_ret
  106. lz_decode(void *coder_ptr,
  107. const lzma_allocator *allocator lzma_attribute((__unused__)),
  108. const uint8_t *restrict in, size_t *restrict in_pos,
  109. size_t in_size, uint8_t *restrict out,
  110. size_t *restrict out_pos, size_t out_size,
  111. lzma_action action)
  112. {
  113. lzma_coder *coder = coder_ptr;
  114. if (coder->next.code == NULL)
  115. return decode_buffer(coder, in, in_pos, in_size,
  116. out, out_pos, out_size);
  117. // We aren't the last coder in the chain, we need to decode
  118. // our input to a temporary buffer.
  119. while (*out_pos < out_size) {
  120. // Fill the temporary buffer if it is empty.
  121. if (!coder->next_finished
  122. && coder->temp.pos == coder->temp.size) {
  123. coder->temp.pos = 0;
  124. coder->temp.size = 0;
  125. const lzma_ret ret = coder->next.code(
  126. coder->next.coder,
  127. allocator, in, in_pos, in_size,
  128. coder->temp.buffer, &coder->temp.size,
  129. LZMA_BUFFER_SIZE, action);
  130. if (ret == LZMA_STREAM_END)
  131. coder->next_finished = true;
  132. else if (ret != LZMA_OK || coder->temp.size == 0)
  133. return ret;
  134. }
  135. if (coder->this_finished) {
  136. if (coder->temp.size != 0)
  137. return LZMA_DATA_ERROR;
  138. if (coder->next_finished)
  139. return LZMA_STREAM_END;
  140. return LZMA_OK;
  141. }
  142. const lzma_ret ret = decode_buffer(coder, coder->temp.buffer,
  143. &coder->temp.pos, coder->temp.size,
  144. out, out_pos, out_size);
  145. if (ret == LZMA_STREAM_END)
  146. coder->this_finished = true;
  147. else if (ret != LZMA_OK)
  148. return ret;
  149. else if (coder->next_finished && *out_pos < out_size)
  150. return LZMA_DATA_ERROR;
  151. }
  152. return LZMA_OK;
  153. }
  154. static void
  155. lz_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
  156. {
  157. lzma_coder *coder = coder_ptr;
  158. lzma_next_end(&coder->next, allocator);
  159. lzma_free(coder->dict.buf, allocator);
  160. if (coder->lz.end != NULL)
  161. coder->lz.end(coder->lz.coder, allocator);
  162. else
  163. lzma_free(coder->lz.coder, allocator);
  164. lzma_free(coder, allocator);
  165. return;
  166. }
  167. extern lzma_ret
  168. lzma_lz_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  169. const lzma_filter_info *filters,
  170. lzma_ret (*lz_init)(lzma_lz_decoder *lz,
  171. const lzma_allocator *allocator, const void *options,
  172. lzma_lz_options *lz_options))
  173. {
  174. // Allocate the base structure if it isn't already allocated.
  175. lzma_coder *coder = next->coder;
  176. if (coder == NULL) {
  177. coder = lzma_alloc(sizeof(lzma_coder), allocator);
  178. if (coder == NULL)
  179. return LZMA_MEM_ERROR;
  180. next->coder = coder;
  181. next->code = &lz_decode;
  182. next->end = &lz_decoder_end;
  183. coder->dict.buf = NULL;
  184. coder->dict.size = 0;
  185. coder->lz = LZMA_LZ_DECODER_INIT;
  186. coder->next = LZMA_NEXT_CODER_INIT;
  187. }
  188. // Allocate and initialize the LZ-based decoder. It will also give
  189. // us the dictionary size.
  190. lzma_lz_options lz_options;
  191. return_if_error(lz_init(&coder->lz, allocator,
  192. filters[0].options, &lz_options));
  193. // If the dictionary size is very small, increase it to 4096 bytes.
  194. // This is to prevent constant wrapping of the dictionary, which
  195. // would slow things down. The downside is that since we don't check
  196. // separately for the real dictionary size, we may happily accept
  197. // corrupt files.
  198. if (lz_options.dict_size < 4096)
  199. lz_options.dict_size = 4096;
  200. // Make dictionary size a multipe of 16. Some LZ-based decoders like
  201. // LZMA use the lowest bits lzma_dict.pos to know the alignment of the
  202. // data. Aligned buffer is also good when memcpying from the
  203. // dictionary to the output buffer, since applications are
  204. // recommended to give aligned buffers to liblzma.
  205. //
  206. // Avoid integer overflow.
  207. if (lz_options.dict_size > SIZE_MAX - 15)
  208. return LZMA_MEM_ERROR;
  209. lz_options.dict_size = (lz_options.dict_size + 15) & ~((size_t)(15));
  210. // Allocate and initialize the dictionary.
  211. if (coder->dict.size != lz_options.dict_size) {
  212. lzma_free(coder->dict.buf, allocator);
  213. coder->dict.buf
  214. = lzma_alloc(lz_options.dict_size, allocator);
  215. if (coder->dict.buf == NULL)
  216. return LZMA_MEM_ERROR;
  217. coder->dict.size = lz_options.dict_size;
  218. }
  219. lz_decoder_reset(next->coder);
  220. // Use the preset dictionary if it was given to us.
  221. if (lz_options.preset_dict != NULL
  222. && lz_options.preset_dict_size > 0) {
  223. // If the preset dictionary is bigger than the actual
  224. // dictionary, copy only the tail.
  225. const size_t copy_size = my_min(lz_options.preset_dict_size,
  226. lz_options.dict_size);
  227. const size_t offset = lz_options.preset_dict_size - copy_size;
  228. memcpy(coder->dict.buf, lz_options.preset_dict + offset,
  229. copy_size);
  230. coder->dict.pos = copy_size;
  231. coder->dict.full = copy_size;
  232. }
  233. // Miscellaneous initializations
  234. coder->next_finished = false;
  235. coder->this_finished = false;
  236. coder->temp.pos = 0;
  237. coder->temp.size = 0;
  238. // Initialize the next filter in the chain, if any.
  239. return lzma_next_filter_init(&coder->next, allocator, filters + 1);
  240. }
  241. extern uint64_t
  242. lzma_lz_decoder_memusage(size_t dictionary_size)
  243. {
  244. return sizeof(lzma_coder) + (uint64_t)(dictionary_size);
  245. }
  246. extern void
  247. lzma_lz_decoder_uncompressed(void *coder_ptr, lzma_vli uncompressed_size)
  248. {
  249. lzma_coder *coder = coder_ptr;
  250. coder->lz.set_uncompressed(coder->lz.coder, uncompressed_size);
  251. }