Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| RunLength | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
6 | |
100.00% |
1 / 1 |
| decode | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
6 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Pdf\Extract\Filter; |
| 16 | |
| 17 | use Pop\Pdf\Extract\Exception; |
| 18 | |
| 19 | /** |
| 20 | * Pdf extract run length filter class |
| 21 | * |
| 22 | * @category Pop |
| 23 | * @package Pop\Pdf |
| 24 | * @author Nick Sagona, III <nick@popphp.org> |
| 25 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 26 | * @license https://www.popphp.org/license New BSD License |
| 27 | * @version 6.0.0 |
| 28 | */ |
| 29 | class RunLength implements FilterInterface |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | * Maximum decoded stream size in bytes |
| 34 | */ |
| 35 | protected const MAX_DECODED_LENGTH = 67108864; |
| 36 | |
| 37 | /** |
| 38 | * Decode a RunLengthDecode stream |
| 39 | * |
| 40 | * @param string $data |
| 41 | * @param array $params |
| 42 | * @throws Exception |
| 43 | * @return string |
| 44 | */ |
| 45 | public function decode(string $data, array $params = []): string |
| 46 | { |
| 47 | $out = ''; |
| 48 | $pos = 0; |
| 49 | $length = strlen($data); |
| 50 | |
| 51 | while ($pos < $length) { |
| 52 | $len = ord($data[$pos]); |
| 53 | $pos++; |
| 54 | |
| 55 | if ($len === 128) { |
| 56 | break; |
| 57 | } elseif ($len < 128) { |
| 58 | $out .= substr($data, $pos, $len + 1); |
| 59 | $pos += $len + 1; |
| 60 | } elseif ($pos < $length) { |
| 61 | $out .= str_repeat($data[$pos], 257 - $len); |
| 62 | $pos++; |
| 63 | } |
| 64 | |
| 65 | if (strlen($out) > self::MAX_DECODED_LENGTH) { |
| 66 | throw new Exception('Error: Decoded RunLengthDecode stream exceeds the maximum allowed size.'); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return $out; |
| 71 | } |
| 72 | |
| 73 | } |