Commit 74e76df0ba015d489589daf18e795cb57518393e

Authored by m-holger
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.
@@ -45,7 +45,6 @@ with modern equivalent. Key updates are: @@ -45,7 +45,6 @@ with modern equivalent. Key updates are:
45 Next steps are: 45 Next steps are:
46 46
47 * review function signatures in the public API 47 * review function signatures in the public API
48 -* replace code that uses QUtil::make_shared_cstr etc  
49 48
50 Except for the above, prefer to make modernization changes as part of other updates. 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,10 +158,11 @@ class QPDFJob
158 158
159 struct PageSpec 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 std::string filename; 164 std::string filename;
164 - std::shared_ptr<char> password; 165 + std::string password;
165 std::string range; 166 std::string range;
166 }; 167 };
167 168
libqpdf/QPDFJob.cc
@@ -262,13 +262,11 @@ ImageOptimizer::provideStreamData(QPDFObjGen const&amp;, Pipeline* pipeline) @@ -262,13 +262,11 @@ ImageOptimizer::provideStreamData(QPDFObjGen const&amp;, Pipeline* pipeline)
262 } 262 }
263 263
264 QPDFJob::PageSpec::PageSpec( 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 filename(filename), 266 filename(filename),
  267 + password(password.empty() ? "" : password),
267 range(range) 268 range(range)
268 { 269 {
269 - if (password) {  
270 - this->password = QUtil::make_shared_cstr(password);  
271 - }  
272 } 270 }
273 271
274 QPDFPageData::QPDFPageData(std::string const& filename, QPDF* qpdf, std::string const& range) : 272 QPDFPageData::QPDFPageData(std::string const& filename, QPDF* qpdf, std::string const& range) :
@@ -2470,11 +2468,11 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf, std::vector&lt;std::unique_ptr&lt;QPDF&gt;&gt;&amp; page_hea @@ -2470,11 +2468,11 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf, std::vector&lt;std::unique_ptr&lt;QPDF&gt;&gt;&amp; page_hea
2470 // you are using this an example of how to do this with the API, you can just create two 2468 // you are using this an example of how to do this with the API, you can just create two
2471 // different QPDF objects to the same underlying file with the same path to achieve the 2469 // different QPDF objects to the same underlying file with the same path to achieve the
2472 // same effect. 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 page_spec.filename == m->encryption_file) { 2473 page_spec.filename == m->encryption_file) {
2476 QTC::TC("qpdf", "QPDFJob pages encryption password"); 2474 QTC::TC("qpdf", "QPDFJob pages encryption password");
2477 - password = m->encryption_file_password.data(); 2475 + password = m->encryption_file_password;
2478 } 2476 }
2479 doIfVerbose([&](Pipeline& v, std::string const& prefix) { 2477 doIfVerbose([&](Pipeline& v, std::string const& prefix) {
2480 v << prefix << ": processing " << page_spec.filename << "\n"; 2478 v << prefix << ": processing " << page_spec.filename << "\n";
@@ -2492,7 +2490,7 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf, std::vector&lt;std::unique_ptr&lt;QPDF&gt;&gt;&amp; page_hea @@ -2492,7 +2490,7 @@ QPDFJob::handlePageSpecs(QPDF&amp; pdf, std::vector&lt;std::unique_ptr&lt;QPDF&gt;&gt;&amp; page_hea
2492 is = std::shared_ptr<InputSource>(fis); 2490 is = std::shared_ptr<InputSource>(fis);
2493 } 2491 }
2494 std::unique_ptr<QPDF> qpdf_sp; 2492 std::unique_ptr<QPDF> qpdf_sp;
2495 - processInputSource(qpdf_sp, is, password, true); 2493 + processInputSource(qpdf_sp, is, password.data(), true);
2496 page_spec_qpdfs[page_spec.filename] = qpdf_sp.get(); 2494 page_spec_qpdfs[page_spec.filename] = qpdf_sp.get();
2497 page_heap.push_back(std::move(qpdf_sp)); 2495 page_heap.push_back(std::move(qpdf_sp));
2498 if (cis) { 2496 if (cis) {
libqpdf/QPDFJob_config.cc
@@ -999,7 +999,7 @@ QPDFJob::PagesConfig::pageSpec( @@ -999,7 +999,7 @@ QPDFJob::PagesConfig::pageSpec(
999 QPDFJob::PagesConfig* 999 QPDFJob::PagesConfig*
1000 QPDFJob::PagesConfig::file(std::string const& arg) 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 return this; 1003 return this;
1004 } 1004 }
1005 1005
@@ -1027,11 +1027,11 @@ QPDFJob::PagesConfig::password(std::string const&amp; arg) @@ -1027,11 +1027,11 @@ QPDFJob::PagesConfig::password(std::string const&amp; arg)
1027 usage("in --pages, --password must follow a file name"); 1027 usage("in --pages, --password must follow a file name");
1028 } 1028 }
1029 auto& last = config->o.m->page_specs.back(); 1029 auto& last = config->o.m->page_specs.back();
1030 - if (last.password) { 1030 + if (!last.password.empty()) {
1031 QTC::TC("qpdf", "QPDFJob duplicated pages password"); 1031 QTC::TC("qpdf", "QPDFJob duplicated pages password");
1032 usage("--password already specified for this file"); 1032 usage("--password already specified for this file");
1033 } 1033 }
1034 - last.password = QUtil::make_shared_cstr(arg); 1034 + last.password = arg;
1035 return this; 1035 return this;
1036 } 1036 }
1037 1037