1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file stream_decoder.c
4 /// \brief Decodes .lzma Streams
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 "stream_decoder.h"
21 #include "block_decoder.h"
34 /// Block or Metadata decoder. This takes little memory and the same
35 /// data structure can be used to decode every Block Header, so it's
36 /// a good idea to have a separate lzma_next_coder structure for it.
37 lzma_next_coder block_decoder;
39 /// Block options decoded by the Block Header decoder and used by
40 /// the Block decoder.
41 lzma_block block_options;
43 /// Stream Flags from Stream Header
44 lzma_stream_flags stream_flags;
46 /// Index is hashed so that it can be compared to the sizes of Blocks
47 /// with O(1) memory usage.
48 lzma_index_hash *index_hash;
50 /// Memory usage limit
53 /// If true, LZMA_NO_CHECK is returned if the Stream has
54 /// no integrity check.
57 /// If true, LZMA_UNSUPPORTED_CHECK is returned if the Stream has
58 /// an integrity check that isn't supported by this liblzma build.
59 bool tell_unsupported_check;
61 /// If true, LZMA_GET_CHECK is returned after decoding Stream Header.
64 /// If true, we will decode concatenated Streams that possibly have
65 /// Stream Padding between or after them. LZMA_STREAM_END is returned
66 /// once the application isn't giving us any new input, and we aren't
67 /// in the middle of a Stream, and possible Stream Padding is a
68 /// multiple of four bytes.
71 /// When decoding concatenated Streams, this is true as long as we
72 /// are decoding the first Stream. This is needed to avoid misleading
73 /// LZMA_FORMAT_ERROR in case the later Streams don't have valid magic
77 /// Write position in buffer[] and position in Stream Padding
80 /// Buffer to hold Stream Header, Block Header, and Stream Footer.
81 /// Block Header has biggest maximum size.
82 uint8_t buffer[LZMA_BLOCK_HEADER_SIZE_MAX];
87 stream_decoder_reset(lzma_coder *coder, lzma_allocator *allocator)
89 // Initialize the Index hash used to verify the Index.
90 coder->index_hash = lzma_index_hash_init(coder->index_hash, allocator);
91 if (coder->index_hash == NULL)
92 return LZMA_MEM_ERROR;
94 // Reset the rest of the variables.
95 coder->sequence = SEQ_STREAM_HEADER;
96 coder->block_options.filters = NULL;
104 stream_decode(lzma_coder *coder, lzma_allocator *allocator,
105 const uint8_t *restrict in, size_t *restrict in_pos,
106 size_t in_size, uint8_t *restrict out,
107 size_t *restrict out_pos, size_t out_size, lzma_action action)
109 // When decoding the actual Block, it may be able to produce more
110 // output even if we don't give it any new input.
112 switch (coder->sequence) {
113 case SEQ_STREAM_HEADER: {
114 // Copy the Stream Header to the internal buffer.
115 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
116 LZMA_STREAM_HEADER_SIZE);
118 // Return if we didn't get the whole Stream Header yet.
119 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
124 // Decode the Stream Header.
125 const lzma_ret ret = lzma_stream_header_decode(
126 &coder->stream_flags, coder->buffer);
128 return ret == LZMA_FORMAT_ERROR && !coder->first_stream
129 ? LZMA_DATA_ERROR : ret;
131 // Copy the type of the Check so that Block Header and Block
133 coder->block_options.check = coder->stream_flags.check;
135 // Even if we return LZMA_*_CHECK below, we want
136 // to continue from Block Header decoding.
137 coder->sequence = SEQ_BLOCK_HEADER;
139 // Detect if there's no integrity check or if it is
140 // unsupported if those were requested by the application.
141 if (coder->tell_no_check && coder->stream_flags.check
143 return LZMA_NO_CHECK;
145 if (coder->tell_unsupported_check
146 && !lzma_check_is_supported(
147 coder->stream_flags.check))
148 return LZMA_UNSUPPORTED_CHECK;
150 if (coder->tell_any_check)
151 return LZMA_GET_CHECK;
156 case SEQ_BLOCK_HEADER: {
157 if (*in_pos >= in_size)
160 if (coder->pos == 0) {
161 // Detect if it's Index.
162 if (in[*in_pos] == 0x00) {
163 coder->sequence = SEQ_INDEX;
167 // Calculate the size of the Block Header. Note that
168 // Block Header decoder wants to see this byte too
169 // so don't advance *in_pos.
170 coder->block_options.header_size
171 = lzma_block_header_size_decode(
175 // Copy the Block Header to the internal buffer.
176 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
177 coder->block_options.header_size);
179 // Return if we didn't get the whole Block Header yet.
180 if (coder->pos < coder->block_options.header_size)
185 // Set up a buffer to hold the filter chain. Block Header
186 // decoder will initialize all members of this array so
187 // we don't need to do it here.
188 lzma_filter filters[LZMA_BLOCK_FILTERS_MAX + 1];
189 coder->block_options.filters = filters;
191 // Decode the Block Header.
192 return_if_error(lzma_block_header_decode(&coder->block_options,
193 allocator, coder->buffer));
195 // Check the memory usage limit.
196 const uint64_t memusage = lzma_memusage_decoder(filters);
199 if (memusage == UINT64_MAX) {
200 // One or more unknown Filter IDs.
201 ret = LZMA_OPTIONS_ERROR;
202 } else if (memusage > coder->memlimit) {
203 // The chain would need too much memory.
204 ret = LZMA_MEMLIMIT_ERROR;
206 // Memory usage is OK. Initialize the Block decoder.
207 ret = lzma_block_decoder_init(
208 &coder->block_decoder,
209 allocator, &coder->block_options);
212 // Free the allocated filter options since they are needed
213 // only to initialize the Block decoder.
214 for (size_t i = 0; i < LZMA_BLOCK_FILTERS_MAX; ++i)
215 lzma_free(filters[i].options, allocator);
217 coder->block_options.filters = NULL;
219 // Check if memory usage calculation and Block enocoder
220 // initialization succeeded.
224 coder->sequence = SEQ_BLOCK;
230 const lzma_ret ret = coder->block_decoder.code(
231 coder->block_decoder.coder, allocator,
232 in, in_pos, in_size, out, out_pos, out_size,
235 if (ret != LZMA_STREAM_END)
238 // Block decoded successfully. Add the new size pair to
240 return_if_error(lzma_index_hash_append(coder->index_hash,
241 lzma_block_total_size_get(
242 &coder->block_options),
243 coder->block_options.uncompressed_size));
245 coder->sequence = SEQ_BLOCK_HEADER;
250 // If we don't have any input, don't call
251 // lzma_index_hash_decode() since it would return
252 // LZMA_BUF_ERROR, which we must not do here.
253 if (*in_pos >= in_size)
256 // Decode the Index and compare it to the hash calculated
257 // from the sizes of the Blocks (if any).
258 const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
259 in, in_pos, in_size);
260 if (ret != LZMA_STREAM_END)
263 coder->sequence = SEQ_STREAM_FOOTER;
268 case SEQ_STREAM_FOOTER:
269 // Copy the Stream Footer to the internal buffer.
270 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
271 LZMA_STREAM_HEADER_SIZE);
273 // Return if we didn't get the whole Stream Footer yet.
274 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
279 // Decode the Stream Footer. The decoder gives
280 // LZMA_FORMAT_ERROR if the magic bytes don't match,
281 // so convert that return code to LZMA_DATA_ERROR.
282 lzma_stream_flags footer_flags;
283 const lzma_ret ret = lzma_stream_footer_decode(
284 &footer_flags, coder->buffer);
286 return ret == LZMA_FORMAT_ERROR
287 ? LZMA_DATA_ERROR : ret;
289 // Check that Index Size stored in the Stream Footer matches
290 // the real size of the Index field.
291 if (lzma_index_hash_size(coder->index_hash)
292 != footer_flags.backward_size)
293 return LZMA_DATA_ERROR;
295 // Compare that the Stream Flags fields are identical in
296 // both Stream Header and Stream Footer.
297 return_if_error(lzma_stream_flags_compare(
298 &coder->stream_flags, &footer_flags));
300 if (!coder->concatenated)
301 return LZMA_STREAM_END;
303 coder->sequence = SEQ_STREAM_PADDING;
307 case SEQ_STREAM_PADDING:
308 assert(coder->concatenated);
310 // Skip over possible Stream Padding.
312 if (*in_pos >= in_size) {
313 // Unless LZMA_FINISH was used, we cannot
314 // know if there's more input coming later.
315 if (action != LZMA_FINISH)
318 // Stream Padding must be a multiple of
320 return coder->pos == 0
325 // If the byte is not zero, it probably indicates
326 // beginning of a new Stream (or the file is corrupt).
327 if (in[*in_pos] != 0x00)
331 coder->pos = (coder->pos + 1) & 3;
334 // Stream Padding must be a multiple of four bytes (empty
335 // Stream Padding is OK).
336 if (coder->pos != 0) {
338 return LZMA_DATA_ERROR;
341 // Prepare to decode the next Stream.
342 return_if_error(stream_decoder_reset(coder, allocator));
347 return LZMA_PROG_ERROR;
355 stream_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
357 lzma_next_end(&coder->block_decoder, allocator);
358 lzma_index_hash_end(coder->index_hash, allocator);
359 lzma_free(coder, allocator);
365 stream_decoder_get_check(const lzma_coder *coder)
367 return coder->stream_flags.check;
372 lzma_stream_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
373 uint64_t memlimit, uint32_t flags)
375 lzma_next_coder_init(lzma_stream_decoder_init, next, allocator);
377 if (flags & ~LZMA_SUPPORTED_FLAGS)
378 return LZMA_OPTIONS_ERROR;
380 if (next->coder == NULL) {
381 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
382 if (next->coder == NULL)
383 return LZMA_MEM_ERROR;
385 next->code = &stream_decode;
386 next->end = &stream_decoder_end;
387 next->get_check = &stream_decoder_get_check;
389 next->coder->block_decoder = LZMA_NEXT_CODER_INIT;
390 next->coder->index_hash = NULL;
393 next->coder->memlimit = memlimit;
394 next->coder->tell_no_check = (flags & LZMA_TELL_NO_CHECK) != 0;
395 next->coder->tell_unsupported_check
396 = (flags & LZMA_TELL_UNSUPPORTED_CHECK) != 0;
397 next->coder->tell_any_check = (flags & LZMA_TELL_ANY_CHECK) != 0;
398 next->coder->concatenated = (flags & LZMA_CONCATENATED) != 0;
400 return stream_decoder_reset(next->coder, allocator);
404 extern LZMA_API lzma_ret
405 lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
407 lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags);
409 strm->internal->supported_actions[LZMA_RUN] = true;
410 strm->internal->supported_actions[LZMA_FINISH] = true;