private RarHeader ReadNextHeader(Stream stream)
        {
            FileHeader            header4;
            RarCryptoBinaryReader reader = new RarCryptoBinaryReader(stream, this.Password);

            if (this.IsEncrypted)
            {
                if (this.Password == null)
                {
                    throw new CryptographicException("Encrypted Rar archive has no password specified.");
                }
                reader.SkipQueue();
                byte[] salt = reader.ReadBytes(8);
                reader.InitializeAes(salt);
            }
            RarHeader header = RarHeader.Create(reader);

            if (header == null)
            {
                return(null);
            }
            switch (header.HeaderType)
            {
            case HeaderType.MarkHeader:
                return(header.PromoteHeader <MarkHeader>(reader));

            case HeaderType.ArchiveHeader:
            {
                ArchiveHeader header2 = header.PromoteHeader <ArchiveHeader>(reader);
                this.IsEncrypted = header2.HasPassword;
                return(header2);
            }

            case HeaderType.FileHeader:
                header4 = header.PromoteHeader <FileHeader>(reader);
                switch (this.StreamingMode)
                {
                case SharpCompress.IO.StreamingMode.Streaming:
                {
                    ReadOnlySubStream actualStream = new ReadOnlySubStream(reader.BaseStream, header4.CompressedSize);
                    if (header4.Salt == null)
                    {
                        header4.PackedStream = actualStream;
                        return(header4);
                    }
                    header4.PackedStream = new RarCryptoWrapper(actualStream, this.Password, header4.Salt);
                    return(header4);
                }

                case SharpCompress.IO.StreamingMode.Seekable:
                {
                    header4.DataStartPosition = reader.BaseStream.Position;
                    Stream baseStream = reader.BaseStream;
                    baseStream.Position += header4.CompressedSize;
                    return(header4);
                }
                }
                throw new InvalidFormatException("Invalid StreamingMode");

            case HeaderType.ProtectHeader:
            {
                ProtectHeader header3 = header.PromoteHeader <ProtectHeader>(reader);
                switch (this.StreamingMode)
                {
                case SharpCompress.IO.StreamingMode.Streaming:
                    Utility.Skip(reader.BaseStream, (long)header3.DataSize);
                    return(header3);

                case SharpCompress.IO.StreamingMode.Seekable:
                {
                    Stream stream1 = reader.BaseStream;
                    stream1.Position += header3.DataSize;
                    return(header3);
                }
                }
                throw new InvalidFormatException("Invalid StreamingMode");
            }

            case HeaderType.NewSubHeader:
                header4 = header.PromoteHeader <FileHeader>(reader);
                switch (this.StreamingMode)
                {
                case SharpCompress.IO.StreamingMode.Streaming:
                    Utility.Skip(reader.BaseStream, header4.CompressedSize);
                    return(header4);

                case SharpCompress.IO.StreamingMode.Seekable:
                {
                    header4.DataStartPosition = reader.BaseStream.Position;
                    Stream stream3 = reader.BaseStream;
                    stream3.Position += header4.CompressedSize;
                    return(header4);
                }
                }
                throw new InvalidFormatException("Invalid StreamingMode");

            case HeaderType.EndArchiveHeader:
                return(header.PromoteHeader <EndArchiveHeader>(reader));
            }
            throw new InvalidFormatException("Invalid Rar Header: " + header.HeaderType.ToString());
        }
Esempio n. 2
0
        private RarHeader ReadNextHeader(Stream stream)
        {
#if !NO_CRYPTO
            var reader = new RarCryptoBinaryReader(stream, Options.Password);

            if (IsEncrypted)
            {
                if (Options.Password == null)
                {
                    throw new CryptographicException("Encrypted Rar archive has no password specified.");
                }
                reader.SkipQueue();
                byte[] salt = reader.ReadBytes(8);
                reader.InitializeAes(salt);
            }
#else
            var reader = new MarkingBinaryReader(stream);
#endif

            RarHeader header = RarHeader.Create(reader);
            if (header == null)
            {
                return(null);
            }
            switch (header.HeaderType)
            {
            case HeaderType.ArchiveHeader:
            {
                var ah = header.PromoteHeader <ArchiveHeader>(reader);
                IsEncrypted = ah.HasPassword;
                return(ah);
            }

            case HeaderType.MarkHeader:
            {
                return(header.PromoteHeader <MarkHeader>(reader));
            }

            case HeaderType.ProtectHeader:
            {
                ProtectHeader ph = header.PromoteHeader <ProtectHeader>(reader);

                // skip the recovery record data, we do not use it.
                switch (StreamingMode)
                {
                case StreamingMode.Seekable:
                {
                    reader.BaseStream.Position += ph.DataSize;
                }
                break;

                case StreamingMode.Streaming:
                {
                    reader.BaseStream.Skip(ph.DataSize);
                }
                break;

                default:
                {
                    throw new InvalidFormatException("Invalid StreamingMode");
                }
                }

                return(ph);
            }

            case HeaderType.NewSubHeader:
            {
                FileHeader fh = header.PromoteHeader <FileHeader>(reader);
                switch (StreamingMode)
                {
                case StreamingMode.Seekable:
                {
                    fh.DataStartPosition        = reader.BaseStream.Position;
                    reader.BaseStream.Position += fh.CompressedSize;
                }
                break;

                case StreamingMode.Streaming:
                {
                    //skip the data because it's useless?
                    reader.BaseStream.Skip(fh.CompressedSize);
                }
                break;

                default:
                {
                    throw new InvalidFormatException("Invalid StreamingMode");
                }
                }
                return(fh);
            }

            case HeaderType.FileHeader:
            {
                FileHeader fh = header.PromoteHeader <FileHeader>(reader);
                switch (StreamingMode)
                {
                case StreamingMode.Seekable:
                {
                    fh.DataStartPosition        = reader.BaseStream.Position;
                    reader.BaseStream.Position += fh.CompressedSize;
                }
                break;

                case StreamingMode.Streaming:
                {
                    var ms = new ReadOnlySubStream(reader.BaseStream, fh.CompressedSize);
                    if (fh.Salt == null)
                    {
                        fh.PackedStream = ms;
                    }
                    else
                    {
#if !NO_CRYPTO
                        fh.PackedStream = new RarCryptoWrapper(ms, Options.Password, fh.Salt);
#else
                        throw new NotSupportedException("RarCrypto not supported");
#endif
                    }
                }
                break;

                default:
                {
                    throw new InvalidFormatException("Invalid StreamingMode");
                }
                }
                return(fh);
            }

            case HeaderType.EndArchiveHeader:
            {
                return(header.PromoteHeader <EndArchiveHeader>(reader));
            }

            default:
            {
                throw new InvalidFormatException("Invalid Rar Header: " + header.HeaderType);
            }
            }
        }
        private RarHeader ReadNextHeader(Stream stream)
        {
            #if PORTABLE
            var reader = new MarkingBinaryReader(stream);
            #else
            var reader = new RarCryptoBinaryReader(stream, Password);

            if (IsEncrypted)
            {
                reader.SkipQueue();
                byte[] salt = reader.ReadBytes(8);
                reader.InitializeAes(salt);
            }
            #endif

            RarHeader header = RarHeader.Create(reader);
            if (header == null)
            {
                return null;
            }
            switch (header.HeaderType)
            {
                case HeaderType.ArchiveHeader:
                    {
                        var ah = header.PromoteHeader<ArchiveHeader>(reader);
                        IsEncrypted = ah.HasPassword;
                        return ah;
                    }
                case HeaderType.MarkHeader:
                    {
                        return header.PromoteHeader<MarkHeader>(reader);
                    }
                case HeaderType.NewSubHeader:
                    {
                        FileHeader fh = header.PromoteHeader<FileHeader>(reader);
                        switch (StreamingMode)
                        {
                            case StreamingMode.Seekable:
                                {
                                    fh.DataStartPosition = reader.BaseStream.Position;
                                    reader.BaseStream.Position += fh.CompressedSize;
                                }
                                break;
                            case StreamingMode.Streaming:
                                {
                                    //skip the data because it's useless?
                                    reader.BaseStream.Skip(fh.CompressedSize);
                                }
                                break;
                            default:
                                {
                                    throw new InvalidFormatException("Invalid StreamingMode");
                                }
                        }
                        return fh;
                    }
                case HeaderType.FileHeader:
                    {
                        FileHeader fh = header.PromoteHeader<FileHeader>(reader);
                        switch (StreamingMode)
                        {
                            case StreamingMode.Seekable:
                                {
                                    fh.DataStartPosition = reader.BaseStream.Position;
                                    reader.BaseStream.Position += fh.CompressedSize;
                                }
                                break;
                            case StreamingMode.Streaming:
                                {
                                    var ms = new ReadOnlySubStream(reader.BaseStream, fh.CompressedSize);
                                    if (fh.Salt == null)
                                    {
                                        fh.PackedStream = ms;
                                    }
                                    else
                                    {
            #if PORTABLE
                                        throw new NotSupportedException("Encrypted Rar files aren't supported in portable distro.");
            #else
                                        fh.PackedStream = new RarCryptoWrapper(ms, Password,  fh.Salt);
            #endif
                                    }
                                }
                                break;
                            default:
                                {
                                    throw new InvalidFormatException("Invalid StreamingMode");
                                }
                        }
                        return fh;
                    }
                case HeaderType.EndArchiveHeader:
                    {
                        return header.PromoteHeader<EndArchiveHeader>(reader);
                    }
                default:
                    {
                        throw new InvalidFormatException("Invalid Rar Header: " + header.HeaderType.ToString());
                    }
            }
        }
        private RarHeader ReadNextHeader(Stream stream)
        {
            #if !NO_CRYPTO
            var reader = new RarCryptoBinaryReader(stream, Password);

            if (IsEncrypted)
            {
                if (Password == null)
                {
                    throw new CryptographicException("Encrypted Rar archive has no password specified.");
                }
                reader.SkipQueue();
                byte[] salt = reader.ReadBytes(8);
                reader.InitializeAes(salt);
            }
            #else
            var reader = new MarkingBinaryReader(stream);

            #endif

            RarHeader header = RarHeader.Create(reader);
            if (header == null)
            {
                return null;
            }
            switch (header.HeaderType)
            {
                case HeaderType.ArchiveHeader:
                    {
                        var ah = header.PromoteHeader<ArchiveHeader>(reader);
                        IsEncrypted = ah.HasPassword;
                        return ah;
                    }
                case HeaderType.MarkHeader:
                    {
                        return header.PromoteHeader<MarkHeader>(reader);
                    }

                case HeaderType.ProtectHeader:
                    {
                        ProtectHeader ph = header.PromoteHeader<ProtectHeader>(reader);

                        // skip the recovery record data, we do not use it.
                        switch (StreamingMode)
                        {
                            case StreamingMode.Seekable:
                                {
                                    reader.BaseStream.Position += ph.DataSize;
                                }
                                break;
                            case StreamingMode.Streaming:
                                {
                                    reader.BaseStream.Skip(ph.DataSize);
                                }
                                break;
                            default:
                                {
                                    throw new InvalidFormatException("Invalid StreamingMode");
                                }
                        }

                        return ph;
                    }

                case HeaderType.NewSubHeader:
                    {
                        FileHeader fh = header.PromoteHeader<FileHeader>(reader);
                        switch (StreamingMode)
                        {
                            case StreamingMode.Seekable:
                                {
                                    fh.DataStartPosition = reader.BaseStream.Position;
                                    reader.BaseStream.Position += fh.CompressedSize;
                                }
                                break;
                            case StreamingMode.Streaming:
                                {
                                    //skip the data because it's useless?
                                    reader.BaseStream.Skip(fh.CompressedSize);
                                }
                                break;
                            default:
                                {
                                    throw new InvalidFormatException("Invalid StreamingMode");
                                }
                        }
                        return fh;
                    }
                case HeaderType.FileHeader:
                    {
                        FileHeader fh = header.PromoteHeader<FileHeader>(reader);
                        switch (StreamingMode)
                        {
                            case StreamingMode.Seekable:
                                {
                                    fh.DataStartPosition = reader.BaseStream.Position;
                                    reader.BaseStream.Position += fh.CompressedSize;
                                }
                                break;
                            case StreamingMode.Streaming:
                                {
                                    var ms = new ReadOnlySubStream(reader.BaseStream, fh.CompressedSize);
                                    if (fh.Salt == null)
                                    {
                                        fh.PackedStream = ms;
                                    }
                                    else
                                    {
            #if !NO_CRYPTO
                                        fh.PackedStream = new RarCryptoWrapper(ms, Password,  fh.Salt);
            #else
                                        throw new NotSupportedException("RarCrypto not supported");
            #endif
                                    }
                                }
                                break;
                            default:
                                {
                                    throw new InvalidFormatException("Invalid StreamingMode");
                                }
                        }
                        return fh;
                    }
                case HeaderType.EndArchiveHeader:
                    {
                        return header.PromoteHeader<EndArchiveHeader>(reader);
                    }
                default:
                    {
                        throw new InvalidFormatException("Invalid Rar Header: " + header.HeaderType.ToString());
                    }
            }
        }
        private RarHeader ReadNextHeader(Stream stream)
        {
#if PORTABLE
            var reader = new MarkingBinaryReader(stream);
#else
            var reader = new RarCryptoBinaryReader(stream, Password);

            if (IsEncrypted)
            {
                reader.SkipQueue();
                byte[] salt = reader.ReadBytes(8);
                reader.InitializeAes(salt);
            }
#endif

            RarHeader header = RarHeader.Create(reader);
            if (header == null)
            {
                return(null);
            }
            switch (header.HeaderType)
            {
            case HeaderType.ArchiveHeader:
            {
                var ah = header.PromoteHeader <ArchiveHeader>(reader);
                IsEncrypted = ah.HasPassword;
                return(ah);
            }

            case HeaderType.MarkHeader:
            {
                return(header.PromoteHeader <MarkHeader>(reader));
            }

            case HeaderType.NewSubHeader:
            {
                FileHeader fh = header.PromoteHeader <FileHeader>(reader);
                switch (StreamingMode)
                {
                case StreamingMode.Seekable:
                {
                    fh.DataStartPosition        = reader.BaseStream.Position;
                    reader.BaseStream.Position += fh.CompressedSize;
                }
                break;

                case StreamingMode.Streaming:
                {
                    //skip the data because it's useless?
                    reader.BaseStream.Skip(fh.CompressedSize);
                }
                break;

                default:
                {
                    throw new InvalidFormatException("Invalid StreamingMode");
                }
                }
                return(fh);
            }

            case HeaderType.FileHeader:
            {
                FileHeader fh = header.PromoteHeader <FileHeader>(reader);
                switch (StreamingMode)
                {
                case StreamingMode.Seekable:
                {
                    fh.DataStartPosition        = reader.BaseStream.Position;
                    reader.BaseStream.Position += fh.CompressedSize;
                }
                break;

                case StreamingMode.Streaming:
                {
                    var ms = new ReadOnlySubStream(reader.BaseStream, fh.CompressedSize);
                    if (fh.Salt == null)
                    {
                        fh.PackedStream = ms;
                    }
                    else
                    {
#if PORTABLE
                        throw new NotSupportedException("Encrypted Rar files aren't supported in portable distro.");
#else
                        fh.PackedStream = new RarCryptoWrapper(ms, Password, fh.Salt);
#endif
                    }
                }
                break;

                default:
                {
                    throw new InvalidFormatException("Invalid StreamingMode");
                }
                }
                return(fh);
            }

            case HeaderType.EndArchiveHeader:
            {
                return(header.PromoteHeader <EndArchiveHeader>(reader));
            }

            default:
            {
                throw new InvalidFormatException("Invalid Rar Header: " + header.HeaderType.ToString());
            }
            }
        }