]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/lzma/lzma2_decoder.c
Renamed constants:
[icculus/xz.git] / src / liblzma / lzma / lzma2_decoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       lzma2_decoder.c
4 /// \brief      LZMA2 decoder
5 //
6 //  Copyright (C) 1999-2008 Igor Pavlov
7 //  Copyright (C) 2008 Lasse Collin
8 //
9 //  This library is free software; you can redistribute it and/or
10 //  modify it under the terms of the GNU Lesser General Public
11 //  License as published by the Free Software Foundation; either
12 //  version 2.1 of the License, or (at your option) any later version.
13 //
14 //  This library is distributed in the hope that it will be useful,
15 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 //  Lesser General Public License for more details.
18 //
19 ///////////////////////////////////////////////////////////////////////////////
20
21 #include "lzma2_decoder.h"
22 #include "lz_decoder.h"
23 #include "lzma_decoder.h"
24
25
26 struct lzma_coder_s {
27         enum sequence {
28                 SEQ_CONTROL,
29                 SEQ_UNCOMPRESSED_1,
30                 SEQ_UNCOMPRESSED_2,
31                 SEQ_COMPRESSED_0,
32                 SEQ_COMPRESSED_1,
33                 SEQ_PROPERTIES,
34                 SEQ_LZMA,
35                 SEQ_COPY,
36         } sequence;
37
38         /// Sequence after the size fields have been decoded.
39         enum sequence next_sequence;
40
41         /// LZMA decoder
42         lzma_lz_decoder lzma;
43
44         /// Uncompressed size of LZMA chunk
45         size_t uncompressed_size;
46
47         /// Compressed size of the chunk (naturally equals to uncompressed
48         /// size of uncompressed chunk)
49         size_t compressed_size;
50
51         /// True if properties are needed. This is false before the
52         /// first LZMA chunk.
53         bool need_properties;
54
55         /// True if dictionary reset is needed. This is false before the
56         /// first chunk (LZMA or uncompressed).
57         bool need_dictionary_reset;
58
59         lzma_options_lzma options;
60 };
61
62
63 static lzma_ret
64 lzma2_decode(lzma_coder *restrict coder, lzma_dict *restrict dict,
65                 const uint8_t *restrict in, size_t *restrict in_pos,
66                 size_t in_size)
67 {
68         // With SEQ_LZMA it is possible that no new input is needed to do
69         // some progress. The rest of the sequences assume that there is
70         // at least one byte of input.
71         while (*in_pos < in_size || coder->sequence == SEQ_LZMA)
72         switch (coder->sequence) {
73         case SEQ_CONTROL:
74                 if (in[*in_pos] & 0x80) {
75                         // Get the highest five bits of uncompressed size.
76                         coder->uncompressed_size
77                                         = (uint32_t)(in[*in_pos] & 0x1F) << 16;
78                         coder->sequence = SEQ_UNCOMPRESSED_1;
79
80                         // See if we need to reset dictionary or state.
81                         switch ((in[(*in_pos)++] >> 5) & 3) {
82                         case 3:
83                                 dict_reset(dict);
84                                 coder->need_dictionary_reset = false;
85
86                         // Fall through
87
88                         case 2:
89                                 if (coder->need_dictionary_reset)
90                                         return LZMA_DATA_ERROR;
91
92                                 coder->need_properties = false;
93                                 coder->next_sequence = SEQ_PROPERTIES;
94                                 break;
95
96                         case 1:
97                                 if (coder->need_properties)
98                                         return LZMA_DATA_ERROR;
99
100                                 coder->lzma.reset(coder->lzma.coder,
101                                                 &coder->options);
102
103                                 coder->next_sequence = SEQ_LZMA;
104                                 break;
105
106                         case 0:
107                                 if (coder->need_properties)
108                                         return LZMA_DATA_ERROR;
109
110                                 coder->next_sequence = SEQ_LZMA;
111                                 break;
112                         }
113
114                 } else {
115                         switch (in[(*in_pos)++]) {
116                         case 0:
117                                 // End of payload marker
118                                 return LZMA_STREAM_END;
119
120                         case 1:
121                                 // Dictionary reset
122                                 dict_reset(dict);
123                                 coder->need_dictionary_reset = false;
124
125                         // Fall through
126
127                         case 2:
128                                 if (coder->need_dictionary_reset)
129                                         return LZMA_DATA_ERROR;
130
131                                 // Uncompressed chunk; we need to read total
132                                 // size first.
133                                 coder->sequence = SEQ_COMPRESSED_0;
134                                 coder->next_sequence = SEQ_COPY;
135                                 break;
136
137                         default:
138                                 return LZMA_DATA_ERROR;
139                         }
140                 }
141
142                 break;
143
144         case SEQ_UNCOMPRESSED_1:
145                 coder->uncompressed_size += (uint32_t)(in[(*in_pos)++]) << 8;
146                 coder->sequence = SEQ_UNCOMPRESSED_2;
147                 break;
148
149         case SEQ_UNCOMPRESSED_2:
150                 coder->uncompressed_size += in[(*in_pos)++] + 1;
151                 coder->sequence = SEQ_COMPRESSED_0;
152                 coder->lzma.set_uncompressed(coder->lzma.coder,
153                                 coder->uncompressed_size);
154                 break;
155
156         case SEQ_COMPRESSED_0:
157                 coder->compressed_size = (uint32_t)(in[(*in_pos)++]) << 8;
158                 coder->sequence = SEQ_COMPRESSED_1;
159                 break;
160
161         case SEQ_COMPRESSED_1:
162                 coder->compressed_size += in[(*in_pos)++] + 1;
163                 coder->sequence = coder->next_sequence;
164                 break;
165
166         case SEQ_PROPERTIES:
167                 if (lzma_lzma_lclppb_decode(&coder->options, in[(*in_pos)++]))
168                         return LZMA_DATA_ERROR;
169
170                 coder->lzma.reset(coder->lzma.coder, &coder->options);
171
172                 coder->sequence = SEQ_LZMA;
173                 break;
174
175         case SEQ_LZMA: {
176                 // Store the start offset so that we can update
177                 // coder->compressed_size later.
178                 const size_t in_start = *in_pos;
179
180                 // Decode from in[] to *dict.
181                 const lzma_ret ret = coder->lzma.code(coder->lzma.coder,
182                                 dict, in, in_pos, in_size);
183
184                 // Validate and update coder->compressed_size.
185                 const size_t in_used = *in_pos - in_start;
186                 if (in_used > coder->compressed_size)
187                         return LZMA_DATA_ERROR;
188
189                 coder->compressed_size -= in_used;
190
191                 // Return if we didn't finish the chunk, or an error occurred.
192                 if (ret != LZMA_STREAM_END)
193                         return ret;
194
195                 // The LZMA decoder must have consumed the whole chunk now.
196                 // We don't need to worry about uncompressed size since it
197                 // is checked by the LZMA decoder.
198                 if (coder->compressed_size != 0)
199                         return LZMA_DATA_ERROR;
200
201                 coder->sequence = SEQ_CONTROL;
202                 break;
203         }
204
205         case SEQ_COPY: {
206                 // Copy from input to the dictionary as is.
207                 // FIXME Can copy too much?
208                 dict_write(dict, in, in_pos, in_size, &coder->compressed_size);
209                 if (coder->compressed_size != 0)
210                         return LZMA_OK;
211
212                 coder->sequence = SEQ_CONTROL;
213                 break;
214         }
215
216         default:
217                 assert(0);
218                 return LZMA_PROG_ERROR;
219         }
220
221         return LZMA_OK;
222 }
223
224
225 static void
226 lzma2_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
227 {
228         assert(coder->lzma.end == NULL);
229         lzma_free(coder->lzma.coder, allocator);
230
231         lzma_free(coder, allocator);
232
233         return;
234 }
235
236
237 static lzma_ret
238 lzma2_decoder_init(lzma_lz_decoder *lz, lzma_allocator *allocator,
239                 const void *options, size_t *dict_size)
240 {
241         if (lz->coder == NULL) {
242                 lz->coder = lzma_alloc(sizeof(lzma_coder), allocator);
243                 if (lz->coder == NULL)
244                         return LZMA_MEM_ERROR;
245
246                 lz->code = &lzma2_decode;
247                 lz->end = &lzma2_decoder_end;
248
249                 lz->coder->lzma = LZMA_LZ_DECODER_INIT;
250         }
251
252         lz->coder->sequence = SEQ_CONTROL;
253         lz->coder->need_properties = true;
254         lz->coder->need_dictionary_reset = true;
255
256         return lzma_lzma_decoder_create(&lz->coder->lzma,
257                         allocator, options, dict_size);
258 }
259
260
261 extern lzma_ret
262 lzma_lzma2_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
263                 const lzma_filter_info *filters)
264 {
265         // LZMA2 can only be the last filter in the chain. This is enforced
266         // by the raw_decoder initialization.
267         assert(filters[1].init == NULL);
268
269         return lzma_lz_decoder_init(next, allocator, filters,
270                         &lzma2_decoder_init);
271 }
272
273
274 extern uint64_t
275 lzma_lzma2_decoder_memusage(const void *options)
276 {
277         const uint64_t lzma_memusage = lzma_lzma_decoder_memusage(options);
278         if (lzma_memusage == UINT64_MAX)
279                 return UINT64_MAX;
280
281         return sizeof(lzma_coder) + lzma_memusage;
282 }
283
284
285 extern lzma_ret
286 lzma_lzma2_props_decode(void **options, lzma_allocator *allocator,
287                 const uint8_t *props, size_t props_size)
288 {
289         if (props_size != 1)
290                 return LZMA_OPTIONS_ERROR;
291
292         // Check that reserved bits are unset.
293         if (props[0] & 0xC0)
294                 return LZMA_OPTIONS_ERROR;
295
296         // Decode the dictionary size.
297         if (props[0] > 40)
298                 return LZMA_OPTIONS_ERROR;
299
300         lzma_options_lzma *opt = lzma_alloc(
301                         sizeof(lzma_options_lzma), allocator);
302         if (opt == NULL)
303                 return LZMA_MEM_ERROR;
304
305         if (props[0] == 40) {
306                 opt->dictionary_size = UINT32_MAX;
307         } else {
308                 opt->dictionary_size = 2 | (props[0] & 1);
309                 opt->dictionary_size <<= props[0] / 2 + 11;
310         }
311
312         opt->preset_dictionary = NULL;
313         opt->preset_dictionary_size = 0;
314
315         *options = opt;
316
317         return LZMA_OK;
318 }