2021-11-05 17:04:26 +03:00
|
|
|
package decode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-11-24 13:15:42 +03:00
|
|
|
"encoding/binary"
|
2021-11-05 17:04:26 +03:00
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/wader/fq/pkg/bitio"
|
2021-12-02 00:48:25 +03:00
|
|
|
"github.com/wader/fq/pkg/scalar"
|
2021-11-05 17:04:26 +03:00
|
|
|
)
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
func bitBufIsZero(s scalar.S, isValidate bool) (scalar.S, error) {
|
2021-11-05 17:04:26 +03:00
|
|
|
bb, ok := s.Actual.(*bitio.Buffer)
|
|
|
|
if !ok {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
isZero := true
|
2021-12-02 00:48:25 +03:00
|
|
|
// TODO: shared
|
|
|
|
b := make([]byte, 32*1024)
|
2021-11-05 17:04:26 +03:00
|
|
|
bLen := len(b) * 8
|
|
|
|
bbLeft := int(bb.Len())
|
|
|
|
bbPos := int64(0)
|
|
|
|
|
|
|
|
for bbLeft > 0 {
|
|
|
|
rl := bbLeft
|
|
|
|
if bbLeft > bLen {
|
|
|
|
rl = bLen
|
|
|
|
}
|
|
|
|
// zero last byte if uneven read
|
|
|
|
if rl%8 != 0 {
|
|
|
|
b[rl/8] = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := bitio.ReadAtFull(bb, b, rl, bbPos)
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
nb := int(bitio.BitsByteCount(int64(n)))
|
|
|
|
|
|
|
|
for i := 0; i < nb; i++ {
|
|
|
|
if b[i] != 0 {
|
|
|
|
isZero = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bbLeft -= n
|
|
|
|
}
|
|
|
|
|
|
|
|
if isZero {
|
|
|
|
s.Description = "all zero"
|
|
|
|
} else {
|
|
|
|
s.Description = "all not zero"
|
2021-12-02 00:48:25 +03:00
|
|
|
if isValidate {
|
2021-11-05 17:04:26 +03:00
|
|
|
return s, errors.New("validate is zero failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
func (d *D) BitBufIsZero() scalar.Mapper {
|
|
|
|
return scalar.Fn(func(s scalar.S) (scalar.S, error) {
|
|
|
|
return bitBufIsZero(s, false)
|
|
|
|
})
|
2021-11-05 17:04:26 +03:00
|
|
|
}
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
func (d *D) BitBufValidateIsZero() scalar.Mapper {
|
|
|
|
return scalar.Fn(func(s scalar.S) (scalar.S, error) {
|
|
|
|
return bitBufIsZero(s, !d.Options.Force)
|
|
|
|
})
|
2021-11-05 17:04:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: generate?
|
2021-12-02 00:48:25 +03:00
|
|
|
func assertBitBuf(s scalar.S, isErr bool, bss ...[]byte) (scalar.S, error) {
|
|
|
|
ab, err := s.ActualBitBuf().Bytes()
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
for _, bs := range bss {
|
|
|
|
if bytes.Equal(ab, bs) {
|
|
|
|
s.Description = "valid"
|
|
|
|
return s, nil
|
2021-11-05 17:04:26 +03:00
|
|
|
}
|
|
|
|
}
|
2021-12-02 00:48:25 +03:00
|
|
|
s.Description = "invalid"
|
|
|
|
if isErr {
|
|
|
|
return s, errors.New("failed to validate raw")
|
|
|
|
}
|
|
|
|
return s, nil
|
2021-11-05 17:04:26 +03:00
|
|
|
}
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
func (d *D) AssertBitBuf(bss ...[]byte) scalar.Mapper {
|
|
|
|
return scalar.Fn(func(s scalar.S) (scalar.S, error) {
|
|
|
|
return assertBitBuf(s, !d.Options.Force, bss...)
|
|
|
|
})
|
2021-11-05 17:04:26 +03:00
|
|
|
}
|
2021-12-02 00:48:25 +03:00
|
|
|
|
|
|
|
func (d *D) ValidateBitBuf(bss ...[]byte) scalar.Mapper {
|
|
|
|
return scalar.Fn(func(s scalar.S) (scalar.S, error) {
|
|
|
|
return assertBitBuf(s, false, bss...)
|
|
|
|
})
|
2021-11-05 17:04:26 +03:00
|
|
|
}
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
func assertUBytes(s scalar.S, isErr bool, endian Endian, bss ...[]byte) (scalar.S, error) {
|
2021-11-24 13:15:42 +03:00
|
|
|
var bo binary.ByteOrder
|
|
|
|
switch endian {
|
|
|
|
case LittleEndian:
|
|
|
|
bo = binary.BigEndian
|
|
|
|
case BigEndian:
|
|
|
|
bo = binary.BigEndian
|
|
|
|
default:
|
|
|
|
panic("invalid endian")
|
|
|
|
}
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
au := s.ActualU()
|
|
|
|
for _, bs := range bss {
|
|
|
|
var bu uint64
|
|
|
|
switch len(bs) {
|
|
|
|
case 1:
|
|
|
|
bu = uint64(bs[0])
|
|
|
|
case 2:
|
|
|
|
bu = uint64(bo.Uint16(bs))
|
|
|
|
case 4:
|
|
|
|
bu = uint64(bo.Uint32(bs))
|
|
|
|
case 8:
|
|
|
|
bu = bo.Uint64(bs)
|
|
|
|
default:
|
|
|
|
panic("invalid bs length")
|
|
|
|
}
|
|
|
|
|
|
|
|
if au == bu {
|
|
|
|
s.Description = "valid"
|
|
|
|
return s, nil
|
2021-11-05 17:04:26 +03:00
|
|
|
}
|
|
|
|
}
|
2021-12-02 00:48:25 +03:00
|
|
|
s.Description = "invalid"
|
|
|
|
if isErr {
|
|
|
|
return s, errors.New("failed to validate raw")
|
2021-11-05 17:04:26 +03:00
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
func (d *D) AssertUBytes(bss ...[]byte) scalar.Mapper {
|
|
|
|
return scalar.Fn(func(s scalar.S) (scalar.S, error) {
|
|
|
|
return assertUBytes(s, true, d.Endian, bss...)
|
|
|
|
})
|
2021-11-05 17:04:26 +03:00
|
|
|
}
|
2021-12-02 00:48:25 +03:00
|
|
|
func (d *D) ValidateUBytes(bss ...[]byte) scalar.Mapper {
|
|
|
|
return scalar.Fn(func(s scalar.S) (scalar.S, error) {
|
|
|
|
return assertUBytes(s, false, d.Endian, bss...)
|
|
|
|
})
|
2021-11-05 17:04:26 +03:00
|
|
|
}
|
2021-12-02 00:48:25 +03:00
|
|
|
func (d *D) AssertULEBytes(bss ...[]byte) scalar.Mapper {
|
|
|
|
return scalar.Fn(func(s scalar.S) (scalar.S, error) {
|
|
|
|
return assertUBytes(s, true, LittleEndian, bss...)
|
|
|
|
})
|
2021-11-05 17:04:26 +03:00
|
|
|
}
|
2021-12-02 00:48:25 +03:00
|
|
|
func (d *D) ValidateULEBytes(bss ...[]byte) scalar.Mapper {
|
|
|
|
return scalar.Fn(func(s scalar.S) (scalar.S, error) {
|
|
|
|
return assertUBytes(s, false, LittleEndian, bss...)
|
|
|
|
})
|
2021-11-05 17:04:26 +03:00
|
|
|
}
|
2021-12-02 00:48:25 +03:00
|
|
|
func (d *D) AssertUBEBytes(bss ...[]byte) scalar.Mapper {
|
|
|
|
return scalar.Fn(func(s scalar.S) (scalar.S, error) {
|
|
|
|
return assertUBytes(s, true, BigEndian, bss...)
|
|
|
|
})
|
2021-11-05 17:04:26 +03:00
|
|
|
}
|
2021-12-02 00:48:25 +03:00
|
|
|
func (d *D) ValidateUBEBytes(bss ...[]byte) scalar.Mapper {
|
|
|
|
return scalar.Fn(func(s scalar.S) (scalar.S, error) {
|
|
|
|
return assertUBytes(s, false, BigEndian, bss...)
|
|
|
|
})
|
2021-11-05 17:04:26 +03:00
|
|
|
}
|