nuclear@14: /* LzmaEnc.c -- LZMA Encoder nuclear@14: 2009-02-02 : Igor Pavlov : Public domain */ nuclear@14: nuclear@14: #include nuclear@14: nuclear@14: /* #define SHOW_STAT */ nuclear@14: /* #define SHOW_STAT2 */ nuclear@14: nuclear@14: #if defined(SHOW_STAT) || defined(SHOW_STAT2) nuclear@14: #include nuclear@14: #endif nuclear@14: nuclear@14: #include "LzmaEnc.h" nuclear@14: nuclear@14: #include "LzFind.h" nuclear@14: #ifdef COMPRESS_MF_MT nuclear@14: #include "LzFindMt.h" nuclear@14: #endif nuclear@14: nuclear@14: #ifdef SHOW_STAT nuclear@14: static int ttt = 0; nuclear@14: #endif nuclear@14: nuclear@14: #define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1) nuclear@14: nuclear@14: #define kBlockSize (9 << 10) nuclear@14: #define kUnpackBlockSize (1 << 18) nuclear@14: #define kMatchArraySize (1 << 21) nuclear@14: #define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX) nuclear@14: nuclear@14: #define kNumMaxDirectBits (31) nuclear@14: nuclear@14: #define kNumTopBits 24 nuclear@14: #define kTopValue ((UInt32)1 << kNumTopBits) nuclear@14: nuclear@14: #define kNumBitModelTotalBits 11 nuclear@14: #define kBitModelTotal (1 << kNumBitModelTotalBits) nuclear@14: #define kNumMoveBits 5 nuclear@14: #define kProbInitValue (kBitModelTotal >> 1) nuclear@14: nuclear@14: #define kNumMoveReducingBits 4 nuclear@14: #define kNumBitPriceShiftBits 4 nuclear@14: #define kBitPrice (1 << kNumBitPriceShiftBits) nuclear@14: nuclear@14: void LzmaEncProps_Init(CLzmaEncProps *p) nuclear@14: { nuclear@14: p->level = 5; nuclear@14: p->dictSize = p->mc = 0; nuclear@14: p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1; nuclear@14: p->writeEndMark = 0; nuclear@14: } nuclear@14: nuclear@14: void LzmaEncProps_Normalize(CLzmaEncProps *p) nuclear@14: { nuclear@14: int level = p->level; nuclear@14: if (level < 0) level = 5; nuclear@14: p->level = level; nuclear@14: if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26))); nuclear@14: if (p->lc < 0) p->lc = 3; nuclear@14: if (p->lp < 0) p->lp = 0; nuclear@14: if (p->pb < 0) p->pb = 2; nuclear@14: if (p->algo < 0) p->algo = (level < 5 ? 0 : 1); nuclear@14: if (p->fb < 0) p->fb = (level < 7 ? 32 : 64); nuclear@14: if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1); nuclear@14: if (p->numHashBytes < 0) p->numHashBytes = 4; nuclear@14: if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1); nuclear@14: if (p->numThreads < 0) nuclear@14: p->numThreads = nuclear@14: #ifdef COMPRESS_MF_MT nuclear@14: ((p->btMode && p->algo) ? 2 : 1); nuclear@14: #else nuclear@14: 1; nuclear@14: #endif nuclear@14: } nuclear@14: nuclear@14: UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2) nuclear@14: { nuclear@14: CLzmaEncProps props = *props2; nuclear@14: LzmaEncProps_Normalize(&props); nuclear@14: return props.dictSize; nuclear@14: } nuclear@14: nuclear@14: /* #define LZMA_LOG_BSR */ nuclear@14: /* Define it for Intel's CPU */ nuclear@14: nuclear@14: nuclear@14: #ifdef LZMA_LOG_BSR nuclear@14: nuclear@14: #define kDicLogSizeMaxCompress 30 nuclear@14: nuclear@14: #define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); } nuclear@14: nuclear@14: static UInt32 GetPosSlot1(UInt32 pos) nuclear@14: { nuclear@14: UInt32 res; nuclear@14: BSR2_RET(pos, res); nuclear@14: return res; nuclear@14: } nuclear@14: #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); } nuclear@14: #define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); } nuclear@14: nuclear@14: #else nuclear@14: nuclear@14: #define kNumLogBits (9 + (int)sizeof(size_t) / 2) nuclear@14: #define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7) nuclear@14: nuclear@14: void LzmaEnc_FastPosInit(Byte *g_FastPos) nuclear@14: { nuclear@14: int c = 2, slotFast; nuclear@14: g_FastPos[0] = 0; nuclear@14: g_FastPos[1] = 1; nuclear@14: nuclear@14: for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++) nuclear@14: { nuclear@14: UInt32 k = (1 << ((slotFast >> 1) - 1)); nuclear@14: UInt32 j; nuclear@14: for (j = 0; j < k; j++, c++) nuclear@14: g_FastPos[c] = (Byte)slotFast; nuclear@14: } nuclear@14: } nuclear@14: nuclear@14: #define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \ nuclear@14: (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \ nuclear@14: res = p->g_FastPos[pos >> i] + (i * 2); } nuclear@14: /* nuclear@14: #define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \ nuclear@14: p->g_FastPos[pos >> 6] + 12 : \ nuclear@14: p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; } nuclear@14: */ nuclear@14: nuclear@14: #define GetPosSlot1(pos) p->g_FastPos[pos] nuclear@14: #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); } nuclear@14: #define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); } nuclear@14: nuclear@14: #endif nuclear@14: nuclear@14: nuclear@14: #define LZMA_NUM_REPS 4 nuclear@14: nuclear@14: typedef unsigned CState; nuclear@14: nuclear@14: typedef struct _COptimal nuclear@14: { nuclear@14: UInt32 price; nuclear@14: nuclear@14: CState state; nuclear@14: int prev1IsChar; nuclear@14: int prev2; nuclear@14: nuclear@14: UInt32 posPrev2; nuclear@14: UInt32 backPrev2; nuclear@14: nuclear@14: UInt32 posPrev; nuclear@14: UInt32 backPrev; nuclear@14: UInt32 backs[LZMA_NUM_REPS]; nuclear@14: } COptimal; nuclear@14: nuclear@14: #define kNumOpts (1 << 12) nuclear@14: nuclear@14: #define kNumLenToPosStates 4 nuclear@14: #define kNumPosSlotBits 6 nuclear@14: #define kDicLogSizeMin 0 nuclear@14: #define kDicLogSizeMax 32 nuclear@14: #define kDistTableSizeMax (kDicLogSizeMax * 2) nuclear@14: nuclear@14: nuclear@14: #define kNumAlignBits 4 nuclear@14: #define kAlignTableSize (1 << kNumAlignBits) nuclear@14: #define kAlignMask (kAlignTableSize - 1) nuclear@14: nuclear@14: #define kStartPosModelIndex 4 nuclear@14: #define kEndPosModelIndex 14 nuclear@14: #define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex) nuclear@14: nuclear@14: #define kNumFullDistances (1 << (kEndPosModelIndex / 2)) nuclear@14: nuclear@14: #ifdef _LZMA_PROB32 nuclear@14: #define CLzmaProb UInt32 nuclear@14: #else nuclear@14: #define CLzmaProb UInt16 nuclear@14: #endif nuclear@14: nuclear@14: #define LZMA_PB_MAX 4 nuclear@14: #define LZMA_LC_MAX 8 nuclear@14: #define LZMA_LP_MAX 4 nuclear@14: nuclear@14: #define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX) nuclear@14: nuclear@14: nuclear@14: #define kLenNumLowBits 3 nuclear@14: #define kLenNumLowSymbols (1 << kLenNumLowBits) nuclear@14: #define kLenNumMidBits 3 nuclear@14: #define kLenNumMidSymbols (1 << kLenNumMidBits) nuclear@14: #define kLenNumHighBits 8 nuclear@14: #define kLenNumHighSymbols (1 << kLenNumHighBits) nuclear@14: nuclear@14: #define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols) nuclear@14: nuclear@14: #define LZMA_MATCH_LEN_MIN 2 nuclear@14: #define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1) nuclear@14: nuclear@14: #define kNumStates 12 nuclear@14: nuclear@14: typedef struct nuclear@14: { nuclear@14: CLzmaProb choice; nuclear@14: CLzmaProb choice2; nuclear@14: CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits]; nuclear@14: CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits]; nuclear@14: CLzmaProb high[kLenNumHighSymbols]; nuclear@14: } CLenEnc; nuclear@14: nuclear@14: typedef struct nuclear@14: { nuclear@14: CLenEnc p; nuclear@14: UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal]; nuclear@14: UInt32 tableSize; nuclear@14: UInt32 counters[LZMA_NUM_PB_STATES_MAX]; nuclear@14: } CLenPriceEnc; nuclear@14: nuclear@14: typedef struct _CRangeEnc nuclear@14: { nuclear@14: UInt32 range; nuclear@14: Byte cache; nuclear@14: UInt64 low; nuclear@14: UInt64 cacheSize; nuclear@14: Byte *buf; nuclear@14: Byte *bufLim; nuclear@14: Byte *bufBase; nuclear@14: ISeqOutStream *outStream; nuclear@14: UInt64 processed; nuclear@14: SRes res; nuclear@14: } CRangeEnc; nuclear@14: nuclear@14: typedef struct _CSeqInStreamBuf nuclear@14: { nuclear@14: ISeqInStream funcTable; nuclear@14: const Byte *data; nuclear@14: SizeT rem; nuclear@14: } CSeqInStreamBuf; nuclear@14: nuclear@14: static SRes MyRead(void *pp, void *data, size_t *size) nuclear@14: { nuclear@14: size_t curSize = *size; nuclear@14: CSeqInStreamBuf *p = (CSeqInStreamBuf *)pp; nuclear@14: if (p->rem < curSize) nuclear@14: curSize = p->rem; nuclear@14: memcpy(data, p->data, curSize); nuclear@14: p->rem -= curSize; nuclear@14: p->data += curSize; nuclear@14: *size = curSize; nuclear@14: return SZ_OK; nuclear@14: } nuclear@14: nuclear@14: typedef struct nuclear@14: { nuclear@14: CLzmaProb *litProbs; nuclear@14: nuclear@14: CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX]; nuclear@14: CLzmaProb isRep[kNumStates]; nuclear@14: CLzmaProb isRepG0[kNumStates]; nuclear@14: CLzmaProb isRepG1[kNumStates]; nuclear@14: CLzmaProb isRepG2[kNumStates]; nuclear@14: CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX]; nuclear@14: nuclear@14: CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits]; nuclear@14: CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex]; nuclear@14: CLzmaProb posAlignEncoder[1 << kNumAlignBits]; nuclear@14: nuclear@14: CLenPriceEnc lenEnc; nuclear@14: CLenPriceEnc repLenEnc; nuclear@14: nuclear@14: UInt32 reps[LZMA_NUM_REPS]; nuclear@14: UInt32 state; nuclear@14: } CSaveState; nuclear@14: nuclear@14: typedef struct _CLzmaEnc nuclear@14: { nuclear@14: IMatchFinder matchFinder; nuclear@14: void *matchFinderObj; nuclear@14: nuclear@14: #ifdef COMPRESS_MF_MT nuclear@14: Bool mtMode; nuclear@14: CMatchFinderMt matchFinderMt; nuclear@14: #endif nuclear@14: nuclear@14: CMatchFinder matchFinderBase; nuclear@14: nuclear@14: #ifdef COMPRESS_MF_MT nuclear@14: Byte pad[128]; nuclear@14: #endif nuclear@14: nuclear@14: UInt32 optimumEndIndex; nuclear@14: UInt32 optimumCurrentIndex; nuclear@14: nuclear@14: UInt32 longestMatchLength; nuclear@14: UInt32 numPairs; nuclear@14: UInt32 numAvail; nuclear@14: COptimal opt[kNumOpts]; nuclear@14: nuclear@14: #ifndef LZMA_LOG_BSR nuclear@14: Byte g_FastPos[1 << kNumLogBits]; nuclear@14: #endif nuclear@14: nuclear@14: UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits]; nuclear@14: UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1]; nuclear@14: UInt32 numFastBytes; nuclear@14: UInt32 additionalOffset; nuclear@14: UInt32 reps[LZMA_NUM_REPS]; nuclear@14: UInt32 state; nuclear@14: nuclear@14: UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax]; nuclear@14: UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances]; nuclear@14: UInt32 alignPrices[kAlignTableSize]; nuclear@14: UInt32 alignPriceCount; nuclear@14: nuclear@14: UInt32 distTableSize; nuclear@14: nuclear@14: unsigned lc, lp, pb; nuclear@14: unsigned lpMask, pbMask; nuclear@14: nuclear@14: CLzmaProb *litProbs; nuclear@14: nuclear@14: CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX]; nuclear@14: CLzmaProb isRep[kNumStates]; nuclear@14: CLzmaProb isRepG0[kNumStates]; nuclear@14: CLzmaProb isRepG1[kNumStates]; nuclear@14: CLzmaProb isRepG2[kNumStates]; nuclear@14: CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX]; nuclear@14: nuclear@14: CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits]; nuclear@14: CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex]; nuclear@14: CLzmaProb posAlignEncoder[1 << kNumAlignBits]; nuclear@14: nuclear@14: CLenPriceEnc lenEnc; nuclear@14: CLenPriceEnc repLenEnc; nuclear@14: nuclear@14: unsigned lclp; nuclear@14: nuclear@14: Bool fastMode; nuclear@14: nuclear@14: CRangeEnc rc; nuclear@14: nuclear@14: Bool writeEndMark; nuclear@14: UInt64 nowPos64; nuclear@14: UInt32 matchPriceCount; nuclear@14: Bool finished; nuclear@14: Bool multiThread; nuclear@14: nuclear@14: SRes result; nuclear@14: UInt32 dictSize; nuclear@14: UInt32 matchFinderCycles; nuclear@14: nuclear@14: ISeqInStream *inStream; nuclear@14: CSeqInStreamBuf seqBufInStream; nuclear@14: nuclear@14: CSaveState saveState; nuclear@14: } CLzmaEnc; nuclear@14: nuclear@14: void LzmaEnc_SaveState(CLzmaEncHandle pp) nuclear@14: { nuclear@14: CLzmaEnc *p = (CLzmaEnc *)pp; nuclear@14: CSaveState *dest = &p->saveState; nuclear@14: int i; nuclear@14: dest->lenEnc = p->lenEnc; nuclear@14: dest->repLenEnc = p->repLenEnc; nuclear@14: dest->state = p->state; nuclear@14: nuclear@14: for (i = 0; i < kNumStates; i++) nuclear@14: { nuclear@14: memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i])); nuclear@14: memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i])); nuclear@14: } nuclear@14: for (i = 0; i < kNumLenToPosStates; i++) nuclear@14: memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i])); nuclear@14: memcpy(dest->isRep, p->isRep, sizeof(p->isRep)); nuclear@14: memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0)); nuclear@14: memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1)); nuclear@14: memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2)); nuclear@14: memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders)); nuclear@14: memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder)); nuclear@14: memcpy(dest->reps, p->reps, sizeof(p->reps)); nuclear@14: memcpy(dest->litProbs, p->litProbs, (0x300 << p->lclp) * sizeof(CLzmaProb)); nuclear@14: } nuclear@14: nuclear@14: void LzmaEnc_RestoreState(CLzmaEncHandle pp) nuclear@14: { nuclear@14: CLzmaEnc *dest = (CLzmaEnc *)pp; nuclear@14: const CSaveState *p = &dest->saveState; nuclear@14: int i; nuclear@14: dest->lenEnc = p->lenEnc; nuclear@14: dest->repLenEnc = p->repLenEnc; nuclear@14: dest->state = p->state; nuclear@14: nuclear@14: for (i = 0; i < kNumStates; i++) nuclear@14: { nuclear@14: memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i])); nuclear@14: memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i])); nuclear@14: } nuclear@14: for (i = 0; i < kNumLenToPosStates; i++) nuclear@14: memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i])); nuclear@14: memcpy(dest->isRep, p->isRep, sizeof(p->isRep)); nuclear@14: memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0)); nuclear@14: memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1)); nuclear@14: memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2)); nuclear@14: memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders)); nuclear@14: memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder)); nuclear@14: memcpy(dest->reps, p->reps, sizeof(p->reps)); nuclear@14: memcpy(dest->litProbs, p->litProbs, (0x300 << dest->lclp) * sizeof(CLzmaProb)); nuclear@14: } nuclear@14: nuclear@14: SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2) nuclear@14: { nuclear@14: CLzmaEnc *p = (CLzmaEnc *)pp; nuclear@14: CLzmaEncProps props = *props2; nuclear@14: LzmaEncProps_Normalize(&props); nuclear@14: nuclear@14: if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX || nuclear@14: props.dictSize > (1U << kDicLogSizeMaxCompress) || props.dictSize > (1U << 30)) nuclear@14: return SZ_ERROR_PARAM; nuclear@14: p->dictSize = props.dictSize; nuclear@14: p->matchFinderCycles = props.mc; nuclear@14: { nuclear@14: unsigned fb = props.fb; nuclear@14: if (fb < 5) nuclear@14: fb = 5; nuclear@14: if (fb > LZMA_MATCH_LEN_MAX) nuclear@14: fb = LZMA_MATCH_LEN_MAX; nuclear@14: p->numFastBytes = fb; nuclear@14: } nuclear@14: p->lc = props.lc; nuclear@14: p->lp = props.lp; nuclear@14: p->pb = props.pb; nuclear@14: p->fastMode = (props.algo == 0); nuclear@14: p->matchFinderBase.btMode = props.btMode; nuclear@14: { nuclear@14: UInt32 numHashBytes = 4; nuclear@14: if (props.btMode) nuclear@14: { nuclear@14: if (props.numHashBytes < 2) nuclear@14: numHashBytes = 2; nuclear@14: else if (props.numHashBytes < 4) nuclear@14: numHashBytes = props.numHashBytes; nuclear@14: } nuclear@14: p->matchFinderBase.numHashBytes = numHashBytes; nuclear@14: } nuclear@14: nuclear@14: p->matchFinderBase.cutValue = props.mc; nuclear@14: nuclear@14: p->writeEndMark = props.writeEndMark; nuclear@14: nuclear@14: #ifdef COMPRESS_MF_MT nuclear@14: /* nuclear@14: if (newMultiThread != _multiThread) nuclear@14: { nuclear@14: ReleaseMatchFinder(); nuclear@14: _multiThread = newMultiThread; nuclear@14: } nuclear@14: */ nuclear@14: p->multiThread = (props.numThreads > 1); nuclear@14: #endif nuclear@14: nuclear@14: return SZ_OK; nuclear@14: } nuclear@14: nuclear@14: static const int kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5}; nuclear@14: static const int kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10}; nuclear@14: static const int kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11}; nuclear@14: static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11}; nuclear@14: nuclear@14: #define IsCharState(s) ((s) < 7) nuclear@14: nuclear@14: #define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1) nuclear@14: nuclear@14: #define kInfinityPrice (1 << 30) nuclear@14: nuclear@14: static void RangeEnc_Construct(CRangeEnc *p) nuclear@14: { nuclear@14: p->outStream = 0; nuclear@14: p->bufBase = 0; nuclear@14: } nuclear@14: nuclear@14: #define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize) nuclear@14: nuclear@14: #define RC_BUF_SIZE (1 << 16) nuclear@14: static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc) nuclear@14: { nuclear@14: if (p->bufBase == 0) nuclear@14: { nuclear@14: p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE); nuclear@14: if (p->bufBase == 0) nuclear@14: return 0; nuclear@14: p->bufLim = p->bufBase + RC_BUF_SIZE; nuclear@14: } nuclear@14: return 1; nuclear@14: } nuclear@14: nuclear@14: static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc) nuclear@14: { nuclear@14: alloc->Free(alloc, p->bufBase); nuclear@14: p->bufBase = 0; nuclear@14: } nuclear@14: nuclear@14: static void RangeEnc_Init(CRangeEnc *p) nuclear@14: { nuclear@14: /* Stream.Init(); */ nuclear@14: p->low = 0; nuclear@14: p->range = 0xFFFFFFFF; nuclear@14: p->cacheSize = 1; nuclear@14: p->cache = 0; nuclear@14: nuclear@14: p->buf = p->bufBase; nuclear@14: nuclear@14: p->processed = 0; nuclear@14: p->res = SZ_OK; nuclear@14: } nuclear@14: nuclear@14: static void RangeEnc_FlushStream(CRangeEnc *p) nuclear@14: { nuclear@14: size_t num; nuclear@14: if (p->res != SZ_OK) nuclear@14: return; nuclear@14: num = p->buf - p->bufBase; nuclear@14: if (num != p->outStream->Write(p->outStream, p->bufBase, num)) nuclear@14: p->res = SZ_ERROR_WRITE; nuclear@14: p->processed += num; nuclear@14: p->buf = p->bufBase; nuclear@14: } nuclear@14: nuclear@14: static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p) nuclear@14: { nuclear@14: if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0) nuclear@14: { nuclear@14: Byte temp = p->cache; nuclear@14: do nuclear@14: { nuclear@14: Byte *buf = p->buf; nuclear@14: *buf++ = (Byte)(temp + (Byte)(p->low >> 32)); nuclear@14: p->buf = buf; nuclear@14: if (buf == p->bufLim) nuclear@14: RangeEnc_FlushStream(p); nuclear@14: temp = 0xFF; nuclear@14: } nuclear@14: while (--p->cacheSize != 0); nuclear@14: p->cache = (Byte)((UInt32)p->low >> 24); nuclear@14: } nuclear@14: p->cacheSize++; nuclear@14: p->low = (UInt32)p->low << 8; nuclear@14: } nuclear@14: nuclear@14: static void RangeEnc_FlushData(CRangeEnc *p) nuclear@14: { nuclear@14: int i; nuclear@14: for (i = 0; i < 5; i++) nuclear@14: RangeEnc_ShiftLow(p); nuclear@14: } nuclear@14: nuclear@14: static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits) nuclear@14: { nuclear@14: do nuclear@14: { nuclear@14: p->range >>= 1; nuclear@14: p->low += p->range & (0 - ((value >> --numBits) & 1)); nuclear@14: if (p->range < kTopValue) nuclear@14: { nuclear@14: p->range <<= 8; nuclear@14: RangeEnc_ShiftLow(p); nuclear@14: } nuclear@14: } nuclear@14: while (numBits != 0); nuclear@14: } nuclear@14: nuclear@14: static void RangeEnc_EncodeBit(CRangeEnc *p, CLzmaProb *prob, UInt32 symbol) nuclear@14: { nuclear@14: UInt32 ttt = *prob; nuclear@14: UInt32 newBound = (p->range >> kNumBitModelTotalBits) * ttt; nuclear@14: if (symbol == 0) nuclear@14: { nuclear@14: p->range = newBound; nuclear@14: ttt += (kBitModelTotal - ttt) >> kNumMoveBits; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: p->low += newBound; nuclear@14: p->range -= newBound; nuclear@14: ttt -= ttt >> kNumMoveBits; nuclear@14: } nuclear@14: *prob = (CLzmaProb)ttt; nuclear@14: if (p->range < kTopValue) nuclear@14: { nuclear@14: p->range <<= 8; nuclear@14: RangeEnc_ShiftLow(p); nuclear@14: } nuclear@14: } nuclear@14: nuclear@14: static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol) nuclear@14: { nuclear@14: symbol |= 0x100; nuclear@14: do nuclear@14: { nuclear@14: RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1); nuclear@14: symbol <<= 1; nuclear@14: } nuclear@14: while (symbol < 0x10000); nuclear@14: } nuclear@14: nuclear@14: static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol, UInt32 matchByte) nuclear@14: { nuclear@14: UInt32 offs = 0x100; nuclear@14: symbol |= 0x100; nuclear@14: do nuclear@14: { nuclear@14: matchByte <<= 1; nuclear@14: RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1); nuclear@14: symbol <<= 1; nuclear@14: offs &= ~(matchByte ^ symbol); nuclear@14: } nuclear@14: while (symbol < 0x10000); nuclear@14: } nuclear@14: nuclear@14: void LzmaEnc_InitPriceTables(UInt32 *ProbPrices) nuclear@14: { nuclear@14: UInt32 i; nuclear@14: for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits)) nuclear@14: { nuclear@14: const int kCyclesBits = kNumBitPriceShiftBits; nuclear@14: UInt32 w = i; nuclear@14: UInt32 bitCount = 0; nuclear@14: int j; nuclear@14: for (j = 0; j < kCyclesBits; j++) nuclear@14: { nuclear@14: w = w * w; nuclear@14: bitCount <<= 1; nuclear@14: while (w >= ((UInt32)1 << 16)) nuclear@14: { nuclear@14: w >>= 1; nuclear@14: bitCount++; nuclear@14: } nuclear@14: } nuclear@14: ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount); nuclear@14: } nuclear@14: } nuclear@14: nuclear@14: nuclear@14: #define GET_PRICE(prob, symbol) \ nuclear@14: p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits]; nuclear@14: nuclear@14: #define GET_PRICEa(prob, symbol) \ nuclear@14: ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits]; nuclear@14: nuclear@14: #define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits] nuclear@14: #define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits] nuclear@14: nuclear@14: #define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits] nuclear@14: #define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits] nuclear@14: nuclear@14: static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *ProbPrices) nuclear@14: { nuclear@14: UInt32 price = 0; nuclear@14: symbol |= 0x100; nuclear@14: do nuclear@14: { nuclear@14: price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1); nuclear@14: symbol <<= 1; nuclear@14: } nuclear@14: while (symbol < 0x10000); nuclear@14: return price; nuclear@14: } nuclear@14: nuclear@14: static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, UInt32 *ProbPrices) nuclear@14: { nuclear@14: UInt32 price = 0; nuclear@14: UInt32 offs = 0x100; nuclear@14: symbol |= 0x100; nuclear@14: do nuclear@14: { nuclear@14: matchByte <<= 1; nuclear@14: price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1); nuclear@14: symbol <<= 1; nuclear@14: offs &= ~(matchByte ^ symbol); nuclear@14: } nuclear@14: while (symbol < 0x10000); nuclear@14: return price; nuclear@14: } nuclear@14: nuclear@14: nuclear@14: static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol) nuclear@14: { nuclear@14: UInt32 m = 1; nuclear@14: int i; nuclear@14: for (i = numBitLevels; i != 0;) nuclear@14: { nuclear@14: UInt32 bit; nuclear@14: i--; nuclear@14: bit = (symbol >> i) & 1; nuclear@14: RangeEnc_EncodeBit(rc, probs + m, bit); nuclear@14: m = (m << 1) | bit; nuclear@14: } nuclear@14: } nuclear@14: nuclear@14: static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol) nuclear@14: { nuclear@14: UInt32 m = 1; nuclear@14: int i; nuclear@14: for (i = 0; i < numBitLevels; i++) nuclear@14: { nuclear@14: UInt32 bit = symbol & 1; nuclear@14: RangeEnc_EncodeBit(rc, probs + m, bit); nuclear@14: m = (m << 1) | bit; nuclear@14: symbol >>= 1; nuclear@14: } nuclear@14: } nuclear@14: nuclear@14: static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices) nuclear@14: { nuclear@14: UInt32 price = 0; nuclear@14: symbol |= (1 << numBitLevels); nuclear@14: while (symbol != 1) nuclear@14: { nuclear@14: price += GET_PRICEa(probs[symbol >> 1], symbol & 1); nuclear@14: symbol >>= 1; nuclear@14: } nuclear@14: return price; nuclear@14: } nuclear@14: nuclear@14: static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices) nuclear@14: { nuclear@14: UInt32 price = 0; nuclear@14: UInt32 m = 1; nuclear@14: int i; nuclear@14: for (i = numBitLevels; i != 0; i--) nuclear@14: { nuclear@14: UInt32 bit = symbol & 1; nuclear@14: symbol >>= 1; nuclear@14: price += GET_PRICEa(probs[m], bit); nuclear@14: m = (m << 1) | bit; nuclear@14: } nuclear@14: return price; nuclear@14: } nuclear@14: nuclear@14: nuclear@14: static void LenEnc_Init(CLenEnc *p) nuclear@14: { nuclear@14: unsigned i; nuclear@14: p->choice = p->choice2 = kProbInitValue; nuclear@14: for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumLowBits); i++) nuclear@14: p->low[i] = kProbInitValue; nuclear@14: for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumMidBits); i++) nuclear@14: p->mid[i] = kProbInitValue; nuclear@14: for (i = 0; i < kLenNumHighSymbols; i++) nuclear@14: p->high[i] = kProbInitValue; nuclear@14: } nuclear@14: nuclear@14: static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState) nuclear@14: { nuclear@14: if (symbol < kLenNumLowSymbols) nuclear@14: { nuclear@14: RangeEnc_EncodeBit(rc, &p->choice, 0); nuclear@14: RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol); nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: RangeEnc_EncodeBit(rc, &p->choice, 1); nuclear@14: if (symbol < kLenNumLowSymbols + kLenNumMidSymbols) nuclear@14: { nuclear@14: RangeEnc_EncodeBit(rc, &p->choice2, 0); nuclear@14: RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols); nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: RangeEnc_EncodeBit(rc, &p->choice2, 1); nuclear@14: RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols); nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: nuclear@14: static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices) nuclear@14: { nuclear@14: UInt32 a0 = GET_PRICE_0a(p->choice); nuclear@14: UInt32 a1 = GET_PRICE_1a(p->choice); nuclear@14: UInt32 b0 = a1 + GET_PRICE_0a(p->choice2); nuclear@14: UInt32 b1 = a1 + GET_PRICE_1a(p->choice2); nuclear@14: UInt32 i = 0; nuclear@14: for (i = 0; i < kLenNumLowSymbols; i++) nuclear@14: { nuclear@14: if (i >= numSymbols) nuclear@14: return; nuclear@14: prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices); nuclear@14: } nuclear@14: for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++) nuclear@14: { nuclear@14: if (i >= numSymbols) nuclear@14: return; nuclear@14: prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices); nuclear@14: } nuclear@14: for (; i < numSymbols; i++) nuclear@14: prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices); nuclear@14: } nuclear@14: nuclear@14: static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices) nuclear@14: { nuclear@14: LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices); nuclear@14: p->counters[posState] = p->tableSize; nuclear@14: } nuclear@14: nuclear@14: static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices) nuclear@14: { nuclear@14: UInt32 posState; nuclear@14: for (posState = 0; posState < numPosStates; posState++) nuclear@14: LenPriceEnc_UpdateTable(p, posState, ProbPrices); nuclear@14: } nuclear@14: nuclear@14: static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices) nuclear@14: { nuclear@14: LenEnc_Encode(&p->p, rc, symbol, posState); nuclear@14: if (updatePrice) nuclear@14: if (--p->counters[posState] == 0) nuclear@14: LenPriceEnc_UpdateTable(p, posState, ProbPrices); nuclear@14: } nuclear@14: nuclear@14: nuclear@14: nuclear@14: nuclear@14: static void MovePos(CLzmaEnc *p, UInt32 num) nuclear@14: { nuclear@14: #ifdef SHOW_STAT nuclear@14: ttt += num; nuclear@14: printf("\n MovePos %d", num); nuclear@14: #endif nuclear@14: if (num != 0) nuclear@14: { nuclear@14: p->additionalOffset += num; nuclear@14: p->matchFinder.Skip(p->matchFinderObj, num); nuclear@14: } nuclear@14: } nuclear@14: nuclear@14: static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes) nuclear@14: { nuclear@14: UInt32 lenRes = 0, numPairs; nuclear@14: p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj); nuclear@14: numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches); nuclear@14: #ifdef SHOW_STAT nuclear@14: printf("\n i = %d numPairs = %d ", ttt, numPairs / 2); nuclear@14: ttt++; nuclear@14: { nuclear@14: UInt32 i; nuclear@14: for (i = 0; i < numPairs; i += 2) nuclear@14: printf("%2d %6d | ", p->matches[i], p->matches[i + 1]); nuclear@14: } nuclear@14: #endif nuclear@14: if (numPairs > 0) nuclear@14: { nuclear@14: lenRes = p->matches[numPairs - 2]; nuclear@14: if (lenRes == p->numFastBytes) nuclear@14: { nuclear@14: const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; nuclear@14: UInt32 distance = p->matches[numPairs - 1] + 1; nuclear@14: UInt32 numAvail = p->numAvail; nuclear@14: if (numAvail > LZMA_MATCH_LEN_MAX) nuclear@14: numAvail = LZMA_MATCH_LEN_MAX; nuclear@14: { nuclear@14: const Byte *pby2 = pby - distance; nuclear@14: for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++); nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: p->additionalOffset++; nuclear@14: *numDistancePairsRes = numPairs; nuclear@14: return lenRes; nuclear@14: } nuclear@14: nuclear@14: nuclear@14: #define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False; nuclear@14: #define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = False; nuclear@14: #define IsShortRep(p) ((p)->backPrev == 0) nuclear@14: nuclear@14: static UInt32 GetRepLen1Price(CLzmaEnc *p, UInt32 state, UInt32 posState) nuclear@14: { nuclear@14: return nuclear@14: GET_PRICE_0(p->isRepG0[state]) + nuclear@14: GET_PRICE_0(p->isRep0Long[state][posState]); nuclear@14: } nuclear@14: nuclear@14: static UInt32 GetPureRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 state, UInt32 posState) nuclear@14: { nuclear@14: UInt32 price; nuclear@14: if (repIndex == 0) nuclear@14: { nuclear@14: price = GET_PRICE_0(p->isRepG0[state]); nuclear@14: price += GET_PRICE_1(p->isRep0Long[state][posState]); nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: price = GET_PRICE_1(p->isRepG0[state]); nuclear@14: if (repIndex == 1) nuclear@14: price += GET_PRICE_0(p->isRepG1[state]); nuclear@14: else nuclear@14: { nuclear@14: price += GET_PRICE_1(p->isRepG1[state]); nuclear@14: price += GET_PRICE(p->isRepG2[state], repIndex - 2); nuclear@14: } nuclear@14: } nuclear@14: return price; nuclear@14: } nuclear@14: nuclear@14: static UInt32 GetRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 len, UInt32 state, UInt32 posState) nuclear@14: { nuclear@14: return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] + nuclear@14: GetPureRepPrice(p, repIndex, state, posState); nuclear@14: } nuclear@14: nuclear@14: static UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur) nuclear@14: { nuclear@14: UInt32 posMem = p->opt[cur].posPrev; nuclear@14: UInt32 backMem = p->opt[cur].backPrev; nuclear@14: p->optimumEndIndex = cur; nuclear@14: do nuclear@14: { nuclear@14: if (p->opt[cur].prev1IsChar) nuclear@14: { nuclear@14: MakeAsChar(&p->opt[posMem]) nuclear@14: p->opt[posMem].posPrev = posMem - 1; nuclear@14: if (p->opt[cur].prev2) nuclear@14: { nuclear@14: p->opt[posMem - 1].prev1IsChar = False; nuclear@14: p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2; nuclear@14: p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2; nuclear@14: } nuclear@14: } nuclear@14: { nuclear@14: UInt32 posPrev = posMem; nuclear@14: UInt32 backCur = backMem; nuclear@14: nuclear@14: backMem = p->opt[posPrev].backPrev; nuclear@14: posMem = p->opt[posPrev].posPrev; nuclear@14: nuclear@14: p->opt[posPrev].backPrev = backCur; nuclear@14: p->opt[posPrev].posPrev = cur; nuclear@14: cur = posPrev; nuclear@14: } nuclear@14: } nuclear@14: while (cur != 0); nuclear@14: *backRes = p->opt[0].backPrev; nuclear@14: p->optimumCurrentIndex = p->opt[0].posPrev; nuclear@14: return p->optimumCurrentIndex; nuclear@14: } nuclear@14: nuclear@14: #define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300) nuclear@14: nuclear@14: static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes) nuclear@14: { nuclear@14: UInt32 numAvail, mainLen, numPairs, repMaxIndex, i, posState, lenEnd, len, cur; nuclear@14: UInt32 matchPrice, repMatchPrice, normalMatchPrice; nuclear@14: UInt32 reps[LZMA_NUM_REPS], repLens[LZMA_NUM_REPS]; nuclear@14: UInt32 *matches; nuclear@14: const Byte *data; nuclear@14: Byte curByte, matchByte; nuclear@14: if (p->optimumEndIndex != p->optimumCurrentIndex) nuclear@14: { nuclear@14: const COptimal *opt = &p->opt[p->optimumCurrentIndex]; nuclear@14: UInt32 lenRes = opt->posPrev - p->optimumCurrentIndex; nuclear@14: *backRes = opt->backPrev; nuclear@14: p->optimumCurrentIndex = opt->posPrev; nuclear@14: return lenRes; nuclear@14: } nuclear@14: p->optimumCurrentIndex = p->optimumEndIndex = 0; nuclear@14: nuclear@14: if (p->additionalOffset == 0) nuclear@14: mainLen = ReadMatchDistances(p, &numPairs); nuclear@14: else nuclear@14: { nuclear@14: mainLen = p->longestMatchLength; nuclear@14: numPairs = p->numPairs; nuclear@14: } nuclear@14: nuclear@14: numAvail = p->numAvail; nuclear@14: if (numAvail < 2) nuclear@14: { nuclear@14: *backRes = (UInt32)(-1); nuclear@14: return 1; nuclear@14: } nuclear@14: if (numAvail > LZMA_MATCH_LEN_MAX) nuclear@14: numAvail = LZMA_MATCH_LEN_MAX; nuclear@14: nuclear@14: data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; nuclear@14: repMaxIndex = 0; nuclear@14: for (i = 0; i < LZMA_NUM_REPS; i++) nuclear@14: { nuclear@14: UInt32 lenTest; nuclear@14: const Byte *data2; nuclear@14: reps[i] = p->reps[i]; nuclear@14: data2 = data - (reps[i] + 1); nuclear@14: if (data[0] != data2[0] || data[1] != data2[1]) nuclear@14: { nuclear@14: repLens[i] = 0; nuclear@14: continue; nuclear@14: } nuclear@14: for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++); nuclear@14: repLens[i] = lenTest; nuclear@14: if (lenTest > repLens[repMaxIndex]) nuclear@14: repMaxIndex = i; nuclear@14: } nuclear@14: if (repLens[repMaxIndex] >= p->numFastBytes) nuclear@14: { nuclear@14: UInt32 lenRes; nuclear@14: *backRes = repMaxIndex; nuclear@14: lenRes = repLens[repMaxIndex]; nuclear@14: MovePos(p, lenRes - 1); nuclear@14: return lenRes; nuclear@14: } nuclear@14: nuclear@14: matches = p->matches; nuclear@14: if (mainLen >= p->numFastBytes) nuclear@14: { nuclear@14: *backRes = matches[numPairs - 1] + LZMA_NUM_REPS; nuclear@14: MovePos(p, mainLen - 1); nuclear@14: return mainLen; nuclear@14: } nuclear@14: curByte = *data; nuclear@14: matchByte = *(data - (reps[0] + 1)); nuclear@14: nuclear@14: if (mainLen < 2 && curByte != matchByte && repLens[repMaxIndex] < 2) nuclear@14: { nuclear@14: *backRes = (UInt32)-1; nuclear@14: return 1; nuclear@14: } nuclear@14: nuclear@14: p->opt[0].state = (CState)p->state; nuclear@14: nuclear@14: posState = (position & p->pbMask); nuclear@14: nuclear@14: { nuclear@14: const CLzmaProb *probs = LIT_PROBS(position, *(data - 1)); nuclear@14: p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) + nuclear@14: (!IsCharState(p->state) ? nuclear@14: LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) : nuclear@14: LitEnc_GetPrice(probs, curByte, p->ProbPrices)); nuclear@14: } nuclear@14: nuclear@14: MakeAsChar(&p->opt[1]); nuclear@14: nuclear@14: matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]); nuclear@14: repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]); nuclear@14: nuclear@14: if (matchByte == curByte) nuclear@14: { nuclear@14: UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState); nuclear@14: if (shortRepPrice < p->opt[1].price) nuclear@14: { nuclear@14: p->opt[1].price = shortRepPrice; nuclear@14: MakeAsShortRep(&p->opt[1]); nuclear@14: } nuclear@14: } nuclear@14: lenEnd = ((mainLen >= repLens[repMaxIndex]) ? mainLen : repLens[repMaxIndex]); nuclear@14: nuclear@14: if (lenEnd < 2) nuclear@14: { nuclear@14: *backRes = p->opt[1].backPrev; nuclear@14: return 1; nuclear@14: } nuclear@14: nuclear@14: p->opt[1].posPrev = 0; nuclear@14: for (i = 0; i < LZMA_NUM_REPS; i++) nuclear@14: p->opt[0].backs[i] = reps[i]; nuclear@14: nuclear@14: len = lenEnd; nuclear@14: do nuclear@14: p->opt[len--].price = kInfinityPrice; nuclear@14: while (len >= 2); nuclear@14: nuclear@14: for (i = 0; i < LZMA_NUM_REPS; i++) nuclear@14: { nuclear@14: UInt32 repLen = repLens[i]; nuclear@14: UInt32 price; nuclear@14: if (repLen < 2) nuclear@14: continue; nuclear@14: price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState); nuclear@14: do nuclear@14: { nuclear@14: UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2]; nuclear@14: COptimal *opt = &p->opt[repLen]; nuclear@14: if (curAndLenPrice < opt->price) nuclear@14: { nuclear@14: opt->price = curAndLenPrice; nuclear@14: opt->posPrev = 0; nuclear@14: opt->backPrev = i; nuclear@14: opt->prev1IsChar = False; nuclear@14: } nuclear@14: } nuclear@14: while (--repLen >= 2); nuclear@14: } nuclear@14: nuclear@14: normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]); nuclear@14: nuclear@14: len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2); nuclear@14: if (len <= mainLen) nuclear@14: { nuclear@14: UInt32 offs = 0; nuclear@14: while (len > matches[offs]) nuclear@14: offs += 2; nuclear@14: for (; ; len++) nuclear@14: { nuclear@14: COptimal *opt; nuclear@14: UInt32 distance = matches[offs + 1]; nuclear@14: nuclear@14: UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN]; nuclear@14: UInt32 lenToPosState = GetLenToPosState(len); nuclear@14: if (distance < kNumFullDistances) nuclear@14: curAndLenPrice += p->distancesPrices[lenToPosState][distance]; nuclear@14: else nuclear@14: { nuclear@14: UInt32 slot; nuclear@14: GetPosSlot2(distance, slot); nuclear@14: curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot]; nuclear@14: } nuclear@14: opt = &p->opt[len]; nuclear@14: if (curAndLenPrice < opt->price) nuclear@14: { nuclear@14: opt->price = curAndLenPrice; nuclear@14: opt->posPrev = 0; nuclear@14: opt->backPrev = distance + LZMA_NUM_REPS; nuclear@14: opt->prev1IsChar = False; nuclear@14: } nuclear@14: if (len == matches[offs]) nuclear@14: { nuclear@14: offs += 2; nuclear@14: if (offs == numPairs) nuclear@14: break; nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: nuclear@14: cur = 0; nuclear@14: nuclear@14: #ifdef SHOW_STAT2 nuclear@14: if (position >= 0) nuclear@14: { nuclear@14: unsigned i; nuclear@14: printf("\n pos = %4X", position); nuclear@14: for (i = cur; i <= lenEnd; i++) nuclear@14: printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price); nuclear@14: } nuclear@14: #endif nuclear@14: nuclear@14: for (;;) nuclear@14: { nuclear@14: UInt32 numAvailFull, newLen, numPairs, posPrev, state, posState, startLen; nuclear@14: UInt32 curPrice, curAnd1Price, matchPrice, repMatchPrice; nuclear@14: Bool nextIsChar; nuclear@14: Byte curByte, matchByte; nuclear@14: const Byte *data; nuclear@14: COptimal *curOpt; nuclear@14: COptimal *nextOpt; nuclear@14: nuclear@14: cur++; nuclear@14: if (cur == lenEnd) nuclear@14: return Backward(p, backRes, cur); nuclear@14: nuclear@14: newLen = ReadMatchDistances(p, &numPairs); nuclear@14: if (newLen >= p->numFastBytes) nuclear@14: { nuclear@14: p->numPairs = numPairs; nuclear@14: p->longestMatchLength = newLen; nuclear@14: return Backward(p, backRes, cur); nuclear@14: } nuclear@14: position++; nuclear@14: curOpt = &p->opt[cur]; nuclear@14: posPrev = curOpt->posPrev; nuclear@14: if (curOpt->prev1IsChar) nuclear@14: { nuclear@14: posPrev--; nuclear@14: if (curOpt->prev2) nuclear@14: { nuclear@14: state = p->opt[curOpt->posPrev2].state; nuclear@14: if (curOpt->backPrev2 < LZMA_NUM_REPS) nuclear@14: state = kRepNextStates[state]; nuclear@14: else nuclear@14: state = kMatchNextStates[state]; nuclear@14: } nuclear@14: else nuclear@14: state = p->opt[posPrev].state; nuclear@14: state = kLiteralNextStates[state]; nuclear@14: } nuclear@14: else nuclear@14: state = p->opt[posPrev].state; nuclear@14: if (posPrev == cur - 1) nuclear@14: { nuclear@14: if (IsShortRep(curOpt)) nuclear@14: state = kShortRepNextStates[state]; nuclear@14: else nuclear@14: state = kLiteralNextStates[state]; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: UInt32 pos; nuclear@14: const COptimal *prevOpt; nuclear@14: if (curOpt->prev1IsChar && curOpt->prev2) nuclear@14: { nuclear@14: posPrev = curOpt->posPrev2; nuclear@14: pos = curOpt->backPrev2; nuclear@14: state = kRepNextStates[state]; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: pos = curOpt->backPrev; nuclear@14: if (pos < LZMA_NUM_REPS) nuclear@14: state = kRepNextStates[state]; nuclear@14: else nuclear@14: state = kMatchNextStates[state]; nuclear@14: } nuclear@14: prevOpt = &p->opt[posPrev]; nuclear@14: if (pos < LZMA_NUM_REPS) nuclear@14: { nuclear@14: UInt32 i; nuclear@14: reps[0] = prevOpt->backs[pos]; nuclear@14: for (i = 1; i <= pos; i++) nuclear@14: reps[i] = prevOpt->backs[i - 1]; nuclear@14: for (; i < LZMA_NUM_REPS; i++) nuclear@14: reps[i] = prevOpt->backs[i]; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: UInt32 i; nuclear@14: reps[0] = (pos - LZMA_NUM_REPS); nuclear@14: for (i = 1; i < LZMA_NUM_REPS; i++) nuclear@14: reps[i] = prevOpt->backs[i - 1]; nuclear@14: } nuclear@14: } nuclear@14: curOpt->state = (CState)state; nuclear@14: nuclear@14: curOpt->backs[0] = reps[0]; nuclear@14: curOpt->backs[1] = reps[1]; nuclear@14: curOpt->backs[2] = reps[2]; nuclear@14: curOpt->backs[3] = reps[3]; nuclear@14: nuclear@14: curPrice = curOpt->price; nuclear@14: nextIsChar = False; nuclear@14: data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; nuclear@14: curByte = *data; nuclear@14: matchByte = *(data - (reps[0] + 1)); nuclear@14: nuclear@14: posState = (position & p->pbMask); nuclear@14: nuclear@14: curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]); nuclear@14: { nuclear@14: const CLzmaProb *probs = LIT_PROBS(position, *(data - 1)); nuclear@14: curAnd1Price += nuclear@14: (!IsCharState(state) ? nuclear@14: LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) : nuclear@14: LitEnc_GetPrice(probs, curByte, p->ProbPrices)); nuclear@14: } nuclear@14: nuclear@14: nextOpt = &p->opt[cur + 1]; nuclear@14: nuclear@14: if (curAnd1Price < nextOpt->price) nuclear@14: { nuclear@14: nextOpt->price = curAnd1Price; nuclear@14: nextOpt->posPrev = cur; nuclear@14: MakeAsChar(nextOpt); nuclear@14: nextIsChar = True; nuclear@14: } nuclear@14: nuclear@14: matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]); nuclear@14: repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]); nuclear@14: nuclear@14: if (matchByte == curByte && !(nextOpt->posPrev < cur && nextOpt->backPrev == 0)) nuclear@14: { nuclear@14: UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState); nuclear@14: if (shortRepPrice <= nextOpt->price) nuclear@14: { nuclear@14: nextOpt->price = shortRepPrice; nuclear@14: nextOpt->posPrev = cur; nuclear@14: MakeAsShortRep(nextOpt); nuclear@14: nextIsChar = True; nuclear@14: } nuclear@14: } nuclear@14: numAvailFull = p->numAvail; nuclear@14: { nuclear@14: UInt32 temp = kNumOpts - 1 - cur; nuclear@14: if (temp < numAvailFull) nuclear@14: numAvailFull = temp; nuclear@14: } nuclear@14: nuclear@14: if (numAvailFull < 2) nuclear@14: continue; nuclear@14: numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes); nuclear@14: nuclear@14: if (!nextIsChar && matchByte != curByte) /* speed optimization */ nuclear@14: { nuclear@14: /* try Literal + rep0 */ nuclear@14: UInt32 temp; nuclear@14: UInt32 lenTest2; nuclear@14: const Byte *data2 = data - (reps[0] + 1); nuclear@14: UInt32 limit = p->numFastBytes + 1; nuclear@14: if (limit > numAvailFull) nuclear@14: limit = numAvailFull; nuclear@14: nuclear@14: for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++); nuclear@14: lenTest2 = temp - 1; nuclear@14: if (lenTest2 >= 2) nuclear@14: { nuclear@14: UInt32 state2 = kLiteralNextStates[state]; nuclear@14: UInt32 posStateNext = (position + 1) & p->pbMask; nuclear@14: UInt32 nextRepMatchPrice = curAnd1Price + nuclear@14: GET_PRICE_1(p->isMatch[state2][posStateNext]) + nuclear@14: GET_PRICE_1(p->isRep[state2]); nuclear@14: /* for (; lenTest2 >= 2; lenTest2--) */ nuclear@14: { nuclear@14: UInt32 curAndLenPrice; nuclear@14: COptimal *opt; nuclear@14: UInt32 offset = cur + 1 + lenTest2; nuclear@14: while (lenEnd < offset) nuclear@14: p->opt[++lenEnd].price = kInfinityPrice; nuclear@14: curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext); nuclear@14: opt = &p->opt[offset]; nuclear@14: if (curAndLenPrice < opt->price) nuclear@14: { nuclear@14: opt->price = curAndLenPrice; nuclear@14: opt->posPrev = cur + 1; nuclear@14: opt->backPrev = 0; nuclear@14: opt->prev1IsChar = True; nuclear@14: opt->prev2 = False; nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: nuclear@14: startLen = 2; /* speed optimization */ nuclear@14: { nuclear@14: UInt32 repIndex; nuclear@14: for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++) nuclear@14: { nuclear@14: UInt32 lenTest; nuclear@14: UInt32 lenTestTemp; nuclear@14: UInt32 price; nuclear@14: const Byte *data2 = data - (reps[repIndex] + 1); nuclear@14: if (data[0] != data2[0] || data[1] != data2[1]) nuclear@14: continue; nuclear@14: for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++); nuclear@14: while (lenEnd < cur + lenTest) nuclear@14: p->opt[++lenEnd].price = kInfinityPrice; nuclear@14: lenTestTemp = lenTest; nuclear@14: price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState); nuclear@14: do nuclear@14: { nuclear@14: UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2]; nuclear@14: COptimal *opt = &p->opt[cur + lenTest]; nuclear@14: if (curAndLenPrice < opt->price) nuclear@14: { nuclear@14: opt->price = curAndLenPrice; nuclear@14: opt->posPrev = cur; nuclear@14: opt->backPrev = repIndex; nuclear@14: opt->prev1IsChar = False; nuclear@14: } nuclear@14: } nuclear@14: while (--lenTest >= 2); nuclear@14: lenTest = lenTestTemp; nuclear@14: nuclear@14: if (repIndex == 0) nuclear@14: startLen = lenTest + 1; nuclear@14: nuclear@14: /* if (_maxMode) */ nuclear@14: { nuclear@14: UInt32 lenTest2 = lenTest + 1; nuclear@14: UInt32 limit = lenTest2 + p->numFastBytes; nuclear@14: UInt32 nextRepMatchPrice; nuclear@14: if (limit > numAvailFull) nuclear@14: limit = numAvailFull; nuclear@14: for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++); nuclear@14: lenTest2 -= lenTest + 1; nuclear@14: if (lenTest2 >= 2) nuclear@14: { nuclear@14: UInt32 state2 = kRepNextStates[state]; nuclear@14: UInt32 posStateNext = (position + lenTest) & p->pbMask; nuclear@14: UInt32 curAndLenCharPrice = nuclear@14: price + p->repLenEnc.prices[posState][lenTest - 2] + nuclear@14: GET_PRICE_0(p->isMatch[state2][posStateNext]) + nuclear@14: LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]), nuclear@14: data[lenTest], data2[lenTest], p->ProbPrices); nuclear@14: state2 = kLiteralNextStates[state2]; nuclear@14: posStateNext = (position + lenTest + 1) & p->pbMask; nuclear@14: nextRepMatchPrice = curAndLenCharPrice + nuclear@14: GET_PRICE_1(p->isMatch[state2][posStateNext]) + nuclear@14: GET_PRICE_1(p->isRep[state2]); nuclear@14: nuclear@14: /* for (; lenTest2 >= 2; lenTest2--) */ nuclear@14: { nuclear@14: UInt32 curAndLenPrice; nuclear@14: COptimal *opt; nuclear@14: UInt32 offset = cur + lenTest + 1 + lenTest2; nuclear@14: while (lenEnd < offset) nuclear@14: p->opt[++lenEnd].price = kInfinityPrice; nuclear@14: curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext); nuclear@14: opt = &p->opt[offset]; nuclear@14: if (curAndLenPrice < opt->price) nuclear@14: { nuclear@14: opt->price = curAndLenPrice; nuclear@14: opt->posPrev = cur + lenTest + 1; nuclear@14: opt->backPrev = 0; nuclear@14: opt->prev1IsChar = True; nuclear@14: opt->prev2 = True; nuclear@14: opt->posPrev2 = cur; nuclear@14: opt->backPrev2 = repIndex; nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: /* for (UInt32 lenTest = 2; lenTest <= newLen; lenTest++) */ nuclear@14: if (newLen > numAvail) nuclear@14: { nuclear@14: newLen = numAvail; nuclear@14: for (numPairs = 0; newLen > matches[numPairs]; numPairs += 2); nuclear@14: matches[numPairs] = newLen; nuclear@14: numPairs += 2; nuclear@14: } nuclear@14: if (newLen >= startLen) nuclear@14: { nuclear@14: UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]); nuclear@14: UInt32 offs, curBack, posSlot; nuclear@14: UInt32 lenTest; nuclear@14: while (lenEnd < cur + newLen) nuclear@14: p->opt[++lenEnd].price = kInfinityPrice; nuclear@14: nuclear@14: offs = 0; nuclear@14: while (startLen > matches[offs]) nuclear@14: offs += 2; nuclear@14: curBack = matches[offs + 1]; nuclear@14: GetPosSlot2(curBack, posSlot); nuclear@14: for (lenTest = /*2*/ startLen; ; lenTest++) nuclear@14: { nuclear@14: UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN]; nuclear@14: UInt32 lenToPosState = GetLenToPosState(lenTest); nuclear@14: COptimal *opt; nuclear@14: if (curBack < kNumFullDistances) nuclear@14: curAndLenPrice += p->distancesPrices[lenToPosState][curBack]; nuclear@14: else nuclear@14: curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask]; nuclear@14: nuclear@14: opt = &p->opt[cur + lenTest]; nuclear@14: if (curAndLenPrice < opt->price) nuclear@14: { nuclear@14: opt->price = curAndLenPrice; nuclear@14: opt->posPrev = cur; nuclear@14: opt->backPrev = curBack + LZMA_NUM_REPS; nuclear@14: opt->prev1IsChar = False; nuclear@14: } nuclear@14: nuclear@14: if (/*_maxMode && */lenTest == matches[offs]) nuclear@14: { nuclear@14: /* Try Match + Literal + Rep0 */ nuclear@14: const Byte *data2 = data - (curBack + 1); nuclear@14: UInt32 lenTest2 = lenTest + 1; nuclear@14: UInt32 limit = lenTest2 + p->numFastBytes; nuclear@14: UInt32 nextRepMatchPrice; nuclear@14: if (limit > numAvailFull) nuclear@14: limit = numAvailFull; nuclear@14: for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++); nuclear@14: lenTest2 -= lenTest + 1; nuclear@14: if (lenTest2 >= 2) nuclear@14: { nuclear@14: UInt32 state2 = kMatchNextStates[state]; nuclear@14: UInt32 posStateNext = (position + lenTest) & p->pbMask; nuclear@14: UInt32 curAndLenCharPrice = curAndLenPrice + nuclear@14: GET_PRICE_0(p->isMatch[state2][posStateNext]) + nuclear@14: LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]), nuclear@14: data[lenTest], data2[lenTest], p->ProbPrices); nuclear@14: state2 = kLiteralNextStates[state2]; nuclear@14: posStateNext = (posStateNext + 1) & p->pbMask; nuclear@14: nextRepMatchPrice = curAndLenCharPrice + nuclear@14: GET_PRICE_1(p->isMatch[state2][posStateNext]) + nuclear@14: GET_PRICE_1(p->isRep[state2]); nuclear@14: nuclear@14: /* for (; lenTest2 >= 2; lenTest2--) */ nuclear@14: { nuclear@14: UInt32 offset = cur + lenTest + 1 + lenTest2; nuclear@14: UInt32 curAndLenPrice; nuclear@14: COptimal *opt; nuclear@14: while (lenEnd < offset) nuclear@14: p->opt[++lenEnd].price = kInfinityPrice; nuclear@14: curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext); nuclear@14: opt = &p->opt[offset]; nuclear@14: if (curAndLenPrice < opt->price) nuclear@14: { nuclear@14: opt->price = curAndLenPrice; nuclear@14: opt->posPrev = cur + lenTest + 1; nuclear@14: opt->backPrev = 0; nuclear@14: opt->prev1IsChar = True; nuclear@14: opt->prev2 = True; nuclear@14: opt->posPrev2 = cur; nuclear@14: opt->backPrev2 = curBack + LZMA_NUM_REPS; nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: offs += 2; nuclear@14: if (offs == numPairs) nuclear@14: break; nuclear@14: curBack = matches[offs + 1]; nuclear@14: if (curBack >= kNumFullDistances) nuclear@14: GetPosSlot2(curBack, posSlot); nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: nuclear@14: #define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist)) nuclear@14: nuclear@14: static UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes) nuclear@14: { nuclear@14: UInt32 numAvail, mainLen, mainDist, numPairs, repIndex, repLen, i; nuclear@14: const Byte *data; nuclear@14: const UInt32 *matches; nuclear@14: nuclear@14: if (p->additionalOffset == 0) nuclear@14: mainLen = ReadMatchDistances(p, &numPairs); nuclear@14: else nuclear@14: { nuclear@14: mainLen = p->longestMatchLength; nuclear@14: numPairs = p->numPairs; nuclear@14: } nuclear@14: nuclear@14: numAvail = p->numAvail; nuclear@14: *backRes = (UInt32)-1; nuclear@14: if (numAvail < 2) nuclear@14: return 1; nuclear@14: if (numAvail > LZMA_MATCH_LEN_MAX) nuclear@14: numAvail = LZMA_MATCH_LEN_MAX; nuclear@14: data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; nuclear@14: nuclear@14: repLen = repIndex = 0; nuclear@14: for (i = 0; i < LZMA_NUM_REPS; i++) nuclear@14: { nuclear@14: UInt32 len; nuclear@14: const Byte *data2 = data - (p->reps[i] + 1); nuclear@14: if (data[0] != data2[0] || data[1] != data2[1]) nuclear@14: continue; nuclear@14: for (len = 2; len < numAvail && data[len] == data2[len]; len++); nuclear@14: if (len >= p->numFastBytes) nuclear@14: { nuclear@14: *backRes = i; nuclear@14: MovePos(p, len - 1); nuclear@14: return len; nuclear@14: } nuclear@14: if (len > repLen) nuclear@14: { nuclear@14: repIndex = i; nuclear@14: repLen = len; nuclear@14: } nuclear@14: } nuclear@14: nuclear@14: matches = p->matches; nuclear@14: if (mainLen >= p->numFastBytes) nuclear@14: { nuclear@14: *backRes = matches[numPairs - 1] + LZMA_NUM_REPS; nuclear@14: MovePos(p, mainLen - 1); nuclear@14: return mainLen; nuclear@14: } nuclear@14: nuclear@14: mainDist = 0; /* for GCC */ nuclear@14: if (mainLen >= 2) nuclear@14: { nuclear@14: mainDist = matches[numPairs - 1]; nuclear@14: while (numPairs > 2 && mainLen == matches[numPairs - 4] + 1) nuclear@14: { nuclear@14: if (!ChangePair(matches[numPairs - 3], mainDist)) nuclear@14: break; nuclear@14: numPairs -= 2; nuclear@14: mainLen = matches[numPairs - 2]; nuclear@14: mainDist = matches[numPairs - 1]; nuclear@14: } nuclear@14: if (mainLen == 2 && mainDist >= 0x80) nuclear@14: mainLen = 1; nuclear@14: } nuclear@14: nuclear@14: if (repLen >= 2 && ( nuclear@14: (repLen + 1 >= mainLen) || nuclear@14: (repLen + 2 >= mainLen && mainDist >= (1 << 9)) || nuclear@14: (repLen + 3 >= mainLen && mainDist >= (1 << 15)))) nuclear@14: { nuclear@14: *backRes = repIndex; nuclear@14: MovePos(p, repLen - 1); nuclear@14: return repLen; nuclear@14: } nuclear@14: nuclear@14: if (mainLen < 2 || numAvail <= 2) nuclear@14: return 1; nuclear@14: nuclear@14: p->longestMatchLength = ReadMatchDistances(p, &p->numPairs); nuclear@14: if (p->longestMatchLength >= 2) nuclear@14: { nuclear@14: UInt32 newDistance = matches[p->numPairs - 1]; nuclear@14: if ((p->longestMatchLength >= mainLen && newDistance < mainDist) || nuclear@14: (p->longestMatchLength == mainLen + 1 && !ChangePair(mainDist, newDistance)) || nuclear@14: (p->longestMatchLength > mainLen + 1) || nuclear@14: (p->longestMatchLength + 1 >= mainLen && mainLen >= 3 && ChangePair(newDistance, mainDist))) nuclear@14: return 1; nuclear@14: } nuclear@14: nuclear@14: data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; nuclear@14: for (i = 0; i < LZMA_NUM_REPS; i++) nuclear@14: { nuclear@14: UInt32 len, limit; nuclear@14: const Byte *data2 = data - (p->reps[i] + 1); nuclear@14: if (data[0] != data2[0] || data[1] != data2[1]) nuclear@14: continue; nuclear@14: limit = mainLen - 1; nuclear@14: for (len = 2; len < limit && data[len] == data2[len]; len++); nuclear@14: if (len >= limit) nuclear@14: return 1; nuclear@14: } nuclear@14: *backRes = mainDist + LZMA_NUM_REPS; nuclear@14: MovePos(p, mainLen - 2); nuclear@14: return mainLen; nuclear@14: } nuclear@14: nuclear@14: static void WriteEndMarker(CLzmaEnc *p, UInt32 posState) nuclear@14: { nuclear@14: UInt32 len; nuclear@14: RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1); nuclear@14: RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0); nuclear@14: p->state = kMatchNextStates[p->state]; nuclear@14: len = LZMA_MATCH_LEN_MIN; nuclear@14: LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices); nuclear@14: RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1); nuclear@14: RangeEnc_EncodeDirectBits(&p->rc, (((UInt32)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits); nuclear@14: RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask); nuclear@14: } nuclear@14: nuclear@14: static SRes CheckErrors(CLzmaEnc *p) nuclear@14: { nuclear@14: if (p->result != SZ_OK) nuclear@14: return p->result; nuclear@14: if (p->rc.res != SZ_OK) nuclear@14: p->result = SZ_ERROR_WRITE; nuclear@14: if (p->matchFinderBase.result != SZ_OK) nuclear@14: p->result = SZ_ERROR_READ; nuclear@14: if (p->result != SZ_OK) nuclear@14: p->finished = True; nuclear@14: return p->result; nuclear@14: } nuclear@14: nuclear@14: static SRes Flush(CLzmaEnc *p, UInt32 nowPos) nuclear@14: { nuclear@14: /* ReleaseMFStream(); */ nuclear@14: p->finished = True; nuclear@14: if (p->writeEndMark) nuclear@14: WriteEndMarker(p, nowPos & p->pbMask); nuclear@14: RangeEnc_FlushData(&p->rc); nuclear@14: RangeEnc_FlushStream(&p->rc); nuclear@14: return CheckErrors(p); nuclear@14: } nuclear@14: nuclear@14: static void FillAlignPrices(CLzmaEnc *p) nuclear@14: { nuclear@14: UInt32 i; nuclear@14: for (i = 0; i < kAlignTableSize; i++) nuclear@14: p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices); nuclear@14: p->alignPriceCount = 0; nuclear@14: } nuclear@14: nuclear@14: static void FillDistancesPrices(CLzmaEnc *p) nuclear@14: { nuclear@14: UInt32 tempPrices[kNumFullDistances]; nuclear@14: UInt32 i, lenToPosState; nuclear@14: for (i = kStartPosModelIndex; i < kNumFullDistances; i++) nuclear@14: { nuclear@14: UInt32 posSlot = GetPosSlot1(i); nuclear@14: UInt32 footerBits = ((posSlot >> 1) - 1); nuclear@14: UInt32 base = ((2 | (posSlot & 1)) << footerBits); nuclear@14: tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices); nuclear@14: } nuclear@14: nuclear@14: for (lenToPosState = 0; lenToPosState < kNumLenToPosStates; lenToPosState++) nuclear@14: { nuclear@14: UInt32 posSlot; nuclear@14: const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState]; nuclear@14: UInt32 *posSlotPrices = p->posSlotPrices[lenToPosState]; nuclear@14: for (posSlot = 0; posSlot < p->distTableSize; posSlot++) nuclear@14: posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices); nuclear@14: for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++) nuclear@14: posSlotPrices[posSlot] += ((((posSlot >> 1) - 1) - kNumAlignBits) << kNumBitPriceShiftBits); nuclear@14: nuclear@14: { nuclear@14: UInt32 *distancesPrices = p->distancesPrices[lenToPosState]; nuclear@14: UInt32 i; nuclear@14: for (i = 0; i < kStartPosModelIndex; i++) nuclear@14: distancesPrices[i] = posSlotPrices[i]; nuclear@14: for (; i < kNumFullDistances; i++) nuclear@14: distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i]; nuclear@14: } nuclear@14: } nuclear@14: p->matchPriceCount = 0; nuclear@14: } nuclear@14: nuclear@14: void LzmaEnc_Construct(CLzmaEnc *p) nuclear@14: { nuclear@14: RangeEnc_Construct(&p->rc); nuclear@14: MatchFinder_Construct(&p->matchFinderBase); nuclear@14: #ifdef COMPRESS_MF_MT nuclear@14: MatchFinderMt_Construct(&p->matchFinderMt); nuclear@14: p->matchFinderMt.MatchFinder = &p->matchFinderBase; nuclear@14: #endif nuclear@14: nuclear@14: { nuclear@14: CLzmaEncProps props; nuclear@14: LzmaEncProps_Init(&props); nuclear@14: LzmaEnc_SetProps(p, &props); nuclear@14: } nuclear@14: nuclear@14: #ifndef LZMA_LOG_BSR nuclear@14: LzmaEnc_FastPosInit(p->g_FastPos); nuclear@14: #endif nuclear@14: nuclear@14: LzmaEnc_InitPriceTables(p->ProbPrices); nuclear@14: p->litProbs = 0; nuclear@14: p->saveState.litProbs = 0; nuclear@14: } nuclear@14: nuclear@14: CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc) nuclear@14: { nuclear@14: void *p; nuclear@14: p = alloc->Alloc(alloc, sizeof(CLzmaEnc)); nuclear@14: if (p != 0) nuclear@14: LzmaEnc_Construct((CLzmaEnc *)p); nuclear@14: return p; nuclear@14: } nuclear@14: nuclear@14: void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc) nuclear@14: { nuclear@14: alloc->Free(alloc, p->litProbs); nuclear@14: alloc->Free(alloc, p->saveState.litProbs); nuclear@14: p->litProbs = 0; nuclear@14: p->saveState.litProbs = 0; nuclear@14: } nuclear@14: nuclear@14: void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig) nuclear@14: { nuclear@14: #ifdef COMPRESS_MF_MT nuclear@14: MatchFinderMt_Destruct(&p->matchFinderMt, allocBig); nuclear@14: #endif nuclear@14: MatchFinder_Free(&p->matchFinderBase, allocBig); nuclear@14: LzmaEnc_FreeLits(p, alloc); nuclear@14: RangeEnc_Free(&p->rc, alloc); nuclear@14: } nuclear@14: nuclear@14: void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig) nuclear@14: { nuclear@14: LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig); nuclear@14: alloc->Free(alloc, p); nuclear@14: } nuclear@14: nuclear@14: static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize) nuclear@14: { nuclear@14: UInt32 nowPos32, startPos32; nuclear@14: if (p->inStream != 0) nuclear@14: { nuclear@14: p->matchFinderBase.stream = p->inStream; nuclear@14: p->matchFinder.Init(p->matchFinderObj); nuclear@14: p->inStream = 0; nuclear@14: } nuclear@14: nuclear@14: if (p->finished) nuclear@14: return p->result; nuclear@14: RINOK(CheckErrors(p)); nuclear@14: nuclear@14: nowPos32 = (UInt32)p->nowPos64; nuclear@14: startPos32 = nowPos32; nuclear@14: nuclear@14: if (p->nowPos64 == 0) nuclear@14: { nuclear@14: UInt32 numPairs; nuclear@14: Byte curByte; nuclear@14: if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0) nuclear@14: return Flush(p, nowPos32); nuclear@14: ReadMatchDistances(p, &numPairs); nuclear@14: RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0); nuclear@14: p->state = kLiteralNextStates[p->state]; nuclear@14: curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset); nuclear@14: LitEnc_Encode(&p->rc, p->litProbs, curByte); nuclear@14: p->additionalOffset--; nuclear@14: nowPos32++; nuclear@14: } nuclear@14: nuclear@14: if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0) nuclear@14: for (;;) nuclear@14: { nuclear@14: UInt32 pos, len, posState; nuclear@14: nuclear@14: if (p->fastMode) nuclear@14: len = GetOptimumFast(p, &pos); nuclear@14: else nuclear@14: len = GetOptimum(p, nowPos32, &pos); nuclear@14: nuclear@14: #ifdef SHOW_STAT2 nuclear@14: printf("\n pos = %4X, len = %d pos = %d", nowPos32, len, pos); nuclear@14: #endif nuclear@14: nuclear@14: posState = nowPos32 & p->pbMask; nuclear@14: if (len == 1 && pos == (UInt32)-1) nuclear@14: { nuclear@14: Byte curByte; nuclear@14: CLzmaProb *probs; nuclear@14: const Byte *data; nuclear@14: nuclear@14: RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0); nuclear@14: data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset; nuclear@14: curByte = *data; nuclear@14: probs = LIT_PROBS(nowPos32, *(data - 1)); nuclear@14: if (IsCharState(p->state)) nuclear@14: LitEnc_Encode(&p->rc, probs, curByte); nuclear@14: else nuclear@14: LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0] - 1)); nuclear@14: p->state = kLiteralNextStates[p->state]; nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1); nuclear@14: if (pos < LZMA_NUM_REPS) nuclear@14: { nuclear@14: RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 1); nuclear@14: if (pos == 0) nuclear@14: { nuclear@14: RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 0); nuclear@14: RangeEnc_EncodeBit(&p->rc, &p->isRep0Long[p->state][posState], ((len == 1) ? 0 : 1)); nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: UInt32 distance = p->reps[pos]; nuclear@14: RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 1); nuclear@14: if (pos == 1) nuclear@14: RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 0); nuclear@14: else nuclear@14: { nuclear@14: RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 1); nuclear@14: RangeEnc_EncodeBit(&p->rc, &p->isRepG2[p->state], pos - 2); nuclear@14: if (pos == 3) nuclear@14: p->reps[3] = p->reps[2]; nuclear@14: p->reps[2] = p->reps[1]; nuclear@14: } nuclear@14: p->reps[1] = p->reps[0]; nuclear@14: p->reps[0] = distance; nuclear@14: } nuclear@14: if (len == 1) nuclear@14: p->state = kShortRepNextStates[p->state]; nuclear@14: else nuclear@14: { nuclear@14: LenEnc_Encode2(&p->repLenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices); nuclear@14: p->state = kRepNextStates[p->state]; nuclear@14: } nuclear@14: } nuclear@14: else nuclear@14: { nuclear@14: UInt32 posSlot; nuclear@14: RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0); nuclear@14: p->state = kMatchNextStates[p->state]; nuclear@14: LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices); nuclear@14: pos -= LZMA_NUM_REPS; nuclear@14: GetPosSlot(pos, posSlot); nuclear@14: RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, posSlot); nuclear@14: nuclear@14: if (posSlot >= kStartPosModelIndex) nuclear@14: { nuclear@14: UInt32 footerBits = ((posSlot >> 1) - 1); nuclear@14: UInt32 base = ((2 | (posSlot & 1)) << footerBits); nuclear@14: UInt32 posReduced = pos - base; nuclear@14: nuclear@14: if (posSlot < kEndPosModelIndex) nuclear@14: RcTree_ReverseEncode(&p->rc, p->posEncoders + base - posSlot - 1, footerBits, posReduced); nuclear@14: else nuclear@14: { nuclear@14: RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits); nuclear@14: RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask); nuclear@14: p->alignPriceCount++; nuclear@14: } nuclear@14: } nuclear@14: p->reps[3] = p->reps[2]; nuclear@14: p->reps[2] = p->reps[1]; nuclear@14: p->reps[1] = p->reps[0]; nuclear@14: p->reps[0] = pos; nuclear@14: p->matchPriceCount++; nuclear@14: } nuclear@14: } nuclear@14: p->additionalOffset -= len; nuclear@14: nowPos32 += len; nuclear@14: if (p->additionalOffset == 0) nuclear@14: { nuclear@14: UInt32 processed; nuclear@14: if (!p->fastMode) nuclear@14: { nuclear@14: if (p->matchPriceCount >= (1 << 7)) nuclear@14: FillDistancesPrices(p); nuclear@14: if (p->alignPriceCount >= kAlignTableSize) nuclear@14: FillAlignPrices(p); nuclear@14: } nuclear@14: if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0) nuclear@14: break; nuclear@14: processed = nowPos32 - startPos32; nuclear@14: if (useLimits) nuclear@14: { nuclear@14: if (processed + kNumOpts + 300 >= maxUnpackSize || nuclear@14: RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize) nuclear@14: break; nuclear@14: } nuclear@14: else if (processed >= (1 << 15)) nuclear@14: { nuclear@14: p->nowPos64 += nowPos32 - startPos32; nuclear@14: return CheckErrors(p); nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: p->nowPos64 += nowPos32 - startPos32; nuclear@14: return Flush(p, nowPos32); nuclear@14: } nuclear@14: nuclear@14: #define kBigHashDicLimit ((UInt32)1 << 24) nuclear@14: nuclear@14: static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig) nuclear@14: { nuclear@14: UInt32 beforeSize = kNumOpts; nuclear@14: Bool btMode; nuclear@14: if (!RangeEnc_Alloc(&p->rc, alloc)) nuclear@14: return SZ_ERROR_MEM; nuclear@14: btMode = (p->matchFinderBase.btMode != 0); nuclear@14: #ifdef COMPRESS_MF_MT nuclear@14: p->mtMode = (p->multiThread && !p->fastMode && btMode); nuclear@14: #endif nuclear@14: nuclear@14: { nuclear@14: unsigned lclp = p->lc + p->lp; nuclear@14: if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp) nuclear@14: { nuclear@14: LzmaEnc_FreeLits(p, alloc); nuclear@14: p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb)); nuclear@14: p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb)); nuclear@14: if (p->litProbs == 0 || p->saveState.litProbs == 0) nuclear@14: { nuclear@14: LzmaEnc_FreeLits(p, alloc); nuclear@14: return SZ_ERROR_MEM; nuclear@14: } nuclear@14: p->lclp = lclp; nuclear@14: } nuclear@14: } nuclear@14: nuclear@14: p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit); nuclear@14: nuclear@14: if (beforeSize + p->dictSize < keepWindowSize) nuclear@14: beforeSize = keepWindowSize - p->dictSize; nuclear@14: nuclear@14: #ifdef COMPRESS_MF_MT nuclear@14: if (p->mtMode) nuclear@14: { nuclear@14: RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig)); nuclear@14: p->matchFinderObj = &p->matchFinderMt; nuclear@14: MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder); nuclear@14: } nuclear@14: else nuclear@14: #endif nuclear@14: { nuclear@14: if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig)) nuclear@14: return SZ_ERROR_MEM; nuclear@14: p->matchFinderObj = &p->matchFinderBase; nuclear@14: MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder); nuclear@14: } nuclear@14: return SZ_OK; nuclear@14: } nuclear@14: nuclear@14: void LzmaEnc_Init(CLzmaEnc *p) nuclear@14: { nuclear@14: UInt32 i; nuclear@14: p->state = 0; nuclear@14: for (i = 0 ; i < LZMA_NUM_REPS; i++) nuclear@14: p->reps[i] = 0; nuclear@14: nuclear@14: RangeEnc_Init(&p->rc); nuclear@14: nuclear@14: nuclear@14: for (i = 0; i < kNumStates; i++) nuclear@14: { nuclear@14: UInt32 j; nuclear@14: for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++) nuclear@14: { nuclear@14: p->isMatch[i][j] = kProbInitValue; nuclear@14: p->isRep0Long[i][j] = kProbInitValue; nuclear@14: } nuclear@14: p->isRep[i] = kProbInitValue; nuclear@14: p->isRepG0[i] = kProbInitValue; nuclear@14: p->isRepG1[i] = kProbInitValue; nuclear@14: p->isRepG2[i] = kProbInitValue; nuclear@14: } nuclear@14: nuclear@14: { nuclear@14: UInt32 num = 0x300 << (p->lp + p->lc); nuclear@14: for (i = 0; i < num; i++) nuclear@14: p->litProbs[i] = kProbInitValue; nuclear@14: } nuclear@14: nuclear@14: { nuclear@14: for (i = 0; i < kNumLenToPosStates; i++) nuclear@14: { nuclear@14: CLzmaProb *probs = p->posSlotEncoder[i]; nuclear@14: UInt32 j; nuclear@14: for (j = 0; j < (1 << kNumPosSlotBits); j++) nuclear@14: probs[j] = kProbInitValue; nuclear@14: } nuclear@14: } nuclear@14: { nuclear@14: for (i = 0; i < kNumFullDistances - kEndPosModelIndex; i++) nuclear@14: p->posEncoders[i] = kProbInitValue; nuclear@14: } nuclear@14: nuclear@14: LenEnc_Init(&p->lenEnc.p); nuclear@14: LenEnc_Init(&p->repLenEnc.p); nuclear@14: nuclear@14: for (i = 0; i < (1 << kNumAlignBits); i++) nuclear@14: p->posAlignEncoder[i] = kProbInitValue; nuclear@14: nuclear@14: p->optimumEndIndex = 0; nuclear@14: p->optimumCurrentIndex = 0; nuclear@14: p->additionalOffset = 0; nuclear@14: nuclear@14: p->pbMask = (1 << p->pb) - 1; nuclear@14: p->lpMask = (1 << p->lp) - 1; nuclear@14: } nuclear@14: nuclear@14: void LzmaEnc_InitPrices(CLzmaEnc *p) nuclear@14: { nuclear@14: if (!p->fastMode) nuclear@14: { nuclear@14: FillDistancesPrices(p); nuclear@14: FillAlignPrices(p); nuclear@14: } nuclear@14: nuclear@14: p->lenEnc.tableSize = nuclear@14: p->repLenEnc.tableSize = nuclear@14: p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN; nuclear@14: LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, p->ProbPrices); nuclear@14: LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices); nuclear@14: } nuclear@14: nuclear@14: static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig) nuclear@14: { nuclear@14: UInt32 i; nuclear@14: for (i = 0; i < (UInt32)kDicLogSizeMaxCompress; i++) nuclear@14: if (p->dictSize <= ((UInt32)1 << i)) nuclear@14: break; nuclear@14: p->distTableSize = i * 2; nuclear@14: nuclear@14: p->finished = False; nuclear@14: p->result = SZ_OK; nuclear@14: RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig)); nuclear@14: LzmaEnc_Init(p); nuclear@14: LzmaEnc_InitPrices(p); nuclear@14: p->nowPos64 = 0; nuclear@14: return SZ_OK; nuclear@14: } nuclear@14: nuclear@14: static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqInStream *inStream, ISeqOutStream *outStream, nuclear@14: ISzAlloc *alloc, ISzAlloc *allocBig) nuclear@14: { nuclear@14: CLzmaEnc *p = (CLzmaEnc *)pp; nuclear@14: p->inStream = inStream; nuclear@14: p->rc.outStream = outStream; nuclear@14: return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig); nuclear@14: } nuclear@14: nuclear@14: SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp, nuclear@14: ISeqInStream *inStream, UInt32 keepWindowSize, nuclear@14: ISzAlloc *alloc, ISzAlloc *allocBig) nuclear@14: { nuclear@14: CLzmaEnc *p = (CLzmaEnc *)pp; nuclear@14: p->inStream = inStream; nuclear@14: return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig); nuclear@14: } nuclear@14: nuclear@14: static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen) nuclear@14: { nuclear@14: p->seqBufInStream.funcTable.Read = MyRead; nuclear@14: p->seqBufInStream.data = src; nuclear@14: p->seqBufInStream.rem = srcLen; nuclear@14: } nuclear@14: nuclear@14: SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen, nuclear@14: UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig) nuclear@14: { nuclear@14: CLzmaEnc *p = (CLzmaEnc *)pp; nuclear@14: LzmaEnc_SetInputBuf(p, src, srcLen); nuclear@14: p->inStream = &p->seqBufInStream.funcTable; nuclear@14: return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig); nuclear@14: } nuclear@14: nuclear@14: void LzmaEnc_Finish(CLzmaEncHandle pp) nuclear@14: { nuclear@14: #ifdef COMPRESS_MF_MT nuclear@14: CLzmaEnc *p = (CLzmaEnc *)pp; nuclear@14: if (p->mtMode) nuclear@14: MatchFinderMt_ReleaseStream(&p->matchFinderMt); nuclear@14: #else nuclear@14: pp = pp; nuclear@14: #endif nuclear@14: } nuclear@14: nuclear@14: typedef struct _CSeqOutStreamBuf nuclear@14: { nuclear@14: ISeqOutStream funcTable; nuclear@14: Byte *data; nuclear@14: SizeT rem; nuclear@14: Bool overflow; nuclear@14: } CSeqOutStreamBuf; nuclear@14: nuclear@14: static size_t MyWrite(void *pp, const void *data, size_t size) nuclear@14: { nuclear@14: CSeqOutStreamBuf *p = (CSeqOutStreamBuf *)pp; nuclear@14: if (p->rem < size) nuclear@14: { nuclear@14: size = p->rem; nuclear@14: p->overflow = True; nuclear@14: } nuclear@14: memcpy(p->data, data, size); nuclear@14: p->rem -= size; nuclear@14: p->data += size; nuclear@14: return size; nuclear@14: } nuclear@14: nuclear@14: nuclear@14: UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp) nuclear@14: { nuclear@14: const CLzmaEnc *p = (CLzmaEnc *)pp; nuclear@14: return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj); nuclear@14: } nuclear@14: nuclear@14: const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp) nuclear@14: { nuclear@14: const CLzmaEnc *p = (CLzmaEnc *)pp; nuclear@14: return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset; nuclear@14: } nuclear@14: nuclear@14: SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, Bool reInit, nuclear@14: Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize) nuclear@14: { nuclear@14: CLzmaEnc *p = (CLzmaEnc *)pp; nuclear@14: UInt64 nowPos64; nuclear@14: SRes res; nuclear@14: CSeqOutStreamBuf outStream; nuclear@14: nuclear@14: outStream.funcTable.Write = MyWrite; nuclear@14: outStream.data = dest; nuclear@14: outStream.rem = *destLen; nuclear@14: outStream.overflow = False; nuclear@14: nuclear@14: p->writeEndMark = False; nuclear@14: p->finished = False; nuclear@14: p->result = SZ_OK; nuclear@14: nuclear@14: if (reInit) nuclear@14: LzmaEnc_Init(p); nuclear@14: LzmaEnc_InitPrices(p); nuclear@14: nowPos64 = p->nowPos64; nuclear@14: RangeEnc_Init(&p->rc); nuclear@14: p->rc.outStream = &outStream.funcTable; nuclear@14: nuclear@14: res = LzmaEnc_CodeOneBlock(p, True, desiredPackSize, *unpackSize); nuclear@14: nuclear@14: *unpackSize = (UInt32)(p->nowPos64 - nowPos64); nuclear@14: *destLen -= outStream.rem; nuclear@14: if (outStream.overflow) nuclear@14: return SZ_ERROR_OUTPUT_EOF; nuclear@14: nuclear@14: return res; nuclear@14: } nuclear@14: nuclear@14: SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress, nuclear@14: ISzAlloc *alloc, ISzAlloc *allocBig) nuclear@14: { nuclear@14: CLzmaEnc *p = (CLzmaEnc *)pp; nuclear@14: SRes res = SZ_OK; nuclear@14: nuclear@14: #ifdef COMPRESS_MF_MT nuclear@14: Byte allocaDummy[0x300]; nuclear@14: int i = 0; nuclear@14: for (i = 0; i < 16; i++) nuclear@14: allocaDummy[i] = (Byte)i; nuclear@14: #endif nuclear@14: nuclear@14: RINOK(LzmaEnc_Prepare(pp, inStream, outStream, alloc, allocBig)); nuclear@14: nuclear@14: for (;;) nuclear@14: { nuclear@14: res = LzmaEnc_CodeOneBlock(p, False, 0, 0); nuclear@14: if (res != SZ_OK || p->finished != 0) nuclear@14: break; nuclear@14: if (progress != 0) nuclear@14: { nuclear@14: res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc)); nuclear@14: if (res != SZ_OK) nuclear@14: { nuclear@14: res = SZ_ERROR_PROGRESS; nuclear@14: break; nuclear@14: } nuclear@14: } nuclear@14: } nuclear@14: LzmaEnc_Finish(pp); nuclear@14: return res; nuclear@14: } nuclear@14: nuclear@14: SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size) nuclear@14: { nuclear@14: CLzmaEnc *p = (CLzmaEnc *)pp; nuclear@14: int i; nuclear@14: UInt32 dictSize = p->dictSize; nuclear@14: if (*size < LZMA_PROPS_SIZE) nuclear@14: return SZ_ERROR_PARAM; nuclear@14: *size = LZMA_PROPS_SIZE; nuclear@14: props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc); nuclear@14: nuclear@14: for (i = 11; i <= 30; i++) nuclear@14: { nuclear@14: if (dictSize <= ((UInt32)2 << i)) nuclear@14: { nuclear@14: dictSize = (2 << i); nuclear@14: break; nuclear@14: } nuclear@14: if (dictSize <= ((UInt32)3 << i)) nuclear@14: { nuclear@14: dictSize = (3 << i); nuclear@14: break; nuclear@14: } nuclear@14: } nuclear@14: nuclear@14: for (i = 0; i < 4; i++) nuclear@14: props[1 + i] = (Byte)(dictSize >> (8 * i)); nuclear@14: return SZ_OK; nuclear@14: } nuclear@14: nuclear@14: SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, nuclear@14: int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig) nuclear@14: { nuclear@14: SRes res; nuclear@14: CLzmaEnc *p = (CLzmaEnc *)pp; nuclear@14: nuclear@14: CSeqOutStreamBuf outStream; nuclear@14: nuclear@14: LzmaEnc_SetInputBuf(p, src, srcLen); nuclear@14: nuclear@14: outStream.funcTable.Write = MyWrite; nuclear@14: outStream.data = dest; nuclear@14: outStream.rem = *destLen; nuclear@14: outStream.overflow = False; nuclear@14: nuclear@14: p->writeEndMark = writeEndMark; nuclear@14: res = LzmaEnc_Encode(pp, &outStream.funcTable, &p->seqBufInStream.funcTable, nuclear@14: progress, alloc, allocBig); nuclear@14: nuclear@14: *destLen -= outStream.rem; nuclear@14: if (outStream.overflow) nuclear@14: return SZ_ERROR_OUTPUT_EOF; nuclear@14: return res; nuclear@14: } nuclear@14: nuclear@14: SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, nuclear@14: const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark, nuclear@14: ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig) nuclear@14: { nuclear@14: CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc); nuclear@14: SRes res; nuclear@14: if (p == 0) nuclear@14: return SZ_ERROR_MEM; nuclear@14: nuclear@14: res = LzmaEnc_SetProps(p, props); nuclear@14: if (res == SZ_OK) nuclear@14: { nuclear@14: res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize); nuclear@14: if (res == SZ_OK) nuclear@14: res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen, nuclear@14: writeEndMark, progress, alloc, allocBig); nuclear@14: } nuclear@14: nuclear@14: LzmaEnc_Destroy(p, alloc, allocBig); nuclear@14: return res; nuclear@14: }