Exemplo n.º 1
0
        public static TarReader Open(Stream stream, Options options)
        {
            Utility.CheckNotNull(stream, "stream");
            RewindableStream stream2 = new RewindableStream(stream);

            stream2.StartRecording();
            if (GZipArchive.IsGZipFile(stream2))
            {
                stream2.Rewind(false);
                GZipStream stream3 = new GZipStream(stream2, CompressionMode.Decompress);
                if (!TarArchive.IsTarFile(stream3))
                {
                    throw new InvalidFormatException("Not a tar file.");
                }
                stream2.Rewind(true);
                return(new TarReader(stream2, CompressionType.GZip, options));
            }
            stream2.Rewind(false);
            if (BZip2Stream.IsBZip2(stream2))
            {
                stream2.Rewind(false);
                BZip2Stream stream4 = new BZip2Stream(stream2, CompressionMode.Decompress, false, false);
                if (!TarArchive.IsTarFile(stream4))
                {
                    throw new InvalidFormatException("Not a tar file.");
                }
                stream2.Rewind(true);
                return(new TarReader(stream2, CompressionType.BZip2, options));
            }
            stream2.Rewind(true);
            return(new TarReader(stream2, CompressionType.None, options));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Opens a Reader for Non-seeking usage
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IReader Open(Stream stream, ReaderOptions options = null)
        {
            stream.CheckNotNull(nameof(stream));
            options = options != null ?options: new ReaderOptions()
            {
                LeaveStreamOpen = false
            };
            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            rewindableStream.Rewind(false);
            if (GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.GZip));
                }
                rewindableStream.Rewind(true);
                return(GZipReader.Open(rewindableStream, options));
            }

            rewindableStream.Rewind(false);

            if (TarArchive.IsTarFile(rewindableStream))
            {
                rewindableStream.Rewind(true);
                return(TarReader.Open(rewindableStream, options));
            }
            rewindableStream.Rewind(false);

            throw new InvalidOperationException("Cannot determine compressed stream type.  Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar, LZip, XZ");
        }
Exemplo n.º 3
0
        internal BinaryReader FixStreamedFileLocation(ref RewindableStream rewindableStream)
        {
            if (Header.IsDirectory)
            {
                return(new BinaryReader(rewindableStream));
            }
            if (Header.HasData)
            {
                if (decompressionStream == null)
                {
                    decompressionStream = GetCompressedStream();
                }
                decompressionStream.SkipAll();

                DeflateStream deflateStream = decompressionStream as DeflateStream;
                if (deflateStream != null)
                {
                    rewindableStream.Rewind(deflateStream.InputBuffer);
                }
            }
            var reader = new BinaryReader(rewindableStream);

            decompressionStream = null;
            return(reader);
        }
Exemplo n.º 4
0
        internal BinaryReader FixStreamedFileLocation(ref RewindableStream rewindableStream)
        {
            if (Header.IsDirectory)
            {
                return(new BinaryReader(rewindableStream));
            }
            if (Header.HasData && !Skipped)
            {
                if (_decompressionStream == null)
                {
                    _decompressionStream = GetCompressedStream();
                }

                _decompressionStream.Skip();

                if (_decompressionStream is DeflateStream deflateStream)
                {
                    rewindableStream.Rewind(deflateStream.InputBuffer);
                }
                Skipped = true;
            }
            var reader = new BinaryReader(rewindableStream);

            _decompressionStream = null;
            return(reader);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Opens a TarReader for Non-seeking usage with a single volume
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static TarReader Open(Stream stream, Options options = Options.KeepStreamsOpen)
        {
            stream.CheckNotNull("stream");

            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            if (GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, CompressionType.GZip, options));
                }
                throw new InvalidFormatException("Not a tar file.");
            }

            rewindableStream.Rewind(false);
            if (BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, false);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, CompressionType.BZip2, options));
                }
                throw new InvalidFormatException("Not a tar file.");
            }
            rewindableStream.Rewind(true);
            return(new TarReader(rewindableStream, CompressionType.None, options));
        }
Exemplo n.º 6
0
        internal GZipFilePart(Stream stream)
        {
            RewindableStream rewind = new RewindableStream(stream);

            rewind.Recording = true;
            ReadAndValidateGzipHeader(rewind);
            rewind.Rewind();
            this.stream = new GZipStream(rewind, CompressionMode.Decompress);
        }
Exemplo n.º 7
0
        internal IEnumerable <ZipHeader> ReadStreamHeader(Stream stream)
        {
            RewindableStream rewindableStream;

            if (stream is RewindableStream)
            {
                rewindableStream = stream as RewindableStream;
            }
            else
            {
                rewindableStream = new RewindableStream(stream);
            }
            while (true)
            {
                ZipHeader    header = null;
                BinaryReader reader = new BinaryReader(rewindableStream);
                if (_lastEntryHeader != null &&
                    (FlagUtility.HasFlag(_lastEntryHeader.Flags, HeaderFlags.UsePostDataDescriptor) || _lastEntryHeader.IsZip64))
                {
                    reader = (_lastEntryHeader.Part as StreamingZipFilePart).FixStreamedFileLocation(ref rewindableStream);
                    long?pos = rewindableStream.CanSeek ? (long?)rewindableStream.Position : null;
                    uint crc = reader.ReadUInt32();
                    if (crc == POST_DATA_DESCRIPTOR)
                    {
                        crc = reader.ReadUInt32();
                    }
                    _lastEntryHeader.Crc              = crc;
                    _lastEntryHeader.CompressedSize   = reader.ReadUInt32();
                    _lastEntryHeader.UncompressedSize = reader.ReadUInt32();
                    if (pos.HasValue)
                    {
                        _lastEntryHeader.DataStartPosition = pos - _lastEntryHeader.CompressedSize;
                    }
                }
                _lastEntryHeader = null;
                uint headerBytes = reader.ReadUInt32();
                header = ReadHeader(headerBytes, reader);
                if (header == null)
                {
                    yield break;
                }

                //entry could be zero bytes so we need to know that.
                if (header.ZipHeaderType == ZipHeaderType.LocalEntry)
                {
                    bool isRecording = rewindableStream.IsRecording;
                    if (!isRecording)
                    {
                        rewindableStream.StartRecording();
                    }
                    uint nextHeaderBytes = reader.ReadUInt32();
                    header.HasData = !IsHeader(nextHeaderBytes);
                    rewindableStream.Rewind(!isRecording);
                }
                yield return(header);
            }
        }
Exemplo n.º 8
0
        private RewindableStream GetRewindableStream(Stream stream)
        {
            RewindableStream rewindableStream = stream as RewindableStream;

            if (rewindableStream == null)
            {
                rewindableStream = new RewindableStream(stream);
            }
            return(rewindableStream);
        }
Exemplo n.º 9
0
        private RewindableStream GetRewindableStream(Stream stream)
        {
            RewindableStream stream2 = stream as RewindableStream;

            if (stream2 == null)
            {
                stream2 = new RewindableStream(stream);
            }
            return(stream2);
        }
Exemplo n.º 10
0
        private Stream CheckSFX(Stream stream)
        {
            RewindableStream rewindableStream = this.GetRewindableStream(stream);

            stream = rewindableStream;
            BinaryReader reader = new BinaryReader(rewindableStream);

            try
            {
                bool flag;
                int  num = 0;
                goto Label_0138;
Label_001B:
                if (reader.ReadByte() == 0x52)
                {
                    MemoryStream stream3 = new MemoryStream();
                    byte[]       buffer  = reader.ReadBytes(3);
                    if (((buffer[0] == 0x45) && (buffer[1] == 0x7e)) && (buffer[2] == 0x5e))
                    {
                        stream3.WriteByte(0x52);
                        stream3.Write(buffer, 0, 3);
                        rewindableStream.Rewind(stream3);
                        return(stream);
                    }
                    byte[] buffer2 = reader.ReadBytes(3);
                    if (((((buffer[0] == 0x61) && (buffer[1] == 0x72)) && ((buffer[2] == 0x21) && (buffer2[0] == 0x1a))) && (buffer2[1] == 7)) && (buffer2[2] == 0))
                    {
                        stream3.WriteByte(0x52);
                        stream3.Write(buffer, 0, 3);
                        stream3.Write(buffer2, 0, 3);
                        rewindableStream.Rewind(stream3);
                        return(stream);
                    }
                    stream3.Write(buffer, 0, 3);
                    stream3.Write(buffer2, 0, 3);
                    rewindableStream.Rewind(stream3);
                }
                if (num > 0x7fff0)
                {
                    return(stream);
                }
Label_0138:
                flag = true;
                goto Label_001B;
            }
            catch (Exception exception)
            {
                if (!this.Options_HasFlag(SharpCompress.Common.Options.KeepStreamsOpen))
                {
                    reader.Close();
                }
                throw new InvalidFormatException("Error trying to read rar signature.", exception);
            }
            return(stream);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Opens a Reader for Non-seeking usage
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IReader Open(Stream stream, Options options = Options.KeepStreamsOpen)
        {
            stream.CheckNotNull("stream");

            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            if (ZipArchive.IsZipFile(rewindableStream, null))
            {
                rewindableStream.Rewind(true);
                return(ZipReader.Open(rewindableStream, null, options));
            }
            rewindableStream.Rewind(false);
            if (GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, CompressionType.GZip, options));
                }
                rewindableStream.Rewind(true);
                return(GZipReader.Open(rewindableStream, options));
            }

            rewindableStream.Rewind(false);
            if (BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, false);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, CompressionType.BZip2, options));
                }
            }

            rewindableStream.Rewind(false);
            if (TarArchive.IsTarFile(rewindableStream))
            {
                rewindableStream.Rewind(true);
                return(TarReader.Open(rewindableStream, options));
            }
            rewindableStream.Rewind(false);
            if (RarArchive.IsRarFile(rewindableStream, options))
            {
                rewindableStream.Rewind(true);
                return(RarReader.Open(rewindableStream, options));
            }

            throw new InvalidOperationException("Cannot determine compressed stream type.  Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar");
        }
Exemplo n.º 12
0
        public static IReader Open(Stream stream, Options options)
        {
            Utility.CheckNotNull(stream, "stream");
            RewindableStream stream2 = new RewindableStream(stream);

            stream2.StartRecording();
            if (ZipArchive.IsZipFile(stream2, null))
            {
                stream2.Rewind(true);
                return(ZipReader.Open(stream2, null, options));
            }
            stream2.Rewind(false);
            if (GZipArchive.IsGZipFile(stream2))
            {
                stream2.Rewind(false);
                GZipStream stream3 = new GZipStream(stream2, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(stream3))
                {
                    stream2.Rewind(true);
                    return(new TarReader(stream2, CompressionType.GZip, options));
                }
                stream2.Rewind(true);
                return(GZipReader.Open(stream2, options));
            }
            stream2.Rewind(false);
            if (BZip2Stream.IsBZip2(stream2))
            {
                stream2.Rewind(false);
                BZip2Stream stream4 = new BZip2Stream(stream2, CompressionMode.Decompress, false, false);
                if (TarArchive.IsTarFile(stream4))
                {
                    stream2.Rewind(true);
                    return(new TarReader(stream2, CompressionType.BZip2, options));
                }
            }
            stream2.Rewind(false);
            if (TarArchive.IsTarFile(stream2))
            {
                stream2.Rewind(true);
                return(TarReader.Open(stream2, options));
            }
            stream2.Rewind(false);
            if (!RarArchive.IsRarFile(stream2, options))
            {
                throw new InvalidOperationException("Cannot determine compressed stream type.  Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar");
            }
            stream2.Rewind(true);
            return(RarReader.Open(stream2, options));
        }
        internal IEnumerable <ZipHeader> ReadStreamHeader(Stream stream)
        {
            RewindableStream iteratorVariable0;

            if (stream is RewindableStream)
            {
                iteratorVariable0 = stream as RewindableStream;
            }
            else
            {
                iteratorVariable0 = new RewindableStream(stream);
            }
            while (true)
            {
                ZipHeader    iteratorVariable1 = null;
                BinaryReader reader            = new BinaryReader(iteratorVariable0);
                if ((this.lastEntryHeader != null) && FlagUtility.HasFlag <HeaderFlags>(this.lastEntryHeader.Flags, HeaderFlags.UsePostDataDescriptor))
                {
                    reader = (this.lastEntryHeader.Part as StreamingZipFilePart).FixStreamedFileLocation(ref iteratorVariable0);
                    long position = iteratorVariable0.Position;
                    uint num2     = reader.ReadUInt32();
                    if (num2 == 0x8074b50)
                    {
                        num2 = reader.ReadUInt32();
                    }
                    this.lastEntryHeader.Crc               = num2;
                    this.lastEntryHeader.CompressedSize    = reader.ReadUInt32();
                    this.lastEntryHeader.UncompressedSize  = reader.ReadUInt32();
                    this.lastEntryHeader.DataStartPosition = new long?(position - this.lastEntryHeader.CompressedSize);
                }
                this.lastEntryHeader = null;
                uint iteratorVariable3 = reader.ReadUInt32();
                iteratorVariable1 = this.ReadHeader(iteratorVariable3, reader);
                if (iteratorVariable1.ZipHeaderType == ZipHeaderType.LocalEntry)
                {
                    bool isRecording = iteratorVariable0.IsRecording;
                    if (!isRecording)
                    {
                        iteratorVariable0.StartRecording();
                    }
                    uint headerBytes = reader.ReadUInt32();
                    iteratorVariable1.HasData = !ZipHeaderFactory.IsHeader(headerBytes);
                    iteratorVariable0.Rewind(!isRecording);
                }
                yield return(iteratorVariable1);
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Opens a TarReader for Non-seeking usage with a single volume
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static TarReader Open(Stream stream, ReaderOptions options = null)
        {
            stream.CheckNotNull(nameof(stream));
            options = options ?? new ReaderOptions();
            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            if (GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.GZip));
                }
                throw new InvalidFormatException("Not a tar file.");
            }

            rewindableStream.Rewind(false);
            if (BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, false);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.BZip2));
                }
                throw new InvalidFormatException("Not a tar file.");
            }

            rewindableStream.Rewind(false);
            if (LZipStream.IsLZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                LZipStream testStream = new LZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.LZip));
                }
                throw new InvalidFormatException("Not a tar file.");
            }
            rewindableStream.Rewind(true);
            return(new TarReader(rewindableStream, options, CompressionType.None));
        }
Exemplo n.º 15
0
        /// <summary>
        /// Opens a Reader for Non-seeking usage
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="listener"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IReader OpenReader(Stream stream, IExtractionListener listener,
                                         Options options = Options.KeepStreamsOpen)
        {
            stream.CheckNotNull("stream");

            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.Recording = true;
            if (ZipArchive.IsZipFile(rewindableStream))
            {
                return(ZipReader.Open(rewindableStream, listener, options));
            }
            rewindableStream.Rewind();
            rewindableStream.Recording = true;
            if (GZipReader.IsGZip(rewindableStream))
            {
                rewindableStream.Rewind();
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                rewindableStream.Recording = true;
                if (TarReader.IsTarFile(testStream))
                {
                    rewindableStream.Rewind();
                    return(TarGZipReader.Open(rewindableStream, listener, options));
                }
                rewindableStream.Rewind();
                return(GZipReader.Open(rewindableStream, listener, options));
            }
            rewindableStream.Rewind();
            rewindableStream.Recording = true;
            if (TarReader.IsTarFile(rewindableStream))
            {
                rewindableStream.Rewind();
                return(TarReader.Open(rewindableStream, listener, options));
            }
            rewindableStream.Rewind();
            rewindableStream.Recording = true;
            if (RarArchive.IsRarFile(rewindableStream))
            {
                rewindableStream.Rewind();
                return(RarReader.Open(rewindableStream, listener, options));
            }
            throw new InvalidOperationException("Cannot determine compressed stream type.");
        }
        public void Test()
        {
            MemoryStream ms = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(ms);

            bw.Write(1);
            bw.Write(2);
            bw.Write(3);
            bw.Write(4);
            bw.Write(5);
            bw.Write(6);
            bw.Write(7);
            bw.Flush();
            ms.Position = 0;
            RewindableStream stream = new RewindableStream(ms);

            stream.Recording = true;
            BinaryReader br = new BinaryReader(stream);

            Debug.Assert(br.ReadInt32() == 1);
            Debug.Assert(br.ReadInt32() == 2);
            Debug.Assert(br.ReadInt32() == 3);
            Debug.Assert(br.ReadInt32() == 4);
            stream.Rewind();
            stream.Recording = true;
            Debug.Assert(br.ReadInt32() == 1);
            Debug.Assert(br.ReadInt32() == 2);
            Debug.Assert(br.ReadInt32() == 3);
            Debug.Assert(br.ReadInt32() == 4);
            Debug.Assert(br.ReadInt32() == 5);
            Debug.Assert(br.ReadInt32() == 6);
            Debug.Assert(br.ReadInt32() == 7);
            stream.Rewind();
            stream.Recording = true;
            Debug.Assert(br.ReadInt32() == 1);
            Debug.Assert(br.ReadInt32() == 2);
            Debug.Assert(br.ReadInt32() == 3);
            Debug.Assert(br.ReadInt32() == 4);
        }
Exemplo n.º 17
0
        public void TestRewind()
        {
            MemoryStream ms = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(ms);

            bw.Write(1);
            bw.Write(2);
            bw.Write(3);
            bw.Write(4);
            bw.Write(5);
            bw.Write(6);
            bw.Write(7);
            bw.Flush();
            ms.Position = 0;
            RewindableStream stream = new RewindableStream(ms);

            stream.StartRecording();
            BinaryReader br = new BinaryReader(stream);

            Assert.Equal(br.ReadInt32(), 1);
            Assert.Equal(br.ReadInt32(), 2);
            Assert.Equal(br.ReadInt32(), 3);
            Assert.Equal(br.ReadInt32(), 4);
            stream.Rewind(true);
            stream.StartRecording();
            Assert.Equal(br.ReadInt32(), 1);
            Assert.Equal(br.ReadInt32(), 2);
            Assert.Equal(br.ReadInt32(), 3);
            Assert.Equal(br.ReadInt32(), 4);
            Assert.Equal(br.ReadInt32(), 5);
            Assert.Equal(br.ReadInt32(), 6);
            Assert.Equal(br.ReadInt32(), 7);
            stream.Rewind(true);
            stream.StartRecording();
            Assert.Equal(br.ReadInt32(), 1);
            Assert.Equal(br.ReadInt32(), 2);
            Assert.Equal(br.ReadInt32(), 3);
            Assert.Equal(br.ReadInt32(), 4);
        }
Exemplo n.º 18
0
        public void TestIncompleteRewind()
        {
            MemoryStream ms = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(ms);

            bw.Write(1);
            bw.Write(2);
            bw.Write(3);
            bw.Write(4);
            bw.Write(5);
            bw.Write(6);
            bw.Write(7);
            bw.Flush();
            ms.Position = 0;
            RewindableStream stream = new RewindableStream(ms);

            stream.StartRecording();
            BinaryReader br = new BinaryReader(stream);

            Assert.Equal(1, br.ReadInt32());
            Assert.Equal(2, br.ReadInt32());
            Assert.Equal(3, br.ReadInt32());
            Assert.Equal(4, br.ReadInt32());
            stream.Rewind(true);
            Assert.Equal(1, br.ReadInt32());
            Assert.Equal(2, br.ReadInt32());
            stream.StartRecording();
            Assert.Equal(3, br.ReadInt32());
            Assert.Equal(4, br.ReadInt32());
            Assert.Equal(5, br.ReadInt32());
            stream.Rewind(true);
            Assert.Equal(3, br.ReadInt32());
            Assert.Equal(4, br.ReadInt32());
            Assert.Equal(5, br.ReadInt32());
            Assert.Equal(6, br.ReadInt32());
            Assert.Equal(7, br.ReadInt32());
        }
Exemplo n.º 19
0
        public static IReader Open(Stream stream, Options options = 1)
        {
            stream.CheckNotNull("stream");
            RewindableStream stream2 = new RewindableStream(stream);

            stream2.StartRecording();
            stream2.Rewind(false);
            if (BZip2Stream.IsBZip2(stream2))
            {
                stream2.Rewind(false);
                if (TarArchive.IsTarFile(new BZip2Stream(stream2, CompressionMode.Decompress, false, false)))
                {
                    stream2.Rewind(true);
                    return(new TarReader(stream2, CompressionType.BZip2, options));
                }
            }
            stream2.Rewind(false);
            if (!TarArchive.IsTarFile(stream2))
            {
                throw new InvalidOperationException("Cannot determine compressed stream type.");
            }
            stream2.Rewind(true);
            return(TarReader.Open(stream2, options));
        }
Exemplo n.º 20
0
        public static Stream DecodeSin(Stream stream, long totalSize)
        {
            var rewindable = new RewindableStream(stream);

            rewindable.StartRecording();
            var reader  = new BinaryReader(rewindable);
            var version = reader.ReadByte();

            switch (version)
            {
            case 1:
            case 2:
                throw new Exception($"SIN version 1 and 2 not supported.");

            case 3:
                rewindable.Rewind(true);
                var tempStream = TempFileStream.Create();
                SinParserV3.CopyTo(reader, tempStream);
                return(tempStream);
            }

            rewindable.Rewind(true);
            return(DecodeTarSin(rewindable, totalSize));
        }
Exemplo n.º 21
0
        internal BinaryReader FixStreamedFileLocation(ref RewindableStream rewindableStream)
        {
            if (base.Header.IsDirectory)
            {
                return(new BinaryReader(rewindableStream));
            }
            if (base.Header.HasData)
            {
                if (this.decompressionStream == null)
                {
                    this.decompressionStream = this.GetCompressedStream();
                }
                Utility.SkipAll(this.decompressionStream);
                DeflateStream decompressionStream = this.decompressionStream as DeflateStream;
                if (decompressionStream != null)
                {
                    rewindableStream.Rewind(decompressionStream.InputBuffer);
                }
            }
            BinaryReader reader = new BinaryReader(rewindableStream);

            this.decompressionStream = null;
            return(reader);
        }
Exemplo n.º 22
0
        internal IEnumerable <RarHeader> ReadHeaders(Stream stream)
        {
            if (Options.HasFlag(Options.CheckForSFX))
            {
                RewindableStream rewindableStream = new RewindableStream(stream);
                rewindableStream.Recording = true;
                stream = rewindableStream;
                BinaryReader reader = new BinaryReader(rewindableStream);
                try
                {
                    int count = 0;
                    while (true)
                    {
                        byte firstByte = reader.ReadByte();
                        if (firstByte == 0x52)
                        {
                            byte[] nextThreeBytes = reader.ReadBytes(3);
                            if ((nextThreeBytes[0] == 0x45) &&
                                (nextThreeBytes[1] == 0x7E) &&
                                (nextThreeBytes[2] == 0x5E))
                            {
                                //old format and isvalid
                                rewindableStream.Rewind();
                                break;
                            }
                            byte[] secondThreeBytes = reader.ReadBytes(3);
                            if ((nextThreeBytes[0] == 0x61) &&
                                (nextThreeBytes[1] == 0x72) &&
                                (nextThreeBytes[2] == 0x21) &&
                                (secondThreeBytes[0] == 0x1A) &&
                                (secondThreeBytes[1] == 0x07) &&
                                (secondThreeBytes[2] == 0x00))
                            {
                                //new format and isvalid
                                rewindableStream.Rewind();
                                break;
                            }
                        }
                        if (count > MAX_SFX_SIZE)
                        {
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    if (!Options.HasFlag(Options.KeepStreamsOpen))
                    {
#if THREEFIVE
                        reader.Close();
#else
                        reader.Dispose();
#endif
                    }
                    throw new InvalidRarFormatException("Error trying to read rar signature.", e);
                }
            }
            RarHeader header;
            while ((header = ReadNextHeader(stream)) != null)
            {
                yield return(header);

                if (header.HeaderType == HeaderType.EndArchiveHeader)
                {
                    yield break; // the end?
                }
            }
        }
Exemplo n.º 23
0
        public static bool TryOpen(Stream stream, ReaderOptions options, ArchiveTypeMask archiveTypes, out IReader reader)
        {
            stream.CheckNotNull("stream");
            options = options ?? new ReaderOptions()
            {
                LeaveStreamOpen = false
            };
            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            if (archiveTypes.HasFlag(ArchiveTypeMask.Zip) && ZipArchive.IsZipFile(rewindableStream, options.Password))
            {
                rewindableStream.Rewind(true);
                reader = ZipReader.Open(rewindableStream, options);
                return(true);
            }
            rewindableStream.Rewind(false);
            if (archiveTypes.HasFlag(ArchiveTypeMask.GZip) && GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    reader = new TarReader(rewindableStream, options, CompressionType.GZip);
                    return(true);
                }
                rewindableStream.Rewind(true);
                reader = GZipReader.Open(rewindableStream, options);
                return(true);
            }

            rewindableStream.Rewind(false);
            if (archiveTypes.HasFlag(ArchiveTypeMask.BZip2) && BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(new NonDisposingStream(rewindableStream), CompressionMode.Decompress, false);
                if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    reader = new TarReader(rewindableStream, options, CompressionType.BZip2);
                    return(true);
                }
            }

            rewindableStream.Rewind(false);
            if (archiveTypes.HasFlag(ArchiveTypeMask.LZip) && LZipStream.IsLZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                LZipStream testStream = new LZipStream(new NonDisposingStream(rewindableStream), CompressionMode.Decompress);
                if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    reader = new TarReader(rewindableStream, options, CompressionType.LZip);
                    return(true);
                }
            }
            rewindableStream.Rewind(false);
            if (archiveTypes.HasFlag(ArchiveTypeMask.Rar) && RarArchive.IsRarFile(rewindableStream, options))
            {
                rewindableStream.Rewind(true);
                reader = RarReader.Open(rewindableStream, options);
                return(true);
            }

            rewindableStream.Rewind(false);
            if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(rewindableStream))
            {
                rewindableStream.Rewind(true);
                reader = TarReader.Open(rewindableStream, options);
                return(true);
            }
            rewindableStream.Rewind(false);
            if (XZStream.IsXZStream(rewindableStream))
            {
                rewindableStream.Rewind(true);
                XZStream testStream = new XZStream(rewindableStream);
                if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    reader = new TarReader(rewindableStream, options, CompressionType.Xz);
                    return(true);
                }
            }
            reader = null;
            return(false);
        }
Exemplo n.º 24
0
        internal IEnumerable <ZipHeader> ReadStreamHeader(Stream stream)
        {
            RewindableStream rewindableStream;

            if (stream is RewindableStream rs)
            {
                rewindableStream = rs;
            }
            else
            {
                rewindableStream = new RewindableStream(stream);
            }
            while (true)
            {
                ZipHeader    header;
                BinaryReader reader = new BinaryReader(rewindableStream);
                if (_lastEntryHeader != null &&
                    (FlagUtility.HasFlag(_lastEntryHeader.Flags, HeaderFlags.UsePostDataDescriptor) || _lastEntryHeader.IsZip64))
                {
                    reader = ((StreamingZipFilePart)_lastEntryHeader.Part).FixStreamedFileLocation(ref rewindableStream);
                    long?pos = rewindableStream.CanSeek ? (long?)rewindableStream.Position : null;
                    uint crc = reader.ReadUInt32();
                    if (crc == POST_DATA_DESCRIPTOR)
                    {
                        crc = reader.ReadUInt32();
                    }
                    _lastEntryHeader.Crc              = crc;
                    _lastEntryHeader.CompressedSize   = reader.ReadUInt32();
                    _lastEntryHeader.UncompressedSize = reader.ReadUInt32();
                    if (pos.HasValue)
                    {
                        _lastEntryHeader.DataStartPosition = pos - _lastEntryHeader.CompressedSize;
                    }
                }
                _lastEntryHeader = null;
                uint headerBytes = reader.ReadUInt32();
                header = ReadHeader(headerBytes, reader);
                if (header is null)
                {
                    yield break;
                }

                //entry could be zero bytes so we need to know that.
                if (header.ZipHeaderType == ZipHeaderType.LocalEntry)
                {
                    var local_header = ((LocalEntryHeader)header);

                    // If we have CompressedSize, there is data to be read
                    if (local_header.CompressedSize > 0)
                    {
                        header.HasData = true;
                    } // Check if zip is streaming ( Length is 0 and is declared in PostDataDescriptor )
                    else if (local_header.Flags.HasFlag(HeaderFlags.UsePostDataDescriptor))
                    {
                        bool isRecording = rewindableStream.IsRecording;
                        if (!isRecording)
                        {
                            rewindableStream.StartRecording();
                        }
                        uint nextHeaderBytes = reader.ReadUInt32();

                        // Check if next data is PostDataDescriptor, streamed file with 0 length
                        header.HasData = !IsHeader(nextHeaderBytes);
                        rewindableStream.Rewind(!isRecording);
                    }
                    else // We are not streaming and compressed size is 0, we have no data
                    {
                        header.HasData = false;
                    }
                }
                yield return(header);
            }
        }
Exemplo n.º 25
0
        private Stream CheckSFX(Stream stream)
        {
            RewindableStream rewindableStream = GetRewindableStream(stream);

            stream = rewindableStream;
            BinaryReader reader = new BinaryReader(rewindableStream);

            try
            {
                int count = 0;
                while (true)
                {
                    byte firstByte = reader.ReadByte();
                    if (firstByte == 0x52)
                    {
                        MemoryStream buffer         = new MemoryStream();
                        byte[]       nextThreeBytes = reader.ReadBytes(3);
                        if ((nextThreeBytes[0] == 0x45) &&
                            (nextThreeBytes[1] == 0x7E) &&
                            (nextThreeBytes[2] == 0x5E))
                        {
                            //old format and isvalid
                            buffer.WriteByte(0x52);
                            buffer.Write(nextThreeBytes, 0, 3);
                            rewindableStream.Rewind(buffer);
                            break;
                        }
                        byte[] secondThreeBytes = reader.ReadBytes(3);
                        if ((nextThreeBytes[0] == 0x61) &&
                            (nextThreeBytes[1] == 0x72) &&
                            (nextThreeBytes[2] == 0x21) &&
                            (secondThreeBytes[0] == 0x1A) &&
                            (secondThreeBytes[1] == 0x07) &&
                            (secondThreeBytes[2] == 0x00))
                        {
                            //new format and isvalid
                            buffer.WriteByte(0x52);
                            buffer.Write(nextThreeBytes, 0, 3);
                            buffer.Write(secondThreeBytes, 0, 3);
                            rewindableStream.Rewind(buffer);
                            break;
                        }
                        buffer.Write(nextThreeBytes, 0, 3);
                        buffer.Write(secondThreeBytes, 0, 3);
                        rewindableStream.Rewind(buffer);
                    }
                    if (count > MAX_SFX_SIZE)
                    {
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                if (!Options.LeaveStreamOpen)
                {
#if NET35
                    reader.Close();
#else
                    reader.Dispose();
#endif
                }
                throw new InvalidFormatException("Error trying to read rar signature.", e);
            }
            return(stream);
        }
Exemplo n.º 26
0
        internal BinaryReader FixStreamedFileLocation(ref RewindableStream rewindableStream)
        {
            if (Header.IsDirectory)
            {
                return(new BinaryReader(rewindableStream));
            }
            if (Header.HasData && !Skipped)
            {
                _decompressionStream ??= GetCompressedStream();

                if (Header.CompressionMethod != ZipCompressionMethod.None)
                {
                    _decompressionStream.Skip();

                    // If we had TotalIn / TotalOut we could have used them
                    Header.CompressedSize = _decompressionStream.Position;

                    if (_decompressionStream is DeflateStream deflateStream)
                    {
                        rewindableStream.Rewind(deflateStream.InputBuffer);
                    }
                }
                else
                {
                    // We would need to search for the magic word
                    rewindableStream.Position -= 4;
                    var pos = rewindableStream.Position;
                    while (Utility.Find(rewindableStream, new byte[] { 0x50, 0x4b, 0x07, 0x08 }))
                    {
                        // We should probably check CRC32 for positive matching as well
                        var size = rewindableStream.Position - pos;
                        var br   = new BinaryReader(rewindableStream);
                        br.ReadUInt32();
                        br.ReadUInt32(); // CRC32
                        var compressed_size    = br.ReadUInt32();
                        var uncompressed_size  = br.ReadUInt32();
                        var uncompressed_64bit = br.ReadInt64();

                        long test_64bit = (long)uncompressed_size << 32 | (long)compressed_size;

                        if (test_64bit == size && test_64bit == uncompressed_64bit)
                        {
                            Header.CompressedSize      = test_64bit;
                            Header.UncompressedSize    = uncompressed_64bit;
                            rewindableStream.Position -= 24;
                            break;
                        }

                        if (compressed_size == size && compressed_size == uncompressed_size)
                        {
                            Header.CompressedSize      = compressed_size;
                            Header.UncompressedSize    = uncompressed_size;
                            rewindableStream.Position -= 24;
                            break;
                        }
                        rewindableStream.Position -= 20;
                    }
                }

                Skipped = true;
            }
            var reader = new BinaryReader(rewindableStream);

            _decompressionStream = null;
            return(reader);
        }
Exemplo n.º 27
0
        /// <summary>
        /// Opens a Reader for Non-seeking usage
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IReader Open(Stream stream, ReaderOptions options = null)
        {
            stream.CheckNotNull("stream");
            options = options ?? new ReaderOptions()
            {
                LeaveStreamOpen = false
            };
            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            if (ZipArchive.IsZipFile(rewindableStream, options.Password))
            {
                rewindableStream.Rewind(true);
                return(ZipReader.Open(rewindableStream, options));
            }
            rewindableStream.Rewind(false);
            if (GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.GZip));
                }
                rewindableStream.Rewind(true);
                return(GZipReader.Open(rewindableStream, options));
            }

            rewindableStream.Rewind(false);
            if (BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, true);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.BZip2));
                }
            }

            rewindableStream.Rewind(false);
            if (LZipStream.IsLZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                LZipStream testStream = new LZipStream(rewindableStream, CompressionMode.Decompress, true);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.LZip));
                }
            }
            rewindableStream.Rewind(false);
            if (RarArchive.IsRarFile(rewindableStream, options))
            {
                rewindableStream.Rewind(true);
                return(RarReader.Open(rewindableStream, options));
            }

            rewindableStream.Rewind(false);
            if (TarArchive.IsTarFile(rewindableStream))
            {
                rewindableStream.Rewind(true);
                return(TarReader.Open(rewindableStream, options));
            }
            rewindableStream.Rewind(false);
            if (XZStream.IsXZStream(rewindableStream))
            {
                rewindableStream.Rewind(true);
                XZStream testStream = new XZStream(rewindableStream);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.Xz));
                }
            }
            throw new InvalidOperationException("Cannot determine compressed stream type.  Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar, LZip, XZ");
        }