2020-01-26 00:39:13 -05:00
|
|
|
package d2mpq
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2020-07-04 22:37:13 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-01-26 00:39:13 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MPQ represents an MPQ archive
|
|
|
|
type MPQ struct {
|
2020-07-04 22:37:13 -04:00
|
|
|
filePath string
|
|
|
|
file *os.File
|
|
|
|
hashEntryMap HashEntryMap
|
|
|
|
blockTableEntries []BlockTableEntry
|
|
|
|
data Data
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Data Represents a MPQ file
|
|
|
|
type Data struct {
|
|
|
|
Magic [4]byte
|
|
|
|
HeaderSize uint32
|
|
|
|
ArchiveSize uint32
|
|
|
|
FormatVersion uint16
|
|
|
|
BlockSize uint16
|
|
|
|
HashTableOffset uint32
|
|
|
|
BlockTableOffset uint32
|
|
|
|
HashTableEntries uint32
|
|
|
|
BlockTableEntries uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// HashTableEntry represents a hashed file entry in the MPQ file
|
|
|
|
type HashTableEntry struct { // 16 bytes
|
|
|
|
NamePartA uint32
|
|
|
|
NamePartB uint32
|
|
|
|
Locale uint16
|
|
|
|
Platform uint16
|
|
|
|
BlockIndex uint32
|
|
|
|
}
|
|
|
|
|
2020-06-28 22:32:34 -04:00
|
|
|
// PatchInfo represents patch info for the MPQ.
|
2020-01-26 00:39:13 -05:00
|
|
|
type PatchInfo struct {
|
|
|
|
Length uint32 // Length of patch info header, in bytes
|
|
|
|
Flags uint32 // Flags. 0x80000000 = MD5 (?)
|
|
|
|
DataSize uint32 // Uncompressed size of the patch file
|
|
|
|
Md5 [16]byte // MD5 of the entire patch file after decompression
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileFlag represents flags for a file record in the MPQ archive
|
|
|
|
type FileFlag uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
// FileImplode - File is compressed using PKWARE Data compression library
|
|
|
|
FileImplode FileFlag = 0x00000100
|
|
|
|
// FileCompress - File is compressed using combination of compression methods
|
|
|
|
FileCompress FileFlag = 0x00000200
|
|
|
|
// FileEncrypted - The file is encrypted
|
|
|
|
FileEncrypted FileFlag = 0x00010000
|
|
|
|
// FileFixKey - The decryption key for the file is altered according to the position of the file in the archive
|
|
|
|
FileFixKey FileFlag = 0x00020000
|
|
|
|
// FilePatchFile - The file contains incremental patch for an existing file in base MPQ
|
|
|
|
FilePatchFile FileFlag = 0x00100000
|
|
|
|
// FileSingleUnit - Instead of being divided to 0x1000-bytes blocks, the file is stored as single unit
|
|
|
|
FileSingleUnit FileFlag = 0x01000000
|
|
|
|
// FileDeleteMarker - File is a deletion marker, indicating that the file no longer exists. This is used to allow patch
|
|
|
|
// archives to delete files present in lower-priority archives in the search chain. The file usually
|
|
|
|
// has length of 0 or 1 byte and its name is a hash
|
|
|
|
FileDeleteMarker FileFlag = 0x02000000
|
|
|
|
// FileSectorCrc - File has checksums for each sector. Ignored if file is not compressed or imploded.
|
|
|
|
FileSectorCrc FileFlag = 0x04000000
|
|
|
|
// FileExists - Set if file exists, reset when the file was deleted
|
|
|
|
FileExists FileFlag = 0x80000000
|
|
|
|
)
|
|
|
|
|
|
|
|
// BlockTableEntry represents an entry in the block table
|
|
|
|
type BlockTableEntry struct { // 16 bytes
|
|
|
|
FilePosition uint32
|
|
|
|
CompressedFileSize uint32
|
|
|
|
UncompressedFileSize uint32
|
|
|
|
Flags FileFlag
|
|
|
|
// Local Stuff...
|
|
|
|
FileName string
|
|
|
|
EncryptionSeed uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasFlag returns true if the specified flag is present
|
|
|
|
func (v BlockTableEntry) HasFlag(flag FileFlag) bool {
|
|
|
|
return (v.Flags & flag) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads an MPQ file and returns a MPQ structure
|
2020-07-04 22:37:13 -04:00
|
|
|
func Load(fileName string) (d2interface.Archive, error) {
|
|
|
|
result := &MPQ{filePath: fileName}
|
2020-01-26 00:39:13 -05:00
|
|
|
|
|
|
|
var err error
|
|
|
|
if runtime.GOOS == "linux" {
|
2020-07-04 22:37:13 -04:00
|
|
|
result.file, err = openIgnoreCase(fileName)
|
2020-01-26 00:39:13 -05:00
|
|
|
} else {
|
2020-07-04 22:37:13 -04:00
|
|
|
result.file, err = os.Open(fileName) //nolint:gosec Will fix later
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := result.readHeader(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func openIgnoreCase(mpqPath string) (*os.File, error) {
|
|
|
|
// First see if file exists with specified case
|
2020-06-28 22:32:34 -04:00
|
|
|
mpqFile, err := os.Open(mpqPath) //nolint:gosec Will fix later
|
2020-01-26 00:39:13 -05:00
|
|
|
if err == nil {
|
|
|
|
return mpqFile, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mpqName := filepath.Base(mpqPath)
|
|
|
|
mpqDir := filepath.Dir(mpqPath)
|
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(mpqDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if strings.EqualFold(file.Name(), mpqName) {
|
|
|
|
mpqName = file.Name()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 22:32:34 -04:00
|
|
|
file, err := os.Open(path.Join(mpqDir, mpqName)) //nolint:gosec Will fix later
|
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
return file, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MPQ) readHeader() error {
|
2020-07-04 22:37:13 -04:00
|
|
|
err := binary.Read(v.file, binary.LittleEndian, &v.data)
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
if string(v.data.Magic[:]) != "MPQ\x1A" {
|
2020-01-26 00:39:13 -05:00
|
|
|
return errors.New("invalid mpq header")
|
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
v.loadHashTable()
|
|
|
|
v.loadBlockTable()
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MPQ) loadHashTable() {
|
2020-07-04 22:37:13 -04:00
|
|
|
_, err := v.file.Seek(int64(v.data.HashTableOffset), 0)
|
2020-01-26 00:39:13 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
hashData := make([]uint32, v.data.HashTableEntries*4) //nolint:gomnd Decryption magic
|
2020-06-22 16:01:14 -04:00
|
|
|
hash := make([]byte, 4)
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-06-22 16:01:14 -04:00
|
|
|
for i := range hashData {
|
2020-07-04 22:37:13 -04:00
|
|
|
_, _ = v.file.Read(hash)
|
2020-06-22 16:01:14 -04:00
|
|
|
hashData[i] = binary.LittleEndian.Uint32(hash)
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
decrypt(hashData, hashString("(hash table)", 3))
|
2020-06-22 16:01:14 -04:00
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
for i := uint32(0); i < v.data.HashTableEntries; i++ {
|
|
|
|
v.hashEntryMap.Insert(&HashTableEntry{
|
2020-01-26 00:39:13 -05:00
|
|
|
NamePartA: hashData[i*4],
|
|
|
|
NamePartB: hashData[(i*4)+1],
|
2020-06-28 22:32:34 -04:00
|
|
|
//nolint:godox // TODO: Verify that we're grabbing the right high/lo word for the vars below
|
|
|
|
Locale: uint16(hashData[(i*4)+2] >> 16), //nolint:gomnd binary data
|
|
|
|
Platform: uint16(hashData[(i*4)+2] & 0xFFFF), //nolint:gomnd binary data
|
2020-01-26 00:39:13 -05:00
|
|
|
BlockIndex: hashData[(i*4)+3],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MPQ) loadBlockTable() {
|
2020-07-04 22:37:13 -04:00
|
|
|
_, err := v.file.Seek(int64(v.data.BlockTableOffset), 0)
|
2020-01-26 00:39:13 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2020-06-22 16:01:14 -04:00
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
blockData := make([]uint32, v.data.BlockTableEntries*4) //nolint:gomnd binary data
|
2020-06-22 16:01:14 -04:00
|
|
|
hash := make([]byte, 4)
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-06-22 16:01:14 -04:00
|
|
|
for i := range blockData {
|
2020-07-04 22:37:13 -04:00
|
|
|
_, _ = v.file.Read(hash[:]) //nolint:errcheck Will fix later
|
2020-06-22 16:01:14 -04:00
|
|
|
blockData[i] = binary.LittleEndian.Uint32(hash)
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
decrypt(blockData, hashString("(block table)", 3))
|
2020-06-22 16:01:14 -04:00
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
for i := uint32(0); i < v.data.BlockTableEntries; i++ {
|
|
|
|
v.blockTableEntries = append(v.blockTableEntries, BlockTableEntry{
|
2020-01-26 00:39:13 -05:00
|
|
|
FilePosition: blockData[(i * 4)],
|
|
|
|
CompressedFileSize: blockData[(i*4)+1],
|
|
|
|
UncompressedFileSize: blockData[(i*4)+2],
|
|
|
|
Flags: FileFlag(blockData[(i*4)+3]),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func decrypt(data []uint32, seed uint32) {
|
2020-06-28 22:32:34 -04:00
|
|
|
seed2 := uint32(0xeeeeeeee) //nolint:gomnd Decryption magic
|
2020-01-26 00:39:13 -05:00
|
|
|
|
|
|
|
for i := 0; i < len(data); i++ {
|
2020-06-28 22:32:34 -04:00
|
|
|
seed2 += cryptoLookup(0x400 + (seed & 0xff)) //nolint:gomnd Decryption magic
|
2020-01-26 00:39:13 -05:00
|
|
|
result := data[i]
|
|
|
|
result ^= seed + seed2
|
|
|
|
|
|
|
|
seed = ((^seed << 21) + 0x11111111) | (seed >> 11)
|
2020-06-28 22:32:34 -04:00
|
|
|
seed2 = result + seed2 + (seed2 << 5) + 3 //nolint:gomnd Decryption magic
|
2020-01-26 00:39:13 -05:00
|
|
|
data[i] = result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func decryptBytes(data []byte, seed uint32) {
|
2020-06-28 22:32:34 -04:00
|
|
|
seed2 := uint32(0xEEEEEEEE) //nolint:gomnd Decryption magic
|
2020-01-26 00:39:13 -05:00
|
|
|
for i := 0; i < len(data)-3; i += 4 {
|
2020-06-28 22:32:34 -04:00
|
|
|
seed2 += cryptoLookup(0x400 + (seed & 0xFF)) //nolint:gomnd Decryption magic
|
2020-01-26 00:39:13 -05:00
|
|
|
result := binary.LittleEndian.Uint32(data[i : i+4])
|
|
|
|
result ^= seed + seed2
|
|
|
|
seed = ((^seed << 21) + 0x11111111) | (seed >> 11)
|
2020-06-28 22:32:34 -04:00
|
|
|
seed2 = result + seed2 + (seed2 << 5) + 3 //nolint:gomnd Decryption magic
|
2020-01-26 00:39:13 -05:00
|
|
|
|
2020-06-28 22:32:34 -04:00
|
|
|
data[i+0] = uint8(result & 0xff) //nolint:gomnd Decryption magic
|
|
|
|
data[i+1] = uint8((result >> 8) & 0xff) //nolint:gomnd Decryption magic
|
|
|
|
data[i+2] = uint8((result >> 16) & 0xff) //nolint:gomnd Decryption magic
|
|
|
|
data[i+3] = uint8((result >> 24) & 0xff) //nolint:gomnd Decryption magic
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func hashString(key string, hashType uint32) uint32 {
|
2020-06-28 22:32:34 -04:00
|
|
|
seed1 := uint32(0x7FED7FED) //nolint:gomnd Decryption magic
|
|
|
|
seed2 := uint32(0xEEEEEEEE) //nolint:gomnd Decryption magic
|
2020-01-26 00:39:13 -05:00
|
|
|
|
|
|
|
/* prepare seeds. */
|
|
|
|
for _, char := range strings.ToUpper(key) {
|
2020-02-08 21:02:37 -05:00
|
|
|
seed1 = cryptoLookup((hashType*0x100)+uint32(char)) ^ (seed1 + seed2)
|
2020-06-28 22:32:34 -04:00
|
|
|
seed2 = uint32(char) + seed1 + seed2 + (seed2 << 5) + 3 //nolint:gomnd Decryption magic
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
return seed1
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFileBlockData gets a block table entry
|
2020-06-24 10:13:11 -04:00
|
|
|
func (v *MPQ) getFileBlockData(fileName string) (BlockTableEntry, error) {
|
2020-07-04 22:37:13 -04:00
|
|
|
fileEntry, found := v.hashEntryMap.Find(fileName)
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
if !found || fileEntry.BlockIndex >= uint32(len(v.blockTableEntries)) {
|
2020-01-26 00:39:13 -05:00
|
|
|
return BlockTableEntry{}, errors.New("file not found")
|
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-07-04 22:37:13 -04:00
|
|
|
return v.blockTableEntries[fileEntry.BlockIndex], nil
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the MPQ file
|
|
|
|
func (v *MPQ) Close() {
|
2020-07-04 22:37:13 -04:00
|
|
|
err := v.file.Close()
|
2020-01-26 00:39:13 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 22:32:34 -04:00
|
|
|
// FileExists checks the mpq to see if the file exists
|
2020-06-24 10:13:11 -04:00
|
|
|
func (v *MPQ) FileExists(fileName string) bool {
|
2020-07-04 22:37:13 -04:00
|
|
|
return v.hashEntryMap.Contains(fileName)
|
2020-01-26 00:39:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFile reads a file from the MPQ and returns a memory stream
|
2020-06-24 10:13:11 -04:00
|
|
|
func (v *MPQ) ReadFile(fileName string) ([]byte, error) {
|
2020-01-26 00:39:13 -05:00
|
|
|
fileBlockData, err := v.getFileBlockData(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
2020-06-27 02:49:27 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
fileBlockData.FileName = strings.ToLower(fileName)
|
2020-06-27 02:49:27 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
fileBlockData.calculateEncryptionSeed()
|
|
|
|
mpqStream, err := CreateStream(v, fileBlockData, fileName)
|
2020-06-27 02:49:27 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
2020-06-27 02:49:27 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
buffer := make([]byte, fileBlockData.UncompressedFileSize)
|
|
|
|
mpqStream.Read(buffer, 0, fileBlockData.UncompressedFileSize)
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
return buffer, nil
|
|
|
|
}
|
|
|
|
|
2020-06-28 22:32:34 -04:00
|
|
|
// ReadFileStream reads the mpq file data and returns a stream
|
2020-07-04 22:37:13 -04:00
|
|
|
func (v *MPQ) ReadFileStream(fileName string) (d2interface.ArchiveDataStream, error) {
|
2020-06-27 02:49:27 -04:00
|
|
|
fileBlockData, err := v.getFileBlockData(fileName)
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-06-27 02:49:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-06-27 02:49:27 -04:00
|
|
|
fileBlockData.FileName = strings.ToLower(fileName)
|
|
|
|
fileBlockData.calculateEncryptionSeed()
|
|
|
|
|
|
|
|
mpqStream, err := CreateStream(v, fileBlockData, fileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &MpqDataStream{stream: mpqStream}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
// ReadTextFile reads a file and returns it as a string
|
2020-06-24 10:13:11 -04:00
|
|
|
func (v *MPQ) ReadTextFile(fileName string) (string, error) {
|
2020-01-26 00:39:13 -05:00
|
|
|
data, err := v.ReadFile(fileName)
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
return string(data), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *BlockTableEntry) calculateEncryptionSeed() {
|
|
|
|
fileName := path.Base(v.FileName)
|
|
|
|
v.EncryptionSeed = hashString(fileName, 3)
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
if !v.HasFlag(FileFixKey) {
|
|
|
|
return
|
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
v.EncryptionSeed = (v.EncryptionSeed + v.FilePosition) ^ v.UncompressedFileSize
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFileList returns the list of files in this MPQ
|
2020-06-27 02:49:27 -04:00
|
|
|
func (v *MPQ) GetFileList() ([]string, error) {
|
2020-01-26 00:39:13 -05:00
|
|
|
data, err := v.ReadFile("(listfile)")
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
raw := strings.TrimRight(string(data), "\x00")
|
|
|
|
s := bufio.NewScanner(strings.NewReader(raw))
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
var filePaths []string
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
for s.Scan() {
|
|
|
|
filePath := s.Text()
|
|
|
|
filePaths = append(filePaths, filePath)
|
|
|
|
}
|
2020-06-28 22:32:34 -04:00
|
|
|
|
2020-01-26 00:39:13 -05:00
|
|
|
return filePaths, nil
|
|
|
|
}
|
2020-07-04 22:37:13 -04:00
|
|
|
|
|
|
|
func (v *MPQ) Path() string {
|
|
|
|
return v.filePath
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MPQ) Contains(filename string) bool {
|
|
|
|
return v.hashEntryMap.Contains(filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *MPQ) Size() uint32 {
|
|
|
|
return v.data.ArchiveSize
|
|
|
|
}
|