1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
5 /// \file microlzma_decoder.c
6 /// \brief Decode MicroLZMA format
8 // Author: Lasse Collin
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "lzma_decoder.h"
13 #include "lz_decoder.h"
20 /// Compressed size of the stream as given by the application.
21 /// This must be exactly correct.
23 /// This will be decremented when input is read.
26 /// Uncompressed size of the stream as given by the application.
27 /// This may be less than the actual uncompressed size if
28 /// uncomp_size_is_exact is false.
30 /// This will be decremented when output is produced.
33 /// LZMA dictionary size as given by the application
36 /// If true, the exact uncompressed size is known. If false,
37 /// uncomp_size may be smaller than the real uncompressed size;
38 /// uncomp_size may never be bigger than the real uncompressed size.
39 bool uncomp_size_is_exact
;
41 /// True once the first byte of the MicroLZMA stream
42 /// has been processed.
44 } lzma_microlzma_coder
;
48 microlzma_decode(void *coder_ptr
, const lzma_allocator
*allocator
,
49 const uint8_t *restrict in
, size_t *restrict in_pos
,
50 size_t in_size
, uint8_t *restrict out
,
51 size_t *restrict out_pos
, size_t out_size
, lzma_action action
)
53 lzma_microlzma_coder
*coder
= coder_ptr
;
55 // Remember the in start position so that we can update comp_size.
56 const size_t in_start
= *in_pos
;
58 // Remember the out start position so that we can update uncomp_size.
59 const size_t out_start
= *out_pos
;
61 // Limit the amount of input so that the decoder won't read more than
62 // comp_size. This is required when uncomp_size isn't exact because
63 // in that case the LZMA decoder will try to decode more input even
64 // when it has no output space (it can be looking for EOPM).
65 if (in_size
- *in_pos
> coder
->comp_size
)
66 in_size
= *in_pos
+ (size_t)(coder
->comp_size
);
68 // When the exact uncompressed size isn't known, we must limit
69 // the available output space to prevent the LZMA decoder from
70 // trying to decode too much.
71 if (!coder
->uncomp_size_is_exact
72 && out_size
- *out_pos
> coder
->uncomp_size
)
73 out_size
= *out_pos
+ (size_t)(coder
->uncomp_size
);
75 if (!coder
->props_decoded
) {
76 // There must be at least one byte of input to decode
77 // the properties byte.
78 if (*in_pos
>= in_size
)
81 lzma_options_lzma options
= {
82 .dict_size
= coder
->dict_size
,
84 .preset_dict_size
= 0,
85 .ext_flags
= 0, // EOPM not allowed when size is known
86 .ext_size_low
= UINT32_MAX
, // Unknown size by default
87 .ext_size_high
= UINT32_MAX
,
90 if (coder
->uncomp_size_is_exact
)
91 lzma_set_ext_size(options
, coder
->uncomp_size
);
93 // The properties are stored as bitwise-negation
94 // of the typical encoding.
95 if (lzma_lzma_lclppb_decode(&options
, ~in
[*in_pos
]))
96 return LZMA_OPTIONS_ERROR
;
100 // Initialize the decoder.
101 lzma_filter_info filters
[2] = {
103 .id
= LZMA_FILTER_LZMA1EXT
,
104 .init
= &lzma_lzma_decoder_init
,
111 return_if_error(lzma_next_filter_init(&coder
->lzma
,
112 allocator
, filters
));
114 // Pass one dummy 0x00 byte to the LZMA decoder since that
115 // is what it expects the first byte to be.
116 const uint8_t dummy_in
= 0;
117 size_t dummy_in_pos
= 0;
118 if (coder
->lzma
.code(coder
->lzma
.coder
, allocator
,
119 &dummy_in
, &dummy_in_pos
, 1,
120 out
, out_pos
, out_size
, LZMA_RUN
) != LZMA_OK
)
121 return LZMA_PROG_ERROR
;
123 assert(dummy_in_pos
== 1);
124 coder
->props_decoded
= true;
127 // The rest is normal LZMA decoding.
128 lzma_ret ret
= coder
->lzma
.code(coder
->lzma
.coder
, allocator
,
130 out
, out_pos
, out_size
, action
);
132 // Update the remaining compressed size.
133 assert(coder
->comp_size
>= *in_pos
- in_start
);
134 coder
->comp_size
-= *in_pos
- in_start
;
136 if (coder
->uncomp_size_is_exact
) {
137 // After successful decompression of the complete stream
138 // the compressed size must match.
139 if (ret
== LZMA_STREAM_END
&& coder
->comp_size
!= 0)
140 ret
= LZMA_DATA_ERROR
;
142 // Update the amount of output remaining.
143 assert(coder
->uncomp_size
>= *out_pos
- out_start
);
144 coder
->uncomp_size
-= *out_pos
- out_start
;
146 // - We must not get LZMA_STREAM_END because the stream
147 // shouldn't have EOPM.
148 // - We must use uncomp_size to determine when to
149 // return LZMA_STREAM_END.
150 if (ret
== LZMA_STREAM_END
)
151 ret
= LZMA_DATA_ERROR
;
152 else if (coder
->uncomp_size
== 0)
153 ret
= LZMA_STREAM_END
;
161 microlzma_decoder_end(void *coder_ptr
, const lzma_allocator
*allocator
)
163 lzma_microlzma_coder
*coder
= coder_ptr
;
164 lzma_next_end(&coder
->lzma
, allocator
);
165 lzma_free(coder
, allocator
);
171 microlzma_decoder_init(lzma_next_coder
*next
, const lzma_allocator
*allocator
,
173 uint64_t uncomp_size
, bool uncomp_size_is_exact
,
176 lzma_next_coder_init(µlzma_decoder_init
, next
, allocator
);
178 lzma_microlzma_coder
*coder
= next
->coder
;
181 coder
= lzma_alloc(sizeof(lzma_microlzma_coder
), allocator
);
183 return LZMA_MEM_ERROR
;
186 next
->code
= µlzma_decode
;
187 next
->end
= µlzma_decoder_end
;
189 coder
->lzma
= LZMA_NEXT_CODER_INIT
;
192 // The public API is uint64_t but the internal LZ decoder API uses
194 if (uncomp_size
> LZMA_VLI_MAX
)
195 return LZMA_OPTIONS_ERROR
;
197 coder
->comp_size
= comp_size
;
198 coder
->uncomp_size
= uncomp_size
;
199 coder
->uncomp_size_is_exact
= uncomp_size_is_exact
;
200 coder
->dict_size
= dict_size
;
202 coder
->props_decoded
= false;
208 extern LZMA_API(lzma_ret
)
209 lzma_microlzma_decoder(lzma_stream
*strm
, uint64_t comp_size
,
210 uint64_t uncomp_size
, lzma_bool uncomp_size_is_exact
,
213 lzma_next_strm_init(microlzma_decoder_init
, strm
, comp_size
,
214 uncomp_size
, uncomp_size_is_exact
, dict_size
);
216 strm
->internal
->supported_actions
[LZMA_RUN
] = true;
217 strm
->internal
->supported_actions
[LZMA_FINISH
] = true;