forked from svn2github/dotnetzip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBZip2Compressor.cs
More file actions
1920 lines (1654 loc) · 70.8 KB
/
Copy pathBZip2Compressor.cs
File metadata and controls
1920 lines (1654 loc) · 70.8 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
// BZip2Compressor.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-28 06:17:22>
//
// ------------------------------------------------------------------
//
// This module defines the BZip2Compressor class, which is a
// BZIP2-compressing encoder. It is used internally in the BZIP2
// library, by the BZip2OutputStream class and its parallel variant,
// ParallelBZip2OutputStream. This code was originally based on Apache
// commons source code, and significantly modified. The license below
// applies to the original Apache code and to this modified variant.
//
// ------------------------------------------------------------------
/*
* 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.
*/
//
// Design notes:
//
// This class performs BZip2 compression. It is derived from the
// BZip2OutputStream from the Apache commons source code, but is
// significantly modified from that code. While the Apache class is a
// stream that compresses, this particular class simply performs
// compression. It follows a Manager pattern. It manages an internal
// buffer for uncompressed data; callers place data into the buffer
// using the Fill() method. This class then compresses the data and
// writes the compressed form out, via the CompressAndWrite() method.
// Because BZip2 uses byte-shredding, this class relies on a BitWriter,
// and not a .NET Stream, to emit its output. (*Think of the BitWriter
// class as an Adapter that enables Bit-oriented output to a standard
// byte-oriented .NET stream.)
//
// This class exists to support the two distinct output streams that
// perform BZip2 compression: BZip2OutputStream and
// ParallelBZip2OutputStream. These streams rely on BZip2Compressor to
// provide the encoder/compression logic. This code has been derived
// from the bzip2 output stream in the Apache commons library; it has
// been significantly modified from that form, in order to provide a
// single compressor that could support both types of streams.
//
// In a bz2 file or stream, there is never any bit padding except for 0..7
// bits in the final byte in the file. Successive compressed blocks in a
// .bz2 file are not byte-aligned.
//
//
using System;
using System.IO;
// flymake: csc.exe /t:module BZip2InputStream.cs BZip2OutputStream.cs Rand.cs BCRC32.cs @@FILE@@
namespace Ionic.BZip2
{
internal class BZip2Compressor
{
private int blockSize100k; // 0...9
private int currentByte = -1;
private int runLength = 0;
private int last; // index into the block of the last char processed
private int outBlockFillThreshold;
private CompressionState cstate;
private readonly Ionic.Crc.CRC32 crc = new Ionic.Crc.CRC32(true);
BitWriter bw;
int runs;
/*
* The following three vars are used when sorting. If too many long
* comparisons happen, we stop sorting, randomise the block slightly, and
* try again. I think this wrinkle in the implementation was removed from
* a later rev of the C-language bzip, not sure. -DPC 24 Jul 2011
*
*/
private int workDone;
private int workLimit;
private bool firstAttempt;
private bool blockRandomised;
private int origPtr;
private int nInUse;
private int nMTF;
private static readonly int SETMASK = (1 << 21);
private static readonly int CLEARMASK = (~SETMASK);
private static readonly byte GREATER_ICOST = 15;
private static readonly byte LESSER_ICOST = 0;
private static readonly int SMALL_THRESH = 20;
private static readonly int DEPTH_THRESH = 10;
private static readonly int WORK_FACTOR = 30;
/**
* Knuth's increments seem to work better than Incerpi-Sedgewick here.
* Possibly because the number of elems to sort is usually small, typically
* <= 20.
*/
private static readonly int[] increments = { 1, 4, 13, 40, 121, 364, 1093, 3280,
9841, 29524, 88573, 265720, 797161,
2391484 };
/// <summary>
/// BZip2Compressor writes its compressed data out via a BitWriter. This
/// is necessary because BZip2 does byte shredding.
/// </summary>
public BZip2Compressor(BitWriter writer)
: this(writer, BZip2.MaxBlockSize)
{
}
public BZip2Compressor(BitWriter writer, int blockSize)
{
this.blockSize100k = blockSize;
this.bw = writer;
// 20 provides a margin of slop (not to say "Safety"). The maximum
// size of an encoded run in the output block is 5 bytes, so really, 5
// bytes ought to do, but this is a margin of slop found in the
// original bzip code. Not sure if important for decoding
// (decompressing). So we'll leave the slop.
this.outBlockFillThreshold = (blockSize * BZip2.BlockSizeMultiple) - 20;
this.cstate = new CompressionState(blockSize);
Reset();
}
void Reset()
{
// initBlock();
this.crc.Reset();
this.currentByte = -1;
this.runLength = 0;
this.last = -1;
for (int i = 256; --i >= 0;)
this.cstate.inUse[i] = false;
//bw.Reset(); xxx? want this? no no no
}
public int BlockSize
{
get { return this.blockSize100k; }
}
public uint Crc32
{
get; private set;
}
public int AvailableBytesOut
{
get; private set;
}
/// <summary>
/// The number of uncompressed bytes being held in the buffer.
/// </summary>
/// <remarks>
/// <para>
/// I am thinking this may be useful in a Stream that uses this
/// compressor class. In the Close() method on the stream it could
/// check this value to see if anything has been written at all. You
/// may think the stream could easily track the number of bytes it
/// wrote, which would eliminate the need for this. But, there is the
/// case where the stream writes a complete block, and it is full, and
/// then writes no more. In that case the stream may want to check.
/// </para>
/// </remarks>
public int UncompressedBytes
{
get { return this.last + 1; }
}
/// <summary>
/// Accept new bytes into the compressor data buffer
/// </summary>
/// <remarks>
/// <para>
/// This method does the first-level (cheap) run-length encoding, and
/// stores the encoded data into the rle block.
/// </para>
/// </remarks>
public int Fill(byte[] buffer, int offset, int count)
{
if (this.last >= this.outBlockFillThreshold)
return 0; // We're full, I tell you!
int bytesWritten = 0;
int limit = offset + count;
int rc;
// do run-length-encoding until block is full
do
{
rc = write0(buffer[offset++]);
if (rc > 0) bytesWritten++;
} while (offset < limit && rc == 1);
return bytesWritten;
}
/// <summary>
/// Process one input byte into the block.
/// </summary>
///
/// <remarks>
/// <para>
/// To "process" the byte means to do the run-length encoding.
/// There are 3 possible return values:
///
/// 0 - the byte was not written, in other words, not
/// encoded into the block. This happens when the
/// byte b would require the start of a new run, and
/// the block has no more room for new runs.
///
/// 1 - the byte was written, and the block is not full.
///
/// 2 - the byte was written, and the block is full.
///
/// </para>
/// </remarks>
/// <returns>0 if the byte was not written, non-zero if written.</returns>
private int write0(byte b)
{
bool rc;
// there is no current run in progress
if (this.currentByte == -1)
{
this.currentByte = b;
this.runLength++;
return 1;
}
// this byte is the same as the current run in progress
if (this.currentByte == b)
{
if (++this.runLength > 254)
{
rc = AddRunToOutputBlock(false);
this.currentByte = -1;
this.runLength = 0;
return (rc) ? 2 : 1;
}
return 1; // not full
}
// This byte requires a new run.
// Put the prior run into the Run-length-encoded block,
// and try to start a new run.
rc = AddRunToOutputBlock(false);
if (rc)
{
this.currentByte = -1;
this.runLength = 0;
// returning 0 implies the block is full, and the byte was not written.
return 0;
}
// start a new run
this.runLength = 1;
this.currentByte = b;
return 1;
}
/// <summary>
/// Append one run to the output block.
/// </summary>
///
/// <remarks>
/// <para>
/// This compressor does run-length-encoding before BWT and etc. This
/// method simply appends a run to the output block. The append always
/// succeeds. The return value indicates whether the block is full:
/// false (not full) implies that at least one additional run could be
/// processed.
/// </para>
/// </remarks>
/// <returns>true if the block is now full; otherwise false.</returns>
private bool AddRunToOutputBlock(bool final)
{
runs++;
/* add_pair_to_block ( EState* s ) */
int previousLast = this.last;
// sanity check only - because of the check done at the
// bottom of this method, and the logic in write0(), this
// should never ever happen.
if (previousLast >= this.outBlockFillThreshold && !final)
{
var msg = String.Format("block overrun(final={2}): {0} >= threshold ({1})",
previousLast, this.outBlockFillThreshold, final);
throw new Exception(msg);
}
// NB: the index used here into block is always (last+2). This is
// because last is -1 based - the initial value is -1, a flag value
// used to indicate that nothing has yet been written into the
// block. The endBlock() fn tests for -1 to detect empty blocks. Also,
// the first byte of block is used, during sorting, to hold block[last
// +1], which is the final byte value that had been written into the
// rle block. For those two reasons, the base offset from last is
// always +2.
byte b = (byte) this.currentByte;
byte[] block = this.cstate.block;
this.cstate.inUse[b] = true;
int rl = this.runLength;
this.crc.UpdateCRC(b, rl);
switch (rl)
{
case 1:
block[previousLast + 2] = b;
this.last = previousLast + 1;
break;
case 2:
block[previousLast + 2] = b;
block[previousLast + 3] = b;
this.last = previousLast + 2;
break;
case 3:
block[previousLast + 2] = b;
block[previousLast + 3] = b;
block[previousLast + 4] = b;
this.last = previousLast + 3;
break;
default:
rl -= 4;
this.cstate.inUse[rl] = true;
block[previousLast + 2] = b;
block[previousLast + 3] = b;
block[previousLast + 4] = b;
block[previousLast + 5] = b;
block[previousLast + 6] = (byte) rl;
this.last = previousLast + 5;
break;
}
// is full?
return (this.last >= this.outBlockFillThreshold);
}
/// <summary>
/// Compress the data that has been placed (Run-length-encoded) into the
/// block. The compressed data goes into the CompressedBytes array.
/// </summary>
/// <remarks>
/// <para>
/// Side effects: 1. fills the CompressedBytes array. 2. sets the
/// AvailableBytesOut property.
/// </para>
/// </remarks>
public void CompressAndWrite() // endBlock
{
if (this.runLength > 0)
AddRunToOutputBlock(true);
this.currentByte = -1;
// Console.WriteLine(" BZip2Compressor:CompressAndWrite (r={0} bcrc={1:X8})",
// runs, this.crc.Crc32Result);
// has any data been written?
if (this.last == -1)
return; // no data; nothing to compress
/* sort the block and establish posn of original string */
blockSort();
/*
* A 6-byte block header, the value chosen arbitrarily as 0x314159265359
* :-). A 32 bit value does not really give a strong enough guarantee
* that the value will not appear by chance in the compressed
* datastream. Worst-case probability of this event, for a 900k block,
* is about 2.0e-3 for 32 bits, 1.0e-5 for 40 bits and 4.0e-8 for 48
* bits. For a compressed file of size 100Gb -- about 100000 blocks --
* only a 48-bit marker will do. NB: normal compression/ decompression
* donot rely on these statistical properties. They are only important
* when trying to recover blocks from damaged files.
*/
this.bw.WriteByte(0x31);
this.bw.WriteByte(0x41);
this.bw.WriteByte(0x59);
this.bw.WriteByte(0x26);
this.bw.WriteByte(0x53);
this.bw.WriteByte(0x59);
this.Crc32 = (uint) this.crc.Crc32Result;
this.bw.WriteInt(this.Crc32);
/* Now a single bit indicating randomisation. */
this.bw.WriteBits(1, (this.blockRandomised)?1U:0U);
/* Finally, block's contents proper. */
moveToFrontCodeAndSend();
Reset();
}
private void randomiseBlock()
{
bool[] inUse = this.cstate.inUse;
byte[] block = this.cstate.block;
int lastShadow = this.last;
for (int i = 256; --i >= 0;)
inUse[i] = false;
int rNToGo = 0;
int rTPos = 0;
for (int i = 0, j = 1; i <= lastShadow; i = j, j++)
{
if (rNToGo == 0)
{
rNToGo = (char) Rand.Rnums(rTPos);
if (++rTPos == 512)
{
rTPos = 0;
}
}
rNToGo--;
block[j] ^= (byte) ((rNToGo == 1) ? 1 : 0);
// handle 16 bit signed numbers
inUse[block[j] & 0xff] = true;
}
this.blockRandomised = true;
}
private void mainSort()
{
CompressionState dataShadow = this.cstate;
int[] runningOrder = dataShadow.mainSort_runningOrder;
int[] copy = dataShadow.mainSort_copy;
bool[] bigDone = dataShadow.mainSort_bigDone;
int[] ftab = dataShadow.ftab;
byte[] block = dataShadow.block;
int[] fmap = dataShadow.fmap;
char[] quadrant = dataShadow.quadrant;
int lastShadow = this.last;
int workLimitShadow = this.workLimit;
bool firstAttemptShadow = this.firstAttempt;
// Set up the 2-byte frequency table
for (int i = 65537; --i >= 0;)
{
ftab[i] = 0;
}
/*
* In the various block-sized structures, live data runs from 0 to
* last+NUM_OVERSHOOT_BYTES inclusive. First, set up the overshoot area
* for block.
*/
for (int i = 0; i < BZip2.NUM_OVERSHOOT_BYTES; i++)
{
block[lastShadow + i + 2] = block[(i % (lastShadow + 1)) + 1];
}
for (int i = lastShadow + BZip2.NUM_OVERSHOOT_BYTES +1; --i >= 0;)
{
quadrant[i] = '\0';
}
block[0] = block[lastShadow + 1];
// Complete the initial radix sort:
int c1 = block[0] & 0xff;
for (int i = 0; i <= lastShadow; i++)
{
int c2 = block[i + 1] & 0xff;
ftab[(c1 << 8) + c2]++;
c1 = c2;
}
for (int i = 1; i <= 65536; i++)
ftab[i] += ftab[i - 1];
c1 = block[1] & 0xff;
for (int i = 0; i < lastShadow; i++)
{
int c2 = block[i + 2] & 0xff;
fmap[--ftab[(c1 << 8) + c2]] = i;
c1 = c2;
}
fmap[--ftab[((block[lastShadow + 1] & 0xff) << 8) + (block[1] & 0xff)]] = lastShadow;
/*
* Now ftab contains the first loc of every small bucket. Calculate the
* running order, from smallest to largest big bucket.
*/
for (int i = 256; --i >= 0;)
{
bigDone[i] = false;
runningOrder[i] = i;
}
for (int h = 364; h != 1;)
{
h /= 3;
for (int i = h; i <= 255; i++)
{
int vv = runningOrder[i];
int a = ftab[(vv + 1) << 8] - ftab[vv << 8];
int b = h - 1;
int j = i;
for (int ro = runningOrder[j - h]; (ftab[(ro + 1) << 8] - ftab[ro << 8]) > a; ro = runningOrder[j
- h])
{
runningOrder[j] = ro;
j -= h;
if (j <= b)
{
break;
}
}
runningOrder[j] = vv;
}
}
/*
* The main sorting loop.
*/
for (int i = 0; i <= 255; i++)
{
/*
* Process big buckets, starting with the least full.
*/
int ss = runningOrder[i];
// Step 1:
/*
* Complete the big bucket [ss] by quicksorting any unsorted small
* buckets [ss, j]. Hopefully previous pointer-scanning phases have
* already completed many of the small buckets [ss, j], so we don't
* have to sort them at all.
*/
for (int j = 0; j <= 255; j++)
{
int sb = (ss << 8) + j;
int ftab_sb = ftab[sb];
if ((ftab_sb & SETMASK) != SETMASK)
{
int lo = ftab_sb & CLEARMASK;
int hi = (ftab[sb + 1] & CLEARMASK) - 1;
if (hi > lo)
{
mainQSort3(dataShadow, lo, hi, 2);
if (firstAttemptShadow
&& (this.workDone > workLimitShadow))
{
return;
}
}
ftab[sb] = ftab_sb | SETMASK;
}
}
// Step 2:
// Now scan this big bucket so as to synthesise the
// sorted order for small buckets [t, ss] for all t != ss.
for (int j = 0; j <= 255; j++)
{
copy[j] = ftab[(j << 8) + ss] & CLEARMASK;
}
for (int j = ftab[ss << 8] & CLEARMASK, hj = (ftab[(ss + 1) << 8] & CLEARMASK); j < hj; j++)
{
int fmap_j = fmap[j];
c1 = block[fmap_j] & 0xff;
if (!bigDone[c1])
{
fmap[copy[c1]] = (fmap_j == 0) ? lastShadow : (fmap_j - 1);
copy[c1]++;
}
}
for (int j = 256; --j >= 0;)
ftab[(j << 8) + ss] |= SETMASK;
// Step 3:
/*
* The ss big bucket is now done. Record this fact, and update the
* quadrant descriptors. Remember to update quadrants in the
* overshoot area too, if necessary. The "if (i < 255)" test merely
* skips this updating for the last bucket processed, since updating
* for the last bucket is pointless.
*/
bigDone[ss] = true;
if (i < 255)
{
int bbStart = ftab[ss << 8] & CLEARMASK;
int bbSize = (ftab[(ss + 1) << 8] & CLEARMASK) - bbStart;
int shifts = 0;
while ((bbSize >> shifts) > 65534)
{
shifts++;
}
for (int j = 0; j < bbSize; j++)
{
int a2update = fmap[bbStart + j];
char qVal = (char) (j >> shifts);
quadrant[a2update] = qVal;
if (a2update < BZip2.NUM_OVERSHOOT_BYTES)
{
quadrant[a2update + lastShadow + 1] = qVal;
}
}
}
}
}
private void blockSort()
{
this.workLimit = WORK_FACTOR * this.last;
this.workDone = 0;
this.blockRandomised = false;
this.firstAttempt = true;
mainSort();
if (this.firstAttempt && (this.workDone > this.workLimit))
{
randomiseBlock();
this.workLimit = this.workDone = 0;
this.firstAttempt = false;
mainSort();
}
int[] fmap = this.cstate.fmap;
this.origPtr = -1;
for (int i = 0, lastShadow = this.last; i <= lastShadow; i++)
{
if (fmap[i] == 0)
{
this.origPtr = i;
break;
}
}
// assert (this.origPtr != -1) : this.origPtr;
}
/**
* This is the most hammered method of this class.
*
* <p>
* This is the version using unrolled loops.
* </p>
*/
private bool mainSimpleSort(CompressionState dataShadow, int lo,
int hi, int d)
{
int bigN = hi - lo + 1;
if (bigN < 2)
{
return this.firstAttempt && (this.workDone > this.workLimit);
}
int hp = 0;
while (increments[hp] < bigN)
hp++;
int[] fmap = dataShadow.fmap;
char[] quadrant = dataShadow.quadrant;
byte[] block = dataShadow.block;
int lastShadow = this.last;
int lastPlus1 = lastShadow + 1;
bool firstAttemptShadow = this.firstAttempt;
int workLimitShadow = this.workLimit;
int workDoneShadow = this.workDone;
// Following block contains unrolled code which could be shortened by
// coding it in additional loops.
// HP:
while (--hp >= 0)
{
int h = increments[hp];
int mj = lo + h - 1;
for (int i = lo + h; i <= hi;)
{
// copy
for (int k = 3; (i <= hi) && (--k >= 0); i++)
{
int v = fmap[i];
int vd = v + d;
int j = i;
// for (int a;
// (j > mj) && mainGtU((a = fmap[j - h]) + d, vd,
// block, quadrant, lastShadow);
// j -= h) {
// fmap[j] = a;
// }
//
// unrolled version:
// start inline mainGTU
bool onceRunned = false;
int a = 0;
HAMMER: while (true)
{
if (onceRunned)
{
fmap[j] = a;
if ((j -= h) <= mj)
{
goto END_HAMMER;
}
}
else {
onceRunned = true;
}
a = fmap[j - h];
int i1 = a + d;
int i2 = vd;
// following could be done in a loop, but
// unrolled it for performance:
if (block[i1 + 1] == block[i2 + 1])
{
if (block[i1 + 2] == block[i2 + 2])
{
if (block[i1 + 3] == block[i2 + 3])
{
if (block[i1 + 4] == block[i2 + 4])
{
if (block[i1 + 5] == block[i2 + 5])
{
if (block[(i1 += 6)] == block[(i2 += 6)])
{
int x = lastShadow;
X: while (x > 0)
{
x -= 4;
if (block[i1 + 1] == block[i2 + 1])
{
if (quadrant[i1] == quadrant[i2])
{
if (block[i1 + 2] == block[i2 + 2])
{
if (quadrant[i1 + 1] == quadrant[i2 + 1])
{
if (block[i1 + 3] == block[i2 + 3])
{
if (quadrant[i1 + 2] == quadrant[i2 + 2])
{
if (block[i1 + 4] == block[i2 + 4])
{
if (quadrant[i1 + 3] == quadrant[i2 + 3])
{
if ((i1 += 4) >= lastPlus1)
{
i1 -= lastPlus1;
}
if ((i2 += 4) >= lastPlus1)
{
i2 -= lastPlus1;
}
workDoneShadow++;
goto X;
}
else if ((quadrant[i1 + 3] > quadrant[i2 + 3]))
{
goto HAMMER;
}
else {
goto END_HAMMER;
}
}
else if ((block[i1 + 4] & 0xff) > (block[i2 + 4] & 0xff))
{
goto HAMMER;
}
else {
goto END_HAMMER;
}
}
else if ((quadrant[i1 + 2] > quadrant[i2 + 2]))
{
goto HAMMER;
}
else {
goto END_HAMMER;
}
}
else if ((block[i1 + 3] & 0xff) > (block[i2 + 3] & 0xff))
{
goto HAMMER;
}
else {
goto END_HAMMER;
}
}
else if ((quadrant[i1 + 1] > quadrant[i2 + 1]))
{
goto HAMMER;
}
else {
goto END_HAMMER;
}
}
else if ((block[i1 + 2] & 0xff) > (block[i2 + 2] & 0xff))
{
goto HAMMER;
}
else {
goto END_HAMMER;
}
}
else if ((quadrant[i1] > quadrant[i2]))
{
goto HAMMER;
}
else {
goto END_HAMMER;
}
}
else if ((block[i1 + 1] & 0xff) > (block[i2 + 1] & 0xff))
{
goto HAMMER;
}
else {
goto END_HAMMER;
}
}
goto END_HAMMER;
} // while x > 0
else {
if ((block[i1] & 0xff) > (block[i2] & 0xff))
{
goto HAMMER;
}
else {
goto END_HAMMER;
}
}
}
else if ((block[i1 + 5] & 0xff) > (block[i2 + 5] & 0xff))
{
goto HAMMER;
}
else {
goto END_HAMMER;
}
}
else if ((block[i1 + 4] & 0xff) > (block[i2 + 4] & 0xff))
{
goto HAMMER;
}
else {
goto END_HAMMER;
}
}
else if ((block[i1 + 3] & 0xff) > (block[i2 + 3] & 0xff))
{
goto HAMMER;
}
else {
goto END_HAMMER;
}
}
else if ((block[i1 + 2] & 0xff) > (block[i2 + 2] & 0xff))
{
goto HAMMER;
}
else {
goto END_HAMMER;
}
}
else if ((block[i1 + 1] & 0xff) > (block[i2 + 1] & 0xff))
{
goto HAMMER;
}
else {
goto END_HAMMER;
}
} // HAMMER
END_HAMMER:
// end inline mainGTU
fmap[j] = v;
}
if (firstAttemptShadow && (i <= hi)
&& (workDoneShadow > workLimitShadow))
{
goto END_HP;
}
}
}
END_HP:
this.workDone = workDoneShadow;
return firstAttemptShadow && (workDoneShadow > workLimitShadow);
}
private static void vswap(int[] fmap, int p1, int p2, int n)
{
n += p1;
while (p1 < n)
{
int t = fmap[p1];
fmap[p1++] = fmap[p2];
fmap[p2++] = t;
}
}
private static byte med3(byte a, byte b, byte c)
{
return (a < b) ? (b < c ? b : a < c ? c : a) : (b > c ? b : a > c ? c
: a);
}
/**
* Method "mainQSort3", file "blocksort.c", BZip2 1.0.2
*/
private void mainQSort3(CompressionState dataShadow, int loSt,
int hiSt, int dSt)
{
int[] stack_ll = dataShadow.stack_ll;
int[] stack_hh = dataShadow.stack_hh;
int[] stack_dd = dataShadow.stack_dd;
int[] fmap = dataShadow.fmap;
byte[] block = dataShadow.block;
stack_ll[0] = loSt;
stack_hh[0] = hiSt;
stack_dd[0] = dSt;
for (int sp = 1; --sp >= 0;)
{
int lo = stack_ll[sp];
int hi = stack_hh[sp];
int d = stack_dd[sp];