-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexefs.go
65 lines (51 loc) · 1.32 KB
/
exefs.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
package ctrsigcheck
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"github.com/connesc/ctrsigcheck/ctrutil"
)
// ExeFS describes the result of ExeFS parsing.
type ExeFS struct {
Icon *SMDH
}
// ParseExeFS extracts some information from the given ExeFS file.
//
// No integrity checks are performed.
func ParseExeFS(input io.Reader) (*ExeFS, error) {
reader := ctrutil.NewReader(input)
header := make([]byte, 0x200)
_, err := io.ReadFull(reader, header)
if err != nil {
return nil, fmt.Errorf("exefs: failed to read header: %w", err)
}
var iconOffset uint32
var iconSize uint32
for i := 0; i < 10; i++ {
fileHeader := header[i*0x10 : (i+1)*0x10]
fileName := string(bytes.TrimRight(fileHeader[:0x8], "\x00"))
if fileName == "icon" {
iconOffset = binary.LittleEndian.Uint32(fileHeader[0x8:])
iconSize = binary.LittleEndian.Uint32(fileHeader[0xc:])
}
}
var icon *SMDH
if iconSize > 0 {
if iconSize != 0x36c0 {
return nil, fmt.Errorf("exefs: when present, icon must have size %d, got %d", 0x36c0, iconSize)
}
err = reader.Discard(int64(iconOffset))
if err != nil {
return nil, fmt.Errorf("exefs: failed to jump to icon: %w", err)
}
data := io.LimitReader(reader, int64(iconSize))
icon, err = ParseSMDH(data)
if err != nil {
return nil, err
}
}
return &ExeFS{
Icon: icon,
}, nil
}