stream_buffer_decoder.c 2.7 KB

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