From c86d38ba63d25498360a8eae759a90399f1ce4b9 Mon Sep 17 00:00:00 2001 From: emmanuel-marty Date: Fri, 5 Apr 2019 09:28:16 +0200 Subject: [PATCH] Reduce the number of literals required at the end of a compressed block --- src/expand.c | 6 +++--- src/shrink.c | 10 ++++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/expand.c b/src/expand.c index 8ad316c..6f0121c 100755 --- a/src/expand.c +++ b/src/expand.c @@ -115,8 +115,8 @@ static inline FORCE_INLINE int lzsa_expand_match_slow(const unsigned char **ppIn int nMaxFast = nMatchLen; if (nMaxFast > (pCurOutData - pSrc)) nMaxFast = (int)(pCurOutData - pSrc); - if ((pCurOutData + nMaxFast) > pOutDataFastEnd) - nMaxFast = (int)(pOutDataFastEnd - pCurOutData); + if ((pCurOutData + nMaxFast) > (pOutDataFastEnd - 15)) + nMaxFast = (int)(pOutDataFastEnd - 15 - pCurOutData); if (nMaxFast > 0) { const unsigned char *pCopySrc = pSrc; @@ -202,7 +202,7 @@ int lzsa_expand_block(const unsigned char *pInBlock, int nBlockSize, unsigned ch return -1; int nMatchLen = (int)((unsigned int)(token & 0x0f)); - if (nMatchLen < (16 - MIN_MATCH_SIZE + 1) && (pSrc + MIN_MATCH_SIZE + nMatchLen) < pCurOutData) { + if (nMatchLen < (16 - MIN_MATCH_SIZE + 1) && (pSrc + MIN_MATCH_SIZE + nMatchLen) < pCurOutData && pCurOutData < pOutDataFastEnd) { memcpy(pCurOutData, pSrc, 16); pCurOutData += (MIN_MATCH_SIZE + nMatchLen); } diff --git a/src/shrink.c b/src/shrink.c index 1659813..364a41f 100755 --- a/src/shrink.c +++ b/src/shrink.c @@ -44,6 +44,9 @@ #define LEAVE_ALONE_MATCH_SIZE 1000 +#define LAST_MATCH_OFFSET 4 +#define LAST_LITERALS 1 + /** One match */ typedef struct _lzsa_match { unsigned short length; @@ -357,12 +360,12 @@ static void lzsa_find_all_matches(lsza_compressor *pCompressor, const int nStart int m; for (m = 0; m < NMATCHES_PER_OFFSET; m++) { - if (nMatches <= m || i >= (nEndOffset - 11)) { + if (nMatches <= m || i > (nEndOffset - LAST_MATCH_OFFSET)) { pMatch->length = 0; pMatch->offset = 0; } else { - int nMaxLen = (nEndOffset - 5) - i; + int nMaxLen = (nEndOffset - LAST_LITERALS) - i; if (nMaxLen < 0) nMaxLen = 0; if (pMatch->length > nMaxLen) @@ -371,7 +374,6 @@ static void lzsa_find_all_matches(lsza_compressor *pCompressor, const int nStart pMatch++; } - } } @@ -619,7 +621,7 @@ static int lzsa_write_block(lsza_compressor *pCompressor, const unsigned char *p } } - if (nNumLiterals != 0) { + { int nNibbleLiteralsLen = (nNumLiterals >= LITERALS_RUN_LEN) ? LITERALS_RUN_LEN : nNumLiterals; int nTokenSize = 1 /* nibble */ + lzsa_get_literals_varlen_size(nNumLiterals) + nNumLiterals;