diff --git a/src/dictionary.c b/src/dictionary.c index fbc4f52..099f422 100644 --- a/src/dictionary.c +++ b/src/dictionary.c @@ -32,6 +32,7 @@ #include #include +#include #include "format.h" #include "lib.h" diff --git a/src/expand_block_v1.c b/src/expand_block_v1.c index c248fb3..0f5b8a7 100644 --- a/src/expand_block_v1.c +++ b/src/expand_block_v1.c @@ -120,7 +120,7 @@ static inline FORCE_INLINE int lzsa_build_match_len_v1(const unsigned char **ppI * * @return size of decompressed data in bytes, or -1 for error */ -int lzsa_decompressor_expand_block_v1(const unsigned char *pInBlock, int nBlockSize, unsigned char *pOutData, int nOutDataOffset, int nBlockMaxSize) { +int lzsa_decompressor_expand_block_v1(const unsigned char *pInBlock, const int nBlockSize, unsigned char *pOutData, const int nOutDataOffset, const int nBlockMaxSize) { const unsigned char *pInBlockEnd = pInBlock + nBlockSize; unsigned char *pCurOutData = pOutData + nOutDataOffset; const unsigned char *pOutDataEnd = pCurOutData + nBlockMaxSize; diff --git a/src/expand_block_v1.h b/src/expand_block_v1.h index a1d5651..0b71e9d 100644 --- a/src/expand_block_v1.h +++ b/src/expand_block_v1.h @@ -44,6 +44,6 @@ * * @return size of decompressed data in bytes, or -1 for error */ -int lzsa_decompressor_expand_block_v1(const unsigned char *pInBlock, int nBlockSize, unsigned char *pOutData, int nOutDataOffset, int nBlockMaxSize); +int lzsa_decompressor_expand_block_v1(const unsigned char *pInBlock, const int nBlockSize, unsigned char *pOutData, const int nOutDataOffset, const int nBlockMaxSize); #endif /* _EXPAND_BLOCK_V1_H */ diff --git a/src/expand_block_v2.c b/src/expand_block_v2.c index e024b4c..a45c67b 100644 --- a/src/expand_block_v2.c +++ b/src/expand_block_v2.c @@ -109,7 +109,7 @@ static inline FORCE_INLINE int lzsa_build_len_v2(const unsigned char **ppInBlock * * @return size of decompressed data in bytes, or -1 for error */ -int lzsa_decompressor_expand_block_v2(const unsigned char *pInBlock, int nBlockSize, unsigned char *pOutData, int nOutDataOffset, int nBlockMaxSize) { +int lzsa_decompressor_expand_block_v2(const unsigned char *pInBlock, const int nBlockSize, unsigned char *pOutData, const int nOutDataOffset, const int nBlockMaxSize) { const unsigned char *pInBlockEnd = pInBlock + nBlockSize; unsigned char *pCurOutData = pOutData + nOutDataOffset; const unsigned char *pOutDataEnd = pCurOutData + nBlockMaxSize; @@ -147,7 +147,7 @@ int lzsa_decompressor_expand_block_v2(const unsigned char *pInBlock, int nBlockS } if (pInBlock < pInBlockEnd) { /* The last token in the block does not include match information */ - unsigned char nOffsetMode = token & 0xc0; + const unsigned char nOffsetMode = token & 0xc0; unsigned int nValue; switch (nOffsetMode) { diff --git a/src/expand_block_v2.h b/src/expand_block_v2.h index f612f7a..bca5e6b 100644 --- a/src/expand_block_v2.h +++ b/src/expand_block_v2.h @@ -44,6 +44,6 @@ * * @return size of decompressed data in bytes, or -1 for error */ -int lzsa_decompressor_expand_block_v2(const unsigned char *pInBlock, int nBlockSize, unsigned char *pOutData, int nOutDataOffset, int nBlockMaxSize); +int lzsa_decompressor_expand_block_v2(const unsigned char *pInBlock, const int nBlockSize, unsigned char *pOutData, const int nOutDataOffset, const int nBlockMaxSize); #endif /* _EXPAND_BLOCK_V2_H */ diff --git a/src/expand_context.c b/src/expand_context.c index b02620d..188231b 100644 --- a/src/expand_context.c +++ b/src/expand_context.c @@ -50,7 +50,7 @@ * * @return size of decompressed data in bytes, or -1 for error */ -int lzsa_decompressor_expand_block(unsigned char *pInBlock, int nBlockSize, unsigned char *pOutData, int nOutDataOffset, int nBlockMaxSize, const int nFormatVersion, const int nFlags) { +int lzsa_decompressor_expand_block(unsigned char *pInBlock, const int nBlockSize, unsigned char *pOutData, const int nOutDataOffset, const int nBlockMaxSize, const int nFormatVersion, const int nFlags) { int nDecompressedSize; if (nFlags & LZSA_FLAG_RAW_BACKWARD) { diff --git a/src/expand_context.h b/src/expand_context.h index 698e039..7065839 100644 --- a/src/expand_context.h +++ b/src/expand_context.h @@ -52,7 +52,7 @@ extern "C" { * * @return size of decompressed data in bytes, or -1 for error */ -int lzsa_decompressor_expand_block(unsigned char *pInBlock, int nBlockSize, unsigned char *pOutData, int nOutDataOffset, int nBlockMaxSize, const int nFormatVersion, const int nFlags); +int lzsa_decompressor_expand_block(unsigned char *pInBlock, const int nBlockSize, unsigned char *pOutData, const int nOutDataOffset, const int nBlockMaxSize, const int nFormatVersion, const int nFlags); #ifdef __cplusplus } diff --git a/src/lzsa.c b/src/lzsa.c index 1b7b0c9..9698064 100755 --- a/src/lzsa.c +++ b/src/lzsa.c @@ -120,7 +120,7 @@ static int do_compress(const char *pszInFilename, const char *pszOutFilename, co nStatus = lzsa_compress_file(pszInFilename, pszOutFilename, pszDictionaryFilename, nFlags, nMinMatchSize, nFormatVersion, compression_progress, &nOriginalSize, &nCompressedSize, &nCommandCount, &nSafeDist, &stats); - if ((nOptions & OPT_VERBOSE)) { + if (nOptions & OPT_VERBOSE) { nEndTime = do_get_time(); } @@ -139,7 +139,7 @@ static int do_compress(const char *pszInFilename, const char *pszOutFilename, co if (nStatus) return 100; - if ((nOptions & OPT_VERBOSE)) { + if (nOptions & OPT_VERBOSE) { double fDelta = ((double)(nEndTime - nStartTime)) / 1000000.0; double fSpeed = ((double)nOriginalSize / 1048576.0) / fDelta; fprintf(stdout, "\rCompressed '%s' in %g seconds, %.02g Mb/s, %d tokens (%g bytes/token), %lld into %lld bytes ==> %g %%\n", @@ -383,13 +383,13 @@ static int do_compare(const char *pszInFilename, const char *pszOutFilename, con /*---------------------------------------------------------------------------*/ -static void generate_compressible_data(unsigned char *pBuffer, size_t nBufferSize, int nMinMatchSize, unsigned int nSeed, int nNumLiteralValues, float fMatchProbability) { +static void generate_compressible_data(unsigned char *pBuffer, const size_t nBufferSize, const int nMinMatchSize, const unsigned int nSeed, const int nNumLiteralValues, const float fMatchProbability) { size_t nIndex = 0; - int nMatchProbability = (int)(fMatchProbability * 1023.0f); + const int nMatchProbability = (int)(fMatchProbability * 1023.0f); srand(nSeed); - if (nIndex >= nBufferSize) return; + if (nBufferSize == 0) return; pBuffer[nIndex++] = rand() % nNumLiteralValues; while (nIndex < nBufferSize) { @@ -423,14 +423,12 @@ static void generate_compressible_data(unsigned char *pBuffer, size_t nBufferSiz } } -static void xor_data(unsigned char *pBuffer, size_t nBufferSize, unsigned int nSeed, float fXorProbability) { +static void xor_data(unsigned char *pBuffer, const size_t nBufferSize, const unsigned int nSeed, const float fXorProbability) { size_t nIndex = 0; - int nXorProbability = (int)(fXorProbability * 1023.0f); + const int nXorProbability = (const int)(fXorProbability * 1023.0f); srand(nSeed); - if (nIndex >= nBufferSize) return; - while (nIndex < nBufferSize) { if ((rand() & 1023) < nXorProbability) { pBuffer[nIndex] ^= 0xff; @@ -439,7 +437,7 @@ static void xor_data(unsigned char *pBuffer, size_t nBufferSize, unsigned int nS } } -static int do_self_test(const unsigned int nOptions, const int nMinMatchSize, int nFormatVersion) { +static int do_self_test(const unsigned int nOptions, const int nMinMatchSize, const int nFormatVersion) { unsigned char *pGeneratedData; unsigned char *pCompressedData; unsigned char *pTmpCompressedData; @@ -470,7 +468,7 @@ static int do_self_test(const unsigned int nOptions, const int nMinMatchSize, in free(pGeneratedData); pGeneratedData = NULL; - fprintf(stderr, "out of memory, %zd bytes needed\n", nMaxCompressedDataSize); + fprintf(stderr, "out of memory, %zu bytes needed\n", nMaxCompressedDataSize); return 100; } @@ -481,7 +479,7 @@ static int do_self_test(const unsigned int nOptions, const int nMinMatchSize, in free(pGeneratedData); pGeneratedData = NULL; - fprintf(stderr, "out of memory, %zd bytes needed\n", nMaxCompressedDataSize); + fprintf(stderr, "out of memory, %zu bytes needed\n", nMaxCompressedDataSize); return 100; } @@ -514,7 +512,7 @@ static int do_self_test(const unsigned int nOptions, const int nMinMatchSize, in for (nGeneratedDataSize = 1024; nGeneratedDataSize <= ((size_t)((nOptions & OPT_RAW) ? BLOCK_SIZE : (4 * BLOCK_SIZE))); nGeneratedDataSize += nDataSizeStep) { float fMatchProbability; - fprintf(stdout, "size %zd", nGeneratedDataSize); + fprintf(stdout, "size %zu", nGeneratedDataSize); for (fMatchProbability = 0; fMatchProbability <= 0.995f; fMatchProbability += fProbabilitySizeStep) { int nNumLiteralValues[12] = { 1, 2, 3, 15, 30, 56, 96, 137, 178, 191, 255, 256 }; float fXorProbability; @@ -539,7 +537,7 @@ static int do_self_test(const unsigned int nOptions, const int nMinMatchSize, in free(pGeneratedData); pGeneratedData = NULL; - fprintf(stderr, "\nself-test: error compressing size %zd, seed %d, match probability %f, literals range %d\n", nGeneratedDataSize, nSeed, fMatchProbability, nNumLiteralValues[i]); + fprintf(stderr, "\nself-test: error compressing size %zu, seed %u, match probability %f, literals range %d\n", nGeneratedDataSize, nSeed, fMatchProbability, nNumLiteralValues[i]); return 100; } @@ -557,7 +555,7 @@ static int do_self_test(const unsigned int nOptions, const int nMinMatchSize, in free(pGeneratedData); pGeneratedData = NULL; - fprintf(stderr, "\nself-test: error decompressing size %zd, seed %d, match probability %f, literals range %d\n", nGeneratedDataSize, nSeed, fMatchProbability, nNumLiteralValues[i]); + fprintf(stderr, "\nself-test: error decompressing size %zu, seed %u, match probability %f, literals range %d\n", nGeneratedDataSize, nSeed, fMatchProbability, nNumLiteralValues[i]); return 100; } @@ -571,7 +569,7 @@ static int do_self_test(const unsigned int nOptions, const int nMinMatchSize, in free(pGeneratedData); pGeneratedData = NULL; - fprintf(stderr, "\nself-test: error comparing decompressed and original data, size %zd, seed %d, match probability %f, literals range %d\n", nGeneratedDataSize, nSeed, fMatchProbability, nNumLiteralValues[i]); + fprintf(stderr, "\nself-test: error comparing decompressed and original data, size %zu, seed %u, match probability %f, literals range %d\n", nGeneratedDataSize, nSeed, fMatchProbability, nNumLiteralValues[i]); return 100; } @@ -651,7 +649,7 @@ static int do_compr_benchmark(const char *pszInFilename, const char *pszOutFilen pFileData = (unsigned char*)malloc(nFileSize); if (!pFileData) { fclose(f_in); - fprintf(stderr, "out of memory for reading '%s', %zd bytes needed\n", pszInFilename, nFileSize); + fprintf(stderr, "out of memory for reading '%s', %zu bytes needed\n", pszInFilename, nFileSize); return 100; } @@ -671,7 +669,7 @@ static int do_compr_benchmark(const char *pszInFilename, const char *pszOutFilen pCompressedData = (unsigned char*)malloc(nMaxCompressedSize + 2048); if (!pCompressedData) { free(pFileData); - fprintf(stderr, "out of memory for compressing '%s', %zd bytes needed\n", pszInFilename, nMaxCompressedSize); + fprintf(stderr, "out of memory for compressing '%s', %zu bytes needed\n", pszInFilename, nMaxCompressedSize); return 100; } @@ -742,7 +740,7 @@ static int do_compr_benchmark(const char *pszInFilename, const char *pszOutFilen free(pCompressedData); free(pFileData); - fprintf(stdout, "compressed size: %zd bytes\n", nActualCompressedSize); + fprintf(stdout, "compressed size: %zu bytes\n", nActualCompressedSize); fprintf(stdout, "compression time: %lld microseconds (%g Mb/s)\n", nBestCompTime, ((double)nActualCompressedSize / 1024.0) / ((double)nBestCompTime / 1000.0)); return 0; @@ -783,7 +781,7 @@ static int do_dec_benchmark(const char *pszInFilename, const char *pszOutFilenam pFileData = (unsigned char*)malloc(nFileSize); if (!pFileData) { fclose(f_in); - fprintf(stderr, "out of memory for reading '%s', %zd bytes needed\n", pszInFilename, nFileSize); + fprintf(stderr, "out of memory for reading '%s', %zu bytes needed\n", pszInFilename, nFileSize); return 100; } @@ -811,7 +809,7 @@ static int do_dec_benchmark(const char *pszInFilename, const char *pszOutFilenam pDecompressedData = (unsigned char*)malloc(nMaxDecompressedSize); if (!pDecompressedData) { free(pFileData); - fprintf(stderr, "out of memory for decompressing '%s', %zd bytes needed\n", pszInFilename, nMaxDecompressedSize); + fprintf(stderr, "out of memory for decompressing '%s', %zu bytes needed\n", pszInFilename, nMaxDecompressedSize); return 100; } @@ -852,7 +850,7 @@ static int do_dec_benchmark(const char *pszInFilename, const char *pszOutFilenam free(pFileData); fprintf(stdout, "format: LZSA%d\n", nFormatVersion); - fprintf(stdout, "decompressed size: %zd bytes\n", nActualDecompressedSize); + fprintf(stdout, "decompressed size: %zu bytes\n", nActualDecompressedSize); fprintf(stdout, "decompression time: %lld microseconds (%g Mb/s)\n", nBestDecTime, ((double)nActualDecompressedSize / 1024.0) / ((double)nBestDecTime / 1000.0)); return 0; @@ -1037,11 +1035,11 @@ int main(int argc, char **argv) { nArgsError = 1; } else if (!strcmp(argv[i], "-stats")) { - if ((nOptions & OPT_STATS) == 0) { - nOptions |= OPT_STATS; - } - else - nArgsError = 1; + if ((nOptions & OPT_STATS) == 0) { + nOptions |= OPT_STATS; + } + else + nArgsError = 1; } else { if (!pszInFilename) diff --git a/src/matchfinder.c b/src/matchfinder.c index 24e9d15..18ed597 100644 --- a/src/matchfinder.c +++ b/src/matchfinder.c @@ -77,7 +77,7 @@ int lzsa_build_suffix_array(lzsa_compressor *pCompressor, const unsigned char *p PLCP[i] = 0; continue; } - int nMaxLen = (i > Phi[i]) ? (nInWindowSize - i) : (nInWindowSize - Phi[i]); + const int nMaxLen = (i > Phi[i]) ? (nInWindowSize - i) : (nInWindowSize - Phi[i]); while (nCurLen < nMaxLen && pInWindow[i + nCurLen] == pInWindow[Phi[i] + nCurLen]) nCurLen++; PLCP[i] = nCurLen; if (nCurLen > 0) @@ -88,11 +88,11 @@ int lzsa_build_suffix_array(lzsa_compressor *pCompressor, const unsigned char *p * saves us from having to build the inverse suffix array index, as the LCP is calculated without it using this method, * and the interval builder below doesn't need it either. */ intervals[0] &= POS_MASK; - int nMinMatchSize = pCompressor->min_match_size; + const int nMinMatchSize = pCompressor->min_match_size; if (pCompressor->format_version >= 2) { for (i = 1; i < nInWindowSize; i++) { - int nIndex = (int)(intervals[i] & POS_MASK); + const int nIndex = (int)(intervals[i] & POS_MASK); int nLen = PLCP[nIndex]; if (nLen < nMinMatchSize) nLen = 0; @@ -106,7 +106,7 @@ int lzsa_build_suffix_array(lzsa_compressor *pCompressor, const unsigned char *p } else { for (i = 1; i < nInWindowSize; i++) { - int nIndex = (int)(intervals[i] & POS_MASK); + const int nIndex = (int)(intervals[i] & POS_MASK); int nLen = PLCP[nIndex]; if (nLen < nMinMatchSize) nLen = 0; @@ -203,7 +203,7 @@ static int lzsa_find_matches_at(lzsa_compressor *pCompressor, const int nOffset, unsigned int super_ref; unsigned int match_pos; lzsa_match *matchptr; - int nPrevOffset = 0; + unsigned int nPrevOffset = 0; /** * Find matches using intervals @@ -240,11 +240,11 @@ static int lzsa_find_matches_at(lzsa_compressor *pCompressor, const int nOffset, if (pCompressor->format_version >= 2 && nInWindowSize < 65536) { if ((matchptr - pMatches) < nMaxMatches) { - int nMatchOffset = (int)(nOffset - match_pos); + const unsigned int nMatchOffset = (const unsigned int)(nOffset - match_pos); if (nMatchOffset <= MAX_OFFSET) { - matchptr->length = (unsigned short)(ref >> (LCP_SHIFT + TAG_BITS)); - matchptr->offset = (unsigned short)nMatchOffset; + matchptr->length = (const unsigned short)(ref >> (LCP_SHIFT + TAG_BITS)); + matchptr->offset = (const unsigned short)nMatchOffset; matchptr++; nPrevOffset = nMatchOffset; @@ -258,11 +258,11 @@ static int lzsa_find_matches_at(lzsa_compressor *pCompressor, const int nOffset, if (pCompressor->format_version >= 2 && nInWindowSize < 65536) { if ((matchptr - pMatches) < nMaxMatches) { - int nMatchOffset = (int)(nOffset - match_pos); + const unsigned int nMatchOffset = (const unsigned int)(nOffset - match_pos); if (nMatchOffset <= MAX_OFFSET && nMatchOffset != nPrevOffset) { - matchptr->length = ((unsigned short)(ref >> (LCP_SHIFT + TAG_BITS))) | 0x8000; - matchptr->offset = (unsigned short)nMatchOffset; + matchptr->length = ((const unsigned short)(ref >> (LCP_SHIFT + TAG_BITS))) | 0x8000; + matchptr->offset = (const unsigned short)nMatchOffset; matchptr++; nPrevOffset = nMatchOffset; @@ -277,16 +277,16 @@ static int lzsa_find_matches_at(lzsa_compressor *pCompressor, const int nOffset, pos_data[match_pos] = ref; if ((matchptr - pMatches) < nMaxMatches) { - int nMatchOffset = (int)(nOffset - match_pos); + const unsigned int nMatchOffset = (const unsigned int)(nOffset - match_pos); if (nMatchOffset <= MAX_OFFSET && nMatchOffset != nPrevOffset) { if (pCompressor->format_version >= 2) { - matchptr->length = (unsigned short)(ref >> (LCP_SHIFT + TAG_BITS)); + matchptr->length = (const unsigned short)(ref >> (LCP_SHIFT + TAG_BITS)); } else { - matchptr->length = (unsigned short)(ref >> LCP_SHIFT); + matchptr->length = (const unsigned short)(ref >> LCP_SHIFT); } - matchptr->offset = (unsigned short)nMatchOffset; + matchptr->offset = (const unsigned short)nMatchOffset; matchptr++; } } @@ -298,13 +298,14 @@ static int lzsa_find_matches_at(lzsa_compressor *pCompressor, const int nOffset, if (pCompressor->format_version >= 2 && nInWindowSize < 65536) { if ((matchptr - pMatches) < nMaxMatches) { - int nMatchOffset = (int)(nOffset - match_pos); + const unsigned int nMatchOffset = (const unsigned int)(nOffset - match_pos); if (nMatchOffset <= MAX_OFFSET && nMatchOffset != nPrevOffset) { - matchptr->length = ((unsigned short)(ref >> (LCP_SHIFT + TAG_BITS))) | 0x8000; - matchptr->offset = (unsigned short)nMatchOffset; + const unsigned short nMatchLen = ((const unsigned short)(ref >> (LCP_SHIFT + TAG_BITS))); - if ((matchptr->length & 0x7fff) > 2) { + if (nMatchLen > 2) { + matchptr->length = nMatchLen | 0x8000; + matchptr->offset = (const unsigned short)nMatchOffset; matchptr++; nPrevOffset = nMatchOffset; @@ -348,12 +349,10 @@ void lzsa_find_all_matches(lzsa_compressor *pCompressor, const int nMatchesPerOf int i; for (i = nStartOffset; i < nEndOffset; i++) { - int nMatches = lzsa_find_matches_at(pCompressor, i, pMatch, nMatchesPerOffset, nEndOffset - nStartOffset); + const int nMatches = lzsa_find_matches_at(pCompressor, i, pMatch, nMatchesPerOffset, nEndOffset - nStartOffset); - while (nMatches < nMatchesPerOffset) { - pMatch[nMatches].length = 0; - pMatch[nMatches].offset = 0; - nMatches++; + if (nMatches < nMatchesPerOffset) { + memset(pMatch + nMatches, 0, (nMatchesPerOffset - nMatches) * sizeof(lzsa_match)); } pMatch += nMatchesPerOffset; diff --git a/src/shrink_block_v1.c b/src/shrink_block_v1.c index a52c991..4bb1948 100644 --- a/src/shrink_block_v1.c +++ b/src/shrink_block_v1.c @@ -448,7 +448,6 @@ static int lzsa_get_compressed_size_v1(lzsa_compressor *pCompressor, lzsa_match const int nCommandSize = 8 /* token */ + lzsa_get_literals_varlen_size_v1(nNumLiterals) + (nNumLiterals << 3); nCompressedSize += nCommandSize; - nNumLiterals = 0; } if (pCompressor->flags & LZSA_FLAG_RAW_BLOCK) { @@ -574,7 +573,7 @@ static int lzsa_write_block_v1(lzsa_compressor *pCompressor, lzsa_match *pBestMa if (pCompressor->flags & LZSA_FLAG_RAW_BLOCK) pOutData[nOutOffset++] = (nTokenLiteralsLen << 4) | 0x0f; else - pOutData[nOutOffset++] = (nTokenLiteralsLen << 4) | 0x00; + pOutData[nOutOffset++] = (nTokenLiteralsLen << 4) /* | 0x00 */; nOutOffset = lzsa_write_literals_varlen_v1(pOutData, nOutOffset, nNumLiterals); if (nNumLiterals < pCompressor->stats.min_literals || pCompressor->stats.min_literals == -1) @@ -587,7 +586,6 @@ static int lzsa_write_block_v1(lzsa_compressor *pCompressor, lzsa_match *pBestMa if (nNumLiterals != 0) { memcpy(pOutData + nOutOffset, pInWindow + nInFirstLiteralOffset, nNumLiterals); nOutOffset += nNumLiterals; - nNumLiterals = 0; } if (pCompressor->flags & LZSA_FLAG_RAW_BLOCK) { @@ -643,7 +641,6 @@ static int lzsa_write_raw_uncompressed_block_v1(lzsa_compressor *pCompressor, co if (nNumLiterals != 0) { memcpy(pOutData + nOutOffset, pInWindow + nStartOffset, nNumLiterals); nOutOffset += nNumLiterals; - nNumLiterals = 0; } pCompressor->num_commands++; diff --git a/src/shrink_block_v2.c b/src/shrink_block_v2.c index e0f5ded..0c149e8 100644 --- a/src/shrink_block_v2.c +++ b/src/shrink_block_v2.c @@ -252,7 +252,7 @@ static void lzsa_insert_forward_match_v2(lzsa_compressor *pCompressor, const uns while (pInWindowAtRepPos < pInWindowMax && pInWindowAtRepPos[0] == pInWindowAtRepPos[-nMatchOffset]) pInWindowAtRepPos++; - fwd_match[r].length = (const unsigned int)(pInWindowAtRepPos - pInWindowStart); + fwd_match[r].length = (const unsigned short)(pInWindowAtRepPos - pInWindowStart); fwd_match[r].offset = nMatchOffset; if (nDepth < 9) @@ -698,7 +698,7 @@ static void lzsa_optimize_forward_v2(lzsa_compressor *pCompressor, const unsigne } if (!nInsertForwardReps) { - const lzsa_arrival* end_arrival = &arrival[(i << ARRIVALS_PER_POSITION_SHIFT_V2) + 0]; + const lzsa_arrival* end_arrival = &arrival[i << ARRIVALS_PER_POSITION_SHIFT_V2]; lzsa_match* pBestMatch = pCompressor->best_match - nStartOffset; while (end_arrival->from_slot > 0 && end_arrival->from_pos >= 0 && (end_arrival->from_pos + nStartOffset) < nEndOffset) { @@ -786,7 +786,12 @@ static int lzsa_optimize_command_count_v2(lzsa_compressor *pCompressor, const un /* Otherwise, try to gain a match forward as well */ if (i >= pBestMatch[nNextIndex].offset && (i - pBestMatch[nNextIndex].offset + pMatch->length) <= nEndOffset) { int nMaxLen = 0; - while (nMaxLen < pMatch->length && pInWindow[i - pBestMatch[nNextIndex].offset + nMaxLen] == pInWindow[i - pMatch->offset + nMaxLen]) + const unsigned char *pInWindowAtPos = pInWindow + i; + while ((nMaxLen + 8) < pMatch->length && !memcmp(pInWindowAtPos + nMaxLen - pBestMatch[nNextIndex].offset, pInWindowAtPos + nMaxLen, 8)) + nMaxLen += 8; + while ((nMaxLen + 4) < pMatch->length && !memcmp(pInWindowAtPos + nMaxLen - pBestMatch[nNextIndex].offset, pInWindowAtPos + nMaxLen, 4)) + nMaxLen += 4; + while (nMaxLen < pMatch->length && pInWindowAtPos[nMaxLen - pBestMatch[nNextIndex].offset] == pInWindowAtPos[nMaxLen - pMatch->offset]) nMaxLen++; if (nMaxLen >= pMatch->length) { /* Replace */ @@ -804,15 +809,16 @@ static int lzsa_optimize_command_count_v2(lzsa_compressor *pCompressor, const un nPartialSizeAfter += lzsa_get_literals_varlen_size_v2(nNextLiterals + (pMatch->length - nMaxLen)) + ((pMatch->length - nMaxLen) << 3); if (nPartialSizeAfter < nPartialSizeBefore) { + const int nMatchLen = pMatch->length; int j; /* We gain a repmatch that is shorter than the original match as this is the best we can do, so it is followed by extra literals, but * we have calculated that this is shorter */ + pMatch->length = nMaxLen; pMatch->offset = pBestMatch[nNextIndex].offset; - for (j = nMaxLen; j < pMatch->length; j++) { + for (j = nMaxLen; j < nMatchLen; j++) { pBestMatch[i + j].length = 0; } - pMatch->length = nMaxLen; nDidReduce = 1; } } @@ -868,8 +874,8 @@ static int lzsa_optimize_command_count_v2(lzsa_compressor *pCompressor, const un } } - if ((i + pMatch->length) < nEndOffset && pMatch->offset > 0 && pMatch->length >= MIN_MATCH_SIZE_V2 && - pBestMatch[i + pMatch->length].offset > 0 && + if ((i + pMatch->length) < nEndOffset && pMatch->offset && pMatch->length >= MIN_MATCH_SIZE_V2 && + pBestMatch[i + pMatch->length].offset && pBestMatch[i + pMatch->length].length >= MIN_MATCH_SIZE_V2 && (pMatch->length + pBestMatch[i + pMatch->length].length) <= MAX_VARLEN && (i + pMatch->length) >= pMatch->offset && @@ -937,7 +943,6 @@ static int lzsa_optimize_command_count_v2(lzsa_compressor *pCompressor, const un * Emit block of compressed data * * @param pCompressor compression context - * @param pBestMatch optimal matches to emit * @param pInWindow pointer to input data window (previously compressed bytes + bytes to compress) * @param nStartOffset current offset in input window (typically the number of previously compressed bytes) * @param nEndOffset offset to end finding matches at (typically the size of the total input window in bytes @@ -946,7 +951,8 @@ static int lzsa_optimize_command_count_v2(lzsa_compressor *pCompressor, const un * * @return size of compressed data in output buffer, or -1 if the data is uncompressible */ -static int lzsa_write_block_v2(lzsa_compressor *pCompressor, const lzsa_match *pBestMatch, const unsigned char *pInWindow, const int nStartOffset, const int nEndOffset, unsigned char *pOutData, const int nMaxOutDataSize) { +static int lzsa_write_block_v2(lzsa_compressor *pCompressor, const unsigned char *pInWindow, const int nStartOffset, const int nEndOffset, unsigned char *pOutData, const int nMaxOutDataSize) { + const lzsa_match *pBestMatch = pCompressor->best_match - nStartOffset; int i; int nNumLiterals = 0; int nInFirstLiteralOffset = 0; @@ -972,7 +978,7 @@ static int lzsa_write_block_v2(lzsa_compressor *pCompressor, const lzsa_match *p } else { if (nMatchOffset <= 32) { - nTokenOffsetMode = 0x00 | ((((-nMatchOffset) & 0x01) << 5) ^ 0x20); + nTokenOffsetMode = /* 0x00 | */ ((((-nMatchOffset) & 0x01) << 5) ^ 0x20); nOffsetSize = 4; } else if (nMatchOffset <= 512) { @@ -1096,7 +1102,7 @@ static int lzsa_write_block_v2(lzsa_compressor *pCompressor, const lzsa_match *p if (pCompressor->flags & LZSA_FLAG_RAW_BLOCK) pOutData[nOutOffset++] = (nTokenLiteralsLen << 3) | 0xe7; else - pOutData[nOutOffset++] = (nTokenLiteralsLen << 3) | 0x00; + pOutData[nOutOffset++] = (nTokenLiteralsLen << 3) /* | 0x00 */; nOutOffset = lzsa_write_literals_varlen_v2(pOutData, nOutOffset, nMaxOutDataSize, &nCurNibbleOffset, nNumLiterals); if (nOutOffset < 0) return -1; @@ -1462,7 +1468,7 @@ int lzsa_optimize_and_write_block_v2(lzsa_compressor *pCompressor, const unsigne } while (nDidReduce && nPasses < 20); /* Write compressed block */ - nResult = lzsa_write_block_v2(pCompressor, pCompressor->best_match - nPreviousBlockSize, pInWindow, nPreviousBlockSize, nPreviousBlockSize + nInDataSize, pOutData, nMaxOutDataSize); + nResult = lzsa_write_block_v2(pCompressor, pInWindow, nPreviousBlockSize, nPreviousBlockSize + nInDataSize, pOutData, nMaxOutDataSize); if (nResult < 0 && (pCompressor->flags & LZSA_FLAG_RAW_BLOCK)) { nResult = lzsa_write_raw_uncompressed_block_v2(pCompressor, pInWindow, nPreviousBlockSize, nPreviousBlockSize + nInDataSize, pOutData, nMaxOutDataSize); }