/// <summary> /// Initializes a new instance of the <see cref="MpqArchive"/> class. /// </summary> /// <param name="sourceStream">The <see cref="Stream"/> from which to load an <see cref="MpqArchive"/>.</param> /// <param name="loadListFile">If <see langword="true"/>, automatically add filenames from the <see cref="MpqArchive"/>'s <see cref="ListFile"/> after the archive is initialized.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="sourceStream"/> is <see langword="null"/>.</exception> /// <exception cref="MpqParserException">Thrown when the <see cref="MpqHeader"/> could not be found, or when the MPQ format version is not 0.</exception> public MpqArchive(Stream sourceStream, bool loadListFile = false) { _isStreamOwner = true; _baseStream = sourceStream ?? throw new ArgumentNullException(nameof(sourceStream)); if (!TryLocateMpqHeader(_baseStream, out var mpqHeader, out _headerOffset)) { throw new MpqParserException("Unable to locate MPQ header."); } _mpqHeader = mpqHeader; _blockSize = BlockSizeModifier << _mpqHeader.BlockSize; _archiveFollowsHeader = _mpqHeader.IsArchiveAfterHeader(); using (var reader = new BinaryReader(_baseStream, new UTF8Encoding(), true)) { // Load hash table _baseStream.Seek(_mpqHeader.HashTablePosition, SeekOrigin.Begin); _hashTable = new HashTable(reader, _mpqHeader.HashTableSize); // Load entry table _baseStream.Seek(_mpqHeader.BlockTablePosition, SeekOrigin.Begin); _blockTable = new BlockTable(reader, _mpqHeader.BlockTableSize, (uint)_headerOffset); } AddFileName(Signature.FileName); AddFileName(ListFile.FileName); AddFileName(Attributes.FileName); if (loadListFile) { if (TryOpenFile(ListFile.FileName, out var listFileStream)) { using var listFileReader = new StreamReader(listFileStream, leaveOpen: false); AddFileNames(listFileReader.ReadListFile().FileNames); } } }
/// <summary> /// Initializes a new instance of the <see cref="MpqArchive"/> class. /// </summary> /// <param name="sourceStream">The <see cref="Stream"/> containing pre-archive data. Can be null.</param> /// <param name="mpqFiles">The <see cref="MpqFile"/>s that should be added to the archive.</param> /// <param name="hashTableSize">The desired size of the <see cref="BlockTable"/>. Larger size decreases the likelihood of hash collisions.</param> /// <param name="blockSize">The size of blocks in compressed files, which is used to enable seeking.</param> /// <param name="writeArchiveFirst">If true, the archive files will be positioned directly after the header. Otherwise, the hashtable and blocktable will come first.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="mpqFiles"/> collection is null.</exception> public MpqArchive(Stream?sourceStream, IEnumerable <MpqFile> inputFiles, ushort?hashTableSize = null, ushort blockSize = DefaultBlockSize, bool writeArchiveFirst = true) { _baseStream = AlignStream(sourceStream); _headerOffset = _baseStream.Position; _blockSize = BlockSizeModifier << blockSize; _archiveFollowsHeader = writeArchiveFirst; var mpqFiles = inputFiles.ToList(); var fileCount = (uint)(mpqFiles ?? throw new ArgumentNullException(nameof(mpqFiles))).Count; _hashTable = new HashTable(Math.Max(hashTableSize ?? fileCount * 8, fileCount)); _blockTable = new BlockTable(); using (var writer = new BinaryWriter(_baseStream, new UTF8Encoding(false, true), true)) { // Skip the MPQ header, since its contents will be calculated afterwards. writer.Seek((int)MpqHeader.Size, SeekOrigin.Current); // Write Archive var fileIndex = 0U; var fileOffset = _archiveFollowsHeader ? MpqHeader.Size : throw new NotImplementedException(); // var gaps = new List<(long Start, long Length)>(); var endOfStream = _baseStream.Position; // Find files that cannot be decrypted, and need to have a specific position in the archive, because that position is used to calculate the encryption seed. var mpqFixedPositionFiles = mpqFiles.Where(mpqFile => mpqFile.IsFilePositionFixed).OrderBy(mpqFile => mpqFile.MpqStream.FilePosition).ToArray(); if (mpqFixedPositionFiles.Length > 0) { if (mpqFixedPositionFiles.First() !.MpqStream.FilePosition < 0) { throw new NotSupportedException($"Cannot place files in front of the header."); } foreach (var mpqFixedPositionFile in mpqFixedPositionFiles) { var position = mpqFixedPositionFile.MpqStream.FilePosition; if (position < endOfStream) { throw new ArgumentException($"Fixed position files overlap with each other and/or the header. Archive cannot be created.", nameof(inputFiles)); } if (position > endOfStream) { var gapSize = position - endOfStream; // gaps.Add((endOfStream, gapSize)); writer.Seek((int)gapSize, SeekOrigin.Current); } mpqFixedPositionFile.AddToArchive(this, fileIndex, out var mpqEntry, out var mpqHash); var hashTableEntries = _hashTable.Add(mpqHash, mpqFixedPositionFile.HashIndex, mpqFixedPositionFile.HashCollisions); for (var i = 0; i < hashTableEntries; i++) { _blockTable.Add(mpqEntry); } mpqFixedPositionFile.Dispose(); fileIndex += hashTableEntries; endOfStream = _baseStream.Position; } } mpqFiles.RemoveAll(mpqFile => mpqFile.IsFilePositionFixed); foreach (var mpqFile in mpqFiles) { // TODO: insert files into the gaps // need to know compressed size of file first, and if file is also encrypted with blockoffsetadjustedkey, encryption needs to happen after gap selection // therefore, can't use current AddToArchive method, which does both compression and encryption at same time // var availableGaps = gaps.Where(gap => gap.Length >= ) var selectedPosition = endOfStream; var selectedGap = false; _baseStream.Position = selectedPosition; mpqFile.AddToArchive(this, fileIndex, out var mpqEntry, out var mpqHash); var hashTableEntries = _hashTable.Add(mpqHash, mpqFile.HashIndex, mpqFile.HashCollisions); for (var i = 0; i < hashTableEntries; i++) { _blockTable.Add(mpqEntry); } mpqFile.Dispose(); fileIndex += hashTableEntries; if (!selectedGap) { endOfStream = _baseStream.Position; } } _baseStream.Position = endOfStream; _hashTable.WriteTo(writer); _blockTable.WriteTo(writer); /*if (!_archiveFollowsHeader) * { * foreach (var mpqFile in mpqFiles) * { * mpqFile.WriteTo(writer, true); * } * }*/ writer.Seek((int)_headerOffset, SeekOrigin.Begin); _mpqHeader = new MpqHeader((uint)(endOfStream - fileOffset), _hashTable.Size, _blockTable.Size, blockSize, _archiveFollowsHeader); _mpqHeader.WriteTo(writer); } }
/// <summary> /// Repairs corrupted values in an <see cref="MpqArchive"/>. /// </summary> /// <param name="sourceStream">The stream containing the archive that needs to be repaired.</param> /// <param name="leaveOpen">If false, the given <paramref name="sourceStream"/> will be disposed at the end of this method.</param> /// <returns>A stream containing the repaired archive.</returns> public static MemoryStream Restore(Stream sourceStream, bool leaveOpen = false) { if (sourceStream is null) { throw new ArgumentNullException(nameof(sourceStream)); } if (!TryLocateMpqHeader(sourceStream, out var mpqHeader, out var headerOffset)) { throw new MpqParserException($"Unable to locate MPQ header."); } if (mpqHeader.MpqVersion != 0) { throw new MpqParserException($"MPQ format version {mpqHeader.MpqVersion} is not supported"); } var memoryStream = new MemoryStream(); using (var writer = new BinaryWriter(memoryStream, new UTF8Encoding(false, true), true)) { // Skip the MPQ header, since its contents will be calculated afterwards. writer.Seek((int)MpqHeader.Size, SeekOrigin.Current); var archiveSize = 0U; var hashTableEntries = mpqHeader.HashTableSize; var blockTableEntries = mpqHeader.BlockTableSize > MpqTable.MaxSize ? mpqHeader.IsArchiveAfterHeader() ? mpqHeader.BlockTablePosition < mpqHeader.HeaderOffset ? (mpqHeader.HeaderOffset - mpqHeader.BlockTablePosition) / MpqEntry.Size : (uint)(sourceStream.Length - sourceStream.Position) / MpqEntry.Size : throw new MpqParserException($"Unable to determine true BlockTable size.") : mpqHeader.BlockTableSize; var hashTable = (HashTable?)null; var blockTable = (BlockTable?)null; using (var reader = new BinaryReader(sourceStream, new UTF8Encoding(), true)) { // Load hash table sourceStream.Seek(mpqHeader.HashTablePosition, SeekOrigin.Begin); hashTable = new HashTable(reader, hashTableEntries); // Load entry table sourceStream.Seek(mpqHeader.BlockTablePosition, SeekOrigin.Begin); blockTable = new BlockTable(reader, blockTableEntries, (uint)headerOffset); // Load archive files for (var i = 0; i < blockTable.Size; i++) { var entry = blockTable[i]; if ((entry.Flags & MpqFileFlags.Garbage) == 0) { var size = entry.CompressedSize; var flags = entry.Flags; if (entry.IsEncrypted && entry.Flags.HasFlag(MpqFileFlags.BlockOffsetAdjustedKey)) { // To prevent encryption seed becoming incorrect, save file uncompressed and unencrypted. var pos = sourceStream.Position; using (var mpqStream = new MpqStream(entry, sourceStream, BlockSizeModifier << mpqHeader.BlockSize)) { mpqStream.CopyTo(memoryStream); } sourceStream.Position = pos + size; size = entry.FileSize; flags = entry.Flags & ~(MpqFileFlags.Compressed | MpqFileFlags.Encrypted | MpqFileFlags.BlockOffsetAdjustedKey); } else { sourceStream.Position = entry.FilePosition; writer.Write(reader.ReadBytes((int)size)); } blockTable[i] = new MpqEntry(null, 0, MpqHeader.Size + archiveSize, size, entry.FileSize, flags); archiveSize += size; } else { blockTable[i] = new MpqEntry(null, 0, MpqHeader.Size + archiveSize, 0, 0, 0); } } } // Fix invalid block indices and locales. for (var i = 0; i < hashTable.Size; i++) { var hash = hashTable[i]; if (!hash.IsEmpty && !hash.IsDeleted && hash.BlockIndex > BlockTable.MaxSize) { // TODO: don't force neutral locale if another MpqHash exists with the same Name1 and Name2, and that has the neutral locale hashTable[i] = new MpqHash(hash.Name, MpqLocale.Neutral /*hash.Locale & (MpqLocale)0x00000FFF*/, hash.BlockIndex & (BlockTable.MaxSize - 1), hash.Mask); } } hashTable.SerializeTo(memoryStream); blockTable.SerializeTo(memoryStream); writer.Seek(0, SeekOrigin.Begin); new MpqHeader(archiveSize, hashTableEntries, blockTableEntries, mpqHeader.BlockSize).WriteTo(writer); } if (!leaveOpen) { sourceStream.Dispose(); } memoryStream.Position = 0; return(memoryStream); }
/// <summary> /// Initializes a new instance of the <see cref="MpqArchive"/> class. /// </summary> /// <param name="sourceStream">The <see cref="Stream"/> containing pre-archive data. Can be null.</param> /// <param name="mpqFiles">The <see cref="MpqFile"/>s that should be added to the archive.</param> /// <param name="hashTableSize">The desired size of the <see cref="BlockTable"/>. Larger size decreases the likelihood of hash collisions.</param> /// <param name="blockSize">The size of blocks in compressed files, which is used to enable seeking.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="mpqFiles"/> collection is null.</exception> public MpqArchive(Stream sourceStream, ICollection <MpqFile> mpqFiles, ushort?hashTableSize = null, ushort blockSize = 8) { // TODO: copy sourceStream contents to a new stream if CanWrite property is false (can do this in alignStream method) _baseStream = AlignStream(sourceStream ?? new MemoryStream()); _headerOffset = _baseStream.Position; _blockSize = BlockSizeModifier << blockSize; var fileCount = (uint)(mpqFiles ?? throw new ArgumentNullException(nameof(mpqFiles))).Count; _hashTable = new HashTable(Math.Max(hashTableSize ?? fileCount * 8, fileCount)); _blockTable = new BlockTable(fileCount); using (var writer = new BinaryWriter(_baseStream, new UTF8Encoding(false, true), true)) { // Skip the MPQ header, since its contents will be calculated afterwards. writer.Seek((int)MpqHeader.Size, SeekOrigin.Current); const bool archiveBeforeTables = true; uint hashTableEntries = 0; // Write Archive var fileIndex = 0U; var fileOffset = archiveBeforeTables ? MpqHeader.Size : throw new NotImplementedException(); var filePos = fileOffset; // TODO: add support for encryption of the archive files foreach (var mpqFile in mpqFiles) { var locale = MpqLocale.Neutral; mpqFile.AddToArchive((uint)_headerOffset, fileIndex, filePos, locale, _hashTable.Mask); if (archiveBeforeTables) { mpqFile.SerializeTo(writer, true); } hashTableEntries += _hashTable.Add(mpqFile.MpqHash, mpqFile.HashIndex, mpqFile.HashCollisions); _blockTable.Add(mpqFile.MpqEntry); filePos += mpqFile.MpqEntry.CompressedSize; fileIndex++; } // Match size of blocktable with amount of occupied entries in hashtable /* * for ( var i = blockTable.Size; i < hashTableEntries; i++ ) * { * var entry = MpqEntry.Dummy; * entry.SetPos( filePos ); * blockTable.Add( entry ); * } * blockTable.UpdateSize(); */ _hashTable.SerializeTo(writer); _blockTable.SerializeTo(writer); if (!archiveBeforeTables) { foreach (var mpqFile in mpqFiles) { mpqFile.SerializeTo(writer, true); } } writer.Seek((int)_headerOffset, SeekOrigin.Begin); _mpqHeader = new MpqHeader(filePos - fileOffset, _hashTable.Size, _blockTable.Size, blockSize, archiveBeforeTables); _mpqHeader.WriteToStream(writer); } }
/// <summary> /// Initializes a new instance of the <see cref="MpqArchive"/> class. /// </summary> /// <param name="sourceStream">The <see cref="Stream"/> containing pre-archive data. Can be <see langword="null"/>.</param> /// <param name="inputFiles">The <see cref="MpqFile"/>s that should be added to the archive.</param> /// <param name="createOptions"></param> /// <param name="leaveOpen">If <see langword="false"/>, the given <paramref name="sourceStream"/> will be disposed when the <see cref="MpqArchive"/> is disposed.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="mpqFiles"/> collection is <see langword="null"/>.</exception> public MpqArchive(Stream?sourceStream, IEnumerable <MpqFile> inputFiles, MpqArchiveCreateOptions createOptions, bool leaveOpen = false) { if (inputFiles is null) { throw new ArgumentNullException(nameof(inputFiles)); } if (createOptions is null) { throw new ArgumentNullException(nameof(createOptions)); } _isStreamOwner = !leaveOpen; _baseStream = AlignStream(sourceStream); _headerOffset = _baseStream.Position; _blockSize = BlockSizeModifier << createOptions.BlockSize; _archiveFollowsHeader = createOptions.WriteArchiveFirst; var signatureName = Signature.FileName.GetStringHash(); var listFileName = ListFile.FileName.GetStringHash(); var attributesName = Attributes.FileName.GetStringHash(); var signatureCreateMode = createOptions.SignatureCreateMode.GetValueOrDefault(MpqFileCreateMode.Prune); var listFileCreateMode = createOptions.ListFileCreateMode.GetValueOrDefault(MpqFileCreateMode.Overwrite); var attributesCreateMode = createOptions.AttributesCreateMode.GetValueOrDefault(MpqFileCreateMode.Overwrite); var haveSignature = false; var haveListFile = false; var haveAttributes = false; var mpqFiles = new HashSet <MpqFile>(MpqFileComparer.Default); foreach (var mpqFile in inputFiles) { if (mpqFile is MpqOrphanedFile) { continue; } if (mpqFile.Name == signatureName) { if (signatureCreateMode.HasFlag(MpqFileCreateMode.RemoveFlag)) { continue; } else { haveSignature = true; } } if (mpqFile.Name == listFileName) { if (listFileCreateMode.HasFlag(MpqFileCreateMode.RemoveFlag)) { continue; } else { haveListFile = true; } } if (mpqFile.Name == attributesName) { if (attributesCreateMode.HasFlag(MpqFileCreateMode.RemoveFlag)) { continue; } else { haveAttributes = true; } } if (!mpqFiles.Add(mpqFile)) { // todo: logging? } } var fileCount = (uint)mpqFiles.Count; var wantGenerateSignature = !haveSignature && signatureCreateMode.HasFlag(MpqFileCreateMode.AddFlag); var signature = wantGenerateSignature ? new Signature() : null; if (wantGenerateSignature) { fileCount++; } var wantGenerateListFile = !haveListFile && listFileCreateMode.HasFlag(MpqFileCreateMode.AddFlag); var listFile = wantGenerateListFile ? new ListFile() : null; if (wantGenerateListFile) { fileCount++; } var wantGenerateAttributes = !haveAttributes && attributesCreateMode.HasFlag(MpqFileCreateMode.AddFlag); var attributes = wantGenerateAttributes ? new Attributes(createOptions) : null; if (wantGenerateAttributes) { fileCount++; } _hashTable = new HashTable(Math.Max(createOptions.HashTableSize ?? fileCount * 8, fileCount)); _blockTable = new BlockTable(); using (var writer = new BinaryWriter(_baseStream, new UTF8Encoding(false, true), true)) { // Skip the MPQ header, since its contents will be calculated afterwards. writer.Seek((int)MpqHeader.Size, SeekOrigin.Current); // Write Archive var fileIndex = 0U; var fileOffset = _archiveFollowsHeader ? MpqHeader.Size : throw new NotImplementedException(); // var gaps = new List<(long Start, long Length)>(); var endOfStream = _baseStream.Position; void InsertMpqFile(MpqFile mpqFile, bool updateEndOfStream, bool allowMultiple = true) { if (listFile is not null && mpqFile is MpqKnownFile knownFile) { listFile.FileNames.Add(knownFile.FileName); } mpqFile.AddToArchive(this, fileIndex, out var mpqEntry, out var mpqHash); var hashTableEntries = _hashTable.Add(mpqHash, mpqFile.HashIndex, mpqFile.HashCollisions); if (!allowMultiple && hashTableEntries > 1) { throw new Exception(); } var crc32 = 0; if (attributes is not null && attributes.Flags.HasFlag(AttributesFlags.Crc32) && allowMultiple) { mpqFile.MpqStream.Position = 0; crc32 = new Ionic.Crc.CRC32().GetCrc32(mpqFile.MpqStream); } for (var i = 0; i < hashTableEntries; i++) { _blockTable.Add(mpqEntry); if (attributes is not null) { if (attributes.Flags.HasFlag(AttributesFlags.Crc32)) { attributes.Crc32s.Add(crc32); } if (attributes.Flags.HasFlag(AttributesFlags.DateTime)) { attributes.DateTimes.Add(DateTime.Now); } if (attributes.Flags.HasFlag(AttributesFlags.Unk0x04)) { attributes.Unk0x04s.Add(new byte[16]); } } } mpqFile.Dispose(); fileIndex += hashTableEntries; if (updateEndOfStream) { endOfStream = _baseStream.Position; } } // Find files that cannot be decrypted, and need to have a specific position in the archive, because that position is used to calculate the encryption seed. var mpqFixedPositionFiles = mpqFiles.Where(mpqFile => mpqFile.IsFilePositionFixed).OrderBy(mpqFile => mpqFile.MpqStream.FilePosition).ToArray(); if (mpqFixedPositionFiles.Length > 0) { if (mpqFixedPositionFiles.First() !.MpqStream.FilePosition < 0) { throw new NotSupportedException($"Cannot place files in front of the header."); } foreach (var mpqFixedPositionFile in mpqFixedPositionFiles) { var position = mpqFixedPositionFile.MpqStream.FilePosition; if (position < endOfStream) { throw new ArgumentException($"Fixed position files overlap with each other and/or the header. Archive cannot be created.", nameof(inputFiles)); } if (position > endOfStream) { var gapSize = position - endOfStream; // gaps.Add((endOfStream, gapSize)); writer.Seek((int)gapSize, SeekOrigin.Current); } InsertMpqFile(mpqFixedPositionFile, true); } } foreach (var mpqFile in mpqFiles.Where(mpqFile => !mpqFile.IsFilePositionFixed)) { // TODO: insert files into the gaps // need to know compressed size of file first, and if file is also encrypted with blockoffsetadjustedkey, encryption needs to happen after gap selection // therefore, can't use current AddToArchive method, which does both compression and encryption at same time // var availableGaps = gaps.Where(gap => gap.Length >= ) var selectedPosition = endOfStream; var selectedGap = false; _baseStream.Position = selectedPosition; InsertMpqFile(mpqFile, !selectedGap); } var signaturePosition = endOfStream + 8; if (signature is not null) { _baseStream.Position = endOfStream; using var signatureStream = new MemoryStream(); using var signatureWriter = new BinaryWriter(signatureStream); signatureWriter.Write(signature); signatureWriter.Flush(); using var signatureMpqFile = MpqFile.New(signatureStream, Signature.FileName); signatureMpqFile.TargetFlags = MpqFileFlags.Exists; InsertMpqFile(signatureMpqFile, true); } if (listFile is not null) { _baseStream.Position = endOfStream; using var listFileStream = new MemoryStream(); using var listFileWriter = new StreamWriter(listFileStream); listFileWriter.WriteListFile(listFile); listFileWriter.Flush(); using var listFileMpqFile = MpqFile.New(listFileStream, ListFile.FileName); listFileMpqFile.TargetFlags = MpqFileFlags.Exists | MpqFileFlags.CompressedMulti | MpqFileFlags.Encrypted | MpqFileFlags.BlockOffsetAdjustedKey; InsertMpqFile(listFileMpqFile, true); } if (attributes is not null) { _baseStream.Position = endOfStream; if (attributes.Flags.HasFlag(AttributesFlags.Crc32)) { attributes.Crc32s.Add(0); } if (attributes.Flags.HasFlag(AttributesFlags.DateTime)) { attributes.DateTimes.Add(DateTime.Now); } if (attributes.Flags.HasFlag(AttributesFlags.Unk0x04)) { attributes.Unk0x04s.Add(new byte[16]); } using var attributesStream = new MemoryStream(); using var attributesWriter = new BinaryWriter(attributesStream); attributesWriter.Write(attributes); attributesWriter.Flush(); using var attributesMpqFile = MpqFile.New(attributesStream, Attributes.FileName); attributesMpqFile.TargetFlags = MpqFileFlags.Exists | MpqFileFlags.CompressedMulti | MpqFileFlags.Encrypted | MpqFileFlags.BlockOffsetAdjustedKey; InsertMpqFile(attributesMpqFile, true, false); } _baseStream.Position = endOfStream; _hashTable.WriteTo(writer); _blockTable.WriteTo(writer); /*if (!_archiveFollowsHeader) * { * foreach (var mpqFile in mpqFiles) * { * mpqFile.WriteTo(writer, true); * } * }*/ writer.Seek((int)_headerOffset, SeekOrigin.Begin); _mpqHeader = new MpqHeader((uint)_headerOffset, (uint)(endOfStream - fileOffset), _hashTable.Size, _blockTable.Size, createOptions.BlockSize, _archiveFollowsHeader); _mpqHeader.WriteTo(writer); if (wantGenerateSignature) { var archiveBytes = new byte[_mpqHeader.ArchiveSize]; _baseStream.Position = _headerOffset; _baseStream.Read(archiveBytes); using var rsa = RSA.Create(); rsa.ImportFromPem(createOptions.SignaturePrivateKey); var signatureBytes = rsa.SignData(archiveBytes, HashAlgorithmName.MD5, RSASignaturePadding.Pkcs1); _baseStream.Position = signaturePosition; _baseStream.Write(signatureBytes.Reverse().ToArray()); } } }