Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (r *BinaryReader) Seek(off int64, whence int) (int64, error) {
if off < -r.f.Len() || 0 < off {
return 0, fmt.Errorf("invalid offset")
}
r.pos = r.f.Len() - off
r.pos = r.f.Len() + off
} else {
return 0, fmt.Errorf("invalid whence")
}
Expand Down Expand Up @@ -439,7 +439,8 @@ func (r *BinaryReader) ReadInt16() int16 {

// ReadInt24 reads a int24 into an int32.
func (r *BinaryReader) ReadInt24() int32 {
return int32(r.ReadUint24())
// no int24 to convert through, so sign-extend from bit 23
return int32(r.ReadUint24()<<8) >> 8
}

// ReadInt32 reads a int32.
Expand Down
28 changes: 28 additions & 0 deletions binary_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parse

import (
"io"
"testing"

"github.com/tdewolff/test"
Expand All @@ -23,3 +24,30 @@ func TestBinaryReaderFullRead(t *testing.T) {
test.T(t, NewBinaryReaderBytes([]byte{1, 2, 3, 4}).ReadUint32(), uint32(0x01020304))
test.T(t, NewBinaryReaderBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8}).ReadUint64(), uint64(0x0102030405060708))
}

func TestBinaryReaderSeekEnd(t *testing.T) {
// io.SeekEnd counts back from the end, so a negative offset moves earlier.
buf := []byte{1, 2, 3, 4, 5, 6, 7, 8}
for _, tt := range []struct {
off int64
want int64
}{{0, 8}, {-1, 7}, {-4, 4}, {-8, 0}} {
r := NewBinaryReaderBytes(buf)
pos, err := r.Seek(tt.off, io.SeekEnd)
test.T(t, err, nil)
test.T(t, pos, tt.want)
}

r := NewBinaryReaderBytes(buf)
_, err := r.Seek(-4, io.SeekEnd)
test.T(t, err, nil)
test.T(t, r.ReadUint32(), uint32(0x05060708))
}

func TestBinaryReaderInt24(t *testing.T) {
for _, v := range []int32{-1, -2, -8388608, 0, 1, 8388607} {
w := NewBinaryWriter(nil)
w.WriteInt24(v)
test.T(t, NewBinaryReaderBytes(w.Bytes()).ReadInt24(), v)
}
}