195 private function unpack( $block, $offset, $struct ) {
197 foreach ( $struct as $key => $length ) {
199 $data[$key] = substr( $block, $offset, $length );
201 $data[$key] = $this->
bin2dec( $block, $offset, $length );
218 $this->
fseek( $offset );
219 Wikimedia\suppressWarnings();
220 $block = fread( $this->file, $length );
221 Wikimedia\restoreWarnings();
222 if ( $block ===
false ) {
223 $this->
error(
'error reading from file', self::ERROR_READ );
225 if ( strlen( $block ) !== $length ) {
226 $this->
error(
'unable to read the required number of bytes from the file',
227 self::ERROR_READ_PAST_END );
250 $binaryDifat = $this->header[
'difat'];
251 $nextDifatSector = $this->header[
'first_difat_sector'];
252 for ( $i = 0; $i < $this->header[
'num_difat_sectors']; $i++ ) {
253 $block = $this->
readSector( $nextDifatSector );
254 $binaryDifat .= substr( $block, 0, $this->sectorLength - 4 );
255 $nextDifatSector = $this->
bin2dec( $block, $this->sectorLength - 4, 4 );
256 if ( $nextDifatSector == 0xFFFFFFFE ) {
262 for ( $pos = 0; $pos < strlen( $binaryDifat ); $pos += 4 ) {
263 $fatSector = $this->
bin2dec( $binaryDifat, $pos, 4 );
264 if ( $fatSector < 0xFFFFFFFC ) {
265 $this->difat[] = $fatSector;
280 if ( !isset( $this->fat[$fatSectorId] ) ) {
282 if ( !isset( $this->difat[$fatSectorId] ) ) {
283 $this->
error(
'FAT sector requested beyond the end of the DIFAT', self::ERROR_INVALID_FORMAT );
285 $absoluteSectorId = $this->difat[$fatSectorId];
286 $block = $this->
readSector( $absoluteSectorId );
287 for ( $pos = 0; $pos < strlen( $block ); $pos += 4 ) {
290 $this->fat[$fatSectorId] =
$fat;
292 return $this->fat[$fatSectorId];
296 $dirSectorId = $this->header[
'first_dir_sector'];
299 while ( $dirSectorId !== 0xFFFFFFFE ) {
300 if ( isset( $seenSectorIds[$dirSectorId] ) ) {
301 $this->
error(
'FAT loop detected', self::ERROR_INVALID_FORMAT );
303 $seenSectorIds[$dirSectorId] =
true;
305 $binaryDir .= $this->
readSector( $dirSectorId );
319 'create_time_low' => 4,
320 'create_time_high' => 4,
321 'modify_time_low' => 4,
322 'modify_time_high' => 4,
327 $entryLength = array_sum( $struct );
329 for ( $pos = 0; $pos < strlen( $binaryDir ); $pos += $entryLength ) {
330 $entry = $this->
unpack( $binaryDir, $pos, $struct );
334 $entry[
'size_high'] = 0;
336 $type = $entry[
'object_type'];
337 if (
$type == self::TYPE_UNALLOCATED ) {
341 $name = iconv(
'UTF-16LE',
'UTF-8', substr( $entry[
'name_raw'], 0, $entry[
'name_length'] - 2 ) );
344 if (
$type == self::TYPE_ROOT && isset( self::$mimesByClsid[$clsid] ) ) {
345 $this->mimeFromClsid = self::$mimesByClsid[$clsid];
348 if ( $name ===
'Workbook' ) {
349 $this->mime =
'application/vnd.ms-excel';
350 } elseif ( $name ===
'WordDocument' ) {
351 $this->mime =
'application/msword';
352 } elseif ( $name ===
'PowerPoint Document' ) {
353 $this->mime =
'application/vnd.ms-powerpoint';