From d42ff1590b4cae74da995e61896c1fa97c9126b9 Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Thu, 24 Sep 2015 22:46:48 -0500 Subject: [PATCH] bt_matchfinder: add BT_MATCHFINDER_REQUIRED_NBYTES --- include/wimlib/bt_matchfinder.h | 8 +++++++- src/lzx_compress.c | 4 ++-- src/xpress_compress.c | 6 ++++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/include/wimlib/bt_matchfinder.h b/include/wimlib/bt_matchfinder.h index c7f2ba8e..3d295cd9 100644 --- a/include/wimlib/bt_matchfinder.h +++ b/include/wimlib/bt_matchfinder.h @@ -123,6 +123,11 @@ TEMPLATED(bt_right_child)(struct TEMPLATED(bt_matchfinder) *mf, u32 node) return &mf->child_tab[(node << 1) + 1]; } +/* The minimum permissible value of 'max_len' for bt_matchfinder_get_matches() + * and bt_matchfinder_skip_position(). There must be sufficiently many bytes + * remaining to load a 32-bit integer from the *next* position. */ +#define BT_MATCHFINDER_REQUIRED_NBYTES 5 + /* Advance the binary tree matchfinder by one byte, optionally recording * matches. @record_matches should be a compile-time constant. */ static inline struct lz_match * @@ -266,7 +271,8 @@ TEMPLATED(bt_matchfinder_advance_one_byte)(struct TEMPLATED(bt_matchfinder) * co * The current position in the input buffer (the position of the sequence * being matched against). * @max_len - * The maximum permissible match length at this position. Must be >= 5. + * The maximum permissible match length at this position. Must be >= + * BT_MATCHFINDER_REQUIRED_NBYTES. * @nice_len * Stop searching if a match of at least this length is found. * Must be <= @max_len. diff --git a/src/lzx_compress.c b/src/lzx_compress.c index 33943af7..ca9906e7 100644 --- a/src/lzx_compress.c +++ b/src/lzx_compress.c @@ -1809,7 +1809,7 @@ lzx_compress_near_optimal(struct lzx_compressor *c, if (unlikely(max_len > in_end - in_next)) { max_len = in_end - in_next; nice_len = min(max_len, nice_len); - if (unlikely(max_len < 5)) { + if (unlikely(max_len < BT_MATCHFINDER_REQUIRED_NBYTES)) { in_next++; cache_ptr->length = 0; cache_ptr++; @@ -1849,7 +1849,7 @@ lzx_compress_near_optimal(struct lzx_compressor *c, if (unlikely(max_len > in_end - in_next)) { max_len = in_end - in_next; nice_len = min(max_len, nice_len); - if (unlikely(max_len < 5)) { + if (unlikely(max_len < BT_MATCHFINDER_REQUIRED_NBYTES)) { in_next++; cache_ptr->length = 0; cache_ptr++; diff --git a/src/xpress_compress.c b/src/xpress_compress.c index 5c3249d0..a04bc52d 100644 --- a/src/xpress_compress.c +++ b/src/xpress_compress.c @@ -920,7 +920,8 @@ xpress_find_matches(struct xpress_compressor * restrict c, /* If we've found so many matches that the cache might overflow * if we keep finding more, then stop finding matches. This * case is very unlikely. */ - if (unlikely(cache_ptr >= c->cache_overflow_mark || max_len < 5)) + if (unlikely(cache_ptr >= c->cache_overflow_mark || + max_len < BT_MATCHFINDER_REQUIRED_NBYTES)) break; matches = cache_ptr; @@ -955,7 +956,8 @@ xpress_find_matches(struct xpress_compressor * restrict c, * highly compressible, so it doesn't matter as much what we do. */ if (best_len >= nice_len) { - if (unlikely(best_len + 5 >= max_len)) + if (unlikely(best_len + + BT_MATCHFINDER_REQUIRED_NBYTES >= max_len)) break; --best_len; do { -- 2.43.0