| @@ -220,10 +220,10 @@ | ||
| 220 | 220 | |
| 221 | 221 | // Store current instance in global array, so that it can be accessed |
| 222 | 222 | // in OLE_ChainedBlockStream::stream_open(). |
| 223 | 223 | // Object is removed from self::$instances in OLE_Stream::close(). |
| 224 | - $GLOBALS['_OLE_INSTANCES'][] = $this; //* @phpstan-ignore-line | |
| 225 | - $keys = array_keys($GLOBALS['_OLE_INSTANCES']); //* @phpstan-ignore-line | |
| 224 | + $GLOBALS['_OLE_INSTANCES'][] = $this; //* @phpstan-ignore offsetAccess.nonOffsetAccessible (I don't know how to fix this) | |
| 225 | + $keys = array_keys($GLOBALS['_OLE_INSTANCES']); //* @phpstan-ignore argument.type (I don't know how to fix this) | |
| 226 | 226 | $instanceId = end($keys); |
| 227 | 227 | |
| 228 | 228 | $path = 'ole-chainedblockstream://oleInstanceId=' . $instanceId; |
| 229 | 229 | if ($blockIdOrPps instanceof OLE\PPS) { |
| @@ -228,8 +228,11 @@ | ||
| 228 | 228 | $path = 'ole-chainedblockstream://oleInstanceId=' . $instanceId; |
| 229 | 229 | if ($blockIdOrPps instanceof OLE\PPS) { |
| 230 | 230 | $path .= '&blockId=' . $blockIdOrPps->startBlock; |
| 231 | 231 | $path .= '&size=' . $blockIdOrPps->Size; |
| 232 | + if ($blockIdOrPps instanceof Root) { | |
| 233 | + $path .= '&isRoot=1'; | |
| 234 | + } | |
| 232 | 235 | } else { |
| 233 | 236 | $path .= '&blockId=' . $blockIdOrPps; |
| 234 | 237 | } |
| 235 | 238 | if (!fopen($path, 'rb')) { |
| @@ -471,8 +474,88 @@ | ||
| 471 | 474 | return $this->_list[$index]->Size; |
| 472 | 475 | } |
| 473 | 476 | |
| 474 | 477 | return 0; |
| 478 | + } | |
| 479 | + | |
| 480 | + /** | |
| 481 | + * Get the complete contents of a named file stream. | |
| 482 | + * | |
| 483 | + * @throws ReaderException if no file stream has the requested name | |
| 484 | + */ | |
| 485 | + public function getDataByName(string $name): string | |
| 486 | + { | |
| 487 | + foreach ($this->_list as $index => $pps) { | |
| 488 | + if ($pps->Type === self::OLE_PPS_TYPE_FILE && $pps->Name === $name) { | |
| 489 | + return $this->getData($index, 0, $pps->Size); | |
| 490 | + } | |
| 491 | + } | |
| 492 | + | |
| 493 | + throw new ReaderException("OLE stream '$name' was not found."); | |
| 494 | + } | |
| 495 | + | |
| 496 | + /** | |
| 497 | + * Check whether a named file stream exists without reading its contents. | |
| 498 | + */ | |
| 499 | + public function hasDataByName(string $name): bool | |
| 500 | + { | |
| 501 | + foreach ($this->_list as $pps) { | |
| 502 | + if ($pps->Type === self::OLE_PPS_TYPE_FILE && $pps->Name === $name) { | |
| 503 | + return true; | |
| 504 | + } | |
| 505 | + } | |
| 506 | + | |
| 507 | + return false; | |
| 508 | + } | |
| 509 | + | |
| 510 | + /** | |
| 511 | + * Copy a named file stream to an open output handle without loading a | |
| 512 | + * large stream into memory. | |
| 513 | + * | |
| 514 | + * @param resource $output | |
| 515 | + * | |
| 516 | + * @throws ReaderException if no file stream has the requested name | |
| 517 | + */ | |
| 518 | + public function copyDataByName(string $name, $output): void | |
| 519 | + { | |
| 520 | + foreach ($this->_list as $pps) { | |
| 521 | + if ($pps->Type !== self::OLE_PPS_TYPE_FILE || $pps->Name !== $name) { | |
| 522 | + continue; | |
| 523 | + } | |
| 524 | + if ($pps->Size < $this->bigBlockThreshold) { | |
| 525 | + $data = $this->getData($pps->No, 0, $pps->Size); | |
| 526 | + if (fwrite($output, $data) !== strlen($data)) { | |
| 527 | + throw new ReaderException("Could not write OLE stream '$name'."); | |
| 528 | + } | |
| 529 | + | |
| 530 | + return; | |
| 531 | + } | |
| 532 | + | |
| 533 | + $remaining = $pps->Size; | |
| 534 | + $blockId = $pps->startBlock; | |
| 535 | + $blocks = []; | |
| 536 | + while ($remaining > 0) { | |
| 537 | + if (!is_int($blockId) || $blockId === -2 || isset($blocks[$blockId], $this->bbat[$blockId])) { | |
| 538 | + throw new ReaderException("Invalid OLE stream '$name'."); | |
| 539 | + } | |
| 540 | + $blocks[$blockId] = true; | |
| 541 | + fseek($this->_file_handle, $this->getBlockOffset($blockId)); | |
| 542 | + $length = min($remaining, $this->bigBlockSize); | |
| 543 | + $data = fread($this->_file_handle, $length); | |
| 544 | + if ($data === false || strlen($data) !== $length || fwrite($output, $data) !== $length) { | |
| 545 | + throw new ReaderException("Could not copy OLE stream '$name'."); | |
| 546 | + } | |
| 547 | + $remaining -= $length; | |
| 548 | + $blockId = $this->bbat[$blockId] ?? null; | |
| 549 | + if (!is_int($blockId)) { | |
| 550 | + throw new ReaderException("Invalid OLE stream '$name'."); | |
| 551 | + } | |
| 552 | + } | |
| 553 | + | |
| 554 | + return; | |
| 555 | + } | |
| 556 | + | |
| 557 | + throw new ReaderException("OLE stream '$name' was not found."); | |
| 475 | 558 | } |
| 476 | 559 | |
| 477 | 560 | /** |
| 478 | 561 | * Utility function to transform ASCII text to Unicode. |