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 /// Amount of memory actually needed (only an estimate)
56 /// If true, LZMA_NO_CHECK is returned if the Stream has
57 /// no integrity check.
60 /// If true, LZMA_UNSUPPORTED_CHECK is returned if the Stream has
61 /// an integrity check that isn't supported by this liblzma build.
62 bool tell_unsupported_check;
64 /// If true, LZMA_GET_CHECK is returned after decoding Stream Header.
67 /// If true, we will decode concatenated Streams that possibly have
68 /// Stream Padding between or after them. LZMA_STREAM_END is returned
69 /// once the application isn't giving us any new input, and we aren't
70 /// in the middle of a Stream, and possible Stream Padding is a
71 /// multiple of four bytes.
74 /// When decoding concatenated Streams, this is true as long as we
75 /// are decoding the first Stream. This is needed to avoid misleading
76 /// LZMA_FORMAT_ERROR in case the later Streams don't have valid magic
80 /// Write position in buffer[] and position in Stream Padding
83 /// Buffer to hold Stream Header, Block Header, and Stream Footer.
84 /// Block Header has biggest maximum size.
85 uint8_t buffer[LZMA_BLOCK_HEADER_SIZE_MAX];
90 stream_decoder_reset(lzma_coder *coder, lzma_allocator *allocator)
92 // Initialize the Index hash used to verify the Index.
93 coder->index_hash = lzma_index_hash_init(coder->index_hash, allocator);
94 if (coder->index_hash == NULL)
95 return LZMA_MEM_ERROR;
97 // Reset the rest of the variables.
98 coder->sequence = SEQ_STREAM_HEADER;
99 coder->block_options.filters = NULL;
107 stream_decode(lzma_coder *coder, lzma_allocator *allocator,
108 const uint8_t *restrict in, size_t *restrict in_pos,
109 size_t in_size, uint8_t *restrict out,
110 size_t *restrict out_pos, size_t out_size, lzma_action action)
112 // When decoding the actual Block, it may be able to produce more
113 // output even if we don't give it any new input.
115 switch (coder->sequence) {
116 case SEQ_STREAM_HEADER: {
117 // Copy the Stream Header to the internal buffer.
118 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
119 LZMA_STREAM_HEADER_SIZE);
121 // Return if we didn't get the whole Stream Header yet.
122 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
127 // Decode the Stream Header.
128 const lzma_ret ret = lzma_stream_header_decode(
129 &coder->stream_flags, coder->buffer);
131 return ret == LZMA_FORMAT_ERROR && !coder->first_stream
132 ? LZMA_DATA_ERROR : ret;
134 // If we are decoding concatenated Streams, and the later
135 // Streams have invalid Header Magic Bytes, we give
136 // LZMA_DATA_ERROR instead of LZMA_FORMAT_ERROR.
137 coder->first_stream = false;
139 // Copy the type of the Check so that Block Header and Block
141 coder->block_options.check = coder->stream_flags.check;
143 // Even if we return LZMA_*_CHECK below, we want
144 // to continue from Block Header decoding.
145 coder->sequence = SEQ_BLOCK_HEADER;
147 // Detect if there's no integrity check or if it is
148 // unsupported if those were requested by the application.
149 if (coder->tell_no_check && coder->stream_flags.check
151 return LZMA_NO_CHECK;
153 if (coder->tell_unsupported_check
154 && !lzma_check_is_supported(
155 coder->stream_flags.check))
156 return LZMA_UNSUPPORTED_CHECK;
158 if (coder->tell_any_check)
159 return LZMA_GET_CHECK;
164 case SEQ_BLOCK_HEADER: {
165 if (*in_pos >= in_size)
168 if (coder->pos == 0) {
169 // Detect if it's Index.
170 if (in[*in_pos] == 0x00) {
171 coder->sequence = SEQ_INDEX;
175 // Calculate the size of the Block Header. Note that
176 // Block Header decoder wants to see this byte too
177 // so don't advance *in_pos.
178 coder->block_options.header_size
179 = lzma_block_header_size_decode(
183 // Copy the Block Header to the internal buffer.
184 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
185 coder->block_options.header_size);
187 // Return if we didn't get the whole Block Header yet.
188 if (coder->pos < coder->block_options.header_size)
193 // Set up a buffer to hold the filter chain. Block Header
194 // decoder will initialize all members of this array so
195 // we don't need to do it here.
196 lzma_filter filters[LZMA_FILTERS_MAX + 1];
197 coder->block_options.filters = filters;
199 // Decode the Block Header.
200 return_if_error(lzma_block_header_decode(&coder->block_options,
201 allocator, coder->buffer));
203 // Check the memory usage limit.
204 const uint64_t memusage = lzma_memusage_decoder(filters);
207 if (memusage == UINT64_MAX) {
208 // One or more unknown Filter IDs.
209 ret = LZMA_OPTIONS_ERROR;
211 // Now we can set coder->memusage since we know that
212 // the filter chain is valid. We don't want
213 // lzma_memusage() to return UINT64_MAX in case of
214 // invalid filter chain.
215 coder->memusage = memusage;
217 if (memusage > coder->memlimit) {
218 // The chain would need too much memory.
219 ret = LZMA_MEMLIMIT_ERROR;
221 // Memory usage is OK.
222 // Initialize the Block decoder.
223 ret = lzma_block_decoder_init(
224 &coder->block_decoder,
226 &coder->block_options);
230 // Free the allocated filter options since they are needed
231 // only to initialize the Block decoder.
232 for (size_t i = 0; i < LZMA_FILTERS_MAX; ++i)
233 lzma_free(filters[i].options, allocator);
235 coder->block_options.filters = NULL;
237 // Check if memory usage calculation and Block enocoder
238 // initialization succeeded.
242 coder->sequence = SEQ_BLOCK;
248 const lzma_ret ret = coder->block_decoder.code(
249 coder->block_decoder.coder, allocator,
250 in, in_pos, in_size, out, out_pos, out_size,
253 if (ret != LZMA_STREAM_END)
256 // Block decoded successfully. Add the new size pair to
258 return_if_error(lzma_index_hash_append(coder->index_hash,
259 lzma_block_unpadded_size(
260 &coder->block_options),
261 coder->block_options.uncompressed_size));
263 coder->sequence = SEQ_BLOCK_HEADER;
268 // If we don't have any input, don't call
269 // lzma_index_hash_decode() since it would return
270 // LZMA_BUF_ERROR, which we must not do here.
271 if (*in_pos >= in_size)
274 // Decode the Index and compare it to the hash calculated
275 // from the sizes of the Blocks (if any).
276 const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
277 in, in_pos, in_size);
278 if (ret != LZMA_STREAM_END)
281 coder->sequence = SEQ_STREAM_FOOTER;
286 case SEQ_STREAM_FOOTER: {
287 // Copy the Stream Footer to the internal buffer.
288 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
289 LZMA_STREAM_HEADER_SIZE);
291 // Return if we didn't get the whole Stream Footer yet.
292 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
297 // Decode the Stream Footer. The decoder gives
298 // LZMA_FORMAT_ERROR if the magic bytes don't match,
299 // so convert that return code to LZMA_DATA_ERROR.
300 lzma_stream_flags footer_flags;
301 const lzma_ret ret = lzma_stream_footer_decode(
302 &footer_flags, coder->buffer);
304 return ret == LZMA_FORMAT_ERROR
305 ? LZMA_DATA_ERROR : ret;
307 // Check that Index Size stored in the Stream Footer matches
308 // the real size of the Index field.
309 if (lzma_index_hash_size(coder->index_hash)
310 != footer_flags.backward_size)
311 return LZMA_DATA_ERROR;
313 // Compare that the Stream Flags fields are identical in
314 // both Stream Header and Stream Footer.
315 return_if_error(lzma_stream_flags_compare(
316 &coder->stream_flags, &footer_flags));
318 if (!coder->concatenated)
319 return LZMA_STREAM_END;
321 coder->sequence = SEQ_STREAM_PADDING;
326 case SEQ_STREAM_PADDING:
327 assert(coder->concatenated);
329 // Skip over possible Stream Padding.
331 if (*in_pos >= in_size) {
332 // Unless LZMA_FINISH was used, we cannot
333 // know if there's more input coming later.
334 if (action != LZMA_FINISH)
337 // Stream Padding must be a multiple of
339 return coder->pos == 0
344 // If the byte is not zero, it probably indicates
345 // beginning of a new Stream (or the file is corrupt).
346 if (in[*in_pos] != 0x00)
350 coder->pos = (coder->pos + 1) & 3;
353 // Stream Padding must be a multiple of four bytes (empty
354 // Stream Padding is OK).
355 if (coder->pos != 0) {
357 return LZMA_DATA_ERROR;
360 // Prepare to decode the next Stream.
361 return_if_error(stream_decoder_reset(coder, allocator));
366 return LZMA_PROG_ERROR;
374 stream_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
376 lzma_next_end(&coder->block_decoder, allocator);
377 lzma_index_hash_end(coder->index_hash, allocator);
378 lzma_free(coder, allocator);
384 stream_decoder_get_check(const lzma_coder *coder)
386 return coder->stream_flags.check;
391 stream_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
392 uint64_t *old_memlimit, uint64_t new_memlimit)
394 if (new_memlimit != 0 && new_memlimit < coder->memusage)
395 return LZMA_MEMLIMIT_ERROR;
397 *memusage = coder->memusage;
398 *old_memlimit = coder->memlimit;
399 coder->memlimit = new_memlimit;
406 lzma_stream_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
407 uint64_t memlimit, uint32_t flags)
409 lzma_next_coder_init(lzma_stream_decoder_init, next, allocator);
412 return LZMA_PROG_ERROR;
414 if (flags & ~LZMA_SUPPORTED_FLAGS)
415 return LZMA_OPTIONS_ERROR;
417 if (next->coder == NULL) {
418 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
419 if (next->coder == NULL)
420 return LZMA_MEM_ERROR;
422 next->code = &stream_decode;
423 next->end = &stream_decoder_end;
424 next->get_check = &stream_decoder_get_check;
425 next->memconfig = &stream_decoder_memconfig;
427 next->coder->block_decoder = LZMA_NEXT_CODER_INIT;
428 next->coder->index_hash = NULL;
431 next->coder->memlimit = memlimit;
432 next->coder->memusage = LZMA_MEMUSAGE_BASE;
433 next->coder->tell_no_check = (flags & LZMA_TELL_NO_CHECK) != 0;
434 next->coder->tell_unsupported_check
435 = (flags & LZMA_TELL_UNSUPPORTED_CHECK) != 0;
436 next->coder->tell_any_check = (flags & LZMA_TELL_ANY_CHECK) != 0;
437 next->coder->concatenated = (flags & LZMA_CONCATENATED) != 0;
438 next->coder->first_stream = true;
440 return stream_decoder_reset(next->coder, allocator);
444 extern LZMA_API lzma_ret
445 lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
447 lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags);
449 strm->internal->supported_actions[LZMA_RUN] = true;
450 strm->internal->supported_actions[LZMA_FINISH] = true;