Skip to content

Instantly share code, notes, and snippets.

@ohir
Created April 22, 2022 10:40
Show Gist options
  • Save ohir/9f633f411305b5dd164ec12626afadf9 to your computer and use it in GitHub Desktop.
Save ohir/9f633f411305b5dd164ec12626afadf9 to your computer and use it in GitHub Desktop.
//lint:file-ignore U1000 silence!
package main
import "fmt"
const (
b6m = 63
b4m = 15
)
type bts struct {
b2 [16]byte
b3 [2]uint64
}
var Bms *bts
func SetBms() {
t := new(bts)
t.b2 = [16]byte{0, 1}
t.b3 = [2]uint64{0, uint64(1) << 2}
Bms = t
return
}
func main() {
SetBms()
/*Atest(0xe0d00101) // bad 1st
Atest(0xe0e00102) // bad 2nd
Atest(0xe0d00102) // ok 3rd
Atest(0xe0d00202) // bad 4th
*/
b := []byte{
0xe0, 0xd0, 0x01, 0x01,
0xe0, 0xe0, 0x01, 0x02,
0xe0, 0xd0, 0x01, 0x02,
0xe0, 0xd0, 0x02, 0x02,
}
Filter(b, func(x u32, i int) { println("Aok:", x, " at", i) })
}
func Atest(a uint32) {
// on this kind (short-circuited ors) debugger entered
// in "true" block with all three parts being false (see pic)
if a>>16 != 0xE0D0 ||
Bms.b2[((a>>8)&b4m)] == 0 ||
Bms.b3[Bms.b2[(a>>8)&b4m]]&(1<<(a&b6m)) == 0 {
fmt.Printf("%08x !match by || short ciruit\n", a)
}
// on short-circuited ands debug worked ok
if a>>16 == 0xE0D0 &&
(Bms.b2[((a>>8)&b4m)] != 0) &&
Bms.b3[Bms.b2[(a>>8)&b4m]]&(1<<(a&b6m)) != 0 {
fmt.Printf("%08x match by && short ciruit\n", a)
}
}
type u32 = uint32
func Filter(buf []byte, aok func(x u32, i int)) {
var a u32
for i := 0; i < len(buf)-4; i += 4 {
a = u32(buf[i+3]) | u32(buf[i+2])<<8 |
u32(buf[i+1])<<16 | u32(buf[i])<<24
if a>>16 != 0xE0D0 ||
Bms.b2[((a>>8)&b4m)] == 0 ||
Bms.b3[Bms.b2[(a>>8)&b4m]]&(1<<(a&b6m)) == 0 {
continue
}
aok(a, i)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment