]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/stream_decoder.c
796a7fd4f8cac82f1e56e00442f180926ea512f9
[icculus/xz.git] / src / liblzma / common / stream_decoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       stream_decoder.c
4 /// \brief      Decodes .xz 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->pos = 0;
100
101         return LZMA_OK;
102 }
103
104
105 static lzma_ret
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)
110 {
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.
113         while (true)
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);
119
120                 // Return if we didn't get the whole Stream Header yet.
121                 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
122                         return LZMA_OK;
123
124                 coder->pos = 0;
125
126                 // Decode the Stream Header.
127                 const lzma_ret ret = lzma_stream_header_decode(
128                                 &coder->stream_flags, coder->buffer);
129                 if (ret != LZMA_OK)
130                         return ret == LZMA_FORMAT_ERROR && !coder->first_stream
131                                         ? LZMA_DATA_ERROR : ret;
132
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;
137
138                 // Copy the type of the Check so that Block Header and Block
139                 // decoders see it.
140                 coder->block_options.check = coder->stream_flags.check;
141
142                 // Even if we return LZMA_*_CHECK below, we want
143                 // to continue from Block Header decoding.
144                 coder->sequence = SEQ_BLOCK_HEADER;
145
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
149                                 == LZMA_CHECK_NONE)
150                         return LZMA_NO_CHECK;
151
152                 if (coder->tell_unsupported_check
153                                 && !lzma_check_is_supported(
154                                         coder->stream_flags.check))
155                         return LZMA_UNSUPPORTED_CHECK;
156
157                 if (coder->tell_any_check)
158                         return LZMA_GET_CHECK;
159         }
160
161         // Fall through
162
163         case SEQ_BLOCK_HEADER: {
164                 if (*in_pos >= in_size)
165                         return LZMA_OK;
166
167                 if (coder->pos == 0) {
168                         // Detect if it's Index.
169                         if (in[*in_pos] == 0x00) {
170                                 coder->sequence = SEQ_INDEX;
171                                 break;
172                         }
173
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(
179                                                 in[*in_pos]);
180                 }
181
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);
185
186                 // Return if we didn't get the whole Block Header yet.
187                 if (coder->pos < coder->block_options.header_size)
188                         return LZMA_OK;
189
190                 coder->pos = 0;
191
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;
197
198                 // Decode the Block Header.
199                 return_if_error(lzma_block_header_decode(&coder->block_options,
200                                 allocator, coder->buffer));
201
202                 // Check the memory usage limit.
203                 const uint64_t memusage = lzma_raw_decoder_memusage(filters);
204                 lzma_ret ret;
205
206                 if (memusage == UINT64_MAX) {
207                         // One or more unknown Filter IDs.
208                         ret = LZMA_OPTIONS_ERROR;
209                 } else {
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;
215
216                         if (memusage > coder->memlimit) {
217                                 // The chain would need too much memory.
218                                 ret = LZMA_MEMLIMIT_ERROR;
219                         } else {
220                                 // Memory usage is OK.
221                                 // Initialize the Block decoder.
222                                 ret = lzma_block_decoder_init(
223                                                 &coder->block_decoder,
224                                                 allocator,
225                                                 &coder->block_options);
226                         }
227                 }
228
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);
233
234                 coder->block_options.filters = NULL;
235
236                 // Check if memory usage calculation and Block enocoder
237                 // initialization succeeded.
238                 if (ret != LZMA_OK)
239                         return ret;
240
241                 coder->sequence = SEQ_BLOCK;
242         }
243
244         // Fall through
245
246         case 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,
250                                 action);
251
252                 if (ret != LZMA_STREAM_END)
253                         return ret;
254
255                 // Block decoded successfully. Add the new size pair to
256                 // the Index hash.
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));
261
262                 coder->sequence = SEQ_BLOCK_HEADER;
263                 break;
264         }
265
266         case SEQ_INDEX: {
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)
271                         return LZMA_OK;
272
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)
278                         return ret;
279
280                 coder->sequence = SEQ_STREAM_FOOTER;
281         }
282
283         // Fall through
284
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);
289
290                 // Return if we didn't get the whole Stream Footer yet.
291                 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
292                         return LZMA_OK;
293
294                 coder->pos = 0;
295
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);
302                 if (ret != LZMA_OK)
303                         return ret == LZMA_FORMAT_ERROR
304                                         ? LZMA_DATA_ERROR : ret;
305
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;
311
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));
316
317                 if (!coder->concatenated)
318                         return LZMA_STREAM_END;
319
320                 coder->sequence = SEQ_STREAM_PADDING;
321         }
322
323         // Fall through
324
325         case SEQ_STREAM_PADDING:
326                 assert(coder->concatenated);
327
328                 // Skip over possible Stream Padding.
329                 while (true) {
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)
334                                         return LZMA_OK;
335
336                                 // Stream Padding must be a multiple of
337                                 // four bytes.
338                                 return coder->pos == 0
339                                                 ? LZMA_STREAM_END
340                                                 : LZMA_DATA_ERROR;
341                         }
342
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)
346                                 break;
347
348                         ++*in_pos;
349                         coder->pos = (coder->pos + 1) & 3;
350                 }
351
352                 // Stream Padding must be a multiple of four bytes (empty
353                 // Stream Padding is OK).
354                 if (coder->pos != 0) {
355                         ++*in_pos;
356                         return LZMA_DATA_ERROR;
357                 }
358
359                 // Prepare to decode the next Stream.
360                 return_if_error(stream_decoder_reset(coder, allocator));
361                 break;
362
363         default:
364                 assert(0);
365                 return LZMA_PROG_ERROR;
366         }
367
368         return LZMA_OK;
369 }
370
371
372 static void
373 stream_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
374 {
375         lzma_next_end(&coder->block_decoder, allocator);
376         lzma_index_hash_end(coder->index_hash, allocator);
377         lzma_free(coder, allocator);
378         return;
379 }
380
381
382 static lzma_check
383 stream_decoder_get_check(const lzma_coder *coder)
384 {
385         return coder->stream_flags.check;
386 }
387
388
389 static lzma_ret
390 stream_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
391                 uint64_t *old_memlimit, uint64_t new_memlimit)
392 {
393         if (new_memlimit != 0 && new_memlimit < coder->memusage)
394                 return LZMA_MEMLIMIT_ERROR;
395
396         *memusage = coder->memusage;
397         *old_memlimit = coder->memlimit;
398         coder->memlimit = new_memlimit;
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 }