1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file lzma2_encoder.c
4 /// \brief LZMA2 encoder
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 "lz_encoder.h"
15 #include "lzma_encoder.h"
17 #include "lzma2_encoder.h"
25 SEQ_UNCOMPRESSED_HEADER,
26 SEQ_UNCOMPRESSED_COPY,
32 /// If this is not NULL, we will check new options from this
33 /// structure when starting a new chunk.
34 const lzma_options_lzma *opt_new;
36 /// LZMA options currently in use.
37 lzma_options_lzma opt_cur;
40 bool need_state_reset;
41 bool need_dictionary_reset;
43 /// Uncompressed size of a chunk
44 size_t uncompressed_size;
46 /// Compressed size of a chunk (excluding headers); this is also used
47 /// to indicate the end of buf[] in SEQ_LZMA_COPY.
48 size_t compressed_size;
50 /// Read position in buf[]
53 /// Buffer to hold the chunk header and LZMA compressed data
54 uint8_t buf[LZMA2_HEADER_MAX + LZMA2_CHUNK_MAX];
59 lzma2_header_lzma(lzma_coder *coder)
61 assert(coder->uncompressed_size > 0);
62 assert(coder->uncompressed_size <= LZMA2_UNCOMPRESSED_MAX);
63 assert(coder->compressed_size > 0);
64 assert(coder->compressed_size <= LZMA2_CHUNK_MAX);
68 if (coder->need_properties) {
71 if (coder->need_dictionary_reset)
72 coder->buf[pos] = 0x80 + (3 << 5);
74 coder->buf[pos] = 0x80 + (2 << 5);
78 if (coder->need_state_reset)
79 coder->buf[pos] = 0x80 + (1 << 5);
81 coder->buf[pos] = 0x80;
84 // Set the start position for copying.
88 size_t size = coder->uncompressed_size - 1;
89 coder->buf[pos++] += size >> 16;
90 coder->buf[pos++] = (size >> 8) & 0xFF;
91 coder->buf[pos++] = size & 0xFF;
94 size = coder->compressed_size - 1;
95 coder->buf[pos++] = size >> 8;
96 coder->buf[pos++] = size & 0xFF;
98 // Properties, if needed
99 if (coder->need_properties)
100 lzma_lzma_lclppb_encode(&coder->opt_cur, coder->buf + pos);
102 coder->need_properties = false;
103 coder->need_state_reset = false;
104 coder->need_dictionary_reset = false;
106 // The copying code uses coder->compressed_size to indicate the end
107 // of coder->buf[], so we need add the maximum size of the header here.
108 coder->compressed_size += LZMA2_HEADER_MAX;
115 lzma2_header_uncompressed(lzma_coder *coder)
117 assert(coder->uncompressed_size > 0);
118 assert(coder->uncompressed_size <= LZMA2_CHUNK_MAX);
120 // If this is the first chunk, we need to include dictionary
122 if (coder->need_dictionary_reset)
127 coder->need_dictionary_reset = false;
130 coder->buf[1] = (coder->uncompressed_size - 1) >> 8;
131 coder->buf[2] = (coder->uncompressed_size - 1) & 0xFF;
133 // Set the start position for copying.
140 lzma2_encode(lzma_coder *restrict coder, lzma_mf *restrict mf,
141 uint8_t *restrict out, size_t *restrict out_pos,
144 while (*out_pos < out_size)
145 switch (coder->sequence) {
147 // If there's no input left and we are flushing or finishing,
148 // don't start a new chunk.
149 if (mf_unencoded(mf) == 0) {
150 // Write end of payload marker if finishing.
151 if (mf->action == LZMA_FINISH)
152 out[(*out_pos)++] = 0;
154 return mf->action == LZMA_RUN
155 ? LZMA_OK : LZMA_STREAM_END;
158 // Look if there are new options. At least for now,
159 // only lc/lp/pb can be changed.
160 if (coder->opt_new != NULL
161 && (coder->opt_cur.lc != coder->opt_new->lc
162 || coder->opt_cur.lp != coder->opt_new->lp
163 || coder->opt_cur.pb != coder->opt_new->pb)) {
164 // Options have been changed, copy them to opt_cur.
165 // These get validated as part of
166 // lzma_lzma_encoder_reset() below.
167 coder->opt_cur.lc = coder->opt_new->lc;
168 coder->opt_cur.lp = coder->opt_new->lp;
169 coder->opt_cur.pb = coder->opt_new->pb;
171 // We need to write the new options and reset
172 // the encoder state.
173 coder->need_properties = true;
174 coder->need_state_reset = true;
177 if (coder->need_state_reset)
178 return_if_error(lzma_lzma_encoder_reset(
179 coder->lzma, &coder->opt_cur));
181 coder->uncompressed_size = 0;
182 coder->compressed_size = 0;
183 coder->sequence = SEQ_LZMA_ENCODE;
187 case SEQ_LZMA_ENCODE: {
188 // Calculate how much more uncompressed data this chunk
190 const uint32_t left = LZMA2_UNCOMPRESSED_MAX
191 - coder->uncompressed_size;
194 if (left < mf->match_len_max) {
195 // Must flush immediatelly since the next LZMA symbol
196 // could make the uncompressed size of the chunk too
200 // Calculate maximum read_limit that is OK from point
201 // of view of LZMA2 chunk size.
202 limit = mf->read_pos - mf->read_ahead
203 + left - mf->match_len_max;
206 // Save the start position so that we can update
207 // coder->uncompressed_size.
208 const uint32_t read_start = mf->read_pos - mf->read_ahead;
210 // Call the LZMA encoder until the chunk is finished.
211 const lzma_ret ret = lzma_lzma_encode(coder->lzma, mf,
212 coder->buf + LZMA2_HEADER_MAX,
213 &coder->compressed_size,
214 LZMA2_CHUNK_MAX, limit);
216 coder->uncompressed_size += mf->read_pos - mf->read_ahead
219 assert(coder->compressed_size <= LZMA2_CHUNK_MAX);
220 assert(coder->uncompressed_size <= LZMA2_UNCOMPRESSED_MAX);
222 if (ret != LZMA_STREAM_END)
225 // See if the chunk compressed. If it didn't, we encode it
226 // as uncompressed chunk. This saves a few bytes of space
227 // and makes decoding faster.
228 if (coder->compressed_size >= coder->uncompressed_size) {
229 coder->uncompressed_size += mf->read_ahead;
230 assert(coder->uncompressed_size
231 <= LZMA2_UNCOMPRESSED_MAX);
233 lzma2_header_uncompressed(coder);
234 coder->need_state_reset = true;
235 coder->sequence = SEQ_UNCOMPRESSED_HEADER;
239 // The chunk did compress at least by one byte, so we store
240 // the chunk as LZMA.
241 lzma2_header_lzma(coder);
243 coder->sequence = SEQ_LZMA_COPY;
249 // Copy the compressed chunk along its headers to the
251 lzma_bufcpy(coder->buf, &coder->buf_pos,
252 coder->compressed_size,
253 out, out_pos, out_size);
254 if (coder->buf_pos != coder->compressed_size)
257 coder->sequence = SEQ_INIT;
260 case SEQ_UNCOMPRESSED_HEADER:
261 // Copy the three-byte header to indicate uncompressed chunk.
262 lzma_bufcpy(coder->buf, &coder->buf_pos,
263 LZMA2_HEADER_UNCOMPRESSED,
264 out, out_pos, out_size);
265 if (coder->buf_pos != LZMA2_HEADER_UNCOMPRESSED)
268 coder->sequence = SEQ_UNCOMPRESSED_COPY;
272 case SEQ_UNCOMPRESSED_COPY:
273 // Copy the uncompressed data as is from the dictionary
274 // to the output buffer.
275 mf_read(mf, out, out_pos, out_size, &coder->uncompressed_size);
276 if (coder->uncompressed_size != 0)
279 coder->sequence = SEQ_INIT;
288 lzma2_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
290 lzma_free(coder->lzma, allocator);
291 lzma_free(coder, allocator);
297 lzma2_encoder_init(lzma_lz_encoder *lz, lzma_allocator *allocator,
298 const void *options, lzma_lz_options *lz_options)
301 return LZMA_PROG_ERROR;
303 if (lz->coder == NULL) {
304 lz->coder = lzma_alloc(sizeof(lzma_coder), allocator);
305 if (lz->coder == NULL)
306 return LZMA_MEM_ERROR;
308 lz->code = &lzma2_encode;
309 lz->end = &lzma2_encoder_end;
311 lz->coder->lzma = NULL;
314 lz->coder->opt_cur = *(const lzma_options_lzma *)(options);
315 lz->coder->opt_new = lz->coder->opt_cur.persistent
318 lz->coder->sequence = SEQ_INIT;
319 lz->coder->need_properties = true;
320 lz->coder->need_state_reset = false;
321 lz->coder->need_dictionary_reset
322 = lz->coder->opt_cur.preset_dict == NULL
323 || lz->coder->opt_cur.preset_dict_size == 0;
325 // Initialize LZMA encoder
326 return_if_error(lzma_lzma_encoder_create(&lz->coder->lzma, allocator,
327 &lz->coder->opt_cur, lz_options));
329 // Make sure that we will always have enough history available in
330 // case we need to use uncompressed chunks. They are used when the
331 // compressed size of a chunk is not smaller than the uncompressed
332 // size, so we need to have at least LZMA2_COMPRESSED_MAX bytes
333 // history available.
334 if (lz_options->before_size + lz_options->dict_size < LZMA2_CHUNK_MAX)
335 lz_options->before_size
336 = LZMA2_CHUNK_MAX - lz_options->dict_size;
343 lzma_lzma2_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
344 const lzma_filter_info *filters)
346 return lzma_lz_encoder_init(
347 next, allocator, filters, &lzma2_encoder_init);
352 lzma_lzma2_encoder_memusage(const void *options)
354 const uint64_t lzma_mem = lzma_lzma_encoder_memusage(options);
355 if (lzma_mem == UINT64_MAX)
358 return sizeof(lzma_coder) + lzma_mem;
363 lzma_lzma2_props_encode(const void *options, uint8_t *out)
365 const lzma_options_lzma *const opt = options;
366 uint32_t d = MAX(opt->dict_size, LZMA_DICT_SIZE_MIN);
368 // Round up to to the next 2^n - 1 or 2^n + 2^(n - 1) - 1 depending
369 // on which one is the next:
377 // Get the highest two bits using the proper encoding:
381 out[0] = get_pos_slot(d + 1) - 24;