diff --git a/internal/data/reader.go b/internal/data/reader.go index e5c2c96..a1ddd05 100644 --- a/internal/data/reader.go +++ b/internal/data/reader.go @@ -16,15 +16,17 @@ type Reader struct { blockSizes []uint32 blockSize uint32 resetable bool + fileSize uint64 } -func NewReader(r io.Reader, d decompress.Decompressor, blockSizes []uint32, blockSize uint32) *Reader { +func NewReader(r io.Reader, d decompress.Decompressor, blockSizes []uint32, blockSize uint32, fileSize uint64) *Reader { return &Reader{ d: d, master: r, blockSizes: blockSizes, blockSize: blockSize, resetable: true, + fileSize: fileSize, } } @@ -49,7 +51,11 @@ func (r *Reader) advance() (err error) { } else { size := realSize(r.blockSizes[0]) if size == 0 { - r.cur = bytes.NewReader(make([]byte, r.blockSize)) + outSize := r.blockSize + if r.fileSize < uint64(r.blockSize) { + outSize = uint32(r.fileSize) + } + r.cur = bytes.NewReader(make([]byte, outSize)) } else { r.cur = io.LimitReader(r.master, int64(size)) if size == r.blockSizes[0] { diff --git a/reader_inode.go b/reader_inode.go index 84c9641..567d52b 100644 --- a/reader_inode.go +++ b/reader_inode.go @@ -54,7 +54,7 @@ func (r Reader) getReaders(i inode.Inode) (full *data.FullReader, rdr *data.Read } else { return nil, nil, errors.New("getReaders called on non-file type") } - rdr = data.NewReader(toreader.NewReader(r.r, int64(blockOffset)), r.d, blockSizes, r.s.BlockSize) + rdr = data.NewReader(toreader.NewReader(r.r, int64(blockOffset)), r.d, blockSizes, r.s.BlockSize, fileSize) full = data.NewFullReader(r.r, uint64(blockOffset), r.d, blockSizes, r.s.BlockSize, fileSize) if fragInd != 0xFFFFFFFF { full.AddFragment(func() (io.Reader, error) {