001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020/*
021 * This package is based on the work done by Keiron Liddle, Aftex Software
022 * <keiron@aftexsw.com> to whom the Ant project is very grateful for his
023 * great code.
024 */
025package org.apache.commons.compress.compressors.bzip2;
026
027import java.io.IOException;
028import java.io.InputStream;
029import java.nio.ByteOrder;
030import java.util.Arrays;
031
032import org.apache.commons.compress.compressors.CompressorInputStream;
033import org.apache.commons.compress.utils.BitInputStream;
034import org.apache.commons.compress.utils.CloseShieldFilterInputStream;
035import org.apache.commons.compress.utils.InputStreamStatistics;
036
037/**
038 * An input stream that decompresses from the BZip2 format to be read as any other stream.
039 *
040 * @NotThreadSafe
041 */
042public class BZip2CompressorInputStream extends CompressorInputStream
043    implements BZip2Constants, InputStreamStatistics {
044
045    /**
046     * Index of the last char in the block, so the block size == last + 1.
047     */
048    private int last;
049
050    /**
051     * Index in zptr[] of original string after sorting.
052     */
053    private int origPtr;
054
055    /**
056     * always: in the range 0 .. 9. The current block size is 100000 * this
057     * number.
058     */
059    private int blockSize100k;
060
061    private boolean blockRandomised;
062
063    private final CRC crc = new CRC();
064
065    private int nInUse;
066
067    private BitInputStream bin;
068    private final boolean decompressConcatenated;
069
070    private static final int EOF = 0;
071    private static final int START_BLOCK_STATE = 1;
072    private static final int RAND_PART_A_STATE = 2;
073    private static final int RAND_PART_B_STATE = 3;
074    private static final int RAND_PART_C_STATE = 4;
075    private static final int NO_RAND_PART_A_STATE = 5;
076    private static final int NO_RAND_PART_B_STATE = 6;
077    private static final int NO_RAND_PART_C_STATE = 7;
078
079    private int currentState = START_BLOCK_STATE;
080
081    private int storedBlockCRC, storedCombinedCRC;
082    private int computedBlockCRC, computedCombinedCRC;
083
084    // Variables used by setup* methods exclusively
085
086    private int su_count;
087    private int su_ch2;
088    private int su_chPrev;
089    private int su_i2;
090    private int su_j2;
091    private int su_rNToGo;
092    private int su_rTPos;
093    private int su_tPos;
094    private char su_z;
095
096    /**
097     * All memory intensive stuff. This field is initialized by initBlock().
098     */
099    private BZip2CompressorInputStream.Data data;
100
101    /**
102     * Constructs a new BZip2CompressorInputStream which decompresses bytes
103     * read from the specified stream. This doesn't suppprt decompressing
104     * concatenated .bz2 files.
105     *
106     * @param in the InputStream from which this object should be created
107     * @throws IOException
108     *             if the stream content is malformed or an I/O error occurs.
109     * @throws NullPointerException
110     *             if {@code in == null}
111     */
112    public BZip2CompressorInputStream(final InputStream in) throws IOException {
113        this(in, false);
114    }
115
116    /**
117     * Constructs a new BZip2CompressorInputStream which decompresses bytes
118     * read from the specified stream.
119     *
120     * @param in the InputStream from which this object should be created
121     * @param decompressConcatenated
122     *                     if true, decompress until the end of the input;
123     *                     if false, stop after the first .bz2 stream and
124     *                     leave the input position to point to the next
125     *                     byte after the .bz2 stream
126     *
127     * @throws IOException
128     *             if {@code in == null}, the stream content is malformed, or an I/O error occurs.
129     */
130    public BZip2CompressorInputStream(final InputStream in, final boolean decompressConcatenated) throws IOException {
131        this.bin = new BitInputStream(in == System.in ? new CloseShieldFilterInputStream(in) : in,
132            ByteOrder.BIG_ENDIAN);
133        this.decompressConcatenated = decompressConcatenated;
134
135        init(true);
136        initBlock();
137    }
138
139    @Override
140    public int read() throws IOException {
141        if (this.bin != null) {
142            final int r = read0();
143            count(r < 0 ? -1 : 1);
144            return r;
145        }
146        throw new IOException("stream closed");
147    }
148
149    /*
150     * (non-Javadoc)
151     *
152     * @see java.io.InputStream#read(byte[], int, int)
153     */
154    @Override
155    public int read(final byte[] dest, final int offs, final int len)
156        throws IOException {
157        if (offs < 0) {
158            throw new IndexOutOfBoundsException("offs(" + offs + ") < 0.");
159        }
160        if (len < 0) {
161            throw new IndexOutOfBoundsException("len(" + len + ") < 0.");
162        }
163        if (offs + len > dest.length) {
164            throw new IndexOutOfBoundsException("offs(" + offs + ") + len("
165                                                + len + ") > dest.length(" + dest.length + ").");
166        }
167        if (this.bin == null) {
168            throw new IOException("stream closed");
169        }
170        if (len == 0) {
171            return 0;
172        }
173
174        final int hi = offs + len;
175        int destOffs = offs;
176        int b;
177        while (destOffs < hi && ((b = read0()) >= 0)) {
178            dest[destOffs++] = (byte) b;
179            count(1);
180        }
181
182        return (destOffs == offs) ? -1 : (destOffs - offs);
183    }
184
185    /**
186     * @since 1.17
187     */
188    @Override
189    public long getCompressedCount() {
190        return bin.getBytesRead();
191    }
192
193    private void makeMaps() {
194        final boolean[] inUse = this.data.inUse;
195        final byte[] seqToUnseq = this.data.seqToUnseq;
196
197        int nInUseShadow = 0;
198
199        for (int i = 0; i < 256; i++) {
200            if (inUse[i]) {
201                seqToUnseq[nInUseShadow++] = (byte) i;
202            }
203        }
204
205        this.nInUse = nInUseShadow;
206    }
207
208    private int read0() throws IOException {
209        switch (currentState) {
210        case EOF:
211            return -1;
212
213        case START_BLOCK_STATE:
214            return setupBlock();
215
216        case RAND_PART_A_STATE:
217            throw new IllegalStateException();
218
219        case RAND_PART_B_STATE:
220            return setupRandPartB();
221
222        case RAND_PART_C_STATE:
223            return setupRandPartC();
224
225        case NO_RAND_PART_A_STATE:
226            throw new IllegalStateException();
227
228        case NO_RAND_PART_B_STATE:
229            return setupNoRandPartB();
230
231        case NO_RAND_PART_C_STATE:
232            return setupNoRandPartC();
233
234        default:
235            throw new IllegalStateException();
236        }
237    }
238
239    private int readNextByte(BitInputStream in) throws IOException {
240        long b = in.readBits(8);
241        return (int) b;
242    }
243
244    private boolean init(final boolean isFirstStream) throws IOException {
245        if (null == bin) {
246            throw new IOException("No InputStream");
247        }
248
249        if (!isFirstStream) {
250            bin.clearBitCache();
251        }
252
253        final int magic0 = readNextByte(this.bin);
254        if (magic0 == -1 && !isFirstStream) {
255            return false;
256        }
257        final int magic1 = readNextByte(this.bin);
258        final int magic2 = readNextByte(this.bin);
259
260        if (magic0 != 'B' || magic1 != 'Z' || magic2 != 'h') {
261            throw new IOException(isFirstStream
262                    ? "Stream is not in the BZip2 format"
263                    : "Garbage after a valid BZip2 stream");
264        }
265
266        final int blockSize = readNextByte(this.bin);
267        if ((blockSize < '1') || (blockSize > '9')) {
268            throw new IOException("BZip2 block size is invalid");
269        }
270
271        this.blockSize100k = blockSize - '0';
272
273        this.computedCombinedCRC = 0;
274
275        return true;
276    }
277
278    private void initBlock() throws IOException {
279        BitInputStream bin = this.bin;
280        char magic0;
281        char magic1;
282        char magic2;
283        char magic3;
284        char magic4;
285        char magic5;
286
287        while (true) {
288            // Get the block magic bytes.
289            magic0 = bsGetUByte(bin);
290            magic1 = bsGetUByte(bin);
291            magic2 = bsGetUByte(bin);
292            magic3 = bsGetUByte(bin);
293            magic4 = bsGetUByte(bin);
294            magic5 = bsGetUByte(bin);
295
296            // If isn't end of stream magic, break out of the loop.
297            if (magic0 != 0x17 || magic1 != 0x72 || magic2 != 0x45
298                    || magic3 != 0x38 || magic4 != 0x50 || magic5 != 0x90) {
299                break;
300            }
301
302            // End of stream was reached. Check the combined CRC and
303            // advance to the next .bz2 stream if decoding concatenated
304            // streams.
305            if (complete()) {
306                return;
307            }
308        }
309
310        if (magic0 != 0x31 || // '1'
311            magic1 != 0x41 || // ')'
312            magic2 != 0x59 || // 'Y'
313            magic3 != 0x26 || // '&'
314            magic4 != 0x53 || // 'S'
315            magic5 != 0x59 // 'Y'
316            ) {
317            this.currentState = EOF;
318            throw new IOException("bad block header");
319        }
320        this.storedBlockCRC = bsGetInt(bin);
321        this.blockRandomised = bsR(bin, 1) == 1;
322
323        /**
324         * Allocate data here instead in constructor, so we do not allocate
325         * it if the input file is empty.
326         */
327        if (this.data == null) {
328            this.data = new Data(this.blockSize100k);
329        }
330
331        // currBlockNo++;
332        getAndMoveToFrontDecode();
333
334        this.crc.initialiseCRC();
335        this.currentState = START_BLOCK_STATE;
336    }
337
338    private void endBlock() throws IOException {
339        this.computedBlockCRC = this.crc.getFinalCRC();
340
341        // A bad CRC is considered a fatal error.
342        if (this.storedBlockCRC != this.computedBlockCRC) {
343            // make next blocks readable without error
344            // (repair feature, not yet documented, not tested)
345            this.computedCombinedCRC = (this.storedCombinedCRC << 1)
346                | (this.storedCombinedCRC >>> 31);
347            this.computedCombinedCRC ^= this.storedBlockCRC;
348
349            throw new IOException("BZip2 CRC error");
350        }
351
352        this.computedCombinedCRC = (this.computedCombinedCRC << 1)
353            | (this.computedCombinedCRC >>> 31);
354        this.computedCombinedCRC ^= this.computedBlockCRC;
355    }
356
357    private boolean complete() throws IOException {
358        this.storedCombinedCRC = bsGetInt(bin);
359        this.currentState = EOF;
360        this.data = null;
361
362        if (this.storedCombinedCRC != this.computedCombinedCRC) {
363            throw new IOException("BZip2 CRC error");
364        }
365
366        // Look for the next .bz2 stream if decompressing
367        // concatenated files.
368        return !decompressConcatenated || !init(false);
369    }
370
371    @Override
372    public void close() throws IOException {
373        final BitInputStream inShadow = this.bin;
374        if (inShadow != null) {
375            try {
376                inShadow.close();
377            } finally {
378                this.data = null;
379                this.bin = null;
380            }
381        }
382    }
383
384    /**
385     * read bits from the input stream
386     * @param n the number of bits to read, must not exceed 32?
387     * @return the requested bits combined into an int
388     * @throws IOException
389     */
390    private static int bsR(BitInputStream bin, final int n) throws IOException {
391        long thech = bin.readBits(n);
392        if (thech < 0) {
393            throw new IOException("unexpected end of stream");
394        }
395        return (int) thech;
396    }
397
398    private static boolean bsGetBit(BitInputStream bin) throws IOException {
399        return bsR(bin, 1) != 0;
400    }
401
402    private static char bsGetUByte(BitInputStream bin) throws IOException {
403        return (char) bsR(bin, 8);
404    }
405
406    private static int bsGetInt(BitInputStream bin) throws IOException {
407        return bsR(bin, 32);
408    }
409
410    private static void checkBounds(final int checkVal, final int limitExclusive, String name)
411        throws IOException {
412        if (checkVal < 0) {
413            throw new IOException("Corrupted input, " + name + " value negative");
414        }
415        if (checkVal >= limitExclusive) {
416            throw new IOException("Corrupted input, " + name + " value too big");
417        }
418    }
419
420    /**
421     * Called by createHuffmanDecodingTables() exclusively.
422     */
423    private static void hbCreateDecodeTables(final int[] limit,
424                                             final int[] base, final int[] perm, final char[] length,
425                                             final int minLen, final int maxLen, final int alphaSize)
426        throws IOException {
427        for (int i = minLen, pp = 0; i <= maxLen; i++) {
428            for (int j = 0; j < alphaSize; j++) {
429                if (length[j] == i) {
430                    perm[pp++] = j;
431                }
432            }
433        }
434
435        for (int i = MAX_CODE_LEN; --i > 0;) {
436            base[i] = 0;
437            limit[i] = 0;
438        }
439
440        for (int i = 0; i < alphaSize; i++) {
441            final int l = length[i];
442            checkBounds(l, MAX_ALPHA_SIZE, "length");
443            base[l + 1]++;
444        }
445
446        for (int i = 1, b = base[0]; i < MAX_CODE_LEN; i++) {
447            b += base[i];
448            base[i] = b;
449        }
450
451        for (int i = minLen, vec = 0, b = base[i]; i <= maxLen; i++) {
452            final int nb = base[i + 1];
453            vec += nb - b;
454            b = nb;
455            limit[i] = vec - 1;
456            vec <<= 1;
457        }
458
459        for (int i = minLen + 1; i <= maxLen; i++) {
460            base[i] = ((limit[i - 1] + 1) << 1) - base[i];
461        }
462    }
463
464    private void recvDecodingTables() throws IOException {
465        final BitInputStream bin = this.bin;
466        final Data dataShadow = this.data;
467        final boolean[] inUse = dataShadow.inUse;
468        final byte[] pos = dataShadow.recvDecodingTables_pos;
469        final byte[] selector = dataShadow.selector;
470        final byte[] selectorMtf = dataShadow.selectorMtf;
471
472        int inUse16 = 0;
473
474        /* Receive the mapping table */
475        for (int i = 0; i < 16; i++) {
476            if (bsGetBit(bin)) {
477                inUse16 |= 1 << i;
478            }
479        }
480
481        Arrays.fill(inUse, false);
482        for (int i = 0; i < 16; i++) {
483            if ((inUse16 & (1 << i)) != 0) {
484                final int i16 = i << 4;
485                for (int j = 0; j < 16; j++) {
486                    if (bsGetBit(bin)) {
487                        inUse[i16 + j] = true;
488                    }
489                }
490            }
491        }
492
493        makeMaps();
494        final int alphaSize = this.nInUse + 2;
495        /* Now the selectors */
496        final int nGroups = bsR(bin, 3);
497        final int nSelectors = bsR(bin, 15);
498        checkBounds(alphaSize, MAX_ALPHA_SIZE + 1, "alphaSize");
499        checkBounds(nGroups, N_GROUPS + 1, "nGroups");
500        checkBounds(nSelectors, MAX_SELECTORS + 1, "nSelectors");
501
502        for (int i = 0; i < nSelectors; i++) {
503            int j = 0;
504            while (bsGetBit(bin)) {
505                j++;
506            }
507            selectorMtf[i] = (byte) j;
508        }
509
510        /* Undo the MTF values for the selectors. */
511        for (int v = nGroups; --v >= 0;) {
512            pos[v] = (byte) v;
513        }
514
515        for (int i = 0; i < nSelectors; i++) {
516            int v = selectorMtf[i] & 0xff;
517            checkBounds(v, N_GROUPS, "selectorMtf");
518            final byte tmp = pos[v];
519            while (v > 0) {
520                // nearly all times v is zero, 4 in most other cases
521                pos[v] = pos[v - 1];
522                v--;
523            }
524            pos[0] = tmp;
525            selector[i] = tmp;
526        }
527
528        final char[][] len = dataShadow.temp_charArray2d;
529
530        /* Now the coding tables */
531        for (int t = 0; t < nGroups; t++) {
532            int curr = bsR(bin, 5);
533            final char[] len_t = len[t];
534            for (int i = 0; i < alphaSize; i++) {
535                while (bsGetBit(bin)) {
536                    curr += bsGetBit(bin) ? -1 : 1;
537                }
538                len_t[i] = (char) curr;
539            }
540        }
541
542        // finally create the Huffman tables
543        createHuffmanDecodingTables(alphaSize, nGroups);
544    }
545
546    /**
547     * Called by recvDecodingTables() exclusively.
548     */
549    private void createHuffmanDecodingTables(final int alphaSize,
550                                             final int nGroups) throws IOException {
551        final Data dataShadow = this.data;
552        final char[][] len = dataShadow.temp_charArray2d;
553        final int[] minLens = dataShadow.minLens;
554        final int[][] limit = dataShadow.limit;
555        final int[][] base = dataShadow.base;
556        final int[][] perm = dataShadow.perm;
557
558        for (int t = 0; t < nGroups; t++) {
559            int minLen = 32;
560            int maxLen = 0;
561            final char[] len_t = len[t];
562            for (int i = alphaSize; --i >= 0;) {
563                final char lent = len_t[i];
564                if (lent > maxLen) {
565                    maxLen = lent;
566                }
567                if (lent < minLen) {
568                    minLen = lent;
569                }
570            }
571            hbCreateDecodeTables(limit[t], base[t], perm[t], len[t], minLen,
572                                 maxLen, alphaSize);
573            minLens[t] = minLen;
574        }
575    }
576
577    private void getAndMoveToFrontDecode() throws IOException {
578        final BitInputStream bin = this.bin;
579        this.origPtr = bsR(bin, 24);
580        recvDecodingTables();
581
582        final Data dataShadow = this.data;
583        final byte[] ll8 = dataShadow.ll8;
584        final int[] unzftab = dataShadow.unzftab;
585        final byte[] selector = dataShadow.selector;
586        final byte[] seqToUnseq = dataShadow.seqToUnseq;
587        final char[] yy = dataShadow.getAndMoveToFrontDecode_yy;
588        final int[] minLens = dataShadow.minLens;
589        final int[][] limit = dataShadow.limit;
590        final int[][] base = dataShadow.base;
591        final int[][] perm = dataShadow.perm;
592        final int limitLast = this.blockSize100k * 100000;
593
594        /*
595         * Setting up the unzftab entries here is not strictly necessary, but it
596         * does save having to do it later in a separate pass, and so saves a
597         * block's worth of cache misses.
598         */
599        for (int i = 256; --i >= 0;) {
600            yy[i] = (char) i;
601            unzftab[i] = 0;
602        }
603
604        int groupNo = 0;
605        int groupPos = G_SIZE - 1;
606        final int eob = this.nInUse + 1;
607        int nextSym = getAndMoveToFrontDecode0();
608        int lastShadow = -1;
609        int zt = selector[groupNo] & 0xff;
610        checkBounds(zt, N_GROUPS, "zt");
611        int[] base_zt = base[zt];
612        int[] limit_zt = limit[zt];
613        int[] perm_zt = perm[zt];
614        int minLens_zt = minLens[zt];
615
616        while (nextSym != eob) {
617            if ((nextSym == RUNA) || (nextSym == RUNB)) {
618                int s = -1;
619
620                for (int n = 1; true; n <<= 1) {
621                    if (nextSym == RUNA) {
622                        s += n;
623                    } else if (nextSym == RUNB) {
624                        s += n << 1;
625                    } else {
626                        break;
627                    }
628
629                    if (groupPos == 0) {
630                        groupPos = G_SIZE - 1;
631                        checkBounds(++groupNo, MAX_SELECTORS, "groupNo");
632                        zt = selector[groupNo] & 0xff;
633                        checkBounds(zt, N_GROUPS, "zt");
634                        base_zt = base[zt];
635                        limit_zt = limit[zt];
636                        perm_zt = perm[zt];
637                        minLens_zt = minLens[zt];
638                    } else {
639                        groupPos--;
640                    }
641
642                    int zn = minLens_zt;
643                    checkBounds(zn, MAX_ALPHA_SIZE, "zn");
644                    int zvec = bsR(bin, zn);
645                    while(zvec > limit_zt[zn]) {
646                        checkBounds(++zn, MAX_ALPHA_SIZE, "zn");
647                        zvec = (zvec << 1) | bsR(bin, 1);
648                    }
649                    final int tmp = zvec - base_zt[zn];
650                    checkBounds(tmp, MAX_ALPHA_SIZE, "zvec");
651                    nextSym = perm_zt[tmp];
652                }
653
654                final int yy0 = yy[0];
655                checkBounds(yy0, 256, "yy");
656                final byte ch = seqToUnseq[yy0];
657                unzftab[ch & 0xff] += s + 1;
658
659                final int from = ++lastShadow;
660                lastShadow += s;
661                Arrays.fill(ll8, from, lastShadow + 1, ch);
662
663                if (lastShadow >= limitLast) {
664                    throw new IOException("block overrun while expanding RLE in MTF, "
665                        + lastShadow + " exceeds " + limitLast);
666                }
667            } else {
668                if (++lastShadow >= limitLast) {
669                    throw new IOException("block overrun in MTF, "
670                        + lastShadow + " exceeds " + limitLast);
671                }
672                checkBounds(nextSym, 256 + 1, "nextSym");
673
674                final char tmp = yy[nextSym - 1];
675                checkBounds(tmp, 256, "yy");
676                unzftab[seqToUnseq[tmp] & 0xff]++;
677                ll8[lastShadow] = seqToUnseq[tmp];
678
679                /*
680                 * This loop is hammered during decompression, hence avoid
681                 * native method call overhead of System.arraycopy for very
682                 * small ranges to copy.
683                 */
684                if (nextSym <= 16) {
685                    for (int j = nextSym - 1; j > 0;) {
686                        yy[j] = yy[--j];
687                    }
688                } else {
689                    System.arraycopy(yy, 0, yy, 1, nextSym - 1);
690                }
691
692                yy[0] = tmp;
693
694                if (groupPos == 0) {
695                    groupPos = G_SIZE - 1;
696                    checkBounds(++groupNo, MAX_SELECTORS, "groupNo");
697                    zt = selector[groupNo] & 0xff;
698                    checkBounds(zt, N_GROUPS, "zt");
699                    base_zt = base[zt];
700                    limit_zt = limit[zt];
701                    perm_zt = perm[zt];
702                    minLens_zt = minLens[zt];
703                } else {
704                    groupPos--;
705                }
706
707                int zn = minLens_zt;
708                checkBounds(zn, MAX_ALPHA_SIZE, "zn");
709                int zvec = bsR(bin, zn);
710                while(zvec > limit_zt[zn]) {
711                    checkBounds(++zn, MAX_ALPHA_SIZE, "zn");
712                    zvec = (zvec << 1) | bsR(bin, 1);
713                }
714                final int idx = zvec - base_zt[zn];
715                checkBounds(idx, MAX_ALPHA_SIZE, "zvec");
716                nextSym = perm_zt[idx];
717            }
718        }
719
720        this.last = lastShadow;
721    }
722
723    private int getAndMoveToFrontDecode0() throws IOException {
724        final Data dataShadow = this.data;
725        final int zt = dataShadow.selector[0] & 0xff;
726        checkBounds(zt, N_GROUPS, "zt");
727        final int[] limit_zt = dataShadow.limit[zt];
728        int zn = dataShadow.minLens[zt];
729        checkBounds(zn, MAX_ALPHA_SIZE, "zn");
730        int zvec = bsR(bin, zn);
731        while (zvec > limit_zt[zn]) {
732            checkBounds(++zn, MAX_ALPHA_SIZE, "zn");
733            zvec = (zvec << 1) | bsR(bin, 1);
734        }
735        final int tmp = zvec - dataShadow.base[zt][zn];
736        checkBounds(tmp, MAX_ALPHA_SIZE, "zvec");
737
738        return dataShadow.perm[zt][tmp];
739    }
740
741    private int setupBlock() throws IOException {
742        if (currentState == EOF || this.data == null) {
743            return -1;
744        }
745
746        final int[] cftab = this.data.cftab;
747        final int ttLen = this.last + 1;
748        final int[] tt = this.data.initTT(ttLen);
749        final byte[] ll8 = this.data.ll8;
750        cftab[0] = 0;
751        System.arraycopy(this.data.unzftab, 0, cftab, 1, 256);
752
753        for (int i = 1, c = cftab[0]; i <= 256; i++) {
754            c += cftab[i];
755            cftab[i] = c;
756        }
757
758        for (int i = 0, lastShadow = this.last; i <= lastShadow; i++) {
759            final int tmp = cftab[ll8[i] & 0xff]++;
760            checkBounds(tmp, ttLen, "tt index");
761            tt[tmp] = i;
762        }
763
764        if ((this.origPtr < 0) || (this.origPtr >= tt.length)) {
765            throw new IOException("stream corrupted");
766        }
767
768        this.su_tPos = tt[this.origPtr];
769        this.su_count = 0;
770        this.su_i2 = 0;
771        this.su_ch2 = 256; /* not a char and not EOF */
772
773        if (this.blockRandomised) {
774            this.su_rNToGo = 0;
775            this.su_rTPos = 0;
776            return setupRandPartA();
777        }
778        return setupNoRandPartA();
779    }
780
781    private int setupRandPartA() throws IOException {
782        if (this.su_i2 <= this.last) {
783            this.su_chPrev = this.su_ch2;
784            int su_ch2Shadow = this.data.ll8[this.su_tPos] & 0xff;
785            checkBounds(this.su_tPos, this.data.tt.length, "su_tPos");
786            this.su_tPos = this.data.tt[this.su_tPos];
787            if (this.su_rNToGo == 0) {
788                this.su_rNToGo = Rand.rNums(this.su_rTPos) - 1;
789                if (++this.su_rTPos == 512) {
790                    this.su_rTPos = 0;
791                }
792            } else {
793                this.su_rNToGo--;
794            }
795            this.su_ch2 = su_ch2Shadow ^= (this.su_rNToGo == 1) ? 1 : 0;
796            this.su_i2++;
797            this.currentState = RAND_PART_B_STATE;
798            this.crc.updateCRC(su_ch2Shadow);
799            return su_ch2Shadow;
800        }
801        endBlock();
802        initBlock();
803        return setupBlock();
804    }
805
806    private int setupNoRandPartA() throws IOException {
807        if (this.su_i2 <= this.last) {
808            this.su_chPrev = this.su_ch2;
809            final int su_ch2Shadow = this.data.ll8[this.su_tPos] & 0xff;
810            this.su_ch2 = su_ch2Shadow;
811            checkBounds(this.su_tPos, this.data.tt.length, "su_tPos");
812            this.su_tPos = this.data.tt[this.su_tPos];
813            this.su_i2++;
814            this.currentState = NO_RAND_PART_B_STATE;
815            this.crc.updateCRC(su_ch2Shadow);
816            return su_ch2Shadow;
817        }
818        this.currentState = NO_RAND_PART_A_STATE;
819        endBlock();
820        initBlock();
821        return setupBlock();
822    }
823
824    private int setupRandPartB() throws IOException {
825        if (this.su_ch2 != this.su_chPrev) {
826            this.currentState = RAND_PART_A_STATE;
827            this.su_count = 1;
828            return setupRandPartA();
829        } else if (++this.su_count >= 4) {
830            this.su_z = (char) (this.data.ll8[this.su_tPos] & 0xff);
831            checkBounds(this.su_tPos, this.data.tt.length, "su_tPos");
832            this.su_tPos = this.data.tt[this.su_tPos];
833            if (this.su_rNToGo == 0) {
834                this.su_rNToGo = Rand.rNums(this.su_rTPos) - 1;
835                if (++this.su_rTPos == 512) {
836                    this.su_rTPos = 0;
837                }
838            } else {
839                this.su_rNToGo--;
840            }
841            this.su_j2 = 0;
842            this.currentState = RAND_PART_C_STATE;
843            if (this.su_rNToGo == 1) {
844                this.su_z ^= 1;
845            }
846            return setupRandPartC();
847        } else {
848            this.currentState = RAND_PART_A_STATE;
849            return setupRandPartA();
850        }
851    }
852
853    private int setupRandPartC() throws IOException {
854        if (this.su_j2 < this.su_z) {
855            this.crc.updateCRC(this.su_ch2);
856            this.su_j2++;
857            return this.su_ch2;
858        }
859        this.currentState = RAND_PART_A_STATE;
860        this.su_i2++;
861        this.su_count = 0;
862        return setupRandPartA();
863    }
864
865    private int setupNoRandPartB() throws IOException {
866        if (this.su_ch2 != this.su_chPrev) {
867            this.su_count = 1;
868            return setupNoRandPartA();
869        } else if (++this.su_count >= 4) {
870            checkBounds(this.su_tPos, this.data.ll8.length, "su_tPos");
871            this.su_z = (char) (this.data.ll8[this.su_tPos] & 0xff);
872            this.su_tPos = this.data.tt[this.su_tPos];
873            this.su_j2 = 0;
874            return setupNoRandPartC();
875        } else {
876            return setupNoRandPartA();
877        }
878    }
879
880    private int setupNoRandPartC() throws IOException {
881        if (this.su_j2 < this.su_z) {
882            final int su_ch2Shadow = this.su_ch2;
883            this.crc.updateCRC(su_ch2Shadow);
884            this.su_j2++;
885            this.currentState = NO_RAND_PART_C_STATE;
886            return su_ch2Shadow;
887        }
888        this.su_i2++;
889        this.su_count = 0;
890        return setupNoRandPartA();
891    }
892
893    private static final class Data {
894
895        // (with blockSize 900k)
896        final boolean[] inUse = new boolean[256]; // 256 byte
897
898        final byte[] seqToUnseq = new byte[256]; // 256 byte
899        final byte[] selector = new byte[MAX_SELECTORS]; // 18002 byte
900        final byte[] selectorMtf = new byte[MAX_SELECTORS]; // 18002 byte
901
902        /**
903         * Freq table collected to save a pass over the data during
904         * decompression.
905         */
906        final int[] unzftab = new int[256]; // 1024 byte
907
908        final int[][] limit = new int[N_GROUPS][MAX_ALPHA_SIZE]; // 6192 byte
909        final int[][] base = new int[N_GROUPS][MAX_ALPHA_SIZE]; // 6192 byte
910        final int[][] perm = new int[N_GROUPS][MAX_ALPHA_SIZE]; // 6192 byte
911        final int[] minLens = new int[N_GROUPS]; // 24 byte
912
913        final int[] cftab = new int[257]; // 1028 byte
914        final char[] getAndMoveToFrontDecode_yy = new char[256]; // 512 byte
915        final char[][] temp_charArray2d = new char[N_GROUPS][MAX_ALPHA_SIZE]; // 3096
916        // byte
917        final byte[] recvDecodingTables_pos = new byte[N_GROUPS]; // 6 byte
918        // ---------------
919        // 60798 byte
920
921        int[] tt; // 3600000 byte
922        byte[] ll8; // 900000 byte
923
924        // ---------------
925        // 4560782 byte
926        // ===============
927
928        Data(final int blockSize100k) {
929            this.ll8 = new byte[blockSize100k * BZip2Constants.BASEBLOCKSIZE];
930        }
931
932        /**
933         * Initializes the {@link #tt} array.
934         *
935         * This method is called when the required length of the array is known.
936         * I don't initialize it at construction time to avoid unneccessary
937         * memory allocation when compressing small files.
938         */
939        int[] initTT(final int length) {
940            int[] ttShadow = this.tt;
941
942            // tt.length should always be >= length, but theoretically
943            // it can happen, if the compressor mixed small and large
944            // blocks. Normally only the last block will be smaller
945            // than others.
946            if ((ttShadow == null) || (ttShadow.length < length)) {
947                this.tt = ttShadow = new int[length];
948            }
949
950            return ttShadow;
951        }
952
953    }
954
955    /**
956     * Checks if the signature matches what is expected for a bzip2 file.
957     *
958     * @param signature
959     *            the bytes to check
960     * @param length
961     *            the number of bytes to check
962     * @return true, if this stream is a bzip2 compressed stream, false otherwise
963     *
964     * @since 1.1
965     */
966    public static boolean matches(final byte[] signature, final int length) {
967        return length >= 3 && signature[0] == 'B' &&
968                signature[1] == 'Z' && signature[2] == 'h';
969    }
970}