alone_decoder.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file alone_decoder.c
  4. /// \brief Decoder for LZMA_Alone files
  5. //
  6. // Author: Lasse Collin
  7. //
  8. // This file has been put into the public domain.
  9. // You can do whatever you want with this file.
  10. //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "alone_decoder.h"
  13. #include "lzma_decoder.h"
  14. #include "lz_decoder.h"
  15. typedef struct {
  16. lzma_next_coder next;
  17. enum {
  18. SEQ_PROPERTIES,
  19. SEQ_DICTIONARY_SIZE,
  20. SEQ_UNCOMPRESSED_SIZE,
  21. SEQ_CODER_INIT,
  22. SEQ_CODE,
  23. } sequence;
  24. /// If true, reject files that are unlikely to be .lzma files.
  25. /// If false, more non-.lzma files get accepted and will give
  26. /// LZMA_DATA_ERROR either immediately or after a few output bytes.
  27. bool picky;
  28. /// Position in the header fields
  29. size_t pos;
  30. /// Uncompressed size decoded from the header
  31. lzma_vli uncompressed_size;
  32. /// Memory usage limit
  33. uint64_t memlimit;
  34. /// Amount of memory actually needed (only an estimate)
  35. uint64_t memusage;
  36. /// Options decoded from the header needed to initialize
  37. /// the LZMA decoder
  38. lzma_options_lzma options;
  39. } lzma_alone_coder;
  40. static lzma_ret
  41. alone_decode(void *coder_ptr, const lzma_allocator *allocator,
  42. const uint8_t *restrict in, size_t *restrict in_pos,
  43. size_t in_size, uint8_t *restrict out,
  44. size_t *restrict out_pos, size_t out_size,
  45. lzma_action action)
  46. {
  47. lzma_alone_coder *coder = coder_ptr;
  48. while (*out_pos < out_size
  49. && (coder->sequence == SEQ_CODE || *in_pos < in_size))
  50. switch (coder->sequence) {
  51. case SEQ_PROPERTIES:
  52. if (lzma_lzma_lclppb_decode(&coder->options, in[*in_pos]))
  53. return LZMA_FORMAT_ERROR;
  54. coder->sequence = SEQ_DICTIONARY_SIZE;
  55. ++*in_pos;
  56. break;
  57. case SEQ_DICTIONARY_SIZE:
  58. coder->options.dict_size
  59. |= (size_t)(in[*in_pos]) << (coder->pos * 8);
  60. if (++coder->pos == 4) {
  61. if (coder->picky && coder->options.dict_size
  62. != UINT32_MAX) {
  63. // A hack to ditch tons of false positives:
  64. // We allow only dictionary sizes that are
  65. // 2^n or 2^n + 2^(n-1). LZMA_Alone created
  66. // only files with 2^n, but accepts any
  67. // dictionary size.
  68. uint32_t d = coder->options.dict_size - 1;
  69. d |= d >> 2;
  70. d |= d >> 3;
  71. d |= d >> 4;
  72. d |= d >> 8;
  73. d |= d >> 16;
  74. ++d;
  75. if (d != coder->options.dict_size)
  76. return LZMA_FORMAT_ERROR;
  77. }
  78. coder->pos = 0;
  79. coder->sequence = SEQ_UNCOMPRESSED_SIZE;
  80. }
  81. ++*in_pos;
  82. break;
  83. case SEQ_UNCOMPRESSED_SIZE:
  84. coder->uncompressed_size
  85. |= (lzma_vli)(in[*in_pos]) << (coder->pos * 8);
  86. ++*in_pos;
  87. if (++coder->pos < 8)
  88. break;
  89. // Another hack to ditch false positives: Assume that
  90. // if the uncompressed size is known, it must be less
  91. // than 256 GiB.
  92. //
  93. // FIXME? Without picky we allow > LZMA_VLI_MAX which doesn't
  94. // really matter in this specific situation (> LZMA_VLI_MAX is
  95. // safe in the LZMA decoder) but it's somewhat weird still.
  96. if (coder->picky
  97. && coder->uncompressed_size != LZMA_VLI_UNKNOWN
  98. && coder->uncompressed_size
  99. >= (LZMA_VLI_C(1) << 38))
  100. return LZMA_FORMAT_ERROR;
  101. // Use LZMA_FILTER_LZMA1EXT features to specify the
  102. // uncompressed size and that the end marker is allowed
  103. // even when the uncompressed size is known. Both .lzma
  104. // header and LZMA1EXT use UINT64_MAX indicate that size
  105. // is unknown.
  106. coder->options.ext_flags = LZMA_LZMA1EXT_ALLOW_EOPM;
  107. lzma_set_ext_size(coder->options, coder->uncompressed_size);
  108. // Calculate the memory usage so that it is ready
  109. // for SEQ_CODER_INIT.
  110. coder->memusage = lzma_lzma_decoder_memusage(&coder->options)
  111. + LZMA_MEMUSAGE_BASE;
  112. coder->pos = 0;
  113. coder->sequence = SEQ_CODER_INIT;
  114. // Fall through
  115. case SEQ_CODER_INIT: {
  116. if (coder->memusage > coder->memlimit)
  117. return LZMA_MEMLIMIT_ERROR;
  118. lzma_filter_info filters[2] = {
  119. {
  120. .id = LZMA_FILTER_LZMA1EXT,
  121. .init = &lzma_lzma_decoder_init,
  122. .options = &coder->options,
  123. }, {
  124. .init = NULL,
  125. }
  126. };
  127. return_if_error(lzma_next_filter_init(&coder->next,
  128. allocator, filters));
  129. coder->sequence = SEQ_CODE;
  130. break;
  131. }
  132. case SEQ_CODE: {
  133. return coder->next.code(coder->next.coder,
  134. allocator, in, in_pos, in_size,
  135. out, out_pos, out_size, action);
  136. }
  137. default:
  138. return LZMA_PROG_ERROR;
  139. }
  140. return LZMA_OK;
  141. }
  142. static void
  143. alone_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
  144. {
  145. lzma_alone_coder *coder = coder_ptr;
  146. lzma_next_end(&coder->next, allocator);
  147. lzma_free(coder, allocator);
  148. return;
  149. }
  150. static lzma_ret
  151. alone_decoder_memconfig(void *coder_ptr, uint64_t *memusage,
  152. uint64_t *old_memlimit, uint64_t new_memlimit)
  153. {
  154. lzma_alone_coder *coder = coder_ptr;
  155. *memusage = coder->memusage;
  156. *old_memlimit = coder->memlimit;
  157. if (new_memlimit != 0) {
  158. if (new_memlimit < coder->memusage)
  159. return LZMA_MEMLIMIT_ERROR;
  160. coder->memlimit = new_memlimit;
  161. }
  162. return LZMA_OK;
  163. }
  164. extern lzma_ret
  165. lzma_alone_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  166. uint64_t memlimit, bool picky)
  167. {
  168. lzma_next_coder_init(&lzma_alone_decoder_init, next, allocator);
  169. lzma_alone_coder *coder = next->coder;
  170. if (coder == NULL) {
  171. coder = lzma_alloc(sizeof(lzma_alone_coder), allocator);
  172. if (coder == NULL)
  173. return LZMA_MEM_ERROR;
  174. next->coder = coder;
  175. next->code = &alone_decode;
  176. next->end = &alone_decoder_end;
  177. next->memconfig = &alone_decoder_memconfig;
  178. coder->next = LZMA_NEXT_CODER_INIT;
  179. }
  180. coder->sequence = SEQ_PROPERTIES;
  181. coder->picky = picky;
  182. coder->pos = 0;
  183. coder->options.dict_size = 0;
  184. coder->options.preset_dict = NULL;
  185. coder->options.preset_dict_size = 0;
  186. coder->uncompressed_size = 0;
  187. coder->memlimit = my_max(1, memlimit);
  188. coder->memusage = LZMA_MEMUSAGE_BASE;
  189. return LZMA_OK;
  190. }
  191. extern LZMA_API(lzma_ret)
  192. lzma_alone_decoder(lzma_stream *strm, uint64_t memlimit)
  193. {
  194. lzma_next_strm_init(lzma_alone_decoder_init, strm, memlimit, false);
  195. strm->internal->supported_actions[LZMA_RUN] = true;
  196. strm->internal->supported_actions[LZMA_FINISH] = true;
  197. return LZMA_OK;
  198. }