OLD | NEW |
1 /* LzmaEnc.c -- LZMA Encoder | 1 /* LzmaEnc.c -- LZMA Encoder |
2 2010-04-16 : Igor Pavlov : Public domain */ | 2 2015-11-08 : Igor Pavlov : Public domain */ |
| 3 |
| 4 #include "Precomp.h" |
3 | 5 |
4 #include <string.h> | 6 #include <string.h> |
5 | 7 |
6 /* #define SHOW_STAT */ | 8 /* #define SHOW_STAT */ |
7 /* #define SHOW_STAT2 */ | 9 /* #define SHOW_STAT2 */ |
8 | 10 |
9 #if defined(SHOW_STAT) || defined(SHOW_STAT2) | 11 #if defined(SHOW_STAT) || defined(SHOW_STAT2) |
10 #include <stdio.h> | 12 #include <stdio.h> |
11 #endif | 13 #endif |
12 | 14 |
13 #include "LzmaEnc.h" | 15 #include "LzmaEnc.h" |
14 | 16 |
15 #include "LzFind.h" | 17 #include "LzFind.h" |
16 #ifndef _7ZIP_ST | 18 #ifndef _7ZIP_ST |
17 #include "LzFindMt.h" | 19 #include "LzFindMt.h" |
18 #endif | 20 #endif |
19 | 21 |
20 #ifdef SHOW_STAT | 22 #ifdef SHOW_STAT |
21 static int ttt = 0; | 23 static unsigned g_STAT_OFFSET = 0; |
22 #endif | 24 #endif |
23 | 25 |
| 26 #define kMaxHistorySize ((UInt32)3 << 29) |
| 27 /* #define kMaxHistorySize ((UInt32)7 << 29) */ |
| 28 |
24 #define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1) | 29 #define kBlockSizeMax ((1 << LZMA_NUM_BLOCK_SIZE_BITS) - 1) |
25 | 30 |
26 #define kBlockSize (9 << 10) | 31 #define kBlockSize (9 << 10) |
27 #define kUnpackBlockSize (1 << 18) | 32 #define kUnpackBlockSize (1 << 18) |
28 #define kMatchArraySize (1 << 21) | 33 #define kMatchArraySize (1 << 21) |
29 #define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX) | 34 #define kMatchRecordMaxSize ((LZMA_MATCH_LEN_MAX * 2 + 3) * LZMA_MATCH_LEN_MAX) |
30 | 35 |
31 #define kNumMaxDirectBits (31) | 36 #define kNumMaxDirectBits (31) |
32 | 37 |
33 #define kNumTopBits 24 | 38 #define kNumTopBits 24 |
34 #define kTopValue ((UInt32)1 << kNumTopBits) | 39 #define kTopValue ((UInt32)1 << kNumTopBits) |
35 | 40 |
36 #define kNumBitModelTotalBits 11 | 41 #define kNumBitModelTotalBits 11 |
37 #define kBitModelTotal (1 << kNumBitModelTotalBits) | 42 #define kBitModelTotal (1 << kNumBitModelTotalBits) |
38 #define kNumMoveBits 5 | 43 #define kNumMoveBits 5 |
39 #define kProbInitValue (kBitModelTotal >> 1) | 44 #define kProbInitValue (kBitModelTotal >> 1) |
40 | 45 |
41 #define kNumMoveReducingBits 4 | 46 #define kNumMoveReducingBits 4 |
42 #define kNumBitPriceShiftBits 4 | 47 #define kNumBitPriceShiftBits 4 |
43 #define kBitPrice (1 << kNumBitPriceShiftBits) | 48 #define kBitPrice (1 << kNumBitPriceShiftBits) |
44 | 49 |
45 void LzmaEncProps_Init(CLzmaEncProps *p) | 50 void LzmaEncProps_Init(CLzmaEncProps *p) |
46 { | 51 { |
47 p->level = 5; | 52 p->level = 5; |
48 p->dictSize = p->mc = 0; | 53 p->dictSize = p->mc = 0; |
| 54 p->reduceSize = (UInt64)(Int64)-1; |
49 p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->num
Threads = -1; | 55 p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->num
Threads = -1; |
50 p->writeEndMark = 0; | 56 p->writeEndMark = 0; |
51 } | 57 } |
52 | 58 |
53 void LzmaEncProps_Normalize(CLzmaEncProps *p) | 59 void LzmaEncProps_Normalize(CLzmaEncProps *p) |
54 { | 60 { |
55 int level = p->level; | 61 int level = p->level; |
56 if (level < 0) level = 5; | 62 if (level < 0) level = 5; |
57 p->level = level; | 63 p->level = level; |
| 64 |
58 if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (l
evel == 6 ? (1 << 25) : (1 << 26))); | 65 if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (l
evel == 6 ? (1 << 25) : (1 << 26))); |
| 66 if (p->dictSize > p->reduceSize) |
| 67 { |
| 68 unsigned i; |
| 69 for (i = 11; i <= 30; i++) |
| 70 { |
| 71 if ((UInt32)p->reduceSize <= ((UInt32)2 << i)) { p->dictSize = ((UInt32)2
<< i); break; } |
| 72 if ((UInt32)p->reduceSize <= ((UInt32)3 << i)) { p->dictSize = ((UInt32)3
<< i); break; } |
| 73 } |
| 74 } |
| 75 |
59 if (p->lc < 0) p->lc = 3; | 76 if (p->lc < 0) p->lc = 3; |
60 if (p->lp < 0) p->lp = 0; | 77 if (p->lp < 0) p->lp = 0; |
61 if (p->pb < 0) p->pb = 2; | 78 if (p->pb < 0) p->pb = 2; |
| 79 |
62 if (p->algo < 0) p->algo = (level < 5 ? 0 : 1); | 80 if (p->algo < 0) p->algo = (level < 5 ? 0 : 1); |
63 if (p->fb < 0) p->fb = (level < 7 ? 32 : 64); | 81 if (p->fb < 0) p->fb = (level < 7 ? 32 : 64); |
64 if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1); | 82 if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1); |
65 if (p->numHashBytes < 0) p->numHashBytes = 4; | 83 if (p->numHashBytes < 0) p->numHashBytes = 4; |
66 if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1); | 84 if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1); |
| 85 |
67 if (p->numThreads < 0) | 86 if (p->numThreads < 0) |
68 p->numThreads = | 87 p->numThreads = |
69 #ifndef _7ZIP_ST | 88 #ifndef _7ZIP_ST |
70 ((p->btMode && p->algo) ? 2 : 1); | 89 ((p->btMode && p->algo) ? 2 : 1); |
71 #else | 90 #else |
72 1; | 91 1; |
73 #endif | 92 #endif |
74 } | 93 } |
75 | 94 |
76 UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2) | 95 UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2) |
77 { | 96 { |
78 CLzmaEncProps props = *props2; | 97 CLzmaEncProps props = *props2; |
79 LzmaEncProps_Normalize(&props); | 98 LzmaEncProps_Normalize(&props); |
80 return props.dictSize; | 99 return props.dictSize; |
81 } | 100 } |
82 | 101 |
| 102 #if (_MSC_VER >= 1400) |
| 103 /* BSR code is fast for some new CPUs */ |
83 /* #define LZMA_LOG_BSR */ | 104 /* #define LZMA_LOG_BSR */ |
84 /* Define it for Intel's CPU */ | 105 #endif |
85 | |
86 | 106 |
87 #ifdef LZMA_LOG_BSR | 107 #ifdef LZMA_LOG_BSR |
88 | 108 |
89 #define kDicLogSizeMaxCompress 30 | 109 #define kDicLogSizeMaxCompress 32 |
90 | 110 |
91 #define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res =
(i + i) + ((pos >> (i - 1)) & 1); } | 111 #define BSR2_RET(pos, res) { unsigned long i; _BitScanReverse(&i, (pos)); res =
(i + i) + ((pos >> (i - 1)) & 1); } |
92 | 112 |
93 UInt32 GetPosSlot1(UInt32 pos) | 113 static UInt32 GetPosSlot1(UInt32 pos) |
94 { | 114 { |
95 UInt32 res; | 115 UInt32 res; |
96 BSR2_RET(pos, res); | 116 BSR2_RET(pos, res); |
97 return res; | 117 return res; |
98 } | 118 } |
99 #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); } | 119 #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); } |
100 #define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res);
} | 120 #define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res);
} |
101 | 121 |
102 #else | 122 #else |
103 | 123 |
104 #define kNumLogBits (9 + (int)sizeof(size_t) / 2) | 124 #define kNumLogBits (9 + sizeof(size_t) / 2) |
| 125 /* #define kNumLogBits (11 + sizeof(size_t) / 8 * 3) */ |
| 126 |
105 #define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7) | 127 #define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7) |
106 | 128 |
107 void LzmaEnc_FastPosInit(Byte *g_FastPos) | 129 static void LzmaEnc_FastPosInit(Byte *g_FastPos) |
108 { | 130 { |
109 int c = 2, slotFast; | 131 unsigned slot; |
110 g_FastPos[0] = 0; | 132 g_FastPos[0] = 0; |
111 g_FastPos[1] = 1; | 133 g_FastPos[1] = 1; |
| 134 g_FastPos += 2; |
112 | 135 |
113 for (slotFast = 2; slotFast < kNumLogBits * 2; slotFast++) | 136 for (slot = 2; slot < kNumLogBits * 2; slot++) |
114 { | 137 { |
115 UInt32 k = (1 << ((slotFast >> 1) - 1)); | 138 size_t k = ((size_t)1 << ((slot >> 1) - 1)); |
116 UInt32 j; | 139 size_t j; |
117 for (j = 0; j < k; j++, c++) | 140 for (j = 0; j < k; j++) |
118 g_FastPos[c] = (Byte)slotFast; | 141 g_FastPos[j] = (Byte)slot; |
| 142 g_FastPos += k; |
119 } | 143 } |
120 } | 144 } |
121 | 145 |
| 146 /* we can use ((limit - pos) >> 31) only if (pos < ((UInt32)1 << 31)) */ |
| 147 /* |
122 #define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \ | 148 #define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \ |
123 (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \ | 149 (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \ |
124 res = p->g_FastPos[pos >> i] + (i * 2); } | 150 res = p->g_FastPos[pos >> i] + (i * 2); } |
| 151 */ |
| 152 |
| 153 /* |
| 154 #define BSR2_RET(pos, res) { UInt32 i = 6 + ((kNumLogBits - 1) & \ |
| 155 (0 - (((((UInt32)1 << (kNumLogBits)) - 1) - (pos >> 6)) >> 31))); \ |
| 156 res = p->g_FastPos[pos >> i] + (i * 2); } |
| 157 */ |
| 158 |
| 159 #define BSR2_RET(pos, res) { UInt32 i = (pos < (1 << (kNumLogBits + 6))) ? 6 : 6
+ kNumLogBits - 1; \ |
| 160 res = p->g_FastPos[pos >> i] + (i * 2); } |
| 161 |
125 /* | 162 /* |
126 #define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \ | 163 #define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \ |
127 p->g_FastPos[pos >> 6] + 12 : \ | 164 p->g_FastPos[pos >> 6] + 12 : \ |
128 p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; } | 165 p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; } |
129 */ | 166 */ |
130 | 167 |
131 #define GetPosSlot1(pos) p->g_FastPos[pos] | 168 #define GetPosSlot1(pos) p->g_FastPos[pos] |
132 #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); } | 169 #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); } |
133 #define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[p
os]; else BSR2_RET(pos, res); } | 170 #define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[p
os]; else BSR2_RET(pos, res); } |
134 | 171 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 #define kLenNumHighBits 8 | 231 #define kLenNumHighBits 8 |
195 #define kLenNumHighSymbols (1 << kLenNumHighBits) | 232 #define kLenNumHighSymbols (1 << kLenNumHighBits) |
196 | 233 |
197 #define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHigh
Symbols) | 234 #define kLenNumSymbolsTotal (kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHigh
Symbols) |
198 | 235 |
199 #define LZMA_MATCH_LEN_MIN 2 | 236 #define LZMA_MATCH_LEN_MIN 2 |
200 #define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1) | 237 #define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1) |
201 | 238 |
202 #define kNumStates 12 | 239 #define kNumStates 12 |
203 | 240 |
| 241 |
204 typedef struct | 242 typedef struct |
205 { | 243 { |
206 CLzmaProb choice; | 244 CLzmaProb choice; |
207 CLzmaProb choice2; | 245 CLzmaProb choice2; |
208 CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits]; | 246 CLzmaProb low[LZMA_NUM_PB_STATES_MAX << kLenNumLowBits]; |
209 CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits]; | 247 CLzmaProb mid[LZMA_NUM_PB_STATES_MAX << kLenNumMidBits]; |
210 CLzmaProb high[kLenNumHighSymbols]; | 248 CLzmaProb high[kLenNumHighSymbols]; |
211 } CLenEnc; | 249 } CLenEnc; |
212 | 250 |
| 251 |
213 typedef struct | 252 typedef struct |
214 { | 253 { |
215 CLenEnc p; | 254 CLenEnc p; |
| 255 UInt32 tableSize; |
216 UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal]; | 256 UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal]; |
217 UInt32 tableSize; | |
218 UInt32 counters[LZMA_NUM_PB_STATES_MAX]; | 257 UInt32 counters[LZMA_NUM_PB_STATES_MAX]; |
219 } CLenPriceEnc; | 258 } CLenPriceEnc; |
220 | 259 |
| 260 |
221 typedef struct | 261 typedef struct |
222 { | 262 { |
223 UInt32 range; | 263 UInt32 range; |
224 Byte cache; | 264 Byte cache; |
225 UInt64 low; | 265 UInt64 low; |
226 UInt64 cacheSize; | 266 UInt64 cacheSize; |
227 Byte *buf; | 267 Byte *buf; |
228 Byte *bufLim; | 268 Byte *bufLim; |
229 Byte *bufBase; | 269 Byte *bufBase; |
230 ISeqOutStream *outStream; | 270 ISeqOutStream *outStream; |
231 UInt64 processed; | 271 UInt64 processed; |
232 SRes res; | 272 SRes res; |
233 } CRangeEnc; | 273 } CRangeEnc; |
234 | 274 |
| 275 |
235 typedef struct | 276 typedef struct |
236 { | 277 { |
237 CLzmaProb *litProbs; | 278 CLzmaProb *litProbs; |
238 | 279 |
| 280 UInt32 state; |
| 281 UInt32 reps[LZMA_NUM_REPS]; |
| 282 |
239 CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX]; | 283 CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX]; |
240 CLzmaProb isRep[kNumStates]; | 284 CLzmaProb isRep[kNumStates]; |
241 CLzmaProb isRepG0[kNumStates]; | 285 CLzmaProb isRepG0[kNumStates]; |
242 CLzmaProb isRepG1[kNumStates]; | 286 CLzmaProb isRepG1[kNumStates]; |
243 CLzmaProb isRepG2[kNumStates]; | 287 CLzmaProb isRepG2[kNumStates]; |
244 CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX]; | 288 CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX]; |
245 | 289 |
246 CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits]; | 290 CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits]; |
247 CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex]; | 291 CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex]; |
248 CLzmaProb posAlignEncoder[1 << kNumAlignBits]; | 292 CLzmaProb posAlignEncoder[1 << kNumAlignBits]; |
249 | 293 |
250 CLenPriceEnc lenEnc; | 294 CLenPriceEnc lenEnc; |
251 CLenPriceEnc repLenEnc; | 295 CLenPriceEnc repLenEnc; |
| 296 } CSaveState; |
252 | 297 |
253 UInt32 reps[LZMA_NUM_REPS]; | |
254 UInt32 state; | |
255 } CSaveState; | |
256 | 298 |
257 typedef struct | 299 typedef struct |
258 { | 300 { |
| 301 void *matchFinderObj; |
259 IMatchFinder matchFinder; | 302 IMatchFinder matchFinder; |
260 void *matchFinderObj; | 303 |
| 304 UInt32 optimumEndIndex; |
| 305 UInt32 optimumCurrentIndex; |
| 306 |
| 307 UInt32 longestMatchLength; |
| 308 UInt32 numPairs; |
| 309 UInt32 numAvail; |
| 310 |
| 311 UInt32 numFastBytes; |
| 312 UInt32 additionalOffset; |
| 313 UInt32 reps[LZMA_NUM_REPS]; |
| 314 UInt32 state; |
| 315 |
| 316 unsigned lc, lp, pb; |
| 317 unsigned lpMask, pbMask; |
| 318 unsigned lclp; |
| 319 |
| 320 CLzmaProb *litProbs; |
| 321 |
| 322 Bool fastMode; |
| 323 Bool writeEndMark; |
| 324 Bool finished; |
| 325 Bool multiThread; |
| 326 Bool needInit; |
| 327 |
| 328 UInt64 nowPos64; |
| 329 |
| 330 UInt32 matchPriceCount; |
| 331 UInt32 alignPriceCount; |
| 332 |
| 333 UInt32 distTableSize; |
| 334 |
| 335 UInt32 dictSize; |
| 336 SRes result; |
| 337 |
| 338 CRangeEnc rc; |
261 | 339 |
262 #ifndef _7ZIP_ST | 340 #ifndef _7ZIP_ST |
263 Bool mtMode; | 341 Bool mtMode; |
264 CMatchFinderMt matchFinderMt; | 342 CMatchFinderMt matchFinderMt; |
265 #endif | 343 #endif |
266 | 344 |
267 CMatchFinder matchFinderBase; | 345 CMatchFinder matchFinderBase; |
268 | 346 |
269 #ifndef _7ZIP_ST | 347 #ifndef _7ZIP_ST |
270 Byte pad[128]; | 348 Byte pad[128]; |
271 #endif | 349 #endif |
272 | 350 |
273 UInt32 optimumEndIndex; | |
274 UInt32 optimumCurrentIndex; | |
275 | |
276 UInt32 longestMatchLength; | |
277 UInt32 numPairs; | |
278 UInt32 numAvail; | |
279 COptimal opt[kNumOpts]; | 351 COptimal opt[kNumOpts]; |
280 | 352 |
281 #ifndef LZMA_LOG_BSR | 353 #ifndef LZMA_LOG_BSR |
282 Byte g_FastPos[1 << kNumLogBits]; | 354 Byte g_FastPos[1 << kNumLogBits]; |
283 #endif | 355 #endif |
284 | 356 |
285 UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits]; | 357 UInt32 ProbPrices[kBitModelTotal >> kNumMoveReducingBits]; |
286 UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1]; | 358 UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1]; |
287 UInt32 numFastBytes; | |
288 UInt32 additionalOffset; | |
289 UInt32 reps[LZMA_NUM_REPS]; | |
290 UInt32 state; | |
291 | 359 |
292 UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax]; | 360 UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax]; |
293 UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances]; | 361 UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances]; |
294 UInt32 alignPrices[kAlignTableSize]; | 362 UInt32 alignPrices[kAlignTableSize]; |
295 UInt32 alignPriceCount; | |
296 | |
297 UInt32 distTableSize; | |
298 | |
299 unsigned lc, lp, pb; | |
300 unsigned lpMask, pbMask; | |
301 | |
302 CLzmaProb *litProbs; | |
303 | 363 |
304 CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX]; | 364 CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX]; |
305 CLzmaProb isRep[kNumStates]; | 365 CLzmaProb isRep[kNumStates]; |
306 CLzmaProb isRepG0[kNumStates]; | 366 CLzmaProb isRepG0[kNumStates]; |
307 CLzmaProb isRepG1[kNumStates]; | 367 CLzmaProb isRepG1[kNumStates]; |
308 CLzmaProb isRepG2[kNumStates]; | 368 CLzmaProb isRepG2[kNumStates]; |
309 CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX]; | 369 CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX]; |
310 | 370 |
311 CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits]; | 371 CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits]; |
312 CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex]; | 372 CLzmaProb posEncoders[kNumFullDistances - kEndPosModelIndex]; |
313 CLzmaProb posAlignEncoder[1 << kNumAlignBits]; | 373 CLzmaProb posAlignEncoder[1 << kNumAlignBits]; |
314 | 374 |
315 CLenPriceEnc lenEnc; | 375 CLenPriceEnc lenEnc; |
316 CLenPriceEnc repLenEnc; | 376 CLenPriceEnc repLenEnc; |
317 | 377 |
318 unsigned lclp; | 378 CSaveState saveState; |
319 | 379 |
320 Bool fastMode; | 380 #ifndef _7ZIP_ST |
321 | 381 Byte pad2[128]; |
322 CRangeEnc rc; | 382 #endif |
| 383 } CLzmaEnc; |
323 | 384 |
324 Bool writeEndMark; | |
325 UInt64 nowPos64; | |
326 UInt32 matchPriceCount; | |
327 Bool finished; | |
328 Bool multiThread; | |
329 | |
330 SRes result; | |
331 UInt32 dictSize; | |
332 UInt32 matchFinderCycles; | |
333 | |
334 int needInit; | |
335 | |
336 CSaveState saveState; | |
337 } CLzmaEnc; | |
338 | 385 |
339 void LzmaEnc_SaveState(CLzmaEncHandle pp) | 386 void LzmaEnc_SaveState(CLzmaEncHandle pp) |
340 { | 387 { |
341 CLzmaEnc *p = (CLzmaEnc *)pp; | 388 CLzmaEnc *p = (CLzmaEnc *)pp; |
342 CSaveState *dest = &p->saveState; | 389 CSaveState *dest = &p->saveState; |
343 int i; | 390 int i; |
344 dest->lenEnc = p->lenEnc; | 391 dest->lenEnc = p->lenEnc; |
345 dest->repLenEnc = p->repLenEnc; | 392 dest->repLenEnc = p->repLenEnc; |
346 dest->state = p->state; | 393 dest->state = p->state; |
347 | 394 |
348 for (i = 0; i < kNumStates; i++) | 395 for (i = 0; i < kNumStates; i++) |
349 { | 396 { |
350 memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i])); | 397 memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i])); |
351 memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i])); | 398 memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i])); |
352 } | 399 } |
353 for (i = 0; i < kNumLenToPosStates; i++) | 400 for (i = 0; i < kNumLenToPosStates; i++) |
354 memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncod
er[i])); | 401 memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncod
er[i])); |
355 memcpy(dest->isRep, p->isRep, sizeof(p->isRep)); | 402 memcpy(dest->isRep, p->isRep, sizeof(p->isRep)); |
356 memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0)); | 403 memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0)); |
357 memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1)); | 404 memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1)); |
358 memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2)); | 405 memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2)); |
359 memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders)); | 406 memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders)); |
360 memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder)); | 407 memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder)); |
361 memcpy(dest->reps, p->reps, sizeof(p->reps)); | 408 memcpy(dest->reps, p->reps, sizeof(p->reps)); |
362 memcpy(dest->litProbs, p->litProbs, (0x300 << p->lclp) * sizeof(CLzmaProb)); | 409 memcpy(dest->litProbs, p->litProbs, ((UInt32)0x300 << p->lclp) * sizeof(CLzmaP
rob)); |
363 } | 410 } |
364 | 411 |
365 void LzmaEnc_RestoreState(CLzmaEncHandle pp) | 412 void LzmaEnc_RestoreState(CLzmaEncHandle pp) |
366 { | 413 { |
367 CLzmaEnc *dest = (CLzmaEnc *)pp; | 414 CLzmaEnc *dest = (CLzmaEnc *)pp; |
368 const CSaveState *p = &dest->saveState; | 415 const CSaveState *p = &dest->saveState; |
369 int i; | 416 int i; |
370 dest->lenEnc = p->lenEnc; | 417 dest->lenEnc = p->lenEnc; |
371 dest->repLenEnc = p->repLenEnc; | 418 dest->repLenEnc = p->repLenEnc; |
372 dest->state = p->state; | 419 dest->state = p->state; |
373 | 420 |
374 for (i = 0; i < kNumStates; i++) | 421 for (i = 0; i < kNumStates; i++) |
375 { | 422 { |
376 memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i])); | 423 memcpy(dest->isMatch[i], p->isMatch[i], sizeof(p->isMatch[i])); |
377 memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i])); | 424 memcpy(dest->isRep0Long[i], p->isRep0Long[i], sizeof(p->isRep0Long[i])); |
378 } | 425 } |
379 for (i = 0; i < kNumLenToPosStates; i++) | 426 for (i = 0; i < kNumLenToPosStates; i++) |
380 memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncod
er[i])); | 427 memcpy(dest->posSlotEncoder[i], p->posSlotEncoder[i], sizeof(p->posSlotEncod
er[i])); |
381 memcpy(dest->isRep, p->isRep, sizeof(p->isRep)); | 428 memcpy(dest->isRep, p->isRep, sizeof(p->isRep)); |
382 memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0)); | 429 memcpy(dest->isRepG0, p->isRepG0, sizeof(p->isRepG0)); |
383 memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1)); | 430 memcpy(dest->isRepG1, p->isRepG1, sizeof(p->isRepG1)); |
384 memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2)); | 431 memcpy(dest->isRepG2, p->isRepG2, sizeof(p->isRepG2)); |
385 memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders)); | 432 memcpy(dest->posEncoders, p->posEncoders, sizeof(p->posEncoders)); |
386 memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder)); | 433 memcpy(dest->posAlignEncoder, p->posAlignEncoder, sizeof(p->posAlignEncoder)); |
387 memcpy(dest->reps, p->reps, sizeof(p->reps)); | 434 memcpy(dest->reps, p->reps, sizeof(p->reps)); |
388 memcpy(dest->litProbs, p->litProbs, (0x300 << dest->lclp) * sizeof(CLzmaProb))
; | 435 memcpy(dest->litProbs, p->litProbs, ((UInt32)0x300 << dest->lclp) * sizeof(CLz
maProb)); |
389 } | 436 } |
390 | 437 |
391 SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2) | 438 SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2) |
392 { | 439 { |
393 CLzmaEnc *p = (CLzmaEnc *)pp; | 440 CLzmaEnc *p = (CLzmaEnc *)pp; |
394 CLzmaEncProps props = *props2; | 441 CLzmaEncProps props = *props2; |
395 LzmaEncProps_Normalize(&props); | 442 LzmaEncProps_Normalize(&props); |
396 | 443 |
397 if (props.lc > LZMA_LC_MAX || props.lp > LZMA_LP_MAX || props.pb > LZMA_PB_MAX
|| | 444 if (props.lc > LZMA_LC_MAX |
398 props.dictSize > ((UInt32)1 << kDicLogSizeMaxCompress) || props.dictSize >
((UInt32)1 << 30)) | 445 || props.lp > LZMA_LP_MAX |
| 446 || props.pb > LZMA_PB_MAX |
| 447 || props.dictSize > ((UInt64)1 << kDicLogSizeMaxCompress) |
| 448 || props.dictSize > kMaxHistorySize) |
399 return SZ_ERROR_PARAM; | 449 return SZ_ERROR_PARAM; |
| 450 |
400 p->dictSize = props.dictSize; | 451 p->dictSize = props.dictSize; |
401 p->matchFinderCycles = props.mc; | |
402 { | 452 { |
403 unsigned fb = props.fb; | 453 unsigned fb = props.fb; |
404 if (fb < 5) | 454 if (fb < 5) |
405 fb = 5; | 455 fb = 5; |
406 if (fb > LZMA_MATCH_LEN_MAX) | 456 if (fb > LZMA_MATCH_LEN_MAX) |
407 fb = LZMA_MATCH_LEN_MAX; | 457 fb = LZMA_MATCH_LEN_MAX; |
408 p->numFastBytes = fb; | 458 p->numFastBytes = fb; |
409 } | 459 } |
410 p->lc = props.lc; | 460 p->lc = props.lc; |
411 p->lp = props.lp; | 461 p->lp = props.lp; |
412 p->pb = props.pb; | 462 p->pb = props.pb; |
413 p->fastMode = (props.algo == 0); | 463 p->fastMode = (props.algo == 0); |
414 p->matchFinderBase.btMode = props.btMode; | 464 p->matchFinderBase.btMode = (Byte)(props.btMode ? 1 : 0); |
415 { | 465 { |
416 UInt32 numHashBytes = 4; | 466 UInt32 numHashBytes = 4; |
417 if (props.btMode) | 467 if (props.btMode) |
418 { | 468 { |
419 if (props.numHashBytes < 2) | 469 if (props.numHashBytes < 2) |
420 numHashBytes = 2; | 470 numHashBytes = 2; |
421 else if (props.numHashBytes < 4) | 471 else if (props.numHashBytes < 4) |
422 numHashBytes = props.numHashBytes; | 472 numHashBytes = props.numHashBytes; |
423 } | 473 } |
424 p->matchFinderBase.numHashBytes = numHashBytes; | 474 p->matchFinderBase.numHashBytes = numHashBytes; |
(...skipping 23 matching lines...) Expand all Loading... |
448 static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11,
11, 11, 11}; | 498 static const int kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11,
11, 11, 11}; |
449 | 499 |
450 #define IsCharState(s) ((s) < 7) | 500 #define IsCharState(s) ((s) < 7) |
451 | 501 |
452 #define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kN
umLenToPosStates - 1) | 502 #define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kN
umLenToPosStates - 1) |
453 | 503 |
454 #define kInfinityPrice (1 << 30) | 504 #define kInfinityPrice (1 << 30) |
455 | 505 |
456 static void RangeEnc_Construct(CRangeEnc *p) | 506 static void RangeEnc_Construct(CRangeEnc *p) |
457 { | 507 { |
458 p->outStream = 0; | 508 p->outStream = NULL; |
459 p->bufBase = 0; | 509 p->bufBase = NULL; |
460 } | 510 } |
461 | 511 |
462 #define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (
p)->cacheSize) | 512 #define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (
p)->cacheSize) |
463 | 513 |
464 #define RC_BUF_SIZE (1 << 16) | 514 #define RC_BUF_SIZE (1 << 16) |
465 static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc) | 515 static int RangeEnc_Alloc(CRangeEnc *p, ISzAlloc *alloc) |
466 { | 516 { |
467 if (p->bufBase == 0) | 517 if (!p->bufBase) |
468 { | 518 { |
469 p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE); | 519 p->bufBase = (Byte *)alloc->Alloc(alloc, RC_BUF_SIZE); |
470 if (p->bufBase == 0) | 520 if (!p->bufBase) |
471 return 0; | 521 return 0; |
472 p->bufLim = p->bufBase + RC_BUF_SIZE; | 522 p->bufLim = p->bufBase + RC_BUF_SIZE; |
473 } | 523 } |
474 return 1; | 524 return 1; |
475 } | 525 } |
476 | 526 |
477 static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc) | 527 static void RangeEnc_Free(CRangeEnc *p, ISzAlloc *alloc) |
478 { | 528 { |
479 alloc->Free(alloc, p->bufBase); | 529 alloc->Free(alloc, p->bufBase); |
480 p->bufBase = 0; | 530 p->bufBase = 0; |
(...skipping 20 matching lines...) Expand all Loading... |
501 return; | 551 return; |
502 num = p->buf - p->bufBase; | 552 num = p->buf - p->bufBase; |
503 if (num != p->outStream->Write(p->outStream, p->bufBase, num)) | 553 if (num != p->outStream->Write(p->outStream, p->bufBase, num)) |
504 p->res = SZ_ERROR_WRITE; | 554 p->res = SZ_ERROR_WRITE; |
505 p->processed += num; | 555 p->processed += num; |
506 p->buf = p->bufBase; | 556 p->buf = p->bufBase; |
507 } | 557 } |
508 | 558 |
509 static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p) | 559 static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p) |
510 { | 560 { |
511 if ((UInt32)p->low < (UInt32)0xFF000000 || (int)(p->low >> 32) != 0) | 561 if ((UInt32)p->low < (UInt32)0xFF000000 || (unsigned)(p->low >> 32) != 0) |
512 { | 562 { |
513 Byte temp = p->cache; | 563 Byte temp = p->cache; |
514 do | 564 do |
515 { | 565 { |
516 Byte *buf = p->buf; | 566 Byte *buf = p->buf; |
517 *buf++ = (Byte)(temp + (Byte)(p->low >> 32)); | 567 *buf++ = (Byte)(temp + (Byte)(p->low >> 32)); |
518 p->buf = buf; | 568 p->buf = buf; |
519 if (buf == p->bufLim) | 569 if (buf == p->bufLim) |
520 RangeEnc_FlushStream(p); | 570 RangeEnc_FlushStream(p); |
521 temp = 0xFF; | 571 temp = 0xFF; |
522 } | 572 } |
523 while (--p->cacheSize != 0); | 573 while (--p->cacheSize != 0); |
524 p->cache = (Byte)((UInt32)p->low >> 24); | 574 p->cache = (Byte)((UInt32)p->low >> 24); |
525 } | 575 } |
526 p->cacheSize++; | 576 p->cacheSize++; |
527 p->low = (UInt32)p->low << 8; | 577 p->low = (UInt32)p->low << 8; |
528 } | 578 } |
529 | 579 |
530 static void RangeEnc_FlushData(CRangeEnc *p) | 580 static void RangeEnc_FlushData(CRangeEnc *p) |
531 { | 581 { |
532 int i; | 582 int i; |
533 for (i = 0; i < 5; i++) | 583 for (i = 0; i < 5; i++) |
534 RangeEnc_ShiftLow(p); | 584 RangeEnc_ShiftLow(p); |
535 } | 585 } |
536 | 586 |
537 static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, int numBits) | 587 static void RangeEnc_EncodeDirectBits(CRangeEnc *p, UInt32 value, unsigned numBi
ts) |
538 { | 588 { |
539 do | 589 do |
540 { | 590 { |
541 p->range >>= 1; | 591 p->range >>= 1; |
542 p->low += p->range & (0 - ((value >> --numBits) & 1)); | 592 p->low += p->range & (0 - ((value >> --numBits) & 1)); |
543 if (p->range < kTopValue) | 593 if (p->range < kTopValue) |
544 { | 594 { |
545 p->range <<= 8; | 595 p->range <<= 8; |
546 RangeEnc_ShiftLow(p); | 596 RangeEnc_ShiftLow(p); |
547 } | 597 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 do | 640 do |
591 { | 641 { |
592 matchByte <<= 1; | 642 matchByte <<= 1; |
593 RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (
symbol >> 7) & 1); | 643 RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (symbol >> 8)), (
symbol >> 7) & 1); |
594 symbol <<= 1; | 644 symbol <<= 1; |
595 offs &= ~(matchByte ^ symbol); | 645 offs &= ~(matchByte ^ symbol); |
596 } | 646 } |
597 while (symbol < 0x10000); | 647 while (symbol < 0x10000); |
598 } | 648 } |
599 | 649 |
600 void LzmaEnc_InitPriceTables(UInt32 *ProbPrices) | 650 static void LzmaEnc_InitPriceTables(UInt32 *ProbPrices) |
601 { | 651 { |
602 UInt32 i; | 652 UInt32 i; |
603 for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumM
oveReducingBits)) | 653 for (i = (1 << kNumMoveReducingBits) / 2; i < kBitModelTotal; i += (1 << kNumM
oveReducingBits)) |
604 { | 654 { |
605 const int kCyclesBits = kNumBitPriceShiftBits; | 655 const int kCyclesBits = kNumBitPriceShiftBits; |
606 UInt32 w = i; | 656 UInt32 w = i; |
607 UInt32 bitCount = 0; | 657 UInt32 bitCount = 0; |
608 int j; | 658 int j; |
609 for (j = 0; j < kCyclesBits; j++) | 659 for (j = 0; j < kCyclesBits; j++) |
610 { | 660 { |
(...skipping 15 matching lines...) Expand all Loading... |
626 | 676 |
627 #define GET_PRICEa(prob, symbol) \ | 677 #define GET_PRICEa(prob, symbol) \ |
628 ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveR
educingBits]; | 678 ProbPrices[((prob) ^ ((-((int)(symbol))) & (kBitModelTotal - 1))) >> kNumMoveR
educingBits]; |
629 | 679 |
630 #define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits] | 680 #define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits] |
631 #define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumM
oveReducingBits] | 681 #define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumM
oveReducingBits] |
632 | 682 |
633 #define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits] | 683 #define GET_PRICE_0a(prob) ProbPrices[(prob) >> kNumMoveReducingBits] |
634 #define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMov
eReducingBits] | 684 #define GET_PRICE_1a(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMov
eReducingBits] |
635 | 685 |
636 static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, UInt32 *Pro
bPrices) | 686 static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 symbol, const UInt3
2 *ProbPrices) |
637 { | 687 { |
638 UInt32 price = 0; | 688 UInt32 price = 0; |
639 symbol |= 0x100; | 689 symbol |= 0x100; |
640 do | 690 do |
641 { | 691 { |
642 price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1); | 692 price += GET_PRICEa(probs[symbol >> 8], (symbol >> 7) & 1); |
643 symbol <<= 1; | 693 symbol <<= 1; |
644 } | 694 } |
645 while (symbol < 0x10000); | 695 while (symbol < 0x10000); |
646 return price; | 696 return price; |
647 } | 697 } |
648 | 698 |
649 static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt
32 matchByte, UInt32 *ProbPrices) | 699 static UInt32 LitEnc_GetPriceMatched(const CLzmaProb *probs, UInt32 symbol, UInt
32 matchByte, const UInt32 *ProbPrices) |
650 { | 700 { |
651 UInt32 price = 0; | 701 UInt32 price = 0; |
652 UInt32 offs = 0x100; | 702 UInt32 offs = 0x100; |
653 symbol |= 0x100; | 703 symbol |= 0x100; |
654 do | 704 do |
655 { | 705 { |
656 matchByte <<= 1; | 706 matchByte <<= 1; |
657 price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbo
l >> 7) & 1); | 707 price += GET_PRICEa(probs[offs + (matchByte & offs) + (symbol >> 8)], (symbo
l >> 7) & 1); |
658 symbol <<= 1; | 708 symbol <<= 1; |
659 offs &= ~(matchByte ^ symbol); | 709 offs &= ~(matchByte ^ symbol); |
(...skipping 23 matching lines...) Expand all Loading... |
683 int i; | 733 int i; |
684 for (i = 0; i < numBitLevels; i++) | 734 for (i = 0; i < numBitLevels; i++) |
685 { | 735 { |
686 UInt32 bit = symbol & 1; | 736 UInt32 bit = symbol & 1; |
687 RangeEnc_EncodeBit(rc, probs + m, bit); | 737 RangeEnc_EncodeBit(rc, probs + m, bit); |
688 m = (m << 1) | bit; | 738 m = (m << 1) | bit; |
689 symbol >>= 1; | 739 symbol >>= 1; |
690 } | 740 } |
691 } | 741 } |
692 | 742 |
693 static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 s
ymbol, UInt32 *ProbPrices) | 743 static UInt32 RcTree_GetPrice(const CLzmaProb *probs, int numBitLevels, UInt32 s
ymbol, const UInt32 *ProbPrices) |
694 { | 744 { |
695 UInt32 price = 0; | 745 UInt32 price = 0; |
696 symbol |= (1 << numBitLevels); | 746 symbol |= (1 << numBitLevels); |
697 while (symbol != 1) | 747 while (symbol != 1) |
698 { | 748 { |
699 price += GET_PRICEa(probs[symbol >> 1], symbol & 1); | 749 price += GET_PRICEa(probs[symbol >> 1], symbol & 1); |
700 symbol >>= 1; | 750 symbol >>= 1; |
701 } | 751 } |
702 return price; | 752 return price; |
703 } | 753 } |
704 | 754 |
705 static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, U
Int32 symbol, UInt32 *ProbPrices) | 755 static UInt32 RcTree_ReverseGetPrice(const CLzmaProb *probs, int numBitLevels, U
Int32 symbol, const UInt32 *ProbPrices) |
706 { | 756 { |
707 UInt32 price = 0; | 757 UInt32 price = 0; |
708 UInt32 m = 1; | 758 UInt32 m = 1; |
709 int i; | 759 int i; |
710 for (i = numBitLevels; i != 0; i--) | 760 for (i = numBitLevels; i != 0; i--) |
711 { | 761 { |
712 UInt32 bit = symbol & 1; | 762 UInt32 bit = symbol & 1; |
713 symbol >>= 1; | 763 symbol >>= 1; |
714 price += GET_PRICEa(probs[m], bit); | 764 price += GET_PRICEa(probs[m], bit); |
715 m = (m << 1) | bit; | 765 m = (m << 1) | bit; |
(...skipping 30 matching lines...) Expand all Loading... |
746 RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, s
ymbol - kLenNumLowSymbols); | 796 RcTree_Encode(rc, p->mid + (posState << kLenNumMidBits), kLenNumMidBits, s
ymbol - kLenNumLowSymbols); |
747 } | 797 } |
748 else | 798 else |
749 { | 799 { |
750 RangeEnc_EncodeBit(rc, &p->choice2, 1); | 800 RangeEnc_EncodeBit(rc, &p->choice2, 1); |
751 RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - k
LenNumMidSymbols); | 801 RcTree_Encode(rc, p->high, kLenNumHighBits, symbol - kLenNumLowSymbols - k
LenNumMidSymbols); |
752 } | 802 } |
753 } | 803 } |
754 } | 804 } |
755 | 805 |
756 static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UIn
t32 *prices, UInt32 *ProbPrices) | 806 static void LenEnc_SetPrices(CLenEnc *p, UInt32 posState, UInt32 numSymbols, UIn
t32 *prices, const UInt32 *ProbPrices) |
757 { | 807 { |
758 UInt32 a0 = GET_PRICE_0a(p->choice); | 808 UInt32 a0 = GET_PRICE_0a(p->choice); |
759 UInt32 a1 = GET_PRICE_1a(p->choice); | 809 UInt32 a1 = GET_PRICE_1a(p->choice); |
760 UInt32 b0 = a1 + GET_PRICE_0a(p->choice2); | 810 UInt32 b0 = a1 + GET_PRICE_0a(p->choice2); |
761 UInt32 b1 = a1 + GET_PRICE_1a(p->choice2); | 811 UInt32 b1 = a1 + GET_PRICE_1a(p->choice2); |
762 UInt32 i = 0; | 812 UInt32 i = 0; |
763 for (i = 0; i < kLenNumLowSymbols; i++) | 813 for (i = 0; i < kLenNumLowSymbols; i++) |
764 { | 814 { |
765 if (i >= numSymbols) | 815 if (i >= numSymbols) |
766 return; | 816 return; |
767 prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLen
NumLowBits, i, ProbPrices); | 817 prices[i] = a0 + RcTree_GetPrice(p->low + (posState << kLenNumLowBits), kLen
NumLowBits, i, ProbPrices); |
768 } | 818 } |
769 for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++) | 819 for (; i < kLenNumLowSymbols + kLenNumMidSymbols; i++) |
770 { | 820 { |
771 if (i >= numSymbols) | 821 if (i >= numSymbols) |
772 return; | 822 return; |
773 prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLen
NumMidBits, i - kLenNumLowSymbols, ProbPrices); | 823 prices[i] = b0 + RcTree_GetPrice(p->mid + (posState << kLenNumMidBits), kLen
NumMidBits, i - kLenNumLowSymbols, ProbPrices); |
774 } | 824 } |
775 for (; i < numSymbols; i++) | 825 for (; i < numSymbols; i++) |
776 prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSym
bols - kLenNumMidSymbols, ProbPrices); | 826 prices[i] = b1 + RcTree_GetPrice(p->high, kLenNumHighBits, i - kLenNumLowSym
bols - kLenNumMidSymbols, ProbPrices); |
777 } | 827 } |
778 | 828 |
779 static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posStat
e, UInt32 *ProbPrices) | 829 static void MY_FAST_CALL LenPriceEnc_UpdateTable(CLenPriceEnc *p, UInt32 posStat
e, const UInt32 *ProbPrices) |
780 { | 830 { |
781 LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrice
s); | 831 LenEnc_SetPrices(&p->p, posState, p->tableSize, p->prices[posState], ProbPrice
s); |
782 p->counters[posState] = p->tableSize; | 832 p->counters[posState] = p->tableSize; |
783 } | 833 } |
784 | 834 |
785 static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, UInt3
2 *ProbPrices) | 835 static void LenPriceEnc_UpdateTables(CLenPriceEnc *p, UInt32 numPosStates, const
UInt32 *ProbPrices) |
786 { | 836 { |
787 UInt32 posState; | 837 UInt32 posState; |
788 for (posState = 0; posState < numPosStates; posState++) | 838 for (posState = 0; posState < numPosStates; posState++) |
789 LenPriceEnc_UpdateTable(p, posState, ProbPrices); | 839 LenPriceEnc_UpdateTable(p, posState, ProbPrices); |
790 } | 840 } |
791 | 841 |
792 static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32
posState, Bool updatePrice, UInt32 *ProbPrices) | 842 static void LenEnc_Encode2(CLenPriceEnc *p, CRangeEnc *rc, UInt32 symbol, UInt32
posState, Bool updatePrice, const UInt32 *ProbPrices) |
793 { | 843 { |
794 LenEnc_Encode(&p->p, rc, symbol, posState); | 844 LenEnc_Encode(&p->p, rc, symbol, posState); |
795 if (updatePrice) | 845 if (updatePrice) |
796 if (--p->counters[posState] == 0) | 846 if (--p->counters[posState] == 0) |
797 LenPriceEnc_UpdateTable(p, posState, ProbPrices); | 847 LenPriceEnc_UpdateTable(p, posState, ProbPrices); |
798 } | 848 } |
799 | 849 |
800 | 850 |
801 | 851 |
802 | 852 |
803 static void MovePos(CLzmaEnc *p, UInt32 num) | 853 static void MovePos(CLzmaEnc *p, UInt32 num) |
804 { | 854 { |
805 #ifdef SHOW_STAT | 855 #ifdef SHOW_STAT |
806 ttt += num; | 856 g_STAT_OFFSET += num; |
807 printf("\n MovePos %d", num); | 857 printf("\n MovePos %u", num); |
808 #endif | 858 #endif |
| 859 |
809 if (num != 0) | 860 if (num != 0) |
810 { | 861 { |
811 p->additionalOffset += num; | 862 p->additionalOffset += num; |
812 p->matchFinder.Skip(p->matchFinderObj, num); | 863 p->matchFinder.Skip(p->matchFinderObj, num); |
813 } | 864 } |
814 } | 865 } |
815 | 866 |
816 static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes) | 867 static UInt32 ReadMatchDistances(CLzmaEnc *p, UInt32 *numDistancePairsRes) |
817 { | 868 { |
818 UInt32 lenRes = 0, numPairs; | 869 UInt32 lenRes = 0, numPairs; |
819 p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj); | 870 p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj); |
820 numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches); | 871 numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches); |
| 872 |
821 #ifdef SHOW_STAT | 873 #ifdef SHOW_STAT |
822 printf("\n i = %d numPairs = %d ", ttt, numPairs / 2); | 874 printf("\n i = %u numPairs = %u ", g_STAT_OFFSET, numPairs / 2); |
823 ttt++; | 875 g_STAT_OFFSET++; |
824 { | 876 { |
825 UInt32 i; | 877 UInt32 i; |
826 for (i = 0; i < numPairs; i += 2) | 878 for (i = 0; i < numPairs; i += 2) |
827 printf("%2d %6d | ", p->matches[i], p->matches[i + 1]); | 879 printf("%2u %6u | ", p->matches[i], p->matches[i + 1]); |
828 } | 880 } |
829 #endif | 881 #endif |
| 882 |
830 if (numPairs > 0) | 883 if (numPairs > 0) |
831 { | 884 { |
832 lenRes = p->matches[numPairs - 2]; | 885 lenRes = p->matches[numPairs - 2]; |
833 if (lenRes == p->numFastBytes) | 886 if (lenRes == p->numFastBytes) |
834 { | 887 { |
835 const Byte *pby = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj)
- 1; | |
836 UInt32 distance = p->matches[numPairs - 1] + 1; | |
837 UInt32 numAvail = p->numAvail; | 888 UInt32 numAvail = p->numAvail; |
838 if (numAvail > LZMA_MATCH_LEN_MAX) | 889 if (numAvail > LZMA_MATCH_LEN_MAX) |
839 numAvail = LZMA_MATCH_LEN_MAX; | 890 numAvail = LZMA_MATCH_LEN_MAX; |
840 { | 891 { |
841 const Byte *pby2 = pby - distance; | 892 const Byte *pbyCur = p->matchFinder.GetPointerToCurrentPos(p->matchFinde
rObj) - 1; |
842 for (; lenRes < numAvail && pby[lenRes] == pby2[lenRes]; lenRes++); | 893 const Byte *pby = pbyCur + lenRes; |
| 894 ptrdiff_t dif = (ptrdiff_t)-1 - p->matches[numPairs - 1]; |
| 895 const Byte *pbyLim = pbyCur + numAvail; |
| 896 for (; pby != pbyLim && *pby == pby[dif]; pby++); |
| 897 lenRes = (UInt32)(pby - pbyCur); |
843 } | 898 } |
844 } | 899 } |
845 } | 900 } |
846 p->additionalOffset++; | 901 p->additionalOffset++; |
847 *numDistancePairsRes = numPairs; | 902 *numDistancePairsRes = numPairs; |
848 return lenRes; | 903 return lenRes; |
849 } | 904 } |
850 | 905 |
851 | 906 |
852 #define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False; | 907 #define MakeAsChar(p) (p)->backPrev = (UInt32)(-1); (p)->prev1IsChar = False; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
917 p->opt[posPrev].posPrev = cur; | 972 p->opt[posPrev].posPrev = cur; |
918 cur = posPrev; | 973 cur = posPrev; |
919 } | 974 } |
920 } | 975 } |
921 while (cur != 0); | 976 while (cur != 0); |
922 *backRes = p->opt[0].backPrev; | 977 *backRes = p->opt[0].backPrev; |
923 p->optimumCurrentIndex = p->opt[0].posPrev; | 978 p->optimumCurrentIndex = p->opt[0].posPrev; |
924 return p->optimumCurrentIndex; | 979 return p->optimumCurrentIndex; |
925 } | 980 } |
926 | 981 |
927 #define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc)
+ ((prevByte) >> (8 - p->lc))) * 0x300) | 982 #define LIT_PROBS(pos, prevByte) (p->litProbs + ((((pos) & p->lpMask) << p->lc)
+ ((prevByte) >> (8 - p->lc))) * (UInt32)0x300) |
928 | 983 |
929 static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes) | 984 static UInt32 GetOptimum(CLzmaEnc *p, UInt32 position, UInt32 *backRes) |
930 { | 985 { |
931 UInt32 numAvail, mainLen, numPairs, repMaxIndex, i, posState, lenEnd, len, cur
; | 986 UInt32 numAvail, mainLen, numPairs, repMaxIndex, i, posState, lenEnd, len, cur
; |
932 UInt32 matchPrice, repMatchPrice, normalMatchPrice; | 987 UInt32 matchPrice, repMatchPrice, normalMatchPrice; |
933 UInt32 reps[LZMA_NUM_REPS], repLens[LZMA_NUM_REPS]; | 988 UInt32 reps[LZMA_NUM_REPS], repLens[LZMA_NUM_REPS]; |
934 UInt32 *matches; | 989 UInt32 *matches; |
935 const Byte *data; | 990 const Byte *data; |
936 Byte curByte, matchByte; | 991 Byte curByte, matchByte; |
937 if (p->optimumEndIndex != p->optimumCurrentIndex) | 992 if (p->optimumEndIndex != p->optimumCurrentIndex) |
(...skipping 23 matching lines...) Expand all Loading... |
961 if (numAvail > LZMA_MATCH_LEN_MAX) | 1016 if (numAvail > LZMA_MATCH_LEN_MAX) |
962 numAvail = LZMA_MATCH_LEN_MAX; | 1017 numAvail = LZMA_MATCH_LEN_MAX; |
963 | 1018 |
964 data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; | 1019 data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; |
965 repMaxIndex = 0; | 1020 repMaxIndex = 0; |
966 for (i = 0; i < LZMA_NUM_REPS; i++) | 1021 for (i = 0; i < LZMA_NUM_REPS; i++) |
967 { | 1022 { |
968 UInt32 lenTest; | 1023 UInt32 lenTest; |
969 const Byte *data2; | 1024 const Byte *data2; |
970 reps[i] = p->reps[i]; | 1025 reps[i] = p->reps[i]; |
971 data2 = data - (reps[i] + 1); | 1026 data2 = data - reps[i] - 1; |
972 if (data[0] != data2[0] || data[1] != data2[1]) | 1027 if (data[0] != data2[0] || data[1] != data2[1]) |
973 { | 1028 { |
974 repLens[i] = 0; | 1029 repLens[i] = 0; |
975 continue; | 1030 continue; |
976 } | 1031 } |
977 for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; len
Test++); | 1032 for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; len
Test++); |
978 repLens[i] = lenTest; | 1033 repLens[i] = lenTest; |
979 if (lenTest > repLens[repMaxIndex]) | 1034 if (lenTest > repLens[repMaxIndex]) |
980 repMaxIndex = i; | 1035 repMaxIndex = i; |
981 } | 1036 } |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1105 offs += 2; | 1160 offs += 2; |
1106 if (offs == numPairs) | 1161 if (offs == numPairs) |
1107 break; | 1162 break; |
1108 } | 1163 } |
1109 } | 1164 } |
1110 } | 1165 } |
1111 | 1166 |
1112 cur = 0; | 1167 cur = 0; |
1113 | 1168 |
1114 #ifdef SHOW_STAT2 | 1169 #ifdef SHOW_STAT2 |
1115 if (position >= 0) | 1170 /* if (position >= 0) */ |
1116 { | 1171 { |
1117 unsigned i; | 1172 unsigned i; |
1118 printf("\n pos = %4X", position); | 1173 printf("\n pos = %4X", position); |
1119 for (i = cur; i <= lenEnd; i++) | 1174 for (i = cur; i <= lenEnd; i++) |
1120 printf("\nprice[%4X] = %d", position - cur + i, p->opt[i].price); | 1175 printf("\nprice[%4X] = %u", position - cur + i, p->opt[i].price); |
1121 } | 1176 } |
1122 #endif | 1177 #endif |
1123 | 1178 |
1124 for (;;) | 1179 for (;;) |
1125 { | 1180 { |
1126 UInt32 numAvailFull, newLen, numPairs, posPrev, state, posState, startLen; | 1181 UInt32 numAvailFull, newLen, numPairs, posPrev, state, posState, startLen; |
1127 UInt32 curPrice, curAnd1Price, matchPrice, repMatchPrice; | 1182 UInt32 curPrice, curAnd1Price, matchPrice, repMatchPrice; |
1128 Bool nextIsChar; | 1183 Bool nextIsChar; |
1129 Byte curByte, matchByte; | 1184 Byte curByte, matchByte; |
1130 const Byte *data; | 1185 const Byte *data; |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1262 | 1317 |
1263 if (numAvailFull < 2) | 1318 if (numAvailFull < 2) |
1264 continue; | 1319 continue; |
1265 numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes
); | 1320 numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes
); |
1266 | 1321 |
1267 if (!nextIsChar && matchByte != curByte) /* speed optimization */ | 1322 if (!nextIsChar && matchByte != curByte) /* speed optimization */ |
1268 { | 1323 { |
1269 /* try Literal + rep0 */ | 1324 /* try Literal + rep0 */ |
1270 UInt32 temp; | 1325 UInt32 temp; |
1271 UInt32 lenTest2; | 1326 UInt32 lenTest2; |
1272 const Byte *data2 = data - (reps[0] + 1); | 1327 const Byte *data2 = data - reps[0] - 1; |
1273 UInt32 limit = p->numFastBytes + 1; | 1328 UInt32 limit = p->numFastBytes + 1; |
1274 if (limit > numAvailFull) | 1329 if (limit > numAvailFull) |
1275 limit = numAvailFull; | 1330 limit = numAvailFull; |
1276 | 1331 |
1277 for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++); | 1332 for (temp = 1; temp < limit && data[temp] == data2[temp]; temp++); |
1278 lenTest2 = temp - 1; | 1333 lenTest2 = temp - 1; |
1279 if (lenTest2 >= 2) | 1334 if (lenTest2 >= 2) |
1280 { | 1335 { |
1281 UInt32 state2 = kLiteralNextStates[state]; | 1336 UInt32 state2 = kLiteralNextStates[state]; |
1282 UInt32 posStateNext = (position + 1) & p->pbMask; | 1337 UInt32 posStateNext = (position + 1) & p->pbMask; |
(...skipping 22 matching lines...) Expand all Loading... |
1305 } | 1360 } |
1306 | 1361 |
1307 startLen = 2; /* speed optimization */ | 1362 startLen = 2; /* speed optimization */ |
1308 { | 1363 { |
1309 UInt32 repIndex; | 1364 UInt32 repIndex; |
1310 for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++) | 1365 for (repIndex = 0; repIndex < LZMA_NUM_REPS; repIndex++) |
1311 { | 1366 { |
1312 UInt32 lenTest; | 1367 UInt32 lenTest; |
1313 UInt32 lenTestTemp; | 1368 UInt32 lenTestTemp; |
1314 UInt32 price; | 1369 UInt32 price; |
1315 const Byte *data2 = data - (reps[repIndex] + 1); | 1370 const Byte *data2 = data - reps[repIndex] - 1; |
1316 if (data[0] != data2[0] || data[1] != data2[1]) | 1371 if (data[0] != data2[0] || data[1] != data2[1]) |
1317 continue; | 1372 continue; |
1318 for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; l
enTest++); | 1373 for (lenTest = 2; lenTest < numAvail && data[lenTest] == data2[lenTest]; l
enTest++); |
1319 while (lenEnd < cur + lenTest) | 1374 while (lenEnd < cur + lenTest) |
1320 p->opt[++lenEnd].price = kInfinityPrice; | 1375 p->opt[++lenEnd].price = kInfinityPrice; |
1321 lenTestTemp = lenTest; | 1376 lenTestTemp = lenTest; |
1322 price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState); | 1377 price = repMatchPrice + GetPureRepPrice(p, repIndex, state, posState); |
1323 do | 1378 do |
1324 { | 1379 { |
1325 UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest -
2]; | 1380 UInt32 curAndLenPrice = price + p->repLenEnc.prices[posState][lenTest -
2]; |
1326 COptimal *opt = &p->opt[cur + lenTest]; | 1381 COptimal *opt = &p->opt[cur + lenTest]; |
1327 if (curAndLenPrice < opt->price) | 1382 if (curAndLenPrice < opt->price) |
1328 { | 1383 { |
1329 opt->price = curAndLenPrice; | 1384 opt->price = curAndLenPrice; |
1330 opt->posPrev = cur; | 1385 opt->posPrev = cur; |
1331 opt->backPrev = repIndex; | 1386 opt->backPrev = repIndex; |
1332 opt->prev1IsChar = False; | 1387 opt->prev1IsChar = False; |
1333 } | 1388 } |
1334 } | 1389 } |
1335 while (--lenTest >= 2); | 1390 while (--lenTest >= 2); |
1336 lenTest = lenTestTemp; | 1391 lenTest = lenTestTemp; |
1337 | 1392 |
1338 if (repIndex == 0) | 1393 if (repIndex == 0) |
1339 startLen = lenTest + 1; | 1394 startLen = lenTest + 1; |
1340 | 1395 |
1341 /* if (_maxMode) */ | 1396 /* if (_maxMode) */ |
1342 { | 1397 { |
1343 UInt32 lenTest2 = lenTest + 1; | 1398 UInt32 lenTest2 = lenTest + 1; |
1344 UInt32 limit = lenTest2 + p->numFastBytes; | 1399 UInt32 limit = lenTest2 + p->numFastBytes; |
1345 UInt32 nextRepMatchPrice; | |
1346 if (limit > numAvailFull) | 1400 if (limit > numAvailFull) |
1347 limit = numAvailFull; | 1401 limit = numAvailFull; |
1348 for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2
++); | 1402 for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2
++); |
1349 lenTest2 -= lenTest + 1; | 1403 lenTest2 -= lenTest + 1; |
1350 if (lenTest2 >= 2) | 1404 if (lenTest2 >= 2) |
1351 { | 1405 { |
| 1406 UInt32 nextRepMatchPrice; |
1352 UInt32 state2 = kRepNextStates[state]; | 1407 UInt32 state2 = kRepNextStates[state]; |
1353 UInt32 posStateNext = (position + lenTest) & p->pbMask; | 1408 UInt32 posStateNext = (position + lenTest) & p->pbMask; |
1354 UInt32 curAndLenCharPrice = | 1409 UInt32 curAndLenCharPrice = |
1355 price + p->repLenEnc.prices[posState][lenTest - 2] + | 1410 price + p->repLenEnc.prices[posState][lenTest - 2] + |
1356 GET_PRICE_0(p->isMatch[state2][posStateNext]) + | 1411 GET_PRICE_0(p->isMatch[state2][posStateNext]) + |
1357 LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTes
t - 1]), | 1412 LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTes
t - 1]), |
1358 data[lenTest], data2[lenTest], p->ProbPrices); | 1413 data[lenTest], data2[lenTest], p->ProbPrices); |
1359 state2 = kLiteralNextStates[state2]; | 1414 state2 = kLiteralNextStates[state2]; |
1360 posStateNext = (position + lenTest + 1) & p->pbMask; | 1415 posStateNext = (position + lenTest + 1) & p->pbMask; |
1361 nextRepMatchPrice = curAndLenCharPrice + | 1416 nextRepMatchPrice = curAndLenCharPrice + |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1422 { | 1477 { |
1423 opt->price = curAndLenPrice; | 1478 opt->price = curAndLenPrice; |
1424 opt->posPrev = cur; | 1479 opt->posPrev = cur; |
1425 opt->backPrev = curBack + LZMA_NUM_REPS; | 1480 opt->backPrev = curBack + LZMA_NUM_REPS; |
1426 opt->prev1IsChar = False; | 1481 opt->prev1IsChar = False; |
1427 } | 1482 } |
1428 | 1483 |
1429 if (/*_maxMode && */lenTest == matches[offs]) | 1484 if (/*_maxMode && */lenTest == matches[offs]) |
1430 { | 1485 { |
1431 /* Try Match + Literal + Rep0 */ | 1486 /* Try Match + Literal + Rep0 */ |
1432 const Byte *data2 = data - (curBack + 1); | 1487 const Byte *data2 = data - curBack - 1; |
1433 UInt32 lenTest2 = lenTest + 1; | 1488 UInt32 lenTest2 = lenTest + 1; |
1434 UInt32 limit = lenTest2 + p->numFastBytes; | 1489 UInt32 limit = lenTest2 + p->numFastBytes; |
1435 UInt32 nextRepMatchPrice; | |
1436 if (limit > numAvailFull) | 1490 if (limit > numAvailFull) |
1437 limit = numAvailFull; | 1491 limit = numAvailFull; |
1438 for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2
++); | 1492 for (; lenTest2 < limit && data[lenTest2] == data2[lenTest2]; lenTest2
++); |
1439 lenTest2 -= lenTest + 1; | 1493 lenTest2 -= lenTest + 1; |
1440 if (lenTest2 >= 2) | 1494 if (lenTest2 >= 2) |
1441 { | 1495 { |
| 1496 UInt32 nextRepMatchPrice; |
1442 UInt32 state2 = kMatchNextStates[state]; | 1497 UInt32 state2 = kMatchNextStates[state]; |
1443 UInt32 posStateNext = (position + lenTest) & p->pbMask; | 1498 UInt32 posStateNext = (position + lenTest) & p->pbMask; |
1444 UInt32 curAndLenCharPrice = curAndLenPrice + | 1499 UInt32 curAndLenCharPrice = curAndLenPrice + |
1445 GET_PRICE_0(p->isMatch[state2][posStateNext]) + | 1500 GET_PRICE_0(p->isMatch[state2][posStateNext]) + |
1446 LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTes
t - 1]), | 1501 LitEnc_GetPriceMatched(LIT_PROBS(position + lenTest, data[lenTes
t - 1]), |
1447 data[lenTest], data2[lenTest], p->ProbPrices); | 1502 data[lenTest], data2[lenTest], p->ProbPrices); |
1448 state2 = kLiteralNextStates[state2]; | 1503 state2 = kLiteralNextStates[state2]; |
1449 posStateNext = (posStateNext + 1) & p->pbMask; | 1504 posStateNext = (posStateNext + 1) & p->pbMask; |
1450 nextRepMatchPrice = curAndLenCharPrice + | 1505 nextRepMatchPrice = curAndLenCharPrice + |
1451 GET_PRICE_1(p->isMatch[state2][posStateNext]) + | 1506 GET_PRICE_1(p->isMatch[state2][posStateNext]) + |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1505 if (numAvail < 2) | 1560 if (numAvail < 2) |
1506 return 1; | 1561 return 1; |
1507 if (numAvail > LZMA_MATCH_LEN_MAX) | 1562 if (numAvail > LZMA_MATCH_LEN_MAX) |
1508 numAvail = LZMA_MATCH_LEN_MAX; | 1563 numAvail = LZMA_MATCH_LEN_MAX; |
1509 data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; | 1564 data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; |
1510 | 1565 |
1511 repLen = repIndex = 0; | 1566 repLen = repIndex = 0; |
1512 for (i = 0; i < LZMA_NUM_REPS; i++) | 1567 for (i = 0; i < LZMA_NUM_REPS; i++) |
1513 { | 1568 { |
1514 UInt32 len; | 1569 UInt32 len; |
1515 const Byte *data2 = data - (p->reps[i] + 1); | 1570 const Byte *data2 = data - p->reps[i] - 1; |
1516 if (data[0] != data2[0] || data[1] != data2[1]) | 1571 if (data[0] != data2[0] || data[1] != data2[1]) |
1517 continue; | 1572 continue; |
1518 for (len = 2; len < numAvail && data[len] == data2[len]; len++); | 1573 for (len = 2; len < numAvail && data[len] == data2[len]; len++); |
1519 if (len >= p->numFastBytes) | 1574 if (len >= p->numFastBytes) |
1520 { | 1575 { |
1521 *backRes = i; | 1576 *backRes = i; |
1522 MovePos(p, len - 1); | 1577 MovePos(p, len - 1); |
1523 return len; | 1578 return len; |
1524 } | 1579 } |
1525 if (len > repLen) | 1580 if (len > repLen) |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1574 (p->longestMatchLength == mainLen + 1 && !ChangePair(mainDist, newDistan
ce)) || | 1629 (p->longestMatchLength == mainLen + 1 && !ChangePair(mainDist, newDistan
ce)) || |
1575 (p->longestMatchLength > mainLen + 1) || | 1630 (p->longestMatchLength > mainLen + 1) || |
1576 (p->longestMatchLength + 1 >= mainLen && mainLen >= 3 && ChangePair(newD
istance, mainDist))) | 1631 (p->longestMatchLength + 1 >= mainLen && mainLen >= 3 && ChangePair(newD
istance, mainDist))) |
1577 return 1; | 1632 return 1; |
1578 } | 1633 } |
1579 | 1634 |
1580 data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; | 1635 data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; |
1581 for (i = 0; i < LZMA_NUM_REPS; i++) | 1636 for (i = 0; i < LZMA_NUM_REPS; i++) |
1582 { | 1637 { |
1583 UInt32 len, limit; | 1638 UInt32 len, limit; |
1584 const Byte *data2 = data - (p->reps[i] + 1); | 1639 const Byte *data2 = data - p->reps[i] - 1; |
1585 if (data[0] != data2[0] || data[1] != data2[1]) | 1640 if (data[0] != data2[0] || data[1] != data2[1]) |
1586 continue; | 1641 continue; |
1587 limit = mainLen - 1; | 1642 limit = mainLen - 1; |
1588 for (len = 2; len < limit && data[len] == data2[len]; len++); | 1643 for (len = 2; len < limit && data[len] == data2[len]; len++); |
1589 if (len >= limit) | 1644 if (len >= limit) |
1590 return 1; | 1645 return 1; |
1591 } | 1646 } |
1592 *backRes = mainDist + LZMA_NUM_REPS; | 1647 *backRes = mainDist + LZMA_NUM_REPS; |
1593 MovePos(p, mainLen - 2); | 1648 MovePos(p, mainLen - 2); |
1594 return mainLen; | 1649 return mainLen; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1670 distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i]; | 1725 distancesPrices[i] = posSlotPrices[GetPosSlot1(i)] + tempPrices[i]; |
1671 } | 1726 } |
1672 } | 1727 } |
1673 p->matchPriceCount = 0; | 1728 p->matchPriceCount = 0; |
1674 } | 1729 } |
1675 | 1730 |
1676 void LzmaEnc_Construct(CLzmaEnc *p) | 1731 void LzmaEnc_Construct(CLzmaEnc *p) |
1677 { | 1732 { |
1678 RangeEnc_Construct(&p->rc); | 1733 RangeEnc_Construct(&p->rc); |
1679 MatchFinder_Construct(&p->matchFinderBase); | 1734 MatchFinder_Construct(&p->matchFinderBase); |
| 1735 |
1680 #ifndef _7ZIP_ST | 1736 #ifndef _7ZIP_ST |
1681 MatchFinderMt_Construct(&p->matchFinderMt); | 1737 MatchFinderMt_Construct(&p->matchFinderMt); |
1682 p->matchFinderMt.MatchFinder = &p->matchFinderBase; | 1738 p->matchFinderMt.MatchFinder = &p->matchFinderBase; |
1683 #endif | 1739 #endif |
1684 | 1740 |
1685 { | 1741 { |
1686 CLzmaEncProps props; | 1742 CLzmaEncProps props; |
1687 LzmaEncProps_Init(&props); | 1743 LzmaEncProps_Init(&props); |
1688 LzmaEnc_SetProps(p, &props); | 1744 LzmaEnc_SetProps(p, &props); |
1689 } | 1745 } |
1690 | 1746 |
1691 #ifndef LZMA_LOG_BSR | 1747 #ifndef LZMA_LOG_BSR |
1692 LzmaEnc_FastPosInit(p->g_FastPos); | 1748 LzmaEnc_FastPosInit(p->g_FastPos); |
1693 #endif | 1749 #endif |
1694 | 1750 |
1695 LzmaEnc_InitPriceTables(p->ProbPrices); | 1751 LzmaEnc_InitPriceTables(p->ProbPrices); |
1696 p->litProbs = 0; | 1752 p->litProbs = NULL; |
1697 p->saveState.litProbs = 0; | 1753 p->saveState.litProbs = NULL; |
1698 } | 1754 } |
1699 | 1755 |
1700 CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc) | 1756 CLzmaEncHandle LzmaEnc_Create(ISzAlloc *alloc) |
1701 { | 1757 { |
1702 void *p; | 1758 void *p; |
1703 p = alloc->Alloc(alloc, sizeof(CLzmaEnc)); | 1759 p = alloc->Alloc(alloc, sizeof(CLzmaEnc)); |
1704 if (p != 0) | 1760 if (p) |
1705 LzmaEnc_Construct((CLzmaEnc *)p); | 1761 LzmaEnc_Construct((CLzmaEnc *)p); |
1706 return p; | 1762 return p; |
1707 } | 1763 } |
1708 | 1764 |
1709 void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc) | 1765 void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAlloc *alloc) |
1710 { | 1766 { |
1711 alloc->Free(alloc, p->litProbs); | 1767 alloc->Free(alloc, p->litProbs); |
1712 alloc->Free(alloc, p->saveState.litProbs); | 1768 alloc->Free(alloc, p->saveState.litProbs); |
1713 p->litProbs = 0; | 1769 p->litProbs = NULL; |
1714 p->saveState.litProbs = 0; | 1770 p->saveState.litProbs = NULL; |
1715 } | 1771 } |
1716 | 1772 |
1717 void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig) | 1773 void LzmaEnc_Destruct(CLzmaEnc *p, ISzAlloc *alloc, ISzAlloc *allocBig) |
1718 { | 1774 { |
1719 #ifndef _7ZIP_ST | 1775 #ifndef _7ZIP_ST |
1720 MatchFinderMt_Destruct(&p->matchFinderMt, allocBig); | 1776 MatchFinderMt_Destruct(&p->matchFinderMt, allocBig); |
1721 #endif | 1777 #endif |
| 1778 |
1722 MatchFinder_Free(&p->matchFinderBase, allocBig); | 1779 MatchFinder_Free(&p->matchFinderBase, allocBig); |
1723 LzmaEnc_FreeLits(p, alloc); | 1780 LzmaEnc_FreeLits(p, alloc); |
1724 RangeEnc_Free(&p->rc, alloc); | 1781 RangeEnc_Free(&p->rc, alloc); |
1725 } | 1782 } |
1726 | 1783 |
1727 void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig) | 1784 void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAlloc *alloc, ISzAlloc *allocBig) |
1728 { | 1785 { |
1729 LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig); | 1786 LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig); |
1730 alloc->Free(alloc, p); | 1787 alloc->Free(alloc, p); |
1731 } | 1788 } |
(...skipping 16 matching lines...) Expand all Loading... |
1748 | 1805 |
1749 if (p->nowPos64 == 0) | 1806 if (p->nowPos64 == 0) |
1750 { | 1807 { |
1751 UInt32 numPairs; | 1808 UInt32 numPairs; |
1752 Byte curByte; | 1809 Byte curByte; |
1753 if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0) | 1810 if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0) |
1754 return Flush(p, nowPos32); | 1811 return Flush(p, nowPos32); |
1755 ReadMatchDistances(p, &numPairs); | 1812 ReadMatchDistances(p, &numPairs); |
1756 RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0); | 1813 RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][0], 0); |
1757 p->state = kLiteralNextStates[p->state]; | 1814 p->state = kLiteralNextStates[p->state]; |
1758 curByte = p->matchFinder.GetIndexByte(p->matchFinderObj, 0 - p->additionalOf
fset); | 1815 curByte = *(p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->ad
ditionalOffset); |
1759 LitEnc_Encode(&p->rc, p->litProbs, curByte); | 1816 LitEnc_Encode(&p->rc, p->litProbs, curByte); |
1760 p->additionalOffset--; | 1817 p->additionalOffset--; |
1761 nowPos32++; | 1818 nowPos32++; |
1762 } | 1819 } |
1763 | 1820 |
1764 if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0) | 1821 if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0) |
1765 for (;;) | 1822 for (;;) |
1766 { | 1823 { |
1767 UInt32 pos, len, posState; | 1824 UInt32 pos, len, posState; |
1768 | 1825 |
1769 if (p->fastMode) | 1826 if (p->fastMode) |
1770 len = GetOptimumFast(p, &pos); | 1827 len = GetOptimumFast(p, &pos); |
1771 else | 1828 else |
1772 len = GetOptimum(p, nowPos32, &pos); | 1829 len = GetOptimum(p, nowPos32, &pos); |
1773 | 1830 |
1774 #ifdef SHOW_STAT2 | 1831 #ifdef SHOW_STAT2 |
1775 printf("\n pos = %4X, len = %d pos = %d", nowPos32, len, pos); | 1832 printf("\n pos = %4X, len = %u pos = %u", nowPos32, len, pos); |
1776 #endif | 1833 #endif |
1777 | 1834 |
1778 posState = nowPos32 & p->pbMask; | 1835 posState = nowPos32 & p->pbMask; |
1779 if (len == 1 && pos == (UInt32)-1) | 1836 if (len == 1 && pos == (UInt32)-1) |
1780 { | 1837 { |
1781 Byte curByte; | 1838 Byte curByte; |
1782 CLzmaProb *probs; | 1839 CLzmaProb *probs; |
1783 const Byte *data; | 1840 const Byte *data; |
1784 | 1841 |
1785 RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0); | 1842 RangeEnc_EncodeBit(&p->rc, &p->isMatch[p->state][posState], 0); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1874 } | 1931 } |
1875 if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0) | 1932 if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0) |
1876 break; | 1933 break; |
1877 processed = nowPos32 - startPos32; | 1934 processed = nowPos32 - startPos32; |
1878 if (useLimits) | 1935 if (useLimits) |
1879 { | 1936 { |
1880 if (processed + kNumOpts + 300 >= maxUnpackSize || | 1937 if (processed + kNumOpts + 300 >= maxUnpackSize || |
1881 RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize) | 1938 RangeEnc_GetProcessed(&p->rc) + kNumOpts * 2 >= maxPackSize) |
1882 break; | 1939 break; |
1883 } | 1940 } |
1884 else if (processed >= (1 << 15)) | 1941 else if (processed >= (1 << 17)) |
1885 { | 1942 { |
1886 p->nowPos64 += nowPos32 - startPos32; | 1943 p->nowPos64 += nowPos32 - startPos32; |
1887 return CheckErrors(p); | 1944 return CheckErrors(p); |
1888 } | 1945 } |
1889 } | 1946 } |
1890 } | 1947 } |
1891 p->nowPos64 += nowPos32 - startPos32; | 1948 p->nowPos64 += nowPos32 - startPos32; |
1892 return Flush(p, nowPos32); | 1949 return Flush(p, nowPos32); |
1893 } | 1950 } |
1894 | 1951 |
1895 #define kBigHashDicLimit ((UInt32)1 << 24) | 1952 #define kBigHashDicLimit ((UInt32)1 << 24) |
1896 | 1953 |
1897 static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, I
SzAlloc *allocBig) | 1954 static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAlloc *alloc, I
SzAlloc *allocBig) |
1898 { | 1955 { |
1899 UInt32 beforeSize = kNumOpts; | 1956 UInt32 beforeSize = kNumOpts; |
1900 Bool btMode; | |
1901 if (!RangeEnc_Alloc(&p->rc, alloc)) | 1957 if (!RangeEnc_Alloc(&p->rc, alloc)) |
1902 return SZ_ERROR_MEM; | 1958 return SZ_ERROR_MEM; |
1903 btMode = (p->matchFinderBase.btMode != 0); | 1959 |
1904 #ifndef _7ZIP_ST | 1960 #ifndef _7ZIP_ST |
1905 p->mtMode = (p->multiThread && !p->fastMode && btMode); | 1961 p->mtMode = (p->multiThread && !p->fastMode && (p->matchFinderBase.btMode != 0
)); |
1906 #endif | 1962 #endif |
1907 | 1963 |
1908 { | 1964 { |
1909 unsigned lclp = p->lc + p->lp; | 1965 unsigned lclp = p->lc + p->lp; |
1910 if (p->litProbs == 0 || p->saveState.litProbs == 0 || p->lclp != lclp) | 1966 if (!p->litProbs || !p->saveState.litProbs || p->lclp != lclp) |
1911 { | 1967 { |
1912 LzmaEnc_FreeLits(p, alloc); | 1968 LzmaEnc_FreeLits(p, alloc); |
1913 p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) * sizeof(CL
zmaProb)); | 1969 p->litProbs = (CLzmaProb *)alloc->Alloc(alloc, ((UInt32)0x300 << lclp) * s
izeof(CLzmaProb)); |
1914 p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, (0x300 << lclp) *
sizeof(CLzmaProb)); | 1970 p->saveState.litProbs = (CLzmaProb *)alloc->Alloc(alloc, ((UInt32)0x300 <<
lclp) * sizeof(CLzmaProb)); |
1915 if (p->litProbs == 0 || p->saveState.litProbs == 0) | 1971 if (!p->litProbs || !p->saveState.litProbs) |
1916 { | 1972 { |
1917 LzmaEnc_FreeLits(p, alloc); | 1973 LzmaEnc_FreeLits(p, alloc); |
1918 return SZ_ERROR_MEM; | 1974 return SZ_ERROR_MEM; |
1919 } | 1975 } |
1920 p->lclp = lclp; | 1976 p->lclp = lclp; |
1921 } | 1977 } |
1922 } | 1978 } |
1923 | 1979 |
1924 p->matchFinderBase.bigHash = (p->dictSize > kBigHashDicLimit); | 1980 p->matchFinderBase.bigHash = (Byte)(p->dictSize > kBigHashDicLimit ? 1 : 0); |
1925 | 1981 |
1926 if (beforeSize + p->dictSize < keepWindowSize) | 1982 if (beforeSize + p->dictSize < keepWindowSize) |
1927 beforeSize = keepWindowSize - p->dictSize; | 1983 beforeSize = keepWindowSize - p->dictSize; |
1928 | 1984 |
1929 #ifndef _7ZIP_ST | 1985 #ifndef _7ZIP_ST |
1930 if (p->mtMode) | 1986 if (p->mtMode) |
1931 { | 1987 { |
1932 RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->nu
mFastBytes, LZMA_MATCH_LEN_MAX, allocBig)); | 1988 RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->nu
mFastBytes, LZMA_MATCH_LEN_MAX, allocBig)); |
1933 p->matchFinderObj = &p->matchFinderMt; | 1989 p->matchFinderObj = &p->matchFinderMt; |
1934 MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder); | 1990 MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder); |
1935 } | 1991 } |
1936 else | 1992 else |
1937 #endif | 1993 #endif |
1938 { | 1994 { |
1939 if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->num
FastBytes, LZMA_MATCH_LEN_MAX, allocBig)) | 1995 if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->num
FastBytes, LZMA_MATCH_LEN_MAX, allocBig)) |
1940 return SZ_ERROR_MEM; | 1996 return SZ_ERROR_MEM; |
1941 p->matchFinderObj = &p->matchFinderBase; | 1997 p->matchFinderObj = &p->matchFinderBase; |
1942 MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder); | 1998 MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder); |
1943 } | 1999 } |
| 2000 |
1944 return SZ_OK; | 2001 return SZ_OK; |
1945 } | 2002 } |
1946 | 2003 |
1947 void LzmaEnc_Init(CLzmaEnc *p) | 2004 void LzmaEnc_Init(CLzmaEnc *p) |
1948 { | 2005 { |
1949 UInt32 i; | 2006 UInt32 i; |
1950 p->state = 0; | 2007 p->state = 0; |
1951 for (i = 0 ; i < LZMA_NUM_REPS; i++) | 2008 for (i = 0 ; i < LZMA_NUM_REPS; i++) |
1952 p->reps[i] = 0; | 2009 p->reps[i] = 0; |
1953 | 2010 |
1954 RangeEnc_Init(&p->rc); | 2011 RangeEnc_Init(&p->rc); |
1955 | 2012 |
1956 | 2013 |
1957 for (i = 0; i < kNumStates; i++) | 2014 for (i = 0; i < kNumStates; i++) |
1958 { | 2015 { |
1959 UInt32 j; | 2016 UInt32 j; |
1960 for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++) | 2017 for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++) |
1961 { | 2018 { |
1962 p->isMatch[i][j] = kProbInitValue; | 2019 p->isMatch[i][j] = kProbInitValue; |
1963 p->isRep0Long[i][j] = kProbInitValue; | 2020 p->isRep0Long[i][j] = kProbInitValue; |
1964 } | 2021 } |
1965 p->isRep[i] = kProbInitValue; | 2022 p->isRep[i] = kProbInitValue; |
1966 p->isRepG0[i] = kProbInitValue; | 2023 p->isRepG0[i] = kProbInitValue; |
1967 p->isRepG1[i] = kProbInitValue; | 2024 p->isRepG1[i] = kProbInitValue; |
1968 p->isRepG2[i] = kProbInitValue; | 2025 p->isRepG2[i] = kProbInitValue; |
1969 } | 2026 } |
1970 | 2027 |
1971 { | 2028 { |
1972 UInt32 num = 0x300 << (p->lp + p->lc); | 2029 UInt32 num = (UInt32)0x300 << (p->lp + p->lc); |
| 2030 CLzmaProb *probs = p->litProbs; |
1973 for (i = 0; i < num; i++) | 2031 for (i = 0; i < num; i++) |
1974 p->litProbs[i] = kProbInitValue; | 2032 probs[i] = kProbInitValue; |
1975 } | 2033 } |
1976 | 2034 |
1977 { | 2035 { |
1978 for (i = 0; i < kNumLenToPosStates; i++) | 2036 for (i = 0; i < kNumLenToPosStates; i++) |
1979 { | 2037 { |
1980 CLzmaProb *probs = p->posSlotEncoder[i]; | 2038 CLzmaProb *probs = p->posSlotEncoder[i]; |
1981 UInt32 j; | 2039 UInt32 j; |
1982 for (j = 0; j < (1 << kNumPosSlotBits); j++) | 2040 for (j = 0; j < (1 << kNumPosSlotBits); j++) |
1983 probs[j] = kProbInitValue; | 2041 probs[j] = kProbInitValue; |
1984 } | 2042 } |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2071 return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig); | 2129 return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig); |
2072 } | 2130 } |
2073 | 2131 |
2074 void LzmaEnc_Finish(CLzmaEncHandle pp) | 2132 void LzmaEnc_Finish(CLzmaEncHandle pp) |
2075 { | 2133 { |
2076 #ifndef _7ZIP_ST | 2134 #ifndef _7ZIP_ST |
2077 CLzmaEnc *p = (CLzmaEnc *)pp; | 2135 CLzmaEnc *p = (CLzmaEnc *)pp; |
2078 if (p->mtMode) | 2136 if (p->mtMode) |
2079 MatchFinderMt_ReleaseStream(&p->matchFinderMt); | 2137 MatchFinderMt_ReleaseStream(&p->matchFinderMt); |
2080 #else | 2138 #else |
2081 pp = pp; | 2139 UNUSED_VAR(pp); |
2082 #endif | 2140 #endif |
2083 } | 2141 } |
2084 | 2142 |
| 2143 |
2085 typedef struct | 2144 typedef struct |
2086 { | 2145 { |
2087 ISeqOutStream funcTable; | 2146 ISeqOutStream funcTable; |
2088 Byte *data; | 2147 Byte *data; |
2089 SizeT rem; | 2148 SizeT rem; |
2090 Bool overflow; | 2149 Bool overflow; |
2091 } CSeqOutStreamBuf; | 2150 } CSeqOutStreamBuf; |
2092 | 2151 |
2093 static size_t MyWrite(void *pp, const void *data, size_t size) | 2152 static size_t MyWrite(void *pp, const void *data, size_t size) |
2094 { | 2153 { |
2095 CSeqOutStreamBuf *p = (CSeqOutStreamBuf *)pp; | 2154 CSeqOutStreamBuf *p = (CSeqOutStreamBuf *)pp; |
2096 if (p->rem < size) | 2155 if (p->rem < size) |
2097 { | 2156 { |
2098 size = p->rem; | 2157 size = p->rem; |
2099 p->overflow = True; | 2158 p->overflow = True; |
2100 } | 2159 } |
2101 memcpy(p->data, data, size); | 2160 memcpy(p->data, data, size); |
2102 p->rem -= size; | 2161 p->rem -= size; |
2103 p->data += size; | 2162 p->data += size; |
2104 return size; | 2163 return size; |
2105 } | 2164 } |
2106 | 2165 |
2107 | 2166 |
2108 UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp) | 2167 UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp) |
2109 { | 2168 { |
2110 const CLzmaEnc *p = (CLzmaEnc *)pp; | 2169 const CLzmaEnc *p = (CLzmaEnc *)pp; |
2111 return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj); | 2170 return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj); |
2112 } | 2171 } |
2113 | 2172 |
| 2173 |
2114 const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp) | 2174 const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp) |
2115 { | 2175 { |
2116 const CLzmaEnc *p = (CLzmaEnc *)pp; | 2176 const CLzmaEnc *p = (CLzmaEnc *)pp; |
2117 return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additiona
lOffset; | 2177 return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additiona
lOffset; |
2118 } | 2178 } |
2119 | 2179 |
| 2180 |
2120 SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, Bool reInit, | 2181 SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, Bool reInit, |
2121 Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize) | 2182 Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize) |
2122 { | 2183 { |
2123 CLzmaEnc *p = (CLzmaEnc *)pp; | 2184 CLzmaEnc *p = (CLzmaEnc *)pp; |
2124 UInt64 nowPos64; | 2185 UInt64 nowPos64; |
2125 SRes res; | 2186 SRes res; |
2126 CSeqOutStreamBuf outStream; | 2187 CSeqOutStreamBuf outStream; |
2127 | 2188 |
2128 outStream.funcTable.Write = MyWrite; | 2189 outStream.funcTable.Write = MyWrite; |
2129 outStream.data = dest; | 2190 outStream.data = dest; |
(...skipping 14 matching lines...) Expand all Loading... |
2144 res = LzmaEnc_CodeOneBlock(p, True, desiredPackSize, *unpackSize); | 2205 res = LzmaEnc_CodeOneBlock(p, True, desiredPackSize, *unpackSize); |
2145 | 2206 |
2146 *unpackSize = (UInt32)(p->nowPos64 - nowPos64); | 2207 *unpackSize = (UInt32)(p->nowPos64 - nowPos64); |
2147 *destLen -= outStream.rem; | 2208 *destLen -= outStream.rem; |
2148 if (outStream.overflow) | 2209 if (outStream.overflow) |
2149 return SZ_ERROR_OUTPUT_EOF; | 2210 return SZ_ERROR_OUTPUT_EOF; |
2150 | 2211 |
2151 return res; | 2212 return res; |
2152 } | 2213 } |
2153 | 2214 |
| 2215 |
2154 static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress) | 2216 static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress) |
2155 { | 2217 { |
2156 SRes res = SZ_OK; | 2218 SRes res = SZ_OK; |
2157 | 2219 |
2158 #ifndef _7ZIP_ST | 2220 #ifndef _7ZIP_ST |
2159 Byte allocaDummy[0x300]; | 2221 Byte allocaDummy[0x300]; |
2160 int i = 0; | 2222 allocaDummy[0] = 0; |
2161 for (i = 0; i < 16; i++) | 2223 allocaDummy[1] = allocaDummy[0]; |
2162 allocaDummy[i] = (Byte)i; | |
2163 #endif | 2224 #endif |
2164 | 2225 |
2165 for (;;) | 2226 for (;;) |
2166 { | 2227 { |
2167 res = LzmaEnc_CodeOneBlock(p, False, 0, 0); | 2228 res = LzmaEnc_CodeOneBlock(p, False, 0, 0); |
2168 if (res != SZ_OK || p->finished != 0) | 2229 if (res != SZ_OK || p->finished) |
2169 break; | 2230 break; |
2170 if (progress != 0) | 2231 if (progress) |
2171 { | 2232 { |
2172 res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->
rc)); | 2233 res = progress->Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->
rc)); |
2173 if (res != SZ_OK) | 2234 if (res != SZ_OK) |
2174 { | 2235 { |
2175 res = SZ_ERROR_PROGRESS; | 2236 res = SZ_ERROR_PROGRESS; |
2176 break; | 2237 break; |
2177 } | 2238 } |
2178 } | 2239 } |
2179 } | 2240 } |
| 2241 |
2180 LzmaEnc_Finish(p); | 2242 LzmaEnc_Finish(p); |
| 2243 |
| 2244 /* |
| 2245 if (res == S_OK && !Inline_MatchFinder_IsFinishedOK(&p->matchFinderBase)) |
| 2246 res = SZ_ERROR_FAIL; |
| 2247 } |
| 2248 */ |
| 2249 |
2181 return res; | 2250 return res; |
2182 } | 2251 } |
2183 | 2252 |
| 2253 |
2184 SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *i
nStream, ICompressProgress *progress, | 2254 SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *i
nStream, ICompressProgress *progress, |
2185 ISzAlloc *alloc, ISzAlloc *allocBig) | 2255 ISzAlloc *alloc, ISzAlloc *allocBig) |
2186 { | 2256 { |
2187 RINOK(LzmaEnc_Prepare(pp, outStream, inStream, alloc, allocBig)); | 2257 RINOK(LzmaEnc_Prepare(pp, outStream, inStream, alloc, allocBig)); |
2188 return LzmaEnc_Encode2((CLzmaEnc *)pp, progress); | 2258 return LzmaEnc_Encode2((CLzmaEnc *)pp, progress); |
2189 } | 2259 } |
2190 | 2260 |
| 2261 |
2191 SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size) | 2262 SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size) |
2192 { | 2263 { |
2193 CLzmaEnc *p = (CLzmaEnc *)pp; | 2264 CLzmaEnc *p = (CLzmaEnc *)pp; |
2194 int i; | 2265 unsigned i; |
2195 UInt32 dictSize = p->dictSize; | 2266 UInt32 dictSize = p->dictSize; |
2196 if (*size < LZMA_PROPS_SIZE) | 2267 if (*size < LZMA_PROPS_SIZE) |
2197 return SZ_ERROR_PARAM; | 2268 return SZ_ERROR_PARAM; |
2198 *size = LZMA_PROPS_SIZE; | 2269 *size = LZMA_PROPS_SIZE; |
2199 props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc); | 2270 props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc); |
2200 | 2271 |
2201 for (i = 11; i <= 30; i++) | 2272 if (dictSize >= ((UInt32)1 << 22)) |
2202 { | 2273 { |
2203 if (dictSize <= ((UInt32)2 << i)) | 2274 UInt32 kDictMask = ((UInt32)1 << 20) - 1; |
2204 { | 2275 if (dictSize < (UInt32)0xFFFFFFFF - kDictMask) |
2205 dictSize = (2 << i); | 2276 dictSize = (dictSize + kDictMask) & ~kDictMask; |
2206 break; | 2277 } |
2207 } | 2278 else for (i = 11; i <= 30; i++) |
2208 if (dictSize <= ((UInt32)3 << i)) | 2279 { |
2209 { | 2280 if (dictSize <= ((UInt32)2 << i)) { dictSize = (2 << i); break; } |
2210 dictSize = (3 << i); | 2281 if (dictSize <= ((UInt32)3 << i)) { dictSize = (3 << i); break; } |
2211 break; | |
2212 } | |
2213 } | 2282 } |
2214 | 2283 |
2215 for (i = 0; i < 4; i++) | 2284 for (i = 0; i < 4; i++) |
2216 props[1 + i] = (Byte)(dictSize >> (8 * i)); | 2285 props[1 + i] = (Byte)(dictSize >> (8 * i)); |
2217 return SZ_OK; | 2286 return SZ_OK; |
2218 } | 2287 } |
2219 | 2288 |
| 2289 |
2220 SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte
*src, SizeT srcLen, | 2290 SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte
*src, SizeT srcLen, |
2221 int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *al
locBig) | 2291 int writeEndMark, ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *al
locBig) |
2222 { | 2292 { |
2223 SRes res; | 2293 SRes res; |
2224 CLzmaEnc *p = (CLzmaEnc *)pp; | 2294 CLzmaEnc *p = (CLzmaEnc *)pp; |
2225 | 2295 |
2226 CSeqOutStreamBuf outStream; | 2296 CSeqOutStreamBuf outStream; |
2227 | 2297 |
2228 LzmaEnc_SetInputBuf(p, src, srcLen); | |
2229 | |
2230 outStream.funcTable.Write = MyWrite; | 2298 outStream.funcTable.Write = MyWrite; |
2231 outStream.data = dest; | 2299 outStream.data = dest; |
2232 outStream.rem = *destLen; | 2300 outStream.rem = *destLen; |
2233 outStream.overflow = False; | 2301 outStream.overflow = False; |
2234 | 2302 |
2235 p->writeEndMark = writeEndMark; | 2303 p->writeEndMark = writeEndMark; |
| 2304 p->rc.outStream = &outStream.funcTable; |
2236 | 2305 |
2237 p->rc.outStream = &outStream.funcTable; | |
2238 res = LzmaEnc_MemPrepare(pp, src, srcLen, 0, alloc, allocBig); | 2306 res = LzmaEnc_MemPrepare(pp, src, srcLen, 0, alloc, allocBig); |
| 2307 |
2239 if (res == SZ_OK) | 2308 if (res == SZ_OK) |
| 2309 { |
2240 res = LzmaEnc_Encode2(p, progress); | 2310 res = LzmaEnc_Encode2(p, progress); |
| 2311 if (res == SZ_OK && p->nowPos64 != srcLen) |
| 2312 res = SZ_ERROR_FAIL; |
| 2313 } |
2241 | 2314 |
2242 *destLen -= outStream.rem; | 2315 *destLen -= outStream.rem; |
2243 if (outStream.overflow) | 2316 if (outStream.overflow) |
2244 return SZ_ERROR_OUTPUT_EOF; | 2317 return SZ_ERROR_OUTPUT_EOF; |
2245 return res; | 2318 return res; |
2246 } | 2319 } |
2247 | 2320 |
| 2321 |
2248 SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, | 2322 SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, |
2249 const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeE
ndMark, | 2323 const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeE
ndMark, |
2250 ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig) | 2324 ICompressProgress *progress, ISzAlloc *alloc, ISzAlloc *allocBig) |
2251 { | 2325 { |
2252 CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc); | 2326 CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc); |
2253 SRes res; | 2327 SRes res; |
2254 if (p == 0) | 2328 if (!p) |
2255 return SZ_ERROR_MEM; | 2329 return SZ_ERROR_MEM; |
2256 | 2330 |
2257 res = LzmaEnc_SetProps(p, props); | 2331 res = LzmaEnc_SetProps(p, props); |
2258 if (res == SZ_OK) | 2332 if (res == SZ_OK) |
2259 { | 2333 { |
2260 res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize); | 2334 res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize); |
2261 if (res == SZ_OK) | 2335 if (res == SZ_OK) |
2262 res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen, | 2336 res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen, |
2263 writeEndMark, progress, alloc, allocBig); | 2337 writeEndMark, progress, alloc, allocBig); |
2264 } | 2338 } |
2265 | 2339 |
2266 LzmaEnc_Destroy(p, alloc, allocBig); | 2340 LzmaEnc_Destroy(p, alloc, allocBig); |
2267 return res; | 2341 return res; |
2268 } | 2342 } |
OLD | NEW |