bt_matchfinder: add BT_MATCHFINDER_REQUIRED_NBYTES
authorEric Biggers <ebiggers3@gmail.com>
Fri, 25 Sep 2015 03:46:48 +0000 (22:46 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Sun, 27 Sep 2015 14:41:30 +0000 (09:41 -0500)
include/wimlib/bt_matchfinder.h
src/lzx_compress.c
src/xpress_compress.c

index c7f2ba8..3d295cd 100644 (file)
@@ -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.
index 33943af..ca9906e 100644 (file)
@@ -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++;
index 5c3249d..a04bc52 100644 (file)
@@ -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 {