-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpointer.go
75 lines (59 loc) · 1.51 KB
/
pointer.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
package vtypes
import (
"encoding/binary"
"errors"
"fmt"
"io"
"github.com/Velocidex/ordereddict"
"www.velocidex.com/golang/vfilter"
)
type PointerParserOptions struct {
Type string
TypeOptions *ordereddict.Dict
}
type PointerParser struct {
options PointerParserOptions
profile *Profile
parser Parser
}
func (self *PointerParser) New(profile *Profile, options *ordereddict.Dict) (Parser, error) {
var pres bool
if options == nil {
return nil, fmt.Errorf("Pointer parser requires a type in the options")
}
result := &PointerParser{profile: profile}
result.options.Type, pres = options.GetString("type")
if !pres {
return nil, errors.New("Pointer 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
}
}
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 {
return vfilter.Null{}
}
address := binary.LittleEndian.Uint64(buf)
return self.parser.Parse(scope, reader, int64(address))
}