Parolin 0.7.9 6796
Console (soon DLLs) to do a tar like job
Loading...
Searching...
No Matches
lz_encoder.h
Go to the documentation of this file.
1// SPDX-License-Identifier: 0BSD
2
4//
8// Authors: Igor Pavlov
9// Lasse Collin
10//
12
13#ifndef LZMA_LZ_ENCODER_H
14#define LZMA_LZ_ENCODER_H
15
16#include "common.h"
17
18
19// For now, the dictionary size is limited to 1.5 GiB. This may grow
20// in the future if needed, but it needs a little more work than just
21// changing this check.
22#define IS_ENC_DICT_SIZE_VALID(size) \
23 ((size) >= LZMA_DICT_SIZE_MIN \
24 && (size) <= (UINT32_C(1) << 30) + (UINT32_C(1) << 29))
25
26
29typedef struct {
30 uint32_t len;
31 uint32_t dist;
33
34
35typedef struct lzma_mf_s lzma_mf;
36struct lzma_mf_s {
38 // In Window //
40
42 uint8_t *buffer;
43
46 uint32_t size;
47
52 uint32_t keep_size_before;
53
58 uint32_t keep_size_after;
59
65 uint32_t offset;
66
70 uint32_t read_pos;
71
74 uint32_t read_ahead;
75
82 uint32_t read_limit;
83
87 uint32_t write_pos;
88
91 uint32_t pending;
92
94 // Match Finder //
96
99 uint32_t (*find)(lzma_mf *mf, lzma_match *matches);
100
104 void (*skip)(lzma_mf *mf, uint32_t num);
105
106 uint32_t *hash;
107 uint32_t *son;
108 uint32_t cyclic_pos;
109 uint32_t cyclic_size; // Must be dictionary size + 1.
110 uint32_t hash_mask;
111
113 uint32_t depth;
114
116 uint32_t nice_len;
117
121 uint32_t match_len_max;
122
127
129 uint32_t hash_count;
130
132 uint32_t sons_count;
133};
134
135
136typedef struct {
139 size_t before_size;
140
142 size_t dict_size;
143
146 size_t after_size;
147
151 size_t match_len_max;
152
155 size_t nice_len;
156
158 lzma_match_finder match_finder;
159
161 uint32_t depth;
162
164 const uint8_t *preset_dict;
165
170 uint32_t preset_dict_size;
171
173
174
175// The total usable buffer space at any moment outside the match finder:
176// before_size + dict_size + after_size + match_len_max
177//
178// In reality, there's some extra space allocated to prevent the number of
179// memmove() calls reasonable. The bigger the dict_size is, the bigger
180// this extra buffer will be since with bigger dictionaries memmove() would
181// also take longer.
182//
183// A single encoder loop in the LZ-based encoder may call the match finder
184// (mf_find() or mf_skip()) at most after_size times. In other words,
185// a single encoder loop may increment lzma_mf.read_pos at most after_size
186// times. Since matches are looked up to
187// lzma_mf.buffer[lzma_mf.read_pos + match_len_max - 1], the total
188// amount of extra buffer needed after dict_size becomes
189// after_size + match_len_max.
190//
191// before_size has two uses. The first one is to keep literals available
192// in cases when the LZ-based encoder has made some read ahead.
193// TODO: Maybe this could be changed by making the LZ-based encoders to
194// store the actual literals as they do with length-distance pairs.
195//
196// Algorithms such as LZMA2 first try to compress a chunk, and then check
197// if the encoded result is smaller than the uncompressed one. If the chunk
198// was incompressible, it is better to store it in uncompressed form in
199// the output stream. To do this, the whole uncompressed chunk has to be
200// still available in the history buffer. before_size achieves that.
201
202
203typedef struct {
205 void *coder;
206
208 lzma_ret (*code)(void *coder,
209 lzma_mf *restrict mf, uint8_t *restrict out,
210 size_t *restrict out_pos, size_t out_size);
211
213 void (*end)(void *coder, const lzma_allocator *allocator);
214
216 lzma_ret (*options_update)(void *coder, const lzma_filter *filter);
217
219 lzma_ret (*set_out_limit)(void *coder, uint64_t *uncomp_size,
220 uint64_t out_limit);
221
223
224
225// Basic steps:
226// 1. Input gets copied into the dictionary.
227// 2. Data in dictionary gets run through the match finder byte by byte.
228// 3. The literals and matches are encoded using e.g. LZMA.
229//
230// The bytes that have been ran through the match finder, but not encoded yet,
231// are called 'read ahead'.
232
233
236static inline uint32_t
237mf_get_hash_bytes(lzma_match_finder match_finder)
238{
239 return (uint32_t)match_finder & 0x0F;
240}
241
242
244static inline const uint8_t *
245mf_ptr(const lzma_mf *mf)
246{
247 return mf->buffer + mf->read_pos;
248}
249
250
252static inline uint32_t
253mf_avail(const lzma_mf *mf)
254{
255 return mf->write_pos - mf->read_pos;
256}
257
258
261static inline uint32_t
262mf_unencoded(const lzma_mf *mf)
263{
264 return mf->write_pos - mf->read_pos + mf->read_ahead;
265}
266
267
275static inline uint32_t
276mf_position(const lzma_mf *mf)
277{
278 return mf->read_pos - mf->read_ahead;
279}
280
281
283#define mf_find lzma_mf_find
284
285
290static inline void
291mf_skip(lzma_mf *mf, uint32_t amount)
292{
293 if (amount != 0) {
294 mf->skip(mf, amount);
295 mf->read_ahead += amount;
296 }
297}
298
299
302static inline void
303mf_read(lzma_mf *mf, uint8_t *out, size_t *out_pos, size_t out_size,
304 size_t *left)
305{
306 const size_t out_avail = out_size - *out_pos;
307 const size_t copy_size = my_min(out_avail, *left);
308
309 assert(mf->read_ahead == 0);
310 assert(mf->read_pos >= *left);
311
312 memcpy(out + *out_pos, mf->buffer + mf->read_pos - *left,
313 copy_size);
314
315 *out_pos += copy_size;
316 *left -= copy_size;
317 return;
318}
319
320
324 lzma_ret (*lz_init)(lzma_lz_encoder *lz,
326 lzma_vli id, const void *options,
327 lzma_lz_options *lz_options));
328
329
330extern uint64_t lzma_lz_encoder_memusage(const lzma_lz_options *lz_options);
331
332
333// These are only for LZ encoder's internal use.
334extern uint32_t lzma_mf_find(
335 lzma_mf *mf, uint32_t *count, lzma_match *matches);
336
337extern uint32_t lzma_mf_hc3_find(lzma_mf *dict, lzma_match *matches);
338extern void lzma_mf_hc3_skip(lzma_mf *dict, uint32_t amount);
339
340extern uint32_t lzma_mf_hc4_find(lzma_mf *dict, lzma_match *matches);
341extern void lzma_mf_hc4_skip(lzma_mf *dict, uint32_t amount);
342
343extern uint32_t lzma_mf_bt2_find(lzma_mf *dict, lzma_match *matches);
344extern void lzma_mf_bt2_skip(lzma_mf *dict, uint32_t amount);
345
346extern uint32_t lzma_mf_bt3_find(lzma_mf *dict, lzma_match *matches);
347extern void lzma_mf_bt3_skip(lzma_mf *dict, uint32_t amount);
348
349extern uint32_t lzma_mf_bt4_find(lzma_mf *dict, lzma_match *matches);
350extern void lzma_mf_bt4_skip(lzma_mf *dict, uint32_t amount);
351
352#endif
#define assert(condition)
Definition lz4.c:273
Definition inftrees.h:24
Custom functions for memory handling.
Definition base.h:372
Definition common.h:177
Filter options.
Definition filter.h:43
Definition lz_encoder.h:192
Definition lz_decoder.h:47
Definition lz_encoder.h:22
Definition lz_encoder.h:29
uint32_t keep_size_after
Definition lz_encoder.h:51
uint8_t * buffer
Pointer to buffer with data to be compressed.
Definition lz_encoder.h:35
uint32_t * hash
Definition lz_encoder.h:99
uint32_t sons_count
Number of elements in son[].
Definition lz_encoder.h:125
uint32_t match_len_max
Definition lz_encoder.h:114
lzma_action action
Definition lz_encoder.h:119
void(* skip)(lzma_mf *mf, uint32_t num)
Definition lz_encoder.h:97
uint32_t cyclic_pos
Definition lz_encoder.h:101
uint32_t(* find)(lzma_mf *mf, lzma_match *matches)
Definition lz_encoder.h:92
uint32_t read_pos
Definition lz_encoder.h:63
uint32_t offset
Definition lz_encoder.h:58
uint32_t keep_size_before
Definition lz_encoder.h:45
uint32_t hash_mask
Definition lz_encoder.h:103
uint32_t read_ahead
Definition lz_encoder.h:67
uint32_t pending
Definition lz_encoder.h:84
uint32_t read_limit
Definition lz_encoder.h:75
uint32_t hash_count
Number of elements in hash[].
Definition lz_encoder.h:122
uint32_t write_pos
Definition lz_encoder.h:80
uint32_t size
Definition lz_encoder.h:39
uint32_t * son
Definition lz_encoder.h:100
uint32_t nice_len
Maximum length of a match that the match finder will try to find.
Definition lz_encoder.h:109
uint32_t cyclic_size
Definition lz_encoder.h:102
uint32_t depth
Maximum number of loops in the match finder.
Definition lz_encoder.h:106
Hold data and function pointers of the next filter in the chain.
Definition common.h:195
size_t * left
Definition util.h:104
lzma_ret
Return values used by several functions in liblzma.
Definition base.h:57
lzma_action
The ‘action’ argument for lzma_code()
Definition base.h:250
const lzma_allocator const uint8_t size_t uint8_t size_t * out_pos
Definition block.h:528
const lzma_allocator const uint8_t size_t uint8_t * out
Definition block.h:528
const lzma_allocator * allocator
Definition block.h:377
const lzma_filter * filters
Definition container.h:315
lzma_match_finder
Match finders.
Definition lzma12.h:58
uint64_t lzma_vli
Variable-length integer type.
Definition vli.h:63
#define my_min(x, y)
Definition sysdefs.h:185
uint64_t uint64_t uncomp_size
Definition container.h:903
uint64_t uint64_t lzma_bool uint32_t dict_size
Definition container.h:904
const lzma_options_lzma * options
Definition container.h:545
static uint32_t const uint8_t uint32_t len
Definition memcmplen.h:44
void lzma_mf_hc4_skip(lzma_mf *dict, uint32_t amount)
uint32_t lzma_mf_hc3_find(lzma_mf *dict, lzma_match *matches)
uint32_t lzma_mf_bt2_find(lzma_mf *dict, lzma_match *matches)
uint32_t lzma_mf_bt3_find(lzma_mf *dict, lzma_match *matches)
uint32_t lzma_mf_find(lzma_mf *mf, uint32_t *count, lzma_match *matches)
Find matches starting from the current byte.
Definition lz_encoder_mf.c:23
uint32_t lzma_mf_bt4_find(lzma_mf *dict, lzma_match *matches)
void lzma_mf_bt2_skip(lzma_mf *dict, uint32_t amount)
uint64_t lzma_lz_encoder_memusage(const lzma_lz_options *lz_options)
Definition lz_encoder.c:468
void lzma_mf_bt3_skip(lzma_mf *dict, uint32_t amount)
lzma_ret lzma_lz_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters, lzma_ret(*lz_init)(lzma_lz_encoder *lz, const lzma_allocator *allocator, lzma_vli id, const void *options, lzma_lz_options *lz_options))
Definition lz_encoder.c:544
void lzma_mf_hc3_skip(lzma_mf *dict, uint32_t amount)
void lzma_mf_bt4_skip(lzma_mf *dict, uint32_t amount)
uint32_t lzma_mf_hc4_find(lzma_mf *dict, lzma_match *matches)
const void * dict
Definition zbuff.h:76