1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file lzma2_decoder.c
4 /// \brief LZMA2 decoder
6 // Authors: Igor Pavlov
9 // This file has been put into the public domain.
10 // You can do whatever you want with this file.
12 ///////////////////////////////////////////////////////////////////////////////
14 #include "lzma2_decoder.h"
15 #include "lz_decoder.h"
16 #include "lzma_decoder.h"
31 /// Sequence after the size fields have been decoded.
32 enum sequence next_sequence
;
37 /// Uncompressed size of LZMA chunk
38 size_t uncompressed_size
;
40 /// Compressed size of the chunk (naturally equals to uncompressed
41 /// size of uncompressed chunk)
42 size_t compressed_size
;
44 /// True if properties are needed. This is false before the
48 /// True if dictionary reset is needed. This is false before the
49 /// first chunk (LZMA or uncompressed).
50 bool need_dictionary_reset
;
52 lzma_options_lzma options
;
57 lzma2_decode(lzma_coder
*restrict coder
, lzma_dict
*restrict dict
,
58 const uint8_t *restrict in
, size_t *restrict in_pos
,
61 // With SEQ_LZMA it is possible that no new input is needed to do
62 // some progress. The rest of the sequences assume that there is
63 // at least one byte of input.
64 while (*in_pos
< in_size
|| coder
->sequence
== SEQ_LZMA
)
65 switch (coder
->sequence
) {
67 const uint32_t control
= in
[*in_pos
];
72 return LZMA_STREAM_END
;
74 if (control
>= 0xE0 || control
== 1) {
75 // Dictionary reset implies that next LZMA chunk has
76 // to set new properties.
77 coder
->need_properties
= true;
78 coder
->need_dictionary_reset
= true;
79 } else if (coder
->need_dictionary_reset
) {
80 return LZMA_DATA_ERROR
;
83 if (control
>= 0x80) {
84 // LZMA chunk. The highest five bits of the
85 // uncompressed size are taken from the control byte.
86 coder
->uncompressed_size
= (control
& 0x1F) << 16;
87 coder
->sequence
= SEQ_UNCOMPRESSED_1
;
89 // See if there are new properties or if we need to
91 if (control
>= 0xC0) {
92 // When there are new properties, state reset
93 // is done at SEQ_PROPERTIES.
94 coder
->need_properties
= false;
95 coder
->next_sequence
= SEQ_PROPERTIES
;
97 } else if (coder
->need_properties
) {
98 return LZMA_DATA_ERROR
;
101 coder
->next_sequence
= SEQ_LZMA
;
103 // If only state reset is wanted with old
104 // properties, do the resetting here for
107 coder
->lzma
.reset(coder
->lzma
.coder
,
111 // Invalid control values
113 return LZMA_DATA_ERROR
;
115 // It's uncompressed chunk
116 coder
->sequence
= SEQ_COMPRESSED_0
;
117 coder
->next_sequence
= SEQ_COPY
;
120 if (coder
->need_dictionary_reset
) {
121 // Finish the dictionary reset and let the caller
122 // flush the dictionary to the actual output buffer.
123 coder
->need_dictionary_reset
= false;
131 case SEQ_UNCOMPRESSED_1
:
132 coder
->uncompressed_size
+= (uint32_t)(in
[(*in_pos
)++]) << 8;
133 coder
->sequence
= SEQ_UNCOMPRESSED_2
;
136 case SEQ_UNCOMPRESSED_2
:
137 coder
->uncompressed_size
+= in
[(*in_pos
)++] + 1;
138 coder
->sequence
= SEQ_COMPRESSED_0
;
139 coder
->lzma
.set_uncompressed(coder
->lzma
.coder
,
140 coder
->uncompressed_size
);
143 case SEQ_COMPRESSED_0
:
144 coder
->compressed_size
= (uint32_t)(in
[(*in_pos
)++]) << 8;
145 coder
->sequence
= SEQ_COMPRESSED_1
;
148 case SEQ_COMPRESSED_1
:
149 coder
->compressed_size
+= in
[(*in_pos
)++] + 1;
150 coder
->sequence
= coder
->next_sequence
;
154 if (lzma_lzma_lclppb_decode(&coder
->options
, in
[(*in_pos
)++]))
155 return LZMA_DATA_ERROR
;
157 coder
->lzma
.reset(coder
->lzma
.coder
, &coder
->options
);
159 coder
->sequence
= SEQ_LZMA
;
163 // Store the start offset so that we can update
164 // coder->compressed_size later.
165 const size_t in_start
= *in_pos
;
167 // Decode from in[] to *dict.
168 const lzma_ret ret
= coder
->lzma
.code(coder
->lzma
.coder
,
169 dict
, in
, in_pos
, in_size
);
171 // Validate and update coder->compressed_size.
172 const size_t in_used
= *in_pos
- in_start
;
173 if (in_used
> coder
->compressed_size
)
174 return LZMA_DATA_ERROR
;
176 coder
->compressed_size
-= in_used
;
178 // Return if we didn't finish the chunk, or an error occurred.
179 if (ret
!= LZMA_STREAM_END
)
182 // The LZMA decoder must have consumed the whole chunk now.
183 // We don't need to worry about uncompressed size since it
184 // is checked by the LZMA decoder.
185 if (coder
->compressed_size
!= 0)
186 return LZMA_DATA_ERROR
;
188 coder
->sequence
= SEQ_CONTROL
;
193 // Copy from input to the dictionary as is.
194 dict_write(dict
, in
, in_pos
, in_size
, &coder
->compressed_size
);
195 if (coder
->compressed_size
!= 0)
198 coder
->sequence
= SEQ_CONTROL
;
204 return LZMA_PROG_ERROR
;
212 lzma2_decoder_end(lzma_coder
*coder
, const lzma_allocator
*allocator
)
214 assert(coder
->lzma
.end
== NULL
);
215 lzma_free(coder
->lzma
.coder
, allocator
);
217 lzma_free(coder
, allocator
);
224 lzma2_decoder_init(lzma_lz_decoder
*lz
, const lzma_allocator
*allocator
,
225 const void *opt
, lzma_lz_options
*lz_options
)
227 if (lz
->coder
== NULL
) {
228 lz
->coder
= lzma_alloc(sizeof(lzma_coder
), allocator
);
229 if (lz
->coder
== NULL
)
230 return LZMA_MEM_ERROR
;
232 lz
->code
= &lzma2_decode
;
233 lz
->end
= &lzma2_decoder_end
;
235 lz
->coder
->lzma
= LZMA_LZ_DECODER_INIT
;
238 const lzma_options_lzma
*options
= opt
;
240 lz
->coder
->sequence
= SEQ_CONTROL
;
241 lz
->coder
->need_properties
= true;
242 lz
->coder
->need_dictionary_reset
= options
->preset_dict
== NULL
243 || options
->preset_dict_size
== 0;
245 return lzma_lzma_decoder_create(&lz
->coder
->lzma
,
246 allocator
, options
, lz_options
);
251 lzma_lzma2_decoder_init(lzma_next_coder
*next
, const lzma_allocator
*allocator
,
252 const lzma_filter_info
*filters
)
254 // LZMA2 can only be the last filter in the chain. This is enforced
255 // by the raw_decoder initialization.
256 assert(filters
[1].init
== NULL
);
258 return lzma_lz_decoder_init(next
, allocator
, filters
,
259 &lzma2_decoder_init
);
264 lzma_lzma2_decoder_memusage(const void *options
)
266 return sizeof(lzma_coder
)
267 + lzma_lzma_decoder_memusage_nocheck(options
);
272 lzma_lzma2_props_decode(void **options
, const lzma_allocator
*allocator
,
273 const uint8_t *props
, size_t props_size
)
276 return LZMA_OPTIONS_ERROR
;
278 // Check that reserved bits are unset.
280 return LZMA_OPTIONS_ERROR
;
282 // Decode the dictionary size.
284 return LZMA_OPTIONS_ERROR
;
286 lzma_options_lzma
*opt
= lzma_alloc(
287 sizeof(lzma_options_lzma
), allocator
);
289 return LZMA_MEM_ERROR
;
291 if (props
[0] == 40) {
292 opt
->dict_size
= UINT32_MAX
;
294 opt
->dict_size
= 2 | (props
[0] & 1);
295 opt
->dict_size
<<= props
[0] / 2 + 11;
298 opt
->preset_dict
= NULL
;
299 opt
->preset_dict_size
= 0;