auto_decoder.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file auto_decoder.c
  4. /// \brief Autodetect between .xz, .lzma (LZMA_Alone), and .lz (lzip)
  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 "stream_decoder.h"
  13. #include "alone_decoder.h"
  14. #ifdef HAVE_LZIP_DECODER
  15. # include "lzip_decoder.h"
  16. #endif
  17. typedef struct {
  18. /// .xz Stream decoder, LZMA_Alone decoder, or lzip decoder
  19. lzma_next_coder next;
  20. uint64_t memlimit;
  21. uint32_t flags;
  22. enum {
  23. SEQ_INIT,
  24. SEQ_CODE,
  25. SEQ_FINISH,
  26. } sequence;
  27. } lzma_auto_coder;
  28. static lzma_ret
  29. auto_decode(void *coder_ptr, const lzma_allocator *allocator,
  30. const uint8_t *restrict in, size_t *restrict in_pos,
  31. size_t in_size, uint8_t *restrict out,
  32. size_t *restrict out_pos, size_t out_size, lzma_action action)
  33. {
  34. lzma_auto_coder *coder = coder_ptr;
  35. switch (coder->sequence) {
  36. case SEQ_INIT:
  37. if (*in_pos >= in_size)
  38. return LZMA_OK;
  39. // Update the sequence now, because we want to continue from
  40. // SEQ_CODE even if we return some LZMA_*_CHECK.
  41. coder->sequence = SEQ_CODE;
  42. // Detect the file format. .xz files start with 0xFD which
  43. // cannot be the first byte of .lzma (LZMA_Alone) format.
  44. // The .lz format starts with 0x4C which could be the
  45. // first byte of a .lzma file but luckily it would mean
  46. // lc/lp/pb being 4/3/1 which liblzma doesn't support because
  47. // lc + lp > 4. So using just 0x4C to detect .lz is OK here.
  48. if (in[*in_pos] == 0xFD) {
  49. return_if_error(lzma_stream_decoder_init(
  50. &coder->next, allocator,
  51. coder->memlimit, coder->flags));
  52. #ifdef HAVE_LZIP_DECODER
  53. } else if (in[*in_pos] == 0x4C) {
  54. return_if_error(lzma_lzip_decoder_init(
  55. &coder->next, allocator,
  56. coder->memlimit, coder->flags));
  57. #endif
  58. } else {
  59. return_if_error(lzma_alone_decoder_init(&coder->next,
  60. allocator, coder->memlimit, true));
  61. // If the application wants to know about missing
  62. // integrity check or about the check in general, we
  63. // need to handle it here, because LZMA_Alone decoder
  64. // doesn't accept any flags.
  65. if (coder->flags & LZMA_TELL_NO_CHECK)
  66. return LZMA_NO_CHECK;
  67. if (coder->flags & LZMA_TELL_ANY_CHECK)
  68. return LZMA_GET_CHECK;
  69. }
  70. // Fall through
  71. case SEQ_CODE: {
  72. const lzma_ret ret = coder->next.code(
  73. coder->next.coder, allocator,
  74. in, in_pos, in_size,
  75. out, out_pos, out_size, action);
  76. if (ret != LZMA_STREAM_END
  77. || (coder->flags & LZMA_CONCATENATED) == 0)
  78. return ret;
  79. coder->sequence = SEQ_FINISH;
  80. }
  81. // Fall through
  82. case SEQ_FINISH:
  83. // When LZMA_CONCATENATED was used and we were decoding
  84. // a LZMA_Alone file, we need to check that there is no
  85. // trailing garbage and wait for LZMA_FINISH.
  86. if (*in_pos < in_size)
  87. return LZMA_DATA_ERROR;
  88. return action == LZMA_FINISH ? LZMA_STREAM_END : LZMA_OK;
  89. default:
  90. assert(0);
  91. return LZMA_PROG_ERROR;
  92. }
  93. }
  94. static void
  95. auto_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
  96. {
  97. lzma_auto_coder *coder = coder_ptr;
  98. lzma_next_end(&coder->next, allocator);
  99. lzma_free(coder, allocator);
  100. return;
  101. }
  102. static lzma_check
  103. auto_decoder_get_check(const void *coder_ptr)
  104. {
  105. const lzma_auto_coder *coder = coder_ptr;
  106. // It is LZMA_Alone if get_check is NULL.
  107. return coder->next.get_check == NULL ? LZMA_CHECK_NONE
  108. : coder->next.get_check(coder->next.coder);
  109. }
  110. static lzma_ret
  111. auto_decoder_memconfig(void *coder_ptr, uint64_t *memusage,
  112. uint64_t *old_memlimit, uint64_t new_memlimit)
  113. {
  114. lzma_auto_coder *coder = coder_ptr;
  115. lzma_ret ret;
  116. if (coder->next.memconfig != NULL) {
  117. ret = coder->next.memconfig(coder->next.coder,
  118. memusage, old_memlimit, new_memlimit);
  119. assert(*old_memlimit == coder->memlimit);
  120. } else {
  121. // No coder is configured yet. Use the base value as
  122. // the current memory usage.
  123. *memusage = LZMA_MEMUSAGE_BASE;
  124. *old_memlimit = coder->memlimit;
  125. ret = LZMA_OK;
  126. if (new_memlimit != 0 && new_memlimit < *memusage)
  127. ret = LZMA_MEMLIMIT_ERROR;
  128. }
  129. if (ret == LZMA_OK && new_memlimit != 0)
  130. coder->memlimit = new_memlimit;
  131. return ret;
  132. }
  133. static lzma_ret
  134. auto_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
  135. uint64_t memlimit, uint32_t flags)
  136. {
  137. lzma_next_coder_init(&auto_decoder_init, next, allocator);
  138. if (flags & ~LZMA_SUPPORTED_FLAGS)
  139. return LZMA_OPTIONS_ERROR;
  140. lzma_auto_coder *coder = next->coder;
  141. if (coder == NULL) {
  142. coder = lzma_alloc(sizeof(lzma_auto_coder), allocator);
  143. if (coder == NULL)
  144. return LZMA_MEM_ERROR;
  145. next->coder = coder;
  146. next->code = &auto_decode;
  147. next->end = &auto_decoder_end;
  148. next->get_check = &auto_decoder_get_check;
  149. next->memconfig = &auto_decoder_memconfig;
  150. coder->next = LZMA_NEXT_CODER_INIT;
  151. }
  152. coder->memlimit = my_max(1, memlimit);
  153. coder->flags = flags;
  154. coder->sequence = SEQ_INIT;
  155. return LZMA_OK;
  156. }
  157. extern LZMA_API(lzma_ret)
  158. lzma_auto_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
  159. {
  160. lzma_next_strm_init(auto_decoder_init, strm, memlimit, flags);
  161. strm->internal->supported_actions[LZMA_RUN] = true;
  162. strm->internal->supported_actions[LZMA_FINISH] = true;
  163. return LZMA_OK;
  164. }