Skip to content

Commit

Permalink
Reduce excessive memory allocations (#582)
Browse files Browse the repository at this point in the history
* reduce excessive memory allocations

* yoda-style comparison

* minor: test nullness explicitly

Co-authored-by: Konrad Abicht <[email protected]>

---------

Co-authored-by: Konrad Abicht <[email protected]>
  • Loading branch information
se-ti and k00ni authored Mar 14, 2023
1 parent 3ef8bc5 commit 43a1c14
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions src/Smalot/PdfParser/RawData/RawDataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,14 +635,10 @@ protected function getRawObject(string $pdfData, int $offset = 0): array
// name object
$objtype = $char;
++$offset;
$pregResult = preg_match(
'/^([^\x00\x09\x0a\x0c\x0d\x20\s\x28\x29\x3c\x3e\x5b\x5d\x7b\x7d\x2f\x25]+)/',
substr($pdfData, $offset, 256),
$matches
);
if (1 == $pregResult) {
$objval = $matches[1]; // unescaped value
$offset += \strlen($objval);
$span = strcspn($pdfData, "\x00\x09\x0a\x0c\x0d\x20\n\t\r\v\f\x28\x29\x3c\x3e\x5b\x5d\x7b\x7d\x2f\x25", $offset, 256);
if ($span > 0) {
$objval = substr($pdfData, $offset, $span); // unescaped value
$offset += $span;
}
break;

Expand Down Expand Up @@ -723,15 +719,13 @@ protected function getRawObject(string $pdfData, int $offset = 0): array
// hexadecimal string object
$objtype = $char;
++$offset;
$pregResult = preg_match(
'/^([0-9A-Fa-f\x09\x0a\x0c\x0d\x20]+)>/iU',
substr($pdfData, $offset),
$matches
);
if (('<' == $char) && 1 == $pregResult) {

$span = strspn($pdfData, "0123456789abcdefABCDEF\x09\x0a\x0c\x0d\x20", $offset);
$dataToCheck = $pdfData[$offset + $span] ?? null;
if ('<' == $char && $span > 0 && '>' == $dataToCheck) {
// remove white space characters
$objval = strtr($matches[1], $this->config->getPdfWhitespaces(), '');
$offset += \strlen($matches[0]);
$objval = strtr(substr($pdfData, $offset, $span), $this->config->getPdfWhitespaces(), '');
$offset += $span + 1;
} elseif (false !== ($endpos = strpos($pdfData, '>', $offset))) {
$offset = $endpos + 1;
}
Expand Down Expand Up @@ -762,17 +756,18 @@ protected function getRawObject(string $pdfData, int $offset = 0): array
// start stream object
$objtype = 'stream';
$offset += 6;
if (1 == preg_match('/^([\r]?[\n])/isU', substr($pdfData, $offset), $matches)) {
if (1 == preg_match('/^([\r]?[\n])/isU', substr($pdfData, $offset, 4), $matches)) {
$offset += \strlen($matches[0]);
$pregResult = preg_match(
'/(endstream)[\x09\x0a\x0c\x0d\x20]/isU',
substr($pdfData, $offset),
$pdfData,
$matches,
\PREG_OFFSET_CAPTURE
\PREG_OFFSET_CAPTURE,
$offset
);
if (1 == $pregResult) {
$objval = substr($pdfData, $offset, $matches[0][1]);
$offset += $matches[1][1];
$objval = substr($pdfData, $offset, $matches[0][1] - $offset);
$offset = $matches[1][1];
}
}
} elseif ('endstream' == substr($pdfData, $offset, 9)) {
Expand Down Expand Up @@ -888,7 +883,7 @@ public function parseData(string $data): array
}

// get PDF content string
$pdfData = substr($data, $trimpos);
$pdfData = $trimpos > 0 ? substr($data, $trimpos) : $data;

// get xref and trailer data
$xref = $this->getXrefData($pdfData);
Expand Down

0 comments on commit 43a1c14

Please sign in to comment.