Mercurial > vba-linux
diff src/win32/7zip/7z/C/LzmaEnc.c @ 1:f9f4f1b99eed
importing src directory
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 10:31:27 -0600 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/win32/7zip/7z/C/LzmaEnc.c Sat Mar 03 10:31:27 2012 -0600 1.3 @@ -0,0 +1,2275 @@ 1.4 +/* LzmaEnc.c -- LZMA Encoder 1.5 +2008-10-04 : Igor Pavlov : Public domain */ 1.6 + 1.7 +#include <string.h> 1.8 + 1.9 +/* #define SHOW_STAT */ 1.10 +/* #define SHOW_STAT2 */ 1.11 + 1.12 +#if defined(SHOW_STAT) || defined(SHOW_STAT2) 1.13 +#include <stdio.h> 1.14 +#endif 1.15 + 1.16 +#include "LzmaEnc.h" 1.17 + 1.18 +#include "LzFind.h" 1.19 +#ifdef COMPRESS_MF_MT 1.20 +#include "LzFindMt.h" 1.21 +#endif 1.22 + 1.23 +#ifdef SHOW_STAT 1.24 +static int ttt = 0; 1.25 +#endif 1.26 + 1.27 +#define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1) 1.28 + 1.29 +#define kBlockSize (9 << 10) 1.30 +#define kUnpackBlockSize (1 << 18) 1.31 +#define kMatchArraySize (1 << 21) 1.32 +#define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX) 1.33 + 1.34 +#define kNumMaxDirectBits (31) 1.35 + 1.36 +#define kNumTopBits 24 1.37 +#define kTopValue ((UInt32)1 << kNumTopBits) 1.38 + 1.39 +#define kNumBitModelTotalBits 11 1.40 +#define kBitModelTotal (1 << kNumBitModelTotalBits) 1.41 +#define kNumMoveBits 5 1.42 +#define kProbInitValue (kBitModelTotal >> 1) 1.43 + 1.44 +#define kNumMoveReducingBits 4 1.45 +#define kNumBitPriceShiftBits 4 1.46 +#define kBitPrice (1 << kNumBitPriceShiftBits) 1.47 + 1.48 +void LzmaEncProps_Init(CLzmaEncProps *p) 1.49 +{ 1.50 + p->level = 5; 1.51 + p->dictSize = p->mc = 0; 1.52 + p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1; 1.53 + p->writeEndMark = 0; 1.54 +} 1.55 + 1.56 +void LzmaEncProps_Normalize(CLzmaEncProps *p) 1.57 +{ 1.58 + int level = p->level; 1.59 + if (level < 0) level = 5; 1.60 + p->level = level; 1.61 + if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26))); 1.62 + if (p->lc < 0) p->lc = 3; 1.63 + if (p->lp < 0) p->lp = 0; 1.64 + if (p->pb < 0) p->pb = 2; 1.65 + if (p->algo < 0) p->algo = (level < 5 ? 0 : 1); 1.66 + if (p->fb < 0) p->fb = (level < 7 ? 32 : 64); 1.67 + if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1); 1.68 + if (p->numHashBytes < 0) p->numHashBytes = 4; 1.69 + if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1); 1.70 + if (p->numThreads < 0) p->numThreads = ((p->btMode && p->algo) ? 2 : 1); 1.71 +} 1.72 + 1.73 +UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2) 1.74 +{ 1.75 + CLzmaEncProps props = *props2; 1.76 + LzmaEncProps_Normalize(&props); 1.77 + return props.dictSize; 1.78 +} 1.79 + 1.80 +/* #define LZMA_LOG_BSR */ 1.81 +/* Define it for Intel's CPU */ 1.82 + 1.83 + 1.84 +#ifdef LZMA_LOG_BSR 1.85 + 1.86 +#define kDicLogSizeMaxCompress 30 1.87 + 1.88 +#define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); } 1.89 + 1.90 +UInt32 GetPosSlot1(UInt32 pos) 1.91 +{ 1.92 + UInt32 res; 1.93 + BSR2_RET(pos, res); 1.94 + return res; 1.95 +} 1.96 +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); } 1.97 +#define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); } 1.98 + 1.99 +#else 1.100 + 1.101 +#define kNumLogBits (9 + (int)sizeof(size_t) / 2) 1.102 +#define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7) 1.103 + 1.104 +void LzmaEnc_FastPosInit(Byte *g_FastPos) 1.105 +{ 1.106 + int c = 2, slotFast; 1.107 + g_FastPos[0] = 0; 1.108 + g_FastPos[1] = 1; 1.109 + 1.110 + for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++) 1.111 + { 1.112 + UInt32 k = (1 << ((slotFast >> 1) - 1)); 1.113 + UInt32 j; 1.114 + for (j = 0; j < k; j++, c++) 1.115 + g_FastPos[c] = (Byte)slotFast; 1.116 + } 1.117 +} 1.118 + 1.119 +#define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \ 1.120 + (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \ 1.121 + res = p->g_FastPos[pos >> i] + (i * 2); } 1.122 +/* 1.123 +#define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \ 1.124 + p->g_FastPos[pos >> 6] + 12 : \ 1.125 + p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; } 1.126 +*/ 1.127 + 1.128 +#define GetPosSlot1(pos) p->g_FastPos[pos] 1.129 +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); } 1.130 +#define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); } 1.131 + 1.132 +#endif 1.133 + 1.134 + 1.135 +#define LZMA_NUM_REPS 4 1.136 + 1.137 +typedef unsigned CState; 1.138 + 1.139 +typedef struct _COptimal 1.140 +{ 1.141 + UInt32 price; 1.142 + 1.143 + CState state; 1.144 + int prev1IsChar; 1.145 + int prev2; 1.146 + 1.147 + UInt32 posPrev2; 1.148 + UInt32 backPrev2; 1.149 + 1.150 + UInt32 posPrev; 1.151 + UInt32 backPrev; 1.152 + UInt32 backs[LZMA_NUM_REPS]; 1.153 +} COptimal; 1.154 + 1.155 +#define kNumOpts (1 << 12) 1.156 + 1.157 +#define kNumLenToPosStates 4 1.158 +#define kNumPosSlotBits 6 1.159 +#define kDicLogSizeMin 0 1.160 +#define kDicLogSizeMax 32 1.161 +#define kDistTableSizeMax (kDicLogSizeMax * 2) 1.162 + 1.163 + 1.164 +#define kNumAlignBits 4 1.165 +#define kAlignTableSize (1 << kNumAlignBits) 1.166 +#define kAlignMask (kAlignTableSize - 1) 1.167 + 1.168 +#define kStartPosModelIndex 4 1.169 +#define kEndPosModelIndex 14 1.170 +#define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex) 1.171 + 1.172 +#define kNumFullDistances (1 << (kEndPosModelIndex / 2)) 1.173 + 1.174 +#ifdef _LZMA_PROB32 1.175 +#define CLzmaProb UInt32 1.176 +#else 1.177 +#define CLzmaProb UInt16 1.178 +#endif 1.179 + 1.180 +#define LZMA_PB_MAX 4 1.181 +#define LZMA_LC_MAX 8 1.182 +#define LZMA_LP_MAX 4 1.183 + 1.184 +#define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX) 1.185 + 1.186 + 1.187 +#define kLenNumLowBits 3 1.188 +#define kLenNumLowSymbols (1 << kLenNumLowBits) 1.189 +#define kLenNumMidBits 3 1.190 +#define kLenNumMidSymbols (1 << kLenNumMidBits) 1.191 +#define kLenNumHighBits 8 1.192 +#define kLenNumHighSymbols (1 << kLenNumHighBits) 1.193 + 1.194 +#define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols) 1.195 + 1.196 +#define LZMA_MATCH_LEN_MIN 2 1.197 +#define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1) 1.198 + 1.199 +#define kNumStates 12 1.200 + 1.201 +typedef struct 1.202 +{ 1.203 + CLzmaProb choice; 1.204 + CLzmaProb choice2; 1.205 + CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits]; 1.206 + CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits]; 1.207 + CLzmaProb high[kLenNumHighSymbols]; 1.208 +} CLenEnc; 1.209 + 1.210 +typedef struct 1.211 +{ 1.212 + CLenEnc p; 1.213 + UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal]; 1.214 + UInt32 tableSize; 1.215 + UInt32 counters[LZMA_NUM_PB_STATES_MAX]; 1.216 +} CLenPriceEnc; 1.217 + 1.218 +typedef struct _CRangeEnc 1.219 +{ 1.220 + UInt32 range; 1.221 + Byte cache; 1.222 + UInt64 low; 1.223 + UInt64 cacheSize; 1.224 + Byte *buf; 1.225 + Byte *bufLim; 1.226 + Byte *bufBase; 1.227 + ISeqOutStream *outStream; 1.228 + UInt64 processed; 1.229 + SRes res; 1.230 +} CRangeEnc; 1.231 + 1.232 +typedef struct _CSeqInStreamBuf 1.233 +{ 1.234 + ISeqInStream funcTable; 1.235 + const Byte *data; 1.236 + SizeT rem; 1.237 +} CSeqInStreamBuf; 1.238 + 1.239 +static SRes MyRead(void *pp, void *data, size_t *size) 1.240 +{ 1.241 + size_t curSize = *size; 1.242 + CSeqInStreamBuf *p = (CSeqInStreamBuf *)pp; 1.243 + if (p->rem < curSize) 1.244 + curSize = p->rem; 1.245 + memcpy(data, p->data, curSize); 1.246 + p->rem -= curSize; 1.247 + p->data += curSize; 1.248 + *size = curSize; 1.249 + return SZ_OK; 1.250 +} 1.251 + 1.252 +typedef struct 1.253 +{ 1.254 + CLzmaProb *litProbs; 1.255 + 1.256 + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX]; 1.257 + CLzmaProb isRep[kNumStates]; 1.258 + CLzmaProb isRepG0[kNumStates]; 1.259 + CLzmaProb isRepG1[kNumStates]; 1.260 + CLzmaProb isRepG2[kNumStates]; 1.261 + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX]; 1.262 + 1.263 + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits]; 1.264 + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex]; 1.265 + CLzmaProb posAlignEncoder[1 << kNumAlignBits]; 1.266 + 1.267 + CLenPriceEnc lenEnc; 1.268 + CLenPriceEnc repLenEnc; 1.269 + 1.270 + UInt32 reps[LZMA_NUM_REPS]; 1.271 + UInt32 state; 1.272 +} CSaveState; 1.273 + 1.274 +typedef struct _CLzmaEnc 1.275 +{ 1.276 + IMatchFinder matchFinder; 1.277 + void *matchFinderObj; 1.278 + 1.279 + #ifdef COMPRESS_MF_MT 1.280 + Bool mtMode; 1.281 + CMatchFinderMt matchFinderMt; 1.282 + #endif 1.283 + 1.284 + CMatchFinder matchFinderBase; 1.285 + 1.286 + #ifdef COMPRESS_MF_MT 1.287 + Byte pad[128]; 1.288 + #endif 1.289 + 1.290 + UInt32 optimumEndIndex; 1.291 + UInt32 optimumCurrentIndex; 1.292 + 1.293 + UInt32 longestMatchLength; 1.294 + UInt32 numPairs; 1.295 + UInt32 numAvail; 1.296 + COptimal opt[kNumOpts]; 1.297 + 1.298 + #ifndef LZMA_LOG_BSR 1.299 + Byte g_FastPos[1 << kNumLogBits]; 1.300 + #endif 1.301 + 1.302 + UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits]; 1.303 + UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1]; 1.304 + UInt32 numFastBytes; 1.305 + UInt32 additionalOffset; 1.306 + UInt32 reps[LZMA_NUM_REPS]; 1.307 + UInt32 state; 1.308 + 1.309 + UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax]; 1.310 + UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances]; 1.311 + UInt32 alignPrices[kAlignTableSize]; 1.312 + UInt32 alignPriceCount; 1.313 + 1.314 + UInt32 distTableSize; 1.315 + 1.316 + unsigned lc, lp, pb; 1.317 + unsigned lpMask, pbMask; 1.318 + 1.319 + CLzmaProb *litProbs; 1.320 + 1.321 + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX]; 1.322 + CLzmaProb isRep[kNumStates]; 1.323 + CLzmaProb isRepG0[kNumStates]; 1.324 + CLzmaProb isRepG1[kNumStates]; 1.325 + CLzmaProb isRepG2[kNumStates]; 1.326 + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX]; 1.327 + 1.328 + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits]; 1.329 + CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex]; 1.330 + CLzmaProb posAlignEncoder[1 << kNumAlignBits]; 1.331 + 1.332 + CLenPriceEnc lenEnc; 1.333 + CLenPriceEnc repLenEnc; 1.334 + 1.335 + unsigned lclp; 1.336 + 1.337 + Bool fastMode; 1.338 + 1.339 + CRangeEnc rc; 1.340 + 1.341 + Bool writeEndMark; 1.342 + UInt64 nowPos64; 1.343 + UInt32 matchPriceCount; 1.344 + Bool finished; 1.345 + Bool multiThread; 1.346 + 1.347 + SRes result; 1.348 + UInt32 dictSize; 1.349 + UInt32 matchFinderCycles; 1.350 + 1.351 + ISeqInStream *inStream; 1.352 + CSeqInStreamBuf seqBufInStream; 1.353 + 1.354 + CSaveState saveState; 1.355 +} CLzmaEnc; 1.356 + 1.357 +void LzmaEnc_SaveState(CLzmaEncHandle pp) 1.358 +{ 1.359 + CLzmaEnc *p = (CLzmaEnc *)pp; 1.360 + CSaveState *dest = &p->saveState; 1.361 + int i; 1.362 + dest->lenEnc = p->lenEnc; 1.363 + dest->repLenEnc = p->repLenEnc; 1.364 + dest->state = p->state; 1.365 + 1.366 + for (i = 0; i < kNumStates; i++) 1.367 + { 1.368 + memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i])); 1.369 + memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i])); 1.370 + } 1.371 + for (i = 0; i < kNumLenToPosStates; i++) 1.372 + memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i])); 1.373 + memcpy(dest->isRep, p->isRep, sizeof(p->isRep)); 1.374 + memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0)); 1.375 + memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1)); 1.376 + memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2)); 1.377 + memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders)); 1.378 + memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder)); 1.379 + memcpy(dest->reps, p->reps, sizeof(p->reps)); 1.380 + memcpy(dest->litProbs, p->litProbs, (0x300 << p->lclp) * sizeof(CLzmaProb)); 1.381 +} 1.382 + 1.383 +void LzmaEnc_RestoreState(CLzmaEncHandle pp) 1.384 +{ 1.385 + CLzmaEnc *dest = (CLzmaEnc *)pp; 1.386 + const CSaveState *p = &dest->saveState; 1.387 + int i; 1.388 + dest->lenEnc = p->lenEnc; 1.389 + dest->repLenEnc = p->repLenEnc; 1.390 + dest->state = p->state; 1.391 + 1.392 + for (i = 0; i < kNumStates; i++) 1.393 + { 1.394 + memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i])); 1.395 + memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i])); 1.396 + } 1.397 + for (i = 0; i < kNumLenToPosStates; i++) 1.398 + memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i])); 1.399 + memcpy(dest->isRep, p->isRep, sizeof(p->isRep)); 1.400 + memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0)); 1.401 + memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1)); 1.402 + memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2)); 1.403 + memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders)); 1.404 + memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder)); 1.405 + memcpy(dest->reps, p->reps, sizeof(p->reps)); 1.406 + memcpy(dest->litProbs, p->litProbs, (0x300 << dest->lclp) * sizeof(CLzmaProb)); 1.407 +} 1.408 + 1.409 +SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2) 1.410 +{ 1.411 + CLzmaEnc *p = (CLzmaEnc *)pp; 1.412 + CLzmaEncProps props = *props2; 1.413 + LzmaEncProps_Normalize(&props); 1.414 + 1.415 + if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX || 1.416 + props.dictSize > (1 << kDicLogSizeMaxCompress) || props.dictSize > (1 << 30)) 1.417 + return SZ_ERROR_PARAM; 1.418 + p->dictSize = props.dictSize; 1.419 + p->matchFinderCycles = props.mc; 1.420 + { 1.421 + unsigned fb = props.fb; 1.422 + if (fb < 5) 1.423 + fb = 5; 1.424 + if (fb > LZMA_MATCH_LEN_MAX) 1.425 + fb = LZMA_MATCH_LEN_MAX; 1.426 + p->numFastBytes = fb; 1.427 + } 1.428 + p->lc = props.lc; 1.429 + p->lp = props.lp; 1.430 + p->pb = props.pb; 1.431 + p->fastMode = (props.algo == 0); 1.432 + p->matchFinderBase.btMode = props.btMode; 1.433 + { 1.434 + UInt32 numHashBytes = 4; 1.435 + if (props.btMode) 1.436 + { 1.437 + if (props.numHashBytes < 2) 1.438 + numHashBytes = 2; 1.439 + else if (props.numHashBytes < 4) 1.440 + numHashBytes = props.numHashBytes; 1.441 + } 1.442 + p->matchFinderBase.numHashBytes = numHashBytes; 1.443 + } 1.444 + 1.445 + p->matchFinderBase.cutValue = props.mc; 1.446 + 1.447 + p->writeEndMark = props.writeEndMark; 1.448 + 1.449 + #ifdef COMPRESS_MF_MT 1.450 + /* 1.451 + if (newMultiThread != _multiThread) 1.452 + { 1.453 + ReleaseMatchFinder(); 1.454 + _multiThread = newMultiThread; 1.455 + } 1.456 + */ 1.457 + p->multiThread = (props.numThreads > 1); 1.458 + #endif 1.459 + 1.460 + return SZ_OK; 1.461 +} 1.462 + 1.463 +static const int kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5}; 1.464 +static const int kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10}; 1.465 +static const int kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11}; 1.466 +static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11}; 1.467 + 1.468 +#define IsCharState(s) ((s) < 7) 1.469 + 1.470 +#define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1) 1.471 + 1.472 +#define kInfinityPrice (1 << 30) 1.473 + 1.474 +static void RangeEnc_Construct(CRangeEnc *p) 1.475 +{ 1.476 + p->outStream = 0; 1.477 + p->bufBase = 0; 1.478 +} 1.479 + 1.480 +#define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize) 1.481 + 1.482 +#define RC_BUF_SIZE (1 << 16) 1.483 +static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc) 1.484 +{ 1.485 + if (p->bufBase == 0) 1.486 + { 1.487 + p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE); 1.488 + if (p->bufBase == 0) 1.489 + return 0; 1.490 + p->bufLim = p->bufBase + RC_BUF_SIZE; 1.491 + } 1.492 + return 1; 1.493 +} 1.494 + 1.495 +static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc) 1.496 +{ 1.497 + alloc->Free(alloc, p->bufBase); 1.498 + p->bufBase = 0; 1.499 +} 1.500 + 1.501 +static void RangeEnc_Init(CRangeEnc *p) 1.502 +{ 1.503 + /* Stream.Init(); */ 1.504 + p->low = 0; 1.505 + p->range = 0xFFFFFFFF; 1.506 + p->cacheSize = 1; 1.507 + p->cache = 0; 1.508 + 1.509 + p->buf = p->bufBase; 1.510 + 1.511 + p->processed = 0; 1.512 + p->res = SZ_OK; 1.513 +} 1.514 + 1.515 +static void RangeEnc_FlushStream(CRangeEnc *p) 1.516 +{ 1.517 + size_t num; 1.518 + if (p->res != SZ_OK) 1.519 + return; 1.520 + num = p->buf - p->bufBase; 1.521 + if (num != p->outStream->Write(p->outStream, p->bufBase, num)) 1.522 + p->res = SZ_ERROR_WRITE; 1.523 + p->processed += num; 1.524 + p->buf = p->bufBase; 1.525 +} 1.526 + 1.527 +static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p) 1.528 +{ 1.529 + if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0) 1.530 + { 1.531 + Byte temp = p->cache; 1.532 + do 1.533 + { 1.534 + Byte *buf = p->buf; 1.535 + *buf++ = (Byte)(temp + (Byte)(p->low >> 32)); 1.536 + p->buf = buf; 1.537 + if (buf == p->bufLim) 1.538 + RangeEnc_FlushStream(p); 1.539 + temp = 0xFF; 1.540 + } 1.541 + while (--p->cacheSize != 0); 1.542 + p->cache = (Byte)((UInt32)p->low >> 24); 1.543 + } 1.544 + p->cacheSize++; 1.545 + p->low = (UInt32)p->low << 8; 1.546 +} 1.547 + 1.548 +static void RangeEnc_FlushData(CRangeEnc *p) 1.549 +{ 1.550 + int i; 1.551 + for (i = 0; i < 5; i++) 1.552 + RangeEnc_ShiftLow(p); 1.553 +} 1.554 + 1.555 +static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits) 1.556 +{ 1.557 + do 1.558 + { 1.559 + p->range >>= 1; 1.560 + p->low += p->range & (0 - ((value >> --numBits) & 1)); 1.561 + if (p->range < kTopValue) 1.562 + { 1.563 + p->range <<= 8; 1.564 + RangeEnc_ShiftLow(p); 1.565 + } 1.566 + } 1.567 + while (numBits != 0); 1.568 +} 1.569 + 1.570 +static void RangeEnc_EncodeBit(CRangeEnc *p, CLzmaProb *prob, UInt32 symbol) 1.571 +{ 1.572 + UInt32 ttt = *prob; 1.573 + UInt32 newBound = (p->range >> kNumBitModelTotalBits) * ttt; 1.574 + if (symbol == 0) 1.575 + { 1.576 + p->range = newBound; 1.577 + ttt += (kBitModelTotal - ttt) >> kNumMoveBits; 1.578 + } 1.579 + else 1.580 + { 1.581 + p->low += newBound; 1.582 + p->range -= newBound; 1.583 + ttt -= ttt >> kNumMoveBits; 1.584 + } 1.585 + *prob = (CLzmaProb)ttt; 1.586 + if (p->range < kTopValue) 1.587 + { 1.588 + p->range <<= 8; 1.589 + RangeEnc_ShiftLow(p); 1.590 + } 1.591 +} 1.592 + 1.593 +static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol) 1.594 +{ 1.595 + symbol |= 0x100; 1.596 + do 1.597 + { 1.598 + RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1); 1.599 + symbol <<= 1; 1.600 + } 1.601 + while (symbol < 0x10000); 1.602 +} 1.603 + 1.604 +static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol, UInt32 matchByte) 1.605 +{ 1.606 + UInt32 offs = 0x100; 1.607 + symbol |= 0x100; 1.608 + do 1.609 + { 1.610 + matchByte <<= 1; 1.611 + RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1); 1.612 + symbol <<= 1; 1.613 + offs &= ~(matchByte ^ symbol); 1.614 + } 1.615 + while (symbol < 0x10000); 1.616 +} 1.617 + 1.618 +void LzmaEnc_InitPriceTables(UInt32 *ProbPrices) 1.619 +{ 1.620 + UInt32 i; 1.621 + for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits)) 1.622 + { 1.623 + const int kCyclesBits = kNumBitPriceShiftBits; 1.624 + UInt32 w = i; 1.625 + UInt32 bitCount = 0; 1.626 + int j; 1.627 + for (j = 0; j < kCyclesBits; j++) 1.628 + { 1.629 + w = w * w; 1.630 + bitCount <<= 1; 1.631 + while (w >= ((UInt32)1 << 16)) 1.632 + { 1.633 + w >>= 1; 1.634 + bitCount++; 1.635 + } 1.636 + } 1.637 + ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount); 1.638 + } 1.639 +} 1.640 + 1.641 + 1.642 +#define GET_PRICE(prob, symbol) \ 1.643 + p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits]; 1.644 + 1.645 +#define GET_PRICEa(prob, symbol) \ 1.646 + ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits]; 1.647 + 1.648 +#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits] 1.649 +#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits] 1.650 + 1.651 +#define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits] 1.652 +#define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits] 1.653 + 1.654 +static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *ProbPrices) 1.655 +{ 1.656 + UInt32 price = 0; 1.657 + symbol |= 0x100; 1.658 + do 1.659 + { 1.660 + price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1); 1.661 + symbol <<= 1; 1.662 + } 1.663 + while (symbol < 0x10000); 1.664 + return price; 1.665 +} 1.666 + 1.667 +static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, UInt32 *ProbPrices) 1.668 +{ 1.669 + UInt32 price = 0; 1.670 + UInt32 offs = 0x100; 1.671 + symbol |= 0x100; 1.672 + do 1.673 + { 1.674 + matchByte <<= 1; 1.675 + price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1); 1.676 + symbol <<= 1; 1.677 + offs &= ~(matchByte ^ symbol); 1.678 + } 1.679 + while (symbol < 0x10000); 1.680 + return price; 1.681 +} 1.682 + 1.683 + 1.684 +static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol) 1.685 +{ 1.686 + UInt32 m = 1; 1.687 + int i; 1.688 + for (i = numBitLevels; i != 0;) 1.689 + { 1.690 + UInt32 bit; 1.691 + i--; 1.692 + bit = (symbol >> i) & 1; 1.693 + RangeEnc_EncodeBit(rc, probs + m, bit); 1.694 + m = (m << 1) | bit; 1.695 + } 1.696 +} 1.697 + 1.698 +static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol) 1.699 +{ 1.700 + UInt32 m = 1; 1.701 + int i; 1.702 + for (i = 0; i < numBitLevels; i++) 1.703 + { 1.704 + UInt32 bit = symbol & 1; 1.705 + RangeEnc_EncodeBit(rc, probs + m, bit); 1.706 + m = (m << 1) | bit; 1.707 + symbol >>= 1; 1.708 + } 1.709 +} 1.710 + 1.711 +static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices) 1.712 +{ 1.713 + UInt32 price = 0; 1.714 + symbol |= (1 << numBitLevels); 1.715 + while (symbol != 1) 1.716 + { 1.717 + price += GET_PRICEa(probs[symbol >> 1], symbol & 1); 1.718 + symbol >>= 1; 1.719 + } 1.720 + return price; 1.721 +} 1.722 + 1.723 +static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices) 1.724 +{ 1.725 + UInt32 price = 0; 1.726 + UInt32 m = 1; 1.727 + int i; 1.728 + for (i = numBitLevels; i != 0; i--) 1.729 + { 1.730 + UInt32 bit = symbol & 1; 1.731 + symbol >>= 1; 1.732 + price += GET_PRICEa(probs[m], bit); 1.733 + m = (m << 1) | bit; 1.734 + } 1.735 + return price; 1.736 +} 1.737 + 1.738 + 1.739 +static void LenEnc_Init(CLenEnc *p) 1.740 +{ 1.741 + unsigned i; 1.742 + p->choice = p->choice2 = kProbInitValue; 1.743 + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumLowBits); i++) 1.744 + p->low[i] = kProbInitValue; 1.745 + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumMidBits); i++) 1.746 + p->mid[i] = kProbInitValue; 1.747 + for (i = 0; i < kLenNumHighSymbols; i++) 1.748 + p->high[i] = kProbInitValue; 1.749 +} 1.750 + 1.751 +static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState) 1.752 +{ 1.753 + if (symbol < kLenNumLowSymbols) 1.754 + { 1.755 + RangeEnc_EncodeBit(rc, &p->choice, 0); 1.756 + RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol); 1.757 + } 1.758 + else 1.759 + { 1.760 + RangeEnc_EncodeBit(rc, &p->choice, 1); 1.761 + if (symbol < kLenNumLowSymbols + kLenNumMidSymbols) 1.762 + { 1.763 + RangeEnc_EncodeBit(rc, &p->choice2, 0); 1.764 + RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols); 1.765 + } 1.766 + else 1.767 + { 1.768 + RangeEnc_EncodeBit(rc, &p->choice2, 1); 1.769 + RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols); 1.770 + } 1.771 + } 1.772 +} 1.773 + 1.774 +static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices) 1.775 +{ 1.776 + UInt32 a0 = GET_PRICE_0a(p->choice); 1.777 + UInt32 a1 = GET_PRICE_1a(p->choice); 1.778 + UInt32 b0 = a1 + GET_PRICE_0a(p->choice2); 1.779 + UInt32 b1 = a1 + GET_PRICE_1a(p->choice2); 1.780 + UInt32 i = 0; 1.781 + for (i = 0; i < kLenNumLowSymbols; i++) 1.782 + { 1.783 + if (i >= numSymbols) 1.784 + return; 1.785 + prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices); 1.786 + } 1.787 + for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++) 1.788 + { 1.789 + if (i >= numSymbols) 1.790 + return; 1.791 + prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices); 1.792 + } 1.793 + for (; i < numSymbols; i++) 1.794 + prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices); 1.795 +} 1.796 + 1.797 +static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices) 1.798 +{ 1.799 + LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices); 1.800 + p->counters[posState] = p->tableSize; 1.801 +} 1.802 + 1.803 +static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices) 1.804 +{ 1.805 + UInt32 posState; 1.806 + for (posState = 0; posState < numPosStates; posState++) 1.807 + LenPriceEnc_UpdateTable(p, posState, ProbPrices); 1.808 +} 1.809 + 1.810 +static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices) 1.811 +{ 1.812 + LenEnc_Encode(&p->p, rc, symbol, posState); 1.813 + if (updatePrice) 1.814 + if (--p->counters[posState] == 0) 1.815 + LenPriceEnc_UpdateTable(p, posState, ProbPrices); 1.816 +} 1.817 + 1.818 + 1.819 + 1.820 + 1.821 +static void MovePos(CLzmaEnc *p, UInt32 num) 1.822 +{ 1.823 + #ifdef SHOW_STAT 1.824 + ttt += num; 1.825 + printf("\n MovePos %d", num); 1.826 + #endif 1.827 + if (num != 0) 1.828 + { 1.829 + p->additionalOffset += num; 1.830 + p->matchFinder.Skip(p->matchFinderObj, num); 1.831 + } 1.832 +} 1.833 + 1.834 +static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes) 1.835 +{ 1.836 + UInt32 lenRes = 0, numPairs; 1.837 + p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj); 1.838 + numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches); 1.839 + #ifdef SHOW_STAT 1.840 + printf("\n i = %d numPairs = %d ", ttt, numPairs / 2); 1.841 + ttt++; 1.842 + { 1.843 + UInt32 i; 1.844 + for (i = 0; i < numPairs; i += 2) 1.845 + printf("%2d %6d | ", p->matches[i], p->matches[i + 1]); 1.846 + } 1.847 + #endif 1.848 + if (numPairs > 0) 1.849 + { 1.850 + lenRes = p->matches[numPairs - 2]; 1.851 + if (lenRes == p->numFastBytes) 1.852 + { 1.853 + const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; 1.854 + UInt32 distance = p->matches[numPairs - 1] + 1; 1.855 + UInt32 numAvail = p->numAvail; 1.856 + if (numAvail > LZMA_MATCH_LEN_MAX) 1.857 + numAvail = LZMA_MATCH_LEN_MAX; 1.858 + { 1.859 + const Byte *pby2 = pby - distance; 1.860 + for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++); 1.861 + } 1.862 + } 1.863 + } 1.864 + p->additionalOffset++; 1.865 + *numDistancePairsRes = numPairs; 1.866 + return lenRes; 1.867 +} 1.868 + 1.869 + 1.870 +#define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False; 1.871 +#define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = False; 1.872 +#define IsShortRep(p) ((p)->backPrev == 0) 1.873 + 1.874 +static UInt32 GetRepLen1Price(CLzmaEnc *p, UInt32 state, UInt32 posState) 1.875 +{ 1.876 + return 1.877 + GET_PRICE_0(p->isRepG0[state]) + 1.878 + GET_PRICE_0(p->isRep0Long[state][posState]); 1.879 +} 1.880 + 1.881 +static UInt32 GetPureRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 state, UInt32 posState) 1.882 +{ 1.883 + UInt32 price; 1.884 + if (repIndex == 0) 1.885 + { 1.886 + price = GET_PRICE_0(p->isRepG0[state]); 1.887 + price += GET_PRICE_1(p->isRep0Long[state][posState]); 1.888 + } 1.889 + else 1.890 + { 1.891 + price = GET_PRICE_1(p->isRepG0[state]); 1.892 + if (repIndex == 1) 1.893 + price += GET_PRICE_0(p->isRepG1[state]); 1.894 + else 1.895 + { 1.896 + price += GET_PRICE_1(p->isRepG1[state]); 1.897 + price += GET_PRICE(p->isRepG2[state], repIndex - 2); 1.898 + } 1.899 + } 1.900 + return price; 1.901 +} 1.902 + 1.903 +static UInt32 GetRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 len, UInt32 state, UInt32 posState) 1.904 +{ 1.905 + return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] + 1.906 + GetPureRepPrice(p, repIndex, state, posState); 1.907 +} 1.908 + 1.909 +static UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur) 1.910 +{ 1.911 + UInt32 posMem = p->opt[cur].posPrev; 1.912 + UInt32 backMem = p->opt[cur].backPrev; 1.913 + p->optimumEndIndex = cur; 1.914 + do 1.915 + { 1.916 + if (p->opt[cur].prev1IsChar) 1.917 + { 1.918 + MakeAsChar(&p->opt[posMem]) 1.919 + p->opt[posMem].posPrev = posMem - 1; 1.920 + if (p->opt[cur].prev2) 1.921 + { 1.922 + p->opt[posMem - 1].prev1IsChar = False; 1.923 + p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2; 1.924 + p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2; 1.925 + } 1.926 + } 1.927 + { 1.928 + UInt32 posPrev = posMem; 1.929 + UInt32 backCur = backMem; 1.930 + 1.931 + backMem = p->opt[posPrev].backPrev; 1.932 + posMem = p->opt[posPrev].posPrev; 1.933 + 1.934 + p->opt[posPrev].backPrev = backCur; 1.935 + p->opt[posPrev].posPrev = cur; 1.936 + cur = posPrev; 1.937 + } 1.938 + } 1.939 + while (cur != 0); 1.940 + *backRes = p->opt[0].backPrev; 1.941 + p->optimumCurrentIndex = p->opt[0].posPrev; 1.942 + return p->optimumCurrentIndex; 1.943 +} 1.944 + 1.945 +#define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300) 1.946 + 1.947 +static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes) 1.948 +{ 1.949 + UInt32 numAvail, mainLen, numPairs, repMaxIndex, i, posState, lenEnd, len, cur; 1.950 + UInt32 matchPrice, repMatchPrice, normalMatchPrice; 1.951 + UInt32 reps[LZMA_NUM_REPS], repLens[LZMA_NUM_REPS]; 1.952 + UInt32 *matches; 1.953 + const Byte *data; 1.954 + Byte curByte, matchByte; 1.955 + if (p->optimumEndIndex != p->optimumCurrentIndex) 1.956 + { 1.957 + const COptimal *opt = &p->opt[p->optimumCurrentIndex]; 1.958 + UInt32 lenRes = opt->posPrev - p->optimumCurrentIndex; 1.959 + *backRes = opt->backPrev; 1.960 + p->optimumCurrentIndex = opt->posPrev; 1.961 + return lenRes; 1.962 + } 1.963 + p->optimumCurrentIndex = p->optimumEndIndex = 0; 1.964 + 1.965 + if (p->additionalOffset == 0) 1.966 + mainLen = ReadMatchDistances(p, &numPairs); 1.967 + else 1.968 + { 1.969 + mainLen = p->longestMatchLength; 1.970 + numPairs = p->numPairs; 1.971 + } 1.972 + 1.973 + numAvail = p->numAvail; 1.974 + if (numAvail < 2) 1.975 + { 1.976 + *backRes = (UInt32)(-1); 1.977 + return 1; 1.978 + } 1.979 + if (numAvail > LZMA_MATCH_LEN_MAX) 1.980 + numAvail = LZMA_MATCH_LEN_MAX; 1.981 + 1.982 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; 1.983 + repMaxIndex = 0; 1.984 + for (i = 0; i < LZMA_NUM_REPS; i++) 1.985 + { 1.986 + UInt32 lenTest; 1.987 + const Byte *data2; 1.988 + reps[i] = p->reps[i]; 1.989 + data2 = data - (reps[i] + 1); 1.990 + if (data[0] != data2[0] || data[1] != data2[1]) 1.991 + { 1.992 + repLens[i] = 0; 1.993 + continue; 1.994 + } 1.995 + for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++); 1.996 + repLens[i] = lenTest; 1.997 + if (lenTest > repLens[repMaxIndex]) 1.998 + repMaxIndex = i; 1.999 + } 1.1000 + if (repLens[repMaxIndex] >= p->numFastBytes) 1.1001 + { 1.1002 + UInt32 lenRes; 1.1003 + *backRes = repMaxIndex; 1.1004 + lenRes = repLens[repMaxIndex]; 1.1005 + MovePos(p, lenRes - 1); 1.1006 + return lenRes; 1.1007 + } 1.1008 + 1.1009 + matches = p->matches; 1.1010 + if (mainLen >= p->numFastBytes) 1.1011 + { 1.1012 + *backRes = matches[numPairs - 1] + LZMA_NUM_REPS; 1.1013 + MovePos(p, mainLen - 1); 1.1014 + return mainLen; 1.1015 + } 1.1016 + curByte = *data; 1.1017 + matchByte = *(data - (reps[0] + 1)); 1.1018 + 1.1019 + if (mainLen < 2 && curByte != matchByte && repLens[repMaxIndex] < 2) 1.1020 + { 1.1021 + *backRes = (UInt32)-1; 1.1022 + return 1; 1.1023 + } 1.1024 + 1.1025 + p->opt[0].state = (CState)p->state; 1.1026 + 1.1027 + posState = (position & p->pbMask); 1.1028 + 1.1029 + { 1.1030 + const CLzmaProb *probs = LIT_PROBS(position, *(data - 1)); 1.1031 + p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) + 1.1032 + (!IsCharState(p->state) ? 1.1033 + LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) : 1.1034 + LitEnc_GetPrice(probs, curByte, p->ProbPrices)); 1.1035 + } 1.1036 + 1.1037 + MakeAsChar(&p->opt[1]); 1.1038 + 1.1039 + matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]); 1.1040 + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]); 1.1041 + 1.1042 + if (matchByte == curByte) 1.1043 + { 1.1044 + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState); 1.1045 + if (shortRepPrice < p->opt[1].price) 1.1046 + { 1.1047 + p->opt[1].price = shortRepPrice; 1.1048 + MakeAsShortRep(&p->opt[1]); 1.1049 + } 1.1050 + } 1.1051 + lenEnd = ((mainLen >= repLens[repMaxIndex]) ? mainLen : repLens[repMaxIndex]); 1.1052 + 1.1053 + if (lenEnd < 2) 1.1054 + { 1.1055 + *backRes = p->opt[1].backPrev; 1.1056 + return 1; 1.1057 + } 1.1058 + 1.1059 + p->opt[1].posPrev = 0; 1.1060 + for (i = 0; i < LZMA_NUM_REPS; i++) 1.1061 + p->opt[0].backs[i] = reps[i]; 1.1062 + 1.1063 + len = lenEnd; 1.1064 + do 1.1065 + p->opt[len--].price = kInfinityPrice; 1.1066 + while (len >= 2); 1.1067 + 1.1068 + for (i = 0; i < LZMA_NUM_REPS; i++) 1.1069 + { 1.1070 + UInt32 repLen = repLens[i]; 1.1071 + UInt32 price; 1.1072 + if (repLen < 2) 1.1073 + continue; 1.1074 + price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState); 1.1075 + do 1.1076 + { 1.1077 + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2]; 1.1078 + COptimal *opt = &p->opt[repLen]; 1.1079 + if (curAndLenPrice < opt->price) 1.1080 + { 1.1081 + opt->price = curAndLenPrice; 1.1082 + opt->posPrev = 0; 1.1083 + opt->backPrev = i; 1.1084 + opt->prev1IsChar = False; 1.1085 + } 1.1086 + } 1.1087 + while (--repLen >= 2); 1.1088 + } 1.1089 + 1.1090 + normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]); 1.1091 + 1.1092 + len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2); 1.1093 + if (len <= mainLen) 1.1094 + { 1.1095 + UInt32 offs = 0; 1.1096 + while (len > matches[offs]) 1.1097 + offs += 2; 1.1098 + for (; ; len++) 1.1099 + { 1.1100 + COptimal *opt; 1.1101 + UInt32 distance = matches[offs + 1]; 1.1102 + 1.1103 + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN]; 1.1104 + UInt32 lenToPosState = GetLenToPosState(len); 1.1105 + if (distance < kNumFullDistances) 1.1106 + curAndLenPrice += p->distancesPrices[lenToPosState][distance]; 1.1107 + else 1.1108 + { 1.1109 + UInt32 slot; 1.1110 + GetPosSlot2(distance, slot); 1.1111 + curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot]; 1.1112 + } 1.1113 + opt = &p->opt[len]; 1.1114 + if (curAndLenPrice < opt->price) 1.1115 + { 1.1116 + opt->price = curAndLenPrice; 1.1117 + opt->posPrev = 0; 1.1118 + opt->backPrev = distance + LZMA_NUM_REPS; 1.1119 + opt->prev1IsChar = False; 1.1120 + } 1.1121 + if (len == matches[offs]) 1.1122 + { 1.1123 + offs += 2; 1.1124 + if (offs == numPairs) 1.1125 + break; 1.1126 + } 1.1127 + } 1.1128 + } 1.1129 + 1.1130 + cur = 0; 1.1131 + 1.1132 + #ifdef SHOW_STAT2 1.1133 + if (position >= 0) 1.1134 + { 1.1135 + unsigned i; 1.1136 + printf("\n pos = %4X", position); 1.1137 + for (i = cur; i <= lenEnd; i++) 1.1138 + printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price); 1.1139 + } 1.1140 + #endif 1.1141 + 1.1142 + for (;;) 1.1143 + { 1.1144 + UInt32 numAvailFull, newLen, numPairs, posPrev, state, posState, startLen; 1.1145 + UInt32 curPrice, curAnd1Price, matchPrice, repMatchPrice; 1.1146 + Bool nextIsChar; 1.1147 + Byte curByte, matchByte; 1.1148 + const Byte *data; 1.1149 + COptimal *curOpt; 1.1150 + COptimal *nextOpt; 1.1151 + 1.1152 + cur++; 1.1153 + if (cur == lenEnd) 1.1154 + return Backward(p, backRes, cur); 1.1155 + 1.1156 + newLen = ReadMatchDistances(p, &numPairs); 1.1157 + if (newLen >= p->numFastBytes) 1.1158 + { 1.1159 + p->numPairs = numPairs; 1.1160 + p->longestMatchLength = newLen; 1.1161 + return Backward(p, backRes, cur); 1.1162 + } 1.1163 + position++; 1.1164 + curOpt = &p->opt[cur]; 1.1165 + posPrev = curOpt->posPrev; 1.1166 + if (curOpt->prev1IsChar) 1.1167 + { 1.1168 + posPrev--; 1.1169 + if (curOpt->prev2) 1.1170 + { 1.1171 + state = p->opt[curOpt->posPrev2].state; 1.1172 + if (curOpt->backPrev2 < LZMA_NUM_REPS) 1.1173 + state = kRepNextStates[state]; 1.1174 + else 1.1175 + state = kMatchNextStates[state]; 1.1176 + } 1.1177 + else 1.1178 + state = p->opt[posPrev].state; 1.1179 + state = kLiteralNextStates[state]; 1.1180 + } 1.1181 + else 1.1182 + state = p->opt[posPrev].state; 1.1183 + if (posPrev == cur - 1) 1.1184 + { 1.1185 + if (IsShortRep(curOpt)) 1.1186 + state = kShortRepNextStates[state]; 1.1187 + else 1.1188 + state = kLiteralNextStates[state]; 1.1189 + } 1.1190 + else 1.1191 + { 1.1192 + UInt32 pos; 1.1193 + const COptimal *prevOpt; 1.1194 + if (curOpt->prev1IsChar && curOpt->prev2) 1.1195 + { 1.1196 + posPrev = curOpt->posPrev2; 1.1197 + pos = curOpt->backPrev2; 1.1198 + state = kRepNextStates[state]; 1.1199 + } 1.1200 + else 1.1201 + { 1.1202 + pos = curOpt->backPrev; 1.1203 + if (pos < LZMA_NUM_REPS) 1.1204 + state = kRepNextStates[state]; 1.1205 + else 1.1206 + state = kMatchNextStates[state]; 1.1207 + } 1.1208 + prevOpt = &p->opt[posPrev]; 1.1209 + if (pos < LZMA_NUM_REPS) 1.1210 + { 1.1211 + UInt32 i; 1.1212 + reps[0] = prevOpt->backs[pos]; 1.1213 + for (i = 1; i <= pos; i++) 1.1214 + reps[i] = prevOpt->backs[i - 1]; 1.1215 + for (; i < LZMA_NUM_REPS; i++) 1.1216 + reps[i] = prevOpt->backs[i]; 1.1217 + } 1.1218 + else 1.1219 + { 1.1220 + UInt32 i; 1.1221 + reps[0] = (pos - LZMA_NUM_REPS); 1.1222 + for (i = 1; i < LZMA_NUM_REPS; i++) 1.1223 + reps[i] = prevOpt->backs[i - 1]; 1.1224 + } 1.1225 + } 1.1226 + curOpt->state = (CState)state; 1.1227 + 1.1228 + curOpt->backs[0] = reps[0]; 1.1229 + curOpt->backs[1] = reps[1]; 1.1230 + curOpt->backs[2] = reps[2]; 1.1231 + curOpt->backs[3] = reps[3]; 1.1232 + 1.1233 + curPrice = curOpt->price; 1.1234 + nextIsChar = False; 1.1235 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; 1.1236 + curByte = *data; 1.1237 + matchByte = *(data - (reps[0] + 1)); 1.1238 + 1.1239 + posState = (position & p->pbMask); 1.1240 + 1.1241 + curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]); 1.1242 + { 1.1243 + const CLzmaProb *probs = LIT_PROBS(position, *(data - 1)); 1.1244 + curAnd1Price += 1.1245 + (!IsCharState(state) ? 1.1246 + LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) : 1.1247 + LitEnc_GetPrice(probs, curByte, p->ProbPrices)); 1.1248 + } 1.1249 + 1.1250 + nextOpt = &p->opt[cur + 1]; 1.1251 + 1.1252 + if (curAnd1Price < nextOpt->price) 1.1253 + { 1.1254 + nextOpt->price = curAnd1Price; 1.1255 + nextOpt->posPrev = cur; 1.1256 + MakeAsChar(nextOpt); 1.1257 + nextIsChar = True; 1.1258 + } 1.1259 + 1.1260 + matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]); 1.1261 + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]); 1.1262 + 1.1263 + if (matchByte == curByte && !(nextOpt->posPrev < cur && nextOpt->backPrev == 0)) 1.1264 + { 1.1265 + UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState); 1.1266 + if (shortRepPrice <= nextOpt->price) 1.1267 + { 1.1268 + nextOpt->price = shortRepPrice; 1.1269 + nextOpt->posPrev = cur; 1.1270 + MakeAsShortRep(nextOpt); 1.1271 + nextIsChar = True; 1.1272 + } 1.1273 + } 1.1274 + numAvailFull = p->numAvail; 1.1275 + { 1.1276 + UInt32 temp = kNumOpts - 1 - cur; 1.1277 + if (temp < numAvailFull) 1.1278 + numAvailFull = temp; 1.1279 + } 1.1280 + 1.1281 + if (numAvailFull < 2) 1.1282 + continue; 1.1283 + numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes); 1.1284 + 1.1285 + if (!nextIsChar && matchByte != curByte) /* speed optimization */ 1.1286 + { 1.1287 + /* try Literal + rep0 */ 1.1288 + UInt32 temp; 1.1289 + UInt32 lenTest2; 1.1290 + const Byte *data2 = data - (reps[0] + 1); 1.1291 + UInt32 limit = p->numFastBytes + 1; 1.1292 + if (limit > numAvailFull) 1.1293 + limit = numAvailFull; 1.1294 + 1.1295 + for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++); 1.1296 + lenTest2 = temp - 1; 1.1297 + if (lenTest2 >= 2) 1.1298 + { 1.1299 + UInt32 state2 = kLiteralNextStates[state]; 1.1300 + UInt32 posStateNext = (position + 1) & p->pbMask; 1.1301 + UInt32 nextRepMatchPrice = curAnd1Price + 1.1302 + GET_PRICE_1(p->isMatch[state2][posStateNext]) + 1.1303 + GET_PRICE_1(p->isRep[state2]); 1.1304 + /* for (; lenTest2 >= 2; lenTest2--) */ 1.1305 + { 1.1306 + UInt32 curAndLenPrice; 1.1307 + COptimal *opt; 1.1308 + UInt32 offset = cur + 1 + lenTest2; 1.1309 + while (lenEnd < offset) 1.1310 + p->opt[++lenEnd].price = kInfinityPrice; 1.1311 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext); 1.1312 + opt = &p->opt[offset]; 1.1313 + if (curAndLenPrice < opt->price) 1.1314 + { 1.1315 + opt->price = curAndLenPrice; 1.1316 + opt->posPrev = cur + 1; 1.1317 + opt->backPrev = 0; 1.1318 + opt->prev1IsChar = True; 1.1319 + opt->prev2 = False; 1.1320 + } 1.1321 + } 1.1322 + } 1.1323 + } 1.1324 + 1.1325 + startLen = 2; /* speed optimization */ 1.1326 + { 1.1327 + UInt32 repIndex; 1.1328 + for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++) 1.1329 + { 1.1330 + UInt32 lenTest; 1.1331 + UInt32 lenTestTemp; 1.1332 + UInt32 price; 1.1333 + const Byte *data2 = data - (reps[repIndex] + 1); 1.1334 + if (data[0] != data2[0] || data[1] != data2[1]) 1.1335 + continue; 1.1336 + for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++); 1.1337 + while (lenEnd < cur + lenTest) 1.1338 + p->opt[++lenEnd].price = kInfinityPrice; 1.1339 + lenTestTemp = lenTest; 1.1340 + price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState); 1.1341 + do 1.1342 + { 1.1343 + UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2]; 1.1344 + COptimal *opt = &p->opt[cur + lenTest]; 1.1345 + if (curAndLenPrice < opt->price) 1.1346 + { 1.1347 + opt->price = curAndLenPrice; 1.1348 + opt->posPrev = cur; 1.1349 + opt->backPrev = repIndex; 1.1350 + opt->prev1IsChar = False; 1.1351 + } 1.1352 + } 1.1353 + while (--lenTest >= 2); 1.1354 + lenTest = lenTestTemp; 1.1355 + 1.1356 + if (repIndex == 0) 1.1357 + startLen = lenTest + 1; 1.1358 + 1.1359 + /* if (_maxMode) */ 1.1360 + { 1.1361 + UInt32 lenTest2 = lenTest + 1; 1.1362 + UInt32 limit = lenTest2 + p->numFastBytes; 1.1363 + UInt32 nextRepMatchPrice; 1.1364 + if (limit > numAvailFull) 1.1365 + limit = numAvailFull; 1.1366 + for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++); 1.1367 + lenTest2 -= lenTest + 1; 1.1368 + if (lenTest2 >= 2) 1.1369 + { 1.1370 + UInt32 state2 = kRepNextStates[state]; 1.1371 + UInt32 posStateNext = (position + lenTest) & p->pbMask; 1.1372 + UInt32 curAndLenCharPrice = 1.1373 + price + p->repLenEnc.prices[posState][lenTest - 2] + 1.1374 + GET_PRICE_0(p->isMatch[state2][posStateNext]) + 1.1375 + LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]), 1.1376 + data[lenTest], data2[lenTest], p->ProbPrices); 1.1377 + state2 = kLiteralNextStates[state2]; 1.1378 + posStateNext = (position + lenTest + 1) & p->pbMask; 1.1379 + nextRepMatchPrice = curAndLenCharPrice + 1.1380 + GET_PRICE_1(p->isMatch[state2][posStateNext]) + 1.1381 + GET_PRICE_1(p->isRep[state2]); 1.1382 + 1.1383 + /* for (; lenTest2 >= 2; lenTest2--) */ 1.1384 + { 1.1385 + UInt32 curAndLenPrice; 1.1386 + COptimal *opt; 1.1387 + UInt32 offset = cur + lenTest + 1 + lenTest2; 1.1388 + while (lenEnd < offset) 1.1389 + p->opt[++lenEnd].price = kInfinityPrice; 1.1390 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext); 1.1391 + opt = &p->opt[offset]; 1.1392 + if (curAndLenPrice < opt->price) 1.1393 + { 1.1394 + opt->price = curAndLenPrice; 1.1395 + opt->posPrev = cur + lenTest + 1; 1.1396 + opt->backPrev = 0; 1.1397 + opt->prev1IsChar = True; 1.1398 + opt->prev2 = True; 1.1399 + opt->posPrev2 = cur; 1.1400 + opt->backPrev2 = repIndex; 1.1401 + } 1.1402 + } 1.1403 + } 1.1404 + } 1.1405 + } 1.1406 + } 1.1407 + /* for (UInt32 lenTest = 2; lenTest <= newLen; lenTest++) */ 1.1408 + if (newLen > numAvail) 1.1409 + { 1.1410 + newLen = numAvail; 1.1411 + for (numPairs = 0; newLen > matches[numPairs]; numPairs += 2); 1.1412 + matches[numPairs] = newLen; 1.1413 + numPairs += 2; 1.1414 + } 1.1415 + if (newLen >= startLen) 1.1416 + { 1.1417 + UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]); 1.1418 + UInt32 offs, curBack, posSlot; 1.1419 + UInt32 lenTest; 1.1420 + while (lenEnd < cur + newLen) 1.1421 + p->opt[++lenEnd].price = kInfinityPrice; 1.1422 + 1.1423 + offs = 0; 1.1424 + while (startLen > matches[offs]) 1.1425 + offs += 2; 1.1426 + curBack = matches[offs + 1]; 1.1427 + GetPosSlot2(curBack, posSlot); 1.1428 + for (lenTest = /*2*/ startLen; ; lenTest++) 1.1429 + { 1.1430 + UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN]; 1.1431 + UInt32 lenToPosState = GetLenToPosState(lenTest); 1.1432 + COptimal *opt; 1.1433 + if (curBack < kNumFullDistances) 1.1434 + curAndLenPrice += p->distancesPrices[lenToPosState][curBack]; 1.1435 + else 1.1436 + curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask]; 1.1437 + 1.1438 + opt = &p->opt[cur + lenTest]; 1.1439 + if (curAndLenPrice < opt->price) 1.1440 + { 1.1441 + opt->price = curAndLenPrice; 1.1442 + opt->posPrev = cur; 1.1443 + opt->backPrev = curBack + LZMA_NUM_REPS; 1.1444 + opt->prev1IsChar = False; 1.1445 + } 1.1446 + 1.1447 + if (/*_maxMode && */lenTest == matches[offs]) 1.1448 + { 1.1449 + /* Try Match + Literal + Rep0 */ 1.1450 + const Byte *data2 = data - (curBack + 1); 1.1451 + UInt32 lenTest2 = lenTest + 1; 1.1452 + UInt32 limit = lenTest2 + p->numFastBytes; 1.1453 + UInt32 nextRepMatchPrice; 1.1454 + if (limit > numAvailFull) 1.1455 + limit = numAvailFull; 1.1456 + for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++); 1.1457 + lenTest2 -= lenTest + 1; 1.1458 + if (lenTest2 >= 2) 1.1459 + { 1.1460 + UInt32 state2 = kMatchNextStates[state]; 1.1461 + UInt32 posStateNext = (position + lenTest) & p->pbMask; 1.1462 + UInt32 curAndLenCharPrice = curAndLenPrice + 1.1463 + GET_PRICE_0(p->isMatch[state2][posStateNext]) + 1.1464 + LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]), 1.1465 + data[lenTest], data2[lenTest], p->ProbPrices); 1.1466 + state2 = kLiteralNextStates[state2]; 1.1467 + posStateNext = (posStateNext + 1) & p->pbMask; 1.1468 + nextRepMatchPrice = curAndLenCharPrice + 1.1469 + GET_PRICE_1(p->isMatch[state2][posStateNext]) + 1.1470 + GET_PRICE_1(p->isRep[state2]); 1.1471 + 1.1472 + /* for (; lenTest2 >= 2; lenTest2--) */ 1.1473 + { 1.1474 + UInt32 offset = cur + lenTest + 1 + lenTest2; 1.1475 + UInt32 curAndLenPrice; 1.1476 + COptimal *opt; 1.1477 + while (lenEnd < offset) 1.1478 + p->opt[++lenEnd].price = kInfinityPrice; 1.1479 + curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext); 1.1480 + opt = &p->opt[offset]; 1.1481 + if (curAndLenPrice < opt->price) 1.1482 + { 1.1483 + opt->price = curAndLenPrice; 1.1484 + opt->posPrev = cur + lenTest + 1; 1.1485 + opt->backPrev = 0; 1.1486 + opt->prev1IsChar = True; 1.1487 + opt->prev2 = True; 1.1488 + opt->posPrev2 = cur; 1.1489 + opt->backPrev2 = curBack + LZMA_NUM_REPS; 1.1490 + } 1.1491 + } 1.1492 + } 1.1493 + offs += 2; 1.1494 + if (offs == numPairs) 1.1495 + break; 1.1496 + curBack = matches[offs + 1]; 1.1497 + if (curBack >= kNumFullDistances) 1.1498 + GetPosSlot2(curBack, posSlot); 1.1499 + } 1.1500 + } 1.1501 + } 1.1502 + } 1.1503 +} 1.1504 + 1.1505 +#define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist)) 1.1506 + 1.1507 +static UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes) 1.1508 +{ 1.1509 + UInt32 numAvail, mainLen, mainDist, numPairs, repIndex, repLen, i; 1.1510 + const Byte *data; 1.1511 + const UInt32 *matches; 1.1512 + 1.1513 + if (p->additionalOffset == 0) 1.1514 + mainLen = ReadMatchDistances(p, &numPairs); 1.1515 + else 1.1516 + { 1.1517 + mainLen = p->longestMatchLength; 1.1518 + numPairs = p->numPairs; 1.1519 + } 1.1520 + 1.1521 + numAvail = p->numAvail; 1.1522 + *backRes = (UInt32)-1; 1.1523 + if (numAvail < 2) 1.1524 + return 1; 1.1525 + if (numAvail > LZMA_MATCH_LEN_MAX) 1.1526 + numAvail = LZMA_MATCH_LEN_MAX; 1.1527 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; 1.1528 + 1.1529 + repLen = repIndex = 0; 1.1530 + for (i = 0; i < LZMA_NUM_REPS; i++) 1.1531 + { 1.1532 + UInt32 len; 1.1533 + const Byte *data2 = data - (p->reps[i] + 1); 1.1534 + if (data[0] != data2[0] || data[1] != data2[1]) 1.1535 + continue; 1.1536 + for (len = 2; len < numAvail && data[len] == data2[len]; len++); 1.1537 + if (len >= p->numFastBytes) 1.1538 + { 1.1539 + *backRes = i; 1.1540 + MovePos(p, len - 1); 1.1541 + return len; 1.1542 + } 1.1543 + if (len > repLen) 1.1544 + { 1.1545 + repIndex = i; 1.1546 + repLen = len; 1.1547 + } 1.1548 + } 1.1549 + 1.1550 + matches = p->matches; 1.1551 + if (mainLen >= p->numFastBytes) 1.1552 + { 1.1553 + *backRes = matches[numPairs - 1] + LZMA_NUM_REPS; 1.1554 + MovePos(p, mainLen - 1); 1.1555 + return mainLen; 1.1556 + } 1.1557 + 1.1558 + mainDist = 0; /* for GCC */ 1.1559 + if (mainLen >= 2) 1.1560 + { 1.1561 + mainDist = matches[numPairs - 1]; 1.1562 + while (numPairs > 2 && mainLen == matches[numPairs - 4] + 1) 1.1563 + { 1.1564 + if (!ChangePair(matches[numPairs - 3], mainDist)) 1.1565 + break; 1.1566 + numPairs -= 2; 1.1567 + mainLen = matches[numPairs - 2]; 1.1568 + mainDist = matches[numPairs - 1]; 1.1569 + } 1.1570 + if (mainLen == 2 && mainDist >= 0x80) 1.1571 + mainLen = 1; 1.1572 + } 1.1573 + 1.1574 + if (repLen >= 2 && ( 1.1575 + (repLen + 1 >= mainLen) || 1.1576 + (repLen + 2 >= mainLen && mainDist >= (1 << 9)) || 1.1577 + (repLen + 3 >= mainLen && mainDist >= (1 << 15)))) 1.1578 + { 1.1579 + *backRes = repIndex; 1.1580 + MovePos(p, repLen - 1); 1.1581 + return repLen; 1.1582 + } 1.1583 + 1.1584 + if (mainLen < 2 || numAvail <= 2) 1.1585 + return 1; 1.1586 + 1.1587 + p->longestMatchLength = ReadMatchDistances(p, &p->numPairs); 1.1588 + if (p->longestMatchLength >= 2) 1.1589 + { 1.1590 + UInt32 newDistance = matches[p->numPairs - 1]; 1.1591 + if ((p->longestMatchLength >= mainLen && newDistance < mainDist) || 1.1592 + (p->longestMatchLength == mainLen + 1 && !ChangePair(mainDist, newDistance)) || 1.1593 + (p->longestMatchLength > mainLen + 1) || 1.1594 + (p->longestMatchLength + 1 >= mainLen && mainLen >= 3 && ChangePair(newDistance, mainDist))) 1.1595 + return 1; 1.1596 + } 1.1597 + 1.1598 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; 1.1599 + for (i = 0; i < LZMA_NUM_REPS; i++) 1.1600 + { 1.1601 + UInt32 len, limit; 1.1602 + const Byte *data2 = data - (p->reps[i] + 1); 1.1603 + if (data[0] != data2[0] || data[1] != data2[1]) 1.1604 + continue; 1.1605 + limit = mainLen - 1; 1.1606 + for (len = 2; len < limit && data[len] == data2[len]; len++); 1.1607 + if (len >= limit) 1.1608 + return 1; 1.1609 + } 1.1610 + *backRes = mainDist + LZMA_NUM_REPS; 1.1611 + MovePos(p, mainLen - 2); 1.1612 + return mainLen; 1.1613 +} 1.1614 + 1.1615 +static void WriteEndMarker(CLzmaEnc *p, UInt32 posState) 1.1616 +{ 1.1617 + UInt32 len; 1.1618 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1); 1.1619 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0); 1.1620 + p->state = kMatchNextStates[p->state]; 1.1621 + len = LZMA_MATCH_LEN_MIN; 1.1622 + LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices); 1.1623 + RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1); 1.1624 + RangeEnc_EncodeDirectBits(&p->rc, (((UInt32)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits); 1.1625 + RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask); 1.1626 +} 1.1627 + 1.1628 +static SRes CheckErrors(CLzmaEnc *p) 1.1629 +{ 1.1630 + if (p->result != SZ_OK) 1.1631 + return p->result; 1.1632 + if (p->rc.res != SZ_OK) 1.1633 + p->result = SZ_ERROR_WRITE; 1.1634 + if (p->matchFinderBase.result != SZ_OK) 1.1635 + p->result = SZ_ERROR_READ; 1.1636 + if (p->result != SZ_OK) 1.1637 + p->finished = True; 1.1638 + return p->result; 1.1639 +} 1.1640 + 1.1641 +static SRes Flush(CLzmaEnc *p, UInt32 nowPos) 1.1642 +{ 1.1643 + /* ReleaseMFStream(); */ 1.1644 + p->finished = True; 1.1645 + if (p->writeEndMark) 1.1646 + WriteEndMarker(p, nowPos & p->pbMask); 1.1647 + RangeEnc_FlushData(&p->rc); 1.1648 + RangeEnc_FlushStream(&p->rc); 1.1649 + return CheckErrors(p); 1.1650 +} 1.1651 + 1.1652 +static void FillAlignPrices(CLzmaEnc *p) 1.1653 +{ 1.1654 + UInt32 i; 1.1655 + for (i = 0; i < kAlignTableSize; i++) 1.1656 + p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices); 1.1657 + p->alignPriceCount = 0; 1.1658 +} 1.1659 + 1.1660 +static void FillDistancesPrices(CLzmaEnc *p) 1.1661 +{ 1.1662 + UInt32 tempPrices[kNumFullDistances]; 1.1663 + UInt32 i, lenToPosState; 1.1664 + for (i = kStartPosModelIndex; i < kNumFullDistances; i++) 1.1665 + { 1.1666 + UInt32 posSlot = GetPosSlot1(i); 1.1667 + UInt32 footerBits = ((posSlot >> 1) - 1); 1.1668 + UInt32 base = ((2 | (posSlot & 1)) << footerBits); 1.1669 + tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices); 1.1670 + } 1.1671 + 1.1672 + for (lenToPosState = 0; lenToPosState < kNumLenToPosStates; lenToPosState++) 1.1673 + { 1.1674 + UInt32 posSlot; 1.1675 + const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState]; 1.1676 + UInt32 *posSlotPrices = p->posSlotPrices[lenToPosState]; 1.1677 + for (posSlot = 0; posSlot < p->distTableSize; posSlot++) 1.1678 + posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices); 1.1679 + for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++) 1.1680 + posSlotPrices[posSlot] += ((((posSlot >> 1) - 1) - kNumAlignBits) << kNumBitPriceShiftBits); 1.1681 + 1.1682 + { 1.1683 + UInt32 *distancesPrices = p->distancesPrices[lenToPosState]; 1.1684 + UInt32 i; 1.1685 + for (i = 0; i < kStartPosModelIndex; i++) 1.1686 + distancesPrices[i] = posSlotPrices[i]; 1.1687 + for (; i < kNumFullDistances; i++) 1.1688 + distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i]; 1.1689 + } 1.1690 + } 1.1691 + p->matchPriceCount = 0; 1.1692 +} 1.1693 + 1.1694 +void LzmaEnc_Construct(CLzmaEnc *p) 1.1695 +{ 1.1696 + RangeEnc_Construct(&p->rc); 1.1697 + MatchFinder_Construct(&p->matchFinderBase); 1.1698 + #ifdef COMPRESS_MF_MT 1.1699 + MatchFinderMt_Construct(&p->matchFinderMt); 1.1700 + p->matchFinderMt.MatchFinder = &p->matchFinderBase; 1.1701 + #endif 1.1702 + 1.1703 + { 1.1704 + CLzmaEncProps props; 1.1705 + LzmaEncProps_Init(&props); 1.1706 + LzmaEnc_SetProps(p, &props); 1.1707 + } 1.1708 + 1.1709 + #ifndef LZMA_LOG_BSR 1.1710 + LzmaEnc_FastPosInit(p->g_FastPos); 1.1711 + #endif 1.1712 + 1.1713 + LzmaEnc_InitPriceTables(p->ProbPrices); 1.1714 + p->litProbs = 0; 1.1715 + p->saveState.litProbs = 0; 1.1716 +} 1.1717 + 1.1718 +CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc) 1.1719 +{ 1.1720 + void *p; 1.1721 + p = alloc->Alloc(alloc, sizeof(CLzmaEnc)); 1.1722 + if (p != 0) 1.1723 + LzmaEnc_Construct((CLzmaEnc *)p); 1.1724 + return p; 1.1725 +} 1.1726 + 1.1727 +void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc) 1.1728 +{ 1.1729 + alloc->Free(alloc, p->litProbs); 1.1730 + alloc->Free(alloc, p->saveState.litProbs); 1.1731 + p->litProbs = 0; 1.1732 + p->saveState.litProbs = 0; 1.1733 +} 1.1734 + 1.1735 +void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig) 1.1736 +{ 1.1737 + #ifdef COMPRESS_MF_MT 1.1738 + MatchFinderMt_Destruct(&p->matchFinderMt, allocBig); 1.1739 + #endif 1.1740 + MatchFinder_Free(&p->matchFinderBase, allocBig); 1.1741 + LzmaEnc_FreeLits(p, alloc); 1.1742 + RangeEnc_Free(&p->rc, alloc); 1.1743 +} 1.1744 + 1.1745 +void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig) 1.1746 +{ 1.1747 + LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig); 1.1748 + alloc->Free(alloc, p); 1.1749 +} 1.1750 + 1.1751 +static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize) 1.1752 +{ 1.1753 + UInt32 nowPos32, startPos32; 1.1754 + if (p->inStream != 0) 1.1755 + { 1.1756 + p->matchFinderBase.stream = p->inStream; 1.1757 + p->matchFinder.Init(p->matchFinderObj); 1.1758 + p->inStream = 0; 1.1759 + } 1.1760 + 1.1761 + if (p->finished) 1.1762 + return p->result; 1.1763 + RINOK(CheckErrors(p)); 1.1764 + 1.1765 + nowPos32 = (UInt32)p->nowPos64; 1.1766 + startPos32 = nowPos32; 1.1767 + 1.1768 + if (p->nowPos64 == 0) 1.1769 + { 1.1770 + UInt32 numPairs; 1.1771 + Byte curByte; 1.1772 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0) 1.1773 + return Flush(p, nowPos32); 1.1774 + ReadMatchDistances(p, &numPairs); 1.1775 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0); 1.1776 + p->state = kLiteralNextStates[p->state]; 1.1777 + curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset); 1.1778 + LitEnc_Encode(&p->rc, p->litProbs, curByte); 1.1779 + p->additionalOffset--; 1.1780 + nowPos32++; 1.1781 + } 1.1782 + 1.1783 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0) 1.1784 + for (;;) 1.1785 + { 1.1786 + UInt32 pos, len, posState; 1.1787 + 1.1788 + if (p->fastMode) 1.1789 + len = GetOptimumFast(p, &pos); 1.1790 + else 1.1791 + len = GetOptimum(p, nowPos32, &pos); 1.1792 + 1.1793 + #ifdef SHOW_STAT2 1.1794 + printf("\n pos = %4X, len = %d pos = %d", nowPos32, len, pos); 1.1795 + #endif 1.1796 + 1.1797 + posState = nowPos32 & p->pbMask; 1.1798 + if (len == 1 && pos == (UInt32)-1) 1.1799 + { 1.1800 + Byte curByte; 1.1801 + CLzmaProb *probs; 1.1802 + const Byte *data; 1.1803 + 1.1804 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0); 1.1805 + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset; 1.1806 + curByte = *data; 1.1807 + probs = LIT_PROBS(nowPos32, *(data - 1)); 1.1808 + if (IsCharState(p->state)) 1.1809 + LitEnc_Encode(&p->rc, probs, curByte); 1.1810 + else 1.1811 + LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0] - 1)); 1.1812 + p->state = kLiteralNextStates[p->state]; 1.1813 + } 1.1814 + else 1.1815 + { 1.1816 + RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1); 1.1817 + if (pos < LZMA_NUM_REPS) 1.1818 + { 1.1819 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 1); 1.1820 + if (pos == 0) 1.1821 + { 1.1822 + RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 0); 1.1823 + RangeEnc_EncodeBit(&p->rc, &p->isRep0Long[p->state][posState], ((len == 1) ? 0 : 1)); 1.1824 + } 1.1825 + else 1.1826 + { 1.1827 + UInt32 distance = p->reps[pos]; 1.1828 + RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 1); 1.1829 + if (pos == 1) 1.1830 + RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 0); 1.1831 + else 1.1832 + { 1.1833 + RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 1); 1.1834 + RangeEnc_EncodeBit(&p->rc, &p->isRepG2[p->state], pos - 2); 1.1835 + if (pos == 3) 1.1836 + p->reps[3] = p->reps[2]; 1.1837 + p->reps[2] = p->reps[1]; 1.1838 + } 1.1839 + p->reps[1] = p->reps[0]; 1.1840 + p->reps[0] = distance; 1.1841 + } 1.1842 + if (len == 1) 1.1843 + p->state = kShortRepNextStates[p->state]; 1.1844 + else 1.1845 + { 1.1846 + LenEnc_Encode2(&p->repLenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices); 1.1847 + p->state = kRepNextStates[p->state]; 1.1848 + } 1.1849 + } 1.1850 + else 1.1851 + { 1.1852 + UInt32 posSlot; 1.1853 + RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0); 1.1854 + p->state = kMatchNextStates[p->state]; 1.1855 + LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices); 1.1856 + pos -= LZMA_NUM_REPS; 1.1857 + GetPosSlot(pos, posSlot); 1.1858 + RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, posSlot); 1.1859 + 1.1860 + if (posSlot >= kStartPosModelIndex) 1.1861 + { 1.1862 + UInt32 footerBits = ((posSlot >> 1) - 1); 1.1863 + UInt32 base = ((2 | (posSlot & 1)) << footerBits); 1.1864 + UInt32 posReduced = pos - base; 1.1865 + 1.1866 + if (posSlot < kEndPosModelIndex) 1.1867 + RcTree_ReverseEncode(&p->rc, p->posEncoders + base - posSlot - 1, footerBits, posReduced); 1.1868 + else 1.1869 + { 1.1870 + RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits); 1.1871 + RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask); 1.1872 + p->alignPriceCount++; 1.1873 + } 1.1874 + } 1.1875 + p->reps[3] = p->reps[2]; 1.1876 + p->reps[2] = p->reps[1]; 1.1877 + p->reps[1] = p->reps[0]; 1.1878 + p->reps[0] = pos; 1.1879 + p->matchPriceCount++; 1.1880 + } 1.1881 + } 1.1882 + p->additionalOffset -= len; 1.1883 + nowPos32 += len; 1.1884 + if (p->additionalOffset == 0) 1.1885 + { 1.1886 + UInt32 processed; 1.1887 + if (!p->fastMode) 1.1888 + { 1.1889 + if (p->matchPriceCount >= (1 << 7)) 1.1890 + FillDistancesPrices(p); 1.1891 + if (p->alignPriceCount >= kAlignTableSize) 1.1892 + FillAlignPrices(p); 1.1893 + } 1.1894 + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0) 1.1895 + break; 1.1896 + processed = nowPos32 - startPos32; 1.1897 + if (useLimits) 1.1898 + { 1.1899 + if (processed + kNumOpts + 300 >= maxUnpackSize || 1.1900 + RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize) 1.1901 + break; 1.1902 + } 1.1903 + else if (processed >= (1 << 15)) 1.1904 + { 1.1905 + p->nowPos64 += nowPos32 - startPos32; 1.1906 + return CheckErrors(p); 1.1907 + } 1.1908 + } 1.1909 + } 1.1910 + p->nowPos64 += nowPos32 - startPos32; 1.1911 + return Flush(p, nowPos32); 1.1912 +} 1.1913 + 1.1914 +#define kBigHashDicLimit ((UInt32)1 << 24) 1.1915 + 1.1916 +static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig) 1.1917 +{ 1.1918 + UInt32 beforeSize = kNumOpts; 1.1919 + Bool btMode; 1.1920 + if (!RangeEnc_Alloc(&p->rc, alloc)) 1.1921 + return SZ_ERROR_MEM; 1.1922 + btMode = (p->matchFinderBase.btMode != 0); 1.1923 + #ifdef COMPRESS_MF_MT 1.1924 + p->mtMode = (p->multiThread && !p->fastMode && btMode); 1.1925 + #endif 1.1926 + 1.1927 + { 1.1928 + unsigned lclp = p->lc + p->lp; 1.1929 + if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp) 1.1930 + { 1.1931 + LzmaEnc_FreeLits(p, alloc); 1.1932 + p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb)); 1.1933 + p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb)); 1.1934 + if (p->litProbs == 0 || p->saveState.litProbs == 0) 1.1935 + { 1.1936 + LzmaEnc_FreeLits(p, alloc); 1.1937 + return SZ_ERROR_MEM; 1.1938 + } 1.1939 + p->lclp = lclp; 1.1940 + } 1.1941 + } 1.1942 + 1.1943 + p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit); 1.1944 + 1.1945 + if (beforeSize + p->dictSize < keepWindowSize) 1.1946 + beforeSize = keepWindowSize - p->dictSize; 1.1947 + 1.1948 + #ifdef COMPRESS_MF_MT 1.1949 + if (p->mtMode) 1.1950 + { 1.1951 + RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig)); 1.1952 + p->matchFinderObj = &p->matchFinderMt; 1.1953 + MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder); 1.1954 + } 1.1955 + else 1.1956 + #endif 1.1957 + { 1.1958 + if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig)) 1.1959 + return SZ_ERROR_MEM; 1.1960 + p->matchFinderObj = &p->matchFinderBase; 1.1961 + MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder); 1.1962 + } 1.1963 + return SZ_OK; 1.1964 +} 1.1965 + 1.1966 +void LzmaEnc_Init(CLzmaEnc *p) 1.1967 +{ 1.1968 + UInt32 i; 1.1969 + p->state = 0; 1.1970 + for (i = 0 ; i < LZMA_NUM_REPS; i++) 1.1971 + p->reps[i] = 0; 1.1972 + 1.1973 + RangeEnc_Init(&p->rc); 1.1974 + 1.1975 + 1.1976 + for (i = 0; i < kNumStates; i++) 1.1977 + { 1.1978 + UInt32 j; 1.1979 + for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++) 1.1980 + { 1.1981 + p->isMatch[i][j] = kProbInitValue; 1.1982 + p->isRep0Long[i][j] = kProbInitValue; 1.1983 + } 1.1984 + p->isRep[i] = kProbInitValue; 1.1985 + p->isRepG0[i] = kProbInitValue; 1.1986 + p->isRepG1[i] = kProbInitValue; 1.1987 + p->isRepG2[i] = kProbInitValue; 1.1988 + } 1.1989 + 1.1990 + { 1.1991 + UInt32 num = 0x300 << (p->lp + p->lc); 1.1992 + for (i = 0; i < num; i++) 1.1993 + p->litProbs[i] = kProbInitValue; 1.1994 + } 1.1995 + 1.1996 + { 1.1997 + for (i = 0; i < kNumLenToPosStates; i++) 1.1998 + { 1.1999 + CLzmaProb *probs = p->posSlotEncoder[i]; 1.2000 + UInt32 j; 1.2001 + for (j = 0; j < (1 << kNumPosSlotBits); j++) 1.2002 + probs[j] = kProbInitValue; 1.2003 + } 1.2004 + } 1.2005 + { 1.2006 + for (i = 0; i < kNumFullDistances - kEndPosModelIndex; i++) 1.2007 + p->posEncoders[i] = kProbInitValue; 1.2008 + } 1.2009 + 1.2010 + LenEnc_Init(&p->lenEnc.p); 1.2011 + LenEnc_Init(&p->repLenEnc.p); 1.2012 + 1.2013 + for (i = 0; i < (1 << kNumAlignBits); i++) 1.2014 + p->posAlignEncoder[i] = kProbInitValue; 1.2015 + 1.2016 + p->optimumEndIndex = 0; 1.2017 + p->optimumCurrentIndex = 0; 1.2018 + p->additionalOffset = 0; 1.2019 + 1.2020 + p->pbMask = (1 << p->pb) - 1; 1.2021 + p->lpMask = (1 << p->lp) - 1; 1.2022 +} 1.2023 + 1.2024 +void LzmaEnc_InitPrices(CLzmaEnc *p) 1.2025 +{ 1.2026 + if (!p->fastMode) 1.2027 + { 1.2028 + FillDistancesPrices(p); 1.2029 + FillAlignPrices(p); 1.2030 + } 1.2031 + 1.2032 + p->lenEnc.tableSize = 1.2033 + p->repLenEnc.tableSize = 1.2034 + p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN; 1.2035 + LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, p->ProbPrices); 1.2036 + LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices); 1.2037 +} 1.2038 + 1.2039 +static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig) 1.2040 +{ 1.2041 + UInt32 i; 1.2042 + for (i = 0; i < (UInt32)kDicLogSizeMaxCompress; i++) 1.2043 + if (p->dictSize <= ((UInt32)1 << i)) 1.2044 + break; 1.2045 + p->distTableSize = i * 2; 1.2046 + 1.2047 + p->finished = False; 1.2048 + p->result = SZ_OK; 1.2049 + RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig)); 1.2050 + LzmaEnc_Init(p); 1.2051 + LzmaEnc_InitPrices(p); 1.2052 + p->nowPos64 = 0; 1.2053 + return SZ_OK; 1.2054 +} 1.2055 + 1.2056 +static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqInStream *inStream, ISeqOutStream *outStream, 1.2057 + ISzAlloc *alloc, ISzAlloc *allocBig) 1.2058 +{ 1.2059 + CLzmaEnc *p = (CLzmaEnc *)pp; 1.2060 + p->inStream = inStream; 1.2061 + p->rc.outStream = outStream; 1.2062 + return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig); 1.2063 +} 1.2064 + 1.2065 +SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp, 1.2066 + ISeqInStream *inStream, UInt32 keepWindowSize, 1.2067 + ISzAlloc *alloc, ISzAlloc *allocBig) 1.2068 +{ 1.2069 + CLzmaEnc *p = (CLzmaEnc *)pp; 1.2070 + p->inStream = inStream; 1.2071 + return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig); 1.2072 +} 1.2073 + 1.2074 +static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen) 1.2075 +{ 1.2076 + p->seqBufInStream.funcTable.Read = MyRead; 1.2077 + p->seqBufInStream.data = src; 1.2078 + p->seqBufInStream.rem = srcLen; 1.2079 +} 1.2080 + 1.2081 +SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen, 1.2082 + UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig) 1.2083 +{ 1.2084 + CLzmaEnc *p = (CLzmaEnc *)pp; 1.2085 + LzmaEnc_SetInputBuf(p, src, srcLen); 1.2086 + p->inStream = &p->seqBufInStream.funcTable; 1.2087 + return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig); 1.2088 +} 1.2089 + 1.2090 +void LzmaEnc_Finish(CLzmaEncHandle pp) 1.2091 +{ 1.2092 + #ifdef COMPRESS_MF_MT 1.2093 + CLzmaEnc *p = (CLzmaEnc *)pp; 1.2094 + if (p->mtMode) 1.2095 + MatchFinderMt_ReleaseStream(&p->matchFinderMt); 1.2096 + #else 1.2097 + pp = pp; 1.2098 + #endif 1.2099 +} 1.2100 + 1.2101 +typedef struct _CSeqOutStreamBuf 1.2102 +{ 1.2103 + ISeqOutStream funcTable; 1.2104 + Byte *data; 1.2105 + SizeT rem; 1.2106 + Bool overflow; 1.2107 +} CSeqOutStreamBuf; 1.2108 + 1.2109 +static size_t MyWrite(void *pp, const void *data, size_t size) 1.2110 +{ 1.2111 + CSeqOutStreamBuf *p = (CSeqOutStreamBuf *)pp; 1.2112 + if (p->rem < size) 1.2113 + { 1.2114 + size = p->rem; 1.2115 + p->overflow = True; 1.2116 + } 1.2117 + memcpy(p->data, data, size); 1.2118 + p->rem -= size; 1.2119 + p->data += size; 1.2120 + return size; 1.2121 +} 1.2122 + 1.2123 + 1.2124 +UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp) 1.2125 +{ 1.2126 + const CLzmaEnc *p = (CLzmaEnc *)pp; 1.2127 + return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj); 1.2128 +} 1.2129 + 1.2130 +const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp) 1.2131 +{ 1.2132 + const CLzmaEnc *p = (CLzmaEnc *)pp; 1.2133 + return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset; 1.2134 +} 1.2135 + 1.2136 +SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, Bool reInit, 1.2137 + Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize) 1.2138 +{ 1.2139 + CLzmaEnc *p = (CLzmaEnc *)pp; 1.2140 + UInt64 nowPos64; 1.2141 + SRes res; 1.2142 + CSeqOutStreamBuf outStream; 1.2143 + 1.2144 + outStream.funcTable.Write = MyWrite; 1.2145 + outStream.data = dest; 1.2146 + outStream.rem = *destLen; 1.2147 + outStream.overflow = False; 1.2148 + 1.2149 + p->writeEndMark = False; 1.2150 + p->finished = False; 1.2151 + p->result = SZ_OK; 1.2152 + 1.2153 + if (reInit) 1.2154 + LzmaEnc_Init(p); 1.2155 + LzmaEnc_InitPrices(p); 1.2156 + nowPos64 = p->nowPos64; 1.2157 + RangeEnc_Init(&p->rc); 1.2158 + p->rc.outStream = &outStream.funcTable; 1.2159 + 1.2160 + res = LzmaEnc_CodeOneBlock(p, True, desiredPackSize, *unpackSize); 1.2161 + 1.2162 + *unpackSize = (UInt32)(p->nowPos64 - nowPos64); 1.2163 + *destLen -= outStream.rem; 1.2164 + if (outStream.overflow) 1.2165 + return SZ_ERROR_OUTPUT_EOF; 1.2166 + 1.2167 + return res; 1.2168 +} 1.2169 + 1.2170 +SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress, 1.2171 + ISzAlloc *alloc, ISzAlloc *allocBig) 1.2172 +{ 1.2173 + CLzmaEnc *p = (CLzmaEnc *)pp; 1.2174 + SRes res = SZ_OK; 1.2175 + 1.2176 + #ifdef COMPRESS_MF_MT 1.2177 + Byte allocaDummy[0x300]; 1.2178 + int i = 0; 1.2179 + for (i = 0; i < 16; i++) 1.2180 + allocaDummy[i] = (Byte)i; 1.2181 + #endif 1.2182 + 1.2183 + RINOK(LzmaEnc_Prepare(pp, inStream, outStream, alloc, allocBig)); 1.2184 + 1.2185 + for (;;) 1.2186 + { 1.2187 + res = LzmaEnc_CodeOneBlock(p, False, 0, 0); 1.2188 + if (res != SZ_OK || p->finished != 0) 1.2189 + break; 1.2190 + if (progress != 0) 1.2191 + { 1.2192 + res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc)); 1.2193 + if (res != SZ_OK) 1.2194 + { 1.2195 + res = SZ_ERROR_PROGRESS; 1.2196 + break; 1.2197 + } 1.2198 + } 1.2199 + } 1.2200 + LzmaEnc_Finish(pp); 1.2201 + return res; 1.2202 +} 1.2203 + 1.2204 +SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size) 1.2205 +{ 1.2206 + CLzmaEnc *p = (CLzmaEnc *)pp; 1.2207 + int i; 1.2208 + UInt32 dictSize = p->dictSize; 1.2209 + if (*size < LZMA_PROPS_SIZE) 1.2210 + return SZ_ERROR_PARAM; 1.2211 + *size = LZMA_PROPS_SIZE; 1.2212 + props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc); 1.2213 + 1.2214 + for (i = 11; i <= 30; i++) 1.2215 + { 1.2216 + if (dictSize <= ((UInt32)2 << i)) 1.2217 + { 1.2218 + dictSize = (2 << i); 1.2219 + break; 1.2220 + } 1.2221 + if (dictSize <= ((UInt32)3 << i)) 1.2222 + { 1.2223 + dictSize = (3 << i); 1.2224 + break; 1.2225 + } 1.2226 + } 1.2227 + 1.2228 + for (i = 0; i < 4; i++) 1.2229 + props[1 + i] = (Byte)(dictSize >> (8 * i)); 1.2230 + return SZ_OK; 1.2231 +} 1.2232 + 1.2233 +SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, 1.2234 + int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig) 1.2235 +{ 1.2236 + SRes res; 1.2237 + CLzmaEnc *p = (CLzmaEnc *)pp; 1.2238 + 1.2239 + CSeqOutStreamBuf outStream; 1.2240 + 1.2241 + LzmaEnc_SetInputBuf(p, src, srcLen); 1.2242 + 1.2243 + outStream.funcTable.Write = MyWrite; 1.2244 + outStream.data = dest; 1.2245 + outStream.rem = *destLen; 1.2246 + outStream.overflow = False; 1.2247 + 1.2248 + p->writeEndMark = writeEndMark; 1.2249 + res = LzmaEnc_Encode(pp, &outStream.funcTable, &p->seqBufInStream.funcTable, 1.2250 + progress, alloc, allocBig); 1.2251 + 1.2252 + *destLen -= outStream.rem; 1.2253 + if (outStream.overflow) 1.2254 + return SZ_ERROR_OUTPUT_EOF; 1.2255 + return res; 1.2256 +} 1.2257 + 1.2258 +SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, 1.2259 + const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark, 1.2260 + ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig) 1.2261 +{ 1.2262 + CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc); 1.2263 + SRes res; 1.2264 + if (p == 0) 1.2265 + return SZ_ERROR_MEM; 1.2266 + 1.2267 + res = LzmaEnc_SetProps(p, props); 1.2268 + if (res == SZ_OK) 1.2269 + { 1.2270 + res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize); 1.2271 + if (res == SZ_OK) 1.2272 + res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen, 1.2273 + writeEndMark, progress, alloc, allocBig); 1.2274 + } 1.2275 + 1.2276 + LzmaEnc_Destroy(p, alloc, allocBig); 1.2277 + return res; 1.2278 +}