From 986a253cdd504c6e2c9d05c5af77e1b4578637bd Mon Sep 17 00:00:00 2001 From: m-holger Date: Thu, 25 Jul 2024 16:42:29 +0100 Subject: [PATCH] Overload QPDFTokenizer::findEI to take a InputSource& --- include/qpdf/QPDFTokenizer.hh | 4 +++- libqpdf/QPDFTokenizer.cc | 40 ++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/include/qpdf/QPDFTokenizer.hh b/include/qpdf/QPDFTokenizer.hh index 15f7a773..ec9bbc1b 100644 --- a/include/qpdf/QPDFTokenizer.hh +++ b/include/qpdf/QPDFTokenizer.hh @@ -191,6 +191,8 @@ class QPDFTokenizer // returns a tt_inline_image token. QPDF_DLL void expectInlineImage(std::shared_ptr input); + QPDF_DLL + void expectInlineImage(InputSource& input); private: friend class QPDFParser; @@ -217,7 +219,7 @@ class QPDFTokenizer bool isSpace(char); bool isDelimiter(char); - void findEI(std::shared_ptr input); + void findEI(InputSource& input); enum state_e { st_top, diff --git a/libqpdf/QPDFTokenizer.cc b/libqpdf/QPDFTokenizer.cc index ca09708a..9b789883 100644 --- a/libqpdf/QPDFTokenizer.cc +++ b/libqpdf/QPDFTokenizer.cc @@ -27,7 +27,7 @@ namespace class QPDFWordTokenFinder: public InputSource::Finder { public: - QPDFWordTokenFinder(std::shared_ptr is, std::string const& str) : + QPDFWordTokenFinder(InputSource& is, std::string const& str) : is(is), str(str) { @@ -36,7 +36,7 @@ namespace bool check() override; private: - std::shared_ptr is; + InputSource& is; std::string str; }; } // namespace @@ -48,21 +48,21 @@ QPDFWordTokenFinder::check() // delimiter or EOF. QPDFTokenizer tokenizer; QPDFTokenizer::Token t = tokenizer.readToken(is, "finder", true); - qpdf_offset_t pos = is->tell(); + qpdf_offset_t pos = is.tell(); if (!(t == QPDFTokenizer::Token(QPDFTokenizer::tt_word, str))) { QTC::TC("qpdf", "QPDFTokenizer finder found wrong word"); return false; } - qpdf_offset_t token_start = is->getLastOffset(); + qpdf_offset_t token_start = is.getLastOffset(); char next; bool next_okay = false; - if (is->read(&next, 1) == 0) { + if (is.read(&next, 1) == 0) { QTC::TC("qpdf", "QPDFTokenizer inline image at EOF"); next_okay = true; } else { next_okay = is_delimiter(next); } - is->seek(pos, SEEK_SET); + is.seek(pos, SEEK_SET); if (!next_okay) { return false; } @@ -763,12 +763,18 @@ QPDFTokenizer::presentEOF() void QPDFTokenizer::expectInlineImage(std::shared_ptr input) +{ + expectInlineImage(*input); +} + +void +QPDFTokenizer::expectInlineImage(InputSource& input) { if (this->state == st_token_ready) { reset(); } else if (this->state != st_before_token) { - throw std::logic_error("QPDFTokenizer::expectInlineImage called" - " when tokenizer is in improper state"); + throw std::logic_error( + "QPDFTokenizer::expectInlineImage called when tokenizer is in improper state"); } findEI(input); this->before_token = false; @@ -777,14 +783,10 @@ QPDFTokenizer::expectInlineImage(std::shared_ptr input) } void -QPDFTokenizer::findEI(std::shared_ptr input) +QPDFTokenizer::findEI(InputSource& input) { - if (!input.get()) { - return; - } - - qpdf_offset_t last_offset = input->getLastOffset(); - qpdf_offset_t pos = input->tell(); + qpdf_offset_t last_offset = input.getLastOffset(); + qpdf_offset_t pos = input.tell(); // Use QPDFWordTokenFinder to find EI surrounded by delimiters. Then read the next several // tokens or up to EOF. If we find any suspicious-looking or tokens, this is probably still part @@ -797,10 +799,10 @@ QPDFTokenizer::findEI(std::shared_ptr input) bool first_try = true; while (!okay) { QPDFWordTokenFinder f(input, "EI"); - if (!input->findFirst("EI", input->tell(), 0, f)) { + if (!input.findFirst("EI", input.tell(), 0, f)) { break; } - this->inline_image_bytes = QIntC::to_size(input->tell() - pos - 2); + inline_image_bytes = QIntC::to_size(input.tell() - pos - 2); QPDFTokenizer check; bool found_bad = false; @@ -858,8 +860,8 @@ QPDFTokenizer::findEI(std::shared_ptr input) QTC::TC("qpdf", "QPDFTokenizer found EI after more than one try"); } - input->seek(pos, SEEK_SET); - input->setLastOffset(last_offset); + input.seek(pos, SEEK_SET); + input.setLastOffset(last_offset); } bool