Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce excessive memory allocations #582

Merged
merged 4 commits into from
Mar 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]+)/',
k00ni marked this conversation as resolved.
Show resolved Hide resolved
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) {
k00ni marked this conversation as resolved.
Show resolved Hide resolved
$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)) {
k00ni marked this conversation as resolved.
Show resolved Hide resolved
$offset += \strlen($matches[0]);
k00ni marked this conversation as resolved.
Show resolved Hide resolved
$pregResult = preg_match(
'/(endstream)[\x09\x0a\x0c\x0d\x20]/isU',
substr($pdfData, $offset),
$pdfData,
$matches,
k00ni marked this conversation as resolved.
Show resolved Hide resolved
\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;

k00ni marked this conversation as resolved.
Show resolved Hide resolved
// get xref and trailer data
$xref = $this->getXrefData($pdfData);
Expand Down