Exemplo n.º 1
1
 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.AreEqual(br.ReadInt32(), 1);
     Assert.AreEqual(br.ReadInt32(), 2);
     Assert.AreEqual(br.ReadInt32(), 3);
     Assert.AreEqual(br.ReadInt32(), 4);
     stream.Rewind(true);
     Assert.AreEqual(br.ReadInt32(), 1);
     Assert.AreEqual(br.ReadInt32(), 2);
     stream.StartRecording();
     Assert.AreEqual(br.ReadInt32(), 3);
     Assert.AreEqual(br.ReadInt32(), 4);
     Assert.AreEqual(br.ReadInt32(), 5);
     stream.Rewind(true);
     Assert.AreEqual(br.ReadInt32(), 3);
     Assert.AreEqual(br.ReadInt32(), 4);
     Assert.AreEqual(br.ReadInt32(), 5);
     Assert.AreEqual(br.ReadInt32(), 6);
     Assert.AreEqual(br.ReadInt32(), 7);
 }
Exemplo n.º 2
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.º 3
0
        public RewindableStream(Stream stream, MemoryStream bufferStream)
        {
            this.bufferStream = new MemoryStream();
            RewindableStream stream2 = stream as RewindableStream;

            this.stream       = ((stream2 == null) || stream2.isRewound) ? stream : stream2.stream;
            this.bufferStream = bufferStream;
            if (bufferStream.Position != bufferStream.Length)
            {
                this.isRewound = true;
            }
        }
        internal IEnumerable<ZipHeader> ReadStreamHeader(Stream stream)
        {
            RewindableStream rewindableStream;

            if (stream is NonDisposingStream)
            {
            stream = ((NonDisposingStream) stream).Stream;
            }
            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))
                {
                    reader = (lastEntryHeader.Part as StreamingZipFilePart).FixStreamedFileLocation(ref rewindableStream);
                    long pos = rewindableStream.Position;
                    uint crc = reader.ReadUInt32();
                    if (crc == POST_DATA_DESCRIPTOR)
                    {
                        crc = reader.ReadUInt32();
                    }
                    lastEntryHeader.Crc = crc;
                    lastEntryHeader.CompressedSize = reader.ReadUInt32();
                    lastEntryHeader.UncompressedSize = reader.ReadUInt32();
                    lastEntryHeader.DataStartPosition = pos - lastEntryHeader.CompressedSize;
                }
                lastEntryHeader = null;
                uint headerBytes = reader.ReadUInt32();
                header = ReadHeader(headerBytes, reader);

                //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.º 5
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.");
        }
Exemplo n.º 6
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.º 8
0
        public RewindableStream(Stream stream, MemoryStream bufferStream)
        {
            RewindableStream parent = stream as RewindableStream;

            if (parent != null && !parent.isRewound)
            {
                this.stream = parent.stream;
            }
            else
            {
                this.stream = stream;
            }
            this.bufferStream = bufferStream;
            if (bufferStream.Position != bufferStream.Length)
            {
                isRewound = true;
            }
        }
Exemplo n.º 9
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.");
        }
Exemplo n.º 10
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.º 11
0
 internal BinaryReader FixStreamedFileLocation(ref RewindableStream rewindableStream)
 {
     if (Header.IsDirectory)
     {
         return new BinaryReader(rewindableStream);
     }
     if (Header.HasData)
     {
         if (decompressionStream == null)
         {
             decompressionStream = GetCompressedStream();
         }
         //decompressionStream.SkipAll();
         Utility.SkipAll(decompressionStream);
         DeflateStream deflateStream = decompressionStream as DeflateStream;
         if (deflateStream != null)
         {
             rewindableStream.Rewind(deflateStream.InputBuffer);
         }
     }
     var reader = new BinaryReader(rewindableStream);
     decompressionStream = null;
     return reader;
 }
 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.º 13
0
 private RewindableStream GetRewindableStream(Stream stream)
 {
     RewindableStream rewindableStream = stream as RewindableStream;
     if (rewindableStream == null)
     {
         rewindableStream = new RewindableStream(stream);
     }
     return rewindableStream;
 }
Exemplo n.º 14
0
 private RewindableStream GetRewindableStream(Stream stream)
 {
     RewindableStream stream2 = stream as RewindableStream;
     if (stream2 == null)
     {
         stream2 = new RewindableStream(stream);
     }
     return stream2;
 }
Exemplo n.º 15
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?
         }
     }
 }