Skip to content

Commit

Permalink
set File size to 0 if not a stream
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlehane committed May 19, 2016
1 parent 9d34deb commit ac11bba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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[:]))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions mscfb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ac11bba

Please sign in to comment.