95 $file = fopen( $fileName,
'r' );
97 return $zdr->execute();
113 return $zdr->execute();
139 private const ZIP64_EXTRA_HEADER = 0x0001;
142 private const SEGSIZE = 16384;
145 private const GENERAL_UTF8 = 11;
148 private const GENERAL_CD_ENCRYPTED = 13;
159 if ( isset( $options[
'zip64'] ) ) {
160 $this->zip64 = $options[
'zip64'];
169 private function execute() {
170 if ( !$this->file ) {
171 return StatusValue::newFatal(
'zip-file-open-error' );
174 $status = StatusValue::newGood();
176 $this->readEndOfCentralDirectoryRecord();
177 if ( $this->zip64 ) {
178 [ $offset, $size ] = $this->findZip64CentralDirectory();
179 $this->readCentralDirectory( $offset, $size );
181 if ( $this->eocdr[
'CD size'] == 0xffffffff
182 || $this->eocdr[
'CD offset'] == 0xffffffff
183 || $this->eocdr[
'CD entries total'] == 0xffff
185 $this->error(
'zip-unsupported',
'Central directory header indicates ZIP64, ' .
186 'but we are in legacy mode. Rejecting this upload is necessary to avoid ' .
187 'opening vulnerabilities on clients using OpenJDK 7 or later.' );
190 [ $offset, $size ] = $this->findOldCentralDirectory();
191 $this->readCentralDirectory( $offset, $size );
193 }
catch ( ZipDirectoryReaderError $e ) {
194 $status->fatal( $e->getErrorCode() );
197 fclose( $this->file );
209 private function error( $code, $debugMessage ) {
210 wfDebug( __CLASS__ .
": Fatal error: $debugMessage" );
211 throw new ZipDirectoryReaderError( $code );
219 private function readEndOfCentralDirectoryRecord() {
223 'CD start disk' => 2,
224 'CD entries this disk' => 2,
225 'CD entries total' => 2,
228 'file comment length' => 2,
230 $structSize = $this->getStructSize( $info );
231 $startPos = $this->getFileLength() - 65536 - $structSize;
232 if ( $startPos < 0 ) {
236 if ( $this->getFileLength() === 0 ) {
237 $this->error(
'zip-wrong-format',
"The file is empty." );
240 $block = $this->getBlock( $startPos );
241 $sigPos = strrpos( $block,
"PK\x05\x06" );
242 if ( $sigPos ===
false ) {
243 $this->error(
'zip-wrong-format',
244 "zip file lacks EOCDR signature. It probably isn't a zip file." );
247 $this->eocdr = $this->unpack( substr( $block, $sigPos ), $info );
248 $this->eocdr[
'EOCDR size'] = $structSize + $this->eocdr[
'file comment length'];
250 if ( $structSize + $this->eocdr[
'file comment length'] != strlen( $block ) - $sigPos ) {
252 $this->error(
'zip-wrong-format',
'there is a ZIP signature but it is not at ' .
253 'the end of the file. It could be an OLE file with a ZIP file embedded.' );
255 if ( $this->eocdr[
'disk'] !== 0
256 || $this->eocdr[
'CD start disk'] !== 0
258 $this->error(
'zip-unsupported',
'more than one disk (in EOCDR)' );
260 $this->eocdr += $this->unpack(
262 [
'file comment' => [
'string', $this->eocdr[
'file comment length'] ] ],
263 $sigPos + $structSize );
264 $this->eocdr[
'position'] = $startPos + $sigPos;
271 private function readZip64EndOfCentralDirectoryLocator() {
273 'signature' => [
'string', 4 ],
274 'eocdr64 start disk' => 4,
275 'eocdr64 offset' => 8,
276 'number of disks' => 4,
278 $structSize = $this->getStructSize( $info );
280 $start = $this->getFileLength() - $this->eocdr[
'EOCDR size'] - $structSize;
281 $block = $this->getBlock( $start, $structSize );
282 $this->eocdr64Locator = $data = $this->unpack( $block, $info );
284 if ( $data[
'signature'] !==
"PK\x06\x07" ) {
288 $this->error(
'zip-bad',
'wrong signature on Zip64 end of central directory locator' );
296 private function readZip64EndOfCentralDirectoryRecord() {
297 if ( $this->eocdr64Locator[
'eocdr64 start disk'] != 0
298 || $this->eocdr64Locator[
'number of disks'] != 0
300 $this->error(
'zip-unsupported',
'more than one disk (in EOCDR64 locator)' );
304 'signature' => [
'string', 4 ],
306 'version made by' => 2,
307 'version needed' => 2,
309 'CD start disk' => 4,
310 'CD entries this disk' => 8,
311 'CD entries total' => 8,
315 $structSize = $this->getStructSize( $info );
316 $block = $this->getBlock( $this->eocdr64Locator[
'eocdr64 offset'], $structSize );
317 $this->eocdr64 = $data = $this->unpack( $block, $info );
318 if ( $data[
'signature'] !==
"PK\x06\x06" ) {
319 $this->error(
'zip-bad',
'wrong signature on Zip64 end of central directory record' );
321 if ( $data[
'disk'] !== 0
322 || $data[
'CD start disk'] !== 0
324 $this->error(
'zip-unsupported',
'more than one disk (in EOCDR64)' );
334 private function findOldCentralDirectory() {
335 $size = $this->eocdr[
'CD size'];
336 $offset = $this->eocdr[
'CD offset'];
337 $endPos = $this->eocdr[
'position'];
341 if ( $offset + $size != $endPos ) {
342 $this->error(
'zip-bad',
'the central directory does not immediately precede the end ' .
343 'of central directory record' );
346 return [ $offset, $size ];
355 private function findZip64CentralDirectory() {
359 $size = $this->eocdr[
'CD size'];
360 $offset = $this->eocdr[
'CD offset'];
361 $numEntries = $this->eocdr[
'CD entries total'];
362 $endPos = $this->eocdr[
'position'];
363 if ( $size == 0xffffffff
364 || $offset == 0xffffffff
365 || $numEntries == 0xffff
367 $this->readZip64EndOfCentralDirectoryLocator();
369 if ( isset( $this->eocdr64Locator[
'eocdr64 offset'] ) ) {
370 $this->readZip64EndOfCentralDirectoryRecord();
371 if ( isset( $this->eocdr64[
'CD offset'] ) ) {
372 $size = $this->eocdr64[
'CD size'];
373 $offset = $this->eocdr64[
'CD offset'];
374 $endPos = $this->eocdr64Locator[
'eocdr64 offset'];
380 if ( $offset + $size != $endPos ) {
381 $this->error(
'zip-bad',
'the central directory does not immediately precede the end ' .
382 'of central directory record' );
385 return [ $offset, $size ];
393 private function readCentralDirectory( $offset, $size ) {
394 $block = $this->getBlock( $offset, $size );
397 'signature' => [
'string', 4 ],
398 'version made by' => 2,
399 'version needed' => 2,
401 'compression method' => 2,
405 'compressed size' => 4,
406 'uncompressed size' => 4,
408 'extra field length' => 2,
409 'comment length' => 2,
410 'disk number start' => 2,
411 'internal attrs' => 2,
412 'external attrs' => 4,
413 'local header offset' => 4,
415 $fixedSize = $this->getStructSize( $fixedInfo );
418 while ( $pos < $size ) {
419 $data = $this->unpack( $block, $fixedInfo, $pos );
422 if ( $data[
'signature'] !==
"PK\x01\x02" ) {
423 $this->error(
'zip-bad',
'Invalid signature found in directory entry' );
427 'name' => [
'string', $data[
'name length'] ],
428 'extra field' => [
'string', $data[
'extra field length'] ],
429 'comment' => [
'string', $data[
'comment length'] ],
431 $data += $this->unpack( $block, $variableInfo, $pos );
432 $pos += $this->getStructSize( $variableInfo );
434 if ( $this->zip64 && (
435 $data[
'compressed size'] == 0xffffffff
436 || $data[
'uncompressed size'] == 0xffffffff
437 || $data[
'local header offset'] == 0xffffffff )
439 $zip64Data = $this->unpackZip64Extra( $data[
'extra field'] );
441 $data = $zip64Data + $data;
445 if ( $this->testBit( $data[
'general bits'], self::GENERAL_CD_ENCRYPTED ) ) {
446 $this->error(
'zip-unsupported',
'central directory encryption is not supported' );
452 $time = $data[
'mod time'];
453 $date = $data[
'mod date'];
455 $year = 1980 + ( $date >> 9 );
456 $month = ( $date >> 5 ) & 15;
458 $hour = ( $time >> 11 ) & 31;
459 $minute = ( $time >> 5 ) & 63;
460 $second = ( $time & 31 ) * 2;
461 $timestamp = sprintf(
"%04d%02d%02d%02d%02d%02d",
462 $year, $month, $day, $hour, $minute, $second );
465 if ( $this->testBit( $data[
'general bits'], self::GENERAL_UTF8 ) ) {
466 $name = $data[
'name'];
468 $name = iconv(
'CP437',
'UTF-8', $data[
'name'] );
474 'mtime' => $timestamp,
475 'size' => $data[
'uncompressed size'],
486 private function unpackZip64Extra( $extraField ) {
491 $extraHeaderSize = $this->getStructSize( $extraHeaderInfo );
494 'uncompressed size' => 8,
495 'compressed size' => 8,
496 'local header offset' => 8,
497 'disk number start' => 4,
501 while ( $extraPos < strlen( $extraField ) ) {
502 $extra = $this->unpack( $extraField, $extraHeaderInfo, $extraPos );
503 $extraPos += $extraHeaderSize;
504 $extra += $this->unpack( $extraField,
505 [
'data' => [
'string', $extra[
'size'] ] ],
507 $extraPos += $extra[
'size'];
509 if ( $extra[
'id'] == self::ZIP64_EXTRA_HEADER ) {
510 return $this->unpack( $extra[
'data'], $zip64ExtraInfo );
521 private function getFileLength() {
522 if ( $this->fileLength ===
null ) {
523 $stat = fstat( $this->file );
524 $this->fileLength = $stat[
'size'];
540 private function getBlock( $start, $length =
null ) {
543 $this->error(
'zip-bad',
"getBlock() requested position $start, " .
544 "file length is $fileLength" );
547 $end = $start + $length;
549 $this->error(
'zip-bad',
"getBlock() requested end position $end, " .
550 "file length is $fileLength" );
552 $startSeg = (int)floor( $start / self::SEGSIZE );
553 $endSeg = (int)ceil( $end / self::SEGSIZE );
556 for ( $segIndex = $startSeg; $segIndex <= $endSeg; $segIndex++ ) {
557 $block .= $this->getSegment( $segIndex );
560 $block = substr( $block,
561 $start - $startSeg * self::SEGSIZE,
564 if ( strlen( $block ) < $length ) {
565 $this->error(
'zip-bad',
'getBlock() returned an unexpectedly small amount of data' );
584 private function getSegment( $segIndex ) {
585 if ( !isset( $this->buffer[$segIndex] ) ) {
586 $bytePos = $segIndex * self::SEGSIZE;
587 if ( $bytePos >= $this->getFileLength() ) {
588 $this->buffer[$segIndex] =
'';
592 if ( fseek( $this->file, $bytePos ) ) {
593 $this->error(
'zip-bad',
"seek to $bytePos failed" );
595 $seg = fread( $this->file, self::SEGSIZE );
596 if ( $seg ===
false ) {
597 $this->error(
'zip-bad',
"read from $bytePos failed" );
599 $this->buffer[$segIndex] = $seg;
602 return $this->buffer[$segIndex];
610 private function getStructSize( $struct ) {
612 foreach ( $struct as $type ) {
613 if ( is_array( $type ) ) {
614 [ , $fieldSize ] = $type;
644 private function unpack( $string, $struct, $offset = 0 ) {
645 $size = $this->getStructSize( $struct );
646 if ( $offset + $size > strlen( $string ) ) {
647 $this->error(
'zip-bad',
'unpack() would run past the end of the supplied string' );
652 foreach ( $struct as $key => $type ) {
653 if ( is_array( $type ) ) {
654 [ $typeName, $fieldSize ] = $type;
655 switch ( $typeName ) {
657 $data[$key] = substr( $string, $pos, $fieldSize );
661 throw new UnexpectedValueException( __METHOD__ .
": invalid type \"$typeName\"" );
665 $length = intval( $type );
670 for ( $i = $length - 1; $i >= 0; $i-- ) {
672 $value += ord( $string[$pos + $i] );
676 if ( $value > 2 ** 52 ) {
677 $this->error(
'zip-unsupported',
'number too large to be stored in a double. ' .
678 'This could happen if we tried to unpack a 64-bit structure ' .
679 'at an invalid location.' );
681 $data[$key] = $value;
697 private function testBit( $value, $bitIndex ) {
698 return (
bool)( ( $value >> $bitIndex ) & 1 );