2020-06-08 03:29:51 +03:00
|
|
|
package interp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-02-21 00:25:46 +03:00
|
|
|
"errors"
|
2020-06-08 03:29:51 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-11-16 13:41:14 +03:00
|
|
|
"io/fs"
|
2020-06-08 03:29:51 +03:00
|
|
|
"math/big"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
2021-11-16 13:41:14 +03:00
|
|
|
"github.com/wader/fq/internal/aheadreadseeker"
|
2022-08-12 16:27:51 +03:00
|
|
|
"github.com/wader/fq/internal/bitioex"
|
2021-11-16 13:41:14 +03:00
|
|
|
"github.com/wader/fq/internal/ctxreadseeker"
|
2022-08-12 16:27:51 +03:00
|
|
|
"github.com/wader/fq/internal/gojqex"
|
|
|
|
"github.com/wader/fq/internal/ioex"
|
2021-11-16 13:41:14 +03:00
|
|
|
"github.com/wader/fq/internal/progressreadseeker"
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/pkg/bitio"
|
|
|
|
"github.com/wader/fq/pkg/ranges"
|
2022-07-26 16:50:18 +03:00
|
|
|
"github.com/wader/gojq"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2021-11-16 13:41:14 +03:00
|
|
|
func init() {
|
2022-07-16 19:39:57 +03:00
|
|
|
RegisterFunc1("_tobits", (*Interp)._toBits)
|
|
|
|
RegisterFunc0("open", (*Interp)._open)
|
2021-11-16 13:41:14 +03:00
|
|
|
}
|
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
type ToBinary interface {
|
|
|
|
ToBinary() (Binary, error)
|
2021-11-30 22:54:44 +03:00
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func toBinary(v any) (Binary, error) {
|
2022-02-08 20:44:48 +03:00
|
|
|
switch vv := v.(type) {
|
|
|
|
case ToBinary:
|
|
|
|
return vv.ToBinary()
|
|
|
|
default:
|
2022-07-16 19:39:57 +03:00
|
|
|
br, err := ToBitReader(v)
|
2022-02-08 20:44:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return Binary{}, err
|
|
|
|
}
|
2022-07-16 19:39:57 +03:00
|
|
|
return NewBinaryFromBitReader(br, 8, 0)
|
2022-02-08 20:44:48 +03:00
|
|
|
}
|
2021-11-30 22:54:44 +03:00
|
|
|
}
|
|
|
|
|
2022-07-16 19:39:57 +03:00
|
|
|
func ToBitReader(v any) (bitio.ReaderAtSeeker, error) {
|
2022-02-08 20:44:48 +03:00
|
|
|
return toBitReaderEx(v, false)
|
|
|
|
}
|
|
|
|
|
2023-06-14 18:46:50 +03:00
|
|
|
type byteRangeError int
|
|
|
|
|
|
|
|
func (b byteRangeError) Error() string {
|
|
|
|
return fmt.Sprintf("byte in binary list must be bytes (0-255) got %d", int(b))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func toBitReaderEx(v any, inArray bool) (bitio.ReaderAtSeeker, error) {
|
2021-11-30 22:54:44 +03:00
|
|
|
switch vv := v.(type) {
|
2022-02-08 20:44:48 +03:00
|
|
|
case ToBinary:
|
|
|
|
bv, err := vv.ToBinary()
|
2021-11-30 22:54:44 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-12 16:27:51 +03:00
|
|
|
return bitioex.Range(bv.br, bv.r.Start, bv.r.Len)
|
2021-11-30 22:54:44 +03:00
|
|
|
case string:
|
2022-01-24 23:21:48 +03:00
|
|
|
return bitio.NewBitReader([]byte(vv), -1), nil
|
2021-11-30 22:54:44 +03:00
|
|
|
case int, float64, *big.Int:
|
|
|
|
bi, err := toBigInt(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if inArray {
|
|
|
|
if bi.Cmp(big.NewInt(255)) > 0 || bi.Cmp(big.NewInt(0)) < 0 {
|
2023-06-14 18:46:50 +03:00
|
|
|
return nil, byteRangeError(bi.Int64())
|
2021-11-30 22:54:44 +03:00
|
|
|
}
|
|
|
|
n := bi.Uint64()
|
|
|
|
b := [1]byte{byte(n)}
|
2022-01-24 23:21:48 +03:00
|
|
|
return bitio.NewBitReader(b[:], -1), nil
|
2021-11-30 22:54:44 +03:00
|
|
|
}
|
|
|
|
|
2022-01-24 23:21:48 +03:00
|
|
|
bitLen := int64(bi.BitLen())
|
|
|
|
// bit.Int "The bit length of 0 is 0."
|
|
|
|
if bitLen == 0 {
|
|
|
|
var z [1]byte
|
|
|
|
return bitio.NewBitReader(z[:], 1), nil
|
|
|
|
}
|
2021-11-30 22:54:44 +03:00
|
|
|
// TODO: how should this work? "0xf | tobytes" 4bits or 8bits? now 4
|
2022-01-24 23:21:48 +03:00
|
|
|
padBefore := (8 - (bitLen % 8)) % 8
|
|
|
|
// padBefore := 0
|
2022-08-12 16:27:51 +03:00
|
|
|
br, err := bitioex.Range(bitio.NewBitReader(bi.Bytes(), -1), padBefore, bitLen)
|
2021-11-30 22:54:44 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-24 23:21:48 +03:00
|
|
|
return br, nil
|
2022-05-20 16:10:41 +03:00
|
|
|
case []any:
|
2022-01-24 23:21:48 +03:00
|
|
|
rr := make([]bitio.ReadAtSeeker, 0, len(vv))
|
2023-06-14 18:46:50 +03:00
|
|
|
|
|
|
|
// fast path for slice containing only 0-255 numbers and strings
|
|
|
|
bs := &bytes.Buffer{}
|
|
|
|
for _, e := range vv {
|
|
|
|
if bs == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch ev := e.(type) {
|
|
|
|
case int:
|
|
|
|
if ev >= 0 && ev <= 255 {
|
|
|
|
bs.WriteByte(byte(ev))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
b := int(ev)
|
|
|
|
if b >= 0 && b <= 255 {
|
|
|
|
bs.WriteByte(byte(ev))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case *big.Int:
|
|
|
|
if ev.Cmp(big.NewInt(0)) >= 0 && ev.Cmp(big.NewInt(255)) <= 0 {
|
|
|
|
bs.WriteByte(byte(ev.Uint64()))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case string:
|
|
|
|
// TODO: maybe only if less then some length?
|
|
|
|
bs.WriteString(ev)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
bs = nil
|
|
|
|
}
|
|
|
|
if bs != nil {
|
|
|
|
return bitio.NewBitReader(bs.Bytes(), -1), nil
|
|
|
|
}
|
|
|
|
|
2021-11-30 22:54:44 +03:00
|
|
|
// TODO: optimize byte array case, flatten into one slice
|
|
|
|
for _, e := range vv {
|
2022-02-08 20:44:48 +03:00
|
|
|
eBR, eErr := toBitReaderEx(e, true)
|
2021-11-30 22:54:44 +03:00
|
|
|
if eErr != nil {
|
|
|
|
return nil, eErr
|
|
|
|
}
|
2022-01-24 23:21:48 +03:00
|
|
|
rr = append(rr, eBR)
|
2021-11-30 22:54:44 +03:00
|
|
|
}
|
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
mb, err := bitio.NewMultiReader(rr...)
|
2021-11-30 22:54:44 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-24 23:21:48 +03:00
|
|
|
return mb, nil
|
2021-11-30 22:54:44 +03:00
|
|
|
default:
|
2022-02-08 20:44:48 +03:00
|
|
|
return nil, fmt.Errorf("value can't be a binary")
|
2021-11-30 22:54:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-16 19:39:57 +03:00
|
|
|
type toBitsOpts struct {
|
|
|
|
Unit int
|
|
|
|
KeepRange bool
|
|
|
|
PadToUnits int
|
|
|
|
}
|
2021-11-16 13:41:14 +03:00
|
|
|
|
2022-07-16 19:39:57 +03:00
|
|
|
// note is used to implement tobytes* also
|
|
|
|
func (i *Interp) _toBits(c any, opts toBitsOpts) any {
|
2021-11-16 13:41:14 +03:00
|
|
|
// TODO: unit > 8?
|
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
bv, err := toBinary(c)
|
2021-11-16 13:41:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-08 20:44:48 +03:00
|
|
|
|
2022-07-16 19:39:57 +03:00
|
|
|
pad := int64(opts.Unit * opts.PadToUnits)
|
2022-02-08 20:44:48 +03:00
|
|
|
if pad == 0 {
|
2022-07-16 19:39:57 +03:00
|
|
|
pad = int64(opts.Unit)
|
2022-02-08 20:44:48 +03:00
|
|
|
}
|
|
|
|
|
2022-07-16 19:39:57 +03:00
|
|
|
bv.unit = opts.Unit
|
2022-02-08 20:44:48 +03:00
|
|
|
bv.pad = (pad - bv.r.Len%pad) % pad
|
2021-11-16 13:41:14 +03:00
|
|
|
|
2022-07-16 19:39:57 +03:00
|
|
|
if opts.KeepRange {
|
2022-02-08 20:44:48 +03:00
|
|
|
return bv
|
2021-11-16 13:41:14 +03:00
|
|
|
}
|
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
br, err := bv.toReader()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-16 19:39:57 +03:00
|
|
|
bb, err := NewBinaryFromBitReader(br, bv.unit, 0)
|
2022-02-08 20:44:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return bb
|
2021-11-16 13:41:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type openFile struct {
|
2022-02-08 20:44:48 +03:00
|
|
|
Binary
|
2021-11-16 13:41:14 +03:00
|
|
|
filename string
|
|
|
|
progressFn progressreadseeker.ProgressFn
|
|
|
|
}
|
|
|
|
|
2021-12-05 03:15:59 +03:00
|
|
|
var _ Value = (*openFile)(nil)
|
2022-02-08 20:44:48 +03:00
|
|
|
var _ ToBinary = (*openFile)(nil)
|
2021-11-16 13:41:14 +03:00
|
|
|
|
2023-05-15 18:23:59 +03:00
|
|
|
func (of *openFile) Display(w io.Writer, opts *Options) error {
|
2021-12-05 03:15:59 +03:00
|
|
|
_, err := fmt.Fprintf(w, "<openfile %q>\n", of.filename)
|
2021-11-16 13:41:14 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
func (of *openFile) ToBinary() (Binary, error) {
|
2022-07-16 19:39:57 +03:00
|
|
|
return NewBinaryFromBitReader(of.br, 8, 0)
|
2021-11-16 13:41:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// opens a file for reading from filesystem
|
2022-01-24 23:21:48 +03:00
|
|
|
// TODO: when to close? when br loses all refs? need to use finalizer somehow?
|
2022-07-16 19:39:57 +03:00
|
|
|
func (i *Interp) _open(c any) any {
|
|
|
|
if i.EvalInstance.IsCompleting {
|
|
|
|
// TODO: have dummy values for each type for completion?
|
|
|
|
br, _ := NewBinaryFromBitReader(bitio.NewBitReader([]byte{}, -1), 8, 0)
|
|
|
|
return br
|
2022-02-10 20:46:23 +03:00
|
|
|
}
|
|
|
|
|
2021-11-16 13:41:14 +03:00
|
|
|
var err error
|
2021-11-30 14:23:20 +03:00
|
|
|
var f fs.File
|
2021-11-16 13:41:14 +03:00
|
|
|
var path string
|
|
|
|
|
2021-11-30 14:23:20 +03:00
|
|
|
switch c.(type) {
|
|
|
|
case nil:
|
|
|
|
path = "<stdin>"
|
2022-07-16 19:39:57 +03:00
|
|
|
f = i.OS.Stdin()
|
2021-11-30 14:23:20 +03:00
|
|
|
default:
|
|
|
|
path, err = toString(c)
|
|
|
|
if err != nil {
|
2022-07-16 19:39:57 +03:00
|
|
|
return fmt.Errorf("%s: %w", path, err)
|
2021-11-30 14:23:20 +03:00
|
|
|
}
|
2022-07-16 19:39:57 +03:00
|
|
|
f, err = i.OS.FS().Open(path)
|
2021-11-16 13:41:14 +03:00
|
|
|
if err != nil {
|
2022-02-21 00:25:46 +03:00
|
|
|
// path context added in jq error code
|
|
|
|
var pe *fs.PathError
|
|
|
|
if errors.As(err, &pe) {
|
2022-07-16 19:39:57 +03:00
|
|
|
return pe.Err
|
2022-02-21 00:25:46 +03:00
|
|
|
}
|
2022-07-16 19:39:57 +03:00
|
|
|
return err
|
2021-11-16 13:41:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-30 14:23:20 +03:00
|
|
|
var bEnd int64
|
2021-11-16 13:41:14 +03:00
|
|
|
var fRS io.ReadSeeker
|
2021-11-30 14:23:20 +03:00
|
|
|
|
2021-11-16 13:41:14 +03:00
|
|
|
fFI, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
2022-07-16 19:39:57 +03:00
|
|
|
return err
|
2021-11-16 13:41:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ctxreadseeker is used to make sure any io calls can be canceled
|
2023-05-14 16:44:39 +03:00
|
|
|
// TODO: ctxreadseeker might leak if the underlying call hangs forever
|
2021-11-16 13:41:14 +03:00
|
|
|
|
|
|
|
// a regular file should be seekable but fallback below to read whole file if not
|
|
|
|
if fFI.Mode().IsRegular() {
|
|
|
|
if rs, ok := f.(io.ReadSeeker); ok {
|
2022-07-16 19:39:57 +03:00
|
|
|
fRS = ctxreadseeker.New(i.EvalInstance.Ctx, rs)
|
2021-11-16 13:41:14 +03:00
|
|
|
bEnd = fFI.Size()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if fRS == nil {
|
2022-08-12 16:27:51 +03:00
|
|
|
buf, err := io.ReadAll(ctxreadseeker.New(i.EvalInstance.Ctx, &ioex.ReadErrSeeker{Reader: f}))
|
2021-11-16 13:41:14 +03:00
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
2022-07-16 19:39:57 +03:00
|
|
|
return err
|
2021-11-16 13:41:14 +03:00
|
|
|
}
|
|
|
|
fRS = bytes.NewReader(buf)
|
|
|
|
bEnd = int64(len(buf))
|
|
|
|
}
|
|
|
|
|
|
|
|
bbf := &openFile{
|
|
|
|
filename: path,
|
|
|
|
}
|
|
|
|
|
|
|
|
const progressPrecision = 1024
|
|
|
|
fRS = progressreadseeker.New(fRS, progressPrecision, bEnd,
|
|
|
|
func(approxReadBytes int64, totalSize int64) {
|
|
|
|
// progressFn is assign by decode etc
|
|
|
|
if bbf.progressFn != nil {
|
|
|
|
bbf.progressFn(approxReadBytes, totalSize)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
const cacheReadAheadSize = 512 * 1024
|
|
|
|
aheadRs := aheadreadseeker.New(fRS, cacheReadAheadSize)
|
|
|
|
|
2021-11-21 23:55:53 +03:00
|
|
|
// bitio.Buffer -> (bitio.Reader) -> aheadreadseeker -> progressreadseeker -> ctxreadseeker -> readseeker
|
2021-11-16 13:41:14 +03:00
|
|
|
|
2022-01-24 23:21:48 +03:00
|
|
|
bbf.br = bitio.NewIOBitReadSeeker(aheadRs)
|
2021-11-16 13:41:14 +03:00
|
|
|
if err != nil {
|
2022-07-16 19:39:57 +03:00
|
|
|
return err
|
2021-11-16 13:41:14 +03:00
|
|
|
}
|
|
|
|
|
2022-07-16 19:39:57 +03:00
|
|
|
return bbf
|
2021-11-16 13:41:14 +03:00
|
|
|
}
|
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
var _ Value = Binary{}
|
|
|
|
var _ ToBinary = Binary{}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
type Binary struct {
|
2022-01-24 23:21:48 +03:00
|
|
|
br bitio.ReaderAtSeeker
|
2021-09-28 00:14:08 +03:00
|
|
|
r ranges.Range
|
2020-06-08 03:29:51 +03:00
|
|
|
unit int
|
2022-02-08 20:44:48 +03:00
|
|
|
pad int64
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2022-07-16 19:39:57 +03:00
|
|
|
func NewBinaryFromBitReader(br bitio.ReaderAtSeeker, unit int, pad int64) (Binary, error) {
|
2022-08-12 16:27:51 +03:00
|
|
|
l, err := bitioex.Len(br)
|
2022-01-24 23:21:48 +03:00
|
|
|
if err != nil {
|
2022-02-08 20:44:48 +03:00
|
|
|
return Binary{}, err
|
2022-01-24 23:21:48 +03:00
|
|
|
}
|
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
return Binary{
|
2022-01-24 23:21:48 +03:00
|
|
|
br: br,
|
|
|
|
r: ranges.Range{Start: 0, Len: l},
|
2020-06-08 03:29:51 +03:00
|
|
|
unit: unit,
|
2022-02-08 20:44:48 +03:00
|
|
|
pad: pad,
|
2022-01-24 23:21:48 +03:00
|
|
|
}, nil
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
func (b Binary) toBytesBuffer(r ranges.Range) (*bytes.Buffer, error) {
|
2022-08-12 16:27:51 +03:00
|
|
|
br, err := bitioex.Range(b.br, r.Start, r.Len)
|
2021-10-05 23:26:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
buf := &bytes.Buffer{}
|
2022-08-12 16:27:51 +03:00
|
|
|
if _, err := bitioex.CopyBits(buf, br); err != nil {
|
2021-10-05 23:26:05 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-24 23:21:48 +03:00
|
|
|
|
2021-10-05 23:26:05 +03:00
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
func (Binary) ExtType() string { return "binary" }
|
2021-12-05 03:15:59 +03:00
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
func (Binary) ExtKeys() []string {
|
2020-06-08 03:29:51 +03:00
|
|
|
return []string{
|
2023-05-03 10:59:09 +03:00
|
|
|
"bits",
|
|
|
|
"bytes",
|
|
|
|
"name",
|
2020-06-08 03:29:51 +03:00
|
|
|
"size",
|
|
|
|
"start",
|
|
|
|
"stop",
|
2023-05-11 18:48:02 +03:00
|
|
|
"unit",
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
func (b Binary) ToBinary() (Binary, error) {
|
2021-12-05 03:15:59 +03:00
|
|
|
return b, nil
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2021-10-05 23:26:05 +03:00
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func (b Binary) JQValueLength() any {
|
2021-12-05 03:15:59 +03:00
|
|
|
return int(b.r.Len / int64(b.unit))
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
func (b Binary) JQValueSliceLen() any {
|
2021-12-05 03:15:59 +03:00
|
|
|
return b.JQValueLength()
|
2021-10-05 23:26:05 +03:00
|
|
|
}
|
2021-09-23 19:35:04 +03:00
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func (b Binary) JQValueIndex(index int) any {
|
2021-10-05 23:26:05 +03:00
|
|
|
if index < 0 {
|
2021-10-20 02:28:26 +03:00
|
|
|
return nil
|
2021-10-05 23:26:05 +03:00
|
|
|
}
|
2021-10-20 02:28:26 +03:00
|
|
|
|
2021-12-05 03:15:59 +03:00
|
|
|
buf, err := b.toBytesBuffer(ranges.Range{Start: b.r.Start + int64(index*b.unit), Len: int64(b.unit)})
|
2021-10-05 23:26:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-20 02:28:26 +03:00
|
|
|
|
2022-01-24 23:21:48 +03:00
|
|
|
extraBits := uint((8 - b.unit%8) % 8)
|
|
|
|
|
2021-10-20 02:28:26 +03:00
|
|
|
return new(big.Int).Rsh(new(big.Int).SetBytes(buf.Bytes()), extraBits)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
func (b Binary) JQValueSlice(start int, end int) any {
|
2021-12-05 03:15:59 +03:00
|
|
|
rStart := int64(start * b.unit)
|
|
|
|
rLen := int64((end - start) * b.unit)
|
2020-06-08 03:29:51 +03:00
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
return Binary{
|
2022-01-24 23:21:48 +03:00
|
|
|
br: b.br,
|
2021-12-05 03:15:59 +03:00
|
|
|
r: ranges.Range{Start: b.r.Start + rStart, Len: rLen},
|
|
|
|
unit: b.unit,
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
func (b Binary) JQValueKey(name string) any {
|
2020-06-08 03:29:51 +03:00
|
|
|
switch name {
|
2023-05-03 10:59:09 +03:00
|
|
|
case "bits":
|
|
|
|
if b.unit == 1 {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
return Binary{br: b.br, r: b.r, unit: 1}
|
|
|
|
case "bytes":
|
|
|
|
if b.unit == 8 {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
return Binary{br: b.br, r: b.r, unit: 8}
|
|
|
|
|
|
|
|
case "name":
|
|
|
|
f := ioex.Unwrap(b.br)
|
|
|
|
// this exploits the fact that *os.File has Name()
|
|
|
|
if n, ok := f.(interface{ Name() string }); ok {
|
|
|
|
return n.Name()
|
|
|
|
}
|
|
|
|
return nil
|
2020-06-08 03:29:51 +03:00
|
|
|
case "size":
|
2021-12-05 03:15:59 +03:00
|
|
|
return new(big.Int).SetInt64(b.r.Len / int64(b.unit))
|
2020-06-08 03:29:51 +03:00
|
|
|
case "start":
|
2021-12-05 03:15:59 +03:00
|
|
|
return new(big.Int).SetInt64(b.r.Start / int64(b.unit))
|
2020-06-08 03:29:51 +03:00
|
|
|
case "stop":
|
2021-12-05 03:15:59 +03:00
|
|
|
stop := b.r.Stop()
|
|
|
|
stopUnits := stop / int64(b.unit)
|
|
|
|
if stop%int64(b.unit) != 0 {
|
2020-06-08 03:29:51 +03:00
|
|
|
stopUnits++
|
|
|
|
}
|
|
|
|
return new(big.Int).SetInt64(stopUnits)
|
2023-05-11 18:48:02 +03:00
|
|
|
case "unit":
|
|
|
|
return b.unit
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
func (b Binary) JQValueEach() any {
|
2020-06-08 03:29:51 +03:00
|
|
|
return nil
|
|
|
|
}
|
2022-02-08 20:44:48 +03:00
|
|
|
func (b Binary) JQValueType() string {
|
2022-07-26 16:50:18 +03:00
|
|
|
return gojq.JQTypeString
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
func (b Binary) JQValueKeys() any {
|
2022-08-12 16:27:51 +03:00
|
|
|
return gojqex.FuncTypeNameError{Name: "keys", Typ: gojq.JQTypeString}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
func (b Binary) JQValueHas(key any) any {
|
2022-08-12 16:27:51 +03:00
|
|
|
return gojqex.HasKeyTypeError{L: gojq.JQTypeString, R: fmt.Sprintf("%v", key)}
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
func (b Binary) JQValueToNumber() any {
|
2021-12-05 03:15:59 +03:00
|
|
|
buf, err := b.toBytesBuffer(b.r)
|
2021-09-28 00:14:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-05 03:15:59 +03:00
|
|
|
extraBits := uint((8 - b.r.Len%8) % 8)
|
2020-06-08 03:29:51 +03:00
|
|
|
return new(big.Int).Rsh(new(big.Int).SetBytes(buf.Bytes()), extraBits)
|
|
|
|
}
|
2022-05-20 16:10:41 +03:00
|
|
|
func (b Binary) JQValueToString() any {
|
2021-12-05 03:15:59 +03:00
|
|
|
return b.JQValueToGoJQ()
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
2023-05-17 12:23:32 +03:00
|
|
|
func (b Binary) JQValueToGoJQEx(optsFn func() (*Options, error)) any {
|
|
|
|
br, err := b.toReader()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
brC, err := bitio.CloneReaderAtSeeker(br)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts, err := optsFn()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := opts.BitsFormatFn(brC)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func (b Binary) JQValueToGoJQ() any {
|
2021-12-05 03:15:59 +03:00
|
|
|
buf, err := b.toBytesBuffer(b.r)
|
2021-09-28 00:14:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
return buf.String()
|
|
|
|
}
|
2021-08-15 15:07:16 +03:00
|
|
|
|
2023-05-15 18:23:59 +03:00
|
|
|
func (b Binary) Display(w io.Writer, opts *Options) error {
|
2021-08-26 16:38:49 +03:00
|
|
|
if opts.RawOutput {
|
2022-02-08 20:44:48 +03:00
|
|
|
br, err := b.toReader()
|
2021-09-28 00:14:08 +03:00
|
|
|
if err != nil {
|
2020-06-08 03:29:51 +03:00
|
|
|
return err
|
|
|
|
}
|
2022-01-24 23:21:48 +03:00
|
|
|
|
2022-08-12 16:27:51 +03:00
|
|
|
if _, err := bitioex.CopyBits(w, br); err != nil {
|
2020-06-08 03:29:51 +03:00
|
|
|
return err
|
|
|
|
}
|
2022-01-24 23:21:48 +03:00
|
|
|
|
2021-09-28 00:14:08 +03:00
|
|
|
return nil
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-12-05 03:15:59 +03:00
|
|
|
return hexdump(w, b, opts)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2022-02-08 20:44:48 +03:00
|
|
|
func (b Binary) toReader() (bitio.ReaderAtSeeker, error) {
|
2022-08-12 16:27:51 +03:00
|
|
|
br, err := bitioex.Range(b.br, b.r.Start, b.r.Len)
|
2022-02-08 20:44:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if b.pad == 0 {
|
|
|
|
return br, nil
|
|
|
|
}
|
2022-08-12 16:27:51 +03:00
|
|
|
return bitio.NewMultiReader(bitioex.NewZeroAtSeeker(b.pad), br)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|