1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: expandtab:ts=8:sw=4:softtabstop=4:
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file block_buffer_decoder.c
6 /// \brief Single-call .xz Block decoder
7 //
8 // Author: Lasse Collin
9 //
10 // This file has been put into the public domain.
11 // You can do whatever you want with this file.
12 //
13 ///////////////////////////////////////////////////////////////////////////////
14
15 #include "block_decoder.h"
16
17
18 extern LZMA_API(lzma_ret)
lzma_block_buffer_decode(lzma_block * block,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)19 lzma_block_buffer_decode(lzma_block *block, lzma_allocator *allocator,
20 const uint8_t *in, size_t *in_pos, size_t in_size,
21 uint8_t *out, size_t *out_pos, size_t out_size)
22 {
23 if (in_pos == NULL || (in == NULL && *in_pos != in_size)
24 || *in_pos > in_size || out_pos == NULL
25 || (out == NULL && *out_pos != out_size)
26 || *out_pos > out_size)
27 return LZMA_PROG_ERROR;
28
29 // Initialize the Block decoder.
30 lzma_next_coder block_decoder = LZMA_NEXT_CODER_INIT;
31 lzma_ret ret = lzma_block_decoder_init(
32 &block_decoder, allocator, block);
33
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
40 // Do the actual decoding.
41 ret = block_decoder.code(block_decoder.coder, allocator,
42 in, in_pos, in_size, out, out_pos, out_size,
43 LZMA_FINISH);
44
45 if (ret == LZMA_STREAM_END) {
46 ret = LZMA_OK;
47 } else {
48 if (ret == LZMA_OK) {
49 // Either the input was truncated or the
50 // output buffer was too small.
51 assert(*in_pos == in_size
52 || *out_pos == out_size);
53
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 Block
58 // never produces output.
59 //
60 // NOTE: This assumption may break when new
61 // filters are added, if the end marker of
62 // the filter doesn't consume at least one
63 // complete byte.
64 if (*in_pos == in_size)
65 ret = LZMA_DATA_ERROR;
66 else
67 ret = LZMA_BUF_ERROR;
68 }
69
70 // Restore the positions.
71 *in_pos = in_start;
72 *out_pos = out_start;
73 }
74 }
75
76 // Free the decoder memory. This needs to be done even if
77 // initialization fails, because the internal API doesn't
78 // require the initialization function to free its memory on error.
79 lzma_next_end(&block_decoder, allocator);
80
81 return ret;
82 }
83