lz_decoder.c 9.5 KB

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