/src/lzma-fuzz/sdk/C/LzmaEnc.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* LzmaEnc.c -- LZMA Encoder |
2 | | 2019-01-10: Igor Pavlov : Public domain */ |
3 | | |
4 | | #include "Precomp.h" |
5 | | |
6 | | #include <string.h> |
7 | | |
8 | | /* #define SHOW_STAT */ |
9 | | /* #define SHOW_STAT2 */ |
10 | | |
11 | | #if defined(SHOW_STAT) || defined(SHOW_STAT2) |
12 | | #include <stdio.h> |
13 | | #endif |
14 | | |
15 | | #include "LzmaEnc.h" |
16 | | |
17 | | #include "LzFind.h" |
18 | | #ifndef _7ZIP_ST |
19 | | #include "LzFindMt.h" |
20 | | #endif |
21 | | |
22 | | #ifdef SHOW_STAT |
23 | | static unsigned g_STAT_OFFSET = 0; |
24 | | #endif |
25 | | |
26 | 15.5k | #define kLzmaMaxHistorySize ((UInt32)3 << 29) |
27 | | /* #define kLzmaMaxHistorySize ((UInt32)7 << 29) */ |
28 | | |
29 | 2.44G | #define kNumTopBits 24 |
30 | 2.44G | #define kTopValue ((UInt32)1 << kNumTopBits) |
31 | | |
32 | 6.44G | #define kNumBitModelTotalBits 11 |
33 | 4.03G | #define kBitModelTotal (1 << kNumBitModelTotalBits) |
34 | 6.55G | #define kNumMoveBits 5 |
35 | 150M | #define kProbInitValue (kBitModelTotal >> 1) |
36 | | |
37 | 1.90G | #define kNumMoveReducingBits 4 |
38 | 9.30M | #define kNumBitPriceShiftBits 4 |
39 | | #define kBitPrice (1 << kNumBitPriceShiftBits) |
40 | | |
41 | 112k | #define REP_LEN_COUNT 64 |
42 | | |
43 | | void LzmaEncProps_Init(CLzmaEncProps *p) |
44 | 31.1k | { |
45 | 31.1k | p->level = 5; |
46 | 31.1k | p->dictSize = p->mc = 0; |
47 | 31.1k | p->reduceSize = (UInt64)(Int64)-1; |
48 | 31.1k | p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1; |
49 | 31.1k | p->writeEndMark = 0; |
50 | 31.1k | } |
51 | | |
52 | | void LzmaEncProps_Normalize(CLzmaEncProps *p) |
53 | 93.3k | { |
54 | 93.3k | int level = p->level; |
55 | 93.3k | if (level < 0) level = 5; |
56 | 93.3k | p->level = level; |
57 | | |
58 | 93.3k | if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level <= 7 ? (1 << 25) : (1 << 26))); |
59 | 93.3k | if (p->dictSize > p->reduceSize) |
60 | 0 | { |
61 | 0 | unsigned i; |
62 | 0 | UInt32 reduceSize = (UInt32)p->reduceSize; |
63 | 0 | for (i = 11; i <= 30; i++) |
64 | 0 | { |
65 | 0 | if (reduceSize <= ((UInt32)2 << i)) { p->dictSize = ((UInt32)2 << i); break; } |
66 | 0 | if (reduceSize <= ((UInt32)3 << i)) { p->dictSize = ((UInt32)3 << i); break; } |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | 93.3k | if (p->lc < 0) p->lc = 3; |
71 | 93.3k | if (p->lp < 0) p->lp = 0; |
72 | 93.3k | if (p->pb < 0) p->pb = 2; |
73 | | |
74 | 93.3k | if (p->algo < 0) p->algo = (level < 5 ? 0 : 1); |
75 | 93.3k | if (p->fb < 0) p->fb = (level < 7 ? 32 : 64); |
76 | 93.3k | if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1); |
77 | 93.3k | if (p->numHashBytes < 0) p->numHashBytes = 4; |
78 | 93.3k | if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1); |
79 | | |
80 | 93.3k | if (p->numThreads < 0) |
81 | 77.7k | p->numThreads = |
82 | | #ifndef _7ZIP_ST |
83 | | ((p->btMode && p->algo) ? 2 : 1); |
84 | | #else |
85 | 77.7k | 1; |
86 | 93.3k | #endif |
87 | 93.3k | } |
88 | | |
89 | | UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps *props2) |
90 | 7.77k | { |
91 | 7.77k | CLzmaEncProps props = *props2; |
92 | 7.77k | LzmaEncProps_Normalize(&props); |
93 | 7.77k | return props.dictSize; |
94 | 7.77k | } |
95 | | |
96 | | #if (_MSC_VER >= 1400) |
97 | | /* BSR code is fast for some new CPUs */ |
98 | | /* #define LZMA_LOG_BSR */ |
99 | | #endif |
100 | | |
101 | | #ifdef LZMA_LOG_BSR |
102 | | |
103 | | #define kDicLogSizeMaxCompress 32 |
104 | | |
105 | | #define BSR2_RET(pos, res) { unsigned long zz; _BitScanReverse(&zz, (pos)); res = (zz + zz) + ((pos >> (zz - 1)) & 1); } |
106 | | |
107 | | static unsigned GetPosSlot1(UInt32 pos) |
108 | | { |
109 | | unsigned res; |
110 | | BSR2_RET(pos, res); |
111 | | return res; |
112 | | } |
113 | | #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); } |
114 | | #define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); } |
115 | | |
116 | | #else |
117 | | |
118 | 105M | #define kNumLogBits (9 + sizeof(size_t) / 2) |
119 | | /* #define kNumLogBits (11 + sizeof(size_t) / 8 * 3) */ |
120 | | |
121 | 15.5k | #define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7) |
122 | | |
123 | | static void LzmaEnc_FastPosInit(Byte *g_FastPos) |
124 | 7.77k | { |
125 | 7.77k | unsigned slot; |
126 | 7.77k | g_FastPos[0] = 0; |
127 | 7.77k | g_FastPos[1] = 1; |
128 | 7.77k | g_FastPos += 2; |
129 | | |
130 | 194k | for (slot = 2; slot < kNumLogBits * 2; slot++) |
131 | 186k | { |
132 | 186k | size_t k = ((size_t)1 << ((slot >> 1) - 1)); |
133 | 186k | size_t j; |
134 | 63.8M | for (j = 0; j < k; j++) |
135 | 63.6M | g_FastPos[j] = (Byte)slot; |
136 | 186k | g_FastPos += k; |
137 | 186k | } |
138 | 7.77k | } |
139 | | |
140 | | /* we can use ((limit - pos) >> 31) only if (pos < ((UInt32)1 << 31)) */ |
141 | | /* |
142 | | #define BSR2_RET(pos, res) { unsigned zz = 6 + ((kNumLogBits - 1) & \ |
143 | | (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \ |
144 | | res = p->g_FastPos[pos >> zz] + (zz * 2); } |
145 | | */ |
146 | | |
147 | | /* |
148 | | #define BSR2_RET(pos, res) { unsigned zz = 6 + ((kNumLogBits - 1) & \ |
149 | | (0 - (((((UInt32)1 << (kNumLogBits)) - 1) - (pos >> 6)) >> 31))); \ |
150 | | res = p->g_FastPos[pos >> zz] + (zz * 2); } |
151 | | */ |
152 | | |
153 | 104M | #define BSR2_RET(pos, res) { unsigned zz = (pos < (1 << (kNumLogBits + 6))) ? 6 : 6 + kNumLogBits - 1; \ |
154 | 104M | res = p->g_FastPos[pos >> zz] + (zz * 2); } |
155 | | |
156 | | /* |
157 | | #define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \ |
158 | | p->g_FastPos[pos >> 6] + 12 : \ |
159 | | p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; } |
160 | | */ |
161 | | |
162 | 35.7M | #define GetPosSlot1(pos) p->g_FastPos[pos] |
163 | 99.7M | #define GetPosSlot2(pos, res) { BSR2_RET(pos, res); } |
164 | 6.42M | #define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos & (kNumFullDistances - 1)]; else BSR2_RET(pos, res); } |
165 | | |
166 | | #endif |
167 | | |
168 | | |
169 | 1.82G | #define LZMA_NUM_REPS 4 |
170 | | |
171 | | typedef UInt16 CState; |
172 | | typedef UInt16 CExtra; |
173 | | |
174 | | typedef struct |
175 | | { |
176 | | UInt32 price; |
177 | | CState state; |
178 | | CExtra extra; |
179 | | // 0 : normal |
180 | | // 1 : LIT : MATCH |
181 | | // > 1 : MATCH (extra-1) : LIT : REP0 (len) |
182 | | UInt32 len; |
183 | | UInt32 dist; |
184 | | UInt32 reps[LZMA_NUM_REPS]; |
185 | | } COptimal; |
186 | | |
187 | | |
188 | | // 18.06 |
189 | 663M | #define kNumOpts (1 << 11) |
190 | 223M | #define kPackReserve (kNumOpts * 8) |
191 | | // #define kNumOpts (1 << 12) |
192 | | // #define kPackReserve (1 + kNumOpts * 2) |
193 | | |
194 | 279M | #define kNumLenToPosStates 4 |
195 | 149M | #define kNumPosSlotBits 6 |
196 | | #define kDicLogSizeMin 0 |
197 | 139k | #define kDicLogSizeMax 32 |
198 | | #define kDistTableSizeMax (kDicLogSizeMax * 2) |
199 | | |
200 | 158M | #define kNumAlignBits 4 |
201 | 158M | #define kAlignTableSize (1 << kNumAlignBits) |
202 | 157M | #define kAlignMask (kAlignTableSize - 1) |
203 | | |
204 | 6.54M | #define kStartPosModelIndex 4 |
205 | 282M | #define kEndPosModelIndex 14 |
206 | 288M | #define kNumFullDistances (1 << (kEndPosModelIndex >> 1)) |
207 | | |
208 | | typedef |
209 | | #ifdef _LZMA_PROB32 |
210 | | UInt32 |
211 | | #else |
212 | | UInt16 |
213 | | #endif |
214 | | CLzmaProb; |
215 | | |
216 | 13.5M | #define LZMA_PB_MAX 4 |
217 | 31.1k | #define LZMA_LC_MAX 8 |
218 | 31.1k | #define LZMA_LP_MAX 4 |
219 | | |
220 | 13.4M | #define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX) |
221 | | |
222 | 501M | #define kLenNumLowBits 3 |
223 | 475M | #define kLenNumLowSymbols (1 << kLenNumLowBits) |
224 | 463M | #define kLenNumHighBits 8 |
225 | 459M | #define kLenNumHighSymbols (1 << kLenNumHighBits) |
226 | 450M | #define kLenNumSymbolsTotal (kLenNumLowSymbols * 2 + kLenNumHighSymbols) |
227 | | |
228 | 1.05G | #define LZMA_MATCH_LEN_MIN 2 |
229 | 450M | #define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1) |
230 | | |
231 | 244k | #define kNumStates 12 |
232 | | |
233 | | |
234 | | typedef struct |
235 | | { |
236 | | CLzmaProb low[LZMA_NUM_PB_STATES_MAX << (kLenNumLowBits + 1)]; |
237 | | CLzmaProb high[kLenNumHighSymbols]; |
238 | | } CLenEnc; |
239 | | |
240 | | |
241 | | typedef struct |
242 | | { |
243 | | unsigned tableSize; |
244 | | UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal]; |
245 | | // UInt32 prices1[LZMA_NUM_PB_STATES_MAX][kLenNumLowSymbols * 2]; |
246 | | // UInt32 prices2[kLenNumSymbolsTotal]; |
247 | | } CLenPriceEnc; |
248 | | |
249 | | #define GET_PRICE_LEN(p, posState, len) \ |
250 | 593M | ((p)->prices[posState][(size_t)(len) - LZMA_MATCH_LEN_MIN]) |
251 | | |
252 | | /* |
253 | | #define GET_PRICE_LEN(p, posState, len) \ |
254 | | ((p)->prices2[(size_t)(len) - 2] + ((p)->prices1[posState][((len) - 2) & (kLenNumLowSymbols * 2 - 1)] & (((len) - 2 - kLenNumLowSymbols * 2) >> 9))) |
255 | | */ |
256 | | |
257 | | typedef struct |
258 | | { |
259 | | UInt32 range; |
260 | | unsigned cache; |
261 | | UInt64 low; |
262 | | UInt64 cacheSize; |
263 | | Byte *buf; |
264 | | Byte *bufLim; |
265 | | Byte *bufBase; |
266 | | ISeqOutStream *outStream; |
267 | | UInt64 processed; |
268 | | SRes res; |
269 | | } CRangeEnc; |
270 | | |
271 | | |
272 | | typedef struct |
273 | | { |
274 | | CLzmaProb *litProbs; |
275 | | |
276 | | unsigned state; |
277 | | UInt32 reps[LZMA_NUM_REPS]; |
278 | | |
279 | | CLzmaProb posAlignEncoder[1 << kNumAlignBits]; |
280 | | CLzmaProb isRep[kNumStates]; |
281 | | CLzmaProb isRepG0[kNumStates]; |
282 | | CLzmaProb isRepG1[kNumStates]; |
283 | | CLzmaProb isRepG2[kNumStates]; |
284 | | CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX]; |
285 | | CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX]; |
286 | | |
287 | | CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits]; |
288 | | CLzmaProb posEncoders[kNumFullDistances]; |
289 | | |
290 | | CLenEnc lenProbs; |
291 | | CLenEnc repLenProbs; |
292 | | |
293 | | } CSaveState; |
294 | | |
295 | | |
296 | | typedef UInt32 CProbPrice; |
297 | | |
298 | | |
299 | | typedef struct |
300 | | { |
301 | | void *matchFinderObj; |
302 | | IMatchFinder matchFinder; |
303 | | |
304 | | unsigned optCur; |
305 | | unsigned optEnd; |
306 | | |
307 | | unsigned longestMatchLen; |
308 | | unsigned numPairs; |
309 | | UInt32 numAvail; |
310 | | |
311 | | unsigned state; |
312 | | unsigned numFastBytes; |
313 | | unsigned additionalOffset; |
314 | | UInt32 reps[LZMA_NUM_REPS]; |
315 | | unsigned lpMask, pbMask; |
316 | | CLzmaProb *litProbs; |
317 | | CRangeEnc rc; |
318 | | |
319 | | UInt32 backRes; |
320 | | |
321 | | unsigned lc, lp, pb; |
322 | | unsigned lclp; |
323 | | |
324 | | BoolInt fastMode; |
325 | | BoolInt writeEndMark; |
326 | | BoolInt finished; |
327 | | BoolInt multiThread; |
328 | | BoolInt needInit; |
329 | | // BoolInt _maxMode; |
330 | | |
331 | | UInt64 nowPos64; |
332 | | |
333 | | unsigned matchPriceCount; |
334 | | // unsigned alignPriceCount; |
335 | | int repLenEncCounter; |
336 | | |
337 | | unsigned distTableSize; |
338 | | |
339 | | UInt32 dictSize; |
340 | | SRes result; |
341 | | |
342 | | #ifndef _7ZIP_ST |
343 | | BoolInt mtMode; |
344 | | // begin of CMatchFinderMt is used in LZ thread |
345 | | CMatchFinderMt matchFinderMt; |
346 | | // end of CMatchFinderMt is used in BT and HASH threads |
347 | | #endif |
348 | | |
349 | | CMatchFinder matchFinderBase; |
350 | | |
351 | | #ifndef _7ZIP_ST |
352 | | Byte pad[128]; |
353 | | #endif |
354 | | |
355 | | // LZ thread |
356 | | CProbPrice ProbPrices[kBitModelTotal >> kNumMoveReducingBits]; |
357 | | |
358 | | UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1]; |
359 | | |
360 | | UInt32 alignPrices[kAlignTableSize]; |
361 | | UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax]; |
362 | | UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances]; |
363 | | |
364 | | CLzmaProb posAlignEncoder[1 << kNumAlignBits]; |
365 | | CLzmaProb isRep[kNumStates]; |
366 | | CLzmaProb isRepG0[kNumStates]; |
367 | | CLzmaProb isRepG1[kNumStates]; |
368 | | CLzmaProb isRepG2[kNumStates]; |
369 | | CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX]; |
370 | | CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX]; |
371 | | CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits]; |
372 | | CLzmaProb posEncoders[kNumFullDistances]; |
373 | | |
374 | | CLenEnc lenProbs; |
375 | | CLenEnc repLenProbs; |
376 | | |
377 | | #ifndef LZMA_LOG_BSR |
378 | | Byte g_FastPos[1 << kNumLogBits]; |
379 | | #endif |
380 | | |
381 | | CLenPriceEnc lenEnc; |
382 | | CLenPriceEnc repLenEnc; |
383 | | |
384 | | COptimal opt[kNumOpts]; |
385 | | |
386 | | CSaveState saveState; |
387 | | |
388 | | #ifndef _7ZIP_ST |
389 | | Byte pad2[128]; |
390 | | #endif |
391 | | } CLzmaEnc; |
392 | | |
393 | | |
394 | | |
395 | 247k | #define COPY_ARR(dest, src, arr) memcpy(dest->arr, src->arr, sizeof(src->arr)); |
396 | | |
397 | | void LzmaEnc_SaveState(CLzmaEncHandle pp) |
398 | 20.2k | { |
399 | 20.2k | CLzmaEnc *p = (CLzmaEnc *)pp; |
400 | 20.2k | CSaveState *dest = &p->saveState; |
401 | | |
402 | 20.2k | dest->state = p->state; |
403 | | |
404 | 20.2k | dest->lenProbs = p->lenProbs; |
405 | 20.2k | dest->repLenProbs = p->repLenProbs; |
406 | | |
407 | 20.2k | COPY_ARR(dest, p, reps); |
408 | | |
409 | 20.2k | COPY_ARR(dest, p, posAlignEncoder); |
410 | 20.2k | COPY_ARR(dest, p, isRep); |
411 | 20.2k | COPY_ARR(dest, p, isRepG0); |
412 | 20.2k | COPY_ARR(dest, p, isRepG1); |
413 | 20.2k | COPY_ARR(dest, p, isRepG2); |
414 | 20.2k | COPY_ARR(dest, p, isMatch); |
415 | 20.2k | COPY_ARR(dest, p, isRep0Long); |
416 | 20.2k | COPY_ARR(dest, p, posSlotEncoder); |
417 | 20.2k | COPY_ARR(dest, p, posEncoders); |
418 | | |
419 | 20.2k | memcpy(dest->litProbs, p->litProbs, ((UInt32)0x300 << p->lclp) * sizeof(CLzmaProb)); |
420 | 20.2k | } |
421 | | |
422 | | |
423 | | void LzmaEnc_RestoreState(CLzmaEncHandle pp) |
424 | 4.42k | { |
425 | 4.42k | CLzmaEnc *dest = (CLzmaEnc *)pp; |
426 | 4.42k | const CSaveState *p = &dest->saveState; |
427 | | |
428 | 4.42k | dest->state = p->state; |
429 | | |
430 | 4.42k | dest->lenProbs = p->lenProbs; |
431 | 4.42k | dest->repLenProbs = p->repLenProbs; |
432 | | |
433 | 4.42k | COPY_ARR(dest, p, reps); |
434 | | |
435 | 4.42k | COPY_ARR(dest, p, posAlignEncoder); |
436 | 4.42k | COPY_ARR(dest, p, isRep); |
437 | 4.42k | COPY_ARR(dest, p, isRepG0); |
438 | 4.42k | COPY_ARR(dest, p, isRepG1); |
439 | 4.42k | COPY_ARR(dest, p, isRepG2); |
440 | 4.42k | COPY_ARR(dest, p, isMatch); |
441 | 4.42k | COPY_ARR(dest, p, isRep0Long); |
442 | 4.42k | COPY_ARR(dest, p, posSlotEncoder); |
443 | 4.42k | COPY_ARR(dest, p, posEncoders); |
444 | | |
445 | 4.42k | memcpy(dest->litProbs, p->litProbs, ((UInt32)0x300 << dest->lclp) * sizeof(CLzmaProb)); |
446 | 4.42k | } |
447 | | |
448 | | |
449 | | |
450 | | SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps *props2) |
451 | 15.5k | { |
452 | 15.5k | CLzmaEnc *p = (CLzmaEnc *)pp; |
453 | 15.5k | CLzmaEncProps props = *props2; |
454 | 15.5k | LzmaEncProps_Normalize(&props); |
455 | | |
456 | 15.5k | if (props.lc > LZMA_LC_MAX |
457 | 15.5k | || props.lp > LZMA_LP_MAX |
458 | 15.5k | || props.pb > LZMA_PB_MAX |
459 | 15.5k | || props.dictSize > ((UInt64)1 << kDicLogSizeMaxCompress) |
460 | 15.5k | || props.dictSize > kLzmaMaxHistorySize) |
461 | 0 | return SZ_ERROR_PARAM; |
462 | | |
463 | 15.5k | p->dictSize = props.dictSize; |
464 | 15.5k | { |
465 | 15.5k | unsigned fb = props.fb; |
466 | 15.5k | if (fb < 5) |
467 | 0 | fb = 5; |
468 | 15.5k | if (fb > LZMA_MATCH_LEN_MAX) |
469 | 0 | fb = LZMA_MATCH_LEN_MAX; |
470 | 15.5k | p->numFastBytes = fb; |
471 | 15.5k | } |
472 | 15.5k | p->lc = props.lc; |
473 | 15.5k | p->lp = props.lp; |
474 | 15.5k | p->pb = props.pb; |
475 | 15.5k | p->fastMode = (props.algo == 0); |
476 | | // p->_maxMode = True; |
477 | 15.5k | p->matchFinderBase.btMode = (Byte)(props.btMode ? 1 : 0); |
478 | 15.5k | { |
479 | 15.5k | unsigned numHashBytes = 4; |
480 | 15.5k | if (props.btMode) |
481 | 15.5k | { |
482 | 15.5k | if (props.numHashBytes < 2) |
483 | 0 | numHashBytes = 2; |
484 | 15.5k | else if (props.numHashBytes < 4) |
485 | 0 | numHashBytes = props.numHashBytes; |
486 | 15.5k | } |
487 | 15.5k | p->matchFinderBase.numHashBytes = numHashBytes; |
488 | 15.5k | } |
489 | | |
490 | 15.5k | p->matchFinderBase.cutValue = props.mc; |
491 | | |
492 | 15.5k | p->writeEndMark = props.writeEndMark; |
493 | | |
494 | | #ifndef _7ZIP_ST |
495 | | /* |
496 | | if (newMultiThread != _multiThread) |
497 | | { |
498 | | ReleaseMatchFinder(); |
499 | | _multiThread = newMultiThread; |
500 | | } |
501 | | */ |
502 | | p->multiThread = (props.numThreads > 1); |
503 | | #endif |
504 | | |
505 | 15.5k | return SZ_OK; |
506 | 15.5k | } |
507 | | |
508 | | |
509 | | void LzmaEnc_SetDataSize(CLzmaEncHandle pp, UInt64 expectedDataSiize) |
510 | 7.77k | { |
511 | 7.77k | CLzmaEnc *p = (CLzmaEnc *)pp; |
512 | 7.77k | p->matchFinderBase.expectedDataSize = expectedDataSiize; |
513 | 7.77k | } |
514 | | |
515 | | |
516 | 7.77k | #define kState_Start 0 |
517 | 17.5M | #define kState_LitAfterMatch 4 |
518 | 38.6M | #define kState_LitAfterRep 5 |
519 | 0 | #define kState_MatchAfterLit 7 |
520 | 5.11M | #define kState_RepAfterLit 8 |
521 | | |
522 | | static const Byte kLiteralNextStates[kNumStates] = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5}; |
523 | | static const Byte kMatchNextStates[kNumStates] = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10}; |
524 | | static const Byte kRepNextStates[kNumStates] = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11}; |
525 | | static const Byte kShortRepNextStates[kNumStates]= {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11}; |
526 | | |
527 | 390M | #define IsLitState(s) ((s) < 7) |
528 | 170M | #define GetLenToPosState2(len) (((len) < kNumLenToPosStates - 1) ? (len) : kNumLenToPosStates - 1) |
529 | 28.5M | #define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1) |
530 | | |
531 | 320M | #define kInfinityPrice (1 << 30) |
532 | | |
533 | | static void RangeEnc_Construct(CRangeEnc *p) |
534 | 7.77k | { |
535 | 7.77k | p->outStream = NULL; |
536 | 7.77k | p->bufBase = NULL; |
537 | 7.77k | } |
538 | | |
539 | | #define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize) |
540 | 223M | #define RangeEnc_GetProcessed_sizet(p) ((size_t)(p)->processed + ((p)->buf - (p)->bufBase) + (size_t)(p)->cacheSize) |
541 | | |
542 | 7.77k | #define RC_BUF_SIZE (1 << 16) |
543 | | |
544 | | static int RangeEnc_Alloc(CRangeEnc *p, ISzAllocPtr alloc) |
545 | 7.77k | { |
546 | 7.77k | if (!p->bufBase) |
547 | 7.77k | { |
548 | 7.77k | p->bufBase = (Byte *)ISzAlloc_Alloc(alloc, RC_BUF_SIZE); |
549 | 7.77k | if (!p->bufBase) |
550 | 0 | return 0; |
551 | 7.77k | p->bufLim = p->bufBase + RC_BUF_SIZE; |
552 | 7.77k | } |
553 | 7.77k | return 1; |
554 | 7.77k | } |
555 | | |
556 | | static void RangeEnc_Free(CRangeEnc *p, ISzAllocPtr alloc) |
557 | 7.77k | { |
558 | 7.77k | ISzAlloc_Free(alloc, p->bufBase); |
559 | 7.77k | p->bufBase = 0; |
560 | 7.77k | } |
561 | | |
562 | | static void RangeEnc_Init(CRangeEnc *p) |
563 | 39.0k | { |
564 | | /* Stream.Init(); */ |
565 | 39.0k | p->range = 0xFFFFFFFF; |
566 | 39.0k | p->cache = 0; |
567 | 39.0k | p->low = 0; |
568 | 39.0k | p->cacheSize = 0; |
569 | | |
570 | 39.0k | p->buf = p->bufBase; |
571 | | |
572 | 39.0k | p->processed = 0; |
573 | 39.0k | p->res = SZ_OK; |
574 | 39.0k | } |
575 | | |
576 | | MY_NO_INLINE static void RangeEnc_FlushStream(CRangeEnc *p) |
577 | 20.2k | { |
578 | 20.2k | size_t num; |
579 | 20.2k | if (p->res != SZ_OK) |
580 | 0 | return; |
581 | 20.2k | num = p->buf - p->bufBase; |
582 | 20.2k | if (num != ISeqOutStream_Write(p->outStream, p->bufBase, num)) |
583 | 0 | p->res = SZ_ERROR_WRITE; |
584 | 20.2k | p->processed += num; |
585 | 20.2k | p->buf = p->bufBase; |
586 | 20.2k | } |
587 | | |
588 | | MY_NO_INLINE static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc *p) |
589 | 257M | { |
590 | 257M | UInt32 low = (UInt32)p->low; |
591 | 257M | unsigned high = (unsigned)(p->low >> 32); |
592 | 257M | p->low = (UInt32)(low << 8); |
593 | 257M | if (low < (UInt32)0xFF000000 || high != 0) |
594 | 256M | { |
595 | 256M | { |
596 | 256M | Byte *buf = p->buf; |
597 | 256M | *buf++ = (Byte)(p->cache + high); |
598 | 256M | p->cache = (unsigned)(low >> 24); |
599 | 256M | p->buf = buf; |
600 | 256M | if (buf == p->bufLim) |
601 | 0 | RangeEnc_FlushStream(p); |
602 | 256M | if (p->cacheSize == 0) |
603 | 255M | return; |
604 | 256M | } |
605 | 1.00M | high += 0xFF; |
606 | 1.00M | for (;;) |
607 | 1.00M | { |
608 | 1.00M | Byte *buf = p->buf; |
609 | 1.00M | *buf++ = (Byte)(high); |
610 | 1.00M | p->buf = buf; |
611 | 1.00M | if (buf == p->bufLim) |
612 | 0 | RangeEnc_FlushStream(p); |
613 | 1.00M | if (--p->cacheSize == 0) |
614 | 1.00M | return; |
615 | 1.00M | } |
616 | 1.00M | } |
617 | 1.00M | p->cacheSize++; |
618 | 1.00M | } |
619 | | |
620 | | static void RangeEnc_FlushData(CRangeEnc *p) |
621 | 20.2k | { |
622 | 20.2k | int i; |
623 | 121k | for (i = 0; i < 5; i++) |
624 | 101k | RangeEnc_ShiftLow(p); |
625 | 20.2k | } |
626 | | |
627 | 2.44G | #define RC_NORM(p) if (range < kTopValue) { range <<= 8; RangeEnc_ShiftLow(p); } |
628 | | |
629 | | #define RC_BIT_PRE(p, prob) \ |
630 | 2.40G | ttt = *(prob); \ |
631 | 2.40G | newBound = (range >> kNumBitModelTotalBits) * ttt; |
632 | | |
633 | | // #define _LZMA_ENC_USE_BRANCH |
634 | | |
635 | | #ifdef _LZMA_ENC_USE_BRANCH |
636 | | |
637 | | #define RC_BIT(p, prob, bit) { \ |
638 | | RC_BIT_PRE(p, prob) \ |
639 | | if (bit == 0) { range = newBound; ttt += (kBitModelTotal - ttt) >> kNumMoveBits; } \ |
640 | | else { (p)->low += newBound; range -= newBound; ttt -= ttt >> kNumMoveBits; } \ |
641 | | *(prob) = (CLzmaProb)ttt; \ |
642 | | RC_NORM(p) \ |
643 | | } |
644 | | |
645 | | #else |
646 | | |
647 | 2.07G | #define RC_BIT(p, prob, bit) { \ |
648 | 2.07G | UInt32 mask; \ |
649 | 2.07G | RC_BIT_PRE(p, prob) \ |
650 | 2.07G | mask = 0 - (UInt32)bit; \ |
651 | 2.07G | range &= mask; \ |
652 | 2.07G | mask &= newBound; \ |
653 | 2.07G | range -= mask; \ |
654 | 2.07G | (p)->low += mask; \ |
655 | 2.07G | mask = (UInt32)bit - 1; \ |
656 | 2.07G | range += newBound & mask; \ |
657 | 2.07G | mask &= (kBitModelTotal - ((1 << kNumMoveBits) - 1)); \ |
658 | 2.07G | mask += ((1 << kNumMoveBits) - 1); \ |
659 | 2.07G | ttt += (Int32)(mask - ttt) >> kNumMoveBits; \ |
660 | 2.07G | *(prob) = (CLzmaProb)ttt; \ |
661 | 2.07G | RC_NORM(p) \ |
662 | 2.07G | } |
663 | | |
664 | | #endif |
665 | | |
666 | | |
667 | | |
668 | | |
669 | | #define RC_BIT_0_BASE(p, prob) \ |
670 | 284M | range = newBound; *(prob) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); |
671 | | |
672 | | #define RC_BIT_1_BASE(p, prob) \ |
673 | 48.2M | range -= newBound; (p)->low += newBound; *(prob) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits)); \ |
674 | | |
675 | | #define RC_BIT_0(p, prob) \ |
676 | 276M | RC_BIT_0_BASE(p, prob) \ |
677 | 276M | RC_NORM(p) |
678 | | |
679 | | #define RC_BIT_1(p, prob) \ |
680 | 41.7M | RC_BIT_1_BASE(p, prob) \ |
681 | 41.7M | RC_NORM(p) |
682 | | |
683 | | static void RangeEnc_EncodeBit_0(CRangeEnc *p, CLzmaProb *prob) |
684 | 7.77k | { |
685 | 7.77k | UInt32 range, ttt, newBound; |
686 | 7.77k | range = p->range; |
687 | 7.77k | RC_BIT_PRE(p, prob) |
688 | 7.77k | RC_BIT_0(p, prob) |
689 | 7.77k | p->range = range; |
690 | 7.77k | } |
691 | | |
692 | | static void LitEnc_Encode(CRangeEnc *p, CLzmaProb *probs, UInt32 sym) |
693 | 231M | { |
694 | 231M | UInt32 range = p->range; |
695 | 231M | sym |= 0x100; |
696 | 231M | do |
697 | 1.84G | { |
698 | 1.84G | UInt32 ttt, newBound; |
699 | | // RangeEnc_EncodeBit(p, probs + (sym >> 8), (sym >> 7) & 1); |
700 | 1.84G | CLzmaProb *prob = probs + (sym >> 8); |
701 | 1.84G | UInt32 bit = (sym >> 7) & 1; |
702 | 1.84G | sym <<= 1; |
703 | 1.84G | RC_BIT(p, prob, bit); |
704 | 1.84G | } |
705 | 1.84G | while (sym < 0x10000); |
706 | 231M | p->range = range; |
707 | 231M | } |
708 | | |
709 | | static void LitEnc_EncodeMatched(CRangeEnc *p, CLzmaProb *probs, UInt32 sym, UInt32 matchByte) |
710 | 14.9M | { |
711 | 14.9M | UInt32 range = p->range; |
712 | 14.9M | UInt32 offs = 0x100; |
713 | 14.9M | sym |= 0x100; |
714 | 14.9M | do |
715 | 119M | { |
716 | 119M | UInt32 ttt, newBound; |
717 | 119M | CLzmaProb *prob; |
718 | 119M | UInt32 bit; |
719 | 119M | matchByte <<= 1; |
720 | | // RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (sym >> 8)), (sym >> 7) & 1); |
721 | 119M | prob = probs + (offs + (matchByte & offs) + (sym >> 8)); |
722 | 119M | bit = (sym >> 7) & 1; |
723 | 119M | sym <<= 1; |
724 | 119M | offs &= ~(matchByte ^ sym); |
725 | 119M | RC_BIT(p, prob, bit); |
726 | 119M | } |
727 | 119M | while (sym < 0x10000); |
728 | 14.9M | p->range = range; |
729 | 14.9M | } |
730 | | |
731 | | |
732 | | |
733 | | static void LzmaEnc_InitPriceTables(CProbPrice *ProbPrices) |
734 | 7.77k | { |
735 | 7.77k | UInt32 i; |
736 | 1.00M | for (i = 0; i < (kBitModelTotal >> kNumMoveReducingBits); i++) |
737 | 995k | { |
738 | 995k | const unsigned kCyclesBits = kNumBitPriceShiftBits; |
739 | 995k | UInt32 w = (i << kNumMoveReducingBits) + (1 << (kNumMoveReducingBits - 1)); |
740 | 995k | unsigned bitCount = 0; |
741 | 995k | unsigned j; |
742 | 4.97M | for (j = 0; j < kCyclesBits; j++) |
743 | 3.98M | { |
744 | 3.98M | w = w * w; |
745 | 3.98M | bitCount <<= 1; |
746 | 53.6M | while (w >= ((UInt32)1 << 16)) |
747 | 49.6M | { |
748 | 49.6M | w >>= 1; |
749 | 49.6M | bitCount++; |
750 | 49.6M | } |
751 | 3.98M | } |
752 | 995k | ProbPrices[i] = (CProbPrice)((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount); |
753 | | // printf("\n%3d: %5d", i, ProbPrices[i]); |
754 | 995k | } |
755 | 7.77k | } |
756 | | |
757 | | |
758 | | #define GET_PRICE(prob, bit) \ |
759 | 19.2M | p->ProbPrices[((prob) ^ (unsigned)(((-(int)(bit))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits]; |
760 | | |
761 | | #define GET_PRICEa(prob, bit) \ |
762 | 1.01G | ProbPrices[((prob) ^ (unsigned)((-((int)(bit))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits]; |
763 | | |
764 | 348M | #define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits] |
765 | 458M | #define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits] |
766 | | |
767 | 28.7M | #define GET_PRICEa_0(prob) ProbPrices[(prob) >> kNumMoveReducingBits] |
768 | 28.7M | #define GET_PRICEa_1(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits] |
769 | | |
770 | | |
771 | | static UInt32 LitEnc_GetPrice(const CLzmaProb *probs, UInt32 sym, const CProbPrice *ProbPrices) |
772 | 36.8M | { |
773 | 36.8M | UInt32 price = 0; |
774 | 36.8M | sym |= 0x100; |
775 | 36.8M | do |
776 | 294M | { |
777 | 294M | unsigned bit = sym & 1; |
778 | 294M | sym >>= 1; |
779 | 294M | price += GET_PRICEa(probs[sym], bit); |
780 | 294M | } |
781 | 294M | while (sym >= 2); |
782 | 36.8M | return price; |
783 | 36.8M | } |
784 | | |
785 | | |
786 | | static UInt32 LitEnc_Matched_GetPrice(const CLzmaProb *probs, UInt32 sym, UInt32 matchByte, const CProbPrice *ProbPrices) |
787 | 76.4M | { |
788 | 76.4M | UInt32 price = 0; |
789 | 76.4M | UInt32 offs = 0x100; |
790 | 76.4M | sym |= 0x100; |
791 | 76.4M | do |
792 | 611M | { |
793 | 611M | matchByte <<= 1; |
794 | 611M | price += GET_PRICEa(probs[offs + (matchByte & offs) + (sym >> 8)], (sym >> 7) & 1); |
795 | 611M | sym <<= 1; |
796 | 611M | offs &= ~(matchByte ^ sym); |
797 | 611M | } |
798 | 611M | while (sym < 0x10000); |
799 | 76.4M | return price; |
800 | 76.4M | } |
801 | | |
802 | | |
803 | | static void RcTree_ReverseEncode(CRangeEnc *rc, CLzmaProb *probs, unsigned numBits, unsigned sym) |
804 | 1.44M | { |
805 | 1.44M | UInt32 range = rc->range; |
806 | 1.44M | unsigned m = 1; |
807 | 1.44M | do |
808 | 4.40M | { |
809 | 4.40M | UInt32 ttt, newBound; |
810 | 4.40M | unsigned bit = sym & 1; |
811 | | // RangeEnc_EncodeBit(rc, probs + m, bit); |
812 | 4.40M | sym >>= 1; |
813 | 4.40M | RC_BIT(rc, probs + m, bit); |
814 | 4.40M | m = (m << 1) | bit; |
815 | 4.40M | } |
816 | 4.40M | while (--numBits); |
817 | 1.44M | rc->range = range; |
818 | 1.44M | } |
819 | | |
820 | | |
821 | | |
822 | | static void LenEnc_Init(CLenEnc *p) |
823 | 37.5k | { |
824 | 37.5k | unsigned i; |
825 | 9.64M | for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << (kLenNumLowBits + 1)); i++) |
826 | 9.61M | p->low[i] = kProbInitValue; |
827 | 9.64M | for (i = 0; i < kLenNumHighSymbols; i++) |
828 | 9.61M | p->high[i] = kProbInitValue; |
829 | 37.5k | } |
830 | | |
831 | | static void LenEnc_Encode(CLenEnc *p, CRangeEnc *rc, unsigned sym, unsigned posState) |
832 | 15.9M | { |
833 | 15.9M | UInt32 range, ttt, newBound; |
834 | 15.9M | CLzmaProb *probs = p->low; |
835 | 15.9M | range = rc->range; |
836 | 15.9M | RC_BIT_PRE(rc, probs); |
837 | 15.9M | if (sym >= kLenNumLowSymbols) |
838 | 1.94M | { |
839 | 1.94M | RC_BIT_1(rc, probs); |
840 | 1.94M | probs += kLenNumLowSymbols; |
841 | 1.94M | RC_BIT_PRE(rc, probs); |
842 | 1.94M | if (sym >= kLenNumLowSymbols * 2) |
843 | 1.21M | { |
844 | 1.21M | RC_BIT_1(rc, probs); |
845 | 1.21M | rc->range = range; |
846 | | // RcTree_Encode(rc, p->high, kLenNumHighBits, sym - kLenNumLowSymbols * 2); |
847 | 1.21M | LitEnc_Encode(rc, p->high, sym - kLenNumLowSymbols * 2); |
848 | 1.21M | return; |
849 | 1.21M | } |
850 | 726k | sym -= kLenNumLowSymbols; |
851 | 726k | } |
852 | | |
853 | | // RcTree_Encode(rc, probs + (posState << kLenNumLowBits), kLenNumLowBits, sym); |
854 | 14.7M | { |
855 | 14.7M | unsigned m; |
856 | 14.7M | unsigned bit; |
857 | 14.7M | RC_BIT_0(rc, probs); |
858 | 14.7M | probs += (posState << (1 + kLenNumLowBits)); |
859 | 14.7M | bit = (sym >> 2) ; RC_BIT(rc, probs + 1, bit); m = (1 << 1) + bit; |
860 | 14.7M | bit = (sym >> 1) & 1; RC_BIT(rc, probs + m, bit); m = (m << 1) + bit; |
861 | 14.7M | bit = sym & 1; RC_BIT(rc, probs + m, bit); |
862 | 14.7M | rc->range = range; |
863 | 14.7M | } |
864 | 14.7M | } |
865 | | |
866 | | static void SetPrices_3(const CLzmaProb *probs, UInt32 startPrice, UInt32 *prices, const CProbPrice *ProbPrices) |
867 | 1.82M | { |
868 | 1.82M | unsigned i; |
869 | 9.12M | for (i = 0; i < 8; i += 2) |
870 | 7.29M | { |
871 | 7.29M | UInt32 price = startPrice; |
872 | 7.29M | UInt32 prob; |
873 | 7.29M | price += GET_PRICEa(probs[1 ], (i >> 2)); |
874 | 7.29M | price += GET_PRICEa(probs[2 + (i >> 2)], (i >> 1) & 1); |
875 | 7.29M | prob = probs[4 + (i >> 1)]; |
876 | 7.29M | prices[i ] = price + GET_PRICEa_0(prob); |
877 | 7.29M | prices[i + 1] = price + GET_PRICEa_1(prob); |
878 | 7.29M | } |
879 | 1.82M | } |
880 | | |
881 | | |
882 | | MY_NO_INLINE static void MY_FAST_CALL LenPriceEnc_UpdateTables( |
883 | | CLenPriceEnc *p, |
884 | | unsigned numPosStates, |
885 | | const CLenEnc *enc, |
886 | | const CProbPrice *ProbPrices) |
887 | 228k | { |
888 | 228k | UInt32 b; |
889 | | |
890 | 228k | { |
891 | 228k | unsigned prob = enc->low[0]; |
892 | 228k | UInt32 a, c; |
893 | 228k | unsigned posState; |
894 | 228k | b = GET_PRICEa_1(prob); |
895 | 228k | a = GET_PRICEa_0(prob); |
896 | 228k | c = b + GET_PRICEa_0(enc->low[kLenNumLowSymbols]); |
897 | 1.14M | for (posState = 0; posState < numPosStates; posState++) |
898 | 912k | { |
899 | 912k | UInt32 *prices = p->prices[posState]; |
900 | 912k | const CLzmaProb *probs = enc->low + (posState << (1 + kLenNumLowBits)); |
901 | 912k | SetPrices_3(probs, a, prices, ProbPrices); |
902 | 912k | SetPrices_3(probs + kLenNumLowSymbols, c, prices + kLenNumLowSymbols, ProbPrices); |
903 | 912k | } |
904 | 228k | } |
905 | | |
906 | | /* |
907 | | { |
908 | | unsigned i; |
909 | | UInt32 b; |
910 | | a = GET_PRICEa_0(enc->low[0]); |
911 | | for (i = 0; i < kLenNumLowSymbols; i++) |
912 | | p->prices2[i] = a; |
913 | | a = GET_PRICEa_1(enc->low[0]); |
914 | | b = a + GET_PRICEa_0(enc->low[kLenNumLowSymbols]); |
915 | | for (i = kLenNumLowSymbols; i < kLenNumLowSymbols * 2; i++) |
916 | | p->prices2[i] = b; |
917 | | a += GET_PRICEa_1(enc->low[kLenNumLowSymbols]); |
918 | | } |
919 | | */ |
920 | | |
921 | | // p->counter = numSymbols; |
922 | | // p->counter = 64; |
923 | | |
924 | 228k | { |
925 | 228k | unsigned i = p->tableSize; |
926 | | |
927 | 228k | if (i > kLenNumLowSymbols * 2) |
928 | 228k | { |
929 | 228k | const CLzmaProb *probs = enc->high; |
930 | 228k | UInt32 *prices = p->prices[0] + kLenNumLowSymbols * 2; |
931 | 228k | i -= kLenNumLowSymbols * 2 - 1; |
932 | 228k | i >>= 1; |
933 | 228k | b += GET_PRICEa_1(enc->low[kLenNumLowSymbols]); |
934 | 228k | do |
935 | 1.82M | { |
936 | | /* |
937 | | p->prices2[i] = a + |
938 | | // RcTree_GetPrice(enc->high, kLenNumHighBits, i - kLenNumLowSymbols * 2, ProbPrices); |
939 | | LitEnc_GetPrice(probs, i - kLenNumLowSymbols * 2, ProbPrices); |
940 | | */ |
941 | | // UInt32 price = a + RcTree_GetPrice(probs, kLenNumHighBits - 1, sym, ProbPrices); |
942 | 1.82M | unsigned sym = --i + (1 << (kLenNumHighBits - 1)); |
943 | 1.82M | UInt32 price = b; |
944 | 1.82M | do |
945 | 12.7M | { |
946 | 12.7M | unsigned bit = sym & 1; |
947 | 12.7M | sym >>= 1; |
948 | 12.7M | price += GET_PRICEa(probs[sym], bit); |
949 | 12.7M | } |
950 | 12.7M | while (sym >= 2); |
951 | | |
952 | 1.82M | { |
953 | 1.82M | unsigned prob = probs[(size_t)i + (1 << (kLenNumHighBits - 1))]; |
954 | 1.82M | prices[(size_t)i * 2 ] = price + GET_PRICEa_0(prob); |
955 | 1.82M | prices[(size_t)i * 2 + 1] = price + GET_PRICEa_1(prob); |
956 | 1.82M | } |
957 | 1.82M | } |
958 | 1.82M | while (i); |
959 | | |
960 | 228k | { |
961 | 228k | unsigned posState; |
962 | 228k | size_t num = (p->tableSize - kLenNumLowSymbols * 2) * sizeof(p->prices[0][0]); |
963 | 912k | for (posState = 1; posState < numPosStates; posState++) |
964 | 684k | memcpy(p->prices[posState] + kLenNumLowSymbols * 2, p->prices[0] + kLenNumLowSymbols * 2, num); |
965 | 228k | } |
966 | 228k | } |
967 | 228k | } |
968 | 228k | } |
969 | | |
970 | | /* |
971 | | #ifdef SHOW_STAT |
972 | | g_STAT_OFFSET += num; |
973 | | printf("\n MovePos %u", num); |
974 | | #endif |
975 | | */ |
976 | | |
977 | 903k | #define MOVE_POS(p, num) { \ |
978 | 903k | p->additionalOffset += (num); \ |
979 | 903k | p->matchFinder.Skip(p->matchFinderObj, (UInt32)(num)); } |
980 | | |
981 | | |
982 | | static unsigned ReadMatchDistances(CLzmaEnc *p, unsigned *numPairsRes) |
983 | 312M | { |
984 | 312M | unsigned numPairs; |
985 | | |
986 | 312M | p->additionalOffset++; |
987 | 312M | p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj); |
988 | 312M | numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches); |
989 | 312M | *numPairsRes = numPairs; |
990 | | |
991 | | #ifdef SHOW_STAT |
992 | | printf("\n i = %u numPairs = %u ", g_STAT_OFFSET, numPairs / 2); |
993 | | g_STAT_OFFSET++; |
994 | | { |
995 | | unsigned i; |
996 | | for (i = 0; i < numPairs; i += 2) |
997 | | printf("%2u %6u | ", p->matches[i], p->matches[i + 1]); |
998 | | } |
999 | | #endif |
1000 | | |
1001 | 312M | if (numPairs == 0) |
1002 | 234M | return 0; |
1003 | 77.9M | { |
1004 | 77.9M | unsigned len = p->matches[(size_t)numPairs - 2]; |
1005 | 77.9M | if (len != p->numFastBytes) |
1006 | 77.0M | return len; |
1007 | 890k | { |
1008 | 890k | UInt32 numAvail = p->numAvail; |
1009 | 890k | if (numAvail > LZMA_MATCH_LEN_MAX) |
1010 | 888k | numAvail = LZMA_MATCH_LEN_MAX; |
1011 | 890k | { |
1012 | 890k | const Byte *p1 = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; |
1013 | 890k | const Byte *p2 = p1 + len; |
1014 | 890k | ptrdiff_t dif = (ptrdiff_t)-1 - p->matches[(size_t)numPairs - 1]; |
1015 | 890k | const Byte *lim = p1 + numAvail; |
1016 | 92.5M | for (; p2 != lim && *p2 == p2[dif]; p2++) |
1017 | 91.7M | {} |
1018 | 890k | return (unsigned)(p2 - p1); |
1019 | 77.9M | } |
1020 | 77.9M | } |
1021 | 77.9M | } |
1022 | 77.9M | } |
1023 | | |
1024 | 528M | #define MARK_LIT ((UInt32)(Int32)-1) |
1025 | | |
1026 | 50.0M | #define MakeAs_Lit(p) { (p)->dist = MARK_LIT; (p)->extra = 0; } |
1027 | 11.0M | #define MakeAs_ShortRep(p) { (p)->dist = 0; (p)->extra = 0; } |
1028 | 47.3M | #define IsShortRep(p) ((p)->dist == 0) |
1029 | | |
1030 | | |
1031 | | #define GetPrice_ShortRep(p, state, posState) \ |
1032 | 12.0M | ( GET_PRICE_0(p->isRepG0[state]) + GET_PRICE_0(p->isRep0Long[state][posState])) |
1033 | | |
1034 | 56.9M | #define GetPrice_Rep_0(p, state, posState) ( \ |
1035 | 56.9M | GET_PRICE_1(p->isMatch[state][posState]) \ |
1036 | 56.9M | + GET_PRICE_1(p->isRep0Long[state][posState])) \ |
1037 | 56.9M | + GET_PRICE_1(p->isRep[state]) \ |
1038 | 56.9M | + GET_PRICE_0(p->isRepG0[state]) |
1039 | | |
1040 | | MY_FORCE_INLINE |
1041 | | static UInt32 GetPrice_PureRep(const CLzmaEnc *p, unsigned repIndex, size_t state, size_t posState) |
1042 | 65.5M | { |
1043 | 65.5M | UInt32 price; |
1044 | 65.5M | UInt32 prob = p->isRepG0[state]; |
1045 | 65.5M | if (repIndex == 0) |
1046 | 27.8M | { |
1047 | 27.8M | price = GET_PRICE_0(prob); |
1048 | 27.8M | price += GET_PRICE_1(p->isRep0Long[state][posState]); |
1049 | 27.8M | } |
1050 | 37.7M | else |
1051 | 37.7M | { |
1052 | 37.7M | price = GET_PRICE_1(prob); |
1053 | 37.7M | prob = p->isRepG1[state]; |
1054 | 37.7M | if (repIndex == 1) |
1055 | 18.4M | price += GET_PRICE_0(prob); |
1056 | 19.2M | else |
1057 | 19.2M | { |
1058 | 19.2M | price += GET_PRICE_1(prob); |
1059 | 19.2M | price += GET_PRICE(p->isRepG2[state], repIndex - 2); |
1060 | 19.2M | } |
1061 | 37.7M | } |
1062 | 65.5M | return price; |
1063 | 65.5M | } |
1064 | | |
1065 | | |
1066 | | static unsigned Backward(CLzmaEnc *p, unsigned cur) |
1067 | 11.5M | { |
1068 | 11.5M | unsigned wr = cur + 1; |
1069 | 11.5M | p->optEnd = wr; |
1070 | | |
1071 | 11.5M | for (;;) |
1072 | 47.5M | { |
1073 | 47.5M | UInt32 dist = p->opt[cur].dist; |
1074 | 47.5M | unsigned len = (unsigned)p->opt[cur].len; |
1075 | 47.5M | unsigned extra = (unsigned)p->opt[cur].extra; |
1076 | 47.5M | cur -= len; |
1077 | | |
1078 | 47.5M | if (extra) |
1079 | 2.62M | { |
1080 | 2.62M | wr--; |
1081 | 2.62M | p->opt[wr].len = (UInt32)len; |
1082 | 2.62M | cur -= extra; |
1083 | 2.62M | len = extra; |
1084 | 2.62M | if (extra == 1) |
1085 | 85.7k | { |
1086 | 85.7k | p->opt[wr].dist = dist; |
1087 | 85.7k | dist = MARK_LIT; |
1088 | 85.7k | } |
1089 | 2.53M | else |
1090 | 2.53M | { |
1091 | 2.53M | p->opt[wr].dist = 0; |
1092 | 2.53M | len--; |
1093 | 2.53M | wr--; |
1094 | 2.53M | p->opt[wr].dist = MARK_LIT; |
1095 | 2.53M | p->opt[wr].len = 1; |
1096 | 2.53M | } |
1097 | 2.62M | } |
1098 | | |
1099 | 47.5M | if (cur == 0) |
1100 | 11.5M | { |
1101 | 11.5M | p->backRes = dist; |
1102 | 11.5M | p->optCur = wr; |
1103 | 11.5M | return len; |
1104 | 11.5M | } |
1105 | | |
1106 | 35.9M | wr--; |
1107 | 35.9M | p->opt[wr].dist = dist; |
1108 | 35.9M | p->opt[wr].len = (UInt32)len; |
1109 | 35.9M | } |
1110 | 11.5M | } |
1111 | | |
1112 | | |
1113 | | |
1114 | | #define LIT_PROBS(pos, prevByte) \ |
1115 | 358M | (p->litProbs + (UInt32)3 * (((((pos) << 8) + (prevByte)) & p->lpMask) << p->lc)) |
1116 | | |
1117 | | |
1118 | | static unsigned GetOptimum(CLzmaEnc *p, UInt32 position) |
1119 | 224M | { |
1120 | 224M | unsigned last, cur; |
1121 | 224M | UInt32 reps[LZMA_NUM_REPS]; |
1122 | 224M | unsigned repLens[LZMA_NUM_REPS]; |
1123 | 224M | UInt32 *matches; |
1124 | | |
1125 | 224M | { |
1126 | 224M | UInt32 numAvail; |
1127 | 224M | unsigned numPairs, mainLen, repMaxIndex, i, posState; |
1128 | 224M | UInt32 matchPrice, repMatchPrice; |
1129 | 224M | const Byte *data; |
1130 | 224M | Byte curByte, matchByte; |
1131 | | |
1132 | 224M | p->optCur = p->optEnd = 0; |
1133 | | |
1134 | 224M | if (p->additionalOffset == 0) |
1135 | 223M | mainLen = ReadMatchDistances(p, &numPairs); |
1136 | 388k | else |
1137 | 388k | { |
1138 | 388k | mainLen = p->longestMatchLen; |
1139 | 388k | numPairs = p->numPairs; |
1140 | 388k | } |
1141 | | |
1142 | 224M | numAvail = p->numAvail; |
1143 | 224M | if (numAvail < 2) |
1144 | 4.45k | { |
1145 | 4.45k | p->backRes = MARK_LIT; |
1146 | 4.45k | return 1; |
1147 | 4.45k | } |
1148 | 224M | if (numAvail > LZMA_MATCH_LEN_MAX) |
1149 | 223M | numAvail = LZMA_MATCH_LEN_MAX; |
1150 | | |
1151 | 224M | data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; |
1152 | 224M | repMaxIndex = 0; |
1153 | | |
1154 | 1.12G | for (i = 0; i < LZMA_NUM_REPS; i++) |
1155 | 897M | { |
1156 | 897M | unsigned len; |
1157 | 897M | const Byte *data2; |
1158 | 897M | reps[i] = p->reps[i]; |
1159 | 897M | data2 = data - reps[i]; |
1160 | 897M | if (data[0] != data2[0] || data[1] != data2[1]) |
1161 | 894M | { |
1162 | 894M | repLens[i] = 0; |
1163 | 894M | continue; |
1164 | 894M | } |
1165 | 271M | for (len = 2; len < numAvail && data[len] == data2[len]; len++) |
1166 | 269M | {} |
1167 | 2.61M | repLens[i] = len; |
1168 | 2.61M | if (len > repLens[repMaxIndex]) |
1169 | 639k | repMaxIndex = i; |
1170 | 2.61M | } |
1171 | | |
1172 | 224M | if (repLens[repMaxIndex] >= p->numFastBytes) |
1173 | 775k | { |
1174 | 775k | unsigned len; |
1175 | 775k | p->backRes = (UInt32)repMaxIndex; |
1176 | 775k | len = repLens[repMaxIndex]; |
1177 | 775k | MOVE_POS(p, len - 1) |
1178 | 775k | return len; |
1179 | 775k | } |
1180 | | |
1181 | 223M | matches = p->matches; |
1182 | | |
1183 | 223M | if (mainLen >= p->numFastBytes) |
1184 | 125k | { |
1185 | 125k | p->backRes = matches[(size_t)numPairs - 1] + LZMA_NUM_REPS; |
1186 | 125k | MOVE_POS(p, mainLen - 1) |
1187 | 125k | return mainLen; |
1188 | 125k | } |
1189 | | |
1190 | 223M | curByte = *data; |
1191 | 223M | matchByte = *(data - reps[0]); |
1192 | | |
1193 | 223M | last = repLens[repMaxIndex]; |
1194 | 223M | if (last <= mainLen) |
1195 | 223M | last = mainLen; |
1196 | | |
1197 | 223M | if (last < 2 && curByte != matchByte) |
1198 | 210M | { |
1199 | 210M | p->backRes = MARK_LIT; |
1200 | 210M | return 1; |
1201 | 210M | } |
1202 | | |
1203 | 13.1M | p->opt[0].state = (CState)p->state; |
1204 | | |
1205 | 13.1M | posState = (position & p->pbMask); |
1206 | | |
1207 | 13.1M | { |
1208 | 13.1M | const CLzmaProb *probs = LIT_PROBS(position, *(data - 1)); |
1209 | 13.1M | p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) + |
1210 | 13.1M | (!IsLitState(p->state) ? |
1211 | 1.26M | LitEnc_Matched_GetPrice(probs, curByte, matchByte, p->ProbPrices) : |
1212 | 13.1M | LitEnc_GetPrice(probs, curByte, p->ProbPrices)); |
1213 | 13.1M | } |
1214 | | |
1215 | 13.1M | MakeAs_Lit(&p->opt[1]); |
1216 | | |
1217 | 13.1M | matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]); |
1218 | 13.1M | repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]); |
1219 | | |
1220 | | // 18.06 |
1221 | 13.1M | if (matchByte == curByte && repLens[0] == 0) |
1222 | 2.03M | { |
1223 | 2.03M | UInt32 shortRepPrice = repMatchPrice + GetPrice_ShortRep(p, p->state, posState); |
1224 | 2.03M | if (shortRepPrice < p->opt[1].price) |
1225 | 1.66M | { |
1226 | 1.66M | p->opt[1].price = shortRepPrice; |
1227 | 1.66M | MakeAs_ShortRep(&p->opt[1]); |
1228 | 1.66M | } |
1229 | 2.03M | if (last < 2) |
1230 | 1.62M | { |
1231 | 1.62M | p->backRes = p->opt[1].dist; |
1232 | 1.62M | return 1; |
1233 | 1.62M | } |
1234 | 2.03M | } |
1235 | | |
1236 | 11.5M | p->opt[1].len = 1; |
1237 | | |
1238 | 11.5M | p->opt[0].reps[0] = reps[0]; |
1239 | 11.5M | p->opt[0].reps[1] = reps[1]; |
1240 | 11.5M | p->opt[0].reps[2] = reps[2]; |
1241 | 11.5M | p->opt[0].reps[3] = reps[3]; |
1242 | | |
1243 | | // ---------- REP ---------- |
1244 | | |
1245 | 57.6M | for (i = 0; i < LZMA_NUM_REPS; i++) |
1246 | 46.1M | { |
1247 | 46.1M | unsigned repLen = repLens[i]; |
1248 | 46.1M | UInt32 price; |
1249 | 46.1M | if (repLen < 2) |
1250 | 45.2M | continue; |
1251 | 868k | price = repMatchPrice + GetPrice_PureRep(p, i, p->state, posState); |
1252 | 868k | do |
1253 | 2.69M | { |
1254 | 2.69M | UInt32 price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState, repLen); |
1255 | 2.69M | COptimal *opt = &p->opt[repLen]; |
1256 | 2.69M | if (price2 < opt->price) |
1257 | 2.27M | { |
1258 | 2.27M | opt->price = price2; |
1259 | 2.27M | opt->len = (UInt32)repLen; |
1260 | 2.27M | opt->dist = (UInt32)i; |
1261 | 2.27M | opt->extra = 0; |
1262 | 2.27M | } |
1263 | 2.69M | } |
1264 | 2.69M | while (--repLen >= 2); |
1265 | 868k | } |
1266 | | |
1267 | | |
1268 | | // ---------- MATCH ---------- |
1269 | 11.5M | { |
1270 | 11.5M | unsigned len = repLens[0] + 1; |
1271 | 11.5M | if (len <= mainLen) |
1272 | 11.3M | { |
1273 | 11.3M | unsigned offs = 0; |
1274 | 11.3M | UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]); |
1275 | | |
1276 | 11.3M | if (len < 2) |
1277 | 11.2M | len = 2; |
1278 | 54.6k | else |
1279 | 104k | while (len > matches[offs]) |
1280 | 50.3k | offs += 2; |
1281 | | |
1282 | 11.3M | for (; ; len++) |
1283 | 22.0M | { |
1284 | 22.0M | COptimal *opt; |
1285 | 22.0M | UInt32 dist = matches[(size_t)offs + 1]; |
1286 | 22.0M | UInt32 price = normalMatchPrice + GET_PRICE_LEN(&p->lenEnc, posState, len); |
1287 | 22.0M | unsigned lenToPosState = GetLenToPosState(len); |
1288 | | |
1289 | 22.0M | if (dist < kNumFullDistances) |
1290 | 3.73M | price += p->distancesPrices[lenToPosState][dist & (kNumFullDistances - 1)]; |
1291 | 18.3M | else |
1292 | 18.3M | { |
1293 | 18.3M | unsigned slot; |
1294 | 18.3M | GetPosSlot2(dist, slot); |
1295 | 18.3M | price += p->alignPrices[dist & kAlignMask]; |
1296 | 18.3M | price += p->posSlotPrices[lenToPosState][slot]; |
1297 | 18.3M | } |
1298 | | |
1299 | 22.0M | opt = &p->opt[len]; |
1300 | | |
1301 | 22.0M | if (price < opt->price) |
1302 | 20.6M | { |
1303 | 20.6M | opt->price = price; |
1304 | 20.6M | opt->len = (UInt32)len; |
1305 | 20.6M | opt->dist = dist + LZMA_NUM_REPS; |
1306 | 20.6M | opt->extra = 0; |
1307 | 20.6M | } |
1308 | | |
1309 | 22.0M | if (len == matches[offs]) |
1310 | 12.8M | { |
1311 | 12.8M | offs += 2; |
1312 | 12.8M | if (offs == numPairs) |
1313 | 11.3M | break; |
1314 | 12.8M | } |
1315 | 22.0M | } |
1316 | 11.3M | } |
1317 | 11.5M | } |
1318 | | |
1319 | | |
1320 | 11.5M | cur = 0; |
1321 | | |
1322 | | #ifdef SHOW_STAT2 |
1323 | | /* if (position >= 0) */ |
1324 | | { |
1325 | | unsigned i; |
1326 | | printf("\n pos = %4X", position); |
1327 | | for (i = cur; i <= last; i++) |
1328 | | printf("\nprice[%4X] = %u", position - cur + i, p->opt[i].price); |
1329 | | } |
1330 | | #endif |
1331 | 11.5M | } |
1332 | | |
1333 | | |
1334 | | |
1335 | | // ---------- Optimal Parsing ---------- |
1336 | | |
1337 | 0 | for (;;) |
1338 | 99.9M | { |
1339 | 99.9M | unsigned numAvail; |
1340 | 99.9M | UInt32 numAvailFull; |
1341 | 99.9M | unsigned newLen, numPairs, prev, state, posState, startLen; |
1342 | 99.9M | UInt32 litPrice, matchPrice, repMatchPrice; |
1343 | 99.9M | BoolInt nextIsLit; |
1344 | 99.9M | Byte curByte, matchByte; |
1345 | 99.9M | const Byte *data; |
1346 | 99.9M | COptimal *curOpt, *nextOpt; |
1347 | | |
1348 | 99.9M | if (++cur == last) |
1349 | 11.1M | break; |
1350 | | |
1351 | | // 18.06 |
1352 | 88.8M | if (cur >= kNumOpts - 64) |
1353 | 6.39k | { |
1354 | 6.39k | unsigned j, best; |
1355 | 6.39k | UInt32 price = p->opt[cur].price; |
1356 | 6.39k | best = cur; |
1357 | 74.5k | for (j = cur + 1; j <= last; j++) |
1358 | 68.1k | { |
1359 | 68.1k | UInt32 price2 = p->opt[j].price; |
1360 | 68.1k | if (price >= price2) |
1361 | 6.34k | { |
1362 | 6.34k | price = price2; |
1363 | 6.34k | best = j; |
1364 | 6.34k | } |
1365 | 68.1k | } |
1366 | 6.39k | { |
1367 | 6.39k | unsigned delta = best - cur; |
1368 | 6.39k | if (delta != 0) |
1369 | 1.98k | { |
1370 | 1.98k | MOVE_POS(p, delta); |
1371 | 1.98k | } |
1372 | 6.39k | } |
1373 | 6.39k | cur = best; |
1374 | 6.39k | break; |
1375 | 6.39k | } |
1376 | | |
1377 | 88.8M | newLen = ReadMatchDistances(p, &numPairs); |
1378 | | |
1379 | 88.8M | if (newLen >= p->numFastBytes) |
1380 | 388k | { |
1381 | 388k | p->numPairs = numPairs; |
1382 | 388k | p->longestMatchLen = newLen; |
1383 | 388k | break; |
1384 | 388k | } |
1385 | | |
1386 | 88.4M | curOpt = &p->opt[cur]; |
1387 | | |
1388 | 88.4M | position++; |
1389 | | |
1390 | | // we need that check here, if skip_items in p->opt are possible |
1391 | | /* |
1392 | | if (curOpt->price >= kInfinityPrice) |
1393 | | continue; |
1394 | | */ |
1395 | | |
1396 | 88.4M | prev = cur - curOpt->len; |
1397 | | |
1398 | 88.4M | if (curOpt->len == 1) |
1399 | 47.3M | { |
1400 | 47.3M | state = (unsigned)p->opt[prev].state; |
1401 | 47.3M | if (IsShortRep(curOpt)) |
1402 | 9.19M | state = kShortRepNextStates[state]; |
1403 | 38.1M | else |
1404 | 38.1M | state = kLiteralNextStates[state]; |
1405 | 47.3M | } |
1406 | 41.0M | else |
1407 | 41.0M | { |
1408 | 41.0M | const COptimal *prevOpt; |
1409 | 41.0M | UInt32 b0; |
1410 | 41.0M | UInt32 dist = curOpt->dist; |
1411 | | |
1412 | 41.0M | if (curOpt->extra) |
1413 | 4.98M | { |
1414 | 4.98M | prev -= (unsigned)curOpt->extra; |
1415 | 4.98M | state = kState_RepAfterLit; |
1416 | 4.98M | if (curOpt->extra == 1) |
1417 | 134k | state = (dist < LZMA_NUM_REPS ? kState_RepAfterLit : kState_MatchAfterLit); |
1418 | 4.98M | } |
1419 | 36.1M | else |
1420 | 36.1M | { |
1421 | 36.1M | state = (unsigned)p->opt[prev].state; |
1422 | 36.1M | if (dist < LZMA_NUM_REPS) |
1423 | 23.4M | state = kRepNextStates[state]; |
1424 | 12.7M | else |
1425 | 12.7M | state = kMatchNextStates[state]; |
1426 | 36.1M | } |
1427 | | |
1428 | 41.0M | prevOpt = &p->opt[prev]; |
1429 | 41.0M | b0 = prevOpt->reps[0]; |
1430 | | |
1431 | 41.0M | if (dist < LZMA_NUM_REPS) |
1432 | 27.8M | { |
1433 | 27.8M | if (dist == 0) |
1434 | 17.9M | { |
1435 | 17.9M | reps[0] = b0; |
1436 | 17.9M | reps[1] = prevOpt->reps[1]; |
1437 | 17.9M | reps[2] = prevOpt->reps[2]; |
1438 | 17.9M | reps[3] = prevOpt->reps[3]; |
1439 | 17.9M | } |
1440 | 9.92M | else |
1441 | 9.92M | { |
1442 | 9.92M | reps[1] = b0; |
1443 | 9.92M | b0 = prevOpt->reps[1]; |
1444 | 9.92M | if (dist == 1) |
1445 | 7.09M | { |
1446 | 7.09M | reps[0] = b0; |
1447 | 7.09M | reps[2] = prevOpt->reps[2]; |
1448 | 7.09M | reps[3] = prevOpt->reps[3]; |
1449 | 7.09M | } |
1450 | 2.83M | else |
1451 | 2.83M | { |
1452 | 2.83M | reps[2] = b0; |
1453 | 2.83M | reps[0] = prevOpt->reps[dist]; |
1454 | 2.83M | reps[3] = prevOpt->reps[dist ^ 1]; |
1455 | 2.83M | } |
1456 | 9.92M | } |
1457 | 27.8M | } |
1458 | 13.2M | else |
1459 | 13.2M | { |
1460 | 13.2M | reps[0] = (dist - LZMA_NUM_REPS + 1); |
1461 | 13.2M | reps[1] = b0; |
1462 | 13.2M | reps[2] = prevOpt->reps[1]; |
1463 | 13.2M | reps[3] = prevOpt->reps[2]; |
1464 | 13.2M | } |
1465 | 41.0M | } |
1466 | | |
1467 | 88.4M | curOpt->state = (CState)state; |
1468 | 88.4M | curOpt->reps[0] = reps[0]; |
1469 | 88.4M | curOpt->reps[1] = reps[1]; |
1470 | 88.4M | curOpt->reps[2] = reps[2]; |
1471 | 88.4M | curOpt->reps[3] = reps[3]; |
1472 | | |
1473 | 88.4M | data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; |
1474 | 88.4M | curByte = *data; |
1475 | 88.4M | matchByte = *(data - reps[0]); |
1476 | | |
1477 | 88.4M | posState = (position & p->pbMask); |
1478 | | |
1479 | | /* |
1480 | | The order of Price checks: |
1481 | | < LIT |
1482 | | <= SHORT_REP |
1483 | | < LIT : REP_0 |
1484 | | < REP [ : LIT : REP_0 ] |
1485 | | < MATCH [ : LIT : REP_0 ] |
1486 | | */ |
1487 | | |
1488 | 88.4M | { |
1489 | 88.4M | UInt32 curPrice = curOpt->price; |
1490 | 88.4M | unsigned prob = p->isMatch[state][posState]; |
1491 | 88.4M | matchPrice = curPrice + GET_PRICE_1(prob); |
1492 | 88.4M | litPrice = curPrice + GET_PRICE_0(prob); |
1493 | 88.4M | } |
1494 | | |
1495 | 88.4M | nextOpt = &p->opt[(size_t)cur + 1]; |
1496 | 88.4M | nextIsLit = False; |
1497 | | |
1498 | | // here we can allow skip_items in p->opt, if we don't check (nextOpt->price < kInfinityPrice) |
1499 | | // 18.new.06 |
1500 | 88.4M | if ((nextOpt->price < kInfinityPrice |
1501 | | // && !IsLitState(state) |
1502 | 88.4M | && matchByte == curByte) |
1503 | 88.4M | || litPrice > nextOpt->price |
1504 | 88.4M | ) |
1505 | 44.5M | litPrice = 0; |
1506 | 43.8M | else |
1507 | 43.8M | { |
1508 | 43.8M | const CLzmaProb *probs = LIT_PROBS(position, *(data - 1)); |
1509 | 43.8M | litPrice += (!IsLitState(state) ? |
1510 | 18.9M | LitEnc_Matched_GetPrice(probs, curByte, matchByte, p->ProbPrices) : |
1511 | 43.8M | LitEnc_GetPrice(probs, curByte, p->ProbPrices)); |
1512 | | |
1513 | 43.8M | if (litPrice < nextOpt->price) |
1514 | 36.8M | { |
1515 | 36.8M | nextOpt->price = litPrice; |
1516 | 36.8M | nextOpt->len = 1; |
1517 | 36.8M | MakeAs_Lit(nextOpt); |
1518 | 36.8M | nextIsLit = True; |
1519 | 36.8M | } |
1520 | 43.8M | } |
1521 | | |
1522 | 88.4M | repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]); |
1523 | | |
1524 | 88.4M | numAvailFull = p->numAvail; |
1525 | 88.4M | { |
1526 | 88.4M | unsigned temp = kNumOpts - 1 - cur; |
1527 | 88.4M | if (numAvailFull > temp) |
1528 | 86.9M | numAvailFull = (UInt32)temp; |
1529 | 88.4M | } |
1530 | | |
1531 | | // 18.06 |
1532 | | // ---------- SHORT_REP ---------- |
1533 | 88.4M | if (IsLitState(state)) // 18.new |
1534 | 38.1M | if (matchByte == curByte) |
1535 | 12.8M | if (repMatchPrice < nextOpt->price) // 18.new |
1536 | | // if (numAvailFull < 2 || data[1] != *(data - reps[0] + 1)) |
1537 | 10.0M | if ( |
1538 | | // nextOpt->price >= kInfinityPrice || |
1539 | 10.0M | nextOpt->len < 2 // we can check nextOpt->len, if skip items are not allowed in p->opt |
1540 | 10.0M | || (nextOpt->dist != 0 |
1541 | | // && nextOpt->extra <= 1 // 17.old |
1542 | 8.95M | ) |
1543 | 10.0M | ) |
1544 | 10.0M | { |
1545 | 10.0M | UInt32 shortRepPrice = repMatchPrice + GetPrice_ShortRep(p, state, posState); |
1546 | | // if (shortRepPrice <= nextOpt->price) // 17.old |
1547 | 10.0M | if (shortRepPrice < nextOpt->price) // 18.new |
1548 | 9.35M | { |
1549 | 9.35M | nextOpt->price = shortRepPrice; |
1550 | 9.35M | nextOpt->len = 1; |
1551 | 9.35M | MakeAs_ShortRep(nextOpt); |
1552 | 9.35M | nextIsLit = False; |
1553 | 9.35M | } |
1554 | 10.0M | } |
1555 | | |
1556 | 88.4M | if (numAvailFull < 2) |
1557 | 3.04k | continue; |
1558 | 88.4M | numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes); |
1559 | | |
1560 | | // numAvail <= p->numFastBytes |
1561 | | |
1562 | | // ---------- LIT : REP_0 ---------- |
1563 | | |
1564 | 88.4M | if (!nextIsLit |
1565 | 88.4M | && litPrice != 0 // 18.new |
1566 | 88.4M | && matchByte != curByte |
1567 | 88.4M | && numAvailFull > 2) |
1568 | 7.02M | { |
1569 | 7.02M | const Byte *data2 = data - reps[0]; |
1570 | 7.02M | if (data[1] == data2[1] && data[2] == data2[2]) |
1571 | 733k | { |
1572 | 733k | unsigned len; |
1573 | 733k | unsigned limit = p->numFastBytes + 1; |
1574 | 733k | if (limit > numAvailFull) |
1575 | 2.62k | limit = numAvailFull; |
1576 | 2.75M | for (len = 3; len < limit && data[len] == data2[len]; len++) |
1577 | 2.02M | {} |
1578 | | |
1579 | 733k | { |
1580 | 733k | unsigned state2 = kLiteralNextStates[state]; |
1581 | 733k | unsigned posState2 = (position + 1) & p->pbMask; |
1582 | 733k | UInt32 price = litPrice + GetPrice_Rep_0(p, state2, posState2); |
1583 | 733k | { |
1584 | 733k | unsigned offset = cur + len; |
1585 | | |
1586 | 733k | if (last < offset) |
1587 | 45.4k | last = offset; |
1588 | | |
1589 | | // do |
1590 | 733k | { |
1591 | 733k | UInt32 price2; |
1592 | 733k | COptimal *opt; |
1593 | 733k | len--; |
1594 | | // price2 = price + GetPrice_Len_Rep_0(p, len, state2, posState2); |
1595 | 733k | price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState2, len); |
1596 | | |
1597 | 733k | opt = &p->opt[offset]; |
1598 | | // offset--; |
1599 | 733k | if (price2 < opt->price) |
1600 | 220k | { |
1601 | 220k | opt->price = price2; |
1602 | 220k | opt->len = (UInt32)len; |
1603 | 220k | opt->dist = 0; |
1604 | 220k | opt->extra = 1; |
1605 | 220k | } |
1606 | 733k | } |
1607 | | // while (len >= 3); |
1608 | 733k | } |
1609 | 733k | } |
1610 | 733k | } |
1611 | 7.02M | } |
1612 | | |
1613 | 88.4M | startLen = 2; /* speed optimization */ |
1614 | | |
1615 | 88.4M | { |
1616 | | // ---------- REP ---------- |
1617 | 88.4M | unsigned repIndex = 0; // 17.old |
1618 | | // unsigned repIndex = IsLitState(state) ? 0 : 1; // 18.notused |
1619 | 442M | for (; repIndex < LZMA_NUM_REPS; repIndex++) |
1620 | 353M | { |
1621 | 353M | unsigned len; |
1622 | 353M | UInt32 price; |
1623 | 353M | const Byte *data2 = data - reps[repIndex]; |
1624 | 353M | if (data[0] != data2[0] || data[1] != data2[1]) |
1625 | 288M | continue; |
1626 | | |
1627 | 301M | for (len = 2; len < numAvail && data[len] == data2[len]; len++) |
1628 | 237M | {} |
1629 | | |
1630 | | // if (len < startLen) continue; // 18.new: speed optimization |
1631 | | |
1632 | 64.7M | { |
1633 | 64.7M | unsigned offset = cur + len; |
1634 | 64.7M | if (last < offset) |
1635 | 527k | last = offset; |
1636 | 64.7M | } |
1637 | 64.7M | { |
1638 | 64.7M | unsigned len2 = len; |
1639 | 64.7M | price = repMatchPrice + GetPrice_PureRep(p, repIndex, state, posState); |
1640 | 64.7M | do |
1641 | 301M | { |
1642 | 301M | UInt32 price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState, len2); |
1643 | 301M | COptimal *opt = &p->opt[cur + len2]; |
1644 | 301M | if (price2 < opt->price) |
1645 | 33.0M | { |
1646 | 33.0M | opt->price = price2; |
1647 | 33.0M | opt->len = (UInt32)len2; |
1648 | 33.0M | opt->dist = (UInt32)repIndex; |
1649 | 33.0M | opt->extra = 0; |
1650 | 33.0M | } |
1651 | 301M | } |
1652 | 301M | while (--len2 >= 2); |
1653 | 64.7M | } |
1654 | | |
1655 | 64.7M | if (repIndex == 0) startLen = len + 1; // 17.old |
1656 | | // startLen = len + 1; // 18.new |
1657 | | |
1658 | | /* if (_maxMode) */ |
1659 | 64.7M | { |
1660 | | // ---------- REP : LIT : REP_0 ---------- |
1661 | | // numFastBytes + 1 + numFastBytes |
1662 | | |
1663 | 64.7M | unsigned len2 = len + 1; |
1664 | 64.7M | unsigned limit = len2 + p->numFastBytes; |
1665 | 64.7M | if (limit > numAvailFull) |
1666 | 143k | limit = numAvailFull; |
1667 | | |
1668 | 64.7M | len2 += 2; |
1669 | 64.7M | if (len2 <= limit) |
1670 | 64.6M | if (data[len2 - 2] == data2[len2 - 2]) |
1671 | 41.6M | if (data[len2 - 1] == data2[len2 - 1]) |
1672 | 38.6M | { |
1673 | 38.6M | unsigned state2 = kRepNextStates[state]; |
1674 | 38.6M | unsigned posState2 = (position + len) & p->pbMask; |
1675 | 38.6M | price += GET_PRICE_LEN(&p->repLenEnc, posState, len) |
1676 | 38.6M | + GET_PRICE_0(p->isMatch[state2][posState2]) |
1677 | 38.6M | + LitEnc_Matched_GetPrice(LIT_PROBS(position + len, data[(size_t)len - 1]), |
1678 | 38.6M | data[len], data2[len], p->ProbPrices); |
1679 | | |
1680 | | // state2 = kLiteralNextStates[state2]; |
1681 | 38.6M | state2 = kState_LitAfterRep; |
1682 | 38.6M | posState2 = (posState2 + 1) & p->pbMask; |
1683 | | |
1684 | | |
1685 | 38.6M | price += GetPrice_Rep_0(p, state2, posState2); |
1686 | | |
1687 | 249M | for (; len2 < limit && data[len2] == data2[len2]; len2++) |
1688 | 211M | {} |
1689 | | |
1690 | 38.6M | len2 -= len; |
1691 | | // if (len2 >= 3) |
1692 | 38.6M | { |
1693 | 38.6M | { |
1694 | 38.6M | unsigned offset = cur + len + len2; |
1695 | | |
1696 | 38.6M | if (last < offset) |
1697 | 7.43M | last = offset; |
1698 | | // do |
1699 | 38.6M | { |
1700 | 38.6M | UInt32 price2; |
1701 | 38.6M | COptimal *opt; |
1702 | 38.6M | len2--; |
1703 | | // price2 = price + GetPrice_Len_Rep_0(p, len2, state2, posState2); |
1704 | 38.6M | price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState2, len2); |
1705 | | |
1706 | 38.6M | opt = &p->opt[offset]; |
1707 | | // offset--; |
1708 | 38.6M | if (price2 < opt->price) |
1709 | 9.63M | { |
1710 | 9.63M | opt->price = price2; |
1711 | 9.63M | opt->len = (UInt32)len2; |
1712 | 9.63M | opt->extra = (CExtra)(len + 1); |
1713 | 9.63M | opt->dist = (UInt32)repIndex; |
1714 | 9.63M | } |
1715 | 38.6M | } |
1716 | | // while (len2 >= 3); |
1717 | 38.6M | } |
1718 | 38.6M | } |
1719 | 38.6M | } |
1720 | 64.7M | } |
1721 | 64.7M | } |
1722 | 88.4M | } |
1723 | | |
1724 | | |
1725 | | // ---------- MATCH ---------- |
1726 | | /* for (unsigned len = 2; len <= newLen; len++) */ |
1727 | 88.4M | if (newLen > numAvail) |
1728 | 0 | { |
1729 | 0 | newLen = numAvail; |
1730 | 0 | for (numPairs = 0; newLen > matches[numPairs]; numPairs += 2); |
1731 | 0 | matches[numPairs] = (UInt32)newLen; |
1732 | 0 | numPairs += 2; |
1733 | 0 | } |
1734 | | |
1735 | | // startLen = 2; /* speed optimization */ |
1736 | | |
1737 | 88.4M | if (newLen >= startLen) |
1738 | 51.9M | { |
1739 | 51.9M | UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]); |
1740 | 51.9M | UInt32 dist; |
1741 | 51.9M | unsigned offs, posSlot, len; |
1742 | | |
1743 | 51.9M | { |
1744 | 51.9M | unsigned offset = cur + newLen; |
1745 | 51.9M | if (last < offset) |
1746 | 15.3M | last = offset; |
1747 | 51.9M | } |
1748 | | |
1749 | 51.9M | offs = 0; |
1750 | 67.9M | while (startLen > matches[offs]) |
1751 | 16.0M | offs += 2; |
1752 | 51.9M | dist = matches[(size_t)offs + 1]; |
1753 | | |
1754 | | // if (dist >= kNumFullDistances) |
1755 | 51.9M | GetPosSlot2(dist, posSlot); |
1756 | | |
1757 | 118M | for (len = /*2*/ startLen; ; len++) |
1758 | 170M | { |
1759 | 170M | UInt32 price = normalMatchPrice + GET_PRICE_LEN(&p->lenEnc, posState, len); |
1760 | 170M | { |
1761 | 170M | COptimal *opt; |
1762 | 170M | unsigned lenNorm = len - 2; |
1763 | 170M | lenNorm = GetLenToPosState2(lenNorm); |
1764 | 170M | if (dist < kNumFullDistances) |
1765 | 32.0M | price += p->distancesPrices[lenNorm][dist & (kNumFullDistances - 1)]; |
1766 | 138M | else |
1767 | 138M | price += p->posSlotPrices[lenNorm][posSlot] + p->alignPrices[dist & kAlignMask]; |
1768 | | |
1769 | 170M | opt = &p->opt[cur + len]; |
1770 | 170M | if (price < opt->price) |
1771 | 58.9M | { |
1772 | 58.9M | opt->price = price; |
1773 | 58.9M | opt->len = (UInt32)len; |
1774 | 58.9M | opt->dist = dist + LZMA_NUM_REPS; |
1775 | 58.9M | opt->extra = 0; |
1776 | 58.9M | } |
1777 | 170M | } |
1778 | | |
1779 | 170M | if (len == matches[offs]) |
1780 | 81.3M | { |
1781 | | // if (p->_maxMode) { |
1782 | | // MATCH : LIT : REP_0 |
1783 | | |
1784 | 81.3M | const Byte *data2 = data - dist - 1; |
1785 | 81.3M | unsigned len2 = len + 1; |
1786 | 81.3M | unsigned limit = len2 + p->numFastBytes; |
1787 | 81.3M | if (limit > numAvailFull) |
1788 | 57.7k | limit = numAvailFull; |
1789 | | |
1790 | 81.3M | len2 += 2; |
1791 | 81.3M | if (len2 <= limit) |
1792 | 81.3M | if (data[len2 - 2] == data2[len2 - 2]) |
1793 | 35.7M | if (data[len2 - 1] == data2[len2 - 1]) |
1794 | 17.5M | { |
1795 | 92.4M | for (; len2 < limit && data[len2] == data2[len2]; len2++) |
1796 | 74.8M | {} |
1797 | | |
1798 | 17.5M | len2 -= len; |
1799 | | |
1800 | | // if (len2 >= 3) |
1801 | 17.5M | { |
1802 | 17.5M | unsigned state2 = kMatchNextStates[state]; |
1803 | 17.5M | unsigned posState2 = (position + len) & p->pbMask; |
1804 | 17.5M | unsigned offset; |
1805 | 17.5M | price += GET_PRICE_0(p->isMatch[state2][posState2]); |
1806 | 17.5M | price += LitEnc_Matched_GetPrice(LIT_PROBS(position + len, data[(size_t)len - 1]), |
1807 | 17.5M | data[len], data2[len], p->ProbPrices); |
1808 | | |
1809 | | // state2 = kLiteralNextStates[state2]; |
1810 | 17.5M | state2 = kState_LitAfterMatch; |
1811 | | |
1812 | 17.5M | posState2 = (posState2 + 1) & p->pbMask; |
1813 | 17.5M | price += GetPrice_Rep_0(p, state2, posState2); |
1814 | | |
1815 | 17.5M | offset = cur + len + len2; |
1816 | | |
1817 | 17.5M | if (last < offset) |
1818 | 3.65M | last = offset; |
1819 | | // do |
1820 | 17.5M | { |
1821 | 17.5M | UInt32 price2; |
1822 | 17.5M | COptimal *opt; |
1823 | 17.5M | len2--; |
1824 | | // price2 = price + GetPrice_Len_Rep_0(p, len2, state2, posState2); |
1825 | 17.5M | price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState2, len2); |
1826 | 17.5M | opt = &p->opt[offset]; |
1827 | | // offset--; |
1828 | 17.5M | if (price2 < opt->price) |
1829 | 5.10M | { |
1830 | 5.10M | opt->price = price2; |
1831 | 5.10M | opt->len = (UInt32)len2; |
1832 | 5.10M | opt->extra = (CExtra)(len + 1); |
1833 | 5.10M | opt->dist = dist + LZMA_NUM_REPS; |
1834 | 5.10M | } |
1835 | 17.5M | } |
1836 | | // while (len2 >= 3); |
1837 | 17.5M | } |
1838 | | |
1839 | 17.5M | } |
1840 | | |
1841 | 81.3M | offs += 2; |
1842 | 81.3M | if (offs == numPairs) |
1843 | 51.9M | break; |
1844 | 29.4M | dist = matches[(size_t)offs + 1]; |
1845 | | // if (dist >= kNumFullDistances) |
1846 | 29.4M | GetPosSlot2(dist, posSlot); |
1847 | 29.4M | } |
1848 | 170M | } |
1849 | 51.9M | } |
1850 | 88.4M | } |
1851 | | |
1852 | 11.5M | do |
1853 | 105M | p->opt[last].price = kInfinityPrice; |
1854 | 105M | while (--last); |
1855 | | |
1856 | 11.5M | return Backward(p, cur); |
1857 | 13.1M | } |
1858 | | |
1859 | | |
1860 | | |
1861 | 0 | #define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist)) |
1862 | | |
1863 | | |
1864 | | |
1865 | | static unsigned GetOptimumFast(CLzmaEnc *p) |
1866 | 0 | { |
1867 | 0 | UInt32 numAvail, mainDist; |
1868 | 0 | unsigned mainLen, numPairs, repIndex, repLen, i; |
1869 | 0 | const Byte *data; |
1870 | |
|
1871 | 0 | if (p->additionalOffset == 0) |
1872 | 0 | mainLen = ReadMatchDistances(p, &numPairs); |
1873 | 0 | else |
1874 | 0 | { |
1875 | 0 | mainLen = p->longestMatchLen; |
1876 | 0 | numPairs = p->numPairs; |
1877 | 0 | } |
1878 | |
|
1879 | 0 | numAvail = p->numAvail; |
1880 | 0 | p->backRes = MARK_LIT; |
1881 | 0 | if (numAvail < 2) |
1882 | 0 | return 1; |
1883 | | // if (mainLen < 2 && p->state == 0) return 1; // 18.06.notused |
1884 | 0 | if (numAvail > LZMA_MATCH_LEN_MAX) |
1885 | 0 | numAvail = LZMA_MATCH_LEN_MAX; |
1886 | 0 | data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; |
1887 | 0 | repLen = repIndex = 0; |
1888 | | |
1889 | 0 | for (i = 0; i < LZMA_NUM_REPS; i++) |
1890 | 0 | { |
1891 | 0 | unsigned len; |
1892 | 0 | const Byte *data2 = data - p->reps[i]; |
1893 | 0 | if (data[0] != data2[0] || data[1] != data2[1]) |
1894 | 0 | continue; |
1895 | 0 | for (len = 2; len < numAvail && data[len] == data2[len]; len++) |
1896 | 0 | {} |
1897 | 0 | if (len >= p->numFastBytes) |
1898 | 0 | { |
1899 | 0 | p->backRes = (UInt32)i; |
1900 | 0 | MOVE_POS(p, len - 1) |
1901 | 0 | return len; |
1902 | 0 | } |
1903 | 0 | if (len > repLen) |
1904 | 0 | { |
1905 | 0 | repIndex = i; |
1906 | 0 | repLen = len; |
1907 | 0 | } |
1908 | 0 | } |
1909 | | |
1910 | 0 | if (mainLen >= p->numFastBytes) |
1911 | 0 | { |
1912 | 0 | p->backRes = p->matches[(size_t)numPairs - 1] + LZMA_NUM_REPS; |
1913 | 0 | MOVE_POS(p, mainLen - 1) |
1914 | 0 | return mainLen; |
1915 | 0 | } |
1916 | | |
1917 | 0 | mainDist = 0; /* for GCC */ |
1918 | | |
1919 | 0 | if (mainLen >= 2) |
1920 | 0 | { |
1921 | 0 | mainDist = p->matches[(size_t)numPairs - 1]; |
1922 | 0 | while (numPairs > 2) |
1923 | 0 | { |
1924 | 0 | UInt32 dist2; |
1925 | 0 | if (mainLen != p->matches[(size_t)numPairs - 4] + 1) |
1926 | 0 | break; |
1927 | 0 | dist2 = p->matches[(size_t)numPairs - 3]; |
1928 | 0 | if (!ChangePair(dist2, mainDist)) |
1929 | 0 | break; |
1930 | 0 | numPairs -= 2; |
1931 | 0 | mainLen--; |
1932 | 0 | mainDist = dist2; |
1933 | 0 | } |
1934 | 0 | if (mainLen == 2 && mainDist >= 0x80) |
1935 | 0 | mainLen = 1; |
1936 | 0 | } |
1937 | |
|
1938 | 0 | if (repLen >= 2) |
1939 | 0 | if ( repLen + 1 >= mainLen |
1940 | 0 | || (repLen + 2 >= mainLen && mainDist >= (1 << 9)) |
1941 | 0 | || (repLen + 3 >= mainLen && mainDist >= (1 << 15))) |
1942 | 0 | { |
1943 | 0 | p->backRes = (UInt32)repIndex; |
1944 | 0 | MOVE_POS(p, repLen - 1) |
1945 | 0 | return repLen; |
1946 | 0 | } |
1947 | | |
1948 | 0 | if (mainLen < 2 || numAvail <= 2) |
1949 | 0 | return 1; |
1950 | | |
1951 | 0 | { |
1952 | 0 | unsigned len1 = ReadMatchDistances(p, &p->numPairs); |
1953 | 0 | p->longestMatchLen = len1; |
1954 | | |
1955 | 0 | if (len1 >= 2) |
1956 | 0 | { |
1957 | 0 | UInt32 newDist = p->matches[(size_t)p->numPairs - 1]; |
1958 | 0 | if ( (len1 >= mainLen && newDist < mainDist) |
1959 | 0 | || (len1 == mainLen + 1 && !ChangePair(mainDist, newDist)) |
1960 | 0 | || (len1 > mainLen + 1) |
1961 | 0 | || (len1 + 1 >= mainLen && mainLen >= 3 && ChangePair(newDist, mainDist))) |
1962 | 0 | return 1; |
1963 | 0 | } |
1964 | 0 | } |
1965 | | |
1966 | 0 | data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; |
1967 | | |
1968 | 0 | for (i = 0; i < LZMA_NUM_REPS; i++) |
1969 | 0 | { |
1970 | 0 | unsigned len, limit; |
1971 | 0 | const Byte *data2 = data - p->reps[i]; |
1972 | 0 | if (data[0] != data2[0] || data[1] != data2[1]) |
1973 | 0 | continue; |
1974 | 0 | limit = mainLen - 1; |
1975 | 0 | for (len = 2;; len++) |
1976 | 0 | { |
1977 | 0 | if (len >= limit) |
1978 | 0 | return 1; |
1979 | 0 | if (data[len] != data2[len]) |
1980 | 0 | break; |
1981 | 0 | } |
1982 | 0 | } |
1983 | | |
1984 | 0 | p->backRes = mainDist + LZMA_NUM_REPS; |
1985 | 0 | if (mainLen != 2) |
1986 | 0 | { |
1987 | 0 | MOVE_POS(p, mainLen - 2) |
1988 | 0 | } |
1989 | 0 | return mainLen; |
1990 | 0 | } |
1991 | | |
1992 | | |
1993 | | |
1994 | | |
1995 | | static void WriteEndMarker(CLzmaEnc *p, unsigned posState) |
1996 | 0 | { |
1997 | 0 | UInt32 range; |
1998 | 0 | range = p->rc.range; |
1999 | 0 | { |
2000 | 0 | UInt32 ttt, newBound; |
2001 | 0 | CLzmaProb *prob = &p->isMatch[p->state][posState]; |
2002 | 0 | RC_BIT_PRE(&p->rc, prob) |
2003 | 0 | RC_BIT_1(&p->rc, prob) |
2004 | 0 | prob = &p->isRep[p->state]; |
2005 | 0 | RC_BIT_PRE(&p->rc, prob) |
2006 | 0 | RC_BIT_0(&p->rc, prob) |
2007 | 0 | } |
2008 | 0 | p->state = kMatchNextStates[p->state]; |
2009 | | |
2010 | 0 | p->rc.range = range; |
2011 | 0 | LenEnc_Encode(&p->lenProbs, &p->rc, 0, posState); |
2012 | 0 | range = p->rc.range; |
2013 | |
|
2014 | 0 | { |
2015 | | // RcTree_Encode_PosSlot(&p->rc, p->posSlotEncoder[0], (1 << kNumPosSlotBits) - 1); |
2016 | 0 | CLzmaProb *probs = p->posSlotEncoder[0]; |
2017 | 0 | unsigned m = 1; |
2018 | 0 | do |
2019 | 0 | { |
2020 | 0 | UInt32 ttt, newBound; |
2021 | 0 | RC_BIT_PRE(p, probs + m) |
2022 | 0 | RC_BIT_1(&p->rc, probs + m); |
2023 | 0 | m = (m << 1) + 1; |
2024 | 0 | } |
2025 | 0 | while (m < (1 << kNumPosSlotBits)); |
2026 | 0 | } |
2027 | 0 | { |
2028 | | // RangeEnc_EncodeDirectBits(&p->rc, ((UInt32)1 << (30 - kNumAlignBits)) - 1, 30 - kNumAlignBits); UInt32 range = p->range; |
2029 | 0 | unsigned numBits = 30 - kNumAlignBits; |
2030 | 0 | do |
2031 | 0 | { |
2032 | 0 | range >>= 1; |
2033 | 0 | p->rc.low += range; |
2034 | 0 | RC_NORM(&p->rc) |
2035 | 0 | } |
2036 | 0 | while (--numBits); |
2037 | 0 | } |
2038 | | |
2039 | 0 | { |
2040 | | // RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask); |
2041 | 0 | CLzmaProb *probs = p->posAlignEncoder; |
2042 | 0 | unsigned m = 1; |
2043 | 0 | do |
2044 | 0 | { |
2045 | 0 | UInt32 ttt, newBound; |
2046 | 0 | RC_BIT_PRE(p, probs + m) |
2047 | 0 | RC_BIT_1(&p->rc, probs + m); |
2048 | 0 | m = (m << 1) + 1; |
2049 | 0 | } |
2050 | 0 | while (m < kAlignTableSize); |
2051 | 0 | } |
2052 | 0 | p->rc.range = range; |
2053 | 0 | } |
2054 | | |
2055 | | |
2056 | | static SRes CheckErrors(CLzmaEnc *p) |
2057 | 40.5k | { |
2058 | 40.5k | if (p->result != SZ_OK) |
2059 | 0 | return p->result; |
2060 | 40.5k | if (p->rc.res != SZ_OK) |
2061 | 0 | p->result = SZ_ERROR_WRITE; |
2062 | 40.5k | if (p->matchFinderBase.result != SZ_OK) |
2063 | 0 | p->result = SZ_ERROR_READ; |
2064 | 40.5k | if (p->result != SZ_OK) |
2065 | 0 | p->finished = True; |
2066 | 40.5k | return p->result; |
2067 | 40.5k | } |
2068 | | |
2069 | | |
2070 | | MY_NO_INLINE static SRes Flush(CLzmaEnc *p, UInt32 nowPos) |
2071 | 20.2k | { |
2072 | | /* ReleaseMFStream(); */ |
2073 | 20.2k | p->finished = True; |
2074 | 20.2k | if (p->writeEndMark) |
2075 | 0 | WriteEndMarker(p, nowPos & p->pbMask); |
2076 | 20.2k | RangeEnc_FlushData(&p->rc); |
2077 | 20.2k | RangeEnc_FlushStream(&p->rc); |
2078 | 20.2k | return CheckErrors(p); |
2079 | 20.2k | } |
2080 | | |
2081 | | |
2082 | | MY_NO_INLINE static void FillAlignPrices(CLzmaEnc *p) |
2083 | 115k | { |
2084 | 115k | unsigned i; |
2085 | 115k | const CProbPrice *ProbPrices = p->ProbPrices; |
2086 | 115k | const CLzmaProb *probs = p->posAlignEncoder; |
2087 | | // p->alignPriceCount = 0; |
2088 | 1.03M | for (i = 0; i < kAlignTableSize / 2; i++) |
2089 | 923k | { |
2090 | 923k | UInt32 price = 0; |
2091 | 923k | unsigned sym = i; |
2092 | 923k | unsigned m = 1; |
2093 | 923k | unsigned bit; |
2094 | 923k | UInt32 prob; |
2095 | 923k | bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[m], bit); m = (m << 1) + bit; |
2096 | 923k | bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[m], bit); m = (m << 1) + bit; |
2097 | 923k | bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[m], bit); m = (m << 1) + bit; |
2098 | 923k | prob = probs[m]; |
2099 | 923k | p->alignPrices[i ] = price + GET_PRICEa_0(prob); |
2100 | 923k | p->alignPrices[i + 8] = price + GET_PRICEa_1(prob); |
2101 | | // p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices); |
2102 | 923k | } |
2103 | 115k | } |
2104 | | |
2105 | | |
2106 | | MY_NO_INLINE static void FillDistancesPrices(CLzmaEnc *p) |
2107 | 115k | { |
2108 | | // int y; for (y = 0; y < 100; y++) { |
2109 | | |
2110 | 115k | UInt32 tempPrices[kNumFullDistances]; |
2111 | 115k | unsigned i, lps; |
2112 | | |
2113 | 115k | const CProbPrice *ProbPrices = p->ProbPrices; |
2114 | 115k | p->matchPriceCount = 0; |
2115 | | |
2116 | 7.27M | for (i = kStartPosModelIndex / 2; i < kNumFullDistances / 2; i++) |
2117 | 7.15M | { |
2118 | 7.15M | unsigned posSlot = GetPosSlot1(i); |
2119 | 7.15M | unsigned footerBits = (posSlot >> 1) - 1; |
2120 | 7.15M | unsigned base = ((2 | (posSlot & 1)) << footerBits); |
2121 | 7.15M | const CLzmaProb *probs = p->posEncoders + (size_t)base * 2; |
2122 | | // tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base, footerBits, i - base, p->ProbPrices); |
2123 | 7.15M | UInt32 price = 0; |
2124 | 7.15M | unsigned m = 1; |
2125 | 7.15M | unsigned sym = i; |
2126 | 7.15M | unsigned offset = (unsigned)1 << footerBits; |
2127 | 7.15M | base += i; |
2128 | | |
2129 | 7.15M | if (footerBits) |
2130 | 6.92M | do |
2131 | 22.6M | { |
2132 | 22.6M | unsigned bit = sym & 1; |
2133 | 22.6M | sym >>= 1; |
2134 | 22.6M | price += GET_PRICEa(probs[m], bit); |
2135 | 22.6M | m = (m << 1) + bit; |
2136 | 22.6M | } |
2137 | 22.6M | while (--footerBits); |
2138 | | |
2139 | 7.15M | { |
2140 | 7.15M | unsigned prob = probs[m]; |
2141 | 7.15M | tempPrices[base ] = price + GET_PRICEa_0(prob); |
2142 | 7.15M | tempPrices[base + offset] = price + GET_PRICEa_1(prob); |
2143 | 7.15M | } |
2144 | 7.15M | } |
2145 | | |
2146 | 577k | for (lps = 0; lps < kNumLenToPosStates; lps++) |
2147 | 461k | { |
2148 | 461k | unsigned slot; |
2149 | 461k | unsigned distTableSize2 = (p->distTableSize + 1) >> 1; |
2150 | 461k | UInt32 *posSlotPrices = p->posSlotPrices[lps]; |
2151 | 461k | const CLzmaProb *probs = p->posSlotEncoder[lps]; |
2152 | | |
2153 | 11.5M | for (slot = 0; slot < distTableSize2; slot++) |
2154 | 11.0M | { |
2155 | | // posSlotPrices[slot] = RcTree_GetPrice(encoder, kNumPosSlotBits, slot, p->ProbPrices); |
2156 | 11.0M | UInt32 price; |
2157 | 11.0M | unsigned bit; |
2158 | 11.0M | unsigned sym = slot + (1 << (kNumPosSlotBits - 1)); |
2159 | 11.0M | unsigned prob; |
2160 | 11.0M | bit = sym & 1; sym >>= 1; price = GET_PRICEa(probs[sym], bit); |
2161 | 11.0M | bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[sym], bit); |
2162 | 11.0M | bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[sym], bit); |
2163 | 11.0M | bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[sym], bit); |
2164 | 11.0M | bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[sym], bit); |
2165 | 11.0M | prob = probs[(size_t)slot + (1 << (kNumPosSlotBits - 1))]; |
2166 | 11.0M | posSlotPrices[(size_t)slot * 2 ] = price + GET_PRICEa_0(prob); |
2167 | 11.0M | posSlotPrices[(size_t)slot * 2 + 1] = price + GET_PRICEa_1(prob); |
2168 | 11.0M | } |
2169 | | |
2170 | 461k | { |
2171 | 461k | UInt32 delta = ((UInt32)((kEndPosModelIndex / 2 - 1) - kNumAlignBits) << kNumBitPriceShiftBits); |
2172 | 8.30M | for (slot = kEndPosModelIndex / 2; slot < distTableSize2; slot++) |
2173 | 7.84M | { |
2174 | 7.84M | posSlotPrices[(size_t)slot * 2 ] += delta; |
2175 | 7.84M | posSlotPrices[(size_t)slot * 2 + 1] += delta; |
2176 | 7.84M | delta += ((UInt32)1 << kNumBitPriceShiftBits); |
2177 | 7.84M | } |
2178 | 461k | } |
2179 | | |
2180 | 461k | { |
2181 | 461k | UInt32 *dp = p->distancesPrices[lps]; |
2182 | | |
2183 | 461k | dp[0] = posSlotPrices[0]; |
2184 | 461k | dp[1] = posSlotPrices[1]; |
2185 | 461k | dp[2] = posSlotPrices[2]; |
2186 | 461k | dp[3] = posSlotPrices[3]; |
2187 | | |
2188 | 29.0M | for (i = 4; i < kNumFullDistances; i += 2) |
2189 | 28.6M | { |
2190 | 28.6M | UInt32 slotPrice = posSlotPrices[GetPosSlot1(i)]; |
2191 | 28.6M | dp[i ] = slotPrice + tempPrices[i]; |
2192 | 28.6M | dp[i + 1] = slotPrice + tempPrices[i + 1]; |
2193 | 28.6M | } |
2194 | 461k | } |
2195 | 461k | } |
2196 | | // } |
2197 | 115k | } |
2198 | | |
2199 | | |
2200 | | |
2201 | | void LzmaEnc_Construct(CLzmaEnc *p) |
2202 | 7.77k | { |
2203 | 7.77k | RangeEnc_Construct(&p->rc); |
2204 | 7.77k | MatchFinder_Construct(&p->matchFinderBase); |
2205 | | |
2206 | | #ifndef _7ZIP_ST |
2207 | | MatchFinderMt_Construct(&p->matchFinderMt); |
2208 | | p->matchFinderMt.MatchFinder = &p->matchFinderBase; |
2209 | | #endif |
2210 | | |
2211 | 7.77k | { |
2212 | 7.77k | CLzmaEncProps props; |
2213 | 7.77k | LzmaEncProps_Init(&props); |
2214 | 7.77k | LzmaEnc_SetProps(p, &props); |
2215 | 7.77k | } |
2216 | | |
2217 | 7.77k | #ifndef LZMA_LOG_BSR |
2218 | 7.77k | LzmaEnc_FastPosInit(p->g_FastPos); |
2219 | 7.77k | #endif |
2220 | | |
2221 | 7.77k | LzmaEnc_InitPriceTables(p->ProbPrices); |
2222 | 7.77k | p->litProbs = NULL; |
2223 | 7.77k | p->saveState.litProbs = NULL; |
2224 | | |
2225 | 7.77k | } |
2226 | | |
2227 | | CLzmaEncHandle LzmaEnc_Create(ISzAllocPtr alloc) |
2228 | 7.77k | { |
2229 | 7.77k | void *p; |
2230 | 7.77k | p = ISzAlloc_Alloc(alloc, sizeof(CLzmaEnc)); |
2231 | 7.77k | if (p) |
2232 | 7.77k | LzmaEnc_Construct((CLzmaEnc *)p); |
2233 | 7.77k | return p; |
2234 | 7.77k | } |
2235 | | |
2236 | | void LzmaEnc_FreeLits(CLzmaEnc *p, ISzAllocPtr alloc) |
2237 | 15.5k | { |
2238 | 15.5k | ISzAlloc_Free(alloc, p->litProbs); |
2239 | 15.5k | ISzAlloc_Free(alloc, p->saveState.litProbs); |
2240 | 15.5k | p->litProbs = NULL; |
2241 | 15.5k | p->saveState.litProbs = NULL; |
2242 | 15.5k | } |
2243 | | |
2244 | | void LzmaEnc_Destruct(CLzmaEnc *p, ISzAllocPtr alloc, ISzAllocPtr allocBig) |
2245 | 7.77k | { |
2246 | | #ifndef _7ZIP_ST |
2247 | | MatchFinderMt_Destruct(&p->matchFinderMt, allocBig); |
2248 | | #endif |
2249 | | |
2250 | 7.77k | MatchFinder_Free(&p->matchFinderBase, allocBig); |
2251 | 7.77k | LzmaEnc_FreeLits(p, alloc); |
2252 | 7.77k | RangeEnc_Free(&p->rc, alloc); |
2253 | 7.77k | } |
2254 | | |
2255 | | void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAllocPtr alloc, ISzAllocPtr allocBig) |
2256 | 7.77k | { |
2257 | 7.77k | LzmaEnc_Destruct((CLzmaEnc *)p, alloc, allocBig); |
2258 | 7.77k | ISzAlloc_Free(alloc, p); |
2259 | 7.77k | } |
2260 | | |
2261 | | |
2262 | | static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, UInt32 maxPackSize, UInt32 maxUnpackSize) |
2263 | 20.2k | { |
2264 | 20.2k | UInt32 nowPos32, startPos32; |
2265 | 20.2k | if (p->needInit) |
2266 | 7.77k | { |
2267 | 7.77k | p->matchFinder.Init(p->matchFinderObj); |
2268 | 7.77k | p->needInit = 0; |
2269 | 7.77k | } |
2270 | | |
2271 | 20.2k | if (p->finished) |
2272 | 0 | return p->result; |
2273 | 20.2k | RINOK(CheckErrors(p)); |
2274 | | |
2275 | 20.2k | nowPos32 = (UInt32)p->nowPos64; |
2276 | 20.2k | startPos32 = nowPos32; |
2277 | | |
2278 | 20.2k | if (p->nowPos64 == 0) |
2279 | 7.77k | { |
2280 | 7.77k | unsigned numPairs; |
2281 | 7.77k | Byte curByte; |
2282 | 7.77k | if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0) |
2283 | 0 | return Flush(p, nowPos32); |
2284 | 7.77k | ReadMatchDistances(p, &numPairs); |
2285 | 7.77k | RangeEnc_EncodeBit_0(&p->rc, &p->isMatch[kState_Start][0]); |
2286 | | // p->state = kLiteralNextStates[p->state]; |
2287 | 7.77k | curByte = *(p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset); |
2288 | 7.77k | LitEnc_Encode(&p->rc, p->litProbs, curByte); |
2289 | 7.77k | p->additionalOffset--; |
2290 | 7.77k | nowPos32++; |
2291 | 7.77k | } |
2292 | | |
2293 | 20.2k | if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0) |
2294 | | |
2295 | 12.5k | for (;;) |
2296 | 265M | { |
2297 | 265M | UInt32 dist; |
2298 | 265M | unsigned len, posState; |
2299 | 265M | UInt32 range, ttt, newBound; |
2300 | 265M | CLzmaProb *probs; |
2301 | | |
2302 | 265M | if (p->fastMode) |
2303 | 0 | len = GetOptimumFast(p); |
2304 | 265M | else |
2305 | 265M | { |
2306 | 265M | unsigned oci = p->optCur; |
2307 | 265M | if (p->optEnd == oci) |
2308 | 224M | len = GetOptimum(p, nowPos32); |
2309 | 41.1M | else |
2310 | 41.1M | { |
2311 | 41.1M | const COptimal *opt = &p->opt[oci]; |
2312 | 41.1M | len = opt->len; |
2313 | 41.1M | p->backRes = opt->dist; |
2314 | 41.1M | p->optCur = oci + 1; |
2315 | 41.1M | } |
2316 | 265M | } |
2317 | | |
2318 | 265M | posState = (unsigned)nowPos32 & p->pbMask; |
2319 | 265M | range = p->rc.range; |
2320 | 265M | probs = &p->isMatch[p->state][posState]; |
2321 | | |
2322 | 265M | RC_BIT_PRE(&p->rc, probs) |
2323 | | |
2324 | 265M | dist = p->backRes; |
2325 | | |
2326 | | #ifdef SHOW_STAT2 |
2327 | | printf("\n pos = %6X, len = %3u pos = %6u", nowPos32, len, dist); |
2328 | | #endif |
2329 | | |
2330 | 265M | if (dist == MARK_LIT) |
2331 | 244M | { |
2332 | 244M | Byte curByte; |
2333 | 244M | const Byte *data; |
2334 | 244M | unsigned state; |
2335 | | |
2336 | 244M | RC_BIT_0(&p->rc, probs); |
2337 | 244M | p->rc.range = range; |
2338 | 244M | data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset; |
2339 | 244M | probs = LIT_PROBS(nowPos32, *(data - 1)); |
2340 | 244M | curByte = *data; |
2341 | 244M | state = p->state; |
2342 | 244M | p->state = kLiteralNextStates[state]; |
2343 | 244M | if (IsLitState(state)) |
2344 | 229M | LitEnc_Encode(&p->rc, probs, curByte); |
2345 | 14.9M | else |
2346 | 14.9M | LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0])); |
2347 | 244M | } |
2348 | 20.4M | else |
2349 | 20.4M | { |
2350 | 20.4M | RC_BIT_1(&p->rc, probs); |
2351 | 20.4M | probs = &p->isRep[p->state]; |
2352 | 20.4M | RC_BIT_PRE(&p->rc, probs) |
2353 | | |
2354 | 20.4M | if (dist < LZMA_NUM_REPS) |
2355 | 14.0M | { |
2356 | 14.0M | RC_BIT_1(&p->rc, probs); |
2357 | 14.0M | probs = &p->isRepG0[p->state]; |
2358 | 14.0M | RC_BIT_PRE(&p->rc, probs) |
2359 | 14.0M | if (dist == 0) |
2360 | 10.7M | { |
2361 | 10.7M | RC_BIT_0(&p->rc, probs); |
2362 | 10.7M | probs = &p->isRep0Long[p->state][posState]; |
2363 | 10.7M | RC_BIT_PRE(&p->rc, probs) |
2364 | 10.7M | if (len != 1) |
2365 | 6.21M | { |
2366 | 6.21M | RC_BIT_1_BASE(&p->rc, probs); |
2367 | 6.21M | } |
2368 | 4.51M | else |
2369 | 4.51M | { |
2370 | 4.51M | RC_BIT_0_BASE(&p->rc, probs); |
2371 | 4.51M | p->state = kShortRepNextStates[p->state]; |
2372 | 4.51M | } |
2373 | 10.7M | } |
2374 | 3.32M | else |
2375 | 3.32M | { |
2376 | 3.32M | RC_BIT_1(&p->rc, probs); |
2377 | 3.32M | probs = &p->isRepG1[p->state]; |
2378 | 3.32M | RC_BIT_PRE(&p->rc, probs) |
2379 | 3.32M | if (dist == 1) |
2380 | 2.57M | { |
2381 | 2.57M | RC_BIT_0_BASE(&p->rc, probs); |
2382 | 2.57M | dist = p->reps[1]; |
2383 | 2.57M | } |
2384 | 751k | else |
2385 | 751k | { |
2386 | 751k | RC_BIT_1(&p->rc, probs); |
2387 | 751k | probs = &p->isRepG2[p->state]; |
2388 | 751k | RC_BIT_PRE(&p->rc, probs) |
2389 | 751k | if (dist == 2) |
2390 | 464k | { |
2391 | 464k | RC_BIT_0_BASE(&p->rc, probs); |
2392 | 464k | dist = p->reps[2]; |
2393 | 464k | } |
2394 | 286k | else |
2395 | 286k | { |
2396 | 286k | RC_BIT_1_BASE(&p->rc, probs); |
2397 | 286k | dist = p->reps[3]; |
2398 | 286k | p->reps[3] = p->reps[2]; |
2399 | 286k | } |
2400 | 751k | p->reps[2] = p->reps[1]; |
2401 | 751k | } |
2402 | 3.32M | p->reps[1] = p->reps[0]; |
2403 | 3.32M | p->reps[0] = dist; |
2404 | 3.32M | } |
2405 | | |
2406 | 14.0M | RC_NORM(&p->rc) |
2407 | | |
2408 | 14.0M | p->rc.range = range; |
2409 | | |
2410 | 14.0M | if (len != 1) |
2411 | 9.54M | { |
2412 | 9.54M | LenEnc_Encode(&p->repLenProbs, &p->rc, len - LZMA_MATCH_LEN_MIN, posState); |
2413 | 9.54M | --p->repLenEncCounter; |
2414 | 9.54M | p->state = kRepNextStates[p->state]; |
2415 | 9.54M | } |
2416 | 14.0M | } |
2417 | 6.42M | else |
2418 | 6.42M | { |
2419 | 6.42M | unsigned posSlot; |
2420 | 6.42M | RC_BIT_0(&p->rc, probs); |
2421 | 6.42M | p->rc.range = range; |
2422 | 6.42M | p->state = kMatchNextStates[p->state]; |
2423 | | |
2424 | 6.42M | LenEnc_Encode(&p->lenProbs, &p->rc, len - LZMA_MATCH_LEN_MIN, posState); |
2425 | | // --p->lenEnc.counter; |
2426 | | |
2427 | 6.42M | dist -= LZMA_NUM_REPS; |
2428 | 6.42M | p->reps[3] = p->reps[2]; |
2429 | 6.42M | p->reps[2] = p->reps[1]; |
2430 | 6.42M | p->reps[1] = p->reps[0]; |
2431 | 6.42M | p->reps[0] = dist + 1; |
2432 | | |
2433 | 6.42M | p->matchPriceCount++; |
2434 | 6.42M | GetPosSlot(dist, posSlot); |
2435 | | // RcTree_Encode_PosSlot(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], posSlot); |
2436 | 6.42M | { |
2437 | 6.42M | UInt32 sym = (UInt32)posSlot + (1 << kNumPosSlotBits); |
2438 | 6.42M | range = p->rc.range; |
2439 | 6.42M | probs = p->posSlotEncoder[GetLenToPosState(len)]; |
2440 | 6.42M | do |
2441 | 38.5M | { |
2442 | 38.5M | CLzmaProb *prob = probs + (sym >> kNumPosSlotBits); |
2443 | 38.5M | UInt32 bit = (sym >> (kNumPosSlotBits - 1)) & 1; |
2444 | 38.5M | sym <<= 1; |
2445 | 38.5M | RC_BIT(&p->rc, prob, bit); |
2446 | 38.5M | } |
2447 | 38.5M | while (sym < (1 << kNumPosSlotBits * 2)); |
2448 | 6.42M | p->rc.range = range; |
2449 | 6.42M | } |
2450 | | |
2451 | 6.42M | if (dist >= kStartPosModelIndex) |
2452 | 6.07M | { |
2453 | 6.07M | unsigned footerBits = ((posSlot >> 1) - 1); |
2454 | | |
2455 | 6.07M | if (dist < kNumFullDistances) |
2456 | 1.44M | { |
2457 | 1.44M | unsigned base = ((2 | (posSlot & 1)) << footerBits); |
2458 | 1.44M | RcTree_ReverseEncode(&p->rc, p->posEncoders + base, footerBits, (unsigned)(dist /* - base */)); |
2459 | 1.44M | } |
2460 | 4.63M | else |
2461 | 4.63M | { |
2462 | 4.63M | UInt32 pos2 = (dist | 0xF) << (32 - footerBits); |
2463 | 4.63M | range = p->rc.range; |
2464 | | // RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits); |
2465 | | /* |
2466 | | do |
2467 | | { |
2468 | | range >>= 1; |
2469 | | p->rc.low += range & (0 - ((dist >> --footerBits) & 1)); |
2470 | | RC_NORM(&p->rc) |
2471 | | } |
2472 | | while (footerBits > kNumAlignBits); |
2473 | | */ |
2474 | 4.63M | do |
2475 | 37.4M | { |
2476 | 37.4M | range >>= 1; |
2477 | 37.4M | p->rc.low += range & (0 - (pos2 >> 31)); |
2478 | 37.4M | pos2 += pos2; |
2479 | 37.4M | RC_NORM(&p->rc) |
2480 | 37.4M | } |
2481 | 37.4M | while (pos2 != 0xF0000000); |
2482 | | |
2483 | | |
2484 | | // RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask); |
2485 | | |
2486 | 4.63M | { |
2487 | 4.63M | unsigned m = 1; |
2488 | 4.63M | unsigned bit; |
2489 | 4.63M | bit = dist & 1; dist >>= 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit); m = (m << 1) + bit; |
2490 | 4.63M | bit = dist & 1; dist >>= 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit); m = (m << 1) + bit; |
2491 | 4.63M | bit = dist & 1; dist >>= 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit); m = (m << 1) + bit; |
2492 | 4.63M | bit = dist & 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit); |
2493 | 4.63M | p->rc.range = range; |
2494 | | // p->alignPriceCount++; |
2495 | 4.63M | } |
2496 | 4.63M | } |
2497 | 6.07M | } |
2498 | 6.42M | } |
2499 | 20.4M | } |
2500 | | |
2501 | 265M | nowPos32 += (UInt32)len; |
2502 | 265M | p->additionalOffset -= len; |
2503 | | |
2504 | 265M | if (p->additionalOffset == 0) |
2505 | 223M | { |
2506 | 223M | UInt32 processed; |
2507 | | |
2508 | 223M | if (!p->fastMode) |
2509 | 223M | { |
2510 | | /* |
2511 | | if (p->alignPriceCount >= 16) // kAlignTableSize |
2512 | | FillAlignPrices(p); |
2513 | | if (p->matchPriceCount >= 128) |
2514 | | FillDistancesPrices(p); |
2515 | | if (p->lenEnc.counter <= 0) |
2516 | | LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, &p->lenProbs, p->ProbPrices); |
2517 | | */ |
2518 | 223M | if (p->matchPriceCount >= 64) |
2519 | 87.3k | { |
2520 | 87.3k | FillAlignPrices(p); |
2521 | | // { int y; for (y = 0; y < 100; y++) { |
2522 | 87.3k | FillDistancesPrices(p); |
2523 | | // }} |
2524 | 87.3k | LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, &p->lenProbs, p->ProbPrices); |
2525 | 87.3k | } |
2526 | 223M | if (p->repLenEncCounter <= 0) |
2527 | 84.5k | { |
2528 | 84.5k | p->repLenEncCounter = REP_LEN_COUNT; |
2529 | 84.5k | LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, &p->repLenProbs, p->ProbPrices); |
2530 | 84.5k | } |
2531 | 223M | } |
2532 | | |
2533 | 223M | if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0) |
2534 | 7.77k | break; |
2535 | 223M | processed = nowPos32 - startPos32; |
2536 | | |
2537 | 223M | if (maxPackSize) |
2538 | 223M | { |
2539 | 223M | if (processed + kNumOpts + 300 >= maxUnpackSize |
2540 | 223M | || RangeEnc_GetProcessed_sizet(&p->rc) + kPackReserve >= maxPackSize) |
2541 | 4.73k | break; |
2542 | 223M | } |
2543 | 0 | else if (processed >= (1 << 17)) |
2544 | 0 | { |
2545 | 0 | p->nowPos64 += nowPos32 - startPos32; |
2546 | 0 | return CheckErrors(p); |
2547 | 0 | } |
2548 | 223M | } |
2549 | 265M | } |
2550 | | |
2551 | 20.2k | p->nowPos64 += nowPos32 - startPos32; |
2552 | 20.2k | return Flush(p, nowPos32); |
2553 | 20.2k | } |
2554 | | |
2555 | | |
2556 | | |
2557 | 7.77k | #define kBigHashDicLimit ((UInt32)1 << 24) |
2558 | | |
2559 | | static SRes LzmaEnc_Alloc(CLzmaEnc *p, UInt32 keepWindowSize, ISzAllocPtr alloc, ISzAllocPtr allocBig) |
2560 | 7.77k | { |
2561 | 7.77k | UInt32 beforeSize = kNumOpts; |
2562 | 7.77k | if (!RangeEnc_Alloc(&p->rc, alloc)) |
2563 | 0 | return SZ_ERROR_MEM; |
2564 | | |
2565 | | #ifndef _7ZIP_ST |
2566 | | p->mtMode = (p->multiThread && !p->fastMode && (p->matchFinderBase.btMode != 0)); |
2567 | | #endif |
2568 | | |
2569 | 7.77k | { |
2570 | 7.77k | unsigned lclp = p->lc + p->lp; |
2571 | 7.77k | if (!p->litProbs || !p->saveState.litProbs || p->lclp != lclp) |
2572 | 7.77k | { |
2573 | 7.77k | LzmaEnc_FreeLits(p, alloc); |
2574 | 7.77k | p->litProbs = (CLzmaProb *)ISzAlloc_Alloc(alloc, ((UInt32)0x300 << lclp) * sizeof(CLzmaProb)); |
2575 | 7.77k | p->saveState.litProbs = (CLzmaProb *)ISzAlloc_Alloc(alloc, ((UInt32)0x300 << lclp) * sizeof(CLzmaProb)); |
2576 | 7.77k | if (!p->litProbs || !p->saveState.litProbs) |
2577 | 0 | { |
2578 | 0 | LzmaEnc_FreeLits(p, alloc); |
2579 | 0 | return SZ_ERROR_MEM; |
2580 | 0 | } |
2581 | 7.77k | p->lclp = lclp; |
2582 | 7.77k | } |
2583 | 7.77k | } |
2584 | | |
2585 | 7.77k | p->matchFinderBase.bigHash = (Byte)(p->dictSize > kBigHashDicLimit ? 1 : 0); |
2586 | | |
2587 | 7.77k | if (beforeSize + p->dictSize < keepWindowSize) |
2588 | 0 | beforeSize = keepWindowSize - p->dictSize; |
2589 | | |
2590 | | #ifndef _7ZIP_ST |
2591 | | if (p->mtMode) |
2592 | | { |
2593 | | RINOK(MatchFinderMt_Create(&p->matchFinderMt, p->dictSize, beforeSize, p->numFastBytes, |
2594 | | LZMA_MATCH_LEN_MAX |
2595 | | + 1 /* 18.04 */ |
2596 | | , allocBig)); |
2597 | | p->matchFinderObj = &p->matchFinderMt; |
2598 | | p->matchFinderBase.bigHash = (Byte)( |
2599 | | (p->dictSize > kBigHashDicLimit && p->matchFinderBase.hashMask >= 0xFFFFFF) ? 1 : 0); |
2600 | | MatchFinderMt_CreateVTable(&p->matchFinderMt, &p->matchFinder); |
2601 | | } |
2602 | | else |
2603 | | #endif |
2604 | 7.77k | { |
2605 | 7.77k | if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig)) |
2606 | 0 | return SZ_ERROR_MEM; |
2607 | 7.77k | p->matchFinderObj = &p->matchFinderBase; |
2608 | 7.77k | MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder); |
2609 | 7.77k | } |
2610 | | |
2611 | 7.77k | return SZ_OK; |
2612 | 7.77k | } |
2613 | | |
2614 | | void LzmaEnc_Init(CLzmaEnc *p) |
2615 | 18.7k | { |
2616 | 18.7k | unsigned i; |
2617 | 18.7k | p->state = 0; |
2618 | 18.7k | p->reps[0] = |
2619 | 18.7k | p->reps[1] = |
2620 | 18.7k | p->reps[2] = |
2621 | 18.7k | p->reps[3] = 1; |
2622 | | |
2623 | 18.7k | RangeEnc_Init(&p->rc); |
2624 | | |
2625 | 319k | for (i = 0; i < (1 << kNumAlignBits); i++) |
2626 | 300k | p->posAlignEncoder[i] = kProbInitValue; |
2627 | | |
2628 | 244k | for (i = 0; i < kNumStates; i++) |
2629 | 225k | { |
2630 | 225k | unsigned j; |
2631 | 3.82M | for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++) |
2632 | 3.60M | { |
2633 | 3.60M | p->isMatch[i][j] = kProbInitValue; |
2634 | 3.60M | p->isRep0Long[i][j] = kProbInitValue; |
2635 | 3.60M | } |
2636 | 225k | p->isRep[i] = kProbInitValue; |
2637 | 225k | p->isRepG0[i] = kProbInitValue; |
2638 | 225k | p->isRepG1[i] = kProbInitValue; |
2639 | 225k | p->isRepG2[i] = kProbInitValue; |
2640 | 225k | } |
2641 | | |
2642 | 18.7k | { |
2643 | 93.8k | for (i = 0; i < kNumLenToPosStates; i++) |
2644 | 75.0k | { |
2645 | 75.0k | CLzmaProb *probs = p->posSlotEncoder[i]; |
2646 | 75.0k | unsigned j; |
2647 | 4.88M | for (j = 0; j < (1 << kNumPosSlotBits); j++) |
2648 | 4.80M | probs[j] = kProbInitValue; |
2649 | 75.0k | } |
2650 | 18.7k | } |
2651 | 18.7k | { |
2652 | 2.42M | for (i = 0; i < kNumFullDistances; i++) |
2653 | 2.40M | p->posEncoders[i] = kProbInitValue; |
2654 | 18.7k | } |
2655 | | |
2656 | 18.7k | { |
2657 | 18.7k | UInt32 num = (UInt32)0x300 << (p->lp + p->lc); |
2658 | 18.7k | UInt32 k; |
2659 | 18.7k | CLzmaProb *probs = p->litProbs; |
2660 | 115M | for (k = 0; k < num; k++) |
2661 | 115M | probs[k] = kProbInitValue; |
2662 | 18.7k | } |
2663 | | |
2664 | | |
2665 | 18.7k | LenEnc_Init(&p->lenProbs); |
2666 | 18.7k | LenEnc_Init(&p->repLenProbs); |
2667 | | |
2668 | 18.7k | p->optEnd = 0; |
2669 | 18.7k | p->optCur = 0; |
2670 | | |
2671 | 18.7k | { |
2672 | 38.4M | for (i = 0; i < kNumOpts; i++) |
2673 | 38.4M | p->opt[i].price = kInfinityPrice; |
2674 | 18.7k | } |
2675 | | |
2676 | 18.7k | p->additionalOffset = 0; |
2677 | | |
2678 | 18.7k | p->pbMask = (1 << p->pb) - 1; |
2679 | 18.7k | p->lpMask = ((UInt32)0x100 << p->lp) - ((unsigned)0x100 >> p->lc); |
2680 | 18.7k | } |
2681 | | |
2682 | | |
2683 | | void LzmaEnc_InitPrices(CLzmaEnc *p) |
2684 | 28.0k | { |
2685 | 28.0k | if (!p->fastMode) |
2686 | 28.0k | { |
2687 | 28.0k | FillDistancesPrices(p); |
2688 | 28.0k | FillAlignPrices(p); |
2689 | 28.0k | } |
2690 | | |
2691 | 28.0k | p->lenEnc.tableSize = |
2692 | 28.0k | p->repLenEnc.tableSize = |
2693 | 28.0k | p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN; |
2694 | | |
2695 | 28.0k | p->repLenEncCounter = REP_LEN_COUNT; |
2696 | | |
2697 | 28.0k | LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, &p->lenProbs, p->ProbPrices); |
2698 | 28.0k | LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, &p->repLenProbs, p->ProbPrices); |
2699 | 28.0k | } |
2700 | | |
2701 | | static SRes LzmaEnc_AllocAndInit(CLzmaEnc *p, UInt32 keepWindowSize, ISzAllocPtr alloc, ISzAllocPtr allocBig) |
2702 | 7.77k | { |
2703 | 7.77k | unsigned i; |
2704 | 139k | for (i = kEndPosModelIndex / 2; i < kDicLogSizeMax; i++) |
2705 | 139k | if (p->dictSize <= ((UInt32)1 << i)) |
2706 | 7.77k | break; |
2707 | 7.77k | p->distTableSize = i * 2; |
2708 | | |
2709 | 7.77k | p->finished = False; |
2710 | 7.77k | p->result = SZ_OK; |
2711 | 7.77k | RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig)); |
2712 | 7.77k | LzmaEnc_Init(p); |
2713 | 7.77k | LzmaEnc_InitPrices(p); |
2714 | 7.77k | p->nowPos64 = 0; |
2715 | 7.77k | return SZ_OK; |
2716 | 7.77k | } |
2717 | | |
2718 | | static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, |
2719 | | ISzAllocPtr alloc, ISzAllocPtr allocBig) |
2720 | 0 | { |
2721 | 0 | CLzmaEnc *p = (CLzmaEnc *)pp; |
2722 | 0 | p->matchFinderBase.stream = inStream; |
2723 | 0 | p->needInit = 1; |
2724 | 0 | p->rc.outStream = outStream; |
2725 | 0 | return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig); |
2726 | 0 | } |
2727 | | |
2728 | | SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp, |
2729 | | ISeqInStream *inStream, UInt32 keepWindowSize, |
2730 | | ISzAllocPtr alloc, ISzAllocPtr allocBig) |
2731 | 7.77k | { |
2732 | 7.77k | CLzmaEnc *p = (CLzmaEnc *)pp; |
2733 | 7.77k | p->matchFinderBase.stream = inStream; |
2734 | 7.77k | p->needInit = 1; |
2735 | 7.77k | return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig); |
2736 | 7.77k | } |
2737 | | |
2738 | | static void LzmaEnc_SetInputBuf(CLzmaEnc *p, const Byte *src, SizeT srcLen) |
2739 | 0 | { |
2740 | 0 | p->matchFinderBase.directInput = 1; |
2741 | 0 | p->matchFinderBase.bufferBase = (Byte *)src; |
2742 | 0 | p->matchFinderBase.directInputRem = srcLen; |
2743 | 0 | } |
2744 | | |
2745 | | SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen, |
2746 | | UInt32 keepWindowSize, ISzAllocPtr alloc, ISzAllocPtr allocBig) |
2747 | 0 | { |
2748 | 0 | CLzmaEnc *p = (CLzmaEnc *)pp; |
2749 | 0 | LzmaEnc_SetInputBuf(p, src, srcLen); |
2750 | 0 | p->needInit = 1; |
2751 | |
|
2752 | 0 | LzmaEnc_SetDataSize(pp, srcLen); |
2753 | 0 | return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig); |
2754 | 0 | } |
2755 | | |
2756 | | void LzmaEnc_Finish(CLzmaEncHandle pp) |
2757 | 7.77k | { |
2758 | | #ifndef _7ZIP_ST |
2759 | | CLzmaEnc *p = (CLzmaEnc *)pp; |
2760 | | if (p->mtMode) |
2761 | | MatchFinderMt_ReleaseStream(&p->matchFinderMt); |
2762 | | #else |
2763 | 7.77k | UNUSED_VAR(pp); |
2764 | 7.77k | #endif |
2765 | 7.77k | } |
2766 | | |
2767 | | |
2768 | | typedef struct |
2769 | | { |
2770 | | ISeqOutStream vt; |
2771 | | Byte *data; |
2772 | | SizeT rem; |
2773 | | BoolInt overflow; |
2774 | | } CLzmaEnc_SeqOutStreamBuf; |
2775 | | |
2776 | | static size_t SeqOutStreamBuf_Write(const ISeqOutStream *pp, const void *data, size_t size) |
2777 | 20.2k | { |
2778 | 20.2k | CLzmaEnc_SeqOutStreamBuf *p = CONTAINER_FROM_VTBL(pp, CLzmaEnc_SeqOutStreamBuf, vt); |
2779 | 20.2k | if (p->rem < size) |
2780 | 0 | { |
2781 | 0 | size = p->rem; |
2782 | 0 | p->overflow = True; |
2783 | 0 | } |
2784 | 20.2k | memcpy(p->data, data, size); |
2785 | 20.2k | p->rem -= size; |
2786 | 20.2k | p->data += size; |
2787 | 20.2k | return size; |
2788 | 20.2k | } |
2789 | | |
2790 | | |
2791 | | UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp) |
2792 | 0 | { |
2793 | 0 | const CLzmaEnc *p = (CLzmaEnc *)pp; |
2794 | 0 | return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj); |
2795 | 0 | } |
2796 | | |
2797 | | |
2798 | | const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp) |
2799 | 4.42k | { |
2800 | 4.42k | const CLzmaEnc *p = (CLzmaEnc *)pp; |
2801 | 4.42k | return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset; |
2802 | 4.42k | } |
2803 | | |
2804 | | |
2805 | | SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, BoolInt reInit, |
2806 | | Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize) |
2807 | 20.2k | { |
2808 | 20.2k | CLzmaEnc *p = (CLzmaEnc *)pp; |
2809 | 20.2k | UInt64 nowPos64; |
2810 | 20.2k | SRes res; |
2811 | 20.2k | CLzmaEnc_SeqOutStreamBuf outStream; |
2812 | | |
2813 | 20.2k | outStream.vt.Write = SeqOutStreamBuf_Write; |
2814 | 20.2k | outStream.data = dest; |
2815 | 20.2k | outStream.rem = *destLen; |
2816 | 20.2k | outStream.overflow = False; |
2817 | | |
2818 | 20.2k | p->writeEndMark = False; |
2819 | 20.2k | p->finished = False; |
2820 | 20.2k | p->result = SZ_OK; |
2821 | | |
2822 | 20.2k | if (reInit) |
2823 | 10.9k | LzmaEnc_Init(p); |
2824 | 20.2k | LzmaEnc_InitPrices(p); |
2825 | | |
2826 | 20.2k | nowPos64 = p->nowPos64; |
2827 | 20.2k | RangeEnc_Init(&p->rc); |
2828 | 20.2k | p->rc.outStream = &outStream.vt; |
2829 | | |
2830 | 20.2k | if (desiredPackSize == 0) |
2831 | 0 | return SZ_ERROR_OUTPUT_EOF; |
2832 | | |
2833 | 20.2k | res = LzmaEnc_CodeOneBlock(p, desiredPackSize, *unpackSize); |
2834 | | |
2835 | 20.2k | *unpackSize = (UInt32)(p->nowPos64 - nowPos64); |
2836 | 20.2k | *destLen -= outStream.rem; |
2837 | 20.2k | if (outStream.overflow) |
2838 | 0 | return SZ_ERROR_OUTPUT_EOF; |
2839 | | |
2840 | 20.2k | return res; |
2841 | 20.2k | } |
2842 | | |
2843 | | |
2844 | | static SRes LzmaEnc_Encode2(CLzmaEnc *p, ICompressProgress *progress) |
2845 | 0 | { |
2846 | 0 | SRes res = SZ_OK; |
2847 | |
|
2848 | | #ifndef _7ZIP_ST |
2849 | | Byte allocaDummy[0x300]; |
2850 | | allocaDummy[0] = 0; |
2851 | | allocaDummy[1] = allocaDummy[0]; |
2852 | | #endif |
2853 | |
|
2854 | 0 | for (;;) |
2855 | 0 | { |
2856 | 0 | res = LzmaEnc_CodeOneBlock(p, 0, 0); |
2857 | 0 | if (res != SZ_OK || p->finished) |
2858 | 0 | break; |
2859 | 0 | if (progress) |
2860 | 0 | { |
2861 | 0 | res = ICompressProgress_Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc)); |
2862 | 0 | if (res != SZ_OK) |
2863 | 0 | { |
2864 | 0 | res = SZ_ERROR_PROGRESS; |
2865 | 0 | break; |
2866 | 0 | } |
2867 | 0 | } |
2868 | 0 | } |
2869 | | |
2870 | 0 | LzmaEnc_Finish(p); |
2871 | | |
2872 | | /* |
2873 | | if (res == SZ_OK && !Inline_MatchFinder_IsFinishedOK(&p->matchFinderBase)) |
2874 | | res = SZ_ERROR_FAIL; |
2875 | | } |
2876 | | */ |
2877 | |
|
2878 | 0 | return res; |
2879 | 0 | } |
2880 | | |
2881 | | |
2882 | | SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress, |
2883 | | ISzAllocPtr alloc, ISzAllocPtr allocBig) |
2884 | 0 | { |
2885 | 0 | RINOK(LzmaEnc_Prepare(pp, outStream, inStream, alloc, allocBig)); |
2886 | 0 | return LzmaEnc_Encode2((CLzmaEnc *)pp, progress); |
2887 | 0 | } |
2888 | | |
2889 | | |
2890 | | SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte *props, SizeT *size) |
2891 | 7.77k | { |
2892 | 7.77k | CLzmaEnc *p = (CLzmaEnc *)pp; |
2893 | 7.77k | unsigned i; |
2894 | 7.77k | UInt32 dictSize = p->dictSize; |
2895 | 7.77k | if (*size < LZMA_PROPS_SIZE) |
2896 | 0 | return SZ_ERROR_PARAM; |
2897 | 7.77k | *size = LZMA_PROPS_SIZE; |
2898 | 7.77k | props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc); |
2899 | | |
2900 | 7.77k | if (dictSize >= ((UInt32)1 << 22)) |
2901 | 7.77k | { |
2902 | 7.77k | UInt32 kDictMask = ((UInt32)1 << 20) - 1; |
2903 | 7.77k | if (dictSize < (UInt32)0xFFFFFFFF - kDictMask) |
2904 | 7.77k | dictSize = (dictSize + kDictMask) & ~kDictMask; |
2905 | 7.77k | } |
2906 | 0 | else for (i = 11; i <= 30; i++) |
2907 | 0 | { |
2908 | 0 | if (dictSize <= ((UInt32)2 << i)) { dictSize = (2 << i); break; } |
2909 | 0 | if (dictSize <= ((UInt32)3 << i)) { dictSize = (3 << i); break; } |
2910 | 0 | } |
2911 | | |
2912 | 38.8k | for (i = 0; i < 4; i++) |
2913 | 31.1k | props[1 + i] = (Byte)(dictSize >> (8 * i)); |
2914 | 7.77k | return SZ_OK; |
2915 | 7.77k | } |
2916 | | |
2917 | | |
2918 | | unsigned LzmaEnc_IsWriteEndMark(CLzmaEncHandle pp) |
2919 | 0 | { |
2920 | 0 | return ((CLzmaEnc *)pp)->writeEndMark; |
2921 | 0 | } |
2922 | | |
2923 | | |
2924 | | SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, |
2925 | | int writeEndMark, ICompressProgress *progress, ISzAllocPtr alloc, ISzAllocPtr allocBig) |
2926 | 0 | { |
2927 | 0 | SRes res; |
2928 | 0 | CLzmaEnc *p = (CLzmaEnc *)pp; |
2929 | |
|
2930 | 0 | CLzmaEnc_SeqOutStreamBuf outStream; |
2931 | |
|
2932 | 0 | outStream.vt.Write = SeqOutStreamBuf_Write; |
2933 | 0 | outStream.data = dest; |
2934 | 0 | outStream.rem = *destLen; |
2935 | 0 | outStream.overflow = False; |
2936 | |
|
2937 | 0 | p->writeEndMark = writeEndMark; |
2938 | 0 | p->rc.outStream = &outStream.vt; |
2939 | |
|
2940 | 0 | res = LzmaEnc_MemPrepare(pp, src, srcLen, 0, alloc, allocBig); |
2941 | | |
2942 | 0 | if (res == SZ_OK) |
2943 | 0 | { |
2944 | 0 | res = LzmaEnc_Encode2(p, progress); |
2945 | 0 | if (res == SZ_OK && p->nowPos64 != srcLen) |
2946 | 0 | res = SZ_ERROR_FAIL; |
2947 | 0 | } |
2948 | |
|
2949 | 0 | *destLen -= outStream.rem; |
2950 | 0 | if (outStream.overflow) |
2951 | 0 | return SZ_ERROR_OUTPUT_EOF; |
2952 | 0 | return res; |
2953 | 0 | } |
2954 | | |
2955 | | |
2956 | | SRes LzmaEncode(Byte *dest, SizeT *destLen, const Byte *src, SizeT srcLen, |
2957 | | const CLzmaEncProps *props, Byte *propsEncoded, SizeT *propsSize, int writeEndMark, |
2958 | | ICompressProgress *progress, ISzAllocPtr alloc, ISzAllocPtr allocBig) |
2959 | 0 | { |
2960 | 0 | CLzmaEnc *p = (CLzmaEnc *)LzmaEnc_Create(alloc); |
2961 | 0 | SRes res; |
2962 | 0 | if (!p) |
2963 | 0 | return SZ_ERROR_MEM; |
2964 | | |
2965 | 0 | res = LzmaEnc_SetProps(p, props); |
2966 | 0 | if (res == SZ_OK) |
2967 | 0 | { |
2968 | 0 | res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize); |
2969 | 0 | if (res == SZ_OK) |
2970 | 0 | res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen, |
2971 | 0 | writeEndMark, progress, alloc, allocBig); |
2972 | 0 | } |
2973 | |
|
2974 | 0 | LzmaEnc_Destroy(p, alloc, allocBig); |
2975 | 0 | return res; |
2976 | 0 | } |