From 90840be59448ba92974629c34ef1b844ccf6a0aa Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Wed, 9 Aug 2017 20:17:18 -0400 Subject: [PATCH] Find lindict without PCRE --- libqpdf/QPDF_linearization.cc | 55 ++++++++++++++++++----------------- qpdf/qpdf.testcov | 2 -- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/libqpdf/QPDF_linearization.cc b/libqpdf/QPDF_linearization.cc index 86a61081..424d6d6f 100644 --- a/libqpdf/QPDF_linearization.cc +++ b/libqpdf/QPDF_linearization.cc @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -101,38 +100,42 @@ QPDF::isLinearized() memset(buf, '\0', tbuf_size); this->file->read(buf, tbuf_size - 1); - PCRE lindict_re("(?s:(\\d+)\\s+0\\s+obj\\s*<<)"); - int lindict_obj = -1; char* p = buf; while (lindict_obj == -1) { - PCRE::Match m(lindict_re.match(p)); - if (m) - { - lindict_obj = atoi(m.getMatch(1).c_str()); - if (m.getMatch(0).find('\n') != std::string::npos) - { - QTC::TC("qpdf", "QPDF lindict found newline"); - } - } - else - { - p = static_cast(memchr(p, '\0', tbuf_size - (p - buf))); - assert(p != 0); - while ((p - buf < tbuf_size) && (*p == 0)) - { - ++p; - } - if ((p - buf) == tbuf_size) - { - break; - } - QTC::TC("qpdf", "QPDF lindict searching after null"); + // Find a digit or end of buffer + while (((p - buf) < tbuf_size) && (! QUtil::is_digit(*p))) + { + ++p; + } + if (p - buf == tbuf_size) + { + break; + } + // Seek to the digit. Then skip over digits for a potential + // next iteration. + this->file->seek(p - buf, SEEK_SET); + while (((p - buf) < tbuf_size) && QUtil::is_digit(*p)) + { + ++p; + } + + QPDFTokenizer::Token t1 = readToken(this->file, true); + QPDFTokenizer::Token t2 = readToken(this->file, true); + QPDFTokenizer::Token t3 = readToken(this->file, true); + QPDFTokenizer::Token t4 = readToken(this->file, true); + if ((t1.getType() == QPDFTokenizer::tt_integer) && + (t2.getType() == QPDFTokenizer::tt_integer) && + (t3 == QPDFTokenizer::Token(QPDFTokenizer::tt_word, "obj")) && + (t4.getType() == QPDFTokenizer::tt_dict_open)) + { + lindict_obj = + static_cast(QUtil::string_to_ll(t1.getValue().c_str())); } } - if (lindict_obj == 0) + if (lindict_obj <= 0) { return false; } diff --git a/qpdf/qpdf.testcov b/qpdf/qpdf.testcov index eb5ef1a2..2860f55e 100644 --- a/qpdf/qpdf.testcov +++ b/qpdf/qpdf.testcov @@ -1,5 +1,4 @@ ignored-scope: libtests -QPDF lindict searching after null 0 QPDF err expected endobj 0 QPDF err wrong objid/generation 0 QPDF check objid 1 @@ -10,7 +9,6 @@ QPDF hint table length direct 0 QPDF P absent in lindict 0 QPDF P present in lindict 0 QPDF expected n n obj 0 -QPDF lindict found newline 0 QPDF /L mismatch 0 QPDF err /T mismatch 0 QPDF err /O mismatch 0