-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchive_read_hash_table.go
70 lines (58 loc) · 1.95 KB
/
archive_read_hash_table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package mpq
import (
"bytes"
"fmt"
"io"
"github.com/Gophercraft/mpq/crypto"
"github.com/Gophercraft/mpq/info"
)
const max_hash_table_length = 50000000
// returns the absolute position of the Archive's hash table
func (archive *Archive) get_hash_table_position() (absolute_pos uint64, err error) {
absolute_pos = uint64(archive.archive_pos) + info.HashTablePos(&archive.header)
return
}
func (archive *Archive) get_hash_table_length() (length uint64, err error) {
return uint64(archive.header.HashTableSize), nil
}
// read all hash table entries into Archive
func (archive *Archive) read_hash_table(file io.ReadSeeker) (err error) {
// seek to the start of the hash table
var hash_table_position uint64
var hash_table_length uint64
hash_table_position, err = archive.get_hash_table_position()
if err != nil {
return
}
if _, err = file.Seek(int64(hash_table_position), io.SeekStart); err != nil {
return
}
// Allocate hash table
hash_table_length, err = archive.get_hash_table_length()
if err != nil {
return
}
if hash_table_length > max_hash_table_length {
err = fmt.Errorf("mpq: '%s' hash table is too long (%d entries)", archive.path, hash_table_length)
return
}
archive.hash_table = make([]info.HashTableEntry, hash_table_length)
// allocate temporary space to hold the encrypted data
hash_table_data := make([]byte, info.HashTableEntrySize*hash_table_length)
// read encrypted hash table
if _, err = io.ReadFull(file, hash_table_data); err != nil {
return
}
// decrypt hash table
decrypt_seed := crypto.HashString("(hash table)", crypto.HashEncryptKey)
crypto.Decrypt(decrypt_seed, hash_table_data)
// read hash table
hash_table_reader := bytes.NewReader(hash_table_data)
for i := range archive.hash_table {
hash_table_entry := &archive.hash_table[i]
if err = info.ReadHashTableEntry(hash_table_reader, hash_table_entry); err != nil {
return
}
}
return
}