Skip to content

Commit

Permalink
decode: Refactor Error/Fatal into printf functions
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Nov 17, 2021
1 parent ede2e77 commit 5d98a69
Show file tree
Hide file tree
Showing 29 changed files with 61 additions and 68 deletions.
2 changes: 1 addition & 1 deletion format/bzip2/bzip2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions format/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion format/elf/elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
19 changes: 9 additions & 10 deletions format/flac/flac_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package flac

import (
"encoding/binary"
"fmt"
"math/bits"

"github.com/wader/fq/format"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]))
}
}

Expand Down
2 changes: 1 addition & 1 deletion format/gif/gif.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func gifDecode(d *decode.D, in interface{}) interface{} {
})
})
default:
d.Fatal("unknown block")
d.Fatalf("unknown block")
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion format/gzip/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion format/id3/id3v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions format/id3/id3v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package id3
// https://id3.org/id3v2-chapters-1.0

import (
"fmt"
"io"
"strings"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
9 changes: 4 additions & 5 deletions format/jpeg/jpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package jpeg

import (
"bytes"
"fmt"

"github.com/wader/fq/format"
"github.com/wader/fq/format/registry"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions format/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions format/matroska/matroska.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion format/mp3/mp3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion format/mp3/xing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions format/mp4/mp4.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion format/mpeg/adts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion format/mpeg/adts_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion format/mpeg/annexb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion format/mpeg/avc_au.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion format/mpeg/hevc_au.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions format/mpeg/mp3_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion format/mpeg/mpeg_pes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion format/ogg/ogg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 5d98a69

Please sign in to comment.