From 27791cc891c51b09386eae3e44db784dc4b72a3b Mon Sep 17 00:00:00 2001 From: m-holger Date: Sat, 2 Nov 2024 14:18:41 +0000 Subject: [PATCH] Refactor QPDF::isLinearized --- libqpdf/QPDF.cc | 3 ++- libqpdf/QPDF_linearization.cc | 29 +++++++++++++---------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/libqpdf/QPDF.cc b/libqpdf/QPDF.cc index ce42916..e56fc22 100644 --- a/libqpdf/QPDF.cc +++ b/libqpdf/QPDF.cc @@ -2764,7 +2764,8 @@ QPDF::pipeStreamData( try { auto buf = file->read(length, offset); if (buf.size() != length) { - throw damagedPDF(*file, "", offset + toO(buf.size()), "unexpected EOF reading stream data"); + throw damagedPDF( + *file, "", offset + toO(buf.size()), "unexpected EOF reading stream data"); } pipeline->write(buf.data(), length); attempted_finish = true; diff --git a/libqpdf/QPDF_linearization.cc b/libqpdf/QPDF_linearization.cc index b79959e..e02b19e 100644 --- a/libqpdf/QPDF_linearization.cc +++ b/libqpdf/QPDF_linearization.cc @@ -20,6 +20,7 @@ #include using namespace qpdf; +using namespace std::literals; template static void @@ -97,26 +98,18 @@ QPDF::isLinearized() // The PDF spec says the linearization dictionary must be completely contained within the first // 1024 bytes of the file. Add a byte for a null terminator. - auto buffer = m->file->read(1024, 0); - - auto buf = buffer.data(); - auto tbuf_size = buffer.size(); + auto buffer = m->file->read(1024, 0); int lindict_obj = -1; - char* p = buf; + size_t pos = 0; while (lindict_obj == -1) { // Find a digit or end of buffer - while (((p - buf) < tbuf_size) && (!util::is_digit(*p))) { - ++p; - } - if (p - buf == tbuf_size) { - break; + pos = buffer.find_first_of("0123456789"sv, pos); + if (pos == std::string::npos) { + return false; } // Seek to the digit. Then skip over digits for a potential // next iteration. - m->file->seek(p - buf, SEEK_SET); - while (((p - buf) < tbuf_size) && util::is_digit(*p)) { - ++p; - } + m->file->seek(toO(pos), SEEK_SET); QPDFTokenizer::Token t1 = readToken(*m->file); if (t1.isInteger() && readToken(*m->file).isInteger() && @@ -124,13 +117,17 @@ QPDF::isLinearized() readToken(*m->file).getType() == QPDFTokenizer::tt_dict_open) { lindict_obj = toI(QUtil::string_to_ll(t1.getValue().c_str())); } + pos = buffer.find_first_not_of("0123456789"sv, pos); + if (pos == std::string::npos) { + return false; + } } if (lindict_obj <= 0) { return false; } - auto candidate = getObjectByID(lindict_obj, 0); + auto candidate = getObject(lindict_obj, 0); if (!candidate.isDictionary()) { return false; } @@ -545,7 +542,7 @@ QPDF::maxEnd(ObjUser const& ou) } qpdf_offset_t end = 0; for (auto const& og: m->obj_user_to_objects[ou]) { - if (m->obj_cache.count(og) == 0) { + if (!m->obj_cache.count(og)) { stopOnError("unknown object referenced in object user table"); } end = std::max(end, m->obj_cache[og].end_after_space); -- libgit2 0.21.4