-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocal.go
279 lines (256 loc) · 7.11 KB
/
local.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package casc
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strconv"
"github.com/jybp/casc/blte"
"github.com/jybp/casc/common"
"github.com/pkg/errors"
)
type local struct {
app string
versionName string
rootEncodedHash []byte
dataDir string
encoding map[string][][]byte
idxs map[uint8][]common.IdxEntry
}
func newLocalStorage(installDir string) (l *local, err error) {
//
// app & versionName
//
var dirToApp = map[string]string{
"Diablo III": Diablo3,
"StarCraft": Starcraft1,
"Warcraft III": Warcraft3,
}
app, ok := dirToApp[filepath.Base(installDir)]
if !ok {
return nil, errors.WithStack(errors.New("unsupported app"))
}
var cascDir string
switch app {
case Diablo3, Starcraft1, Warcraft3:
cascDir = filepath.Join(installDir, "Data")
default:
return nil, errors.WithStack(errors.New("unsupported app"))
}
buildInfoB, err := ioutil.ReadFile(filepath.Join(installDir, ".build.info"))
if err != nil {
return nil, errors.WithStack(err)
}
versions, err := common.ParseLocalBuildInfo(bytes.NewReader(buildInfoB))
if err != nil {
return nil, errors.WithStack(err)
}
if len(versions) == 0 {
return nil, errors.WithStack(errors.New("no entries within .build.info"))
}
var version common.Version = versions[0]
for _, v := range versions {
if len(v.ProductCode) > 0 && v.ProductCode == app {
version = v
break
}
}
fmt.Fprintf(common.Wlog, "app %s, version %s and region %s\n", app, version.Name, version.Region)
//
// rootEncodedHash & app
//
configDir := filepath.Join(cascDir, common.PathTypeConfig)
dataDir := filepath.Join(cascDir, common.PathTypeData)
buildConfigHash := hex.EncodeToString(version.BuildConfigHash)
buildConfigB, err := ioutil.ReadFile(filepath.Join(
configDir,
buildConfigHash[0:2],
buildConfigHash[2:4],
buildConfigHash))
if err != nil {
return nil, errors.WithStack(err)
}
buildCfg, err := common.ParseBuildConfig(bytes.NewReader(buildConfigB))
if err != nil {
return nil, err
}
rootHash := buildCfg.RootHash
var productToApps = map[string]string{
"Diablo3": Diablo3,
"StarCraft1": Starcraft1,
"War3": Warcraft3,
}
buildApp, ok := productToApps[buildCfg.BuildProduct]
if !ok {
return nil, errors.WithStack(errors.Errorf("unknown build-product: %s", buildCfg.BuildProduct))
}
if app != buildApp {
return nil, errors.WithStack(errors.Errorf("inconsistent app %s != %s", app, buildApp))
}
//
// encoding & dataFromEncodedHashFn
//
// Load all indices
files, err := ioutil.ReadDir(dataDir)
if err != nil {
return nil, errors.WithStack(err)
}
// There is multiple files for the same bucket with duplicate entries.
// It looks like the last file contains the most up to date indices.
// Sort the files accordingly so that the first index findIdx finds is the correct.
sort.Slice(files, func(i, j int) bool { return files[i].Name() > files[j].Name() })
idxEntries := map[uint8][]common.IdxEntry{}
for _, file := range files {
name := file.Name()
if len(name) < 4 {
continue
}
if name[len(name)-4:] != ".idx" {
continue
}
f, err := os.Open(filepath.Join(dataDir, name))
if err != nil {
return nil, errors.WithStack(err)
}
defer func() {
if cerr := f.Close(); cerr != nil {
err = cerr
}
}()
bucketID, err := strconv.ParseUint(string(name[1]), 16, 8)
if err != nil {
return nil, errors.WithStack(err)
}
fmt.Fprintf(common.Wlog, "bucket %x: %s\n", uint8(bucketID), name)
indices, err := common.ParseIdx(f)
if err != nil {
return nil, err
}
idxEntries[uint8(bucketID)] = append(idxEntries[uint8(bucketID)], indices...)
}
if len(buildCfg.EncodingHashes) < 2 {
return nil, errors.WithStack(errors.New("expected at least two encoding hash"))
}
encodingR, err := dataFromEncodedHash(buildCfg.EncodingHashes[1], dataDir, idxEntries)
if err != nil {
return nil, err
}
encoding, err := common.ParseEncoding(bytes.NewReader(encodingR))
if err != nil {
return nil, err
}
return &local{
app: app,
versionName: version.Name,
rootEncodedHash: rootHash,
encoding: encoding,
dataDir: dataDir,
idxs: idxEntries,
}, nil
}
func (s *local) App() string {
return s.app
}
func (s *local) Version() string {
return s.versionName
}
func (s *local) RootHash() []byte {
return s.rootEncodedHash
}
func (s *local) FromContentHash(hash []byte) ([]byte, error) {
encodedHashes, ok := s.encoding[hex.EncodeToString(hash)]
if !ok || len(encodedHashes) == 0 {
return nil, ErrNotFound
}
return dataFromEncodedHash(encodedHashes[0], s.dataDir, s.idxs)
}
func bucketID(hash []byte) (uint8, error) {
if len(hash) < 9 {
return 0, errors.WithStack(errors.New("invalid hash len"))
}
i := hash[0] ^ hash[1] ^ hash[2] ^ hash[3] ^ hash[4] ^ hash[5] ^ hash[6] ^ hash[7] ^ hash[8]
return (i & 0xf) ^ (i >> 4), nil
}
func findIdx(hash []byte, idxs []common.IdxEntry) (common.IdxEntry, error) {
foundIdx := common.IdxEntry{}
for _, idx := range idxs {
keyLen := len(idx.Key)
hashLen := len(hash)
shift := hashLen - keyLen
if shift < 0 {
return common.IdxEntry{}, errors.WithStack(errors.New("invalid key/hash len"))
}
h := hash[:len(hash)-shift]
if bytes.Compare(h, idx.Key) == 0 {
foundIdx = idx
break
}
}
if foundIdx.Key == nil {
return common.IdxEntry{}, ErrNotFound
}
return foundIdx, nil
}
func dataFromEncodedHash(hash []byte, dataDir string, idxs map[uint8][]common.IdxEntry) (b []byte, err error) {
bucketID, err := bucketID(hash)
if err != nil {
return nil, err
}
indices, ok := idxs[bucketID]
if !ok {
return nil, errors.WithStack(fmt.Errorf("bucket %x not found", bucketID))
}
idx, err := findIdx(hash, indices)
if err != nil {
return nil, err
}
dataFilename := filepath.Join(dataDir, "data."+fmt.Sprintf("%03d", idx.Index))
f, err := os.Open(dataFilename)
if err != nil {
return nil, errors.WithStack(err)
}
defer func() {
if cerr := f.Close(); cerr != nil {
err = cerr
}
}()
if _, err := f.Seek(int64(idx.Offset), io.SeekStart); err != nil {
return nil, errors.WithStack(err)
}
// first 9 bytes of reversed blteHash must match hash
blteHash := make([]byte, 16)
if err := binary.Read(f, binary.LittleEndian, &blteHash); err != nil {
return nil, errors.WithStack(err)
}
for i := len(blteHash)/2 - 1; i >= 0; i-- { //reverse
opp := len(blteHash) - 1 - i
blteHash[i], blteHash[opp] = blteHash[opp], blteHash[i]
}
if len(hash) < 9 || bytes.Compare(blteHash[:9], hash[:9]) != 0 {
return nil, errors.WithStack(errors.New("corrupted local file"))
}
var size uint32
if err := binary.Read(f, binary.LittleEndian, &size); err != nil {
return nil, errors.WithStack(err)
}
if size != idx.Size {
return nil, errors.WithStack(errors.New("inconsistent size"))
}
if _, err := f.Seek(10, io.SeekCurrent); err != nil { //unk, ChecksumA, ChecksumB
return nil, errors.WithStack(err)
}
blteReader, err := blte.NewReader(io.LimitReader(f, int64(idx.Size-30)))
if err != nil {
return nil, errors.WithStack(err)
}
b, err = ioutil.ReadAll(blteReader)
if err != nil {
return nil, errors.WithStack(err)
}
return b, nil
}