goat3d

annotate libs/openctm/liblzma/LzmaEnc.c @ 51:fa5c52ea9d59

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