Commit ba3953f1bf20d5d0331f253e4da1e3e3ce10e895

Authored by m-holger
1 parent 5754ab8b

Tune QPDF::readObject

Showing 1 changed file with 14 additions and 24 deletions
libqpdf/QPDF.cc
@@ -1291,32 +1291,27 @@ QPDF::readObject(std::string const& description, QPDFObjGen og) @@ -1291,32 +1291,27 @@ QPDF::readObject(std::string const& description, QPDFObjGen og)
1291 { 1291 {
1292 setLastObjectDescription(description, og); 1292 setLastObjectDescription(description, og);
1293 qpdf_offset_t offset = m->file->tell(); 1293 qpdf_offset_t offset = m->file->tell();
1294 -  
1295 bool empty = false; 1294 bool empty = false;
1296 - std::shared_ptr<StringDecrypter> decrypter_ph;  
1297 - StringDecrypter* decrypter = nullptr;  
1298 - if (m->encp->encrypted) {  
1299 - decrypter_ph = std::make_unique<StringDecrypter>(this, og);  
1300 - decrypter = decrypter_ph.get();  
1301 - }  
1302 - auto object = QPDFParser(m->file, m->last_object_description, m->tokenizer, decrypter, this) 1295 +
  1296 + StringDecrypter decrypter{this, og};
  1297 + StringDecrypter* decrypter_ptr = m->encp->encrypted ? &decrypter : nullptr;
  1298 + auto object = QPDFParser(m->file, m->last_object_description, m->tokenizer, decrypter_ptr, this)
1303 .parse(empty, false); 1299 .parse(empty, false);
1304 if (empty) { 1300 if (empty) {
1305 // Nothing in the PDF spec appears to allow empty objects, but they have been encountered in 1301 // Nothing in the PDF spec appears to allow empty objects, but they have been encountered in
1306 // actual PDF files and Adobe Reader appears to ignore them. 1302 // actual PDF files and Adobe Reader appears to ignore them.
1307 warn(damagedPDF(m->file, m->file->getLastOffset(), "empty object treated as null")); 1303 warn(damagedPDF(m->file, m->file->getLastOffset(), "empty object treated as null"));
1308 - } else if (object.isDictionary()) {  
1309 - // check for stream  
1310 - qpdf_offset_t cur_offset = m->file->tell();  
1311 - if (readToken(m->file).isWord("stream")) {  
1312 - readStream(object, og, offset);  
1313 - } else {  
1314 - m->file->seek(cur_offset, SEEK_SET);  
1315 - } 1304 + return object;
  1305 + }
  1306 + auto token = readToken(m->file);
  1307 + if (object.isDictionary() && token.isWord("stream")) {
  1308 + readStream(object, og, offset);
  1309 + token = readToken(m->file);
  1310 + }
  1311 + if (!token.isWord("endobj")) {
  1312 + QTC::TC("qpdf", "QPDF err expected endobj");
  1313 + warn(damagedPDF("expected endobj"));
1316 } 1314 }
1317 -  
1318 - // Override last_offset so that it points to the beginning of the object we just read  
1319 - m->file->setLastOffset(offset);  
1320 return object; 1315 return object;
1321 } 1316 }
1322 1317
@@ -1602,11 +1597,6 @@ QPDF::readObjectAtOffset( @@ -1602,11 +1597,6 @@ QPDF::readObjectAtOffset(
1602 1597
1603 QPDFObjectHandle oh = readObject(description, og); 1598 QPDFObjectHandle oh = readObject(description, og);
1604 1599
1605 - if (!readToken(m->file).isWord("endobj")) {  
1606 - QTC::TC("qpdf", "QPDF err expected endobj");  
1607 - warn(damagedPDF("expected endobj"));  
1608 - }  
1609 -  
1610 if (isUnresolved(og)) { 1600 if (isUnresolved(og)) {
1611 // Store the object in the cache here so it gets cached whether we first know the offset or 1601 // Store the object in the cache here so it gets cached whether we first know the offset or
1612 // whether we first know the object ID and generation (in which we case we would get here 1602 // whether we first know the object ID and generation (in which we case we would get here