diff --git a/decode_reader.go b/decode_reader.go index 19a3dbd..456120d 100644 --- a/decode_reader.go +++ b/decode_reader.go @@ -143,6 +143,9 @@ func (d *decodeReader) copyBytes(length, offset int) { d.win[d.w] = d.win[i] d.w++ i++ + if i >= d.size { + i = 0 + } length-- } } diff --git a/decode_reader_test.go b/decode_reader_test.go new file mode 100644 index 0000000..cf293df --- /dev/null +++ b/decode_reader_test.go @@ -0,0 +1,15 @@ +package rardecode + +import "testing" + +func TestCopyBytes_SourceIndexWraps(t *testing.T) { + d := &decodeReader{size: 8, win: make([]byte, 8)} + d.win[6] = 0xAA + d.win[7] = 0xBB + d.w = 4 + // offset=6: source starts at (4-6)%8 = 6, wraps past 7 to 0 + d.copyBytes(4, 6) + if d.win[4] != 0xAA || d.win[5] != 0xBB { + t.Errorf("first two bytes wrong: got %x %x, want AA BB", d.win[4], d.win[5]) + } +}