From 35ec6d73da8f021636f5993e77596dee3989c7a2 Mon Sep 17 00:00:00 2001 From: Emmanuel Marty Date: Mon, 27 Feb 2023 08:26:42 +0100 Subject: [PATCH] Optimal LZSA1 compression --- src/matchfinder.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/matchfinder.c b/src/matchfinder.c index 6e4fee3..49092d6 100644 --- a/src/matchfinder.c +++ b/src/matchfinder.c @@ -203,6 +203,7 @@ static int lzsa_find_matches_at(lzsa_compressor *pCompressor, const int nOffset, unsigned int match_pos; lzsa_match *matchptr; unsigned int nPrevOffset = 0; + unsigned char nV1OffsetFound[2] = { 0, 0 }; /** * Find matches using intervals @@ -281,14 +282,25 @@ static int lzsa_find_matches_at(lzsa_compressor *pCompressor, const int nOffset, if (nMatchOffset <= MAX_OFFSET && nMatchOffset != nPrevOffset) { if (pCompressor->format_version >= 2) { matchptr->length = (const unsigned short)(ref >> (LCP_SHIFT + TAG_BITS)); + matchptr->offset = (const unsigned short)nMatchOffset; + matchptr++; + + nPrevOffset = nMatchOffset; } else { - matchptr->length = (const unsigned short)(ref >> LCP_SHIFT); - } - matchptr->offset = (const unsigned short)nMatchOffset; - matchptr++; + unsigned int nV1OffsetType = (nMatchOffset <= 256) ? 0 : 1; - nPrevOffset = nMatchOffset; + if (!nV1OffsetFound[nV1OffsetType]) { + matchptr->length = (const unsigned short)(ref >> LCP_SHIFT); + matchptr->offset = (const unsigned short)nMatchOffset; + + if (matchptr->length < 256) + nV1OffsetFound[nV1OffsetType] = 1; + matchptr++; + + nPrevOffset = nMatchOffset; + } + } } }