1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file stream_decoder.c
4 /// \brief Decodes .xz Streams
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "stream_decoder.h"
14 #include "block_decoder.h"
27 /// Block or Metadata decoder. This takes little memory and the same
28 /// data structure can be used to decode every Block Header, so it's
29 /// a good idea to have a separate lzma_next_coder structure for it.
30 lzma_next_coder block_decoder;
32 /// Block options decoded by the Block Header decoder and used by
33 /// the Block decoder.
34 lzma_block block_options;
36 /// Stream Flags from Stream Header
37 lzma_stream_flags stream_flags;
39 /// Index is hashed so that it can be compared to the sizes of Blocks
40 /// with O(1) memory usage.
41 lzma_index_hash *index_hash;
43 /// Memory usage limit
46 /// Amount of memory actually needed (only an estimate)
49 /// If true, LZMA_NO_CHECK is returned if the Stream has
50 /// no integrity check.
53 /// If true, LZMA_UNSUPPORTED_CHECK is returned if the Stream has
54 /// an integrity check that isn't supported by this liblzma build.
55 bool tell_unsupported_check;
57 /// If true, LZMA_GET_CHECK is returned after decoding Stream Header.
60 /// If true, we will decode concatenated Streams that possibly have
61 /// Stream Padding between or after them. LZMA_STREAM_END is returned
62 /// once the application isn't giving us any new input, and we aren't
63 /// in the middle of a Stream, and possible Stream Padding is a
64 /// multiple of four bytes.
67 /// When decoding concatenated Streams, this is true as long as we
68 /// are decoding the first Stream. This is needed to avoid misleading
69 /// LZMA_FORMAT_ERROR in case the later Streams don't have valid magic
73 /// Write position in buffer[] and position in Stream Padding
76 /// Buffer to hold Stream Header, Block Header, and Stream Footer.
77 /// Block Header has biggest maximum size.
78 uint8_t buffer[LZMA_BLOCK_HEADER_SIZE_MAX];
83 stream_decoder_reset(lzma_coder *coder, lzma_allocator *allocator)
85 // Initialize the Index hash used to verify the Index.
86 coder->index_hash = lzma_index_hash_init(coder->index_hash, allocator);
87 if (coder->index_hash == NULL)
88 return LZMA_MEM_ERROR;
90 // Reset the rest of the variables.
91 coder->sequence = SEQ_STREAM_HEADER;
99 stream_decode(lzma_coder *coder, lzma_allocator *allocator,
100 const uint8_t *restrict in, size_t *restrict in_pos,
101 size_t in_size, uint8_t *restrict out,
102 size_t *restrict out_pos, size_t out_size, lzma_action action)
104 // When decoding the actual Block, it may be able to produce more
105 // output even if we don't give it any new input.
107 switch (coder->sequence) {
108 case SEQ_STREAM_HEADER: {
109 // Copy the Stream Header to the internal buffer.
110 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
111 LZMA_STREAM_HEADER_SIZE);
113 // Return if we didn't get the whole Stream Header yet.
114 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
119 // Decode the Stream Header.
120 const lzma_ret ret = lzma_stream_header_decode(
121 &coder->stream_flags, coder->buffer);
123 return ret == LZMA_FORMAT_ERROR && !coder->first_stream
124 ? LZMA_DATA_ERROR : ret;
126 // If we are decoding concatenated Streams, and the later
127 // Streams have invalid Header Magic Bytes, we give
128 // LZMA_DATA_ERROR instead of LZMA_FORMAT_ERROR.
129 coder->first_stream = false;
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 // Version 0 is currently the only possible version.
186 coder->block_options.version = 0;
188 // Set up a buffer to hold the filter chain. Block Header
189 // decoder will initialize all members of this array so
190 // we don't need to do it here.
191 lzma_filter filters[LZMA_FILTERS_MAX + 1];
192 coder->block_options.filters = filters;
194 // Decode the Block Header.
195 return_if_error(lzma_block_header_decode(&coder->block_options,
196 allocator, coder->buffer));
198 // Check the memory usage limit.
199 const uint64_t memusage = lzma_raw_decoder_memusage(filters);
202 if (memusage == UINT64_MAX) {
203 // One or more unknown Filter IDs.
204 ret = LZMA_OPTIONS_ERROR;
206 // Now we can set coder->memusage since we know that
207 // the filter chain is valid. We don't want
208 // lzma_memusage() to return UINT64_MAX in case of
209 // invalid filter chain.
210 coder->memusage = memusage;
212 if (memusage > coder->memlimit) {
213 // The chain would need too much memory.
214 ret = LZMA_MEMLIMIT_ERROR;
216 // Memory usage is OK.
217 // Initialize the Block decoder.
218 ret = lzma_block_decoder_init(
219 &coder->block_decoder,
221 &coder->block_options);
225 // Free the allocated filter options since they are needed
226 // only to initialize the Block decoder.
228 for (i = 0; i < LZMA_FILTERS_MAX; ++i)
229 lzma_free(filters[i].options, allocator);
231 coder->block_options.filters = NULL;
233 // Check if memory usage calculation and Block enocoder
234 // initialization succeeded.
238 coder->sequence = SEQ_BLOCK;
244 const lzma_ret ret = coder->block_decoder.code(
245 coder->block_decoder.coder, allocator,
246 in, in_pos, in_size, out, out_pos, out_size,
249 if (ret != LZMA_STREAM_END)
252 // Block decoded successfully. Add the new size pair to
254 return_if_error(lzma_index_hash_append(coder->index_hash,
255 lzma_block_unpadded_size(
256 &coder->block_options),
257 coder->block_options.uncompressed_size));
259 coder->sequence = SEQ_BLOCK_HEADER;
264 // If we don't have any input, don't call
265 // lzma_index_hash_decode() since it would return
266 // LZMA_BUF_ERROR, which we must not do here.
267 if (*in_pos >= in_size)
270 // Decode the Index and compare it to the hash calculated
271 // from the sizes of the Blocks (if any).
272 const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
273 in, in_pos, in_size);
274 if (ret != LZMA_STREAM_END)
277 coder->sequence = SEQ_STREAM_FOOTER;
282 case SEQ_STREAM_FOOTER: {
283 // Copy the Stream Footer to the internal buffer.
284 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
285 LZMA_STREAM_HEADER_SIZE);
287 // Return if we didn't get the whole Stream Footer yet.
288 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
293 // Decode the Stream Footer. The decoder gives
294 // LZMA_FORMAT_ERROR if the magic bytes don't match,
295 // so convert that return code to LZMA_DATA_ERROR.
296 lzma_stream_flags footer_flags;
297 const lzma_ret ret = lzma_stream_footer_decode(
298 &footer_flags, coder->buffer);
300 return ret == LZMA_FORMAT_ERROR
301 ? LZMA_DATA_ERROR : ret;
303 // Check that Index Size stored in the Stream Footer matches
304 // the real size of the Index field.
305 if (lzma_index_hash_size(coder->index_hash)
306 != footer_flags.backward_size)
307 return LZMA_DATA_ERROR;
309 // Compare that the Stream Flags fields are identical in
310 // both Stream Header and Stream Footer.
311 return_if_error(lzma_stream_flags_compare(
312 &coder->stream_flags, &footer_flags));
314 if (!coder->concatenated)
315 return LZMA_STREAM_END;
317 coder->sequence = SEQ_STREAM_PADDING;
322 case SEQ_STREAM_PADDING:
323 assert(coder->concatenated);
325 // Skip over possible Stream Padding.
327 if (*in_pos >= in_size) {
328 // Unless LZMA_FINISH was used, we cannot
329 // know if there's more input coming later.
330 if (action != LZMA_FINISH)
333 // Stream Padding must be a multiple of
335 return coder->pos == 0
340 // If the byte is not zero, it probably indicates
341 // beginning of a new Stream (or the file is corrupt).
342 if (in[*in_pos] != 0x00)
346 coder->pos = (coder->pos + 1) & 3;
349 // Stream Padding must be a multiple of four bytes (empty
350 // Stream Padding is OK).
351 if (coder->pos != 0) {
353 return LZMA_DATA_ERROR;
356 // Prepare to decode the next Stream.
357 return_if_error(stream_decoder_reset(coder, allocator));
362 return LZMA_PROG_ERROR;
370 stream_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
372 lzma_next_end(&coder->block_decoder, allocator);
373 lzma_index_hash_end(coder->index_hash, allocator);
374 lzma_free(coder, allocator);
380 stream_decoder_get_check(const lzma_coder *coder)
382 return coder->stream_flags.check;
387 stream_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
388 uint64_t *old_memlimit, uint64_t new_memlimit)
390 *memusage = coder->memusage;
391 *old_memlimit = coder->memlimit;
393 if (new_memlimit != 0) {
394 if (new_memlimit < coder->memusage)
395 return LZMA_MEMLIMIT_ERROR;
397 coder->memlimit = new_memlimit;
405 lzma_stream_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
406 uint64_t memlimit, uint32_t flags)
408 lzma_next_coder_init(&lzma_stream_decoder_init, next, allocator);
411 return LZMA_PROG_ERROR;
413 if (flags & ~LZMA_SUPPORTED_FLAGS)
414 return LZMA_OPTIONS_ERROR;
416 if (next->coder == NULL) {
417 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
418 if (next->coder == NULL)
419 return LZMA_MEM_ERROR;
421 next->code = &stream_decode;
422 next->end = &stream_decoder_end;
423 next->get_check = &stream_decoder_get_check;
424 next->memconfig = &stream_decoder_memconfig;
426 next->coder->block_decoder = LZMA_NEXT_CODER_INIT;
427 next->coder->index_hash = NULL;
430 next->coder->memlimit = memlimit;
431 next->coder->memusage = LZMA_MEMUSAGE_BASE;
432 next->coder->tell_no_check = (flags & LZMA_TELL_NO_CHECK) != 0;
433 next->coder->tell_unsupported_check
434 = (flags & LZMA_TELL_UNSUPPORTED_CHECK) != 0;
435 next->coder->tell_any_check = (flags & LZMA_TELL_ANY_CHECK) != 0;
436 next->coder->concatenated = (flags & LZMA_CONCATENATED) != 0;
437 next->coder->first_stream = true;
439 return stream_decoder_reset(next->coder, allocator);
443 extern LZMA_API(lzma_ret)
444 lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
446 lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags);
448 strm->internal->supported_actions[LZMA_RUN] = true;
449 strm->internal->supported_actions[LZMA_FINISH] = true;