stream_buffer_decoder.c 2.7 KB

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