-/* LzmaEnc.c -- LZMA Encoder\r
-2022-07-15: Igor Pavlov : Public domain */\r
-\r
-#include "Precomp.h"\r
-\r
-#include <string.h>\r
-\r
-/* #define SHOW_STAT */\r
-/* #define SHOW_STAT2 */\r
-\r
-#if defined(SHOW_STAT) || defined(SHOW_STAT2)\r
-#include <stdio.h>\r
-#endif\r
-\r
-#include "CpuArch.h"\r
-#include "LzmaEnc.h"\r
-\r
-#include "LzFind.h"\r
-#ifndef _7ZIP_ST\r
-#include "LzFindMt.h"\r
-#endif\r
-\r
-/* the following LzmaEnc_* declarations is internal LZMA interface for LZMA2 encoder */\r
-\r
-SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp, ISeqInStream *inStream, UInt32 keepWindowSize,\r
- ISzAllocPtr alloc, ISzAllocPtr allocBig);\r
-SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen,\r
- UInt32 keepWindowSize, ISzAllocPtr alloc, ISzAllocPtr allocBig);\r
-SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, BoolInt reInit,\r
- Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize);\r
-const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp);\r
-void LzmaEnc_Finish(CLzmaEncHandle pp);\r
-void LzmaEnc_SaveState(CLzmaEncHandle pp);\r
-void LzmaEnc_RestoreState(CLzmaEncHandle pp);\r
-\r
-#ifdef SHOW_STAT\r
-static unsigned g_STAT_OFFSET = 0;\r
-#endif\r
-\r
-/* for good normalization speed we still reserve 256 MB before 4 GB range */\r
-#define kLzmaMaxHistorySize ((UInt32)15 << 28)\r
-\r
-#define kNumTopBits 24\r
-#define kTopValue ((UInt32)1 << kNumTopBits)\r
-\r
-#define kNumBitModelTotalBits 11\r
-#define kBitModelTotal (1 << kNumBitModelTotalBits)\r
-#define kNumMoveBits 5\r
-#define kProbInitValue (kBitModelTotal >> 1)\r
-\r
-#define kNumMoveReducingBits 4\r
-#define kNumBitPriceShiftBits 4\r
-// #define kBitPrice (1 << kNumBitPriceShiftBits)\r
-\r
-#define REP_LEN_COUNT 64\r
-\r
-void LzmaEncProps_Init(CLzmaEncProps *p)\r
-{\r
- p->level = 5;\r
- p->dictSize = p->mc = 0;\r
- p->reduceSize = (UInt64)(Int64)-1;\r
- p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;\r
- p->writeEndMark = 0;\r
- p->affinity = 0;\r
-}\r
-\r
-void LzmaEncProps_Normalize(CLzmaEncProps *p)\r
-{\r
- int level = p->level;\r
- if (level < 0) level = 5;\r
- p->level = level;\r
- \r
- if (p->dictSize == 0)\r
- p->dictSize =\r
- ( level <= 3 ? ((UInt32)1 << (level * 2 + 16)) :\r
- ( level <= 6 ? ((UInt32)1 << (level + 19)) :\r
- ( level <= 7 ? ((UInt32)1 << 25) : ((UInt32)1 << 26)\r
- )));\r
-\r
- if (p->dictSize > p->reduceSize)\r
- {\r
- UInt32 v = (UInt32)p->reduceSize;\r
- const UInt32 kReduceMin = ((UInt32)1 << 12);\r
- if (v < kReduceMin)\r
- v = kReduceMin;\r
- if (p->dictSize > v)\r
- p->dictSize = v;\r
- }\r
-\r
- if (p->lc < 0) p->lc = 3;\r
- if (p->lp < 0) p->lp = 0;\r
- if (p->pb < 0) p->pb = 2;\r
-\r
- if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);\r
- if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);\r
- if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);\r
- if (p->numHashBytes < 0) p->numHashBytes = (p->btMode ? 4 : 5);\r
- if (p->mc == 0) p->mc = (16 + ((unsigned)p->fb >> 1)) >> (p->btMode ? 0 : 1);\r
- \r
- if (p->numThreads < 0)\r
- p->numThreads =\r
- #ifndef _7ZIP_ST\r
- ((p->btMode && p->algo) ? 2 : 1);\r
- #else\r
- 1;\r
- #endif\r
-}\r
-\r
-UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2)\r
-{\r
- CLzmaEncProps props = *props2;\r
- LzmaEncProps_Normalize(&props);\r
- return props.dictSize;\r
-}\r
-\r
-\r
-/*\r
-x86/x64:\r
-\r
-BSR:\r
- IF (SRC == 0) ZF = 1, DEST is undefined;\r
- AMD : DEST is unchanged;\r
- IF (SRC != 0) ZF = 0; DEST is index of top non-zero bit\r
- BSR is slow in some processors\r
-\r
-LZCNT:\r
- IF (SRC == 0) CF = 1, DEST is size_in_bits_of_register(src) (32 or 64)\r
- IF (SRC != 0) CF = 0, DEST = num_lead_zero_bits\r
- IF (DEST == 0) ZF = 1;\r
-\r
-LZCNT works only in new processors starting from Haswell.\r
-if LZCNT is not supported by processor, then it's executed as BSR.\r
-LZCNT can be faster than BSR, if supported.\r
-*/\r
-\r
-// #define LZMA_LOG_BSR\r
-\r
-#if defined(MY_CPU_ARM_OR_ARM64) /* || defined(MY_CPU_X86_OR_AMD64) */\r
-\r
- #if (defined(__clang__) && (__clang_major__ >= 6)) \\r
- || (defined(__GNUC__) && (__GNUC__ >= 6))\r
- #define LZMA_LOG_BSR\r
- #elif defined(_MSC_VER) && (_MSC_VER >= 1300)\r
- // #if defined(MY_CPU_ARM_OR_ARM64)\r
- #define LZMA_LOG_BSR\r
- // #endif\r
- #endif\r
-#endif\r
-\r
-// #include <intrin.h>\r
-\r
-#ifdef LZMA_LOG_BSR\r
-\r
-#if defined(__clang__) \\r
- || defined(__GNUC__)\r
-\r
-/*\r
- C code: : (30 - __builtin_clz(x))\r
- gcc9/gcc10 for x64 /x86 : 30 - (bsr(x) xor 31)\r
- clang10 for x64 : 31 + (bsr(x) xor -32)\r
-*/\r
-\r
- #define MY_clz(x) ((unsigned)__builtin_clz(x))\r
- // __lzcnt32\r
- // __builtin_ia32_lzcnt_u32\r
-\r
-#else // #if defined(_MSC_VER)\r
-\r
- #ifdef MY_CPU_ARM_OR_ARM64\r
-\r
- #define MY_clz _CountLeadingZeros\r
-\r
- #else // if defined(MY_CPU_X86_OR_AMD64)\r
-\r
- // #define MY_clz __lzcnt // we can use lzcnt (unsupported by old CPU)\r
- // _BitScanReverse code is not optimal for some MSVC compilers\r
- #define BSR2_RET(pos, res) { unsigned long zz; _BitScanReverse(&zz, (pos)); zz--; \\r
- res = (zz + zz) + (pos >> zz); }\r
-\r
- #endif // MY_CPU_X86_OR_AMD64\r
-\r
-#endif // _MSC_VER\r
-\r
-\r
-#ifndef BSR2_RET\r
-\r
- #define BSR2_RET(pos, res) { unsigned zz = 30 - MY_clz(pos); \\r
- res = (zz + zz) + (pos >> zz); }\r
-\r
-#endif\r
-\r
-\r
-unsigned GetPosSlot1(UInt32 pos);\r
-unsigned GetPosSlot1(UInt32 pos)\r
-{\r
- unsigned res;\r
- BSR2_RET(pos, res);\r
- return res;\r
-}\r
-#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }\r
-#define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); }\r
-\r
-\r
-#else // ! LZMA_LOG_BSR\r
-\r
-#define kNumLogBits (11 + sizeof(size_t) / 8 * 3)\r
-\r
-#define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)\r
-\r
-static void LzmaEnc_FastPosInit(Byte *g_FastPos)\r
-{\r
- unsigned slot;\r
- g_FastPos[0] = 0;\r
- g_FastPos[1] = 1;\r
- g_FastPos += 2;\r
- \r
- for (slot = 2; slot < kNumLogBits * 2; slot++)\r
- {\r
- size_t k = ((size_t)1 << ((slot >> 1) - 1));\r
- size_t j;\r
- for (j = 0; j < k; j++)\r
- g_FastPos[j] = (Byte)slot;\r
- g_FastPos += k;\r
- }\r
-}\r
-\r
-/* we can use ((limit - pos) >> 31) only if (pos < ((UInt32)1 << 31)) */\r
-/*\r
-#define BSR2_RET(pos, res) { unsigned zz = 6 + ((kNumLogBits - 1) & \\r
- (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \\r
- res = p->g_FastPos[pos >> zz] + (zz * 2); }\r
-*/\r
-\r
-/*\r
-#define BSR2_RET(pos, res) { unsigned zz = 6 + ((kNumLogBits - 1) & \\r
- (0 - (((((UInt32)1 << (kNumLogBits)) - 1) - (pos >> 6)) >> 31))); \\r
- res = p->g_FastPos[pos >> zz] + (zz * 2); }\r
-*/\r
-\r
-#define BSR2_RET(pos, res) { unsigned zz = (pos < (1 << (kNumLogBits + 6))) ? 6 : 6 + kNumLogBits - 1; \\r
- res = p->g_FastPos[pos >> zz] + (zz * 2); }\r
-\r
-/*\r
-#define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \\r
- p->g_FastPos[pos >> 6] + 12 : \\r
- p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }\r
-*/\r
-\r
-#define GetPosSlot1(pos) p->g_FastPos[pos]\r
-#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }\r
-#define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos & (kNumFullDistances - 1)]; else BSR2_RET(pos, res); }\r
-\r
-#endif // LZMA_LOG_BSR\r
-\r
-\r
-#define LZMA_NUM_REPS 4\r
-\r
-typedef UInt16 CState;\r
-typedef UInt16 CExtra;\r
-\r
-typedef struct\r
-{\r
- UInt32 price;\r
- CState state;\r
- CExtra extra;\r
- // 0 : normal\r
- // 1 : LIT : MATCH\r
- // > 1 : MATCH (extra-1) : LIT : REP0 (len)\r
- UInt32 len;\r
- UInt32 dist;\r
- UInt32 reps[LZMA_NUM_REPS];\r
-} COptimal;\r
-\r
-\r
-// 18.06\r
-#define kNumOpts (1 << 11)\r
-#define kPackReserve (kNumOpts * 8)\r
-// #define kNumOpts (1 << 12)\r
-// #define kPackReserve (1 + kNumOpts * 2)\r
-\r
-#define kNumLenToPosStates 4\r
-#define kNumPosSlotBits 6\r
-// #define kDicLogSizeMin 0\r
-#define kDicLogSizeMax 32\r
-#define kDistTableSizeMax (kDicLogSizeMax * 2)\r
-\r
-#define kNumAlignBits 4\r
-#define kAlignTableSize (1 << kNumAlignBits)\r
-#define kAlignMask (kAlignTableSize - 1)\r
-\r
-#define kStartPosModelIndex 4\r
-#define kEndPosModelIndex 14\r
-#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))\r
-\r
-typedef\r
-#ifdef _LZMA_PROB32\r
- UInt32\r
-#else\r
- UInt16\r
-#endif\r
- CLzmaProb;\r
-\r
-#define LZMA_PB_MAX 4\r
-#define LZMA_LC_MAX 8\r
-#define LZMA_LP_MAX 4\r
-\r
-#define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)\r
-\r
-#define kLenNumLowBits 3\r
-#define kLenNumLowSymbols (1 << kLenNumLowBits)\r
-#define kLenNumHighBits 8\r
-#define kLenNumHighSymbols (1 << kLenNumHighBits)\r
-#define kLenNumSymbolsTotal (kLenNumLowSymbols * 2 + kLenNumHighSymbols)\r
-\r
-#define LZMA_MATCH_LEN_MIN 2\r
-#define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)\r
-\r
-#define kNumStates 12\r
-\r
-\r
-typedef struct\r
-{\r
- CLzmaProb low[LZMA_NUM_PB_STATES_MAX << (kLenNumLowBits + 1)];\r
- CLzmaProb high[kLenNumHighSymbols];\r
-} CLenEnc;\r
-\r
-\r
-typedef struct\r
-{\r
- unsigned tableSize;\r
- UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];\r
- // UInt32 prices1[LZMA_NUM_PB_STATES_MAX][kLenNumLowSymbols * 2];\r
- // UInt32 prices2[kLenNumSymbolsTotal];\r
-} CLenPriceEnc;\r
-\r
-#define GET_PRICE_LEN(p, posState, len) \\r
- ((p)->prices[posState][(size_t)(len) - LZMA_MATCH_LEN_MIN])\r
-\r
-/*\r
-#define GET_PRICE_LEN(p, posState, len) \\r
- ((p)->prices2[(size_t)(len) - 2] + ((p)->prices1[posState][((len) - 2) & (kLenNumLowSymbols * 2 - 1)] & (((len) - 2 - kLenNumLowSymbols * 2) >> 9)))\r
-*/\r
-\r
-typedef struct\r
-{\r
- UInt32 range;\r
- unsigned cache;\r
- UInt64 low;\r
- UInt64 cacheSize;\r
- Byte *buf;\r
- Byte *bufLim;\r
- Byte *bufBase;\r
- ISeqOutStream *outStream;\r
- UInt64 processed;\r
- SRes res;\r
-} CRangeEnc;\r
-\r
-\r
-typedef struct\r
-{\r
- CLzmaProb *litProbs;\r
-\r
- unsigned state;\r
- UInt32 reps[LZMA_NUM_REPS];\r
-\r
- CLzmaProb posAlignEncoder[1 << kNumAlignBits];\r
- CLzmaProb isRep[kNumStates];\r
- CLzmaProb isRepG0[kNumStates];\r
- CLzmaProb isRepG1[kNumStates];\r
- CLzmaProb isRepG2[kNumStates];\r
- CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];\r
- CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];\r
-\r
- CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];\r
- CLzmaProb posEncoders[kNumFullDistances];\r
- \r
- CLenEnc lenProbs;\r
- CLenEnc repLenProbs;\r
-\r
-} CSaveState;\r
-\r
-\r
-typedef UInt32 CProbPrice;\r
-\r
-\r
-typedef struct\r
-{\r
- void *matchFinderObj;\r
- IMatchFinder2 matchFinder;\r
-\r
- unsigned optCur;\r
- unsigned optEnd;\r
-\r
- unsigned longestMatchLen;\r
- unsigned numPairs;\r
- UInt32 numAvail;\r
-\r
- unsigned state;\r
- unsigned numFastBytes;\r
- unsigned additionalOffset;\r
- UInt32 reps[LZMA_NUM_REPS];\r
- unsigned lpMask, pbMask;\r
- CLzmaProb *litProbs;\r
- CRangeEnc rc;\r
-\r
- UInt32 backRes;\r
-\r
- unsigned lc, lp, pb;\r
- unsigned lclp;\r
-\r
- BoolInt fastMode;\r
- BoolInt writeEndMark;\r
- BoolInt finished;\r
- BoolInt multiThread;\r
- BoolInt needInit;\r
- // BoolInt _maxMode;\r
-\r
- UInt64 nowPos64;\r
- \r
- unsigned matchPriceCount;\r
- // unsigned alignPriceCount;\r
- int repLenEncCounter;\r
-\r
- unsigned distTableSize;\r
-\r
- UInt32 dictSize;\r
- SRes result;\r
-\r
- #ifndef _7ZIP_ST\r
- BoolInt mtMode;\r
- // begin of CMatchFinderMt is used in LZ thread\r
- CMatchFinderMt matchFinderMt;\r
- // end of CMatchFinderMt is used in BT and HASH threads\r
- // #else\r
- // CMatchFinder matchFinderBase;\r
- #endif\r
- CMatchFinder matchFinderBase;\r
-\r
- \r
- // we suppose that we have 8-bytes alignment after CMatchFinder\r
- \r
- #ifndef _7ZIP_ST\r
- Byte pad[128];\r
- #endif\r
- \r
- // LZ thread\r
- CProbPrice ProbPrices[kBitModelTotal >> kNumMoveReducingBits];\r
-\r
- // we want {len , dist} pairs to be 8-bytes aligned in matches array\r
- UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2];\r
-\r
- // we want 8-bytes alignment here\r
- UInt32 alignPrices[kAlignTableSize];\r
- UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];\r
- UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances];\r
-\r
- CLzmaProb posAlignEncoder[1 << kNumAlignBits];\r
- CLzmaProb isRep[kNumStates];\r
- CLzmaProb isRepG0[kNumStates];\r
- CLzmaProb isRepG1[kNumStates];\r
- CLzmaProb isRepG2[kNumStates];\r
- CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];\r
- CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];\r
- CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];\r
- CLzmaProb posEncoders[kNumFullDistances];\r
- \r
- CLenEnc lenProbs;\r
- CLenEnc repLenProbs;\r
-\r
- #ifndef LZMA_LOG_BSR\r
- Byte g_FastPos[1 << kNumLogBits];\r
- #endif\r
-\r
- CLenPriceEnc lenEnc;\r
- CLenPriceEnc repLenEnc;\r
-\r
- COptimal opt[kNumOpts];\r
-\r
- CSaveState saveState;\r
-\r
- // BoolInt mf_Failure;\r
- #ifndef _7ZIP_ST\r
- Byte pad2[128];\r
- #endif\r
-} CLzmaEnc;\r
-\r
-\r
-#define MFB (p->matchFinderBase)\r
-/*\r
-#ifndef _7ZIP_ST\r
-#define MFB (p->matchFinderMt.MatchFinder)\r
-#endif\r
-*/\r
-\r
-#define COPY_ARR(dest, src, arr) memcpy(dest->arr, src->arr, sizeof(src->arr));\r
-\r
-void LzmaEnc_SaveState(CLzmaEncHandle pp)\r
-{\r
- CLzmaEnc *p = (CLzmaEnc *)pp;\r
- CSaveState *dest = &p->saveState;\r
- \r
- dest->state = p->state;\r
- \r
- dest->lenProbs = p->lenProbs;\r
- dest->repLenProbs = p->repLenProbs;\r
-\r
- COPY_ARR(dest, p, reps);\r
-\r
- COPY_ARR(dest, p, posAlignEncoder);\r
- COPY_ARR(dest, p, isRep);\r
- COPY_ARR(dest, p, isRepG0);\r
- COPY_ARR(dest, p, isRepG1);\r
- COPY_ARR(dest, p, isRepG2);\r
- COPY_ARR(dest, p, isMatch);\r
- COPY_ARR(dest, p, isRep0Long);\r
- COPY_ARR(dest, p, posSlotEncoder);\r
- COPY_ARR(dest, p, posEncoders);\r
-\r
- memcpy(dest->litProbs, p->litProbs, ((UInt32)0x300 << p->lclp) * sizeof(CLzmaProb));\r
-}\r
-\r
-\r
-void LzmaEnc_RestoreState(CLzmaEncHandle pp)\r
-{\r
- CLzmaEnc *dest = (CLzmaEnc *)pp;\r
- const CSaveState *p = &dest->saveState;\r
-\r
- dest->state = p->state;\r
-\r
- dest->lenProbs = p->lenProbs;\r
- dest->repLenProbs = p->repLenProbs;\r
- \r
- COPY_ARR(dest, p, reps);\r
- \r
- COPY_ARR(dest, p, posAlignEncoder);\r
- COPY_ARR(dest, p, isRep);\r
- COPY_ARR(dest, p, isRepG0);\r
- COPY_ARR(dest, p, isRepG1);\r
- COPY_ARR(dest, p, isRepG2);\r
- COPY_ARR(dest, p, isMatch);\r
- COPY_ARR(dest, p, isRep0Long);\r
- COPY_ARR(dest, p, posSlotEncoder);\r
- COPY_ARR(dest, p, posEncoders);\r
-\r
- memcpy(dest->litProbs, p->litProbs, ((UInt32)0x300 << dest->lclp) * sizeof(CLzmaProb));\r
-}\r
-\r
-\r
-\r
-SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)\r
-{\r
- CLzmaEnc *p = (CLzmaEnc *)pp;\r
- CLzmaEncProps props = *props2;\r
- LzmaEncProps_Normalize(&props);\r
-\r
- if (props.lc > LZMA_LC_MAX\r
- || props.lp > LZMA_LP_MAX\r
- || props.pb > LZMA_PB_MAX)\r
- return SZ_ERROR_PARAM;\r
-\r
-\r
- if (props.dictSize > kLzmaMaxHistorySize)\r
- props.dictSize = kLzmaMaxHistorySize;\r
-\r
- #ifndef LZMA_LOG_BSR\r
- {\r
- const UInt64 dict64 = props.dictSize;\r
- if (dict64 > ((UInt64)1 << kDicLogSizeMaxCompress))\r
- return SZ_ERROR_PARAM;\r
- }\r
- #endif\r
-\r
- p->dictSize = props.dictSize;\r
- {\r
- unsigned fb = (unsigned)props.fb;\r
- if (fb < 5)\r
- fb = 5;\r
- if (fb > LZMA_MATCH_LEN_MAX)\r
- fb = LZMA_MATCH_LEN_MAX;\r
- p->numFastBytes = fb;\r
- }\r
- p->lc = (unsigned)props.lc;\r
- p->lp = (unsigned)props.lp;\r
- p->pb = (unsigned)props.pb;\r
- p->fastMode = (props.algo == 0);\r
- // p->_maxMode = True;\r
- MFB.btMode = (Byte)(props.btMode ? 1 : 0);\r
- {\r
- unsigned numHashBytes = 4;\r
- if (props.btMode)\r
- {\r
- if (props.numHashBytes < 2) numHashBytes = 2;\r
- else if (props.numHashBytes < 4) numHashBytes = (unsigned)props.numHashBytes;\r
- }\r
- if (props.numHashBytes >= 5) numHashBytes = 5;\r
-\r
- MFB.numHashBytes = numHashBytes;\r
- }\r
-\r
- MFB.cutValue = props.mc;\r
-\r
- p->writeEndMark = (BoolInt)props.writeEndMark;\r
-\r
- #ifndef _7ZIP_ST\r
- /*\r
- if (newMultiThread != _multiThread)\r
- {\r
- ReleaseMatchFinder();\r
- _multiThread = newMultiThread;\r
- }\r
- */\r
- p->multiThread = (props.numThreads > 1);\r
- p->matchFinderMt.btSync.affinity =\r
- p->matchFinderMt.hashSync.affinity = props.affinity;\r
- #endif\r
-\r
- return SZ_OK;\r
-}\r
-\r
-\r
-void LzmaEnc_SetDataSize(CLzmaEncHandle pp, UInt64 expectedDataSiize)\r
-{\r
- CLzmaEnc *p = (CLzmaEnc *)pp;\r
- MFB.expectedDataSize = expectedDataSiize;\r
-}\r
-\r
-\r
-#define kState_Start 0\r
-#define kState_LitAfterMatch 4\r
-#define kState_LitAfterRep 5\r
-#define kState_MatchAfterLit 7\r
-#define kState_RepAfterLit 8\r
-\r
-static const Byte kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};\r
-static const Byte kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};\r
-static const Byte kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};\r
-static const Byte kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};\r
-\r
-#define IsLitState(s) ((s) < 7)\r
-#define GetLenToPosState2(len) (((len) < kNumLenToPosStates - 1) ? (len) : kNumLenToPosStates - 1)\r
-#define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)\r
-\r
-#define kInfinityPrice (1 << 30)\r
-\r
-static void RangeEnc_Construct(CRangeEnc *p)\r
-{\r
- p->outStream = NULL;\r
- p->bufBase = NULL;\r
-}\r
-\r
-#define RangeEnc_GetProcessed(p) ( (p)->processed + (size_t)((p)->buf - (p)->bufBase) + (p)->cacheSize)\r
-#define RangeEnc_GetProcessed_sizet(p) ((size_t)(p)->processed + (size_t)((p)->buf - (p)->bufBase) + (size_t)(p)->cacheSize)\r
-\r
-#define RC_BUF_SIZE (1 << 16)\r
-\r
-static int RangeEnc_Alloc(CRangeEnc *p, ISzAllocPtr alloc)\r
-{\r
- if (!p->bufBase)\r
- {\r
- p->bufBase = (Byte *)ISzAlloc_Alloc(alloc, RC_BUF_SIZE);\r
- if (!p->bufBase)\r
- return 0;\r
- p->bufLim = p->bufBase + RC_BUF_SIZE;\r
- }\r
- return 1;\r
-}\r
-\r
-static void RangeEnc_Free(CRangeEnc *p, ISzAllocPtr alloc)\r
-{\r
- ISzAlloc_Free(alloc, p->bufBase);\r
- p->bufBase = NULL;\r
-}\r
-\r
-static void RangeEnc_Init(CRangeEnc *p)\r
-{\r
- p->range = 0xFFFFFFFF;\r
- p->cache = 0;\r
- p->low = 0;\r
- p->cacheSize = 0;\r
-\r
- p->buf = p->bufBase;\r
-\r
- p->processed = 0;\r
- p->res = SZ_OK;\r
-}\r
-\r
-MY_NO_INLINE static void RangeEnc_FlushStream(CRangeEnc *p)\r
-{\r
- const size_t num = (size_t)(p->buf - p->bufBase);\r
- if (p->res == SZ_OK)\r
- {\r
- if (num != ISeqOutStream_Write(p->outStream, p->bufBase, num))\r
- p->res = SZ_ERROR_WRITE;\r
- }\r
- p->processed += num;\r
- p->buf = p->bufBase;\r
-}\r
-\r
-MY_NO_INLINE static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p)\r
-{\r
- UInt32 low = (UInt32)p->low;\r
- unsigned high = (unsigned)(p->low >> 32);\r
- p->low = (UInt32)(low << 8);\r
- if (low < (UInt32)0xFF000000 || high != 0)\r
- {\r
- {\r
- Byte *buf = p->buf;\r
- *buf++ = (Byte)(p->cache + high);\r
- p->cache = (unsigned)(low >> 24);\r
- p->buf = buf;\r
- if (buf == p->bufLim)\r
- RangeEnc_FlushStream(p);\r
- if (p->cacheSize == 0)\r
- return;\r
- }\r
- high += 0xFF;\r
- for (;;)\r
- {\r
- Byte *buf = p->buf;\r
- *buf++ = (Byte)(high);\r
- p->buf = buf;\r
- if (buf == p->bufLim)\r
- RangeEnc_FlushStream(p);\r
- if (--p->cacheSize == 0)\r
- return;\r
- }\r
- }\r
- p->cacheSize++;\r
-}\r
-\r
-static void RangeEnc_FlushData(CRangeEnc *p)\r
-{\r
- int i;\r
- for (i = 0; i < 5; i++)\r
- RangeEnc_ShiftLow(p);\r
-}\r
-\r
-#define RC_NORM(p) if (range < kTopValue) { range <<= 8; RangeEnc_ShiftLow(p); }\r
-\r
-#define RC_BIT_PRE(p, prob) \\r
- ttt = *(prob); \\r
- newBound = (range >> kNumBitModelTotalBits) * ttt;\r
-\r
-// #define _LZMA_ENC_USE_BRANCH\r
-\r
-#ifdef _LZMA_ENC_USE_BRANCH\r
-\r
-#define RC_BIT(p, prob, bit) { \\r
- RC_BIT_PRE(p, prob) \\r
- if (bit == 0) { range = newBound; ttt += (kBitModelTotal - ttt) >> kNumMoveBits; } \\r
- else { (p)->low += newBound; range -= newBound; ttt -= ttt >> kNumMoveBits; } \\r
- *(prob) = (CLzmaProb)ttt; \\r
- RC_NORM(p) \\r
- }\r
-\r
-#else\r
-\r
-#define RC_BIT(p, prob, bit) { \\r
- UInt32 mask; \\r
- RC_BIT_PRE(p, prob) \\r
- mask = 0 - (UInt32)bit; \\r
- range &= mask; \\r
- mask &= newBound; \\r
- range -= mask; \\r
- (p)->low += mask; \\r
- mask = (UInt32)bit - 1; \\r
- range += newBound & mask; \\r
- mask &= (kBitModelTotal - ((1 << kNumMoveBits) - 1)); \\r
- mask += ((1 << kNumMoveBits) - 1); \\r
- ttt += (UInt32)((Int32)(mask - ttt) >> kNumMoveBits); \\r
- *(prob) = (CLzmaProb)ttt; \\r
- RC_NORM(p) \\r
- }\r
-\r
-#endif\r
-\r
-\r
-\r
-\r
-#define RC_BIT_0_BASE(p, prob) \\r
- range = newBound; *(prob) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));\r
-\r
-#define RC_BIT_1_BASE(p, prob) \\r
- range -= newBound; (p)->low += newBound; *(prob) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits)); \\r
-\r
-#define RC_BIT_0(p, prob) \\r
- RC_BIT_0_BASE(p, prob) \\r
- RC_NORM(p)\r
-\r
-#define RC_BIT_1(p, prob) \\r
- RC_BIT_1_BASE(p, prob) \\r
- RC_NORM(p)\r
-\r
-static void RangeEnc_EncodeBit_0(CRangeEnc *p, CLzmaProb *prob)\r
-{\r
- UInt32 range, ttt, newBound;\r
- range = p->range;\r
- RC_BIT_PRE(p, prob)\r
- RC_BIT_0(p, prob)\r
- p->range = range;\r
-}\r
-\r
-static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 sym)\r
-{\r
- UInt32 range = p->range;\r
- sym |= 0x100;\r
- do\r
- {\r
- UInt32 ttt, newBound;\r
- // RangeEnc_EncodeBit(p, probs + (sym >> 8), (sym >> 7) & 1);\r
- CLzmaProb *prob = probs + (sym >> 8);\r
- UInt32 bit = (sym >> 7) & 1;\r
- sym <<= 1;\r
- RC_BIT(p, prob, bit);\r
- }\r
- while (sym < 0x10000);\r
- p->range = range;\r
-}\r
-\r
-static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 sym, UInt32 matchByte)\r
-{\r
- UInt32 range = p->range;\r
- UInt32 offs = 0x100;\r
- sym |= 0x100;\r
- do\r
- {\r
- UInt32 ttt, newBound;\r
- CLzmaProb *prob;\r
- UInt32 bit;\r
- matchByte <<= 1;\r
- // RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (sym >> 8)), (sym >> 7) & 1);\r
- prob = probs + (offs + (matchByte & offs) + (sym >> 8));\r
- bit = (sym >> 7) & 1;\r
- sym <<= 1;\r
- offs &= ~(matchByte ^ sym);\r
- RC_BIT(p, prob, bit);\r
- }\r
- while (sym < 0x10000);\r
- p->range = range;\r
-}\r
-\r
-\r
-\r
-static void LzmaEnc_InitPriceTables(CProbPrice *ProbPrices)\r
-{\r
- UInt32 i;\r
- for (i = 0; i < (kBitModelTotal >> kNumMoveReducingBits); i++)\r
- {\r
- const unsigned kCyclesBits = kNumBitPriceShiftBits;\r
- UInt32 w = (i << kNumMoveReducingBits) + (1 << (kNumMoveReducingBits - 1));\r
- unsigned bitCount = 0;\r
- unsigned j;\r
- for (j = 0; j < kCyclesBits; j++)\r
- {\r
- w = w * w;\r
- bitCount <<= 1;\r
- while (w >= ((UInt32)1 << 16))\r
- {\r
- w >>= 1;\r
- bitCount++;\r
- }\r
- }\r
- ProbPrices[i] = (CProbPrice)(((unsigned)kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount);\r
- // printf("\n%3d: %5d", i, ProbPrices[i]);\r
- }\r
-}\r
-\r
-\r
-#define GET_PRICE(prob, bit) \\r
- p->ProbPrices[((prob) ^ (unsigned)(((-(int)(bit))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];\r
-\r
-#define GET_PRICEa(prob, bit) \\r
- ProbPrices[((prob) ^ (unsigned)((-((int)(bit))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];\r
-\r
-#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]\r
-#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]\r
-\r
-#define GET_PRICEa_0(prob) ProbPrices[(prob) >> kNumMoveReducingBits]\r
-#define GET_PRICEa_1(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]\r
-\r
-\r
-static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 sym, const CProbPrice *ProbPrices)\r
-{\r
- UInt32 price = 0;\r
- sym |= 0x100;\r
- do\r
- {\r
- unsigned bit = sym & 1;\r
- sym >>= 1;\r
- price += GET_PRICEa(probs[sym], bit);\r
- }\r
- while (sym >= 2);\r
- return price;\r
-}\r
-\r
-\r
-static UInt32 LitEnc_Matched_GetPrice(const CLzmaProb *probs, UInt32 sym, UInt32 matchByte, const CProbPrice *ProbPrices)\r
-{\r
- UInt32 price = 0;\r
- UInt32 offs = 0x100;\r
- sym |= 0x100;\r
- do\r
- {\r
- matchByte <<= 1;\r
- price += GET_PRICEa(probs[offs + (matchByte & offs) + (sym >> 8)], (sym >> 7) & 1);\r
- sym <<= 1;\r
- offs &= ~(matchByte ^ sym);\r
- }\r
- while (sym < 0x10000);\r
- return price;\r
-}\r
-\r
-\r
-static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, unsigned numBits, unsigned sym)\r
-{\r
- UInt32 range = rc->range;\r
- unsigned m = 1;\r
- do\r
- {\r
- UInt32 ttt, newBound;\r
- unsigned bit = sym & 1;\r
- // RangeEnc_EncodeBit(rc, probs + m, bit);\r
- sym >>= 1;\r
- RC_BIT(rc, probs + m, bit);\r
- m = (m << 1) | bit;\r
- }\r
- while (--numBits);\r
- rc->range = range;\r
-}\r
-\r
-\r
-\r
-static void LenEnc_Init(CLenEnc *p)\r
-{\r
- unsigned i;\r
- for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << (kLenNumLowBits + 1)); i++)\r
- p->low[i] = kProbInitValue;\r
- for (i = 0; i < kLenNumHighSymbols; i++)\r
- p->high[i] = kProbInitValue;\r
-}\r
-\r
-static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, unsigned sym, unsigned posState)\r
-{\r
- UInt32 range, ttt, newBound;\r
- CLzmaProb *probs = p->low;\r
- range = rc->range;\r
- RC_BIT_PRE(rc, probs);\r
- if (sym >= kLenNumLowSymbols)\r
- {\r
- RC_BIT_1(rc, probs);\r
- probs += kLenNumLowSymbols;\r
- RC_BIT_PRE(rc, probs);\r
- if (sym >= kLenNumLowSymbols * 2)\r
- {\r
- RC_BIT_1(rc, probs);\r
- rc->range = range;\r
- // RcTree_Encode(rc, p->high, kLenNumHighBits, sym - kLenNumLowSymbols * 2);\r
- LitEnc_Encode(rc, p->high, sym - kLenNumLowSymbols * 2);\r
- return;\r
- }\r
- sym -= kLenNumLowSymbols;\r
- }\r
-\r
- // RcTree_Encode(rc, probs + (posState << kLenNumLowBits), kLenNumLowBits, sym);\r
- {\r
- unsigned m;\r
- unsigned bit;\r
- RC_BIT_0(rc, probs);\r
- probs += (posState << (1 + kLenNumLowBits));\r
- bit = (sym >> 2) ; RC_BIT(rc, probs + 1, bit); m = (1 << 1) + bit;\r
- bit = (sym >> 1) & 1; RC_BIT(rc, probs + m, bit); m = (m << 1) + bit;\r
- bit = sym & 1; RC_BIT(rc, probs + m, bit);\r
- rc->range = range;\r
- }\r
-}\r
-\r
-static void SetPrices_3(const CLzmaProb *probs, UInt32 startPrice, UInt32 *prices, const CProbPrice *ProbPrices)\r
-{\r
- unsigned i;\r
- for (i = 0; i < 8; i += 2)\r
- {\r
- UInt32 price = startPrice;\r
- UInt32 prob;\r
- price += GET_PRICEa(probs[1 ], (i >> 2));\r
- price += GET_PRICEa(probs[2 + (i >> 2)], (i >> 1) & 1);\r
- prob = probs[4 + (i >> 1)];\r
- prices[i ] = price + GET_PRICEa_0(prob);\r
- prices[i + 1] = price + GET_PRICEa_1(prob);\r
- }\r
-}\r
-\r
-\r
-MY_NO_INLINE static void MY_FAST_CALL LenPriceEnc_UpdateTables(\r
- CLenPriceEnc *p,\r
- unsigned numPosStates,\r
- const CLenEnc *enc,\r
- const CProbPrice *ProbPrices)\r
-{\r
- UInt32 b;\r
- \r
- {\r
- unsigned prob = enc->low[0];\r
- UInt32 a, c;\r
- unsigned posState;\r
- b = GET_PRICEa_1(prob);\r
- a = GET_PRICEa_0(prob);\r
- c = b + GET_PRICEa_0(enc->low[kLenNumLowSymbols]);\r
- for (posState = 0; posState < numPosStates; posState++)\r
- {\r
- UInt32 *prices = p->prices[posState];\r
- const CLzmaProb *probs = enc->low + (posState << (1 + kLenNumLowBits));\r
- SetPrices_3(probs, a, prices, ProbPrices);\r
- SetPrices_3(probs + kLenNumLowSymbols, c, prices + kLenNumLowSymbols, ProbPrices);\r
- }\r
- }\r
-\r
- /*\r
- {\r
- unsigned i;\r
- UInt32 b;\r
- a = GET_PRICEa_0(enc->low[0]);\r
- for (i = 0; i < kLenNumLowSymbols; i++)\r
- p->prices2[i] = a;\r
- a = GET_PRICEa_1(enc->low[0]);\r
- b = a + GET_PRICEa_0(enc->low[kLenNumLowSymbols]);\r
- for (i = kLenNumLowSymbols; i < kLenNumLowSymbols * 2; i++)\r
- p->prices2[i] = b;\r
- a += GET_PRICEa_1(enc->low[kLenNumLowSymbols]);\r
- }\r
- */\r
- \r
- // p->counter = numSymbols;\r
- // p->counter = 64;\r
-\r
- {\r
- unsigned i = p->tableSize;\r
- \r
- if (i > kLenNumLowSymbols * 2)\r
- {\r
- const CLzmaProb *probs = enc->high;\r
- UInt32 *prices = p->prices[0] + kLenNumLowSymbols * 2;\r
- i -= kLenNumLowSymbols * 2 - 1;\r
- i >>= 1;\r
- b += GET_PRICEa_1(enc->low[kLenNumLowSymbols]);\r
- do\r
- {\r
- /*\r
- p->prices2[i] = a +\r
- // RcTree_GetPrice(enc->high, kLenNumHighBits, i - kLenNumLowSymbols * 2, ProbPrices);\r
- LitEnc_GetPrice(probs, i - kLenNumLowSymbols * 2, ProbPrices);\r
- */\r
- // UInt32 price = a + RcTree_GetPrice(probs, kLenNumHighBits - 1, sym, ProbPrices);\r
- unsigned sym = --i + (1 << (kLenNumHighBits - 1));\r
- UInt32 price = b;\r
- do\r
- {\r
- unsigned bit = sym & 1;\r
- sym >>= 1;\r
- price += GET_PRICEa(probs[sym], bit);\r
- }\r
- while (sym >= 2);\r
-\r
- {\r
- unsigned prob = probs[(size_t)i + (1 << (kLenNumHighBits - 1))];\r
- prices[(size_t)i * 2 ] = price + GET_PRICEa_0(prob);\r
- prices[(size_t)i * 2 + 1] = price + GET_PRICEa_1(prob);\r
- }\r
- }\r
- while (i);\r
-\r
- {\r
- unsigned posState;\r
- size_t num = (p->tableSize - kLenNumLowSymbols * 2) * sizeof(p->prices[0][0]);\r
- for (posState = 1; posState < numPosStates; posState++)\r
- memcpy(p->prices[posState] + kLenNumLowSymbols * 2, p->prices[0] + kLenNumLowSymbols * 2, num);\r
- }\r
- }\r
- }\r
-}\r
-\r
-/*\r
- #ifdef SHOW_STAT\r
- g_STAT_OFFSET += num;\r
- printf("\n MovePos %u", num);\r
- #endif\r
-*/\r
- \r
-#define MOVE_POS(p, num) { \\r
- p->additionalOffset += (num); \\r
- p->matchFinder.Skip(p->matchFinderObj, (UInt32)(num)); }\r
-\r
-\r
-static unsigned ReadMatchDistances(CLzmaEnc *p, unsigned *numPairsRes)\r
-{\r
- unsigned numPairs;\r
- \r
- p->additionalOffset++;\r
- p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);\r
- {\r
- const UInt32 *d = p->matchFinder.GetMatches(p->matchFinderObj, p->matches);\r
- // if (!d) { p->mf_Failure = True; *numPairsRes = 0; return 0; }\r
- numPairs = (unsigned)(d - p->matches);\r
- }\r
- *numPairsRes = numPairs;\r
- \r
- #ifdef SHOW_STAT\r
- printf("\n i = %u numPairs = %u ", g_STAT_OFFSET, numPairs / 2);\r
- g_STAT_OFFSET++;\r
- {\r
- unsigned i;\r
- for (i = 0; i < numPairs; i += 2)\r
- printf("%2u %6u | ", p->matches[i], p->matches[i + 1]);\r
- }\r
- #endif\r
- \r
- if (numPairs == 0)\r
- return 0;\r
- {\r
- const unsigned len = p->matches[(size_t)numPairs - 2];\r
- if (len != p->numFastBytes)\r
- return len;\r
- {\r
- UInt32 numAvail = p->numAvail;\r
- if (numAvail > LZMA_MATCH_LEN_MAX)\r
- numAvail = LZMA_MATCH_LEN_MAX;\r
- {\r
- const Byte *p1 = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;\r
- const Byte *p2 = p1 + len;\r
- const ptrdiff_t dif = (ptrdiff_t)-1 - (ptrdiff_t)p->matches[(size_t)numPairs - 1];\r
- const Byte *lim = p1 + numAvail;\r
- for (; p2 != lim && *p2 == p2[dif]; p2++)\r
- {}\r
- return (unsigned)(p2 - p1);\r
- }\r
- }\r
- }\r
-}\r
-\r
-#define MARK_LIT ((UInt32)(Int32)-1)\r
-\r
-#define MakeAs_Lit(p) { (p)->dist = MARK_LIT; (p)->extra = 0; }\r
-#define MakeAs_ShortRep(p) { (p)->dist = 0; (p)->extra = 0; }\r
-#define IsShortRep(p) ((p)->dist == 0)\r
-\r
-\r
-#define GetPrice_ShortRep(p, state, posState) \\r
- ( GET_PRICE_0(p->isRepG0[state]) + GET_PRICE_0(p->isRep0Long[state][posState]))\r
-\r
-#define GetPrice_Rep_0(p, state, posState) ( \\r
- GET_PRICE_1(p->isMatch[state][posState]) \\r
- + GET_PRICE_1(p->isRep0Long[state][posState])) \\r
- + GET_PRICE_1(p->isRep[state]) \\r
- + GET_PRICE_0(p->isRepG0[state])\r
- \r
-MY_FORCE_INLINE\r
-static UInt32 GetPrice_PureRep(const CLzmaEnc *p, unsigned repIndex, size_t state, size_t posState)\r
-{\r
- UInt32 price;\r
- UInt32 prob = p->isRepG0[state];\r
- if (repIndex == 0)\r
- {\r
- price = GET_PRICE_0(prob);\r
- price += GET_PRICE_1(p->isRep0Long[state][posState]);\r
- }\r
- else\r
- {\r
- price = GET_PRICE_1(prob);\r
- prob = p->isRepG1[state];\r
- if (repIndex == 1)\r
- price += GET_PRICE_0(prob);\r
- else\r
- {\r
- price += GET_PRICE_1(prob);\r
- price += GET_PRICE(p->isRepG2[state], repIndex - 2);\r
- }\r
- }\r
- return price;\r
-}\r
-\r
-\r
-static unsigned Backward(CLzmaEnc *p, unsigned cur)\r
-{\r
- unsigned wr = cur + 1;\r
- p->optEnd = wr;\r
-\r
- for (;;)\r
- {\r
- UInt32 dist = p->opt[cur].dist;\r
- unsigned len = (unsigned)p->opt[cur].len;\r
- unsigned extra = (unsigned)p->opt[cur].extra;\r
- cur -= len;\r
-\r
- if (extra)\r
- {\r
- wr--;\r
- p->opt[wr].len = (UInt32)len;\r
- cur -= extra;\r
- len = extra;\r
- if (extra == 1)\r
- {\r
- p->opt[wr].dist = dist;\r
- dist = MARK_LIT;\r
- }\r
- else\r
- {\r
- p->opt[wr].dist = 0;\r
- len--;\r
- wr--;\r
- p->opt[wr].dist = MARK_LIT;\r
- p->opt[wr].len = 1;\r
- }\r
- }\r
-\r
- if (cur == 0)\r
- {\r
- p->backRes = dist;\r
- p->optCur = wr;\r
- return len;\r
- }\r
- \r
- wr--;\r
- p->opt[wr].dist = dist;\r
- p->opt[wr].len = (UInt32)len;\r
- }\r
-}\r
-\r
-\r
-\r
-#define LIT_PROBS(pos, prevByte) \\r
- (p->litProbs + (UInt32)3 * (((((pos) << 8) + (prevByte)) & p->lpMask) << p->lc))\r
-\r
-\r
-static unsigned GetOptimum(CLzmaEnc *p, UInt32 position)\r
-{\r
- unsigned last, cur;\r
- UInt32 reps[LZMA_NUM_REPS];\r
- unsigned repLens[LZMA_NUM_REPS];\r
- UInt32 *matches;\r
-\r
- {\r
- UInt32 numAvail;\r
- unsigned numPairs, mainLen, repMaxIndex, i, posState;\r
- UInt32 matchPrice, repMatchPrice;\r
- const Byte *data;\r
- Byte curByte, matchByte;\r
- \r
- p->optCur = p->optEnd = 0;\r
- \r
- if (p->additionalOffset == 0)\r
- mainLen = ReadMatchDistances(p, &numPairs);\r
- else\r
- {\r
- mainLen = p->longestMatchLen;\r
- numPairs = p->numPairs;\r
- }\r
- \r
- numAvail = p->numAvail;\r
- if (numAvail < 2)\r
- {\r
- p->backRes = MARK_LIT;\r
- return 1;\r
- }\r
- if (numAvail > LZMA_MATCH_LEN_MAX)\r
- numAvail = LZMA_MATCH_LEN_MAX;\r
- \r
- data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;\r
- repMaxIndex = 0;\r
- \r
- for (i = 0; i < LZMA_NUM_REPS; i++)\r
- {\r
- unsigned len;\r
- const Byte *data2;\r
- reps[i] = p->reps[i];\r
- data2 = data - reps[i];\r
- if (data[0] != data2[0] || data[1] != data2[1])\r
- {\r
- repLens[i] = 0;\r
- continue;\r
- }\r
- for (len = 2; len < numAvail && data[len] == data2[len]; len++)\r
- {}\r
- repLens[i] = len;\r
- if (len > repLens[repMaxIndex])\r
- repMaxIndex = i;\r
- if (len == LZMA_MATCH_LEN_MAX) // 21.03 : optimization\r
- break;\r
- }\r
- \r
- if (repLens[repMaxIndex] >= p->numFastBytes)\r
- {\r
- unsigned len;\r
- p->backRes = (UInt32)repMaxIndex;\r
- len = repLens[repMaxIndex];\r
- MOVE_POS(p, len - 1)\r
- return len;\r
- }\r
- \r
- matches = p->matches;\r
- #define MATCHES matches\r
- // #define MATCHES p->matches\r
- \r
- if (mainLen >= p->numFastBytes)\r
- {\r
- p->backRes = MATCHES[(size_t)numPairs - 1] + LZMA_NUM_REPS;\r
- MOVE_POS(p, mainLen - 1)\r
- return mainLen;\r
- }\r
- \r
- curByte = *data;\r
- matchByte = *(data - reps[0]);\r
-\r
- last = repLens[repMaxIndex];\r
- if (last <= mainLen)\r
- last = mainLen;\r
- \r
- if (last < 2 && curByte != matchByte)\r
- {\r
- p->backRes = MARK_LIT;\r
- return 1;\r
- }\r
- \r
- p->opt[0].state = (CState)p->state;\r
- \r
- posState = (position & p->pbMask);\r
- \r
- {\r
- const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));\r
- p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) +\r
- (!IsLitState(p->state) ?\r
- LitEnc_Matched_GetPrice(probs, curByte, matchByte, p->ProbPrices) :\r
- LitEnc_GetPrice(probs, curByte, p->ProbPrices));\r
- }\r
-\r
- MakeAs_Lit(&p->opt[1]);\r
- \r
- matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);\r
- repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);\r
- \r
- // 18.06\r
- if (matchByte == curByte && repLens[0] == 0)\r
- {\r
- UInt32 shortRepPrice = repMatchPrice + GetPrice_ShortRep(p, p->state, posState);\r
- if (shortRepPrice < p->opt[1].price)\r
- {\r
- p->opt[1].price = shortRepPrice;\r
- MakeAs_ShortRep(&p->opt[1]);\r
- }\r
- if (last < 2)\r
- {\r
- p->backRes = p->opt[1].dist;\r
- return 1;\r
- }\r
- }\r
- \r
- p->opt[1].len = 1;\r
- \r
- p->opt[0].reps[0] = reps[0];\r
- p->opt[0].reps[1] = reps[1];\r
- p->opt[0].reps[2] = reps[2];\r
- p->opt[0].reps[3] = reps[3];\r
- \r
- // ---------- REP ----------\r
- \r
- for (i = 0; i < LZMA_NUM_REPS; i++)\r
- {\r
- unsigned repLen = repLens[i];\r
- UInt32 price;\r
- if (repLen < 2)\r
- continue;\r
- price = repMatchPrice + GetPrice_PureRep(p, i, p->state, posState);\r
- do\r
- {\r
- UInt32 price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState, repLen);\r
- COptimal *opt = &p->opt[repLen];\r
- if (price2 < opt->price)\r
- {\r
- opt->price = price2;\r
- opt->len = (UInt32)repLen;\r
- opt->dist = (UInt32)i;\r
- opt->extra = 0;\r
- }\r
- }\r
- while (--repLen >= 2);\r
- }\r
- \r
- \r
- // ---------- MATCH ----------\r
- {\r
- unsigned len = repLens[0] + 1;\r
- if (len <= mainLen)\r
- {\r
- unsigned offs = 0;\r
- UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);\r
-\r
- if (len < 2)\r
- len = 2;\r
- else\r
- while (len > MATCHES[offs])\r
- offs += 2;\r
- \r
- for (; ; len++)\r
- {\r
- COptimal *opt;\r
- UInt32 dist = MATCHES[(size_t)offs + 1];\r
- UInt32 price = normalMatchPrice + GET_PRICE_LEN(&p->lenEnc, posState, len);\r
- unsigned lenToPosState = GetLenToPosState(len);\r
- \r
- if (dist < kNumFullDistances)\r
- price += p->distancesPrices[lenToPosState][dist & (kNumFullDistances - 1)];\r
- else\r
- {\r
- unsigned slot;\r
- GetPosSlot2(dist, slot);\r
- price += p->alignPrices[dist & kAlignMask];\r
- price += p->posSlotPrices[lenToPosState][slot];\r
- }\r
- \r
- opt = &p->opt[len];\r
- \r
- if (price < opt->price)\r
- {\r
- opt->price = price;\r
- opt->len = (UInt32)len;\r
- opt->dist = dist + LZMA_NUM_REPS;\r
- opt->extra = 0;\r
- }\r
- \r
- if (len == MATCHES[offs])\r
- {\r
- offs += 2;\r
- if (offs == numPairs)\r
- break;\r
- }\r
- }\r
- }\r
- }\r
- \r
-\r
- cur = 0;\r
-\r
- #ifdef SHOW_STAT2\r
- /* if (position >= 0) */\r
- {\r
- unsigned i;\r
- printf("\n pos = %4X", position);\r
- for (i = cur; i <= last; i++)\r
- printf("\nprice[%4X] = %u", position - cur + i, p->opt[i].price);\r
- }\r
- #endif\r
- }\r
-\r
-\r
- \r
- // ---------- Optimal Parsing ----------\r
-\r
- for (;;)\r
- {\r
- unsigned numAvail;\r
- UInt32 numAvailFull;\r
- unsigned newLen, numPairs, prev, state, posState, startLen;\r
- UInt32 litPrice, matchPrice, repMatchPrice;\r
- BoolInt nextIsLit;\r
- Byte curByte, matchByte;\r
- const Byte *data;\r
- COptimal *curOpt, *nextOpt;\r
-\r
- if (++cur == last)\r
- break;\r
- \r
- // 18.06\r
- if (cur >= kNumOpts - 64)\r
- {\r
- unsigned j, best;\r
- UInt32 price = p->opt[cur].price;\r
- best = cur;\r
- for (j = cur + 1; j <= last; j++)\r
- {\r
- UInt32 price2 = p->opt[j].price;\r
- if (price >= price2)\r
- {\r
- price = price2;\r
- best = j;\r
- }\r
- }\r
- {\r
- unsigned delta = best - cur;\r
- if (delta != 0)\r
- {\r
- MOVE_POS(p, delta);\r
- }\r
- }\r
- cur = best;\r
- break;\r
- }\r
-\r
- newLen = ReadMatchDistances(p, &numPairs);\r
- \r
- if (newLen >= p->numFastBytes)\r
- {\r
- p->numPairs = numPairs;\r
- p->longestMatchLen = newLen;\r
- break;\r
- }\r
- \r
- curOpt = &p->opt[cur];\r
-\r
- position++;\r
-\r
- // we need that check here, if skip_items in p->opt are possible\r
- /*\r
- if (curOpt->price >= kInfinityPrice)\r
- continue;\r
- */\r
-\r
- prev = cur - curOpt->len;\r
-\r
- if (curOpt->len == 1)\r
- {\r
- state = (unsigned)p->opt[prev].state;\r
- if (IsShortRep(curOpt))\r
- state = kShortRepNextStates[state];\r
- else\r
- state = kLiteralNextStates[state];\r
- }\r
- else\r
- {\r
- const COptimal *prevOpt;\r
- UInt32 b0;\r
- UInt32 dist = curOpt->dist;\r
-\r
- if (curOpt->extra)\r
- {\r
- prev -= (unsigned)curOpt->extra;\r
- state = kState_RepAfterLit;\r
- if (curOpt->extra == 1)\r
- state = (dist < LZMA_NUM_REPS ? kState_RepAfterLit : kState_MatchAfterLit);\r
- }\r
- else\r
- {\r
- state = (unsigned)p->opt[prev].state;\r
- if (dist < LZMA_NUM_REPS)\r
- state = kRepNextStates[state];\r
- else\r
- state = kMatchNextStates[state];\r
- }\r
-\r
- prevOpt = &p->opt[prev];\r
- b0 = prevOpt->reps[0];\r
-\r
- if (dist < LZMA_NUM_REPS)\r
- {\r
- if (dist == 0)\r
- {\r
- reps[0] = b0;\r
- reps[1] = prevOpt->reps[1];\r
- reps[2] = prevOpt->reps[2];\r
- reps[3] = prevOpt->reps[3];\r
- }\r
- else\r
- {\r
- reps[1] = b0;\r
- b0 = prevOpt->reps[1];\r
- if (dist == 1)\r
- {\r
- reps[0] = b0;\r
- reps[2] = prevOpt->reps[2];\r
- reps[3] = prevOpt->reps[3];\r
- }\r
- else\r
- {\r
- reps[2] = b0;\r
- reps[0] = prevOpt->reps[dist];\r
- reps[3] = prevOpt->reps[dist ^ 1];\r
- }\r
- }\r
- }\r
- else\r
- {\r
- reps[0] = (dist - LZMA_NUM_REPS + 1);\r
- reps[1] = b0;\r
- reps[2] = prevOpt->reps[1];\r
- reps[3] = prevOpt->reps[2];\r
- }\r
- }\r
- \r
- curOpt->state = (CState)state;\r
- curOpt->reps[0] = reps[0];\r
- curOpt->reps[1] = reps[1];\r
- curOpt->reps[2] = reps[2];\r
- curOpt->reps[3] = reps[3];\r
-\r
- data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;\r
- curByte = *data;\r
- matchByte = *(data - reps[0]);\r
-\r
- posState = (position & p->pbMask);\r
-\r
- /*\r
- The order of Price checks:\r
- < LIT\r
- <= SHORT_REP\r
- < LIT : REP_0\r
- < REP [ : LIT : REP_0 ]\r
- < MATCH [ : LIT : REP_0 ]\r
- */\r
-\r
- {\r
- UInt32 curPrice = curOpt->price;\r
- unsigned prob = p->isMatch[state][posState];\r
- matchPrice = curPrice + GET_PRICE_1(prob);\r
- litPrice = curPrice + GET_PRICE_0(prob);\r
- }\r
-\r
- nextOpt = &p->opt[(size_t)cur + 1];\r
- nextIsLit = False;\r
-\r
- // here we can allow skip_items in p->opt, if we don't check (nextOpt->price < kInfinityPrice)\r
- // 18.new.06\r
- if ((nextOpt->price < kInfinityPrice\r
- // && !IsLitState(state)\r
- && matchByte == curByte)\r
- || litPrice > nextOpt->price\r
- )\r
- litPrice = 0;\r
- else\r
- {\r
- const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));\r
- litPrice += (!IsLitState(state) ?\r
- LitEnc_Matched_GetPrice(probs, curByte, matchByte, p->ProbPrices) :\r
- LitEnc_GetPrice(probs, curByte, p->ProbPrices));\r
- \r
- if (litPrice < nextOpt->price)\r
- {\r
- nextOpt->price = litPrice;\r
- nextOpt->len = 1;\r
- MakeAs_Lit(nextOpt);\r
- nextIsLit = True;\r
- }\r
- }\r
-\r
- repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]);\r
- \r
- numAvailFull = p->numAvail;\r
- {\r
- unsigned temp = kNumOpts - 1 - cur;\r
- if (numAvailFull > temp)\r
- numAvailFull = (UInt32)temp;\r
- }\r
-\r
- // 18.06\r
- // ---------- SHORT_REP ----------\r
- if (IsLitState(state)) // 18.new\r
- if (matchByte == curByte)\r
- if (repMatchPrice < nextOpt->price) // 18.new\r
- // if (numAvailFull < 2 || data[1] != *(data - reps[0] + 1))\r
- if (\r
- // nextOpt->price >= kInfinityPrice ||\r
- nextOpt->len < 2 // we can check nextOpt->len, if skip items are not allowed in p->opt\r
- || (nextOpt->dist != 0\r
- // && nextOpt->extra <= 1 // 17.old\r
- )\r
- )\r
- {\r
- UInt32 shortRepPrice = repMatchPrice + GetPrice_ShortRep(p, state, posState);\r
- // if (shortRepPrice <= nextOpt->price) // 17.old\r
- if (shortRepPrice < nextOpt->price) // 18.new\r
- {\r
- nextOpt->price = shortRepPrice;\r
- nextOpt->len = 1;\r
- MakeAs_ShortRep(nextOpt);\r
- nextIsLit = False;\r
- }\r
- }\r
- \r
- if (numAvailFull < 2)\r
- continue;\r
- numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes);\r
-\r
- // numAvail <= p->numFastBytes\r
-\r
- // ---------- LIT : REP_0 ----------\r
-\r
- if (!nextIsLit\r
- && litPrice != 0 // 18.new\r
- && matchByte != curByte\r
- && numAvailFull > 2)\r
- {\r
- const Byte *data2 = data - reps[0];\r
- if (data[1] == data2[1] && data[2] == data2[2])\r
- {\r
- unsigned len;\r
- unsigned limit = p->numFastBytes + 1;\r
- if (limit > numAvailFull)\r
- limit = numAvailFull;\r
- for (len = 3; len < limit && data[len] == data2[len]; len++)\r
- {}\r
- \r
- {\r
- unsigned state2 = kLiteralNextStates[state];\r
- unsigned posState2 = (position + 1) & p->pbMask;\r
- UInt32 price = litPrice + GetPrice_Rep_0(p, state2, posState2);\r
- {\r
- unsigned offset = cur + len;\r
-\r
- if (last < offset)\r
- last = offset;\r
- \r
- // do\r
- {\r
- UInt32 price2;\r
- COptimal *opt;\r
- len--;\r
- // price2 = price + GetPrice_Len_Rep_0(p, len, state2, posState2);\r
- price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState2, len);\r
-\r
- opt = &p->opt[offset];\r
- // offset--;\r
- if (price2 < opt->price)\r
- {\r
- opt->price = price2;\r
- opt->len = (UInt32)len;\r
- opt->dist = 0;\r
- opt->extra = 1;\r
- }\r
- }\r
- // while (len >= 3);\r
- }\r
- }\r
- }\r
- }\r
- \r
- startLen = 2; /* speed optimization */\r
-\r
- {\r
- // ---------- REP ----------\r
- unsigned repIndex = 0; // 17.old\r
- // unsigned repIndex = IsLitState(state) ? 0 : 1; // 18.notused\r
- for (; repIndex < LZMA_NUM_REPS; repIndex++)\r
- {\r
- unsigned len;\r
- UInt32 price;\r
- const Byte *data2 = data - reps[repIndex];\r
- if (data[0] != data2[0] || data[1] != data2[1])\r
- continue;\r
- \r
- for (len = 2; len < numAvail && data[len] == data2[len]; len++)\r
- {}\r
- \r
- // if (len < startLen) continue; // 18.new: speed optimization\r
-\r
- {\r
- unsigned offset = cur + len;\r
- if (last < offset)\r
- last = offset;\r
- }\r
- {\r
- unsigned len2 = len;\r
- price = repMatchPrice + GetPrice_PureRep(p, repIndex, state, posState);\r
- do\r
- {\r
- UInt32 price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState, len2);\r
- COptimal *opt = &p->opt[cur + len2];\r
- if (price2 < opt->price)\r
- {\r
- opt->price = price2;\r
- opt->len = (UInt32)len2;\r
- opt->dist = (UInt32)repIndex;\r
- opt->extra = 0;\r
- }\r
- }\r
- while (--len2 >= 2);\r
- }\r
- \r
- if (repIndex == 0) startLen = len + 1; // 17.old\r
- // startLen = len + 1; // 18.new\r
-\r
- /* if (_maxMode) */\r
- {\r
- // ---------- REP : LIT : REP_0 ----------\r
- // numFastBytes + 1 + numFastBytes\r
-\r
- unsigned len2 = len + 1;\r
- unsigned limit = len2 + p->numFastBytes;\r
- if (limit > numAvailFull)\r
- limit = numAvailFull;\r
- \r
- len2 += 2;\r
- if (len2 <= limit)\r
- if (data[len2 - 2] == data2[len2 - 2])\r
- if (data[len2 - 1] == data2[len2 - 1])\r
- {\r
- unsigned state2 = kRepNextStates[state];\r
- unsigned posState2 = (position + len) & p->pbMask;\r
- price += GET_PRICE_LEN(&p->repLenEnc, posState, len)\r
- + GET_PRICE_0(p->isMatch[state2][posState2])\r
- + LitEnc_Matched_GetPrice(LIT_PROBS(position + len, data[(size_t)len - 1]),\r
- data[len], data2[len], p->ProbPrices);\r
- \r
- // state2 = kLiteralNextStates[state2];\r
- state2 = kState_LitAfterRep;\r
- posState2 = (posState2 + 1) & p->pbMask;\r
-\r
-\r
- price += GetPrice_Rep_0(p, state2, posState2);\r
-\r
- for (; len2 < limit && data[len2] == data2[len2]; len2++)\r
- {}\r
- \r
- len2 -= len;\r
- // if (len2 >= 3)\r
- {\r
- {\r
- unsigned offset = cur + len + len2;\r
-\r
- if (last < offset)\r
- last = offset;\r
- // do\r
- {\r
- UInt32 price2;\r
- COptimal *opt;\r
- len2--;\r
- // price2 = price + GetPrice_Len_Rep_0(p, len2, state2, posState2);\r
- price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState2, len2);\r
-\r
- opt = &p->opt[offset];\r
- // offset--;\r
- if (price2 < opt->price)\r
- {\r
- opt->price = price2;\r
- opt->len = (UInt32)len2;\r
- opt->extra = (CExtra)(len + 1);\r
- opt->dist = (UInt32)repIndex;\r
- }\r
- }\r
- // while (len2 >= 3);\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
-\r
-\r
- // ---------- MATCH ----------\r
- /* for (unsigned len = 2; len <= newLen; len++) */\r
- if (newLen > numAvail)\r
- {\r
- newLen = numAvail;\r
- for (numPairs = 0; newLen > MATCHES[numPairs]; numPairs += 2);\r
- MATCHES[numPairs] = (UInt32)newLen;\r
- numPairs += 2;\r
- }\r
- \r
- // startLen = 2; /* speed optimization */\r
-\r
- if (newLen >= startLen)\r
- {\r
- UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);\r
- UInt32 dist;\r
- unsigned offs, posSlot, len;\r
- \r
- {\r
- unsigned offset = cur + newLen;\r
- if (last < offset)\r
- last = offset;\r
- }\r
-\r
- offs = 0;\r
- while (startLen > MATCHES[offs])\r
- offs += 2;\r
- dist = MATCHES[(size_t)offs + 1];\r
- \r
- // if (dist >= kNumFullDistances)\r
- GetPosSlot2(dist, posSlot);\r
- \r
- for (len = /*2*/ startLen; ; len++)\r
- {\r
- UInt32 price = normalMatchPrice + GET_PRICE_LEN(&p->lenEnc, posState, len);\r
- {\r
- COptimal *opt;\r
- unsigned lenNorm = len - 2;\r
- lenNorm = GetLenToPosState2(lenNorm);\r
- if (dist < kNumFullDistances)\r
- price += p->distancesPrices[lenNorm][dist & (kNumFullDistances - 1)];\r
- else\r
- price += p->posSlotPrices[lenNorm][posSlot] + p->alignPrices[dist & kAlignMask];\r
- \r
- opt = &p->opt[cur + len];\r
- if (price < opt->price)\r
- {\r
- opt->price = price;\r
- opt->len = (UInt32)len;\r
- opt->dist = dist + LZMA_NUM_REPS;\r
- opt->extra = 0;\r
- }\r
- }\r
-\r
- if (len == MATCHES[offs])\r
- {\r
- // if (p->_maxMode) {\r
- // MATCH : LIT : REP_0\r
-\r
- const Byte *data2 = data - dist - 1;\r
- unsigned len2 = len + 1;\r
- unsigned limit = len2 + p->numFastBytes;\r
- if (limit > numAvailFull)\r
- limit = numAvailFull;\r
- \r
- len2 += 2;\r
- if (len2 <= limit)\r
- if (data[len2 - 2] == data2[len2 - 2])\r
- if (data[len2 - 1] == data2[len2 - 1])\r
- {\r
- for (; len2 < limit && data[len2] == data2[len2]; len2++)\r
- {}\r
- \r
- len2 -= len;\r
- \r
- // if (len2 >= 3)\r
- {\r
- unsigned state2 = kMatchNextStates[state];\r
- unsigned posState2 = (position + len) & p->pbMask;\r
- unsigned offset;\r
- price += GET_PRICE_0(p->isMatch[state2][posState2]);\r
- price += LitEnc_Matched_GetPrice(LIT_PROBS(position + len, data[(size_t)len - 1]),\r
- data[len], data2[len], p->ProbPrices);\r
-\r
- // state2 = kLiteralNextStates[state2];\r
- state2 = kState_LitAfterMatch;\r
-\r
- posState2 = (posState2 + 1) & p->pbMask;\r
- price += GetPrice_Rep_0(p, state2, posState2);\r
-\r
- offset = cur + len + len2;\r
-\r
- if (last < offset)\r
- last = offset;\r
- // do\r
- {\r
- UInt32 price2;\r
- COptimal *opt;\r
- len2--;\r
- // price2 = price + GetPrice_Len_Rep_0(p, len2, state2, posState2);\r
- price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState2, len2);\r
- opt = &p->opt[offset];\r
- // offset--;\r
- if (price2 < opt->price)\r
- {\r
- opt->price = price2;\r
- opt->len = (UInt32)len2;\r
- opt->extra = (CExtra)(len + 1);\r
- opt->dist = dist + LZMA_NUM_REPS;\r
- }\r
- }\r
- // while (len2 >= 3);\r
- }\r
-\r
- }\r
- \r
- offs += 2;\r
- if (offs == numPairs)\r
- break;\r
- dist = MATCHES[(size_t)offs + 1];\r
- // if (dist >= kNumFullDistances)\r
- GetPosSlot2(dist, posSlot);\r
- }\r
- }\r
- }\r
- }\r
-\r
- do\r
- p->opt[last].price = kInfinityPrice;\r
- while (--last);\r
-\r
- return Backward(p, cur);\r
-}\r
-\r
-\r
-\r
-#define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))\r
-\r
-\r
-\r
-static unsigned GetOptimumFast(CLzmaEnc *p)\r
-{\r
- UInt32 numAvail, mainDist;\r
- unsigned mainLen, numPairs, repIndex, repLen, i;\r
- const Byte *data;\r
-\r
- if (p->additionalOffset == 0)\r
- mainLen = ReadMatchDistances(p, &numPairs);\r
- else\r
- {\r
- mainLen = p->longestMatchLen;\r
- numPairs = p->numPairs;\r
- }\r
-\r
- numAvail = p->numAvail;\r
- p->backRes = MARK_LIT;\r
- if (numAvail < 2)\r
- return 1;\r
- // if (mainLen < 2 && p->state == 0) return 1; // 18.06.notused\r
- if (numAvail > LZMA_MATCH_LEN_MAX)\r
- numAvail = LZMA_MATCH_LEN_MAX;\r
- data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;\r
- repLen = repIndex = 0;\r
- \r
- for (i = 0; i < LZMA_NUM_REPS; i++)\r
- {\r
- unsigned len;\r
- const Byte *data2 = data - p->reps[i];\r
- if (data[0] != data2[0] || data[1] != data2[1])\r
- continue;\r
- for (len = 2; len < numAvail && data[len] == data2[len]; len++)\r
- {}\r
- if (len >= p->numFastBytes)\r
- {\r
- p->backRes = (UInt32)i;\r
- MOVE_POS(p, len - 1)\r
- return len;\r
- }\r
- if (len > repLen)\r
- {\r
- repIndex = i;\r
- repLen = len;\r
- }\r
- }\r
-\r
- if (mainLen >= p->numFastBytes)\r
- {\r
- p->backRes = p->matches[(size_t)numPairs - 1] + LZMA_NUM_REPS;\r
- MOVE_POS(p, mainLen - 1)\r
- return mainLen;\r
- }\r
-\r
- mainDist = 0; /* for GCC */\r
- \r
- if (mainLen >= 2)\r
- {\r
- mainDist = p->matches[(size_t)numPairs - 1];\r
- while (numPairs > 2)\r
- {\r
- UInt32 dist2;\r
- if (mainLen != p->matches[(size_t)numPairs - 4] + 1)\r
- break;\r
- dist2 = p->matches[(size_t)numPairs - 3];\r
- if (!ChangePair(dist2, mainDist))\r
- break;\r
- numPairs -= 2;\r
- mainLen--;\r
- mainDist = dist2;\r
- }\r
- if (mainLen == 2 && mainDist >= 0x80)\r
- mainLen = 1;\r
- }\r
-\r
- if (repLen >= 2)\r
- if ( repLen + 1 >= mainLen\r
- || (repLen + 2 >= mainLen && mainDist >= (1 << 9))\r
- || (repLen + 3 >= mainLen && mainDist >= (1 << 15)))\r
- {\r
- p->backRes = (UInt32)repIndex;\r
- MOVE_POS(p, repLen - 1)\r
- return repLen;\r
- }\r
- \r
- if (mainLen < 2 || numAvail <= 2)\r
- return 1;\r
-\r
- {\r
- unsigned len1 = ReadMatchDistances(p, &p->numPairs);\r
- p->longestMatchLen = len1;\r
- \r
- if (len1 >= 2)\r
- {\r
- UInt32 newDist = p->matches[(size_t)p->numPairs - 1];\r
- if ( (len1 >= mainLen && newDist < mainDist)\r
- || (len1 == mainLen + 1 && !ChangePair(mainDist, newDist))\r
- || (len1 > mainLen + 1)\r
- || (len1 + 1 >= mainLen && mainLen >= 3 && ChangePair(newDist, mainDist)))\r
- return 1;\r
- }\r
- }\r
- \r
- data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;\r
- \r
- for (i = 0; i < LZMA_NUM_REPS; i++)\r
- {\r
- unsigned len, limit;\r
- const Byte *data2 = data - p->reps[i];\r
- if (data[0] != data2[0] || data[1] != data2[1])\r
- continue;\r
- limit = mainLen - 1;\r
- for (len = 2;; len++)\r
- {\r
- if (len >= limit)\r
- return 1;\r
- if (data[len] != data2[len])\r
- break;\r
- }\r
- }\r
- \r
- p->backRes = mainDist + LZMA_NUM_REPS;\r
- if (mainLen != 2)\r
- {\r
- MOVE_POS(p, mainLen - 2)\r
- }\r
- return mainLen;\r
-}\r
-\r
-\r
-\r
-\r
-static void WriteEndMarker(CLzmaEnc *p, unsigned posState)\r
-{\r
- UInt32 range;\r
- range = p->rc.range;\r
- {\r
- UInt32 ttt, newBound;\r
- CLzmaProb *prob = &p->isMatch[p->state][posState];\r
- RC_BIT_PRE(&p->rc, prob)\r
- RC_BIT_1(&p->rc, prob)\r
- prob = &p->isRep[p->state];\r
- RC_BIT_PRE(&p->rc, prob)\r
- RC_BIT_0(&p->rc, prob)\r
- }\r
- p->state = kMatchNextStates[p->state];\r
- \r
- p->rc.range = range;\r
- LenEnc_Encode(&p->lenProbs, &p->rc, 0, posState);\r
- range = p->rc.range;\r
-\r
- {\r
- // RcTree_Encode_PosSlot(&p->rc, p->posSlotEncoder[0], (1 << kNumPosSlotBits) - 1);\r
- CLzmaProb *probs = p->posSlotEncoder[0];\r
- unsigned m = 1;\r
- do\r
- {\r
- UInt32 ttt, newBound;\r
- RC_BIT_PRE(p, probs + m)\r
- RC_BIT_1(&p->rc, probs + m);\r
- m = (m << 1) + 1;\r
- }\r
- while (m < (1 << kNumPosSlotBits));\r
- }\r
- {\r
- // RangeEnc_EncodeDirectBits(&p->rc, ((UInt32)1 << (30 - kNumAlignBits)) - 1, 30 - kNumAlignBits); UInt32 range = p->range;\r
- unsigned numBits = 30 - kNumAlignBits;\r
- do\r
- {\r
- range >>= 1;\r
- p->rc.low += range;\r
- RC_NORM(&p->rc)\r
- }\r
- while (--numBits);\r
- }\r
- \r
- {\r
- // RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);\r
- CLzmaProb *probs = p->posAlignEncoder;\r
- unsigned m = 1;\r
- do\r
- {\r
- UInt32 ttt, newBound;\r
- RC_BIT_PRE(p, probs + m)\r
- RC_BIT_1(&p->rc, probs + m);\r
- m = (m << 1) + 1;\r
- }\r
- while (m < kAlignTableSize);\r
- }\r
- p->rc.range = range;\r
-}\r
-\r
-\r
-static SRes CheckErrors(CLzmaEnc *p)\r
-{\r
- if (p->result != SZ_OK)\r
- return p->result;\r
- if (p->rc.res != SZ_OK)\r
- p->result = SZ_ERROR_WRITE;\r
-\r
- #ifndef _7ZIP_ST\r
- if (\r
- // p->mf_Failure ||\r
- (p->mtMode &&\r
- ( // p->matchFinderMt.failure_LZ_LZ ||\r
- p->matchFinderMt.failure_LZ_BT))\r
- )\r
- {\r
- p->result = MY_HRES_ERROR__INTERNAL_ERROR;\r
- // printf("\nCheckErrors p->matchFinderMt.failureLZ\n");\r
- }\r
- #endif\r
-\r
- if (MFB.result != SZ_OK)\r
- p->result = SZ_ERROR_READ;\r
- \r
- if (p->result != SZ_OK)\r
- p->finished = True;\r
- return p->result;\r
-}\r
-\r
-\r
-MY_NO_INLINE static SRes Flush(CLzmaEnc *p, UInt32 nowPos)\r
-{\r
- /* ReleaseMFStream(); */\r
- p->finished = True;\r
- if (p->writeEndMark)\r
- WriteEndMarker(p, nowPos & p->pbMask);\r
- RangeEnc_FlushData(&p->rc);\r
- RangeEnc_FlushStream(&p->rc);\r
- return CheckErrors(p);\r
-}\r
-\r
-\r
-MY_NO_INLINE static void FillAlignPrices(CLzmaEnc *p)\r
-{\r
- unsigned i;\r
- const CProbPrice *ProbPrices = p->ProbPrices;\r
- const CLzmaProb *probs = p->posAlignEncoder;\r
- // p->alignPriceCount = 0;\r
- for (i = 0; i < kAlignTableSize / 2; i++)\r
- {\r
- UInt32 price = 0;\r
- unsigned sym = i;\r
- unsigned m = 1;\r
- unsigned bit;\r
- UInt32 prob;\r
- bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[m], bit); m = (m << 1) + bit;\r
- bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[m], bit); m = (m << 1) + bit;\r
- bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[m], bit); m = (m << 1) + bit;\r
- prob = probs[m];\r
- p->alignPrices[i ] = price + GET_PRICEa_0(prob);\r
- p->alignPrices[i + 8] = price + GET_PRICEa_1(prob);\r
- // p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);\r
- }\r
-}\r
-\r
-\r
-MY_NO_INLINE static void FillDistancesPrices(CLzmaEnc *p)\r
-{\r
- // int y; for (y = 0; y < 100; y++) {\r
-\r
- UInt32 tempPrices[kNumFullDistances];\r
- unsigned i, lps;\r
-\r
- const CProbPrice *ProbPrices = p->ProbPrices;\r
- p->matchPriceCount = 0;\r
-\r
- for (i = kStartPosModelIndex / 2; i < kNumFullDistances / 2; i++)\r
- {\r
- unsigned posSlot = GetPosSlot1(i);\r
- unsigned footerBits = (posSlot >> 1) - 1;\r
- unsigned base = ((2 | (posSlot & 1)) << footerBits);\r
- const CLzmaProb *probs = p->posEncoders + (size_t)base * 2;\r
- // tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base, footerBits, i - base, p->ProbPrices);\r
- UInt32 price = 0;\r
- unsigned m = 1;\r
- unsigned sym = i;\r
- unsigned offset = (unsigned)1 << footerBits;\r
- base += i;\r
- \r
- if (footerBits)\r
- do\r
- {\r
- unsigned bit = sym & 1;\r
- sym >>= 1;\r
- price += GET_PRICEa(probs[m], bit);\r
- m = (m << 1) + bit;\r
- }\r
- while (--footerBits);\r
-\r
- {\r
- unsigned prob = probs[m];\r
- tempPrices[base ] = price + GET_PRICEa_0(prob);\r
- tempPrices[base + offset] = price + GET_PRICEa_1(prob);\r
- }\r
- }\r
-\r
- for (lps = 0; lps < kNumLenToPosStates; lps++)\r
- {\r
- unsigned slot;\r
- unsigned distTableSize2 = (p->distTableSize + 1) >> 1;\r
- UInt32 *posSlotPrices = p->posSlotPrices[lps];\r
- const CLzmaProb *probs = p->posSlotEncoder[lps];\r
- \r
- for (slot = 0; slot < distTableSize2; slot++)\r
- {\r
- // posSlotPrices[slot] = RcTree_GetPrice(encoder, kNumPosSlotBits, slot, p->ProbPrices);\r
- UInt32 price;\r
- unsigned bit;\r
- unsigned sym = slot + (1 << (kNumPosSlotBits - 1));\r
- unsigned prob;\r
- bit = sym & 1; sym >>= 1; price = GET_PRICEa(probs[sym], bit);\r
- bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[sym], bit);\r
- bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[sym], bit);\r
- bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[sym], bit);\r
- bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[sym], bit);\r
- prob = probs[(size_t)slot + (1 << (kNumPosSlotBits - 1))];\r
- posSlotPrices[(size_t)slot * 2 ] = price + GET_PRICEa_0(prob);\r
- posSlotPrices[(size_t)slot * 2 + 1] = price + GET_PRICEa_1(prob);\r
- }\r
- \r
- {\r
- UInt32 delta = ((UInt32)((kEndPosModelIndex / 2 - 1) - kNumAlignBits) << kNumBitPriceShiftBits);\r
- for (slot = kEndPosModelIndex / 2; slot < distTableSize2; slot++)\r
- {\r
- posSlotPrices[(size_t)slot * 2 ] += delta;\r
- posSlotPrices[(size_t)slot * 2 + 1] += delta;\r
- delta += ((UInt32)1 << kNumBitPriceShiftBits);\r
- }\r
- }\r
-\r
- {\r
- UInt32 *dp = p->distancesPrices[lps];\r
- \r
- dp[0] = posSlotPrices[0];\r
- dp[1] = posSlotPrices[1];\r
- dp[2] = posSlotPrices[2];\r
- dp[3] = posSlotPrices[3];\r
-\r
- for (i = 4; i < kNumFullDistances; i += 2)\r
- {\r
- UInt32 slotPrice = posSlotPrices[GetPosSlot1(i)];\r
- dp[i ] = slotPrice + tempPrices[i];\r
- dp[i + 1] = slotPrice + tempPrices[i + 1];\r
- }\r
- }\r
- }\r
- // }\r
-}\r
-\r
-\r
-\r
-static void LzmaEnc_Construct(CLzmaEnc *p)\r
-{\r
- RangeEnc_Construct(&p->rc);\r
- MatchFinder_Construct(&MFB);\r
- \r
- #ifndef _7ZIP_ST\r
- p->matchFinderMt.MatchFinder = &MFB;\r
- MatchFinderMt_Construct(&p->matchFinderMt);\r
- #endif\r
-\r
- {\r
- CLzmaEncProps props;\r
- LzmaEncProps_Init(&props);\r
- LzmaEnc_SetProps(p, &props);\r
- }\r
-\r
- #ifndef LZMA_LOG_BSR\r
- LzmaEnc_FastPosInit(p->g_FastPos);\r
- #endif\r
-\r
- LzmaEnc_InitPriceTables(p->ProbPrices);\r
- p->litProbs = NULL;\r
- p->saveState.litProbs = NULL;\r
-}\r
-\r
-CLzmaEncHandle LzmaEnc_Create(ISzAllocPtr alloc)\r
-{\r
- void *p;\r
- p = ISzAlloc_Alloc(alloc, sizeof(CLzmaEnc));\r
- if (p)\r
- LzmaEnc_Construct((CLzmaEnc *)p);\r
- return p;\r
-}\r
-\r
-static void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAllocPtr alloc)\r
-{\r
- ISzAlloc_Free(alloc, p->litProbs);\r
- ISzAlloc_Free(alloc, p->saveState.litProbs);\r
- p->litProbs = NULL;\r
- p->saveState.litProbs = NULL;\r
-}\r
-\r
-static void LzmaEnc_Destruct(CLzmaEnc *p, ISzAllocPtr alloc, ISzAllocPtr allocBig)\r
-{\r
- #ifndef _7ZIP_ST\r
- MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);\r
- #endif\r
- \r
- MatchFinder_Free(&MFB, allocBig);\r
- LzmaEnc_FreeLits(p, alloc);\r
- RangeEnc_Free(&p->rc, alloc);\r
-}\r
-\r
-void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAllocPtr alloc, ISzAllocPtr allocBig)\r
-{\r
- LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig);\r
- ISzAlloc_Free(alloc, p);\r
-}\r
-\r
-\r
-MY_NO_INLINE\r
-static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, UInt32 maxPackSize, UInt32 maxUnpackSize)\r
-{\r
- UInt32 nowPos32, startPos32;\r
- if (p->needInit)\r
- {\r
- #ifndef _7ZIP_ST\r
- if (p->mtMode)\r
- {\r
- RINOK(MatchFinderMt_InitMt(&p->matchFinderMt));\r
- }\r
- #endif\r
- p->matchFinder.Init(p->matchFinderObj);\r
- p->needInit = 0;\r
- }\r
-\r
- if (p->finished)\r
- return p->result;\r
- RINOK(CheckErrors(p));\r
-\r
- nowPos32 = (UInt32)p->nowPos64;\r
- startPos32 = nowPos32;\r
-\r
- if (p->nowPos64 == 0)\r
- {\r
- unsigned numPairs;\r
- Byte curByte;\r
- if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)\r
- return Flush(p, nowPos32);\r
- ReadMatchDistances(p, &numPairs);\r
- RangeEnc_EncodeBit_0(&p->rc, &p->isMatch[kState_Start][0]);\r
- // p->state = kLiteralNextStates[p->state];\r
- curByte = *(p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset);\r
- LitEnc_Encode(&p->rc, p->litProbs, curByte);\r
- p->additionalOffset--;\r
- nowPos32++;\r
- }\r
-\r
- if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0)\r
- \r
- for (;;)\r
- {\r
- UInt32 dist;\r
- unsigned len, posState;\r
- UInt32 range, ttt, newBound;\r
- CLzmaProb *probs;\r
- \r
- if (p->fastMode)\r
- len = GetOptimumFast(p);\r
- else\r
- {\r
- unsigned oci = p->optCur;\r
- if (p->optEnd == oci)\r
- len = GetOptimum(p, nowPos32);\r
- else\r
- {\r
- const COptimal *opt = &p->opt[oci];\r
- len = opt->len;\r
- p->backRes = opt->dist;\r
- p->optCur = oci + 1;\r
- }\r
- }\r
-\r
- posState = (unsigned)nowPos32 & p->pbMask;\r
- range = p->rc.range;\r
- probs = &p->isMatch[p->state][posState];\r
- \r
- RC_BIT_PRE(&p->rc, probs)\r
- \r
- dist = p->backRes;\r
-\r
- #ifdef SHOW_STAT2\r
- printf("\n pos = %6X, len = %3u pos = %6u", nowPos32, len, dist);\r
- #endif\r
-\r
- if (dist == MARK_LIT)\r
- {\r
- Byte curByte;\r
- const Byte *data;\r
- unsigned state;\r
-\r
- RC_BIT_0(&p->rc, probs);\r
- p->rc.range = range;\r
- data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;\r
- probs = LIT_PROBS(nowPos32, *(data - 1));\r
- curByte = *data;\r
- state = p->state;\r
- p->state = kLiteralNextStates[state];\r
- if (IsLitState(state))\r
- LitEnc_Encode(&p->rc, probs, curByte);\r
- else\r
- LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0]));\r
- }\r
- else\r
- {\r
- RC_BIT_1(&p->rc, probs);\r
- probs = &p->isRep[p->state];\r
- RC_BIT_PRE(&p->rc, probs)\r
- \r
- if (dist < LZMA_NUM_REPS)\r
- {\r
- RC_BIT_1(&p->rc, probs);\r
- probs = &p->isRepG0[p->state];\r
- RC_BIT_PRE(&p->rc, probs)\r
- if (dist == 0)\r
- {\r
- RC_BIT_0(&p->rc, probs);\r
- probs = &p->isRep0Long[p->state][posState];\r
- RC_BIT_PRE(&p->rc, probs)\r
- if (len != 1)\r
- {\r
- RC_BIT_1_BASE(&p->rc, probs);\r
- }\r
- else\r
- {\r
- RC_BIT_0_BASE(&p->rc, probs);\r
- p->state = kShortRepNextStates[p->state];\r
- }\r
- }\r
- else\r
- {\r
- RC_BIT_1(&p->rc, probs);\r
- probs = &p->isRepG1[p->state];\r
- RC_BIT_PRE(&p->rc, probs)\r
- if (dist == 1)\r
- {\r
- RC_BIT_0_BASE(&p->rc, probs);\r
- dist = p->reps[1];\r
- }\r
- else\r
- {\r
- RC_BIT_1(&p->rc, probs);\r
- probs = &p->isRepG2[p->state];\r
- RC_BIT_PRE(&p->rc, probs)\r
- if (dist == 2)\r
- {\r
- RC_BIT_0_BASE(&p->rc, probs);\r
- dist = p->reps[2];\r
- }\r
- else\r
- {\r
- RC_BIT_1_BASE(&p->rc, probs);\r
- dist = p->reps[3];\r
- p->reps[3] = p->reps[2];\r
- }\r
- p->reps[2] = p->reps[1];\r
- }\r
- p->reps[1] = p->reps[0];\r
- p->reps[0] = dist;\r
- }\r
-\r
- RC_NORM(&p->rc)\r
-\r
- p->rc.range = range;\r
-\r
- if (len != 1)\r
- {\r
- LenEnc_Encode(&p->repLenProbs, &p->rc, len - LZMA_MATCH_LEN_MIN, posState);\r
- --p->repLenEncCounter;\r
- p->state = kRepNextStates[p->state];\r
- }\r
- }\r
- else\r
- {\r
- unsigned posSlot;\r
- RC_BIT_0(&p->rc, probs);\r
- p->rc.range = range;\r
- p->state = kMatchNextStates[p->state];\r
-\r
- LenEnc_Encode(&p->lenProbs, &p->rc, len - LZMA_MATCH_LEN_MIN, posState);\r
- // --p->lenEnc.counter;\r
-\r
- dist -= LZMA_NUM_REPS;\r
- p->reps[3] = p->reps[2];\r
- p->reps[2] = p->reps[1];\r
- p->reps[1] = p->reps[0];\r
- p->reps[0] = dist + 1;\r
- \r
- p->matchPriceCount++;\r
- GetPosSlot(dist, posSlot);\r
- // RcTree_Encode_PosSlot(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], posSlot);\r
- {\r
- UInt32 sym = (UInt32)posSlot + (1 << kNumPosSlotBits);\r
- range = p->rc.range;\r
- probs = p->posSlotEncoder[GetLenToPosState(len)];\r
- do\r
- {\r
- CLzmaProb *prob = probs + (sym >> kNumPosSlotBits);\r
- UInt32 bit = (sym >> (kNumPosSlotBits - 1)) & 1;\r
- sym <<= 1;\r
- RC_BIT(&p->rc, prob, bit);\r
- }\r
- while (sym < (1 << kNumPosSlotBits * 2));\r
- p->rc.range = range;\r
- }\r
- \r
- if (dist >= kStartPosModelIndex)\r
- {\r
- unsigned footerBits = ((posSlot >> 1) - 1);\r
-\r
- if (dist < kNumFullDistances)\r
- {\r
- unsigned base = ((2 | (posSlot & 1)) << footerBits);\r
- RcTree_ReverseEncode(&p->rc, p->posEncoders + base, footerBits, (unsigned)(dist /* - base */));\r
- }\r
- else\r
- {\r
- UInt32 pos2 = (dist | 0xF) << (32 - footerBits);\r
- range = p->rc.range;\r
- // RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);\r
- /*\r
- do\r
- {\r
- range >>= 1;\r
- p->rc.low += range & (0 - ((dist >> --footerBits) & 1));\r
- RC_NORM(&p->rc)\r
- }\r
- while (footerBits > kNumAlignBits);\r
- */\r
- do\r
- {\r
- range >>= 1;\r
- p->rc.low += range & (0 - (pos2 >> 31));\r
- pos2 += pos2;\r
- RC_NORM(&p->rc)\r
- }\r
- while (pos2 != 0xF0000000);\r
-\r
-\r
- // RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);\r
-\r
- {\r
- unsigned m = 1;\r
- unsigned bit;\r
- bit = dist & 1; dist >>= 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit); m = (m << 1) + bit;\r
- bit = dist & 1; dist >>= 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit); m = (m << 1) + bit;\r
- bit = dist & 1; dist >>= 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit); m = (m << 1) + bit;\r
- bit = dist & 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit);\r
- p->rc.range = range;\r
- // p->alignPriceCount++;\r
- }\r
- }\r
- }\r
- }\r
- }\r
-\r
- nowPos32 += (UInt32)len;\r
- p->additionalOffset -= len;\r
- \r
- if (p->additionalOffset == 0)\r
- {\r
- UInt32 processed;\r
-\r
- if (!p->fastMode)\r
- {\r
- /*\r
- if (p->alignPriceCount >= 16) // kAlignTableSize\r
- FillAlignPrices(p);\r
- if (p->matchPriceCount >= 128)\r
- FillDistancesPrices(p);\r
- if (p->lenEnc.counter <= 0)\r
- LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, &p->lenProbs, p->ProbPrices);\r
- */\r
- if (p->matchPriceCount >= 64)\r
- {\r
- FillAlignPrices(p);\r
- // { int y; for (y = 0; y < 100; y++) {\r
- FillDistancesPrices(p);\r
- // }}\r
- LenPriceEnc_UpdateTables(&p->lenEnc, (unsigned)1 << p->pb, &p->lenProbs, p->ProbPrices);\r
- }\r
- if (p->repLenEncCounter <= 0)\r
- {\r
- p->repLenEncCounter = REP_LEN_COUNT;\r
- LenPriceEnc_UpdateTables(&p->repLenEnc, (unsigned)1 << p->pb, &p->repLenProbs, p->ProbPrices);\r
- }\r
- }\r
- \r
- if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)\r
- break;\r
- processed = nowPos32 - startPos32;\r
- \r
- if (maxPackSize)\r
- {\r
- if (processed + kNumOpts + 300 >= maxUnpackSize\r
- || RangeEnc_GetProcessed_sizet(&p->rc) + kPackReserve >= maxPackSize)\r
- break;\r
- }\r
- else if (processed >= (1 << 17))\r
- {\r
- p->nowPos64 += nowPos32 - startPos32;\r
- return CheckErrors(p);\r
- }\r
- }\r
- }\r
-\r
- p->nowPos64 += nowPos32 - startPos32;\r
- return Flush(p, nowPos32);\r
-}\r
-\r
-\r
-\r
-#define kBigHashDicLimit ((UInt32)1 << 24)\r
-\r
-static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAllocPtr alloc, ISzAllocPtr allocBig)\r
-{\r
- UInt32 beforeSize = kNumOpts;\r
- UInt32 dictSize;\r
-\r
- if (!RangeEnc_Alloc(&p->rc, alloc))\r
- return SZ_ERROR_MEM;\r
-\r
- #ifndef _7ZIP_ST\r
- p->mtMode = (p->multiThread && !p->fastMode && (MFB.btMode != 0));\r
- #endif\r
-\r
- {\r
- unsigned lclp = p->lc + p->lp;\r
- if (!p->litProbs || !p->saveState.litProbs || p->lclp != lclp)\r
- {\r
- LzmaEnc_FreeLits(p, alloc);\r
- p->litProbs = (CLzmaProb *)ISzAlloc_Alloc(alloc, ((UInt32)0x300 << lclp) * sizeof(CLzmaProb));\r
- p->saveState.litProbs = (CLzmaProb *)ISzAlloc_Alloc(alloc, ((UInt32)0x300 << lclp) * sizeof(CLzmaProb));\r
- if (!p->litProbs || !p->saveState.litProbs)\r
- {\r
- LzmaEnc_FreeLits(p, alloc);\r
- return SZ_ERROR_MEM;\r
- }\r
- p->lclp = lclp;\r
- }\r
- }\r
-\r
- MFB.bigHash = (Byte)(p->dictSize > kBigHashDicLimit ? 1 : 0);\r
-\r
-\r
- dictSize = p->dictSize;\r
- if (dictSize == ((UInt32)2 << 30) ||\r
- dictSize == ((UInt32)3 << 30))\r
- {\r
- /* 21.03 : here we reduce the dictionary for 2 reasons:\r
- 1) we don't want 32-bit back_distance matches in decoder for 2 GB dictionary.\r
- 2) we want to elimate useless last MatchFinder_Normalize3() for corner cases,\r
- where data size is aligned for 1 GB: 5/6/8 GB.\r
- That reducing must be >= 1 for such corner cases. */\r
- dictSize -= 1;\r
- }\r
-\r
- if (beforeSize + dictSize < keepWindowSize)\r
- beforeSize = keepWindowSize - dictSize;\r
-\r
- /* in worst case we can look ahead for\r
- max(LZMA_MATCH_LEN_MAX, numFastBytes + 1 + numFastBytes) bytes.\r
- we send larger value for (keepAfter) to MantchFinder_Create():\r
- (numFastBytes + LZMA_MATCH_LEN_MAX + 1)\r
- */\r
-\r
- #ifndef _7ZIP_ST\r
- if (p->mtMode)\r
- {\r
- RINOK(MatchFinderMt_Create(&p->matchFinderMt, dictSize, beforeSize,\r
- p->numFastBytes, LZMA_MATCH_LEN_MAX + 1 /* 18.04 */\r
- , allocBig));\r
- p->matchFinderObj = &p->matchFinderMt;\r
- MFB.bigHash = (Byte)(\r
- (p->dictSize > kBigHashDicLimit && MFB.hashMask >= 0xFFFFFF) ? 1 : 0);\r
- MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder);\r
- }\r
- else\r
- #endif\r
- {\r
- if (!MatchFinder_Create(&MFB, dictSize, beforeSize,\r
- p->numFastBytes, LZMA_MATCH_LEN_MAX + 1 /* 21.03 */\r
- , allocBig))\r
- return SZ_ERROR_MEM;\r
- p->matchFinderObj = &MFB;\r
- MatchFinder_CreateVTable(&MFB, &p->matchFinder);\r
- }\r
- \r
- return SZ_OK;\r
-}\r
-\r
-static void LzmaEnc_Init(CLzmaEnc *p)\r
-{\r
- unsigned i;\r
- p->state = 0;\r
- p->reps[0] =\r
- p->reps[1] =\r
- p->reps[2] =\r
- p->reps[3] = 1;\r
-\r
- RangeEnc_Init(&p->rc);\r
-\r
- for (i = 0; i < (1 << kNumAlignBits); i++)\r
- p->posAlignEncoder[i] = kProbInitValue;\r
-\r
- for (i = 0; i < kNumStates; i++)\r
- {\r
- unsigned j;\r
- for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++)\r
- {\r
- p->isMatch[i][j] = kProbInitValue;\r
- p->isRep0Long[i][j] = kProbInitValue;\r
- }\r
- p->isRep[i] = kProbInitValue;\r
- p->isRepG0[i] = kProbInitValue;\r
- p->isRepG1[i] = kProbInitValue;\r
- p->isRepG2[i] = kProbInitValue;\r
- }\r
-\r
- {\r
- for (i = 0; i < kNumLenToPosStates; i++)\r
- {\r
- CLzmaProb *probs = p->posSlotEncoder[i];\r
- unsigned j;\r
- for (j = 0; j < (1 << kNumPosSlotBits); j++)\r
- probs[j] = kProbInitValue;\r
- }\r
- }\r
- {\r
- for (i = 0; i < kNumFullDistances; i++)\r
- p->posEncoders[i] = kProbInitValue;\r
- }\r
-\r
- {\r
- UInt32 num = (UInt32)0x300 << (p->lp + p->lc);\r
- UInt32 k;\r
- CLzmaProb *probs = p->litProbs;\r
- for (k = 0; k < num; k++)\r
- probs[k] = kProbInitValue;\r
- }\r
-\r
-\r
- LenEnc_Init(&p->lenProbs);\r
- LenEnc_Init(&p->repLenProbs);\r
-\r
- p->optEnd = 0;\r
- p->optCur = 0;\r
-\r
- {\r
- for (i = 0; i < kNumOpts; i++)\r
- p->opt[i].price = kInfinityPrice;\r
- }\r
-\r
- p->additionalOffset = 0;\r
-\r
- p->pbMask = ((unsigned)1 << p->pb) - 1;\r
- p->lpMask = ((UInt32)0x100 << p->lp) - ((unsigned)0x100 >> p->lc);\r
-\r
- // p->mf_Failure = False;\r
-}\r
-\r
-\r
-static void LzmaEnc_InitPrices(CLzmaEnc *p)\r
-{\r
- if (!p->fastMode)\r
- {\r
- FillDistancesPrices(p);\r
- FillAlignPrices(p);\r
- }\r
-\r
- p->lenEnc.tableSize =\r
- p->repLenEnc.tableSize =\r
- p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN;\r
-\r
- p->repLenEncCounter = REP_LEN_COUNT;\r
-\r
- LenPriceEnc_UpdateTables(&p->lenEnc, (unsigned)1 << p->pb, &p->lenProbs, p->ProbPrices);\r
- LenPriceEnc_UpdateTables(&p->repLenEnc, (unsigned)1 << p->pb, &p->repLenProbs, p->ProbPrices);\r
-}\r
-\r
-static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAllocPtr alloc, ISzAllocPtr allocBig)\r
-{\r
- unsigned i;\r
- for (i = kEndPosModelIndex / 2; i < kDicLogSizeMax; i++)\r
- if (p->dictSize <= ((UInt32)1 << i))\r
- break;\r
- p->distTableSize = i * 2;\r
-\r
- p->finished = False;\r
- p->result = SZ_OK;\r
- RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig));\r
- LzmaEnc_Init(p);\r
- LzmaEnc_InitPrices(p);\r
- p->nowPos64 = 0;\r
- return SZ_OK;\r
-}\r
-\r
-static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream,\r
- ISzAllocPtr alloc, ISzAllocPtr allocBig)\r
-{\r
- CLzmaEnc *p = (CLzmaEnc *)pp;\r
- MFB.stream = inStream;\r
- p->needInit = 1;\r
- p->rc.outStream = outStream;\r
- return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig);\r
-}\r
-\r
-SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp,\r
- ISeqInStream *inStream, UInt32 keepWindowSize,\r
- ISzAllocPtr alloc, ISzAllocPtr allocBig)\r
-{\r
- CLzmaEnc *p = (CLzmaEnc *)pp;\r
- MFB.stream = inStream;\r
- p->needInit = 1;\r
- return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);\r
-}\r
-\r
-static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen)\r
-{\r
- MFB.directInput = 1;\r
- MFB.bufferBase = (Byte *)src;\r
- MFB.directInputRem = srcLen;\r
-}\r
-\r
-SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen,\r
- UInt32 keepWindowSize, ISzAllocPtr alloc, ISzAllocPtr allocBig)\r
-{\r
- CLzmaEnc *p = (CLzmaEnc *)pp;\r
- LzmaEnc_SetInputBuf(p, src, srcLen);\r
- p->needInit = 1;\r
-\r
- LzmaEnc_SetDataSize(pp, srcLen);\r
- return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);\r
-}\r
-\r
-void LzmaEnc_Finish(CLzmaEncHandle pp)\r
-{\r
- #ifndef _7ZIP_ST\r
- CLzmaEnc *p = (CLzmaEnc *)pp;\r
- if (p->mtMode)\r
- MatchFinderMt_ReleaseStream(&p->matchFinderMt);\r
- #else\r
- UNUSED_VAR(pp);\r
- #endif\r
-}\r
-\r
-\r
-typedef struct\r
-{\r
- ISeqOutStream vt;\r
- Byte *data;\r
- SizeT rem;\r
- BoolInt overflow;\r
-} CLzmaEnc_SeqOutStreamBuf;\r
-\r
-static size_t SeqOutStreamBuf_Write(const ISeqOutStream *pp, const void *data, size_t size)\r
-{\r
- CLzmaEnc_SeqOutStreamBuf *p = CONTAINER_FROM_VTBL(pp, CLzmaEnc_SeqOutStreamBuf, vt);\r
- if (p->rem < size)\r
- {\r
- size = p->rem;\r
- p->overflow = True;\r
- }\r
- if (size != 0)\r
- {\r
- memcpy(p->data, data, size);\r
- p->rem -= size;\r
- p->data += size;\r
- }\r
- return size;\r
-}\r
-\r
-\r
-/*\r
-UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp)\r
-{\r
- const CLzmaEnc *p = (CLzmaEnc *)pp;\r
- return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);\r
-}\r
-*/\r
-\r
-const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp)\r
-{\r
- const CLzmaEnc *p = (CLzmaEnc *)pp;\r
- return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;\r
-}\r
-\r
-\r
-// (desiredPackSize == 0) is not allowed\r
-SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, BoolInt reInit,\r
- Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize)\r
-{\r
- CLzmaEnc *p = (CLzmaEnc *)pp;\r
- UInt64 nowPos64;\r
- SRes res;\r
- CLzmaEnc_SeqOutStreamBuf outStream;\r
-\r
- outStream.vt.Write = SeqOutStreamBuf_Write;\r
- outStream.data = dest;\r
- outStream.rem = *destLen;\r
- outStream.overflow = False;\r
-\r
- p->writeEndMark = False;\r
- p->finished = False;\r
- p->result = SZ_OK;\r
-\r
- if (reInit)\r
- LzmaEnc_Init(p);\r
- LzmaEnc_InitPrices(p);\r
- RangeEnc_Init(&p->rc);\r
- p->rc.outStream = &outStream.vt;\r
- nowPos64 = p->nowPos64;\r
- \r
- res = LzmaEnc_CodeOneBlock(p, desiredPackSize, *unpackSize);\r
- \r
- *unpackSize = (UInt32)(p->nowPos64 - nowPos64);\r
- *destLen -= outStream.rem;\r
- if (outStream.overflow)\r
- return SZ_ERROR_OUTPUT_EOF;\r
-\r
- return res;\r
-}\r
-\r
-\r
-MY_NO_INLINE\r
-static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress)\r
-{\r
- SRes res = SZ_OK;\r
-\r
- #ifndef _7ZIP_ST\r
- Byte allocaDummy[0x300];\r
- allocaDummy[0] = 0;\r
- allocaDummy[1] = allocaDummy[0];\r
- #endif\r
-\r
- for (;;)\r
- {\r
- res = LzmaEnc_CodeOneBlock(p, 0, 0);\r
- if (res != SZ_OK || p->finished)\r
- break;\r
- if (progress)\r
- {\r
- res = ICompressProgress_Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));\r
- if (res != SZ_OK)\r
- {\r
- res = SZ_ERROR_PROGRESS;\r
- break;\r
- }\r
- }\r
- }\r
- \r
- LzmaEnc_Finish(p);\r
-\r
- /*\r
- if (res == SZ_OK && !Inline_MatchFinder_IsFinishedOK(&MFB))\r
- res = SZ_ERROR_FAIL;\r
- }\r
- */\r
-\r
- return res;\r
-}\r
-\r
-\r
-SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress,\r
- ISzAllocPtr alloc, ISzAllocPtr allocBig)\r
-{\r
- RINOK(LzmaEnc_Prepare(pp, outStream, inStream, alloc, allocBig));\r
- return LzmaEnc_Encode2((CLzmaEnc *)pp, progress);\r
-}\r
-\r
-\r
-SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size)\r
-{\r
- if (*size < LZMA_PROPS_SIZE)\r
- return SZ_ERROR_PARAM;\r
- *size = LZMA_PROPS_SIZE;\r
- {\r
- const CLzmaEnc *p = (const CLzmaEnc *)pp;\r
- const UInt32 dictSize = p->dictSize;\r
- UInt32 v;\r
- props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc);\r
- \r
- // we write aligned dictionary value to properties for lzma decoder\r
- if (dictSize >= ((UInt32)1 << 21))\r
- {\r
- const UInt32 kDictMask = ((UInt32)1 << 20) - 1;\r
- v = (dictSize + kDictMask) & ~kDictMask;\r
- if (v < dictSize)\r
- v = dictSize;\r
- }\r
- else\r
- {\r
- unsigned i = 11 * 2;\r
- do\r
- {\r
- v = (UInt32)(2 + (i & 1)) << (i >> 1);\r
- i++;\r
- }\r
- while (v < dictSize);\r
- }\r
-\r
- SetUi32(props + 1, v);\r
- return SZ_OK;\r
- }\r
-}\r
-\r
-\r
-unsigned LzmaEnc_IsWriteEndMark(CLzmaEncHandle pp)\r
-{\r
- return (unsigned)((CLzmaEnc *)pp)->writeEndMark;\r
-}\r
-\r
-\r
-SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,\r
- int writeEndMark, ICompressProgress *progress, ISzAllocPtr alloc, ISzAllocPtr allocBig)\r
-{\r
- SRes res;\r
- CLzmaEnc *p = (CLzmaEnc *)pp;\r
-\r
- CLzmaEnc_SeqOutStreamBuf outStream;\r
-\r
- outStream.vt.Write = SeqOutStreamBuf_Write;\r
- outStream.data = dest;\r
- outStream.rem = *destLen;\r
- outStream.overflow = False;\r
-\r
- p->writeEndMark = writeEndMark;\r
- p->rc.outStream = &outStream.vt;\r
-\r
- res = LzmaEnc_MemPrepare(pp, src, srcLen, 0, alloc, allocBig);\r
- \r
- if (res == SZ_OK)\r
- {\r
- res = LzmaEnc_Encode2(p, progress);\r
- if (res == SZ_OK && p->nowPos64 != srcLen)\r
- res = SZ_ERROR_FAIL;\r
- }\r
-\r
- *destLen -= outStream.rem;\r
- if (outStream.overflow)\r
- return SZ_ERROR_OUTPUT_EOF;\r
- return res;\r
-}\r
-\r
-\r
-SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,\r
- const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,\r
- ICompressProgress *progress, ISzAllocPtr alloc, ISzAllocPtr allocBig)\r
-{\r
- CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc);\r
- SRes res;\r
- if (!p)\r
- return SZ_ERROR_MEM;\r
-\r
- res = LzmaEnc_SetProps(p, props);\r
- if (res == SZ_OK)\r
- {\r
- res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize);\r
- if (res == SZ_OK)\r
- res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen,\r
- writeEndMark, progress, alloc, allocBig);\r
- }\r
-\r
- LzmaEnc_Destroy(p, alloc, allocBig);\r
- return res;\r
-}\r
-\r
-\r
-/*\r
-#ifndef _7ZIP_ST\r
-void LzmaEnc_GetLzThreads(CLzmaEncHandle pp, HANDLE lz_threads[2])\r
-{\r
- const CLzmaEnc *p = (CLzmaEnc *)pp;\r
- lz_threads[0] = p->matchFinderMt.hashSync.thread;\r
- lz_threads[1] = p->matchFinderMt.btSync.thread;\r
-}\r
-#endif\r
-*/\r
+/* LzmaEnc.c -- LZMA Encoder
+2024-01-24: Igor Pavlov : Public domain */
+
+#include "Precomp.h"
+
+#include <string.h>
+
+/* #define SHOW_STAT */
+/* #define SHOW_STAT2 */
+
+#if defined(SHOW_STAT) || defined(SHOW_STAT2)
+#include <stdio.h>
+#endif
+
+#include "CpuArch.h"
+#include "LzmaEnc.h"
+
+#include "LzFind.h"
+#ifndef Z7_ST
+#include "LzFindMt.h"
+#endif
+
+/* the following LzmaEnc_* declarations is internal LZMA interface for LZMA2 encoder */
+
+SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle p, ISeqInStreamPtr inStream, UInt32 keepWindowSize,
+ ISzAllocPtr alloc, ISzAllocPtr allocBig);
+SRes LzmaEnc_MemPrepare(CLzmaEncHandle p, const Byte *src, SizeT srcLen,
+ UInt32 keepWindowSize, ISzAllocPtr alloc, ISzAllocPtr allocBig);
+SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle p, BoolInt reInit,
+ Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize);
+const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle p);
+void LzmaEnc_Finish(CLzmaEncHandle p);
+void LzmaEnc_SaveState(CLzmaEncHandle p);
+void LzmaEnc_RestoreState(CLzmaEncHandle p);
+
+#ifdef SHOW_STAT
+static unsigned g_STAT_OFFSET = 0;
+#endif
+
+/* for good normalization speed we still reserve 256 MB before 4 GB range */
+#define kLzmaMaxHistorySize ((UInt32)15 << 28)
+
+// #define kNumTopBits 24
+#define kTopValue ((UInt32)1 << 24)
+
+#define kNumBitModelTotalBits 11
+#define kBitModelTotal (1 << kNumBitModelTotalBits)
+#define kNumMoveBits 5
+#define kProbInitValue (kBitModelTotal >> 1)
+
+#define kNumMoveReducingBits 4
+#define kNumBitPriceShiftBits 4
+// #define kBitPrice (1 << kNumBitPriceShiftBits)
+
+#define REP_LEN_COUNT 64
+
+void LzmaEncProps_Init(CLzmaEncProps *p)
+{
+ p->level = 5;
+ p->dictSize = p->mc = 0;
+ p->reduceSize = (UInt64)(Int64)-1;
+ p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;
+ p->numHashOutBits = 0;
+ p->writeEndMark = 0;
+ p->affinity = 0;
+}
+
+void LzmaEncProps_Normalize(CLzmaEncProps *p)
+{
+ int level = p->level;
+ if (level < 0) level = 5;
+ p->level = level;
+
+ if (p->dictSize == 0)
+ p->dictSize =
+ ( level <= 3 ? ((UInt32)1 << (level * 2 + 16)) :
+ ( level <= 6 ? ((UInt32)1 << (level + 19)) :
+ ( level <= 7 ? ((UInt32)1 << 25) : ((UInt32)1 << 26)
+ )));
+
+ if (p->dictSize > p->reduceSize)
+ {
+ UInt32 v = (UInt32)p->reduceSize;
+ const UInt32 kReduceMin = ((UInt32)1 << 12);
+ if (v < kReduceMin)
+ v = kReduceMin;
+ if (p->dictSize > v)
+ p->dictSize = v;
+ }
+
+ if (p->lc < 0) p->lc = 3;
+ if (p->lp < 0) p->lp = 0;
+ if (p->pb < 0) p->pb = 2;
+
+ if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);
+ if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);
+ if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);
+ if (p->numHashBytes < 0) p->numHashBytes = (p->btMode ? 4 : 5);
+ if (p->mc == 0) p->mc = (16 + ((unsigned)p->fb >> 1)) >> (p->btMode ? 0 : 1);
+
+ if (p->numThreads < 0)
+ p->numThreads =
+ #ifndef Z7_ST
+ ((p->btMode && p->algo) ? 2 : 1);
+ #else
+ 1;
+ #endif
+}
+
+UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2)
+{
+ CLzmaEncProps props = *props2;
+ LzmaEncProps_Normalize(&props);
+ return props.dictSize;
+}
+
+
+/*
+x86/x64:
+
+BSR:
+ IF (SRC == 0) ZF = 1, DEST is undefined;
+ AMD : DEST is unchanged;
+ IF (SRC != 0) ZF = 0; DEST is index of top non-zero bit
+ BSR is slow in some processors
+
+LZCNT:
+ IF (SRC == 0) CF = 1, DEST is size_in_bits_of_register(src) (32 or 64)
+ IF (SRC != 0) CF = 0, DEST = num_lead_zero_bits
+ IF (DEST == 0) ZF = 1;
+
+LZCNT works only in new processors starting from Haswell.
+if LZCNT is not supported by processor, then it's executed as BSR.
+LZCNT can be faster than BSR, if supported.
+*/
+
+// #define LZMA_LOG_BSR
+
+#if defined(MY_CPU_ARM_OR_ARM64) /* || defined(MY_CPU_X86_OR_AMD64) */
+
+ #if (defined(__clang__) && (__clang_major__ >= 6)) \
+ || (defined(__GNUC__) && (__GNUC__ >= 6))
+ #define LZMA_LOG_BSR
+ #elif defined(_MSC_VER) && (_MSC_VER >= 1300)
+ // #if defined(MY_CPU_ARM_OR_ARM64)
+ #define LZMA_LOG_BSR
+ // #endif
+ #endif
+#endif
+
+// #include <intrin.h>
+
+#ifdef LZMA_LOG_BSR
+
+#if defined(__clang__) \
+ || defined(__GNUC__)
+
+/*
+ C code: : (30 - __builtin_clz(x))
+ gcc9/gcc10 for x64 /x86 : 30 - (bsr(x) xor 31)
+ clang10 for x64 : 31 + (bsr(x) xor -32)
+*/
+
+ #define MY_clz(x) ((unsigned)__builtin_clz(x))
+ // __lzcnt32
+ // __builtin_ia32_lzcnt_u32
+
+#else // #if defined(_MSC_VER)
+
+ #ifdef MY_CPU_ARM_OR_ARM64
+
+ #define MY_clz _CountLeadingZeros
+
+ #else // if defined(MY_CPU_X86_OR_AMD64)
+
+ // #define MY_clz __lzcnt // we can use lzcnt (unsupported by old CPU)
+ // _BitScanReverse code is not optimal for some MSVC compilers
+ #define BSR2_RET(pos, res) { unsigned long zz; _BitScanReverse(&zz, (pos)); zz--; \
+ res = (zz + zz) + (pos >> zz); }
+
+ #endif // MY_CPU_X86_OR_AMD64
+
+#endif // _MSC_VER
+
+
+#ifndef BSR2_RET
+
+ #define BSR2_RET(pos, res) { unsigned zz = 30 - MY_clz(pos); \
+ res = (zz + zz) + (pos >> zz); }
+
+#endif
+
+
+unsigned GetPosSlot1(UInt32 pos);
+unsigned GetPosSlot1(UInt32 pos)
+{
+ unsigned res;
+ BSR2_RET(pos, res)
+ return res;
+}
+#define GetPosSlot2(pos, res) { BSR2_RET(pos, res) }
+#define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res) }
+
+
+#else // ! LZMA_LOG_BSR
+
+#define kNumLogBits (11 + sizeof(size_t) / 8 * 3)
+
+#define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
+
+static void LzmaEnc_FastPosInit(Byte *g_FastPos)
+{
+ unsigned slot;
+ g_FastPos[0] = 0;
+ g_FastPos[1] = 1;
+ g_FastPos += 2;
+
+ for (slot = 2; slot < kNumLogBits * 2; slot++)
+ {
+ size_t k = ((size_t)1 << ((slot >> 1) - 1));
+ size_t j;
+ for (j = 0; j < k; j++)
+ g_FastPos[j] = (Byte)slot;
+ g_FastPos += k;
+ }
+}
+
+/* we can use ((limit - pos) >> 31) only if (pos < ((UInt32)1 << 31)) */
+/*
+#define BSR2_RET(pos, res) { unsigned zz = 6 + ((kNumLogBits - 1) & \
+ (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
+ res = p->g_FastPos[pos >> zz] + (zz * 2); }
+*/
+
+/*
+#define BSR2_RET(pos, res) { unsigned zz = 6 + ((kNumLogBits - 1) & \
+ (0 - (((((UInt32)1 << (kNumLogBits)) - 1) - (pos >> 6)) >> 31))); \
+ res = p->g_FastPos[pos >> zz] + (zz * 2); }
+*/
+
+#define BSR2_RET(pos, res) { unsigned zz = (pos < (1 << (kNumLogBits + 6))) ? 6 : 6 + kNumLogBits - 1; \
+ res = p->g_FastPos[pos >> zz] + (zz * 2); }
+
+/*
+#define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
+ p->g_FastPos[pos >> 6] + 12 : \
+ p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
+*/
+
+#define GetPosSlot1(pos) p->g_FastPos[pos]
+#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
+#define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos & (kNumFullDistances - 1)]; else BSR2_RET(pos, res); }
+
+#endif // LZMA_LOG_BSR
+
+
+#define LZMA_NUM_REPS 4
+
+typedef UInt16 CState;
+typedef UInt16 CExtra;
+
+typedef struct
+{
+ UInt32 price;
+ CState state;
+ CExtra extra;
+ // 0 : normal
+ // 1 : LIT : MATCH
+ // > 1 : MATCH (extra-1) : LIT : REP0 (len)
+ UInt32 len;
+ UInt32 dist;
+ UInt32 reps[LZMA_NUM_REPS];
+} COptimal;
+
+
+// 18.06
+#define kNumOpts (1 << 11)
+#define kPackReserve (kNumOpts * 8)
+// #define kNumOpts (1 << 12)
+// #define kPackReserve (1 + kNumOpts * 2)
+
+#define kNumLenToPosStates 4
+#define kNumPosSlotBits 6
+// #define kDicLogSizeMin 0
+#define kDicLogSizeMax 32
+#define kDistTableSizeMax (kDicLogSizeMax * 2)
+
+#define kNumAlignBits 4
+#define kAlignTableSize (1 << kNumAlignBits)
+#define kAlignMask (kAlignTableSize - 1)
+
+#define kStartPosModelIndex 4
+#define kEndPosModelIndex 14
+#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
+
+typedef
+#ifdef Z7_LZMA_PROB32
+ UInt32
+#else
+ UInt16
+#endif
+ CLzmaProb;
+
+#define LZMA_PB_MAX 4
+#define LZMA_LC_MAX 8
+#define LZMA_LP_MAX 4
+
+#define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
+
+#define kLenNumLowBits 3
+#define kLenNumLowSymbols (1 << kLenNumLowBits)
+#define kLenNumHighBits 8
+#define kLenNumHighSymbols (1 << kLenNumHighBits)
+#define kLenNumSymbolsTotal (kLenNumLowSymbols * 2 + kLenNumHighSymbols)
+
+#define LZMA_MATCH_LEN_MIN 2
+#define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
+
+#define kNumStates 12
+
+
+typedef struct
+{
+ CLzmaProb low[LZMA_NUM_PB_STATES_MAX << (kLenNumLowBits + 1)];
+ CLzmaProb high[kLenNumHighSymbols];
+} CLenEnc;
+
+
+typedef struct
+{
+ unsigned tableSize;
+ UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];
+ // UInt32 prices1[LZMA_NUM_PB_STATES_MAX][kLenNumLowSymbols * 2];
+ // UInt32 prices2[kLenNumSymbolsTotal];
+} CLenPriceEnc;
+
+#define GET_PRICE_LEN(p, posState, len) \
+ ((p)->prices[posState][(size_t)(len) - LZMA_MATCH_LEN_MIN])
+
+/*
+#define GET_PRICE_LEN(p, posState, len) \
+ ((p)->prices2[(size_t)(len) - 2] + ((p)->prices1[posState][((len) - 2) & (kLenNumLowSymbols * 2 - 1)] & (((len) - 2 - kLenNumLowSymbols * 2) >> 9)))
+*/
+
+typedef struct
+{
+ UInt32 range;
+ unsigned cache;
+ UInt64 low;
+ UInt64 cacheSize;
+ Byte *buf;
+ Byte *bufLim;
+ Byte *bufBase;
+ ISeqOutStreamPtr outStream;
+ UInt64 processed;
+ SRes res;
+} CRangeEnc;
+
+
+typedef struct
+{
+ CLzmaProb *litProbs;
+
+ unsigned state;
+ UInt32 reps[LZMA_NUM_REPS];
+
+ CLzmaProb posAlignEncoder[1 << kNumAlignBits];
+ CLzmaProb isRep[kNumStates];
+ CLzmaProb isRepG0[kNumStates];
+ CLzmaProb isRepG1[kNumStates];
+ CLzmaProb isRepG2[kNumStates];
+ CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
+ CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
+
+ CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
+ CLzmaProb posEncoders[kNumFullDistances];
+
+ CLenEnc lenProbs;
+ CLenEnc repLenProbs;
+
+} CSaveState;
+
+
+typedef UInt32 CProbPrice;
+
+
+struct CLzmaEnc
+{
+ void *matchFinderObj;
+ IMatchFinder2 matchFinder;
+
+ unsigned optCur;
+ unsigned optEnd;
+
+ unsigned longestMatchLen;
+ unsigned numPairs;
+ UInt32 numAvail;
+
+ unsigned state;
+ unsigned numFastBytes;
+ unsigned additionalOffset;
+ UInt32 reps[LZMA_NUM_REPS];
+ unsigned lpMask, pbMask;
+ CLzmaProb *litProbs;
+ CRangeEnc rc;
+
+ UInt32 backRes;
+
+ unsigned lc, lp, pb;
+ unsigned lclp;
+
+ BoolInt fastMode;
+ BoolInt writeEndMark;
+ BoolInt finished;
+ BoolInt multiThread;
+ BoolInt needInit;
+ // BoolInt _maxMode;
+
+ UInt64 nowPos64;
+
+ unsigned matchPriceCount;
+ // unsigned alignPriceCount;
+ int repLenEncCounter;
+
+ unsigned distTableSize;
+
+ UInt32 dictSize;
+ SRes result;
+
+ #ifndef Z7_ST
+ BoolInt mtMode;
+ // begin of CMatchFinderMt is used in LZ thread
+ CMatchFinderMt matchFinderMt;
+ // end of CMatchFinderMt is used in BT and HASH threads
+ // #else
+ // CMatchFinder matchFinderBase;
+ #endif
+ CMatchFinder matchFinderBase;
+
+
+ // we suppose that we have 8-bytes alignment after CMatchFinder
+
+ #ifndef Z7_ST
+ Byte pad[128];
+ #endif
+
+ // LZ thread
+ CProbPrice ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
+
+ // we want {len , dist} pairs to be 8-bytes aligned in matches array
+ UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2];
+
+ // we want 8-bytes alignment here
+ UInt32 alignPrices[kAlignTableSize];
+ UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
+ UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances];
+
+ CLzmaProb posAlignEncoder[1 << kNumAlignBits];
+ CLzmaProb isRep[kNumStates];
+ CLzmaProb isRepG0[kNumStates];
+ CLzmaProb isRepG1[kNumStates];
+ CLzmaProb isRepG2[kNumStates];
+ CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
+ CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
+ CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
+ CLzmaProb posEncoders[kNumFullDistances];
+
+ CLenEnc lenProbs;
+ CLenEnc repLenProbs;
+
+ #ifndef LZMA_LOG_BSR
+ Byte g_FastPos[1 << kNumLogBits];
+ #endif
+
+ CLenPriceEnc lenEnc;
+ CLenPriceEnc repLenEnc;
+
+ COptimal opt[kNumOpts];
+
+ CSaveState saveState;
+
+ // BoolInt mf_Failure;
+ #ifndef Z7_ST
+ Byte pad2[128];
+ #endif
+};
+
+
+#define MFB (p->matchFinderBase)
+/*
+#ifndef Z7_ST
+#define MFB (p->matchFinderMt.MatchFinder)
+#endif
+*/
+
+// #define GET_CLzmaEnc_p CLzmaEnc *p = (CLzmaEnc*)(void *)p;
+// #define GET_const_CLzmaEnc_p const CLzmaEnc *p = (const CLzmaEnc*)(const void *)p;
+
+#define COPY_ARR(dest, src, arr) memcpy((dest)->arr, (src)->arr, sizeof((src)->arr));
+
+#define COPY_LZMA_ENC_STATE(d, s, p) \
+ (d)->state = (s)->state; \
+ COPY_ARR(d, s, reps) \
+ COPY_ARR(d, s, posAlignEncoder) \
+ COPY_ARR(d, s, isRep) \
+ COPY_ARR(d, s, isRepG0) \
+ COPY_ARR(d, s, isRepG1) \
+ COPY_ARR(d, s, isRepG2) \
+ COPY_ARR(d, s, isMatch) \
+ COPY_ARR(d, s, isRep0Long) \
+ COPY_ARR(d, s, posSlotEncoder) \
+ COPY_ARR(d, s, posEncoders) \
+ (d)->lenProbs = (s)->lenProbs; \
+ (d)->repLenProbs = (s)->repLenProbs; \
+ memcpy((d)->litProbs, (s)->litProbs, ((size_t)0x300 * sizeof(CLzmaProb)) << (p)->lclp);
+
+void LzmaEnc_SaveState(CLzmaEncHandle p)
+{
+ // GET_CLzmaEnc_p
+ CSaveState *v = &p->saveState;
+ COPY_LZMA_ENC_STATE(v, p, p)
+}
+
+void LzmaEnc_RestoreState(CLzmaEncHandle p)
+{
+ // GET_CLzmaEnc_p
+ const CSaveState *v = &p->saveState;
+ COPY_LZMA_ENC_STATE(p, v, p)
+}
+
+
+Z7_NO_INLINE
+SRes LzmaEnc_SetProps(CLzmaEncHandle p, const CLzmaEncProps *props2)
+{
+ // GET_CLzmaEnc_p
+ CLzmaEncProps props = *props2;
+ LzmaEncProps_Normalize(&props);
+
+ if (props.lc > LZMA_LC_MAX
+ || props.lp > LZMA_LP_MAX
+ || props.pb > LZMA_PB_MAX)
+ return SZ_ERROR_PARAM;
+
+
+ if (props.dictSize > kLzmaMaxHistorySize)
+ props.dictSize = kLzmaMaxHistorySize;
+
+ #ifndef LZMA_LOG_BSR
+ {
+ const UInt64 dict64 = props.dictSize;
+ if (dict64 > ((UInt64)1 << kDicLogSizeMaxCompress))
+ return SZ_ERROR_PARAM;
+ }
+ #endif
+
+ p->dictSize = props.dictSize;
+ {
+ unsigned fb = (unsigned)props.fb;
+ if (fb < 5)
+ fb = 5;
+ if (fb > LZMA_MATCH_LEN_MAX)
+ fb = LZMA_MATCH_LEN_MAX;
+ p->numFastBytes = fb;
+ }
+ p->lc = (unsigned)props.lc;
+ p->lp = (unsigned)props.lp;
+ p->pb = (unsigned)props.pb;
+ p->fastMode = (props.algo == 0);
+ // p->_maxMode = True;
+ MFB.btMode = (Byte)(props.btMode ? 1 : 0);
+ // MFB.btMode = (Byte)(props.btMode);
+ {
+ unsigned numHashBytes = 4;
+ if (props.btMode)
+ {
+ if (props.numHashBytes < 2) numHashBytes = 2;
+ else if (props.numHashBytes < 4) numHashBytes = (unsigned)props.numHashBytes;
+ }
+ if (props.numHashBytes >= 5) numHashBytes = 5;
+
+ MFB.numHashBytes = numHashBytes;
+ // MFB.numHashBytes_Min = 2;
+ MFB.numHashOutBits = (Byte)props.numHashOutBits;
+ }
+
+ MFB.cutValue = props.mc;
+
+ p->writeEndMark = (BoolInt)props.writeEndMark;
+
+ #ifndef Z7_ST
+ /*
+ if (newMultiThread != _multiThread)
+ {
+ ReleaseMatchFinder();
+ _multiThread = newMultiThread;
+ }
+ */
+ p->multiThread = (props.numThreads > 1);
+ p->matchFinderMt.btSync.affinity =
+ p->matchFinderMt.hashSync.affinity = props.affinity;
+ #endif
+
+ return SZ_OK;
+}
+
+
+void LzmaEnc_SetDataSize(CLzmaEncHandle p, UInt64 expectedDataSiize)
+{
+ // GET_CLzmaEnc_p
+ MFB.expectedDataSize = expectedDataSiize;
+}
+
+
+#define kState_Start 0
+#define kState_LitAfterMatch 4
+#define kState_LitAfterRep 5
+#define kState_MatchAfterLit 7
+#define kState_RepAfterLit 8
+
+static const Byte kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
+static const Byte kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
+static const Byte kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
+static const Byte kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
+
+#define IsLitState(s) ((s) < 7)
+#define GetLenToPosState2(len) (((len) < kNumLenToPosStates - 1) ? (len) : kNumLenToPosStates - 1)
+#define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
+
+#define kInfinityPrice (1 << 30)
+
+static void RangeEnc_Construct(CRangeEnc *p)
+{
+ p->outStream = NULL;
+ p->bufBase = NULL;
+}
+
+#define RangeEnc_GetProcessed(p) ( (p)->processed + (size_t)((p)->buf - (p)->bufBase) + (p)->cacheSize)
+#define RangeEnc_GetProcessed_sizet(p) ((size_t)(p)->processed + (size_t)((p)->buf - (p)->bufBase) + (size_t)(p)->cacheSize)
+
+#define RC_BUF_SIZE (1 << 16)
+
+static int RangeEnc_Alloc(CRangeEnc *p, ISzAllocPtr alloc)
+{
+ if (!p->bufBase)
+ {
+ p->bufBase = (Byte *)ISzAlloc_Alloc(alloc, RC_BUF_SIZE);
+ if (!p->bufBase)
+ return 0;
+ p->bufLim = p->bufBase + RC_BUF_SIZE;
+ }
+ return 1;
+}
+
+static void RangeEnc_Free(CRangeEnc *p, ISzAllocPtr alloc)
+{
+ ISzAlloc_Free(alloc, p->bufBase);
+ p->bufBase = NULL;
+}
+
+static void RangeEnc_Init(CRangeEnc *p)
+{
+ p->range = 0xFFFFFFFF;
+ p->cache = 0;
+ p->low = 0;
+ p->cacheSize = 0;
+
+ p->buf = p->bufBase;
+
+ p->processed = 0;
+ p->res = SZ_OK;
+}
+
+Z7_NO_INLINE static void RangeEnc_FlushStream(CRangeEnc *p)
+{
+ const size_t num = (size_t)(p->buf - p->bufBase);
+ if (p->res == SZ_OK)
+ {
+ if (num != ISeqOutStream_Write(p->outStream, p->bufBase, num))
+ p->res = SZ_ERROR_WRITE;
+ }
+ p->processed += num;
+ p->buf = p->bufBase;
+}
+
+Z7_NO_INLINE static void Z7_FASTCALL RangeEnc_ShiftLow(CRangeEnc *p)
+{
+ UInt32 low = (UInt32)p->low;
+ unsigned high = (unsigned)(p->low >> 32);
+ p->low = (UInt32)(low << 8);
+ if (low < (UInt32)0xFF000000 || high != 0)
+ {
+ {
+ Byte *buf = p->buf;
+ *buf++ = (Byte)(p->cache + high);
+ p->cache = (unsigned)(low >> 24);
+ p->buf = buf;
+ if (buf == p->bufLim)
+ RangeEnc_FlushStream(p);
+ if (p->cacheSize == 0)
+ return;
+ }
+ high += 0xFF;
+ for (;;)
+ {
+ Byte *buf = p->buf;
+ *buf++ = (Byte)(high);
+ p->buf = buf;
+ if (buf == p->bufLim)
+ RangeEnc_FlushStream(p);
+ if (--p->cacheSize == 0)
+ return;
+ }
+ }
+ p->cacheSize++;
+}
+
+static void RangeEnc_FlushData(CRangeEnc *p)
+{
+ int i;
+ for (i = 0; i < 5; i++)
+ RangeEnc_ShiftLow(p);
+}
+
+#define RC_NORM(p) if (range < kTopValue) { range <<= 8; RangeEnc_ShiftLow(p); }
+
+#define RC_BIT_PRE(p, prob) \
+ ttt = *(prob); \
+ newBound = (range >> kNumBitModelTotalBits) * ttt;
+
+// #define Z7_LZMA_ENC_USE_BRANCH
+
+#ifdef Z7_LZMA_ENC_USE_BRANCH
+
+#define RC_BIT(p, prob, bit) { \
+ RC_BIT_PRE(p, prob) \
+ if (bit == 0) { range = newBound; ttt += (kBitModelTotal - ttt) >> kNumMoveBits; } \
+ else { (p)->low += newBound; range -= newBound; ttt -= ttt >> kNumMoveBits; } \
+ *(prob) = (CLzmaProb)ttt; \
+ RC_NORM(p) \
+ }
+
+#else
+
+#define RC_BIT(p, prob, bit) { \
+ UInt32 mask; \
+ RC_BIT_PRE(p, prob) \
+ mask = 0 - (UInt32)bit; \
+ range &= mask; \
+ mask &= newBound; \
+ range -= mask; \
+ (p)->low += mask; \
+ mask = (UInt32)bit - 1; \
+ range += newBound & mask; \
+ mask &= (kBitModelTotal - ((1 << kNumMoveBits) - 1)); \
+ mask += ((1 << kNumMoveBits) - 1); \
+ ttt += (UInt32)((Int32)(mask - ttt) >> kNumMoveBits); \
+ *(prob) = (CLzmaProb)ttt; \
+ RC_NORM(p) \
+ }
+
+#endif
+
+
+
+
+#define RC_BIT_0_BASE(p, prob) \
+ range = newBound; *(prob) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
+
+#define RC_BIT_1_BASE(p, prob) \
+ range -= newBound; (p)->low += newBound; *(prob) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits)); \
+
+#define RC_BIT_0(p, prob) \
+ RC_BIT_0_BASE(p, prob) \
+ RC_NORM(p)
+
+#define RC_BIT_1(p, prob) \
+ RC_BIT_1_BASE(p, prob) \
+ RC_NORM(p)
+
+static void RangeEnc_EncodeBit_0(CRangeEnc *p, CLzmaProb *prob)
+{
+ UInt32 range, ttt, newBound;
+ range = p->range;
+ RC_BIT_PRE(p, prob)
+ RC_BIT_0(p, prob)
+ p->range = range;
+}
+
+static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 sym)
+{
+ UInt32 range = p->range;
+ sym |= 0x100;
+ do
+ {
+ UInt32 ttt, newBound;
+ // RangeEnc_EncodeBit(p, probs + (sym >> 8), (sym >> 7) & 1);
+ CLzmaProb *prob = probs + (sym >> 8);
+ UInt32 bit = (sym >> 7) & 1;
+ sym <<= 1;
+ RC_BIT(p, prob, bit)
+ }
+ while (sym < 0x10000);
+ p->range = range;
+}
+
+static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 sym, UInt32 matchByte)
+{
+ UInt32 range = p->range;
+ UInt32 offs = 0x100;
+ sym |= 0x100;
+ do
+ {
+ UInt32 ttt, newBound;
+ CLzmaProb *prob;
+ UInt32 bit;
+ matchByte <<= 1;
+ // RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (sym >> 8)), (sym >> 7) & 1);
+ prob = probs + (offs + (matchByte & offs) + (sym >> 8));
+ bit = (sym >> 7) & 1;
+ sym <<= 1;
+ offs &= ~(matchByte ^ sym);
+ RC_BIT(p, prob, bit)
+ }
+ while (sym < 0x10000);
+ p->range = range;
+}
+
+
+
+static void LzmaEnc_InitPriceTables(CProbPrice *ProbPrices)
+{
+ UInt32 i;
+ for (i = 0; i < (kBitModelTotal >> kNumMoveReducingBits); i++)
+ {
+ const unsigned kCyclesBits = kNumBitPriceShiftBits;
+ UInt32 w = (i << kNumMoveReducingBits) + (1 << (kNumMoveReducingBits - 1));
+ unsigned bitCount = 0;
+ unsigned j;
+ for (j = 0; j < kCyclesBits; j++)
+ {
+ w = w * w;
+ bitCount <<= 1;
+ while (w >= ((UInt32)1 << 16))
+ {
+ w >>= 1;
+ bitCount++;
+ }
+ }
+ ProbPrices[i] = (CProbPrice)(((unsigned)kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount);
+ // printf("\n%3d: %5d", i, ProbPrices[i]);
+ }
+}
+
+
+#define GET_PRICE(prob, bit) \
+ p->ProbPrices[((prob) ^ (unsigned)(((-(int)(bit))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits]
+
+#define GET_PRICEa(prob, bit) \
+ ProbPrices[((prob) ^ (unsigned)((-((int)(bit))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits]
+
+#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
+#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
+
+#define GET_PRICEa_0(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
+#define GET_PRICEa_1(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
+
+
+static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 sym, const CProbPrice *ProbPrices)
+{
+ UInt32 price = 0;
+ sym |= 0x100;
+ do
+ {
+ unsigned bit = sym & 1;
+ sym >>= 1;
+ price += GET_PRICEa(probs[sym], bit);
+ }
+ while (sym >= 2);
+ return price;
+}
+
+
+static UInt32 LitEnc_Matched_GetPrice(const CLzmaProb *probs, UInt32 sym, UInt32 matchByte, const CProbPrice *ProbPrices)
+{
+ UInt32 price = 0;
+ UInt32 offs = 0x100;
+ sym |= 0x100;
+ do
+ {
+ matchByte <<= 1;
+ price += GET_PRICEa(probs[offs + (matchByte & offs) + (sym >> 8)], (sym >> 7) & 1);
+ sym <<= 1;
+ offs &= ~(matchByte ^ sym);
+ }
+ while (sym < 0x10000);
+ return price;
+}
+
+
+static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, unsigned numBits, unsigned sym)
+{
+ UInt32 range = rc->range;
+ unsigned m = 1;
+ do
+ {
+ UInt32 ttt, newBound;
+ unsigned bit = sym & 1;
+ // RangeEnc_EncodeBit(rc, probs + m, bit);
+ sym >>= 1;
+ RC_BIT(rc, probs + m, bit)
+ m = (m << 1) | bit;
+ }
+ while (--numBits);
+ rc->range = range;
+}
+
+
+
+static void LenEnc_Init(CLenEnc *p)
+{
+ unsigned i;
+ for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << (kLenNumLowBits + 1)); i++)
+ p->low[i] = kProbInitValue;
+ for (i = 0; i < kLenNumHighSymbols; i++)
+ p->high[i] = kProbInitValue;
+}
+
+static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, unsigned sym, unsigned posState)
+{
+ UInt32 range, ttt, newBound;
+ CLzmaProb *probs = p->low;
+ range = rc->range;
+ RC_BIT_PRE(rc, probs)
+ if (sym >= kLenNumLowSymbols)
+ {
+ RC_BIT_1(rc, probs)
+ probs += kLenNumLowSymbols;
+ RC_BIT_PRE(rc, probs)
+ if (sym >= kLenNumLowSymbols * 2)
+ {
+ RC_BIT_1(rc, probs)
+ rc->range = range;
+ // RcTree_Encode(rc, p->high, kLenNumHighBits, sym - kLenNumLowSymbols * 2);
+ LitEnc_Encode(rc, p->high, sym - kLenNumLowSymbols * 2);
+ return;
+ }
+ sym -= kLenNumLowSymbols;
+ }
+
+ // RcTree_Encode(rc, probs + (posState << kLenNumLowBits), kLenNumLowBits, sym);
+ {
+ unsigned m;
+ unsigned bit;
+ RC_BIT_0(rc, probs)
+ probs += (posState << (1 + kLenNumLowBits));
+ bit = (sym >> 2) ; RC_BIT(rc, probs + 1, bit) m = (1 << 1) + bit;
+ bit = (sym >> 1) & 1; RC_BIT(rc, probs + m, bit) m = (m << 1) + bit;
+ bit = sym & 1; RC_BIT(rc, probs + m, bit)
+ rc->range = range;
+ }
+}
+
+static void SetPrices_3(const CLzmaProb *probs, UInt32 startPrice, UInt32 *prices, const CProbPrice *ProbPrices)
+{
+ unsigned i;
+ for (i = 0; i < 8; i += 2)
+ {
+ UInt32 price = startPrice;
+ UInt32 prob;
+ price += GET_PRICEa(probs[1 ], (i >> 2));
+ price += GET_PRICEa(probs[2 + (i >> 2)], (i >> 1) & 1);
+ prob = probs[4 + (i >> 1)];
+ prices[i ] = price + GET_PRICEa_0(prob);
+ prices[i + 1] = price + GET_PRICEa_1(prob);
+ }
+}
+
+
+Z7_NO_INLINE static void Z7_FASTCALL LenPriceEnc_UpdateTables(
+ CLenPriceEnc *p,
+ unsigned numPosStates,
+ const CLenEnc *enc,
+ const CProbPrice *ProbPrices)
+{
+ UInt32 b;
+
+ {
+ unsigned prob = enc->low[0];
+ UInt32 a, c;
+ unsigned posState;
+ b = GET_PRICEa_1(prob);
+ a = GET_PRICEa_0(prob);
+ c = b + GET_PRICEa_0(enc->low[kLenNumLowSymbols]);
+ for (posState = 0; posState < numPosStates; posState++)
+ {
+ UInt32 *prices = p->prices[posState];
+ const CLzmaProb *probs = enc->low + (posState << (1 + kLenNumLowBits));
+ SetPrices_3(probs, a, prices, ProbPrices);
+ SetPrices_3(probs + kLenNumLowSymbols, c, prices + kLenNumLowSymbols, ProbPrices);
+ }
+ }
+
+ /*
+ {
+ unsigned i;
+ UInt32 b;
+ a = GET_PRICEa_0(enc->low[0]);
+ for (i = 0; i < kLenNumLowSymbols; i++)
+ p->prices2[i] = a;
+ a = GET_PRICEa_1(enc->low[0]);
+ b = a + GET_PRICEa_0(enc->low[kLenNumLowSymbols]);
+ for (i = kLenNumLowSymbols; i < kLenNumLowSymbols * 2; i++)
+ p->prices2[i] = b;
+ a += GET_PRICEa_1(enc->low[kLenNumLowSymbols]);
+ }
+ */
+
+ // p->counter = numSymbols;
+ // p->counter = 64;
+
+ {
+ unsigned i = p->tableSize;
+
+ if (i > kLenNumLowSymbols * 2)
+ {
+ const CLzmaProb *probs = enc->high;
+ UInt32 *prices = p->prices[0] + kLenNumLowSymbols * 2;
+ i -= kLenNumLowSymbols * 2 - 1;
+ i >>= 1;
+ b += GET_PRICEa_1(enc->low[kLenNumLowSymbols]);
+ do
+ {
+ /*
+ p->prices2[i] = a +
+ // RcTree_GetPrice(enc->high, kLenNumHighBits, i - kLenNumLowSymbols * 2, ProbPrices);
+ LitEnc_GetPrice(probs, i - kLenNumLowSymbols * 2, ProbPrices);
+ */
+ // UInt32 price = a + RcTree_GetPrice(probs, kLenNumHighBits - 1, sym, ProbPrices);
+ unsigned sym = --i + (1 << (kLenNumHighBits - 1));
+ UInt32 price = b;
+ do
+ {
+ const unsigned bit = sym & 1;
+ sym >>= 1;
+ price += GET_PRICEa(probs[sym], bit);
+ }
+ while (sym >= 2);
+
+ {
+ const unsigned prob = probs[(size_t)i + (1 << (kLenNumHighBits - 1))];
+ prices[(size_t)i * 2 ] = price + GET_PRICEa_0(prob);
+ prices[(size_t)i * 2 + 1] = price + GET_PRICEa_1(prob);
+ }
+ }
+ while (i);
+
+ {
+ unsigned posState;
+ const size_t num = (p->tableSize - kLenNumLowSymbols * 2) * sizeof(p->prices[0][0]);
+ for (posState = 1; posState < numPosStates; posState++)
+ memcpy(p->prices[posState] + kLenNumLowSymbols * 2, p->prices[0] + kLenNumLowSymbols * 2, num);
+ }
+ }
+ }
+}
+
+/*
+ #ifdef SHOW_STAT
+ g_STAT_OFFSET += num;
+ printf("\n MovePos %u", num);
+ #endif
+*/
+
+#define MOVE_POS(p, num) { \
+ p->additionalOffset += (num); \
+ p->matchFinder.Skip(p->matchFinderObj, (UInt32)(num)); }
+
+
+static unsigned ReadMatchDistances(CLzmaEnc *p, unsigned *numPairsRes)
+{
+ unsigned numPairs;
+
+ p->additionalOffset++;
+ p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
+ {
+ const UInt32 *d = p->matchFinder.GetMatches(p->matchFinderObj, p->matches);
+ // if (!d) { p->mf_Failure = True; *numPairsRes = 0; return 0; }
+ numPairs = (unsigned)(d - p->matches);
+ }
+ *numPairsRes = numPairs;
+
+ #ifdef SHOW_STAT
+ printf("\n i = %u numPairs = %u ", g_STAT_OFFSET, numPairs / 2);
+ g_STAT_OFFSET++;
+ {
+ unsigned i;
+ for (i = 0; i < numPairs; i += 2)
+ printf("%2u %6u | ", p->matches[i], p->matches[i + 1]);
+ }
+ #endif
+
+ if (numPairs == 0)
+ return 0;
+ {
+ const unsigned len = p->matches[(size_t)numPairs - 2];
+ if (len != p->numFastBytes)
+ return len;
+ {
+ UInt32 numAvail = p->numAvail;
+ if (numAvail > LZMA_MATCH_LEN_MAX)
+ numAvail = LZMA_MATCH_LEN_MAX;
+ {
+ const Byte *p1 = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
+ const Byte *p2 = p1 + len;
+ const ptrdiff_t dif = (ptrdiff_t)-1 - (ptrdiff_t)p->matches[(size_t)numPairs - 1];
+ const Byte *lim = p1 + numAvail;
+ for (; p2 != lim && *p2 == p2[dif]; p2++)
+ {}
+ return (unsigned)(p2 - p1);
+ }
+ }
+ }
+}
+
+#define MARK_LIT ((UInt32)(Int32)-1)
+
+#define MakeAs_Lit(p) { (p)->dist = MARK_LIT; (p)->extra = 0; }
+#define MakeAs_ShortRep(p) { (p)->dist = 0; (p)->extra = 0; }
+#define IsShortRep(p) ((p)->dist == 0)
+
+
+#define GetPrice_ShortRep(p, state, posState) \
+ ( GET_PRICE_0(p->isRepG0[state]) + GET_PRICE_0(p->isRep0Long[state][posState]))
+
+#define GetPrice_Rep_0(p, state, posState) ( \
+ GET_PRICE_1(p->isMatch[state][posState]) \
+ + GET_PRICE_1(p->isRep0Long[state][posState])) \
+ + GET_PRICE_1(p->isRep[state]) \
+ + GET_PRICE_0(p->isRepG0[state])
+
+Z7_FORCE_INLINE
+static UInt32 GetPrice_PureRep(const CLzmaEnc *p, unsigned repIndex, size_t state, size_t posState)
+{
+ UInt32 price;
+ UInt32 prob = p->isRepG0[state];
+ if (repIndex == 0)
+ {
+ price = GET_PRICE_0(prob);
+ price += GET_PRICE_1(p->isRep0Long[state][posState]);
+ }
+ else
+ {
+ price = GET_PRICE_1(prob);
+ prob = p->isRepG1[state];
+ if (repIndex == 1)
+ price += GET_PRICE_0(prob);
+ else
+ {
+ price += GET_PRICE_1(prob);
+ price += GET_PRICE(p->isRepG2[state], repIndex - 2);
+ }
+ }
+ return price;
+}
+
+
+static unsigned Backward(CLzmaEnc *p, unsigned cur)
+{
+ unsigned wr = cur + 1;
+ p->optEnd = wr;
+
+ for (;;)
+ {
+ UInt32 dist = p->opt[cur].dist;
+ unsigned len = (unsigned)p->opt[cur].len;
+ unsigned extra = (unsigned)p->opt[cur].extra;
+ cur -= len;
+
+ if (extra)
+ {
+ wr--;
+ p->opt[wr].len = (UInt32)len;
+ cur -= extra;
+ len = extra;
+ if (extra == 1)
+ {
+ p->opt[wr].dist = dist;
+ dist = MARK_LIT;
+ }
+ else
+ {
+ p->opt[wr].dist = 0;
+ len--;
+ wr--;
+ p->opt[wr].dist = MARK_LIT;
+ p->opt[wr].len = 1;
+ }
+ }
+
+ if (cur == 0)
+ {
+ p->backRes = dist;
+ p->optCur = wr;
+ return len;
+ }
+
+ wr--;
+ p->opt[wr].dist = dist;
+ p->opt[wr].len = (UInt32)len;
+ }
+}
+
+
+
+#define LIT_PROBS(pos, prevByte) \
+ (p->litProbs + (UInt32)3 * (((((pos) << 8) + (prevByte)) & p->lpMask) << p->lc))
+
+
+static unsigned GetOptimum(CLzmaEnc *p, UInt32 position)
+{
+ unsigned last, cur;
+ UInt32 reps[LZMA_NUM_REPS];
+ unsigned repLens[LZMA_NUM_REPS];
+ UInt32 *matches;
+
+ {
+ UInt32 numAvail;
+ unsigned numPairs, mainLen, repMaxIndex, i, posState;
+ UInt32 matchPrice, repMatchPrice;
+ const Byte *data;
+ Byte curByte, matchByte;
+
+ p->optCur = p->optEnd = 0;
+
+ if (p->additionalOffset == 0)
+ mainLen = ReadMatchDistances(p, &numPairs);
+ else
+ {
+ mainLen = p->longestMatchLen;
+ numPairs = p->numPairs;
+ }
+
+ numAvail = p->numAvail;
+ if (numAvail < 2)
+ {
+ p->backRes = MARK_LIT;
+ return 1;
+ }
+ if (numAvail > LZMA_MATCH_LEN_MAX)
+ numAvail = LZMA_MATCH_LEN_MAX;
+
+ data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
+ repMaxIndex = 0;
+
+ for (i = 0; i < LZMA_NUM_REPS; i++)
+ {
+ unsigned len;
+ const Byte *data2;
+ reps[i] = p->reps[i];
+ data2 = data - reps[i];
+ if (data[0] != data2[0] || data[1] != data2[1])
+ {
+ repLens[i] = 0;
+ continue;
+ }
+ for (len = 2; len < numAvail && data[len] == data2[len]; len++)
+ {}
+ repLens[i] = len;
+ if (len > repLens[repMaxIndex])
+ repMaxIndex = i;
+ if (len == LZMA_MATCH_LEN_MAX) // 21.03 : optimization
+ break;
+ }
+
+ if (repLens[repMaxIndex] >= p->numFastBytes)
+ {
+ unsigned len;
+ p->backRes = (UInt32)repMaxIndex;
+ len = repLens[repMaxIndex];
+ MOVE_POS(p, len - 1)
+ return len;
+ }
+
+ matches = p->matches;
+ #define MATCHES matches
+ // #define MATCHES p->matches
+
+ if (mainLen >= p->numFastBytes)
+ {
+ p->backRes = MATCHES[(size_t)numPairs - 1] + LZMA_NUM_REPS;
+ MOVE_POS(p, mainLen - 1)
+ return mainLen;
+ }
+
+ curByte = *data;
+ matchByte = *(data - reps[0]);
+
+ last = repLens[repMaxIndex];
+ if (last <= mainLen)
+ last = mainLen;
+
+ if (last < 2 && curByte != matchByte)
+ {
+ p->backRes = MARK_LIT;
+ return 1;
+ }
+
+ p->opt[0].state = (CState)p->state;
+
+ posState = (position & p->pbMask);
+
+ {
+ const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
+ p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) +
+ (!IsLitState(p->state) ?
+ LitEnc_Matched_GetPrice(probs, curByte, matchByte, p->ProbPrices) :
+ LitEnc_GetPrice(probs, curByte, p->ProbPrices));
+ }
+
+ MakeAs_Lit(&p->opt[1])
+
+ matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
+ repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
+
+ // 18.06
+ if (matchByte == curByte && repLens[0] == 0)
+ {
+ UInt32 shortRepPrice = repMatchPrice + GetPrice_ShortRep(p, p->state, posState);
+ if (shortRepPrice < p->opt[1].price)
+ {
+ p->opt[1].price = shortRepPrice;
+ MakeAs_ShortRep(&p->opt[1])
+ }
+ if (last < 2)
+ {
+ p->backRes = p->opt[1].dist;
+ return 1;
+ }
+ }
+
+ p->opt[1].len = 1;
+
+ p->opt[0].reps[0] = reps[0];
+ p->opt[0].reps[1] = reps[1];
+ p->opt[0].reps[2] = reps[2];
+ p->opt[0].reps[3] = reps[3];
+
+ // ---------- REP ----------
+
+ for (i = 0; i < LZMA_NUM_REPS; i++)
+ {
+ unsigned repLen = repLens[i];
+ UInt32 price;
+ if (repLen < 2)
+ continue;
+ price = repMatchPrice + GetPrice_PureRep(p, i, p->state, posState);
+ do
+ {
+ UInt32 price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState, repLen);
+ COptimal *opt = &p->opt[repLen];
+ if (price2 < opt->price)
+ {
+ opt->price = price2;
+ opt->len = (UInt32)repLen;
+ opt->dist = (UInt32)i;
+ opt->extra = 0;
+ }
+ }
+ while (--repLen >= 2);
+ }
+
+
+ // ---------- MATCH ----------
+ {
+ unsigned len = repLens[0] + 1;
+ if (len <= mainLen)
+ {
+ unsigned offs = 0;
+ UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);
+
+ if (len < 2)
+ len = 2;
+ else
+ while (len > MATCHES[offs])
+ offs += 2;
+
+ for (; ; len++)
+ {
+ COptimal *opt;
+ UInt32 dist = MATCHES[(size_t)offs + 1];
+ UInt32 price = normalMatchPrice + GET_PRICE_LEN(&p->lenEnc, posState, len);
+ unsigned lenToPosState = GetLenToPosState(len);
+
+ if (dist < kNumFullDistances)
+ price += p->distancesPrices[lenToPosState][dist & (kNumFullDistances - 1)];
+ else
+ {
+ unsigned slot;
+ GetPosSlot2(dist, slot)
+ price += p->alignPrices[dist & kAlignMask];
+ price += p->posSlotPrices[lenToPosState][slot];
+ }
+
+ opt = &p->opt[len];
+
+ if (price < opt->price)
+ {
+ opt->price = price;
+ opt->len = (UInt32)len;
+ opt->dist = dist + LZMA_NUM_REPS;
+ opt->extra = 0;
+ }
+
+ if (len == MATCHES[offs])
+ {
+ offs += 2;
+ if (offs == numPairs)
+ break;
+ }
+ }
+ }
+ }
+
+
+ cur = 0;
+
+ #ifdef SHOW_STAT2
+ /* if (position >= 0) */
+ {
+ unsigned i;
+ printf("\n pos = %4X", position);
+ for (i = cur; i <= last; i++)
+ printf("\nprice[%4X] = %u", position - cur + i, p->opt[i].price);
+ }
+ #endif
+ }
+
+
+
+ // ---------- Optimal Parsing ----------
+
+ for (;;)
+ {
+ unsigned numAvail;
+ UInt32 numAvailFull;
+ unsigned newLen, numPairs, prev, state, posState, startLen;
+ UInt32 litPrice, matchPrice, repMatchPrice;
+ BoolInt nextIsLit;
+ Byte curByte, matchByte;
+ const Byte *data;
+ COptimal *curOpt, *nextOpt;
+
+ if (++cur == last)
+ break;
+
+ // 18.06
+ if (cur >= kNumOpts - 64)
+ {
+ unsigned j, best;
+ UInt32 price = p->opt[cur].price;
+ best = cur;
+ for (j = cur + 1; j <= last; j++)
+ {
+ UInt32 price2 = p->opt[j].price;
+ if (price >= price2)
+ {
+ price = price2;
+ best = j;
+ }
+ }
+ {
+ unsigned delta = best - cur;
+ if (delta != 0)
+ {
+ MOVE_POS(p, delta)
+ }
+ }
+ cur = best;
+ break;
+ }
+
+ newLen = ReadMatchDistances(p, &numPairs);
+
+ if (newLen >= p->numFastBytes)
+ {
+ p->numPairs = numPairs;
+ p->longestMatchLen = newLen;
+ break;
+ }
+
+ curOpt = &p->opt[cur];
+
+ position++;
+
+ // we need that check here, if skip_items in p->opt are possible
+ /*
+ if (curOpt->price >= kInfinityPrice)
+ continue;
+ */
+
+ prev = cur - curOpt->len;
+
+ if (curOpt->len == 1)
+ {
+ state = (unsigned)p->opt[prev].state;
+ if (IsShortRep(curOpt))
+ state = kShortRepNextStates[state];
+ else
+ state = kLiteralNextStates[state];
+ }
+ else
+ {
+ const COptimal *prevOpt;
+ UInt32 b0;
+ UInt32 dist = curOpt->dist;
+
+ if (curOpt->extra)
+ {
+ prev -= (unsigned)curOpt->extra;
+ state = kState_RepAfterLit;
+ if (curOpt->extra == 1)
+ state = (dist < LZMA_NUM_REPS ? kState_RepAfterLit : kState_MatchAfterLit);
+ }
+ else
+ {
+ state = (unsigned)p->opt[prev].state;
+ if (dist < LZMA_NUM_REPS)
+ state = kRepNextStates[state];
+ else
+ state = kMatchNextStates[state];
+ }
+
+ prevOpt = &p->opt[prev];
+ b0 = prevOpt->reps[0];
+
+ if (dist < LZMA_NUM_REPS)
+ {
+ if (dist == 0)
+ {
+ reps[0] = b0;
+ reps[1] = prevOpt->reps[1];
+ reps[2] = prevOpt->reps[2];
+ reps[3] = prevOpt->reps[3];
+ }
+ else
+ {
+ reps[1] = b0;
+ b0 = prevOpt->reps[1];
+ if (dist == 1)
+ {
+ reps[0] = b0;
+ reps[2] = prevOpt->reps[2];
+ reps[3] = prevOpt->reps[3];
+ }
+ else
+ {
+ reps[2] = b0;
+ reps[0] = prevOpt->reps[dist];
+ reps[3] = prevOpt->reps[dist ^ 1];
+ }
+ }
+ }
+ else
+ {
+ reps[0] = (dist - LZMA_NUM_REPS + 1);
+ reps[1] = b0;
+ reps[2] = prevOpt->reps[1];
+ reps[3] = prevOpt->reps[2];
+ }
+ }
+
+ curOpt->state = (CState)state;
+ curOpt->reps[0] = reps[0];
+ curOpt->reps[1] = reps[1];
+ curOpt->reps[2] = reps[2];
+ curOpt->reps[3] = reps[3];
+
+ data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
+ curByte = *data;
+ matchByte = *(data - reps[0]);
+
+ posState = (position & p->pbMask);
+
+ /*
+ The order of Price checks:
+ < LIT
+ <= SHORT_REP
+ < LIT : REP_0
+ < REP [ : LIT : REP_0 ]
+ < MATCH [ : LIT : REP_0 ]
+ */
+
+ {
+ UInt32 curPrice = curOpt->price;
+ unsigned prob = p->isMatch[state][posState];
+ matchPrice = curPrice + GET_PRICE_1(prob);
+ litPrice = curPrice + GET_PRICE_0(prob);
+ }
+
+ nextOpt = &p->opt[(size_t)cur + 1];
+ nextIsLit = False;
+
+ // here we can allow skip_items in p->opt, if we don't check (nextOpt->price < kInfinityPrice)
+ // 18.new.06
+ if ((nextOpt->price < kInfinityPrice
+ // && !IsLitState(state)
+ && matchByte == curByte)
+ || litPrice > nextOpt->price
+ )
+ litPrice = 0;
+ else
+ {
+ const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
+ litPrice += (!IsLitState(state) ?
+ LitEnc_Matched_GetPrice(probs, curByte, matchByte, p->ProbPrices) :
+ LitEnc_GetPrice(probs, curByte, p->ProbPrices));
+
+ if (litPrice < nextOpt->price)
+ {
+ nextOpt->price = litPrice;
+ nextOpt->len = 1;
+ MakeAs_Lit(nextOpt)
+ nextIsLit = True;
+ }
+ }
+
+ repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]);
+
+ numAvailFull = p->numAvail;
+ {
+ unsigned temp = kNumOpts - 1 - cur;
+ if (numAvailFull > temp)
+ numAvailFull = (UInt32)temp;
+ }
+
+ // 18.06
+ // ---------- SHORT_REP ----------
+ if (IsLitState(state)) // 18.new
+ if (matchByte == curByte)
+ if (repMatchPrice < nextOpt->price) // 18.new
+ // if (numAvailFull < 2 || data[1] != *(data - reps[0] + 1))
+ if (
+ // nextOpt->price >= kInfinityPrice ||
+ nextOpt->len < 2 // we can check nextOpt->len, if skip items are not allowed in p->opt
+ || (nextOpt->dist != 0
+ // && nextOpt->extra <= 1 // 17.old
+ )
+ )
+ {
+ UInt32 shortRepPrice = repMatchPrice + GetPrice_ShortRep(p, state, posState);
+ // if (shortRepPrice <= nextOpt->price) // 17.old
+ if (shortRepPrice < nextOpt->price) // 18.new
+ {
+ nextOpt->price = shortRepPrice;
+ nextOpt->len = 1;
+ MakeAs_ShortRep(nextOpt)
+ nextIsLit = False;
+ }
+ }
+
+ if (numAvailFull < 2)
+ continue;
+ numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes);
+
+ // numAvail <= p->numFastBytes
+
+ // ---------- LIT : REP_0 ----------
+
+ if (!nextIsLit
+ && litPrice != 0 // 18.new
+ && matchByte != curByte
+ && numAvailFull > 2)
+ {
+ const Byte *data2 = data - reps[0];
+ if (data[1] == data2[1] && data[2] == data2[2])
+ {
+ unsigned len;
+ unsigned limit = p->numFastBytes + 1;
+ if (limit > numAvailFull)
+ limit = numAvailFull;
+ for (len = 3; len < limit && data[len] == data2[len]; len++)
+ {}
+
+ {
+ unsigned state2 = kLiteralNextStates[state];
+ unsigned posState2 = (position + 1) & p->pbMask;
+ UInt32 price = litPrice + GetPrice_Rep_0(p, state2, posState2);
+ {
+ unsigned offset = cur + len;
+
+ if (last < offset)
+ last = offset;
+
+ // do
+ {
+ UInt32 price2;
+ COptimal *opt;
+ len--;
+ // price2 = price + GetPrice_Len_Rep_0(p, len, state2, posState2);
+ price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState2, len);
+
+ opt = &p->opt[offset];
+ // offset--;
+ if (price2 < opt->price)
+ {
+ opt->price = price2;
+ opt->len = (UInt32)len;
+ opt->dist = 0;
+ opt->extra = 1;
+ }
+ }
+ // while (len >= 3);
+ }
+ }
+ }
+ }
+
+ startLen = 2; /* speed optimization */
+
+ {
+ // ---------- REP ----------
+ unsigned repIndex = 0; // 17.old
+ // unsigned repIndex = IsLitState(state) ? 0 : 1; // 18.notused
+ for (; repIndex < LZMA_NUM_REPS; repIndex++)
+ {
+ unsigned len;
+ UInt32 price;
+ const Byte *data2 = data - reps[repIndex];
+ if (data[0] != data2[0] || data[1] != data2[1])
+ continue;
+
+ for (len = 2; len < numAvail && data[len] == data2[len]; len++)
+ {}
+
+ // if (len < startLen) continue; // 18.new: speed optimization
+
+ {
+ unsigned offset = cur + len;
+ if (last < offset)
+ last = offset;
+ }
+ {
+ unsigned len2 = len;
+ price = repMatchPrice + GetPrice_PureRep(p, repIndex, state, posState);
+ do
+ {
+ UInt32 price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState, len2);
+ COptimal *opt = &p->opt[cur + len2];
+ if (price2 < opt->price)
+ {
+ opt->price = price2;
+ opt->len = (UInt32)len2;
+ opt->dist = (UInt32)repIndex;
+ opt->extra = 0;
+ }
+ }
+ while (--len2 >= 2);
+ }
+
+ if (repIndex == 0) startLen = len + 1; // 17.old
+ // startLen = len + 1; // 18.new
+
+ /* if (_maxMode) */
+ {
+ // ---------- REP : LIT : REP_0 ----------
+ // numFastBytes + 1 + numFastBytes
+
+ unsigned len2 = len + 1;
+ unsigned limit = len2 + p->numFastBytes;
+ if (limit > numAvailFull)
+ limit = numAvailFull;
+
+ len2 += 2;
+ if (len2 <= limit)
+ if (data[len2 - 2] == data2[len2 - 2])
+ if (data[len2 - 1] == data2[len2 - 1])
+ {
+ unsigned state2 = kRepNextStates[state];
+ unsigned posState2 = (position + len) & p->pbMask;
+ price += GET_PRICE_LEN(&p->repLenEnc, posState, len)
+ + GET_PRICE_0(p->isMatch[state2][posState2])
+ + LitEnc_Matched_GetPrice(LIT_PROBS(position + len, data[(size_t)len - 1]),
+ data[len], data2[len], p->ProbPrices);
+
+ // state2 = kLiteralNextStates[state2];
+ state2 = kState_LitAfterRep;
+ posState2 = (posState2 + 1) & p->pbMask;
+
+
+ price += GetPrice_Rep_0(p, state2, posState2);
+
+ for (; len2 < limit && data[len2] == data2[len2]; len2++)
+ {}
+
+ len2 -= len;
+ // if (len2 >= 3)
+ {
+ {
+ unsigned offset = cur + len + len2;
+
+ if (last < offset)
+ last = offset;
+ // do
+ {
+ UInt32 price2;
+ COptimal *opt;
+ len2--;
+ // price2 = price + GetPrice_Len_Rep_0(p, len2, state2, posState2);
+ price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState2, len2);
+
+ opt = &p->opt[offset];
+ // offset--;
+ if (price2 < opt->price)
+ {
+ opt->price = price2;
+ opt->len = (UInt32)len2;
+ opt->extra = (CExtra)(len + 1);
+ opt->dist = (UInt32)repIndex;
+ }
+ }
+ // while (len2 >= 3);
+ }
+ }
+ }
+ }
+ }
+ }
+
+
+ // ---------- MATCH ----------
+ /* for (unsigned len = 2; len <= newLen; len++) */
+ if (newLen > numAvail)
+ {
+ newLen = numAvail;
+ for (numPairs = 0; newLen > MATCHES[numPairs]; numPairs += 2);
+ MATCHES[numPairs] = (UInt32)newLen;
+ numPairs += 2;
+ }
+
+ // startLen = 2; /* speed optimization */
+
+ if (newLen >= startLen)
+ {
+ UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);
+ UInt32 dist;
+ unsigned offs, posSlot, len;
+
+ {
+ unsigned offset = cur + newLen;
+ if (last < offset)
+ last = offset;
+ }
+
+ offs = 0;
+ while (startLen > MATCHES[offs])
+ offs += 2;
+ dist = MATCHES[(size_t)offs + 1];
+
+ // if (dist >= kNumFullDistances)
+ GetPosSlot2(dist, posSlot)
+
+ for (len = /*2*/ startLen; ; len++)
+ {
+ UInt32 price = normalMatchPrice + GET_PRICE_LEN(&p->lenEnc, posState, len);
+ {
+ COptimal *opt;
+ unsigned lenNorm = len - 2;
+ lenNorm = GetLenToPosState2(lenNorm);
+ if (dist < kNumFullDistances)
+ price += p->distancesPrices[lenNorm][dist & (kNumFullDistances - 1)];
+ else
+ price += p->posSlotPrices[lenNorm][posSlot] + p->alignPrices[dist & kAlignMask];
+
+ opt = &p->opt[cur + len];
+ if (price < opt->price)
+ {
+ opt->price = price;
+ opt->len = (UInt32)len;
+ opt->dist = dist + LZMA_NUM_REPS;
+ opt->extra = 0;
+ }
+ }
+
+ if (len == MATCHES[offs])
+ {
+ // if (p->_maxMode) {
+ // MATCH : LIT : REP_0
+
+ const Byte *data2 = data - dist - 1;
+ unsigned len2 = len + 1;
+ unsigned limit = len2 + p->numFastBytes;
+ if (limit > numAvailFull)
+ limit = numAvailFull;
+
+ len2 += 2;
+ if (len2 <= limit)
+ if (data[len2 - 2] == data2[len2 - 2])
+ if (data[len2 - 1] == data2[len2 - 1])
+ {
+ for (; len2 < limit && data[len2] == data2[len2]; len2++)
+ {}
+
+ len2 -= len;
+
+ // if (len2 >= 3)
+ {
+ unsigned state2 = kMatchNextStates[state];
+ unsigned posState2 = (position + len) & p->pbMask;
+ unsigned offset;
+ price += GET_PRICE_0(p->isMatch[state2][posState2]);
+ price += LitEnc_Matched_GetPrice(LIT_PROBS(position + len, data[(size_t)len - 1]),
+ data[len], data2[len], p->ProbPrices);
+
+ // state2 = kLiteralNextStates[state2];
+ state2 = kState_LitAfterMatch;
+
+ posState2 = (posState2 + 1) & p->pbMask;
+ price += GetPrice_Rep_0(p, state2, posState2);
+
+ offset = cur + len + len2;
+
+ if (last < offset)
+ last = offset;
+ // do
+ {
+ UInt32 price2;
+ COptimal *opt;
+ len2--;
+ // price2 = price + GetPrice_Len_Rep_0(p, len2, state2, posState2);
+ price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState2, len2);
+ opt = &p->opt[offset];
+ // offset--;
+ if (price2 < opt->price)
+ {
+ opt->price = price2;
+ opt->len = (UInt32)len2;
+ opt->extra = (CExtra)(len + 1);
+ opt->dist = dist + LZMA_NUM_REPS;
+ }
+ }
+ // while (len2 >= 3);
+ }
+
+ }
+
+ offs += 2;
+ if (offs == numPairs)
+ break;
+ dist = MATCHES[(size_t)offs + 1];
+ // if (dist >= kNumFullDistances)
+ GetPosSlot2(dist, posSlot)
+ }
+ }
+ }
+ }
+
+ do
+ p->opt[last].price = kInfinityPrice;
+ while (--last);
+
+ return Backward(p, cur);
+}
+
+
+
+#define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
+
+
+
+static unsigned GetOptimumFast(CLzmaEnc *p)
+{
+ UInt32 numAvail, mainDist;
+ unsigned mainLen, numPairs, repIndex, repLen, i;
+ const Byte *data;
+
+ if (p->additionalOffset == 0)
+ mainLen = ReadMatchDistances(p, &numPairs);
+ else
+ {
+ mainLen = p->longestMatchLen;
+ numPairs = p->numPairs;
+ }
+
+ numAvail = p->numAvail;
+ p->backRes = MARK_LIT;
+ if (numAvail < 2)
+ return 1;
+ // if (mainLen < 2 && p->state == 0) return 1; // 18.06.notused
+ if (numAvail > LZMA_MATCH_LEN_MAX)
+ numAvail = LZMA_MATCH_LEN_MAX;
+ data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
+ repLen = repIndex = 0;
+
+ for (i = 0; i < LZMA_NUM_REPS; i++)
+ {
+ unsigned len;
+ const Byte *data2 = data - p->reps[i];
+ if (data[0] != data2[0] || data[1] != data2[1])
+ continue;
+ for (len = 2; len < numAvail && data[len] == data2[len]; len++)
+ {}
+ if (len >= p->numFastBytes)
+ {
+ p->backRes = (UInt32)i;
+ MOVE_POS(p, len - 1)
+ return len;
+ }
+ if (len > repLen)
+ {
+ repIndex = i;
+ repLen = len;
+ }
+ }
+
+ if (mainLen >= p->numFastBytes)
+ {
+ p->backRes = p->matches[(size_t)numPairs - 1] + LZMA_NUM_REPS;
+ MOVE_POS(p, mainLen - 1)
+ return mainLen;
+ }
+
+ mainDist = 0; /* for GCC */
+
+ if (mainLen >= 2)
+ {
+ mainDist = p->matches[(size_t)numPairs - 1];
+ while (numPairs > 2)
+ {
+ UInt32 dist2;
+ if (mainLen != p->matches[(size_t)numPairs - 4] + 1)
+ break;
+ dist2 = p->matches[(size_t)numPairs - 3];
+ if (!ChangePair(dist2, mainDist))
+ break;
+ numPairs -= 2;
+ mainLen--;
+ mainDist = dist2;
+ }
+ if (mainLen == 2 && mainDist >= 0x80)
+ mainLen = 1;
+ }
+
+ if (repLen >= 2)
+ if ( repLen + 1 >= mainLen
+ || (repLen + 2 >= mainLen && mainDist >= (1 << 9))
+ || (repLen + 3 >= mainLen && mainDist >= (1 << 15)))
+ {
+ p->backRes = (UInt32)repIndex;
+ MOVE_POS(p, repLen - 1)
+ return repLen;
+ }
+
+ if (mainLen < 2 || numAvail <= 2)
+ return 1;
+
+ {
+ unsigned len1 = ReadMatchDistances(p, &p->numPairs);
+ p->longestMatchLen = len1;
+
+ if (len1 >= 2)
+ {
+ UInt32 newDist = p->matches[(size_t)p->numPairs - 1];
+ if ( (len1 >= mainLen && newDist < mainDist)
+ || (len1 == mainLen + 1 && !ChangePair(mainDist, newDist))
+ || (len1 > mainLen + 1)
+ || (len1 + 1 >= mainLen && mainLen >= 3 && ChangePair(newDist, mainDist)))
+ return 1;
+ }
+ }
+
+ data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
+
+ for (i = 0; i < LZMA_NUM_REPS; i++)
+ {
+ unsigned len, limit;
+ const Byte *data2 = data - p->reps[i];
+ if (data[0] != data2[0] || data[1] != data2[1])
+ continue;
+ limit = mainLen - 1;
+ for (len = 2;; len++)
+ {
+ if (len >= limit)
+ return 1;
+ if (data[len] != data2[len])
+ break;
+ }
+ }
+
+ p->backRes = mainDist + LZMA_NUM_REPS;
+ if (mainLen != 2)
+ {
+ MOVE_POS(p, mainLen - 2)
+ }
+ return mainLen;
+}
+
+
+
+
+static void WriteEndMarker(CLzmaEnc *p, unsigned posState)
+{
+ UInt32 range;
+ range = p->rc.range;
+ {
+ UInt32 ttt, newBound;
+ CLzmaProb *prob = &p->isMatch[p->state][posState];
+ RC_BIT_PRE(&p->rc, prob)
+ RC_BIT_1(&p->rc, prob)
+ prob = &p->isRep[p->state];
+ RC_BIT_PRE(&p->rc, prob)
+ RC_BIT_0(&p->rc, prob)
+ }
+ p->state = kMatchNextStates[p->state];
+
+ p->rc.range = range;
+ LenEnc_Encode(&p->lenProbs, &p->rc, 0, posState);
+ range = p->rc.range;
+
+ {
+ // RcTree_Encode_PosSlot(&p->rc, p->posSlotEncoder[0], (1 << kNumPosSlotBits) - 1);
+ CLzmaProb *probs = p->posSlotEncoder[0];
+ unsigned m = 1;
+ do
+ {
+ UInt32 ttt, newBound;
+ RC_BIT_PRE(p, probs + m)
+ RC_BIT_1(&p->rc, probs + m)
+ m = (m << 1) + 1;
+ }
+ while (m < (1 << kNumPosSlotBits));
+ }
+ {
+ // RangeEnc_EncodeDirectBits(&p->rc, ((UInt32)1 << (30 - kNumAlignBits)) - 1, 30 - kNumAlignBits); UInt32 range = p->range;
+ unsigned numBits = 30 - kNumAlignBits;
+ do
+ {
+ range >>= 1;
+ p->rc.low += range;
+ RC_NORM(&p->rc)
+ }
+ while (--numBits);
+ }
+
+ {
+ // RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
+ CLzmaProb *probs = p->posAlignEncoder;
+ unsigned m = 1;
+ do
+ {
+ UInt32 ttt, newBound;
+ RC_BIT_PRE(p, probs + m)
+ RC_BIT_1(&p->rc, probs + m)
+ m = (m << 1) + 1;
+ }
+ while (m < kAlignTableSize);
+ }
+ p->rc.range = range;
+}
+
+
+static SRes CheckErrors(CLzmaEnc *p)
+{
+ if (p->result != SZ_OK)
+ return p->result;
+ if (p->rc.res != SZ_OK)
+ p->result = SZ_ERROR_WRITE;
+
+ #ifndef Z7_ST
+ if (
+ // p->mf_Failure ||
+ (p->mtMode &&
+ ( // p->matchFinderMt.failure_LZ_LZ ||
+ p->matchFinderMt.failure_LZ_BT))
+ )
+ {
+ p->result = MY_HRES_ERROR_INTERNAL_ERROR;
+ // printf("\nCheckErrors p->matchFinderMt.failureLZ\n");
+ }
+ #endif
+
+ if (MFB.result != SZ_OK)
+ p->result = SZ_ERROR_READ;
+
+ if (p->result != SZ_OK)
+ p->finished = True;
+ return p->result;
+}
+
+
+Z7_NO_INLINE static SRes Flush(CLzmaEnc *p, UInt32 nowPos)
+{
+ /* ReleaseMFStream(); */
+ p->finished = True;
+ if (p->writeEndMark)
+ WriteEndMarker(p, nowPos & p->pbMask);
+ RangeEnc_FlushData(&p->rc);
+ RangeEnc_FlushStream(&p->rc);
+ return CheckErrors(p);
+}
+
+
+Z7_NO_INLINE static void FillAlignPrices(CLzmaEnc *p)
+{
+ unsigned i;
+ const CProbPrice *ProbPrices = p->ProbPrices;
+ const CLzmaProb *probs = p->posAlignEncoder;
+ // p->alignPriceCount = 0;
+ for (i = 0; i < kAlignTableSize / 2; i++)
+ {
+ UInt32 price = 0;
+ unsigned sym = i;
+ unsigned m = 1;
+ unsigned bit;
+ UInt32 prob;
+ bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[m], bit); m = (m << 1) + bit;
+ bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[m], bit); m = (m << 1) + bit;
+ bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[m], bit); m = (m << 1) + bit;
+ prob = probs[m];
+ p->alignPrices[i ] = price + GET_PRICEa_0(prob);
+ p->alignPrices[i + 8] = price + GET_PRICEa_1(prob);
+ // p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
+ }
+}
+
+
+Z7_NO_INLINE static void FillDistancesPrices(CLzmaEnc *p)
+{
+ // int y; for (y = 0; y < 100; y++) {
+
+ UInt32 tempPrices[kNumFullDistances];
+ unsigned i, lps;
+
+ const CProbPrice *ProbPrices = p->ProbPrices;
+ p->matchPriceCount = 0;
+
+ for (i = kStartPosModelIndex / 2; i < kNumFullDistances / 2; i++)
+ {
+ unsigned posSlot = GetPosSlot1(i);
+ unsigned footerBits = (posSlot >> 1) - 1;
+ unsigned base = ((2 | (posSlot & 1)) << footerBits);
+ const CLzmaProb *probs = p->posEncoders + (size_t)base * 2;
+ // tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base, footerBits, i - base, p->ProbPrices);
+ UInt32 price = 0;
+ unsigned m = 1;
+ unsigned sym = i;
+ unsigned offset = (unsigned)1 << footerBits;
+ base += i;
+
+ if (footerBits)
+ do
+ {
+ unsigned bit = sym & 1;
+ sym >>= 1;
+ price += GET_PRICEa(probs[m], bit);
+ m = (m << 1) + bit;
+ }
+ while (--footerBits);
+
+ {
+ unsigned prob = probs[m];
+ tempPrices[base ] = price + GET_PRICEa_0(prob);
+ tempPrices[base + offset] = price + GET_PRICEa_1(prob);
+ }
+ }
+
+ for (lps = 0; lps < kNumLenToPosStates; lps++)
+ {
+ unsigned slot;
+ unsigned distTableSize2 = (p->distTableSize + 1) >> 1;
+ UInt32 *posSlotPrices = p->posSlotPrices[lps];
+ const CLzmaProb *probs = p->posSlotEncoder[lps];
+
+ for (slot = 0; slot < distTableSize2; slot++)
+ {
+ // posSlotPrices[slot] = RcTree_GetPrice(encoder, kNumPosSlotBits, slot, p->ProbPrices);
+ UInt32 price;
+ unsigned bit;
+ unsigned sym = slot + (1 << (kNumPosSlotBits - 1));
+ unsigned prob;
+ bit = sym & 1; sym >>= 1; price = GET_PRICEa(probs[sym], bit);
+ bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[sym], bit);
+ bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[sym], bit);
+ bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[sym], bit);
+ bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[sym], bit);
+ prob = probs[(size_t)slot + (1 << (kNumPosSlotBits - 1))];
+ posSlotPrices[(size_t)slot * 2 ] = price + GET_PRICEa_0(prob);
+ posSlotPrices[(size_t)slot * 2 + 1] = price + GET_PRICEa_1(prob);
+ }
+
+ {
+ UInt32 delta = ((UInt32)((kEndPosModelIndex / 2 - 1) - kNumAlignBits) << kNumBitPriceShiftBits);
+ for (slot = kEndPosModelIndex / 2; slot < distTableSize2; slot++)
+ {
+ posSlotPrices[(size_t)slot * 2 ] += delta;
+ posSlotPrices[(size_t)slot * 2 + 1] += delta;
+ delta += ((UInt32)1 << kNumBitPriceShiftBits);
+ }
+ }
+
+ {
+ UInt32 *dp = p->distancesPrices[lps];
+
+ dp[0] = posSlotPrices[0];
+ dp[1] = posSlotPrices[1];
+ dp[2] = posSlotPrices[2];
+ dp[3] = posSlotPrices[3];
+
+ for (i = 4; i < kNumFullDistances; i += 2)
+ {
+ UInt32 slotPrice = posSlotPrices[GetPosSlot1(i)];
+ dp[i ] = slotPrice + tempPrices[i];
+ dp[i + 1] = slotPrice + tempPrices[i + 1];
+ }
+ }
+ }
+ // }
+}
+
+
+
+static void LzmaEnc_Construct(CLzmaEnc *p)
+{
+ RangeEnc_Construct(&p->rc);
+ MatchFinder_Construct(&MFB);
+
+ #ifndef Z7_ST
+ p->matchFinderMt.MatchFinder = &MFB;
+ MatchFinderMt_Construct(&p->matchFinderMt);
+ #endif
+
+ {
+ CLzmaEncProps props;
+ LzmaEncProps_Init(&props);
+ LzmaEnc_SetProps((CLzmaEncHandle)(void *)p, &props);
+ }
+
+ #ifndef LZMA_LOG_BSR
+ LzmaEnc_FastPosInit(p->g_FastPos);
+ #endif
+
+ LzmaEnc_InitPriceTables(p->ProbPrices);
+ p->litProbs = NULL;
+ p->saveState.litProbs = NULL;
+}
+
+CLzmaEncHandle LzmaEnc_Create(ISzAllocPtr alloc)
+{
+ void *p;
+ p = ISzAlloc_Alloc(alloc, sizeof(CLzmaEnc));
+ if (p)
+ LzmaEnc_Construct((CLzmaEnc *)p);
+ return p;
+}
+
+static void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAllocPtr alloc)
+{
+ ISzAlloc_Free(alloc, p->litProbs);
+ ISzAlloc_Free(alloc, p->saveState.litProbs);
+ p->litProbs = NULL;
+ p->saveState.litProbs = NULL;
+}
+
+static void LzmaEnc_Destruct(CLzmaEnc *p, ISzAllocPtr alloc, ISzAllocPtr allocBig)
+{
+ #ifndef Z7_ST
+ MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);
+ #endif
+
+ MatchFinder_Free(&MFB, allocBig);
+ LzmaEnc_FreeLits(p, alloc);
+ RangeEnc_Free(&p->rc, alloc);
+}
+
+void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAllocPtr alloc, ISzAllocPtr allocBig)
+{
+ // GET_CLzmaEnc_p
+ LzmaEnc_Destruct(p, alloc, allocBig);
+ ISzAlloc_Free(alloc, p);
+}
+
+
+Z7_NO_INLINE
+static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, UInt32 maxPackSize, UInt32 maxUnpackSize)
+{
+ UInt32 nowPos32, startPos32;
+ if (p->needInit)
+ {
+ #ifndef Z7_ST
+ if (p->mtMode)
+ {
+ RINOK(MatchFinderMt_InitMt(&p->matchFinderMt))
+ }
+ #endif
+ p->matchFinder.Init(p->matchFinderObj);
+ p->needInit = 0;
+ }
+
+ if (p->finished)
+ return p->result;
+ RINOK(CheckErrors(p))
+
+ nowPos32 = (UInt32)p->nowPos64;
+ startPos32 = nowPos32;
+
+ if (p->nowPos64 == 0)
+ {
+ unsigned numPairs;
+ Byte curByte;
+ if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
+ return Flush(p, nowPos32);
+ ReadMatchDistances(p, &numPairs);
+ RangeEnc_EncodeBit_0(&p->rc, &p->isMatch[kState_Start][0]);
+ // p->state = kLiteralNextStates[p->state];
+ curByte = *(p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset);
+ LitEnc_Encode(&p->rc, p->litProbs, curByte);
+ p->additionalOffset--;
+ nowPos32++;
+ }
+
+ if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0)
+
+ for (;;)
+ {
+ UInt32 dist;
+ unsigned len, posState;
+ UInt32 range, ttt, newBound;
+ CLzmaProb *probs;
+
+ if (p->fastMode)
+ len = GetOptimumFast(p);
+ else
+ {
+ unsigned oci = p->optCur;
+ if (p->optEnd == oci)
+ len = GetOptimum(p, nowPos32);
+ else
+ {
+ const COptimal *opt = &p->opt[oci];
+ len = opt->len;
+ p->backRes = opt->dist;
+ p->optCur = oci + 1;
+ }
+ }
+
+ posState = (unsigned)nowPos32 & p->pbMask;
+ range = p->rc.range;
+ probs = &p->isMatch[p->state][posState];
+
+ RC_BIT_PRE(&p->rc, probs)
+
+ dist = p->backRes;
+
+ #ifdef SHOW_STAT2
+ printf("\n pos = %6X, len = %3u pos = %6u", nowPos32, len, dist);
+ #endif
+
+ if (dist == MARK_LIT)
+ {
+ Byte curByte;
+ const Byte *data;
+ unsigned state;
+
+ RC_BIT_0(&p->rc, probs)
+ p->rc.range = range;
+ data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
+ probs = LIT_PROBS(nowPos32, *(data - 1));
+ curByte = *data;
+ state = p->state;
+ p->state = kLiteralNextStates[state];
+ if (IsLitState(state))
+ LitEnc_Encode(&p->rc, probs, curByte);
+ else
+ LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0]));
+ }
+ else
+ {
+ RC_BIT_1(&p->rc, probs)
+ probs = &p->isRep[p->state];
+ RC_BIT_PRE(&p->rc, probs)
+
+ if (dist < LZMA_NUM_REPS)
+ {
+ RC_BIT_1(&p->rc, probs)
+ probs = &p->isRepG0[p->state];
+ RC_BIT_PRE(&p->rc, probs)
+ if (dist == 0)
+ {
+ RC_BIT_0(&p->rc, probs)
+ probs = &p->isRep0Long[p->state][posState];
+ RC_BIT_PRE(&p->rc, probs)
+ if (len != 1)
+ {
+ RC_BIT_1_BASE(&p->rc, probs)
+ }
+ else
+ {
+ RC_BIT_0_BASE(&p->rc, probs)
+ p->state = kShortRepNextStates[p->state];
+ }
+ }
+ else
+ {
+ RC_BIT_1(&p->rc, probs)
+ probs = &p->isRepG1[p->state];
+ RC_BIT_PRE(&p->rc, probs)
+ if (dist == 1)
+ {
+ RC_BIT_0_BASE(&p->rc, probs)
+ dist = p->reps[1];
+ }
+ else
+ {
+ RC_BIT_1(&p->rc, probs)
+ probs = &p->isRepG2[p->state];
+ RC_BIT_PRE(&p->rc, probs)
+ if (dist == 2)
+ {
+ RC_BIT_0_BASE(&p->rc, probs)
+ dist = p->reps[2];
+ }
+ else
+ {
+ RC_BIT_1_BASE(&p->rc, probs)
+ dist = p->reps[3];
+ p->reps[3] = p->reps[2];
+ }
+ p->reps[2] = p->reps[1];
+ }
+ p->reps[1] = p->reps[0];
+ p->reps[0] = dist;
+ }
+
+ RC_NORM(&p->rc)
+
+ p->rc.range = range;
+
+ if (len != 1)
+ {
+ LenEnc_Encode(&p->repLenProbs, &p->rc, len - LZMA_MATCH_LEN_MIN, posState);
+ --p->repLenEncCounter;
+ p->state = kRepNextStates[p->state];
+ }
+ }
+ else
+ {
+ unsigned posSlot;
+ RC_BIT_0(&p->rc, probs)
+ p->rc.range = range;
+ p->state = kMatchNextStates[p->state];
+
+ LenEnc_Encode(&p->lenProbs, &p->rc, len - LZMA_MATCH_LEN_MIN, posState);
+ // --p->lenEnc.counter;
+
+ dist -= LZMA_NUM_REPS;
+ p->reps[3] = p->reps[2];
+ p->reps[2] = p->reps[1];
+ p->reps[1] = p->reps[0];
+ p->reps[0] = dist + 1;
+
+ p->matchPriceCount++;
+ GetPosSlot(dist, posSlot)
+ // RcTree_Encode_PosSlot(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], posSlot);
+ {
+ UInt32 sym = (UInt32)posSlot + (1 << kNumPosSlotBits);
+ range = p->rc.range;
+ probs = p->posSlotEncoder[GetLenToPosState(len)];
+ do
+ {
+ CLzmaProb *prob = probs + (sym >> kNumPosSlotBits);
+ UInt32 bit = (sym >> (kNumPosSlotBits - 1)) & 1;
+ sym <<= 1;
+ RC_BIT(&p->rc, prob, bit)
+ }
+ while (sym < (1 << kNumPosSlotBits * 2));
+ p->rc.range = range;
+ }
+
+ if (dist >= kStartPosModelIndex)
+ {
+ unsigned footerBits = ((posSlot >> 1) - 1);
+
+ if (dist < kNumFullDistances)
+ {
+ unsigned base = ((2 | (posSlot & 1)) << footerBits);
+ RcTree_ReverseEncode(&p->rc, p->posEncoders + base, footerBits, (unsigned)(dist /* - base */));
+ }
+ else
+ {
+ UInt32 pos2 = (dist | 0xF) << (32 - footerBits);
+ range = p->rc.range;
+ // RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);
+ /*
+ do
+ {
+ range >>= 1;
+ p->rc.low += range & (0 - ((dist >> --footerBits) & 1));
+ RC_NORM(&p->rc)
+ }
+ while (footerBits > kNumAlignBits);
+ */
+ do
+ {
+ range >>= 1;
+ p->rc.low += range & (0 - (pos2 >> 31));
+ pos2 += pos2;
+ RC_NORM(&p->rc)
+ }
+ while (pos2 != 0xF0000000);
+
+
+ // RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);
+
+ {
+ unsigned m = 1;
+ unsigned bit;
+ bit = dist & 1; dist >>= 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit) m = (m << 1) + bit;
+ bit = dist & 1; dist >>= 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit) m = (m << 1) + bit;
+ bit = dist & 1; dist >>= 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit) m = (m << 1) + bit;
+ bit = dist & 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit)
+ p->rc.range = range;
+ // p->alignPriceCount++;
+ }
+ }
+ }
+ }
+ }
+
+ nowPos32 += (UInt32)len;
+ p->additionalOffset -= len;
+
+ if (p->additionalOffset == 0)
+ {
+ UInt32 processed;
+
+ if (!p->fastMode)
+ {
+ /*
+ if (p->alignPriceCount >= 16) // kAlignTableSize
+ FillAlignPrices(p);
+ if (p->matchPriceCount >= 128)
+ FillDistancesPrices(p);
+ if (p->lenEnc.counter <= 0)
+ LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, &p->lenProbs, p->ProbPrices);
+ */
+ if (p->matchPriceCount >= 64)
+ {
+ FillAlignPrices(p);
+ // { int y; for (y = 0; y < 100; y++) {
+ FillDistancesPrices(p);
+ // }}
+ LenPriceEnc_UpdateTables(&p->lenEnc, (unsigned)1 << p->pb, &p->lenProbs, p->ProbPrices);
+ }
+ if (p->repLenEncCounter <= 0)
+ {
+ p->repLenEncCounter = REP_LEN_COUNT;
+ LenPriceEnc_UpdateTables(&p->repLenEnc, (unsigned)1 << p->pb, &p->repLenProbs, p->ProbPrices);
+ }
+ }
+
+ if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
+ break;
+ processed = nowPos32 - startPos32;
+
+ if (maxPackSize)
+ {
+ if (processed + kNumOpts + 300 >= maxUnpackSize
+ || RangeEnc_GetProcessed_sizet(&p->rc) + kPackReserve >= maxPackSize)
+ break;
+ }
+ else if (processed >= (1 << 17))
+ {
+ p->nowPos64 += nowPos32 - startPos32;
+ return CheckErrors(p);
+ }
+ }
+ }
+
+ p->nowPos64 += nowPos32 - startPos32;
+ return Flush(p, nowPos32);
+}
+
+
+
+#define kBigHashDicLimit ((UInt32)1 << 24)
+
+static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAllocPtr alloc, ISzAllocPtr allocBig)
+{
+ UInt32 beforeSize = kNumOpts;
+ UInt32 dictSize;
+
+ if (!RangeEnc_Alloc(&p->rc, alloc))
+ return SZ_ERROR_MEM;
+
+ #ifndef Z7_ST
+ p->mtMode = (p->multiThread && !p->fastMode && (MFB.btMode != 0));
+ #endif
+
+ {
+ const unsigned lclp = p->lc + p->lp;
+ if (!p->litProbs || !p->saveState.litProbs || p->lclp != lclp)
+ {
+ LzmaEnc_FreeLits(p, alloc);
+ p->litProbs = (CLzmaProb *)ISzAlloc_Alloc(alloc, ((size_t)0x300 * sizeof(CLzmaProb)) << lclp);
+ p->saveState.litProbs = (CLzmaProb *)ISzAlloc_Alloc(alloc, ((size_t)0x300 * sizeof(CLzmaProb)) << lclp);
+ if (!p->litProbs || !p->saveState.litProbs)
+ {
+ LzmaEnc_FreeLits(p, alloc);
+ return SZ_ERROR_MEM;
+ }
+ p->lclp = lclp;
+ }
+ }
+
+ MFB.bigHash = (Byte)(p->dictSize > kBigHashDicLimit ? 1 : 0);
+
+
+ dictSize = p->dictSize;
+ if (dictSize == ((UInt32)2 << 30) ||
+ dictSize == ((UInt32)3 << 30))
+ {
+ /* 21.03 : here we reduce the dictionary for 2 reasons:
+ 1) we don't want 32-bit back_distance matches in decoder for 2 GB dictionary.
+ 2) we want to elimate useless last MatchFinder_Normalize3() for corner cases,
+ where data size is aligned for 1 GB: 5/6/8 GB.
+ That reducing must be >= 1 for such corner cases. */
+ dictSize -= 1;
+ }
+
+ if (beforeSize + dictSize < keepWindowSize)
+ beforeSize = keepWindowSize - dictSize;
+
+ /* in worst case we can look ahead for
+ max(LZMA_MATCH_LEN_MAX, numFastBytes + 1 + numFastBytes) bytes.
+ we send larger value for (keepAfter) to MantchFinder_Create():
+ (numFastBytes + LZMA_MATCH_LEN_MAX + 1)
+ */
+
+ #ifndef Z7_ST
+ if (p->mtMode)
+ {
+ RINOK(MatchFinderMt_Create(&p->matchFinderMt, dictSize, beforeSize,
+ p->numFastBytes, LZMA_MATCH_LEN_MAX + 1 /* 18.04 */
+ , allocBig))
+ p->matchFinderObj = &p->matchFinderMt;
+ MFB.bigHash = (Byte)(MFB.hashMask >= 0xFFFFFF ? 1 : 0);
+ MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder);
+ }
+ else
+ #endif
+ {
+ if (!MatchFinder_Create(&MFB, dictSize, beforeSize,
+ p->numFastBytes, LZMA_MATCH_LEN_MAX + 1 /* 21.03 */
+ , allocBig))
+ return SZ_ERROR_MEM;
+ p->matchFinderObj = &MFB;
+ MatchFinder_CreateVTable(&MFB, &p->matchFinder);
+ }
+
+ return SZ_OK;
+}
+
+static void LzmaEnc_Init(CLzmaEnc *p)
+{
+ unsigned i;
+ p->state = 0;
+ p->reps[0] =
+ p->reps[1] =
+ p->reps[2] =
+ p->reps[3] = 1;
+
+ RangeEnc_Init(&p->rc);
+
+ for (i = 0; i < (1 << kNumAlignBits); i++)
+ p->posAlignEncoder[i] = kProbInitValue;
+
+ for (i = 0; i < kNumStates; i++)
+ {
+ unsigned j;
+ for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++)
+ {
+ p->isMatch[i][j] = kProbInitValue;
+ p->isRep0Long[i][j] = kProbInitValue;
+ }
+ p->isRep[i] = kProbInitValue;
+ p->isRepG0[i] = kProbInitValue;
+ p->isRepG1[i] = kProbInitValue;
+ p->isRepG2[i] = kProbInitValue;
+ }
+
+ {
+ for (i = 0; i < kNumLenToPosStates; i++)
+ {
+ CLzmaProb *probs = p->posSlotEncoder[i];
+ unsigned j;
+ for (j = 0; j < (1 << kNumPosSlotBits); j++)
+ probs[j] = kProbInitValue;
+ }
+ }
+ {
+ for (i = 0; i < kNumFullDistances; i++)
+ p->posEncoders[i] = kProbInitValue;
+ }
+
+ {
+ const size_t num = (size_t)0x300 << (p->lp + p->lc);
+ size_t k;
+ CLzmaProb *probs = p->litProbs;
+ for (k = 0; k < num; k++)
+ probs[k] = kProbInitValue;
+ }
+
+
+ LenEnc_Init(&p->lenProbs);
+ LenEnc_Init(&p->repLenProbs);
+
+ p->optEnd = 0;
+ p->optCur = 0;
+
+ {
+ for (i = 0; i < kNumOpts; i++)
+ p->opt[i].price = kInfinityPrice;
+ }
+
+ p->additionalOffset = 0;
+
+ p->pbMask = ((unsigned)1 << p->pb) - 1;
+ p->lpMask = ((UInt32)0x100 << p->lp) - ((unsigned)0x100 >> p->lc);
+
+ // p->mf_Failure = False;
+}
+
+
+static void LzmaEnc_InitPrices(CLzmaEnc *p)
+{
+ if (!p->fastMode)
+ {
+ FillDistancesPrices(p);
+ FillAlignPrices(p);
+ }
+
+ p->lenEnc.tableSize =
+ p->repLenEnc.tableSize =
+ p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN;
+
+ p->repLenEncCounter = REP_LEN_COUNT;
+
+ LenPriceEnc_UpdateTables(&p->lenEnc, (unsigned)1 << p->pb, &p->lenProbs, p->ProbPrices);
+ LenPriceEnc_UpdateTables(&p->repLenEnc, (unsigned)1 << p->pb, &p->repLenProbs, p->ProbPrices);
+}
+
+static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAllocPtr alloc, ISzAllocPtr allocBig)
+{
+ unsigned i;
+ for (i = kEndPosModelIndex / 2; i < kDicLogSizeMax; i++)
+ if (p->dictSize <= ((UInt32)1 << i))
+ break;
+ p->distTableSize = i * 2;
+
+ p->finished = False;
+ p->result = SZ_OK;
+ p->nowPos64 = 0;
+ p->needInit = 1;
+ RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig))
+ LzmaEnc_Init(p);
+ LzmaEnc_InitPrices(p);
+ return SZ_OK;
+}
+
+static SRes LzmaEnc_Prepare(CLzmaEncHandle p,
+ ISeqOutStreamPtr outStream,
+ ISeqInStreamPtr inStream,
+ ISzAllocPtr alloc, ISzAllocPtr allocBig)
+{
+ // GET_CLzmaEnc_p
+ MatchFinder_SET_STREAM(&MFB, inStream)
+ p->rc.outStream = outStream;
+ return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig);
+}
+
+SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle p,
+ ISeqInStreamPtr inStream, UInt32 keepWindowSize,
+ ISzAllocPtr alloc, ISzAllocPtr allocBig)
+{
+ // GET_CLzmaEnc_p
+ MatchFinder_SET_STREAM(&MFB, inStream)
+ return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
+}
+
+SRes LzmaEnc_MemPrepare(CLzmaEncHandle p,
+ const Byte *src, SizeT srcLen,
+ UInt32 keepWindowSize,
+ ISzAllocPtr alloc, ISzAllocPtr allocBig)
+{
+ // GET_CLzmaEnc_p
+ MatchFinder_SET_DIRECT_INPUT_BUF(&MFB, src, srcLen)
+ LzmaEnc_SetDataSize(p, srcLen);
+ return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
+}
+
+void LzmaEnc_Finish(CLzmaEncHandle p)
+{
+ #ifndef Z7_ST
+ // GET_CLzmaEnc_p
+ if (p->mtMode)
+ MatchFinderMt_ReleaseStream(&p->matchFinderMt);
+ #else
+ UNUSED_VAR(p)
+ #endif
+}
+
+
+typedef struct
+{
+ ISeqOutStream vt;
+ Byte *data;
+ size_t rem;
+ BoolInt overflow;
+} CLzmaEnc_SeqOutStreamBuf;
+
+static size_t SeqOutStreamBuf_Write(ISeqOutStreamPtr pp, const void *data, size_t size)
+{
+ Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CLzmaEnc_SeqOutStreamBuf)
+ if (p->rem < size)
+ {
+ size = p->rem;
+ p->overflow = True;
+ }
+ if (size != 0)
+ {
+ memcpy(p->data, data, size);
+ p->rem -= size;
+ p->data += size;
+ }
+ return size;
+}
+
+
+/*
+UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle p)
+{
+ GET_const_CLzmaEnc_p
+ return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
+}
+*/
+
+const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle p)
+{
+ // GET_const_CLzmaEnc_p
+ return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
+}
+
+
+// (desiredPackSize == 0) is not allowed
+SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle p, BoolInt reInit,
+ Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize)
+{
+ // GET_CLzmaEnc_p
+ UInt64 nowPos64;
+ SRes res;
+ CLzmaEnc_SeqOutStreamBuf outStream;
+
+ outStream.vt.Write = SeqOutStreamBuf_Write;
+ outStream.data = dest;
+ outStream.rem = *destLen;
+ outStream.overflow = False;
+
+ p->writeEndMark = False;
+ p->finished = False;
+ p->result = SZ_OK;
+
+ if (reInit)
+ LzmaEnc_Init(p);
+ LzmaEnc_InitPrices(p);
+ RangeEnc_Init(&p->rc);
+ p->rc.outStream = &outStream.vt;
+ nowPos64 = p->nowPos64;
+
+ res = LzmaEnc_CodeOneBlock(p, desiredPackSize, *unpackSize);
+
+ *unpackSize = (UInt32)(p->nowPos64 - nowPos64);
+ *destLen -= outStream.rem;
+ if (outStream.overflow)
+ return SZ_ERROR_OUTPUT_EOF;
+
+ return res;
+}
+
+
+Z7_NO_INLINE
+static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgressPtr progress)
+{
+ SRes res = SZ_OK;
+
+ #ifndef Z7_ST
+ Byte allocaDummy[0x300];
+ allocaDummy[0] = 0;
+ allocaDummy[1] = allocaDummy[0];
+ #endif
+
+ for (;;)
+ {
+ res = LzmaEnc_CodeOneBlock(p, 0, 0);
+ if (res != SZ_OK || p->finished)
+ break;
+ if (progress)
+ {
+ res = ICompressProgress_Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));
+ if (res != SZ_OK)
+ {
+ res = SZ_ERROR_PROGRESS;
+ break;
+ }
+ }
+ }
+
+ LzmaEnc_Finish((CLzmaEncHandle)(void *)p);
+
+ /*
+ if (res == SZ_OK && !Inline_MatchFinder_IsFinishedOK(&MFB))
+ res = SZ_ERROR_FAIL;
+ }
+ */
+
+ return res;
+}
+
+
+SRes LzmaEnc_Encode(CLzmaEncHandle p, ISeqOutStreamPtr outStream, ISeqInStreamPtr inStream, ICompressProgressPtr progress,
+ ISzAllocPtr alloc, ISzAllocPtr allocBig)
+{
+ // GET_CLzmaEnc_p
+ RINOK(LzmaEnc_Prepare(p, outStream, inStream, alloc, allocBig))
+ return LzmaEnc_Encode2(p, progress);
+}
+
+
+SRes LzmaEnc_WriteProperties(CLzmaEncHandle p, Byte *props, SizeT *size)
+{
+ if (*size < LZMA_PROPS_SIZE)
+ return SZ_ERROR_PARAM;
+ *size = LZMA_PROPS_SIZE;
+ {
+ // GET_CLzmaEnc_p
+ const UInt32 dictSize = p->dictSize;
+ UInt32 v;
+ props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc);
+
+ // we write aligned dictionary value to properties for lzma decoder
+ if (dictSize >= ((UInt32)1 << 21))
+ {
+ const UInt32 kDictMask = ((UInt32)1 << 20) - 1;
+ v = (dictSize + kDictMask) & ~kDictMask;
+ if (v < dictSize)
+ v = dictSize;
+ }
+ else
+ {
+ unsigned i = 11 * 2;
+ do
+ {
+ v = (UInt32)(2 + (i & 1)) << (i >> 1);
+ i++;
+ }
+ while (v < dictSize);
+ }
+
+ SetUi32(props + 1, v)
+ return SZ_OK;
+ }
+}
+
+
+unsigned LzmaEnc_IsWriteEndMark(CLzmaEncHandle p)
+{
+ // GET_CLzmaEnc_p
+ return (unsigned)p->writeEndMark;
+}
+
+
+SRes LzmaEnc_MemEncode(CLzmaEncHandle p, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
+ int writeEndMark, ICompressProgressPtr progress, ISzAllocPtr alloc, ISzAllocPtr allocBig)
+{
+ SRes res;
+ // GET_CLzmaEnc_p
+
+ CLzmaEnc_SeqOutStreamBuf outStream;
+
+ outStream.vt.Write = SeqOutStreamBuf_Write;
+ outStream.data = dest;
+ outStream.rem = *destLen;
+ outStream.overflow = False;
+
+ p->writeEndMark = writeEndMark;
+ p->rc.outStream = &outStream.vt;
+
+ res = LzmaEnc_MemPrepare(p, src, srcLen, 0, alloc, allocBig);
+
+ if (res == SZ_OK)
+ {
+ res = LzmaEnc_Encode2(p, progress);
+ if (res == SZ_OK && p->nowPos64 != srcLen)
+ res = SZ_ERROR_FAIL;
+ }
+
+ *destLen -= (SizeT)outStream.rem;
+ if (outStream.overflow)
+ return SZ_ERROR_OUTPUT_EOF;
+ return res;
+}
+
+
+SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
+ const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
+ ICompressProgressPtr progress, ISzAllocPtr alloc, ISzAllocPtr allocBig)
+{
+ CLzmaEncHandle p = LzmaEnc_Create(alloc);
+ SRes res;
+ if (!p)
+ return SZ_ERROR_MEM;
+
+ res = LzmaEnc_SetProps(p, props);
+ if (res == SZ_OK)
+ {
+ res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize);
+ if (res == SZ_OK)
+ res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen,
+ writeEndMark, progress, alloc, allocBig);
+ }
+
+ LzmaEnc_Destroy(p, alloc, allocBig);
+ return res;
+}
+
+
+/*
+#ifndef Z7_ST
+void LzmaEnc_GetLzThreads(CLzmaEncHandle p, HANDLE lz_threads[2])
+{
+ GET_const_CLzmaEnc_p
+ lz_threads[0] = p->matchFinderMt.hashSync.thread;
+ lz_threads[1] = p->matchFinderMt.btSync.thread;
+}
+#endif
+*/