summaryrefslogtreecommitdiffstats
path: root/vendor/setasign/fpdi/src/PdfParser/Filter
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/setasign/fpdi/src/PdfParser/Filter')
-rw-r--r--vendor/setasign/fpdi/src/PdfParser/Filter/Ascii85.php102
-rw-r--r--vendor/setasign/fpdi/src/PdfParser/Filter/Ascii85Exception.php27
-rw-r--r--vendor/setasign/fpdi/src/PdfParser/Filter/AsciiHex.php47
-rw-r--r--vendor/setasign/fpdi/src/PdfParser/Filter/FilterException.php23
-rw-r--r--vendor/setasign/fpdi/src/PdfParser/Filter/FilterInterface.php25
-rw-r--r--vendor/setasign/fpdi/src/PdfParser/Filter/Flate.php86
-rw-r--r--vendor/setasign/fpdi/src/PdfParser/Filter/FlateException.php27
-rw-r--r--vendor/setasign/fpdi/src/PdfParser/Filter/Lzw.php187
-rw-r--r--vendor/setasign/fpdi/src/PdfParser/Filter/LzwException.php22
9 files changed, 546 insertions, 0 deletions
diff --git a/vendor/setasign/fpdi/src/PdfParser/Filter/Ascii85.php b/vendor/setasign/fpdi/src/PdfParser/Filter/Ascii85.php
new file mode 100644
index 0000000..1dc936d
--- /dev/null
+++ b/vendor/setasign/fpdi/src/PdfParser/Filter/Ascii85.php
@@ -0,0 +1,102 @@
+<?php
+
+/**
+ * This file is part of FPDI
+ *
+ * @package setasign\Fpdi
+ * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
+ * @license http://opensource.org/licenses/mit-license The MIT License
+ */
+
+namespace setasign\Fpdi\PdfParser\Filter;
+
+/**
+ * Class for handling ASCII base-85 encoded data
+ */
+class Ascii85 implements FilterInterface
+{
+ /**
+ * Decode ASCII85 encoded string.
+ *
+ * @param string $data The input string
+ * @return string
+ * @throws Ascii85Exception
+ */
+ public function decode($data)
+ {
+ $out = '';
+ $state = 0;
+ $chn = null;
+
+ $data = \preg_replace('/\s/', '', $data);
+
+ $l = \strlen($data);
+
+ /** @noinspection ForeachInvariantsInspection */
+ for ($k = 0; $k < $l; ++$k) {
+ $ch = \ord($data[$k]) & 0xff;
+
+ //Start <~
+ if ($k === 0 && $ch === 60 && isset($data[$k + 1]) && (\ord($data[$k + 1]) & 0xFF) === 126) {
+ $k++;
+ continue;
+ }
+ //End ~>
+ if ($ch === 126 && isset($data[$k + 1]) && (\ord($data[$k + 1]) & 0xFF) === 62) {
+ break;
+ }
+
+ if ($ch === 122 /* z */ && $state === 0) {
+ $out .= \chr(0) . \chr(0) . \chr(0) . \chr(0);
+ continue;
+ }
+
+ if ($ch < 33 /* ! */ || $ch > 117 /* u */) {
+ throw new Ascii85Exception(
+ 'Illegal character found while ASCII85 decode.',
+ Ascii85Exception::ILLEGAL_CHAR_FOUND
+ );
+ }
+
+ $chn[$state] = $ch - 33;/* ! */
+ $state++;
+
+ if ($state === 5) {
+ $state = 0;
+ $r = 0;
+ for ($j = 0; $j < 5; ++$j) {
+ /** @noinspection UnnecessaryCastingInspection */
+ $r = (int)($r * 85 + $chn[$j]);
+ }
+
+ $out .= \chr($r >> 24)
+ . \chr($r >> 16)
+ . \chr($r >> 8)
+ . \chr($r);
+ }
+ }
+
+ if ($state === 1) {
+ throw new Ascii85Exception(
+ 'Illegal length while ASCII85 decode.',
+ Ascii85Exception::ILLEGAL_LENGTH
+ );
+ }
+
+ if ($state === 2) {
+ $r = $chn[0] * 85 * 85 * 85 * 85 + ($chn[1] + 1) * 85 * 85 * 85;
+ $out .= \chr($r >> 24);
+ } elseif ($state === 3) {
+ $r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85 + ($chn[2] + 1) * 85 * 85;
+ $out .= \chr($r >> 24);
+ $out .= \chr($r >> 16);
+ } elseif ($state === 4) {
+ $r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85 + $chn[2] * 85 * 85 + ($chn[3] + 1) * 85;
+ $out .= \chr($r >> 24);
+ $out .= \chr($r >> 16);
+ $out .= \chr($r >> 8);
+ }
+
+ return $out;
+ }
+}
diff --git a/vendor/setasign/fpdi/src/PdfParser/Filter/Ascii85Exception.php b/vendor/setasign/fpdi/src/PdfParser/Filter/Ascii85Exception.php
new file mode 100644
index 0000000..f4b6758
--- /dev/null
+++ b/vendor/setasign/fpdi/src/PdfParser/Filter/Ascii85Exception.php
@@ -0,0 +1,27 @@
+<?php
+
+/**
+ * This file is part of FPDI
+ *
+ * @package setasign\Fpdi
+ * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
+ * @license http://opensource.org/licenses/mit-license The MIT License
+ */
+
+namespace setasign\Fpdi\PdfParser\Filter;
+
+/**
+ * Exception for Ascii85 filter class
+ */
+class Ascii85Exception extends FilterException
+{
+ /**
+ * @var integer
+ */
+ const ILLEGAL_CHAR_FOUND = 0x0301;
+
+ /**
+ * @var integer
+ */
+ const ILLEGAL_LENGTH = 0x0302;
+}
diff --git a/vendor/setasign/fpdi/src/PdfParser/Filter/AsciiHex.php b/vendor/setasign/fpdi/src/PdfParser/Filter/AsciiHex.php
new file mode 100644
index 0000000..d0c3436
--- /dev/null
+++ b/vendor/setasign/fpdi/src/PdfParser/Filter/AsciiHex.php
@@ -0,0 +1,47 @@
+<?php
+
+/**
+ * This file is part of FPDI
+ *
+ * @package setasign\Fpdi
+ * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
+ * @license http://opensource.org/licenses/mit-license The MIT License
+ */
+
+namespace setasign\Fpdi\PdfParser\Filter;
+
+/**
+ * Class for handling ASCII hexadecimal encoded data
+ */
+class AsciiHex implements FilterInterface
+{
+ /**
+ * Converts an ASCII hexadecimal encoded string into its binary representation.
+ *
+ * @param string $data The input string
+ * @return string
+ */
+ public function decode($data)
+ {
+ $data = \preg_replace('/[^0-9A-Fa-f]/', '', \rtrim($data, '>'));
+ if ((\strlen($data) % 2) === 1) {
+ $data .= '0';
+ }
+
+ return \pack('H*', $data);
+ }
+
+ /**
+ * Converts a string into ASCII hexadecimal representation.
+ *
+ * @param string $data The input string
+ * @param boolean $leaveEOD
+ * @return string
+ */
+ public function encode($data, $leaveEOD = false)
+ {
+ $t = \unpack('H*', $data);
+ return \current($t)
+ . ($leaveEOD ? '' : '>');
+ }
+}
diff --git a/vendor/setasign/fpdi/src/PdfParser/Filter/FilterException.php b/vendor/setasign/fpdi/src/PdfParser/Filter/FilterException.php
new file mode 100644
index 0000000..c55a7a8
--- /dev/null
+++ b/vendor/setasign/fpdi/src/PdfParser/Filter/FilterException.php
@@ -0,0 +1,23 @@
+<?php
+
+/**
+ * This file is part of FPDI
+ *
+ * @package setasign\Fpdi
+ * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
+ * @license http://opensource.org/licenses/mit-license The MIT License
+ */
+
+namespace setasign\Fpdi\PdfParser\Filter;
+
+use setasign\Fpdi\PdfParser\PdfParserException;
+
+/**
+ * Exception for filters
+ */
+class FilterException extends PdfParserException
+{
+ const UNSUPPORTED_FILTER = 0x0201;
+
+ const NOT_IMPLEMENTED = 0x0202;
+}
diff --git a/vendor/setasign/fpdi/src/PdfParser/Filter/FilterInterface.php b/vendor/setasign/fpdi/src/PdfParser/Filter/FilterInterface.php
new file mode 100644
index 0000000..3700190
--- /dev/null
+++ b/vendor/setasign/fpdi/src/PdfParser/Filter/FilterInterface.php
@@ -0,0 +1,25 @@
+<?php
+
+/**
+ * This file is part of FPDI
+ *
+ * @package setasign\Fpdi
+ * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
+ * @license http://opensource.org/licenses/mit-license The MIT License
+ */
+
+namespace setasign\Fpdi\PdfParser\Filter;
+
+/**
+ * Interface for filters
+ */
+interface FilterInterface
+{
+ /**
+ * Decode a string.
+ *
+ * @param string $data The input string
+ * @return string
+ */
+ public function decode($data);
+}
diff --git a/vendor/setasign/fpdi/src/PdfParser/Filter/Flate.php b/vendor/setasign/fpdi/src/PdfParser/Filter/Flate.php
new file mode 100644
index 0000000..b8f79d1
--- /dev/null
+++ b/vendor/setasign/fpdi/src/PdfParser/Filter/Flate.php
@@ -0,0 +1,86 @@
+<?php
+
+/**
+ * This file is part of FPDI
+ *
+ * @package setasign\Fpdi
+ * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
+ * @license http://opensource.org/licenses/mit-license The MIT License
+ */
+
+namespace setasign\Fpdi\PdfParser\Filter;
+
+/**
+ * Class for handling zlib/deflate encoded data
+ */
+class Flate implements FilterInterface
+{
+ /**
+ * Checks whether the zlib extension is loaded.
+ *
+ * Used for testing purpose.
+ *
+ * @return boolean
+ * @internal
+ */
+ protected function extensionLoaded()
+ {
+ return \extension_loaded('zlib');
+ }
+
+ /**
+ * Decodes a flate compressed string.
+ *
+ * @param string|false $data The input string
+ * @return string
+ * @throws FlateException
+ */
+ public function decode($data)
+ {
+ if ($this->extensionLoaded()) {
+ $oData = $data;
+ $data = (($data !== '') ? @\gzuncompress($data) : '');
+ if ($data === false) {
+ // let's try if the checksum is CRC32
+ $fh = fopen('php://temp', 'w+b');
+ fwrite($fh, "\x1f\x8b\x08\x00\x00\x00\x00\x00" . $oData);
+ stream_filter_append($fh, 'zlib.inflate', STREAM_FILTER_READ, ['window' => 30]);
+ fseek($fh, 0);
+ $data = @stream_get_contents($fh);
+ fclose($fh);
+
+ if ($data) {
+ return $data;
+ }
+
+ // Try this fallback
+ $tries = 0;
+
+ $oDataLen = strlen($oData);
+ while ($tries < 6 && ($data === false || (strlen($data) < ($oDataLen - $tries - 1)))) {
+ $data = @(gzinflate(substr($oData, $tries)));
+ $tries++;
+ }
+
+ // let's use this fallback only if the $data is longer than the original data
+ if (strlen($data) > ($oDataLen - $tries - 1)) {
+ return $data;
+ }
+
+ if (!$data) {
+ throw new FlateException(
+ 'Error while decompressing stream.',
+ FlateException::DECOMPRESS_ERROR
+ );
+ }
+ }
+ } else {
+ throw new FlateException(
+ 'To handle FlateDecode filter, enable zlib support in PHP.',
+ FlateException::NO_ZLIB
+ );
+ }
+
+ return $data;
+ }
+}
diff --git a/vendor/setasign/fpdi/src/PdfParser/Filter/FlateException.php b/vendor/setasign/fpdi/src/PdfParser/Filter/FlateException.php
new file mode 100644
index 0000000..d897ac8
--- /dev/null
+++ b/vendor/setasign/fpdi/src/PdfParser/Filter/FlateException.php
@@ -0,0 +1,27 @@
+<?php
+
+/**
+ * This file is part of FPDI
+ *
+ * @package setasign\Fpdi
+ * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
+ * @license http://opensource.org/licenses/mit-license The MIT License
+ */
+
+namespace setasign\Fpdi\PdfParser\Filter;
+
+/**
+ * Exception for flate filter class
+ */
+class FlateException extends FilterException
+{
+ /**
+ * @var integer
+ */
+ const NO_ZLIB = 0x0401;
+
+ /**
+ * @var integer
+ */
+ const DECOMPRESS_ERROR = 0x0402;
+}
diff --git a/vendor/setasign/fpdi/src/PdfParser/Filter/Lzw.php b/vendor/setasign/fpdi/src/PdfParser/Filter/Lzw.php
new file mode 100644
index 0000000..bedb5b7
--- /dev/null
+++ b/vendor/setasign/fpdi/src/PdfParser/Filter/Lzw.php
@@ -0,0 +1,187 @@
+<?php
+
+/**
+ * This file is part of FPDI
+ *
+ * @package setasign\Fpdi
+ * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
+ * @license http://opensource.org/licenses/mit-license The MIT License
+ */
+
+namespace setasign\Fpdi\PdfParser\Filter;
+
+/**
+ * Class for handling LZW encoded data
+ */
+class Lzw implements FilterInterface
+{
+ /**
+ * @var null|string
+ */
+ protected $data;
+
+ /**
+ * @var array
+ */
+ protected $sTable = [];
+
+ /**
+ * @var int
+ */
+ protected $dataLength = 0;
+
+ /**
+ * @var int
+ */
+ protected $tIdx;
+
+ /**
+ * @var int
+ */
+ protected $bitsToGet = 9;
+
+ /**
+ * @var int
+ */
+ protected $bytePointer;
+
+ /**
+ * @var int
+ */
+ protected $nextData = 0;
+
+ /**
+ * @var int
+ */
+ protected $nextBits = 0;
+
+ /**
+ * @var array
+ */
+ protected $andTable = [511, 1023, 2047, 4095];
+
+ /**
+ * Method to decode LZW compressed data.
+ *
+ * @param string $data The compressed data
+ * @return string The uncompressed data
+ * @throws LzwException
+ */
+ public function decode($data)
+ {
+ if ($data[0] === "\x00" && $data[1] === "\x01") {
+ throw new LzwException(
+ 'LZW flavour not supported.',
+ LzwException::LZW_FLAVOUR_NOT_SUPPORTED
+ );
+ }
+
+ $this->initsTable();
+
+ $this->data = $data;
+ $this->dataLength = \strlen($data);
+
+ // Initialize pointers
+ $this->bytePointer = 0;
+
+ $this->nextData = 0;
+ $this->nextBits = 0;
+
+ $oldCode = 0;
+
+ $uncompData = '';
+
+ while (($code = $this->getNextCode()) !== 257) {
+ if ($code === 256) {
+ $this->initsTable();
+ $code = $this->getNextCode();
+
+ if ($code === 257) {
+ break;
+ }
+
+ $uncompData .= $this->sTable[$code];
+ $oldCode = $code;
+ } else {
+ if ($code < $this->tIdx) {
+ $string = $this->sTable[$code];
+ $uncompData .= $string;
+
+ $this->addStringToTable($this->sTable[$oldCode], $string[0]);
+ $oldCode = $code;
+ } else {
+ $string = $this->sTable[$oldCode];
+ $string .= $string[0];
+ $uncompData .= $string;
+
+ $this->addStringToTable($string);
+ $oldCode = $code;
+ }
+ }
+ }
+
+ return $uncompData;
+ }
+
+ /**
+ * Initialize the string table.
+ */
+ protected function initsTable()
+ {
+ $this->sTable = [];
+
+ for ($i = 0; $i < 256; $i++) {
+ $this->sTable[$i] = \chr($i);
+ }
+
+ $this->tIdx = 258;
+ $this->bitsToGet = 9;
+ }
+
+ /**
+ * Add a new string to the string table.
+ *
+ * @param string $oldString
+ * @param string $newString
+ */
+ protected function addStringToTable($oldString, $newString = '')
+ {
+ $string = $oldString . $newString;
+
+ // Add this new String to the table
+ $this->sTable[$this->tIdx++] = $string;
+
+ if ($this->tIdx === 511) {
+ $this->bitsToGet = 10;
+ } elseif ($this->tIdx === 1023) {
+ $this->bitsToGet = 11;
+ } elseif ($this->tIdx === 2047) {
+ $this->bitsToGet = 12;
+ }
+ }
+
+ /**
+ * Returns the next 9, 10, 11 or 12 bits.
+ *
+ * @return integer
+ */
+ protected function getNextCode()
+ {
+ if ($this->bytePointer === $this->dataLength) {
+ return 257;
+ }
+
+ $this->nextData = ($this->nextData << 8) | (\ord($this->data[$this->bytePointer++]) & 0xff);
+ $this->nextBits += 8;
+
+ if ($this->nextBits < $this->bitsToGet) {
+ $this->nextData = ($this->nextData << 8) | (\ord($this->data[$this->bytePointer++]) & 0xff);
+ $this->nextBits += 8;
+ }
+
+ $code = ($this->nextData >> ($this->nextBits - $this->bitsToGet)) & $this->andTable[$this->bitsToGet - 9];
+ $this->nextBits -= $this->bitsToGet;
+
+ return $code;
+ }
+}
diff --git a/vendor/setasign/fpdi/src/PdfParser/Filter/LzwException.php b/vendor/setasign/fpdi/src/PdfParser/Filter/LzwException.php
new file mode 100644
index 0000000..6ebad4f
--- /dev/null
+++ b/vendor/setasign/fpdi/src/PdfParser/Filter/LzwException.php
@@ -0,0 +1,22 @@
+<?php
+
+/**
+ * This file is part of FPDI
+ *
+ * @package setasign\Fpdi
+ * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
+ * @license http://opensource.org/licenses/mit-license The MIT License
+ */
+
+namespace setasign\Fpdi\PdfParser\Filter;
+
+/**
+ * Exception for LZW filter class
+ */
+class LzwException extends FilterException
+{
+ /**
+ * @var integer
+ */
+ const LZW_FLAVOUR_NOT_SUPPORTED = 0x0501;
+}