]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/stream_decoder.c
Bunch of liblzma API cleanups and fixes.
[icculus/xz.git] / src / liblzma / common / stream_decoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       stream_decoder.c
4 /// \brief      Decodes .lzma Streams
5 //
6 //  Copyright (C) 2007 Lasse Collin
7 //
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.
12 //
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.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "stream_decoder.h"
21 #include "block_decoder.h"
22
23
24 struct lzma_coder_s {
25         enum {
26                 SEQ_STREAM_HEADER,
27                 SEQ_BLOCK_HEADER,
28                 SEQ_BLOCK,
29                 SEQ_INDEX,
30                 SEQ_STREAM_FOOTER,
31                 SEQ_STREAM_PADDING,
32         } sequence;
33
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;
38
39         /// Block options decoded by the Block Header decoder and used by
40         /// the Block decoder.
41         lzma_block block_options;
42
43         /// Stream Flags from Stream Header
44         lzma_stream_flags stream_flags;
45
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;
49
50         /// Memory usage limit
51         uint64_t memlimit;
52
53         /// Amount of memory actually needed (only an estimate)
54         uint64_t memusage;
55
56         /// If true, LZMA_NO_CHECK is returned if the Stream has
57         /// no integrity check.
58         bool tell_no_check;
59
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;
63
64         /// If true, LZMA_GET_CHECK is returned after decoding Stream Header.
65         bool tell_any_check;
66
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.
72         bool concatenated;
73
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
77         /// bytes.
78         bool first_stream;
79
80         /// Write position in buffer[] and position in Stream Padding
81         size_t pos;
82
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];
86 };
87
88
89 static lzma_ret
90 stream_decoder_reset(lzma_coder *coder, lzma_allocator *allocator)
91 {
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;
96
97         // Reset the rest of the variables.
98         coder->sequence = SEQ_STREAM_HEADER;
99         coder->block_options.filters = NULL;
100         coder->pos = 0;
101
102         return LZMA_OK;
103 }
104
105
106 static lzma_ret
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)
111 {
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.
114         while (true)
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);
120
121                 // Return if we didn't get the whole Stream Header yet.
122                 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
123                         return LZMA_OK;
124
125                 coder->pos = 0;
126
127                 // Decode the Stream Header.
128                 const lzma_ret ret = lzma_stream_header_decode(
129                                 &coder->stream_flags, coder->buffer);
130                 if (ret != LZMA_OK)
131                         return ret == LZMA_FORMAT_ERROR && !coder->first_stream
132                                         ? LZMA_DATA_ERROR : ret;
133
134                 // If we are decoding concatenated Streams, and the later
135                 // Streams have invalid Header Magic Bytes, we give
136                 // LZMA_DATA_ERROR instead of LZMA_FORMAT_ERROR.
137                 coder->first_stream = false;
138
139                 // Copy the type of the Check so that Block Header and Block
140                 // decoders see it.
141                 coder->block_options.check = coder->stream_flags.check;
142
143                 // Even if we return LZMA_*_CHECK below, we want
144                 // to continue from Block Header decoding.
145                 coder->sequence = SEQ_BLOCK_HEADER;
146
147                 // Detect if there's no integrity check or if it is
148                 // unsupported if those were requested by the application.
149                 if (coder->tell_no_check && coder->stream_flags.check
150                                 == LZMA_CHECK_NONE)
151                         return LZMA_NO_CHECK;
152
153                 if (coder->tell_unsupported_check
154                                 && !lzma_check_is_supported(
155                                         coder->stream_flags.check))
156                         return LZMA_UNSUPPORTED_CHECK;
157
158                 if (coder->tell_any_check)
159                         return LZMA_GET_CHECK;
160         }
161
162         // Fall through
163
164         case SEQ_BLOCK_HEADER: {
165                 if (*in_pos >= in_size)
166                         return LZMA_OK;
167
168                 if (coder->pos == 0) {
169                         // Detect if it's Index.
170                         if (in[*in_pos] == 0x00) {
171                                 coder->sequence = SEQ_INDEX;
172                                 break;
173                         }
174
175                         // Calculate the size of the Block Header. Note that
176                         // Block Header decoder wants to see this byte too
177                         // so don't advance *in_pos.
178                         coder->block_options.header_size
179                                         = lzma_block_header_size_decode(
180                                                 in[*in_pos]);
181                 }
182
183                 // Copy the Block Header to the internal buffer.
184                 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
185                                 coder->block_options.header_size);
186
187                 // Return if we didn't get the whole Block Header yet.
188                 if (coder->pos < coder->block_options.header_size)
189                         return LZMA_OK;
190
191                 coder->pos = 0;
192
193                 // Set up a buffer to hold the filter chain. Block Header
194                 // decoder will initialize all members of this array so
195                 // we don't need to do it here.
196                 lzma_filter filters[LZMA_FILTERS_MAX + 1];
197                 coder->block_options.filters = filters;
198
199                 // Decode the Block Header.
200                 return_if_error(lzma_block_header_decode(&coder->block_options,
201                                 allocator, coder->buffer));
202
203                 // Check the memory usage limit.
204                 const uint64_t memusage = lzma_memusage_decoder(filters);
205                 lzma_ret ret;
206
207                 if (memusage == UINT64_MAX) {
208                         // One or more unknown Filter IDs.
209                         ret = LZMA_OPTIONS_ERROR;
210                 } else {
211                         // Now we can set coder->memusage since we know that
212                         // the filter chain is valid. We don't want
213                         // lzma_memusage() to return UINT64_MAX in case of
214                         // invalid filter chain.
215                         coder->memusage = memusage;
216
217                         if (memusage > coder->memlimit) {
218                                 // The chain would need too much memory.
219                                 ret = LZMA_MEMLIMIT_ERROR;
220                         } else {
221                                 // Memory usage is OK.
222                                 // Initialize the Block decoder.
223                                 ret = lzma_block_decoder_init(
224                                                 &coder->block_decoder,
225                                                 allocator,
226                                                 &coder->block_options);
227                         }
228                 }
229
230                 // Free the allocated filter options since they are needed
231                 // only to initialize the Block decoder.
232                 for (size_t i = 0; i < LZMA_FILTERS_MAX; ++i)
233                         lzma_free(filters[i].options, allocator);
234
235                 coder->block_options.filters = NULL;
236
237                 // Check if memory usage calculation and Block enocoder
238                 // initialization succeeded.
239                 if (ret != LZMA_OK)
240                         return ret;
241
242                 coder->sequence = SEQ_BLOCK;
243         }
244
245         // Fall through
246
247         case SEQ_BLOCK: {
248                 const lzma_ret ret = coder->block_decoder.code(
249                                 coder->block_decoder.coder, allocator,
250                                 in, in_pos, in_size, out, out_pos, out_size,
251                                 action);
252
253                 if (ret != LZMA_STREAM_END)
254                         return ret;
255
256                 // Block decoded successfully. Add the new size pair to
257                 // the Index hash.
258                 return_if_error(lzma_index_hash_append(coder->index_hash,
259                                 lzma_block_unpadded_size(
260                                         &coder->block_options),
261                                 coder->block_options.uncompressed_size));
262
263                 coder->sequence = SEQ_BLOCK_HEADER;
264                 break;
265         }
266
267         case SEQ_INDEX: {
268                 // If we don't have any input, don't call
269                 // lzma_index_hash_decode() since it would return
270                 // LZMA_BUF_ERROR, which we must not do here.
271                 if (*in_pos >= in_size)
272                         return LZMA_OK;
273
274                 // Decode the Index and compare it to the hash calculated
275                 // from the sizes of the Blocks (if any).
276                 const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
277                                 in, in_pos, in_size);
278                 if (ret != LZMA_STREAM_END)
279                         return ret;
280
281                 coder->sequence = SEQ_STREAM_FOOTER;
282         }
283
284         // Fall through
285
286         case SEQ_STREAM_FOOTER: {
287                 // Copy the Stream Footer to the internal buffer.
288                 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
289                                 LZMA_STREAM_HEADER_SIZE);
290
291                 // Return if we didn't get the whole Stream Footer yet.
292                 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
293                         return LZMA_OK;
294
295                 coder->pos = 0;
296
297                 // Decode the Stream Footer. The decoder gives
298                 // LZMA_FORMAT_ERROR if the magic bytes don't match,
299                 // so convert that return code to LZMA_DATA_ERROR.
300                 lzma_stream_flags footer_flags;
301                 const lzma_ret ret = lzma_stream_footer_decode(
302                                 &footer_flags, coder->buffer);
303                 if (ret != LZMA_OK)
304                         return ret == LZMA_FORMAT_ERROR
305                                         ? LZMA_DATA_ERROR : ret;
306
307                 // Check that Index Size stored in the Stream Footer matches
308                 // the real size of the Index field.
309                 if (lzma_index_hash_size(coder->index_hash)
310                                 != footer_flags.backward_size)
311                         return LZMA_DATA_ERROR;
312
313                 // Compare that the Stream Flags fields are identical in
314                 // both Stream Header and Stream Footer.
315                 return_if_error(lzma_stream_flags_compare(
316                                 &coder->stream_flags, &footer_flags));
317
318                 if (!coder->concatenated)
319                         return LZMA_STREAM_END;
320
321                 coder->sequence = SEQ_STREAM_PADDING;
322         }
323
324         // Fall through
325
326         case SEQ_STREAM_PADDING:
327                 assert(coder->concatenated);
328
329                 // Skip over possible Stream Padding.
330                 while (true) {
331                         if (*in_pos >= in_size) {
332                                 // Unless LZMA_FINISH was used, we cannot
333                                 // know if there's more input coming later.
334                                 if (action != LZMA_FINISH)
335                                         return LZMA_OK;
336
337                                 // Stream Padding must be a multiple of
338                                 // four bytes.
339                                 return coder->pos == 0
340                                                 ? LZMA_STREAM_END
341                                                 : LZMA_DATA_ERROR;
342                         }
343
344                         // If the byte is not zero, it probably indicates
345                         // beginning of a new Stream (or the file is corrupt).
346                         if (in[*in_pos] != 0x00)
347                                 break;
348
349                         ++*in_pos;
350                         coder->pos = (coder->pos + 1) & 3;
351                 }
352
353                 // Stream Padding must be a multiple of four bytes (empty
354                 // Stream Padding is OK).
355                 if (coder->pos != 0) {
356                         ++*in_pos;
357                         return LZMA_DATA_ERROR;
358                 }
359
360                 // Prepare to decode the next Stream.
361                 return_if_error(stream_decoder_reset(coder, allocator));
362                 break;
363
364         default:
365                 assert(0);
366                 return LZMA_PROG_ERROR;
367         }
368
369         return LZMA_OK;
370 }
371
372
373 static void
374 stream_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
375 {
376         lzma_next_end(&coder->block_decoder, allocator);
377         lzma_index_hash_end(coder->index_hash, allocator);
378         lzma_free(coder, allocator);
379         return;
380 }
381
382
383 static lzma_check
384 stream_decoder_get_check(const lzma_coder *coder)
385 {
386         return coder->stream_flags.check;
387 }
388
389
390 static lzma_ret
391 stream_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
392                 uint64_t *old_memlimit, uint64_t new_memlimit)
393 {
394         if (new_memlimit != 0 && new_memlimit < coder->memusage)
395                 return LZMA_MEMLIMIT_ERROR;
396
397         *memusage = coder->memusage;
398         *old_memlimit = coder->memlimit;
399         coder->memlimit = new_memlimit;
400
401         return LZMA_OK;
402 }
403
404
405 extern lzma_ret
406 lzma_stream_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
407                 uint64_t memlimit, uint32_t flags)
408 {
409         lzma_next_coder_init(lzma_stream_decoder_init, next, allocator);
410
411         if (memlimit == 0)
412                 return LZMA_PROG_ERROR;
413
414         if (flags & ~LZMA_SUPPORTED_FLAGS)
415                 return LZMA_OPTIONS_ERROR;
416
417         if (next->coder == NULL) {
418                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
419                 if (next->coder == NULL)
420                         return LZMA_MEM_ERROR;
421
422                 next->code = &stream_decode;
423                 next->end = &stream_decoder_end;
424                 next->get_check = &stream_decoder_get_check;
425                 next->memconfig = &stream_decoder_memconfig;
426
427                 next->coder->block_decoder = LZMA_NEXT_CODER_INIT;
428                 next->coder->index_hash = NULL;
429         }
430
431         next->coder->memlimit = memlimit;
432         next->coder->memusage = LZMA_MEMUSAGE_BASE;
433         next->coder->tell_no_check = (flags & LZMA_TELL_NO_CHECK) != 0;
434         next->coder->tell_unsupported_check
435                         = (flags & LZMA_TELL_UNSUPPORTED_CHECK) != 0;
436         next->coder->tell_any_check = (flags & LZMA_TELL_ANY_CHECK) != 0;
437         next->coder->concatenated = (flags & LZMA_CONCATENATED) != 0;
438         next->coder->first_stream = true;
439
440         return stream_decoder_reset(next->coder, allocator);
441 }
442
443
444 extern LZMA_API lzma_ret
445 lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
446 {
447         lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags);
448
449         strm->internal->supported_actions[LZMA_RUN] = true;
450         strm->internal->supported_actions[LZMA_FINISH] = true;
451
452         return LZMA_OK;
453 }