| 1 |
package matchers |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/binary" |
| 6 |
) |
| 7 |
|
| 8 |
var ( |
| 9 |
TypeDoc = newType("doc", "application/msword") |
| 10 |
TypeDocx = newType("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document") |
| 11 |
TypeXls = newType("xls", "application/vnd.ms-excel") |
| 12 |
TypeXlsx = newType("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") |
| 13 |
TypePpt = newType("ppt", "application/vnd.ms-powerpoint") |
| 14 |
TypePptx = newType("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation") |
| 15 |
) |
| 16 |
|
| 17 |
var Document = Map{ |
| 18 |
TypeDoc: Doc, |
| 19 |
TypeDocx: Docx, |
| 20 |
TypeXls: Xls, |
| 21 |
TypeXlsx: Xlsx, |
| 22 |
TypePpt: Ppt, |
| 23 |
TypePptx: Pptx, |
| 24 |
} |
| 25 |
|
| 26 |
type docType int |
| 27 |
|
| 28 |
const ( |
| 29 |
TYPE_DOC docType = iota |
| 30 |
TYPE_DOCX |
| 31 |
TYPE_XLS |
| 32 |
TYPE_XLSX |
| 33 |
TYPE_PPT |
| 34 |
TYPE_PPTX |
| 35 |
TYPE_OOXML |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
func Doc(buf []byte) bool { |
| 40 |
if len(buf) > 513 { |
| 41 |
return buf[0] == 0xD0 && buf[1] == 0xCF && |
| 42 |
buf[2] == 0x11 && buf[3] == 0xE0 && |
| 43 |
buf[512] == 0xEC && buf[513] == 0xA5 |
| 44 |
} else { |
| 45 |
return len(buf) > 3 && |
| 46 |
buf[0] == 0xD0 && buf[1] == 0xCF && |
| 47 |
buf[2] == 0x11 && buf[3] == 0xE0 |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
func Docx(buf []byte) bool { |
| 52 |
typ, ok := msooxml(buf) |
| 53 |
return ok && typ == TYPE_DOCX |
| 54 |
} |
| 55 |
|
| 56 |
func Xls(buf []byte) bool { |
| 57 |
if len(buf) > 513 { |
| 58 |
return buf[0] == 0xD0 && buf[1] == 0xCF && |
| 59 |
buf[2] == 0x11 && buf[3] == 0xE0 && |
| 60 |
buf[512] == 0x09 && buf[513] == 0x08 |
| 61 |
} else { |
| 62 |
return len(buf) > 3 && |
| 63 |
buf[0] == 0xD0 && buf[1] == 0xCF && |
| 64 |
buf[2] == 0x11 && buf[3] == 0xE0 |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
func Xlsx(buf []byte) bool { |
| 69 |
typ, ok := msooxml(buf) |
| 70 |
return ok && typ == TYPE_XLSX |
| 71 |
} |
| 72 |
|
| 73 |
func Ppt(buf []byte) bool { |
| 74 |
if len(buf) > 513 { |
| 75 |
return buf[0] == 0xD0 && buf[1] == 0xCF && |
| 76 |
buf[2] == 0x11 && buf[3] == 0xE0 && |
| 77 |
buf[512] == 0xA0 && buf[513] == 0x46 |
| 78 |
} else { |
| 79 |
return len(buf) > 3 && |
| 80 |
buf[0] == 0xD0 && buf[1] == 0xCF && |
| 81 |
buf[2] == 0x11 && buf[3] == 0xE0 |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func Pptx(buf []byte) bool { |
| 86 |
typ, ok := msooxml(buf) |
| 87 |
return ok && typ == TYPE_PPTX |
| 88 |
} |
| 89 |
|
| 90 |
func msooxml(buf []byte) (typ docType, found bool) { |
| 91 |
signature := []byte{'P', 'K', 0x03, 0x04} |
| 92 |
|
| 93 |
|
| 94 |
if ok := compareBytes(buf, signature, 0); !ok { |
| 95 |
return |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
if v, ok := checkMSOoml(buf, 0x1E); ok { |
| 100 |
return v, ok |
| 101 |
} |
| 102 |
|
| 103 |
if !compareBytes(buf, []byte("[Content_Types].xml"), 0x1E) && |
| 104 |
!compareBytes(buf, []byte("_rels/.rels"), 0x1E) && |
| 105 |
!compareBytes(buf, []byte("docProps"), 0x1E) { |
| 106 |
return |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
startOffset := int(binary.LittleEndian.Uint32(buf[18:22]) + 49) |
| 113 |
idx := search(buf, startOffset, 6000) |
| 114 |
if idx == -1 { |
| 115 |
return |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
startOffset += idx + 4 + 26 |
| 121 |
idx = search(buf, startOffset, 6000) |
| 122 |
if idx == -1 { |
| 123 |
return |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
startOffset += idx + 4 + 26 |
| 130 |
if typ, ok := checkMSOoml(buf, startOffset); ok { |
| 131 |
return typ, ok |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
startOffset += 26 |
| 136 |
idx = search(buf, startOffset, 6000) |
| 137 |
if idx == -1 { |
| 138 |
return TYPE_OOXML, true |
| 139 |
} |
| 140 |
|
| 141 |
startOffset += idx + 4 + 26 |
| 142 |
if typ, ok := checkMSOoml(buf, startOffset); ok { |
| 143 |
return typ, ok |
| 144 |
} else { |
| 145 |
return TYPE_OOXML, true |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func compareBytes(slice, subSlice []byte, startOffset int) bool { |
| 150 |
sl := len(subSlice) |
| 151 |
|
| 152 |
if startOffset+sl > len(slice) { |
| 153 |
return false |
| 154 |
} |
| 155 |
|
| 156 |
s := slice[startOffset : startOffset+sl] |
| 157 |
for i := range s { |
| 158 |
if subSlice[i] != s[i] { |
| 159 |
return false |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return true |
| 164 |
} |
| 165 |
|
| 166 |
func checkMSOoml(buf []byte, offset int) (typ docType, ok bool) { |
| 167 |
ok = true |
| 168 |
|
| 169 |
switch { |
| 170 |
case compareBytes(buf, []byte("word/"), offset): |
| 171 |
typ = TYPE_DOCX |
| 172 |
case compareBytes(buf, []byte("ppt/"), offset): |
| 173 |
typ = TYPE_PPTX |
| 174 |
case compareBytes(buf, []byte("xl/"), offset): |
| 175 |
typ = TYPE_XLSX |
| 176 |
default: |
| 177 |
ok = false |
| 178 |
} |
| 179 |
|
| 180 |
return |
| 181 |
} |
| 182 |
|
| 183 |
func search(buf []byte, start, rangeNum int) int { |
| 184 |
length := len(buf) |
| 185 |
end := start + rangeNum |
| 186 |
signature := []byte{'P', 'K', 0x03, 0x04} |
| 187 |
|
| 188 |
if end > length { |
| 189 |
end = length |
| 190 |
} |
| 191 |
|
| 192 |
if start >= end { |
| 193 |
return -1 |
| 194 |
} |
| 195 |
|
| 196 |
return bytes.Index(buf[start:end], signature) |
| 197 |
} |