mscfb icon indicating copy to clipboard operation
mscfb copied to clipboard

Add HasVBA function in file.go

Open phaag opened this issue 6 years ago • 2 comments

Add function HasVBA in file.go in order to test if there is VBA Code in it.

phaag avatar Feb 17 '20 14:02 phaag

thanks for this Peter, I'll take a look

richardlehane avatar Feb 18 '20 08:02 richardlehane

Hi Peter thanks again for this pull request. I'd like to keep the footprint of mscfb as small as possible & it seems like this might make more sense as a function in a dedicated VBA package. What do you think about adding this as a func to your own go-vba package instead?? Then would be trivial to combine with mscfb. Few suggestions for tweaks to your func:

// HasVBA: Checks if this file contains VBA code
// Returns true/false and offset to start decoding VBA
func HasVBA(rdr io.Reader) (bool, int64) {
	var (
		marker = []byte("\x00Attribut")
		mlen   = len(marker)
		cur    int
		offset int64
		size   = 4 * 1024
		buff   = make([]byte, size)
	)
	for nr, _ := rdr.Read(buff); nr > 0; nr, _ = rdr.Read(buff) {
		for i, b := range buff[:nr] {
			if b == marker[cur] {
				cur++
				if cur == mlen {
					return true, offset + int64(i) - 3
				}
			} else {
				cur = 0
			}
		}
		offset += int64(nr)
	}
	return false, -1
}

richardlehane avatar Feb 18 '20 20:02 richardlehane