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
2//
6// Authors: Igor Pavlov
7// Lasse Collin
8//
9// This file has been put into the public domain.
10// You can do whatever you want with this file.
11//
13
14#ifndef LZMA_LZ_ENCODER_H
15#define LZMA_LZ_ENCODER_H
16
17#include "common.h"
18
19
22typedef struct {
23 uint32_t len;
24 uint32_t dist;
26
27
28typedef struct lzma_mf_s lzma_mf;
29struct lzma_mf_s {
31 // In Window //
33
35 uint8_t *buffer;
36
39 uint32_t size;
40
45 uint32_t keep_size_before;
46
51 uint32_t keep_size_after;
52
58 uint32_t offset;
59
63 uint32_t read_pos;
64
67 uint32_t read_ahead;
68
75 uint32_t read_limit;
76
80 uint32_t write_pos;
81
84 uint32_t pending;
85
87 // Match Finder //
89
92 uint32_t (*find)(lzma_mf *mf, lzma_match *matches);
93
97 void (*skip)(lzma_mf *mf, uint32_t num);
98
99 uint32_t *hash;
100 uint32_t *son;
101 uint32_t cyclic_pos;
102 uint32_t cyclic_size; // Must be dictionary size + 1.
103 uint32_t hash_mask;
104
106 uint32_t depth;
107
109 uint32_t nice_len;
110
114 uint32_t match_len_max;
115
120
122 uint32_t hash_count;
123
125 uint32_t sons_count;
126};
127
128
129typedef struct {
132 size_t before_size;
133
135 size_t dict_size;
136
139 size_t after_size;
140
144 size_t match_len_max;
145
148 size_t nice_len;
149
151 lzma_match_finder match_finder;
152
154 uint32_t depth;
155
157 const uint8_t *preset_dict;
158
159 uint32_t preset_dict_size;
160
162
163
164// The total usable buffer space at any moment outside the match finder:
165// before_size + dict_size + after_size + match_len_max
166//
167// In reality, there's some extra space allocated to prevent the number of
168// memmove() calls reasonable. The bigger the dict_size is, the bigger
169// this extra buffer will be since with bigger dictionaries memmove() would
170// also take longer.
171//
172// A single encoder loop in the LZ-based encoder may call the match finder
173// (mf_find() or mf_skip()) at most after_size times. In other words,
174// a single encoder loop may increment lzma_mf.read_pos at most after_size
175// times. Since matches are looked up to
176// lzma_mf.buffer[lzma_mf.read_pos + match_len_max - 1], the total
177// amount of extra buffer needed after dict_size becomes
178// after_size + match_len_max.
179//
180// before_size has two uses. The first one is to keep literals available
181// in cases when the LZ-based encoder has made some read ahead.
182// TODO: Maybe this could be changed by making the LZ-based encoders to
183// store the actual literals as they do with length-distance pairs.
184//
185// Algorithms such as LZMA2 first try to compress a chunk, and then check
186// if the encoded result is smaller than the uncompressed one. If the chunk
187// was uncompressible, it is better to store it in uncompressed form in
188// the output stream. To do this, the whole uncompressed chunk has to be
189// still available in the history buffer. before_size achieves that.
190
191
192typedef struct {
194 void *coder;
195
197 lzma_ret (*code)(void *coder,
198 lzma_mf *restrict mf, uint8_t *restrict out,
199 size_t *restrict out_pos, size_t out_size);
200
202 void (*end)(void *coder, const lzma_allocator *allocator);
203
205 lzma_ret (*options_update)(void *coder, const lzma_filter *filter);
206
208 lzma_ret (*set_out_limit)(void *coder, uint64_t *uncomp_size,
209 uint64_t out_limit);
210
212
213
214// Basic steps:
215// 1. Input gets copied into the dictionary.
216// 2. Data in dictionary gets run through the match finder byte by byte.
217// 3. The literals and matches are encoded using e.g. LZMA.
218//
219// The bytes that have been ran through the match finder, but not encoded yet,
220// are called `read ahead'.
221
222
225static inline uint32_t
226mf_get_hash_bytes(lzma_match_finder match_finder)
227{
228 return (uint32_t)match_finder & 0x0F;
229}
230
231
233static inline const uint8_t *
234mf_ptr(const lzma_mf *mf)
235{
236 return mf->buffer + mf->read_pos;
237}
238
239
241static inline uint32_t
242mf_avail(const lzma_mf *mf)
243{
244 return mf->write_pos - mf->read_pos;
245}
246
247
250static inline uint32_t
251mf_unencoded(const lzma_mf *mf)
252{
253 return mf->write_pos - mf->read_pos + mf->read_ahead;
254}
255
256
264static inline uint32_t
265mf_position(const lzma_mf *mf)
266{
267 return mf->read_pos - mf->read_ahead;
268}
269
270
272#define mf_find lzma_mf_find
273
274
279static inline void
280mf_skip(lzma_mf *mf, uint32_t amount)
281{
282 if (amount != 0) {
283 mf->skip(mf, amount);
284 mf->read_ahead += amount;
285 }
286}
287
288
291static inline void
292mf_read(lzma_mf *mf, uint8_t *out, size_t *out_pos, size_t out_size,
293 size_t *left)
294{
295 const size_t out_avail = out_size - *out_pos;
296 const size_t copy_size = my_min(out_avail, *left);
297
298 assert(mf->read_ahead == 0);
299 assert(mf->read_pos >= *left);
300
301 memcpy(out + *out_pos, mf->buffer + mf->read_pos - *left,
302 copy_size);
303
304 *out_pos += copy_size;
305 *left -= copy_size;
306 return;
307}
308
309
313 lzma_ret (*lz_init)(lzma_lz_encoder *lz,
315 lzma_vli id, const void *options,
316 lzma_lz_options *lz_options));
317
318
319extern uint64_t lzma_lz_encoder_memusage(const lzma_lz_options *lz_options);
320
321
322// These are only for LZ encoder's internal use.
323extern uint32_t lzma_mf_find(
324 lzma_mf *mf, uint32_t *count, lzma_match *matches);
325
326extern uint32_t lzma_mf_hc3_find(lzma_mf *dict, lzma_match *matches);
327extern void lzma_mf_hc3_skip(lzma_mf *dict, uint32_t amount);
328
329extern uint32_t lzma_mf_hc4_find(lzma_mf *dict, lzma_match *matches);
330extern void lzma_mf_hc4_skip(lzma_mf *dict, uint32_t amount);
331
332extern uint32_t lzma_mf_bt2_find(lzma_mf *dict, lzma_match *matches);
333extern void lzma_mf_bt2_skip(lzma_mf *dict, uint32_t amount);
334
335extern uint32_t lzma_mf_bt3_find(lzma_mf *dict, lzma_match *matches);
336extern void lzma_mf_bt3_skip(lzma_mf *dict, uint32_t amount);
337
338extern uint32_t lzma_mf_bt4_find(lzma_mf *dict, lzma_match *matches);
339extern void lzma_mf_bt4_skip(lzma_mf *dict, uint32_t amount);
340
341#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