-
Notifications
You must be signed in to change notification settings - Fork 187
/
resources.go
375 lines (334 loc) · 8.42 KB
/
resources.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package qml
import (
"bytes"
"fmt"
"sort"
"strings"
)
// ParseResources parses the resources collection serialized in data.
func ParseResources(data []byte) (*Resources, error) {
if len(data) < 20 || string(data[:4]) != "qres" {
return nil, fmt.Errorf("invalid resources data")
}
r, err := parseResourcesHeader(data[:20], len(data))
if err != nil {
return nil, err
}
r.bdata = data
return r, nil
}
// ParseResourcesString parses the resources collection serialized in data.
func ParseResourcesString(data string) (*Resources, error) {
if len(data) < 20 || data[:4] != "qres" {
return nil, fmt.Errorf("invalid resources data")
}
r, err := parseResourcesHeader([]byte(data[:20]), len(data))
if err != nil {
return nil, err
}
r.sdata = data
return r, nil
}
func parseResourcesHeader(h []byte, size int) (*Resources, error) {
r := &Resources{
version: read32(h[4:]),
treeOffset: read32(h[8:]),
dataOffset: read32(h[12:]),
nameOffset: read32(h[16:]),
}
if r.version != resVersion {
return nil, fmt.Errorf("unsupported resources version: %d", r.version)
}
// Ideally this would do a full validation, but it's a good start.
if !(20 <= r.treeOffset && r.treeOffset < size &&
20 <= r.dataOffset && r.dataOffset < size &&
20 <= r.nameOffset && r.nameOffset < size) {
return nil, fmt.Errorf("corrupted resources data")
}
return r, nil
}
func read32(b []byte) int {
return int(uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3]))
}
// Resources is a compact representation of a collection of resources
// (images, qml files, etc) that may be loaded by an Engine and referenced
// by QML at "qrc:///some/path", where "some/path" is the path the
// resource was added with.
//
// Resources must be registered with LoadResources to become available.
type Resources struct {
sdata string
bdata []byte
version int
treeOffset int
dataOffset int
nameOffset int
}
// Bytes returns a binary representation of the resources collection that
// may be parsed back with ParseResources or ParseResourcesString.
func (r *Resources) Bytes() []byte {
if len(r.sdata) > 0 {
return []byte(r.sdata)
}
return r.bdata
}
// ResourcesPacker builds a Resources collection with provided resources.
type ResourcesPacker struct {
root resFile
}
// Pack builds a resources collection with all resources previously added.
func (rp *ResourcesPacker) Pack() *Resources {
rw := newResourcesWriter(rp)
rw.write()
return &Resources{
bdata: rw.out.Bytes(),
version: resVersion,
dataOffset: rw.dataOffset,
nameOffset: rw.nameOffset,
treeOffset: rw.treeOffset,
}
}
type resFile struct {
name string
sdata string
bdata []byte
children resFiles
}
// Add adds a resource with the provided data under "qrc:///"+path.
func (rp *ResourcesPacker) Add(path string, data []byte) {
file := rp.addFile(path)
file.bdata = data
}
// AddString adds a resource with the provided data under "qrc:///"+path.
func (rp *ResourcesPacker) AddString(path, data string) {
file := rp.addFile(path)
file.sdata = data
}
func (rp *ResourcesPacker) addFile(path string) *resFile {
file := &rp.root
names := strings.Split(path, "/")
if len(names[0]) == 0 {
names = names[1:]
}
NextItem:
for _, name := range names {
for i := range file.children {
child := &file.children[i]
if child.name == name {
file = child
continue NextItem
}
}
file.children = append(file.children, resFile{name: name})
file = &file.children[len(file.children)-1]
}
if len(file.children) > 0 || file.sdata != "" || file.bdata != nil {
panic("cannot add same resources path twice: " + path)
}
return file
}
type resWriter struct {
root *resFile
treeOffset int
dataOffset int
nameOffset int
treeOffsets map[*resFile]int
dataOffsets map[*resFile]int
nameOffsets map[string]int
pending []*resFile
out bytes.Buffer
}
func newResourcesWriter(rp *ResourcesPacker) *resWriter {
rw := &resWriter{
root: &rp.root,
treeOffsets: make(map[*resFile]int),
dataOffsets: make(map[*resFile]int),
nameOffsets: make(map[string]int),
pending: make([]*resFile, maxPending(&rp.root)),
}
pending := rw.pending
pending[0] = rw.root
n := 1
for n > 0 {
n--
file := pending[n]
sort.Sort(file.children)
for i := range file.children {
child := &file.children[i]
if len(child.children) > 0 {
pending[n] = child
n++
}
}
}
return rw
}
func maxPending(file *resFile) int {
max := 1
for i := range file.children {
if len(file.children) > 0 {
max += maxPending(&file.children[i])
}
}
return max
}
func (rw *resWriter) write() {
rw.writeHeader()
rw.writeDataBlobs()
rw.writeDataNames()
rw.writeDataTree()
rw.finishHeader()
}
func (rw *resWriter) writeHeader() {
rw.out.WriteString("qres\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
}
func (rw *resWriter) finishHeader() {
rw.write32at(4, resVersion)
rw.write32at(8, uint32(rw.treeOffset))
rw.write32at(12, uint32(rw.dataOffset))
rw.write32at(16, uint32(rw.nameOffset))
}
func (rw *resWriter) writeDataBlobs() {
rw.dataOffset = rw.out.Len()
pending := rw.pending
pending[0] = rw.root
n := 1
for n > 0 {
n--
file := pending[n]
for i := range file.children {
child := &file.children[i]
if len(child.children) > 0 {
pending[n] = child
n++
} else {
rw.dataOffsets[child] = rw.out.Len() - rw.dataOffset
rw.writeDataBlob(child)
}
}
}
}
func (rw *resWriter) writeDataBlob(file *resFile) {
if len(file.sdata) > 0 {
rw.write32(uint32(len(file.sdata)))
rw.out.WriteString(file.sdata)
} else {
rw.write32(uint32(len(file.bdata)))
rw.out.Write(file.bdata)
}
}
func (rw *resWriter) writeDataNames() {
rw.nameOffset = rw.out.Len()
pending := rw.pending
pending[0] = rw.root
n := 1
for n > 0 {
n--
file := pending[n]
for i := range file.children {
child := &file.children[i]
if len(child.children) > 0 {
pending[n] = child
n++
}
if _, ok := rw.nameOffsets[child.name]; !ok {
rw.nameOffsets[child.name] = rw.out.Len() - rw.nameOffset
rw.writeDataName(child.name)
}
}
}
}
func (rw *resWriter) writeDataName(name string) {
rw.write16(uint16(len(name)))
rw.write32(qt_hash(name))
for _, r := range name {
rw.write16(uint16(r))
}
}
func (rw *resWriter) writeDataTree() {
rw.treeOffset = rw.out.Len()
// Compute first child offset for each parent.
pending := rw.pending
pending[0] = rw.root
n := 1
offset := 1
for n > 0 {
n--
file := pending[n]
rw.treeOffsets[file] = offset
for i := range file.children {
child := &file.children[i]
offset++
if len(child.children) > 0 {
pending[n] = child
n++
}
}
}
// Actually write it out.
rw.writeDataInfo(rw.root)
pending[0] = rw.root
n = 1
for n > 0 {
n--
file := pending[n]
for i := range file.children {
child := &file.children[i]
rw.writeDataInfo(child)
if len(child.children) > 0 {
pending[n] = child
n++
}
}
}
}
func (rw *resWriter) writeDataInfo(file *resFile) {
rw.write32(uint32(rw.nameOffsets[file.name]))
if len(file.children) > 0 {
rw.write16(uint16(resDirectory))
rw.write32(uint32(len(file.children)))
rw.write32(uint32(rw.treeOffsets[file]))
} else {
rw.write16(uint16(resNone))
rw.write16(0) // QLocale::AnyCountry
rw.write16(1) // QLocale::C
rw.write32(uint32(rw.dataOffsets[file]))
}
}
const (
resVersion = 1
resNone = 0
resCompressed = 1
resDirectory = 2
)
func (rw *resWriter) write16(v uint16) {
rw.out.Write([]byte{byte(v >> 8), byte(v)})
}
func (rw *resWriter) write32(v uint32) {
rw.out.Write([]byte{byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)})
}
func (rw *resWriter) write32at(index int, v uint32) {
b := rw.out.Bytes()
b[index+0] = byte(v >> 24)
b[index+1] = byte(v >> 16)
b[index+2] = byte(v >> 8)
b[index+3] = byte(v)
}
type resFiles []resFile
func (rf resFiles) Len() int { return len(rf) }
func (rf resFiles) Less(i, j int) bool { return qt_hash(rf[i].name) < qt_hash(rf[j].name) }
func (rf resFiles) Swap(i, j int) { rf[i], rf[j] = rf[j], rf[i] }
// qt_hash returns the hash of p as determined by the internal qt_hash function in Qt.
//
// According to the documentation in qhash.cpp this algorithm is used whenever
// the hash may be stored or reused across Qt versions, and must not change.
// The algorithm in qHash (used in QString, etc) is different and may change.
func qt_hash(p string) uint32 {
var h uint32
for _, r := range p {
h = (h << 4) + uint32(r)
h ^= (h & 0xf0000000) >> 23
h &= 0x0fffffff
}
return h
}