diff --git a/xls/cfb/cfb.go b/xls/cfb/cfb.go index 13ce1cd..f9daf44 100644 --- a/xls/cfb/cfb.go +++ b/xls/cfb/cfb.go @@ -292,7 +292,7 @@ func (d *doc) getStreamReader(sid uint32, size uint64) io.Reader { panic("incomplete read") } - return &sliceReader{data: streamData, offset: 0} + return &SliceReader{Data: streamData} } func (d *doc) getMiniStreamReader(sid uint32, size uint64) io.Reader { @@ -341,5 +341,5 @@ func (d *doc) getMiniStreamReader(sid uint32, size uint64) io.Reader { sid = d.minifat[sid] } - return &sliceReader{data: streamData, offset: 0} + return &SliceReader{Data: streamData} } diff --git a/xls/cfb/simple_test.go b/xls/cfb/simple_test.go index 76e7030..3256e39 100644 --- a/xls/cfb/simple_test.go +++ b/xls/cfb/simple_test.go @@ -9,7 +9,7 @@ import ( func TestHeader(t *testing.T) { d := &doc{} - f, _ := os.Open("test.xls") + f, _ := os.Open("../testdata/test.xls") err := d.load(f) if err != nil { t.Fatal(err) @@ -18,7 +18,7 @@ func TestHeader(t *testing.T) { func TestHeader2(t *testing.T) { d := &doc{} - f, _ := os.Open("test2.xls") + f, _ := os.Open("../testdata/test2.xls") err := d.load(f) if err != nil { t.Fatal(err) @@ -27,7 +27,7 @@ func TestHeader2(t *testing.T) { func TestHeader3(t *testing.T) { d := &doc{} - f, _ := os.Open("test3.xls") + f, _ := os.Open("../testdata/test3.xls") err := d.load(f) if err != nil { t.Fatal(err) @@ -36,7 +36,7 @@ func TestHeader3(t *testing.T) { func TestHeader4(t *testing.T) { d := &doc{} - f, _ := os.Open("test4.xls") + f, _ := os.Open("../testdata/test4.xls") err := d.load(f) if err != nil { t.Fatal(err) diff --git a/xls/cfb/slicereader.go b/xls/cfb/slicereader.go index 4fd3994..6f637e4 100644 --- a/xls/cfb/slicereader.go +++ b/xls/cfb/slicereader.go @@ -1,25 +1,28 @@ package cfb -import "io" +import ( + "io" +) -type sliceReader struct { - data [][]byte - offset uint +type SliceReader struct { + Data [][]byte + Index uint + Offset uint } -func (s *sliceReader) Read(b []byte) (int, error) { - var err error - if s.offset >= uint(len(s.data)) { +func (s *SliceReader) Read(b []byte) (int, error) { + if s.Index >= uint(len(s.Data)) { return 0, io.EOF } - if len(b) < len(s.data[s.offset]) { - return 0, io.ErrShortBuffer + n := copy(b, s.Data[s.Index][s.Offset:]) + if n > 0 { + s.Offset += uint(n) + if s.Offset == uint(len(s.Data[s.Index])) { + s.Offset = 0 + s.Index++ + } + return n, nil } - n := copy(b, s.data[s.offset]) - if n == 0 { - err = io.EOF - } - s.offset++ - return n, err + return 0, io.EOF }