From 74e76df0ba015d489589daf18e795cb57518393e Mon Sep 17 00:00:00 2001 From: m-holger Date: Tue, 13 May 2025 17:40:22 +0100 Subject: [PATCH] Refactor password handling in QPDFJob::PageSpec. --- TODO.md | 1 - include/qpdf/QPDFJob.hh | 5 +++-- libqpdf/QPDFJob.cc | 14 ++++++-------- libqpdf/QPDFJob_config.cc | 6 +++--- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/TODO.md b/TODO.md index 877c45e..1c717e1 100644 --- a/TODO.md +++ b/TODO.md @@ -45,7 +45,6 @@ with modern equivalent. Key updates are: Next steps are: * review function signatures in the public API -* replace code that uses QUtil::make_shared_cstr etc Except for the above, prefer to make modernization changes as part of other updates. diff --git a/include/qpdf/QPDFJob.hh b/include/qpdf/QPDFJob.hh index 0cf2d71..3bcdd89 100644 --- a/include/qpdf/QPDFJob.hh +++ b/include/qpdf/QPDFJob.hh @@ -158,10 +158,11 @@ class QPDFJob struct PageSpec { - PageSpec(std::string const& filename, char const* password, std::string const& range); + PageSpec( + std::string const& filename, std::string const& password, std::string const& range); std::string filename; - std::shared_ptr password; + std::string password; std::string range; }; diff --git a/libqpdf/QPDFJob.cc b/libqpdf/QPDFJob.cc index 6205f3c..38d8693 100644 --- a/libqpdf/QPDFJob.cc +++ b/libqpdf/QPDFJob.cc @@ -262,13 +262,11 @@ ImageOptimizer::provideStreamData(QPDFObjGen const&, Pipeline* pipeline) } QPDFJob::PageSpec::PageSpec( - std::string const& filename, char const* password, std::string const& range) : + std::string const& filename, std::string const& password, std::string const& range) : filename(filename), + password(password.empty() ? "" : password), range(range) { - if (password) { - this->password = QUtil::make_shared_cstr(password); - } } QPDFPageData::QPDFPageData(std::string const& filename, QPDF* qpdf, std::string const& range) : @@ -2470,11 +2468,11 @@ QPDFJob::handlePageSpecs(QPDF& pdf, std::vector>& page_hea // 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. - char const* password = page_spec.password.get(); - if (!m->encryption_file.empty() && password == nullptr && + auto password = page_spec.password; + if (!m->encryption_file.empty() && password.empty() && page_spec.filename == m->encryption_file) { QTC::TC("qpdf", "QPDFJob pages encryption password"); - password = m->encryption_file_password.data(); + password = m->encryption_file_password; } doIfVerbose([&](Pipeline& v, std::string const& prefix) { v << prefix << ": processing " << page_spec.filename << "\n"; @@ -2492,7 +2490,7 @@ QPDFJob::handlePageSpecs(QPDF& pdf, std::vector>& page_hea is = std::shared_ptr(fis); } std::unique_ptr qpdf_sp; - processInputSource(qpdf_sp, is, password, true); + processInputSource(qpdf_sp, is, password.data(), true); page_spec_qpdfs[page_spec.filename] = qpdf_sp.get(); page_heap.push_back(std::move(qpdf_sp)); if (cis) { diff --git a/libqpdf/QPDFJob_config.cc b/libqpdf/QPDFJob_config.cc index 70c9f48..8e85c37 100644 --- a/libqpdf/QPDFJob_config.cc +++ b/libqpdf/QPDFJob_config.cc @@ -999,7 +999,7 @@ QPDFJob::PagesConfig::pageSpec( QPDFJob::PagesConfig* QPDFJob::PagesConfig::file(std::string const& arg) { - this->config->o.m->page_specs.emplace_back(arg, nullptr, ""); + this->config->o.m->page_specs.emplace_back(arg, "", ""); return this; } @@ -1027,11 +1027,11 @@ QPDFJob::PagesConfig::password(std::string const& arg) usage("in --pages, --password must follow a file name"); } auto& last = config->o.m->page_specs.back(); - if (last.password) { + if (!last.password.empty()) { QTC::TC("qpdf", "QPDFJob duplicated pages password"); usage("--password already specified for this file"); } - last.password = QUtil::make_shared_cstr(arg); + last.password = arg; return this; } -- libgit2 0.21.4