-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprofile_parser.go
82 lines (67 loc) · 1.83 KB
/
profile_parser.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
package vtypes
import (
"errors"
"fmt"
"io"
"github.com/Velocidex/ordereddict"
"www.velocidex.com/golang/vfilter"
)
type ProfileParserOptions struct {
Type string
TypeOptions *ordereddict.Dict
Offset *vfilter.Lambda
}
type ProfileParser struct {
options ProfileParserOptions
profile *Profile
parser Parser
}
func (self *ProfileParser) New(profile *Profile, options *ordereddict.Dict) (Parser, error) {
var pres bool
if options == nil {
return nil, fmt.Errorf("Profile parser requires a type in the options")
}
result := &ProfileParser{profile: profile}
result.options.Type, pres = options.GetString("type")
if !pres {
return nil, errors.New("Profile parser must specify the type in options")
}
topts, pres := options.Get("type_options")
if pres {
topts_dict, ok := topts.(*ordereddict.Dict)
if ok {
result.options.TypeOptions = topts_dict
}
}
offset_expression, _ := options.GetString("offset")
if offset_expression != "" {
var err error
result.options.Offset, err = vfilter.ParseLambda(offset_expression)
if err != nil {
return nil, fmt.Errorf("Profile offset '%v': %w",
offset_expression, err)
}
}
if result.options.Offset == nil {
return nil, fmt.Errorf("Profile offset must be specified.")
}
return result, nil
}
func (self *ProfileParser) Parse(
scope vfilter.Scope,
reader io.ReaderAt, offset int64) interface{} {
if self.parser == nil {
parser, err := self.profile.GetParser(
self.options.Type, self.options.TypeOptions)
if err != nil {
scope.Log("ERROR:binary_parser: ProfileParser: %v", err)
self.parser = NullParser{}
return vfilter.Null{}
}
// Cache the parser for next time.
self.parser = parser
}
// Take the offset from the expression.
offset = EvalLambdaAsInt64(self.options.Offset, scope)
return self.parser.Parse(scope, reader, offset)
}