1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
|
// Rar3Decoder.cpp
// According to unRAR license, this code may not be used to develop
// a program that creates RAR archives
#include "StdAfx.h"
#include "../Common/StreamUtils.h"
#include "Rar3Decoder.h"
namespace NCompress {
namespace NRar3 {
static const UInt32 kNumAlignReps = 15;
static const UInt32 kSymbolReadTable = 256;
static const UInt32 kSymbolRep = 259;
static const UInt32 kSymbolLen2 = kSymbolRep + kNumReps;
static const Byte kLenStart[kLenTableSize] = {0,1,2,3,4,5,6,7,8,10,12,14,16,20,24,28,32,40,48,56,64,80,96,112,128,160,192,224};
static const Byte kLenDirectBits[kLenTableSize] = {0,0,0,0,0,0,0,0,1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5};
static const Byte kDistDirectBits[kDistTableSize] =
{0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,
16,16,16,16,16,16,16,16,16,16,16,16,16,16,
18,18,18,18,18,18,18,18,18,18,18,18};
static const Byte kLevelDirectBits[kLevelTableSize] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
static const Byte kLen2DistStarts[kNumLen2Symbols]={0,4,8,16,32,64,128,192};
static const Byte kLen2DistDirectBits[kNumLen2Symbols]={2,2,3, 4, 5, 6, 6, 6};
static const UInt32 kDistLimit3 = 0x2000 - 2;
static const UInt32 kDistLimit4 = 0x40000 - 2;
static const UInt32 kNormalMatchMinLen = 3;
static const UInt32 kVmDataSizeMax = 1 << 16;
static const UInt32 kVmCodeSizeMax = 1 << 16;
CDecoder::CDecoder():
_window(0),
_winPos(0),
_wrPtr(0),
_lzSize(0),
_writtenFileSize(0),
_vmData(0),
_vmCode(0),
m_IsSolid(false)
{
}
CDecoder::~CDecoder()
{
InitFilters();
::MidFree(_vmData);
::MidFree(_window);
}
HRESULT CDecoder::WriteDataToStream(const Byte *data, UInt32 size)
{
return WriteStream(_outStream, data, size);
}
HRESULT CDecoder::WriteData(const Byte *data, UInt32 size)
{
HRESULT res = S_OK;
if (_writtenFileSize < _unpackSize)
{
UInt32 curSize = size;
UInt64 remain = _unpackSize - _writtenFileSize;
if (remain < curSize)
curSize = (UInt32)remain;
res = WriteDataToStream(data, curSize);
}
_writtenFileSize += size;
return res;
}
HRESULT CDecoder::WriteArea(UInt32 startPtr, UInt32 endPtr)
{
if (startPtr <= endPtr)
return WriteData(_window + startPtr, endPtr - startPtr);
RINOK(WriteData(_window + startPtr, kWindowSize - startPtr));
return WriteData(_window, endPtr);
}
void CDecoder::ExecuteFilter(int tempFilterIndex, NVm::CBlockRef &outBlockRef)
{
CTempFilter *tempFilter = _tempFilters[tempFilterIndex];
tempFilter->InitR[6] = (UInt32)_writtenFileSize;
NVm::SetValue32(&tempFilter->GlobalData[0x24], (UInt32)_writtenFileSize);
NVm::SetValue32(&tempFilter->GlobalData[0x28], (UInt32)(_writtenFileSize >> 32));
CFilter *filter = _filters[tempFilter->FilterIndex];
_vm.Execute(filter, tempFilter, outBlockRef, filter->GlobalData);
delete tempFilter;
_tempFilters[tempFilterIndex] = 0;
}
HRESULT CDecoder::WriteBuf()
{
UInt32 writtenBorder = _wrPtr;
UInt32 writeSize = (_winPos - writtenBorder) & kWindowMask;
for (int i = 0; i < _tempFilters.Size(); i++)
{
CTempFilter *filter = _tempFilters[i];
if (filter == NULL)
continue;
if (filter->NextWindow)
{
filter->NextWindow = false;
continue;
}
UInt32 blockStart = filter->BlockStart;
UInt32 blockSize = filter->BlockSize;
if (((blockStart - writtenBorder) & kWindowMask) < writeSize)
{
if (writtenBorder != blockStart)
{
RINOK(WriteArea(writtenBorder, blockStart));
writtenBorder = blockStart;
writeSize = (_winPos - writtenBorder) & kWindowMask;
}
if (blockSize <= writeSize)
{
UInt32 blockEnd = (blockStart + blockSize) & kWindowMask;
if (blockStart < blockEnd || blockEnd == 0)
_vm.SetMemory(0, _window + blockStart, blockSize);
else
{
UInt32 tailSize = kWindowSize - blockStart;
_vm.SetMemory(0, _window + blockStart, tailSize);
_vm.SetMemory(tailSize, _window, blockEnd);
}
NVm::CBlockRef outBlockRef;
ExecuteFilter(i, outBlockRef);
while (i + 1 < _tempFilters.Size())
{
CTempFilter *nextFilter = _tempFilters[i + 1];
if (nextFilter == NULL || nextFilter->BlockStart != blockStart ||
nextFilter->BlockSize != outBlockRef.Size || nextFilter->NextWindow)
break;
_vm.SetMemory(0, _vm.GetDataPointer(outBlockRef.Offset), outBlockRef.Size);
ExecuteFilter(++i, outBlockRef);
}
WriteDataToStream(_vm.GetDataPointer(outBlockRef.Offset), outBlockRef.Size);
_writtenFileSize += outBlockRef.Size;
writtenBorder = blockEnd;
writeSize = (_winPos - writtenBorder) & kWindowMask;
}
else
{
for (int j = i; j < _tempFilters.Size(); j++)
{
CTempFilter *filter = _tempFilters[j];
if (filter != NULL && filter->NextWindow)
filter->NextWindow = false;
}
_wrPtr = writtenBorder;
return S_OK; // check it
}
}
}
_wrPtr = _winPos;
return WriteArea(writtenBorder, _winPos);
}
void CDecoder::InitFilters()
{
_lastFilter = 0;
int i;
for (i = 0; i < _tempFilters.Size(); i++)
delete _tempFilters[i];
_tempFilters.Clear();
for (i = 0; i < _filters.Size(); i++)
delete _filters[i];
_filters.Clear();
}
bool CDecoder::AddVmCode(UInt32 firstByte, UInt32 codeSize)
{
CMemBitDecoder inp;
inp.Init(_vmData, codeSize);
UInt32 filterIndex;
if (firstByte & 0x80)
{
filterIndex = NVm::ReadEncodedUInt32(inp);
if (filterIndex == 0)
InitFilters();
else
filterIndex--;
}
else
filterIndex = _lastFilter;
if (filterIndex > (UInt32)_filters.Size())
return false;
_lastFilter = filterIndex;
bool newFilter = (filterIndex == (UInt32)_filters.Size());
CFilter *filter;
if (newFilter)
{
// check if too many filters
if (filterIndex > 1024)
return false;
filter = new CFilter;
_filters.Add(filter);
}
else
{
filter = _filters[filterIndex];
filter->ExecCount++;
}
int numEmptyItems = 0;
int i;
for (i = 0; i < _tempFilters.Size(); i++)
{
_tempFilters[i - numEmptyItems] = _tempFilters[i];
if (_tempFilters[i] == NULL)
numEmptyItems++;
if (numEmptyItems > 0)
_tempFilters[i] = NULL;
}
if (numEmptyItems == 0)
{
_tempFilters.Add(NULL);
numEmptyItems = 1;
}
CTempFilter *tempFilter = new CTempFilter;
_tempFilters[_tempFilters.Size() - numEmptyItems] = tempFilter;
tempFilter->FilterIndex = filterIndex;
tempFilter->ExecCount = filter->ExecCount;
UInt32 blockStart = NVm::ReadEncodedUInt32(inp);
if (firstByte & 0x40)
blockStart += 258;
tempFilter->BlockStart = (blockStart + _winPos) & kWindowMask;
if (firstByte & 0x20)
filter->BlockSize = NVm::ReadEncodedUInt32(inp);
tempFilter->BlockSize = filter->BlockSize;
tempFilter->NextWindow = _wrPtr != _winPos && ((_wrPtr - _winPos) & kWindowMask) <= blockStart;
memset(tempFilter->InitR, 0, sizeof(tempFilter->InitR));
tempFilter->InitR[3] = NVm::kGlobalOffset;
tempFilter->InitR[4] = tempFilter->BlockSize;
tempFilter->InitR[5] = tempFilter->ExecCount;
if (firstByte & 0x10)
{
UInt32 initMask = inp.ReadBits(NVm::kNumGpRegs);
for (int i = 0; i < NVm::kNumGpRegs; i++)
if (initMask & (1 << i))
tempFilter->InitR[i] = NVm::ReadEncodedUInt32(inp);
}
if (newFilter)
{
UInt32 vmCodeSize = NVm::ReadEncodedUInt32(inp);
if (vmCodeSize >= kVmCodeSizeMax || vmCodeSize == 0)
return false;
for (UInt32 i = 0; i < vmCodeSize; i++)
_vmCode[i] = (Byte)inp.ReadBits(8);
_vm.PrepareProgram(_vmCode, vmCodeSize, filter);
}
tempFilter->AllocateEmptyFixedGlobal();
Byte *globalData = &tempFilter->GlobalData[0];
for (i = 0; i < NVm::kNumGpRegs; i++)
NVm::SetValue32(&globalData[i * 4], tempFilter->InitR[i]);
NVm::SetValue32(&globalData[NVm::NGlobalOffset::kBlockSize], tempFilter->BlockSize);
NVm::SetValue32(&globalData[NVm::NGlobalOffset::kBlockPos], 0); // It was commented. why?
NVm::SetValue32(&globalData[NVm::NGlobalOffset::kExecCount], tempFilter->ExecCount);
if (firstByte & 8)
{
UInt32 dataSize = NVm::ReadEncodedUInt32(inp);
if (dataSize > NVm::kGlobalSize - NVm::kFixedGlobalSize)
return false;
CRecordVector<Byte> &globalData = tempFilter->GlobalData;
int requredSize = (int)(dataSize + NVm::kFixedGlobalSize);
if (globalData.Size() < requredSize)
{
globalData.Reserve(requredSize);
for (; globalData.Size() < requredSize; i++)
globalData.Add(0);
}
for (UInt32 i = 0; i < dataSize; i++)
globalData[NVm::kFixedGlobalSize + i] = (Byte)inp.ReadBits(8);
}
return true;
}
bool CDecoder::ReadVmCodeLZ()
{
UInt32 firstByte = m_InBitStream.ReadBits(8);
UInt32 length = (firstByte & 7) + 1;
if (length == 7)
length = m_InBitStream.ReadBits(8) + 7;
else if (length == 8)
length = m_InBitStream.ReadBits(16);
if (length > kVmDataSizeMax)
return false;
for (UInt32 i = 0; i < length; i++)
_vmData[i] = (Byte)m_InBitStream.ReadBits(8);
return AddVmCode(firstByte, length);
}
bool CDecoder::ReadVmCodePPM()
{
int firstByte = DecodePpmSymbol();
if (firstByte == -1)
return false;
UInt32 length = (firstByte & 7) + 1;
if (length == 7)
{
int b1 = DecodePpmSymbol();
if (b1 == -1)
return false;
length = b1 + 7;
}
else if (length == 8)
{
int b1 = DecodePpmSymbol();
if (b1 == -1)
return false;
int b2 = DecodePpmSymbol();
if (b2 == -1)
return false;
length = b1 * 256 + b2;
}
if (length > kVmDataSizeMax)
return false;
for (UInt32 i = 0; i < length; i++)
{
int b = DecodePpmSymbol();
if (b == -1)
return false;
_vmData[i] = (Byte)b;
}
return AddVmCode(firstByte, length);
}
#define RIF(x) { if (!(x)) return S_FALSE; }
UInt32 CDecoder::ReadBits(int numBits) { return m_InBitStream.ReadBits(numBits); }
/////////////////////////////////////////////////
// PPM
HRESULT CDecoder::InitPPM()
{
Byte maxOrder = (Byte)ReadBits(7);
bool reset = ((maxOrder & 0x20) != 0);
int maxMB = 0;
if (reset)
maxMB = (Byte)ReadBits(8);
else
{
if (_ppm.SubAllocator.GetSubAllocatorSize()== 0)
return S_FALSE;
}
if (maxOrder & 0x40)
PpmEscChar = (Byte)ReadBits(8);
m_InBitStream.InitRangeCoder();
/*
if (m_InBitStream.m_BitPos != 0)
return S_FALSE;
*/
if (reset)
{
maxOrder = (maxOrder & 0x1F) + 1;
if (maxOrder > 16)
maxOrder = 16 + (maxOrder - 16) * 3;
if (maxOrder == 1)
{
// SubAlloc.StopSubAllocator();
_ppm.SubAllocator.StopSubAllocator();
return S_FALSE;
}
// SubAlloc.StartSubAllocator(MaxMB+1);
// StartModelRare(maxOrder);
if (!_ppm.SubAllocator.StartSubAllocator((maxMB + 1) << 20))
return E_OUTOFMEMORY;
_ppm.MaxOrder = 0;
_ppm.StartModelRare(maxOrder);
}
// return (minContext != NULL);
return S_OK;
}
int CDecoder::DecodePpmSymbol() { return _ppm.DecodeSymbol(&m_InBitStream); }
HRESULT CDecoder::DecodePPM(Int32 num, bool &keepDecompressing)
{
keepDecompressing = false;
do
{
if (((_wrPtr - _winPos) & kWindowMask) < 260 && _wrPtr != _winPos)
{
RINOK(WriteBuf());
if (_writtenFileSize > _unpackSize)
{
keepDecompressing = false;
return S_OK;
}
}
int c = DecodePpmSymbol();
if (c == -1)
{
// Original code sets PPMError=true here and then it returns S_OK. Why ???
// return S_OK;
return S_FALSE;
}
if (c == PpmEscChar)
{
int nextCh = DecodePpmSymbol();
if (nextCh == 0)
return ReadTables(keepDecompressing);
if (nextCh == 2 || nextCh == -1)
return S_OK;
if (nextCh == 3)
{
if (!ReadVmCodePPM())
return S_FALSE;
continue;
}
if (nextCh == 4 || nextCh == 5)
{
UInt32 distance = 0;
UInt32 length = 4;
if (nextCh == 4)
{
for (int i = 0; i < 3; i++)
{
int c = DecodePpmSymbol();
if (c == -1)
return S_OK;
distance = (distance << 8) + (Byte)c;
}
distance++;
length += 28;
}
int c = DecodePpmSymbol();
if (c == -1)
return S_OK;
length += c;
if (distance >= _lzSize)
return S_FALSE;
CopyBlock(distance, length);
num -= (Int32)length;
continue;
}
}
PutByte((Byte)c);
num--;
}
while (num >= 0);
keepDecompressing = true;
return S_OK;
}
/////////////////////////////////////////////////
// LZ
HRESULT CDecoder::ReadTables(bool &keepDecompressing)
{
keepDecompressing = true;
ReadBits((8 - m_InBitStream.GetBitPosition()) & 7);
if (ReadBits(1) != 0)
{
_lzMode = false;
return InitPPM();
}
_lzMode = true;
PrevAlignBits = 0;
PrevAlignCount = 0;
Byte levelLevels[kLevelTableSize];
Byte newLevels[kTablesSizesSum];
if (ReadBits(1) == 0)
memset(m_LastLevels, 0, kTablesSizesSum);
int i;
for (i = 0; i < kLevelTableSize; i++)
{
UInt32 length = ReadBits(4);
if (length == 15)
{
UInt32 zeroCount = ReadBits(4);
if (zeroCount != 0)
{
zeroCount += 2;
while (zeroCount-- > 0 && i < kLevelTableSize)
levelLevels[i++]=0;
i--;
continue;
}
}
levelLevels[i] = (Byte)length;
}
RIF(m_LevelDecoder.SetCodeLengths(levelLevels));
i = 0;
while (i < kTablesSizesSum)
{
UInt32 number = m_LevelDecoder.DecodeSymbol(&m_InBitStream);
if (number < 16)
{
newLevels[i] = Byte((number + m_LastLevels[i]) & 15);
i++;
}
else if (number > kLevelTableSize)
return S_FALSE;
else
{
int num;
if (((number - 16) & 1) == 0)
num = ReadBits(3) + 3;
else
num = ReadBits(7) + 11;
if (number < 18)
{
if (i == 0)
return S_FALSE;
for (; num > 0 && i < kTablesSizesSum; num--, i++)
newLevels[i] = newLevels[i - 1];
}
else
{
for (; num > 0 && i < kTablesSizesSum; num--)
newLevels[i++] = 0;
}
}
}
TablesRead = true;
// original code has check here:
/*
if (InAddr > ReadTop)
{
keepDecompressing = false;
return true;
}
*/
RIF(m_MainDecoder.SetCodeLengths(&newLevels[0]));
RIF(m_DistDecoder.SetCodeLengths(&newLevels[kMainTableSize]));
RIF(m_AlignDecoder.SetCodeLengths(&newLevels[kMainTableSize + kDistTableSize]));
RIF(m_LenDecoder.SetCodeLengths(&newLevels[kMainTableSize + kDistTableSize + kAlignTableSize]));
memcpy(m_LastLevels, newLevels, kTablesSizesSum);
return S_OK;
}
class CCoderReleaser
{
CDecoder *m_Coder;
public:
CCoderReleaser(CDecoder *coder): m_Coder(coder) {}
~CCoderReleaser()
{
// m_Coder->m_OutWindowStream.Flush();
m_Coder->ReleaseStreams();
}
};
HRESULT CDecoder::ReadEndOfBlock(bool &keepDecompressing)
{
if (ReadBits(1) != 0)
{
// old file
TablesRead = false;
return ReadTables(keepDecompressing);
}
// new file
keepDecompressing = false;
TablesRead = (ReadBits(1) == 0);
return S_OK;
}
UInt32 kDistStart[kDistTableSize];
class CDistInit
{
public:
CDistInit() { Init(); }
void Init()
{
UInt32 start = 0;
for (UInt32 i = 0; i < kDistTableSize; i++)
{
kDistStart[i] = start;
start += (1 << kDistDirectBits[i]);
}
}
} g_DistInit;
HRESULT CDecoder::DecodeLZ(bool &keepDecompressing)
{
UInt32 rep0 = _reps[0];
UInt32 rep1 = _reps[1];
UInt32 rep2 = _reps[2];
UInt32 rep3 = _reps[3];
UInt32 length = _lastLength;
for (;;)
{
if (((_wrPtr - _winPos) & kWindowMask) < 260 && _wrPtr != _winPos)
{
RINOK(WriteBuf());
if (_writtenFileSize > _unpackSize)
{
keepDecompressing = false;
return S_OK;
}
}
UInt32 number = m_MainDecoder.DecodeSymbol(&m_InBitStream);
if (number < 256)
{
PutByte((Byte)number);
continue;
}
else if (number == kSymbolReadTable)
{
RINOK(ReadEndOfBlock(keepDecompressing));
break;
}
else if (number == 257)
{
if (!ReadVmCodeLZ())
return S_FALSE;
continue;
}
else if (number == 258)
{
if (length == 0)
return S_FALSE;
}
else if (number < kSymbolRep + 4)
{
if (number != kSymbolRep)
{
UInt32 distance;
if (number == kSymbolRep + 1)
distance = rep1;
else
{
if (number == kSymbolRep + 2)
distance = rep2;
else
{
distance = rep3;
rep3 = rep2;
}
rep2 = rep1;
}
rep1 = rep0;
rep0 = distance;
}
UInt32 number = m_LenDecoder.DecodeSymbol(&m_InBitStream);
if (number >= kLenTableSize)
return S_FALSE;
length = 2 + kLenStart[number] + m_InBitStream.ReadBits(kLenDirectBits[number]);
}
else
{
rep3 = rep2;
rep2 = rep1;
rep1 = rep0;
if (number < 271)
{
number -= 263;
rep0 = kLen2DistStarts[number] + m_InBitStream.ReadBits(kLen2DistDirectBits[number]);
length = 2;
}
else if (number < 299)
{
number -= 271;
length = kNormalMatchMinLen + (UInt32)kLenStart[number] + m_InBitStream.ReadBits(kLenDirectBits[number]);
UInt32 number = m_DistDecoder.DecodeSymbol(&m_InBitStream);
if (number >= kDistTableSize)
return S_FALSE;
rep0 = kDistStart[number];
int numBits = kDistDirectBits[number];
if (number >= (kNumAlignBits * 2) + 2)
{
if (numBits > kNumAlignBits)
rep0 += (m_InBitStream.ReadBits(numBits - kNumAlignBits) << kNumAlignBits);
if (PrevAlignCount > 0)
{
PrevAlignCount--;
rep0 += PrevAlignBits;
}
else
{
UInt32 number = m_AlignDecoder.DecodeSymbol(&m_InBitStream);
if (number < (1 << kNumAlignBits))
{
rep0 += number;
PrevAlignBits = number;
}
else if (number == (1 << kNumAlignBits))
{
PrevAlignCount = kNumAlignReps;
rep0 += PrevAlignBits;
}
else
return S_FALSE;
}
}
else
rep0 += m_InBitStream.ReadBits(numBits);
length += ((kDistLimit4 - rep0) >> 31) + ((kDistLimit3 - rep0) >> 31);
}
else
return S_FALSE;
}
if (rep0 >= _lzSize)
return S_FALSE;
CopyBlock(rep0, length);
}
_reps[0] = rep0;
_reps[1] = rep1;
_reps[2] = rep2;
_reps[3] = rep3;
_lastLength = length;
return S_OK;
}
HRESULT CDecoder::CodeReal(ICompressProgressInfo *progress)
{
_writtenFileSize = 0;
if (!m_IsSolid)
{
_lzSize = 0;
_winPos = 0;
_wrPtr = 0;
for (int i = 0; i < kNumReps; i++)
_reps[i] = 0;
_lastLength = 0;
memset(m_LastLevels, 0, kTablesSizesSum);
TablesRead = false;
PpmEscChar = 2;
InitFilters();
}
if (!m_IsSolid || !TablesRead)
{
bool keepDecompressing;
RINOK(ReadTables(keepDecompressing));
if (!keepDecompressing)
return S_OK;
}
for(;;)
{
bool keepDecompressing;
if (_lzMode)
{
RINOK(DecodeLZ(keepDecompressing))
}
else
{
RINOK(DecodePPM(1 << 18, keepDecompressing))
}
UInt64 packSize = m_InBitStream.GetProcessedSize();
RINOK(progress->SetRatioInfo(&packSize, &_writtenFileSize));
if (!keepDecompressing)
break;
}
RINOK(WriteBuf());
if (_writtenFileSize < _unpackSize)
return S_FALSE;
// return m_OutWindowStream.Flush();
return S_OK;
}
STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, ISequentialOutStream *outStream,
const UInt64 *inSize, const UInt64 *outSize, ICompressProgressInfo *progress)
{
try
{
if (inSize == NULL || outSize == NULL)
return E_INVALIDARG;
if (_vmData == 0)
{
_vmData = (Byte *)::MidAlloc(kVmDataSizeMax + kVmCodeSizeMax);
if (_vmData == 0)
return E_OUTOFMEMORY;
_vmCode = _vmData + kVmDataSizeMax;
}
if (_window == 0)
{
_window = (Byte *)::MidAlloc(kWindowSize);
if (_window == 0)
return E_OUTOFMEMORY;
}
if (!m_InBitStream.Create(1 << 20))
return E_OUTOFMEMORY;
if (!_vm.Create())
return E_OUTOFMEMORY;
m_InBitStream.SetStream(inStream);
m_InBitStream.Init();
_outStream = outStream;
CCoderReleaser coderReleaser(this);
_unpackSize = *outSize;
return CodeReal(progress);
}
catch(const CInBufferException &e) { return e.ErrorCode; }
catch(...) { return S_FALSE; }
// CNewException is possible here. But probably CNewException is caused
// by error in data stream.
}
STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size)
{
if (size < 1)
return E_INVALIDARG;
m_IsSolid = (data[0] != 0);
return S_OK;
}
}}
|