]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/stream_decoder.c
Sort of garbage collection commit. :-| Many things are still
[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. FIXME
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                 break;
157         }
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                 break;
229         }
230
231         case SEQ_BLOCK: {
232                 const lzma_ret ret = coder->block_decoder.code(
233                                 coder->block_decoder.coder, allocator,
234                                 in, in_pos, in_size, out, out_pos, out_size,
235                                 action);
236
237                 if (ret != LZMA_STREAM_END)
238                         return ret;
239
240                 // Block decoded successfully. Add the new size pair to
241                 // the Index hash.
242                 return_if_error(lzma_index_hash_append(coder->index_hash,
243                                 lzma_block_total_size_get(
244                                         &coder->block_options),
245                                 coder->block_options.uncompressed_size));
246
247                 coder->sequence = SEQ_BLOCK_HEADER;
248                 break;
249         }
250
251         case SEQ_INDEX: {
252                 // If we don't have any input, don't call
253                 // lzma_index_hash_decode() since it would return
254                 // LZMA_BUF_ERROR, which we must not do here.
255                 if (*in_pos >= in_size)
256                         return LZMA_OK;
257
258                 // Decode the Index and compare it to the hash calculated
259                 // from the sizes of the Blocks (if any).
260                 const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
261                                 in, in_pos, in_size);
262                 if (ret != LZMA_STREAM_END)
263                         return ret;
264
265                 coder->sequence = SEQ_STREAM_FOOTER;
266                 break;
267         }
268
269         case SEQ_STREAM_FOOTER:
270                 // Copy the Stream Footer to the internal buffer.
271                 lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
272                                 LZMA_STREAM_HEADER_SIZE);
273
274                 // Return if we didn't get the whole Stream Footer yet.
275                 if (coder->pos < LZMA_STREAM_HEADER_SIZE)
276                         return LZMA_OK;
277
278                 coder->pos = 0;
279
280                 // Decode the Stream Footer.
281                 // FIXME LZMA_FORMAT_ERROR doesn't make sense here.
282                 lzma_stream_flags footer_flags;
283                 return_if_error(lzma_stream_footer_decode(
284                                 &footer_flags, coder->buffer));
285
286                 // Check that Index Size stored in the Stream Footer matches
287                 // the real size of the Index field.
288                 if (lzma_index_hash_size(coder->index_hash)
289                                 != footer_flags.backward_size)
290                         return LZMA_DATA_ERROR;
291
292                 // Compare that the Stream Flags fields are identical in
293                 // both Stream Header and Stream Footer.
294                 if (!lzma_stream_flags_equal(&coder->stream_flags,
295                                 &footer_flags))
296                         return LZMA_DATA_ERROR;
297
298                 if (!coder->concatenated)
299                         return LZMA_STREAM_END;
300
301                 coder->sequence = SEQ_STREAM_PADDING;
302                 break;
303
304         case SEQ_STREAM_PADDING:
305                 assert(coder->concatenated);
306
307                 while (true) {
308                         if (*in_pos >= in_size) {
309                                 // Unless LZMA_FINISH was used, we cannot
310                                 // know if there's more input coming later.
311                                 if (action != LZMA_FINISH)
312                                         return LZMA_OK;
313
314                                 // Stream Padding must be a multiple of
315                                 // four bytes.
316                                 return coder->pos == 0
317                                                 ? LZMA_STREAM_END
318                                                 : LZMA_DATA_ERROR;
319                         }
320
321                         if (in[*in_pos] != 0x00) {
322                                 if (coder->pos != 0) {
323                                         // Stream Padding is not a multiple of
324                                         // four bytes.
325                                         ++*in_pos;
326                                         return LZMA_DATA_ERROR;
327                                 }
328
329                                 // Prepare to decode the next Stream.
330                                 return_if_error(stream_decoder_reset(
331                                                 coder, allocator));
332                                 break;
333                         }
334
335                         ++*in_pos;
336                         coder->pos = (coder->pos + 1) & 3;
337                 }
338
339                 break;
340
341         default:
342                 assert(0);
343                 return LZMA_PROG_ERROR;
344         }
345
346         return LZMA_OK;
347 }
348
349
350 static void
351 stream_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
352 {
353         lzma_next_end(&coder->block_decoder, allocator);
354         lzma_index_hash_end(coder->index_hash, allocator);
355         lzma_free(coder, allocator);
356         return;
357 }
358
359
360 static lzma_check
361 stream_decoder_see_check(const lzma_coder *coder)
362 {
363         return coder->stream_flags.check;
364 }
365
366
367 extern lzma_ret
368 lzma_stream_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
369                 uint64_t memlimit, uint32_t flags)
370 {
371         lzma_next_coder_init(lzma_stream_decoder_init, next, allocator);
372
373         if (flags & ~LZMA_SUPPORTED_FLAGS)
374                 return LZMA_HEADER_ERROR;
375
376         if (next->coder == NULL) {
377                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
378                 if (next->coder == NULL)
379                         return LZMA_MEM_ERROR;
380
381                 next->code = &stream_decode;
382                 next->end = &stream_decoder_end;
383                 next->see_check = &stream_decoder_see_check;
384
385                 next->coder->block_decoder = LZMA_NEXT_CODER_INIT;
386                 next->coder->index_hash = NULL;
387         }
388
389         next->coder->memlimit = memlimit;
390         next->coder->warn_no_check = (flags & LZMA_WARN_NO_CHECK) != 0;
391         next->coder->warn_unsupported_check
392                         = (flags & LZMA_WARN_UNSUPPORTED_CHECK) != 0;
393         next->coder->tell_check = (flags & LZMA_TELL_CHECK) != 0;
394         next->coder->concatenated
395                         = (flags & LZMA_CONCATENATED) != 0;
396
397         return stream_decoder_reset(next->coder, allocator);
398 }
399
400
401 extern LZMA_API lzma_ret
402 lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
403 {
404         lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags);
405
406         strm->internal->supported_actions[LZMA_RUN] = true;
407 //      strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true; // FIXME
408         strm->internal->supported_actions[LZMA_FINISH] = true;
409
410         return LZMA_OK;
411 }