auto_decoder.c 4.8 KB

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