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 // If we are decoding concatenated Streams, and the later
132 // Streams have invalid Header Magic Bytes, we give
133 // LZMA_DATA_ERROR instead of LZMA_FORMAT_ERROR.
134 coder->first_stream = false;
136 // Copy the type of the Check so that Block Header and Block
138 coder->block_options.check = coder->stream_flags.check;
140 // Even if we return LZMA_*_CHECK below, we want
141 // to continue from Block Header decoding.
142 coder->sequence = SEQ_BLOCK_HEADER;
144 // Detect if there's no integrity check or if it is
145 // unsupported if those were requested by the application.
146 if (coder->tell_no_check && coder->stream_flags.check
148 return LZMA_NO_CHECK;
150 if (coder->tell_unsupported_check
151 && !lzma_check_is_supported(
152 coder->stream_flags.check))
153 return LZMA_UNSUPPORTED_CHECK;
155 if (coder->tell_any_check)
156 return LZMA_GET_CHECK;
161 case SEQ_BLOCK_HEADER: {
162 if (*in_pos >= in_size)
165 if (coder->pos == 0) {
166 // Detect if it's Index.
167 if (in[*in_pos] == 0x00) {
168 coder->sequence = SEQ_INDEX;
172 // Calculate the size of the Block Header. Note that
173 // Block Header decoder wants to see this byte too
174 // so don't advance *in_pos.
175 coder->block_options.header_size
176 = lzma_block_header_size_decode(
180 // Copy the Block Header to the internal buffer.
181 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
182 coder->block_options.header_size);
184 // Return if we didn't get the whole Block Header yet.
185 if (coder->pos < coder->block_options.header_size)
190 // Set up a buffer to hold the filter chain. Block Header
191 // decoder will initialize all members of this array so
192 // we don't need to do it here.
193 lzma_filter filters[LZMA_FILTERS_MAX + 1];
194 coder->block_options.filters = filters;
196 // Decode the Block Header.
197 return_if_error(lzma_block_header_decode(&coder->block_options,
198 allocator, coder->buffer));
200 // Check the memory usage limit.
201 const uint64_t memusage = lzma_memusage_decoder(filters);
204 if (memusage == UINT64_MAX) {
205 // One or more unknown Filter IDs.
206 ret = LZMA_OPTIONS_ERROR;
207 } else if (memusage > coder->memlimit) {
208 // The chain would need too much memory.
209 ret = LZMA_MEMLIMIT_ERROR;
211 // Memory usage is OK. Initialize the Block decoder.
212 ret = lzma_block_decoder_init(
213 &coder->block_decoder,
214 allocator, &coder->block_options);
217 // Free the allocated filter options since they are needed
218 // only to initialize the Block decoder.
219 for (size_t i = 0; i < LZMA_FILTERS_MAX; ++i)
220 lzma_free(filters[i].options, allocator);
222 coder->block_options.filters = NULL;
224 // Check if memory usage calculation and Block enocoder
225 // initialization succeeded.
229 coder->sequence = SEQ_BLOCK;
235 const lzma_ret ret = coder->block_decoder.code(
236 coder->block_decoder.coder, allocator,
237 in, in_pos, in_size, out, out_pos, out_size,
240 if (ret != LZMA_STREAM_END)
243 // Block decoded successfully. Add the new size pair to
245 return_if_error(lzma_index_hash_append(coder->index_hash,
246 lzma_block_unpadded_size(
247 &coder->block_options),
248 coder->block_options.uncompressed_size));
250 coder->sequence = SEQ_BLOCK_HEADER;
255 // If we don't have any input, don't call
256 // lzma_index_hash_decode() since it would return
257 // LZMA_BUF_ERROR, which we must not do here.
258 if (*in_pos >= in_size)
261 // Decode the Index and compare it to the hash calculated
262 // from the sizes of the Blocks (if any).
263 const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
264 in, in_pos, in_size);
265 if (ret != LZMA_STREAM_END)
268 coder->sequence = SEQ_STREAM_FOOTER;
273 case SEQ_STREAM_FOOTER: {
274 // Copy the Stream Footer to the internal buffer.
275 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
276 LZMA_STREAM_HEADER_SIZE);
278 // Return if we didn't get the whole Stream Footer yet.
279 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
284 // Decode the Stream Footer. The decoder gives
285 // LZMA_FORMAT_ERROR if the magic bytes don't match,
286 // so convert that return code to LZMA_DATA_ERROR.
287 lzma_stream_flags footer_flags;
288 const lzma_ret ret = lzma_stream_footer_decode(
289 &footer_flags, coder->buffer);
291 return ret == LZMA_FORMAT_ERROR
292 ? LZMA_DATA_ERROR : ret;
294 // Check that Index Size stored in the Stream Footer matches
295 // the real size of the Index field.
296 if (lzma_index_hash_size(coder->index_hash)
297 != footer_flags.backward_size)
298 return LZMA_DATA_ERROR;
300 // Compare that the Stream Flags fields are identical in
301 // both Stream Header and Stream Footer.
302 return_if_error(lzma_stream_flags_compare(
303 &coder->stream_flags, &footer_flags));
305 if (!coder->concatenated)
306 return LZMA_STREAM_END;
308 coder->sequence = SEQ_STREAM_PADDING;
313 case SEQ_STREAM_PADDING:
314 assert(coder->concatenated);
316 // Skip over possible Stream Padding.
318 if (*in_pos >= in_size) {
319 // Unless LZMA_FINISH was used, we cannot
320 // know if there's more input coming later.
321 if (action != LZMA_FINISH)
324 // Stream Padding must be a multiple of
326 return coder->pos == 0
331 // If the byte is not zero, it probably indicates
332 // beginning of a new Stream (or the file is corrupt).
333 if (in[*in_pos] != 0x00)
337 coder->pos = (coder->pos + 1) & 3;
340 // Stream Padding must be a multiple of four bytes (empty
341 // Stream Padding is OK).
342 if (coder->pos != 0) {
344 return LZMA_DATA_ERROR;
347 // Prepare to decode the next Stream.
348 return_if_error(stream_decoder_reset(coder, allocator));
353 return LZMA_PROG_ERROR;
361 stream_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
363 lzma_next_end(&coder->block_decoder, allocator);
364 lzma_index_hash_end(coder->index_hash, allocator);
365 lzma_free(coder, allocator);
371 stream_decoder_get_check(const lzma_coder *coder)
373 return coder->stream_flags.check;
378 lzma_stream_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
379 uint64_t memlimit, uint32_t flags)
381 lzma_next_coder_init(lzma_stream_decoder_init, next, allocator);
383 if (flags & ~LZMA_SUPPORTED_FLAGS)
384 return LZMA_OPTIONS_ERROR;
386 if (next->coder == NULL) {
387 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
388 if (next->coder == NULL)
389 return LZMA_MEM_ERROR;
391 next->code = &stream_decode;
392 next->end = &stream_decoder_end;
393 next->get_check = &stream_decoder_get_check;
395 next->coder->block_decoder = LZMA_NEXT_CODER_INIT;
396 next->coder->index_hash = NULL;
399 next->coder->memlimit = memlimit;
400 next->coder->tell_no_check = (flags & LZMA_TELL_NO_CHECK) != 0;
401 next->coder->tell_unsupported_check
402 = (flags & LZMA_TELL_UNSUPPORTED_CHECK) != 0;
403 next->coder->tell_any_check = (flags & LZMA_TELL_ANY_CHECK) != 0;
404 next->coder->concatenated = (flags & LZMA_CONCATENATED) != 0;
405 next->coder->first_stream = true;
407 return stream_decoder_reset(next->coder, allocator);
411 extern LZMA_API lzma_ret
412 lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
414 lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags);
416 strm->internal->supported_actions[LZMA_RUN] = true;
417 strm->internal->supported_actions[LZMA_FINISH] = true;