From 69d2101865c759f18ef34ff262eb1be0ae784056 Mon Sep 17 00:00:00 2001 From: m-holger Date: Mon, 15 Sep 2025 10:22:29 +0100 Subject: [PATCH] Refactor `QPDFJob::handlePageSpecs`: add `password` to `Input`, revise file-opening logic, and streamline password handling and verbose output. --- libqpdf/QPDFJob.cc | 23 +++++++++++++---------- libqpdf/qpdf/QPDFJob_private.hh | 1 + qpdf/qtest/qpdf/verbose-merge.out | 2 +- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/libqpdf/QPDFJob.cc b/libqpdf/QPDFJob.cc index 5caa5d2..035c85e 100644 --- a/libqpdf/QPDFJob.cc +++ b/libqpdf/QPDFJob.cc @@ -2350,7 +2350,7 @@ QPDFJob::handlePageSpecs(QPDF& pdf) // Parse all page specifications and translate them into lists of actual pages. - // Handle "." as a shortcut for the input file + // Handle "." as a shortcut for the input file. for (auto& selection: m->selections) { if (selection.filename == ".") { selection.filename = m->infilename; @@ -2358,6 +2358,9 @@ QPDFJob::handlePageSpecs(QPDF& pdf) // Force insertion (void)m->inputs.files[selection.filename]; } + if (!selection.password.empty()) { + m->inputs.files[selection.filename].password = selection.password; + } if (selection.range.empty()) { selection.range = "1-z"; } @@ -2376,9 +2379,7 @@ QPDFJob::handlePageSpecs(QPDF& pdf) } // Create a QPDF object for each file that we may take pages from. - std::map> copied_pages; - for (auto& selection: m->selections) { - auto& input = m->inputs.files[selection.filename]; + for (auto& [filename, input]: m->inputs.files) { if (!input.qpdf) { // Open the PDF file and store the QPDF object. Throw a std::shared_ptr to the qpdf into // a heap so that it survives through copying to the output but gets cleaned up @@ -2387,16 +2388,15 @@ QPDFJob::handlePageSpecs(QPDF& pdf) // you are using this an example of how to do this with the API, you can just create two // different QPDF objects to the same underlying file with the same path to achieve the // same effect. - auto password = selection.password; - if (!m->encryption_file.empty() && password.empty() && - selection.filename == m->encryption_file) { + auto password = input.password; + if (!m->encryption_file.empty() && password.empty() && filename == m->encryption_file) { password = m->encryption_file_password; } doIfVerbose([&](Pipeline& v, std::string const& prefix) { - v << prefix << ": processing " << selection.filename << "\n"; + v << prefix << ": processing " << filename << "\n"; }); if (!m->keep_files_open) { - auto cis = std::make_shared(selection.filename.data()); + auto cis = std::make_shared(filename.data()); cis->stayOpen(true); processInputSource(input.qpdf_p, cis, password.data(), true); cis->stayOpen(false); @@ -2404,7 +2404,7 @@ QPDFJob::handlePageSpecs(QPDF& pdf) } else { processInputSource( input.qpdf_p, - std::make_shared(selection.filename.data()), + std::make_shared(filename.data()), password.data(), true); } @@ -2413,7 +2413,10 @@ QPDFJob::handlePageSpecs(QPDF& pdf) input.cfis->stayOpen(false); } } + } + std::map> copied_pages; + for (auto& selection: m->selections) { // Read original pages from the PDF, and parse the page range associated with this // occurrence of the file. selection.qpdf = m->inputs.files[selection.filename].qpdf; diff --git a/libqpdf/qpdf/QPDFJob_private.hh b/libqpdf/qpdf/QPDFJob_private.hh index dda5725..272892b 100644 --- a/libqpdf/qpdf/QPDFJob_private.hh +++ b/libqpdf/qpdf/QPDFJob_private.hh @@ -40,6 +40,7 @@ struct QPDFJob::Selection // filename. This is a documented work-around. struct QPDFJob::Input { + std::string password; std::unique_ptr qpdf_p; QPDF* qpdf; ClosedFileInputSource* cfis{}; diff --git a/qpdf/qtest/qpdf/verbose-merge.out b/qpdf/qtest/qpdf/verbose-merge.out index 6f9846f..3299416 100644 --- a/qpdf/qtest/qpdf/verbose-merge.out +++ b/qpdf/qtest/qpdf/verbose-merge.out @@ -1,6 +1,6 @@ qpdf: selecting --keep-open-files=y -qpdf: processing 20-pages.pdf qpdf: processing ./20-pages.pdf +qpdf: processing 20-pages.pdf qpdf: processing minimal.pdf qpdf: ./20-pages.pdf: checking for shared resources qpdf: no shared resources found -- libgit2 0.21.4