stream_buffer_decoder.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file stream_buffer_decoder.c
  5. /// \brief Single-call .xz Stream decoder
  6. //
  7. // Author: Lasse Collin
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include "stream_decoder.h"
  11. extern LZMA_API(lzma_ret)
  12. lzma_stream_buffer_decode(uint64_t *memlimit, uint32_t flags,
  13. const lzma_allocator *allocator,
  14. const uint8_t *in, size_t *in_pos, size_t in_size,
  15. uint8_t *out, size_t *out_pos, size_t out_size)
  16. {
  17. // Sanity checks
  18. if (in_pos == NULL || (in == NULL && *in_pos != in_size)
  19. || *in_pos > in_size || out_pos == NULL
  20. || (out == NULL && *out_pos != out_size)
  21. || *out_pos > out_size)
  22. return LZMA_PROG_ERROR;
  23. // Catch flags that are not allowed in buffer-to-buffer decoding.
  24. if (flags & LZMA_TELL_ANY_CHECK)
  25. return LZMA_PROG_ERROR;
  26. // Initialize the Stream decoder.
  27. // TODO: We need something to tell the decoder that it can use the
  28. // output buffer as workspace, and thus save significant amount of RAM.
  29. lzma_next_coder stream_decoder = LZMA_NEXT_CODER_INIT;
  30. lzma_ret ret = lzma_stream_decoder_init(
  31. &stream_decoder, allocator, *memlimit, flags);
  32. if (ret == LZMA_OK) {
  33. // Save the positions so that we can restore them in case
  34. // an error occurs.
  35. const size_t in_start = *in_pos;
  36. const size_t out_start = *out_pos;
  37. // Do the actual decoding.
  38. ret = stream_decoder.code(stream_decoder.coder, allocator,
  39. in, in_pos, in_size, out, out_pos, out_size,
  40. LZMA_FINISH);
  41. if (ret == LZMA_STREAM_END) {
  42. ret = LZMA_OK;
  43. } else {
  44. // Something went wrong, restore the positions.
  45. *in_pos = in_start;
  46. *out_pos = out_start;
  47. if (ret == LZMA_OK) {
  48. // Either the input was truncated or the
  49. // output buffer was too small.
  50. assert(*in_pos == in_size
  51. || *out_pos == out_size);
  52. // If all the input was consumed, then the
  53. // input is truncated, even if the output
  54. // buffer is also full. This is because
  55. // processing the last byte of the Stream
  56. // never produces output.
  57. if (*in_pos == in_size)
  58. ret = LZMA_DATA_ERROR;
  59. else
  60. ret = LZMA_BUF_ERROR;
  61. } else if (ret == LZMA_MEMLIMIT_ERROR) {
  62. // Let the caller know how much memory would
  63. // have been needed.
  64. uint64_t memusage;
  65. (void)stream_decoder.memconfig(
  66. stream_decoder.coder,
  67. memlimit, &memusage, 0);
  68. }
  69. }
  70. }
  71. // Free the decoder memory. This needs to be done even if
  72. // initialization fails, because the internal API doesn't
  73. // require the initialization function to free its memory on error.
  74. lzma_next_end(&stream_decoder, allocator);
  75. return ret;
  76. }