1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
5 /// \file lz_encoder_mf.c
6 /// \brief Match finders
8 // Authors: Igor Pavlov
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "lz_encoder.h"
14 #include "lz_encoder_hash.h"
15 #include "memcmplen.h"
18 /// \brief Find matches starting from the current byte
20 /// \return The length of the longest match found
22 lzma_mf_find(lzma_mf
*mf
, uint32_t *count_ptr
, lzma_match
*matches
)
24 // Call the match finder. It returns the number of length-distance
26 // FIXME: Minimum count is zero, what _exactly_ is the maximum?
27 const uint32_t count
= mf
->find(mf
, matches
);
29 // Length of the longest match; assume that no matches were found
30 // and thus the maximum length is zero.
31 uint32_t len_best
= 0;
35 // Validate the matches.
36 for (uint32_t i
= 0; i
< count
; ++i
) {
37 assert(matches
[i
].len
<= mf
->nice_len
);
38 assert(matches
[i
].dist
< mf
->read_pos
);
39 assert(memcmp(mf_ptr(mf
) - 1,
40 mf_ptr(mf
) - matches
[i
].dist
- 2,
41 matches
[i
].len
) == 0);
45 // The last used element in the array contains
47 len_best
= matches
[count
- 1].len
;
49 // If a match of maximum search length was found, try to
50 // extend the match to maximum possible length.
51 if (len_best
== mf
->nice_len
) {
52 // The limit for the match length is either the
53 // maximum match length supported by the LZ-based
54 // encoder or the number of bytes left in the
55 // dictionary, whichever is smaller.
56 uint32_t limit
= mf_avail(mf
) + 1;
57 if (limit
> mf
->match_len_max
)
58 limit
= mf
->match_len_max
;
60 // Pointer to the byte we just ran through
62 const uint8_t *p1
= mf_ptr(mf
) - 1;
64 // Pointer to the beginning of the match. We need -1
65 // here because the match distances are zero based.
66 const uint8_t *p2
= p1
- matches
[count
- 1].dist
- 1;
68 len_best
= lzma_memcmplen(p1
, p2
, len_best
, limit
);
74 // Finally update the read position to indicate that match finder was
75 // run for this dictionary offset.
82 /// Hash value to indicate unused element in the hash. Since we start the
83 /// positions from dict_size + 1, zero is always too far to qualify
84 /// as usable match position.
85 #define EMPTY_HASH_VALUE 0
88 /// Normalization must be done when lzma_mf.offset + lzma_mf.read_pos
89 /// reaches MUST_NORMALIZE_POS.
90 #define MUST_NORMALIZE_POS UINT32_MAX
93 /// \brief Normalizes hash values
95 /// The hash arrays store positions of match candidates. The positions are
96 /// relative to an arbitrary offset that is not the same as the absolute
97 /// offset in the input stream. The relative position of the current byte
98 /// is lzma_mf.offset + lzma_mf.read_pos. The distances of the matches are
99 /// the differences of the current read position and the position found from
102 /// To prevent integer overflows of the offsets stored in the hash arrays,
103 /// we need to "normalize" the stored values now and then. During the
104 /// normalization, we drop values that indicate distance greater than the
105 /// dictionary size, thus making space for new values.
107 normalize(lzma_mf
*mf
)
109 assert(mf
->read_pos
+ mf
->offset
== MUST_NORMALIZE_POS
);
111 // In future we may not want to touch the lowest bits, because there
112 // may be match finders that use larger resolution than one byte.
113 const uint32_t subvalue
114 = (MUST_NORMALIZE_POS
- mf
->cyclic_size
);
115 // & ~((UINT32_C(1) << 10) - 1);
117 for (uint32_t i
= 0; i
< mf
->hash_count
; ++i
) {
118 // If the distance is greater than the dictionary size,
119 // we can simply mark the hash element as empty.
120 if (mf
->hash
[i
] <= subvalue
)
121 mf
->hash
[i
] = EMPTY_HASH_VALUE
;
123 mf
->hash
[i
] -= subvalue
;
126 for (uint32_t i
= 0; i
< mf
->sons_count
; ++i
) {
127 // Do the same for mf->son.
129 // NOTE: There may be uninitialized elements in mf->son.
130 // Valgrind may complain that the "if" below depends on
131 // an uninitialized value. In this case it is safe to ignore
132 // the warning. See also the comments in lz_encoder_init()
134 if (mf
->son
[i
] <= subvalue
)
135 mf
->son
[i
] = EMPTY_HASH_VALUE
;
137 mf
->son
[i
] -= subvalue
;
140 // Update offset to match the new locations.
141 mf
->offset
-= subvalue
;
147 /// Mark the current byte as processed from point of view of the match finder.
149 move_pos(lzma_mf
*mf
)
151 if (++mf
->cyclic_pos
== mf
->cyclic_size
)
155 assert(mf
->read_pos
<= mf
->write_pos
);
157 if (unlikely(mf
->read_pos
+ mf
->offset
== UINT32_MAX
))
162 /// When flushing, we cannot run the match finder unless there is nice_len
163 /// bytes available in the dictionary. Instead, we skip running the match
164 /// finder (indicating that no match was found), and count how many bytes we
165 /// have ignored this way.
167 /// When new data is given after the flushing was completed, the match finder
168 /// is restarted by rewinding mf->read_pos backwards by mf->pending. Then
169 /// the missed bytes are added to the hash using the match finder's skip
170 /// function (with small amount of input, it may start using mf->pending
171 /// again if flushing).
173 /// Due to this rewinding, we don't touch cyclic_pos or test for
174 /// normalization. It will be done when the match finder's skip function
175 /// catches up after a flush.
177 move_pending(lzma_mf
*mf
)
180 assert(mf
->read_pos
<= mf
->write_pos
);
185 /// Calculate len_limit and determine if there is enough input to run
186 /// the actual match finder code. Sets up "cur" and "pos". This macro
187 /// is used by all find functions and binary tree skip functions. Hash
188 /// chain skip function doesn't need len_limit so a simpler code is used
190 #define header(is_bt, len_min, ret_op) \
191 uint32_t len_limit = mf_avail(mf); \
192 if (mf->nice_len <= len_limit) { \
193 len_limit = mf->nice_len; \
194 } else if (len_limit < (len_min) \
195 || (is_bt && mf->action == LZMA_SYNC_FLUSH)) { \
196 assert(mf->action != LZMA_RUN); \
200 const uint8_t *cur = mf_ptr(mf); \
201 const uint32_t pos = mf->read_pos + mf->offset
204 /// Header for find functions. "return 0" indicates that zero matches
206 #define header_find(is_bt, len_min) \
207 header(is_bt, len_min, return 0); \
208 uint32_t matches_count = 0
211 /// Header for a loop in a skip function. "continue" tells to skip the rest
212 /// of the code in the loop.
213 #define header_skip(is_bt, len_min) \
214 header(is_bt, len_min, continue)
217 /// Calls hc_find_func() or bt_find_func() and calculates the total number
218 /// of matches found. Updates the dictionary position and returns the number
219 /// of matches found.
220 #define call_find(func, len_best) \
222 matches_count = (uint32_t)(func(len_limit, pos, cur, cur_match, \
223 mf->depth, mf->son, \
224 mf->cyclic_pos, mf->cyclic_size, \
225 matches + matches_count, len_best) \
228 return matches_count; \
236 #if defined(HAVE_MF_HC3) || defined(HAVE_MF_HC4)
239 /// \param len_limit Don't look for matches longer than len_limit.
240 /// \param pos lzma_mf.read_pos + lzma_mf.offset
241 /// \param cur Pointer to current byte (mf_ptr(mf))
242 /// \param cur_match Start position of the current match candidate
243 /// \param depth Maximum length of the hash chain
244 /// \param son lzma_mf.son (contains the hash chain)
245 /// \param cyclic_pos lzma_mf.cyclic_pos
246 /// \param cyclic_size lzma_mf_cyclic_size
247 /// \param matches Array to hold the matches.
248 /// \param len_best The length of the longest match found so far.
251 const uint32_t len_limit
,
253 const uint8_t *const cur
,
257 const uint32_t cyclic_pos
,
258 const uint32_t cyclic_size
,
262 son
[cyclic_pos
] = cur_match
;
265 const uint32_t delta
= pos
- cur_match
;
266 if (depth
-- == 0 || delta
>= cyclic_size
)
269 const uint8_t *const pb
= cur
- delta
;
270 cur_match
= son
[cyclic_pos
- delta
271 + (delta
> cyclic_pos
? cyclic_size
: 0)];
273 if (pb
[len_best
] == cur
[len_best
] && pb
[0] == cur
[0]) {
274 uint32_t len
= lzma_memcmplen(pb
, cur
, 1, len_limit
);
276 if (len_best
< len
) {
279 matches
->dist
= delta
- 1;
282 if (len
== len_limit
)
290 #define hc_find(len_best) \
291 call_find(hc_find_func, len_best)
296 mf->son[mf->cyclic_pos] = cur_match; \
305 lzma_mf_hc3_find(lzma_mf
*mf
, lzma_match
*matches
)
307 header_find(false, 3);
311 const uint32_t delta2
= pos
- mf
->hash
[hash_2_value
];
312 const uint32_t cur_match
= mf
->hash
[FIX_3_HASH_SIZE
+ hash_value
];
314 mf
->hash
[hash_2_value
] = pos
;
315 mf
->hash
[FIX_3_HASH_SIZE
+ hash_value
] = pos
;
317 uint32_t len_best
= 2;
319 if (delta2
< mf
->cyclic_size
&& *(cur
- delta2
) == *cur
) {
320 len_best
= lzma_memcmplen(cur
- delta2
, cur
,
321 len_best
, len_limit
);
323 matches
[0].len
= len_best
;
324 matches
[0].dist
= delta2
- 1;
327 if (len_best
== len_limit
) {
329 return 1; // matches_count
338 lzma_mf_hc3_skip(lzma_mf
*mf
, uint32_t amount
)
341 if (mf_avail(mf
) < 3) {
346 const uint8_t *cur
= mf_ptr(mf
);
347 const uint32_t pos
= mf
->read_pos
+ mf
->offset
;
351 const uint32_t cur_match
352 = mf
->hash
[FIX_3_HASH_SIZE
+ hash_value
];
354 mf
->hash
[hash_2_value
] = pos
;
355 mf
->hash
[FIX_3_HASH_SIZE
+ hash_value
] = pos
;
359 } while (--amount
!= 0);
366 lzma_mf_hc4_find(lzma_mf
*mf
, lzma_match
*matches
)
368 header_find(false, 4);
372 uint32_t delta2
= pos
- mf
->hash
[hash_2_value
];
373 const uint32_t delta3
374 = pos
- mf
->hash
[FIX_3_HASH_SIZE
+ hash_3_value
];
375 const uint32_t cur_match
= mf
->hash
[FIX_4_HASH_SIZE
+ hash_value
];
377 mf
->hash
[hash_2_value
] = pos
;
378 mf
->hash
[FIX_3_HASH_SIZE
+ hash_3_value
] = pos
;
379 mf
->hash
[FIX_4_HASH_SIZE
+ hash_value
] = pos
;
381 uint32_t len_best
= 1;
383 if (delta2
< mf
->cyclic_size
&& *(cur
- delta2
) == *cur
) {
386 matches
[0].dist
= delta2
- 1;
390 if (delta2
!= delta3
&& delta3
< mf
->cyclic_size
391 && *(cur
- delta3
) == *cur
) {
393 matches
[matches_count
++].dist
= delta3
- 1;
397 if (matches_count
!= 0) {
398 len_best
= lzma_memcmplen(cur
- delta2
, cur
,
399 len_best
, len_limit
);
401 matches
[matches_count
- 1].len
= len_best
;
403 if (len_best
== len_limit
) {
405 return matches_count
;
417 lzma_mf_hc4_skip(lzma_mf
*mf
, uint32_t amount
)
420 if (mf_avail(mf
) < 4) {
425 const uint8_t *cur
= mf_ptr(mf
);
426 const uint32_t pos
= mf
->read_pos
+ mf
->offset
;
430 const uint32_t cur_match
431 = mf
->hash
[FIX_4_HASH_SIZE
+ hash_value
];
433 mf
->hash
[hash_2_value
] = pos
;
434 mf
->hash
[FIX_3_HASH_SIZE
+ hash_3_value
] = pos
;
435 mf
->hash
[FIX_4_HASH_SIZE
+ hash_value
] = pos
;
439 } while (--amount
!= 0);
448 #if defined(HAVE_MF_BT2) || defined(HAVE_MF_BT3) || defined(HAVE_MF_BT4)
451 const uint32_t len_limit
,
453 const uint8_t *const cur
,
457 const uint32_t cyclic_pos
,
458 const uint32_t cyclic_size
,
462 uint32_t *ptr0
= son
+ (cyclic_pos
<< 1) + 1;
463 uint32_t *ptr1
= son
+ (cyclic_pos
<< 1);
469 const uint32_t delta
= pos
- cur_match
;
470 if (depth
-- == 0 || delta
>= cyclic_size
) {
471 *ptr0
= EMPTY_HASH_VALUE
;
472 *ptr1
= EMPTY_HASH_VALUE
;
476 uint32_t *const pair
= son
+ ((cyclic_pos
- delta
477 + (delta
> cyclic_pos
? cyclic_size
: 0))
480 const uint8_t *const pb
= cur
- delta
;
481 uint32_t len
= my_min(len0
, len1
);
483 if (pb
[len
] == cur
[len
]) {
484 len
= lzma_memcmplen(pb
, cur
, len
+ 1, len_limit
);
486 if (len_best
< len
) {
489 matches
->dist
= delta
- 1;
492 if (len
== len_limit
) {
500 if (pb
[len
] < cur
[len
]) {
517 const uint32_t len_limit
,
519 const uint8_t *const cur
,
523 const uint32_t cyclic_pos
,
524 const uint32_t cyclic_size
)
526 uint32_t *ptr0
= son
+ (cyclic_pos
<< 1) + 1;
527 uint32_t *ptr1
= son
+ (cyclic_pos
<< 1);
533 const uint32_t delta
= pos
- cur_match
;
534 if (depth
-- == 0 || delta
>= cyclic_size
) {
535 *ptr0
= EMPTY_HASH_VALUE
;
536 *ptr1
= EMPTY_HASH_VALUE
;
540 uint32_t *pair
= son
+ ((cyclic_pos
- delta
541 + (delta
> cyclic_pos
? cyclic_size
: 0))
543 const uint8_t *pb
= cur
- delta
;
544 uint32_t len
= my_min(len0
, len1
);
546 if (pb
[len
] == cur
[len
]) {
547 len
= lzma_memcmplen(pb
, cur
, len
+ 1, len_limit
);
549 if (len
== len_limit
) {
556 if (pb
[len
] < cur
[len
]) {
571 #define bt_find(len_best) \
572 call_find(bt_find_func, len_best)
576 bt_skip_func(len_limit, pos, cur, cur_match, mf->depth, \
577 mf->son, mf->cyclic_pos, \
587 lzma_mf_bt2_find(lzma_mf
*mf
, lzma_match
*matches
)
589 header_find(true, 2);
593 const uint32_t cur_match
= mf
->hash
[hash_value
];
594 mf
->hash
[hash_value
] = pos
;
601 lzma_mf_bt2_skip(lzma_mf
*mf
, uint32_t amount
)
604 header_skip(true, 2);
608 const uint32_t cur_match
= mf
->hash
[hash_value
];
609 mf
->hash
[hash_value
] = pos
;
613 } while (--amount
!= 0);
620 lzma_mf_bt3_find(lzma_mf
*mf
, lzma_match
*matches
)
622 header_find(true, 3);
626 const uint32_t delta2
= pos
- mf
->hash
[hash_2_value
];
627 const uint32_t cur_match
= mf
->hash
[FIX_3_HASH_SIZE
+ hash_value
];
629 mf
->hash
[hash_2_value
] = pos
;
630 mf
->hash
[FIX_3_HASH_SIZE
+ hash_value
] = pos
;
632 uint32_t len_best
= 2;
634 if (delta2
< mf
->cyclic_size
&& *(cur
- delta2
) == *cur
) {
635 len_best
= lzma_memcmplen(
636 cur
, cur
- delta2
, len_best
, len_limit
);
638 matches
[0].len
= len_best
;
639 matches
[0].dist
= delta2
- 1;
642 if (len_best
== len_limit
) {
644 return 1; // matches_count
653 lzma_mf_bt3_skip(lzma_mf
*mf
, uint32_t amount
)
656 header_skip(true, 3);
660 const uint32_t cur_match
661 = mf
->hash
[FIX_3_HASH_SIZE
+ hash_value
];
663 mf
->hash
[hash_2_value
] = pos
;
664 mf
->hash
[FIX_3_HASH_SIZE
+ hash_value
] = pos
;
668 } while (--amount
!= 0);
675 lzma_mf_bt4_find(lzma_mf
*mf
, lzma_match
*matches
)
677 header_find(true, 4);
681 uint32_t delta2
= pos
- mf
->hash
[hash_2_value
];
682 const uint32_t delta3
683 = pos
- mf
->hash
[FIX_3_HASH_SIZE
+ hash_3_value
];
684 const uint32_t cur_match
= mf
->hash
[FIX_4_HASH_SIZE
+ hash_value
];
686 mf
->hash
[hash_2_value
] = pos
;
687 mf
->hash
[FIX_3_HASH_SIZE
+ hash_3_value
] = pos
;
688 mf
->hash
[FIX_4_HASH_SIZE
+ hash_value
] = pos
;
690 uint32_t len_best
= 1;
692 if (delta2
< mf
->cyclic_size
&& *(cur
- delta2
) == *cur
) {
695 matches
[0].dist
= delta2
- 1;
699 if (delta2
!= delta3
&& delta3
< mf
->cyclic_size
700 && *(cur
- delta3
) == *cur
) {
702 matches
[matches_count
++].dist
= delta3
- 1;
706 if (matches_count
!= 0) {
707 len_best
= lzma_memcmplen(
708 cur
, cur
- delta2
, len_best
, len_limit
);
710 matches
[matches_count
- 1].len
= len_best
;
712 if (len_best
== len_limit
) {
714 return matches_count
;
726 lzma_mf_bt4_skip(lzma_mf
*mf
, uint32_t amount
)
729 header_skip(true, 4);
733 const uint32_t cur_match
734 = mf
->hash
[FIX_4_HASH_SIZE
+ hash_value
];
736 mf
->hash
[hash_2_value
] = pos
;
737 mf
->hash
[FIX_3_HASH_SIZE
+ hash_3_value
] = pos
;
738 mf
->hash
[FIX_4_HASH_SIZE
+ hash_value
] = pos
;
742 } while (--amount
!= 0);