From 4a4ed3a0e7396d642fc5985b9790f734f0d788d7 Mon Sep 17 00:00:00 2001 From: Peter Viszt Date: Sun, 1 Sep 2019 23:15:37 +0200 Subject: [PATCH 1/9] Update tcpdi_parser.php Fixes offset warnings, countable warning --- tcpdi_parser.php | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/tcpdi_parser.php b/tcpdi_parser.php index 1e26664..8791aa8 100644 --- a/tcpdi_parser.php +++ b/tcpdi_parser.php @@ -48,7 +48,7 @@ */ // include class for decoding filters -require_once(dirname(__FILE__).'/include/tcpdf_filters.php'); +//require_once(dirname(__FILE__).'/include/tcpdf_filters.php'); if (!defined ('PDF_TYPE_NULL')) define ('PDF_TYPE_NULL', 0); @@ -483,7 +483,7 @@ protected function decodeXrefStream($startxref, $xref=array()) { $v = $sarr[$key]; if (($key == '/Type') AND ($v[0] == PDF_TYPE_TOKEN AND ($v[1] == 'XRef'))) { $valid_crs = true; - } elseif (($key == '/Index') AND ($v[0] == PDF_TYPE_ARRAY AND count($v[1] >= 2))) { + } elseif (($key == '/Index') AND ($v[0] == PDF_TYPE_ARRAY AND (count($v[1]) >= 2))) { // first object number in the subsection $index_first = intval($v[1][0][1]); // number of entries in the subsection @@ -709,7 +709,7 @@ protected function getRawObject($offset=0, $data=null) { $objtype = ''; // object type to be returned $objval = ''; // object value to be returned // skip initial white space chars: \x00 null (NUL), \x09 horizontal tab (HT), \x0A line feed (LF), \x0C form feed (FF), \x0D carriage return (CR), \x20 space (SP) - while (strspn($data{$offset}, "\x00\x09\x0a\x0c\x0d\x20") == 1) { + while (strspn($data{$offset}, "\x00\x09\x0a\x0c\x0d\x20")) { $offset++; } // get first char @@ -721,8 +721,8 @@ protected function getRawObject($offset=0, $data=null) { $next = strcspn($data, "\r\n", $offset); if ($next > 0) { $offset += $next; - list($obj, $unused) = $this->getRawObject($offset, $data); - return $obj; + + return $this->getRawObject($offset, $data); } break; } @@ -802,15 +802,16 @@ protected function getRawObject($offset=0, $data=null) { $offset += 2; } } else { - // hexadecimal string object - $objtype = PDF_TYPE_HEX; - ++$offset; - // The "Panose" entry in the FontDescriptor Style dict seems to have hex bytes separated by spaces. - if (($char == '<') AND (preg_match('/^([0-9A-Fa-f ]+)[>]/iU', substr($data, $offset), $matches) == 1)) { - $objval = $matches[1]; - $offset += strlen($matches[0]); - unset($matches); - } + // hexadecimal string object + $objtype = PDF_TYPE_HEX; ++$offset; + // The "Panose" entry in the FontDescriptor Style dict seems to have hex bytes separated by spaces. + if (($char == '<') AND (preg_match('/^([0-9A-Fa-f ]+)[>]/iU', substr($data, $offset), $matches) == 1)) { + $objval = $matches[1]; $offset += strlen($matches[0]); + } else if (($char == '<') AND ($endpos = strpos($this->pdfdata, '>', $offset)) !== FALSE) { + $objval = substr($data, $offset,$endpos-$offset); + $offset = $endpos + 1; + } + unset($matches); } break; } @@ -888,19 +889,24 @@ private function getDictValue($offset, &$data) { $objval = array(); // Extract dict from data. - $i=1; + $i=2; $dict = ''; $offset += 2; do { if ($data{$offset} == '>' && $data{$offset+1} == '>') { - $i--; + $i -= 2; $dict .= '>>'; $offset += 2; } else if ($data{$offset} == '<' && $data{$offset+1} == '<') { - $i++; + $i += 2; $dict .= '<<'; $offset += 2; } else { + if ($data{$offset} == '<') { + $i++; + } else if ($data{$offset} == '>') { + $i--; + } $dict .= $data{$offset}; $offset++; } From 4f06c3b8fd1b59f954b8b576c53abb326791c393 Mon Sep 17 00:00:00 2001 From: "Yuzuru Sano (sizenkainookite)" Date: Tue, 3 Dec 2019 13:25:17 +0900 Subject: [PATCH 2/9] Update tcpdi_parser.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 処理失敗時に例外を投げるよう変更 --- tcpdi_parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tcpdi_parser.php b/tcpdi_parser.php index 1e26664..bc993e7 100644 --- a/tcpdi_parser.php +++ b/tcpdi_parser.php @@ -1441,7 +1441,7 @@ private function _getPageRotation($obj) { // $obj = /Page */ public function Error($msg) { // exit program and print error - die("TCPDI_PARSER ERROR [{$this->uniqueid}]: ".$msg); + throw new Exception("TCPDI_PARSER ERROR [{$this->uniqueid}]: ".$msg); } } // END OF TCPDF_PARSER CLASS From ea880d3fa31c55bcd8766e2cb6c91123de4a342e Mon Sep 17 00:00:00 2001 From: Claudio Guarisco Date: Wed, 19 Dec 2018 11:36:14 +0100 Subject: [PATCH 3/9] Fixed parsing for new PDF files --- tcpdi_parser.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tcpdi_parser.php b/tcpdi_parser.php index bc993e7..78d3465 100644 --- a/tcpdi_parser.php +++ b/tcpdi_parser.php @@ -809,8 +809,11 @@ protected function getRawObject($offset=0, $data=null) { if (($char == '<') AND (preg_match('/^([0-9A-Fa-f ]+)[>]/iU', substr($data, $offset), $matches) == 1)) { $objval = $matches[1]; $offset += strlen($matches[0]); - unset($matches); + } else if (($char == '<') AND ($endpos = strpos($this->pdfdata, '>', $offset)) !== FALSE) { + $objval = substr($data, $offset,$endpos-$offset); + $offset = $endpos + 1; } + unset($matches); } break; } From 64a0e7619e624abc55e0d8f1abe2da78b2406877 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Sat, 29 Jan 2022 18:19:36 -0300 Subject: [PATCH 4/9] fix string sintax --- tcpdi_parser.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tcpdi_parser.php b/tcpdi_parser.php index 0500b40..45d812e 100644 --- a/tcpdi_parser.php +++ b/tcpdi_parser.php @@ -709,11 +709,11 @@ protected function getRawObject($offset=0, $data=null) { $objtype = ''; // object type to be returned $objval = ''; // object value to be returned // skip initial white space chars: \x00 null (NUL), \x09 horizontal tab (HT), \x0A line feed (LF), \x0C form feed (FF), \x0D carriage return (CR), \x20 space (SP) - while (strspn($data{$offset}, "\x00\x09\x0a\x0c\x0d\x20")) { + while (strspn($data[$offset], "\x00\x09\x0a\x0c\x0d\x20")) { $offset++; } // get first char - $char = $data{$offset}; + $char = $data[$offset]; // get object type switch ($char) { case '%': { // \x25 PERCENT SIGN @@ -743,10 +743,10 @@ protected function getRawObject($offset=0, $data=null) { if ($char == '(') { $open_bracket = 1; while ($open_bracket > 0) { - if (!isset($data{$strpos})) { + if (!isset($data[$strpos])) { break; } - $ch = $data{$strpos}; + $ch = $data[$strpos]; switch ($ch) { case '\\': { // REVERSE SOLIDUS (5Ch) (Backslash) // skip next character @@ -791,7 +791,7 @@ protected function getRawObject($offset=0, $data=null) { } case '<': // \x3C LESS-THAN SIGN case '>': { // \x3E GREATER-THAN SIGN - if (isset($data{($offset + 1)}) AND ($data{($offset + 1)} == $char)) { + if (isset($data[($offset + 1)]) AND ($data[($offset + 1)] == $char)) { // dictionary object $objtype = PDF_TYPE_DICTIONARY; if ($char == '<') { @@ -817,7 +817,7 @@ protected function getRawObject($offset=0, $data=null) { break; } default: { - $frag = $data{$offset} . @$data{$offset+1} . @$data{$offset+2} . @$data{$offset+3}; + $frag = $data[$offset] . @$data[$offset+1] . @$data[$offset+2] . @$data[$offset+3]; switch ($frag) { case 'endo': // indirect object @@ -894,21 +894,21 @@ private function getDictValue($offset, &$data) { $dict = ''; $offset += 2; do { - if ($data{$offset} == '>' && $data{$offset+1} == '>') { + if ($data[$offset] == '>' && $data[$offset+1] == '>') { $i -= 2; $dict .= '>>'; $offset += 2; - } else if ($data{$offset} == '<' && $data{$offset+1} == '<') { + } else if ($data[$offset] == '<' && $data[$offset+1] == '<') { $i += 2; $dict .= '<<'; $offset += 2; } else { - if ($data{$offset} == '<') { + if ($data[$offset] == '<') { $i++; - } else if ($data{$offset} == '>') { + } else if ($data[$offset] == '>') { $i--; } - $dict .= $data{$offset}; + $dict .= $data[$offset]; $offset++; } } while ($i>0); From 3cc9ee11360fa5fb9f44978aca23b73215969a39 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Sat, 29 Jan 2022 18:33:03 -0300 Subject: [PATCH 5/9] Add composer --- .gitignore | 1 + composer.json | 19 +++++++++++++++++++ tcpdi_parser.php => src/tcpdi_parser.php | 3 +++ 3 files changed, 23 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json rename tcpdi_parser.php => src/tcpdi_parser.php (99%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..c5be353 --- /dev/null +++ b/composer.json @@ -0,0 +1,19 @@ +{ + "name": "librecode/tcpdi_parser", + "license": "LGPL-3.0-or-later", + "autoload": { + "psr-4": { + "LibreCode\\TcpdiParser\\": "src/" + } + }, + "authors": [ + { + "name": "Vitor Mattos", + "email": "vitor@php.rio" + }, + { + "name": "Paul Nicholls", + "homepage": "https://github.com/pauln" + } + ] +} diff --git a/tcpdi_parser.php b/src/tcpdi_parser.php similarity index 99% rename from tcpdi_parser.php rename to src/tcpdi_parser.php index 45d812e..cdd136f 100644 --- a/tcpdi_parser.php +++ b/src/tcpdi_parser.php @@ -1,4 +1,7 @@ Date: Sat, 29 Jan 2022 19:19:37 -0300 Subject: [PATCH 6/9] Update with version on project https://github.com/hanneskod/libmergepdf --- src/tcpdi_parser.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/tcpdi_parser.php b/src/tcpdi_parser.php index cdd136f..754cdb2 100644 --- a/src/tcpdi_parser.php +++ b/src/tcpdi_parser.php @@ -50,9 +50,6 @@ * @version 1.1 */ -// include class for decoding filters -require_once(dirname(__FILE__).'/include/tcpdf_filters.php'); - if (!defined ('PDF_TYPE_NULL')) define ('PDF_TYPE_NULL', 0); if (!defined ('PDF_TYPE_NUMERIC')) @@ -259,10 +256,12 @@ public function getPDFVersion() { function readPages() { $params = $this->getObjectVal($this->xref['trailer'][1]['/Root']); $objref = null; - foreach ($params[1][1] as $k=>$v) { - if ($k == '/Pages') { - $objref = $v; - break; + if ($params && !empty($params[1]) && is_array($params[1][1])) { + foreach ($params[1][1] as $k=>$v) { + if ($k == '/Pages') { + $objref = $v; + break; + } } } if ($objref == null || $objref[0] !== PDF_TYPE_OBJREF) { @@ -486,7 +485,7 @@ protected function decodeXrefStream($startxref, $xref=array()) { $v = $sarr[$key]; if (($key == '/Type') AND ($v[0] == PDF_TYPE_TOKEN AND ($v[1] == 'XRef'))) { $valid_crs = true; - } elseif (($key == '/Index') AND ($v[0] == PDF_TYPE_ARRAY AND (count($v[1]) >= 2))) { + } elseif (($key == '/Index') AND ($v[0] == PDF_TYPE_ARRAY AND count($v[1]) >= 2)) { // first object number in the subsection $index_first = intval($v[1][0][1]); // number of entries in the subsection @@ -1435,7 +1434,7 @@ private function _getPageRotation($obj) { // $obj = /Page if (!isset ($obj[1][1]['/Parent'])) { return false; } else { - $res = $this->_getPageRotation($obj[1][1]['/Parent']); + $res = (array)$this->_getPageRotation($obj[1][1]['/Parent']); if ($res[0] == PDF_TYPE_OBJECT) return $res[1]; return $res; From ffc0c8ac0ca1dc4e876d3d96da9cab6a09944274 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Sun, 30 Jan 2022 00:33:15 -0300 Subject: [PATCH 7/9] Fix namespace --- src/tcpdi_parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tcpdi_parser.php b/src/tcpdi_parser.php index 754cdb2..3161be0 100644 --- a/src/tcpdi_parser.php +++ b/src/tcpdi_parser.php @@ -1,6 +1,6 @@ Date: Sun, 30 Jan 2022 15:24:19 -0300 Subject: [PATCH 8/9] use TCPDF_FILTERS --- src/tcpdi_parser.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/tcpdi_parser.php b/src/tcpdi_parser.php index 3161be0..bd72b25 100644 --- a/src/tcpdi_parser.php +++ b/src/tcpdi_parser.php @@ -2,6 +2,8 @@ namespace LibreCode\TcpdiParser; +use TCPDF_FILTERS; + //============================================================+ // File name : tcpdi_parser.php // Version : 1.1 From c958caddb089e8986a509dc538cda0c5e14f18a2 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Sun, 30 Jan 2022 15:39:05 -0300 Subject: [PATCH 9/9] Change organization --- composer.json | 4 ++-- src/tcpdi_parser.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index c5be353..78c4bad 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,9 @@ { - "name": "librecode/tcpdi_parser", + "name": "libresign/tcpdi_parser", "license": "LGPL-3.0-or-later", "autoload": { "psr-4": { - "LibreCode\\TcpdiParser\\": "src/" + "LibreSign\\TcpdiParser\\": "src/" } }, "authors": [ diff --git a/src/tcpdi_parser.php b/src/tcpdi_parser.php index bd72b25..35450da 100644 --- a/src/tcpdi_parser.php +++ b/src/tcpdi_parser.php @@ -1,6 +1,6 @@