annotate src/win32/7zip/7z/C/LzmaEnc.c @ 2:3549bbe597ed

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