Commit 727d14cb43106b252fae92c4c30e4b85a4f6d7d0

Authored by m-holger
1 parent 7a23bfb6

Refactor `validatePDFVersion` and `findHeader`: simplify logic, improve readabil…

…ity, and replace deprecated API usage.
Showing 1 changed file with 26 additions and 27 deletions
libqpdf/QPDF.cc
@@ -366,21 +366,20 @@ QPDF::numWarnings() const @@ -366,21 +366,20 @@ QPDF::numWarnings() const
366 bool 366 bool
367 QPDF::validatePDFVersion(char const*& p, std::string& version) 367 QPDF::validatePDFVersion(char const*& p, std::string& version)
368 { 368 {
369 - bool valid = util::is_digit(*p);  
370 - if (valid) {  
371 - while (util::is_digit(*p)) {  
372 - version.append(1, *p++);  
373 - }  
374 - if ((*p == '.') && util::is_digit(*(p + 1))) {  
375 - version.append(1, *p++);  
376 - while (util::is_digit(*p)) {  
377 - version.append(1, *p++);  
378 - }  
379 - } else {  
380 - valid = false;  
381 - } 369 + if (!util::is_digit(*p)) {
  370 + return false;
  371 + }
  372 + while (util::is_digit(*p)) {
  373 + version.append(1, *p++);
382 } 374 }
383 - return valid; 375 + if (!(*p == '.' && util::is_digit(*(p + 1)))) {
  376 + return false;
  377 + }
  378 + version.append(1, *p++);
  379 + while (util::is_digit(*p)) {
  380 + version.append(1, *p++);
  381 + }
  382 + return true;
384 } 383 }
385 384
386 bool 385 bool
@@ -388,26 +387,26 @@ QPDF::findHeader() @@ -388,26 +387,26 @@ QPDF::findHeader()
388 { 387 {
389 qpdf_offset_t global_offset = m->file->tell(); 388 qpdf_offset_t global_offset = m->file->tell();
390 std::string line = m->file->readLine(1024); 389 std::string line = m->file->readLine(1024);
391 - char const* p = line.c_str(); 390 + char const* p = line.data();
392 if (strncmp(p, "%PDF-", 5) != 0) { 391 if (strncmp(p, "%PDF-", 5) != 0) {
393 throw std::logic_error("findHeader is not looking at %PDF-"); 392 throw std::logic_error("findHeader is not looking at %PDF-");
394 } 393 }
395 p += 5; 394 p += 5;
396 std::string version; 395 std::string version;
397 - // Note: The string returned by line.c_str() is always null-terminated. The code below never 396 + // Note: The string returned by line.data() is always null-terminated. The code below never
398 // overruns the buffer because a null character always short-circuits further advancement. 397 // overruns the buffer because a null character always short-circuits further advancement.
399 - bool valid = validatePDFVersion(p, version);  
400 - if (valid) {  
401 - m->pdf_version = version;  
402 - if (global_offset != 0) {  
403 - // Empirical evidence strongly suggests that when there is leading material prior to the  
404 - // PDF header, all explicit offsets in the file are such that 0 points to the beginning  
405 - // of the header.  
406 - QTC::TC("qpdf", "QPDF global offset");  
407 - m->file = std::shared_ptr<InputSource>(new OffsetInputSource(m->file, global_offset));  
408 - } 398 + if (!validatePDFVersion(p, version)) {
  399 + return false;
  400 + }
  401 + m->pdf_version = version;
  402 + if (global_offset != 0) {
  403 + // Empirical evidence strongly suggests (codified in PDF 2.0 spec) that when there is
  404 + // leading material prior to the PDF header, all explicit offsets in the file are such that
  405 + // 0 points to the beginning of the header.
  406 + QTC::TC("qpdf", "QPDF global offset");
  407 + m->file = std::make_shared<OffsetInputSource>(m->file, global_offset);
409 } 408 }
410 - return valid; 409 + return true;
411 } 410 }
412 411
413 void 412 void