-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbitfield.go
More file actions
77 lines (61 loc) · 1.98 KB
/
bitfield.go
File metadata and controls
77 lines (61 loc) · 1.98 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
package vtypes
import (
"context"
"fmt"
"io"
"github.com/Velocidex/ordereddict"
"www.velocidex.com/golang/vfilter"
)
type BitFieldOptions struct {
StartBit int64 `json:"start_bit" vfilter:"optional,field=start_bit,doc=The start bit in the int to read"`
EndBit int64 `json:"end_bit" vfilter:"optional,field=end_bit,doc=The end bit in the int to read"`
Type string `json:"type" vfilter:"required,field=type,doc=The underlying type of the bit field"`
}
type BitFieldParser struct {
options BitFieldOptions
parser Parser
}
func (self *BitFieldParser) New(profile *Profile, options *ordereddict.Dict) (Parser, error) {
if options == nil {
return nil, fmt.Errorf("BitField parser requires an options dict")
}
result := &BitFieldParser{}
ctx := context.Background()
err := ParseOptions(ctx, options, &result.options)
if err != nil {
return nil, err
}
if result.options.EndBit == 0 {
result.options.EndBit = 64
}
if result.options.StartBit < 0 || result.options.StartBit > 64 {
return nil, fmt.Errorf("BitField start_bit should be between 0-64")
}
if result.options.EndBit < 0 || result.options.EndBit > 64 {
return nil, fmt.Errorf("BitField end_bit should be between 0-64")
}
if result.options.EndBit <= result.options.StartBit {
return nil, fmt.Errorf(
"BitField end_bit (%v) should be larger than start_bit (%v)",
result.options.EndBit, result.options.StartBit)
}
// Type must be available at definition time because bit fields
// can not operate on custome types.
result.parser, err = profile.GetParser(result.options.Type, nil)
return result, err
}
func (self *BitFieldParser) Parse(
scope vfilter.Scope, reader io.ReaderAt, offset int64) interface{} {
if self.parser == nil {
return vfilter.Null{}
}
result := int64(0)
value, ok := to_int64(self.parser.Parse(scope, reader, offset))
if !ok {
return 0
}
for i := self.options.StartBit; i < self.options.EndBit; i++ {
result |= value & (1 << uint8(i))
}
return result >> self.options.StartBit
}