Skip to content

Commit

Permalink
feat: make u24 and i24 simple units so they can be used for bit patte…
Browse files Browse the repository at this point in the history
…rn matching
  • Loading branch information
martinlindhe committed Jan 11, 2024
1 parent 560793b commit bd9820f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
9 changes: 8 additions & 1 deletion mapper/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ structs:
u16 Flags16:
bit b0000_0000_0000_1111: Lo16
bit b1111_1111_1111_0000: Hi16
u24 Flags24:
bit b0000_0000_0000_0000_0000_1111: Lo24
bit b1111_1111_1111_1111_1111_0000: Hi24
u32 Flags32:
bit b0000_0000_0000_0000_0000_0000_0000_1111: Lo32
bit b1111_1111_1111_1111_1111_1111_1111_0000: Hi32
Expand All @@ -77,6 +80,7 @@ layout:
in := []byte{
0x11,
0x00, 0x11,
0x00, 0x00, 0x11,
0x00, 0x00, 0x00, 0x11,
}
expected := `
Expand All @@ -87,7 +91,10 @@ Header
[000001] Flags16 u16 be 17 00 11
- Lo16 bit 0:4 1
- Hi16 bit 4:12 1
[000003] Flags32 u32 be 17 00 00 00 11
[000003] Flags24 u24 be 17 00 00 11
- Lo24 bit 0:4 1
- Hi24 bit 4:20 1
[000006] Flags32 u32 be 17 00 00 00 11
- Lo32 bit 0:4 1
- Hi32 bit 4:28 1
`
Expand Down
1 change: 1 addition & 0 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ type EvaluatedStruct struct {
Expressions []Expression
}

// matches data stream with bit patterns
func (es *Expression) EvaluateMatchPatterns(b []byte, endian string) ([]value.MatchedPattern, error) {
res := []value.MatchedPattern{}
if len(es.MatchPatterns) == 0 {
Expand Down
5 changes: 4 additions & 1 deletion value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func (df *DataField) IsSimpleUnit() bool {
return false
}
switch df.Kind {
case "u8", "i8", "u16", "i16", "u32", "i32", "f32", "u64", "i64", "ascii":
case "u8", "i8", "u16", "i16", "i24", "u24", "u32", "i32", "f32", "u64", "i64", "ascii":
return true
}
return false
Expand Down Expand Up @@ -389,6 +389,9 @@ func AsUint64(kind string, b []byte) uint64 {
return uint64(b[0])
case "u16", "i16", "dosdate", "dostime":
return uint64(binary.BigEndian.Uint16(b))
case "u24", "i24":
four := append([]byte{0}, b...)
return uint64(binary.BigEndian.Uint32(four))
case "u32", "i32", "f32", "time_t_32":
return uint64(binary.BigEndian.Uint32(b))
case "u64", "i64":
Expand Down

0 comments on commit bd9820f

Please sign in to comment.