Skip to content

Commit

Permalink
decode: Cleanup io panic a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Apr 12, 2024
1 parent d04a846 commit ee2ee24
Show file tree
Hide file tree
Showing 6 changed files with 870 additions and 870 deletions.
4 changes: 2 additions & 2 deletions format/asn1/asn1_ber.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func decodeASN1BERValue(d *decode.D, bib *bitio.Buffer, sb *strings.Builder, par
if bib != nil {
// TODO: helper?
if _, err := bitio.Copy(bib, br); err != nil {
d.IOPanic(err, "bitio.Copy")
d.IOPanic(err, "value", "bitio.Copy")
}
}
if unusedBitsCount > 0 {
Expand All @@ -292,7 +292,7 @@ func decodeASN1BERValue(d *decode.D, bib *bitio.Buffer, sb *strings.Builder, par
if bib != nil {
// TODO: helper?
if _, err := bitio.Copy(bib, br); err != nil {
d.IOPanic(err, "bitio.Copy")
d.IOPanic(err, "value", "bitio.Copy")
}
}
case class == classUniversal && tag == universalTypeNull:
Expand Down
4 changes: 2 additions & 2 deletions format/gzip/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func gzipDecodeMember(d *decode.D) bitio.ReaderAtSeeker {
readCompressedSize, uncompressedBR, err =
d.FieldReaderRange("uncompressed", d.Pos(), d.BitsLeft(), rFn)
if err != nil {
d.IOPanic(err, "TryFieldReaderRange")
d.IOPanic(err, "uncompressed", "FieldReaderRange")
}
d.FieldRawLen("compressed", readCompressedSize)
crc32W := crc32.NewIEEE()
Expand Down Expand Up @@ -149,7 +149,7 @@ func gzipDecode(d *decode.D) any {

cbr, err := bitio.NewMultiReader(brs...)
if err != nil {
d.IOPanic(err, "NewMultiReader")
d.IOPanic(err, "members", "NewMultiReader")
}
dv, _, _ := d.TryFieldFormatBitBuf("uncompressed", cbr, &probeGroup, format.Probe_In{})
if dv == nil {
Expand Down
2 changes: 1 addition & 1 deletion format/id3/id3v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func textNullLenFn(encoding int, notFoundFixedBytes int) func(d *decode.D) strin
func(v uint64) bool { return v == 0 },
)
if err != nil {
d.IOPanic(err, "textNullLenFn")
d.IOPanic(err, "", "textNullLenFn")
}
if offset < 0 {
if notFoundFixedBytes < 0 {
Expand Down
50 changes: 25 additions & 25 deletions pkg/decode/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (d *D) TryCopyBits(w io.Writer, r bitio.Reader) (int64, error) {
func (d *D) CopyBits(w io.Writer, r bitio.Reader) int64 {
n, err := d.TryCopyBits(w, r)
if err != nil {
d.IOPanic(err, "CopyBits: Copy")
d.IOPanic(err, "", "CopyBits")
}
return n
}
Expand All @@ -277,15 +277,15 @@ func (d *D) TryCopy(w io.Writer, r io.Reader) (int64, error) {
func (d *D) Copy(w io.Writer, r io.Reader) int64 {
n, err := d.TryCopy(w, r)
if err != nil {
d.IOPanic(err, "Copy")
d.IOPanic(err, "", "Copy")
}
return n
}

func (d *D) CloneReadSeeker(br bitio.ReadSeeker) bitio.ReadSeeker {
br, err := bitio.CloneReadSeeker(br)
if err != nil {
d.IOPanic(err, "CloneReadSeeker")
d.IOPanic(err, "", "CloneReadSeeker")
}
return br
}
Expand All @@ -308,7 +308,7 @@ func (d *D) TryReadAllBits(r bitio.Reader) ([]byte, error) {
func (d *D) ReadAllBits(r bitio.Reader) []byte {
buf, err := d.TryReadAllBits(r)
if err != nil {
d.IOPanic(err, "Bytes ReadAllBytes")
d.IOPanic(err, "", "ReadAllBytes")
}
return buf
}
Expand Down Expand Up @@ -350,7 +350,7 @@ func (d *D) FillGaps(r ranges.Range, namePrefix string) {
for i, gap := range gaps {
br, err := bitiox.Range(d.bitBuf, gap.Start, gap.Len)
if err != nil {
d.IOPanic(err, "FillGaps: Range")
d.IOPanic(err, namePrefix, "bitiox.Range")
}

v := &Value{
Expand Down Expand Up @@ -380,8 +380,8 @@ func (d *D) Fatalf(format string, a ...any) {
panic(DecoderError{Reason: fmt.Sprintf(format, a...), Pos: d.Pos()})
}

func (d *D) IOPanic(err error, op string) {
panic(IOError{Err: err, Pos: d.Pos(), Op: op})
func (d *D) IOPanic(err error, name string, op string) {
panic(IOError{Err: err, Name: name, Pos: d.Pos(), Op: op})
}

// TryBits reads nBits bits from buffer
Expand Down Expand Up @@ -543,7 +543,7 @@ func (d *D) TryPeekFind(nBits int, seekBits int64, maxLen int64, fn func(v uint6
func (d *D) PeekFind(nBits int, seekBits int64, maxLen int64, fn func(v uint64) bool) (int64, uint64) {
peekBits, v, err := d.TryPeekFind(nBits, seekBits, maxLen, fn)
if err != nil {
d.IOPanic(err, "PeekFind: TryPeekFind")
d.IOPanic(err, "", "PeekFind")
}
if peekBits == -1 {
d.Errorf("peek not found")
Expand Down Expand Up @@ -771,7 +771,7 @@ func (d *D) TrySeekRel(delta int64, fns ...func(d *D)) (int64, error) {
func (d *D) SeekRel(delta int64, fns ...func(d *D)) int64 {
n, err := d.trySeekAbs(d.Pos()+delta, fns...)
if err != nil {
d.IOPanic(err, "SeekRel")
d.IOPanic(err, "", "SeekRel")
}
return n
}
Expand All @@ -785,7 +785,7 @@ func (d *D) TrySeekAbs(pos int64, fns ...func(d *D)) (int64, error) {
func (d *D) SeekAbs(pos int64, fns ...func(d *D)) int64 {
n, err := d.trySeekAbs(pos, fns...)
if err != nil {
d.IOPanic(err, "SeekAbs")
d.IOPanic(err, "", "SeekAbs")
}
return n
}
Expand Down Expand Up @@ -955,7 +955,7 @@ func (d *D) RangeFn(firstBit int64, nBits int64, fn func(d *D)) int64 {
// TODO: do some kind of DecodeLimitedLen/RangeFn?
br := d.BitBufRange(0, firstBit+nBits)
if _, err := br.SeekBits(firstBit, io.SeekStart); err != nil {
d.IOPanic(err, "RangeFn: SeekAbs")
d.IOPanic(err, "", "RangeFn: SeekAbs")
}

nd := *d
Expand All @@ -979,7 +979,7 @@ func (d *D) Format(group *Group, inArg any) any {
ReadBuf: d.readBuf,
})
if dv == nil || dv.Errors() != nil {
d.IOPanic(err, "Format: decode")
d.IOPanic(err, "", "Format: decode")
}

switch vv := dv.V.(type) {
Expand All @@ -992,7 +992,7 @@ func (d *D) Format(group *Group, inArg any) any {
}

if _, err := d.bitBuf.SeekBits(dv.Range.Len, io.SeekCurrent); err != nil {
d.IOPanic(err, "Format: SeekRel")
d.IOPanic(err, "", "Format: SeekRel")
}

return v
Expand All @@ -1015,7 +1015,7 @@ func (d *D) TryFieldFormat(name string, group *Group, inArg any) (*Value, any, e

d.AddChild(dv)
if _, err := d.bitBuf.SeekBits(dv.Range.Len, io.SeekCurrent); err != nil {
d.IOPanic(err, "TryFieldFormat: SeekRel")
d.IOPanic(err, name, "TryFieldFormat: SeekRel")
}

return dv, v, err
Expand All @@ -1024,7 +1024,7 @@ func (d *D) TryFieldFormat(name string, group *Group, inArg any) (*Value, any, e
func (d *D) FieldFormat(name string, group *Group, inArg any) (*Value, any) {
dv, v, err := d.TryFieldFormat(name, group, inArg)
if dv == nil || dv.Errors() != nil {
d.IOPanic(err, "FieldFormat: TryFieldFormat")
d.IOPanic(err, name, "FieldFormat: TryFieldFormat")
}
return dv, v
}
Expand Down Expand Up @@ -1054,7 +1054,7 @@ func (d *D) TryFieldFormatLen(name string, nBits int64, group *Group, inArg any)

d.AddChild(dv)
if _, err := d.bitBuf.SeekBits(nBits, io.SeekCurrent); err != nil {
d.IOPanic(err, "TryFieldFormatLen: SeekRel")
d.IOPanic(err, name, "TryFieldFormatLen: SeekRel")
}

return dv, v, err
Expand All @@ -1063,7 +1063,7 @@ func (d *D) TryFieldFormatLen(name string, nBits int64, group *Group, inArg any)
func (d *D) FieldFormatLen(name string, nBits int64, group *Group, inArg any) (*Value, any) {
dv, v, err := d.TryFieldFormatLen(name, nBits, group, inArg)
if dv == nil || dv.Errors() != nil {
d.IOPanic(err, "FieldFormatLen: TryFieldFormatLen")
d.IOPanic(err, name, "FieldFormatLen: TryFieldFormatLen")
}
return dv, v
}
Expand Down Expand Up @@ -1100,7 +1100,7 @@ func (d *D) TryFieldFormatRange(name string, firstBit int64, nBits int64, group
func (d *D) FieldFormatRange(name string, firstBit int64, nBits int64, group *Group, inArg any) (*Value, any) {
dv, v, err := d.TryFieldFormatRange(name, firstBit, nBits, group, inArg)
if dv == nil || dv.Errors() != nil {
d.IOPanic(err, "FieldFormatRange: TryFieldFormatRange")
d.IOPanic(err, name, "FieldFormatRange: TryFieldFormatRange")
}

return dv, v
Expand Down Expand Up @@ -1130,7 +1130,7 @@ func (d *D) TryFieldFormatBitBuf(name string, br bitio.ReaderAtSeeker, group *Gr
func (d *D) FieldFormatBitBuf(name string, br bitio.ReaderAtSeeker, group *Group, inArg any) (*Value, any) {
dv, v, err := d.TryFieldFormatBitBuf(name, br, group, inArg)
if dv == nil || dv.Errors() != nil {
d.IOPanic(err, "FieldFormatBitBuf: TryFieldFormatBitBuf")
d.IOPanic(err, name, "FieldFormatBitBuf: TryFieldFormatBitBuf")
}

return dv, v
Expand All @@ -1141,7 +1141,7 @@ func (d *D) FieldFormatBitBuf(name string, br bitio.ReaderAtSeeker, group *Group
func (d *D) FieldRootBitBuf(name string, br bitio.ReaderAtSeeker, sms ...scalar.BitBufMapper) *Value {
brLen, err := bitiox.Len(br)
if err != nil {
d.IOPanic(err, "br Len")
d.IOPanic(err, name, "br Len")
}

v := &Value{}
Expand Down Expand Up @@ -1188,17 +1188,17 @@ func (d *D) FieldStructRootBitBufFn(name string, br bitio.ReaderAtSeeker, fn fun
func (d *D) FieldFormatReaderLen(name string, nBits int64, fn func(r io.Reader) (io.ReadCloser, error), group *Group) (*Value, any) {
br, err := d.TryBitBufLen(nBits)
if err != nil {
d.IOPanic(err, "FieldFormatReaderLen: BitBufLen")
d.IOPanic(err, name, "FieldFormatReaderLen: BitBufLen")
}

bbBR := bitio.NewIOReader(br)
r, err := fn(bbBR)
if err != nil {
d.IOPanic(err, "FieldFormatReaderLen: fn")
d.IOPanic(err, name, "FieldFormatReaderLen: fn")
}
rBuf, err := io.ReadAll(r)
if err != nil {
d.IOPanic(err, "FieldFormatReaderLen: ReadAll")
d.IOPanic(err, name, "FieldFormatReaderLen: ReadAll")
}
rBR := bitio.NewBitReader(rBuf, -1)

Expand Down Expand Up @@ -1245,7 +1245,7 @@ func (d *D) FieldReaderRange(name string, startBit int64, nBits int64, fn func(r
func (d *D) FieldReaderRangeFormat(name string, startBit int64, nBits int64, fn func(r io.Reader) io.Reader, group *Group, inArg any) (int64, bitio.ReaderAtSeeker, *Value, any) {
cz, rBR, dv, v, err := d.TryFieldReaderRangeFormat(name, startBit, nBits, fn, group, inArg)
if err != nil {
d.IOPanic(err, "TryFieldReaderRangeFormat")
d.IOPanic(err, name, "TryFieldReaderRangeFormat")
}
return cz, rBR, dv, v
}
Expand All @@ -1268,7 +1268,7 @@ func (d *D) TryFieldValue(name string, fn func() (*Value, error)) (*Value, error
func (d *D) FieldValue(name string, fn func() *Value) *Value {
v, err := d.TryFieldValue(name, func() (*Value, error) { return fn(), nil })
if err != nil {
d.IOPanic(err, "FieldValue: TryFieldValue")
d.IOPanic(err, name, "FieldValue: TryFieldValue")
}
return v
}
Expand Down
Loading

0 comments on commit ee2ee24

Please sign in to comment.