~ubuntu-branches/ubuntu/trusty/grub2/trusty-updates

« back to all changes in this revision

Viewing changes to grub-core/lib/LzmaEnc.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2011-02-08 11:39:26 UTC
  • mfrom: (17.6.26 experimental)
  • mto: (17.6.27 experimental)
  • mto: This revision was merged to the branch mainline in revision 104.
  • Revision ID: james.westby@ubuntu.com-20110208113926-clfs90haboyk9zip
Tags: 1.99~rc1-2
* Merge 1.98+20100804-13 and 1.98+20100804-14, updating translations:
  - Kazakh (Baurzhan Muftakhidinov / Timur Birsh).
* mkconfig_skip_dmcrypt.patch: Refer to GRUB_PRELOAD_MODULES rather than
  suggesting people write a /etc/grub.d/01_modules script (thanks, Jordan
  Uggla).
* Handle empty dir passed to grub_find_root_device_from_mountinfo; fixes
  grub-mkrelpath on btrfs subvolumes (LP: #712029).
* Add rootflags=subvol=<name> if / is on a btrfs subvolume (LP: #712029).
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

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