-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbitfield.go
60 lines (48 loc) · 1.25 KB
/
bitfield.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
package vtypes
import (
"fmt"
"io"
"github.com/Velocidex/ordereddict"
"www.velocidex.com/golang/vfilter"
)
type BitField struct {
StartBit int64 `json:"start_bit"`
EndBit int64 `json:"end_bit"`
Type string `json:"type"`
parser Parser
}
func (self *BitField) New(profile *Profile, options *ordereddict.Dict) (Parser, error) {
parser_type, pres := options.GetString("type")
if !pres {
return nil, fmt.Errorf("BitField parser requires a type in the options")
}
parser, err := profile.GetParser(parser_type, ordereddict.NewDict())
if err != nil {
return nil, fmt.Errorf("BitField parser requires a type in the options: %w", err)
}
start_bit, pres := options.GetInt64("start_bit")
if !pres || start_bit < 0 {
start_bit = 0
}
end_bit, pres := options.GetInt64("end_bit")
if !pres || end_bit > 64 {
end_bit = 64
}
return &BitField{
StartBit: start_bit,
EndBit: end_bit,
parser: parser,
}, nil
}
func (self *BitField) Parse(
scope vfilter.Scope, reader io.ReaderAt, offset int64) interface{} {
result := int64(0)
value, ok := to_int64(self.parser.Parse(scope, reader, offset))
if !ok {
return 0
}
for i := self.StartBit; i < self.EndBit; i++ {
result |= value & (1 << uint8(i))
}
return result >> self.StartBit
}