aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2019-01-18 00:01:55 +0100
committerJay Berkenbilt <ejb@ql.org>2019-01-19 16:10:58 +0100
commite4fa5a3c2a90be455e04a8e4d5b9257a1ba92883 (patch)
treea38a958ef32214541e911cd32f66db3495888ec1
parente87d149918ed6ed211f733f932df3b62ab445c12 (diff)
downloadqpdf-e4fa5a3c2a90be455e04a8e4d5b9257a1ba92883.tar.zst
Refactor qpdf processing
Push calls to processFile and processInputSource into separate functions in preparation for password recovery changes
-rw-r--r--qpdf/qpdf.cc63
1 files 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 <typename T>
+static PointerHolder<QPDF> do_process(
+ void (QPDF::*fn)(T, char const*),
+ T item, char const* password,
+ Options& o, bool empty)
+{
+ PointerHolder<QPDF> pdf = new QPDF;
+ set_qpdf_options(*pdf, o);
+ if (empty)
+ {
+ pdf->emptyPDF();
+ }
+ else
+ {
+ ((*pdf).*fn)(item, password);
+ }
+ return pdf;
+}
+
+static PointerHolder<QPDF> process_file(char const* filename,
+ char const* password,
+ Options& o)
+{
+ return do_process(&QPDF::processFile, filename, password, o,
+ strcmp(filename, "") == 0);
+}
+
+static PointerHolder<QPDF> process_input_source(
+ PointerHolder<InputSource> 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> 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> 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<QPDF> 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<QPDF> pdf_ph =
+ process_file(o.infilename, o.password, o);
+ QPDF& pdf = *pdf_ph;
handle_transformations(pdf, o);
if (! o.page_specs.empty())
{