Commit d501e1c0d43eb5bf9fbbc39ac5e9ed5c5842b4ed

Authored by Jay Berkenbilt
1 parent 043c4455

Only update output version from files used as input

If we're opening a PDF file to copy its encryption information or
attachments, its version doesn't need to influence the output version.
include/qpdf/QPDFJob.hh
@@ -444,15 +444,17 @@ class QPDFJob @@ -444,15 +444,17 @@ class QPDFJob
444 444
445 // Basic file processing 445 // Basic file processing
446 std::shared_ptr<QPDF> processFile( 446 std::shared_ptr<QPDF> processFile(
447 - char const* filename, char const* password); 447 + char const* filename, char const* password,
  448 + bool used_for_input);
448 std::shared_ptr<QPDF> processInputSource( 449 std::shared_ptr<QPDF> processInputSource(
449 - PointerHolder<InputSource> is, char const* password); 450 + PointerHolder<InputSource> is, char const* password,
  451 + bool used_for_input);
450 std::shared_ptr<QPDF> doProcess( 452 std::shared_ptr<QPDF> doProcess(
451 std::function<void(QPDF*, char const*)> fn, 453 std::function<void(QPDF*, char const*)> fn,
452 - char const* password, bool empty); 454 + char const* password, bool empty, bool used_for_input);
453 std::shared_ptr<QPDF> doProcessOnce( 455 std::shared_ptr<QPDF> doProcessOnce(
454 std::function<void(QPDF*, char const*)> fn, 456 std::function<void(QPDF*, char const*)> fn,
455 - char const* password, bool empty); 457 + char const* password, bool empty, bool used_for_input);
456 458
457 // Transformations 459 // Transformations
458 void setQPDFOptions(QPDF& pdf); 460 void setQPDFOptions(QPDF& pdf);
libqpdf/QPDFJob.cc
@@ -574,7 +574,7 @@ QPDFJob::run() @@ -574,7 +574,7 @@ QPDFJob::run()
574 std::shared_ptr<QPDF> pdf_ph; 574 std::shared_ptr<QPDF> pdf_ph;
575 try 575 try
576 { 576 {
577 - pdf_ph = processFile(m->infilename.get(), m->password.get()); 577 + pdf_ph = processFile(m->infilename.get(), m->password.get(), true);
578 } 578 }
579 catch (QPDFExc& e) 579 catch (QPDFExc& e)
580 { 580 {
@@ -2035,7 +2035,7 @@ QPDFJob::doInspection(QPDF&amp; pdf) @@ -2035,7 +2035,7 @@ QPDFJob::doInspection(QPDF&amp; pdf)
2035 std::shared_ptr<QPDF> 2035 std::shared_ptr<QPDF>
2036 QPDFJob::doProcessOnce( 2036 QPDFJob::doProcessOnce(
2037 std::function<void(QPDF*, char const*)> fn, 2037 std::function<void(QPDF*, char const*)> fn,
2038 - char const* password, bool empty) 2038 + char const* password, bool empty, bool used_for_input)
2039 { 2039 {
2040 auto pdf = std::make_shared<QPDF>(); 2040 auto pdf = std::make_shared<QPDF>();
2041 setQPDFOptions(*pdf); 2041 setQPDFOptions(*pdf);
@@ -2047,14 +2047,18 @@ QPDFJob::doProcessOnce( @@ -2047,14 +2047,18 @@ QPDFJob::doProcessOnce(
2047 { 2047 {
2048 fn(pdf.get(), password); 2048 fn(pdf.get(), password);
2049 } 2049 }
2050 - this->m->max_input_version.updateIfGreater(pdf->getVersionAsPDFVersion()); 2050 + if (used_for_input)
  2051 + {
  2052 + this->m->max_input_version.updateIfGreater(
  2053 + pdf->getVersionAsPDFVersion());
  2054 + }
2051 return pdf; 2055 return pdf;
2052 } 2056 }
2053 2057
2054 std::shared_ptr<QPDF> 2058 std::shared_ptr<QPDF>
2055 QPDFJob::doProcess( 2059 QPDFJob::doProcess(
2056 std::function<void(QPDF*, char const*)> fn, 2060 std::function<void(QPDF*, char const*)> fn,
2057 - char const* password, bool empty) 2061 + char const* password, bool empty, bool used_for_input)
2058 { 2062 {
2059 // If a password has been specified but doesn't work, try other 2063 // If a password has been specified but doesn't work, try other
2060 // passwords that are equivalent in different character encodings. 2064 // passwords that are equivalent in different character encodings.
@@ -2083,7 +2087,7 @@ QPDFJob::doProcess( @@ -2083,7 +2087,7 @@ QPDFJob::doProcess(
2083 { 2087 {
2084 // There is no password, or we're not doing recovery, so just 2088 // There is no password, or we're not doing recovery, so just
2085 // do the normal processing with the supplied password. 2089 // do the normal processing with the supplied password.
2086 - return doProcessOnce(fn, password, empty); 2090 + return doProcessOnce(fn, password, empty, used_for_input);
2087 } 2091 }
2088 2092
2089 // Get a list of otherwise encoded strings. Keep in scope for this 2093 // Get a list of otherwise encoded strings. Keep in scope for this
@@ -2117,7 +2121,7 @@ QPDFJob::doProcess( @@ -2117,7 +2121,7 @@ QPDFJob::doProcess(
2117 { 2121 {
2118 try 2122 try
2119 { 2123 {
2120 - return doProcessOnce(fn, *iter, empty); 2124 + return doProcessOnce(fn, *iter, empty, used_for_input);
2121 } 2125 }
2122 catch (QPDFExc& e) 2126 catch (QPDFExc& e)
2123 { 2127 {
@@ -2144,22 +2148,23 @@ QPDFJob::doProcess( @@ -2144,22 +2148,23 @@ QPDFJob::doProcess(
2144 } 2148 }
2145 2149
2146 std::shared_ptr<QPDF> 2150 std::shared_ptr<QPDF>
2147 -QPDFJob::processFile(char const* filename, char const* password) 2151 +QPDFJob::processFile(char const* filename, char const* password,
  2152 + bool used_for_input)
2148 { 2153 {
2149 auto f1 = std::mem_fn<void(char const*, char const*)>(&QPDF::processFile); 2154 auto f1 = std::mem_fn<void(char const*, char const*)>(&QPDF::processFile);
2150 auto fn = std::bind( 2155 auto fn = std::bind(
2151 f1, std::placeholders::_1, filename, std::placeholders::_2); 2156 f1, std::placeholders::_1, filename, std::placeholders::_2);
2152 - return doProcess(fn, password, strcmp(filename, "") == 0); 2157 + return doProcess(fn, password, strcmp(filename, "") == 0, used_for_input);
2153 } 2158 }
2154 2159
2155 std::shared_ptr<QPDF> 2160 std::shared_ptr<QPDF>
2156 QPDFJob::processInputSource( 2161 QPDFJob::processInputSource(
2157 - PointerHolder<InputSource> is, char const* password) 2162 + PointerHolder<InputSource> is, char const* password, bool used_for_input)
2158 { 2163 {
2159 auto f1 = std::mem_fn(&QPDF::processInputSource); 2164 auto f1 = std::mem_fn(&QPDF::processInputSource);
2160 auto fn = std::bind( 2165 auto fn = std::bind(
2161 f1, std::placeholders::_1, is, std::placeholders::_2); 2166 f1, std::placeholders::_1, is, std::placeholders::_2);
2162 - return doProcess(fn, password, false); 2167 + return doProcess(fn, password, false, used_for_input);
2163 } 2168 }
2164 2169
2165 void 2170 void
@@ -2171,7 +2176,7 @@ QPDFJob::validateUnderOverlay(QPDF&amp; pdf, UnderOverlay* uo) @@ -2171,7 +2176,7 @@ QPDFJob::validateUnderOverlay(QPDF&amp; pdf, UnderOverlay* uo)
2171 } 2176 }
2172 QPDFPageDocumentHelper main_pdh(pdf); 2177 QPDFPageDocumentHelper main_pdh(pdf);
2173 int main_npages = QIntC::to_int(main_pdh.getAllPages().size()); 2178 int main_npages = QIntC::to_int(main_pdh.getAllPages().size());
2174 - uo->pdf = processFile(uo->filename.c_str(), uo->password.get()); 2179 + uo->pdf = processFile(uo->filename.c_str(), uo->password.get(), true);
2175 QPDFPageDocumentHelper uo_pdh(*(uo->pdf)); 2180 QPDFPageDocumentHelper uo_pdh(*(uo->pdf));
2176 int uo_npages = QIntC::to_int(uo_pdh.getAllPages().size()); 2181 int uo_npages = QIntC::to_int(uo_pdh.getAllPages().size());
2177 try 2182 try
@@ -2455,7 +2460,7 @@ QPDFJob::copyAttachments(QPDF&amp; pdf) @@ -2455,7 +2460,7 @@ QPDFJob::copyAttachments(QPDF&amp; pdf)
2455 << to_copy.path << std::endl; 2460 << to_copy.path << std::endl;
2456 }); 2461 });
2457 auto other = processFile( 2462 auto other = processFile(
2458 - to_copy.path.c_str(), to_copy.password.c_str()); 2463 + to_copy.path.c_str(), to_copy.password.c_str(), false);
2459 QPDFEmbeddedFileDocumentHelper other_efdh(*other); 2464 QPDFEmbeddedFileDocumentHelper other_efdh(*other);
2460 auto other_attachments = other_efdh.getEmbeddedFiles(); 2465 auto other_attachments = other_efdh.getEmbeddedFiles();
2461 for (auto const& iter: other_attachments) 2466 for (auto const& iter: other_attachments)
@@ -2865,7 +2870,8 @@ QPDFJob::handlePageSpecs( @@ -2865,7 +2870,8 @@ QPDFJob::handlePageSpecs(
2865 is = PointerHolder<InputSource>(fis); 2870 is = PointerHolder<InputSource>(fis);
2866 fis->setFilename(page_spec.filename.c_str()); 2871 fis->setFilename(page_spec.filename.c_str());
2867 } 2872 }
2868 - std::shared_ptr<QPDF> qpdf_ph = processInputSource(is, password); 2873 + std::shared_ptr<QPDF> qpdf_ph = processInputSource(
  2874 + is, password, true);
2869 page_heap.push_back(qpdf_ph); 2875 page_heap.push_back(qpdf_ph);
2870 page_spec_qpdfs[page_spec.filename] = qpdf_ph.get(); 2876 page_spec_qpdfs[page_spec.filename] = qpdf_ph.get();
2871 if (cis) 2877 if (cis)
@@ -3454,7 +3460,7 @@ QPDFJob::setWriterOptions(QPDF&amp; pdf, QPDFWriter&amp; w) @@ -3454,7 +3460,7 @@ QPDFJob::setWriterOptions(QPDF&amp; pdf, QPDFWriter&amp; w)
3454 { 3460 {
3455 std::shared_ptr<QPDF> encryption_pdf = 3461 std::shared_ptr<QPDF> encryption_pdf =
3456 processFile(m->encryption_file.c_str(), 3462 processFile(m->encryption_file.c_str(),
3457 - m->encryption_file_password.get()); 3463 + m->encryption_file_password.get(), false);
3458 w.copyEncryptionParameters(*encryption_pdf); 3464 w.copyEncryptionParameters(*encryption_pdf);
3459 } 3465 }
3460 if (m->encrypt) 3466 if (m->encrypt)
qpdf/qtest/qpdf/job-json-copy-attachments.pdf
No preview for this file type