-
Notifications
You must be signed in to change notification settings - Fork 16
/
section.go
63 lines (53 loc) · 1.2 KB
/
section.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
// Copyright (c) 2018 Timo Savola. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package section
import (
"io"
"io/ioutil"
"github.com/tsavola/wag/internal/loader"
"github.com/tsavola/wag/internal/module"
"github.com/tsavola/wag/internal/reader"
)
func Find(
findId module.SectionId,
load loader.L,
sectionMapper func(sectionId byte, r reader.R) (payloadLen uint32, err error),
customLoader func(reader.R, uint32) error,
) module.SectionId {
for {
sectionId, err := load.R.ReadByte()
if err != nil {
if err == io.EOF {
return 0
}
panic(err)
}
id := module.SectionId(sectionId)
switch {
case id == module.SectionCustom:
var payloadLen uint32
if sectionMapper != nil {
payloadLen, err = sectionMapper(sectionId, load.R)
if err != nil {
panic(err)
}
} else {
payloadLen = load.Varuint32()
}
if customLoader != nil {
err = customLoader(load.R, payloadLen)
} else {
_, err = io.CopyN(ioutil.Discard, load.R, int64(payloadLen))
}
if err != nil {
panic(err)
}
case id == findId:
return id
default:
load.R.UnreadByte()
return id
}
}
}