Skip to content

Commit

Permalink
Wrap errors to fix some linter problems
Browse files Browse the repository at this point in the history
  • Loading branch information
GreyCat committed Mar 29, 2024
1 parent 1be0eec commit ed6b4df
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions kaitai/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,55 +87,55 @@ func (k *Stream) Pos() (int64, error) {
// ReadU1 reads 1 byte and returns this as uint8.
func (k *Stream) ReadU1() (v uint8, err error) {
if _, err = k.Read(k.buf[:1]); err != nil {
return 0, err
return 0, fmt.Errorf("ReadU1: error reading 1 byte: %w", err)
}
return k.buf[0], nil
}

// ReadU2be reads 2 bytes in big-endian order and returns those as uint16.
func (k *Stream) ReadU2be() (v uint16, err error) {
if _, err = k.Read(k.buf[:2]); err != nil {
return 0, err
return 0, fmt.Errorf("ReadU2be: error reading 2 bytes: %w", err)
}
return binary.BigEndian.Uint16(k.buf[:2]), nil
}

// ReadU4be reads 4 bytes in big-endian order and returns those as uint32.
func (k *Stream) ReadU4be() (v uint32, err error) {
if _, err = k.Read(k.buf[:4]); err != nil {
return 0, err
return 0, fmt.Errorf("ReadU4be: error reading 4 bytes: %w", err)
}
return binary.BigEndian.Uint32(k.buf[:4]), nil
}

// ReadU8be reads 8 bytes in big-endian order and returns those as uint64.
func (k *Stream) ReadU8be() (v uint64, err error) {
if _, err = k.Read(k.buf[:8]); err != nil {
return 0, err
return 0, fmt.Errorf("ReadU8be: error reading 8 bytes: %w", err)
}
return binary.BigEndian.Uint64(k.buf[:8]), nil
}

// ReadU2le reads 2 bytes in little-endian order and returns those as uint16.
func (k *Stream) ReadU2le() (v uint16, err error) {
if _, err = k.Read(k.buf[:2]); err != nil {
return 0, err
return 0, fmt.Errorf("ReadU2le: error reading 2 bytes: %w", err)
}
return binary.LittleEndian.Uint16(k.buf[:2]), nil
}

// ReadU4le reads 4 bytes in little-endian order and returns those as uint32.
func (k *Stream) ReadU4le() (v uint32, err error) {
if _, err = k.Read(k.buf[:4]); err != nil {
return 0, err
return 0, fmt.Errorf("ReadU4le: error reading 4 bytes: %w", err)
}
return binary.LittleEndian.Uint32(k.buf[:4]), nil
}

// ReadU8le reads 8 bytes in little-endian order and returns those as uint64.
func (k *Stream) ReadU8le() (v uint64, err error) {
if _, err = k.Read(k.buf[:8]); err != nil {
return 0, err
return 0, fmt.Errorf("ReadU8le: error reading 8 bytes: %w", err)
}
return binary.LittleEndian.Uint64(k.buf[:8]), nil
}
Expand Down Expand Up @@ -214,12 +214,19 @@ func (k *Stream) ReadBytes(n int) (b []byte, err error) {

b = make([]byte, n)
_, err = io.ReadFull(k, b)
return b, err
if err != nil {
return nil, fmt.Errorf("ReadBytes: error reading %d bytes: %w", n, err)
}
return b, nil
}

// ReadBytesFull reads all remaining bytes and returns those as a byte array.
func (k *Stream) ReadBytesFull() ([]byte, error) {
return ioutil.ReadAll(k)
res, err := ioutil.ReadAll(k)
if err != nil {
return nil, fmt.Errorf("ReadBytesFull: error reading all bytes: %w", err)
}
return res, nil
}

// ReadBytesPadTerm reads up to size bytes. pad bytes are discarded. It
Expand Down Expand Up @@ -319,7 +326,7 @@ func (k *Stream) ReadBitsIntBe(n int) (res uint64, err error) {
}
_, err = k.Read(k.buf[:bytesNeeded])
if err != nil {
return res, err
return res, fmt.Errorf("ReadBitsIntBe(%d): %w", n, err)
}
for i := 0; i < bytesNeeded; i++ {
res = res<<8 | uint64(k.buf[i])
Expand All @@ -335,7 +342,7 @@ func (k *Stream) ReadBitsIntBe(n int) (res uint64, err error) {
var mask uint64 = (1 << k.bitsLeft) - 1 // `bitsLeft` is in range 0..7
k.bits &= mask

return res, err
return res, nil
}

// ReadBitsInt reads n-bit integer in big-endian byte order and returns it as uint64.
Expand All @@ -360,7 +367,7 @@ func (k *Stream) ReadBitsIntLe(n int) (res uint64, err error) {
}
_, err = k.Read(k.buf[:bytesNeeded])
if err != nil {
return res, err
return res, fmt.Errorf("ReadBitsIntLe(%d): %w", n, err)
}
for i := 0; i < bytesNeeded; i++ {
res |= uint64(k.buf[i]) << (i * 8)
Expand All @@ -378,7 +385,7 @@ func (k *Stream) ReadBitsIntLe(n int) (res uint64, err error) {

var mask uint64 = (1 << n) - 1 // unlike some other languages, no problem with this in Go
res &= mask
return res, err
return res, nil
}

// ReadBitsArray is not implemented yet.
Expand Down

0 comments on commit ed6b4df

Please sign in to comment.