1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file block_decoder.c
4 /// \brief Decodes .lzma 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 encoding has been finished.
39 /// Compressed Size calculated while encoding
40 lzma_vli compressed_size;
42 /// Uncompressed Size calculated while encoding
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->options->uncompressed_size))
105 return LZMA_DATA_ERROR;
107 lzma_check_update(&coder->check, coder->options->check,
108 out + out_start, out_used);
110 if (ret != LZMA_STREAM_END)
113 coder->sequence = SEQ_PADDING;
119 // Compressed Data is padded to a multiple of four bytes.
120 while (coder->compressed_size & 3) {
121 if (*in_pos >= in_size)
124 if (in[(*in_pos)++] != 0x00)
125 return LZMA_DATA_ERROR;
127 if (update_size(&coder->compressed_size, 1,
128 coder->compressed_limit))
129 return LZMA_DATA_ERROR;
132 // Compressed and Uncompressed Sizes are now at their final
133 // values. Verify that they match the values given to us.
134 if (!is_size_valid(coder->compressed_size,
135 coder->options->compressed_size)
136 || !is_size_valid(coder->uncompressed_size,
137 coder->options->uncompressed_size))
138 return LZMA_DATA_ERROR;
140 // Copy the values into coder->options. The caller
141 // may use this information to construct Index.
142 coder->options->compressed_size = coder->compressed_size;
143 coder->options->uncompressed_size = coder->uncompressed_size;
145 if (coder->options->check == LZMA_CHECK_NONE)
146 return LZMA_STREAM_END;
148 lzma_check_finish(&coder->check, coder->options->check);
149 coder->sequence = SEQ_CHECK;
154 const bool chksup = lzma_check_is_supported(
155 coder->options->check);
157 while (*in_pos < in_size) {
158 // coder->check.buffer[] may be uninitialized when
159 // the Check ID is not supported.
160 if (chksup && coder->check.buffer.u8[coder->check_pos]
163 return LZMA_DATA_ERROR;
168 if (++coder->check_pos == lzma_check_size(
169 coder->options->check))
170 return LZMA_STREAM_END;
177 return LZMA_PROG_ERROR;
182 block_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
184 lzma_next_end(&coder->next, allocator);
185 lzma_free(coder, allocator);
191 lzma_block_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
194 lzma_next_coder_init(lzma_block_decoder_init, next, allocator);
196 // While lzma_block_total_size_get() is meant to calculate the Total
197 // Size, it also validates the options excluding the filters.
198 if (lzma_block_total_size_get(options) == 0)
199 return LZMA_PROG_ERROR;
201 // options->check is used for array indexing so we need to know that
202 // it is in the valid range.
203 if ((unsigned)(options->check) > LZMA_CHECK_ID_MAX)
204 return LZMA_PROG_ERROR;
206 // Allocate and initialize *next->coder if needed.
207 if (next->coder == NULL) {
208 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
209 if (next->coder == NULL)
210 return LZMA_MEM_ERROR;
212 next->code = &block_decode;
213 next->end = &block_decoder_end;
214 next->coder->next = LZMA_NEXT_CODER_INIT;
217 // Basic initializations
218 next->coder->sequence = SEQ_CODE;
219 next->coder->options = options;
220 next->coder->compressed_size = 0;
221 next->coder->uncompressed_size = 0;
223 // If Compressed Size is not known, we calculate the maximum allowed
224 // value so that Total Size of the Block still is a valid VLI and
225 // a multiple of four.
226 next->coder->compressed_limit
227 = options->compressed_size == LZMA_VLI_UNKNOWN
228 ? (LZMA_VLI_MAX & ~LZMA_VLI_C(3))
229 - options->header_size
230 - lzma_check_size(options->check)
231 : options->compressed_size;
233 // Initialize the check. It's caller's problem if the Check ID is not
234 // supported, and the Block decoder cannot verify the Check field.
235 // Caller can test lzma_checks[options->check].
236 next->coder->check_pos = 0;
237 lzma_check_init(&next->coder->check, options->check);
239 // Initialize the filter chain.
240 return lzma_raw_decoder_init(&next->coder->next, allocator,
245 extern LZMA_API lzma_ret
246 lzma_block_decoder(lzma_stream *strm, lzma_block *options)
248 lzma_next_strm_init(lzma_block_decoder_init, strm, options);
250 strm->internal->supported_actions[LZMA_RUN] = true;
251 strm->internal->supported_actions[LZMA_FINISH] = true;