-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.go
More file actions
101 lines (82 loc) · 1.85 KB
/
program.go
File metadata and controls
101 lines (82 loc) · 1.85 KB
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
package pbf
import (
"encoding/binary"
"errors"
"io"
"math"
)
const bytecodeHeader = uint32(0x00464250) // "PBF\0"
var (
errBytecodeFormat = errors.New("pbf: unknown bytecode format")
errBytecodeInvalid = errors.New("pbf: bytecode is invalid")
errBytecodeTooLong = errors.New("pbf: bytecode is too long")
)
// Program for filtering protobuf messages.
type Program struct {
program
}
// NewProgram decodes and verifies a PBF bytecode program.
func NewProgram(bytecode []byte) (*Program, error) {
if len(bytecode) < 4 {
return nil, io.ErrUnexpectedEOF
}
header := binary.LittleEndian.Uint32(bytecode)
off := 4
if header != bytecodeHeader {
return nil, errBytecodeFormat
}
if len(bytecode) > math.MaxInt32 {
// Const offsets and lengths could overflow the field data encoding.
return nil, errBytecodeTooLong
}
fieldspec, fieldcount, n, err := parseFieldSection(bytecode[off:])
if err != nil {
return nil, err
}
off += n
p := program{
bytecode: bytecode,
fieldcount: fieldcount,
insnoffset: off,
}
p.initFieldSpec(fieldspec)
if debugging {
debugf("prog: Instruction offset: %d\n", p.insnoffset)
}
if err := verify(p); err != nil {
return nil, err
}
return &Program{p}, nil
}
type program struct {
bytecode []byte
fieldcount uint8
insnoffset int
fieldspecarr *[256]fieldSpec
maxarrindex uint8
fieldspecmap map[int32]fieldSpec
}
func (p *program) initFieldSpec(spec map[int32]fieldSpec) {
var maxtag uint8
for tag := range spec {
if tag < 0 || tag >= 256 {
p.fieldspecmap = spec
return
}
if uint8(tag) > maxtag {
maxtag = uint8(tag)
}
}
var arr [256]fieldSpec
for i := range arr {
arr[i].mod = 0xff // Invalid mod value.
}
for tag, s := range spec {
arr[uint8(tag)] = s
}
p.fieldspecarr = &arr
p.maxarrindex = maxtag
}
func (p *program) insn() []byte {
return p.bytecode[p.insnoffset:]
}