]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/stream_decoder.c
Oh well, big messy commit again. Some highlights:
[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                 // If we are decoding concatenated Streams, and the later
132                 // Streams have invalid Header Magic Bytes, we give
133                 // LZMA_DATA_ERROR instead of LZMA_FORMAT_ERROR.
134                 coder->first_stream = false;
135
136                 // Copy the type of the Check so that Block Header and Block
137                 // decoders see it.
138                 coder->block_options.check = coder->stream_flags.check;
139
140                 // Even if we return LZMA_*_CHECK below, we want
141                 // to continue from Block Header decoding.
142                 coder->sequence = SEQ_BLOCK_HEADER;
143
144                 // Detect if there's no integrity check or if it is
145                 // unsupported if those were requested by the application.
146                 if (coder->tell_no_check && coder->stream_flags.check
147                                 == LZMA_CHECK_NONE)
148                         return LZMA_NO_CHECK;
149
150                 if (coder->tell_unsupported_check
151                                 && !lzma_check_is_supported(
152                                         coder->stream_flags.check))
153                         return LZMA_UNSUPPORTED_CHECK;
154
155                 if (coder->tell_any_check)
156                         return LZMA_GET_CHECK;
157         }
158
159         // Fall through
160
161         case SEQ_BLOCK_HEADER: {
162                 if (*in_pos >= in_size)
163                         return LZMA_OK;
164
165                 if (coder->pos == 0) {
166                         // Detect if it's Index.
167                         if (in[*in_pos] == 0x00) {
168                                 coder->sequence = SEQ_INDEX;
169                                 break;
170                         }
171
172                         // Calculate the size of the Block Header. Note that
173                         // Block Header decoder wants to see this byte too
174                         // so don't advance *in_pos.
175                         coder->block_options.header_size
176                                         = lzma_block_header_size_decode(
177                                                 in[*in_pos]);
178                 }
179
180                 // Copy the Block Header to the internal buffer.
181                 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
182                                 coder->block_options.header_size);
183
184                 // Return if we didn't get the whole Block Header yet.
185                 if (coder->pos < coder->block_options.header_size)
186                         return LZMA_OK;
187
188                 coder->pos = 0;
189
190                 // Set up a buffer to hold the filter chain. Block Header
191                 // decoder will initialize all members of this array so
192                 // we don't need to do it here.
193                 lzma_filter filters[LZMA_FILTERS_MAX + 1];
194                 coder->block_options.filters = filters;
195
196                 // Decode the Block Header.
197                 return_if_error(lzma_block_header_decode(&coder->block_options,
198                                 allocator, coder->buffer));
199
200                 // Check the memory usage limit.
201                 const uint64_t memusage = lzma_memusage_decoder(filters);
202                 lzma_ret ret;
203
204                 if (memusage == UINT64_MAX) {
205                         // One or more unknown Filter IDs.
206                         ret = LZMA_OPTIONS_ERROR;
207                 } else if (memusage > coder->memlimit) {
208                         // The chain would need too much memory.
209                         ret = LZMA_MEMLIMIT_ERROR;
210                 } else {
211                         // Memory usage is OK. Initialize the Block decoder.
212                         ret = lzma_block_decoder_init(
213                                         &coder->block_decoder,
214                                         allocator, &coder->block_options);
215                 }
216
217                 // Free the allocated filter options since they are needed
218                 // only to initialize the Block decoder.
219                 for (size_t i = 0; i < LZMA_FILTERS_MAX; ++i)
220                         lzma_free(filters[i].options, allocator);
221
222                 coder->block_options.filters = NULL;
223
224                 // Check if memory usage calculation and Block enocoder
225                 // initialization succeeded.
226                 if (ret != LZMA_OK)
227                         return ret;
228
229                 coder->sequence = SEQ_BLOCK;
230         }
231
232         // Fall through
233
234         case SEQ_BLOCK: {
235                 const lzma_ret ret = coder->block_decoder.code(
236                                 coder->block_decoder.coder, allocator,
237                                 in, in_pos, in_size, out, out_pos, out_size,
238                                 action);
239
240                 if (ret != LZMA_STREAM_END)
241                         return ret;
242
243                 // Block decoded successfully. Add the new size pair to
244                 // the Index hash.
245                 return_if_error(lzma_index_hash_append(coder->index_hash,
246                                 lzma_block_unpadded_size(
247                                         &coder->block_options),
248                                 coder->block_options.uncompressed_size));
249
250                 coder->sequence = SEQ_BLOCK_HEADER;
251                 break;
252         }
253
254         case SEQ_INDEX: {
255                 // If we don't have any input, don't call
256                 // lzma_index_hash_decode() since it would return
257                 // LZMA_BUF_ERROR, which we must not do here.
258                 if (*in_pos >= in_size)
259                         return LZMA_OK;
260
261                 // Decode the Index and compare it to the hash calculated
262                 // from the sizes of the Blocks (if any).
263                 const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
264                                 in, in_pos, in_size);
265                 if (ret != LZMA_STREAM_END)
266                         return ret;
267
268                 coder->sequence = SEQ_STREAM_FOOTER;
269         }
270
271         // Fall through
272
273         case SEQ_STREAM_FOOTER: {
274                 // Copy the Stream Footer to the internal buffer.
275                 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
276                                 LZMA_STREAM_HEADER_SIZE);
277
278                 // Return if we didn't get the whole Stream Footer yet.
279                 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
280                         return LZMA_OK;
281
282                 coder->pos = 0;
283
284                 // Decode the Stream Footer. The decoder gives
285                 // LZMA_FORMAT_ERROR if the magic bytes don't match,
286                 // so convert that return code to LZMA_DATA_ERROR.
287                 lzma_stream_flags footer_flags;
288                 const lzma_ret ret = lzma_stream_footer_decode(
289                                 &footer_flags, coder->buffer);
290                 if (ret != LZMA_OK)
291                         return ret == LZMA_FORMAT_ERROR
292                                         ? LZMA_DATA_ERROR : ret;
293
294                 // Check that Index Size stored in the Stream Footer matches
295                 // the real size of the Index field.
296                 if (lzma_index_hash_size(coder->index_hash)
297                                 != footer_flags.backward_size)
298                         return LZMA_DATA_ERROR;
299
300                 // Compare that the Stream Flags fields are identical in
301                 // both Stream Header and Stream Footer.
302                 return_if_error(lzma_stream_flags_compare(
303                                 &coder->stream_flags, &footer_flags));
304
305                 if (!coder->concatenated)
306                         return LZMA_STREAM_END;
307
308                 coder->sequence = SEQ_STREAM_PADDING;
309         }
310
311         // Fall through
312
313         case SEQ_STREAM_PADDING:
314                 assert(coder->concatenated);
315
316                 // Skip over possible Stream Padding.
317                 while (true) {
318                         if (*in_pos >= in_size) {
319                                 // Unless LZMA_FINISH was used, we cannot
320                                 // know if there's more input coming later.
321                                 if (action != LZMA_FINISH)
322                                         return LZMA_OK;
323
324                                 // Stream Padding must be a multiple of
325                                 // four bytes.
326                                 return coder->pos == 0
327                                                 ? LZMA_STREAM_END
328                                                 : LZMA_DATA_ERROR;
329                         }
330
331                         // If the byte is not zero, it probably indicates
332                         // beginning of a new Stream (or the file is corrupt).
333                         if (in[*in_pos] != 0x00)
334                                 break;
335
336                         ++*in_pos;
337                         coder->pos = (coder->pos + 1) & 3;
338                 }
339
340                 // Stream Padding must be a multiple of four bytes (empty
341                 // Stream Padding is OK).
342                 if (coder->pos != 0) {
343                         ++*in_pos;
344                         return LZMA_DATA_ERROR;
345                 }
346
347                 // Prepare to decode the next Stream.
348                 return_if_error(stream_decoder_reset(coder, allocator));
349                 break;
350
351         default:
352                 assert(0);
353                 return LZMA_PROG_ERROR;
354         }
355
356         return LZMA_OK;
357 }
358
359
360 static void
361 stream_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
362 {
363         lzma_next_end(&coder->block_decoder, allocator);
364         lzma_index_hash_end(coder->index_hash, allocator);
365         lzma_free(coder, allocator);
366         return;
367 }
368
369
370 static lzma_check
371 stream_decoder_get_check(const lzma_coder *coder)
372 {
373         return coder->stream_flags.check;
374 }
375
376
377 extern lzma_ret
378 lzma_stream_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
379                 uint64_t memlimit, uint32_t flags)
380 {
381         lzma_next_coder_init(lzma_stream_decoder_init, next, allocator);
382
383         if (flags & ~LZMA_SUPPORTED_FLAGS)
384                 return LZMA_OPTIONS_ERROR;
385
386         if (next->coder == NULL) {
387                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
388                 if (next->coder == NULL)
389                         return LZMA_MEM_ERROR;
390
391                 next->code = &stream_decode;
392                 next->end = &stream_decoder_end;
393                 next->get_check = &stream_decoder_get_check;
394
395                 next->coder->block_decoder = LZMA_NEXT_CODER_INIT;
396                 next->coder->index_hash = NULL;
397         }
398
399         next->coder->memlimit = memlimit;
400         next->coder->tell_no_check = (flags & LZMA_TELL_NO_CHECK) != 0;
401         next->coder->tell_unsupported_check
402                         = (flags & LZMA_TELL_UNSUPPORTED_CHECK) != 0;
403         next->coder->tell_any_check = (flags & LZMA_TELL_ANY_CHECK) != 0;
404         next->coder->concatenated = (flags & LZMA_CONCATENATED) != 0;
405         next->coder->first_stream = true;
406
407         return stream_decoder_reset(next->coder, allocator);
408 }
409
410
411 extern LZMA_API lzma_ret
412 lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
413 {
414         lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags);
415
416         strm->internal->supported_actions[LZMA_RUN] = true;
417         strm->internal->supported_actions[LZMA_FINISH] = true;
418
419         return LZMA_OK;
420 }