diff --git a/file.go b/file.go index 49b91ee..94c308a 100644 --- a/file.go +++ b/file.go @@ -116,6 +116,9 @@ func (r *Reader) setDirEntries() error { func fixFile(v uint16, f *File) { fixName(f) + if f.objectType != stream { + return + } // if the MSCFB major version is 4, then this can be a uint64 otherwise is a uint32 and the least signficant bits can contain junk if v > 3 { f.Size = int64(binary.LittleEndian.Uint64(f.streamSize[:])) @@ -232,7 +235,7 @@ func (f *File) Modified() time.Time { // Read this directory entry // Returns 0, io.EOF if no stream is available (i.e. for a storage object) func (f *File) Read(b []byte) (int, error) { - if f.objectType != stream || f.Size < 1 || f.i >= f.Size { + if f.Size < 1 || f.i >= f.Size { return 0, io.EOF } sz := len(b) @@ -272,7 +275,7 @@ func (f *File) Read(b []byte) (int, error) { // Depends on the io.ReaderAt supplied to mscfb.New() being a WriterAt too // Returns 0, io.EOF if no stream is available (i.e. for a storage object) func (f *File) Write(b []byte) (int, error) { - if f.objectType != stream || f.Size < 1 || f.i >= f.Size { + if f.Size < 1 || f.i >= f.Size { return 0, io.EOF } if f.r.wa == nil { diff --git a/mscfb_test.go b/mscfb_test.go index 2bd8181..f93e1c9 100644 --- a/mscfb_test.go +++ b/mscfb_test.go @@ -143,6 +143,21 @@ func TestXls(t *testing.T) { testFile(t, testXls) } +func TestSeek(t *testing.T) { + file, _ := os.Open(testXls) + defer file.Close() + doc, _ := New(file) + // the first entry in the XLS file is 2719 bytes + if doc.File[3].Size != 2719 { + t.Fatalf("Expecting the third entry of the XLS file to be 2719 bytes long; it is %d", doc.File[3].Size) + } + buf := make([]byte, 2719) + i, err := doc.File[3].Read(buf) + if i != 2719 || err != nil { + t.Fatalf("Expecting 2719 length and an EOF; got %d and %v", i, err) + } +} + func TestWrite(t *testing.T) { file, err := os.OpenFile(testXls, os.O_RDWR, 0666) if err != nil {