2021-12-02 00:48:25 +03:00
|
|
|
package scalar
|
|
|
|
|
|
|
|
// TODO: d.Pos check err
|
|
|
|
// TODO: fn for actual?
|
|
|
|
// TODO: dsl import .? own scalar package?
|
|
|
|
// TODO: better IOError op names
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2022-01-28 19:18:30 +03:00
|
|
|
"strconv"
|
2021-12-02 00:48:25 +03:00
|
|
|
"strings"
|
2022-05-07 14:13:37 +03:00
|
|
|
"time"
|
2021-12-02 00:48:25 +03:00
|
|
|
|
2022-08-12 16:27:51 +03:00
|
|
|
"github.com/wader/fq/internal/bitioex"
|
2021-12-02 00:48:25 +03:00
|
|
|
"github.com/wader/fq/pkg/bitio"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:generate sh -c "cat scalar_gen.go.tmpl | go run ../../dev/tmpl.go ../decode/types.json | gofmt > scalar_gen.go"
|
|
|
|
|
|
|
|
type DisplayFormat int
|
|
|
|
|
|
|
|
const (
|
|
|
|
NumberDecimal DisplayFormat = iota
|
|
|
|
NumberBinary
|
|
|
|
NumberOctal
|
|
|
|
NumberHex
|
|
|
|
)
|
|
|
|
|
|
|
|
func (df DisplayFormat) FormatBase() int {
|
|
|
|
switch df {
|
|
|
|
case NumberDecimal:
|
|
|
|
return 10
|
|
|
|
case NumberBinary:
|
|
|
|
return 2
|
|
|
|
case NumberOctal:
|
|
|
|
return 8
|
|
|
|
case NumberHex:
|
|
|
|
return 16
|
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type S struct {
|
2022-05-20 16:10:41 +03:00
|
|
|
Actual any // nil, int, int64, uint64, float64, string, bool, []byte, *bit.Int, bitio.BitReaderAtSeeker,
|
2021-12-06 00:03:39 +03:00
|
|
|
ActualDisplay DisplayFormat
|
2022-05-20 16:10:41 +03:00
|
|
|
Sym any
|
2021-12-06 00:03:39 +03:00
|
|
|
SymDisplay DisplayFormat
|
2021-12-02 00:48:25 +03:00
|
|
|
Description string
|
|
|
|
Unknown bool
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func (s S) Value() any {
|
2021-12-02 00:48:25 +03:00
|
|
|
if s.Sym != nil {
|
|
|
|
return s.Sym
|
|
|
|
}
|
|
|
|
return s.Actual
|
|
|
|
}
|
|
|
|
|
|
|
|
type Mapper interface {
|
|
|
|
MapScalar(S) (S, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Fn func(S) (S, error)
|
|
|
|
|
|
|
|
func (fn Fn) MapScalar(s S) (S, error) {
|
|
|
|
return fn(s)
|
|
|
|
}
|
|
|
|
|
2022-05-07 13:46:34 +03:00
|
|
|
var ActualBin = Fn(func(s S) (S, error) { s.ActualDisplay = NumberBinary; return s, nil })
|
|
|
|
var ActualOct = Fn(func(s S) (S, error) { s.ActualDisplay = NumberOctal; return s, nil })
|
|
|
|
var ActualDec = Fn(func(s S) (S, error) { s.ActualDisplay = NumberDecimal; return s, nil })
|
|
|
|
var ActualHex = Fn(func(s S) (S, error) { s.ActualDisplay = NumberHex; return s, nil })
|
2021-12-06 00:03:39 +03:00
|
|
|
|
|
|
|
var SymBin = Fn(func(s S) (S, error) { s.SymDisplay = NumberBinary; return s, nil })
|
|
|
|
var SymOct = Fn(func(s S) (S, error) { s.SymDisplay = NumberOctal; return s, nil })
|
|
|
|
var SymDec = Fn(func(s S) (S, error) { s.SymDisplay = NumberDecimal; return s, nil })
|
|
|
|
var SymHex = Fn(func(s S) (S, error) { s.SymDisplay = NumberHex; return s, nil })
|
2021-12-02 00:48:25 +03:00
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func Actual(v any) Mapper {
|
2021-12-02 00:48:25 +03:00
|
|
|
return Fn(func(s S) (S, error) { s.Actual = v; return s, nil })
|
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
func Sym(v any) Mapper {
|
2021-12-02 00:48:25 +03:00
|
|
|
return Fn(func(s S) (S, error) { s.Sym = v; return s, nil })
|
|
|
|
}
|
|
|
|
func Description(v string) Mapper {
|
|
|
|
return Fn(func(s S) (S, error) { s.Description = v; return s, nil })
|
|
|
|
}
|
|
|
|
|
2022-05-07 13:46:34 +03:00
|
|
|
func ActualUAdd(n int) ActualUFn {
|
|
|
|
// TODO: use math.Add/Sub?
|
|
|
|
return ActualUFn(func(a uint64) uint64 { return uint64(int64(a) + int64(n)) })
|
2021-12-02 00:48:25 +03:00
|
|
|
}
|
|
|
|
|
2022-05-07 13:46:34 +03:00
|
|
|
func ActualSAdd(n int) ActualSFn {
|
|
|
|
return ActualSFn(func(a int64) int64 { return a + int64(n) })
|
2021-12-02 00:48:25 +03:00
|
|
|
}
|
|
|
|
|
2022-05-07 13:46:34 +03:00
|
|
|
func ActualTrim(cutset string) ActualStrFn {
|
|
|
|
return ActualStrFn(func(a string) string { return strings.Trim(a, cutset) })
|
2021-12-02 00:48:25 +03:00
|
|
|
}
|
|
|
|
|
2022-05-07 13:46:34 +03:00
|
|
|
var ActualTrimSpace = ActualStrFn(strings.TrimSpace)
|
2021-12-02 00:48:25 +03:00
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func strMapToSym(fn func(s string) (any, error)) Mapper {
|
2022-01-28 19:18:30 +03:00
|
|
|
return Fn(func(s S) (S, error) {
|
|
|
|
ts := strings.TrimSpace(s.ActualStr())
|
2022-08-04 13:21:36 +03:00
|
|
|
n, err := fn(ts)
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
2022-01-28 19:18:30 +03:00
|
|
|
}
|
2022-08-04 13:21:36 +03:00
|
|
|
s.Sym = n
|
2022-01-28 19:18:30 +03:00
|
|
|
return s, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-07 13:46:34 +03:00
|
|
|
func SymUParseUint(base int) Mapper {
|
2022-05-20 16:10:41 +03:00
|
|
|
return strMapToSym(func(s string) (any, error) { return strconv.ParseUint(s, base, 64) })
|
2022-01-28 19:18:30 +03:00
|
|
|
}
|
|
|
|
|
2022-05-07 13:46:34 +03:00
|
|
|
func SymSParseInt(base int) Mapper {
|
2022-05-20 16:10:41 +03:00
|
|
|
return strMapToSym(func(s string) (any, error) { return strconv.ParseInt(s, base, 64) })
|
2022-01-28 19:18:30 +03:00
|
|
|
}
|
|
|
|
|
2022-05-07 13:46:34 +03:00
|
|
|
func SymFParseFloat(base int) Mapper {
|
2022-05-20 16:10:41 +03:00
|
|
|
return strMapToSym(func(s string) (any, error) { return strconv.ParseFloat(s, base) })
|
2022-01-28 19:18:30 +03:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:33:17 +03:00
|
|
|
type URangeEntry struct {
|
|
|
|
Range [2]uint64
|
|
|
|
S S
|
|
|
|
}
|
|
|
|
|
|
|
|
// URangeToScalar maps uint64 ranges to a scalar, first in range is chosen
|
|
|
|
type URangeToScalar []URangeEntry
|
2021-12-02 00:48:25 +03:00
|
|
|
|
2021-12-06 21:33:17 +03:00
|
|
|
func (rs URangeToScalar) MapScalar(s S) (S, error) {
|
2022-08-08 19:09:49 +03:00
|
|
|
n := s.ActualU()
|
2021-12-06 21:33:17 +03:00
|
|
|
for _, re := range rs {
|
|
|
|
if n >= re.Range[0] && n <= re.Range[1] {
|
|
|
|
ns := re.S
|
2021-12-02 00:48:25 +03:00
|
|
|
ns.Actual = s.Actual
|
|
|
|
s = ns
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:33:17 +03:00
|
|
|
// SRangeToScalar maps ranges to a scalar, first in range is chosen
|
|
|
|
type SRangeEntry struct {
|
|
|
|
Range [2]int64
|
|
|
|
S S
|
|
|
|
}
|
|
|
|
|
|
|
|
// SRangeToScalar maps sint64 ranges to a scalar, first in range is chosen
|
|
|
|
type SRangeToScalar []SRangeEntry
|
2021-12-02 00:48:25 +03:00
|
|
|
|
2021-12-06 21:33:17 +03:00
|
|
|
func (rs SRangeToScalar) MapScalar(s S) (S, error) {
|
2022-08-08 19:09:49 +03:00
|
|
|
n := s.ActualS()
|
2021-12-06 21:33:17 +03:00
|
|
|
for _, re := range rs {
|
|
|
|
if n >= re.Range[0] && n <= re.Range[1] {
|
|
|
|
ns := re.S
|
2021-12-02 00:48:25 +03:00
|
|
|
ns.Actual = s.Actual
|
|
|
|
s = ns
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2022-04-17 11:33:01 +03:00
|
|
|
func RawSym(s S, nBytes int, fn func(b []byte) string) (S, error) {
|
2022-08-08 19:09:49 +03:00
|
|
|
br := s.ActualBitBuf()
|
2022-08-12 16:27:51 +03:00
|
|
|
brLen, err := bitioex.Len(br)
|
2022-01-24 23:21:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return S{}, err
|
|
|
|
}
|
|
|
|
if nBytes < 0 {
|
|
|
|
nBytes = int(brLen) / 8
|
|
|
|
if brLen%8 != 0 {
|
|
|
|
nBytes++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if brLen < int64(nBytes)*8 {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
// TODO: shared somehow?
|
|
|
|
b := make([]byte, nBytes)
|
|
|
|
if _, err := br.ReadBitsAt(b, int64(nBytes)*8, 0); err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Sym = fn(b[0:nBytes])
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var RawUUID = Fn(func(s S) (S, error) {
|
2022-04-17 11:33:01 +03:00
|
|
|
return RawSym(s, -1, func(b []byte) string {
|
2022-01-24 23:21:48 +03:00
|
|
|
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
var RawHex = Fn(func(s S) (S, error) {
|
2022-04-17 11:33:01 +03:00
|
|
|
return RawSym(s, -1, func(b []byte) string { return fmt.Sprintf("%x", b) })
|
2022-01-24 23:21:48 +03:00
|
|
|
})
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
type BytesToScalar []struct {
|
|
|
|
Bytes []byte
|
|
|
|
Scalar S
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m BytesToScalar) MapScalar(s S) (S, error) {
|
2022-05-28 14:04:04 +03:00
|
|
|
rc, err := bitio.CloneReader(s.ActualBitBuf())
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
2022-01-24 23:21:48 +03:00
|
|
|
bb := &bytes.Buffer{}
|
2022-08-12 16:27:51 +03:00
|
|
|
if _, err := bitioex.CopyBits(bb, rc); err != nil {
|
2021-12-02 00:48:25 +03:00
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
for _, bs := range m {
|
2022-01-24 23:21:48 +03:00
|
|
|
if bytes.Equal(bb.Bytes(), bs.Bytes) {
|
2021-12-02 00:48:25 +03:00
|
|
|
ns := bs.Scalar
|
|
|
|
ns.Actual = s.Actual
|
|
|
|
s = ns
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
2022-05-07 14:13:37 +03:00
|
|
|
|
|
|
|
// TODO: nicer api, use generics
|
|
|
|
|
|
|
|
var unixTimeEpochDate = time.Date(1970, time.January, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
func DescriptionActualUTime(epoch time.Time, format string) Mapper {
|
|
|
|
return Fn(func(s S) (S, error) {
|
|
|
|
s.Description = epoch.Add(time.Second * time.Duration(s.ActualU())).Format(format)
|
|
|
|
return s, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func DescriptionSymUTime(epoch time.Time, format string) Mapper {
|
|
|
|
return Fn(func(s S) (S, error) {
|
|
|
|
s.Description = epoch.Add(time.Second * time.Duration(s.SymU())).Format(format)
|
|
|
|
return s, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var DescriptionActualUUnixTime = DescriptionActualUTime(unixTimeEpochDate, time.RFC3339)
|
|
|
|
var DescriptionSymUUnixTime = DescriptionSymUTime(unixTimeEpochDate, time.RFC3339)
|
|
|
|
|
|
|
|
func DescriptionActualSTime(epoch time.Time, format string) Mapper {
|
|
|
|
return Fn(func(s S) (S, error) {
|
|
|
|
s.Description = epoch.Add(time.Second * time.Duration(s.ActualS())).Format(format)
|
|
|
|
return s, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func DescriptionSymSTime(epoch time.Time, format string) Mapper {
|
|
|
|
return Fn(func(s S) (S, error) {
|
|
|
|
s.Description = epoch.Add(time.Second * time.Duration(s.SymS())).Format(format)
|
|
|
|
return s, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var DescriptionActualSUnixTime = DescriptionActualSTime(unixTimeEpochDate, time.RFC3339)
|
|
|
|
var DescriptionSymSUnixTime = DescriptionSymSTime(unixTimeEpochDate, time.RFC3339)
|