]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/stream_decoder.c
Removed what I believe is an incorrect #ifdef HAVE_* test.
[icculus/xz.git] / src / liblzma / common / stream_decoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       stream_decoder.c
4 /// \brief      Decodes .xz Streams
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #include "stream_decoder.h"
14 #include "block_decoder.h"
15
16
17 struct lzma_coder_s {
18         enum {
19                 SEQ_STREAM_HEADER,
20                 SEQ_BLOCK_HEADER,
21                 SEQ_BLOCK,
22                 SEQ_INDEX,
23                 SEQ_STREAM_FOOTER,
24                 SEQ_STREAM_PADDING,
25         } sequence;
26
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;
31
32         /// Block options decoded by the Block Header decoder and used by
33         /// the Block decoder.
34         lzma_block block_options;
35
36         /// Stream Flags from Stream Header
37         lzma_stream_flags stream_flags;
38
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;
42
43         /// Memory usage limit
44         uint64_t memlimit;
45
46         /// Amount of memory actually needed (only an estimate)
47         uint64_t memusage;
48
49         /// If true, LZMA_NO_CHECK is returned if the Stream has
50         /// no integrity check.
51         bool tell_no_check;
52
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;
56
57         /// If true, LZMA_GET_CHECK is returned after decoding Stream Header.
58         bool tell_any_check;
59
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.
65         bool concatenated;
66
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
70         /// bytes.
71         bool first_stream;
72
73         /// Write position in buffer[] and position in Stream Padding
74         size_t pos;
75
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];
79 };
80
81
82 static lzma_ret
83 stream_decoder_reset(lzma_coder *coder, lzma_allocator *allocator)
84 {
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;
89
90         // Reset the rest of the variables.
91         coder->sequence = SEQ_STREAM_HEADER;
92         coder->pos = 0;
93
94         return LZMA_OK;
95 }
96
97
98 static lzma_ret
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)
103 {
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.
106         while (true)
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);
112
113                 // Return if we didn't get the whole Stream Header yet.
114                 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
115                         return LZMA_OK;
116
117                 coder->pos = 0;
118
119                 // Decode the Stream Header.
120                 const lzma_ret ret = lzma_stream_header_decode(
121                                 &coder->stream_flags, coder->buffer);
122                 if (ret != LZMA_OK)
123                         return ret == LZMA_FORMAT_ERROR && !coder->first_stream
124                                         ? LZMA_DATA_ERROR : ret;
125
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;
130
131                 // Copy the type of the Check so that Block Header and Block
132                 // decoders see it.
133                 coder->block_options.check = coder->stream_flags.check;
134
135                 // Even if we return LZMA_*_CHECK below, we want
136                 // to continue from Block Header decoding.
137                 coder->sequence = SEQ_BLOCK_HEADER;
138
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
142                                 == LZMA_CHECK_NONE)
143                         return LZMA_NO_CHECK;
144
145                 if (coder->tell_unsupported_check
146                                 && !lzma_check_is_supported(
147                                         coder->stream_flags.check))
148                         return LZMA_UNSUPPORTED_CHECK;
149
150                 if (coder->tell_any_check)
151                         return LZMA_GET_CHECK;
152         }
153
154         // Fall through
155
156         case SEQ_BLOCK_HEADER: {
157                 if (*in_pos >= in_size)
158                         return LZMA_OK;
159
160                 if (coder->pos == 0) {
161                         // Detect if it's Index.
162                         if (in[*in_pos] == 0x00) {
163                                 coder->sequence = SEQ_INDEX;
164                                 break;
165                         }
166
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(
172                                                 in[*in_pos]);
173                 }
174
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);
178
179                 // Return if we didn't get the whole Block Header yet.
180                 if (coder->pos < coder->block_options.header_size)
181                         return LZMA_OK;
182
183                 coder->pos = 0;
184
185                 // Version 0 is currently the only possible version.
186                 coder->block_options.version = 0;
187
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;
193
194                 // Decode the Block Header.
195                 return_if_error(lzma_block_header_decode(&coder->block_options,
196                                 allocator, coder->buffer));
197
198                 // Check the memory usage limit.
199                 const uint64_t memusage = lzma_raw_decoder_memusage(filters);
200                 lzma_ret ret;
201
202                 if (memusage == UINT64_MAX) {
203                         // One or more unknown Filter IDs.
204                         ret = LZMA_OPTIONS_ERROR;
205                 } else {
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;
211
212                         if (memusage > coder->memlimit) {
213                                 // The chain would need too much memory.
214                                 ret = LZMA_MEMLIMIT_ERROR;
215                         } else {
216                                 // Memory usage is OK.
217                                 // Initialize the Block decoder.
218                                 ret = lzma_block_decoder_init(
219                                                 &coder->block_decoder,
220                                                 allocator,
221                                                 &coder->block_options);
222                         }
223                 }
224
225                 // Free the allocated filter options since they are needed
226                 // only to initialize the Block decoder.
227                 size_t i;
228                 for (i = 0; i < LZMA_FILTERS_MAX; ++i)
229                         lzma_free(filters[i].options, allocator);
230
231                 coder->block_options.filters = NULL;
232
233                 // Check if memory usage calculation and Block enocoder
234                 // initialization succeeded.
235                 if (ret != LZMA_OK)
236                         return ret;
237
238                 coder->sequence = SEQ_BLOCK;
239         }
240
241         // Fall through
242
243         case 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,
247                                 action);
248
249                 if (ret != LZMA_STREAM_END)
250                         return ret;
251
252                 // Block decoded successfully. Add the new size pair to
253                 // the Index hash.
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));
258
259                 coder->sequence = SEQ_BLOCK_HEADER;
260                 break;
261         }
262
263         case SEQ_INDEX: {
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)
268                         return LZMA_OK;
269
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)
275                         return ret;
276
277                 coder->sequence = SEQ_STREAM_FOOTER;
278         }
279
280         // Fall through
281
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);
286
287                 // Return if we didn't get the whole Stream Footer yet.
288                 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
289                         return LZMA_OK;
290
291                 coder->pos = 0;
292
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);
299                 if (ret != LZMA_OK)
300                         return ret == LZMA_FORMAT_ERROR
301                                         ? LZMA_DATA_ERROR : ret;
302
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;
308
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));
313
314                 if (!coder->concatenated)
315                         return LZMA_STREAM_END;
316
317                 coder->sequence = SEQ_STREAM_PADDING;
318         }
319
320         // Fall through
321
322         case SEQ_STREAM_PADDING:
323                 assert(coder->concatenated);
324
325                 // Skip over possible Stream Padding.
326                 while (true) {
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)
331                                         return LZMA_OK;
332
333                                 // Stream Padding must be a multiple of
334                                 // four bytes.
335                                 return coder->pos == 0
336                                                 ? LZMA_STREAM_END
337                                                 : LZMA_DATA_ERROR;
338                         }
339
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)
343                                 break;
344
345                         ++*in_pos;
346                         coder->pos = (coder->pos + 1) & 3;
347                 }
348
349                 // Stream Padding must be a multiple of four bytes (empty
350                 // Stream Padding is OK).
351                 if (coder->pos != 0) {
352                         ++*in_pos;
353                         return LZMA_DATA_ERROR;
354                 }
355
356                 // Prepare to decode the next Stream.
357                 return_if_error(stream_decoder_reset(coder, allocator));
358                 break;
359
360         default:
361                 assert(0);
362                 return LZMA_PROG_ERROR;
363         }
364
365         // Never reached
366 }
367
368
369 static void
370 stream_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
371 {
372         lzma_next_end(&coder->block_decoder, allocator);
373         lzma_index_hash_end(coder->index_hash, allocator);
374         lzma_free(coder, allocator);
375         return;
376 }
377
378
379 static lzma_check
380 stream_decoder_get_check(const lzma_coder *coder)
381 {
382         return coder->stream_flags.check;
383 }
384
385
386 static lzma_ret
387 stream_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
388                 uint64_t *old_memlimit, uint64_t new_memlimit)
389 {
390         *memusage = coder->memusage;
391         *old_memlimit = coder->memlimit;
392
393         if (new_memlimit != 0) {
394                 if (new_memlimit < coder->memusage)
395                         return LZMA_MEMLIMIT_ERROR;
396
397                 coder->memlimit = new_memlimit;
398         }
399
400         return LZMA_OK;
401 }
402
403
404 extern lzma_ret
405 lzma_stream_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
406                 uint64_t memlimit, uint32_t flags)
407 {
408         lzma_next_coder_init(&lzma_stream_decoder_init, next, allocator);
409
410         if (memlimit == 0)
411                 return LZMA_PROG_ERROR;
412
413         if (flags & ~LZMA_SUPPORTED_FLAGS)
414                 return LZMA_OPTIONS_ERROR;
415
416         if (next->coder == NULL) {
417                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
418                 if (next->coder == NULL)
419                         return LZMA_MEM_ERROR;
420
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;
425
426                 next->coder->block_decoder = LZMA_NEXT_CODER_INIT;
427                 next->coder->index_hash = NULL;
428         }
429
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;
438
439         return stream_decoder_reset(next->coder, allocator);
440 }
441
442
443 extern LZMA_API(lzma_ret)
444 lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
445 {
446         lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags);
447
448         strm->internal->supported_actions[LZMA_RUN] = true;
449         strm->internal->supported_actions[LZMA_FINISH] = true;
450
451         return LZMA_OK;
452 }