forked from svn2github/dotnetzip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBZip2InputStream.cs
More file actions
1447 lines (1255 loc) · 46.4 KB
/
Copy pathBZip2InputStream.cs
File metadata and controls
1447 lines (1255 loc) · 46.4 KB
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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// BZip2InputStream.cs
// ------------------------------------------------------------------
//
// Copyright (c) 2011 Dino Chiesa.
// All rights reserved.
//
// This code module is part of DotNetZip, a zipfile class library.
//
// ------------------------------------------------------------------
//
// This code is licensed under the Microsoft Public License.
// See the file License.txt for the license details.
// More info on: http://dotnetzip.codeplex.com
//
// ------------------------------------------------------------------
//
// Last Saved: <2011-July-31 11:57:32>
//
// ------------------------------------------------------------------
//
// This module defines the BZip2InputStream class, which is a decompressing
// stream that handles BZIP2. This code is derived from Apache commons source code.
// The license below applies to the original Apache code.
//
// ------------------------------------------------------------------
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* This package is based on the work done by Keiron Liddle, Aftex Software
* <keiron@aftexsw.com> to whom the Ant project is very grateful for his
* great code.
*/
// compile: msbuild
// not: csc.exe /t:library /debug+ /out:Ionic.BZip2.dll BZip2InputStream.cs BCRC32.cs Rand.cs
using System;
using System.IO;
namespace Ionic.BZip2
{
/// <summary>
/// A read-only decorator stream that performs BZip2 decompression on Read.
/// </summary>
public class BZip2InputStream : System.IO.Stream
{
bool _disposed;
bool _leaveOpen;
Int64 totalBytesRead;
private int last;
/* for undoing the Burrows-Wheeler transform */
private int origPtr;
// blockSize100k: 0 .. 9.
//
// This var name is a misnomer. The actual block size is 100000
// * blockSize100k. (not 100k * blocksize100k)
private int blockSize100k;
private bool blockRandomised;
private int bsBuff;
private int bsLive;
private readonly Ionic.Crc.CRC32 crc = new Ionic.Crc.CRC32(true);
private int nInUse;
private Stream input;
private int currentChar = -1;
/// <summary>
/// Compressor State
/// </summary>
enum CState
{
EOF = 0,
START_BLOCK = 1,
RAND_PART_A = 2,
RAND_PART_B = 3,
RAND_PART_C = 4,
NO_RAND_PART_A = 5,
NO_RAND_PART_B = 6,
NO_RAND_PART_C = 7,
}
private CState currentState = CState.START_BLOCK;
private uint storedBlockCRC, storedCombinedCRC;
private uint computedBlockCRC, computedCombinedCRC;
// Variables used by setup* methods exclusively
private int su_count;
private int su_ch2;
private int su_chPrev;
private int su_i2;
private int su_j2;
private int su_rNToGo;
private int su_rTPos;
private int su_tPos;
private char su_z;
private BZip2InputStream.DecompressionState data;
/// <summary>
/// Create a BZip2InputStream, wrapping it around the given input Stream.
/// </summary>
/// <remarks>
/// <para>
/// The input stream will be closed when the BZip2InputStream is closed.
/// </para>
/// </remarks>
/// <param name='input'>The stream from which to read compressed data</param>
public BZip2InputStream(Stream input)
: this(input, false)
{}
/// <summary>
/// Create a BZip2InputStream with the given stream, and
/// specifying whether to leave the wrapped stream open when
/// the BZip2InputStream is closed.
/// </summary>
/// <param name='input'>The stream from which to read compressed data</param>
/// <param name='leaveOpen'>
/// Whether to leave the input stream open, when the BZip2InputStream closes.
/// </param>
///
/// <example>
///
/// This example reads a bzip2-compressed file, decompresses it,
/// and writes the decompressed data into a newly created file.
///
/// <code>
/// var fname = "logfile.log.bz2";
/// using (var fs = File.OpenRead(fname))
/// {
/// using (var decompressor = new Ionic.BZip2.BZip2InputStream(fs))
/// {
/// var outFname = fname + ".decompressed";
/// using (var output = File.Create(outFname))
/// {
/// byte[] buffer = new byte[2048];
/// int n;
/// while ((n = decompressor.Read(buffer, 0, buffer.Length)) > 0)
/// {
/// output.Write(buffer, 0, n);
/// }
/// }
/// }
/// }
/// </code>
/// </example>
public BZip2InputStream(Stream input, bool leaveOpen)
: base()
{
this.input = input;
this._leaveOpen = leaveOpen;
init();
}
/// <summary>
/// Read data from the stream.
/// </summary>
///
/// <remarks>
/// <para>
/// To decompress a BZip2 data stream, create a <c>BZip2InputStream</c>,
/// providing a stream that reads compressed data. Then call Read() on
/// that <c>BZip2InputStream</c>, and the data read will be decompressed
/// as you read.
/// </para>
///
/// <para>
/// A <c>BZip2InputStream</c> can be used only for <c>Read()</c>, not for <c>Write()</c>.
/// </para>
/// </remarks>
///
/// <param name="buffer">The buffer into which the read data should be placed.</param>
/// <param name="offset">the offset within that data array to put the first byte read.</param>
/// <param name="count">the number of bytes to read.</param>
/// <returns>the number of bytes actually read</returns>
public override int Read(byte[] buffer, int offset, int count)
{
if (offset < 0)
throw new IndexOutOfRangeException(String.Format("offset ({0}) must be > 0", offset));
if (count < 0)
throw new IndexOutOfRangeException(String.Format("count ({0}) must be > 0", count));
if (offset + count > buffer.Length)
throw new IndexOutOfRangeException(String.Format("offset({0}) count({1}) bLength({2})",
offset, count, buffer.Length));
if (this.input == null)
throw new IOException("the stream is not open");
int hi = offset + count;
int destOffset = offset;
for (int b; (destOffset < hi) && ((b = ReadByte()) >= 0);)
{
buffer[destOffset++] = (byte) b;
}
return (destOffset == offset) ? -1 : (destOffset - offset);
}
private void MakeMaps()
{
bool[] inUse = this.data.inUse;
byte[] seqToUnseq = this.data.seqToUnseq;
int n = 0;
for (int i = 0; i < 256; i++)
{
if (inUse[i])
seqToUnseq[n++] = (byte) i;
}
this.nInUse = n;
}
/// <summary>
/// Read a single byte from the stream.
/// </summary>
/// <returns>the byte read from the stream, or -1 if EOF</returns>
public override int ReadByte()
{
int retChar = this.currentChar;
totalBytesRead++;
switch (this.currentState)
{
case CState.EOF:
return -1;
case CState.START_BLOCK:
throw new IOException("bad state");
case CState.RAND_PART_A:
throw new IOException("bad state");
case CState.RAND_PART_B:
SetupRandPartB();
break;
case CState.RAND_PART_C:
SetupRandPartC();
break;
case CState.NO_RAND_PART_A:
throw new IOException("bad state");
case CState.NO_RAND_PART_B:
SetupNoRandPartB();
break;
case CState.NO_RAND_PART_C:
SetupNoRandPartC();
break;
default:
throw new IOException("bad state");
}
return retChar;
}
/// <summary>
/// Indicates whether the stream can be read.
/// </summary>
/// <remarks>
/// The return value depends on whether the captive stream supports reading.
/// </remarks>
public override bool CanRead
{
get
{
if (_disposed) throw new ObjectDisposedException("BZip2Stream");
return this.input.CanRead;
}
}
/// <summary>
/// Indicates whether the stream supports Seek operations.
/// </summary>
/// <remarks>
/// Always returns false.
/// </remarks>
public override bool CanSeek
{
get { return false; }
}
/// <summary>
/// Indicates whether the stream can be written.
/// </summary>
/// <remarks>
/// The return value depends on whether the captive stream supports writing.
/// </remarks>
public override bool CanWrite
{
get
{
if (_disposed) throw new ObjectDisposedException("BZip2Stream");
return input.CanWrite;
}
}
/// <summary>
/// Flush the stream.
/// </summary>
public override void Flush()
{
if (_disposed) throw new ObjectDisposedException("BZip2Stream");
input.Flush();
}
/// <summary>
/// Reading this property always throws a <see cref="NotImplementedException"/>.
/// </summary>
public override long Length
{
get { throw new NotImplementedException(); }
}
/// <summary>
/// The position of the stream pointer.
/// </summary>
///
/// <remarks>
/// Setting this property always throws a <see
/// cref="NotImplementedException"/>. Reading will return the
/// total number of uncompressed bytes read in.
/// </remarks>
public override long Position
{
get
{
return this.totalBytesRead;
}
set { throw new NotImplementedException(); }
}
/// <summary>
/// Calling this method always throws a <see cref="NotImplementedException"/>.
/// </summary>
/// <param name="offset">this is irrelevant, since it will always throw!</param>
/// <param name="origin">this is irrelevant, since it will always throw!</param>
/// <returns>irrelevant!</returns>
public override long Seek(long offset, System.IO.SeekOrigin origin)
{
throw new NotImplementedException();
}
/// <summary>
/// Calling this method always throws a <see cref="NotImplementedException"/>.
/// </summary>
/// <param name="value">this is irrelevant, since it will always throw!</param>
public override void SetLength(long value)
{
throw new NotImplementedException();
}
/// <summary>
/// Calling this method always throws a <see cref="NotImplementedException"/>.
/// </summary>
/// <param name='buffer'>this parameter is never used</param>
/// <param name='offset'>this parameter is never used</param>
/// <param name='count'>this parameter is never used</param>
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
/// <summary>
/// Dispose the stream.
/// </summary>
/// <param name="disposing">
/// indicates whether the Dispose method was invoked by user code.
/// </param>
protected override void Dispose(bool disposing)
{
try
{
if (!_disposed)
{
if (disposing && (this.input != null))
this.input.Close();
_disposed = true;
}
}
finally
{
base.Dispose(disposing);
}
}
void init()
{
if (null == this.input)
throw new IOException("No input Stream");
if (!this.input.CanRead)
throw new IOException("Unreadable input Stream");
CheckMagicChar('B', 0);
CheckMagicChar('Z', 1);
CheckMagicChar('h', 2);
int blockSize = this.input.ReadByte();
if ((blockSize < '1') || (blockSize > '9'))
throw new IOException("Stream is not BZip2 formatted: illegal "
+ "blocksize " + (char) blockSize);
this.blockSize100k = blockSize - '0';
InitBlock();
SetupBlock();
}
void CheckMagicChar(char expected, int position)
{
int magic = this.input.ReadByte();
if (magic != (int)expected)
{
var msg = String.Format("Not a valid BZip2 stream. byte {0}, expected '{1}', got '{2}'",
position, (int)expected, magic);
throw new IOException(msg);
}
}
void InitBlock()
{
char magic0 = bsGetUByte();
char magic1 = bsGetUByte();
char magic2 = bsGetUByte();
char magic3 = bsGetUByte();
char magic4 = bsGetUByte();
char magic5 = bsGetUByte();
if (magic0 == 0x17 && magic1 == 0x72 && magic2 == 0x45
&& magic3 == 0x38 && magic4 == 0x50 && magic5 == 0x90)
{
complete(); // end of file
}
else if (magic0 != 0x31 ||
magic1 != 0x41 ||
magic2 != 0x59 ||
magic3 != 0x26 ||
magic4 != 0x53 ||
magic5 != 0x59)
{
this.currentState = CState.EOF;
var msg = String.Format("bad block header at offset 0x{0:X}",
this.input.Position);
throw new IOException(msg);
}
else
{
this.storedBlockCRC = bsGetInt();
// Console.WriteLine(" stored block CRC : {0:X8}", this.storedBlockCRC);
this.blockRandomised = (GetBits(1) == 1);
// Lazily allocate data
if (this.data == null)
this.data = new DecompressionState(this.blockSize100k);
// currBlockNo++;
getAndMoveToFrontDecode();
this.crc.Reset();
this.currentState = CState.START_BLOCK;
}
}
private void EndBlock()
{
this.computedBlockCRC = (uint)this.crc.Crc32Result;
// A bad CRC is considered a fatal error.
if (this.storedBlockCRC != this.computedBlockCRC)
{
// make next blocks readable without error
// (repair feature, not yet documented, not tested)
// this.computedCombinedCRC = (this.storedCombinedCRC << 1)
// | (this.storedCombinedCRC >> 31);
// this.computedCombinedCRC ^= this.storedBlockCRC;
var msg = String.Format("BZip2 CRC error (expected {0:X8}, computed {1:X8})",
this.storedBlockCRC, this.computedBlockCRC);
throw new IOException(msg);
}
// Console.WriteLine(" combined CRC (before): {0:X8}", this.computedCombinedCRC);
this.computedCombinedCRC = (this.computedCombinedCRC << 1)
| (this.computedCombinedCRC >> 31);
this.computedCombinedCRC ^= this.computedBlockCRC;
// Console.WriteLine(" computed block CRC : {0:X8}", this.computedBlockCRC);
// Console.WriteLine(" combined CRC (after) : {0:X8}", this.computedCombinedCRC);
// Console.WriteLine();
}
private void complete()
{
this.storedCombinedCRC = bsGetInt();
this.currentState = CState.EOF;
this.data = null;
if (this.storedCombinedCRC != this.computedCombinedCRC)
{
var msg = String.Format("BZip2 CRC error (expected {0:X8}, computed {1:X8})",
this.storedCombinedCRC, this.computedCombinedCRC);
throw new IOException(msg);
}
}
/// <summary>
/// Close the stream.
/// </summary>
public override void Close()
{
Stream inShadow = this.input;
if (inShadow != null)
{
try
{
if (!this._leaveOpen)
inShadow.Close();
}
finally
{
this.data = null;
this.input = null;
}
}
}
/// <summary>
/// Read n bits from input, right justifying the result.
/// </summary>
/// <remarks>
/// <para>
/// For example, if you read 1 bit, the result is either 0
/// or 1.
/// </para>
/// </remarks>
/// <param name ="n">
/// The number of bits to read, always between 1 and 32.
/// </param>
private int GetBits(int n)
{
int bsLiveShadow = this.bsLive;
int bsBuffShadow = this.bsBuff;
if (bsLiveShadow < n)
{
do
{
int thech = this.input.ReadByte();
if (thech < 0)
throw new IOException("unexpected end of stream");
// Console.WriteLine("R {0:X2}", thech);
bsBuffShadow = (bsBuffShadow << 8) | thech;
bsLiveShadow += 8;
} while (bsLiveShadow < n);
this.bsBuff = bsBuffShadow;
}
this.bsLive = bsLiveShadow - n;
return (bsBuffShadow >> (bsLiveShadow - n)) & ((1 << n) - 1);
}
// private bool bsGetBit()
// {
// int bsLiveShadow = this.bsLive;
// int bsBuffShadow = this.bsBuff;
//
// if (bsLiveShadow < 1)
// {
// int thech = this.input.ReadByte();
//
// if (thech < 0)
// {
// throw new IOException("unexpected end of stream");
// }
//
// bsBuffShadow = (bsBuffShadow << 8) | thech;
// bsLiveShadow += 8;
// this.bsBuff = bsBuffShadow;
// }
//
// this.bsLive = bsLiveShadow - 1;
// return ((bsBuffShadow >> (bsLiveShadow - 1)) & 1) != 0;
// }
private bool bsGetBit()
{
int bit = GetBits(1);
return bit != 0;
}
private char bsGetUByte()
{
return (char) GetBits(8);
}
private uint bsGetInt()
{
return (uint)((((((GetBits(8) << 8) | GetBits(8)) << 8) | GetBits(8)) << 8) | GetBits(8));
}
/**
* Called by createHuffmanDecodingTables() exclusively.
*/
private static void hbCreateDecodeTables(int[] limit,
int[] bbase, int[] perm, char[] length,
int minLen, int maxLen, int alphaSize)
{
for (int i = minLen, pp = 0; i <= maxLen; i++)
{
for (int j = 0; j < alphaSize; j++)
{
if (length[j] == i)
{
perm[pp++] = j;
}
}
}
for (int i = BZip2.MaxCodeLength; --i > 0;)
{
bbase[i] = 0;
limit[i] = 0;
}
for (int i = 0; i < alphaSize; i++)
{
bbase[length[i] + 1]++;
}
for (int i = 1, b = bbase[0]; i < BZip2.MaxCodeLength; i++)
{
b += bbase[i];
bbase[i] = b;
}
for (int i = minLen, vec = 0, b = bbase[i]; i <= maxLen; i++)
{
int nb = bbase[i + 1];
vec += nb - b;
b = nb;
limit[i] = vec - 1;
vec <<= 1;
}
for (int i = minLen + 1; i <= maxLen; i++)
{
bbase[i] = ((limit[i - 1] + 1) << 1) - bbase[i];
}
}
private void recvDecodingTables()
{
var s = this.data;
bool[] inUse = s.inUse;
byte[] pos = s.recvDecodingTables_pos;
//byte[] selector = s.selector;
int inUse16 = 0;
/* Receive the mapping table */
for (int i = 0; i < 16; i++)
{
if (bsGetBit())
{
inUse16 |= 1 << i;
}
}
for (int i = 256; --i >= 0;)
{
inUse[i] = false;
}
for (int i = 0; i < 16; i++)
{
if ((inUse16 & (1 << i)) != 0)
{
int i16 = i << 4;
for (int j = 0; j < 16; j++)
{
if (bsGetBit())
{
inUse[i16 + j] = true;
}
}
}
}
MakeMaps();
int alphaSize = this.nInUse + 2;
/* Now the selectors */
int nGroups = GetBits(3);
int nSelectors = GetBits(15);
for (int i = 0; i < nSelectors; i++)
{
int j = 0;
while (bsGetBit())
{
j++;
}
s.selectorMtf[i] = (byte) j;
}
/* Undo the MTF values for the selectors. */
for (int v = nGroups; --v >= 0;)
{
pos[v] = (byte) v;
}
for (int i = 0; i < nSelectors; i++)
{
int v = s.selectorMtf[i];
byte tmp = pos[v];
while (v > 0)
{
// nearly all times v is zero, 4 in most other cases
pos[v] = pos[v - 1];
v--;
}
pos[0] = tmp;
s.selector[i] = tmp;
}
char[][] len = s.temp_charArray2d;
/* Now the coding tables */
for (int t = 0; t < nGroups; t++)
{
int curr = GetBits(5);
char[] len_t = len[t];
for (int i = 0; i < alphaSize; i++)
{
while (bsGetBit())
{
curr += bsGetBit() ? -1 : 1;
}
len_t[i] = (char) curr;
}
}
// finally create the Huffman tables
createHuffmanDecodingTables(alphaSize, nGroups);
}
/**
* Called by recvDecodingTables() exclusively.
*/
private void createHuffmanDecodingTables(int alphaSize,
int nGroups)
{
var s = this.data;
char[][] len = s.temp_charArray2d;
for (int t = 0; t < nGroups; t++)
{
int minLen = 32;
int maxLen = 0;
char[] len_t = len[t];
for (int i = alphaSize; --i >= 0;)
{
char lent = len_t[i];
if (lent > maxLen)
maxLen = lent;
if (lent < minLen)
minLen = lent;
}
hbCreateDecodeTables(s.gLimit[t], s.gBase[t], s.gPerm[t], len[t], minLen,
maxLen, alphaSize);
s.gMinlen[t] = minLen;
}
}
private void getAndMoveToFrontDecode()
{
var s = this.data;
this.origPtr = GetBits(24);
if (this.origPtr < 0)
throw new IOException("BZ_DATA_ERROR");
if (this.origPtr > 10 + BZip2.BlockSizeMultiple * this.blockSize100k)
throw new IOException("BZ_DATA_ERROR");
recvDecodingTables();
byte[] yy = s.getAndMoveToFrontDecode_yy;
int limitLast = this.blockSize100k * BZip2.BlockSizeMultiple;
/*
* Setting up the unzftab entries here is not strictly necessary, but it
* does save having to do it later in a separate pass, and so saves a
* block's worth of cache misses.
*/
for (int i = 256; --i >= 0;)
{
yy[i] = (byte) i;
s.unzftab[i] = 0;
}
int groupNo = 0;
int groupPos = BZip2.G_SIZE - 1;
int eob = this.nInUse + 1;
int nextSym = getAndMoveToFrontDecode0(0);
int bsBuffShadow = this.bsBuff;
int bsLiveShadow = this.bsLive;
int lastShadow = -1;
int zt = s.selector[groupNo] & 0xff;
int[] base_zt = s.gBase[zt];
int[] limit_zt = s.gLimit[zt];
int[] perm_zt = s.gPerm[zt];
int minLens_zt = s.gMinlen[zt];
while (nextSym != eob)
{
if ((nextSym == BZip2.RUNA) || (nextSym == BZip2.RUNB))
{
int es = -1;
for (int n = 1; true; n <<= 1)
{
if (nextSym == BZip2.RUNA)
{
es += n;
}
else if (nextSym == BZip2.RUNB)
{
es += n << 1;
}
else
{
break;
}
if (groupPos == 0)
{
groupPos = BZip2.G_SIZE - 1;
zt = s.selector[++groupNo] & 0xff;
base_zt = s.gBase[zt];
limit_zt = s.gLimit[zt];
perm_zt = s.gPerm[zt];
minLens_zt = s.gMinlen[zt];
}
else
{
groupPos--;
}
int zn = minLens_zt;
// Inlined:
// int zvec = GetBits(zn);
while (bsLiveShadow < zn)
{
int thech = this.input.ReadByte();
if (thech >= 0)
{
bsBuffShadow = (bsBuffShadow << 8) | thech;
bsLiveShadow += 8;
continue;
}
else
{
throw new IOException("unexpected end of stream");
}
}
int zvec = (bsBuffShadow >> (bsLiveShadow - zn))
& ((1 << zn) - 1);
bsLiveShadow -= zn;
while (zvec > limit_zt[zn])
{
zn++;
while (bsLiveShadow < 1)
{
int thech = this.input.ReadByte();
if (thech >= 0)
{
bsBuffShadow = (bsBuffShadow << 8) | thech;
bsLiveShadow += 8;
continue;
}
else
{
throw new IOException("unexpected end of stream");
}
}
bsLiveShadow--;
zvec = (zvec << 1)
| ((bsBuffShadow >> bsLiveShadow) & 1);
}
nextSym = perm_zt[zvec - base_zt[zn]];
}
byte ch = s.seqToUnseq[yy[0]];
s.unzftab[ch & 0xff] += es + 1;
while (es-- >= 0)
{
s.ll8[++lastShadow] = ch;
}
if (lastShadow >= limitLast)
throw new IOException("block overrun");
}
else
{
if (++lastShadow >= limitLast)
throw new IOException("block overrun");
byte tmp = yy[nextSym - 1];
s.unzftab[s.seqToUnseq[tmp] & 0xff]++;
s.ll8[lastShadow] = s.seqToUnseq[tmp];
/*
* This loop is hammered during decompression, hence avoid
* native method call overhead of System.Buffer.BlockCopy for very
* small ranges to copy.
*/
if (nextSym <= 16)
{
for (int j = nextSym - 1; j > 0;)
{
yy[j] = yy[--j];
}
}
else
{
System.Buffer.BlockCopy(yy, 0, yy, 1, nextSym - 1);
}
yy[0] = tmp;
if (groupPos == 0)
{
groupPos = BZip2.G_SIZE - 1;
zt = s.selector[++groupNo] & 0xff;
base_zt = s.gBase[zt];
limit_zt = s.gLimit[zt];
perm_zt = s.gPerm[zt];
minLens_zt = s.gMinlen[zt];
}