]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/stream_decoder.c
Stream decoder cleanups
[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 "stream_flags_common.h"
22 #include "check.h"
23 #include "stream_flags_decoder.h"
24 #include "block_decoder.h"
25
26
27 struct lzma_coder_s {
28         enum {
29                 SEQ_STREAM_HEADER,
30                 SEQ_BLOCK_HEADER,
31                 SEQ_BLOCK,
32                 SEQ_INDEX,
33                 SEQ_STREAM_FOOTER,
34                 SEQ_STREAM_PADDING,
35         } sequence;
36
37         /// Block or Metadata decoder. This takes little memory and the same
38         /// data structure can be used to decode every Block Header, so it's
39         /// a good idea to have a separate lzma_next_coder structure for it.
40         lzma_next_coder block_decoder;
41
42         /// Block options decoded by the Block Header decoder and used by
43         /// the Block decoder.
44         lzma_block block_options;
45
46         /// Stream Flags from Stream Header
47         lzma_stream_flags stream_flags;
48
49         /// Index is hashed so that it can be compared to the sizes of Blocks
50         /// with O(1) memory usage.
51         lzma_index_hash *index_hash;
52
53         /// Memory usage limit
54         uint64_t memlimit;
55
56         /// If true, LZMA_NO_CHECK is returned if the Stream has
57         /// no integrity check.
58         bool warn_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 warn_unsupported_check;
63
64         /// If true, LZMA_SEE_CHECK is returned after decoding Stream Header.
65         bool tell_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                 // Copy the type of the Check so that Block Header and Block
135                 // decoders see it.
136                 coder->block_options.check = coder->stream_flags.check;
137
138                 // Even if we return LZMA_*_CHECK below, we want
139                 // to continue from Block Header decoding.
140                 coder->sequence = SEQ_BLOCK_HEADER;
141
142                 // Detect if there's no integrity check or if it is
143                 // unsupported if those were requested by the application.
144                 if (coder->warn_no_check && coder->stream_flags.check
145                                 == LZMA_CHECK_NONE)
146                         return LZMA_NO_CHECK;
147
148                 if (coder->warn_unsupported_check
149                                 && !lzma_check_is_supported(
150                                         coder->stream_flags.check))
151                         return LZMA_UNSUPPORTED_CHECK;
152
153                 if (coder->tell_check)
154                         return LZMA_SEE_CHECK;
155         }
156
157         // Fall through
158
159         case SEQ_BLOCK_HEADER: {
160                 if (*in_pos >= in_size)
161                         return LZMA_OK;
162
163                 if (coder->pos == 0) {
164                         // Detect if it's Index.
165                         if (in[*in_pos] == 0x00) {
166                                 coder->sequence = SEQ_INDEX;
167                                 break;
168                         }
169
170                         // Calculate the size of the Block Header. Note that
171                         // Block Header decoder wants to see this byte too
172                         // so don't advance *in_pos.
173                         coder->block_options.header_size
174                                         = lzma_block_header_size_decode(
175                                                 in[*in_pos]);
176                 }
177
178                 // Copy the Block Header to the internal buffer.
179                 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
180                                 coder->block_options.header_size);
181
182                 // Return if we didn't get the whole Block Header yet.
183                 if (coder->pos < coder->block_options.header_size)
184                         return LZMA_OK;
185
186                 coder->pos = 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_BLOCK_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_memusage_decoder(filters);
200                 lzma_ret ret;
201
202                 if (memusage == UINT64_MAX) {
203                         // One or more unknown Filter IDs.
204                         ret = LZMA_HEADER_ERROR;
205                 } else if (memusage > coder->memlimit) {
206                         // The chain would need too much memory.
207                         ret = LZMA_MEMLIMIT_ERROR;
208                 } else {
209                         // Memory usage is OK. Initialize the Block decoder.
210                         ret = lzma_block_decoder_init(
211                                         &coder->block_decoder,
212                                         allocator, &coder->block_options);
213                 }
214
215                 // Free the allocated filter options since they are needed
216                 // only to initialize the Block decoder.
217                 for (size_t i = 0; i < LZMA_BLOCK_FILTERS_MAX; ++i)
218                         lzma_free(filters[i].options, allocator);
219
220                 coder->block_options.filters = NULL;
221
222                 // Check if memory usage calculation and Block enocoder
223                 // initialization succeeded.
224                 if (ret != LZMA_OK)
225                         return ret;
226
227                 coder->sequence = SEQ_BLOCK;
228         }
229
230         // Fall through
231
232         case SEQ_BLOCK: {
233                 const lzma_ret ret = coder->block_decoder.code(
234                                 coder->block_decoder.coder, allocator,
235                                 in, in_pos, in_size, out, out_pos, out_size,
236                                 action);
237
238                 if (ret != LZMA_STREAM_END)
239                         return ret;
240
241                 // Block decoded successfully. Add the new size pair to
242                 // the Index hash.
243                 return_if_error(lzma_index_hash_append(coder->index_hash,
244                                 lzma_block_total_size_get(
245                                         &coder->block_options),
246                                 coder->block_options.uncompressed_size));
247
248                 coder->sequence = SEQ_BLOCK_HEADER;
249                 break;
250         }
251
252         case SEQ_INDEX: {
253                 // If we don't have any input, don't call
254                 // lzma_index_hash_decode() since it would return
255                 // LZMA_BUF_ERROR, which we must not do here.
256                 if (*in_pos >= in_size)
257                         return LZMA_OK;
258
259                 // Decode the Index and compare it to the hash calculated
260                 // from the sizes of the Blocks (if any).
261                 const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
262                                 in, in_pos, in_size);
263                 if (ret != LZMA_STREAM_END)
264                         return ret;
265
266                 coder->sequence = SEQ_STREAM_FOOTER;
267         }
268
269         // Fall through
270
271         case SEQ_STREAM_FOOTER:
272                 // Copy the Stream Footer to the internal buffer.
273                 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
274                                 LZMA_STREAM_HEADER_SIZE);
275
276                 // Return if we didn't get the whole Stream Footer yet.
277                 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
278                         return LZMA_OK;
279
280                 coder->pos = 0;
281
282                 // Decode the Stream Footer. The decoder gives
283                 // LZMA_FORMAT_ERROR if the magic bytes don't match,
284                 // so convert that return code to LZMA_DATA_ERROR.
285                 lzma_stream_flags footer_flags;
286                 const lzma_ret ret = lzma_stream_footer_decode(
287                                 &footer_flags, coder->buffer);
288                 if (ret != LZMA_OK)
289                         return ret == LZMA_FORMAT_ERROR
290                                         ? LZMA_DATA_ERROR : ret;
291
292                 // Check that Index Size stored in the Stream Footer matches
293                 // the real size of the Index field.
294                 if (lzma_index_hash_size(coder->index_hash)
295                                 != footer_flags.backward_size)
296                         return LZMA_DATA_ERROR;
297
298                 // Compare that the Stream Flags fields are identical in
299                 // both Stream Header and Stream Footer.
300                 if (!lzma_stream_flags_equal(&coder->stream_flags,
301                                 &footer_flags))
302                         return LZMA_DATA_ERROR;
303
304                 if (!coder->concatenated)
305                         return LZMA_STREAM_END;
306
307                 coder->sequence = SEQ_STREAM_PADDING;
308
309         // Fall through
310
311         case SEQ_STREAM_PADDING:
312                 assert(coder->concatenated);
313
314                 // Skip over possible Stream Padding.
315                 while (true) {
316                         if (*in_pos >= in_size) {
317                                 // Unless LZMA_FINISH was used, we cannot
318                                 // know if there's more input coming later.
319                                 if (action != LZMA_FINISH)
320                                         return LZMA_OK;
321
322                                 // Stream Padding must be a multiple of
323                                 // four bytes.
324                                 return coder->pos == 0
325                                                 ? LZMA_STREAM_END
326                                                 : LZMA_DATA_ERROR;
327                         }
328
329                         // If the byte is not zero, it probably indicates
330                         // beginning of a new Stream (or the file is corrupt).
331                         if (in[*in_pos] != 0x00)
332                                 break;
333
334                         ++*in_pos;
335                         coder->pos = (coder->pos + 1) & 3;
336                 }
337
338                 // Stream Padding must be a multiple of four bytes (empty
339                 // Stream Padding is OK).
340                 if (coder->pos != 0) {
341                         ++*in_pos;
342                         return LZMA_DATA_ERROR;
343                 }
344
345                 // Prepare to decode the next Stream.
346                 return_if_error(stream_decoder_reset(coder, allocator));
347                 break;
348
349         default:
350                 assert(0);
351                 return LZMA_PROG_ERROR;
352         }
353
354         return LZMA_OK;
355 }
356
357
358 static void
359 stream_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
360 {
361         lzma_next_end(&coder->block_decoder, allocator);
362         lzma_index_hash_end(coder->index_hash, allocator);
363         lzma_free(coder, allocator);
364         return;
365 }
366
367
368 static lzma_check
369 stream_decoder_see_check(const lzma_coder *coder)
370 {
371         return coder->stream_flags.check;
372 }
373
374
375 extern lzma_ret
376 lzma_stream_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
377                 uint64_t memlimit, uint32_t flags)
378 {
379         lzma_next_coder_init(lzma_stream_decoder_init, next, allocator);
380
381         if (flags & ~LZMA_SUPPORTED_FLAGS)
382                 return LZMA_HEADER_ERROR;
383
384         if (next->coder == NULL) {
385                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
386                 if (next->coder == NULL)
387                         return LZMA_MEM_ERROR;
388
389                 next->code = &stream_decode;
390                 next->end = &stream_decoder_end;
391                 next->see_check = &stream_decoder_see_check;
392
393                 next->coder->block_decoder = LZMA_NEXT_CODER_INIT;
394                 next->coder->index_hash = NULL;
395         }
396
397         next->coder->memlimit = memlimit;
398         next->coder->warn_no_check = (flags & LZMA_WARN_NO_CHECK) != 0;
399         next->coder->warn_unsupported_check
400                         = (flags & LZMA_WARN_UNSUPPORTED_CHECK) != 0;
401         next->coder->tell_check = (flags & LZMA_TELL_CHECK) != 0;
402         next->coder->concatenated
403                         = (flags & LZMA_CONCATENATED) != 0;
404
405         return stream_decoder_reset(next->coder, allocator);
406 }
407
408
409 extern LZMA_API lzma_ret
410 lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
411 {
412         lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags);
413
414         strm->internal->supported_actions[LZMA_RUN] = true;
415         strm->internal->supported_actions[LZMA_FINISH] = true;
416
417         return LZMA_OK;
418 }