diff --git a/reader_legacy.go b/reader_legacy.go index 846c3db4..73865c09 100644 --- a/reader_legacy.go +++ b/reader_legacy.go @@ -16,15 +16,16 @@ type ReaderLegacy struct { // It provides the number of bytes read. OnBlockDone func(size int) - buf [8]byte // Scrap buffer. - pos int64 // Current position in src. - src io.Reader // Source. - zdata []byte // Compressed data. - data []byte // Uncompressed data. - idx int // Index of unread bytes into data. - checksum xxh32.XXHZero // Frame hash. - skip int64 // Bytes to skip before next read. - dpos int64 // Position in dest + lastBlock bool + buf [8]byte // Scrap buffer. + pos int64 // Current position in src. + src io.Reader // Source. + zdata []byte // Compressed data. + data []byte // Uncompressed data. + idx int // Index of unread bytes into data. + checksum xxh32.XXHZero // Frame hash. + skip int64 // Bytes to skip before next read. + dpos int64 // Position in dest } // NewReaderLegacy returns a new LZ4Demo frame decoder. @@ -38,6 +39,7 @@ func NewReaderLegacy(src io.Reader) *ReaderLegacy { // Skippable frames are supported even as a first frame although the LZ4 // specifications recommends skippable frames not to be used as first frames. func (z *ReaderLegacy) readLegacyHeader() error { + z.lastBlock = false magic, err := z.readUint32() if err != nil { z.pos += 4 @@ -81,8 +83,6 @@ func (z *ReaderLegacy) readLegacyHeader() error { // change between calls to Read(). If that is the case, no data is actually read from // the underlying io.Reader, to allow for potential input buffer resizing. func (z *ReaderLegacy) Read(buf []byte) (int, error) { - lastBlock := false - if debugFlag { debug("Read buf len=%d", len(buf)) } @@ -151,7 +151,7 @@ func (z *ReaderLegacy) Read(buf []byte) (int, error) { // Legacy blocks are fixed to 8MB, if we see a block smaller than this it means we've reached the end... if n < blockSize4M*2 { - lastBlock = true + z.lastBlock = true } } @@ -159,9 +159,9 @@ func (z *ReaderLegacy) Read(buf []byte) (int, error) { z.idx += n z.dpos += int64(n) if debugFlag { - debug("copied %d bytes to input", n) + debug("%v] copied %d bytes to input (%d:%d)", z.lastBlock, n, z.idx, len(z.data)) } - if lastBlock { + if z.lastBlock && len(z.data) == z.idx { return n, io.EOF } return n, nil diff --git a/reader_legacy_test.go b/reader_legacy_test.go index 6ee298d9..e15b1a0b 100644 --- a/reader_legacy_test.go +++ b/reader_legacy_test.go @@ -16,6 +16,7 @@ import ( func TestReaderLegacy(t *testing.T) { goldenFiles := []string{ "testdata/vmlinux_LZ4_19377.lz4", + "testdata/bzImage_lz4_isolated.lz4", } for _, fname := range goldenFiles { @@ -29,34 +30,30 @@ func TestReaderLegacy(t *testing.T) { t.Fatal(err) } - /* - f, err := os.Open(fname) - if err != nil { - t.Fatal(err) - } - defer f.Close() - + f, err := os.Open(fname) + if err != nil { + t.Fatal(err) + } + defer f.Close() + zr := lz4.NewReaderLegacy(f) + n, err := io.Copy(&out, zr) + if err != nil { + t.Fatal(err, n) + } - var out bytes.Buffer - zr := lz4.NewReaderLegacy(f) - n, err := io.Copy(&out, zr) - if err != nil { - t.Fatal(err) - } + if got, want := int(n), len(raw); got != want { + t.Errorf("invalid sizes: got %d; want %d", got, want) + } - if got, want := int(n), len(raw); got != want { - t.Errorf("invalid sizes: got %d; want %d", got, want) - } + if got, want := out.Bytes(), raw; !reflect.DeepEqual(got, want) { + t.Fatal("uncompressed data does not match original") + } - if got, want := out.Bytes(), raw; !reflect.DeepEqual(got, want) { - t.Fatal("uncompressed data does not match original") - } + if len(raw) < 20 { + return + } - if len(raw) < 20 { - return - } - */ f2, err := os.Open(fname) if err != nil { t.Fatal(err) @@ -64,7 +61,7 @@ func TestReaderLegacy(t *testing.T) { defer f2.Close() out.Reset() - zr := lz4.NewReaderLegacy(f2) + zr = lz4.NewReaderLegacy(f2) _, err = io.CopyN(&out, zr, 10) if err != nil { t.Fatal(err) diff --git a/testdata/bzImage_lz4_isolated b/testdata/bzImage_lz4_isolated new file mode 100644 index 00000000..e9b8360f Binary files /dev/null and b/testdata/bzImage_lz4_isolated differ diff --git a/testdata/bzImage_lz4_isolated.lz4 b/testdata/bzImage_lz4_isolated.lz4 new file mode 100644 index 00000000..89c7275e Binary files /dev/null and b/testdata/bzImage_lz4_isolated.lz4 differ diff --git a/testdata/vmlinux_LZ4_19377 b/testdata/vmlinux_LZ4_19377 new file mode 100644 index 00000000..16a593d9 Binary files /dev/null and b/testdata/vmlinux_LZ4_19377 differ diff --git a/testdata/vmlinux_LZ4_19377.lz4 b/testdata/vmlinux_LZ4_19377.lz4 new file mode 100644 index 00000000..81dca8ab Binary files /dev/null and b/testdata/vmlinux_LZ4_19377.lz4 differ