1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
5 /// \file lzma2_encoder.c
6 /// \brief LZMA2 encoder
8 // Authors: Igor Pavlov
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "lz_encoder.h"
14 #include "lzma_encoder.h"
16 #include "lzma2_encoder.h"
24 SEQ_UNCOMPRESSED_HEADER
,
25 SEQ_UNCOMPRESSED_COPY
,
31 /// LZMA options currently in use.
32 lzma_options_lzma opt_cur
;
35 bool need_state_reset
;
36 bool need_dictionary_reset
;
38 /// Uncompressed size of a chunk
39 size_t uncompressed_size
;
41 /// Compressed size of a chunk (excluding headers); this is also used
42 /// to indicate the end of buf[] in SEQ_LZMA_COPY.
43 size_t compressed_size
;
45 /// Read position in buf[]
48 /// Buffer to hold the chunk header and LZMA compressed data
49 uint8_t buf
[LZMA2_HEADER_MAX
+ LZMA2_CHUNK_MAX
];
54 lzma2_header_lzma(lzma_lzma2_coder
*coder
)
56 assert(coder
->uncompressed_size
> 0);
57 assert(coder
->uncompressed_size
<= LZMA2_UNCOMPRESSED_MAX
);
58 assert(coder
->compressed_size
> 0);
59 assert(coder
->compressed_size
<= LZMA2_CHUNK_MAX
);
63 if (coder
->need_properties
) {
66 if (coder
->need_dictionary_reset
)
67 coder
->buf
[pos
] = 0x80 + (3 << 5);
69 coder
->buf
[pos
] = 0x80 + (2 << 5);
73 if (coder
->need_state_reset
)
74 coder
->buf
[pos
] = 0x80 + (1 << 5);
76 coder
->buf
[pos
] = 0x80;
79 // Set the start position for copying.
83 size_t size
= coder
->uncompressed_size
- 1;
84 coder
->buf
[pos
++] += size
>> 16;
85 coder
->buf
[pos
++] = (size
>> 8) & 0xFF;
86 coder
->buf
[pos
++] = size
& 0xFF;
89 size
= coder
->compressed_size
- 1;
90 coder
->buf
[pos
++] = size
>> 8;
91 coder
->buf
[pos
++] = size
& 0xFF;
93 // Properties, if needed
94 if (coder
->need_properties
)
95 lzma_lzma_lclppb_encode(&coder
->opt_cur
, coder
->buf
+ pos
);
97 coder
->need_properties
= false;
98 coder
->need_state_reset
= false;
99 coder
->need_dictionary_reset
= false;
101 // The copying code uses coder->compressed_size to indicate the end
102 // of coder->buf[], so we need add the maximum size of the header here.
103 coder
->compressed_size
+= LZMA2_HEADER_MAX
;
110 lzma2_header_uncompressed(lzma_lzma2_coder
*coder
)
112 assert(coder
->uncompressed_size
> 0);
113 assert(coder
->uncompressed_size
<= LZMA2_CHUNK_MAX
);
115 // If this is the first chunk, we need to include dictionary
117 if (coder
->need_dictionary_reset
)
122 coder
->need_dictionary_reset
= false;
125 coder
->buf
[1] = (coder
->uncompressed_size
- 1) >> 8;
126 coder
->buf
[2] = (coder
->uncompressed_size
- 1) & 0xFF;
128 // Set the start position for copying.
135 lzma2_encode(void *coder_ptr
, lzma_mf
*restrict mf
,
136 uint8_t *restrict out
, size_t *restrict out_pos
,
139 lzma_lzma2_coder
*restrict coder
= coder_ptr
;
141 while (*out_pos
< out_size
)
142 switch (coder
->sequence
) {
144 // If there's no input left and we are flushing or finishing,
145 // don't start a new chunk.
146 if (mf_unencoded(mf
) == 0) {
147 // Write end of payload marker if finishing.
148 if (mf
->action
== LZMA_FINISH
)
149 out
[(*out_pos
)++] = 0;
151 return mf
->action
== LZMA_RUN
152 ? LZMA_OK
: LZMA_STREAM_END
;
155 if (coder
->need_state_reset
)
156 return_if_error(lzma_lzma_encoder_reset(
157 coder
->lzma
, &coder
->opt_cur
));
159 coder
->uncompressed_size
= 0;
160 coder
->compressed_size
= 0;
161 coder
->sequence
= SEQ_LZMA_ENCODE
;
165 case SEQ_LZMA_ENCODE
: {
166 // Calculate how much more uncompressed data this chunk
168 const uint32_t left
= LZMA2_UNCOMPRESSED_MAX
169 - coder
->uncompressed_size
;
172 if (left
< mf
->match_len_max
) {
173 // Must flush immediately since the next LZMA symbol
174 // could make the uncompressed size of the chunk too
178 // Calculate maximum read_limit that is OK from point
179 // of view of LZMA2 chunk size.
180 limit
= mf
->read_pos
- mf
->read_ahead
181 + left
- mf
->match_len_max
;
184 // Save the start position so that we can update
185 // coder->uncompressed_size.
186 const uint32_t read_start
= mf
->read_pos
- mf
->read_ahead
;
188 // Call the LZMA encoder until the chunk is finished.
189 const lzma_ret ret
= lzma_lzma_encode(coder
->lzma
, mf
,
190 coder
->buf
+ LZMA2_HEADER_MAX
,
191 &coder
->compressed_size
,
192 LZMA2_CHUNK_MAX
, limit
);
194 coder
->uncompressed_size
+= mf
->read_pos
- mf
->read_ahead
197 assert(coder
->compressed_size
<= LZMA2_CHUNK_MAX
);
198 assert(coder
->uncompressed_size
<= LZMA2_UNCOMPRESSED_MAX
);
200 if (ret
!= LZMA_STREAM_END
)
203 // See if the chunk compressed. If it didn't, we encode it
204 // as uncompressed chunk. This saves a few bytes of space
205 // and makes decoding faster.
206 if (coder
->compressed_size
>= coder
->uncompressed_size
) {
207 coder
->uncompressed_size
+= mf
->read_ahead
;
208 assert(coder
->uncompressed_size
209 <= LZMA2_UNCOMPRESSED_MAX
);
211 lzma2_header_uncompressed(coder
);
212 coder
->need_state_reset
= true;
213 coder
->sequence
= SEQ_UNCOMPRESSED_HEADER
;
217 // The chunk did compress at least by one byte, so we store
218 // the chunk as LZMA.
219 lzma2_header_lzma(coder
);
221 coder
->sequence
= SEQ_LZMA_COPY
;
227 // Copy the compressed chunk along its headers to the
229 lzma_bufcpy(coder
->buf
, &coder
->buf_pos
,
230 coder
->compressed_size
,
231 out
, out_pos
, out_size
);
232 if (coder
->buf_pos
!= coder
->compressed_size
)
235 coder
->sequence
= SEQ_INIT
;
238 case SEQ_UNCOMPRESSED_HEADER
:
239 // Copy the three-byte header to indicate uncompressed chunk.
240 lzma_bufcpy(coder
->buf
, &coder
->buf_pos
,
241 LZMA2_HEADER_UNCOMPRESSED
,
242 out
, out_pos
, out_size
);
243 if (coder
->buf_pos
!= LZMA2_HEADER_UNCOMPRESSED
)
246 coder
->sequence
= SEQ_UNCOMPRESSED_COPY
;
250 case SEQ_UNCOMPRESSED_COPY
:
251 // Copy the uncompressed data as is from the dictionary
252 // to the output buffer.
253 mf_read(mf
, out
, out_pos
, out_size
, &coder
->uncompressed_size
);
254 if (coder
->uncompressed_size
!= 0)
257 coder
->sequence
= SEQ_INIT
;
266 lzma2_encoder_end(void *coder_ptr
, const lzma_allocator
*allocator
)
268 lzma_lzma2_coder
*coder
= coder_ptr
;
269 lzma_free(coder
->lzma
, allocator
);
270 lzma_free(coder
, allocator
);
276 lzma2_encoder_options_update(void *coder_ptr
, const lzma_filter
*filter
)
278 lzma_lzma2_coder
*coder
= coder_ptr
;
280 // New options can be set only when there is no incomplete chunk.
281 // This is the case at the beginning of the raw stream and right
282 // after LZMA_SYNC_FLUSH.
283 if (filter
->options
== NULL
|| coder
->sequence
!= SEQ_INIT
)
284 return LZMA_PROG_ERROR
;
286 // Look if there are new options. At least for now,
287 // only lc/lp/pb can be changed.
288 const lzma_options_lzma
*opt
= filter
->options
;
289 if (coder
->opt_cur
.lc
!= opt
->lc
|| coder
->opt_cur
.lp
!= opt
->lp
290 || coder
->opt_cur
.pb
!= opt
->pb
) {
291 // Validate the options.
292 if (opt
->lc
> LZMA_LCLP_MAX
|| opt
->lp
> LZMA_LCLP_MAX
293 || opt
->lc
+ opt
->lp
> LZMA_LCLP_MAX
294 || opt
->pb
> LZMA_PB_MAX
)
295 return LZMA_OPTIONS_ERROR
;
297 // The new options will be used when the encoder starts
298 // a new LZMA2 chunk.
299 coder
->opt_cur
.lc
= opt
->lc
;
300 coder
->opt_cur
.lp
= opt
->lp
;
301 coder
->opt_cur
.pb
= opt
->pb
;
302 coder
->need_properties
= true;
303 coder
->need_state_reset
= true;
311 lzma2_encoder_init(lzma_lz_encoder
*lz
, const lzma_allocator
*allocator
,
312 lzma_vli id
lzma_attribute((__unused__
)), const void *options
,
313 lzma_lz_options
*lz_options
)
316 return LZMA_PROG_ERROR
;
318 lzma_lzma2_coder
*coder
= lz
->coder
;
320 coder
= lzma_alloc(sizeof(lzma_lzma2_coder
), allocator
);
322 return LZMA_MEM_ERROR
;
325 lz
->code
= &lzma2_encode
;
326 lz
->end
= &lzma2_encoder_end
;
327 lz
->options_update
= &lzma2_encoder_options_update
;
332 coder
->opt_cur
= *(const lzma_options_lzma
*)(options
);
334 coder
->sequence
= SEQ_INIT
;
335 coder
->need_properties
= true;
336 coder
->need_state_reset
= false;
337 coder
->need_dictionary_reset
338 = coder
->opt_cur
.preset_dict
== NULL
339 || coder
->opt_cur
.preset_dict_size
== 0;
341 // Initialize LZMA encoder
342 return_if_error(lzma_lzma_encoder_create(&coder
->lzma
, allocator
,
343 LZMA_FILTER_LZMA2
, &coder
->opt_cur
, lz_options
));
345 // Make sure that we will always have enough history available in
346 // case we need to use uncompressed chunks. They are used when the
347 // compressed size of a chunk is not smaller than the uncompressed
348 // size, so we need to have at least LZMA2_COMPRESSED_MAX bytes
349 // history available.
350 if (lz_options
->before_size
+ lz_options
->dict_size
< LZMA2_CHUNK_MAX
)
351 lz_options
->before_size
352 = LZMA2_CHUNK_MAX
- lz_options
->dict_size
;
359 lzma_lzma2_encoder_init(lzma_next_coder
*next
, const lzma_allocator
*allocator
,
360 const lzma_filter_info
*filters
)
362 return lzma_lz_encoder_init(
363 next
, allocator
, filters
, &lzma2_encoder_init
);
368 lzma_lzma2_encoder_memusage(const void *options
)
370 const uint64_t lzma_mem
= lzma_lzma_encoder_memusage(options
);
371 if (lzma_mem
== UINT64_MAX
)
374 return sizeof(lzma_lzma2_coder
) + lzma_mem
;
379 lzma_lzma2_props_encode(const void *options
, uint8_t *out
)
382 return LZMA_PROG_ERROR
;
384 const lzma_options_lzma
*const opt
= options
;
385 uint32_t d
= my_max(opt
->dict_size
, LZMA_DICT_SIZE_MIN
);
387 // Round up to the next 2^n - 1 or 2^n + 2^(n - 1) - 1 depending
388 // on which one is the next:
396 // Get the highest two bits using the proper encoding:
400 out
[0] = get_dist_slot(d
+ 1) - 24;
407 lzma_lzma2_block_size(const void *options
)
409 const lzma_options_lzma
*const opt
= options
;
411 if (!IS_ENC_DICT_SIZE_VALID(opt
->dict_size
))
414 // Use at least 1 MiB to keep compression ratio better.
415 return my_max((uint64_t)(opt
->dict_size
) * 3, UINT64_C(1) << 20);