Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2274 lines
61 KiB

  1. /* LzmaEnc.c -- LZMA Encoder
  2. 2014-12-29 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #include <string.h>
  5. /* #define SHOW_STAT */
  6. /* #define SHOW_STAT2 */
  7. #if defined(SHOW_STAT) || defined(SHOW_STAT2)
  8. #include <stdio.h>
  9. #endif
  10. #include "LzmaEnc.h"
  11. #include "LzFind.h"
  12. #ifndef _7ZIP_ST
  13. #include "LzFindMt.h"
  14. #endif
  15. #ifdef SHOW_STAT
  16. static unsigned g_STAT_OFFSET = 0;
  17. #endif
  18. #define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1)
  19. #define kBlockSize (9 << 10)
  20. #define kUnpackBlockSize (1 << 18)
  21. #define kMatchArraySize (1 << 21)
  22. #define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX)
  23. #define kNumMaxDirectBits (31)
  24. #define kNumTopBits 24
  25. #define kTopValue ((UInt32)1 << kNumTopBits)
  26. #define kNumBitModelTotalBits 11
  27. #define kBitModelTotal (1 << kNumBitModelTotalBits)
  28. #define kNumMoveBits 5
  29. #define kProbInitValue (kBitModelTotal >> 1)
  30. #define kNumMoveReducingBits 4
  31. #define kNumBitPriceShiftBits 4
  32. #define kBitPrice (1 << kNumBitPriceShiftBits)
  33. void LzmaEncProps_Init(CLzmaEncProps *p)
  34. {
  35. p->level = 5;
  36. p->dictSize = p->mc = 0;
  37. p->reduceSize = (UInt64)(Int64)-1;
  38. p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1;
  39. p->writeEndMark = 0;
  40. }
  41. void LzmaEncProps_Normalize(CLzmaEncProps *p)
  42. {
  43. int level = p->level;
  44. if (level < 0) level = 5;
  45. p->level = level;
  46. if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level == 6 ? (1 << 25) : (1 << 26)));
  47. if (p->dictSize > p->reduceSize)
  48. {
  49. unsigned i;
  50. for (i = 11; i <= 30; i++)
  51. {
  52. if ((UInt32)p->reduceSize <= ((UInt32)2 << i)) { p->dictSize = ((UInt32)2 << i); break; }
  53. if ((UInt32)p->reduceSize <= ((UInt32)3 << i)) { p->dictSize = ((UInt32)3 << i); break; }
  54. }
  55. }
  56. if (p->lc < 0) p->lc = 3;
  57. if (p->lp < 0) p->lp = 0;
  58. if (p->pb < 0) p->pb = 2;
  59. if (p->algo < 0) p->algo = (level < 5 ? 0 : 1);
  60. if (p->fb < 0) p->fb = (level < 7 ? 32 : 64);
  61. if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1);
  62. if (p->numHashBytes < 0) p->numHashBytes = 4;
  63. if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1);
  64. if (p->numThreads < 0)
  65. p->numThreads =
  66. #ifndef _7ZIP_ST
  67. ((p->btMode && p->algo) ? 2 : 1);
  68. #else
  69. 1;
  70. #endif
  71. }
  72. UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2)
  73. {
  74. CLzmaEncProps props = *props2;
  75. LzmaEncProps_Normalize(&props);
  76. return props.dictSize;
  77. }
  78. /* #define LZMA_LOG_BSR */
  79. /* Define it for Intel's CPU */
  80. #ifdef LZMA_LOG_BSR
  81. #define kDicLogSizeMaxCompress 30
  82. #define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res = (i + i) + ((pos >> (i - 1)) & 1); }
  83. UInt32 GetPosSlot1(UInt32 pos)
  84. {
  85. UInt32 res;
  86. BSR2_RET(pos, res);
  87. return res;
  88. }
  89. #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
  90. #define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); }
  91. #else
  92. #define kNumLogBits (9 + (int)sizeof(size_t) / 2)
  93. #define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7)
  94. void LzmaEnc_FastPosInit(Byte *g_FastPos)
  95. {
  96. int c = 2, slotFast;
  97. g_FastPos[0] = 0;
  98. g_FastPos[1] = 1;
  99. for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++)
  100. {
  101. UInt32 k = (1 << ((slotFast >> 1) - 1));
  102. UInt32 j;
  103. for (j = 0; j < k; j++, c++)
  104. g_FastPos[c] = (Byte)slotFast;
  105. }
  106. }
  107. #define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \
  108. (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \
  109. res = p->g_FastPos[pos >> i] + (i * 2); }
  110. /*
  111. #define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \
  112. p->g_FastPos[pos >> 6] + 12 : \
  113. p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; }
  114. */
  115. #define GetPosSlot1(pos) p->g_FastPos[pos]
  116. #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); }
  117. #define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos]; else BSR2_RET(pos, res); }
  118. #endif
  119. #define LZMA_NUM_REPS 4
  120. typedef unsigned CState;
  121. typedef struct
  122. {
  123. UInt32 price;
  124. CState state;
  125. int prev1IsChar;
  126. int prev2;
  127. UInt32 posPrev2;
  128. UInt32 backPrev2;
  129. UInt32 posPrev;
  130. UInt32 backPrev;
  131. UInt32 backs[LZMA_NUM_REPS];
  132. } COptimal;
  133. #define kNumOpts (1 << 12)
  134. #define kNumLenToPosStates 4
  135. #define kNumPosSlotBits 6
  136. #define kDicLogSizeMin 0
  137. #define kDicLogSizeMax 32
  138. #define kDistTableSizeMax (kDicLogSizeMax * 2)
  139. #define kNumAlignBits 4
  140. #define kAlignTableSize (1 << kNumAlignBits)
  141. #define kAlignMask (kAlignTableSize - 1)
  142. #define kStartPosModelIndex 4
  143. #define kEndPosModelIndex 14
  144. #define kNumPosModels (kEndPosModelIndex - kStartPosModelIndex)
  145. #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
  146. #ifdef _LZMA_PROB32
  147. #define CLzmaProb UInt32
  148. #else
  149. #define CLzmaProb UInt16
  150. #endif
  151. #define LZMA_PB_MAX 4
  152. #define LZMA_LC_MAX 8
  153. #define LZMA_LP_MAX 4
  154. #define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX)
  155. #define kLenNumLowBits 3
  156. #define kLenNumLowSymbols (1 << kLenNumLowBits)
  157. #define kLenNumMidBits 3
  158. #define kLenNumMidSymbols (1 << kLenNumMidBits)
  159. #define kLenNumHighBits 8
  160. #define kLenNumHighSymbols (1 << kLenNumHighBits)
  161. #define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
  162. #define LZMA_MATCH_LEN_MIN 2
  163. #define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1)
  164. #define kNumStates 12
  165. typedef struct
  166. {
  167. CLzmaProb choice;
  168. CLzmaProb choice2;
  169. CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits];
  170. CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits];
  171. CLzmaProb high[kLenNumHighSymbols];
  172. } CLenEnc;
  173. typedef struct
  174. {
  175. CLenEnc p;
  176. UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal];
  177. UInt32 tableSize;
  178. UInt32 counters[LZMA_NUM_PB_STATES_MAX];
  179. } CLenPriceEnc;
  180. typedef struct
  181. {
  182. UInt32 range;
  183. Byte cache;
  184. UInt64 low;
  185. UInt64 cacheSize;
  186. Byte *buf;
  187. Byte *bufLim;
  188. Byte *bufBase;
  189. ISeqOutStream *outStream;
  190. UInt64 processed;
  191. SRes res;
  192. } CRangeEnc;
  193. typedef struct
  194. {
  195. CLzmaProb *litProbs;
  196. CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
  197. CLzmaProb isRep[kNumStates];
  198. CLzmaProb isRepG0[kNumStates];
  199. CLzmaProb isRepG1[kNumStates];
  200. CLzmaProb isRepG2[kNumStates];
  201. CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
  202. CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
  203. CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
  204. CLzmaProb posAlignEncoder[1 << kNumAlignBits];
  205. CLenPriceEnc lenEnc;
  206. CLenPriceEnc repLenEnc;
  207. UInt32 reps[LZMA_NUM_REPS];
  208. UInt32 state;
  209. } CSaveState;
  210. typedef struct
  211. {
  212. IMatchFinder matchFinder;
  213. void *matchFinderObj;
  214. #ifndef _7ZIP_ST
  215. Bool mtMode;
  216. CMatchFinderMt matchFinderMt;
  217. #endif
  218. CMatchFinder matchFinderBase;
  219. #ifndef _7ZIP_ST
  220. Byte pad[128];
  221. #endif
  222. UInt32 optimumEndIndex;
  223. UInt32 optimumCurrentIndex;
  224. UInt32 longestMatchLength;
  225. UInt32 numPairs;
  226. UInt32 numAvail;
  227. COptimal opt[kNumOpts];
  228. #ifndef LZMA_LOG_BSR
  229. Byte g_FastPos[1 << kNumLogBits];
  230. #endif
  231. UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits];
  232. UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1];
  233. UInt32 numFastBytes;
  234. UInt32 additionalOffset;
  235. UInt32 reps[LZMA_NUM_REPS];
  236. UInt32 state;
  237. UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax];
  238. UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances];
  239. UInt32 alignPrices[kAlignTableSize];
  240. UInt32 alignPriceCount;
  241. UInt32 distTableSize;
  242. unsigned lc, lp, pb;
  243. unsigned lpMask, pbMask;
  244. CLzmaProb *litProbs;
  245. CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX];
  246. CLzmaProb isRep[kNumStates];
  247. CLzmaProb isRepG0[kNumStates];
  248. CLzmaProb isRepG1[kNumStates];
  249. CLzmaProb isRepG2[kNumStates];
  250. CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX];
  251. CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits];
  252. CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex];
  253. CLzmaProb posAlignEncoder[1 << kNumAlignBits];
  254. CLenPriceEnc lenEnc;
  255. CLenPriceEnc repLenEnc;
  256. unsigned lclp;
  257. Bool fastMode;
  258. CRangeEnc rc;
  259. Bool writeEndMark;
  260. UInt64 nowPos64;
  261. UInt32 matchPriceCount;
  262. Bool finished;
  263. Bool multiThread;
  264. SRes result;
  265. UInt32 dictSize;
  266. int needInit;
  267. CSaveState saveState;
  268. } CLzmaEnc;
  269. void LzmaEnc_SaveState(CLzmaEncHandle pp)
  270. {
  271. CLzmaEnc *p = (CLzmaEnc *)pp;
  272. CSaveState *dest = &p->saveState;
  273. int i;
  274. dest->lenEnc = p->lenEnc;
  275. dest->repLenEnc = p->repLenEnc;
  276. dest->state = p->state;
  277. for (i = 0; i < kNumStates; i++)
  278. {
  279. memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
  280. memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
  281. }
  282. for (i = 0; i < kNumLenToPosStates; i++)
  283. memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
  284. memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
  285. memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
  286. memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
  287. memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
  288. memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
  289. memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
  290. memcpy(dest->reps, p->reps, sizeof(p->reps));
  291. memcpy(dest->litProbs, p->litProbs, (0x300 << p->lclp) * sizeof(CLzmaProb));
  292. }
  293. void LzmaEnc_RestoreState(CLzmaEncHandle pp)
  294. {
  295. CLzmaEnc *dest = (CLzmaEnc *)pp;
  296. const CSaveState *p = &dest->saveState;
  297. int i;
  298. dest->lenEnc = p->lenEnc;
  299. dest->repLenEnc = p->repLenEnc;
  300. dest->state = p->state;
  301. for (i = 0; i < kNumStates; i++)
  302. {
  303. memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i]));
  304. memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i]));
  305. }
  306. for (i = 0; i < kNumLenToPosStates; i++)
  307. memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncoder[i]));
  308. memcpy(dest->isRep, p->isRep, sizeof(p->isRep));
  309. memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0));
  310. memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1));
  311. memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2));
  312. memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders));
  313. memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder));
  314. memcpy(dest->reps, p->reps, sizeof(p->reps));
  315. memcpy(dest->litProbs, p->litProbs, (0x300 << dest->lclp) * sizeof(CLzmaProb));
  316. }
  317. SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2)
  318. {
  319. CLzmaEnc *p = (CLzmaEnc *)pp;
  320. CLzmaEncProps props = *props2;
  321. LzmaEncProps_Normalize(&props);
  322. if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX ||
  323. props.dictSize > ((UInt32)1 << kDicLogSizeMaxCompress) || props.dictSize > ((UInt32)1 << 30))
  324. return SZ_ERROR_PARAM;
  325. p->dictSize = props.dictSize;
  326. {
  327. unsigned fb = props.fb;
  328. if (fb < 5)
  329. fb = 5;
  330. if (fb > LZMA_MATCH_LEN_MAX)
  331. fb = LZMA_MATCH_LEN_MAX;
  332. p->numFastBytes = fb;
  333. }
  334. p->lc = props.lc;
  335. p->lp = props.lp;
  336. p->pb = props.pb;
  337. p->fastMode = (props.algo == 0);
  338. p->matchFinderBase.btMode = props.btMode;
  339. {
  340. UInt32 numHashBytes = 4;
  341. if (props.btMode)
  342. {
  343. if (props.numHashBytes < 2)
  344. numHashBytes = 2;
  345. else if (props.numHashBytes < 4)
  346. numHashBytes = props.numHashBytes;
  347. }
  348. p->matchFinderBase.numHashBytes = numHashBytes;
  349. }
  350. p->matchFinderBase.cutValue = props.mc;
  351. p->writeEndMark = props.writeEndMark;
  352. #ifndef _7ZIP_ST
  353. /*
  354. if (newMultiThread != _multiThread)
  355. {
  356. ReleaseMatchFinder();
  357. _multiThread = newMultiThread;
  358. }
  359. */
  360. p->multiThread = (props.numThreads > 1);
  361. #endif
  362. return SZ_OK;
  363. }
  364. static const int kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5};
  365. static const int kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10};
  366. static const int kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11};
  367. static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11};
  368. #define IsCharState(s) ((s) < 7)
  369. #define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1)
  370. #define kInfinityPrice (1 << 30)
  371. static void RangeEnc_Construct(CRangeEnc *p)
  372. {
  373. p->outStream = 0;
  374. p->bufBase = 0;
  375. }
  376. #define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize)
  377. #define RC_BUF_SIZE (1 << 16)
  378. static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc)
  379. {
  380. if (p->bufBase == 0)
  381. {
  382. p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE);
  383. if (p->bufBase == 0)
  384. return 0;
  385. p->bufLim = p->bufBase + RC_BUF_SIZE;
  386. }
  387. return 1;
  388. }
  389. static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc)
  390. {
  391. alloc->Free(alloc, p->bufBase);
  392. p->bufBase = 0;
  393. }
  394. static void RangeEnc_Init(CRangeEnc *p)
  395. {
  396. /* Stream.Init(); */
  397. p->low = 0;
  398. p->range = 0xFFFFFFFF;
  399. p->cacheSize = 1;
  400. p->cache = 0;
  401. p->buf = p->bufBase;
  402. p->processed = 0;
  403. p->res = SZ_OK;
  404. }
  405. static void RangeEnc_FlushStream(CRangeEnc *p)
  406. {
  407. size_t num;
  408. if (p->res != SZ_OK)
  409. return;
  410. num = p->buf - p->bufBase;
  411. if (num != p->outStream->Write(p->outStream, p->bufBase, num))
  412. p->res = SZ_ERROR_WRITE;
  413. p->processed += num;
  414. p->buf = p->bufBase;
  415. }
  416. static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p)
  417. {
  418. if ((UInt32)p->low < (UInt32)0xFF000000 || (unsigned)(p->low >> 32) != 0)
  419. {
  420. Byte temp = p->cache;
  421. do
  422. {
  423. Byte *buf = p->buf;
  424. *buf++ = (Byte)(temp + (Byte)(p->low >> 32));
  425. p->buf = buf;
  426. if (buf == p->bufLim)
  427. RangeEnc_FlushStream(p);
  428. temp = 0xFF;
  429. }
  430. while (--p->cacheSize != 0);
  431. p->cache = (Byte)((UInt32)p->low >> 24);
  432. }
  433. p->cacheSize++;
  434. p->low = (UInt32)p->low << 8;
  435. }
  436. static void RangeEnc_FlushData(CRangeEnc *p)
  437. {
  438. int i;
  439. for (i = 0; i < 5; i++)
  440. RangeEnc_ShiftLow(p);
  441. }
  442. static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, unsigned numBits)
  443. {
  444. do
  445. {
  446. p->range >>= 1;
  447. p->low += p->range & (0 - ((value >> --numBits) & 1));
  448. if (p->range < kTopValue)
  449. {
  450. p->range <<= 8;
  451. RangeEnc_ShiftLow(p);
  452. }
  453. }
  454. while (numBits != 0);
  455. }
  456. static void RangeEnc_EncodeBit(CRangeEnc *p, CLzmaProb *prob, UInt32 symbol)
  457. {
  458. UInt32 ttt = *prob;
  459. UInt32 newBound = (p->range >> kNumBitModelTotalBits) * ttt;
  460. if (symbol == 0)
  461. {
  462. p->range = newBound;
  463. ttt += (kBitModelTotal - ttt) >> kNumMoveBits;
  464. }
  465. else
  466. {
  467. p->low += newBound;
  468. p->range -= newBound;
  469. ttt -= ttt >> kNumMoveBits;
  470. }
  471. *prob = (CLzmaProb)ttt;
  472. if (p->range < kTopValue)
  473. {
  474. p->range <<= 8;
  475. RangeEnc_ShiftLow(p);
  476. }
  477. }
  478. static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol)
  479. {
  480. symbol |= 0x100;
  481. do
  482. {
  483. RangeEnc_EncodeBit(p, probs + (symbol >> 8), (symbol >> 7) & 1);
  484. symbol <<= 1;
  485. }
  486. while (symbol < 0x10000);
  487. }
  488. static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 symbol, UInt32 matchByte)
  489. {
  490. UInt32 offs = 0x100;
  491. symbol |= 0x100;
  492. do
  493. {
  494. matchByte <<= 1;
  495. RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (symbol >> 7) & 1);
  496. symbol <<= 1;
  497. offs &= ~(matchByte ^ symbol);
  498. }
  499. while (symbol < 0x10000);
  500. }
  501. void LzmaEnc_InitPriceTables(UInt32 *ProbPrices)
  502. {
  503. UInt32 i;
  504. for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumMoveReducingBits))
  505. {
  506. const int kCyclesBits = kNumBitPriceShiftBits;
  507. UInt32 w = i;
  508. UInt32 bitCount = 0;
  509. int j;
  510. for (j = 0; j < kCyclesBits; j++)
  511. {
  512. w = w * w;
  513. bitCount <<= 1;
  514. while (w >= ((UInt32)1 << 16))
  515. {
  516. w >>= 1;
  517. bitCount++;
  518. }
  519. }
  520. ProbPrices[i >> kNumMoveReducingBits] = ((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount);
  521. }
  522. }
  523. #define GET_PRICE(prob, symbol) \
  524. p->ProbPrices[((prob) ^ (((-(int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
  525. #define GET_PRICEa(prob, symbol) \
  526. ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits];
  527. #define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits]
  528. #define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
  529. #define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits]
  530. #define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits]
  531. static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *ProbPrices)
  532. {
  533. UInt32 price = 0;
  534. symbol |= 0x100;
  535. do
  536. {
  537. price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1);
  538. symbol <<= 1;
  539. }
  540. while (symbol < 0x10000);
  541. return price;
  542. }
  543. static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt32 matchByte, UInt32 *ProbPrices)
  544. {
  545. UInt32 price = 0;
  546. UInt32 offs = 0x100;
  547. symbol |= 0x100;
  548. do
  549. {
  550. matchByte <<= 1;
  551. price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbol >> 7) & 1);
  552. symbol <<= 1;
  553. offs &= ~(matchByte ^ symbol);
  554. }
  555. while (symbol < 0x10000);
  556. return price;
  557. }
  558. static void RcTree_Encode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
  559. {
  560. UInt32 m = 1;
  561. int i;
  562. for (i = numBitLevels; i != 0;)
  563. {
  564. UInt32 bit;
  565. i--;
  566. bit = (symbol >> i) & 1;
  567. RangeEnc_EncodeBit(rc, probs + m, bit);
  568. m = (m << 1) | bit;
  569. }
  570. }
  571. static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, int numBitLevels, UInt32 symbol)
  572. {
  573. UInt32 m = 1;
  574. int i;
  575. for (i = 0; i < numBitLevels; i++)
  576. {
  577. UInt32 bit = symbol & 1;
  578. RangeEnc_EncodeBit(rc, probs + m, bit);
  579. m = (m << 1) | bit;
  580. symbol >>= 1;
  581. }
  582. }
  583. static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
  584. {
  585. UInt32 price = 0;
  586. symbol |= (1 << numBitLevels);
  587. while (symbol != 1)
  588. {
  589. price += GET_PRICEa(probs[symbol >> 1], symbol & 1);
  590. symbol >>= 1;
  591. }
  592. return price;
  593. }
  594. static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 symbol, UInt32 *ProbPrices)
  595. {
  596. UInt32 price = 0;
  597. UInt32 m = 1;
  598. int i;
  599. for (i = numBitLevels; i != 0; i--)
  600. {
  601. UInt32 bit = symbol & 1;
  602. symbol >>= 1;
  603. price += GET_PRICEa(probs[m], bit);
  604. m = (m << 1) | bit;
  605. }
  606. return price;
  607. }
  608. static void LenEnc_Init(CLenEnc *p)
  609. {
  610. unsigned i;
  611. p->choice = p->choice2 = kProbInitValue;
  612. for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumLowBits); i++)
  613. p->low[i] = kProbInitValue;
  614. for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << kLenNumMidBits); i++)
  615. p->mid[i] = kProbInitValue;
  616. for (i = 0; i < kLenNumHighSymbols; i++)
  617. p->high[i] = kProbInitValue;
  618. }
  619. static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState)
  620. {
  621. if (symbol < kLenNumLowSymbols)
  622. {
  623. RangeEnc_EncodeBit(rc, &p->choice, 0);
  624. RcTree_Encode(rc, p->low + (posState << kLenNumLowBits), kLenNumLowBits, symbol);
  625. }
  626. else
  627. {
  628. RangeEnc_EncodeBit(rc, &p->choice, 1);
  629. if (symbol < kLenNumLowSymbols + kLenNumMidSymbols)
  630. {
  631. RangeEnc_EncodeBit(rc, &p->choice2, 0);
  632. RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, symbol - kLenNumLowSymbols);
  633. }
  634. else
  635. {
  636. RangeEnc_EncodeBit(rc, &p->choice2, 1);
  637. RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - kLenNumMidSymbols);
  638. }
  639. }
  640. }
  641. static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UInt32 *prices, UInt32 *ProbPrices)
  642. {
  643. UInt32 a0 = GET_PRICE_0a(p->choice);
  644. UInt32 a1 = GET_PRICE_1a(p->choice);
  645. UInt32 b0 = a1 + GET_PRICE_0a(p->choice2);
  646. UInt32 b1 = a1 + GET_PRICE_1a(p->choice2);
  647. UInt32 i = 0;
  648. for (i = 0; i < kLenNumLowSymbols; i++)
  649. {
  650. if (i >= numSymbols)
  651. return;
  652. prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLenNumLowBits, i, ProbPrices);
  653. }
  654. for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++)
  655. {
  656. if (i >= numSymbols)
  657. return;
  658. prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLenNumMidBits, i - kLenNumLowSymbols, ProbPrices);
  659. }
  660. for (; i < numSymbols; i++)
  661. prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSymbols - kLenNumMidSymbols, ProbPrices);
  662. }
  663. static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posState, UInt32 *ProbPrices)
  664. {
  665. LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrices);
  666. p->counters[posState] = p->tableSize;
  667. }
  668. static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt32 *ProbPrices)
  669. {
  670. UInt32 posState;
  671. for (posState = 0; posState < numPosStates; posState++)
  672. LenPriceEnc_UpdateTable(p, posState, ProbPrices);
  673. }
  674. static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32 posState, Bool updatePrice, UInt32 *ProbPrices)
  675. {
  676. LenEnc_Encode(&p->p, rc, symbol, posState);
  677. if (updatePrice)
  678. if (--p->counters[posState] == 0)
  679. LenPriceEnc_UpdateTable(p, posState, ProbPrices);
  680. }
  681. static void MovePos(CLzmaEnc *p, UInt32 num)
  682. {
  683. #ifdef SHOW_STAT
  684. g_STAT_OFFSET += num;
  685. printf("\n MovePos %d", num);
  686. #endif
  687. if (num != 0)
  688. {
  689. p->additionalOffset += num;
  690. p->matchFinder.Skip(p->matchFinderObj, num);
  691. }
  692. }
  693. static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes)
  694. {
  695. UInt32 lenRes = 0, numPairs;
  696. p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
  697. numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches);
  698. #ifdef SHOW_STAT
  699. printf("\n i = %d numPairs = %d ", g_STAT_OFFSET, numPairs / 2);
  700. g_STAT_OFFSET++;
  701. {
  702. UInt32 i;
  703. for (i = 0; i < numPairs; i += 2)
  704. printf("%2d %6d | ", p->matches[i], p->matches[i + 1]);
  705. }
  706. #endif
  707. if (numPairs > 0)
  708. {
  709. lenRes = p->matches[numPairs - 2];
  710. if (lenRes == p->numFastBytes)
  711. {
  712. const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
  713. UInt32 distance = p->matches[numPairs - 1] + 1;
  714. UInt32 numAvail = p->numAvail;
  715. if (numAvail > LZMA_MATCH_LEN_MAX)
  716. numAvail = LZMA_MATCH_LEN_MAX;
  717. {
  718. const Byte *pby2 = pby - distance;
  719. for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++);
  720. }
  721. }
  722. }
  723. p->additionalOffset++;
  724. *numDistancePairsRes = numPairs;
  725. return lenRes;
  726. }
  727. #define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False;
  728. #define MakeAsShortRep(p) (p)->backPrev = 0; (p)->prev1IsChar = False;
  729. #define IsShortRep(p) ((p)->backPrev == 0)
  730. static UInt32 GetRepLen1Price(CLzmaEnc *p, UInt32 state, UInt32 posState)
  731. {
  732. return
  733. GET_PRICE_0(p->isRepG0[state]) +
  734. GET_PRICE_0(p->isRep0Long[state][posState]);
  735. }
  736. static UInt32 GetPureRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 state, UInt32 posState)
  737. {
  738. UInt32 price;
  739. if (repIndex == 0)
  740. {
  741. price = GET_PRICE_0(p->isRepG0[state]);
  742. price += GET_PRICE_1(p->isRep0Long[state][posState]);
  743. }
  744. else
  745. {
  746. price = GET_PRICE_1(p->isRepG0[state]);
  747. if (repIndex == 1)
  748. price += GET_PRICE_0(p->isRepG1[state]);
  749. else
  750. {
  751. price += GET_PRICE_1(p->isRepG1[state]);
  752. price += GET_PRICE(p->isRepG2[state], repIndex - 2);
  753. }
  754. }
  755. return price;
  756. }
  757. static UInt32 GetRepPrice(CLzmaEnc *p, UInt32 repIndex, UInt32 len, UInt32 state, UInt32 posState)
  758. {
  759. return p->repLenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN] +
  760. GetPureRepPrice(p, repIndex, state, posState);
  761. }
  762. static UInt32 Backward(CLzmaEnc *p, UInt32 *backRes, UInt32 cur)
  763. {
  764. UInt32 posMem = p->opt[cur].posPrev;
  765. UInt32 backMem = p->opt[cur].backPrev;
  766. p->optimumEndIndex = cur;
  767. do
  768. {
  769. if (p->opt[cur].prev1IsChar)
  770. {
  771. MakeAsChar(&p->opt[posMem])
  772. p->opt[posMem].posPrev = posMem - 1;
  773. if (p->opt[cur].prev2)
  774. {
  775. p->opt[posMem - 1].prev1IsChar = False;
  776. p->opt[posMem - 1].posPrev = p->opt[cur].posPrev2;
  777. p->opt[posMem - 1].backPrev = p->opt[cur].backPrev2;
  778. }
  779. }
  780. {
  781. UInt32 posPrev = posMem;
  782. UInt32 backCur = backMem;
  783. backMem = p->opt[posPrev].backPrev;
  784. posMem = p->opt[posPrev].posPrev;
  785. p->opt[posPrev].backPrev = backCur;
  786. p->opt[posPrev].posPrev = cur;
  787. cur = posPrev;
  788. }
  789. }
  790. while (cur != 0);
  791. *backRes = p->opt[0].backPrev;
  792. p->optimumCurrentIndex = p->opt[0].posPrev;
  793. return p->optimumCurrentIndex;
  794. }
  795. #define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc) + ((prevByte) >> (8 - p->lc))) * 0x300)
  796. static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes)
  797. {
  798. UInt32 numAvail, mainLen, numPairs, repMaxIndex, posState, lenEnd, len, cur;
  799. UInt32 matchPrice, repMatchPrice, normalMatchPrice;
  800. UInt32 reps[LZMA_NUM_REPS], repLens[LZMA_NUM_REPS];
  801. UInt32 *matches;
  802. const Byte *data;
  803. Byte curByte, matchByte;
  804. if (p->optimumEndIndex != p->optimumCurrentIndex)
  805. {
  806. const COptimal *opt = &p->opt[p->optimumCurrentIndex];
  807. UInt32 lenRes = opt->posPrev - p->optimumCurrentIndex;
  808. *backRes = opt->backPrev;
  809. p->optimumCurrentIndex = opt->posPrev;
  810. return lenRes;
  811. }
  812. p->optimumCurrentIndex = p->optimumEndIndex = 0;
  813. if (p->additionalOffset == 0)
  814. mainLen = ReadMatchDistances(p, &numPairs);
  815. else
  816. {
  817. mainLen = p->longestMatchLength;
  818. numPairs = p->numPairs;
  819. }
  820. numAvail = p->numAvail;
  821. if (numAvail < 2)
  822. {
  823. *backRes = (UInt32)(-1);
  824. return 1;
  825. }
  826. if (numAvail > LZMA_MATCH_LEN_MAX)
  827. numAvail = LZMA_MATCH_LEN_MAX;
  828. data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
  829. repMaxIndex = 0;
  830. for (int i = 0; i < LZMA_NUM_REPS; i++)
  831. {
  832. UInt32 lenTest;
  833. const Byte *data2;
  834. reps[i] = p->reps[i];
  835. data2 = data - (reps[i] + 1);
  836. if (data[0] != data2[0] || data[1] != data2[1])
  837. {
  838. repLens[i] = 0;
  839. continue;
  840. }
  841. for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
  842. repLens[i] = lenTest;
  843. if (lenTest > repLens[repMaxIndex])
  844. repMaxIndex = i;
  845. }
  846. if (repLens[repMaxIndex] >= p->numFastBytes)
  847. {
  848. UInt32 lenRes;
  849. *backRes = repMaxIndex;
  850. lenRes = repLens[repMaxIndex];
  851. MovePos(p, lenRes - 1);
  852. return lenRes;
  853. }
  854. matches = p->matches;
  855. if (mainLen >= p->numFastBytes)
  856. {
  857. *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
  858. MovePos(p, mainLen - 1);
  859. return mainLen;
  860. }
  861. curByte = *data;
  862. matchByte = *(data - (reps[0] + 1));
  863. if (mainLen < 2 && curByte != matchByte && repLens[repMaxIndex] < 2)
  864. {
  865. *backRes = (UInt32)-1;
  866. return 1;
  867. }
  868. p->opt[0].state = (CState)p->state;
  869. posState = (position & p->pbMask);
  870. {
  871. const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
  872. p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) +
  873. (!IsCharState(p->state) ?
  874. LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
  875. LitEnc_GetPrice(probs, curByte, p->ProbPrices));
  876. }
  877. MakeAsChar(&p->opt[1]);
  878. matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]);
  879. repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]);
  880. if (matchByte == curByte)
  881. {
  882. UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, p->state, posState);
  883. if (shortRepPrice < p->opt[1].price)
  884. {
  885. p->opt[1].price = shortRepPrice;
  886. MakeAsShortRep(&p->opt[1]);
  887. }
  888. }
  889. lenEnd = ((mainLen >= repLens[repMaxIndex]) ? mainLen : repLens[repMaxIndex]);
  890. if (lenEnd < 2)
  891. {
  892. *backRes = p->opt[1].backPrev;
  893. return 1;
  894. }
  895. p->opt[1].posPrev = 0;
  896. for (int i = 0; i < LZMA_NUM_REPS; i++)
  897. p->opt[0].backs[i] = reps[i];
  898. len = lenEnd;
  899. do
  900. p->opt[len--].price = kInfinityPrice;
  901. while (len >= 2);
  902. for (int i = 0; i < LZMA_NUM_REPS; i++)
  903. {
  904. UInt32 repLen = repLens[i];
  905. UInt32 price;
  906. if (repLen < 2)
  907. continue;
  908. price = repMatchPrice + GetPureRepPrice(p, i, p->state, posState);
  909. do
  910. {
  911. UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][repLen - 2];
  912. COptimal *opt = &p->opt[repLen];
  913. if (curAndLenPrice < opt->price)
  914. {
  915. opt->price = curAndLenPrice;
  916. opt->posPrev = 0;
  917. opt->backPrev = i;
  918. opt->prev1IsChar = False;
  919. }
  920. }
  921. while (--repLen >= 2);
  922. }
  923. normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]);
  924. len = ((repLens[0] >= 2) ? repLens[0] + 1 : 2);
  925. if (len <= mainLen)
  926. {
  927. UInt32 offs = 0;
  928. while (len > matches[offs])
  929. offs += 2;
  930. for (; ; len++)
  931. {
  932. COptimal *opt;
  933. UInt32 distance = matches[offs + 1];
  934. UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][len - LZMA_MATCH_LEN_MIN];
  935. UInt32 lenToPosState = GetLenToPosState(len);
  936. if (distance < kNumFullDistances)
  937. curAndLenPrice += p->distancesPrices[lenToPosState][distance];
  938. else
  939. {
  940. UInt32 slot;
  941. GetPosSlot2(distance, slot);
  942. curAndLenPrice += p->alignPrices[distance & kAlignMask] + p->posSlotPrices[lenToPosState][slot];
  943. }
  944. opt = &p->opt[len];
  945. if (curAndLenPrice < opt->price)
  946. {
  947. opt->price = curAndLenPrice;
  948. opt->posPrev = 0;
  949. opt->backPrev = distance + LZMA_NUM_REPS;
  950. opt->prev1IsChar = False;
  951. }
  952. if (len == matches[offs])
  953. {
  954. offs += 2;
  955. if (offs == numPairs)
  956. break;
  957. }
  958. }
  959. }
  960. cur = 0;
  961. #ifdef SHOW_STAT2
  962. if (position >= 0)
  963. {
  964. unsigned i;
  965. printf("\n pos = %4X", position);
  966. for (i = cur; i <= lenEnd; i++)
  967. printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price);
  968. }
  969. #endif
  970. for (;;)
  971. {
  972. UInt32 numAvailFull, newLen, posPrev, state, startLen;
  973. UInt32 curPrice, curAnd1Price;
  974. Bool nextIsChar;
  975. COptimal *curOpt;
  976. COptimal *nextOpt;
  977. cur++;
  978. if (cur == lenEnd)
  979. return Backward(p, backRes, cur);
  980. newLen = ReadMatchDistances(p, &numPairs);
  981. if (newLen >= p->numFastBytes)
  982. {
  983. p->numPairs = numPairs;
  984. p->longestMatchLength = newLen;
  985. return Backward(p, backRes, cur);
  986. }
  987. position++;
  988. curOpt = &p->opt[cur];
  989. posPrev = curOpt->posPrev;
  990. if (curOpt->prev1IsChar)
  991. {
  992. posPrev--;
  993. if (curOpt->prev2)
  994. {
  995. state = p->opt[curOpt->posPrev2].state;
  996. if (curOpt->backPrev2 < LZMA_NUM_REPS)
  997. state = kRepNextStates[state];
  998. else
  999. state = kMatchNextStates[state];
  1000. }
  1001. else
  1002. state = p->opt[posPrev].state;
  1003. state = kLiteralNextStates[state];
  1004. }
  1005. else
  1006. state = p->opt[posPrev].state;
  1007. if (posPrev == cur - 1)
  1008. {
  1009. if (IsShortRep(curOpt))
  1010. state = kShortRepNextStates[state];
  1011. else
  1012. state = kLiteralNextStates[state];
  1013. }
  1014. else
  1015. {
  1016. UInt32 pos;
  1017. const COptimal *prevOpt;
  1018. if (curOpt->prev1IsChar && curOpt->prev2)
  1019. {
  1020. posPrev = curOpt->posPrev2;
  1021. pos = curOpt->backPrev2;
  1022. state = kRepNextStates[state];
  1023. }
  1024. else
  1025. {
  1026. pos = curOpt->backPrev;
  1027. if (pos < LZMA_NUM_REPS)
  1028. state = kRepNextStates[state];
  1029. else
  1030. state = kMatchNextStates[state];
  1031. }
  1032. prevOpt = &p->opt[posPrev];
  1033. if (pos < LZMA_NUM_REPS)
  1034. {
  1035. UInt32 i;
  1036. reps[0] = prevOpt->backs[pos];
  1037. for (i = 1; i <= pos; i++)
  1038. reps[i] = prevOpt->backs[i - 1];
  1039. for (; i < LZMA_NUM_REPS; i++)
  1040. reps[i] = prevOpt->backs[i];
  1041. }
  1042. else
  1043. {
  1044. UInt32 i;
  1045. reps[0] = (pos - LZMA_NUM_REPS);
  1046. for (i = 1; i < LZMA_NUM_REPS; i++)
  1047. reps[i] = prevOpt->backs[i - 1];
  1048. }
  1049. }
  1050. curOpt->state = (CState)state;
  1051. curOpt->backs[0] = reps[0];
  1052. curOpt->backs[1] = reps[1];
  1053. curOpt->backs[2] = reps[2];
  1054. curOpt->backs[3] = reps[3];
  1055. curPrice = curOpt->price;
  1056. nextIsChar = False;
  1057. data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
  1058. curByte = *data;
  1059. matchByte = *(data - (reps[0] + 1));
  1060. posState = (position & p->pbMask);
  1061. curAnd1Price = curPrice + GET_PRICE_0(p->isMatch[state][posState]);
  1062. {
  1063. const CLzmaProb *probs = LIT_PROBS(position, *(data - 1));
  1064. curAnd1Price +=
  1065. (!IsCharState(state) ?
  1066. LitEnc_GetPriceMatched(probs, curByte, matchByte, p->ProbPrices) :
  1067. LitEnc_GetPrice(probs, curByte, p->ProbPrices));
  1068. }
  1069. nextOpt = &p->opt[cur + 1];
  1070. if (curAnd1Price < nextOpt->price)
  1071. {
  1072. nextOpt->price = curAnd1Price;
  1073. nextOpt->posPrev = cur;
  1074. MakeAsChar(nextOpt);
  1075. nextIsChar = True;
  1076. }
  1077. matchPrice = curPrice + GET_PRICE_1(p->isMatch[state][posState]);
  1078. repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]);
  1079. if (matchByte == curByte && !(nextOpt->posPrev < cur && nextOpt->backPrev == 0))
  1080. {
  1081. UInt32 shortRepPrice = repMatchPrice + GetRepLen1Price(p, state, posState);
  1082. if (shortRepPrice <= nextOpt->price)
  1083. {
  1084. nextOpt->price = shortRepPrice;
  1085. nextOpt->posPrev = cur;
  1086. MakeAsShortRep(nextOpt);
  1087. nextIsChar = True;
  1088. }
  1089. }
  1090. numAvailFull = p->numAvail;
  1091. {
  1092. UInt32 temp = kNumOpts - 1 - cur;
  1093. if (temp < numAvailFull)
  1094. numAvailFull = temp;
  1095. }
  1096. if (numAvailFull < 2)
  1097. continue;
  1098. numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes);
  1099. if (!nextIsChar && matchByte != curByte) /* speed optimization */
  1100. {
  1101. /* try Literal + rep0 */
  1102. UInt32 temp;
  1103. UInt32 lenTest2;
  1104. const Byte *data2 = data - (reps[0] + 1);
  1105. UInt32 limit = p->numFastBytes + 1;
  1106. if (limit > numAvailFull)
  1107. limit = numAvailFull;
  1108. for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++);
  1109. lenTest2 = temp - 1;
  1110. if (lenTest2 >= 2)
  1111. {
  1112. UInt32 state2 = kLiteralNextStates[state];
  1113. UInt32 posStateNext = (position + 1) & p->pbMask;
  1114. UInt32 nextRepMatchPrice = curAnd1Price +
  1115. GET_PRICE_1(p->isMatch[state2][posStateNext]) +
  1116. GET_PRICE_1(p->isRep[state2]);
  1117. /* for (; lenTest2 >= 2; lenTest2--) */
  1118. {
  1119. UInt32 curAndLenPrice;
  1120. COptimal *opt;
  1121. UInt32 offset = cur + 1 + lenTest2;
  1122. while (lenEnd < offset)
  1123. p->opt[++lenEnd].price = kInfinityPrice;
  1124. curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
  1125. opt = &p->opt[offset];
  1126. if (curAndLenPrice < opt->price)
  1127. {
  1128. opt->price = curAndLenPrice;
  1129. opt->posPrev = cur + 1;
  1130. opt->backPrev = 0;
  1131. opt->prev1IsChar = True;
  1132. opt->prev2 = False;
  1133. }
  1134. }
  1135. }
  1136. }
  1137. startLen = 2; /* speed optimization */
  1138. {
  1139. UInt32 repIndex;
  1140. for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++)
  1141. {
  1142. UInt32 lenTest;
  1143. UInt32 lenTestTemp;
  1144. UInt32 price;
  1145. const Byte *data2 = data - (reps[repIndex] + 1);
  1146. if (data[0] != data2[0] || data[1] != data2[1])
  1147. continue;
  1148. for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; lenTest++);
  1149. while (lenEnd < cur + lenTest)
  1150. p->opt[++lenEnd].price = kInfinityPrice;
  1151. lenTestTemp = lenTest;
  1152. price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState);
  1153. do
  1154. {
  1155. UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest - 2];
  1156. COptimal *opt = &p->opt[cur + lenTest];
  1157. if (curAndLenPrice < opt->price)
  1158. {
  1159. opt->price = curAndLenPrice;
  1160. opt->posPrev = cur;
  1161. opt->backPrev = repIndex;
  1162. opt->prev1IsChar = False;
  1163. }
  1164. }
  1165. while (--lenTest >= 2);
  1166. lenTest = lenTestTemp;
  1167. if (repIndex == 0)
  1168. startLen = lenTest + 1;
  1169. /* if (_maxMode) */
  1170. {
  1171. UInt32 lenTest2 = lenTest + 1;
  1172. UInt32 limit = lenTest2 + p->numFastBytes;
  1173. UInt32 nextRepMatchPrice;
  1174. if (limit > numAvailFull)
  1175. limit = numAvailFull;
  1176. for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
  1177. lenTest2 -= lenTest + 1;
  1178. if (lenTest2 >= 2)
  1179. {
  1180. UInt32 state2 = kRepNextStates[state];
  1181. UInt32 posStateNext = (position + lenTest) & p->pbMask;
  1182. UInt32 curAndLenCharPrice =
  1183. price + p->repLenEnc.prices[posState][lenTest - 2] +
  1184. GET_PRICE_0(p->isMatch[state2][posStateNext]) +
  1185. LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
  1186. data[lenTest], data2[lenTest], p->ProbPrices);
  1187. state2 = kLiteralNextStates[state2];
  1188. posStateNext = (position + lenTest + 1) & p->pbMask;
  1189. nextRepMatchPrice = curAndLenCharPrice +
  1190. GET_PRICE_1(p->isMatch[state2][posStateNext]) +
  1191. GET_PRICE_1(p->isRep[state2]);
  1192. /* for (; lenTest2 >= 2; lenTest2--) */
  1193. {
  1194. UInt32 curAndLenPrice;
  1195. COptimal *opt;
  1196. UInt32 offset = cur + lenTest + 1 + lenTest2;
  1197. while (lenEnd < offset)
  1198. p->opt[++lenEnd].price = kInfinityPrice;
  1199. curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
  1200. opt = &p->opt[offset];
  1201. if (curAndLenPrice < opt->price)
  1202. {
  1203. opt->price = curAndLenPrice;
  1204. opt->posPrev = cur + lenTest + 1;
  1205. opt->backPrev = 0;
  1206. opt->prev1IsChar = True;
  1207. opt->prev2 = True;
  1208. opt->posPrev2 = cur;
  1209. opt->backPrev2 = repIndex;
  1210. }
  1211. }
  1212. }
  1213. }
  1214. }
  1215. }
  1216. /* for (UInt32 lenTest = 2; lenTest <= newLen; lenTest++) */
  1217. if (newLen > numAvail)
  1218. {
  1219. newLen = numAvail;
  1220. for (numPairs = 0; newLen > matches[numPairs]; numPairs += 2);
  1221. matches[numPairs] = newLen;
  1222. numPairs += 2;
  1223. }
  1224. if (newLen >= startLen)
  1225. {
  1226. normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]);
  1227. UInt32 offs, curBack, posSlot;
  1228. UInt32 lenTest;
  1229. while (lenEnd < cur + newLen)
  1230. p->opt[++lenEnd].price = kInfinityPrice;
  1231. offs = 0;
  1232. while (startLen > matches[offs])
  1233. offs += 2;
  1234. curBack = matches[offs + 1];
  1235. GetPosSlot2(curBack, posSlot);
  1236. for (lenTest = /*2*/ startLen; ; lenTest++)
  1237. {
  1238. UInt32 curAndLenPrice = normalMatchPrice + p->lenEnc.prices[posState][lenTest - LZMA_MATCH_LEN_MIN];
  1239. UInt32 lenToPosState = GetLenToPosState(lenTest);
  1240. COptimal *opt;
  1241. if (curBack < kNumFullDistances)
  1242. curAndLenPrice += p->distancesPrices[lenToPosState][curBack];
  1243. else
  1244. curAndLenPrice += p->posSlotPrices[lenToPosState][posSlot] + p->alignPrices[curBack & kAlignMask];
  1245. opt = &p->opt[cur + lenTest];
  1246. if (curAndLenPrice < opt->price)
  1247. {
  1248. opt->price = curAndLenPrice;
  1249. opt->posPrev = cur;
  1250. opt->backPrev = curBack + LZMA_NUM_REPS;
  1251. opt->prev1IsChar = False;
  1252. }
  1253. if (/*_maxMode && */lenTest == matches[offs])
  1254. {
  1255. /* Try Match + Literal + Rep0 */
  1256. const Byte *data2 = data - (curBack + 1);
  1257. UInt32 lenTest2 = lenTest + 1;
  1258. UInt32 limit = lenTest2 + p->numFastBytes;
  1259. UInt32 nextRepMatchPrice;
  1260. if (limit > numAvailFull)
  1261. limit = numAvailFull;
  1262. for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2++);
  1263. lenTest2 -= lenTest + 1;
  1264. if (lenTest2 >= 2)
  1265. {
  1266. UInt32 state2 = kMatchNextStates[state];
  1267. UInt32 posStateNext = (position + lenTest) & p->pbMask;
  1268. UInt32 curAndLenCharPrice = curAndLenPrice +
  1269. GET_PRICE_0(p->isMatch[state2][posStateNext]) +
  1270. LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTest - 1]),
  1271. data[lenTest], data2[lenTest], p->ProbPrices);
  1272. state2 = kLiteralNextStates[state2];
  1273. posStateNext = (posStateNext + 1) & p->pbMask;
  1274. nextRepMatchPrice = curAndLenCharPrice +
  1275. GET_PRICE_1(p->isMatch[state2][posStateNext]) +
  1276. GET_PRICE_1(p->isRep[state2]);
  1277. /* for (; lenTest2 >= 2; lenTest2--) */
  1278. {
  1279. UInt32 offset = cur + lenTest + 1 + lenTest2;
  1280. while (lenEnd < offset)
  1281. p->opt[++lenEnd].price = kInfinityPrice;
  1282. curAndLenPrice = nextRepMatchPrice + GetRepPrice(p, 0, lenTest2, state2, posStateNext);
  1283. opt = &p->opt[offset];
  1284. if (curAndLenPrice < opt->price)
  1285. {
  1286. opt->price = curAndLenPrice;
  1287. opt->posPrev = cur + lenTest + 1;
  1288. opt->backPrev = 0;
  1289. opt->prev1IsChar = True;
  1290. opt->prev2 = True;
  1291. opt->posPrev2 = cur;
  1292. opt->backPrev2 = curBack + LZMA_NUM_REPS;
  1293. }
  1294. }
  1295. }
  1296. offs += 2;
  1297. if (offs == numPairs)
  1298. break;
  1299. curBack = matches[offs + 1];
  1300. if (curBack >= kNumFullDistances)
  1301. GetPosSlot2(curBack, posSlot);
  1302. }
  1303. }
  1304. }
  1305. }
  1306. }
  1307. #define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist))
  1308. static UInt32 GetOptimumFast(CLzmaEnc *p, UInt32 *backRes)
  1309. {
  1310. UInt32 numAvail, mainLen, mainDist, numPairs, repIndex, repLen, i;
  1311. const Byte *data;
  1312. const UInt32 *matches;
  1313. if (p->additionalOffset == 0)
  1314. mainLen = ReadMatchDistances(p, &numPairs);
  1315. else
  1316. {
  1317. mainLen = p->longestMatchLength;
  1318. numPairs = p->numPairs;
  1319. }
  1320. numAvail = p->numAvail;
  1321. *backRes = (UInt32)-1;
  1322. if (numAvail < 2)
  1323. return 1;
  1324. if (numAvail > LZMA_MATCH_LEN_MAX)
  1325. numAvail = LZMA_MATCH_LEN_MAX;
  1326. data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
  1327. repLen = repIndex = 0;
  1328. for (i = 0; i < LZMA_NUM_REPS; i++)
  1329. {
  1330. UInt32 len;
  1331. const Byte *data2 = data - (p->reps[i] + 1);
  1332. if (data[0] != data2[0] || data[1] != data2[1])
  1333. continue;
  1334. for (len = 2; len < numAvail && data[len] == data2[len]; len++);
  1335. if (len >= p->numFastBytes)
  1336. {
  1337. *backRes = i;
  1338. MovePos(p, len - 1);
  1339. return len;
  1340. }
  1341. if (len > repLen)
  1342. {
  1343. repIndex = i;
  1344. repLen = len;
  1345. }
  1346. }
  1347. matches = p->matches;
  1348. if (mainLen >= p->numFastBytes)
  1349. {
  1350. *backRes = matches[numPairs - 1] + LZMA_NUM_REPS;
  1351. MovePos(p, mainLen - 1);
  1352. return mainLen;
  1353. }
  1354. mainDist = 0; /* for GCC */
  1355. if (mainLen >= 2)
  1356. {
  1357. mainDist = matches[numPairs - 1];
  1358. while (numPairs > 2 && mainLen == matches[numPairs - 4] + 1)
  1359. {
  1360. if (!ChangePair(matches[numPairs - 3], mainDist))
  1361. break;
  1362. numPairs -= 2;
  1363. mainLen = matches[numPairs - 2];
  1364. mainDist = matches[numPairs - 1];
  1365. }
  1366. if (mainLen == 2 && mainDist >= 0x80)
  1367. mainLen = 1;
  1368. }
  1369. if (repLen >= 2 && (
  1370. (repLen + 1 >= mainLen) ||
  1371. (repLen + 2 >= mainLen && mainDist >= (1 << 9)) ||
  1372. (repLen + 3 >= mainLen && mainDist >= (1 << 15))))
  1373. {
  1374. *backRes = repIndex;
  1375. MovePos(p, repLen - 1);
  1376. return repLen;
  1377. }
  1378. if (mainLen < 2 || numAvail <= 2)
  1379. return 1;
  1380. p->longestMatchLength = ReadMatchDistances(p, &p->numPairs);
  1381. if (p->longestMatchLength >= 2)
  1382. {
  1383. UInt32 newDistance = matches[p->numPairs - 1];
  1384. if ((p->longestMatchLength >= mainLen && newDistance < mainDist) ||
  1385. (p->longestMatchLength == mainLen + 1 && !ChangePair(mainDist, newDistance)) ||
  1386. (p->longestMatchLength > mainLen + 1) ||
  1387. (p->longestMatchLength + 1 >= mainLen && mainLen >= 3 && ChangePair(newDistance, mainDist)))
  1388. return 1;
  1389. }
  1390. data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1;
  1391. for (i = 0; i < LZMA_NUM_REPS; i++)
  1392. {
  1393. UInt32 len, limit;
  1394. const Byte *data2 = data - (p->reps[i] + 1);
  1395. if (data[0] != data2[0] || data[1] != data2[1])
  1396. continue;
  1397. limit = mainLen - 1;
  1398. for (len = 2; len < limit && data[len] == data2[len]; len++);
  1399. if (len >= limit)
  1400. return 1;
  1401. }
  1402. *backRes = mainDist + LZMA_NUM_REPS;
  1403. MovePos(p, mainLen - 2);
  1404. return mainLen;
  1405. }
  1406. static void WriteEndMarker(CLzmaEnc *p, UInt32 posState)
  1407. {
  1408. UInt32 len;
  1409. RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
  1410. RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
  1411. p->state = kMatchNextStates[p->state];
  1412. len = LZMA_MATCH_LEN_MIN;
  1413. LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
  1414. RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, (1 << kNumPosSlotBits) - 1);
  1415. RangeEnc_EncodeDirectBits(&p->rc, (((UInt32)1 << 30) - 1) >> kNumAlignBits, 30 - kNumAlignBits);
  1416. RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask);
  1417. }
  1418. static SRes CheckErrors(CLzmaEnc *p)
  1419. {
  1420. if (p->result != SZ_OK)
  1421. return p->result;
  1422. if (p->rc.res != SZ_OK)
  1423. p->result = SZ_ERROR_WRITE;
  1424. if (p->matchFinderBase.result != SZ_OK)
  1425. p->result = SZ_ERROR_READ;
  1426. if (p->result != SZ_OK)
  1427. p->finished = True;
  1428. return p->result;
  1429. }
  1430. static SRes Flush(CLzmaEnc *p, UInt32 nowPos)
  1431. {
  1432. /* ReleaseMFStream(); */
  1433. p->finished = True;
  1434. if (p->writeEndMark)
  1435. WriteEndMarker(p, nowPos & p->pbMask);
  1436. RangeEnc_FlushData(&p->rc);
  1437. RangeEnc_FlushStream(&p->rc);
  1438. return CheckErrors(p);
  1439. }
  1440. static void FillAlignPrices(CLzmaEnc *p)
  1441. {
  1442. UInt32 i;
  1443. for (i = 0; i < kAlignTableSize; i++)
  1444. p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices);
  1445. p->alignPriceCount = 0;
  1446. }
  1447. static void FillDistancesPrices(CLzmaEnc *p)
  1448. {
  1449. UInt32 tempPrices[kNumFullDistances];
  1450. UInt32 i, lenToPosState;
  1451. for (i = kStartPosModelIndex; i < kNumFullDistances; i++)
  1452. {
  1453. UInt32 posSlot = GetPosSlot1(i);
  1454. UInt32 footerBits = ((posSlot >> 1) - 1);
  1455. UInt32 base = ((2 | (posSlot & 1)) << footerBits);
  1456. tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base - posSlot - 1, footerBits, i - base, p->ProbPrices);
  1457. }
  1458. for (lenToPosState = 0; lenToPosState < kNumLenToPosStates; lenToPosState++)
  1459. {
  1460. UInt32 posSlot;
  1461. const CLzmaProb *encoder = p->posSlotEncoder[lenToPosState];
  1462. UInt32 *posSlotPrices = p->posSlotPrices[lenToPosState];
  1463. for (posSlot = 0; posSlot < p->distTableSize; posSlot++)
  1464. posSlotPrices[posSlot] = RcTree_GetPrice(encoder, kNumPosSlotBits, posSlot, p->ProbPrices);
  1465. for (posSlot = kEndPosModelIndex; posSlot < p->distTableSize; posSlot++)
  1466. posSlotPrices[posSlot] += ((((posSlot >> 1) - 1) - kNumAlignBits) << kNumBitPriceShiftBits);
  1467. {
  1468. UInt32 *distancesPrices = p->distancesPrices[lenToPosState];
  1469. UInt32 j;
  1470. for (j = 0; j < kStartPosModelIndex; j++)
  1471. distancesPrices[j] = posSlotPrices[j];
  1472. for (; j < kNumFullDistances; j++)
  1473. distancesPrices[j] = posSlotPrices[GetPosSlot1(j)] + tempPrices[j];
  1474. }
  1475. }
  1476. p->matchPriceCount = 0;
  1477. }
  1478. void LzmaEnc_Construct(CLzmaEnc *p)
  1479. {
  1480. RangeEnc_Construct(&p->rc);
  1481. MatchFinder_Construct(&p->matchFinderBase);
  1482. #ifndef _7ZIP_ST
  1483. MatchFinderMt_Construct(&p->matchFinderMt);
  1484. p->matchFinderMt.MatchFinder = &p->matchFinderBase;
  1485. #endif
  1486. {
  1487. CLzmaEncProps props;
  1488. LzmaEncProps_Init(&props);
  1489. LzmaEnc_SetProps(p, &props);
  1490. }
  1491. #ifndef LZMA_LOG_BSR
  1492. LzmaEnc_FastPosInit(p->g_FastPos);
  1493. #endif
  1494. LzmaEnc_InitPriceTables(p->ProbPrices);
  1495. p->litProbs = 0;
  1496. p->saveState.litProbs = 0;
  1497. }
  1498. CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc)
  1499. {
  1500. void *p;
  1501. p = alloc->Alloc(alloc, sizeof(CLzmaEnc));
  1502. if (p != 0)
  1503. LzmaEnc_Construct((CLzmaEnc *)p);
  1504. return p;
  1505. }
  1506. void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc)
  1507. {
  1508. alloc->Free(alloc, p->litProbs);
  1509. alloc->Free(alloc, p->saveState.litProbs);
  1510. p->litProbs = 0;
  1511. p->saveState.litProbs = 0;
  1512. }
  1513. void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig)
  1514. {
  1515. #ifndef _7ZIP_ST
  1516. MatchFinderMt_Destruct(&p->matchFinderMt, allocBig);
  1517. #endif
  1518. MatchFinder_Free(&p->matchFinderBase, allocBig);
  1519. LzmaEnc_FreeLits(p, alloc);
  1520. RangeEnc_Free(&p->rc, alloc);
  1521. }
  1522. void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig)
  1523. {
  1524. LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig);
  1525. alloc->Free(alloc, p);
  1526. }
  1527. static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize, UInt32 maxUnpackSize)
  1528. {
  1529. UInt32 nowPos32, startPos32;
  1530. if (p->needInit)
  1531. {
  1532. p->matchFinder.Init(p->matchFinderObj);
  1533. p->needInit = 0;
  1534. }
  1535. if (p->finished)
  1536. return p->result;
  1537. RINOK(CheckErrors(p));
  1538. nowPos32 = (UInt32)p->nowPos64;
  1539. startPos32 = nowPos32;
  1540. if (p->nowPos64 == 0)
  1541. {
  1542. UInt32 numPairs;
  1543. Byte curByte;
  1544. if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
  1545. return Flush(p, nowPos32);
  1546. ReadMatchDistances(p, &numPairs);
  1547. RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0);
  1548. p->state = kLiteralNextStates[p->state];
  1549. curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOffset);
  1550. LitEnc_Encode(&p->rc, p->litProbs, curByte);
  1551. p->additionalOffset--;
  1552. nowPos32++;
  1553. }
  1554. if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0)
  1555. for (;;)
  1556. {
  1557. UInt32 pos, len, posState;
  1558. if (p->fastMode)
  1559. len = GetOptimumFast(p, &pos);
  1560. else
  1561. len = GetOptimum(p, nowPos32, &pos);
  1562. #ifdef SHOW_STAT2
  1563. printf("\n pos = %4X, len = %d pos = %d", nowPos32, len, pos);
  1564. #endif
  1565. posState = nowPos32 & p->pbMask;
  1566. if (len == 1 && pos == (UInt32)-1)
  1567. {
  1568. Byte curByte;
  1569. CLzmaProb *probs;
  1570. const Byte *data;
  1571. RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0);
  1572. data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
  1573. curByte = *data;
  1574. probs = LIT_PROBS(nowPos32, *(data - 1));
  1575. if (IsCharState(p->state))
  1576. LitEnc_Encode(&p->rc, probs, curByte);
  1577. else
  1578. LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0] - 1));
  1579. p->state = kLiteralNextStates[p->state];
  1580. }
  1581. else
  1582. {
  1583. RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 1);
  1584. if (pos < LZMA_NUM_REPS)
  1585. {
  1586. RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 1);
  1587. if (pos == 0)
  1588. {
  1589. RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 0);
  1590. RangeEnc_EncodeBit(&p->rc, &p->isRep0Long[p->state][posState], ((len == 1) ? 0 : 1));
  1591. }
  1592. else
  1593. {
  1594. UInt32 distance = p->reps[pos];
  1595. RangeEnc_EncodeBit(&p->rc, &p->isRepG0[p->state], 1);
  1596. if (pos == 1)
  1597. RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 0);
  1598. else
  1599. {
  1600. RangeEnc_EncodeBit(&p->rc, &p->isRepG1[p->state], 1);
  1601. RangeEnc_EncodeBit(&p->rc, &p->isRepG2[p->state], pos - 2);
  1602. if (pos == 3)
  1603. p->reps[3] = p->reps[2];
  1604. p->reps[2] = p->reps[1];
  1605. }
  1606. p->reps[1] = p->reps[0];
  1607. p->reps[0] = distance;
  1608. }
  1609. if (len == 1)
  1610. p->state = kShortRepNextStates[p->state];
  1611. else
  1612. {
  1613. LenEnc_Encode2(&p->repLenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
  1614. p->state = kRepNextStates[p->state];
  1615. }
  1616. }
  1617. else
  1618. {
  1619. UInt32 posSlot;
  1620. RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
  1621. p->state = kMatchNextStates[p->state];
  1622. LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
  1623. pos -= LZMA_NUM_REPS;
  1624. GetPosSlot(pos, posSlot);
  1625. RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, posSlot);
  1626. if (posSlot >= kStartPosModelIndex)
  1627. {
  1628. UInt32 footerBits = ((posSlot >> 1) - 1);
  1629. UInt32 base = ((2 | (posSlot & 1)) << footerBits);
  1630. UInt32 posReduced = pos - base;
  1631. if (posSlot < kEndPosModelIndex)
  1632. RcTree_ReverseEncode(&p->rc, p->posEncoders + base - posSlot - 1, footerBits, posReduced);
  1633. else
  1634. {
  1635. RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits);
  1636. RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask);
  1637. p->alignPriceCount++;
  1638. }
  1639. }
  1640. p->reps[3] = p->reps[2];
  1641. p->reps[2] = p->reps[1];
  1642. p->reps[1] = p->reps[0];
  1643. p->reps[0] = pos;
  1644. p->matchPriceCount++;
  1645. }
  1646. }
  1647. p->additionalOffset -= len;
  1648. nowPos32 += len;
  1649. if (p->additionalOffset == 0)
  1650. {
  1651. UInt32 processed;
  1652. if (!p->fastMode)
  1653. {
  1654. if (p->matchPriceCount >= (1 << 7))
  1655. FillDistancesPrices(p);
  1656. if (p->alignPriceCount >= kAlignTableSize)
  1657. FillAlignPrices(p);
  1658. }
  1659. if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0)
  1660. break;
  1661. processed = nowPos32 - startPos32;
  1662. if (useLimits)
  1663. {
  1664. if (processed + kNumOpts + 300 >= maxUnpackSize ||
  1665. RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize)
  1666. break;
  1667. }
  1668. else if (processed >= (1 << 15))
  1669. {
  1670. p->nowPos64 += nowPos32 - startPos32;
  1671. return CheckErrors(p);
  1672. }
  1673. }
  1674. }
  1675. p->nowPos64 += nowPos32 - startPos32;
  1676. return Flush(p, nowPos32);
  1677. }
  1678. #define kBigHashDicLimit ((UInt32)1 << 24)
  1679. static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
  1680. {
  1681. UInt32 beforeSize = kNumOpts;
  1682. if (!RangeEnc_Alloc(&p->rc, alloc))
  1683. return SZ_ERROR_MEM;
  1684. #ifndef _7ZIP_ST
  1685. p->mtMode = (p->multiThread && !p->fastMode && (p->matchFinderBase.btMode != 0));
  1686. #endif
  1687. {
  1688. unsigned lclp = p->lc + p->lp;
  1689. if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp)
  1690. {
  1691. LzmaEnc_FreeLits(p, alloc);
  1692. p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
  1693. p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CLzmaProb));
  1694. if (p->litProbs == 0 || p->saveState.litProbs == 0)
  1695. {
  1696. LzmaEnc_FreeLits(p, alloc);
  1697. return SZ_ERROR_MEM;
  1698. }
  1699. p->lclp = lclp;
  1700. }
  1701. }
  1702. p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit);
  1703. if (beforeSize + p->dictSize < keepWindowSize)
  1704. beforeSize = keepWindowSize - p->dictSize;
  1705. #ifndef _7ZIP_ST
  1706. if (p->mtMode)
  1707. {
  1708. RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig));
  1709. p->matchFinderObj = &p->matchFinderMt;
  1710. MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder);
  1711. }
  1712. else
  1713. #endif
  1714. {
  1715. if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig))
  1716. return SZ_ERROR_MEM;
  1717. p->matchFinderObj = &p->matchFinderBase;
  1718. MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder);
  1719. }
  1720. return SZ_OK;
  1721. }
  1722. void LzmaEnc_Init(CLzmaEnc *p)
  1723. {
  1724. UInt32 i;
  1725. p->state = 0;
  1726. for (i = 0 ; i < LZMA_NUM_REPS; i++)
  1727. p->reps[i] = 0;
  1728. RangeEnc_Init(&p->rc);
  1729. for (i = 0; i < kNumStates; i++)
  1730. {
  1731. UInt32 j;
  1732. for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++)
  1733. {
  1734. p->isMatch[i][j] = kProbInitValue;
  1735. p->isRep0Long[i][j] = kProbInitValue;
  1736. }
  1737. p->isRep[i] = kProbInitValue;
  1738. p->isRepG0[i] = kProbInitValue;
  1739. p->isRepG1[i] = kProbInitValue;
  1740. p->isRepG2[i] = kProbInitValue;
  1741. }
  1742. {
  1743. UInt32 num = 0x300 << (p->lp + p->lc);
  1744. for (i = 0; i < num; i++)
  1745. p->litProbs[i] = kProbInitValue;
  1746. }
  1747. {
  1748. for (i = 0; i < kNumLenToPosStates; i++)
  1749. {
  1750. CLzmaProb *probs = p->posSlotEncoder[i];
  1751. UInt32 j;
  1752. for (j = 0; j < (1 << kNumPosSlotBits); j++)
  1753. probs[j] = kProbInitValue;
  1754. }
  1755. }
  1756. {
  1757. for (i = 0; i < kNumFullDistances - kEndPosModelIndex; i++)
  1758. p->posEncoders[i] = kProbInitValue;
  1759. }
  1760. LenEnc_Init(&p->lenEnc.p);
  1761. LenEnc_Init(&p->repLenEnc.p);
  1762. for (i = 0; i < (1 << kNumAlignBits); i++)
  1763. p->posAlignEncoder[i] = kProbInitValue;
  1764. p->optimumEndIndex = 0;
  1765. p->optimumCurrentIndex = 0;
  1766. p->additionalOffset = 0;
  1767. p->pbMask = (1 << p->pb) - 1;
  1768. p->lpMask = (1 << p->lp) - 1;
  1769. }
  1770. void LzmaEnc_InitPrices(CLzmaEnc *p)
  1771. {
  1772. if (!p->fastMode)
  1773. {
  1774. FillDistancesPrices(p);
  1775. FillAlignPrices(p);
  1776. }
  1777. p->lenEnc.tableSize =
  1778. p->repLenEnc.tableSize =
  1779. p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN;
  1780. LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, p->ProbPrices);
  1781. LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, p->ProbPrices);
  1782. }
  1783. static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
  1784. {
  1785. UInt32 i;
  1786. for (i = 0; i < (UInt32)kDicLogSizeMaxCompress; i++)
  1787. if (p->dictSize <= ((UInt32)1 << i))
  1788. break;
  1789. p->distTableSize = i * 2;
  1790. p->finished = False;
  1791. p->result = SZ_OK;
  1792. RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig));
  1793. LzmaEnc_Init(p);
  1794. LzmaEnc_InitPrices(p);
  1795. p->nowPos64 = 0;
  1796. return SZ_OK;
  1797. }
  1798. static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream,
  1799. ISzAlloc *alloc, ISzAlloc *allocBig)
  1800. {
  1801. CLzmaEnc *p = (CLzmaEnc *)pp;
  1802. p->matchFinderBase.stream = inStream;
  1803. p->needInit = 1;
  1804. p->rc.outStream = outStream;
  1805. return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig);
  1806. }
  1807. SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp,
  1808. ISeqInStream *inStream, UInt32 keepWindowSize,
  1809. ISzAlloc *alloc, ISzAlloc *allocBig)
  1810. {
  1811. CLzmaEnc *p = (CLzmaEnc *)pp;
  1812. p->matchFinderBase.stream = inStream;
  1813. p->needInit = 1;
  1814. return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
  1815. }
  1816. static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen)
  1817. {
  1818. p->matchFinderBase.directInput = 1;
  1819. p->matchFinderBase.bufferBase = (Byte *)src;
  1820. p->matchFinderBase.directInputRem = srcLen;
  1821. }
  1822. SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen,
  1823. UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig)
  1824. {
  1825. CLzmaEnc *p = (CLzmaEnc *)pp;
  1826. LzmaEnc_SetInputBuf(p, src, srcLen);
  1827. p->needInit = 1;
  1828. return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig);
  1829. }
  1830. void LzmaEnc_Finish(CLzmaEncHandle pp)
  1831. {
  1832. #ifndef _7ZIP_ST
  1833. CLzmaEnc *p = (CLzmaEnc *)pp;
  1834. if (p->mtMode)
  1835. MatchFinderMt_ReleaseStream(&p->matchFinderMt);
  1836. #else
  1837. pp = pp;
  1838. #endif
  1839. }
  1840. typedef struct
  1841. {
  1842. ISeqOutStream funcTable;
  1843. Byte *data;
  1844. SizeT rem;
  1845. Bool overflow;
  1846. } CSeqOutStreamBuf;
  1847. static size_t MyWrite(void *pp, const void *data, size_t size)
  1848. {
  1849. CSeqOutStreamBuf *p = (CSeqOutStreamBuf *)pp;
  1850. if (p->rem < size)
  1851. {
  1852. size = p->rem;
  1853. p->overflow = True;
  1854. }
  1855. memcpy(p->data, data, size);
  1856. p->rem -= size;
  1857. p->data += size;
  1858. return size;
  1859. }
  1860. UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp)
  1861. {
  1862. const CLzmaEnc *p = (CLzmaEnc *)pp;
  1863. return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj);
  1864. }
  1865. const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp)
  1866. {
  1867. const CLzmaEnc *p = (CLzmaEnc *)pp;
  1868. return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset;
  1869. }
  1870. SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, Bool reInit,
  1871. Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize)
  1872. {
  1873. CLzmaEnc *p = (CLzmaEnc *)pp;
  1874. UInt64 nowPos64;
  1875. SRes res;
  1876. CSeqOutStreamBuf outStream;
  1877. outStream.funcTable.Write = MyWrite;
  1878. outStream.data = dest;
  1879. outStream.rem = *destLen;
  1880. outStream.overflow = False;
  1881. p->writeEndMark = False;
  1882. p->finished = False;
  1883. p->result = SZ_OK;
  1884. if (reInit)
  1885. LzmaEnc_Init(p);
  1886. LzmaEnc_InitPrices(p);
  1887. nowPos64 = p->nowPos64;
  1888. RangeEnc_Init(&p->rc);
  1889. p->rc.outStream = &outStream.funcTable;
  1890. res = LzmaEnc_CodeOneBlock(p, True, desiredPackSize, *unpackSize);
  1891. *unpackSize = (UInt32)(p->nowPos64 - nowPos64);
  1892. *destLen -= outStream.rem;
  1893. if (outStream.overflow)
  1894. return SZ_ERROR_OUTPUT_EOF;
  1895. return res;
  1896. }
  1897. static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress)
  1898. {
  1899. SRes res = SZ_OK;
  1900. #ifndef _7ZIP_ST
  1901. Byte allocaDummy[0x300];
  1902. allocaDummy[0] = 0;
  1903. allocaDummy[1] = allocaDummy[0];
  1904. #endif
  1905. for (;;)
  1906. {
  1907. res = LzmaEnc_CodeOneBlock(p, False, 0, 0);
  1908. if (res != SZ_OK || p->finished != 0)
  1909. break;
  1910. if (progress != 0)
  1911. {
  1912. res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc));
  1913. if (res != SZ_OK)
  1914. {
  1915. res = SZ_ERROR_PROGRESS;
  1916. break;
  1917. }
  1918. }
  1919. }
  1920. LzmaEnc_Finish(p);
  1921. return res;
  1922. }
  1923. SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress,
  1924. ISzAlloc *alloc, ISzAlloc *allocBig)
  1925. {
  1926. RINOK(LzmaEnc_Prepare(pp, outStream, inStream, alloc, allocBig));
  1927. return LzmaEnc_Encode2((CLzmaEnc *)pp, progress);
  1928. }
  1929. SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size)
  1930. {
  1931. CLzmaEnc *p = (CLzmaEnc *)pp;
  1932. int i;
  1933. UInt32 dictSize = p->dictSize;
  1934. if (*size < LZMA_PROPS_SIZE)
  1935. return SZ_ERROR_PARAM;
  1936. *size = LZMA_PROPS_SIZE;
  1937. props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc);
  1938. for (i = 11; i <= 30; i++)
  1939. {
  1940. if (dictSize <= ((UInt32)2 << i))
  1941. {
  1942. dictSize = (2 << i);
  1943. break;
  1944. }
  1945. if (dictSize <= ((UInt32)3 << i))
  1946. {
  1947. dictSize = (3 << i);
  1948. break;
  1949. }
  1950. }
  1951. for (i = 0; i < 4; i++)
  1952. props[1 + i] = (Byte)(dictSize >> (8 * i));
  1953. return SZ_OK;
  1954. }
  1955. SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
  1956. int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
  1957. {
  1958. SRes res;
  1959. CLzmaEnc *p = (CLzmaEnc *)pp;
  1960. CSeqOutStreamBuf outStream;
  1961. LzmaEnc_SetInputBuf(p, src, srcLen);
  1962. outStream.funcTable.Write = MyWrite;
  1963. outStream.data = dest;
  1964. outStream.rem = *destLen;
  1965. outStream.overflow = False;
  1966. p->writeEndMark = writeEndMark;
  1967. p->rc.outStream = &outStream.funcTable;
  1968. res = LzmaEnc_MemPrepare(pp, src, srcLen, 0, alloc, allocBig);
  1969. if (res == SZ_OK)
  1970. res = LzmaEnc_Encode2(p, progress);
  1971. *destLen -= outStream.rem;
  1972. if (outStream.overflow)
  1973. return SZ_ERROR_OUTPUT_EOF;
  1974. return res;
  1975. }
  1976. SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen,
  1977. const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark,
  1978. ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig)
  1979. {
  1980. CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc);
  1981. SRes res;
  1982. if (p == 0)
  1983. return SZ_ERROR_MEM;
  1984. res = LzmaEnc_SetProps(p, props);
  1985. if (res == SZ_OK)
  1986. {
  1987. res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize);
  1988. if (res == SZ_OK)
  1989. res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen,
  1990. writeEndMark, progress, alloc, allocBig);
  1991. }
  1992. LzmaEnc_Destroy(p, alloc, allocBig);
  1993. return res;
  1994. }