]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/block_decoder.c
Imported to git.
[icculus/xz.git] / src / liblzma / common / block_decoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       block_decoder.c
4 /// \brief      Decodes .lzma Blocks
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 "block_decoder.h"
21 #include "block_private.h"
22 #include "raw_decoder.h"
23 #include "check.h"
24
25
26 struct lzma_coder_s {
27         enum {
28                 SEQ_CODE,
29                 SEQ_CHECK,
30                 SEQ_UNCOMPRESSED_SIZE,
31                 SEQ_BACKWARD_SIZE,
32                 SEQ_PADDING,
33                 SEQ_END,
34         } sequence;
35
36         /// The filters in the chain; initialized with lzma_raw_decoder_init().
37         lzma_next_coder next;
38
39         /// Decoding options; we also write Total Size, Compressed Size, and
40         /// Uncompressed Size back to this structure when the encoding has
41         /// been finished.
42         lzma_options_block *options;
43
44         /// Position in variable-length integers (and in some other places).
45         size_t pos;
46
47         /// Check of the uncompressed data
48         lzma_check check;
49
50         /// Total Size calculated while encoding
51         lzma_vli total_size;
52
53         /// Compressed Size calculated while encoding
54         lzma_vli compressed_size;
55
56         /// Uncompressed Size calculated while encoding
57         lzma_vli uncompressed_size;
58
59         /// Maximum allowed total_size
60         lzma_vli total_limit;
61
62         /// Maximum allowed uncompressed_size
63         lzma_vli uncompressed_limit;
64
65         /// Temporary location for the Uncompressed Size and Backward Size
66         /// fields in Block Footer.
67         lzma_vli tmp;
68
69         /// Size of the Backward Size field - This is needed so that we
70         /// can verify the Backward Size and still keep updating total_size.
71         size_t size_of_backward_size;
72 };
73
74
75 static lzma_ret
76 update_sequence(lzma_coder *coder)
77 {
78         switch (coder->sequence) {
79         case SEQ_CODE:
80                 if (coder->options->check != LZMA_CHECK_NONE) {
81                         lzma_check_finish(&coder->check,
82                                         coder->options->check);
83                         coder->sequence = SEQ_CHECK;
84                         break;
85                 }
86
87         // Fall through
88
89         case SEQ_CHECK:
90                 if (coder->options->has_uncompressed_size_in_footer) {
91                         coder->sequence = SEQ_UNCOMPRESSED_SIZE;
92                         break;
93                 }
94
95         // Fall through
96
97         case SEQ_UNCOMPRESSED_SIZE:
98                 if (coder->options->has_backward_size) {
99                         coder->sequence = SEQ_BACKWARD_SIZE;
100                         break;
101                 }
102
103         // Fall through
104
105         case SEQ_BACKWARD_SIZE:
106                 if (coder->options->handle_padding) {
107                         coder->sequence = SEQ_PADDING;
108                         break;
109                 }
110
111         case SEQ_PADDING:
112                 if (!is_size_valid(coder->total_size,
113                                         coder->options->total_size)
114                                 || !is_size_valid(coder->compressed_size,
115                                         coder->options->compressed_size)
116                                 || !is_size_valid(coder->uncompressed_size,
117                                         coder->options->uncompressed_size))
118                         return LZMA_DATA_ERROR;
119
120                 // Copy the values into coder->options. The caller
121                 // may use this information to construct Index.
122                 coder->options->total_size = coder->total_size;
123                 coder->options->compressed_size = coder->compressed_size;
124                 coder->options->uncompressed_size = coder->uncompressed_size;
125
126                 return LZMA_STREAM_END;
127
128         default:
129                 assert(0);
130                 return LZMA_PROG_ERROR;
131         }
132
133         return LZMA_OK;
134 }
135
136
137 static lzma_ret
138 block_decode(lzma_coder *coder, lzma_allocator *allocator,
139                 const uint8_t *restrict in, size_t *restrict in_pos,
140                 size_t in_size, uint8_t *restrict out,
141                 size_t *restrict out_pos, size_t out_size, lzma_action action)
142 {
143         // Special case when the Block has only Block Header.
144         if (coder->sequence == SEQ_END)
145                 return LZMA_STREAM_END;
146
147         // FIXME: Termination condition should work but could be cleaner.
148         while (*out_pos < out_size && (*in_pos < in_size
149                         || coder->sequence == SEQ_CODE))
150         switch (coder->sequence) {
151         case SEQ_CODE: {
152                 const size_t in_start = *in_pos;
153                 const size_t out_start = *out_pos;
154
155                 lzma_ret ret = coder->next.code(coder->next.coder,
156                                 allocator, in, in_pos, in_size,
157                                 out, out_pos, out_size, action);
158
159                 const size_t in_used = *in_pos - in_start;
160                 const size_t out_used = *out_pos - out_start;
161
162                 if (update_size(&coder->total_size, in_used,
163                                         coder->total_limit)
164                                 || update_size(&coder->compressed_size,
165                                         in_used,
166                                         coder->options->compressed_size)
167                                 || update_size(&coder->uncompressed_size,
168                                         out_used, coder->uncompressed_limit))
169                         return LZMA_DATA_ERROR;
170
171                 lzma_check_update(&coder->check, coder->options->check,
172                                 out + out_start, out_used);
173
174                 if (ret != LZMA_STREAM_END)
175                         return ret;
176
177                 ret = update_sequence(coder);
178                 if (ret != LZMA_OK)
179                         return ret;
180
181                 break;
182         }
183
184         case SEQ_CHECK:
185                 switch (coder->options->check) {
186                 case LZMA_CHECK_CRC32:
187                         if (((coder->check.crc32 >> (coder->pos * 8))
188                                         & 0xFF) != in[*in_pos])
189                                 return LZMA_DATA_ERROR;
190                         break;
191
192                 case LZMA_CHECK_CRC64:
193                         if (((coder->check.crc64 >> (coder->pos * 8))
194                                         & 0xFF) != in[*in_pos])
195                                 return LZMA_DATA_ERROR;
196                         break;
197
198                 case LZMA_CHECK_SHA256:
199                         if (coder->check.sha256.buffer[coder->pos]
200                                         != in[*in_pos])
201                                 return LZMA_DATA_ERROR;
202                         break;
203
204                 default:
205                         assert(coder->options->check != LZMA_CHECK_NONE);
206                         assert(coder->options->check <= LZMA_CHECK_ID_MAX);
207                         break;
208                 }
209
210                 if (update_size(&coder->total_size, 1, coder->total_limit))
211                         return LZMA_DATA_ERROR;
212
213                 ++*in_pos;
214
215                 if (++coder->pos == lzma_check_sizes[coder->options->check]) {
216                         const lzma_ret ret = update_sequence(coder);
217                         if (ret != LZMA_OK)
218                                 return ret;
219
220                         coder->pos = 0;
221                 }
222
223                 break;
224
225         case SEQ_UNCOMPRESSED_SIZE: {
226                 const size_t in_start = *in_pos;
227
228                 lzma_ret ret = lzma_vli_decode(&coder->tmp,
229                                 &coder->pos, in, in_pos, in_size);
230
231                 if (update_size(&coder->total_size, *in_pos - in_start,
232                                 coder->total_limit))
233                         return LZMA_DATA_ERROR;
234
235                 if (ret != LZMA_STREAM_END)
236                         return ret;
237
238                 if (coder->tmp != coder->uncompressed_size)
239                         return LZMA_DATA_ERROR;
240
241                 coder->pos = 0;
242                 coder->tmp = 0;
243
244                 ret = update_sequence(coder);
245                 if (ret != LZMA_OK)
246                         return ret;
247
248                 break;
249         }
250
251         case SEQ_BACKWARD_SIZE: {
252                 const size_t in_start = *in_pos;
253
254                 lzma_ret ret = lzma_vli_decode(&coder->tmp,
255                                 &coder->pos, in, in_pos, in_size);
256
257                 const size_t in_used = *in_pos - in_start;
258
259                 if (update_size(&coder->total_size, in_used,
260                                 coder->total_limit))
261                         return LZMA_DATA_ERROR;
262
263                 coder->size_of_backward_size += in_used;
264
265                 if (ret != LZMA_STREAM_END)
266                         return ret;
267
268                 if (coder->tmp != coder->total_size
269                                 - coder->size_of_backward_size)
270                         return LZMA_DATA_ERROR;
271
272                 ret = update_sequence(coder);
273                 if (ret != LZMA_OK)
274                         return ret;
275
276                 break;
277         }
278
279         case SEQ_PADDING:
280                 if (in[*in_pos] == 0x00) {
281                         if (update_size(&coder->total_size, 1,
282                                         coder->total_limit))
283                                 return LZMA_DATA_ERROR;
284
285                         ++*in_pos;
286                         break;
287                 }
288
289                 return update_sequence(coder);
290
291         default:
292                 return LZMA_PROG_ERROR;
293         }
294
295         return LZMA_OK;
296 }
297
298
299 static void
300 block_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
301 {
302         lzma_next_coder_end(&coder->next, allocator);
303         lzma_free(coder, allocator);
304         return;
305 }
306
307
308 extern lzma_ret
309 lzma_block_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
310                 lzma_options_block *options)
311 {
312         // This is pretty similar to lzma_block_encoder_init().
313         // See comments there.
314
315         if (next->coder == NULL) {
316                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
317                 if (next->coder == NULL)
318                         return LZMA_MEM_ERROR;
319
320                 next->code = &block_decode;
321                 next->end = &block_decoder_end;
322                 next->coder->next = LZMA_NEXT_CODER_INIT;
323         }
324
325         if (!lzma_vli_is_valid(options->total_size)
326                         || !lzma_vli_is_valid(options->compressed_size)
327                         || !lzma_vli_is_valid(options->uncompressed_size)
328                         || !lzma_vli_is_valid(options->total_size)
329                         || !lzma_vli_is_valid(options->total_limit)
330                         || !lzma_vli_is_valid(options->uncompressed_limit)
331                         || (options->uncompressed_size
332                                         != LZMA_VLI_VALUE_UNKNOWN
333                                 && options->uncompressed_size
334                                         > options->uncompressed_limit)
335                         || (options->total_size != LZMA_VLI_VALUE_UNKNOWN
336                                 && options->total_size
337                                         > options->total_limit)
338                         || (!options->has_eopm && options->uncompressed_size
339                                 == LZMA_VLI_VALUE_UNKNOWN)
340                         || options->header_size > options->total_size
341                         || (options->handle_padding
342                                 && (options->has_uncompressed_size_in_footer
343                                         || options->has_backward_size)))
344                 return LZMA_PROG_ERROR;
345
346         {
347                 const lzma_ret ret = lzma_check_init(
348                                 &next->coder->check, options->check);
349                 if (ret != LZMA_OK)
350                         return ret;
351         }
352
353         if (!options->has_eopm && options->uncompressed_size == 0) {
354                 if (!is_size_valid(0, options->compressed_size))
355                         return LZMA_PROG_ERROR;
356
357                 if (options->check != LZMA_CHECK_NONE) {
358                         lzma_check_finish(&next->coder->check, options->check);
359                         next->coder->sequence = SEQ_CHECK;
360                 } else if (options->handle_padding) {
361                         next->coder->sequence = SEQ_PADDING;
362                 } else {
363                         next->coder->sequence = SEQ_END;
364                 }
365         } else {
366                 next->coder->sequence = SEQ_CODE;
367         }
368
369         {
370                 const lzma_ret ret = lzma_raw_decoder_init(
371                                 &next->coder->next, allocator,
372                                 options->filters, options->has_eopm
373                                         ? LZMA_VLI_VALUE_UNKNOWN
374                                         : options->uncompressed_size,
375                                 true);
376                 if (ret != LZMA_OK)
377                         return ret;
378         }
379
380         next->coder->options = options;
381         next->coder->pos = 0;
382         next->coder->total_size = options->header_size;
383         next->coder->compressed_size = 0;
384         next->coder->uncompressed_size = 0;
385         next->coder->total_limit
386                         = MIN(options->total_size, options->total_limit);
387         next->coder->uncompressed_limit = MIN(options->uncompressed_size,
388                         options->uncompressed_limit);
389         next->coder->tmp = 0;
390         next->coder->size_of_backward_size = 0;
391
392         return LZMA_OK;
393 }
394
395
396 extern LZMA_API lzma_ret
397 lzma_block_decoder(lzma_stream *strm, lzma_options_block *options)
398 {
399         lzma_next_strm_init(strm, lzma_block_decoder_init, options);
400
401         strm->internal->supported_actions[LZMA_RUN] = true;
402         strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
403
404         return LZMA_OK;
405 }