alone_decoder.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. struct lzma_coder_s {
  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. };
  40. static lzma_ret
  41. alone_decode(lzma_coder *coder,
  42. lzma_allocator *allocator lzma_attribute((__unused__)),
  43. const uint8_t *LZMA_RESTRICT in, size_t *LZMA_RESTRICT in_pos,
  44. size_t in_size, uint8_t *LZMA_RESTRICT out,
  45. size_t *LZMA_RESTRICT out_pos, size_t out_size,
  46. lzma_action action)
  47. {
  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. if (coder->picky
  93. && coder->uncompressed_size != LZMA_VLI_UNKNOWN
  94. && coder->uncompressed_size
  95. >= (LZMA_VLI_C(1) << 38))
  96. return LZMA_FORMAT_ERROR;
  97. // Calculate the memory usage so that it is ready
  98. // for SEQ_CODER_INIT.
  99. coder->memusage = lzma_lzma_decoder_memusage(&coder->options)
  100. + LZMA_MEMUSAGE_BASE;
  101. coder->pos = 0;
  102. coder->sequence = SEQ_CODER_INIT;
  103. // Fall through
  104. case SEQ_CODER_INIT: {
  105. lzma_ret ret;
  106. lzma_filter_info filters[2] = {
  107. { 0, &lzma_lzma_decoder_init, &coder->options },
  108. { 0, NULL, NULL }
  109. };
  110. if (coder->memusage > coder->memlimit)
  111. return LZMA_MEMLIMIT_ERROR;
  112. ret = lzma_next_filter_init(&coder->next,
  113. allocator, filters);
  114. if (ret != LZMA_OK)
  115. return ret;
  116. // Use a hack to set the uncompressed size.
  117. lzma_lz_decoder_uncompressed(coder->next.coder,
  118. coder->uncompressed_size);
  119. coder->sequence = SEQ_CODE;
  120. break;
  121. }
  122. case SEQ_CODE: {
  123. return coder->next.code(coder->next.coder,
  124. allocator, in, in_pos, in_size,
  125. out, out_pos, out_size, action);
  126. }
  127. default:
  128. return LZMA_PROG_ERROR;
  129. }
  130. return LZMA_OK;
  131. }
  132. static void
  133. alone_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
  134. {
  135. lzma_next_end(&coder->next, allocator);
  136. lzma_free(coder, allocator);
  137. return;
  138. }
  139. static lzma_ret
  140. alone_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
  141. uint64_t *old_memlimit, uint64_t new_memlimit)
  142. {
  143. *memusage = coder->memusage;
  144. *old_memlimit = coder->memlimit;
  145. if (new_memlimit != 0) {
  146. if (new_memlimit < coder->memusage)
  147. return LZMA_MEMLIMIT_ERROR;
  148. coder->memlimit = new_memlimit;
  149. }
  150. return LZMA_OK;
  151. }
  152. extern lzma_ret
  153. lzma_alone_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
  154. uint64_t memlimit, bool picky)
  155. {
  156. lzma_next_coder_init(&lzma_alone_decoder_init, next, allocator);
  157. if (memlimit == 0)
  158. return LZMA_PROG_ERROR;
  159. if (next->coder == NULL) {
  160. next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
  161. if (next->coder == NULL)
  162. return LZMA_MEM_ERROR;
  163. next->code = &alone_decode;
  164. next->end = &alone_decoder_end;
  165. next->memconfig = &alone_decoder_memconfig;
  166. next->coder->next = LZMA_NEXT_CODER_INIT;
  167. }
  168. next->coder->sequence = SEQ_PROPERTIES;
  169. next->coder->picky = picky;
  170. next->coder->pos = 0;
  171. next->coder->options.dict_size = 0;
  172. next->coder->options.preset_dict = NULL;
  173. next->coder->options.preset_dict_size = 0;
  174. next->coder->uncompressed_size = 0;
  175. next->coder->memlimit = memlimit;
  176. next->coder->memusage = LZMA_MEMUSAGE_BASE;
  177. return LZMA_OK;
  178. }
  179. extern LZMA_API(lzma_ret)
  180. lzma_alone_decoder(lzma_stream *strm, uint64_t memlimit)
  181. {
  182. lzma_next_strm_init2(lzma_alone_decoder_init, strm, memlimit, false);
  183. strm->internal->supported_actions[LZMA_RUN] = true;
  184. strm->internal->supported_actions[LZMA_FINISH] = true;
  185. return LZMA_OK;
  186. }