diff --git a/file.go b/file.go index f9825a7..36a1ba7 100644 --- a/file.go +++ b/file.go @@ -25,7 +25,7 @@ import ( "github.com/richardlehane/msoleps/types" ) -//objectType types +// objectType types const ( unknown uint8 = 0x0 // this means unallocated - typically zeroed dir entries storage uint8 = 0x1 // this means dir @@ -33,14 +33,6 @@ const ( rootStorage uint8 = 0x5 // this means root ) -// color flags -const ( - red uint8 = 0x0 - black uint8 = 0x1 -) - -const lenDirEntry int = 64 + 4*4 + 16 + 4 + 8*2 + 4 + 8 - type directoryEntryFields struct { rawName [32]uint16 //64 bytes, unicode string encoded in UTF-16. If root, "Root Entry\0" w nameLength uint16 //2 bytes @@ -177,7 +169,6 @@ func (r *Reader) traverse() error { if file.rightSibID != noStream { recurse(int(file.rightSibID), path) } - return } recurse(0, []string{}) return err diff --git a/file_test.go b/file_test.go index a05d68e..7aab2f9 100644 --- a/file_test.go +++ b/file_test.go @@ -1,8 +1,6 @@ package mscfb -import ( - "testing" -) +import "testing" func equal(a [][2]int64, b [][2]int64) bool { if len(a) != len(b) { @@ -17,14 +15,14 @@ func equal(a [][2]int64, b [][2]int64) bool { } func TestCompress(t *testing.T) { - a := [][2]int64{[2]int64{4608, 1024}, [2]int64{5632, 1024}, [2]int64{6656, 1024}, [2]int64{7680, 1024}, [2]int64{8704, 1024}, [2]int64{9728, 1024}, [2]int64{10752, 512}} - ar := [][2]int64{[2]int64{4608, 6656}} + a := [][2]int64{{4608, 1024}, {5632, 1024}, {6656, 1024}, {7680, 1024}, {8704, 1024}, {9728, 1024}, {10752, 512}} + ar := [][2]int64{{4608, 6656}} a = compressChain(a) if !equal(a, ar) { t.Errorf("Streams compress fail; Expecting: %v, Got: %v", ar, a) } - b := [][2]int64{[2]int64{4608, 1024}, [2]int64{6656, 1024}, [2]int64{7680, 1024}, [2]int64{8704, 1024}, [2]int64{10752, 512}} - br := [][2]int64{[2]int64{4608, 1024}, [2]int64{6656, 3072}, [2]int64{10752, 512}} + b := [][2]int64{{4608, 1024}, {6656, 1024}, {7680, 1024}, {8704, 1024}, {10752, 512}} + br := [][2]int64{{4608, 1024}, {6656, 3072}, {10752, 512}} b = compressChain(b) if !equal(b, br) { t.Errorf("Streams compress fail; Expecting: %v, Got: %v", br, b) diff --git a/fuzz.go b/fuzz.go index 50b8b4c..d2d817f 100644 --- a/fuzz.go +++ b/fuzz.go @@ -1,3 +1,4 @@ +//go:build gofuzz // +build gofuzz // fuzzing with https://github.com/dvyukov/go-fuzz @@ -8,6 +9,7 @@ import ( "io" ) +// todo: replace with Fuzzing from go test package func Fuzz(data []byte) int { doc, err := New(bytes.NewReader(data)) if err != nil { diff --git a/mscfb.go b/mscfb.go index dfc78a8..2bb6d47 100644 --- a/mscfb.go +++ b/mscfb.go @@ -351,11 +351,11 @@ func (r *Reader) Read(b []byte) (n int, err error) { // Debug provides granular information from an mscfb file to assist with debugging func (r *Reader) Debug() map[string][]uint32 { ret := map[string][]uint32{ - "sector size": []uint32{r.sectorSize}, + "sector size": {r.sectorSize}, "mini fat locs": r.header.miniFatLocs, "mini stream locs": r.header.miniStreamLocs, - "directory sector": []uint32{r.header.directorySectorLoc}, - "mini stream start/size": []uint32{r.File[0].startingSectorLoc, binary.LittleEndian.Uint32(r.File[0].streamSize[:])}, + "directory sector": {r.header.directorySectorLoc}, + "mini stream start/size": {r.File[0].startingSectorLoc, binary.LittleEndian.Uint32(r.File[0].streamSize[:])}, } for f, err := r.Next(); err == nil; f, err = r.Next() { ret[f.Name+" start/size"] = []uint32{f.startingSectorLoc, binary.LittleEndian.Uint32(f.streamSize[:])} diff --git a/mscfb_test.go b/mscfb_test.go index a07044c..9bd637b 100644 --- a/mscfb_test.go +++ b/mscfb_test.go @@ -3,7 +3,6 @@ package mscfb import ( "bytes" "io" - "io/ioutil" "log" "os" "sync" @@ -17,66 +16,45 @@ var ( testPpt = "test/test.ppt" testMsg = "test/test.msg" testEntries = []*File{ - &File{Name: "Root Node", + {Name: "Root Node", directoryEntryFields: &directoryEntryFields{leftSibID: noStream, rightSibID: noStream, childID: 1}, }, - &File{Name: "Alpha", + {Name: "Alpha", directoryEntryFields: &directoryEntryFields{leftSibID: noStream, rightSibID: 2, childID: noStream}, }, - &File{Name: "Bravo", + {Name: "Bravo", directoryEntryFields: &directoryEntryFields{leftSibID: noStream, rightSibID: 3, childID: 5}, }, - &File{Name: "Charlie", + {Name: "Charlie", directoryEntryFields: &directoryEntryFields{leftSibID: noStream, rightSibID: noStream, childID: 7}, }, - &File{Name: "Delta", + {Name: "Delta", directoryEntryFields: &directoryEntryFields{leftSibID: noStream, rightSibID: noStream, childID: noStream}, }, - &File{Name: "Echo", + {Name: "Echo", directoryEntryFields: &directoryEntryFields{leftSibID: 4, rightSibID: 6, childID: 9}, }, - &File{Name: "Foxtrot", + {Name: "Foxtrot", directoryEntryFields: &directoryEntryFields{leftSibID: noStream, rightSibID: noStream, childID: noStream}, }, - &File{Name: "Golf", + {Name: "Golf", directoryEntryFields: &directoryEntryFields{leftSibID: noStream, rightSibID: noStream, childID: 10}, }, - &File{Name: "Hotel", + {Name: "Hotel", directoryEntryFields: &directoryEntryFields{leftSibID: noStream, rightSibID: noStream, childID: noStream}, }, - &File{Name: "Indigo", + {Name: "Indigo", directoryEntryFields: &directoryEntryFields{leftSibID: 8, rightSibID: noStream, childID: 11}, }, - &File{Name: "Jello", + {Name: "Jello", directoryEntryFields: &directoryEntryFields{leftSibID: noStream, rightSibID: noStream, childID: noStream}, }, - &File{Name: "Kilo", + {Name: "Kilo", directoryEntryFields: &directoryEntryFields{leftSibID: noStream, rightSibID: noStream, childID: noStream}, }, } ) -func equals(a, b []int) bool { - if len(a) != len(b) { - return false - } - for i, v := range a { - if v != b[i] { - return false - } - } - return true -} - -func empty(sl []byte) bool { - for _, v := range sl { - if v != 0 { - return false - } - } - return true -} - func testFile(t *testing.T, path string) { file, _ := os.Open(path) defer file.Close() @@ -155,13 +133,13 @@ func TestConcurrentAccess(t *testing.T) { var wg sync.WaitGroup wg.Add(len(doc.File)) for _, f := range doc.File { - go func() { + go func(ff *File) { defer wg.Done() - _, err := io.Copy(io.Discard, f) + _, err := io.Copy(io.Discard, ff) if err != nil { log.Println(err) } - }() + }(f) } wg.Wait() } @@ -306,7 +284,7 @@ func TestWrite(t *testing.T) { func benchFile(b *testing.B, path string) { b.StopTimer() - buf, _ := ioutil.ReadFile(path) + buf, _ := os.ReadFile(path) entrybuf := make([]byte, 32000) b.StartTimer() rdr := bytes.NewReader(buf)