-
Notifications
You must be signed in to change notification settings - Fork 2
/
pack.go
144 lines (119 loc) · 2.78 KB
/
pack.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
package main
import (
"crypto/sha1"
"errors"
"fmt"
"io"
"sort"
"golang.org/x/crypto/blowfish"
)
const (
flagChecksum uint32 = 0x00010000
flagEncrypted uint32 = 0x00020000
)
type filesAndIds struct {
files []fileInfo
ids []uint32
}
func (fids *filesAndIds) Len() int {
return len(fids.ids)
}
func (fids *filesAndIds) Less(i, j int) bool {
return int32(fids.ids[i]) < int32(fids.ids[j])
}
func (fids *filesAndIds) Swap(i, j int) {
fids.files[i], fids.files[j] = fids.files[j], fids.files[i]
fids.ids[i], fids.ids[j] = fids.ids[j], fids.ids[i]
}
func writeIndex(w io.Writer, files []fileInfo, fileID fileID) error {
count := uint16(len(files))
size := uint32(0)
for _, fi := range files {
size += uint32(fi.Size())
}
if _, err := writeUint16(w, count); err != nil {
return err
} else if _, err := writeUint32(w, size); err != nil {
return err
}
var ids []uint32
for _, fi := range files {
ids = append(ids, fileID(fi.Name()))
}
sort.Sort(&filesAndIds{
files: files,
ids: ids,
})
for i := 1; i < len(ids); i++ {
if ids[i-1] == ids[i] {
return fmt.Errorf("ID collision %x on %s and %s", ids[i], files[i].Name(), files[i-1].Name())
}
}
offset := uint32(0)
for i, fi := range files {
id := ids[i]
size := uint32(fi.Size())
if _, err := writeUint32(w, id); err != nil {
return err
} else if _, err := writeUint32(w, offset); err != nil {
return err
} else if _, err := writeUint32(w, size); err != nil {
return err
}
offset += size
}
return nil
}
func writeBody(w io.Writer, files []fileInfo) error {
for _, fi := range files {
f, err := fi.Open()
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(w, f); err != nil {
return err
}
}
return nil
}
func pack(w io.Writer, files []fileInfo, game gameID, flags uint32, keySource []byte) error {
if game != gameCC1 {
if _, err := writeUint32(w, flags); err != nil {
return err
}
} else if flags != 0 {
return errors.New("game cc1 does not support flags")
}
fileID := getFileID(game)
if (flags & flagEncrypted) != 0 {
if _, err := w.Write(keySource); err != nil {
return err
}
blowfishKey := blowfishKeyFromKeySource(keySource)
cipher, err := blowfish.NewCipher(blowfishKey)
if err != nil {
return err
}
e := newEcbWriter(w, cipher)
if err := writeIndex(e, files, fileID); err != nil {
return err
} else if err := e.Flush(); err != nil {
return err
}
} else if err := writeIndex(w, files, fileID); err != nil {
return err
}
if (flags & flagChecksum) != 0 {
h := sha1.New()
mw := io.MultiWriter(w, h)
if err := writeBody(mw, files); err != nil {
return err
} else if _, err := w.Write(h.Sum(nil)); err != nil {
return err
}
} else if err := writeBody(w, files); err != nil {
return err
}
return nil
}