Skip to content

Commit

Permalink
Fixing detection of last frame and when to return io.EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
Caesurus committed Oct 21, 2020
1 parent d06f405 commit a9d291e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 38 deletions.
28 changes: 14 additions & 14 deletions reader_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -151,17 +151,17 @@ 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
}
}

n := copy(buf, z.data[z.idx:])
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
Expand Down
45 changes: 21 additions & 24 deletions reader_legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -29,42 +30,38 @@ 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)
}
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)
Expand Down
Binary file added testdata/bzImage_lz4_isolated
Binary file not shown.
Binary file added testdata/bzImage_lz4_isolated.lz4
Binary file not shown.
Binary file added testdata/vmlinux_LZ4_19377
Binary file not shown.
Binary file added testdata/vmlinux_LZ4_19377.lz4
Binary file not shown.

0 comments on commit a9d291e

Please sign in to comment.