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. FIXME
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;
232 const lzma_ret ret = coder->block_decoder.code(
233 coder->block_decoder.coder, allocator,
234 in, in_pos, in_size, out, out_pos, out_size,
237 if (ret != LZMA_STREAM_END)
240 // Block decoded successfully. Add the new size pair to
242 return_if_error(lzma_index_hash_append(coder->index_hash,
243 lzma_block_total_size_get(
244 &coder->block_options),
245 coder->block_options.uncompressed_size));
247 coder->sequence = SEQ_BLOCK_HEADER;
252 // If we don't have any input, don't call
253 // lzma_index_hash_decode() since it would return
254 // LZMA_BUF_ERROR, which we must not do here.
255 if (*in_pos >= in_size)
258 // Decode the Index and compare it to the hash calculated
259 // from the sizes of the Blocks (if any).
260 const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
261 in, in_pos, in_size);
262 if (ret != LZMA_STREAM_END)
265 coder->sequence = SEQ_STREAM_FOOTER;
269 case SEQ_STREAM_FOOTER:
270 // Copy the Stream Footer to the internal buffer.
271 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
272 LZMA_STREAM_HEADER_SIZE);
274 // Return if we didn't get the whole Stream Footer yet.
275 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
280 // Decode the Stream Footer.
281 // FIXME LZMA_FORMAT_ERROR doesn't make sense here.
282 lzma_stream_flags footer_flags;
283 return_if_error(lzma_stream_footer_decode(
284 &footer_flags, coder->buffer));
286 // Check that Index Size stored in the Stream Footer matches
287 // the real size of the Index field.
288 if (lzma_index_hash_size(coder->index_hash)
289 != footer_flags.backward_size)
290 return LZMA_DATA_ERROR;
292 // Compare that the Stream Flags fields are identical in
293 // both Stream Header and Stream Footer.
294 if (!lzma_stream_flags_equal(&coder->stream_flags,
296 return LZMA_DATA_ERROR;
298 if (!coder->concatenated)
299 return LZMA_STREAM_END;
301 coder->sequence = SEQ_STREAM_PADDING;
304 case SEQ_STREAM_PADDING:
305 assert(coder->concatenated);
308 if (*in_pos >= in_size) {
309 // Unless LZMA_FINISH was used, we cannot
310 // know if there's more input coming later.
311 if (action != LZMA_FINISH)
314 // Stream Padding must be a multiple of
316 return coder->pos == 0
321 if (in[*in_pos] != 0x00) {
322 if (coder->pos != 0) {
323 // Stream Padding is not a multiple of
326 return LZMA_DATA_ERROR;
329 // Prepare to decode the next Stream.
330 return_if_error(stream_decoder_reset(
336 coder->pos = (coder->pos + 1) & 3;
343 return LZMA_PROG_ERROR;
351 stream_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
353 lzma_next_end(&coder->block_decoder, allocator);
354 lzma_index_hash_end(coder->index_hash, allocator);
355 lzma_free(coder, allocator);
361 stream_decoder_see_check(const lzma_coder *coder)
363 return coder->stream_flags.check;
368 lzma_stream_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
369 uint64_t memlimit, uint32_t flags)
371 lzma_next_coder_init(lzma_stream_decoder_init, next, allocator);
373 if (flags & ~LZMA_SUPPORTED_FLAGS)
374 return LZMA_HEADER_ERROR;
376 if (next->coder == NULL) {
377 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
378 if (next->coder == NULL)
379 return LZMA_MEM_ERROR;
381 next->code = &stream_decode;
382 next->end = &stream_decoder_end;
383 next->see_check = &stream_decoder_see_check;
385 next->coder->block_decoder = LZMA_NEXT_CODER_INIT;
386 next->coder->index_hash = NULL;
389 next->coder->memlimit = memlimit;
390 next->coder->warn_no_check = (flags & LZMA_WARN_NO_CHECK) != 0;
391 next->coder->warn_unsupported_check
392 = (flags & LZMA_WARN_UNSUPPORTED_CHECK) != 0;
393 next->coder->tell_check = (flags & LZMA_TELL_CHECK) != 0;
394 next->coder->concatenated
395 = (flags & LZMA_CONCATENATED) != 0;
397 return stream_decoder_reset(next->coder, allocator);
401 extern LZMA_API lzma_ret
402 lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
404 lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags);
406 strm->internal->supported_actions[LZMA_RUN] = true;
407 // strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true; // FIXME
408 strm->internal->supported_actions[LZMA_FINISH] = true;