auto_decoder.c 5.1 KB

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