diff --git a/format/avro/avro_ocf.go b/format/avro/avro_ocf.go index 08c192ac6..eece16684 100644 --- a/format/avro/avro_ocf.go +++ b/format/avro/avro_ocf.go @@ -94,7 +94,7 @@ func decodeBlockCodec(d *decode.D, dataSize int64, codec string) *bytes.Buffer { bb := &bytes.Buffer{} if codec == "deflate" { br := d.FieldRawLen("compressed", dataSize*8) - d.MustCopy(bb, flate.NewReader(bitio.NewIOReader(br))) + d.Copy(bb, flate.NewReader(bitio.NewIOReader(br))) } else if codec == "snappy" { // Everything but last 4 bytes which are the checksum n := dataSize - 4 @@ -110,11 +110,11 @@ func decodeBlockCodec(d *decode.D, dataSize int64, codec string) *bytes.Buffer { if err != nil { d.Fatalf("failed decompressing data: %v", err) } - d.MustCopy(bb, bytes.NewReader(decompressed)) + d.Copy(bb, bytes.NewReader(decompressed)) // Check the checksum crc32W := crc32.NewIEEE() - d.MustCopy(crc32W, bytes.NewReader(bb.Bytes())) + d.Copy(crc32W, bytes.NewReader(bb.Bytes())) d.FieldU32("crc", d.ValidateUBytes(crc32W.Sum(nil)), scalar.ActualHex) } else { // Unknown codec, just dump the compressed data. diff --git a/format/bzip2/bzip2.go b/format/bzip2/bzip2.go index d32e7291a..cf8aad2cf 100644 --- a/format/bzip2/bzip2.go +++ b/format/bzip2/bzip2.go @@ -116,7 +116,7 @@ func bzip2Decode(d *decode.D, in any) any { } blockCRC32W := crc32.NewIEEE() - d.MustCopy(blockCRC32W, bitFlipReader{bitio.NewIOReader(uncompressedBR)}) + d.Copy(blockCRC32W, bitFlipReader{bitio.NewIOReader(uncompressedBR)}) blockCRC32N := bits.Reverse32(binary.BigEndian.Uint32(blockCRC32W.Sum(nil))) _ = blockCRCValue.TryScalarFn(d.ValidateU(uint64(blockCRC32N))) streamCRCN = blockCRC32N ^ ((streamCRCN << 1) | (streamCRCN >> 31)) diff --git a/format/cbor/cbor.go b/format/cbor/cbor.go index f962ca1d8..ff7da331f 100644 --- a/format/cbor/cbor.go +++ b/format/cbor/cbor.go @@ -140,7 +140,7 @@ func decodeCBORValue(d *decode.D) any { return nil } - buf := d.MustReadAllBits(d.FieldRawLen("value", int64(count)*8)) + buf := d.ReadAllBits(d.FieldRawLen("value", int64(count)*8)) return buf }}, diff --git a/format/flac/flac.go b/format/flac/flac.go index 58c66ebe0..09672a6d6 100644 --- a/format/flac/flac.go +++ b/format/flac/flac.go @@ -72,7 +72,7 @@ func flacDecode(d *decode.D, in any) any { frameStreamSamplesBuf := ffo.SamplesBuf[0 : samplesInFrame*uint64(ffo.Channels*ffo.BitsPerSample/8)] framesNDecodedSamples += ffo.Samples - d.MustCopy(md5Samples, bytes.NewReader(frameStreamSamplesBuf)) + d.Copy(md5Samples, bytes.NewReader(frameStreamSamplesBuf)) streamDecodedSamples += ffo.Samples // reuse buffer if possible diff --git a/format/flac/flac_frame.go b/format/flac/flac_frame.go index 57b84bb38..4b4ae0d71 100644 --- a/format/flac/flac_frame.go +++ b/format/flac/flac_frame.go @@ -319,7 +319,7 @@ func frameDecode(d *decode.D, in any) any { }) headerCRC := &checksum.CRC{Bits: 8, Table: checksum.ATM8Table} - d.MustCopyBits(headerCRC, d.BitBufRange(frameStart, d.Pos()-frameStart)) + d.CopyBits(headerCRC, d.BitBufRange(frameStart, d.Pos()-frameStart)) d.FieldU8("crc", d.ValidateUBytes(headerCRC.Sum(nil)), scalar.ActualHex) }) @@ -565,7 +565,7 @@ func frameDecode(d *decode.D, in any) any { d.FieldU("byte_align", d.ByteAlignBits(), d.AssertU(0)) // <16> CRC-16 (polynomial = x^16 + x^15 + x^2 + x^0, initialized with 0) of everything before the crc, back to and including the frame header sync code footerCRC := &checksum.CRC{Bits: 16, Table: checksum.ANSI16Table} - d.MustCopyBits(footerCRC, d.BitBufRange(frameStart, d.Pos()-frameStart)) + d.CopyBits(footerCRC, d.BitBufRange(frameStart, d.Pos()-frameStart)) d.FieldRawLen("footer_crc", 16, d.ValidateBitBuf(footerCRC.Sum(nil)), scalar.RawHex) streamSamples := len(channelSamples[0]) diff --git a/format/flac/flac_streaminfo.go b/format/flac/flac_streaminfo.go index e82115bbd..248414c20 100644 --- a/format/flac/flac_streaminfo.go +++ b/format/flac/flac_streaminfo.go @@ -27,7 +27,7 @@ func streaminfoDecode(d *decode.D, in any) any { bitsPerSample := d.FieldU5("bits_per_sample", scalar.ActualUAdd(1)) totalSamplesInStream := d.FieldU("total_samples_in_stream", 36) md5BR := d.FieldRawLen("md5", 16*8, scalar.RawHex) - md5b := d.MustReadAllBits(md5BR) + md5b := d.ReadAllBits(md5BR) return format.FlacStreaminfoOut{ StreamInfo: format.FlacStreamInfo{ diff --git a/format/gif/gif.go b/format/gif/gif.go index 029b5dee0..15a553c38 100644 --- a/format/gif/gif.go +++ b/format/gif/gif.go @@ -93,7 +93,7 @@ func gifDecode(d *decode.D, in any) any { d.FieldU8("terminator") seenTerminator = true } - d.MustCopyBits(dataBytes, d.MustCloneReadSeeker(b)) + d.CopyBits(dataBytes, d.CloneReadSeeker(b)) }) } }) diff --git a/format/gzip/gzip.go b/format/gzip/gzip.go index a8a0d6309..9a2e87e08 100644 --- a/format/gzip/gzip.go +++ b/format/gzip/gzip.go @@ -115,7 +115,7 @@ func gzDecode(d *decode.D, in any) any { d.FieldRawLen("compressed", readCompressedSize) crc32W := crc32.NewIEEE() // TODO: cleanup clone - d.MustCopyBits(crc32W, d.MustCloneReadSeeker(uncompressedBR)) + d.CopyBits(crc32W, d.CloneReadSeeker(uncompressedBR)) d.FieldU32("crc32", d.ValidateUBytes(crc32W.Sum(nil)), scalar.ActualHex) d.FieldU32("isize") } diff --git a/format/id3/id3v2.go b/format/id3/id3v2.go index ba333020f..40f6b38df 100644 --- a/format/id3/id3v2.go +++ b/format/id3/id3v2.go @@ -544,7 +544,7 @@ func decodeFrame(d *decode.D, version int) uint64 { if unsyncFlag { // TODO: DecodeFn // TODO: unknown after frame decode - unsyncedBR := d.MustNewBitBufFromReader(unsyncReader{Reader: bitio.NewIOReader(d.BitBufRange(d.Pos(), int64(dataSize)*8))}) + unsyncedBR := d.NewBitBufFromReader(unsyncReader{Reader: bitio.NewIOReader(d.BitBufRange(d.Pos(), int64(dataSize)*8))}) d.FieldFormatBitBuf("unsync", unsyncedBR, decode.FormatFn(func(d *decode.D, in any) any { if fn, ok := frames[idNormalized]; ok { fn(d) diff --git a/format/inet/ipv4_packet.go b/format/inet/ipv4_packet.go index d4964484a..a9b9ab337 100644 --- a/format/inet/ipv4_packet.go +++ b/format/inet/ipv4_packet.go @@ -94,8 +94,8 @@ func decodeIPv4(d *decode.D, in any) any { headerEnd := d.Pos() ipv4Checksum := &checksum.IPv4{} - d.MustCopy(ipv4Checksum, bitio.NewIOReader(d.BitBufRange(0, checksumStart))) - d.MustCopy(ipv4Checksum, bitio.NewIOReader(d.BitBufRange(checksumEnd, headerEnd-checksumEnd))) + d.Copy(ipv4Checksum, bitio.NewIOReader(d.BitBufRange(0, checksumStart))) + d.Copy(ipv4Checksum, bitio.NewIOReader(d.BitBufRange(checksumEnd, headerEnd-checksumEnd))) _ = d.FieldMustGet("header_checksum").TryScalarFn(d.ValidateUBytes(ipv4Checksum.Sum(nil)), scalar.ActualHex) dataLen := int64(totalLength-(ihl*4)) * 8 diff --git a/format/jpeg/jpeg.go b/format/jpeg/jpeg.go index dddc26724..098381bb2 100644 --- a/format/jpeg/jpeg.go +++ b/format/jpeg/jpeg.go @@ -306,7 +306,7 @@ func jpegDecode(d *decode.D, in any) any { // TODO: FieldBitsLen? concat bitbuf? chunk := d.FieldRawLen("data", d.BitsLeft()) // TODO: redo this? multi reader? - chunkBytes := d.MustReadAllBits(chunk) + chunkBytes := d.ReadAllBits(chunk) if extendedXMP == nil { extendedXMP = make([]byte, fullLength) diff --git a/format/mp4/boxes.go b/format/mp4/boxes.go index 597eee3f0..2d5e27ffa 100644 --- a/format/mp4/boxes.go +++ b/format/mp4/boxes.go @@ -1086,7 +1086,7 @@ func init() { d.FieldU24("flags") systemIDBR := d.FieldRawLen("system_id", 16*8, systemIDNames) // TODO: make nicer - systemID := d.MustReadAllBits(systemIDBR) + systemID := d.ReadAllBits(systemIDBR) switch version { case 0: case 1: diff --git a/format/mpeg/avc_nalu.go b/format/mpeg/avc_nalu.go index eeb60a655..a24cbfd3b 100644 --- a/format/mpeg/avc_nalu.go +++ b/format/mpeg/avc_nalu.go @@ -102,7 +102,7 @@ func avcNALUDecode(d *decode.D, in any) any { d.FieldBool("forbidden_zero_bit") d.FieldU2("nal_ref_idc") nalType := d.FieldU5("nal_unit_type", avcNALNames) - unescapedBR := d.MustNewBitBufFromReader(decode.NALUnescapeReader{Reader: bitio.NewIOReader(d.BitBufRange(d.Pos(), d.BitsLeft()))}) + unescapedBR := d.NewBitBufFromReader(decode.NALUnescapeReader{Reader: bitio.NewIOReader(d.BitBufRange(d.Pos(), d.BitsLeft()))}) switch nalType { case avcNALCodedSliceNonIDR, diff --git a/format/mpeg/hevc_nalu.go b/format/mpeg/hevc_nalu.go index 2fe82c7d7..69a1493c3 100644 --- a/format/mpeg/hevc_nalu.go +++ b/format/mpeg/hevc_nalu.go @@ -87,7 +87,7 @@ func hevcNALUDecode(d *decode.D, in any) any { nalType := d.FieldU6("nal_unit_type", hevcNALNames) d.FieldU6("nuh_layer_id") d.FieldU3("nuh_temporal_id_plus1") - unescapedBR := d.MustNewBitBufFromReader(decode.NALUnescapeReader{Reader: bitio.NewIOReader(d.BitBufRange(d.Pos(), d.BitsLeft()))}) + unescapedBR := d.NewBitBufFromReader(decode.NALUnescapeReader{Reader: bitio.NewIOReader(d.BitBufRange(d.Pos(), d.BitsLeft()))}) switch nalType { case hevcNALNUTVPS: diff --git a/format/mpeg/mp3_frame.go b/format/mpeg/mp3_frame.go index 74a24685e..aa7ada233 100644 --- a/format/mpeg/mp3_frame.go +++ b/format/mpeg/mp3_frame.go @@ -381,8 +381,8 @@ func frameDecode(d *decode.D, in any) any { crcHash := &checksum.CRC{Bits: 16, Current: 0xffff, Table: checksum.ANSI16Table} // 2 bytes after sync and some other fields + all of side info - d.MustCopyBits(crcHash, d.BitBufRange(2*8, 2*8)) - d.MustCopyBits(crcHash, d.BitBufRange(6*8, sideInfoBytes*8)) + d.CopyBits(crcHash, d.BitBufRange(2*8, 2*8)) + d.CopyBits(crcHash, d.BitBufRange(6*8, sideInfoBytes*8)) if crcValue != nil { _ = crcValue.TryScalarFn(d.ValidateUBytes(crcHash.Sum(nil))) diff --git a/format/mpeg/mpeg_pes_packet.go b/format/mpeg/mpeg_pes_packet.go index 80c10d2e1..48ac5f27a 100644 --- a/format/mpeg/mpeg_pes_packet.go +++ b/format/mpeg/mpeg_pes_packet.go @@ -190,7 +190,7 @@ func pesPacketDecode(d *decode.D, in any) any { v = subStreamPacket{ number: int(substreamNumber), - buf: d.MustReadAllBits(substreamBR), + buf: d.ReadAllBits(substreamBR), } }) }) diff --git a/format/ogg/ogg_page.go b/format/ogg/ogg_page.go index ba4a42bd1..7c5e99848 100644 --- a/format/ogg/ogg_page.go +++ b/format/ogg/ogg_page.go @@ -44,7 +44,7 @@ func pageDecode(d *decode.D, in any) any { }) d.FieldArray("segments", func(d *decode.D) { for _, ss := range segmentTable { - bs := d.MustReadAllBits(d.FieldRawLen("segment", int64(ss)*8)) + bs := d.ReadAllBits(d.FieldRawLen("segment", int64(ss)*8)) p.Segments = append(p.Segments, bs) } }) @@ -52,9 +52,9 @@ func pageDecode(d *decode.D, in any) any { pageChecksumValue := d.FieldGet("crc") pageCRC := &checksum.CRC{Bits: 32, Table: checksum.Poly04c11db7Table} - d.MustCopy(pageCRC, bitio.NewIOReader(d.BitBufRange(startPos, pageChecksumValue.Range.Start-startPos))) // header before checksum - d.MustCopy(pageCRC, bytes.NewReader([]byte{0, 0, 0, 0})) // zero checksum bits - d.MustCopy(pageCRC, bitio.NewIOReader(d.BitBufRange(pageChecksumValue.Range.Stop(), endPos-pageChecksumValue.Range.Stop()))) // rest of page + d.Copy(pageCRC, bitio.NewIOReader(d.BitBufRange(startPos, pageChecksumValue.Range.Start-startPos))) // header before checksum + d.Copy(pageCRC, bytes.NewReader([]byte{0, 0, 0, 0})) // zero checksum bits + d.Copy(pageCRC, bitio.NewIOReader(d.BitBufRange(pageChecksumValue.Range.Stop(), endPos-pageChecksumValue.Range.Stop()))) // rest of page _ = pageChecksumValue.TryScalarFn(d.ValidateUBytes(pageCRC.Sum(nil))) return p diff --git a/format/pcap/pcap.go b/format/pcap/pcap.go index 40683f8b2..16ee9f30c 100644 --- a/format/pcap/pcap.go +++ b/format/pcap/pcap.go @@ -78,7 +78,7 @@ func decodePcap(d *decode.D, in any) any { d.Errorf("incl_len %d > orig_len %d", inclLen, origLen) } - bs := d.MustReadAllBits(d.BitBufRange(d.Pos(), int64(inclLen)*8)) + bs := d.ReadAllBits(d.BitBufRange(d.Pos(), int64(inclLen)*8)) if fn, ok := linkToDecodeFn[linkType]; ok { // TODO: report decode errors diff --git a/format/pcap/pcapng.go b/format/pcap/pcapng.go index 7a1159df1..5aef621f9 100644 --- a/format/pcap/pcapng.go +++ b/format/pcap/pcapng.go @@ -230,7 +230,7 @@ var blockFns = map[uint64]func(d *decode.D, dc *decodeContext){ capturedLength := d.FieldU32("capture_packet_length") d.FieldU32("original_packet_length") - bs := d.MustReadAllBits(d.BitBufRange(d.Pos(), int64(capturedLength)*8)) + bs := d.ReadAllBits(d.BitBufRange(d.Pos(), int64(capturedLength)*8)) linkType := dc.interfaceTypes[int(interfaceID)] diff --git a/format/png/png.go b/format/png/png.go index 5393850f7..40245a0da 100644 --- a/format/png/png.go +++ b/format/png/png.go @@ -228,7 +228,7 @@ func pngDecode(d *decode.D, in any) any { }) chunkCRC := crc32.NewIEEE() - d.MustCopy(chunkCRC, bitio.NewIOReader(d.BitBufRange(crcStartPos, d.Pos()-crcStartPos))) + d.Copy(chunkCRC, bitio.NewIOReader(d.BitBufRange(crcStartPos, d.Pos()-crcStartPos))) d.FieldU32("crc", d.ValidateUBytes(chunkCRC.Sum(nil)), scalar.ActualHex) }) diff --git a/format/rtmp/rtmp.go b/format/rtmp/rtmp.go index 1b84c11e0..c24a68070 100644 --- a/format/rtmp/rtmp.go +++ b/format/rtmp/rtmp.go @@ -459,7 +459,7 @@ func rtmpDecode(d *decode.D, in any) any { } if payloadLength > 0 { - d.MustCopyBits(&m.b, d.FieldRawLen("data", payloadLength)) + d.CopyBits(&m.b, d.FieldRawLen("data", payloadLength)) } if m.l == uint64(m.b.Len()) { diff --git a/pkg/decode/decode.go b/pkg/decode/decode.go index a37a39575..80bd02ec8 100644 --- a/pkg/decode/decode.go +++ b/pkg/decode/decode.go @@ -210,49 +210,49 @@ func (d *D) FieldDecoder(name string, bitBuf bitio.ReaderAtSeeker, v any) *D { } } -func (d *D) CopyBits(w io.Writer, r bitio.Reader) (int64, error) { +func (d *D) TryCopyBits(w io.Writer, r bitio.Reader) (int64, error) { // TODO: what size? now same as io.Copy buf := d.SharedReadBuf(32 * 1024) return bitioextra.CopyBitsBuffer(w, r, buf) } -func (d *D) MustCopyBits(w io.Writer, r bitio.Reader) int64 { - n, err := d.CopyBits(w, r) +func (d *D) CopyBits(w io.Writer, r bitio.Reader) int64 { + n, err := d.TryCopyBits(w, r) if err != nil { - d.IOPanic(err, "MustCopy: Copy") + d.IOPanic(err, "CopyBits: Copy") } return n } -func (d *D) Copy(w io.Writer, r io.Reader) (int64, error) { +func (d *D) TryCopy(w io.Writer, r io.Reader) (int64, error) { // TODO: what size? now same as io.Copy buf := d.SharedReadBuf(32 * 1024) return io.CopyBuffer(w, r, buf) } -func (d *D) MustCopy(w io.Writer, r io.Reader) int64 { - n, err := d.Copy(w, r) +func (d *D) Copy(w io.Writer, r io.Reader) int64 { + n, err := d.TryCopy(w, r) if err != nil { - d.IOPanic(err, "MustCopy: Copy") + d.IOPanic(err, "Copy") } return n } -func (d *D) MustCloneReadSeeker(br bitio.ReadSeeker) bitio.ReadSeeker { +func (d *D) CloneReadSeeker(br bitio.ReadSeeker) bitio.ReadSeeker { br, err := bitio.CloneReadSeeker(br) if err != nil { - d.IOPanic(err, "MustClone") + d.IOPanic(err, "CloneReadSeeker") } return br } -func (d *D) MustNewBitBufFromReader(r io.Reader) bitio.ReaderAtSeeker { +func (d *D) NewBitBufFromReader(r io.Reader) bitio.ReaderAtSeeker { b := &bytes.Buffer{} - d.MustCopy(b, r) + d.Copy(b, r) return bitio.NewBitReader(b.Bytes(), -1) } -func (d *D) ReadAllBits(r bitio.Reader) ([]byte, error) { +func (d *D) TryReadAllBits(r bitio.Reader) ([]byte, error) { bb := &bytes.Buffer{} buf := d.SharedReadBuf(32 * 1024) if _, err := bitioextra.CopyBitsBuffer(bb, r, buf); err != nil { @@ -261,8 +261,8 @@ func (d *D) ReadAllBits(r bitio.Reader) ([]byte, error) { return bb.Bytes(), nil } -func (d *D) MustReadAllBits(r bitio.Reader) []byte { - buf, err := d.ReadAllBits(r) +func (d *D) ReadAllBits(r bitio.Reader) []byte { + buf, err := d.TryReadAllBits(r) if err != nil { d.IOPanic(err, "Bytes ReadAllBytes") } @@ -340,7 +340,7 @@ func (d *D) IOPanic(err error, op string) { } // Bits reads nBits bits from buffer -func (d *D) bits(nBits int) (uint64, error) { +func (d *D) TryBits(nBits int) (uint64, error) { if nBits < 0 || nBits > 64 { return 0, fmt.Errorf("nBits must be 0-64 (%d)", nBits) } @@ -355,12 +355,12 @@ func (d *D) bits(nBits int) (uint64, error) { } // Bits reads nBits bits from buffer -func (d *D) Bits(nBits int) (uint64, error) { - n, err := d.bits(nBits) +func (d *D) Bits(nBits int) uint64 { + n, err := d.TryBits(nBits) if err != nil { - return 0, err + panic(IOError{Err: err, Op: "Bits", ReadSize: int64(nBits), Pos: d.Pos()}) } - return n, nil + return n } func (d *D) PeekBits(nBits int) uint64 { @@ -419,7 +419,7 @@ func (d *D) TryPeekBits(nBits int) (uint64, error) { if err != nil { return 0, err } - n, err := d.bits(nBits) + n, err := d.TryBits(nBits) if _, err := d.bitBuf.SeekBits(start, io.SeekStart); err != nil { return 0, err } diff --git a/pkg/decode/read.go b/pkg/decode/read.go index 3661a41ca..893cb2627 100644 --- a/pkg/decode/read.go +++ b/pkg/decode/read.go @@ -20,7 +20,7 @@ func (d *D) tryUEndian(nBits int, endian Endian) (uint64, error) { if nBits < 0 { return 0, fmt.Errorf("tryUEndian nBits must be >= 0 (%d)", nBits) } - n, err := d.bits(nBits) + n, err := d.TryBits(nBits) if err != nil { return 0, err } @@ -88,7 +88,7 @@ func (d *D) tryFEndian(nBits int, endian Endian) (float64, error) { if nBits < 0 { return 0, fmt.Errorf("tryFEndian nBits must be >= 0 (%d)", nBits) } - n, err := d.bits(nBits) + n, err := d.TryBits(nBits) if err != nil { return 0, err } @@ -111,7 +111,7 @@ func (d *D) tryFPEndian(nBits int, fBits int, endian Endian) (float64, error) { if nBits < 0 { return 0, fmt.Errorf("tryFPEndian nBits must be >= 0 (%d)", nBits) } - n, err := d.bits(nBits) + n, err := d.TryBits(nBits) if err != nil { return 0, err } @@ -158,7 +158,7 @@ func (d *D) tryTextLenPrefixed(lenBits int, fixedBytes int, e encoding.Encoding) } p := d.Pos() - l, err := d.bits(lenBits) + l, err := d.TryBits(lenBits) if err != nil { return "", err } @@ -226,7 +226,7 @@ func (d *D) tryUnary(ov uint64) (uint64, error) { p := d.Pos() var n uint64 for { - b, err := d.bits(1) + b, err := d.TryBits(1) if err != nil { d.SeekAbs(p) return 0, err @@ -240,7 +240,7 @@ func (d *D) tryUnary(ov uint64) (uint64, error) { } func (d *D) tryBool() (bool, error) { - n, err := d.bits(1) + n, err := d.TryBits(1) if err != nil { return false, err }