Commit 32eca9691850fde20e8bf5d81ffbb58f37a8dacb

Authored by m-holger
1 parent 370399a9

Refactor QPDF::isLinearized

Showing 1 changed file with 28 additions and 34 deletions
libqpdf/QPDF_linearization.cc
@@ -99,9 +99,8 @@ QPDF::isLinearized() @@ -99,9 +99,8 @@ QPDF::isLinearized()
99 // The PDF spec says the linearization dictionary must be completely contained within the first 99 // The PDF spec says the linearization dictionary must be completely contained within the first
100 // 1024 bytes of the file. Add a byte for a null terminator. 100 // 1024 bytes of the file. Add a byte for a null terminator.
101 auto buffer = m->file->read(1024, 0); 101 auto buffer = m->file->read(1024, 0);
102 - int lindict_obj = -1;  
103 size_t pos = 0; 102 size_t pos = 0;
104 - while (lindict_obj == -1) { 103 + while (true) {
105 // Find a digit or end of buffer 104 // Find a digit or end of buffer
106 pos = buffer.find_first_of("0123456789"sv, pos); 105 pos = buffer.find_first_of("0123456789"sv, pos);
107 if (pos == std::string::npos) { 106 if (pos == std::string::npos) {
@@ -111,46 +110,41 @@ QPDF::isLinearized() @@ -111,46 +110,41 @@ QPDF::isLinearized()
111 // next iteration. 110 // next iteration.
112 m->file->seek(toO(pos), SEEK_SET); 111 m->file->seek(toO(pos), SEEK_SET);
113 112
114 - QPDFTokenizer::Token t1 = readToken(*m->file);  
115 - if (t1.isInteger() && readToken(*m->file).isInteger() &&  
116 - readToken(*m->file).isWord("obj")) {  
117 - lindict_obj = toI(QUtil::string_to_ll(t1.getValue().c_str())); 113 + auto t1 = readToken(*m->file, 20);
  114 + if (!(t1.isInteger() && readToken(*m->file, 6).isInteger() &&
  115 + readToken(*m->file, 4).isWord("obj"))) {
  116 + pos = buffer.find_first_not_of("0123456789"sv, pos);
  117 + if (pos == std::string::npos) {
  118 + return false;
  119 + }
  120 + continue;
118 } 121 }
119 - pos = buffer.find_first_not_of("0123456789"sv, pos);  
120 - if (pos == std::string::npos) { 122 +
  123 + auto candidate = getObject(toI(QUtil::string_to_ll(t1.getValue().data())), 0);
  124 + if (!candidate.isDictionary()) {
121 return false; 125 return false;
122 } 126 }
123 - }  
124 -  
125 - if (lindict_obj <= 0) {  
126 - return false;  
127 - }  
128 -  
129 - auto candidate = getObject(lindict_obj, 0);  
130 - if (!candidate.isDictionary()) {  
131 - return false;  
132 - }  
133 127
134 - QPDFObjectHandle linkey = candidate.getKey("/Linearized");  
135 - if (!(linkey.isNumber() && (toI(floor(linkey.getNumericValue())) == 1))) {  
136 - return false;  
137 - }  
138 -  
139 - QPDFObjectHandle L = candidate.getKey("/L");  
140 - if (L.isInteger()) {  
141 - qpdf_offset_t Li = L.getIntValue();  
142 - m->file->seek(0, SEEK_END);  
143 - if (Li != m->file->tell()) {  
144 - QTC::TC("qpdf", "QPDF /L mismatch"); 128 + auto linkey = candidate.getKey("/Linearized");
  129 + if (!(linkey.isNumber() && toI(floor(linkey.getNumericValue())) == 1)) {
145 return false; 130 return false;
146 - } else {  
147 - m->linp.file_size = Li;  
148 } 131 }
149 - }  
150 132
151 - m->lindict = candidate; 133 + QPDFObjectHandle L = candidate.getKey("/L");
  134 + if (L.isInteger()) {
  135 + qpdf_offset_t Li = L.getIntValue();
  136 + m->file->seek(0, SEEK_END);
  137 + if (Li != m->file->tell()) {
  138 + QTC::TC("qpdf", "QPDF /L mismatch");
  139 + return false;
  140 + } else {
  141 + m->linp.file_size = Li;
  142 + }
  143 + }
152 144
153 - return true; 145 + m->lindict = candidate;
  146 + return true;
  147 + }
154 } 148 }
155 149
156 void 150 void