1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file block_buffer_decoder.c
4 /// \brief Single-call .xz Block decoder
6 // Copyright (C) 2009 Lasse Collin
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
20 #include "block_decoder.h"
23 extern LZMA_API(lzma_ret)
24 lzma_block_buffer_decode(lzma_block *block, lzma_allocator *allocator,
25 const uint8_t *in, size_t *in_pos, size_t in_size,
26 uint8_t *out, size_t *out_pos, size_t out_size)
28 if (in_pos == NULL || (in == NULL && *in_pos != in_size)
29 || *in_pos > in_size || out_pos == NULL
30 || (out == NULL && *out_pos != out_size)
31 || *out_pos > out_size)
32 return LZMA_PROG_ERROR;
34 // Initialize the Block decoder.
35 lzma_next_coder block_decoder = LZMA_NEXT_CODER_INIT;
36 lzma_ret ret = lzma_block_decoder_init(
37 &block_decoder, allocator, block);
40 // Save the positions so that we can restore them in case
42 const size_t in_start = *in_pos;
43 const size_t out_start = *out_pos;
45 // Do the actual decoding.
46 ret = block_decoder.code(block_decoder.coder, allocator,
47 in, in_pos, in_size, out, out_pos, out_size,
50 if (ret == LZMA_STREAM_END) {
54 // Either the input was truncated or the
55 // output buffer was too small.
56 assert(*in_pos == in_size
57 || *out_pos == out_size);
59 // If all the input was consumed, then the
60 // input is truncated, even if the output
61 // buffer is also full. This is because
62 // processing the last byte of the Block
63 // never produces output.
65 // NOTE: This assumption may break when new
66 // filters are added, if the end marker of
67 // the filter doesn't consume at least one
69 if (*in_pos == in_size)
70 ret = LZMA_DATA_ERROR;
75 // Restore the positions.
81 // Free the decoder memory. This needs to be done even if
82 // initialization fails, because the internal API doesn't
83 // require the initialization function to free its memory on error.
84 lzma_next_end(&block_decoder, allocator);