1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file stream_decoder.c
4 /// \brief Decodes .xz 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;
106 stream_decode(lzma_coder *coder, lzma_allocator *allocator,
107 const uint8_t *restrict in, size_t *restrict in_pos,
108 size_t in_size, uint8_t *restrict out,
109 size_t *restrict out_pos, size_t out_size, lzma_action action)
111 // When decoding the actual Block, it may be able to produce more
112 // output even if we don't give it any new input.
114 switch (coder->sequence) {
115 case SEQ_STREAM_HEADER: {
116 // Copy the Stream Header to the internal buffer.
117 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
118 LZMA_STREAM_HEADER_SIZE);
120 // Return if we didn't get the whole Stream Header yet.
121 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
126 // Decode the Stream Header.
127 const lzma_ret ret = lzma_stream_header_decode(
128 &coder->stream_flags, coder->buffer);
130 return ret == LZMA_FORMAT_ERROR && !coder->first_stream
131 ? LZMA_DATA_ERROR : ret;
133 // If we are decoding concatenated Streams, and the later
134 // Streams have invalid Header Magic Bytes, we give
135 // LZMA_DATA_ERROR instead of LZMA_FORMAT_ERROR.
136 coder->first_stream = false;
138 // Copy the type of the Check so that Block Header and Block
140 coder->block_options.check = coder->stream_flags.check;
142 // Even if we return LZMA_*_CHECK below, we want
143 // to continue from Block Header decoding.
144 coder->sequence = SEQ_BLOCK_HEADER;
146 // Detect if there's no integrity check or if it is
147 // unsupported if those were requested by the application.
148 if (coder->tell_no_check && coder->stream_flags.check
150 return LZMA_NO_CHECK;
152 if (coder->tell_unsupported_check
153 && !lzma_check_is_supported(
154 coder->stream_flags.check))
155 return LZMA_UNSUPPORTED_CHECK;
157 if (coder->tell_any_check)
158 return LZMA_GET_CHECK;
163 case SEQ_BLOCK_HEADER: {
164 if (*in_pos >= in_size)
167 if (coder->pos == 0) {
168 // Detect if it's Index.
169 if (in[*in_pos] == 0x00) {
170 coder->sequence = SEQ_INDEX;
174 // Calculate the size of the Block Header. Note that
175 // Block Header decoder wants to see this byte too
176 // so don't advance *in_pos.
177 coder->block_options.header_size
178 = lzma_block_header_size_decode(
182 // Copy the Block Header to the internal buffer.
183 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
184 coder->block_options.header_size);
186 // Return if we didn't get the whole Block Header yet.
187 if (coder->pos < coder->block_options.header_size)
192 // Set up a buffer to hold the filter chain. Block Header
193 // decoder will initialize all members of this array so
194 // we don't need to do it here.
195 lzma_filter filters[LZMA_FILTERS_MAX + 1];
196 coder->block_options.filters = filters;
198 // Decode the Block Header.
199 return_if_error(lzma_block_header_decode(&coder->block_options,
200 allocator, coder->buffer));
202 // Check the memory usage limit.
203 const uint64_t memusage = lzma_raw_decoder_memusage(filters);
206 if (memusage == UINT64_MAX) {
207 // One or more unknown Filter IDs.
208 ret = LZMA_OPTIONS_ERROR;
210 // Now we can set coder->memusage since we know that
211 // the filter chain is valid. We don't want
212 // lzma_memusage() to return UINT64_MAX in case of
213 // invalid filter chain.
214 coder->memusage = memusage;
216 if (memusage > coder->memlimit) {
217 // The chain would need too much memory.
218 ret = LZMA_MEMLIMIT_ERROR;
220 // Memory usage is OK.
221 // Initialize the Block decoder.
222 ret = lzma_block_decoder_init(
223 &coder->block_decoder,
225 &coder->block_options);
229 // Free the allocated filter options since they are needed
230 // only to initialize the Block decoder.
231 for (size_t i = 0; i < LZMA_FILTERS_MAX; ++i)
232 lzma_free(filters[i].options, allocator);
234 coder->block_options.filters = NULL;
236 // Check if memory usage calculation and Block enocoder
237 // initialization succeeded.
241 coder->sequence = SEQ_BLOCK;
247 const lzma_ret ret = coder->block_decoder.code(
248 coder->block_decoder.coder, allocator,
249 in, in_pos, in_size, out, out_pos, out_size,
252 if (ret != LZMA_STREAM_END)
255 // Block decoded successfully. Add the new size pair to
257 return_if_error(lzma_index_hash_append(coder->index_hash,
258 lzma_block_unpadded_size(
259 &coder->block_options),
260 coder->block_options.uncompressed_size));
262 coder->sequence = SEQ_BLOCK_HEADER;
267 // If we don't have any input, don't call
268 // lzma_index_hash_decode() since it would return
269 // LZMA_BUF_ERROR, which we must not do here.
270 if (*in_pos >= in_size)
273 // Decode the Index and compare it to the hash calculated
274 // from the sizes of the Blocks (if any).
275 const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
276 in, in_pos, in_size);
277 if (ret != LZMA_STREAM_END)
280 coder->sequence = SEQ_STREAM_FOOTER;
285 case SEQ_STREAM_FOOTER: {
286 // Copy the Stream Footer to the internal buffer.
287 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
288 LZMA_STREAM_HEADER_SIZE);
290 // Return if we didn't get the whole Stream Footer yet.
291 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
296 // Decode the Stream Footer. The decoder gives
297 // LZMA_FORMAT_ERROR if the magic bytes don't match,
298 // so convert that return code to LZMA_DATA_ERROR.
299 lzma_stream_flags footer_flags;
300 const lzma_ret ret = lzma_stream_footer_decode(
301 &footer_flags, coder->buffer);
303 return ret == LZMA_FORMAT_ERROR
304 ? LZMA_DATA_ERROR : ret;
306 // Check that Index Size stored in the Stream Footer matches
307 // the real size of the Index field.
308 if (lzma_index_hash_size(coder->index_hash)
309 != footer_flags.backward_size)
310 return LZMA_DATA_ERROR;
312 // Compare that the Stream Flags fields are identical in
313 // both Stream Header and Stream Footer.
314 return_if_error(lzma_stream_flags_compare(
315 &coder->stream_flags, &footer_flags));
317 if (!coder->concatenated)
318 return LZMA_STREAM_END;
320 coder->sequence = SEQ_STREAM_PADDING;
325 case SEQ_STREAM_PADDING:
326 assert(coder->concatenated);
328 // Skip over possible Stream Padding.
330 if (*in_pos >= in_size) {
331 // Unless LZMA_FINISH was used, we cannot
332 // know if there's more input coming later.
333 if (action != LZMA_FINISH)
336 // Stream Padding must be a multiple of
338 return coder->pos == 0
343 // If the byte is not zero, it probably indicates
344 // beginning of a new Stream (or the file is corrupt).
345 if (in[*in_pos] != 0x00)
349 coder->pos = (coder->pos + 1) & 3;
352 // Stream Padding must be a multiple of four bytes (empty
353 // Stream Padding is OK).
354 if (coder->pos != 0) {
356 return LZMA_DATA_ERROR;
359 // Prepare to decode the next Stream.
360 return_if_error(stream_decoder_reset(coder, allocator));
365 return LZMA_PROG_ERROR;
373 stream_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
375 lzma_next_end(&coder->block_decoder, allocator);
376 lzma_index_hash_end(coder->index_hash, allocator);
377 lzma_free(coder, allocator);
383 stream_decoder_get_check(const lzma_coder *coder)
385 return coder->stream_flags.check;
390 stream_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
391 uint64_t *old_memlimit, uint64_t new_memlimit)
393 if (new_memlimit != 0 && new_memlimit < coder->memusage)
394 return LZMA_MEMLIMIT_ERROR;
396 *memusage = coder->memusage;
397 *old_memlimit = coder->memlimit;
398 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;