Debian Codegraph @

internal/magic/ms_office.go

raw symbols SCIP
1 package magic
2
3 import (
4 "bytes"
5 "encoding/binary"
6
7 "github.com/gabriel-vasile/mimetype/internal/cdf"
8 )
9
10 // Xlsx matches a Microsoft Excel 2007 file.
11 func Xlsx(raw []byte, limit uint32) bool {
12 return msoxml(raw, zipEntries{{
13 name: []byte("xl/"),
14 dir: true,
15 }}, 100)
16 }
17
18 // Docx matches a Microsoft Word 2007 file.
19 func Docx(raw []byte, limit uint32) bool {
20 return msoxml(raw, zipEntries{{
21 name: []byte("word/"),
22 dir: true,
23 }}, 100)
24 }
25
26 // Pptx matches a Microsoft PowerPoint 2007 file.
27 func Pptx(raw []byte, limit uint32) bool {
28 return msoxml(raw, zipEntries{{
29 name: []byte("ppt/"),
30 dir: true,
31 }}, 100)
32 }
33
34 // Visio matches a Microsoft Visio 2013+ file.
35 func Visio(raw []byte, limit uint32) bool {
36 return msoxml(raw, zipEntries{{
37 name: []byte("visio/"),
38 dir: true,
39 }}, 100)
40 }
41
42 // Ole matches an Open Linking and Embedding file.
43 //
44 // https://en.wikipedia.org/wiki/Object_Linking_and_Embedding
45 func Ole(raw []byte, limit uint32) bool {
46 return bytes.HasPrefix(raw, []byte{0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1})
47 }
48
49 // Doc matches a Microsoft Word 97-2003 file.
50 // See: https://github.com/decalage2/oletools/blob/412ee36ae45e70f42123e835871bac956d958461/oletools/common/clsid.py
51 func Doc(raw []byte, _ uint32) bool {
52 fromParsing := cdf.Detect(raw)
53 if fromParsing == cdf.CDFTypeDoc {
54 return true
55 }
56 if fromParsing != cdf.CDFTypeGeneric {
57 return false
58 }
59 // Fallback for inputs where the CDF directory is past the read limit: match
60 // the root storage CLSID, which often lies within the first sectors.
61 clsids := [][]byte{
62 // Microsoft Word 97-2003 Document (Word.Document.8)
63 {0x06, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46},
64 // Microsoft Word 6.0-7.0 Document (Word.Document.6)
65 {0x00, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46},
66 // Microsoft Word Picture (Word.Picture.8)
67 {0x07, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46},
68 }
69 for _, clsid := range clsids {
70 if matchOleClsid(raw, clsid) {
71 return true
72 }
73 }
74 return false
75 }
76
77 // Ppt matches a Microsoft PowerPoint 97-2003 file or a PowerPoint 95 presentation.
78 func Ppt(raw []byte, limit uint32) bool {
79 fromParsing := cdf.Detect(raw)
80 if fromParsing == cdf.CDFTypePpt {
81 return true
82 }
83 if fromParsing != cdf.CDFTypeGeneric {
84 return false
85 }
86 // Fallback for inputs where the CDF directory is past the read limit.
87 // Root CLSID test is the safest way to identify the OLE, however, the format
88 // often places the root CLSID at the end of the file.
89 if matchOleClsid(raw, []byte{
90 0x10, 0x8d, 0x81, 0x64, 0x9b, 0x4f, 0xcf, 0x11,
91 0x86, 0xea, 0x00, 0xaa, 0x00, 0xb9, 0x29, 0xe8,
92 }) || matchOleClsid(raw, []byte{
93 0x70, 0xae, 0x7b, 0xea, 0x3b, 0xfb, 0xcd, 0x11,
94 0xa9, 0x03, 0x00, 0xaa, 0x00, 0x51, 0x0e, 0xa3,
95 }) {
96 return true
97 }
98
99 lin := len(raw)
100 if lin < 520 {
101 return false
102 }
103 pptSubHeaders := [][]byte{
104 {0xA0, 0x46, 0x1D, 0xF0},
105 {0x00, 0x6E, 0x1E, 0xF0},
106 {0x0F, 0x00, 0xE8, 0x03},
107 }
108 for _, h := range pptSubHeaders {
109 if bytes.HasPrefix(raw[512:], h) {
110 return true
111 }
112 }
113
114 return lin > 1152 && bytes.Contains(raw[1152:min(4096, lin)],
115 []byte("P\x00o\x00w\x00e\x00r\x00P\x00o\x00i\x00n\x00t\x00 D\x00o\x00c\x00u\x00m\x00e\x00n\x00t"))
116 }
117
118 // Xls matches a Microsoft Excel 97-2003 file.
119 func Xls(raw []byte, limit uint32) bool {
120 fromParsing := cdf.Detect(raw)
121 if fromParsing == cdf.CDFTypeXls {
122 return true
123 }
124 if fromParsing != cdf.CDFTypeGeneric {
125 return false
126 }
127 // Fallback for inputs where the CDF directory is past the read limit.
128 // Root CLSID test is the safest way to identify the OLE, however, the format
129 // often places the root CLSID at the end of the file.
130 if matchOleClsid(raw, []byte{
131 0x10, 0x08, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
132 }) || matchOleClsid(raw, []byte{
133 0x20, 0x08, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
134 }) {
135 return true
136 }
137
138 lin := len(raw)
139 if lin < 520 {
140 return false
141 }
142 xlsSubHeaders := [][]byte{
143 {0x09, 0x08, 0x10, 0x00, 0x00, 0x06, 0x05, 0x00},
144 {0xFD, 0xFF, 0xFF, 0xFF, 0x10},
145 {0xFD, 0xFF, 0xFF, 0xFF, 0x1F},
146 {0xFD, 0xFF, 0xFF, 0xFF, 0x22},
147 {0xFD, 0xFF, 0xFF, 0xFF, 0x23},
148 {0xFD, 0xFF, 0xFF, 0xFF, 0x28},
149 {0xFD, 0xFF, 0xFF, 0xFF, 0x29},
150 }
151 for _, h := range xlsSubHeaders {
152 if bytes.HasPrefix(raw[512:], h) {
153 return true
154 }
155 }
156
157 return lin > 1152 && bytes.Contains(raw[1152:min(4096, lin)],
158 []byte("W\x00k\x00s\x00S\x00S\x00W\x00o\x00r\x00k\x00B\x00o\x00o\x00k"))
159 }
160
161 // Pub matches a Microsoft Publisher file.
162 func Pub(raw []byte, limit uint32) bool {
163 return matchOleClsid(raw, []byte{
164 0x01, 0x12, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
165 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46,
166 })
167 }
168
169 // Msg matches a Microsoft Outlook email file.
170 func Msg(raw []byte, limit uint32) bool {
171 fromParsing := cdf.Detect(raw)
172 if fromParsing == cdf.CDFTypeMsg {
173 return true
174 }
175 if fromParsing != cdf.CDFTypeGeneric {
176 return false
177 }
178 // Fallback for inputs where the CDF directory does not carry the streams the
179 // parser keys on: match the root storage CLSID instead.
180 return matchOleClsid(raw, []byte{
181 0x0B, 0x0D, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
182 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46,
183 })
184 }
185
186 // Msi matches a Microsoft Windows Installer file.
187 // http://fileformats.archiveteam.org/wiki/Microsoft_Compound_File
188 func Msi(raw []byte, limit uint32) bool {
189 return cdf.Detect(raw) == cdf.CDFTypeInstaller
190 }
191
192 // One matches a Microsoft OneNote file.
193 func One(raw []byte, limit uint32) bool {
194 return bytes.HasPrefix(raw, []byte{
195 0xe4, 0x52, 0x5c, 0x7b, 0x8c, 0xd8, 0xa7, 0x4d,
196 0xae, 0xb1, 0x53, 0x78, 0xd0, 0x29, 0x96, 0xd3,
197 })
198 }
199
200 // Helper to match by a specific CLSID of a compound file.
201 //
202 // http://fileformats.archiveteam.org/wiki/Microsoft_Compound_File
203 func matchOleClsid(in []byte, clsid []byte) bool {
204 // Microsoft Compound files v3 have a sector length of 512, while v4 has 4096.
205 // Change sector offset depending on file version.
206 // https://www.loc.gov/preservation/digital/formats/fdd/fdd000392.shtml
207 sectorLength := 512
208 if len(in) < sectorLength {
209 return false
210 }
211 if in[26] == 0x04 && in[27] == 0x00 {
212 sectorLength = 4096
213 }
214
215 // SecID of first sector of the directory stream.
216 firstSecID := int(binary.LittleEndian.Uint32(in[48:52]))
217
218 // Expected offset of CLSID for root storage object.
219 clsidOffset := sectorLength*(1+firstSecID) + 80
220
221 // #731 offset is outside in or wrapped around due to integer overflow.
222 if len(in) <= clsidOffset+16 || clsidOffset < 0 {
223 return false
224 }
225
226 return bytes.HasPrefix(in[clsidOffset:], clsid)
227 }
228
229 // WPD matches a WordPerfect document.
230 func WPD(raw []byte, _ uint32) bool {
231 if len(raw) < 10 {
232 return false
233 }
234 if !bytes.HasPrefix(raw, []byte("\xffWPC")) {
235 return false
236 }
237 return raw[8] == 1 && raw[9] == 10
238 }