197 private function unpack( $block, $offset, $struct ) {
199 foreach ( $struct as $key => $length ) {
201 $data[$key] = substr( $block, $offset, $length );
203 $data[$key] = $this->
bin2dec( $block, $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 );
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 );
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 ) {
265 for ( $pos = 0; $pos < strlen( $binaryDifat ); $pos += 4 ) {
266 $fatSector = $this->
bin2dec( $binaryDifat, $pos, 4 );
267 if ( $fatSector < 0xFFFFFFFC ) {
268 $this->difat[] = $fatSector;
283 if ( !isset( $this->fat[$fatSectorId] ) ) {
285 if ( !isset( $this->difat[$fatSectorId] ) ) {
286 $this->
error(
'FAT sector requested beyond the end of the DIFAT', self::ERROR_INVALID_FORMAT );
288 $absoluteSectorId = $this->difat[$fatSectorId];
289 $block = $this->
readSector( $absoluteSectorId );
290 for ( $pos = 0; $pos < strlen( $block ); $pos += 4 ) {
293 $this->fat[$fatSectorId] =
$fat;
295 return $this->fat[$fatSectorId];
299 $dirSectorId = $this->header[
'first_dir_sector'];
302 while ( $dirSectorId !== 0xFFFFFFFE ) {
303 if ( isset( $seenSectorIds[$dirSectorId] ) ) {
304 $this->
error(
'FAT loop detected', self::ERROR_INVALID_FORMAT );
306 $seenSectorIds[$dirSectorId] =
true;
308 $binaryDir .= $this->
readSector( $dirSectorId );
322 'create_time_low' => 4,
323 'create_time_high' => 4,
324 'modify_time_low' => 4,
325 'modify_time_high' => 4,
330 $entryLength = array_sum( $struct );
332 for ( $pos = 0; $pos < strlen( $binaryDir ); $pos += $entryLength ) {
333 $entry = $this->
unpack( $binaryDir, $pos, $struct );
337 $entry[
'size_high'] = 0;
339 $type = $entry[
'object_type'];
340 if (
$type == self::TYPE_UNALLOCATED ) {
344 $name = iconv(
'UTF-16LE',
'UTF-8', substr( $entry[
'name_raw'], 0, $entry[
'name_length'] - 2 ) );
347 if (
$type == self::TYPE_ROOT && isset( self::$mimesByClsid[$clsid] ) ) {
348 $this->mimeFromClsid = self::$mimesByClsid[$clsid];
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';