@@ -3,26 +3,18 @@ use core::convert::TryInto;
3
3
#[ allow( clippy:: upper_case_acronyms) ]
4
4
#[ derive( Debug , Eq , PartialEq ) ]
5
5
enum DocType {
6
- // DOC,
6
+ DOC ,
7
7
DOCX ,
8
- // XLS,
8
+ XLS ,
9
9
XLSX ,
10
- // PPT,
10
+ PPT ,
11
11
PPTX ,
12
- OOXLM ,
12
+ OOXML ,
13
13
}
14
14
15
15
/// Returns whether a buffer is Microsoft Word Document (DOC) data.
16
16
pub fn is_doc ( buf : & [ u8 ] ) -> bool {
17
- buf. len ( ) > 7
18
- && buf[ 0 ] == 0xD0
19
- && buf[ 1 ] == 0xCF
20
- && buf[ 2 ] == 0x11
21
- && buf[ 3 ] == 0xE0
22
- && buf[ 4 ] == 0xA1
23
- && buf[ 5 ] == 0xB1
24
- && buf[ 6 ] == 0x1A
25
- && buf[ 7 ] == 0xE1
17
+ ole2 ( buf) == Some ( DocType :: DOC )
26
18
}
27
19
28
20
/// Returns whether a buffer is Microsoft Word Open XML Format Document (DOCX) data.
@@ -32,15 +24,7 @@ pub fn is_docx(buf: &[u8]) -> bool {
32
24
33
25
/// Returns whether a buffer is Microsoft Excel 97-2003 Worksheet (XLS) data.
34
26
pub fn is_xls ( buf : & [ u8 ] ) -> bool {
35
- buf. len ( ) > 7
36
- && buf[ 0 ] == 0xD0
37
- && buf[ 1 ] == 0xCF
38
- && buf[ 2 ] == 0x11
39
- && buf[ 3 ] == 0xE0
40
- && buf[ 4 ] == 0xA1
41
- && buf[ 5 ] == 0xB1
42
- && buf[ 6 ] == 0x1A
43
- && buf[ 7 ] == 0xE1
27
+ ole2 ( buf) == Some ( DocType :: XLS )
44
28
}
45
29
46
30
/// Returns whether a buffer is Microsoft Excel Open XML Format Spreadsheet (XLSX) data.
@@ -50,15 +34,7 @@ pub fn is_xlsx(buf: &[u8]) -> bool {
50
34
51
35
/// Returns whether a buffer is Microsoft PowerPoint 97-2003 Presentation (PPT) data.
52
36
pub fn is_ppt ( buf : & [ u8 ] ) -> bool {
53
- buf. len ( ) > 7
54
- && buf[ 0 ] == 0xD0
55
- && buf[ 1 ] == 0xCF
56
- && buf[ 2 ] == 0x11
57
- && buf[ 3 ] == 0xE0
58
- && buf[ 4 ] == 0xA1
59
- && buf[ 5 ] == 0xB1
60
- && buf[ 6 ] == 0x1A
61
- && buf[ 7 ] == 0xE1
37
+ ole2 ( buf) == Some ( DocType :: PPT )
62
38
}
63
39
64
40
/// Returns whether a buffer is Microsoft PowerPoint Open XML Presentation (PPTX) data.
@@ -108,15 +84,43 @@ fn msooxml(buf: &[u8]) -> Option<DocType> {
108
84
let idx = search ( buf, start_offset, 6000 ) ;
109
85
match idx {
110
86
Some ( idx) => start_offset += idx + 4 + 26 ,
111
- None => return Some ( DocType :: OOXLM ) ,
87
+ None => return Some ( DocType :: OOXML ) ,
112
88
} ;
113
89
114
90
let typo = check_msooml ( buf, start_offset) ;
115
91
if typo. is_some ( ) {
116
92
return typo;
117
93
}
118
94
119
- Some ( DocType :: OOXLM )
95
+ Some ( DocType :: OOXML )
96
+ }
97
+
98
+ #[ cfg( feature = "std" ) ]
99
+ fn ole2 ( buf : & [ u8 ] ) -> Option < DocType > {
100
+ use std:: io:: Cursor ;
101
+
102
+ if !compare_bytes ( buf, & [ 0xD0 , 0xCF , 0x11 , 0xE0 , 0xA1 , 0xB1 , 0x1A , 0xE1 ] , 0 ) {
103
+ return None ;
104
+ }
105
+ if let Ok ( file) = cfb:: CompoundFile :: open ( Cursor :: new ( buf) ) {
106
+ return match file. root_entry ( ) . clsid ( ) . to_string ( ) . as_str ( ) {
107
+ "00020810-0000-0000-c000-000000000046" | "00020820-0000-0000-c000-000000000046" => {
108
+ Some ( DocType :: XLS )
109
+ }
110
+ "00020906-0000-0000-c000-000000000046" => Some ( DocType :: DOC ) ,
111
+ "64818d10-4f9b-11cf-86ea-00aa00b929e8" => Some ( DocType :: PPT ) ,
112
+ _ => None ,
113
+ } ;
114
+ }
115
+ None
116
+ }
117
+
118
+ #[ cfg( not( feature = "std" ) ) ]
119
+ fn ole2 ( buf : & [ u8 ] ) -> Option < DocType > {
120
+ if !compare_bytes ( buf, & [ 0xD0 , 0xCF , 0x11 , 0xE0 , 0xA1 , 0xB1 , 0x1A , 0xE1 ] , 0 ) {
121
+ return None ;
122
+ }
123
+ Some ( DocType :: DOC )
120
124
}
121
125
122
126
fn compare_bytes ( slice : & [ u8 ] , sub_slice : & [ u8 ] , start_offset : usize ) -> bool {
0 commit comments