1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file block_decoder.c
4 /// \brief Decodes .xz Blocks
6 // Copyright (C) 2007 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"
21 #include "filter_decoder.h"
32 /// The filters in the chain; initialized with lzma_raw_decoder_init().
35 /// Decoding options; we also write Compressed Size and Uncompressed
36 /// Size back to this structure when the decoding has been finished.
39 /// Compressed Size calculated while decoding
40 lzma_vli compressed_size;
42 /// Uncompressed Size calculated while decoding
43 lzma_vli uncompressed_size;
45 /// Maximum allowed Compressed Size; this takes into account the
46 /// size of the Block Header and Check fields when Compressed Size
48 lzma_vli compressed_limit;
50 /// Position when reading the Check field
53 /// Check of the uncompressed data
54 lzma_check_state check;
59 update_size(lzma_vli *size, lzma_vli add, lzma_vli limit)
61 if (limit > LZMA_VLI_MAX)
64 if (limit < *size || limit - *size < add)
74 is_size_valid(lzma_vli size, lzma_vli reference)
76 return reference == LZMA_VLI_UNKNOWN || reference == size;
81 block_decode(lzma_coder *coder, lzma_allocator *allocator,
82 const uint8_t *restrict in, size_t *restrict in_pos,
83 size_t in_size, uint8_t *restrict out,
84 size_t *restrict out_pos, size_t out_size, lzma_action action)
86 switch (coder->sequence) {
88 const size_t in_start = *in_pos;
89 const size_t out_start = *out_pos;
91 const lzma_ret ret = coder->next.code(coder->next.coder,
92 allocator, in, in_pos, in_size,
93 out, out_pos, out_size, action);
95 const size_t in_used = *in_pos - in_start;
96 const size_t out_used = *out_pos - out_start;
98 // NOTE: We compare to compressed_limit here, which prevents
99 // the total size of the Block growing past LZMA_VLI_MAX.
100 if (update_size(&coder->compressed_size, in_used,
101 coder->compressed_limit)
102 || update_size(&coder->uncompressed_size,
104 coder->block->uncompressed_size))
105 return LZMA_DATA_ERROR;
107 lzma_check_update(&coder->check, coder->block->check,
108 out + out_start, out_used);
110 if (ret != LZMA_STREAM_END)
113 // Compressed and Uncompressed Sizes are now at their final
114 // values. Verify that they match the values given to us.
115 if (!is_size_valid(coder->compressed_size,
116 coder->block->compressed_size)
117 || !is_size_valid(coder->uncompressed_size,
118 coder->block->uncompressed_size))
119 return LZMA_DATA_ERROR;
121 // Copy the values into coder->block. The caller
122 // may use this information to construct Index.
123 coder->block->compressed_size = coder->compressed_size;
124 coder->block->uncompressed_size = coder->uncompressed_size;
126 coder->sequence = SEQ_PADDING;
132 // Compressed Data is padded to a multiple of four bytes.
133 while (coder->compressed_size & 3) {
134 if (*in_pos >= in_size)
137 // We use compressed_size here just get the Padding
138 // right. The actual Compressed Size was stored to
139 // coder->block already, and won't be modified by
141 ++coder->compressed_size;
143 if (in[(*in_pos)++] != 0x00)
144 return LZMA_DATA_ERROR;
147 if (coder->block->check == LZMA_CHECK_NONE)
148 return LZMA_STREAM_END;
150 lzma_check_finish(&coder->check, coder->block->check);
151 coder->sequence = SEQ_CHECK;
156 const bool chksup = lzma_check_is_supported(
157 coder->block->check);
159 while (*in_pos < in_size) {
160 // coder->check.buffer[] may be uninitialized when
161 // the Check ID is not supported.
162 if (chksup && coder->check.buffer.u8[coder->check_pos]
165 return LZMA_DATA_ERROR;
170 if (++coder->check_pos == lzma_check_size(
171 coder->block->check))
172 return LZMA_STREAM_END;
179 return LZMA_PROG_ERROR;
184 block_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
186 lzma_next_end(&coder->next, allocator);
187 lzma_free(coder, allocator);
193 lzma_block_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
196 lzma_next_coder_init(lzma_block_decoder_init, next, allocator);
198 // Validate the options. lzma_block_unpadded_size() does that for us
199 // except for Uncompressed Size and filters. Filters are validated
200 // by the raw decoder.
201 if (lzma_block_unpadded_size(block) == 0
202 || !lzma_vli_is_valid(block->uncompressed_size))
203 return LZMA_PROG_ERROR;
205 // Allocate and initialize *next->coder if needed.
206 if (next->coder == NULL) {
207 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
208 if (next->coder == NULL)
209 return LZMA_MEM_ERROR;
211 next->code = &block_decode;
212 next->end = &block_decoder_end;
213 next->coder->next = LZMA_NEXT_CODER_INIT;
216 // Basic initializations
217 next->coder->sequence = SEQ_CODE;
218 next->coder->block = block;
219 next->coder->compressed_size = 0;
220 next->coder->uncompressed_size = 0;
222 // If Compressed Size is not known, we calculate the maximum allowed
223 // value so that encoded size of the Block (including Block Padding)
224 // is still a valid VLI and a multiple of four.
225 next->coder->compressed_limit
226 = block->compressed_size == LZMA_VLI_UNKNOWN
227 ? (LZMA_VLI_MAX & ~LZMA_VLI_C(3))
229 - lzma_check_size(block->check)
230 : block->compressed_size;
232 // Initialize the check. It's caller's problem if the Check ID is not
233 // supported, and the Block decoder cannot verify the Check field.
234 // Caller can test lzma_check_is_supported(block->check).
235 next->coder->check_pos = 0;
236 lzma_check_init(&next->coder->check, block->check);
238 // Initialize the filter chain.
239 return lzma_raw_decoder_init(&next->coder->next, allocator,
244 extern LZMA_API lzma_ret
245 lzma_block_decoder(lzma_stream *strm, lzma_block *block)
247 lzma_next_strm_init(lzma_block_decoder_init, strm, block);
249 strm->internal->supported_actions[LZMA_RUN] = true;
250 strm->internal->supported_actions[LZMA_FINISH] = true;