Skip to content

Commit 2cfa0d9

Browse files
Cikik00ni
andauthored
Fix chr() deprecation in ASCII85 last-tuple decoding (#793)
Fix chr() deprecation in ASCII85 last-tuple decoding PR #779 fixed the chr() deprecation warning in the main decoding loop but missed the "last tuple" switch/case block (lines 233-248) which handles incomplete groups at the end of the encoded data. The same issue applies: bit-shifted values ($tuple >> 16, $tuple >> 8) can exceed 255, triggering: chr(): Providing a value not in-between 0 and 255 is deprecated Apply `& 0xFF` bitmask to extract individual bytes, consistent with the fix in the main loop. Added test to PHPUnit/Integration/ParserTest.php to load and read demo PDF Demo PDF would trigger the deprecation warnings without the fixes of this PR. --------- Co-authored-by: Konrad Abicht <abicht@mcon-consulting.de>
1 parent a606166 commit 2cfa0d9

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

samples/bugs/PullRequest793.pdf

657 Bytes
Binary file not shown.

src/Smalot/PdfParser/RawData/FilterHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ protected function decodeFilterASCII85Decode(string $data): string
209209
if (255 < $tuple >> 24) {
210210
$chr24Part = \chr(($tuple >> 24) % 256);
211211
} else {
212-
$chr24Part = \chr($tuple >> 24);
212+
$chr24Part = \chr(($tuple >> 24) & 0xFF);
213213
}
214214

215215
if (255 < $tuple) {
@@ -232,15 +232,15 @@ protected function decodeFilterASCII85Decode(string $data): string
232232
// last tuple (if any)
233233
switch ($group_pos) {
234234
case 4:
235-
$decoded .= \chr($tuple >> 24).\chr($tuple >> 16).\chr($tuple >> 8);
235+
$decoded .= \chr(($tuple >> 24) & 0xFF).\chr(($tuple >> 16) & 0xFF).\chr(($tuple >> 8) & 0xFF);
236236
break;
237237

238238
case 3:
239-
$decoded .= \chr($tuple >> 24).\chr($tuple >> 16);
239+
$decoded .= \chr(($tuple >> 24) & 0xFF).\chr(($tuple >> 16) & 0xFF);
240240
break;
241241

242242
case 2:
243-
$decoded .= \chr($tuple >> 24);
243+
$decoded .= \chr(($tuple >> 24) & 0xFF);
244244
break;
245245

246246
case 1:

tests/PHPUnit/Integration/ParserTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,18 @@ public function testIgnoreEncryption(): void
438438

439439
// without the configuration option set, an exception would be thrown.
440440
}
441+
442+
/**
443+
* Tests a fix for chr() which threw deprecations when running PHP 8.5
444+
*
445+
* @see https://github.com/smalot/pdfparser/pull/793
446+
*/
447+
public function testPullRequest793ChrDeprecationFix(): void
448+
{
449+
$document = (new Parser())->parseFile($this->rootDir.'/samples/bugs/PullRequest793.pdf');
450+
451+
$this->assertEquals('ASCII85 last-tuple overflow test', $document->getText());
452+
}
441453
}
442454

443455
class ParserSub extends Parser

0 commit comments

Comments
 (0)