]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/stream_decoder.c
Improved the Stream Flags handling API.
[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         /// If true, LZMA_NO_CHECK is returned if the Stream has
54         /// no integrity check.
55         bool tell_no_check;
56
57         /// If true, LZMA_UNSUPPORTED_CHECK is returned if the Stream has
58         /// an integrity check that isn't supported by this liblzma build.
59         bool tell_unsupported_check;
60
61         /// If true, LZMA_GET_CHECK is returned after decoding Stream Header.
62         bool tell_any_check;
63
64         /// If true, we will decode concatenated Streams that possibly have
65         /// Stream Padding between or after them. LZMA_STREAM_END is returned
66         /// once the application isn't giving us any new input, and we aren't
67         /// in the middle of a Stream, and possible Stream Padding is a
68         /// multiple of four bytes.
69         bool concatenated;
70
71         /// When decoding concatenated Streams, this is true as long as we
72         /// are decoding the first Stream. This is needed to avoid misleading
73         /// LZMA_FORMAT_ERROR in case the later Streams don't have valid magic
74         /// bytes.
75         bool first_stream;
76
77         /// Write position in buffer[] and position in Stream Padding
78         size_t pos;
79
80         /// Buffer to hold Stream Header, Block Header, and Stream Footer.
81         /// Block Header has biggest maximum size.
82         uint8_t buffer[LZMA_BLOCK_HEADER_SIZE_MAX];
83 };
84
85
86 static lzma_ret
87 stream_decoder_reset(lzma_coder *coder, lzma_allocator *allocator)
88 {
89         // Initialize the Index hash used to verify the Index.
90         coder->index_hash = lzma_index_hash_init(coder->index_hash, allocator);
91         if (coder->index_hash == NULL)
92                 return LZMA_MEM_ERROR;
93
94         // Reset the rest of the variables.
95         coder->sequence = SEQ_STREAM_HEADER;
96         coder->block_options.filters = NULL;
97         coder->pos = 0;
98
99         return LZMA_OK;
100 }
101
102
103 static lzma_ret
104 stream_decode(lzma_coder *coder, lzma_allocator *allocator,
105                 const uint8_t *restrict in, size_t *restrict in_pos,
106                 size_t in_size, uint8_t *restrict out,
107                 size_t *restrict out_pos, size_t out_size, lzma_action action)
108 {
109         // When decoding the actual Block, it may be able to produce more
110         // output even if we don't give it any new input.
111         while (true)
112         switch (coder->sequence) {
113         case SEQ_STREAM_HEADER: {
114                 // Copy the Stream Header to the internal buffer.
115                 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
116                                 LZMA_STREAM_HEADER_SIZE);
117
118                 // Return if we didn't get the whole Stream Header yet.
119                 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
120                         return LZMA_OK;
121
122                 coder->pos = 0;
123
124                 // Decode the Stream Header.
125                 const lzma_ret ret = lzma_stream_header_decode(
126                                 &coder->stream_flags, coder->buffer);
127                 if (ret != LZMA_OK)
128                         return ret == LZMA_FORMAT_ERROR && !coder->first_stream
129                                         ? LZMA_DATA_ERROR : ret;
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                 // Set up a buffer to hold the filter chain. Block Header
186                 // decoder will initialize all members of this array so
187                 // we don't need to do it here.
188                 lzma_filter filters[LZMA_BLOCK_FILTERS_MAX + 1];
189                 coder->block_options.filters = filters;
190
191                 // Decode the Block Header.
192                 return_if_error(lzma_block_header_decode(&coder->block_options,
193                                 allocator, coder->buffer));
194
195                 // Check the memory usage limit.
196                 const uint64_t memusage = lzma_memusage_decoder(filters);
197                 lzma_ret ret;
198
199                 if (memusage == UINT64_MAX) {
200                         // One or more unknown Filter IDs.
201                         ret = LZMA_HEADER_ERROR;
202                 } else if (memusage > coder->memlimit) {
203                         // The chain would need too much memory.
204                         ret = LZMA_MEMLIMIT_ERROR;
205                 } else {
206                         // Memory usage is OK. Initialize the Block decoder.
207                         ret = lzma_block_decoder_init(
208                                         &coder->block_decoder,
209                                         allocator, &coder->block_options);
210                 }
211
212                 // Free the allocated filter options since they are needed
213                 // only to initialize the Block decoder.
214                 for (size_t i = 0; i < LZMA_BLOCK_FILTERS_MAX; ++i)
215                         lzma_free(filters[i].options, allocator);
216
217                 coder->block_options.filters = NULL;
218
219                 // Check if memory usage calculation and Block enocoder
220                 // initialization succeeded.
221                 if (ret != LZMA_OK)
222                         return ret;
223
224                 coder->sequence = SEQ_BLOCK;
225         }
226
227         // Fall through
228
229         case SEQ_BLOCK: {
230                 const lzma_ret ret = coder->block_decoder.code(
231                                 coder->block_decoder.coder, allocator,
232                                 in, in_pos, in_size, out, out_pos, out_size,
233                                 action);
234
235                 if (ret != LZMA_STREAM_END)
236                         return ret;
237
238                 // Block decoded successfully. Add the new size pair to
239                 // the Index hash.
240                 return_if_error(lzma_index_hash_append(coder->index_hash,
241                                 lzma_block_total_size_get(
242                                         &coder->block_options),
243                                 coder->block_options.uncompressed_size));
244
245                 coder->sequence = SEQ_BLOCK_HEADER;
246                 break;
247         }
248
249         case SEQ_INDEX: {
250                 // If we don't have any input, don't call
251                 // lzma_index_hash_decode() since it would return
252                 // LZMA_BUF_ERROR, which we must not do here.
253                 if (*in_pos >= in_size)
254                         return LZMA_OK;
255
256                 // Decode the Index and compare it to the hash calculated
257                 // from the sizes of the Blocks (if any).
258                 const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
259                                 in, in_pos, in_size);
260                 if (ret != LZMA_STREAM_END)
261                         return ret;
262
263                 coder->sequence = SEQ_STREAM_FOOTER;
264         }
265
266         // Fall through
267
268         case SEQ_STREAM_FOOTER:
269                 // Copy the Stream Footer to the internal buffer.
270                 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
271                                 LZMA_STREAM_HEADER_SIZE);
272
273                 // Return if we didn't get the whole Stream Footer yet.
274                 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
275                         return LZMA_OK;
276
277                 coder->pos = 0;
278
279                 // Decode the Stream Footer. The decoder gives
280                 // LZMA_FORMAT_ERROR if the magic bytes don't match,
281                 // so convert that return code to LZMA_DATA_ERROR.
282                 lzma_stream_flags footer_flags;
283                 const lzma_ret ret = lzma_stream_footer_decode(
284                                 &footer_flags, coder->buffer);
285                 if (ret != LZMA_OK)
286                         return ret == LZMA_FORMAT_ERROR
287                                         ? LZMA_DATA_ERROR : ret;
288
289                 // Check that Index Size stored in the Stream Footer matches
290                 // the real size of the Index field.
291                 if (lzma_index_hash_size(coder->index_hash)
292                                 != footer_flags.backward_size)
293                         return LZMA_DATA_ERROR;
294
295                 // Compare that the Stream Flags fields are identical in
296                 // both Stream Header and Stream Footer.
297                 return_if_error(lzma_stream_flags_compare(
298                                 &coder->stream_flags, &footer_flags));
299
300                 if (!coder->concatenated)
301                         return LZMA_STREAM_END;
302
303                 coder->sequence = SEQ_STREAM_PADDING;
304
305         // Fall through
306
307         case SEQ_STREAM_PADDING:
308                 assert(coder->concatenated);
309
310                 // Skip over possible Stream Padding.
311                 while (true) {
312                         if (*in_pos >= in_size) {
313                                 // Unless LZMA_FINISH was used, we cannot
314                                 // know if there's more input coming later.
315                                 if (action != LZMA_FINISH)
316                                         return LZMA_OK;
317
318                                 // Stream Padding must be a multiple of
319                                 // four bytes.
320                                 return coder->pos == 0
321                                                 ? LZMA_STREAM_END
322                                                 : LZMA_DATA_ERROR;
323                         }
324
325                         // If the byte is not zero, it probably indicates
326                         // beginning of a new Stream (or the file is corrupt).
327                         if (in[*in_pos] != 0x00)
328                                 break;
329
330                         ++*in_pos;
331                         coder->pos = (coder->pos + 1) & 3;
332                 }
333
334                 // Stream Padding must be a multiple of four bytes (empty
335                 // Stream Padding is OK).
336                 if (coder->pos != 0) {
337                         ++*in_pos;
338                         return LZMA_DATA_ERROR;
339                 }
340
341                 // Prepare to decode the next Stream.
342                 return_if_error(stream_decoder_reset(coder, allocator));
343                 break;
344
345         default:
346                 assert(0);
347                 return LZMA_PROG_ERROR;
348         }
349
350         return LZMA_OK;
351 }
352
353
354 static void
355 stream_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
356 {
357         lzma_next_end(&coder->block_decoder, allocator);
358         lzma_index_hash_end(coder->index_hash, allocator);
359         lzma_free(coder, allocator);
360         return;
361 }
362
363
364 static lzma_check
365 stream_decoder_get_check(const lzma_coder *coder)
366 {
367         return coder->stream_flags.check;
368 }
369
370
371 extern lzma_ret
372 lzma_stream_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
373                 uint64_t memlimit, uint32_t flags)
374 {
375         lzma_next_coder_init(lzma_stream_decoder_init, next, allocator);
376
377         if (flags & ~LZMA_SUPPORTED_FLAGS)
378                 return LZMA_HEADER_ERROR;
379
380         if (next->coder == NULL) {
381                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
382                 if (next->coder == NULL)
383                         return LZMA_MEM_ERROR;
384
385                 next->code = &stream_decode;
386                 next->end = &stream_decoder_end;
387                 next->get_check = &stream_decoder_get_check;
388
389                 next->coder->block_decoder = LZMA_NEXT_CODER_INIT;
390                 next->coder->index_hash = NULL;
391         }
392
393         next->coder->memlimit = memlimit;
394         next->coder->tell_no_check = (flags & LZMA_TELL_NO_CHECK) != 0;
395         next->coder->tell_unsupported_check
396                         = (flags & LZMA_TELL_UNSUPPORTED_CHECK) != 0;
397         next->coder->tell_any_check = (flags & LZMA_TELL_ANY_CHECK) != 0;
398         next->coder->concatenated = (flags & LZMA_CONCATENATED) != 0;
399
400         return stream_decoder_reset(next->coder, allocator);
401 }
402
403
404 extern LZMA_API lzma_ret
405 lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
406 {
407         lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags);
408
409         strm->internal->supported_actions[LZMA_RUN] = true;
410         strm->internal->supported_actions[LZMA_FINISH] = true;
411
412         return LZMA_OK;
413 }