2021-12-02 00:48:25 +03:00
|
|
|
package decode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-03-10 03:14:53 +03:00
|
|
|
"encoding/binary"
|
2021-12-02 00:48:25 +03:00
|
|
|
"fmt"
|
|
|
|
"math"
|
2022-01-14 03:58:42 +03:00
|
|
|
"math/big"
|
2021-12-02 00:48:25 +03:00
|
|
|
|
2022-08-12 16:27:51 +03:00
|
|
|
"github.com/wader/fq/internal/mathex"
|
2021-12-02 00:48:25 +03:00
|
|
|
"github.com/wader/fq/pkg/bitio"
|
|
|
|
"golang.org/x/text/encoding"
|
|
|
|
"golang.org/x/text/encoding/unicode"
|
|
|
|
)
|
|
|
|
|
2022-01-24 23:21:48 +03:00
|
|
|
func (d *D) tryBitBuf(nBits int64) (bitio.ReaderAtSeeker, error) {
|
|
|
|
return d.TryBitBufLen(nBits)
|
2022-01-14 03:58:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *D) tryUEndian(nBits int, endian Endian) (uint64, error) {
|
2022-01-18 14:00:28 +03:00
|
|
|
if nBits < 0 {
|
|
|
|
return 0, fmt.Errorf("tryUEndian nBits must be >= 0 (%d)", nBits)
|
2022-01-14 03:58:42 +03:00
|
|
|
}
|
2023-03-10 03:14:53 +03:00
|
|
|
n, err := d.TryUintBits(nBits)
|
2021-12-02 00:48:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if endian == LittleEndian {
|
2022-01-24 23:21:48 +03:00
|
|
|
n = bitio.ReverseBytes64(nBits, n)
|
2021-12-02 00:48:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2022-01-14 03:58:42 +03:00
|
|
|
func (d *D) trySEndian(nBits int, endian Endian) (int64, error) {
|
2022-10-11 13:10:52 +03:00
|
|
|
if nBits < 1 {
|
|
|
|
return 0, fmt.Errorf("trySEndian nBits must be >= 1 (%d)", nBits)
|
2022-01-18 14:00:28 +03:00
|
|
|
}
|
2022-01-14 03:58:42 +03:00
|
|
|
n, err := d.tryUEndian(nBits, endian)
|
2021-12-02 00:48:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
var s int64
|
|
|
|
if n&(1<<(nBits-1)) > 0 {
|
|
|
|
// two's complement
|
|
|
|
s = -int64((^n & ((1 << nBits) - 1)) + 1)
|
|
|
|
} else {
|
|
|
|
s = int64(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2022-01-14 03:58:42 +03:00
|
|
|
// from https://github.com/golang/go/wiki/SliceTricks#reversing
|
2022-04-17 11:33:01 +03:00
|
|
|
func ReverseBytes(a []byte) {
|
2022-01-14 03:58:42 +03:00
|
|
|
for i := len(a)/2 - 1; i >= 0; i-- {
|
|
|
|
opp := len(a) - 1 - i
|
|
|
|
a[i], a[opp] = a[opp], a[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *D) tryBigIntEndianSign(nBits int, endian Endian, sign bool) (*big.Int, error) {
|
|
|
|
if nBits < 0 {
|
2022-01-18 14:00:28 +03:00
|
|
|
return nil, fmt.Errorf("tryBigIntEndianSign nBits must be >= 0 (%d)", nBits)
|
2022-01-14 03:58:42 +03:00
|
|
|
}
|
|
|
|
b := int(bitio.BitsByteCount(int64(nBits)))
|
|
|
|
buf := d.SharedReadBuf(b)[0:b]
|
2022-01-24 23:21:48 +03:00
|
|
|
_, err := bitio.ReadFull(d.bitBuf, buf, int64(nBits))
|
2022-01-14 03:58:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if endian == LittleEndian {
|
2022-04-17 11:33:01 +03:00
|
|
|
ReverseBytes(buf)
|
2022-01-14 03:58:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
n := new(big.Int)
|
|
|
|
if sign {
|
2022-08-12 16:27:51 +03:00
|
|
|
mathex.BigIntSetBytesSigned(n, buf)
|
2022-01-14 03:58:42 +03:00
|
|
|
} else {
|
|
|
|
n.SetBytes(buf)
|
|
|
|
}
|
|
|
|
n.Rsh(n, uint((8-nBits%8)%8))
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *D) tryFEndian(nBits int, endian Endian) (float64, error) {
|
2022-01-18 14:00:28 +03:00
|
|
|
if nBits < 0 {
|
|
|
|
return 0, fmt.Errorf("tryFEndian nBits must be >= 0 (%d)", nBits)
|
|
|
|
}
|
2023-03-10 03:14:53 +03:00
|
|
|
b, err := d.TryBits(nBits)
|
2021-12-02 00:48:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if endian == LittleEndian {
|
2023-03-10 03:14:53 +03:00
|
|
|
ReverseBytes(b)
|
2021-12-02 00:48:25 +03:00
|
|
|
}
|
|
|
|
switch nBits {
|
2022-01-11 23:24:54 +03:00
|
|
|
case 16:
|
2023-03-10 03:14:53 +03:00
|
|
|
return float64(mathex.Float16(binary.BigEndian.Uint16(b)).Float32()), nil
|
2021-12-02 00:48:25 +03:00
|
|
|
case 32:
|
2023-03-10 03:14:53 +03:00
|
|
|
return float64(math.Float32frombits(binary.BigEndian.Uint32(b))), nil
|
2021-12-02 00:48:25 +03:00
|
|
|
case 64:
|
2023-03-10 03:14:53 +03:00
|
|
|
return math.Float64frombits(binary.BigEndian.Uint64(b)), nil
|
|
|
|
case 80:
|
|
|
|
return mathex.NewFloat80FromBytes(b).Float64(), nil
|
2021-12-02 00:48:25 +03:00
|
|
|
default:
|
|
|
|
return 0, fmt.Errorf("unsupported float size %d", nBits)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 03:58:42 +03:00
|
|
|
func (d *D) tryFPEndian(nBits int, fBits int, endian Endian) (float64, error) {
|
2022-01-18 14:00:28 +03:00
|
|
|
if nBits < 0 {
|
|
|
|
return 0, fmt.Errorf("tryFPEndian nBits must be >= 0 (%d)", nBits)
|
|
|
|
}
|
2023-03-10 03:14:53 +03:00
|
|
|
n, err := d.TryUintBits(nBits)
|
2021-12-02 00:48:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if endian == LittleEndian {
|
2022-01-24 23:21:48 +03:00
|
|
|
n = bitio.ReverseBytes64(nBits, n)
|
2021-12-02 00:48:25 +03:00
|
|
|
}
|
|
|
|
return float64(n) / float64(uint64(1<<fBits)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var UTF8BOM = unicode.UTF8BOM
|
|
|
|
var UTF16BOM = unicode.UTF16(unicode.LittleEndian, unicode.UseBOM)
|
|
|
|
var UTF16BE = unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM)
|
|
|
|
var UTF16LE = unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)
|
|
|
|
|
|
|
|
func (d *D) tryText(nBytes int, e encoding.Encoding) (string, error) {
|
2022-01-24 18:56:24 +03:00
|
|
|
if nBytes < 0 {
|
|
|
|
return "", fmt.Errorf("tryText nBytes must be >= 0 (%d)", nBytes)
|
|
|
|
}
|
|
|
|
bytesLeft := d.BitsLeft() / 8
|
|
|
|
if int64(nBytes) > bytesLeft {
|
|
|
|
return "", fmt.Errorf("tryText nBytes %d outside buffer, %d bytes left", nBytes, bytesLeft)
|
|
|
|
}
|
|
|
|
|
2022-01-24 23:21:48 +03:00
|
|
|
bs, err := d.TryBytesLen(nBytes)
|
2021-12-02 00:48:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return e.NewDecoder().String(string(bs))
|
|
|
|
}
|
|
|
|
|
|
|
|
// read length prefixed text (ex pascal short string)
|
|
|
|
// lBits length prefix
|
|
|
|
// fixedBytes if != -1 read nBytes but trim to length
|
2022-08-05 01:20:14 +03:00
|
|
|
//
|
|
|
|
//nolint:unparam
|
2021-12-02 00:48:25 +03:00
|
|
|
func (d *D) tryTextLenPrefixed(lenBits int, fixedBytes int, e encoding.Encoding) (string, error) {
|
2022-01-24 18:56:24 +03:00
|
|
|
if lenBits < 0 {
|
|
|
|
return "", fmt.Errorf("tryTextLenPrefixed lenBits must be >= 0 (%d)", lenBits)
|
|
|
|
}
|
|
|
|
if fixedBytes < 0 {
|
|
|
|
return "", fmt.Errorf("tryTextLenPrefixed fixedBytes must be >= 0 (%d)", fixedBytes)
|
|
|
|
}
|
|
|
|
bytesLeft := d.BitsLeft() / 8
|
|
|
|
if int64(fixedBytes) > bytesLeft {
|
|
|
|
return "", fmt.Errorf("tryTextLenPrefixed fixedBytes %d outside, %d bytes left", fixedBytes, bytesLeft)
|
|
|
|
}
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
p := d.Pos()
|
2023-03-10 03:14:53 +03:00
|
|
|
l, err := d.TryUintBits(lenBits)
|
2021-12-02 00:48:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
n := int(l)
|
|
|
|
if fixedBytes != -1 {
|
|
|
|
n = fixedBytes - 1
|
|
|
|
// TODO: error?
|
|
|
|
if l > uint64(n) {
|
|
|
|
l = uint64(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 23:21:48 +03:00
|
|
|
bs, err := d.TryBytesLen(n)
|
2021-12-02 00:48:25 +03:00
|
|
|
if err != nil {
|
|
|
|
d.SeekAbs(p)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return e.NewDecoder().String(string(bs[0:l]))
|
|
|
|
}
|
|
|
|
|
2022-08-01 19:42:24 +03:00
|
|
|
func (d *D) tryTextNull(charBytes int, e encoding.Encoding) (string, error) {
|
|
|
|
if charBytes < 1 {
|
|
|
|
return "", fmt.Errorf("tryTextNull charBytes must be >= 1 (%d)", charBytes)
|
2022-01-24 18:56:24 +03:00
|
|
|
}
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
p := d.Pos()
|
2022-08-01 19:42:24 +03:00
|
|
|
peekBits, _, err := d.TryPeekFind(charBytes*8, int64(charBytes)*8, -1, func(v uint64) bool { return v == 0 })
|
2021-12-02 00:48:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-08-01 19:42:24 +03:00
|
|
|
n := (int(peekBits) / 8) + charBytes
|
2022-01-24 23:21:48 +03:00
|
|
|
bs, err := d.TryBytesLen(n)
|
2021-12-02 00:48:25 +03:00
|
|
|
if err != nil {
|
|
|
|
d.SeekAbs(p)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-08-01 19:42:24 +03:00
|
|
|
return e.NewDecoder().String(string(bs[0 : n-charBytes]))
|
2021-12-02 00:48:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *D) tryTextNullLen(fixedBytes int, e encoding.Encoding) (string, error) {
|
2022-01-24 18:56:24 +03:00
|
|
|
if fixedBytes < 0 {
|
|
|
|
return "", fmt.Errorf("tryTextNullLen fixedBytes must be >= 0 (%d)", fixedBytes)
|
|
|
|
}
|
|
|
|
bytesLeft := d.BitsLeft() / 8
|
|
|
|
if int64(fixedBytes) > bytesLeft {
|
|
|
|
return "", fmt.Errorf("tryTextNullLen fixedBytes %d outside, %d bytes left", fixedBytes, bytesLeft)
|
|
|
|
}
|
|
|
|
|
2022-01-24 23:21:48 +03:00
|
|
|
bs, err := d.TryBytesLen(fixedBytes)
|
2021-12-02 00:48:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
nullIndex := bytes.IndexByte(bs, 0)
|
|
|
|
if nullIndex != -1 {
|
|
|
|
bs = bs[:nullIndex]
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.NewDecoder().String(string(bs))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ov is what to treat as 1
|
|
|
|
func (d *D) tryUnary(ov uint64) (uint64, error) {
|
|
|
|
p := d.Pos()
|
|
|
|
var n uint64
|
|
|
|
for {
|
2023-03-10 03:14:53 +03:00
|
|
|
b, err := d.TryUintBits(1)
|
2021-12-02 00:48:25 +03:00
|
|
|
if err != nil {
|
|
|
|
d.SeekAbs(p)
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if b != ov {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *D) tryBool() (bool, error) {
|
2023-03-10 03:14:53 +03:00
|
|
|
n, err := d.TryUintBits(1)
|
2021-12-02 00:48:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return n == 1, nil
|
|
|
|
}
|
2022-08-20 13:05:07 +03:00
|
|
|
|
|
|
|
// Unsigned LEB128, description from wasm spec
|
|
|
|
//
|
|
|
|
// uN ::= n:byte => n (if n < 2^7 && n < 2^N)
|
|
|
|
// n:byte m:u(N-7) => 2^7 * m + (n - 2^7) (if n >= 2^7 && N > 7)
|
|
|
|
func (d *D) tryULEB128() (uint64, error) {
|
|
|
|
var result uint64
|
|
|
|
var shift uint
|
|
|
|
|
|
|
|
for {
|
|
|
|
b := d.U8()
|
|
|
|
if shift >= 63 && b != 0 {
|
|
|
|
return 0, fmt.Errorf("overflow when reading unsigned leb128, shift %d >= 63", shift)
|
|
|
|
}
|
|
|
|
result |= (b & 0x7f) << shift
|
|
|
|
if b&0x80 == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
shift += 7
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signed LEB128, description from wasm spec
|
|
|
|
//
|
|
|
|
// sN ::= n:byte => n (if n < 2^6 && n < 2^(N-1))
|
|
|
|
// n:byte => n - 2^7 (if 2^6 <= n < 2^7 && n >= 2^7 - 2^(N-1))
|
|
|
|
// n:byte m:s(N-7) => 2^7 * m + (n - 2^7) (if n >= 2^7 && N > 7)
|
|
|
|
func (d *D) trySLEB128() (int64, error) {
|
|
|
|
const n = 64
|
|
|
|
var result int64
|
|
|
|
var shift uint
|
|
|
|
var b byte
|
|
|
|
|
|
|
|
for {
|
|
|
|
b = byte(d.U8())
|
|
|
|
if shift == 63 && b != 0 && b != 0x7f {
|
|
|
|
return 0, fmt.Errorf("overflow when reading signed leb128, shift %d >= 63", shift)
|
|
|
|
}
|
|
|
|
|
|
|
|
result |= int64(b&0x7f) << shift
|
|
|
|
shift += 7
|
|
|
|
|
|
|
|
if b&0x80 == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if shift < n && (b&0x40) == 0x40 {
|
|
|
|
result |= -1 << shift
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|