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 "stream_flags_common.h"
23 #include "stream_flags_decoder.h"
24 #include "block_decoder.h"
37 /// Block or Metadata decoder. This takes little memory and the same
38 /// data structure can be used to decode every Block Header, so it's
39 /// a good idea to have a separate lzma_next_coder structure for it.
40 lzma_next_coder block_decoder;
42 /// Block options decoded by the Block Header decoder and used by
43 /// the Block decoder.
44 lzma_block block_options;
46 /// Stream Flags from Stream Header
47 lzma_stream_flags stream_flags;
49 /// Index is hashed so that it can be compared to the sizes of Blocks
50 /// with O(1) memory usage.
51 lzma_index_hash *index_hash;
53 /// Memory usage limit
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 warn_unsupported_check;
64 /// If true, LZMA_SEE_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 // Copy the type of the Check so that Block Header and Block
136 coder->block_options.check = coder->stream_flags.check;
138 // Even if we return LZMA_*_CHECK below, we want
139 // to continue from Block Header decoding.
140 coder->sequence = SEQ_BLOCK_HEADER;
142 // Detect if there's no integrity check or if it is
143 // unsupported if those were requested by the application.
144 if (coder->warn_no_check && coder->stream_flags.check
146 return LZMA_NO_CHECK;
148 if (coder->warn_unsupported_check
149 && !lzma_check_is_supported(
150 coder->stream_flags.check))
151 return LZMA_UNSUPPORTED_CHECK;
153 if (coder->tell_check)
154 return LZMA_SEE_CHECK;
159 case SEQ_BLOCK_HEADER: {
160 if (*in_pos >= in_size)
163 if (coder->pos == 0) {
164 // Detect if it's Index.
165 if (in[*in_pos] == 0x00) {
166 coder->sequence = SEQ_INDEX;
170 // Calculate the size of the Block Header. Note that
171 // Block Header decoder wants to see this byte too
172 // so don't advance *in_pos.
173 coder->block_options.header_size
174 = lzma_block_header_size_decode(
178 // Copy the Block Header to the internal buffer.
179 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
180 coder->block_options.header_size);
182 // Return if we didn't get the whole Block Header yet.
183 if (coder->pos < coder->block_options.header_size)
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_BLOCK_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_memusage_decoder(filters);
202 if (memusage == UINT64_MAX) {
203 // One or more unknown Filter IDs.
204 ret = LZMA_HEADER_ERROR;
205 } else if (memusage > coder->memlimit) {
206 // The chain would need too much memory.
207 ret = LZMA_MEMLIMIT_ERROR;
209 // Memory usage is OK. Initialize the Block decoder.
210 ret = lzma_block_decoder_init(
211 &coder->block_decoder,
212 allocator, &coder->block_options);
215 // Free the allocated filter options since they are needed
216 // only to initialize the Block decoder.
217 for (size_t i = 0; i < LZMA_BLOCK_FILTERS_MAX; ++i)
218 lzma_free(filters[i].options, allocator);
220 coder->block_options.filters = NULL;
222 // Check if memory usage calculation and Block enocoder
223 // initialization succeeded.
227 coder->sequence = SEQ_BLOCK;
233 const lzma_ret ret = coder->block_decoder.code(
234 coder->block_decoder.coder, allocator,
235 in, in_pos, in_size, out, out_pos, out_size,
238 if (ret != LZMA_STREAM_END)
241 // Block decoded successfully. Add the new size pair to
243 return_if_error(lzma_index_hash_append(coder->index_hash,
244 lzma_block_total_size_get(
245 &coder->block_options),
246 coder->block_options.uncompressed_size));
248 coder->sequence = SEQ_BLOCK_HEADER;
253 // If we don't have any input, don't call
254 // lzma_index_hash_decode() since it would return
255 // LZMA_BUF_ERROR, which we must not do here.
256 if (*in_pos >= in_size)
259 // Decode the Index and compare it to the hash calculated
260 // from the sizes of the Blocks (if any).
261 const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
262 in, in_pos, in_size);
263 if (ret != LZMA_STREAM_END)
266 coder->sequence = SEQ_STREAM_FOOTER;
271 case SEQ_STREAM_FOOTER:
272 // Copy the Stream Footer to the internal buffer.
273 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
274 LZMA_STREAM_HEADER_SIZE);
276 // Return if we didn't get the whole Stream Footer yet.
277 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
282 // Decode the Stream Footer. The decoder gives
283 // LZMA_FORMAT_ERROR if the magic bytes don't match,
284 // so convert that return code to LZMA_DATA_ERROR.
285 lzma_stream_flags footer_flags;
286 const lzma_ret ret = lzma_stream_footer_decode(
287 &footer_flags, coder->buffer);
289 return ret == LZMA_FORMAT_ERROR
290 ? LZMA_DATA_ERROR : ret;
292 // Check that Index Size stored in the Stream Footer matches
293 // the real size of the Index field.
294 if (lzma_index_hash_size(coder->index_hash)
295 != footer_flags.backward_size)
296 return LZMA_DATA_ERROR;
298 // Compare that the Stream Flags fields are identical in
299 // both Stream Header and Stream Footer.
300 if (!lzma_stream_flags_equal(&coder->stream_flags,
302 return LZMA_DATA_ERROR;
304 if (!coder->concatenated)
305 return LZMA_STREAM_END;
307 coder->sequence = SEQ_STREAM_PADDING;
311 case SEQ_STREAM_PADDING:
312 assert(coder->concatenated);
314 // Skip over possible Stream Padding.
316 if (*in_pos >= in_size) {
317 // Unless LZMA_FINISH was used, we cannot
318 // know if there's more input coming later.
319 if (action != LZMA_FINISH)
322 // Stream Padding must be a multiple of
324 return coder->pos == 0
329 // If the byte is not zero, it probably indicates
330 // beginning of a new Stream (or the file is corrupt).
331 if (in[*in_pos] != 0x00)
335 coder->pos = (coder->pos + 1) & 3;
338 // Stream Padding must be a multiple of four bytes (empty
339 // Stream Padding is OK).
340 if (coder->pos != 0) {
342 return LZMA_DATA_ERROR;
345 // Prepare to decode the next Stream.
346 return_if_error(stream_decoder_reset(coder, allocator));
351 return LZMA_PROG_ERROR;
359 stream_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
361 lzma_next_end(&coder->block_decoder, allocator);
362 lzma_index_hash_end(coder->index_hash, allocator);
363 lzma_free(coder, allocator);
369 stream_decoder_see_check(const lzma_coder *coder)
371 return coder->stream_flags.check;
376 lzma_stream_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
377 uint64_t memlimit, uint32_t flags)
379 lzma_next_coder_init(lzma_stream_decoder_init, next, allocator);
381 if (flags & ~LZMA_SUPPORTED_FLAGS)
382 return LZMA_HEADER_ERROR;
384 if (next->coder == NULL) {
385 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
386 if (next->coder == NULL)
387 return LZMA_MEM_ERROR;
389 next->code = &stream_decode;
390 next->end = &stream_decoder_end;
391 next->see_check = &stream_decoder_see_check;
393 next->coder->block_decoder = LZMA_NEXT_CODER_INIT;
394 next->coder->index_hash = NULL;
397 next->coder->memlimit = memlimit;
398 next->coder->warn_no_check = (flags & LZMA_WARN_NO_CHECK) != 0;
399 next->coder->warn_unsupported_check
400 = (flags & LZMA_WARN_UNSUPPORTED_CHECK) != 0;
401 next->coder->tell_check = (flags & LZMA_TELL_CHECK) != 0;
402 next->coder->concatenated
403 = (flags & LZMA_CONCATENATED) != 0;
405 return stream_decoder_reset(next->coder, allocator);
409 extern LZMA_API lzma_ret
410 lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
412 lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags);
414 strm->internal->supported_actions[LZMA_RUN] = true;
415 strm->internal->supported_actions[LZMA_FINISH] = true;