MediaWiki REL1_34
MSCompoundFileReader.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright 2019 Wikimedia Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software distributed
12 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
13 * OF ANY KIND, either express or implied. See the License for the
14 * specific language governing permissions and limitations under the License.
15 */
16
33 private $file;
34 private $header;
35 private $mime;
37 private $error;
38 private $errorCode;
39 private $valid = false;
40
42 private $difat;
43 private $fat = [];
44 private $fileLength;
45
47 const TYPE_STORAGE = 1;
48 const TYPE_STREAM = 2;
49 const TYPE_ROOT = 5;
50
51 const ERROR_FILE_OPEN = 1;
52 const ERROR_SEEK = 2;
53 const ERROR_READ = 3;
57
58 private static $mimesByClsid = [
59 // From http://justsolve.archiveteam.org/wiki/Microsoft_Compound_File
60 '00020810-0000-0000-C000-000000000046' => 'application/vnd.ms-excel',
61 '00020820-0000-0000-C000-000000000046' => 'application/vnd.ms-excel',
62 '00020906-0000-0000-C000-000000000046' => 'application/msword',
63 '64818D10-4F9B-11CF-86EA-00AA00B929E8' => 'application/vnd.ms-powerpoint',
64 ];
65
78 public static function readFile( $fileName ) {
79 $handle = fopen( $fileName, 'r' );
80 if ( $handle === false ) {
81 return [
82 'valid' => false,
83 'error' => 'file does not exist',
84 'errorCode' => self::ERROR_FILE_OPEN
85 ];
86 }
87 return self::readHandle( $handle );
88 }
89
102 public static function readHandle( $fileHandle ) {
103 $reader = new self( $fileHandle );
104 $info = [
105 'valid' => $reader->valid,
106 'mime' => $reader->mime,
107 'mimeFromClsid' => $reader->mimeFromClsid
108 ];
109 if ( $reader->error ) {
110 $info['error'] = $reader->error;
111 $info['errorCode'] = $reader->errorCode;
112 }
113 return $info;
114 }
115
116 private function __construct( $fileHandle ) {
117 $this->file = $fileHandle;
118 try {
119 $this->init();
120 } catch ( RuntimeException $e ) {
121 $this->valid = false;
122 $this->error = $e->getMessage();
123 $this->errorCode = $e->getCode();
124 }
125 }
126
127 private function init() {
128 $this->header = $this->unpackOffset( 0, [
129 'header_signature' => 8,
130 'header_clsid' => 16,
131 'minor_version' => 2,
132 'major_version' => 2,
133 'byte_order' => 2,
134 'sector_shift' => 2,
135 'mini_sector_shift' => 2,
136 'reserved' => 6,
137 'num_dir_sectors' => 4,
138 'num_fat_sectors' => 4,
139 'first_dir_sector' => 4,
140 'transaction_signature_number' => 4,
141 'mini_stream_cutoff_size' => 4,
142 'first_mini_fat_sector' => 4,
143 'num_mini_fat_sectors' => 4,
144 'first_difat_sector' => 4,
145 'num_difat_sectors' => 4,
146 'difat' => 436,
147 ] );
148 if ( $this->header['header_signature'] !== "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" ) {
149 $this->error( 'invalid signature: ' . bin2hex( $this->header['header_signature'] ),
150 self::ERROR_INVALID_SIGNATURE );
151 }
152 // @phan-suppress-next-line PhanTypeInvalidRightOperandOfIntegerOp
153 $this->sectorLength = 1 << $this->header['sector_shift'];
154 $this->readDifat();
155 $this->readDirectory();
156
157 $this->valid = true;
158 }
159
160 private function sectorOffset( $sectorId ) {
161 return $this->sectorLength * ( $sectorId + 1 );
162 }
163
164 private function decodeClsid( $binaryClsid ) {
165 $parts = unpack( 'Va/vb/vc/C8d', $binaryClsid );
166 return sprintf( "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
167 $parts['a'],
168 $parts['b'],
169 $parts['c'],
170 $parts['d1'],
171 $parts['d2'],
172 $parts['d3'],
173 $parts['d4'],
174 $parts['d5'],
175 $parts['d6'],
176 $parts['d7'],
177 $parts['d8']
178 );
179 }
180
186 private function unpackOffset( $offset, $struct ) {
187 $block = $this->readOffset( $offset, array_sum( $struct ) );
188 return $this->unpack( $block, 0, $struct );
189 }
190
197 private function unpack( $block, $offset, $struct ) {
198 $data = [];
199 foreach ( $struct as $key => $length ) {
200 if ( $length > 4 ) {
201 $data[$key] = substr( $block, $offset, $length );
202 } else {
203 $data[$key] = $this->bin2dec( $block, $offset, $length );
204 }
205 $offset += $length;
206 }
207 return $data;
208 }
209
210 private function bin2dec( $str, $offset, $length ) {
211 $value = 0;
212 for ( $i = $length - 1; $i >= 0; $i-- ) {
213 $value *= 256;
214 $value += ord( $str[$offset + $i] );
215 }
216 return $value;
217 }
218
219 private function readOffset( $offset, $length ) {
220 $this->fseek( $offset );
221 Wikimedia\suppressWarnings();
222 $block = fread( $this->file, $length );
223 Wikimedia\restoreWarnings();
224 if ( $block === false ) {
225 $this->error( 'error reading from file', self::ERROR_READ );
226 }
227 if ( strlen( $block ) !== $length ) {
228 $this->error( 'unable to read the required number of bytes from the file',
229 self::ERROR_READ_PAST_END );
230 }
231 return $block;
232 }
233
234 private function readSector( $sectorId ) {
235 // @phan-suppress-next-line PhanTypeInvalidRightOperandOfIntegerOp
236 return $this->readOffset( $this->sectorOffset( $sectorId ), 1 << $this->header['sector_shift'] );
237 }
238
239 private function error( $message, $code ) {
240 throw new RuntimeException( $message, $code );
241 }
242
243 private function fseek( $offset ) {
244 Wikimedia\suppressWarnings();
245 $result = fseek( $this->file, $offset );
246 Wikimedia\restoreWarnings();
247 if ( $result !== 0 ) {
248 $this->error( "unable to seek to offset $offset", self::ERROR_SEEK );
249 }
250 }
251
252 private function readDifat() {
253 $binaryDifat = $this->header['difat'];
254 $nextDifatSector = $this->header['first_difat_sector'];
255 for ( $i = 0; $i < $this->header['num_difat_sectors']; $i++ ) {
256 $block = $this->readSector( $nextDifatSector );
257 $binaryDifat .= substr( $block, 0, $this->sectorLength - 4 );
258 $nextDifatSector = $this->bin2dec( $block, $this->sectorLength - 4, 4 );
259 if ( $nextDifatSector == 0xFFFFFFFE ) {
260 break;
261 }
262 }
263
264 $this->difat = [];
265 for ( $pos = 0; $pos < strlen( $binaryDifat ); $pos += 4 ) {
266 $fatSector = $this->bin2dec( $binaryDifat, $pos, 4 );
267 if ( $fatSector < 0xFFFFFFFC ) {
268 $this->difat[] = $fatSector;
269 } else {
270 break;
271 }
272 }
273 }
274
275 private function getNextSectorIdFromFat( $sectorId ) {
276 $entriesPerSector = intdiv( $this->sectorLength, 4 );
277 $fatSectorId = intdiv( $sectorId, $entriesPerSector );
278 $fatSectorArray = $this->getFatSector( $fatSectorId );
279 return $fatSectorArray[$sectorId % $entriesPerSector];
280 }
281
282 private function getFatSector( $fatSectorId ) {
283 if ( !isset( $this->fat[$fatSectorId] ) ) {
284 $fat = [];
285 if ( !isset( $this->difat[$fatSectorId] ) ) {
286 $this->error( 'FAT sector requested beyond the end of the DIFAT', self::ERROR_INVALID_FORMAT );
287 }
288 $absoluteSectorId = $this->difat[$fatSectorId];
289 $block = $this->readSector( $absoluteSectorId );
290 for ( $pos = 0; $pos < strlen( $block ); $pos += 4 ) {
291 $fat[] = $this->bin2dec( $block, $pos, 4 );
292 }
293 $this->fat[$fatSectorId] = $fat;
294 }
295 return $this->fat[$fatSectorId];
296 }
297
298 private function readDirectory() {
299 $dirSectorId = $this->header['first_dir_sector'];
300 $binaryDir = '';
301 $seenSectorIds = [];
302 while ( $dirSectorId !== 0xFFFFFFFE ) {
303 if ( isset( $seenSectorIds[$dirSectorId] ) ) {
304 $this->error( 'FAT loop detected', self::ERROR_INVALID_FORMAT );
305 }
306 $seenSectorIds[$dirSectorId] = true;
307
308 $binaryDir .= $this->readSector( $dirSectorId );
309 $dirSectorId = $this->getNextSectorIdFromFat( $dirSectorId );
310 }
311
312 $struct = [
313 'name_raw' => 64,
314 'name_length' => 2,
315 'object_type' => 1,
316 'color' => 1,
317 'sid_left' => 4,
318 'sid_right' => 4,
319 'sid_child' => 4,
320 'clsid' => 16,
321 'state_bits' => 4,
322 'create_time_low' => 4,
323 'create_time_high' => 4,
324 'modify_time_low' => 4,
325 'modify_time_high' => 4,
326 'first_sector' => 4,
327 'size_low' => 4,
328 'size_high' => 4,
329 ];
330 $entryLength = array_sum( $struct );
331
332 for ( $pos = 0; $pos < strlen( $binaryDir ); $pos += $entryLength ) {
333 $entry = $this->unpack( $binaryDir, $pos, $struct );
334
335 // According to [MS-CFB] size_high may contain garbage due to a
336 // bug in a writer, it's best to pretend it is zero
337 $entry['size_high'] = 0;
338
339 $type = $entry['object_type'];
340 if ( $type == self::TYPE_UNALLOCATED ) {
341 continue;
342 }
343
344 $name = iconv( 'UTF-16LE', 'UTF-8', substr( $entry['name_raw'], 0, $entry['name_length'] - 2 ) );
345
346 $clsid = $this->decodeClsid( $entry['clsid'] );
347 if ( $type == self::TYPE_ROOT && isset( self::$mimesByClsid[$clsid] ) ) {
348 $this->mimeFromClsid = self::$mimesByClsid[$clsid];
349 }
350
351 if ( $name === 'Workbook' ) {
352 $this->mime = 'application/vnd.ms-excel';
353 } elseif ( $name === 'WordDocument' ) {
354 $this->mime = 'application/msword';
355 } elseif ( $name === 'PowerPoint Document' ) {
356 $this->mime = 'application/vnd.ms-powerpoint';
357 }
358 }
359 }
360}
static $fileHandle
Definition cdb.php:57
Read the directory of a Microsoft Compound File Binary file, a.k.a.
static readFile( $fileName)
Read a file by name.
unpack( $block, $offset, $struct)
readOffset( $offset, $length)
unpackOffset( $offset, $struct)
bin2dec( $str, $offset, $length)
static readHandle( $fileHandle)
Read from an open seekable handle.