liblzma: Pass the Filter ID to LZ encoder and decoder.
[xz.git] / src / liblzma / common / microlzma_decoder.c
blobba2f10ad39ae5d9cd69da4e6dc209e5bb628f2b3
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file microlzma_decoder.c
4 /// \brief Decode MicroLZMA format
5 //
6 // Author: Lasse Collin
7 //
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "lzma_decoder.h"
14 #include "lz_decoder.h"
17 typedef struct {
18 /// LZMA1 decoder
19 lzma_next_coder lzma;
21 /// Compressed size of the stream as given by the application.
22 /// This must be exactly correct.
23 ///
24 /// This will be decremented when input is read.
25 uint64_t comp_size;
27 /// Uncompressed size of the stream as given by the application.
28 /// This may be less than the actual uncompressed size if
29 /// uncomp_size_is_exact is false.
30 ///
31 /// This will be decremented when output is produced.
32 lzma_vli uncomp_size;
34 /// LZMA dictionary size as given by the application
35 uint32_t dict_size;
37 /// If true, the exact uncompressed size is known. If false,
38 /// uncomp_size may be smaller than the real uncompressed size;
39 /// uncomp_size may never be bigger than the real uncompressed size.
40 bool uncomp_size_is_exact;
42 /// True once the first byte of the MicroLZMA stream
43 /// has been processed.
44 bool props_decoded;
45 } lzma_microlzma_coder;
48 static lzma_ret
49 microlzma_decode(void *coder_ptr, const lzma_allocator *allocator,
50 const uint8_t *restrict in, size_t *restrict in_pos,
51 size_t in_size, uint8_t *restrict out,
52 size_t *restrict out_pos, size_t out_size, lzma_action action)
54 lzma_microlzma_coder *coder = coder_ptr;
56 // Remember the in start position so that we can update comp_size.
57 const size_t in_start = *in_pos;
59 // Remember the out start position so that we can update uncomp_size.
60 const size_t out_start = *out_pos;
62 // Limit the amount of input so that the decoder won't read more than
63 // comp_size. This is required when uncomp_size isn't exact because
64 // in that case the LZMA decoder will try to decode more input even
65 // when it has no output space (it can be looking for EOPM).
66 if (in_size - *in_pos > coder->comp_size)
67 in_size = *in_pos + (size_t)(coder->comp_size);
69 // When the exact uncompressed size isn't known, we must limit
70 // the available output space to prevent the LZMA decoder from
71 // trying to decode too much.
72 if (!coder->uncomp_size_is_exact
73 && out_size - *out_pos > coder->uncomp_size)
74 out_size = *out_pos + (size_t)(coder->uncomp_size);
76 if (!coder->props_decoded) {
77 // There must be at least one byte of input to decode
78 // the properties byte.
79 if (*in_pos >= in_size)
80 return LZMA_OK;
82 lzma_options_lzma options = {
83 .preset_dict = NULL,
84 .preset_dict_size = 0,
87 // The properties are stored as bitwise-negation
88 // of the typical encoding.
89 if (lzma_lzma_lclppb_decode(&options, ~in[*in_pos]))
90 return LZMA_OPTIONS_ERROR;
92 ++*in_pos;
94 // Initialize the decoder.
95 options.dict_size = coder->dict_size;
96 lzma_filter_info filters[2] = {
98 .id = LZMA_FILTER_LZMA1,
99 .init = &lzma_lzma_decoder_init,
100 .options = &options,
101 }, {
102 .init = NULL,
106 return_if_error(lzma_next_filter_init(&coder->lzma,
107 allocator, filters));
109 // Use a hack to set the uncompressed size.
110 if (coder->uncomp_size_is_exact)
111 lzma_lz_decoder_uncompressed(coder->lzma.coder,
112 coder->uncomp_size, false);
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,
129 in, in_pos, in_size,
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;
141 } else {
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;
156 return ret;
160 static void
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);
166 return;
170 static lzma_ret
171 microlzma_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
172 uint64_t comp_size,
173 uint64_t uncomp_size, bool uncomp_size_is_exact,
174 uint32_t dict_size)
176 lzma_next_coder_init(&microlzma_decoder_init, next, allocator);
178 lzma_microlzma_coder *coder = next->coder;
180 if (coder == NULL) {
181 coder = lzma_alloc(sizeof(lzma_microlzma_coder), allocator);
182 if (coder == NULL)
183 return LZMA_MEM_ERROR;
185 next->coder = coder;
186 next->code = &microlzma_decode;
187 next->end = &microlzma_decoder_end;
189 coder->lzma = LZMA_NEXT_CODER_INIT;
192 // The public API is uint64_t but the internal LZ decoder API uses
193 // lzma_vli.
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;
204 return LZMA_OK;
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,
211 uint32_t dict_size)
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;
219 return LZMA_OK;