1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
<?php
/*
* Copyright 2019 Wikimedia Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
* OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
namespace Wikimedia\Mime;
use RuntimeException;
/**
* Read the directory of a Microsoft Compound File Binary file, a.k.a. an OLE
* file, and detect the MIME type.
*
* References:
* - MS-CFB https://msdn.microsoft.com/en-us/library/dd942138.aspx
* - MS-XLS https://msdn.microsoft.com/en-us/library/cc313154.aspx
* - MS-PPT https://msdn.microsoft.com/en-us/library/cc313106.aspx
* - MS-DOC https://msdn.microsoft.com/en-us/library/cc313153.aspx
* - Python olefile https://github.com/decalage2/olefile
* - OpenOffice.org's Documentation of the Microsoft Compound Document
* File Format https://www.openoffice.org/sc/compdocfileformat.pdf
*
* @since 1.33
* @ingroup Mime
*/
class MSCompoundFileReader {
/** @var resource */
private $file;
/** @var array */
private $header;
/** @var string */
private $mime;
/** @var string */
private $mimeFromClsid;
/** @var string|null */
private $error;
/** @var int|null */
private $errorCode;
/** @var bool */
private $valid = false;
/** @var int */
private $sectorLength;
/** @var int[] */
private $difat;
/** @var int[][] */
private $fat = [];
private const TYPE_UNALLOCATED = 0;
private const TYPE_STORAGE = 1;
private const TYPE_STREAM = 2;
private const TYPE_ROOT = 5;
public const ERROR_FILE_OPEN = 1;
public const ERROR_SEEK = 2;
public const ERROR_READ = 3;
public const ERROR_INVALID_SIGNATURE = 4;
public const ERROR_READ_PAST_END = 5;
public const ERROR_INVALID_FORMAT = 6;
private const MIMES_BY_CLSID = [
// From http://justsolve.archiveteam.org/wiki/Microsoft_Compound_File
'00020810-0000-0000-C000-000000000046' => 'application/vnd.ms-excel',
'00020820-0000-0000-C000-000000000046' => 'application/vnd.ms-excel',
'00020906-0000-0000-C000-000000000046' => 'application/msword',
'64818D10-4F9B-11CF-86EA-00AA00B929E8' => 'application/vnd.ms-powerpoint',
];
/**
* Read a file by name
*
* @param string $fileName The full path to the file
* @return array An associative array of information about the file:
* - valid: true if the file is valid, false otherwise
* - error: An error message in English, should be present if valid=false
* - errorCode: One of the self::ERROR_* constants
* - mime: The MIME type detected from the directory contents
* - mimeFromClsid: The MIME type detected from the CLSID on the root
* directory entry
*/
public static function readFile( $fileName ) {
$handle = fopen( $fileName, 'r' );
if ( $handle === false ) {
return [
'valid' => false,
'error' => 'file does not exist',
'errorCode' => self::ERROR_FILE_OPEN
];
}
return self::readHandle( $handle );
}
/**
* Read from an open seekable handle
*
* @param resource $fileHandle
* @return array An associative array of information about the file:
* - valid: true if the file is valid, false otherwise
* - error: An error message in English, should be present if valid=false
* - errorCode: One of the self::ERROR_* constants
* - mime: The MIME type detected from the directory contents
* - mimeFromClsid: The MIME type detected from the CLSID on the root
* directory entry
*/
public static function readHandle( $fileHandle ) {
$reader = new self( $fileHandle );
$info = [
'valid' => $reader->valid,
'mime' => $reader->mime,
'mimeFromClsid' => $reader->mimeFromClsid
];
if ( $reader->error ) {
$info['error'] = $reader->error;
$info['errorCode'] = $reader->errorCode;
}
return $info;
}
private function __construct( $fileHandle ) {
$this->file = $fileHandle;
try {
$this->init();
} catch ( RuntimeException $e ) {
$this->valid = false;
$this->error = $e->getMessage();
$this->errorCode = $e->getCode();
}
}
private function init() {
$this->header = $this->unpackOffset( 0, [
'header_signature' => 8,
'header_clsid' => 16,
'minor_version' => 2,
'major_version' => 2,
'byte_order' => 2,
'sector_shift' => 2,
'mini_sector_shift' => 2,
'reserved' => 6,
'num_dir_sectors' => 4,
'num_fat_sectors' => 4,
'first_dir_sector' => 4,
'transaction_signature_number' => 4,
'mini_stream_cutoff_size' => 4,
'first_mini_fat_sector' => 4,
'num_mini_fat_sectors' => 4,
'first_difat_sector' => 4,
'num_difat_sectors' => 4,
'difat' => 436,
] );
if ( $this->header['header_signature'] !== "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" ) {
$this->error( 'invalid signature: ' . bin2hex( $this->header['header_signature'] ),
self::ERROR_INVALID_SIGNATURE );
}
$this->sectorLength = 1 << $this->header['sector_shift'];
$this->readDifat();
$this->readDirectory();
$this->valid = true;
}
private function sectorOffset( int $sectorId ): int {
return $this->sectorLength * ( $sectorId + 1 );
}
private function decodeClsid( string $binaryClsid ): string {
$parts = unpack( 'Va/vb/vc/C8d', $binaryClsid );
return sprintf( "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
$parts['a'],
$parts['b'],
$parts['c'],
$parts['d1'],
$parts['d2'],
$parts['d3'],
$parts['d4'],
$parts['d5'],
$parts['d6'],
$parts['d7'],
$parts['d8']
);
}
/**
* @param int $offset
* @param int[] $struct
* @return array
*/
private function unpackOffset( $offset, $struct ) {
$block = $this->readOffset( $offset, array_sum( $struct ) );
return $this->unpack( $block, 0, $struct );
}
/**
* @param string $block
* @param int $offset
* @param int[] $struct
* @return array
*/
private function unpack( $block, $offset, $struct ) {
$data = [];
foreach ( $struct as $key => $length ) {
if ( $length > 4 ) {
$data[$key] = substr( $block, $offset, $length );
} else {
$data[$key] = $this->bin2dec( $block, $offset, $length );
}
$offset += $length;
}
return $data;
}
private function bin2dec( string $str, int $offset, int $length ): int {
$value = 0;
for ( $i = $length - 1; $i >= 0; $i-- ) {
$value *= 256;
$value += ord( $str[$offset + $i] );
}
return $value;
}
private function readOffset( int $offset, int $length ): string {
$this->fseek( $offset );
// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
$block = @fread( $this->file, $length );
if ( $block === false ) {
$this->error( 'error reading from file', self::ERROR_READ );
}
if ( strlen( $block ) !== $length ) {
$this->error( 'unable to read the required number of bytes from the file',
self::ERROR_READ_PAST_END );
}
return $block;
}
private function readSector( int $sectorId ): string {
return $this->readOffset( $this->sectorOffset( $sectorId ), 1 << $this->header['sector_shift'] );
}
/**
* @param string $message
* @param int $code
* @return never
*/
private function error( $message, $code ) {
throw new RuntimeException( $message, $code );
}
private function fseek( int $offset ) {
// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
$result = @fseek( $this->file, $offset );
if ( $result !== 0 ) {
$this->error( "unable to seek to offset $offset", self::ERROR_SEEK );
}
}
private function readDifat() {
$binaryDifat = $this->header['difat'];
$nextDifatSector = $this->header['first_difat_sector'];
for ( $i = 0; $i < $this->header['num_difat_sectors']; $i++ ) {
$block = $this->readSector( $nextDifatSector );
$binaryDifat .= substr( $block, 0, $this->sectorLength - 4 );
$nextDifatSector = $this->bin2dec( $block, $this->sectorLength - 4, 4 );
if ( $nextDifatSector == 0xFFFFFFFE ) {
break;
}
}
$this->difat = [];
for ( $pos = 0; $pos < strlen( $binaryDifat ); $pos += 4 ) {
$fatSector = $this->bin2dec( $binaryDifat, $pos, 4 );
if ( $fatSector < 0xFFFFFFFC ) {
$this->difat[] = $fatSector;
} else {
break;
}
}
}
private function getNextSectorIdFromFat( int $sectorId ): int {
$entriesPerSector = intdiv( $this->sectorLength, 4 );
$fatSectorId = intdiv( $sectorId, $entriesPerSector );
$fatSectorArray = $this->getFatSector( $fatSectorId );
return $fatSectorArray[$sectorId % $entriesPerSector];
}
private function getFatSector( int $fatSectorId ): array {
if ( !isset( $this->fat[$fatSectorId] ) ) {
$fat = [];
if ( !isset( $this->difat[$fatSectorId] ) ) {
$this->error( 'FAT sector requested beyond the end of the DIFAT', self::ERROR_INVALID_FORMAT );
}
$absoluteSectorId = $this->difat[$fatSectorId];
$block = $this->readSector( $absoluteSectorId );
for ( $pos = 0; $pos < strlen( $block ); $pos += 4 ) {
$fat[] = $this->bin2dec( $block, $pos, 4 );
}
$this->fat[$fatSectorId] = $fat;
}
return $this->fat[$fatSectorId];
}
private function readDirectory() {
$dirSectorId = $this->header['first_dir_sector'];
$binaryDir = '';
$seenSectorIds = [];
while ( $dirSectorId !== 0xFFFFFFFE ) {
if ( isset( $seenSectorIds[$dirSectorId] ) ) {
$this->error( 'FAT loop detected', self::ERROR_INVALID_FORMAT );
}
$seenSectorIds[$dirSectorId] = true;
$binaryDir .= $this->readSector( $dirSectorId );
$dirSectorId = $this->getNextSectorIdFromFat( $dirSectorId );
}
$struct = [
'name_raw' => 64,
'name_length' => 2,
'object_type' => 1,
'color' => 1,
'sid_left' => 4,
'sid_right' => 4,
'sid_child' => 4,
'clsid' => 16,
'state_bits' => 4,
'create_time_low' => 4,
'create_time_high' => 4,
'modify_time_low' => 4,
'modify_time_high' => 4,
'first_sector' => 4,
'size_low' => 4,
'size_high' => 4,
];
$entryLength = array_sum( $struct );
for ( $pos = 0; $pos < strlen( $binaryDir ); $pos += $entryLength ) {
$entry = $this->unpack( $binaryDir, $pos, $struct );
// According to [MS-CFB] size_high may contain garbage due to a
// bug in a writer, it's best to pretend it is zero
$entry['size_high'] = 0;
$type = $entry['object_type'];
if ( $type == self::TYPE_UNALLOCATED ) {
continue;
}
$name = iconv( 'UTF-16LE', 'UTF-8', substr( $entry['name_raw'], 0, $entry['name_length'] - 2 ) );
$clsid = $this->decodeClsid( $entry['clsid'] );
if ( $type == self::TYPE_ROOT && isset( self::MIMES_BY_CLSID[$clsid] ) ) {
$this->mimeFromClsid = self::MIMES_BY_CLSID[$clsid];
}
if ( $name === 'Workbook' ) {
$this->mime = 'application/vnd.ms-excel';
} elseif ( $name === 'WordDocument' ) {
$this->mime = 'application/msword';
} elseif ( $name === 'PowerPoint Document' ) {
$this->mime = 'application/vnd.ms-powerpoint';
}
}
}
}
/** @deprecated class alias since 1.43 */
class_alias( MSCompoundFileReader::class, 'MSCompoundFileReader' );
|