From f92368f44f0367d9e829d69a3f388d78cd9d9b7d 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_linearization.cc | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/libqpdf/QPDF_linearization.cc b/libqpdf/QPDF_linearization.cc index 70e7f42a..531fdc84 100644 --- a/libqpdf/QPDF_linearization.cc +++ b/libqpdf/QPDF_linearization.cc @@ -18,6 +18,8 @@ #include #include +using namespace std::literals; + template static void load_vector_int( @@ -95,25 +97,17 @@ 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(); 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) && (!QUtil::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) && QUtil::is_digit(*p)) { - ++p; - } + m->file->seek(toO(pos), SEEK_SET); QPDFTokenizer::Token t1 = readToken(*m->file); if (t1.isInteger() && readToken(*m->file).isInteger() && @@ -121,6 +115,10 @@ 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) {