-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patharray.go
More file actions
186 lines (151 loc) · 4.33 KB
/
array.go
File metadata and controls
186 lines (151 loc) · 4.33 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package vtypes
import (
"context"
"encoding/json"
"fmt"
"io"
"github.com/Velocidex/ordereddict"
"www.velocidex.com/golang/vfilter"
)
type ArrayParserOptions 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"`
Count int64 `vfilter:"optional,lambda=CountExpression,field=count,doc=Number of elements in the array (default 0)"`
MaxCount int64 `vfilter:"optional,field=max_count,doc=Maximum number of elements in the array (default 1000)"`
CountExpression *vfilter.Lambda
SentinelExpression *vfilter.Lambda `vfilter:"optional,field=sentinel,doc=A lambda expression that will be used to determine the end of the array"`
}
type ArrayParser struct {
options ArrayParserOptions
profile *Profile
parser Parser
invalid_parser bool
}
func (self *ArrayParser) New(profile *Profile, options *ordereddict.Dict) (Parser, error) {
if options == nil {
return nil, fmt.Errorf("Array parser requires a type in the options")
}
result := &ArrayParser{profile: profile}
ctx := context.Background()
err := ParseOptions(ctx, options, &result.options)
if err != nil {
return nil, fmt.Errorf("ArrayParser: %v", err)
}
if result.options.MaxCount == 0 {
result.options.MaxCount = 1000
}
// Get the parser now so we can catch errors in sub parser
// definitions
parser, err := maybeGetParser(profile,
result.options.Type, result.options.TypeOptions)
if err != nil {
return nil, err
}
// Cache the parser for next time.
result.parser = parser
return result, nil
}
func (self *ArrayParser) getCount(scope vfilter.Scope) int64 {
result := self.options.Count
if self.options.CountExpression != nil {
// Evaluate the offset expression with the current scope.
result = EvalLambdaAsInt64(self.options.CountExpression, scope)
}
if result > self.options.MaxCount {
return self.options.MaxCount
}
if result < 0 {
result = 0
}
return result
}
func (self *ArrayParser) Parse(
scope vfilter.Scope,
reader io.ReaderAt, offset int64) interface{} {
result_len := self.getCount(scope)
result := make([]interface{}, 0, result_len)
if self.invalid_parser {
return vfilter.Null{}
}
if self.parser == nil {
parser, err := self.profile.GetParser(
self.options.Type, self.options.TypeOptions)
if err != nil {
scope.Log("ERROR:binary_parser: ArrayParser: %v", err)
self.invalid_parser = true
return vfilter.Null{}
}
// Cache the parser for next time.
self.parser = parser
}
member_offset := int64(0)
for i := int64(0); i < result_len; i++ {
element := self.parser.Parse(
scope, reader, offset+member_offset)
// Check for a sentinel value
if self.options.SentinelExpression != nil {
ctx := context.Background()
subscope := scope.Copy()
sentinel := self.options.SentinelExpression.Reduce(
ctx, subscope, []vfilter.Any{element})
subscope.Close()
if scope.Bool(sentinel) {
break
}
}
// The parser may know about the element size, or the
// element itself.
element_size := SizeOf(self.parser)
if element_size == 0 {
element_size = SizeOf(element)
}
if element_size == 0 {
break
}
result = append(result, element)
member_offset += int64(element_size)
}
return &ArrayObject{
contents: result,
offset: offset,
size: member_offset,
}
}
type ArrayObject struct {
contents []interface{}
offset int64
size int64
}
func (self *ArrayObject) SetParent(parent *StructObject) {
for _, e := range self.contents {
switch t := e.(type) {
case *StructObject:
t.parent = parent
}
}
}
func (self *ArrayObject) Contents() []interface{} {
res := make([]interface{}, 0, len(self.contents))
for _, v := range self.contents {
res = append(res, ValueOf(v))
}
return res
}
func (self *ArrayObject) Get(i int64) (interface{}, error) {
if i < 0 || i > int64(len(self.contents)) {
return nil, NotFoundError
}
return self.contents[i], nil
}
func (self *ArrayObject) Size() int {
return int(self.size)
}
func (self *ArrayObject) Start() int64 {
return self.offset
}
func (self *ArrayObject) End() int64 {
return self.offset + self.size
}
func (self *ArrayObject) MarshalJSON() ([]byte, error) {
return json.Marshal(self.Contents())
}