-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpointer.go
More file actions
74 lines (59 loc) · 1.71 KB
/
pointer.go
File metadata and controls
74 lines (59 loc) · 1.71 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
package vtypes
import (
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"github.com/Velocidex/ordereddict"
"www.velocidex.com/golang/vfilter"
)
type PointerParserOptions struct {
Type string `vfilter:"required,field=type,doc=The underlying type of the choice"`
TypeOptions *ordereddict.Dict `vfilter:"optional,field=type_options,doc=Any additional options required to parse the type"`
}
type PointerParser struct {
options PointerParserOptions
profile *Profile
parser Parser
}
func (self *PointerParser) New(profile *Profile, options *ordereddict.Dict) (Parser, error) {
if options == nil {
return nil, fmt.Errorf("Pointer parser requires a type in the options")
}
result := &PointerParser{profile: profile}
ctx := context.Background()
err := ParseOptions(ctx, options, &result.options)
if err != nil {
return nil, fmt.Errorf("PointerParser: %v", err)
}
parser, err := maybeGetParser(profile,
result.options.Type, result.options.TypeOptions)
if err != nil {
return nil, err
}
result.parser = parser
return result, nil
}
func (self *PointerParser) 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: PointerParser: %v", err)
self.parser = NullParser{}
return vfilter.Null{}
}
// Cache the parser for next time.
self.parser = parser
}
buf := make([]byte, 8)
n, err := reader.ReadAt(buf, offset)
if n == 0 || (err != nil && !errors.Is(err, io.EOF)) {
return vfilter.Null{}
}
address := binary.LittleEndian.Uint64(buf)
return self.parser.Parse(scope, reader, int64(address))
}