12940b44dSPeter Avalos ///////////////////////////////////////////////////////////////////////////////
22940b44dSPeter Avalos //
32940b44dSPeter Avalos /// \file stream_buffer_decoder.c
42940b44dSPeter Avalos /// \brief Single-call .xz Stream decoder
52940b44dSPeter Avalos //
62940b44dSPeter Avalos // Author: Lasse Collin
72940b44dSPeter Avalos //
82940b44dSPeter Avalos // This file has been put into the public domain.
92940b44dSPeter Avalos // You can do whatever you want with this file.
102940b44dSPeter Avalos //
112940b44dSPeter Avalos ///////////////////////////////////////////////////////////////////////////////
122940b44dSPeter Avalos
132940b44dSPeter Avalos #include "stream_decoder.h"
142940b44dSPeter Avalos
152940b44dSPeter Avalos
162940b44dSPeter Avalos extern LZMA_API(lzma_ret)
lzma_stream_buffer_decode(uint64_t * memlimit,uint32_t flags,const lzma_allocator * allocator,const uint8_t * in,size_t * in_pos,size_t in_size,uint8_t * out,size_t * out_pos,size_t out_size)172940b44dSPeter Avalos lzma_stream_buffer_decode(uint64_t *memlimit, uint32_t flags,
18*15ab8c86SJohn Marino const lzma_allocator *allocator,
192940b44dSPeter Avalos const uint8_t *in, size_t *in_pos, size_t in_size,
202940b44dSPeter Avalos uint8_t *out, size_t *out_pos, size_t out_size)
212940b44dSPeter Avalos {
222940b44dSPeter Avalos // Sanity checks
232940b44dSPeter Avalos if (in_pos == NULL || (in == NULL && *in_pos != in_size)
242940b44dSPeter Avalos || *in_pos > in_size || out_pos == NULL
252940b44dSPeter Avalos || (out == NULL && *out_pos != out_size)
262940b44dSPeter Avalos || *out_pos > out_size)
272940b44dSPeter Avalos return LZMA_PROG_ERROR;
282940b44dSPeter Avalos
292940b44dSPeter Avalos // Catch flags that are not allowed in buffer-to-buffer decoding.
302940b44dSPeter Avalos if (flags & LZMA_TELL_ANY_CHECK)
312940b44dSPeter Avalos return LZMA_PROG_ERROR;
322940b44dSPeter Avalos
332940b44dSPeter Avalos // Initialize the Stream decoder.
342940b44dSPeter Avalos // TODO: We need something to tell the decoder that it can use the
352940b44dSPeter Avalos // output buffer as workspace, and thus save significant amount of RAM.
362940b44dSPeter Avalos lzma_next_coder stream_decoder = LZMA_NEXT_CODER_INIT;
372940b44dSPeter Avalos lzma_ret ret = lzma_stream_decoder_init(
382940b44dSPeter Avalos &stream_decoder, allocator, *memlimit, flags);
392940b44dSPeter Avalos
402940b44dSPeter Avalos if (ret == LZMA_OK) {
412940b44dSPeter Avalos // Save the positions so that we can restore them in case
422940b44dSPeter Avalos // an error occurs.
432940b44dSPeter Avalos const size_t in_start = *in_pos;
442940b44dSPeter Avalos const size_t out_start = *out_pos;
452940b44dSPeter Avalos
462940b44dSPeter Avalos // Do the actual decoding.
472940b44dSPeter Avalos ret = stream_decoder.code(stream_decoder.coder, allocator,
482940b44dSPeter Avalos in, in_pos, in_size, out, out_pos, out_size,
492940b44dSPeter Avalos LZMA_FINISH);
502940b44dSPeter Avalos
512940b44dSPeter Avalos if (ret == LZMA_STREAM_END) {
522940b44dSPeter Avalos ret = LZMA_OK;
532940b44dSPeter Avalos } else {
542940b44dSPeter Avalos // Something went wrong, restore the positions.
552940b44dSPeter Avalos *in_pos = in_start;
562940b44dSPeter Avalos *out_pos = out_start;
572940b44dSPeter Avalos
582940b44dSPeter Avalos if (ret == LZMA_OK) {
592940b44dSPeter Avalos // Either the input was truncated or the
602940b44dSPeter Avalos // output buffer was too small.
612940b44dSPeter Avalos assert(*in_pos == in_size
622940b44dSPeter Avalos || *out_pos == out_size);
632940b44dSPeter Avalos
642940b44dSPeter Avalos // If all the input was consumed, then the
652940b44dSPeter Avalos // input is truncated, even if the output
662940b44dSPeter Avalos // buffer is also full. This is because
672940b44dSPeter Avalos // processing the last byte of the Stream
682940b44dSPeter Avalos // never produces output.
692940b44dSPeter Avalos if (*in_pos == in_size)
702940b44dSPeter Avalos ret = LZMA_DATA_ERROR;
712940b44dSPeter Avalos else
722940b44dSPeter Avalos ret = LZMA_BUF_ERROR;
732940b44dSPeter Avalos
742940b44dSPeter Avalos } else if (ret == LZMA_MEMLIMIT_ERROR) {
752940b44dSPeter Avalos // Let the caller know how much memory would
762940b44dSPeter Avalos // have been needed.
772940b44dSPeter Avalos uint64_t memusage;
782940b44dSPeter Avalos (void)stream_decoder.memconfig(
792940b44dSPeter Avalos stream_decoder.coder,
802940b44dSPeter Avalos memlimit, &memusage, 0);
812940b44dSPeter Avalos }
822940b44dSPeter Avalos }
832940b44dSPeter Avalos }
842940b44dSPeter Avalos
852940b44dSPeter Avalos // Free the decoder memory. This needs to be done even if
862940b44dSPeter Avalos // initialization fails, because the internal API doesn't
872940b44dSPeter Avalos // require the initialization function to free its memory on error.
882940b44dSPeter Avalos lzma_next_end(&stream_decoder, allocator);
892940b44dSPeter Avalos
902940b44dSPeter Avalos return ret;
912940b44dSPeter Avalos }
92