From 87f7533a175de1a37865801a2bbcfb0d5e4bbc16 Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Fri, 11 Aug 2023 15:32:52 -0500 Subject: [PATCH] Fix Error decompressing files with lots of NULLs #24 --- go.mod | 8 ++++---- go.sum | 12 ++++++------ internal/data/fullreader.go | 10 ++++++++-- reader_inode.go | 5 ++++- squashfs_test.go | 2 +- 5 files changed, 23 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index b121f5a..8599653 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,14 @@ module github.com/CalebQ42/squashfs -go 1.20 +go 1.21 require ( github.com/CalebQ42/fuse v0.1.0 - github.com/klauspost/compress v1.16.4 - github.com/pierrec/lz4/v4 v4.1.17 + github.com/klauspost/compress v1.16.7 + github.com/pierrec/lz4/v4 v4.1.18 github.com/rasky/go-lzo v0.0.0-20200203143853-96a758eda86e github.com/seaweedfs/fuse v1.2.2 github.com/therootcompany/xz v1.0.1 github.com/ulikunitz/xz v0.5.11 - golang.org/x/sys v0.7.0 + golang.org/x/sys v0.11.0 ) diff --git a/go.sum b/go.sum index a29f5ba..6e8a10b 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,9 @@ github.com/CalebQ42/fuse v0.1.0 h1:KLCNjun7zcd2kBNVFfH+SWJyhuwJdE0nhw5/q8K8HGQ= github.com/CalebQ42/fuse v0.1.0/go.mod h1:pJpoKG03HJKVhsp8o0YQYqmfbFsr3Eowt90yQGQVO+4= -github.com/klauspost/compress v1.16.4 h1:91KN02FnsOYhuunwU4ssRe8lc2JosWmizWa91B5v1PU= -github.com/klauspost/compress v1.16.4/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= -github.com/pierrec/lz4/v4 v4.1.17 h1:kV4Ip+/hUBC+8T6+2EgburRtkE9ef4nbY3f4dFhGjMc= -github.com/pierrec/lz4/v4 v4.1.17/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= +github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= +github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/pierrec/lz4/v4 v4.1.18 h1:xaKrnTkyoqfh1YItXl56+6KJNVYWlEEPuAQW9xsplYQ= +github.com/pierrec/lz4/v4 v4.1.18/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= github.com/rasky/go-lzo v0.0.0-20200203143853-96a758eda86e h1:dCWirM5F3wMY+cmRda/B1BiPsFtmzXqV9b0hLWtVBMs= github.com/rasky/go-lzo v0.0.0-20200203143853-96a758eda86e/go.mod h1:9leZcVcItj6m9/CfHY5Em/iBrCz7js8LcRQGTKEEv2M= github.com/seaweedfs/fuse v1.2.2 h1:01l8OjIdyATRNqVc/gDPgFobuC8ubQF3hRKOPColROw= @@ -12,5 +12,5 @@ github.com/therootcompany/xz v1.0.1 h1:CmOtsn1CbtmyYiusbfmhmkpAAETj0wBIH6kCYaX+x github.com/therootcompany/xz v1.0.1/go.mod h1:3K3UH1yCKgBneZYhuQUvJ9HPD19UEXEI0BWbMn8qNMY= github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8= github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= -golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= -golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/internal/data/fullreader.go b/internal/data/fullreader.go index 415a766..2630357 100644 --- a/internal/data/fullreader.go +++ b/internal/data/fullreader.go @@ -14,15 +14,17 @@ type FullReader struct { sizes []uint32 blockSize uint32 start uint64 + fileSize uint64 } -func NewFullReader(r io.ReaderAt, start uint64, d decompress.Decompressor, blockSizes []uint32, blockSize uint32) *FullReader { +func NewFullReader(r io.ReaderAt, start uint64, d decompress.Decompressor, blockSizes []uint32, blockSize uint32, fileSize uint64) *FullReader { return &FullReader{ r: r, start: start, blockSize: blockSize, sizes: blockSizes, d: d, + fileSize: fileSize, } } @@ -43,10 +45,14 @@ func (r FullReader) process(index int, offset int64, out chan outDat) { var rdr io.ReadCloser size := realSize(r.sizes[index]) if size == 0 { + outSize := r.blockSize + if r.fileSize < uint64(r.blockSize) { + outSize = uint32(r.fileSize) + } out <- outDat{ i: index, err: nil, - data: make([]byte, r.blockSize), + data: make([]byte, outSize), } return } diff --git a/reader_inode.go b/reader_inode.go index 0a8e11d..84c9641 100644 --- a/reader_inode.go +++ b/reader_inode.go @@ -36,23 +36,26 @@ func (r Reader) getReaders(i inode.Inode) (full *data.FullReader, rdr *data.Read var blockSizes []uint32 var fragInd uint32 var fragSize uint32 + var fileSize uint64 if i.Type == inode.Fil { fragOffset = uint64(i.Data.(inode.File).FragOffset) blockOffset = uint64(i.Data.(inode.File).BlockStart) blockSizes = i.Data.(inode.File).BlockSizes fragInd = i.Data.(inode.File).FragInd fragSize = i.Data.(inode.File).Size % r.s.BlockSize + fileSize = uint64(i.Data.(inode.File).Size) } else if i.Type == inode.EFil { fragOffset = uint64(i.Data.(inode.EFile).FragOffset) blockOffset = i.Data.(inode.EFile).BlockStart blockSizes = i.Data.(inode.EFile).BlockSizes fragInd = i.Data.(inode.EFile).FragInd fragSize = uint32(i.Data.(inode.EFile).Size % uint64(r.s.BlockSize)) + fileSize = i.Data.(inode.EFile).Size } 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) - full = data.NewFullReader(r.r, uint64(blockOffset), r.d, blockSizes, r.s.BlockSize) + full = data.NewFullReader(r.r, uint64(blockOffset), r.d, blockSizes, r.s.BlockSize, fileSize) if fragInd != 0xFFFFFFFF { full.AddFragment(func() (io.Reader, error) { var fragRdr io.Reader diff --git a/squashfs_test.go b/squashfs_test.go index faeb1c7..4d42960 100644 --- a/squashfs_test.go +++ b/squashfs_test.go @@ -19,7 +19,7 @@ import ( const ( squashfsURL = "https://darkstorm.tech/files/LinuxPATest.sfs" - squashfsName = "LinuxPATest.sfs" + squashfsName = "bug.sqfs" filePath = "PortableApps/Notepad++Portable/App/DefaultData/Config/contextMenu.xml" )