1
1
mirror of https://github.com/wader/fq.git synced 2024-11-23 00:57:15 +03:00

decode: Refactor Error/Fatal into printf functions

This commit is contained in:
Mattias Wadman 2021-11-17 16:26:13 +01:00
parent ede2e77975
commit 5d98a6949a
29 changed files with 61 additions and 68 deletions

View File

@ -92,7 +92,7 @@ func gzDecode(d *decode.D, in interface{}) interface{} {
uncompressed := &bytes.Buffer{}
crc32W := crc32.NewIEEE()
if _, err := decode.Copy(d, io.MultiWriter(uncompressed, crc32W), deflateR); err != nil {
d.Fatal(err.Error())
d.Fatalf(err.Error())
}
// calculatedCRC32 := crc32W.Sum(nil)
uncompressedBB := bitio.NewBufferFromBytes(uncompressed.Bytes(), -1)

View File

@ -3,7 +3,6 @@ package dns
// TODO: https://github.com/Forescout/namewreck/blob/main/rfc/draft-dashevskyi-dnsrr-antipatterns-00.txt
import (
"fmt"
"net"
"strings"
@ -143,7 +142,7 @@ func fieldFormatLabel(d *decode.D, name string) {
}
jumpCount++
if jumpCount > maxJumps {
d.Fatal(fmt.Sprintf("label has more than %d jumps", maxJumps))
d.Fatalf("label has more than %d jumps", maxJumps)
}
d.SeekAbs(int64(pointer) * 8)
}

View File

@ -146,7 +146,7 @@ func elfDecode(d *decode.D, in interface{}) interface{} {
case BIG_ENDIAN:
d.Endian = decode.BigEndian
default:
d.Fatal("unknown endian")
d.Fatalf("unknown endian")
}
// TODO: hex functions?

View File

@ -2,7 +2,6 @@ package flac
import (
"encoding/binary"
"fmt"
"math/bits"
"github.com/wader/fq/format"
@ -78,7 +77,7 @@ func utf8Uint(d *decode.D) uint64 {
n = n<<6 | d.U8()&0x3f
}
default:
d.Error("invalid UTF8Uint")
d.Errorf("invalid UTF8Uint")
}
return n
}
@ -164,7 +163,7 @@ func frameDecode(d *decode.D, in interface{}) interface{} {
switch sampleRateBits {
case 0:
if inStreamInfo == nil {
d.Fatal("streaminfo required for sample rate")
d.Fatalf("streaminfo required for sample rate")
}
return decode.Scalar{Actual: sampleRateBits, Sym: inStreamInfo.SampleRate, Description: "streaminfo"}
case 0b0001:
@ -256,7 +255,7 @@ func frameDecode(d *decode.D, in interface{}) interface{} {
return decode.Scalar{Actual: v, Sym: ch, Description: desc}
})
if channels == 0 {
d.Fatal("unknown number of channels")
d.Fatalf("unknown number of channels")
}
// <3> Sample size in bits:
@ -274,7 +273,7 @@ func frameDecode(d *decode.D, in interface{}) interface{} {
switch sampleSizeBits {
case 0b000:
if inStreamInfo == nil {
d.Fatal("streaminfo required for bit per sample")
d.Fatalf("streaminfo required for bit per sample")
}
sampleSize = int(inStreamInfo.BitPerSample)
s.Description = "streaminfo"
@ -394,7 +393,7 @@ func frameDecode(d *decode.D, in interface{}) interface{} {
subframeSampleSize := sampleSize - wastedBitsK
if subframeSampleSize < 0 {
d.Fatal(fmt.Sprintf("negative subframeSampleSize %d", subframeSampleSize))
d.Fatalf("negative subframeSampleSize %d", subframeSampleSize)
}
// if channel is side, add en extra sample bit
// https://github.com/xiph/flac/blob/37e675b777d4e0de53ac9ff69e2aea10d92e729c/src/libFLAC/stream_decoder.c#L2040
@ -405,7 +404,7 @@ func frameDecode(d *decode.D, in interface{}) interface{} {
decodeWarmupSamples := func(samples []int64, n int, sampleSize int) {
if len(samples) < n {
d.Fatal("decodeWarmupSamples outside block size")
d.Fatalf("decodeWarmupSamples outside block size")
}
d.FieldArray("warmup_samples", func(d *decode.D) {
@ -473,7 +472,7 @@ func frameDecode(d *decode.D, in interface{}) interface{} {
riceParameter := int(d.FieldU("rice_parameter", riceBits))
if samplesLen < n+count {
d.Fatal("decodeResiduals outside block size")
d.Fatalf("decodeResiduals outside block size")
}
if riceParameter == riceEscape {
@ -559,7 +558,7 @@ func frameDecode(d *decode.D, in interface{}) interface{} {
// <5> Quantized linear predictor coefficient shift needed in bits (NOTE: this number is signed two's-complement).
shift := d.FieldS5("shift")
if shift < 0 {
d.Fatal(fmt.Sprintf("negative LPC shift %d", shift))
d.Fatalf("negative LPC shift %d", shift)
}
// <n> Unencoded predictor coefficients (n = qlp coeff precision * lpc order) (NOTE: the coefficients are signed two's-complement).
var coeffs []int64
@ -594,7 +593,7 @@ func frameDecode(d *decode.D, in interface{}) interface{} {
streamSamples := len(channelSamples[0])
for j := 0; j < len(channelSamples); j++ {
if streamSamples > len(channelSamples[j]) {
d.Fatal(fmt.Sprintf("different amount of samples in channels %d != %d", streamSamples, len(channelSamples[j])))
d.Fatalf("different amount of samples in channels %d != %d", streamSamples, len(channelSamples[j]))
}
}

View File

@ -143,7 +143,7 @@ func gifDecode(d *decode.D, in interface{}) interface{} {
})
})
default:
d.Fatal("unknown block")
d.Fatalf("unknown block")
}
}
})

View File

@ -106,7 +106,7 @@ func gzDecode(d *decode.D, in interface{}) interface{} {
deflateR := flate.NewReader(compressedBB)
uncompressed := &bytes.Buffer{}
if _, err := decode.Copy(d, io.MultiWriter(uncompressed, crc32W), deflateR); err != nil {
d.Fatal(err.Error())
d.Fatalf(err.Error())
}
uncompressedBB := bitio.NewBufferFromBytes(uncompressed.Bytes(), -1)
dv, _, _ := d.FieldTryFormatBitBuf("uncompressed", uncompressedBB, probeFormat, nil)

View File

@ -21,7 +21,7 @@ func id3v1Decode(d *decode.D, in interface{}) interface{} {
d.AssertAtLeastBitsLeft(128 * 8)
d.FieldUTF8("magic", 3, d.AssertStr("TAG"))
if d.PeekBits(8) == uint64('+') {
d.Error("looks like id3v11")
d.Errorf("looks like id3v11")
}
d.FieldUTF8NullFixedLen("song_name", 30)
d.FieldUTF8NullFixedLen("artist", 30)

View File

@ -6,7 +6,6 @@ package id3
// https://id3.org/id3v2-chapters-1.0
import (
"fmt"
"io"
"strings"
@ -384,7 +383,7 @@ func decodeFrame(d *decode.D, version int) uint64 {
size = dataSize + headerLen
default:
// can't know size
d.Fatal("unknown version")
d.Fatalf("unknown version")
}
// note frame function run inside a SubLenFn so they can use BitLefts and
@ -581,7 +580,7 @@ func id3v2Decode(d *decode.D, in interface{}) interface{} {
version := int(d.FieldU8("version"))
versionValid := version == 2 || version == 3 || version == 4
if !versionValid {
d.Fatal(fmt.Sprintf("unsupported version %d", version))
d.Fatalf("unsupported version %d", version)
}
d.FieldU8("revision")

View File

@ -6,7 +6,6 @@ package jpeg
import (
"bytes"
"fmt"
"github.com/wader/fq/format"
"github.com/wader/fq/format/registry"
@ -167,7 +166,7 @@ var markers = decode.UToScalar{
func jpegDecode(d *decode.D, in interface{}) interface{} {
d.AssertLeastBytesLeft(2)
if !bytes.Equal(d.PeekBytes(2), []byte{0xff, SOI}) {
d.Error("no SOI marker")
d.Errorf("no SOI marker")
}
var extendedXMP []byte
@ -268,7 +267,7 @@ func jpegDecode(d *decode.D, in interface{}) interface{} {
eoiMarkerFound = true
default:
if !markerFound {
d.Error(fmt.Sprintf("unknown marker %x", markerCode))
d.Errorf("unknown marker %x", markerCode)
}
markerLen := d.FieldU16("length")
@ -308,7 +307,7 @@ func jpegDecode(d *decode.D, in interface{}) interface{} {
// TODO: redo this? multi reader?
chunkBytes, err := chunk.Bytes()
if err != nil {
d.Fatal(fmt.Sprintf("failed to read xmp chunk: %s", err))
d.Fatalf("failed to read xmp chunk: %s", err)
}
if extendedXMP == nil {
@ -344,7 +343,7 @@ func jpegDecode(d *decode.D, in interface{}) interface{} {
})
if !soiMarkerFound {
d.Error("no SOI marker found")
d.Errorf("no SOI marker found")
}
if extendedXMP != nil {

View File

@ -23,13 +23,13 @@ func decodeJSON(d *decode.D, in interface{}) interface{} {
jd := stdjson.NewDecoder(bb)
var s decode.Scalar
if err := jd.Decode(&s.Actual); err != nil {
d.Fatal(err.Error())
d.Fatalf(err.Error())
}
switch s.Actual.(type) {
case map[string]interface{},
[]interface{}:
default:
d.Fatal("root not object or array")
d.Fatalf("root not object or array")
}
// TODO: root not array/struct how to add unknown gaps?
// TODO: ranges not end up correct

View File

@ -160,7 +160,7 @@ func decodeMaster(d *decode.D, bitsLimit int64, tag ebml.Tag, dc *decodeContext)
if !ok {
a, ok = ebml.Global[n]
if !ok {
d.Fatal(fmt.Sprintf("unknown id %d", n))
d.Fatalf("unknown id %d", n)
}
}
return decode.Scalar{Actual: n, DisplayFormat: decode.NumberHex, Sym: a.Name, Description: a.Definition}, nil
@ -183,7 +183,7 @@ func decodeMaster(d *decode.D, bitsLimit int64, tag ebml.Tag, dc *decodeContext)
(a.Type == ebml.Integer ||
a.Type == ebml.Uinteger ||
a.Type == ebml.Float) {
d.Fatal(fmt.Sprintf("invalid tagSize %d for non-master type", tagSize))
d.Fatalf("invalid tagSize %d for non-master type", tagSize)
}
switch a.Type {
@ -305,7 +305,7 @@ func decodeMaster(d *decode.D, bitsLimit int64, tag ebml.Tag, dc *decodeContext)
func matroskaDecode(d *decode.D, in interface{}) interface{} {
ebmlHeaderID := uint64(0x1a45dfa3)
if d.PeekBits(32) != ebmlHeaderID {
d.Error("no EBML header found")
d.Errorf("no EBML header found")
}
dc := &decodeContext{tracks: []*track{}}
decodeMaster(d, d.BitsLeft(), ebml_matroska.Root, dc)

View File

@ -70,7 +70,7 @@ func mp3Decode(d *decode.D, in interface{}) interface{} {
})
// TODO: better validate
if validFrames == 0 || (validFrames < 2 && decodeFailures > 0) {
d.Error("no frames found")
d.Errorf("no frames found")
}
d.SeekAbs(lastValidEnd)

View File

@ -24,7 +24,7 @@ func xingDecode(d *decode.D, in interface{}) interface{} {
case "Info":
hasLameExtension = true
default:
d.Error("no xing header found")
d.Errorf("no xing header found")
}
qualityPresent := false

View File

@ -137,13 +137,13 @@ func mp4Decode(d *decode.D, in interface{}) interface{} {
d.AssertLeastBytesLeft(16)
size := d.U32()
if size < 8 {
d.Fatal("first box size too small < 8")
d.Fatalf("first box size too small < 8")
}
firstType := d.UTF8(4)
switch firstType {
case "styp", "ftyp", "free", "moov":
default:
d.Error("no styp, ftyp, free or moov box found")
d.Errorf("no styp, ftyp, free or moov box found")
}
d.SeekRel(-8 * 8)

View File

@ -32,7 +32,7 @@ func adtsDecoder(d *decode.D, in interface{}) interface{} {
}
if validFrames == 0 {
d.Fatal("no valid frames")
d.Fatalf("no valid frames")
}
return nil

View File

@ -101,7 +101,7 @@ func adtsFrameDecoder(d *decode.D, in interface{}) interface{} {
}
if dataLength < 0 {
d.Fatal("dataLength < 0")
d.Fatalf("dataLength < 0")
}
d.FieldArray("raw_data_blocks", func(d *decode.D) {

View File

@ -26,7 +26,7 @@ func annexBDecode(d *decode.D, _ interface{}, format []*decode.Format) interface
currentOffset, currentPrefixLen, err := annexBFindStartCode(d)
// TODO: really restrict to 0?
if err != nil || currentOffset != 0 {
d.Error("could not find start code (first)")
d.Errorf("could not find start code (first)")
}
for d.NotEnd() {

View File

@ -26,7 +26,7 @@ func init() {
func avcAUDecode(d *decode.D, in interface{}) interface{} {
avcIn, ok := in.(format.AvcIn)
if !ok {
d.Fatal("avcIn required")
d.Fatalf("avcIn required")
}
for d.NotEnd() {

View File

@ -24,7 +24,7 @@ func init() {
func hevcAUDecode(d *decode.D, in interface{}) interface{} {
hevcIn, ok := in.(format.HevcIn)
if !ok {
d.Error("hevcIn required")
d.Errorf("hevcIn required")
}
for d.NotEnd() {

View File

@ -159,12 +159,12 @@ func frameDecode(d *decode.D, in interface{}) interface{} {
mpegVersion := d.FieldU2("mpeg_version", d.MapUToScalar(mpegVersionNames))
mpegVersionNr = mpegVersionN[mpegVersion]
if mpegVersionNr == 0 {
d.Error("Unsupported mpeg version")
d.Errorf("Unsupported mpeg version")
}
mpegLayer := d.FieldU2("layer", d.MapUToScalar(mpegLayerNames))
mpegLayerNr = mpegLayerN[mpegLayer]
if mpegLayerNr != 3 {
d.Error("Not layer 3")
d.Errorf("Not layer 3")
mpegLayerNr = 3
}
// [mpeg layer][mpeg version]
@ -210,7 +210,7 @@ func frameDecode(d *decode.D, in interface{}) interface{} {
default:
i := (mpegVersionNr-1)*3 + (mpegLayerNr - 1)
if i >= 9 {
d.Fatal("Invalid bitrate index")
d.Fatalf("Invalid bitrate index")
}
bitRate = uint64(bitRateIndex[uint(u)][(mpegVersionNr-1)*3+(mpegLayerNr-1)]) * 1000
return decode.Scalar{Actual: u, Sym: bitRate}
@ -342,7 +342,7 @@ func frameDecode(d *decode.D, in interface{}) interface{} {
}
if sampleRate == 0 {
d.Error("zero sample rate")
d.Errorf("zero sample rate")
}
calcFrameBytes := int64(144*bitRate/sampleRate + paddingBytes)

View File

@ -40,7 +40,7 @@ func pesDecode(d *decode.D, in interface{}) interface{} {
prefix := d.PeekBits(24)
if prefix != 0b0000_0000_0000_0000_0000_0001 {
d.Error("no pes prefix found")
d.Errorf("no pes prefix found")
}
i := 0

View File

@ -179,7 +179,7 @@ func decodeOgg(d *decode.D, in interface{}) interface{} {
})
if validPages == 0 {
d.Fatal("no pages found")
d.Fatalf("no pages found")
}
return nil

View File

@ -5,7 +5,6 @@ package tar
import (
"bytes"
"fmt"
"strconv"
"strings"
@ -43,7 +42,7 @@ func tarDecode(d *decode.D, in interface{}) interface{} {
var err error
n, err = strconv.ParseUint(ts, 8, 64)
if err != nil {
d.Error(fmt.Sprintf("failed to parse %s number %s: %s", name, ts, err))
d.Errorf("failed to parse %s number %s: %s", name, ts, err)
}
}
return decode.Scalar{Actual: a, Sym: n}, nil
@ -76,7 +75,7 @@ func tarDecode(d *decode.D, in interface{}) interface{} {
fieldStr(d, "linkname", 100)
magic := fieldStr(d, "magic", 6)
if magic != "ustar" {
d.Error(fmt.Sprintf("invalid magic %s", magic))
d.Errorf("invalid magic %s", magic)
}
fieldNumStr(d, "version", 2)
fieldStr(d, "uname", 32)
@ -104,7 +103,7 @@ func tarDecode(d *decode.D, in interface{}) interface{} {
d.FieldRawLen("end_marker", 512*2*8)
if !foundEndMarker {
d.Error("no files found")
d.Errorf("no files found")
}
return nil

View File

@ -188,7 +188,7 @@ func decodeIfd(d *decode.D, s *strips, tagNames map[uint64]string) int64 {
case SRATIONAL:
fieldSRational(d, "value")
default:
d.Error("unknown type")
d.Errorf("unknown type")
}
}
})
@ -214,7 +214,7 @@ func tiffDecode(d *decode.D, in interface{}) interface{} {
case bigEndian:
d.Endian = decode.BigEndian
default:
d.Fatal("unknown endian")
d.Fatalf("unknown endian")
}
d.SeekRel(-4 * 8)

View File

@ -5,8 +5,6 @@ package vorbis
// TODO: end padding? byte align?
import (
"fmt"
"github.com/wader/fq/format"
"github.com/wader/fq/format/registry"
"github.com/wader/fq/pkg/decode"
@ -59,7 +57,7 @@ func vorbisDecode(d *decode.D, in interface{}) interface{} {
d.FieldUTF8("magic", 6, d.AssertStr("vorbis"))
case packetTypeAudio:
default:
d.Fatal(fmt.Sprintf("unknown packet type %d", packetType))
d.Fatalf("unknown packet type %d", packetType)
}
switch packetType {

View File

@ -160,7 +160,7 @@ func decodeChunk(d *decode.D, expectedChunkID string, stringData bool) int64 { /
return strings.TrimSpace(d.UTF8(4))
})
if expectedChunkID != "" && trimChunkID != expectedChunkID {
d.Error(fmt.Sprintf("expected chunk id %q found %q", expectedChunkID, trimChunkID))
d.Errorf(fmt.Sprintf("expected chunk id %q found %q", expectedChunkID, trimChunkID))
}
const restOfFileLen = 0xffffffff
chunkLen := int64(d.FieldUScalarFn("size", func(d *decode.D) decode.Scalar {

View File

@ -64,7 +64,7 @@ func webpDecode(d *decode.D, in interface{}) interface{} {
})
})
default:
d.Fatal("could not find VP8 or VP8L chunk")
d.Fatalf("could not find VP8 or VP8L chunk")
}
})

View File

@ -215,16 +215,16 @@ func (d *D) FillGaps(r ranges.Range, namePrefix string) {
}
}
// Error stops decode with a reason unless forced
func (d *D) Error(reason string) {
// Errorf stops decode with a reason unless forced
func (d *D) Errorf(format string, a ...interface{}) {
if !d.Options.Force {
panic(ValidateError{Reason: reason, Pos: d.Pos()})
panic(DecoderError{Reason: format, Pos: d.Pos()})
}
}
// Fatal stops decode with a reason regardless of forced
func (d *D) Fatal(reason string) {
panic(ValidateError{Reason: reason, Pos: d.Pos()})
// Fatalf stops decode with a reason regardless of forced
func (d *D) Fatalf(format string, a ...interface{}) {
panic(DecoderError{Reason: format, Pos: d.Pos()})
}
func (d *D) IOPanic(err error) {
@ -449,7 +449,7 @@ func (d *D) AddChild(v *Value) {
if !fv.IsArray {
for _, ff := range *fv.Children {
if ff.Name == v.Name {
d.Fatal(fmt.Sprintf("%q already exist in struct %s", v.Name, d.Value.Name))
d.Fatalf("%q already exist in struct %s", v.Name, d.Value.Name)
}
}
}
@ -569,7 +569,7 @@ func (d *D) AssertAtLeastBitsLeft(nBits int64) {
bl := d.BitsLeft()
if bl < nBits {
// TODO:
panic(ValidateError{Reason: fmt.Sprintf("expected bits left %d, found %d", nBits, bl), Pos: d.Pos()})
panic(DecoderError{Reason: fmt.Sprintf("expected bits left %d, found %d", nBits, bl), Pos: d.Pos()})
}
}
@ -580,7 +580,7 @@ func (d *D) AssertLeastBytesLeft(nBytes int64) {
bl := d.BitsLeft()
if bl < nBytes*8 {
// TODO:
panic(ValidateError{Reason: fmt.Sprintf("expected bytes left %d, found %d bits", nBytes, bl), Pos: d.Pos()})
panic(DecoderError{Reason: fmt.Sprintf("expected bytes left %d, found %d bits", nBytes, bl), Pos: d.Pos()})
}
}

View File

@ -78,13 +78,13 @@ func (e IOError) Unwrap() error { return e.Err }
func (IOError) IsRecoverableError() bool { return true }
type ValidateError struct {
type DecoderError struct {
Reason string
Pos int64
}
func (e ValidateError) Error() string {
func (e DecoderError) Error() string {
return fmt.Sprintf("failed to validate at position %s: %s", num.Bits(e.Pos).StringByteBits(16), e.Reason)
}
func (ValidateError) IsRecoverableError() bool { return true }
func (DecoderError) IsRecoverableError() bool { return true }