Commit e4fa5a3c2a90be455e04a8e4d5b9257a1ba92883

Authored by Jay Berkenbilt
1 parent e87d1499

Refactor qpdf processing

Push calls to processFile and processInputSource into separate
functions in preparation for password recovery changes
Showing 1 changed file with 43 additions and 20 deletions
qpdf/qpdf.cc
... ... @@ -3670,6 +3670,39 @@ ImageOptimizer::provideStreamData(int, int, Pipeline* pipeline)
3670 3670 false, false);
3671 3671 }
3672 3672  
  3673 +template <typename T>
  3674 +static PointerHolder<QPDF> do_process(
  3675 + void (QPDF::*fn)(T, char const*),
  3676 + T item, char const* password,
  3677 + Options& o, bool empty)
  3678 +{
  3679 + PointerHolder<QPDF> pdf = new QPDF;
  3680 + set_qpdf_options(*pdf, o);
  3681 + if (empty)
  3682 + {
  3683 + pdf->emptyPDF();
  3684 + }
  3685 + else
  3686 + {
  3687 + ((*pdf).*fn)(item, password);
  3688 + }
  3689 + return pdf;
  3690 +}
  3691 +
  3692 +static PointerHolder<QPDF> process_file(char const* filename,
  3693 + char const* password,
  3694 + Options& o)
  3695 +{
  3696 + return do_process(&QPDF::processFile, filename, password, o,
  3697 + strcmp(filename, "") == 0);
  3698 +}
  3699 +
  3700 +static PointerHolder<QPDF> process_input_source(
  3701 + PointerHolder<InputSource> is, char const* password, Options& o)
  3702 +{
  3703 + return do_process(&QPDF::processInputSource, is, password, o, false);
  3704 +}
  3705 +
3673 3706 static void handle_transformations(QPDF& pdf, Options& o)
3674 3707 {
3675 3708 QPDFPageDocumentHelper dh(pdf);
... ... @@ -3807,9 +3840,6 @@ static void handle_page_specs(QPDF&amp; pdf, Options&amp; o)
3807 3840 // the API, you can just create two different QPDF objects
3808 3841 // to the same underlying file with the same path to
3809 3842 // achieve the same affect.
3810   - PointerHolder<QPDF> qpdf_ph = new QPDF();
3811   - page_heap.push_back(qpdf_ph);
3812   - QPDF* qpdf = qpdf_ph.getPointer();
3813 3843 char const* password = page_spec.password;
3814 3844 if (o.encryption_file && (password == 0) &&
3815 3845 (page_spec.filename == o.encryption_file))
... ... @@ -3838,8 +3868,9 @@ static void handle_page_specs(QPDF&amp; pdf, Options&amp; o)
3838 3868 is = fis;
3839 3869 fis->setFilename(page_spec.filename.c_str());
3840 3870 }
3841   - qpdf->processInputSource(is, password);
3842   - page_spec_qpdfs[page_spec.filename] = qpdf;
  3871 + PointerHolder<QPDF> qpdf_ph = process_input_source(is, password, o);
  3872 + page_heap.push_back(qpdf_ph);
  3873 + page_spec_qpdfs[page_spec.filename] = qpdf_ph.getPointer();
3843 3874 if (cis)
3844 3875 {
3845 3876 cis->stayOpen(false);
... ... @@ -4176,10 +4207,10 @@ static void set_writer_options(QPDF&amp; pdf, Options&amp; o, QPDFWriter&amp; w)
4176 4207 }
4177 4208 if (o.copy_encryption)
4178 4209 {
4179   - QPDF encryption_pdf;
4180   - encryption_pdf.processFile(
4181   - o.encryption_file, o.encryption_file_password);
4182   - w.copyEncryptionParameters(encryption_pdf);
  4210 + PointerHolder<QPDF> encryption_pdf =
  4211 + process_file(
  4212 + o.encryption_file, o.encryption_file_password, o);
  4213 + w.copyEncryptionParameters(*encryption_pdf);
4183 4214 }
4184 4215 if (o.encrypt)
4185 4216 {
... ... @@ -4355,17 +4386,9 @@ int realmain(int argc, char* argv[])
4355 4386  
4356 4387 try
4357 4388 {
4358   - QPDF pdf;
4359   - set_qpdf_options(pdf, o);
4360   - if (strcmp(o.infilename, "") == 0)
4361   - {
4362   - pdf.emptyPDF();
4363   - }
4364   - else
4365   - {
4366   - pdf.processFile(o.infilename, o.password);
4367   - }
4368   -
  4389 + PointerHolder<QPDF> pdf_ph =
  4390 + process_file(o.infilename, o.password, o);
  4391 + QPDF& pdf = *pdf_ph;
4369 4392 handle_transformations(pdf, o);
4370 4393 if (! o.page_specs.empty())
4371 4394 {
... ...