-
Notifications
You must be signed in to change notification settings - Fork 6
/
rcc.go
67 lines (53 loc) · 1.74 KB
/
rcc.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
package qrc
import (
"encoding/binary"
"fmt"
"io"
)
// RCCHeaderMagic identifies a RCC file.
var RCCHeaderMagic = [4]byte{'q', 'r', 'e', 's'}
// RCCHeader is the header of a Qt resource file in the binary format. Note that
// the offsets are relative to the start of the file (i.e. the start of the
// header).
type RCCHeader struct {
Magic [4]byte
FormatVersion int32
TreeOffset int32
DataOffset int32
NamesOffset int32
// FormatVersion >= 3
OverallFlags int32
}
// ParseRCCHeader parses the RCC header. If the magic bytes are invalid, an
// error is returned. If an error occurs, any number of bytes may have been read
// from the reader.
func ParseRCCHeader(r io.Reader) (*RCCHeader, error) {
var h RCCHeader
if err := binary.Read(r, binary.BigEndian, &h.Magic); err != nil {
return nil, fmt.Errorf("read magic: %w", err)
}
if h.Magic != RCCHeaderMagic {
return nil, fmt.Errorf("invalid magic %#v", h.Magic)
}
if err := binary.Read(r, binary.BigEndian, &h.FormatVersion); err != nil {
return nil, fmt.Errorf("read format version: %w", err)
}
if err := binary.Read(r, binary.BigEndian, &h.TreeOffset); err != nil {
return nil, fmt.Errorf("read tree offset: %w", err)
}
if err := binary.Read(r, binary.BigEndian, &h.DataOffset); err != nil {
return nil, fmt.Errorf("read data offset: %w", err)
}
if err := binary.Read(r, binary.BigEndian, &h.NamesOffset); err != nil {
return nil, fmt.Errorf("read names offset: %w", err)
}
if h.FormatVersion >= 3 {
if err := binary.Read(r, binary.BigEndian, &h.OverallFlags); err != nil {
return nil, fmt.Errorf("read overall flags: %w", err)
}
}
if h.FormatVersion > 3 {
return nil, fmt.Errorf("unsupported format version %d", &h.FormatVersion)
}
return &h, nil
}