auto_decoder.c 4.6 KB

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