Commit 74e76df0ba015d489589daf18e795cb57518393e
1 parent
6e2a07e1
Refactor password handling in QPDFJob::PageSpec.
Replaced shared pointer usage with a plain std::string for passwords in PageSpec to simplify code and enhance readability. Updated relevant logic and function calls to align with this change, removing unnecessary shared pointer management. Updated TODO to reflect completion of this modernization step.
Showing
4 changed files
with
12 additions
and
14 deletions
TODO.md
| ... | ... | @@ -45,7 +45,6 @@ with modern equivalent. Key updates are: |
| 45 | 45 | Next steps are: |
| 46 | 46 | |
| 47 | 47 | * review function signatures in the public API |
| 48 | -* replace code that uses QUtil::make_shared_cstr etc | |
| 49 | 48 | |
| 50 | 49 | Except for the above, prefer to make modernization changes as part of other updates. |
| 51 | 50 | ... | ... |
include/qpdf/QPDFJob.hh
| ... | ... | @@ -158,10 +158,11 @@ class QPDFJob |
| 158 | 158 | |
| 159 | 159 | struct PageSpec |
| 160 | 160 | { |
| 161 | - PageSpec(std::string const& filename, char const* password, std::string const& range); | |
| 161 | + PageSpec( | |
| 162 | + std::string const& filename, std::string const& password, std::string const& range); | |
| 162 | 163 | |
| 163 | 164 | std::string filename; |
| 164 | - std::shared_ptr<char> password; | |
| 165 | + std::string password; | |
| 165 | 166 | std::string range; |
| 166 | 167 | }; |
| 167 | 168 | ... | ... |
libqpdf/QPDFJob.cc
| ... | ... | @@ -262,13 +262,11 @@ ImageOptimizer::provideStreamData(QPDFObjGen const&, Pipeline* pipeline) |
| 262 | 262 | } |
| 263 | 263 | |
| 264 | 264 | QPDFJob::PageSpec::PageSpec( |
| 265 | - std::string const& filename, char const* password, std::string const& range) : | |
| 265 | + std::string const& filename, std::string const& password, std::string const& range) : | |
| 266 | 266 | filename(filename), |
| 267 | + password(password.empty() ? "" : password), | |
| 267 | 268 | range(range) |
| 268 | 269 | { |
| 269 | - if (password) { | |
| 270 | - this->password = QUtil::make_shared_cstr(password); | |
| 271 | - } | |
| 272 | 270 | } |
| 273 | 271 | |
| 274 | 272 | QPDFPageData::QPDFPageData(std::string const& filename, QPDF* qpdf, std::string const& range) : |
| ... | ... | @@ -2470,11 +2468,11 @@ QPDFJob::handlePageSpecs(QPDF& pdf, std::vector<std::unique_ptr<QPDF>>& page_hea |
| 2470 | 2468 | // you are using this an example of how to do this with the API, you can just create two |
| 2471 | 2469 | // different QPDF objects to the same underlying file with the same path to achieve the |
| 2472 | 2470 | // same effect. |
| 2473 | - char const* password = page_spec.password.get(); | |
| 2474 | - if (!m->encryption_file.empty() && password == nullptr && | |
| 2471 | + auto password = page_spec.password; | |
| 2472 | + if (!m->encryption_file.empty() && password.empty() && | |
| 2475 | 2473 | page_spec.filename == m->encryption_file) { |
| 2476 | 2474 | QTC::TC("qpdf", "QPDFJob pages encryption password"); |
| 2477 | - password = m->encryption_file_password.data(); | |
| 2475 | + password = m->encryption_file_password; | |
| 2478 | 2476 | } |
| 2479 | 2477 | doIfVerbose([&](Pipeline& v, std::string const& prefix) { |
| 2480 | 2478 | v << prefix << ": processing " << page_spec.filename << "\n"; |
| ... | ... | @@ -2492,7 +2490,7 @@ QPDFJob::handlePageSpecs(QPDF& pdf, std::vector<std::unique_ptr<QPDF>>& page_hea |
| 2492 | 2490 | is = std::shared_ptr<InputSource>(fis); |
| 2493 | 2491 | } |
| 2494 | 2492 | std::unique_ptr<QPDF> qpdf_sp; |
| 2495 | - processInputSource(qpdf_sp, is, password, true); | |
| 2493 | + processInputSource(qpdf_sp, is, password.data(), true); | |
| 2496 | 2494 | page_spec_qpdfs[page_spec.filename] = qpdf_sp.get(); |
| 2497 | 2495 | page_heap.push_back(std::move(qpdf_sp)); |
| 2498 | 2496 | if (cis) { | ... | ... |
libqpdf/QPDFJob_config.cc
| ... | ... | @@ -999,7 +999,7 @@ QPDFJob::PagesConfig::pageSpec( |
| 999 | 999 | QPDFJob::PagesConfig* |
| 1000 | 1000 | QPDFJob::PagesConfig::file(std::string const& arg) |
| 1001 | 1001 | { |
| 1002 | - this->config->o.m->page_specs.emplace_back(arg, nullptr, ""); | |
| 1002 | + this->config->o.m->page_specs.emplace_back(arg, "", ""); | |
| 1003 | 1003 | return this; |
| 1004 | 1004 | } |
| 1005 | 1005 | |
| ... | ... | @@ -1027,11 +1027,11 @@ QPDFJob::PagesConfig::password(std::string const& arg) |
| 1027 | 1027 | usage("in --pages, --password must follow a file name"); |
| 1028 | 1028 | } |
| 1029 | 1029 | auto& last = config->o.m->page_specs.back(); |
| 1030 | - if (last.password) { | |
| 1030 | + if (!last.password.empty()) { | |
| 1031 | 1031 | QTC::TC("qpdf", "QPDFJob duplicated pages password"); |
| 1032 | 1032 | usage("--password already specified for this file"); |
| 1033 | 1033 | } |
| 1034 | - last.password = QUtil::make_shared_cstr(arg); | |
| 1034 | + last.password = arg; | |
| 1035 | 1035 | return this; |
| 1036 | 1036 | } |
| 1037 | 1037 | ... | ... |