Example #1
0
        private RarHeader TryReadNextHeader(Stream stream)
        {
            RarCrcBinaryReader reader;

            if (!IsEncrypted)
            {
                reader = new RarCrcBinaryReader(stream);
            }
            else
            {
                if (Options.Password == null)
                {
                    throw new CryptographicException("Encrypted Rar archive has no password specified.");
                }
                reader = new RarCryptoBinaryReader(stream, Options.Password);
            }

            var header = RarHeader.TryReadBase(reader, _isRar5, Options.ArchiveEncoding);

            if (header == null)
            {
                return(null);
            }
            switch (header.HeaderCode)
            {
            case HeaderCodeV.RAR5_ARCHIVE_HEADER:
            case HeaderCodeV.RAR4_ARCHIVE_HEADER:
            {
                var ah = new ArchiveHeader(header, reader);
                if (ah.IsEncrypted == true)
                {
                    //!!! rar5 we don't know yet
                    IsEncrypted = true;
                }
                return(ah);
            }

            case HeaderCodeV.RAR4_PROTECT_HEADER:
            {
                var ph = new ProtectHeader(header, 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 HeaderCodeV.RAR5_SERVICE_HEADER:
            {
                var fh = new FileHeader(header, reader, HeaderType.Service);
                SkipData(fh, reader);
                return(fh);
            }

            case HeaderCodeV.RAR4_NEW_SUB_HEADER:
            {
                var fh = new FileHeader(header, reader, HeaderType.NewSub);
                SkipData(fh, reader);
                return(fh);
            }

            case HeaderCodeV.RAR5_FILE_HEADER:
            case HeaderCodeV.RAR4_FILE_HEADER:
            {
                var fh = new FileHeader(header, reader, HeaderType.File);
                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.R4Salt == null)
                    {
                        fh.PackedStream = ms;
                    }
                    else
                    {
                        fh.PackedStream = new RarCryptoWrapper(ms, Options.Password, fh.R4Salt);
                    }
                }
                break;

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

            case HeaderCodeV.RAR5_END_ARCHIVE_HEADER:
            case HeaderCodeV.RAR4_END_ARCHIVE_HEADER:
            {
                return(new EndArchiveHeader(header, reader));
            }

            case HeaderCodeV.RAR5_ARCHIVE_ENCRYPTION_HEADER:
            {
                var ch = new ArchiveCryptHeader(header, reader);
                IsEncrypted = true;
                return(ch);
            }

            default:
            {
                throw new InvalidFormatException("Unknown Rar Header: " + header.HeaderCode);
            }
            }
        }
        private RarHeader TryReadNextHeader(Stream stream)
        {
            RarCrcBinaryReader reader;

            if (!IsEncrypted)
            {
                reader = new RarCrcBinaryReader(stream);
            }
            else
            {
#if !NO_CRYPTO
                if (Options.Password == null)
                {
                    throw new CryptographicException("Encrypted Rar archive has no password specified.");
                }
                reader = new RarCryptoBinaryReader(stream, Options.Password);
#else
                throw new CryptographicException("Rar encryption unsupported on this platform");
#endif
            }

            var header = RarHeader.TryReadBase(reader, this.isRar5, Options.ArchiveEncoding);
            if (header == null)
            {
                return(null);
            }
            switch (header.HeaderCode)
            {
            case HeaderCodeV.Rar5ArchiveHeader:
            case HeaderCodeV.Rar4ArchiveHeader:
            {
                var ah = new ArchiveHeader(header, reader);
                if (ah.IsEncrypted == true)
                {
                    //!!! rar5 we don't know yet
                    IsEncrypted = true;
                }
                return(ah);
            }

            case HeaderCodeV.Rar4ProtectHeader:
            {
                var ph = new ProtectHeader(header, 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 HeaderCodeV.Rar5ServiceHeader:
            {
                var fh = new FileHeader(header, reader, HeaderType.Service);
                SkipData(fh, reader);
                return(fh);
            }

            case HeaderCodeV.Rar4NewSubHeader:
            {
                var fh = new FileHeader(header, reader, HeaderType.NewSub);
                SkipData(fh, reader);
                return(fh);
            }

            case HeaderCodeV.Rar5FileHeader:
            case HeaderCodeV.Rar4FileHeader:
            {
                var fh = new FileHeader(header, reader, HeaderType.File);
                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.R4Salt == null)
                    {
                        fh.PackedStream = ms;
                    }
                    else
                    {
#if !NO_CRYPTO
                        fh.PackedStream = new RarCryptoWrapper(ms, Options.Password, fh.R4Salt);
#else
                        throw new NotSupportedException("RarCrypto not supported");
#endif
                    }
                }
                break;

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

            case HeaderCodeV.Rar5EndArchiveHeader:
            case HeaderCodeV.Rar4EndArchiveHeader:
            {
                return(new EndArchiveHeader(header, reader));
            }

            case HeaderCodeV.Rar5ArchiveEncryptionHeader:
            {
                var ch = new ArchiveCryptHeader(header, reader);
                IsEncrypted = true;
                return(ch);
            }

            default:
            {
                throw new InvalidFormatException("Unknown Rar Header: " + header.HeaderCode);
            }
            }
        }