]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/block_buffer_decoder.c
Fix a bug in lzma_block_buffer_decode(), although this
[icculus/xz.git] / src / liblzma / common / block_buffer_decoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       block_buffer_decoder.c
4 /// \brief      Single-call .xz Block decoder
5 //
6 //  Copyright (C) 2009 Lasse Collin
7 //
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.
12 //
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.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "block_decoder.h"
21
22
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)
27 {
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;
33
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);
38
39         if (ret == LZMA_OK) {
40                 // Save the positions so that we can restore them in case
41                 // an error occurs.
42                 const size_t in_start = *in_pos;
43                 const size_t out_start = *out_pos;
44
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,
48                                 LZMA_FINISH);
49
50                 if (ret == LZMA_STREAM_END) {
51                         ret = LZMA_OK;
52                 } else {
53                         if (ret == LZMA_OK) {
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);
58
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.
64                                 //
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
68                                 // complete byte.
69                                 if (*in_pos == in_size)
70                                         ret = LZMA_DATA_ERROR;
71                                 else
72                                         ret = LZMA_BUF_ERROR;
73                         }
74
75                         // Restore the positions.
76                         *in_pos = in_start;
77                         *out_pos = out_start;
78                 }
79         }
80
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);
85
86         return ret;
87 }