private void FillBase(RarHeader baseHeader) { HeadCRC = baseHeader.HeadCRC; HeaderType = baseHeader.HeaderType; Flags = baseHeader.Flags; HeaderSize = baseHeader.HeaderSize; AdditionalSize = baseHeader.AdditionalSize; ReadBytes = baseHeader.ReadBytes; }
internal static RarHeader Create(MarkingBinaryReader reader) { try { RarHeader header = new RarHeader(); reader.Mark(); header.ReadFromReader(reader); header.ReadBytes += reader.CurrentReadByteCount; return(header); } catch (EndOfStreamException) { return(null); } }
private RarHeader ReadNextHeader(Stream stream) { #if PORTABLE var reader = new MarkingBinaryReader(stream); #else 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); } #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 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()); } } }