From e4fa5a3c2a90be455e04a8e4d5b9257a1ba92883 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 17 Jan 2019 18:01:55 -0500 Subject: Refactor qpdf processing Push calls to processFile and processInputSource into separate functions in preparation for password recovery changes --- qpdf/qpdf.cc | 63 +++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/qpdf/qpdf.cc b/qpdf/qpdf.cc index c0e52a1c..9ee2b423 100644 --- a/qpdf/qpdf.cc +++ b/qpdf/qpdf.cc @@ -3670,6 +3670,39 @@ ImageOptimizer::provideStreamData(int, int, Pipeline* pipeline) false, false); } +template +static PointerHolder do_process( + void (QPDF::*fn)(T, char const*), + T item, char const* password, + Options& o, bool empty) +{ + PointerHolder pdf = new QPDF; + set_qpdf_options(*pdf, o); + if (empty) + { + pdf->emptyPDF(); + } + else + { + ((*pdf).*fn)(item, password); + } + return pdf; +} + +static PointerHolder process_file(char const* filename, + char const* password, + Options& o) +{ + return do_process(&QPDF::processFile, filename, password, o, + strcmp(filename, "") == 0); +} + +static PointerHolder process_input_source( + PointerHolder is, char const* password, Options& o) +{ + return do_process(&QPDF::processInputSource, is, password, o, false); +} + static void handle_transformations(QPDF& pdf, Options& o) { QPDFPageDocumentHelper dh(pdf); @@ -3807,9 +3840,6 @@ static void handle_page_specs(QPDF& pdf, Options& o) // the API, you can just create two different QPDF objects // to the same underlying file with the same path to // achieve the same affect. - PointerHolder qpdf_ph = new QPDF(); - page_heap.push_back(qpdf_ph); - QPDF* qpdf = qpdf_ph.getPointer(); char const* password = page_spec.password; if (o.encryption_file && (password == 0) && (page_spec.filename == o.encryption_file)) @@ -3838,8 +3868,9 @@ static void handle_page_specs(QPDF& pdf, Options& o) is = fis; fis->setFilename(page_spec.filename.c_str()); } - qpdf->processInputSource(is, password); - page_spec_qpdfs[page_spec.filename] = qpdf; + PointerHolder qpdf_ph = process_input_source(is, password, o); + page_heap.push_back(qpdf_ph); + page_spec_qpdfs[page_spec.filename] = qpdf_ph.getPointer(); if (cis) { cis->stayOpen(false); @@ -4176,10 +4207,10 @@ static void set_writer_options(QPDF& pdf, Options& o, QPDFWriter& w) } if (o.copy_encryption) { - QPDF encryption_pdf; - encryption_pdf.processFile( - o.encryption_file, o.encryption_file_password); - w.copyEncryptionParameters(encryption_pdf); + PointerHolder encryption_pdf = + process_file( + o.encryption_file, o.encryption_file_password, o); + w.copyEncryptionParameters(*encryption_pdf); } if (o.encrypt) { @@ -4355,17 +4386,9 @@ int realmain(int argc, char* argv[]) try { - QPDF pdf; - set_qpdf_options(pdf, o); - if (strcmp(o.infilename, "") == 0) - { - pdf.emptyPDF(); - } - else - { - pdf.processFile(o.infilename, o.password); - } - + PointerHolder pdf_ph = + process_file(o.infilename, o.password, o); + QPDF& pdf = *pdf_ph; handle_transformations(pdf, o); if (! o.page_specs.empty()) { -- cgit v1.2.3-70-g09d2