Beispiel #1
0
 /// <exception cref="System.IO.IOException"/>
 public BZip2CompressionInputStream(InputStream @in, long start, long end, SplittableCompressionCodec.READ_MODE
                                    readMode)
     : base(@in, start, end)
 {
     // class data starts here//
     // Following state machine handles different states of compressed stream
     // position
     // HOLD : Don't advertise compressed stream position
     // ADVERTISE : Read 1 more character and advertise stream position
     // See more comments about it before updatePos method.
     // class data ends here//
     needsReset       = false;
     bufferedIn       = new BufferedInputStream(base.@in);
     this.startingPos = base.GetPos();
     this.readMode    = readMode;
     if (this.startingPos == 0)
     {
         // We only strip header if it is start of file
         bufferedIn = ReadStreamHeader();
     }
     input = new CBZip2InputStream(bufferedIn, readMode);
     if (this.isHeaderStripped)
     {
         input.UpdateReportedByteCount(HeaderLen);
     }
     if (this.isSubHeaderStripped)
     {
         input.UpdateReportedByteCount(SubHeaderLen);
     }
     this.UpdatePos(false);
 }
Beispiel #2
0
        /// <summary>
        ///     Uncompresses a BZIP2 file.
        /// </summary>
        /// <param name="bytes"> The compressed bytes without the header. </param>
        /// <returns> The uncompressed bytes. </returns>
        /// <exception cref="IOException"> if an I/O error occurs. </exception>
        public static byte[] Bunzip2(byte[] bytes)
        {
            /* prepare a new byte array with the bzip2 header at the start */
            var bzip2 = new byte[bytes.Length + 2];

            bzip2[0] = (byte)'h';
            bzip2[1] = (byte)'1';
            Array.Copy(bytes, 0, bzip2, 2, bytes.Length);

            InputStream @is = new CBZip2InputStream(new ByteArrayInputStream(bzip2));

            try
            {
                var os = new ByteArrayOutputStream();
                try
                {
                    var buf = new byte[4096];
                    int len;
                    while ((len = @is.read(buf, 0, buf.Length)) != -1)
                    {
                        os.write(buf, 0, len);
                    }
                }
                finally
                {
                    os.close();
                }

                return(os.toByteArray());
            }
            finally
            {
                @is.close();
            }
        }
Beispiel #3
0
 /// <exception cref="System.IO.IOException"/>
 private void InternalReset()
 {
     if (needsReset)
     {
         needsReset = false;
         BufferedInputStream bufferedIn = ReadStreamHeader();
         input = new CBZip2InputStream(bufferedIn, this.readMode);
     }
 }
Beispiel #4
0
        public static byte[] UncompressData(byte[] data)
        {
            using (CBZip2InputStream s = new CBZip2InputStream(new MemoryStream(data)))
            {
                using (MemoryStream uncompressed = new MemoryStream())
                {
                    int b;
                    while ((b = s.ReadByte()) != -1)
                    {
                        uncompressed.WriteByte((byte)b);
                    }

                    byte[] udata = new byte[uncompressed.Length];
                    uncompressed.Seek(0, SeekOrigin.Begin);
                    uncompressed.Read(udata, 0, (int)uncompressed.Length);
                    return(udata);
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Creates CompressionInputStream to be used to read off uncompressed data
        /// in one of the two reading modes.
        /// </summary>
        /// <remarks>
        /// Creates CompressionInputStream to be used to read off uncompressed data
        /// in one of the two reading modes. i.e. Continuous or Blocked reading modes
        /// </remarks>
        /// <param name="seekableIn">The InputStream</param>
        /// <param name="start">The start offset into the compressed stream</param>
        /// <param name="end">The end offset into the compressed stream</param>
        /// <param name="readMode">
        /// Controls whether progress is reported continuously or
        /// only at block boundaries.
        /// </param>
        /// <returns>CompressionInputStream for BZip2 aligned at block boundaries</returns>
        /// <exception cref="System.IO.IOException"/>
        public virtual SplitCompressionInputStream CreateInputStream(InputStream seekableIn
                                                                     , Decompressor decompressor, long start, long end, SplittableCompressionCodec.READ_MODE
                                                                     readMode)
        {
            if (!(seekableIn is Seekable))
            {
                throw new IOException("seekableIn must be an instance of " + typeof(Seekable).FullName
                                      );
            }
            //find the position of first BZip2 start up marker
            ((Seekable)seekableIn).Seek(0);
            // BZip2 start of block markers are of 6 bytes.  But the very first block
            // also has "BZh9", making it 10 bytes.  This is the common case.  But at
            // time stream might start without a leading BZ.
            long FirstBzip2BlockMarkerPosition = CBZip2InputStream.NumberOfBytesTillNextMarker
                                                     (seekableIn);
            long adjStart = Math.Max(0L, start - FirstBzip2BlockMarkerPosition);

            ((Seekable)seekableIn).Seek(adjStart);
            SplitCompressionInputStream @in = new BZip2Codec.BZip2CompressionInputStream(seekableIn
                                                                                         , adjStart, end, readMode);

            // The following if clause handles the following case:
            // Assume the following scenario in BZip2 compressed stream where
            // . represent compressed data.
            // .....[48 bit Block].....[48 bit   Block].....[48 bit Block]...
            // ........................[47 bits][1 bit].....[48 bit Block]...
            // ................................^[Assume a Byte alignment here]
            // ........................................^^[current position of stream]
            // .....................^^[We go back 10 Bytes in stream and find a Block marker]
            // ........................................^^[We align at wrong position!]
            // ...........................................................^^[While this pos is correct]
            if (@in.GetPos() < start)
            {
                ((Seekable)seekableIn).Seek(start);
                @in = new BZip2Codec.BZip2CompressionInputStream(seekableIn, start, end, readMode
                                                                 );
            }
            return(@in);
        }
Beispiel #6
0
        internal ZipReturn LocalFileOpenReadStream(Stream zipFs, bool raw, out Stream readStream, out ulong streamSize, out ushort compressionMethod)
        {
            streamSize        = 0;
            compressionMethod = _compressionMethod;

            readStream = null;
            zipFs.Seek((long)_dataLocation, SeekOrigin.Begin);

            switch (_compressionMethod)
            {
            case 0:
                readStream = zipFs;
                streamSize = _compressedSize;     // same as UncompressedSize
                break;

            case 8:
                if (raw)
                {
                    readStream = zipFs;
                    streamSize = _compressedSize;
                }
                else
                {
                    //readStream = new ZlibBaseStream(zipFs, CompressionMode.Decompress, CompressionLevel.Default, ZlibStreamFlavor.DEFLATE, true);
                    readStream = new System.IO.Compression.DeflateStream(zipFs, System.IO.Compression.CompressionMode.Decompress, true);
                    streamSize = UncompressedSize;
                }

                break;

            case 9:
                readStream = new Deflate64Stream(zipFs, System.IO.Compression.CompressionMode.Decompress);
                streamSize = UncompressedSize;
                break;

            //case 10:
            //    readStream = new BlastStream(zipFs);
            //    streamSize = UncompressedSize;
            //    break;

            case 12:
                readStream = new CBZip2InputStream(zipFs, false);
                streamSize = UncompressedSize;
                break;

            case 14:
            {
                zipFs.ReadByte();         // Major version
                zipFs.ReadByte();         // Minor version
                int    headerSize = zipFs.ReadByte() + (zipFs.ReadByte() << 8);
                byte[] header     = new byte[headerSize];
                zipFs.Read(header, 0, headerSize);
                readStream = new LzmaStream(header, zipFs);
                streamSize = UncompressedSize;
                break;
            }

            case 20:
            case 93:
                readStream = new ZstdSharp.DecompressionStream(zipFs);
                streamSize = UncompressedSize;
                break;

            case 98:
            {
                int    headerSize = 2;
                byte[] header     = new byte[headerSize];
                zipFs.Read(header, 0, headerSize);
                readStream = new PpmdStream(new PpmdProperties(header), zipFs, false);
                streamSize = UncompressedSize;
                break;
            }
            }

            return(readStream == null ? ZipReturn.ZipErrorGettingDataStream : ZipReturn.ZipGood);
        }
    private unsafe void SendMTFValues()
    {
        char[][] array = CBZip2InputStream.InitCharArray(6, 258);
        int      num   = 0;
        int      num2  = nInUse + 2;

        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < num2; j++)
            {
                array[i][j] = '\u000f';
            }
        }
        if (nMTF <= 0)
        {
            Panic();
        }
        int num3 = (nMTF < 200) ? 2 : ((nMTF < 600) ? 3 : ((nMTF < 1200) ? 4 : ((nMTF >= 2400) ? 6 : 5)));
        int num4 = num3;
        int num5 = nMTF;
        int num6 = 0;

        while (num4 > 0)
        {
            int num7 = num5 / num4;
            int num8 = num6 - 1;
            int k;
            for (k = 0; k < num7; k += mtfFreq[num8])
            {
                if (num8 >= num2 - 1)
                {
                    break;
                }
                num8++;
            }
            if (num8 > num6 && num4 != num3 && num4 != 1 && (num3 - num4) % 2 == 1)
            {
                k -= mtfFreq[num8];
                num8--;
            }
            for (int j = 0; j < num2; j++)
            {
                if (j >= num6 && j <= num8)
                {
                    array[num4 - 1][j] = '\0';
                }
                else
                {
                    array[num4 - 1][j] = '\u000f';
                }
            }
            num4--;
            num6  = num8 + 1;
            num5 -= k;
        }
        int[][] array2 = CBZip2InputStream.InitIntArray(6, 258);
        int[]   array3 = new int[6];
        short[] array4 = new short[6];
        for (int l = 0; l < 4; l++)
        {
            for (int i = 0; i < num3; i++)
            {
                array3[i] = 0;
            }
            for (int i = 0; i < num3; i++)
            {
                for (int j = 0; j < num2; j++)
                {
                    array2[i][j] = 0;
                }
            }
            num = 0;
            int num9 = 0;
            int num8;
            for (num6 = 0; num6 < nMTF; num6 = num8 + 1)
            {
                num8 = num6 + 50 - 1;
                if (num8 >= nMTF)
                {
                    num8 = nMTF - 1;
                }
                for (int i = 0; i < num3; i++)
                {
                    array4[i] = 0;
                }
                IntPtr intPtr;
                if (num3 == 6)
                {
                    short num14;
                    short num13;
                    short num12;
                    short num11;
                    short num10;
                    short num15 = num14 = (num13 = (num12 = (num11 = (num10 = 0))));
                    for (int m = num6; m <= num8; m++)
                    {
                        short num16 = szptr[m];
                        num15 = (short)(num15 + (short)array[0][num16]);
                        num14 = (short)(num14 + (short)array[1][num16]);
                        num13 = (short)(num13 + (short)array[2][num16]);
                        num12 = (short)(num12 + (short)array[3][num16]);
                        num11 = (short)(num11 + (short)array[4][num16]);
                        num10 = (short)(num10 + (short)array[5][num16]);
                    }
                    array4[0] = num15;
                    array4[1] = num14;
                    array4[2] = num13;
                    array4[3] = num12;
                    array4[4] = num11;
                    array4[5] = num10;
                }
                else
                {
                    for (int m = num6; m <= num8; m++)
                    {
                        short num17 = szptr[m];
                        for (int i = 0; i < num3; i++)
                        {
                            short[] array5;
                            short[] array6 = array5 = array4;
                            int     num18  = i;
                            intPtr        = (IntPtr)num18;
                            array6[num18] = (short)(array5[(long)intPtr] + (short)array[i][num17]);
                        }
                    }
                }
                int num19 = 999999999;
                int num20 = -1;
                for (int i = 0; i < num3; i++)
                {
                    if (array4[i] < num19)
                    {
                        num19 = array4[i];
                        num20 = i;
                    }
                }
                num9 += num19;
                int[] array7;
                int[] array8 = array7 = array3;
                int   num21  = num20;
                intPtr        = (IntPtr)num21;
                array8[num21] = array7[(long)intPtr] + 1;
                selector[num] = (char)num20;
                num++;
                for (int m = num6; m <= num8; m++)
                {
                    int[] array9 = array7 = array2[num20];
                    short num22  = szptr[m];
                    intPtr        = (IntPtr)(void *)num22;
                    array9[num22] = array7[(long)intPtr] + 1;
                }
            }
            for (int i = 0; i < num3; i++)
            {
                HbMakeCodeLengths(array[i], array2[i], num2, 20);
            }
        }
        array2 = null;
        array3 = null;
        array4 = null;
        if (num3 >= 8)
        {
            Panic();
        }
        if (num >= 32768 || num > 18002)
        {
            Panic();
        }
        char[] array10 = new char[6];
        for (int m = 0; m < num3; m++)
        {
            array10[m] = (char)m;
        }
        for (int m = 0; m < num; m++)
        {
            char c     = selector[m];
            int  num23 = 0;
            char c2    = array10[num23];
            while (c != c2)
            {
                num23++;
                char c3 = c2;
                c2             = array10[num23];
                array10[num23] = c3;
            }
            array10[0]     = c2;
            selectorMtf[m] = (char)num23;
        }
        int[][] array11 = CBZip2InputStream.InitIntArray(6, 258);
        for (int i = 0; i < num3; i++)
        {
            int num24 = 32;
            int num25 = 0;
            for (int m = 0; m < num2; m++)
            {
                if (array[i][m] > num25)
                {
                    num25 = array[i][m];
                }
                if (array[i][m] < num24)
                {
                    num24 = array[i][m];
                }
            }
            if (num25 > 20)
            {
                Panic();
            }
            if (num24 < 1)
            {
                Panic();
            }
            HbAssignCodes(array11[i], array[i], num24, num25, num2);
        }
        bool[] array12 = new bool[16];
        for (int m = 0; m < 16; m++)
        {
            array12[m] = false;
            for (int num23 = 0; num23 < 16; num23++)
            {
                if (inUse[m * 16 + num23])
                {
                    array12[m] = true;
                }
            }
        }
        for (int m = 0; m < 16; m++)
        {
            if (array12[m])
            {
                BsW(1, 1);
            }
            else
            {
                BsW(1, 0);
            }
        }
        for (int m = 0; m < 16; m++)
        {
            if (!array12[m])
            {
                continue;
            }
            for (int num23 = 0; num23 < 16; num23++)
            {
                if (inUse[m * 16 + num23])
                {
                    BsW(1, 1);
                }
                else
                {
                    BsW(1, 0);
                }
            }
        }
        BsW(3, num3);
        BsW(15, num);
        for (int m = 0; m < num; m++)
        {
            for (int num23 = 0; num23 < selectorMtf[m]; num23++)
            {
                BsW(1, 1);
            }
            BsW(1, 0);
        }
        for (int i = 0; i < num3; i++)
        {
            int n = array[i][0];
            BsW(5, n);
            for (int m = 0; m < num2; m++)
            {
                for (; n < array[i][m]; n++)
                {
                    BsW(2, 2);
                }
                while (n > array[i][m])
                {
                    BsW(2, 3);
                    n--;
                }
                BsW(1, 0);
            }
        }
        int num26 = 0;

        num6 = 0;
        while (num6 < nMTF)
        {
            int num8 = num6 + 50 - 1;
            if (num8 >= nMTF)
            {
                num8 = nMTF - 1;
            }
            for (int m = num6; m <= num8; m++)
            {
                BsW(array[selector[num26]][szptr[m]], array11[selector[num26]][szptr[m]]);
            }
            num6 = num8 + 1;
            num26++;
        }
        if (num26 != num)
        {
            Panic();
        }
    }